• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022  Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #define private public
17 #define protected public
18 
19 #include <string>
20 
21 #include <gtest/gtest.h>
22 #include "system_ability_definition.h"
23 
24 #include "bundle_active_client.h"
25 #include "bundle_active_event.h"
26 #include "app_group_callback_stub.h"
27 #include "bundle_active_group_map.h"
28 #include "app_group_callback_info.h"
29 #include "iapp_group_callback.h"
30 
31 using namespace testing::ext;
32 
33 namespace OHOS {
34 namespace DeviceUsageStats {
35 static std::string g_defaultBundleName = "com.ohos.camera";
36 static std::string g_defaultMoudleName = "defaultmodulename";
37 static std::string g_defaultFormName = "defaultformname";
38 static int32_t DEFAULT_DIMENSION = 4;
39 static int64_t DEFAULT_FORMID = 1;
40 static int32_t DEFAULT_USERID = 0;
41 static int32_t COMMON_USERID = 100;
42 static int32_t DEFAULT_ERRCODE = 0;
43 static int64_t LARGE_NUM = 20000000000000;
44 static int32_t DEFAULT_GROUP = 10;
45 static std::vector<int32_t> GROUP_TYPE {10, 20, 30, 40, 50};
46 static sptr<IAppGroupCallback> observer = nullptr;
47 
48 class DeviceUsageStatisticsTest : public testing::Test {
49 public:
50     static void SetUpTestCase(void);
51     static void TearDownTestCase(void);
52     void SetUp();
53     void TearDown();
54 };
55 
SetUpTestCase(void)56 void DeviceUsageStatisticsTest::SetUpTestCase(void)
57 {
58 }
59 
TearDownTestCase(void)60 void DeviceUsageStatisticsTest::TearDownTestCase(void)
61 {
62 }
63 
SetUp(void)64 void DeviceUsageStatisticsTest::SetUp(void)
65 {
66 }
67 
TearDown(void)68 void DeviceUsageStatisticsTest::TearDown(void)
69 {
70 }
71 
72 class TestAppGroupChangeCallback : public AppGroupCallbackStub {
73 public:
74     void OnAppGroupChanged(const AppGroupCallbackInfo &appGroupCallbackInfo) override;
75 };
76 
OnAppGroupChanged(const AppGroupCallbackInfo & appGroupCallbackInfo)77 void TestAppGroupChangeCallback::OnAppGroupChanged(const AppGroupCallbackInfo &appGroupCallbackInfo)
78 {
79     BUNDLE_ACTIVE_LOGI("TestAppGroupChangeCallback::OnAppGroupChanged!");
80     MessageParcel data;
81     if (!appGroupCallbackInfo.Marshalling(data)) {
82         BUNDLE_ACTIVE_LOGE("Marshalling fail");
83     }
84     appGroupCallbackInfo.Unmarshalling(data);
85 }
86 
87 /*
88  * @tc.name: DeviceUsageStatisticsTest_GetServiceObject_001
89  * @tc.desc: get service object
90  * @tc.type: FUNC
91  * @tc.require: SR000GGTO8 AR000GH6PK
92  */
93 HWTEST_F(DeviceUsageStatisticsTest, DeviceUsageStatisticsTest_GetServiceObject_001, Function | MediumTest | Level0)
94 {
95     sptr<ISystemAbilityManager> systemAbilityManager =
96         SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
97     EXPECT_NE(systemAbilityManager, nullptr);
98 
99     sptr<IRemoteObject> remoteObject =
100         systemAbilityManager->GetSystemAbility(DEVICE_USAGE_STATISTICS_SYS_ABILITY_ID);
101     EXPECT_NE(remoteObject, nullptr);
102 }
103 
104 /*
105  * @tc.name: DeviceUsageStatisticsTest_ReportEvent_001
106  * @tc.desc: report a mock event
107  * @tc.type: FUNC
108  * @tc.require: SR000GGTO7 SR000GU31B AR000GH6PJ AR000GU380
109  */
110 HWTEST_F(DeviceUsageStatisticsTest, DeviceUsageStatisticsTest_ReportEvent_001, Function | MediumTest | Level0)
111 {
112     BundleActiveEvent eventA(g_defaultBundleName, g_defaultMoudleName, g_defaultFormName,
113         DEFAULT_DIMENSION, DEFAULT_FORMID, BundleActiveEvent::FORM_IS_CLICKED);
114     BundleActiveClient::GetInstance().ReportEvent(eventA, DEFAULT_USERID);
115     BundleActiveEvent eventB(g_defaultBundleName, g_defaultMoudleName, g_defaultFormName,
116         DEFAULT_DIMENSION, DEFAULT_FORMID, BundleActiveEvent::FORM_IS_REMOVED);
117     BundleActiveClient::GetInstance().ReportEvent(eventB, DEFAULT_USERID);
118 }
119 
120 /*
121  * @tc.name: DeviceUsageStatisticsTest_QueryBundleEvents_001
122  * @tc.desc: QueryBundleEvents
123  * @tc.type: FUNC
124  * @tc.require: SR000GGTO6 AR000GH6PH
125  */
126 HWTEST_F(DeviceUsageStatisticsTest, DeviceUsageStatisticsTest_QueryBundleEvents_001, Function | MediumTest | Level0)
127 {
128     std::vector<BundleActiveEvent> result;
129     BundleActiveClient::GetInstance().QueryBundleEvents(result, 0, LARGE_NUM, 100);
130     EXPECT_EQ(result.size() > 0, true);
131     EXPECT_NE(BundleActiveClient::GetInstance().QueryBundleEvents(result, LARGE_NUM, LARGE_NUM, 100), 0);
132 }
133 
134 /*
135  * @tc.name: DeviceUsageStatisticsTest_QueryCurrentBundleEvents_001
136  * @tc.desc: QueryCurrentBundleEvents
137  * @tc.type: FUNC
138  * @tc.require: SR000GGTO4 AR000GH6PF
139  */
140 HWTEST_F(DeviceUsageStatisticsTest,
141     DeviceUsageStatisticsTest_QueryCurrentBundleEvents_001, Function | MediumTest | Level0)
142 {
143     std::vector<BundleActiveEvent> result;
144     BundleActiveClient::GetInstance().QueryCurrentBundleEvents(result, 0, LARGE_NUM);
145     EXPECT_EQ(result.size(), 0);
146 }
147 
148 /*
149  * @tc.name: DeviceUsageStatisticsTest_QueryPackagesStats_001
150  * @tc.desc: querypackagestats
151  * @tc.type: FUNC
152  * @tc.require: SR000GGTO3 AR000GH6PD
153  */
154 HWTEST_F(DeviceUsageStatisticsTest, DeviceUsageStatisticsTest_QueryPackagesStats_001, Function | MediumTest | Level0)
155 {
156     std::vector<BundleActivePackageStats> result;
157     BundleActiveClient::GetInstance().QueryBundleStatsInfoByInterval(result, 4, 0, LARGE_NUM);
158     EXPECT_EQ(result.size(), 0);
159     EXPECT_NE(BundleActiveClient::GetInstance().QueryBundleStatsInfoByInterval(result, 4, LARGE_NUM, LARGE_NUM), 0);
160 }
161 
162 /*
163  * @tc.name: DeviceUsageStatisticsTest_QueryBundleStatsInfos_001
164  * @tc.desc: QueryBundleStatsInfos
165  * @tc.type: FUNC
166  * @tc.require: issuesI5QJD9
167  */
168 HWTEST_F(DeviceUsageStatisticsTest, DeviceUsageStatisticsTest_QueryBundleStatsInfos_001,
169     Function | MediumTest | Level0)
170 {
171     std::vector<BundleActivePackageStats> result;
172     BundleActiveClient::GetInstance().QueryBundleStatsInfos(result, 4, 0, LARGE_NUM);
173     EXPECT_EQ(result.size(), 0);
174 }
175 
176 /*
177  * @tc.name: DeviceUsageStatisticsTest_IsBundleIdle_001
178  * @tc.desc: isbundleidle
179  * @tc.type: FUNC
180  * @tc.require: SR000GGTO5 AR000GH6PG
181  */
182 HWTEST_F(DeviceUsageStatisticsTest, DeviceUsageStatisticsTest_IsBundleIdle_001, Function | MediumTest | Level0)
183 {
184     bool result = false;
185     int32_t errCode = BundleActiveClient::GetInstance().IsBundleIdle(result, g_defaultBundleName, DEFAULT_USERID);
186     EXPECT_EQ(result, false);
187     EXPECT_EQ(errCode, 0);
188 }
189 
190 /*
191  * @tc.name: DeviceUsageStatisticsTest_QueryModuleUsageRecords_001
192  * @tc.desc: QueryModuleUsageRecords
193  * @tc.type: FUNC
194  * @tc.require: SR000GU2T1 AR000GU37U
195  */
196 HWTEST_F(DeviceUsageStatisticsTest,
197     DeviceUsageStatisticsTest_QueryModuleUsageRecords_001, Function | MediumTest | Level0)
198 {
199     int32_t maxNum = 1;
200     BundleActiveEvent eventA(g_defaultBundleName, g_defaultMoudleName, g_defaultFormName,
201         DEFAULT_DIMENSION, DEFAULT_FORMID, BundleActiveEvent::FORM_IS_CLICKED);
202     BundleActiveClient::GetInstance().ReportEvent(eventA, DEFAULT_USERID);
203     std::vector<BundleActiveModuleRecord> results;
204     int32_t errCode = BundleActiveClient::GetInstance().QueryModuleUsageRecords(maxNum, results, DEFAULT_USERID);
205     EXPECT_EQ(errCode, 0);
206     EXPECT_EQ(results.size(), 0);
207 
208     results.clear();
209     maxNum = 0;
210     errCode = BundleActiveClient::GetInstance().QueryModuleUsageRecords(maxNum, results, DEFAULT_USERID);
211     EXPECT_NE(errCode, 0);
212 
213     results.clear();
214     maxNum = 1001;
215     errCode = BundleActiveClient::GetInstance().QueryModuleUsageRecords(maxNum, results, DEFAULT_USERID);
216     EXPECT_NE(errCode, 0);
217 }
218 
219 /*
220  * @tc.name: DeviceUsageStatisticsTest_RegisterAppGroupCallBack_001
221  * @tc.desc: RegisterAppGroupCallBack
222  * @tc.type: FUNC
223  * @tc.require: SR000H0HAQ AR000H0ROE
224  */
225 HWTEST_F(DeviceUsageStatisticsTest,
226     DeviceUsageStatisticsTest_RegisterAppGroupCallBack_001, Function | MediumTest | Level0)
227 {
228     if (!observer) {
229         BUNDLE_ACTIVE_LOGI("RegisterAppGroupCallBack construct observer!");
230         observer = new (std::nothrow) TestAppGroupChangeCallback();
231     }
232     ASSERT_NE(observer, nullptr);
233     int32_t result = BundleActiveClient::GetInstance().RegisterAppGroupCallBack(observer);
234     EXPECT_EQ(result, DEFAULT_ERRCODE);
235 }
236 
237 /*
238  * @tc.name: DeviceUsageStatisticsTest_SetBundleGroup_001
239  * @tc.desc: setbundlename
240  * @tc.type: FUNC
241  * @tc.require: SR000H0HAQ AR000H0ROE
242  */
243 HWTEST_F(DeviceUsageStatisticsTest, DeviceUsageStatisticsTest_SetAppGroup_001, Function | MediumTest | Level0)
244 {
245     int32_t result = 0;
246     BundleActiveClient::GetInstance().QueryAppGroup(result, g_defaultBundleName, COMMON_USERID);
247     DEFAULT_GROUP = (result == DEFAULT_GROUP) ? (result + 10) : DEFAULT_GROUP;
248     result = BundleActiveClient::GetInstance().SetAppGroup(g_defaultBundleName, DEFAULT_GROUP, COMMON_USERID);
249     EXPECT_EQ(result, DEFAULT_ERRCODE);
250 
251     result = BundleActiveClient::GetInstance().SetAppGroup(g_defaultBundleName, DEFAULT_GROUP, -1);
252     EXPECT_NE(result, DEFAULT_ERRCODE);
253 }
254 
255 /*
256  * @tc.name: DeviceUsageStatisticsTest_QueryAppGroup_001
257  * @tc.desc: QueryAppGroup, no bundleName
258  * @tc.type: FUNC
259  * @tc.require: SR000H0HAQ AR000H0ROE
260  */
261 HWTEST_F(DeviceUsageStatisticsTest, DeviceUsageStatisticsTest_QueryAppGroup_001, Function | MediumTest | Level0)
262 {
263     BundleActiveClient::GetInstance().SetAppGroup(g_defaultBundleName, DEFAULT_GROUP, COMMON_USERID);
264     int32_t result = 0;
265     BundleActiveClient::GetInstance().QueryAppGroup(result, g_defaultBundleName, COMMON_USERID);
266     bool flag = false;
267     for (auto item = GROUP_TYPE.begin(); item != GROUP_TYPE.end(); item++) {
268         if (*item == result) {
269             flag = true;
270             break;
271         }
272     }
273     EXPECT_EQ(flag, true);
274 }
275 
276 /*
277  * @tc.name: DeviceUsageStatisticsTest_UnRegisterAppGroupCallBack_001
278  * @tc.desc: UnRegisterAppGroupCallBack
279  * @tc.type: FUNC
280  * @tc.require: SR000H0HAQ AR000H0ROE
281  */
282 HWTEST_F(DeviceUsageStatisticsTest, DeviceUsageStatisticsTest_UnRegisterAppGroupCallBack_001,
283     Function | MediumTest | Level0)
284 {
285     if (!observer) {
286         BUNDLE_ACTIVE_LOGI("observer has been delete");
287     }
288     ASSERT_NE(observer, nullptr);
289     int32_t result = BundleActiveClient::GetInstance().UnRegisterAppGroupCallBack(observer);
290     observer = nullptr;
291     EXPECT_EQ(result, DEFAULT_ERRCODE);
292 }
293 
294 /*
295  * @tc.name: DeviceUsageStatisticsTest_QueryDeviceEventStats_001
296  * @tc.desc: QueryDeviceEventStats
297  * @tc.type: FUNC
298  * @tc.require: SR000H0H9H AR000H0ROG
299  */
300 HWTEST_F(DeviceUsageStatisticsTest,
301     DeviceUsageStatisticsTest_QueryDeviceEventStats_001, Function | MediumTest | Level0)
302 {
303     std::vector<BundleActiveEventStats> eventStats;
304     int32_t errCode = BundleActiveClient::GetInstance().QueryDeviceEventStats(0, LARGE_NUM, eventStats);
305     EXPECT_EQ(errCode, 0);
306     errCode = BundleActiveClient::GetInstance().QueryDeviceEventStats(0, LARGE_NUM, eventStats, COMMON_USERID);
307     EXPECT_EQ(errCode, 0);
308 }
309 
310 /*
311  * @tc.name: DeviceUsageStatisticsTest_QueryNotificationEventStats_001
312  * @tc.desc: QueryNotificationEventStats
313  * @tc.type: FUNC
314  * @tc.require: SR000H0H7D AR000H0RR6
315  */
316 HWTEST_F(DeviceUsageStatisticsTest, DeviceUsageStatisticsTest_QueryNotificationEventStats_001, Function
317     | MediumTest | Level0)
318 {
319     std::vector<BundleActiveEventStats> eventStats;
320     int32_t errCode = BundleActiveClient::GetInstance().QueryNotificationEventStats(0, LARGE_NUM, eventStats);
321     EXPECT_EQ(errCode, 0);
322     errCode = BundleActiveClient::GetInstance().QueryNotificationEventStats(0, LARGE_NUM, eventStats, COMMON_USERID);
323     EXPECT_EQ(errCode, 0);
324 }
325 
326 /*
327  * @tc.name: DeviceUsageStatisticsTest_BundleActiveGroupMap_001
328  * @tc.desc: BundleActiveGroupMap
329  * @tc.type: FUNC
330  * @tc.require: SR000H0G4F AR000H2US8
331  */
332 HWTEST_F(DeviceUsageStatisticsTest, DeviceUsageStatisticsTest_BundleActiveGroupMap_001, Function | MediumTest | Level0)
333 {
334     int64_t minInterval = DeviceUsageStatsGroupMap::groupIntervalMap_
335         .at(DeviceUsageStatsGroupConst::ACTIVE_GROUP_FORCED_SET);
336     EXPECT_EQ(minInterval, 0);
337     minInterval = DeviceUsageStatsGroupMap::groupIntervalMap_
338         .at(DeviceUsageStatsGroupConst::ACTIVE_GROUP_ALIVE);
339     EXPECT_EQ(minInterval, DeviceUsageStatsGroupConst::TWO_HOUR);
340     minInterval = DeviceUsageStatsGroupMap::groupIntervalMap_
341         .at(DeviceUsageStatsGroupConst::ACTIVE_GROUP_DAILY);
342     EXPECT_EQ(minInterval, 2 * DeviceUsageStatsGroupConst::TWO_HOUR);
343     minInterval = DeviceUsageStatsGroupMap::groupIntervalMap_
344         .at(DeviceUsageStatsGroupConst::ACTIVE_GROUP_FIXED);
345     EXPECT_EQ(minInterval, DeviceUsageStatsGroupConst::TWENTY_FOUR_HOUR);
346     minInterval = DeviceUsageStatsGroupMap::groupIntervalMap_
347         .at(DeviceUsageStatsGroupConst::ACTIVE_GROUP_RARE);
348     EXPECT_EQ(minInterval, DeviceUsageStatsGroupConst::FOURTY_EIGHT_HOUR);
349 }
350 
351 /*
352  * @tc.name: DeviceUsageStatisticsTest_DeathRecipient_001
353  * @tc.desc: DeathRecipient_001
354  * @tc.type: FUNC
355  * @tc.require: issuesI5SOZY
356  */
357 HWTEST_F(DeviceUsageStatisticsTest, DeviceUsageStatisticsTest_DeathRecipient_001, Function | MediumTest | Level0)
358 {
359     auto deathTest = std::make_shared<BundleActiveClient::BundleActiveClientDeathRecipient>();
360     deathTest->AddObserver(observer);
361     deathTest->RemoveObserver();
362     deathTest->OnServiceDiedInner();
363 
364     deathTest->observer_ = new (std::nothrow) TestAppGroupChangeCallback();
365     deathTest->OnServiceDiedInner();
366     EXPECT_TRUE(deathTest != nullptr);
367     deathTest->OnRemoteDied(nullptr);
368 }
369 }  // namespace DeviceUsageStats
370 }  // namespace OHOS
371 
372