• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (c) 2024 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 #define LOG_TAG "StoreTest"
16 
17 #include "access_token.h"
18 #include "gtest/gtest.h"
19 #include "general_store_mock.h"
20 #include "log_print.h"
21 #include "metadata/store_meta_data.h"
22 #include "rdb_query.h"
23 #include "rdb_types.h"
24 #include "store/auto_cache.h"
25 #include "store/general_store.h"
26 #include "store/general_value.h"
27 #include "store/general_watcher.h"
28 #include "screen_lock_mock.h"
29 
30 using namespace testing::ext;
31 using namespace OHOS::DistributedData;
32 namespace OHOS::Test {
33 class GeneralValueTest : public testing::Test {
34 public:
SetUpTestCase(void)35     static void SetUpTestCase(void){};
TearDownTestCase(void)36     static void TearDownTestCase(void){};
SetUp()37     void SetUp(){};
TearDown()38     void TearDown(){};
39 };
40 
41 class GeneralStoreTest : public testing::Test {
42 public:
SetUpTestCase(void)43     static void SetUpTestCase(void){};
TearDownTestCase(void)44     static void TearDownTestCase(void){};
SetUp()45     void SetUp(){};
TearDown()46     void TearDown(){};
47 };
48 
49 class AutoCacheTest : public testing::Test {
50 public:
51     static void SetUpTestCase(void);
TearDownTestCase(void)52     static void TearDownTestCase(void){};
SetUp()53     void SetUp(){};
TearDown()54     void TearDown(){};
55 protected:
56     static std::shared_ptr<ScreenLockMock> mock_;
57 };
58 
59 
SetUpTestCase(void)60 void AutoCacheTest::SetUpTestCase(void)
61 {
62     ScreenManager::RegisterInstance(mock_);
63 }
64 
65 std::shared_ptr<ScreenLockMock> AutoCacheTest::mock_ = std::make_shared<ScreenLockMock>();
66 /**
67  * @tc.name: SetQueryNodesTest
68  * @tc.desc: Set and query nodes.
69  * @tc.type: FUNC
70  * @tc.require:
71  */
72 HWTEST_F(GeneralValueTest, SetQueryNodesTest, TestSize.Level2)
73 {
74     ZLOGI("GeneralValueTest SetQueryNodesTest begin.");
75     std::string tableName = "test_tableName";
76     QueryNode node;
77     node.op = OHOS::DistributedData::QueryOperation::EQUAL_TO;
78     node.fieldName =  "test_fieldName";
79     node.fieldValue = {"aaa", "bbb", "ccc"};
80     QueryNodes nodes{
81         {node}
82     };
83     OHOS::DistributedRdb::RdbQuery query;
84     query.SetQueryNodes(tableName, std::move(nodes));
85     QueryNodes nodes1 =  query.GetQueryNodes("test_tableName");
86     EXPECT_EQ(nodes1[0].fieldName, "test_fieldName");
87     EXPECT_EQ(nodes1[0].op, OHOS::DistributedData::QueryOperation::EQUAL_TO);
88     EXPECT_EQ(nodes1[0].fieldValue.size(), 3);
89 }
90 
91 /**
92 * @tc.name: GetMixModeTest
93 * @tc.desc: get mix mode
94 * @tc.type: FUNC
95 * @tc.require:
96 * @tc.author:
97 */
98 HWTEST_F(GeneralStoreTest, GetMixModeTest, TestSize.Level2)
99 {
100     ZLOGI("GeneralStoreTest GetMixModeTest begin.");
101     auto mode1 = OHOS::DistributedRdb::TIME_FIRST;
102     auto mode2 = GeneralStore::AUTO_SYNC_MODE;
103 
104     auto mixMode = GeneralStore::MixMode(mode1, mode2);
105     EXPECT_EQ(mixMode, mode1 | mode2);
106 
107     auto syncMode = GeneralStore::GetSyncMode(mixMode);
108     EXPECT_EQ(syncMode, OHOS::DistributedRdb::TIME_FIRST);
109 
110     auto highMode = GeneralStore::GetHighMode(mixMode);
111     EXPECT_EQ(highMode, GeneralStore::AUTO_SYNC_MODE);
112 }
113 
114 /**
115 * @tc.name: OnChange001
116 * @tc.desc: AutoCache Delegate OnChange
117 * @tc.type: FUNC
118 * @tc.require:
119 * @tc.author: SQL
120 */
121 HWTEST_F(AutoCacheTest, OnChange001, TestSize.Level2)
122 {
123     GeneralStoreMock* store = new (std::nothrow) GeneralStoreMock();
124     ASSERT_NE(store, nullptr);
125     AutoCache::Watchers watchers;
126     int32_t user = 0;
127     StoreMetaData meta;
128     AutoCache::Delegate delegate(store, watchers, user, meta);
129     delegate.SetObservers(watchers);
130     GeneralWatcher::Origin origin;
131     GeneralWatcher::PRIFields primaryFields;
132     GeneralWatcher::ChangeInfo values;
133     auto ret = delegate.OnChange(origin, primaryFields, std::move(values));
134     EXPECT_EQ(ret, GeneralError::E_OK);
135 }
136 
137 /**
138 * @tc.name: OnChange002
139 * @tc.desc: AutoCache Delegate OnChange
140 * @tc.type: FUNC
141 * @tc.require:
142 * @tc.author: SQL
143 */
144 HWTEST_F(AutoCacheTest, OnChange002, TestSize.Level2)
145 {
146     GeneralStoreMock* store = new (std::nothrow) GeneralStoreMock();
147     ASSERT_NE(store, nullptr);
148     AutoCache::Watchers watchers;
149     int32_t user = 0;
150     StoreMetaData meta;
151     AutoCache::Delegate delegate(store, watchers, user, meta);
152     delegate.SetObservers(watchers);
153     GeneralWatcher::Origin origin;
154     GeneralWatcher::Fields fields;
155     GeneralWatcher::ChangeData datas;
156     auto ret = delegate.OnChange(origin, fields, std::move(datas));
157     EXPECT_EQ(ret, GeneralError::E_OK);
158 }
159 
160 /**
161 * @tc.name: operatorStore
162 * @tc.desc: AutoCache Delegate operator Store()
163 * @tc.type: FUNC
164 * @tc.require:
165 * @tc.author: SQL
166 */
167 HWTEST_F(AutoCacheTest, operatorStore, TestSize.Level2)
168 {
169     GeneralStoreMock* store = new (std::nothrow) GeneralStoreMock();
170     ASSERT_NE(store, nullptr);
171     AutoCache::Watchers watchers;
172     int32_t user = 0;
173     StoreMetaData meta;
174     AutoCache::Delegate delegate(store, watchers, user, meta);
175     AutoCache::Store result = static_cast<AutoCache::Store>(delegate);
176     EXPECT_NE(result, nullptr);
177     GeneralWatcher::Origin origin;
178     GeneralWatcher::Fields fields;
179     GeneralWatcher::ChangeData datas;
180     auto ret = delegate.OnChange(origin, fields, std::move(datas));
181     EXPECT_EQ(ret, GeneralError::E_OK);
182     EXPECT_EQ(delegate.GetUser(), user);
183 }
184 
185 /**
186 * @tc.name: GetMeta
187 * @tc.desc: AutoCache Delegate operator GetMeta()
188 * @tc.type: FUNC
189 * @tc.require:
190 * @tc.author:
191 */
192 HWTEST_F(AutoCacheTest, GetMeta, TestSize.Level2)
193 {
194     GeneralStoreMock* store = new (std::nothrow) GeneralStoreMock();
195     ASSERT_NE(store, nullptr);
196     AutoCache::Watchers watchers;
197     int32_t user = 0;
198     StoreMetaData meta;
199     meta.enableCloud = true;
200     AutoCache::Delegate delegate(store, watchers, user, meta);
201     auto newMate = delegate.GetMeta();
202     ASSERT_TRUE(newMate.enableCloud);
203 }
204 
205 /**
206 * @tc.name: GetArea
207 * @tc.desc: AutoCache Delegate operator GetArea()
208 * @tc.type: FUNC
209 * @tc.require:
210 * @tc.author:
211 */
212 HWTEST_F(AutoCacheTest, GetArea, TestSize.Level2)
213 {
214     AutoCache::Watchers watchers;
215     int32_t user = 0;
216     StoreMetaData meta;
217     meta.area = GeneralStore::EL1;
218     AutoCache::Delegate delegate(nullptr, watchers, user, meta);
219     auto newArea = delegate.GetArea();
220     ASSERT_EQ(newArea, GeneralStore::EL1);
221 }
222 
223 /**
224 * @tc.name: GetDBStore
225 * @tc.desc: AutoCache GetDBStore
226 * @tc.type: FUNC
227 * @tc.require:
228 * @tc.author:
229 */
230 HWTEST_F(AutoCacheTest, GetDBStore, TestSize.Level2)
231 {
__anon81c67fd90102(const StoreMetaData &metaData) 232     auto creator = [](const StoreMetaData &metaData) -> GeneralStore* {
233         return new (std::nothrow) GeneralStoreMock();
234     };
235     AutoCache::GetInstance().RegCreator(DistributedRdb::RDB_DEVICE_COLLABORATION, creator);
236     AutoCache::Watchers watchers;
237     StoreMetaData meta;
238     meta.storeType = DistributedRdb::RDB_DEVICE_COLLABORATION;
239     mock_->isLocked_ = true;
240     meta.area = GeneralStore::EL4;
241     EXPECT_EQ(AutoCache::GetInstance().GetDBStore(meta, watchers).first, GeneralError::E_SCREEN_LOCKED);
242     meta.area = GeneralStore::EL1;
243     EXPECT_NE(AutoCache::GetInstance().GetDBStore(meta, watchers).first, GeneralError::E_SCREEN_LOCKED);
244     meta.area = GeneralStore::EL2;
245     EXPECT_NE(AutoCache::GetInstance().GetDBStore(meta, watchers).first, GeneralError::E_SCREEN_LOCKED);
246     meta.area = GeneralStore::EL3;
247     EXPECT_NE(AutoCache::GetInstance().GetDBStore(meta, watchers).first, GeneralError::E_SCREEN_LOCKED);
248     meta.area = GeneralStore::EL5;
249     EXPECT_NE(AutoCache::GetInstance().GetDBStore(meta, watchers).first, GeneralError::E_SCREEN_LOCKED);
250 
251     mock_->isLocked_ = false;
252     meta.area = GeneralStore::EL4;
253     EXPECT_NE(AutoCache::GetInstance().GetDBStore(meta, watchers).first, GeneralError::E_SCREEN_LOCKED);
254     meta.area = GeneralStore::EL1;
255     EXPECT_NE(AutoCache::GetInstance().GetDBStore(meta, watchers).first, GeneralError::E_SCREEN_LOCKED);
256     meta.area = GeneralStore::EL2;
257     EXPECT_NE(AutoCache::GetInstance().GetDBStore(meta, watchers).first, GeneralError::E_SCREEN_LOCKED);
258     meta.area = GeneralStore::EL3;
259     EXPECT_NE(AutoCache::GetInstance().GetDBStore(meta, watchers).first, GeneralError::E_SCREEN_LOCKED);
260     meta.area = GeneralStore::EL5;
261     EXPECT_NE(AutoCache::GetInstance().GetDBStore(meta, watchers).first, GeneralError::E_SCREEN_LOCKED);
262 }
263 } // namespace OHOS::Test