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#ifndef CALLLOGABILITY_TEST_H
17#define CALLLOGABILITY_TEST_H
18
19#include "base_test.h"
20#include "test_common.h"
21
22namespace Contacts {
23namespace Test {
24namespace Lock {
25std::mutex calllogMtx_;
26}
27
28class CallLogAsync {
29public:
30    OHOS::DataShare::DataShareValuesBucket values;
31    std::map<int, OHOS::DataShare::DataShareValuesBucket> result;
32    OHOS::DataShare::DataShareValuesBucket updateValues;
33    int predicatesId;
34    std::vector<std::shared_ptr<OHOS::DataShare::DataShareResultSet>> resultSet;
35    std::vector<int64_t> predicatesQueryId;
36    int predicatesDeleteId;
37    CallLogAsync(OHOS::DataShare::DataShareValuesBucket &values,
38        std::map<int, OHOS::DataShare::DataShareValuesBucket> &result)
39    {
40        this->values = values;
41        this->result = result;
42        this->predicatesId = -1;
43        this->predicatesDeleteId = -1;
44    }
45    CallLogAsync(OHOS::DataShare::DataShareValuesBucket &updateValues, int &predicatesId)
46    {
47        this->updateValues = updateValues;
48        this->predicatesId = predicatesId;
49        this->predicatesDeleteId = -1;
50    }
51
52    CallLogAsync(std::vector<std::shared_ptr<OHOS::DataShare::DataShareResultSet>> resultSet,
53        std::vector<int64_t> predicatesQueryId)
54    {
55        this->resultSet = resultSet;
56        this->predicatesQueryId = predicatesQueryId;
57        this->predicatesId = -1;
58        this->predicatesDeleteId = -1;
59    }
60
61    explicit CallLogAsync(int &predicatesDeleteId)
62    {
63        this->predicatesId = -1;
64        this->predicatesDeleteId = predicatesDeleteId;
65    }
66    void Insert()
67    {
68        OHOS::AbilityRuntime::CallLogAbility calllogAbility;
69        OHOS::Uri callLogUri(CallLogUri::CALL_LOG);
70        int64_t code = calllogAbility.Insert(callLogUri, this->values);
71        int callLogId = code;
72        EXPECT_GT(callLogId, 0);
73        Lock::calllogMtx_.lock();
74        this->result.insert(std::map<int, OHOS::DataShare::DataShareValuesBucket>::value_type(callLogId, this->values));
75        Lock::calllogMtx_.unlock();
76        HILOG_INFO("--- VoicemailAsync Insert---%{public}s", CallLogUri::CALL_LOG);
77    }
78    void Update()
79    {
80        OHOS::Uri callLogUri(CallLogUri::CALL_LOG);
81        OHOS::DataShare::DataSharePredicates predicates;
82        predicates.EqualTo("id", std::to_string(this->predicatesId));
83        OHOS::AbilityRuntime::CallLogAbility calllogAbility;
84        int resultCode = calllogAbility.Update(callLogUri, predicates, this->updateValues);
85        EXPECT_EQ(0, resultCode);
86        HILOG_INFO("--- VoicemailAsync Update---%{public}s", CallLogUri::CALL_LOG);
87    }
88    void Query()
89    {
90        OHOS::Uri callLogUri(CallLogUri::CALL_LOG);
91        OHOS::DataShare::DataSharePredicates predicates;
92        int size = this->predicatesQueryId.size();
93        for (int i = 0; i < size; i++) {
94            predicates.EqualTo("id", std::to_string(this->predicatesQueryId[i]));
95            if (i < size - 1) {
96                predicates.Or();
97            }
98        }
99        OHOS::AbilityRuntime::CallLogAbility calllogAbility;
100        std::vector<std::string> columns;
101        std::shared_ptr<OHOS::DataShare::DataShareResultSet> resultSet =
102            calllogAbility.Query(callLogUri, predicates, columns);
103        Lock::calllogMtx_.lock();
104        this->resultSet.push_back(resultSet);
105        Lock::calllogMtx_.unlock();
106        HILOG_INFO("--- VoicemailAsync Query---%{public}s", CallLogUri::CALL_LOG);
107    }
108
109    void Delete()
110    {
111        OHOS::Uri callLogUri(CallLogUri::CALL_LOG);
112        OHOS::DataShare::DataSharePredicates predicates;
113        predicates.EqualTo("id", std::to_string(this->predicatesDeleteId));
114        OHOS::AbilityRuntime::CallLogAbility calllogAbility;
115        int resultCode = calllogAbility.Delete(callLogUri, predicates);
116        EXPECT_EQ(0, resultCode);
117        HILOG_INFO("--- VoicemailAsync Delete---%{public}s", CallLogUri::CALL_LOG);
118    }
119};
120
121class CalllogAbilityTest : public BaseTest {
122public:
123    CalllogAbilityTest();
124    ~CalllogAbilityTest();
125    int64_t CalllogInsert(std::string phoneNumber);
126    int CalllogUpdate(OHOS::DataShare::DataShareValuesBucket updateValues,
127        OHOS::DataShare::DataSharePredicates predicates);
128    int CalllogDelete(OHOS::DataShare::DataSharePredicates predicates);
129    std::shared_ptr<OHOS::DataShare::DataShareResultSet> CalllogQuery(
130        std::vector<std::string> columns, OHOS::DataShare::DataSharePredicates predicates);
131    OHOS::DataShare::DataShareValuesBucket GetCallLogValues(int columnsStart, int columnsEnd,
132        std::vector<std::string> &columns);
133    void GetAllValuesColumn(std::vector<std::string> &columns);
134    int64_t CalllogInsertValues(OHOS::DataShare::DataShareValuesBucket &values);
135    int64_t CalllogInsertValue(std::string displayName, OHOS::DataShare::DataShareValuesBucket &values);
136    void ClearCallLog();
137};
138} // namespace Test
139} // namespace Contacts
140#endif // CALLLOGABILITY_TEST_H
141