• 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 
16 #include <gtest/gtest.h>
17 #include <unistd.h>
18 
19 #include "asset_change_timer.h"
20 
21 #include "client_adaptor.h"
22 #include "logger.h"
23 #include "objectstore_errors.h"
24 #include "js_watcher.h"
25 #include "distributed_objectstore_impl.h"
26 #include "app_types.h"
27 #include "flat_object_storage_engine.h"
28 #include "distributed_object_impl.h"
29 
30 using namespace testing::ext;
31 using namespace OHOS::ObjectStore;
32 using namespace OHOS;
33 using namespace std;
34 
35 namespace {
36 static constexpr uint32_t WAIT_INTERVAL = 100;
37 static constexpr char ASSET_SEPARATOR = '#';
38 class AssetChangeTimerTest : public testing::Test {
39 public:
40     static void SetUpTestCase(void);
41     static void TearDownTestCase(void);
42     void SetUp();
43     void TearDown();
44 };
45 
SetUpTestCase(void)46 void AssetChangeTimerTest::SetUpTestCase(void)
47 {
48     // input testsuit setup step,setup invoked before all testcases
49 }
50 
TearDownTestCase(void)51 void AssetChangeTimerTest::TearDownTestCase(void)
52 {
53     // input testsuit teardown step,teardown invoked after all testcases
54 }
55 
SetUp(void)56 void AssetChangeTimerTest::SetUp(void)
57 {
58     // input testcase setup step,setup invoked before each testcases
59 }
60 
TearDown(void)61 void AssetChangeTimerTest::TearDown(void)
62 {
63     // input testcase teardown step,teardown invoked after each testcases
64 }
65 
66 /**
67  * @tc.name: StartTimer_001
68  * @tc.desc: Normal test for StartTimer
69  * @tc.type: FUNC
70  */
71 
72 HWTEST_F(AssetChangeTimerTest, StartTimer_001, TestSize.Level1)
73 {
74     string sessionId = "sessionId1";
75     string assetKey = "assetKey1";
76     std::weak_ptr<JSWatcher> watcher;
77     const std::string bundleName = "bundleName";
78     std::shared_ptr<WatcherImpl> watcherImpl = std::make_shared<WatcherImpl>(watcher);
79     FlatObjectStore *flatObjectStore = nullptr;
80     AssetChangeTimer assetChangeTimer(flatObjectStore);
81     assetChangeTimer.StartTimer(sessionId, assetKey, watcherImpl);
82     EXPECT_EQ(assetChangeTimer.assetChangeTasks_[sessionId + ASSET_SEPARATOR + assetKey],
83         assetChangeTimer.executor_->Reset(assetChangeTimer.assetChangeTasks_[sessionId +
84         ASSET_SEPARATOR + assetKey], std::chrono::milliseconds(WAIT_INTERVAL)));
85 }
86 
87 /**
88  * @tc.name: StopTimer_001
89  * @tc.desc: Normal test for StopTimer
90  * @tc.type: FUNC
91  */
92 
93 HWTEST_F(AssetChangeTimerTest, StopTimer_001, TestSize.Level1)
94 {
95     string sessionId = "sessionId2";
96     string assetKey = "assetKey2";
97     FlatObjectStore *flatObjectStore = nullptr;
98     AssetChangeTimer assetChangeTimer(flatObjectStore);
99     std::shared_ptr<ExecutorPool> executor_ = std::make_shared<ExecutorPool>(2, 3);
100     assetChangeTimer.StopTimer(sessionId, assetKey);
101     EXPECT_TRUE(assetChangeTimer.assetChangeTasks_.empty());
102 }
103 
104 /**
105  * @tc.name: GetInstance_001
106  * @tc.desc: Normal test for GetInstance
107  * @tc.type: FUNC
108  */
109 
110 HWTEST_F(AssetChangeTimerTest, GetInstance_001, TestSize.Level1)
111 {
112     FlatObjectStore *flatObjectStore = nullptr;
113     AssetChangeTimer assetChangeTimer(flatObjectStore);
114     auto ret = assetChangeTimer.GetInstance(flatObjectStore);
115     EXPECT_NE(ret, nullptr);
116 }
117 
118 /**
119  * @tc.name: OnAssetChanged_001
120  * @tc.desc: Normal test for OnAssetChanged
121  * @tc.type: FUNC
122  */
123 
124 HWTEST_F(AssetChangeTimerTest, OnAssetChanged_001, TestSize.Level1)
125 {
126     string sessionId = "sessionId1";
127     string assetKey = "assetKey1";
128     std::weak_ptr<JSWatcher> watcher;
129     const std::string bundleName = "bundleName";
130     std::shared_ptr<WatcherImpl> watcherImpl = std::make_shared<WatcherImpl>(watcher);
131     FlatObjectStore *flatObjectStore = nullptr;
132     AssetChangeTimer assetChangeTimer(flatObjectStore);
133     assetChangeTimer.OnAssetChanged(sessionId, assetKey, watcherImpl);
134     EXPECT_EQ(assetChangeTimer.assetChangeTasks_[sessionId + ASSET_SEPARATOR + assetKey],
135         assetChangeTimer.executor_->Reset(assetChangeTimer.assetChangeTasks_[sessionId +
136         ASSET_SEPARATOR + assetKey], std::chrono::milliseconds(WAIT_INTERVAL)));
137 }
138 
139 /**
140  * @tc.name: HandleAssetChanges_001
141  * @tc.desc: Abnormal test for HandleAssetChanges, no sessionid is stored in delegates_
142  * @tc.type: FUNC
143  */
144 
145 HWTEST_F(AssetChangeTimerTest, HandleAssetChanges_001, TestSize.Level1)
146 {
147     string sessionId = "sessionId1";
148     string assetKey = "assetKey1";
149     std::string bundleName = "default";
150     FlatObjectStore *flatObjectStore = new FlatObjectStore(bundleName);
151     AssetChangeTimer assetChangeTimer(flatObjectStore);
152     uint32_t ret = assetChangeTimer.HandleAssetChanges(sessionId, assetKey);
153     EXPECT_EQ(ret, ERR_DB_GET_FAIL);
154     delete flatObjectStore;
155 }
156 
157 /**
158  * @tc.name: GetAssetValue_001
159  * @tc.desc: Abnormal test for GetAssetValue, no sessionid is stored in delegates_
160  * @tc.type: FUNC
161  */
162 
163 HWTEST_F(AssetChangeTimerTest, GetAssetValue_001, TestSize.Level1)
164 {
165     string sessionId = "sessionId1";
166     string assetKey = "assetKey1";
167     Asset assetValue = {
168             .version = 0,
169             .status = 0,
170             .id = "id",
171             .name = "1.txt",
172             .uri = "file://com.example.myapp/data/dir/1.txt",
173             .createTime = "2024/10/26 19:48:00",
174             .modifyTime = "2024/10/26 20:10:00",
175             .size = "1",
176             .hash = "hash",
177             .path = "/dir/1.txt",
178     };
179     std::string bundleName = "default";
180     FlatObjectStore *flatObjectStore = new FlatObjectStore(bundleName);
181     AssetChangeTimer assetChangeTimer(flatObjectStore);
182     uint32_t ret = assetChangeTimer.GetAssetValue(sessionId, assetKey, assetValue);
183     EXPECT_FALSE(ret);
184     delete flatObjectStore;
185 }
186 }
187