1115cd2caSopenharmony_ci/* 2115cd2caSopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 3115cd2caSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4115cd2caSopenharmony_ci * you may not use this file except in compliance with the License. 5115cd2caSopenharmony_ci * You may obtain a copy of the License at 6115cd2caSopenharmony_ci * 7115cd2caSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8115cd2caSopenharmony_ci * 9115cd2caSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10115cd2caSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11115cd2caSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12115cd2caSopenharmony_ci * See the License for the specific language governing permissions and 13115cd2caSopenharmony_ci * limitations under the License. 14115cd2caSopenharmony_ci */ 15115cd2caSopenharmony_ci 16115cd2caSopenharmony_ci#include "calllogfuzzyquery_test.h" 17115cd2caSopenharmony_ci 18115cd2caSopenharmony_ci#include "random_number_utils.h" 19115cd2caSopenharmony_ci#include "test_common.h" 20115cd2caSopenharmony_ci 21115cd2caSopenharmony_cinamespace Contacts { 22115cd2caSopenharmony_cinamespace Test { 23115cd2caSopenharmony_ciCalllogFuzzyQueryTest::CalllogFuzzyQueryTest() 24115cd2caSopenharmony_ci{ 25115cd2caSopenharmony_ci} 26115cd2caSopenharmony_ci 27115cd2caSopenharmony_ciCalllogFuzzyQueryTest::~CalllogFuzzyQueryTest() 28115cd2caSopenharmony_ci{ 29115cd2caSopenharmony_ci} 30115cd2caSopenharmony_ci 31115cd2caSopenharmony_ciint CalllogFuzzyQueryTest::CalllogDelete(OHOS::DataShare::DataSharePredicates predicates) 32115cd2caSopenharmony_ci{ 33115cd2caSopenharmony_ci OHOS::Uri uriCalllog(CallLogUri::CALL_LOG); 34115cd2caSopenharmony_ci int code = calllogAbility.Delete(uriCalllog, predicates); 35115cd2caSopenharmony_ci return code; 36115cd2caSopenharmony_ci} 37115cd2caSopenharmony_ci 38115cd2caSopenharmony_cistd::shared_ptr<OHOS::DataShare::DataShareResultSet> CalllogFuzzyQueryTest::CalllogQuery( 39115cd2caSopenharmony_ci std::vector<std::string> columns, OHOS::DataShare::DataSharePredicates predicates) 40115cd2caSopenharmony_ci{ 41115cd2caSopenharmony_ci OHOS::Uri uriCalllog(CallLogUri::CALL_LOG); 42115cd2caSopenharmony_ci std::shared_ptr<OHOS::DataShare::DataShareResultSet> resultSet = 43115cd2caSopenharmony_ci calllogAbility.Query(uriCalllog, predicates, columns); 44115cd2caSopenharmony_ci return resultSet; 45115cd2caSopenharmony_ci} 46115cd2caSopenharmony_ci 47115cd2caSopenharmony_ciint64_t CalllogFuzzyQueryTest::CalllogInsertValues(OHOS::DataShare::DataShareValuesBucket &values) 48115cd2caSopenharmony_ci{ 49115cd2caSopenharmony_ci OHOS::Uri uriCalllog(CallLogUri::CALL_LOG); 50115cd2caSopenharmony_ci int64_t code = calllogAbility.Insert(uriCalllog, values); 51115cd2caSopenharmony_ci return code; 52115cd2caSopenharmony_ci} 53115cd2caSopenharmony_ci 54115cd2caSopenharmony_civoid CalllogFuzzyQueryTest::ClearCallLog() 55115cd2caSopenharmony_ci{ 56115cd2caSopenharmony_ci // clear all callLog data 57115cd2caSopenharmony_ci OHOS::DataShare::DataSharePredicates predicates; 58115cd2caSopenharmony_ci predicates.GreaterThan("id", "0"); 59115cd2caSopenharmony_ci int deleteCode = CalllogDelete(predicates); 60115cd2caSopenharmony_ci EXPECT_EQ(deleteCode, 0); 61115cd2caSopenharmony_ci} 62115cd2caSopenharmony_ci 63115cd2caSopenharmony_ci/** 64115cd2caSopenharmony_ci * @tc.number calllog_Query_test_100 65115cd2caSopenharmony_ci * @tc.name Fuzzy query according to the beginning of mobile phone number 66115cd2caSopenharmony_ci * @tc.desc Query call records of mobile phone numbers that start with 123 67115cd2caSopenharmony_ci * @tc.level Level1 68115cd2caSopenharmony_ci * @tc.size MediumTest 69115cd2caSopenharmony_ci * @tc.type Function 70115cd2caSopenharmony_ci */ 71115cd2caSopenharmony_ciHWTEST_F(CalllogFuzzyQueryTest, calllog_Query_test_100, testing::ext::TestSize.Level1) 72115cd2caSopenharmony_ci{ 73115cd2caSopenharmony_ci HILOG_INFO("----calllog_Query_test_100 is starting!----"); 74115cd2caSopenharmony_ci OHOS::DataShare::DataShareValuesBucket valuesBucket; 75115cd2caSopenharmony_ci OHOS::Contacts::RandomNumberUtils randomNumberUtils; 76115cd2caSopenharmony_ci std::string phoneNumber = randomNumberUtils.Generating(9); 77115cd2caSopenharmony_ci valuesBucket.Put("phone_number", phoneNumber); 78115cd2caSopenharmony_ci int rawId = CalllogInsertValues(valuesBucket); 79115cd2caSopenharmony_ci EXPECT_GT(rawId, 0); 80115cd2caSopenharmony_ci 81115cd2caSopenharmony_ci valuesBucket.Put("id", rawId); 82115cd2caSopenharmony_ci OHOS::DataShare::DataShareValuesBucket valuesBucketTwo; 83115cd2caSopenharmony_ci std::string phoneNumber_test = randomNumberUtils.Generating(10); 84115cd2caSopenharmony_ci valuesBucketTwo.Put("phone_number", phoneNumber_test); 85115cd2caSopenharmony_ci rawId = CalllogInsertValues(valuesBucketTwo); 86115cd2caSopenharmony_ci valuesBucketTwo.Put("id", rawId); 87115cd2caSopenharmony_ci EXPECT_GT(rawId, 0); 88115cd2caSopenharmony_ci 89115cd2caSopenharmony_ci std::vector<std::string> columns; 90115cd2caSopenharmony_ci columns.push_back("id"); 91115cd2caSopenharmony_ci columns.push_back("phone_number"); 92115cd2caSopenharmony_ci OHOS::DataShare::DataSharePredicates predicates; 93115cd2caSopenharmony_ci predicates.Like("phone_number", "123%"); 94115cd2caSopenharmony_ci predicates.OrderByAsc("id"); 95115cd2caSopenharmony_ci std::vector<OHOS::DataShare::DataShareValuesBucket> valuesBucketVector; 96115cd2caSopenharmony_ci valuesBucketVector.push_back(valuesBucket); 97115cd2caSopenharmony_ci valuesBucketVector.push_back(valuesBucketTwo); 98115cd2caSopenharmony_ci std::shared_ptr<OHOS::DataShare::DataShareResultSet> resultSet = CalllogQuery(columns, predicates); 99115cd2caSopenharmony_ci // resultSet count 1 100115cd2caSopenharmony_ci int rowCount = 0; 101115cd2caSopenharmony_ci resultSet->GetRowCount(rowCount); 102115cd2caSopenharmony_ci int currentCount = 1; 103115cd2caSopenharmony_ci EXPECT_EQ(currentCount, rowCount); 104115cd2caSopenharmony_ci CheckResultSetList(valuesBucketVector, resultSet, "calllog_Query_test_100"); 105115cd2caSopenharmony_ci resultSet->Close(); 106115cd2caSopenharmony_ci ClearCallLog(); 107115cd2caSopenharmony_ci} 108115cd2caSopenharmony_ci 109115cd2caSopenharmony_ci/** 110115cd2caSopenharmony_ci * @tc.number calllog_Query_test_200 111115cd2caSopenharmony_ci * @tc.name Fuzzy query according to the end of mobile phone number 112115cd2caSopenharmony_ci * @tc.desc Query call records with mobile phone number ending in 0 113115cd2caSopenharmony_ci * @tc.level Level1 114115cd2caSopenharmony_ci * @tc.size MediumTest 115115cd2caSopenharmony_ci * @tc.type Function 116115cd2caSopenharmony_ci */ 117115cd2caSopenharmony_ciHWTEST_F(CalllogFuzzyQueryTest, calllog_Query_test_200, testing::ext::TestSize.Level1) 118115cd2caSopenharmony_ci{ 119115cd2caSopenharmony_ci HILOG_INFO("----calllog_Query_test_200 is starting!----"); 120115cd2caSopenharmony_ci OHOS::DataShare::DataShareValuesBucket valuesBucket; 121115cd2caSopenharmony_ci OHOS::Contacts::RandomNumberUtils randomNumberUtils; 122115cd2caSopenharmony_ci std::string phoneNumber = randomNumberUtils.Generating(11); 123115cd2caSopenharmony_ci valuesBucket.Put("phone_number", phoneNumber); 124115cd2caSopenharmony_ci int rawId = CalllogInsertValues(valuesBucket); 125115cd2caSopenharmony_ci EXPECT_GT(rawId, 0); 126115cd2caSopenharmony_ci valuesBucket.Put("id", rawId); 127115cd2caSopenharmony_ci OHOS::DataShare::DataShareValuesBucket valuesBucketTwo; 128115cd2caSopenharmony_ci valuesBucketTwo.Put("phone_number", phoneNumber); 129115cd2caSopenharmony_ci rawId = CalllogInsertValues(valuesBucketTwo); 130115cd2caSopenharmony_ci valuesBucketTwo.Put("id", rawId); 131115cd2caSopenharmony_ci EXPECT_GT(rawId, 0); 132115cd2caSopenharmony_ci std::vector<std::string> columns; 133115cd2caSopenharmony_ci columns.push_back("id"); 134115cd2caSopenharmony_ci columns.push_back("phone_number"); 135115cd2caSopenharmony_ci OHOS::DataShare::DataSharePredicates predicates; 136115cd2caSopenharmony_ci predicates.Like("phone_number", "%750"); 137115cd2caSopenharmony_ci predicates.OrderByAsc("id"); 138115cd2caSopenharmony_ci std::shared_ptr<OHOS::DataShare::DataShareResultSet> resultSet = CalllogQuery(columns, predicates); 139115cd2caSopenharmony_ci // resultSet count 1 140115cd2caSopenharmony_ci int rowCount = 0; 141115cd2caSopenharmony_ci resultSet->GetRowCount(rowCount); 142115cd2caSopenharmony_ci int currentCount = 1; 143115cd2caSopenharmony_ci EXPECT_EQ(currentCount, rowCount); 144115cd2caSopenharmony_ci CheckResultSet(valuesBucket, resultSet, "calllog_Query_test_200"); 145115cd2caSopenharmony_ci resultSet->Close(); 146115cd2caSopenharmony_ci ClearCallLog(); 147115cd2caSopenharmony_ci} 148115cd2caSopenharmony_ci 149115cd2caSopenharmony_ci/** 150115cd2caSopenharmony_ci * @tc.number calllog_Query_test_300 151115cd2caSopenharmony_ci * @tc.name Fuzzy query according to the middle digits of the mobile phone number 152115cd2caSopenharmony_ci * @tc.desc Query the call history of the mobile phone number starting with 123 and ending with 8910 153115cd2caSopenharmony_ci * @tc.level Level1 154115cd2caSopenharmony_ci * @tc.size MediumTest 155115cd2caSopenharmony_ci * @tc.type Function 156115cd2caSopenharmony_ci */ 157115cd2caSopenharmony_ciHWTEST_F(CalllogFuzzyQueryTest, calllog_Query_test_300, testing::ext::TestSize.Level1) 158115cd2caSopenharmony_ci{ 159115cd2caSopenharmony_ci HILOG_INFO("----calllog_Query_test_300 is starting!----"); 160115cd2caSopenharmony_ci OHOS::DataShare::DataShareValuesBucket valuesBucket; 161115cd2caSopenharmony_ci OHOS::Contacts::RandomNumberUtils randomNumberUtils; 162115cd2caSopenharmony_ci std::string phoneNumber = randomNumberUtils.Generating(11); 163115cd2caSopenharmony_ci valuesBucket.Put("phone_number", phoneNumber); 164115cd2caSopenharmony_ci int rawId = CalllogInsertValues(valuesBucket); 165115cd2caSopenharmony_ci EXPECT_GT(rawId, 0); 166115cd2caSopenharmony_ci valuesBucket.Put("id", rawId); 167115cd2caSopenharmony_ci OHOS::DataShare::DataShareValuesBucket valuesBucketTwo; 168115cd2caSopenharmony_ci valuesBucketTwo.Put("phone_number", phoneNumber); 169115cd2caSopenharmony_ci rawId = CalllogInsertValues(valuesBucketTwo); 170115cd2caSopenharmony_ci valuesBucketTwo.Put("id", rawId); 171115cd2caSopenharmony_ci EXPECT_GT(rawId, 0); 172115cd2caSopenharmony_ci std::vector<std::string> columns; 173115cd2caSopenharmony_ci columns.push_back("id"); 174115cd2caSopenharmony_ci columns.push_back("phone_number"); 175115cd2caSopenharmony_ci OHOS::DataShare::DataSharePredicates predicates; 176115cd2caSopenharmony_ci std::string phoneNumber_test = randomNumberUtils.Generating(17); 177115cd2caSopenharmony_ci predicates.Like("phone_number", phoneNumber_test); 178115cd2caSopenharmony_ci predicates.OrderByAsc("id"); 179115cd2caSopenharmony_ci std::shared_ptr<OHOS::DataShare::DataShareResultSet> resultSet = CalllogQuery(columns, predicates); 180115cd2caSopenharmony_ci // resultSet count 0 181115cd2caSopenharmony_ci int rowCount = 0; 182115cd2caSopenharmony_ci resultSet->GetRowCount(rowCount); 183115cd2caSopenharmony_ci EXPECT_EQ(0, rowCount); 184115cd2caSopenharmony_ci resultSet->Close(); 185115cd2caSopenharmony_ci ClearCallLog(); 186115cd2caSopenharmony_ci} 187115cd2caSopenharmony_ci 188115cd2caSopenharmony_ci/** 189115cd2caSopenharmony_ci * @tc.number calllog_Query_test_400 190115cd2caSopenharmony_ci * @tc.name Fuzzy query according to the fields contained in the mobile phone number 191115cd2caSopenharmony_ci * @tc.desc Query the call history of the mobile phone number starting with 123 192115cd2caSopenharmony_ci * @tc.level Level1 193115cd2caSopenharmony_ci * @tc.size MediumTest 194115cd2caSopenharmony_ci * @tc.type Function 195115cd2caSopenharmony_ci */ 196115cd2caSopenharmony_ciHWTEST_F(CalllogFuzzyQueryTest, calllog_Query_test_400, testing::ext::TestSize.Level1) 197115cd2caSopenharmony_ci{ 198115cd2caSopenharmony_ci HILOG_INFO("----calllog_Query_test_400 is starting!----"); 199115cd2caSopenharmony_ci OHOS::DataShare::DataShareValuesBucket valuesBucket; 200115cd2caSopenharmony_ci OHOS::Contacts::RandomNumberUtils randomNumberUtils; 201115cd2caSopenharmony_ci std::string phoneNumber = randomNumberUtils.Generating(11); 202115cd2caSopenharmony_ci valuesBucket.Put("phone_number", phoneNumber); 203115cd2caSopenharmony_ci int rawId = CalllogInsertValues(valuesBucket); 204115cd2caSopenharmony_ci EXPECT_GT(rawId, 0); 205115cd2caSopenharmony_ci valuesBucket.Put("id", rawId); 206115cd2caSopenharmony_ci OHOS::DataShare::DataShareValuesBucket valuesBucketTwo; 207115cd2caSopenharmony_ci valuesBucketTwo.Put("phone_number", phoneNumber); 208115cd2caSopenharmony_ci rawId = CalllogInsertValues(valuesBucketTwo); 209115cd2caSopenharmony_ci valuesBucketTwo.Put("id", rawId); 210115cd2caSopenharmony_ci EXPECT_GT(rawId, 0); 211115cd2caSopenharmony_ci std::vector<std::string> columns; 212115cd2caSopenharmony_ci columns.push_back("id"); 213115cd2caSopenharmony_ci columns.push_back("phone_number"); 214115cd2caSopenharmony_ci OHOS::DataShare::DataSharePredicates predicates; 215115cd2caSopenharmony_ci predicates.Like("phone_number", "%37845%"); 216115cd2caSopenharmony_ci predicates.OrderByAsc("id"); 217115cd2caSopenharmony_ci std::shared_ptr<OHOS::DataShare::DataShareResultSet> resultSet = CalllogQuery(columns, predicates); 218115cd2caSopenharmony_ci // resultSet count 0 219115cd2caSopenharmony_ci int rowCount = 0; 220115cd2caSopenharmony_ci resultSet->GetRowCount(rowCount); 221115cd2caSopenharmony_ci EXPECT_EQ(1, rowCount); 222115cd2caSopenharmony_ci CheckResultSet(valuesBucketTwo, resultSet, "calllog_Query_test_400"); 223115cd2caSopenharmony_ci resultSet->Close(); 224115cd2caSopenharmony_ci ClearCallLog(); 225115cd2caSopenharmony_ci} 226115cd2caSopenharmony_ci 227115cd2caSopenharmony_ci/** 228115cd2caSopenharmony_ci * @tc.number abnormal_calllog_Query_test_500 229115cd2caSopenharmony_ci * @tc.name An error field is passed in during fuzzy query based on mobile phone number 230115cd2caSopenharmony_ci * @tc.desc Query the call history of the mobile phone number starting with 123 and ending with 8910 231115cd2caSopenharmony_ci * @tc.level Level1 232115cd2caSopenharmony_ci * @tc.size MediumTest 233115cd2caSopenharmony_ci * @tc.type Function 234115cd2caSopenharmony_ci */ 235115cd2caSopenharmony_ciHWTEST_F(CalllogFuzzyQueryTest, abnormal_calllog_Query_test_500, testing::ext::TestSize.Level1) 236115cd2caSopenharmony_ci{ 237115cd2caSopenharmony_ci HILOG_INFO("----abnormal_calllog_Query_test_500 is starting!----"); 238115cd2caSopenharmony_ci OHOS::DataShare::DataShareValuesBucket valuesBucket; 239115cd2caSopenharmony_ci OHOS::Contacts::RandomNumberUtils randomNumberUtils; 240115cd2caSopenharmony_ci std::string phoneNumber = randomNumberUtils.Generating(11); 241115cd2caSopenharmony_ci valuesBucket.Put("phone_number", phoneNumber); 242115cd2caSopenharmony_ci int rawId = CalllogInsertValues(valuesBucket); 243115cd2caSopenharmony_ci EXPECT_GT(rawId, 0); 244115cd2caSopenharmony_ci 245115cd2caSopenharmony_ci valuesBucket.Put("id", rawId); 246115cd2caSopenharmony_ci OHOS::DataShare::DataShareValuesBucket valuesBucketTwo; 247115cd2caSopenharmony_ci valuesBucketTwo.Put("phone_number", phoneNumber); 248115cd2caSopenharmony_ci rawId = CalllogInsertValues(valuesBucketTwo); 249115cd2caSopenharmony_ci valuesBucketTwo.Put("id", rawId); 250115cd2caSopenharmony_ci EXPECT_GT(rawId, 0); 251115cd2caSopenharmony_ci 252115cd2caSopenharmony_ci std::vector<std::string> columns; 253115cd2caSopenharmony_ci columns.push_back("id"); 254115cd2caSopenharmony_ci columns.push_back("phone_number"); 255115cd2caSopenharmony_ci std::string phoneNumber_test = randomNumberUtils.Generating(17); 256115cd2caSopenharmony_ci OHOS::DataShare::DataSharePredicates predicates; 257115cd2caSopenharmony_ci predicates.Like("phone_numbers", phoneNumber_test); 258115cd2caSopenharmony_ci predicates.OrderByAsc("id"); 259115cd2caSopenharmony_ci std::shared_ptr<OHOS::DataShare::DataShareResultSet> resultSet = CalllogQuery(columns, predicates); 260115cd2caSopenharmony_ci int rowCount = 0; 261115cd2caSopenharmony_ci resultSet->GetRowCount(rowCount); 262115cd2caSopenharmony_ci EXPECT_EQ(-1, rowCount); 263115cd2caSopenharmony_ci resultSet->Close(); 264115cd2caSopenharmony_ci ClearCallLog(); 265115cd2caSopenharmony_ci} 266115cd2caSopenharmony_ci} // namespace Test 267115cd2caSopenharmony_ci} // namespace Contacts