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#ifndef CONTACTABILITY_TEST_H
17115cd2caSopenharmony_ci#define CONTACTABILITY_TEST_H
18115cd2caSopenharmony_ci
19115cd2caSopenharmony_ci#include "base_test.h"
20115cd2caSopenharmony_ci#include "test_common.h"
21115cd2caSopenharmony_ci
22115cd2caSopenharmony_cinamespace Contacts {
23115cd2caSopenharmony_cinamespace Test {
24115cd2caSopenharmony_cinamespace Lock {
25115cd2caSopenharmony_cistd::mutex contactsMtx_;
26115cd2caSopenharmony_ci}
27115cd2caSopenharmony_ci
28115cd2caSopenharmony_ciclass ContactAsync {
29115cd2caSopenharmony_cipublic:
30115cd2caSopenharmony_ci    OHOS::DataShare::DataShareValuesBucket values;
31115cd2caSopenharmony_ci    std::map<int, OHOS::DataShare::DataShareValuesBucket> result;
32115cd2caSopenharmony_ci    OHOS::DataShare::DataShareValuesBucket updateValues;
33115cd2caSopenharmony_ci    int predicatesId;
34115cd2caSopenharmony_ci    std::vector<std::shared_ptr<OHOS::DataShare::DataShareResultSet>> resultSet;
35115cd2caSopenharmony_ci    std::vector<int64_t> predicatesQueryId;
36115cd2caSopenharmony_ci    int predicatesDeleteId;
37115cd2caSopenharmony_ci    ContactAsync(OHOS::DataShare::DataShareValuesBucket &values, std::map<int,
38115cd2caSopenharmony_ci        OHOS::DataShare::DataShareValuesBucket> &result)
39115cd2caSopenharmony_ci    {
40115cd2caSopenharmony_ci        this->values = values;
41115cd2caSopenharmony_ci        this->result = result;
42115cd2caSopenharmony_ci        this->predicatesId = -1;
43115cd2caSopenharmony_ci        this->predicatesDeleteId = -1;
44115cd2caSopenharmony_ci    }
45115cd2caSopenharmony_ci    ContactAsync(OHOS::DataShare::DataShareValuesBucket &updateValues, int &predicatesId)
46115cd2caSopenharmony_ci    {
47115cd2caSopenharmony_ci        this->updateValues = updateValues;
48115cd2caSopenharmony_ci        this->predicatesId = predicatesId;
49115cd2caSopenharmony_ci        this->predicatesDeleteId = -1;
50115cd2caSopenharmony_ci    }
51115cd2caSopenharmony_ci
52115cd2caSopenharmony_ci    ContactAsync(std::vector<std::shared_ptr<OHOS::DataShare::DataShareResultSet>> resultSet,
53115cd2caSopenharmony_ci        std::vector<int64_t> predicatesQueryId)
54115cd2caSopenharmony_ci    {
55115cd2caSopenharmony_ci        this->resultSet = resultSet;
56115cd2caSopenharmony_ci        this->predicatesQueryId = predicatesQueryId;
57115cd2caSopenharmony_ci        this->predicatesId = -1;
58115cd2caSopenharmony_ci        this->predicatesDeleteId = -1;
59115cd2caSopenharmony_ci    }
60115cd2caSopenharmony_ci
61115cd2caSopenharmony_ci    explicit ContactAsync(int &predicatesDeleteId)
62115cd2caSopenharmony_ci    {
63115cd2caSopenharmony_ci        this->predicatesId = -1;
64115cd2caSopenharmony_ci        this->predicatesDeleteId = predicatesDeleteId;
65115cd2caSopenharmony_ci    }
66115cd2caSopenharmony_ci    void Insert()
67115cd2caSopenharmony_ci    {
68115cd2caSopenharmony_ci        OHOS::AbilityRuntime::ContactsDataAbility contactsDataAbility;
69115cd2caSopenharmony_ci        OHOS::Uri uriRawContact(ContactsUri::RAW_CONTACT);
70115cd2caSopenharmony_ci        int64_t code = contactsDataAbility.Insert(uriRawContact, this->values);
71115cd2caSopenharmony_ci        int rawContactId = code;
72115cd2caSopenharmony_ci        EXPECT_GT(rawContactId, 0);
73115cd2caSopenharmony_ci        Lock::contactsMtx_.lock();
74115cd2caSopenharmony_ci        this->result.insert(std::map<int, OHOS::DataShare::DataShareValuesBucket>::value_type(rawContactId,
75115cd2caSopenharmony_ci            this->values));
76115cd2caSopenharmony_ci        Lock::contactsMtx_.unlock();
77115cd2caSopenharmony_ci        HILOG_INFO("--- VoicemailAsync Insert---%{public}s", ContactsUri::RAW_CONTACT);
78115cd2caSopenharmony_ci    }
79115cd2caSopenharmony_ci    void Update()
80115cd2caSopenharmony_ci    {
81115cd2caSopenharmony_ci        OHOS::Uri uriRawContact(ContactsUri::RAW_CONTACT);
82115cd2caSopenharmony_ci        OHOS::DataShare::DataSharePredicates predicates;
83115cd2caSopenharmony_ci        predicates.EqualTo("id", std::to_string(this->predicatesId));
84115cd2caSopenharmony_ci        OHOS::AbilityRuntime::ContactsDataAbility contactsDataAbility;
85115cd2caSopenharmony_ci        int resultCode = contactsDataAbility.Update(uriRawContact, predicates, this->updateValues);
86115cd2caSopenharmony_ci        EXPECT_EQ(0, resultCode);
87115cd2caSopenharmony_ci        HILOG_INFO("--- VoicemailAsync Update---%{public}s", ContactsUri::RAW_CONTACT);
88115cd2caSopenharmony_ci    }
89115cd2caSopenharmony_ci    void Query()
90115cd2caSopenharmony_ci    {
91115cd2caSopenharmony_ci        OHOS::Uri uriRawContact(ContactsUri::RAW_CONTACT);
92115cd2caSopenharmony_ci        OHOS::DataShare::DataSharePredicates predicates;
93115cd2caSopenharmony_ci        int size = this->predicatesQueryId.size();
94115cd2caSopenharmony_ci        for (int i = 0; i < size; i++) {
95115cd2caSopenharmony_ci            predicates.EqualTo("id", std::to_string(this->predicatesQueryId[i]));
96115cd2caSopenharmony_ci            if (i < size - 1) {
97115cd2caSopenharmony_ci                predicates.Or();
98115cd2caSopenharmony_ci            }
99115cd2caSopenharmony_ci        }
100115cd2caSopenharmony_ci        OHOS::AbilityRuntime::ContactsDataAbility contactsDataAbility;
101115cd2caSopenharmony_ci        std::vector<std::string> columns;
102115cd2caSopenharmony_ci        std::shared_ptr<OHOS::DataShare::DataShareResultSet> resultSet =
103115cd2caSopenharmony_ci            contactsDataAbility.Query(uriRawContact, predicates, columns);
104115cd2caSopenharmony_ci        Lock::contactsMtx_.lock();
105115cd2caSopenharmony_ci        this->resultSet.push_back(resultSet);
106115cd2caSopenharmony_ci        Lock::contactsMtx_.unlock();
107115cd2caSopenharmony_ci        HILOG_INFO("--- VoicemailAsync Query---%{public}s", ContactsUri::RAW_CONTACT);
108115cd2caSopenharmony_ci    }
109115cd2caSopenharmony_ci
110115cd2caSopenharmony_ci    void Delete()
111115cd2caSopenharmony_ci    {
112115cd2caSopenharmony_ci        OHOS::Uri uriRawContact(ContactsUri::RAW_CONTACT);
113115cd2caSopenharmony_ci        OHOS::DataShare::DataSharePredicates predicates;
114115cd2caSopenharmony_ci        predicates.EqualTo("id", std::to_string(this->predicatesDeleteId));
115115cd2caSopenharmony_ci        OHOS::AbilityRuntime::ContactsDataAbility contactsDataAbility;
116115cd2caSopenharmony_ci        int resultCode = contactsDataAbility.Delete(uriRawContact, predicates);
117115cd2caSopenharmony_ci        EXPECT_EQ(0, resultCode);
118115cd2caSopenharmony_ci        HILOG_INFO("--- VoicemailAsync Delete---%{public}s", ContactsUri::RAW_CONTACT);
119115cd2caSopenharmony_ci    }
120115cd2caSopenharmony_ci};
121115cd2caSopenharmony_ci
122115cd2caSopenharmony_ciclass ContactAbilityTest : public BaseTest {
123115cd2caSopenharmony_cipublic:
124115cd2caSopenharmony_ci    static constexpr int SLEEP_TIME = 2;
125115cd2caSopenharmony_ci
126115cd2caSopenharmony_ci    ContactAbilityTest();
127115cd2caSopenharmony_ci    ~ContactAbilityTest();
128115cd2caSopenharmony_ci    int64_t RawContactInsert(std::string displayName, OHOS::DataShare::DataShareValuesBucket &alues);
129115cd2caSopenharmony_ci    int64_t RawContactExpandInsert(
130115cd2caSopenharmony_ci        std::vector<std::string> valueVector, int isFavorite, OHOS::DataShare::DataShareValuesBucket &rawContactValues);
131115cd2caSopenharmony_ci    int64_t RawContactLastContactedInsert(
132115cd2caSopenharmony_ci        std::string displayName, int lastestContactedTime, OHOS::DataShare::DataShareValuesBucket &rawContactValues);
133115cd2caSopenharmony_ci    int64_t ContactDataInsert(int64_t rawContactId, std::string contentType, std::string detailInfo,
134115cd2caSopenharmony_ci        std::string position, OHOS::DataShare::DataShareValuesBucket &contactDataValues);
135115cd2caSopenharmony_ci    int64_t GroupsInsert(std::string groupName, OHOS::DataShare::DataShareValuesBucket &groupValues);
136115cd2caSopenharmony_ci    int64_t ContactBlocklistInsert(std::string phoneNumber, OHOS::DataShare::DataShareValuesBucket &rawContactValues);
137115cd2caSopenharmony_ci    int ContactUpdate(const std::string &tableName, OHOS::DataShare::DataShareValuesBucket updateValues,
138115cd2caSopenharmony_ci        OHOS::DataShare::DataSharePredicates predicates);
139115cd2caSopenharmony_ci    int ContactDelete(const std::string &tableName, OHOS::DataShare::DataSharePredicates predicates);
140115cd2caSopenharmony_ci    std::shared_ptr<OHOS::DataShare::DataShareResultSet> ContactQuery(const std::string &tableName,
141115cd2caSopenharmony_ci        std::vector<std::string> &columns, OHOS::DataShare::DataSharePredicates predicates);
142115cd2caSopenharmony_ci    void QueryAndExpectResult(std::string &tableName, OHOS::DataShare::DataSharePredicates predicates,
143115cd2caSopenharmony_ci        OHOS::DataShare::DataShareValuesBucket &values, std::string testName);
144115cd2caSopenharmony_ci    OHOS::DataShare::DataShareValuesBucket GetAllColumnsValues(
145115cd2caSopenharmony_ci        std::vector<std::string> &columnsInt, std::vector<std::string> &columnsStr);
146115cd2caSopenharmony_ci    void GetAllRawContactColumns(std::vector<std::string> &columnsInt, std::vector<std::string> &columnsStr);
147115cd2caSopenharmony_ci    void GetAllContactDataColumns(std::vector<std::string> &columnInt, std::vector<std::string> &columnStr);
148115cd2caSopenharmony_ci    void GetAllGroupsColumns(std::vector<std::string> &columnInt, std::vector<std::string> &columnStr);
149115cd2caSopenharmony_ci    void GetAllContactBlocklistColumns(std::vector<std::string> &columnInt, std::vector<std::string> &columnStr);
150115cd2caSopenharmony_ci    void GetDetailsContactDataColumns(std::vector<std::string> &columns);
151115cd2caSopenharmony_ci    void MergeColumns(
152115cd2caSopenharmony_ci        std::vector<std::string> &columns, std::vector<std::string> &columnsInt, std::vector<std::string> &columnsStr);
153115cd2caSopenharmony_ci    int64_t RawContactInsertValues(OHOS::DataShare::DataShareValuesBucket &values);
154115cd2caSopenharmony_ci    int64_t ContactDataInsertValues(OHOS::DataShare::DataShareValuesBucket &values);
155115cd2caSopenharmony_ci    int64_t GroupsInsertValues(OHOS::DataShare::DataShareValuesBucket &values);
156115cd2caSopenharmony_ci    int64_t ContactBlocklistInsertValues(OHOS::DataShare::DataShareValuesBucket &values);
157115cd2caSopenharmony_ci    std::vector<OHOS::DataShare::DataShareValuesBucket> GetBatchList(int64_t rawContactId);
158115cd2caSopenharmony_ci    void ClearContacts();
159115cd2caSopenharmony_ci};
160115cd2caSopenharmony_ci} // namespace Test
161115cd2caSopenharmony_ci} // namespace Contacts
162115cd2caSopenharmony_ci#endif // CONTACTABILITY_TEST_H
163