1 /*
2 * Copyright (c) 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 #define private public
17 #define protected public
18
19 #include <string>
20
21 #include <gtest/gtest.h>
22 #include "system_ability_definition.h"
23
24 #include "bundle_active_power_state_callback_proxy.h"
25 #include "bundle_active_stub.h"
26 #include "bundle_active_core.h"
27 #include "bundle_active_power_state_callback_stub.h"
28 #include "bundle_active_continuous_task_observer.h"
29 #include "bundle_active_bundle_mgr_helper.h"
30 #include "bundle_active_app_state_observer.h"
31 #include "bundle_active_usage_database.h"
32 #include "bundle_active_account_helper.h"
33 #include "bundle_active_power_state_callback_service.h"
34 #include "bundle_active_open_callback.h"
35 #include "bundle_active_shutdown_callback_service.h"
36 #include "bundle_active_binary_search.h"
37 #include "bundle_active_debug_mode.h"
38
39 using namespace testing::ext;
40
41 namespace OHOS {
42 namespace DeviceUsageStats {
43
44 class BundleActiveTotalTest : public testing::Test {
45 public:
46 static void SetUpTestCase(void);
47 static void TearDownTestCase(void);
48 void SetUp();
49 void TearDown();
50 };
51
SetUpTestCase(void)52 void BundleActiveTotalTest::SetUpTestCase(void)
53 {
54 }
55
TearDownTestCase(void)56 void BundleActiveTotalTest::TearDownTestCase(void)
57 {
58 }
59
SetUp(void)60 void BundleActiveTotalTest::SetUp(void)
61 {
62 }
63
TearDown(void)64 void BundleActiveTotalTest::TearDown(void)
65 {
66 }
67
68 /*
69 * @tc.name: BundleActiveAccountHelperTest_001
70 * @tc.desc: test the interface of bundle_active_account_helper
71 * @tc.type: FUNC
72 * @tc.require: DTS2023121404861
73 */
74 HWTEST_F(BundleActiveTotalTest, BundleActiveAccountHelperTest_001, Function | MediumTest | Level0)
75 {
76 std::vector<int32_t> activatedOsAccountIds;
77 BundleActiveAccountHelper::GetActiveUserId(activatedOsAccountIds);
78 }
79
80 /*
81 * @tc.name: BundleActiveContinuousTaskObserverTest_001
82 * @tc.desc: test the interface of bundle_active_continuous_task_observer
83 * @tc.type: FUNC
84 * @tc.require: DTS2023121404861
85 */
86 HWTEST_F(BundleActiveTotalTest, BundleActiveContinuousTaskObserverTest_001, Function | MediumTest | Level0)
87 {
88 #ifdef BGTASKMGR_ENABLE
89 #ifdef OS_ACCOUNT_PART_ENABLED
90 std::shared_ptr<AppExecFwk::EventRunner> runner;
91 auto reportHandler = std::make_shared<BundleActiveReportHandler>(runner);
92 auto reportHandler1 = std::make_shared<BundleActiveReportHandler>(runner);
93 BundleActiveContinuousTaskObserver test;
94 test.Init(reportHandler);
95 test.Init(reportHandler1);
96
97 std::shared_ptr<OHOS::BackgroundTaskMgr::ContinuousTaskCallbackInfo> continuousTaskCallbackInfo;
98 test.OnContinuousTaskStart(continuousTaskCallbackInfo);
99
100 test.ReportContinuousTaskEvent(continuousTaskCallbackInfo, true);
101 test.ReportContinuousTaskEvent(continuousTaskCallbackInfo, false);
102
103 test.GetBundleMgr();
104 test.bundleMgr_ = nullptr;
105 test.GetBundleMgr();
106 #endif
107 #endif
108 }
109
110 /*
111 * @tc.name: BundleActiveBundleMgrHelperTest_001
112 * @tc.desc: test the interface of bundle_active_bundle_mgr_helper
113 * @tc.type: FUNC
114 * @tc.require: DTS2023121404861
115 */
116 HWTEST_F(BundleActiveTotalTest, BundleActiveBundleMgrHelperTest_001, Function | MediumTest | Level0)
117 {
118 AppExecFwk::ApplicationFlag flag = AppExecFwk::GET_BASIC_APPLICATION_INFO;
119 AppExecFwk::ApplicationInfo appInfo;
120 AppExecFwk::BundleInfo bundleInfo;
121 BundleActiveBundleMgrHelper test;
122
123 std::string string = "test";
124 test.GetNameForUid(0, string);
125 test.GetApplicationInfo(string, flag, 0, appInfo);
126 test.GetBundleInfo(string, AppExecFwk::BundleFlag::GET_BUNDLE_WITH_EXTENSION_INFO, bundleInfo, 0);
127
128 test.bundleMgr_ = nullptr;
129 test.GetNameForUid(0, string);
130 test.GetApplicationInfo(string, flag, 0, appInfo);
131 test.GetBundleInfo(string, AppExecFwk::BundleFlag::GET_BUNDLE_WITH_EXTENSION_INFO, bundleInfo, 0);
132 }
133
134 /*
135 * @tc.name: BundleActiveAppStateObserverTest_001
136 * @tc.desc: test the interface of bundle_active_app_state_observer
137 * @tc.type: FUNC
138 * @tc.require: DTS2023121404861
139 */
140 HWTEST_F(BundleActiveTotalTest, BundleActiveAppStateObserverTest_001, Function | MediumTest | Level0)
141 {
142 AbilityStateData abilityStateData;
143 std::shared_ptr<AppExecFwk::EventRunner> runner;
144 auto reportHandler = std::make_shared<BundleActiveReportHandler>(runner);
145 BundleActiveAppStateObserver test;
146 test.Init(reportHandler);
147 test.OnAbilityStateChanged(abilityStateData);
148
149 test.Init(reportHandler);
150 abilityStateData.abilityState = static_cast<int32_t>(AppExecFwk::AbilityState::ABILITY_STATE_FOREGROUND);
151 test.OnAbilityStateChanged(abilityStateData);
152
153 abilityStateData.abilityState = static_cast<int32_t>(AppExecFwk::AbilityState::ABILITY_STATE_BACKGROUND);
154 test.OnAbilityStateChanged(abilityStateData);
155
156 abilityStateData.abilityState = static_cast<int32_t>(AppExecFwk::AbilityState::ABILITY_STATE_TERMINATED);
157 test.OnAbilityStateChanged(abilityStateData);
158 }
159
160 /*
161 * @tc.name: BundleActiveUsageDatabaseTest_001
162 * @tc.desc: test the interface of bundle_active_usage_database
163 * @tc.type: FUNC
164 * @tc.require: DTS2023121404861
165 */
166 HWTEST_F(BundleActiveTotalTest, BundleActiveUsageDatabaseTest_001, Function | MediumTest | Level0)
167 {
168 BundleActiveUsageDatabase test;
169 test.InitDatabaseTableInfo(test.ParseStartTime(test.eventTableName_)-1);
170 }
171
172 /*
173 * @tc.name: BundleActiveUsageDatabaseTest_002
174 * @tc.desc: test the interface of bundle_active_usage_database
175 * @tc.type: FUNC
176 * @tc.require: DTS2023121404861
177 */
178 HWTEST_F(BundleActiveTotalTest, BundleActiveUsageDatabaseTest_002, Function | MediumTest | Level0)
179 {
180 BundleActiveUsageDatabase test;
181 test.debugDatabase_ = true;
182 test.DeleteExcessiveTableData(100);
183 test.DeleteExcessiveTableData(0);
184 }
185
186 /*
187 * @tc.name: BundleActiveUsageDatabaseTest_003
188 * @tc.desc: test the interface of bundle_active_usage_database
189 * @tc.type: FUNC
190 * @tc.require: DTS2023121404861
191 */
192 HWTEST_F(BundleActiveTotalTest, BundleActiveUsageDatabaseTest_003, Function | MediumTest | Level0)
193 {
194 BundleActiveUsageDatabase test;
195 test.GetOverdueTableCreateTime(100, 0);
196 }
197
198 /*
199 * @tc.name: BundleActiveUsageDatabaseTest_005
200 * @tc.desc: test the interface of bundle_active_usage_database
201 * @tc.type: FUNC
202 * @tc.require: DTS2023121404861
203 */
204 HWTEST_F(BundleActiveTotalTest, BundleActiveUsageDatabaseTest_005, Function | MediumTest | Level0)
205 {
206 BundleActiveUsageDatabase test;
207 test.bundleHistoryTableName_ = "unknownTableName";
208 std::shared_ptr<map<string, std::shared_ptr<BundleActivePackageHistory>>> userHistory;
209 test.PutBundleHistoryData(0, userHistory);
210 test.GetTableIndexSql(0, 0, true, 0);
211 test.GetTableIndexSql(5, 0, true, 0);
212 test.GetTableIndexSql(5, 0, false, 0);
213 test.GetTableIndexSql(5, 0, false, 1);
214 test.GetTableIndexSql(5, 0, false, 2);
215 test.GetTableIndexSql(0, 0, false, 0);
216 }
217
218 /*
219 * @tc.name: BundleActiveUsageDatabaseTest_006
220 * @tc.desc: test the interface of bundle_active_usage_database
221 * @tc.type: FUNC
222 * @tc.require: DTS2023121404861
223 */
224 HWTEST_F(BundleActiveTotalTest, BundleActiveUsageDatabaseTest_006, Function | MediumTest | Level0)
225 {
226 BundleActiveUsageDatabase test;
227 std::shared_ptr<NativeRdb::RdbStore> rdbStore;
228 test.SetNewIndexWhenTimeChanged(5, 0, 0, rdbStore);
229 test.RenameTableName(0, 0, 0);
230 test.ExecuteRenameTableName("test", 0, 0, rdbStore);
231 }
232
233 /*
234 * @tc.name: BundleActiveUsageDatabaseTest_007
235 * @tc.desc: test the interface of bundle_active_usage_database
236 * @tc.type: FUNC
237 * @tc.require: DTS2023121404861
238 */
239 HWTEST_F(BundleActiveTotalTest, BundleActiveUsageDatabaseTest_007, Function | MediumTest | Level0)
240 {
241 BundleActiveUsageDatabase test;
242 std::shared_ptr<NativeRdb::RdbStore> rdbStore;
243 BundleActiveFormRecord formRecord;
244 test.UpdateFormData(0, "test", "test", formRecord, rdbStore);
245 test.GetSystemEventName(0);
246 test.JudgeQueryCondition(0, 0, 1);
247 }
248
249 /*
250 * @tc.name: BundleActivePowerStateCallbackServiceTest_001
251 * @tc.desc: test the interface of bundle_active_usage_database
252 * @tc.type: FUNC
253 * @tc.require: DTS2023121404861
254 */
255 HWTEST_F(BundleActiveTotalTest, BundleActivePowerStateCallbackServiceTest_001, Function | MediumTest | Level0)
256 {
257 #ifdef DEVICE_USAGES_STATISTICS_POWERMANGER_ENABLE
258 BundleActivePowerStateCallbackService test1(nullptr);
259
260 std::shared_ptr<BundleActiveCore> bundleActiveCore = std::make_shared<BundleActiveCore>();
261 BundleActivePowerStateCallbackService test2(bundleActiveCore);
262
263 test2.OnPowerStateChanged(PowerState::AWAKE);
264 test2.OnPowerStateChanged(PowerState::SLEEP);
265 #endif
266 }
267
268 /*
269 * @tc.name: BundleActiveBinarySearchTest_001
270 * @tc.desc: test the interface of bundle_active_binary_search
271 * @tc.type: FUNC
272 * @tc.require: DTS2023121404861
273 */
274 HWTEST_F(BundleActiveTotalTest, BundleActiveBinarySearchTest_001, Function | MediumTest | Level0)
275 {
276 std::vector<int64_t> tableNameArray;
277 BundleActiveBinarySearch test;
278 test.BinarySearch(tableNameArray, 0);
279 test.BinarySearch(tableNameArray, -100);
280 }
281 }
282 }