1 /*
2  * Copyright (c) 2022-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 <string>
17 #include <thread>
18 
19 #include <gtest/gtest.h>
20 #include "system_ability_definition.h"
21 
22 #include "bundle_active_service.h"
23 #include "bundle_active_user_service.h"
24 #include "bundle_active_event.h"
25 #include "app_group_callback_stub.h"
26 #include "app_group_callback_info.h"
27 #include "bundle_active_usage_database.h"
28 #include "bundle_active_user_history.h"
29 #include "bundle_active_group_controller.h"
30 #include "bundle_active_log.h"
31 #include "bundle_active_config_reader.h"
32 #include "accesstoken_kit.h"
33 #include "token_setproc.h"
34 #include "nativetoken_kit.h"
35 
36 using namespace testing::ext;
37 
38 namespace OHOS {
39 namespace DeviceUsageStats {
40 using namespace Security::AccessToken;
41 class DeviceUsageStatisticsServiceTest : public testing::Test {
42 public:
43     static void SetUpTestCase(void);
44     static void TearDownTestCase(void);
45     void SetUp();
46     void TearDown();
47     static std::shared_ptr<BundleActiveCore> bundleActiveCore_;
48 };
49 
50 std::shared_ptr<BundleActiveCore> DeviceUsageStatisticsServiceTest::bundleActiveCore_ = nullptr;
51 
SetUpTestCase(void)52 void DeviceUsageStatisticsServiceTest::SetUpTestCase(void)
53 {
54     static const char *perms[] = {
55         "ohos.permission.BUNDLE_ACTIVE_INFO",
56     };
57     uint64_t tokenId;
58     NativeTokenInfoParams infoInstance = {
59         .dcapsNum = 0,
60         .permsNum = 1,
61         .aclsNum = 0,
62         .dcaps = nullptr,
63         .perms = perms,
64         .acls = nullptr,
65         .processName = "DeviceUsageStatisticsServiceTest",
66         .aplStr = "system_core",
67     };
68     tokenId = GetAccessTokenId(&infoInstance);
69     SetSelfTokenID(tokenId);
70     AccessTokenKit::ReloadNativeTokenInfo();
71     bundleActiveCore_ = std::make_shared<BundleActiveCore>();
72     bundleActiveCore_->Init();
73     bundleActiveCore_->InitBundleGroupController();
74 }
75 
TearDownTestCase(void)76 void DeviceUsageStatisticsServiceTest::TearDownTestCase(void)
77 {
78     bundleActiveCore_->bundleGroupHandler_->ffrtQueue_.reset();
79     int64_t sleepTime = 10;
80     std::this_thread::sleep_for(std::chrono::seconds(sleepTime));
81 }
82 
SetUp(void)83 void DeviceUsageStatisticsServiceTest::SetUp(void)
84 {
85 }
86 
TearDown(void)87 void DeviceUsageStatisticsServiceTest::TearDown(void)
88 {
89     int64_t sleepTime = 300;
90     std::this_thread::sleep_for(std::chrono::milliseconds(sleepTime));
91 }
92 
93 class TestServiceAppGroupChangeCallback : public AppGroupCallbackStub {
94 public:
95     void OnAppGroupChanged(const AppGroupCallbackInfo &appGroupCallbackInfo) override;
96 };
97 
OnAppGroupChanged(const AppGroupCallbackInfo & appGroupCallbackInfo)98 void TestServiceAppGroupChangeCallback::OnAppGroupChanged(const AppGroupCallbackInfo &appGroupCallbackInfo)
99 {
100     BUNDLE_ACTIVE_LOGI("TestServiceAppGroupChangeCallback::OnAppGroupChanged!");
101 }
102 
103 /*
104  * @tc.name: DeviceUsageStatisticsServiceTest_GetServiceObject_001
105  * @tc.desc: get service object
106  * @tc.type: FUNC
107  * @tc.require: issuesI5SOZY
108  */
109 HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_GetServiceObject_001,
110     Function | MediumTest | Level0)
111 {
112     sptr<ISystemAbilityManager> systemAbilityManager =
113         SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
114     EXPECT_NE(systemAbilityManager, nullptr);
115 
116     sptr<IRemoteObject> remoteObject =
117         systemAbilityManager->GetSystemAbility(BACKGROUND_TASK_MANAGER_SERVICE_ID);
118     EXPECT_NE(remoteObject, nullptr);
119 }
120 
121 /*
122  * @tc.name: DeviceUsageStatisticsServiceTest_dump_001
123  * @tc.desc: test dump
124  * @tc.type: FUNC
125  * @tc.require: issuesI5SOZY
126  */
127 HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_dump_001, Function | MediumTest | Level0)
128 {
129     DelayedSingleton<BundleActiveService>::GetInstance()->bundleActiveCore_ = std::make_shared<BundleActiveCore>();
130     DelayedSingleton<BundleActiveService>::GetInstance()->bundleActiveCore_->Init();
131     BUNDLE_ACTIVE_LOGI("DeviceUsageStatisticsServiceTest create BundleActiveService!");
132 
133     std::vector<std::string> dumpOption{"-A", "Events"};
134     std::vector<std::string> dumpInfo;
135     DelayedSingleton<BundleActiveService>::GetInstance()->ShellDump(dumpOption, dumpInfo);
136 
137     dumpOption.clear();
138     dumpInfo.clear();
139     dumpOption = {"-A", "Events", "0", "20000000000000", "100"};
140     DelayedSingleton<BundleActiveService>::GetInstance()->ShellDump(dumpOption, dumpInfo);
141 
142     dumpOption.clear();
143     dumpInfo.clear();
144     dumpOption = {"-A", "PackageUsage"};
145     DelayedSingleton<BundleActiveService>::GetInstance()->ShellDump(dumpOption, dumpInfo);
146 
147     dumpOption.clear();
148     dumpInfo.clear();
149     dumpOption = {"-A", "PackageUsage", "1", "0", "20000000000000", "100"};
150     DelayedSingleton<BundleActiveService>::GetInstance()->ShellDump(dumpOption, dumpInfo);
151 
152     dumpOption.clear();
153     dumpInfo.clear();
154     dumpOption = {"-A", "ModuleUsage"};
155     DelayedSingleton<BundleActiveService>::GetInstance()->ShellDump(dumpOption, dumpInfo);
156 
157     dumpOption.clear();
158     dumpInfo.clear();
159     dumpOption = {"-A", "ModuleUsage", "1", "100"};
160     DelayedSingleton<BundleActiveService>::GetInstance()->ShellDump(dumpOption, dumpInfo);
161 
162     std::vector<std::u16string> args;
163     DelayedSingleton<BundleActiveService>::GetInstance()->Dump(-1, args);
164 
165     args.clear();
166     args = {to_utf16("-h")};
167     DelayedSingleton<BundleActiveService>::GetInstance()->Dump(-1, args);
168 
169     args.clear();
170     args = {to_utf16("-A")};
171     DelayedSingleton<BundleActiveService>::GetInstance()->Dump(-1, args);
172 
173     args.clear();
174     args = {to_utf16("-D")};
175     DelayedSingleton<BundleActiveService>::GetInstance()->Dump(-1, args);
176 }
177 
178 /*
179  * @tc.name: DeviceUsageStatisticsServiceTest_QueryModuleUsageRecords_001
180  * @tc.desc: QueryModuleUsageRecords
181  * @tc.type: FUNC
182  * @tc.require: issuesI5SOZY
183  */
184 HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_QueryModuleUsageRecords_001,
185     Function | MediumTest | Level0)
186 {
187     std::vector<BundleActiveModuleRecord> results;
188     int32_t maxNum = 0;
189     ErrCode code = DelayedSingleton<BundleActiveService>::GetInstance()->QueryModuleUsageRecords(maxNum, results, 100);
190     EXPECT_NE(code, 0);
191 
192     maxNum = 1001;
193     code = DelayedSingleton<BundleActiveService>::GetInstance()->QueryModuleUsageRecords(maxNum, results, 100);
194     EXPECT_NE(code, 0);
195 }
196 
197 /*
198  * @tc.name: DeviceUsageStatisticsServiceTest_AppGroupCallback_001
199  * @tc.desc: AppGroupCallback
200  * @tc.type: FUNC
201  * @tc.require: issuesI5SOZY
202  */
203 HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_AppGroupCallback_001,
204     Function | MediumTest | Level0)
205 {
206     BUNDLE_ACTIVE_LOGI("DeviceUsageStatisticsServiceTest create BundleActiveService!");
207     sptr<TestServiceAppGroupChangeCallback> observer = new (std::nothrow) TestServiceAppGroupChangeCallback();
208     DelayedSingleton<BundleActiveService>::GetInstance()->bundleActiveCore_ = std::make_shared<BundleActiveCore>();
209     EXPECT_EQ(DelayedSingleton<BundleActiveService>::GetInstance()->RegisterAppGroupCallBack(observer), ERR_OK);
210     EXPECT_NE(DelayedSingleton<BundleActiveService>::GetInstance()->RegisterAppGroupCallBack(observer), ERR_OK);
211     DelayedSingleton<BundleActiveService>::GetInstance()->bundleActiveCore_->AddObserverDeathRecipient(observer);
212 
213     EXPECT_EQ(DelayedSingleton<BundleActiveService>::GetInstance()->UnRegisterAppGroupCallBack(observer), ERR_OK);
214     EXPECT_NE(DelayedSingleton<BundleActiveService>::GetInstance()->UnRegisterAppGroupCallBack(observer), ERR_OK);
215 
216     observer = nullptr;
217     EXPECT_NE(DelayedSingleton<BundleActiveService>::GetInstance()->RegisterAppGroupCallBack(observer), ERR_OK);
218 
219     DelayedSingleton<BundleActiveService>::GetInstance()->bundleActiveCore_->AddObserverDeathRecipient(observer);
220     DelayedSingleton<BundleActiveService>::GetInstance()->bundleActiveCore_->RemoveObserverDeathRecipient(observer);
221 
222     wptr<IRemoteObject> remote = nullptr;
223     DelayedSingleton<BundleActiveService>::GetInstance()->bundleActiveCore_->OnObserverDied(remote);
224     DelayedSingleton<BundleActiveService>::GetInstance()->bundleActiveCore_->OnObserverDiedInner(remote);
225 }
226 
227 /*
228  * @tc.name: DeviceUsageStatisticsServiceTest_AppGroupCallback_002
229  * @tc.desc: AppGroupCallback
230  * @tc.type: FUNC
231  * @tc.require: issuesI5SOZY
232  */
233 HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_AppGroupCallback_002,
234     Function | MediumTest | Level0)
235 {
236     sptr<TestServiceAppGroupChangeCallback> observer = new (std::nothrow) TestServiceAppGroupChangeCallback();
237     Security::AccessToken::AccessTokenID tokenId {};
238     bundleActiveCore_->groupChangeObservers_[tokenId] = observer;
239     int32_t userId = 100;
240     int32_t newGroup = 10;
241     int32_t oldGroup = 60;
242     int32_t reasonInGroup = 0;
243     AppGroupCallbackInfo appGroupCallbackInfo(userId, newGroup, oldGroup, reasonInGroup, "test");
244     bundleActiveCore_->OnAppGroupChanged(appGroupCallbackInfo);
245     SUCCEED();
246 }
247 
248 /*
249  * @tc.name: DeviceUsageStatisticsServiceTest_OnUserRemoved_001
250  * @tc.desc: OnUserRemoved
251  * @tc.type: FUNC
252  * @tc.require: issuesI5SOZY
253  */
254 HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_OnUserRemoved_001,
255     Function | MediumTest | Level0)
256 {
257     int userId = 100;
258     auto coreObject = bundleActiveCore_;
259     coreObject->RestoreToDatabase(userId);
260     auto userService = std::make_shared<BundleActiveUserService>(userId, *(coreObject.get()), false);
261     coreObject->userStatServices_[userId] = userService;
262 
263     BundleActiveEvent event;
264     coreObject->ReportEventToAllUserId(event);
265     coreObject->currentUsedUser_ = userId;
266     coreObject->OnUserRemoved(userId);
267     coreObject->OnUserSwitched(userId);
268     EXPECT_NE(coreObject, nullptr);
269 }
270 
271 /*
272  * @tc.name: DeviceUsageStatisticsServiceTest_RestoreAllData_001
273  * @tc.desc: RestoreAllData
274  * @tc.type: FUNC
275  * @tc.require: issuesI5SOZY
276  */
277 HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_RestoreAllData_001,
278     Function | MediumTest | Level0)
279 {
280     auto coreObject = bundleActiveCore_;
281     int userId = 100;
282     auto userService = std::make_shared<BundleActiveUserService>(userId, *(coreObject.get()), false);
283     int64_t timeStamp = 20000000000000;
284     userService->Init(timeStamp);
285     coreObject->userStatServices_[userId] = userService;
286     userId = 101;
287     coreObject->userStatServices_[userId] = nullptr;
288     coreObject->RestoreAllData();
289 
290     BundleActiveEvent event;
291     coreObject->ReportEventToAllUserId(event);
292     EXPECT_NE(coreObject, nullptr);
293 }
294 
295 /*
296  * @tc.name: DeviceUsageStatisticsServiceTest_ObtainSystemEventName_001
297  * @tc.desc: ObtainSystemEventName
298  * @tc.type: FUNC
299  * @tc.require: issuesI5SOZY
300  */
301 HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_ObtainSystemEventName_001,
302     Function | MediumTest | Level0)
303 {
304     auto coreObject = bundleActiveCore_;
305     BundleActiveEvent event;
306     event.eventId_ = BundleActiveEvent::SYSTEM_LOCK;
307     coreObject->ObtainSystemEventName(event);
308 
309     event.eventId_ = BundleActiveEvent::SYSTEM_UNLOCK;
310     coreObject->ObtainSystemEventName(event);
311 
312     event.eventId_ = BundleActiveEvent::SYSTEM_SLEEP;
313     coreObject->ObtainSystemEventName(event);
314 
315     event.eventId_ = BundleActiveEvent::SYSTEM_WAKEUP;
316     coreObject->ObtainSystemEventName(event);
317     EXPECT_NE(coreObject, nullptr);
318 }
319 
320 /*
321  * @tc.name: DeviceUsageStatisticsServiceTest_JudgeQueryCondition_001
322  * @tc.desc: JudgeQueryCondition
323  * @tc.type: FUNC
324  * @tc.require: issuesI5SOZY
325  */
326 HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_JudgeQueryCondition_001,
327     Function | MediumTest | Level0)
328 {
329     auto database = std::make_shared<BundleActiveUsageDatabase>();
330     int64_t beginTime = 0;
331     int64_t endTime = 0;
332     int64_t eventTableTime = 0;
333     database->eventTableName_ = "defaultTableName";
334     EXPECT_EQ(database->JudgeQueryCondition(beginTime, endTime, eventTableTime), QUERY_CONDITION_INVALID);
335 
336     endTime = 10;
337     eventTableTime = 11;
338     EXPECT_EQ(database->JudgeQueryCondition(beginTime, endTime, eventTableTime), QUERY_CONDITION_INVALID);
339 }
340 
341 /*
342  * @tc.name: DeviceUsageStatisticsServiceTest_GetSystemEventName_001
343  * @tc.desc: GetSystemEventName
344  * @tc.type: FUNC
345  * @tc.require: issuesI5SOZY
346  */
347 HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_GetSystemEventName_001,
348     Function | MediumTest | Level0)
349 {
350     auto database = std::make_shared<BundleActiveUsageDatabase>();
351     int32_t userId = BundleActiveEvent::SYSTEM_LOCK;
352     EXPECT_EQ(database->GetSystemEventName(userId), "SYSTEM_LOCK");
353 
354     userId = BundleActiveEvent::SYSTEM_UNLOCK;
355     EXPECT_EQ(database->GetSystemEventName(userId), "SYSTEM_UNLOCK");
356 
357     userId = BundleActiveEvent::SYSTEM_SLEEP;
358     EXPECT_EQ(database->GetSystemEventName(userId), "SYSTEM_SLEEP");
359 
360     userId = BundleActiveEvent::SYSTEM_WAKEUP;
361     EXPECT_EQ(database->GetSystemEventName(userId), "SYSTEM_WAKEUP");
362 
363     userId = BundleActiveEvent::ABILITY_FOREGROUND;
364     EXPECT_EQ(database->GetSystemEventName(userId), "");
365 }
366 
367 /*
368  * @tc.name: DeviceUsageStatisticsServiceTest_GetOverdueTableCreateTime_001
369  * @tc.desc: GetOverdueTableCreateTime
370  * @tc.type: FUNC
371  * @tc.require: issuesI5SOZY
372  */
373 HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_GetOverdueTableCreateTime_001,
374     Function | MediumTest | Level0)
375 {
376     auto database = std::make_shared<BundleActiveUsageDatabase>();
377     uint32_t databaseType = 4;
378     int64_t currentTimeMillis = 20000000000000;
379     database->GetOverdueTableCreateTime(databaseType, currentTimeMillis);
380 
381     databaseType = 0;
382     database->GetOverdueTableCreateTime(databaseType, currentTimeMillis);
383     EXPECT_NE(database, nullptr);
384 }
385 
386 /*
387  * @tc.name: DeviceUsageStatisticsServiceTest_DeleteInvalidTable_001
388  * @tc.desc: DeleteInvalidTable
389  * @tc.type: FUNC
390  * @tc.require: issuesI5SOZY
391  */
392 HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_DeleteInvalidTable_001,
393     Function | MediumTest | Level0)
394 {
395     auto database = std::make_shared<BundleActiveUsageDatabase>();
396     uint32_t databaseType = 4;
397     int64_t currentTimeMillis = 20000000000000;
398     database->DeleteInvalidTable(databaseType, currentTimeMillis);
399 
400     databaseType = 0;
401     database->DeleteInvalidTable(databaseType, currentTimeMillis);
402     EXPECT_NE(database, nullptr);
403 }
404 
405 /*
406  * @tc.name: DeviceUsageStatisticsServiceTest_CreatePackageLogTable_001
407  * @tc.desc: CreatePackageLogTable
408  * @tc.type: FUNC
409  * @tc.require: issuesI5SOZY
410  */
411 HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_CreatePackageLogTable_001,
412     Function | MediumTest | Level0)
413 {
414     auto database = std::make_shared<BundleActiveUsageDatabase>();
415     uint32_t databaseType = 0;
416     int64_t currentTimeMillis = 20000000000000;
417     database->CreatePackageLogTable(databaseType, currentTimeMillis);
418     EXPECT_NE(database, nullptr);
419 }
420 
421 /*
422  * @tc.name: DeviceUsageStatisticsServiceTest_CreateModuleRecordTable_001
423  * @tc.desc: CreateModuleRecordTable
424  * @tc.type: FUNC
425  * @tc.require: issuesI5SOZY
426  */
427 HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_CreateModuleRecordTable_001,
428     Function | MediumTest | Level0)
429 {
430     auto database = std::make_shared<BundleActiveUsageDatabase>();
431     uint32_t databaseType = 0;
432     int64_t currentTimeMillis = 20000000000000;
433     database->CreateModuleRecordTable(databaseType, currentTimeMillis);
434     EXPECT_NE(database, nullptr);
435 }
436 
437 /*
438  * @tc.name: DeviceUsageStatisticsServiceTest_CreateFormRecordTable_001
439  * @tc.desc: CreateFormRecordTable
440  * @tc.type: FUNC
441  * @tc.require: issuesI5SOZY
442  */
443 HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_CreateFormRecordTable_001,
444     Function | MediumTest | Level0)
445 {
446     auto database = std::make_shared<BundleActiveUsageDatabase>();
447     uint32_t databaseType = 0;
448     int64_t currentTimeMillis = 20000000000000;
449     database->CreateFormRecordTable(databaseType, currentTimeMillis);
450     EXPECT_NE(database, nullptr);
451 }
452 
453 /*
454  * @tc.name: DeviceUsageStatisticsServiceTest_CreateDurationTable_001
455  * @tc.desc: CreateDurationTable
456  * @tc.type: FUNC
457  * @tc.require: issuesI5SOZY
458  */
459 HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_CreateDurationTable_001,
460     Function | MediumTest | Level0)
461 {
462     auto database = std::make_shared<BundleActiveUsageDatabase>();
463     int32_t databaseType = DAILY_DATABASE_INDEX;
464     bool forModuleRecords = true;
465     database->InitUsageGroupDatabase(databaseType, forModuleRecords);
466     EXPECT_NE(database, nullptr);
467 }
468 
469 /*
470  * @tc.name: DeviceUsageStatisticsServiceTest_CreateBundleHistoryTable_001
471  * @tc.desc: CreateBundleHistoryTable
472  * @tc.type: FUNC
473  * @tc.require: issuesI5SOZY
474  */
475 HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_CreateBundleHistoryTable_001,
476     Function | MediumTest | Level0)
477 {
478     auto database = std::make_shared<BundleActiveUsageDatabase>();
479     uint32_t databaseType = 0;
480     database->CreateBundleHistoryTable(databaseType);
481     EXPECT_NE(database, nullptr);
482 }
483 
484 /*
485  * @tc.name: DeviceUsageStatisticsServiceTest_PutBundleHistoryData_001
486  * @tc.desc: PutBundleHistoryData
487  * @tc.type: FUNC
488  * @tc.require: issuesI5SOZY
489  */
490 HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_PutBundleHistoryData_001,
491     Function | MediumTest | Level0)
492 {
493     auto database = std::make_shared<BundleActiveUsageDatabase>();
494     int32_t userId = 100;
495     std::shared_ptr<std::map<std::string, std::shared_ptr<BundleActivePackageHistory>>> userHistory = nullptr;
496     database->PutBundleHistoryData(userId, userHistory);
497 
498     userHistory = std::make_shared<std::map<string, std::shared_ptr<BundleActivePackageHistory>>>();
499     userHistory->emplace("defaultTest", std::make_shared<BundleActivePackageHistory>());
500     database->PutBundleHistoryData(userId, userHistory);
501     database->PutBundleHistoryData(userId, userHistory);
502     EXPECT_NE(database, nullptr);
503 }
504 
505 /*
506  * @tc.name: DeviceUsageStatisticsServiceTest_GetBundleHistoryData_001
507  * @tc.desc: GetBundleHistoryData
508  * @tc.type: FUNC
509  * @tc.require: issuesI5SOZY
510  */
511 HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_GetBundleHistoryData_001,
512     Function | MediumTest | Level0)
513 {
514     auto database = std::make_shared<BundleActiveUsageDatabase>();
515     int32_t userId = 100;
516     database->GetBundleHistoryData(userId);
517     EXPECT_NE(database, nullptr);
518 }
519 
520 /*
521  * @tc.name: DeviceUsageStatisticsServiceTest_FlushPackageInfo_001
522  * @tc.desc: FlushPackageInfo
523  * @tc.type: FUNC
524  * @tc.require: issuesI5SOZY
525  */
526 HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_FlushPackageInfo_001,
527     Function | MediumTest | Level0)
528 {
529     auto database = std::make_shared<BundleActiveUsageDatabase>();
530     uint32_t databaseType = 0;
531     BundleActivePeriodStats stats;
532     stats.bundleStats_.emplace("defaultTest", std::make_shared<BundleActivePackageStats>());
533     database->FlushPackageInfo(databaseType, stats);
534     EXPECT_NE(database, nullptr);
535 }
536 
537 /*
538  * @tc.name: DeviceUsageStatisticsServiceTest_GetCurrentUsageData_001
539  * @tc.desc: GetCurrentUsageData
540  * @tc.type: FUNC
541  * @tc.require: issuesI5SOZY
542  */
543 HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_GetCurrentUsageData_001,
544     Function | MediumTest | Level0)
545 {
546     auto database = std::make_shared<BundleActiveUsageDatabase>();
547     int32_t databaseType = -1;
548     int32_t userId = 100;
549     database->GetCurrentUsageData(databaseType, userId);
550 
551     databaseType = 4;
552     database->GetCurrentUsageData(databaseType, userId);
553 
554     databaseType = 0;
555     database->GetCurrentUsageData(databaseType, userId);
556     EXPECT_NE(database, nullptr);
557 }
558 
559 /*
560  * @tc.name: DeviceUsageStatisticsServiceTest_GetTableIndexSql_001
561  * @tc.desc: GetTableIndexSql
562  * @tc.type: FUNC
563  * @tc.require: issuesI5SOZY
564  */
565 HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_GetTableIndexSql_001,
566     Function | MediumTest | Level0)
567 {
568     auto database = std::make_shared<BundleActiveUsageDatabase>();
569     uint32_t databaseType = 0;
570     int64_t tableTime = 20000000000000;
571     bool createFlag = false;
572     int32_t indexFlag = 0;
573     database->GetTableIndexSql(databaseType, tableTime, createFlag, indexFlag);
574 
575     createFlag = true;
576     database->GetTableIndexSql(databaseType, tableTime, createFlag, indexFlag);
577 
578     databaseType = 4;
579     createFlag = false;
580     database->GetTableIndexSql(databaseType, tableTime, createFlag, indexFlag);
581 
582     databaseType = 5;
583     createFlag = false;
584     database->GetTableIndexSql(databaseType, tableTime, createFlag, indexFlag);
585     indexFlag = BUNDLE_ACTIVE_DB_INDEX_MODULE;
586     database->GetTableIndexSql(databaseType, tableTime, createFlag, indexFlag);
587 
588     createFlag = true;
589     indexFlag = BUNDLE_ACTIVE_DB_INDEX_NORMAL;
590     database->GetTableIndexSql(databaseType, tableTime, createFlag, indexFlag);
591 
592     indexFlag = BUNDLE_ACTIVE_DB_INDEX_MODULE;
593     database->GetTableIndexSql(databaseType, tableTime, createFlag, indexFlag);
594     EXPECT_NE(database, nullptr);
595 }
596 
597 /*
598  * @tc.name: DeviceUsageStatisticsServiceTest_RenameTableName_001
599  * @tc.desc: RenameTableName
600  * @tc.type: FUNC
601  * @tc.require: issuesI5SOZY
602  */
603 HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_RenameTableName_001,
604     Function | MediumTest | Level0)
605 {
606     auto database = std::make_shared<BundleActiveUsageDatabase>();
607     uint32_t databaseType = 0;
608     int64_t tableOldTime = 0;
609     int64_t tableNewTime = 20000000000000;
610     database->RenameTableName(databaseType, tableOldTime, tableNewTime);
611 
612     databaseType = 4;
613     database->RenameTableName(databaseType, tableOldTime, tableNewTime);
614 
615     databaseType = 5;
616     database->RenameTableName(databaseType, tableOldTime, tableNewTime);
617     EXPECT_NE(database, nullptr);
618 }
619 
620 /*
621  * @tc.name: DeviceUsageStatisticsServiceTest_RemoveOldData_001
622  * @tc.desc: RemoveOldData
623  * @tc.type: FUNC
624  * @tc.require: issuesI5SOZY
625  */
626 HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_RemoveOldData_001,
627     Function | MediumTest | Level0)
628 {
629     auto database = std::make_shared<BundleActiveUsageDatabase>();
630     int64_t currentTime = 20000000000000;
631     database->RemoveOldData(currentTime);
632     EXPECT_NE(database, nullptr);
633 }
634 
635 /*
636  * @tc.name: DeviceUsageStatisticsServiceTest_QueryDatabaseUsageStats_001
637  * @tc.desc: QueryDatabaseUsageStats
638  * @tc.type: FUNC
639  * @tc.require: issuesI5SOZY
640  */
641 HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_QueryDatabaseUsageStats_001,
642     Function | MediumTest | Level0)
643 {
644     auto database = std::make_shared<BundleActiveUsageDatabase>();
645     int32_t databaseType = -1;
646     int64_t beginTime = 0;
647     int64_t endTime = 0;
648     int32_t userId = 100;
649     EXPECT_EQ(database->QueryDatabaseUsageStats(databaseType, beginTime, endTime, userId).size(), 0);
650 
651     databaseType = 4;
652     EXPECT_EQ(database->QueryDatabaseUsageStats(databaseType, beginTime, endTime, userId).size(), 0);
653 
654     databaseType = 0;
655     EXPECT_EQ(database->QueryDatabaseUsageStats(databaseType, beginTime, endTime, userId).size(), 0);
656 
657     databaseType = 0;
658     endTime = 20000000000000;
659     database->QueryDatabaseUsageStats(databaseType, beginTime, endTime, userId);
660     EXPECT_NE(database, nullptr);
661 }
662 
663 /*
664  * @tc.name: DeviceUsageStatisticsServiceTest_GetSystemTimeMs_001
665  * @tc.desc: GetSystemTimeMs
666  * @tc.type: FUNC
667  * @tc.require: issuesI5SOZY
668  */
669 HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_GetSystemTimeMs_001,
670     Function | MediumTest | Level0)
671 {
672     auto database = std::make_shared<BundleActiveUsageDatabase>();
673     database->GetSystemTimeMs();
674     EXPECT_NE(database, nullptr);
675 }
676 
677 /*
678  * @tc.name: DeviceUsageStatisticsServiceTest_ReportEvent_001
679  * @tc.desc: ReportEvent
680  * @tc.type: FUNC
681  * @tc.require: issuesI5SOZY
682  */
683 HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_ReportEvent_001,
684     Function | MediumTest | Level0)
685 {
686     bundleActiveCore_ = std::make_shared<BundleActiveCore>();
687     bundleActiveCore_->Init();
688     bundleActiveCore_->InitBundleGroupController();
689     BundleActiveEvent event;
690     int32_t userId = 0;
691     EXPECT_NE(bundleActiveCore_->ReportEvent(event, userId), ERR_OK);
692 
693     userId = 101;
694     event.bundleName_ = "com.ohos.launcher";
695     EXPECT_EQ(bundleActiveCore_->ReportEvent(event, userId), ERR_OK);
696 
697     event.bundleName_ = "com.ohos.settings";
698     event.eventId_ = 15;
699     EXPECT_EQ(bundleActiveCore_->ReportEvent(event, userId), ERR_OK);
700 
701     event.eventId_ = 16;
702     EXPECT_EQ(bundleActiveCore_->ReportEvent(event, userId), ERR_OK);
703 }
704 
705 /*
706  * @tc.name: DeviceUsageStatisticsServiceTest_InitBundleGroupController_001
707  * @tc.desc: InitBundleGroupController
708  * @tc.type: FUNC
709  * @tc.require: issuesI5SOZY
710  */
711 HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_InitBundleGroupController_001,
712     Function | MediumTest | Level0)
713 {
714     auto coreObject = bundleActiveCore_;
715     coreObject->InitBundleGroupController();
716 }
717 
718 /*
719  * @tc.name: DeviceUsageStatisticsServiceTest_ReportEventToAllUserId_001
720  * @tc.desc: ReportEventToAllUserId
721  * @tc.type: FUNC
722  * @tc.require: issuesI5SOZY
723  */
724 HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_ReportEventToAllUserId_001,
725     Function | MediumTest | Level0)
726 {
727     auto coreObject = std::make_shared<BundleActiveCore>();
728     int userId = 100;
729     auto userService = std::make_shared<BundleActiveUserService>(userId, *(coreObject.get()), false);
730     coreObject->userStatServices_[userId] = userService;
731     userId = 101;
732     coreObject->userStatServices_[userId] = nullptr;
733     BundleActiveEvent event;
734     coreObject->ReportEventToAllUserId(event);
735 
736     coreObject->Init();
737     coreObject->InitBundleGroupController();
738     coreObject->ReportEventToAllUserId(event);
739     EXPECT_NE(coreObject, nullptr);
740 }
741 
742 /*
743  * @tc.name: DeviceUsageStatisticsServiceTest_RestoreToDatabase_001
744  * @tc.desc: RestoreToDatabase
745  * @tc.type: FUNC
746  * @tc.require: issuesI5SOZY
747  */
748 HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_RestoreToDatabase_001,
749     Function | MediumTest | Level0)
750 {
751     auto coreObject = bundleActiveCore_;
752     int userId = 100;
753     auto userService = std::make_shared<BundleActiveUserService>(userId, *(coreObject.get()), false);
754     int64_t timeStamp = 20000000000000;
755     userService->Init(timeStamp);
756     coreObject->userStatServices_[userId] = userService;
757     coreObject->RestoreToDatabase(userId);
758 
759     userId = 101;
760     coreObject->RestoreToDatabase(userId);
761     EXPECT_NE(coreObject, nullptr);
762 }
763 
764 /*
765  * @tc.name: BundleActiveGroupControllerTest_001
766  * @tc.desc: test the interface
767  * @tc.type: FUNC
768  * @tc.require: DTS2023121404861
769  */
770 HWTEST_F(DeviceUsageStatisticsServiceTest, BundleActiveGroupControllerTest_001,
771     Function | MediumTest | Level0)
772 {
773     auto coreObject = bundleActiveCore_;
774     int userId = 100;
775     auto userService = std::make_shared<BundleActiveUserService>(userId, *(coreObject.get()), false);
776     int64_t timeStamp = 20000000000000;
777     userService->Init(timeStamp);
778     coreObject->userStatServices_[userId] = userService;
779     coreObject->OnUserRemoved(userId);
780     SUCCEED();
781 }
782 
783 /*
784  * @tc.name: BundleActiveGroupControllerTest_002
785  * @tc.desc: test the interface
786  * @tc.type: FUNC
787  * @tc.require: DTS2023121404861
788  */
789 HWTEST_F(DeviceUsageStatisticsServiceTest, BundleActiveGroupControllerTest_002,
790     Function | MediumTest | Level0)
791 {
792     auto coreObject = bundleActiveCore_;
793     int userId = 100;
794     auto userService = std::make_shared<BundleActiveUserService>(userId, *(coreObject.get()), false);
795     int64_t timeStamp = 20000000000000;
796     userService->Init(timeStamp);
797     coreObject->userStatServices_[userId] = userService;
798     coreObject->bundleGroupController_->OnUserSwitched(userId, userId);
799     SUCCEED();
800 }
801 
802 /*
803  * @tc.name: BundleActiveGroupControllerTest_003
804  * @tc.desc: test the interface
805  * @tc.type: FUNC
806  * @tc.require: DTS2023121404861
807  */
808 HWTEST_F(DeviceUsageStatisticsServiceTest, BundleActiveGroupControllerTest_003,
809     Function | MediumTest | Level0)
810 {
811     auto coreObject = bundleActiveCore_;
812     int userId = 100;
813     auto userService = std::make_shared<BundleActiveUserService>(userId, *(coreObject.get()), false);
814     int64_t timeStamp = 20000000000000;
815     userService->Init(timeStamp);
816     coreObject->userStatServices_[userId] = userService;
817     EXPECT_EQ(coreObject->bundleGroupController_->GetNewGroup("test", userId, timeStamp, 0), -1);
818 }
819 
820 /*
821  * @tc.name: BundleActiveGroupControllerTest_004
822  * @tc.desc: test the interface
823  * @tc.type: FUNC
824  * @tc.require: DTS2023121404861
825  */
826 HWTEST_F(DeviceUsageStatisticsServiceTest, BundleActiveGroupControllerTest_004,
827     Function | MediumTest | Level0)
828 {
829     auto coreObject = bundleActiveCore_;
830     int userId = 100;
831     BundleActiveEvent event;
832     int64_t timeStamp = 20000000000000;
833     coreObject->bundleGroupController_->ReportEvent(event, timeStamp, userId);
834     SUCCEED();
835     coreObject->bundleGroupController_->ReportEvent(event, timeStamp, 19);
836     SUCCEED();
837     coreObject->bundleGroupController_->ReportEvent(event, timeStamp, 7);
838     SUCCEED();
839     coreObject->bundleGroupController_->bundleGroupEnable_ = false;
840     SUCCEED();
841     coreObject->bundleGroupController_->ReportEvent(event, timeStamp, userId);
842     SUCCEED();
843 }
844 
845 /*
846  * @tc.name: BundleActiveGroupControllerTest_005
847  * @tc.desc: test the interface
848  * @tc.type: FUNC
849  * @tc.require: DTS2023121404861
850  */
851 HWTEST_F(DeviceUsageStatisticsServiceTest, BundleActiveGroupControllerTest_005,
852     Function | MediumTest | Level0)
853 {
854     auto coreObject = bundleActiveCore_;
855     int userId = 100;
856     int64_t timeStamp = 20000000000000;
857     coreObject->bundleGroupController_->CheckAndUpdateGroup("test", userId, 0, timeStamp);
858     SUCCEED();
859 }
860 
861 /*
862  * @tc.name: BundleActiveGroupControllerTest_006
863  * @tc.desc: test the interface
864  * @tc.type: FUNC
865  * @tc.require: DTS2023121404861
866  */
867 HWTEST_F(DeviceUsageStatisticsServiceTest, BundleActiveGroupControllerTest_006,
868     Function | MediumTest | Level0)
869 {
870     auto coreObject = bundleActiveCore_;
871     int userId = 100;
872     int64_t time = 20000000000000;
873     EXPECT_EQ(coreObject->bundleGroupController_->SetAppGroup("test", userId, 0, 0, time, true),
874         ERR_NO_APP_GROUP_INFO_IN_DATABASE);
875 }
876 
877 /*
878  * @tc.name: BundleActiveGroupControllerTest_007
879  * @tc.desc: test the interface
880  * @tc.type: FUNC
881  * @tc.require: DTS2023121404861
882  */
883 HWTEST_F(DeviceUsageStatisticsServiceTest, BundleActiveGroupControllerTest_007,
884     Function | MediumTest | Level0)
885 {
886     auto coreObject = bundleActiveCore_;
887     int userId = 100;
888     EXPECT_EQ(coreObject->bundleGroupController_->IsBundleIdle("test", userId), -1);
889 }
890 
891 /*
892  * @tc.name: BundleActiveGroupControllerTest_009
893  * @tc.desc: test the interface
894  * @tc.type: FUNC
895  * @tc.require: DTS2023121404861
896  */
897 HWTEST_F(DeviceUsageStatisticsServiceTest, BundleActiveGroupControllerTest_009,
898     Function | MediumTest | Level0)
899 {
900     auto coreObject = std::make_shared<BundleActiveCore>();
901     coreObject->bundleGroupController_ = std::make_shared<BundleActiveGroupController>(true);
902     coreObject->InitBundleGroupController();
903     int userId = 100;
904     EXPECT_EQ(coreObject->bundleGroupController_->IsBundleInstalled("test", userId), false);
905     coreObject->bundleGroupController_->sptrBundleMgr_ = nullptr;
906     EXPECT_EQ(coreObject->bundleGroupController_->IsBundleInstalled("test", userId), false);
907 }
908 
909 /*
910  * @tc.name: BundleActiveGroupControllerTest_010
911  * @tc.desc: test the interface
912  * @tc.type: FUNC
913  * @tc.require: DTS2023121404861
914  */
915 HWTEST_F(DeviceUsageStatisticsServiceTest, BundleActiveGroupControllerTest_010,
916     Function | MediumTest | Level0)
917 {
918     auto coreObject = bundleActiveCore_;
919     int userId = 100;
920     int64_t timeStamp = 20000000000000;
921     coreObject->bundleGroupController_->ShutDown(timeStamp, userId);
922     SUCCEED();
923 }
924 
925 /*
926  * @tc.name: BundleActiveGroupControllerTest_011
927  * @tc.desc: test the interface
928  * @tc.type: FUNC
929  * @tc.require: IssuesIA9M7I
930  */
931 HWTEST_F(DeviceUsageStatisticsServiceTest, BundleActiveGroupControllerTest_011,
932     Function | MediumTest | Level0)
933 {
934     bool isScreenOn = true;
935     int64_t timeStamp = 0;
936     bundleActiveCore_->bundleGroupController_->OnScreenChanged(isScreenOn, timeStamp);
937     SUCCEED();
938 }
939 
940 /*
941  * @tc.name: BundleActiveUserHistoryTest_001
942  * @tc.desc: test the interface
943  * @tc.type: FUNC
944  * @tc.require: DTS2023121404861
945  */
946 HWTEST_F(DeviceUsageStatisticsServiceTest, BundleActiveUserHistoryTest_001,
947     Function | MediumTest | Level0)
948 {
949     int64_t bootBasedTimeStamp = 2000;
950     std::vector<int64_t> screenTimeLevel;
951     std::vector<int64_t> bootFromTimeLevel;
952     auto bundleUserHistory_ = std::make_shared<BundleActiveUserHistory>(bootBasedTimeStamp, bundleActiveCore_);
953     bundleUserHistory_->GetLevelIndex("test", 0, 20000, screenTimeLevel, bootFromTimeLevel, 0);
954 }
955 
956 /*
957  * @tc.name: BundleActiveUserHistoryTest_002
958  * @tc.desc: test the interface
959  * @tc.type: FUNC
960  * @tc.require: DTS2023121404861
961  */
962 HWTEST_F(DeviceUsageStatisticsServiceTest, BundleActiveUserHistoryTest_002,
963     Function | MediumTest | Level0)
964 {
965     int64_t bootBasedTimeStamp = 2000;
966     auto bundleUserHistory_ = std::make_shared<BundleActiveUserHistory>(bootBasedTimeStamp, bundleActiveCore_);
967     bundleUserHistory_->WriteDeviceDuration();
968     bundleUserHistory_->OnBundleUninstalled(0, "test", 0, 0);
969     SUCCEED();
970 }
971 
972 /*
973  * @tc.name: BundleActiveUserHistoryTest_003
974  * @tc.desc: test the interface
975  * @tc.type: FUNC
976  * @tc.require: DTS2023121404861
977  */
978 HWTEST_F(DeviceUsageStatisticsServiceTest, BundleActiveUserHistoryTest_003,
979     Function | MediumTest | Level0)
980 {
981     int64_t bootBasedTimeStamp = 2000;
982     auto oneBundleUsageHistory = std::make_shared<BundleActivePackageHistory>();
983     auto bundleUserHistory_ = std::make_shared<BundleActiveUserHistory>(bootBasedTimeStamp, bundleActiveCore_);
984     bundleUserHistory_->ReportUsage(oneBundleUsageHistory, "test", 0, 0, 1000, 2000, 100, 0);
985     SUCCEED();
986     bundleUserHistory_->ReportUsage(oneBundleUsageHistory, "test", 0, 0, 2000, 1000, 100, 0);
987     SUCCEED();
988     bundleUserHistory_->ReportUsage(oneBundleUsageHistory, "test", 10, 0, 1000, 2000, 100, 0);
989     SUCCEED();
990     bundleUserHistory_->ReportUsage(oneBundleUsageHistory, "test", 20, 0, 1000, 2000, 100, 0);
991     SUCCEED();
992     bundleUserHistory_->ReportUsage(oneBundleUsageHistory, "test", 20, 0, 0, 2000, 100, 0);
993     SUCCEED();
994 }
995 
996 /*
997  * @tc.name: BundleActiveUserHistoryTest_004
998  * @tc.desc: test the interface
999  * @tc.type: FUNC
1000  * @tc.require: DTS2023121404861
1001  */
1002 HWTEST_F(DeviceUsageStatisticsServiceTest, BundleActiveUserHistoryTest_004,
1003     Function | MediumTest | Level0)
1004 {
1005     int32_t userId = 100;
1006     int64_t bootBasedTimeStamp = 2000;
1007     int32_t newgroup = 0;
1008     uint32_t groupReason = 0;
1009     int32_t uid = 0;
1010     auto bundleUserHistory_ = std::make_shared<BundleActiveUserHistory>(bootBasedTimeStamp, bundleActiveCore_);
1011     bundleUserHistory_->SetAppGroup("test", userId, uid, bootBasedTimeStamp, newgroup, groupReason, true);
1012     SUCCEED();
1013     bundleUserHistory_->SetAppGroup("test", userId, uid, bootBasedTimeStamp, newgroup, groupReason, false);
1014     SUCCEED();
1015 }
1016 
1017 /*
1018  * @tc.name: BundleActiveUserHistoryTest_005
1019  * @tc.desc: test the interface
1020  * @tc.type: FUNC
1021  * @tc.require: DTS2023121404861
1022  */
1023 HWTEST_F(DeviceUsageStatisticsServiceTest, BundleActiveUserHistoryTest_005,
1024     Function | MediumTest | Level0)
1025 {
1026     int64_t bootBasedTimeStamp = 2000;
1027     auto bundleUserHistory_ = std::make_shared<BundleActiveUserHistory>(bootBasedTimeStamp, bundleActiveCore_);
1028     bundleUserHistory_->PrintData(0);
1029     SUCCEED();
1030 }
1031 
1032 /*
1033  * @tc.name: DeviceUsageStatisticsServiceTest_ShutDown_001
1034  * @tc.desc: ShutDown
1035  * @tc.type: FUNC
1036  * @tc.require: issuesI5SOZY
1037  */
1038 HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_ShutDown_001,
1039     Function | MediumTest | Level0)
1040 {
1041     auto coreObject = bundleActiveCore_;
1042     coreObject->ShutDown();
1043     EXPECT_NE(coreObject, nullptr);
1044 }
1045 
1046 /*
1047  * @tc.name: DeviceUsageStatisticsServiceTest_CheckTimeChangeAndGetWallTime_001
1048  * @tc.desc: CheckTimeChangeAndGetWallTime
1049  * @tc.type: FUNC
1050  * @tc.require: issuesI5SOZY
1051  */
1052 HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_CheckTimeChangeAndGetWallTime_001,
1053     Function | MediumTest | Level0)
1054 {
1055     int userId = 100;
1056     auto coreObject = bundleActiveCore_;
1057     coreObject->OnUserRemoved(100);
1058     auto userService = std::make_shared<BundleActiveUserService>(userId, *(coreObject.get()), false);
1059     coreObject->userStatServices_[userId] = userService;
1060 
1061     coreObject->CheckTimeChangeAndGetWallTime(userId);
1062     coreObject->OnUserSwitched(userId);
1063     EXPECT_NE(coreObject, nullptr);
1064 }
1065 
1066 /*
1067  * @tc.name: DeviceUsageStatisticsServiceTest_QueryAppGroup_001
1068  * @tc.desc: QueryAppGroup
1069  * @tc.type: FUNC
1070  * @tc.require: issuesI5SOZY
1071  */
1072 HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_QueryAppGroup_001,
1073     Function | MediumTest | Level0)
1074 {
1075     auto groupController = bundleActiveCore_->bundleGroupController_;
1076     int32_t appGroup = 10;
1077     std::string bundleName = "";
1078     int32_t userId = 100;
1079     EXPECT_NE(groupController->QueryAppGroup(appGroup, bundleName, userId), ERR_OK);
1080 
1081     groupController->sptrBundleMgr_ = nullptr;
1082     bundleName = "defaultBundleName";
1083     EXPECT_NE(groupController->QueryAppGroup(appGroup, bundleName, userId), ERR_OK);
1084 }
1085 
1086 /*
1087  * @tc.name: DeviceUsageStatisticsServiceTest_ControllerReportEvent_001
1088  * @tc.desc: ControllerReportEvent
1089  * @tc.type: FUNC
1090  * @tc.require: issuesI5SOZY
1091  */
1092 HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_ControllerReportEvent_001,
1093     Function | MediumTest | Level0)
1094 {
1095     auto coreObject = bundleActiveCore_;
1096 
1097     int64_t bootBasedTimeStamp = 20000000000000;
1098     BundleActiveEvent event;
1099     int32_t userId = 100;
1100     coreObject->bundleGroupController_->bundleGroupEnable_ = false;
1101     coreObject->bundleGroupController_->ReportEvent(event, bootBasedTimeStamp, userId);
1102 
1103     coreObject->bundleGroupController_->bundleGroupEnable_ = true;
1104     event.bundleName_ = "com.ohos.camera";
1105     coreObject->bundleGroupController_->ReportEvent(event, bootBasedTimeStamp, userId);
1106 
1107     event.eventId_ = BundleActiveEvent::NOTIFICATION_SEEN;
1108     coreObject->bundleGroupController_->ReportEvent(event, bootBasedTimeStamp, userId);
1109 
1110     event.eventId_ = BundleActiveEvent::SYSTEM_INTERACTIVE;
1111     coreObject->bundleGroupController_->ReportEvent(event, bootBasedTimeStamp, userId);
1112     EXPECT_NE(coreObject, nullptr);
1113 }
1114 
1115 /*
1116  * @tc.name: DeviceUsageStatisticsServiceTest_CheckAndUpdateGroup_001
1117  * @tc.desc: CheckAndUpdateGroup
1118  * @tc.type: FUNC
1119  * @tc.require: issuesI5SOZY
1120  */
1121 HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_CheckAndUpdateGroup_001,
1122     Function | MediumTest | Level0)
1123 {
1124     auto coreObject = bundleActiveCore_;
1125 
1126     std::string bundleName = "com.ohos.camera";
1127     int32_t userId = 100;
1128     int64_t bootBasedTimeStamp = 20000000000000;
1129     int32_t newGroup = 30;
1130     uint32_t reason = GROUP_CONTROL_REASON_TIMEOUT;
1131     bool isFlush = false;
1132 
1133     int32_t appGroup = 0;
1134     coreObject->bundleGroupController_->QueryAppGroup(appGroup, bundleName, userId);
1135 
1136     coreObject->bundleGroupController_->SetAppGroup(bundleName, userId, newGroup, reason, bootBasedTimeStamp, isFlush);
1137     coreObject->bundleGroupController_->CheckAndUpdateGroup(bundleName, 0, userId, bootBasedTimeStamp);
1138 
1139     newGroup = 20;
1140     reason = GROUP_CONTROL_REASON_CALCULATED;
1141     coreObject->bundleGroupController_->SetAppGroup(bundleName, userId, newGroup, reason, bootBasedTimeStamp, isFlush);
1142     coreObject->bundleGroupController_->CheckAndUpdateGroup(bundleName, 0, userId, bootBasedTimeStamp);
1143     EXPECT_NE(coreObject, nullptr);
1144 }
1145 
1146 /*
1147  * @tc.name: DeviceUsageStatisticsServiceTest_PreservePowerStateInfo_001
1148  * @tc.desc: PreservePowerStateInfo
1149  * @tc.type: FUNC
1150  * @tc.require: issuesI5SOZY
1151  */
1152 HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_PreservePowerStateInfo_001,
1153     Function | MediumTest | Level0)
1154 {
1155     auto coreObject = bundleActiveCore_;
1156 
1157     int32_t eventId = BundleActiveEvent::ABILITY_FOREGROUND;
1158     coreObject->PreservePowerStateInfo(eventId);
1159     EXPECT_NE(coreObject, nullptr);
1160 }
1161 
1162 /*
1163  * @tc.name: DeviceUsageStatisticsServiceTest_UpdateModuleData_001
1164  * @tc.desc: UpdateModuleData
1165  * @tc.type: FUNC
1166  * @tc.require: issuesI5SOZY
1167  */
1168 HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_UpdateModuleData_001,
1169     Function | MediumTest | Level0)
1170 {
1171     auto database = std::make_shared<BundleActiveUsageDatabase>();
1172     auto moduleRecords = std::map<std::string, std::shared_ptr<BundleActiveModuleRecord>>();
1173     int64_t timeStamp = 20000000000;
1174     int32_t userId = 100;
1175     database->UpdateModuleData(userId, moduleRecords, timeStamp);
1176     EXPECT_NE(database, nullptr);
1177 }
1178 
1179 /*
1180  * @tc.name: DeviceUsageStatisticsServiceTest_QueryNotificationEventStats_001
1181  * @tc.desc: QueryNotificationEventStats
1182  * @tc.type: FUNC
1183  * @tc.require: issuesI5SOZY
1184  */
1185 HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_QueryNotificationEventStats_001,
1186     Function | MediumTest | Level0)
1187 {
1188     auto database = std::make_shared<BundleActiveUsageDatabase>();
1189 
1190     int32_t eventId = 2;
1191     int64_t beginTime = 0;
1192     int64_t endTime = 0;
1193     auto notificationEventStats = std::map<std::string, BundleActiveEventStats>();
1194     int32_t userId = 100;
1195     BUNDLE_ACTIVE_LOGI("database->QueryNotificationEventStats");
1196     database->QueryNotificationEventStats(eventId, beginTime, endTime, notificationEventStats, userId);
1197     EXPECT_NE(database, nullptr);
1198 }
1199 
1200 /*
1201  * @tc.name: DeviceUsageStatisticsServiceTest_QueryDeviceEventStats_001
1202  * @tc.desc: QueryDeviceEventStats
1203  * @tc.type: FUNC
1204  * @tc.require: issuesI5SOZY
1205  */
1206 HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_QueryDeviceEventStats_001,
1207     Function | MediumTest | Level0)
1208 {
1209     auto database = std::make_shared<BundleActiveUsageDatabase>();
1210 
1211     int32_t eventId = 2;
1212     int64_t beginTime = 0;
1213     int64_t endTime = 0;
1214     auto eventStats = std::map<std::string, BundleActiveEventStats>();
1215     int32_t userId = 100;
1216     BUNDLE_ACTIVE_LOGI("database->QueryDeviceEventStats");
1217     database->QueryDeviceEventStats(eventId, beginTime, endTime, eventStats, userId);
1218     EXPECT_NE(database, nullptr);
1219 }
1220 
1221 /*
1222  * @tc.name: DeviceUsageStatisticsServiceTest_QueryDatabaseEvents_001
1223  * @tc.desc: QueryDatabaseEvents
1224  * @tc.type: FUNC
1225  * @tc.require: issuesI5SOZY
1226  */
1227 HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_QueryDatabaseEvents_001,
1228     Function | MediumTest | Level0)
1229 {
1230     auto database = std::make_shared<BundleActiveUsageDatabase>();
1231 
1232     int64_t beginTime = 0;
1233     int64_t endTime = 0;
1234     std::string bundleName;
1235     int32_t userId = 100;
1236     BUNDLE_ACTIVE_LOGI("database->QueryDatabaseEvents");
1237     database->QueryDatabaseEvents(beginTime, endTime, userId, bundleName);
1238 
1239     bundleName = "com.ohos.camera";
1240     database->QueryDatabaseEvents(beginTime, endTime, userId, bundleName);
1241     EXPECT_NE(database, nullptr);
1242 }
1243 
1244 /*
1245  * @tc.name: DeviceUsageStatisticsServiceTest_UpdateEventData_001
1246  * @tc.desc: UpdateEventData
1247  * @tc.type: FUNC
1248  * @tc.require: issuesI5SOZY
1249  */
1250 HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_UpdateEventData_001,
1251     Function | MediumTest | Level0)
1252 {
1253     auto database = std::make_shared<BundleActiveUsageDatabase>();
1254 
1255     int32_t databaseType = WEEKLY_DATABASE_INDEX;
1256     BundleActivePeriodStats stats;
1257     database->UpdateEventData(databaseType, stats);
1258 
1259     databaseType = DAILY_DATABASE_INDEX;
1260     database->UpdateEventData(databaseType, stats);
1261     EXPECT_NE(database, nullptr);
1262 }
1263 
1264 /*
1265  * @tc.name: DeviceUsageStatisticsServiceTest_UpdateBundleUsageData_001
1266  * @tc.desc: UpdateBundleUsageData
1267  * @tc.type: FUNC
1268  * @tc.require: issuesI5SOZY
1269  */
1270 HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_UpdateBundleUsageData_001,
1271     Function | MediumTest | Level0)
1272 {
1273     auto database = std::make_shared<BundleActiveUsageDatabase>();
1274 
1275     int32_t databaseType = -1;
1276     BundleActivePeriodStats stats;
1277     database->UpdateBundleUsageData(databaseType, stats);
1278 
1279     databaseType = EVENT_DATABASE_INDEX;
1280     database->UpdateBundleUsageData(databaseType, stats);
1281 
1282     databaseType = DAILY_DATABASE_INDEX;
1283     database->UpdateBundleUsageData(databaseType, stats);
1284     EXPECT_NE(database, nullptr);
1285 }
1286 
1287 /*
1288  * @tc.name: DeviceUsageStatisticsServiceTest_RenewTableTime_001
1289  * @tc.desc: RenewTableTime
1290  * @tc.type: FUNC
1291  * @tc.require: issuesI5SOZY
1292  */
1293 HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_RenewTableTime_001,
1294     Function | MediumTest | Level0)
1295 {
1296     auto database = std::make_shared<BundleActiveUsageDatabase>();
1297 
1298     int64_t timeDiffMillis = 0;
1299     database->sortedTableArray_[0] = {-1, 0};
1300     database->RenewTableTime(timeDiffMillis);
1301 
1302     database->eventTableName_ = "";
1303     database->RenewTableTime(timeDiffMillis);
1304 
1305     database->eventTableName_ = "defaultTableName";
1306     database->RenewTableTime(timeDiffMillis);
1307 
1308     database->formRecordsTableName_ = "defaultFormRecordsTableName";
1309     database->RenewTableTime(timeDiffMillis);
1310 
1311     database->moduleRecordsTableName_ = "defaultModuleRecordsTableName_";
1312     database->RenewTableTime(timeDiffMillis);
1313     EXPECT_NE(database, nullptr);
1314 }
1315 
1316 /*
1317  * @tc.name: DeviceUsageStatisticsServiceTest_SetNewIndexWhenTimeChanged_001
1318  * @tc.desc: SetNewIndexWhenTimeChanged
1319  * @tc.type: FUNC
1320  * @tc.require: issuesI5SOZY
1321  */
1322 HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_SetNewIndexWhenTimeChanged_001,
1323     Function | MediumTest | Level0)
1324 {
1325     auto database = std::make_shared<BundleActiveUsageDatabase>();
1326 
1327     uint32_t databaseType = APP_GROUP_DATABASE_INDEX;
1328     int64_t tableOldTime = 0;
1329     int64_t tableNewTime = 20000000000000;
1330     std::shared_ptr<NativeRdb::RdbStore> rdbStore = nullptr;
1331     EXPECT_NE(database->SetNewIndexWhenTimeChanged(databaseType, tableOldTime, tableNewTime, rdbStore), ERR_OK);
1332 
1333     rdbStore = database->GetBundleActiveRdbStore(databaseType);
1334     database->SetNewIndexWhenTimeChanged(databaseType, tableOldTime, tableNewTime, rdbStore);
1335     EXPECT_NE(database, nullptr);
1336 }
1337 
1338 /*
1339  * @tc.name: DeviceUsageStatisticsServiceTest_ReportContinuousTaskEvent_001
1340  * @tc.desc: ReportContinuousTaskEvent
1341  * @tc.type: FUNC
1342  * @tc.require: issuesI5SOZY
1343  */
1344 HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_ReportContinuousTaskEvent_001,
1345     Function | MediumTest | Level0)
1346 {
1347     #ifdef BGTASKMGR_ENABLE
1348     auto bgtaskObserver = std::make_shared<BundleActiveContinuousTaskObserver>();
1349     auto continuousTaskCallbackInfo = std::shared_ptr<OHOS::BackgroundTaskMgr::ContinuousTaskCallbackInfo>();
1350     continuousTaskCallbackInfo->creatorUid_ = 20000000;
1351     bool isStart = false;
1352     bgtaskObserver->ReportContinuousTaskEvent(continuousTaskCallbackInfo, isStart);
1353 
1354     isStart = true;
1355     bgtaskObserver->ReportContinuousTaskEvent(continuousTaskCallbackInfo, isStart);
1356     EXPECT_NE(bgtaskObserver, nullptr);
1357     #endif
1358 }
1359 
1360 /*
1361  * @tc.name: DeviceUsageStatisticsServiceTest_RemoveFormData_001
1362  * @tc.desc: RemoveFormData
1363  * @tc.type: FUNC
1364  * @tc.require: issuesI5SOZY
1365  */
1366 HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_RemoveFormData_001,
1367     Function | MediumTest | Level0)
1368 {
1369     auto database = std::make_shared<BundleActiveUsageDatabase>();
1370     int32_t userId = 100;
1371     std::string bundleName = "defaultBundleName";
1372     std::string moduleName = "defaultModuleName";
1373     std::string formName = "defaultFormName";
1374     database->InitUsageGroupDatabase(0, true);
1375     database->RemoveFormData(userId, bundleName, moduleName, formName, 0, 0, 0);
1376     EXPECT_NE(database, nullptr);
1377 }
1378 
1379 /*
1380  * @tc.name: DeviceUsageStatisticsServiceTest_UpdateFormData_001
1381  * @tc.desc: UpdateFormData
1382  * @tc.type: FUNC
1383  * @tc.require: issuesI5SOZY
1384  */
1385 HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_UpdateFormData_001,
1386     Function | MediumTest | Level0)
1387 {
1388     auto database = std::make_shared<BundleActiveUsageDatabase>();
1389     int32_t userId = 100;
1390     std::string bundleName = "defaultBundleName";
1391     std::string moduleName = "defaultModuleName";
1392     std::string formName = "defaultFormName";
1393     database->InitUsageGroupDatabase(0, true);
1394     BundleActiveFormRecord formRecord;
1395     auto rdbStore = database->GetBundleActiveRdbStore(0);
1396     database->UpdateFormData(userId, bundleName, moduleName, formRecord, rdbStore);
1397     EXPECT_NE(database, nullptr);
1398 }
1399 
1400 /*
1401  * @tc.name: DeviceUsageStatisticsServiceTest_DeleteUninstalledBundleStats_001
1402  * @tc.desc: onstart
1403  * @tc.type: FUNC
1404  * @tc.require: issuesI9Q9ZJ
1405  */
1406 HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_DeleteUninstalledBundleStats_001,
1407     Function | MediumTest | Level0)
1408 {
1409     auto coreObject = bundleActiveCore_;
1410     int userId = 100;
1411     auto userService = std::make_shared<BundleActiveUserService>(userId, *(coreObject.get()), false);
1412     int appIndex = 1;
1413     std::vector<std::shared_ptr<BundleActivePeriodStats>> curStats;
1414     std::shared_ptr<BundleActivePeriodStats> stats = std::make_shared<BundleActivePeriodStats>();
1415     stats->bundleStats_["test0"] = std::make_shared<BundleActivePackageStats>();
1416     stats->bundleStats_["test1"] = std::make_shared<BundleActivePackageStats>();
1417     stats->bundleStats_["test2"] = std::make_shared<BundleActivePackageStats>();
1418     curStats.push_back(stats);
1419     userService->currentStats_ = curStats;
1420     userService->DeleteUninstalledBundleStats("test", 0, appIndex);
1421     appIndex = 0;
1422     userService->DeleteUninstalledBundleStats("test", 0, appIndex);
1423 }
1424 
1425 /*
1426  * @tc.name: DeviceUsageStatisticsServiceTest_DeleteUninstalledBundleStats_002
1427  * @tc.desc: onstart
1428  * @tc.type: FUNC
1429  * @tc.require: issuesI9Q9ZJ
1430  */
1431 HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_DeleteUninstalledBundleStats_002,
1432     Function | MediumTest | Level0)
1433 {
1434     auto coreObject = bundleActiveCore_;
1435     int userId = 100;
1436     auto userService = std::make_shared<BundleActiveUserService>(userId, *(coreObject.get()), false);
1437     int appIndex = 1;
1438     std::vector<std::shared_ptr<BundleActivePeriodStats>> curStats;
1439     std::shared_ptr<BundleActivePeriodStats> stats = std::make_shared<BundleActivePeriodStats>();
1440     stats->bundleStats_["test0"] = std::make_shared<BundleActivePackageStats>();
1441     stats->bundleStats_["test1"] = std::make_shared<BundleActivePackageStats>();
1442     stats->bundleStats_["test2"] = std::make_shared<BundleActivePackageStats>();
1443 
1444     BundleActiveEvent event;
1445     event.bundleName_ = "test";
1446     event.uid_ = 0;
1447     stats->events_.Insert(event);
1448     BundleActiveEvent event2;
1449     event.bundleName_ = "test";
1450     event.uid_ = 1;
1451     stats->events_.Insert(event);
1452     curStats.push_back(stats);
1453     userService->moduleRecords_["test0"] = std::make_shared<BundleActiveModuleRecord>();
1454     userService->moduleRecords_["test1"] = std::make_shared<BundleActiveModuleRecord>();
1455     userService->moduleRecords_["test2"] = std::make_shared<BundleActiveModuleRecord>();
1456     userService->currentStats_ = curStats;
1457     userService->DeleteUninstalledBundleStats("test", 100, appIndex);
1458     appIndex = 0;
1459     userService->DeleteUninstalledBundleStats("test", 100, appIndex);
1460 }
1461 
1462 /*
1463  * @tc.name: DeviceUsageStatisticsServiceTest_ConfigReader_001
1464  * @tc.desc: ConfigReader
1465  * @tc.type: FUNC
1466  * @tc.require: issuesIBCE1G
1467  */
1468 HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_ConfigReader_001,
1469     Function | MediumTest | Level0)
1470 {
1471     auto bundleActiveConfigReader = std::make_shared<BundleActiveConfigReader>();
1472     EXPECT_NE(bundleActiveConfigReader, nullptr);
1473     bundleActiveConfigReader->LoadConfig();
1474     EXPECT_NE(bundleActiveConfigReader->GetApplicationUsePeriodicallyConfig().minUseTimes, 0);
1475 }
1476 
1477 /*
1478  * @tc.name: DeviceUsageStatisticsServiceTest_ConfigReader_002
1479  * @tc.desc: ConfigReader
1480  * @tc.type: FUNC
1481  * @tc.require: issuesIBCE1G
1482  */
1483 HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_ConfigReader_002,
1484     Function | MediumTest | Level0)
1485 {
1486     auto bundleActiveConfigReader = std::make_shared<BundleActiveConfigReader>();
1487     EXPECT_NE(bundleActiveConfigReader, nullptr);
1488     const char *path = "test";
1489     bundleActiveConfigReader->LoadApplicationUsePeriodically(path);
1490     EXPECT_EQ(bundleActiveConfigReader->GetApplicationUsePeriodicallyConfig().minUseTimes, 0);
1491 }
1492 
1493 /*
1494  * @tc.name: DeviceUsageStatisticsServiceTest_ConfigReader_003
1495  * @tc.desc: ConfigReader
1496  * @tc.type: FUNC
1497  * @tc.require: issuesIBCE1G
1498  */
1499 HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_ConfigReader_003,
1500     Function | MediumTest | Level0)
1501 {
1502     auto bundleActiveConfigReader = std::make_shared<BundleActiveConfigReader>();
1503     EXPECT_NE(bundleActiveConfigReader, nullptr);
1504     const char *path = "test";
1505     Json::Value root;
1506     bool result = bundleActiveConfigReader->GetJsonFromFile(path, root);
1507     EXPECT_EQ(result, false);
1508 }
1509 
1510 /*
1511  * @tc.name: DeviceUsageStatisticsServiceTest_ConfigReader_004
1512  * @tc.desc: ConfigReader
1513  * @tc.type: FUNC
1514  * @tc.require: issuesIBCE1G
1515  */
1516 HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_ConfigReader_004,
1517     Function | MediumTest | Level0)
1518 {
1519     auto bundleActiveConfigReader = std::make_shared<BundleActiveConfigReader>();
1520     EXPECT_NE(bundleActiveConfigReader, nullptr);
1521     std::string partialPath = "test";
1522     std::string fullPath = "";
1523     bool result = bundleActiveConfigReader->ConvertFullPath(partialPath, fullPath);
1524     EXPECT_EQ(result, false);
1525 }
1526 
1527 }  // namespace DeviceUsageStats
1528 }  // namespace OHOS