1/* 2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16#include "base_test.h" 17 18#include <cstdio> 19#include <fcntl.h> 20#include "rdb_utils.h" 21#include <sys/stat.h> 22#include <sys/types.h> 23 24#include "contacts_path.h" 25#include "test_common.h" 26 27namespace Contacts { 28namespace Test { 29BaseTest::BaseTest() 30{ 31 OHOS::Contacts::ContactsPath::RDB_PATH = DataPath::RDB_PATH; 32 OHOS::Contacts::ContactsPath::RDB_BACKUP_PATH = DataPath::RDB_BACKUP_PATH; 33} 34 35BaseTest::~BaseTest() 36{ 37} 38 39int BaseTest::ContactsRand() 40{ 41 int fd = 0; 42 int randNum = 0; 43 int result; 44 fd = open("/dev/urandom", O_RDONLY); 45 read(fd, &result, sizeof(randNum)); 46 close(fd); 47 return result; 48} 49 50void BaseTest::InitAbility() 51{ 52 std::shared_ptr<OHOS::AppExecFwk::AbilityInfo> abilityInfo = std::make_shared<OHOS::AppExecFwk::AbilityInfo>(); 53 abilityInfo->name = "AbilityClassName"; 54 abilityInfo->type = OHOS::AppExecFwk::AbilityType::DATA; 55 abilityInfo->isNativeAbility = true; 56 std::shared_ptr<OHOS::AppExecFwk::AbilityLocalRecord> abilityLocalRecord = 57 std::make_shared<OHOS::AppExecFwk::AbilityLocalRecord>(abilityInfo, nullptr); 58 std::shared_ptr<OHOS::AppExecFwk::OHOSApplication> application; 59 std::shared_ptr<OHOS::AppExecFwk::AbilityHandler> handler; 60 OHOS::sptr<IRemoteObject> remoteObject; 61 calllogAbility.Init(abilityLocalRecord, application, handler, remoteObject); 62 voicemailAbility.Init(abilityLocalRecord, application, handler, remoteObject); 63 contactsDataAbility.Init(abilityLocalRecord, application, handler, remoteObject); 64} 65 66/** 67 * @brief check values and resultSet value 68 * 69 * @param rawContactValues of data source 70 * @param resultSet of database 71 * @param test name 72 */ 73void BaseTest::CheckResultSet(OHOS::DataShare::DataShareValuesBucket &values, 74 const std::shared_ptr<OHOS::DataShare::DataShareResultSet> &resultSet, std::string testName) 75{ 76 std::vector<std::string> columnNames; 77 resultSet->GetAllColumnNames(columnNames); 78 if (resultSet->GoToFirstRow() == OHOS::NativeRdb::E_OK) { 79 int size = columnNames.size(); 80 for (int i = 0; i < size; i++) { 81 CheckData(values, resultSet, columnNames[i], testName); 82 } 83 } 84 resultSet->Close(); 85} 86 87void BaseTest::CheckData(OHOS::DataShare::DataShareValuesBucket &values, 88 const std::shared_ptr<OHOS::DataShare::DataShareResultSet> &resultSet, std::string &columnName, 89 std::string testName) 90{ 91 std::string typeValue = columnName; 92 int columnIndex = 0; 93 resultSet->GetColumnIndex(typeValue, columnIndex); 94 OHOS::DataShare::DataType columnType; 95 resultSet->GetDataType(columnIndex, columnType); 96 OHOS::NativeRdb::ValuesBucket valuesBucket = OHOS::RdbDataShareAdapter::RdbUtils::ToValuesBucket(values); 97 // Compare values and resultSet column value equality 98 if (columnType == OHOS::DataShare::DataType::TYPE_INTEGER) { 99 int resultSetIntValue = 0; 100 resultSet->GetInt(columnIndex, resultSetIntValue); 101 if (valuesBucket.HasColumn(typeValue)) { 102 int valuesIntValue = 0; 103 OHOS::NativeRdb::ValueObject valuesObject; 104 valuesBucket.GetObject(typeValue, valuesObject); 105 valuesObject.GetInt(valuesIntValue); 106 std::string tempName = testName; 107 tempName.append("CheckResultSet columnName : %{public}s insertValue = %{public}d "); 108 tempName.append("databaseValue = %{public}d"); 109 HILOG_INFO(tempName.c_str(), typeValue.c_str(), valuesIntValue, resultSetIntValue); 110 EXPECT_EQ(resultSetIntValue, valuesIntValue); 111 } 112 } else if (columnType == OHOS::DataShare::DataType::TYPE_STRING) { 113 std::string resultSetStringValue; 114 resultSet->GetString(columnIndex, resultSetStringValue); 115 if (valuesBucket.HasColumn(typeValue)) { 116 OHOS::NativeRdb::ValueObject valuesObject; 117 std::string valuesStringValue; 118 valuesBucket.GetObject(typeValue, valuesObject); 119 valuesObject.GetString(valuesStringValue); 120 std::string tempName = testName; 121 tempName.append("CheckResultSet columnName : %{public}s insertValue = %{public}s "); 122 tempName.append("databaseValue = %{public}s"); 123 HILOG_INFO(tempName.c_str(), typeValue.c_str(), valuesStringValue.c_str(), resultSetStringValue.c_str()); 124 EXPECT_EQ(resultSetStringValue, valuesStringValue); 125 } 126 } 127} 128 129/** 130 * @brief check values and resultSet value 131 * 132 * @param values of data source 133 * @param resultSet of database 134 */ 135void BaseTest::CheckResultSetList(std::vector<OHOS::DataShare::DataShareValuesBucket> &valuesVector, 136 std::shared_ptr<OHOS::DataShare::DataShareResultSet> &resultSet, std::string testName) 137{ 138 std::vector<std::string> columnNames; 139 resultSet->GetAllColumnNames(columnNames); 140 int resultSetNum = resultSet->GoToFirstRow(); 141 int index = 0; 142 while (resultSetNum == OHOS::NativeRdb::E_OK) { 143 OHOS::DataShare::DataShareValuesBucket values = valuesVector[index]; 144 int size = columnNames.size(); 145 for (int i = 0; i < size; i++) { 146 CheckData(valuesVector[index], resultSet, columnNames[i], testName); 147 } 148 resultSetNum = resultSet->GoToNextRow(); 149 index++; 150 } 151 resultSet->Close(); 152} 153} // namespace Test 154} // namespace Contacts