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