• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023  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 <gtest/hwext/gtest-multithread.h>
23 #include <unistd.h>
24 #include "system_ability_definition.h"
25 
26 #include "bundle_active_client.h"
27 #include "bundle_active_event.h"
28 #include "app_group_callback_stub.h"
29 #include "bundle_active_group_map.h"
30 #include "app_group_callback_info.h"
31 #include "bundle_active_form_record.h"
32 #include "bundle_active_event_stats.h"
33 #include "bundle_active_module_record.h"
34 #include "bundle_active_package_stats.h"
35 #include "app_group_callback_proxy.h"
36 #include "iapp_group_callback.h"
37 
38 using namespace testing::ext;
39 using namespace testing::mt;
40 
41 namespace OHOS {
42 namespace DeviceUsageStats {
43 #ifdef __aarch64__
44 static std::string g_defaultBundleName = "com.huawei.hmos.camera";
45 #else
46 static std::string g_defaultBundleName = "com.ohos.camera";
47 #endif
48 static std::string g_defaultMoudleName = "defaultmodulename";
49 static std::string g_defaultFormName = "defaultformname";
50 static int32_t DEFAULT_DIMENSION = 4;
51 static int64_t DEFAULT_FORMID = 1;
52 static int32_t DEFAULT_USERID = 0;
53 static int32_t COMMON_USERID = 100;
54 static int64_t LARGE_NUM = 20000000000000;
55 static int32_t DEFAULT_GROUP = 10;
56 static std::vector<int32_t> GROUP_TYPE {10, 20, 30, 40, 50};
57 static sptr<IAppGroupCallback> observer = nullptr;
58 
59 class DeviceUsageStatisticsMultiTest : public testing::Test {
60 public:
61     static void SetUpTestCase(void);
62     static void TearDownTestCase(void);
63     void SetUp();
64     void TearDown();
65 };
66 
SetUpTestCase(void)67 void DeviceUsageStatisticsMultiTest::SetUpTestCase(void)
68 {
69 }
70 
TearDownTestCase(void)71 void DeviceUsageStatisticsMultiTest::TearDownTestCase(void)
72 {
73 }
74 
SetUp(void)75 void DeviceUsageStatisticsMultiTest::SetUp(void)
76 {
77 }
78 
TearDown(void)79 void DeviceUsageStatisticsMultiTest::TearDown(void)
80 {
81 }
82 
83 class TestAppGroupChangeCallback : public AppGroupCallbackStub {
84 public:
85     void OnAppGroupChanged(const AppGroupCallbackInfo &appGroupCallbackInfo) override;
86 };
87 
OnAppGroupChanged(const AppGroupCallbackInfo & appGroupCallbackInfo)88 void TestAppGroupChangeCallback::OnAppGroupChanged(const AppGroupCallbackInfo &appGroupCallbackInfo)
89 {
90     BUNDLE_ACTIVE_LOGI("TestAppGroupChangeCallback::OnAppGroupChanged!");
91     MessageParcel data;
92     if (!appGroupCallbackInfo.Marshalling(data)) {
93         BUNDLE_ACTIVE_LOGE("Marshalling fail");
94     }
95     appGroupCallbackInfo.Unmarshalling(data);
96 }
97 
98 /*
99  * @tc.name: DeviceUsageStatisticsMultiTest_IsBundleIdle_001
100  * @tc.desc: isbundleidle
101  * @tc.type: FUNC
102  * @tc.require: issuesI5SOZY
103  */
MultiTestIsBundleIdle(void)104 void MultiTestIsBundleIdle(void)
105 {
106     bool result = false;
107     int32_t errCode = BundleActiveClient::GetInstance().IsBundleIdle(result, g_defaultBundleName, DEFAULT_USERID);
108     EXPECT_EQ(result, false);
109     EXPECT_EQ(errCode, 0);
110 }
111 
112 HWTEST_F(DeviceUsageStatisticsMultiTest, DeviceUsageStatisticsMultiTest_IsBundleIdle_001,
113     Function | MediumTest | Level0)
114 {
115     SET_THREAD_NUM(100);
116     GTEST_RUN_TASK(MultiTestIsBundleIdle);
117 }
118 
119 /*
120  * @tc.name: DeviceUsageStatisticsMultiTest_ReportEvent_001
121  * @tc.desc: report a mock event
122  * @tc.type: FUNC
123  * @tc.require: issuesI5SOZY
124  */
MultiTestReportEvent(void)125 void MultiTestReportEvent(void)
126 {
127     BundleActiveEvent eventA(g_defaultBundleName, g_defaultMoudleName, g_defaultFormName,
128         DEFAULT_DIMENSION, DEFAULT_FORMID, BundleActiveEvent::FORM_IS_CLICKED);
129     BundleActiveClient::GetInstance().ReportEvent(eventA, DEFAULT_USERID);
130     BundleActiveEvent eventB(g_defaultBundleName, g_defaultMoudleName, g_defaultFormName,
131         DEFAULT_DIMENSION, DEFAULT_FORMID, BundleActiveEvent::FORM_IS_REMOVED);
132     BundleActiveClient::GetInstance().ReportEvent(eventB, DEFAULT_USERID);
133 }
134 
135 HWTEST_F(DeviceUsageStatisticsMultiTest, DeviceUsageStatisticsMultiTest_ReportEvent_001, Function | MediumTest | Level0)
136 {
137     SET_THREAD_NUM(100);
138     GTEST_RUN_TASK(MultiTestReportEvent);
139 }
140 
141 /*
142  * @tc.name: DeviceUsageStatisticsMultiTest_QueryBundleEvents_001
143  * @tc.desc: QueryBundleEvents
144  * @tc.type: FUNC
145  * @tc.require: issuesI5SOZY
146  */
MultiTestQueryBundleEvents(void)147 void MultiTestQueryBundleEvents(void)
148 {
149     std::vector<BundleActiveEvent> result;
150     BundleActiveClient::GetInstance().QueryBundleEvents(result, 0, LARGE_NUM, 100);
151     EXPECT_EQ(result.size() > 0, true);
152     EXPECT_NE(BundleActiveClient::GetInstance().QueryBundleEvents(result, LARGE_NUM, LARGE_NUM, 100), 0);
153 }
154 
155 HWTEST_F(DeviceUsageStatisticsMultiTest, DeviceUsageStatisticsMultiTest_QueryBundleEvents_001,
156     Function | MediumTest | Level0)
157 {
158     SET_THREAD_NUM(100);
159     GTEST_RUN_TASK(MultiTestQueryBundleEvents);
160 }
161 
162 /*
163  * @tc.name: DeviceUsageStatisticsMultiTest_QueryCurrentBundleEvents_001
164  * @tc.desc: QueryCurrentBundleEvents
165  * @tc.type: FUNC
166  * @tc.require: issuesI5SOZY
167  */
MultiTestQueryCurrentBundleEvents(void)168 void MultiTestQueryCurrentBundleEvents(void)
169 {
170     std::vector<BundleActiveEvent> result;
171     BundleActiveClient::GetInstance().QueryCurrentBundleEvents(result, 0, LARGE_NUM);
172     EXPECT_EQ(result.size(), 0);
173 }
174 
175 HWTEST_F(DeviceUsageStatisticsMultiTest, DeviceUsageStatisticsMultiTest_QueryCurrentBundleEvents_001,
176     Function | MediumTest | Level0)
177 {
178     SET_THREAD_NUM(100);
179     GTEST_RUN_TASK(MultiTestQueryCurrentBundleEvents);
180 }
181 
182 /*
183  * @tc.name: DeviceUsageStatisticsMultiTest_QueryPackagesStats_001
184  * @tc.desc: querypackagestats
185  * @tc.type: FUNC
186  * @tc.require: issuesI5SOZY
187  */
MultiTestQueryBundleStatsInfoByInterval(void)188 void MultiTestQueryBundleStatsInfoByInterval(void)
189 {
190     std::vector<BundleActivePackageStats> result;
191     BundleActiveClient::GetInstance().QueryBundleStatsInfoByInterval(result, 4, 0, LARGE_NUM);
192     EXPECT_EQ(result.size(), 0);
193     EXPECT_NE(BundleActiveClient::GetInstance().QueryBundleStatsInfoByInterval(result, 4, LARGE_NUM, LARGE_NUM), 0);
194 }
195 
196 HWTEST_F(DeviceUsageStatisticsMultiTest, DeviceUsageStatisticsMultiTest_QueryPackagesStats_001,
197     Function | MediumTest | Level0)
198 {
199     SET_THREAD_NUM(100);
200     GTEST_RUN_TASK(MultiTestQueryBundleStatsInfoByInterval);
201 }
202 
203 /*
204  * @tc.name: DeviceUsageStatisticsMultiTest_QueryBundleStatsInfos_001
205  * @tc.desc: QueryBundleStatsInfos
206  * @tc.type: FUNC
207  * @tc.require: issuesI5QJD9
208  */
MultiTestQueryBundleStatsInfos(void)209 void MultiTestQueryBundleStatsInfos(void)
210 {
211     std::vector<BundleActivePackageStats> result;
212     BundleActiveClient::GetInstance().QueryBundleStatsInfos(result, 4, 0, LARGE_NUM);
213     EXPECT_EQ(result.size(), 0);
214 }
215 
216 HWTEST_F(DeviceUsageStatisticsMultiTest, DeviceUsageStatisticsMultiTest_QueryBundleStatsInfos_001,
217     Function | MediumTest | Level0)
218 {
219     SET_THREAD_NUM(100);
220     GTEST_RUN_TASK(MultiTestQueryBundleStatsInfos);
221 }
222 
223 /*
224  * @tc.name: DeviceUsageStatisticsMultiTest_QueryModuleUsageRecords_001
225  * @tc.desc: QueryModuleUsageRecords
226  * @tc.type: FUNC
227  * @tc.require: issuesI5SOZY
228  */
MultiTestQueryModuleUsageRecords(void)229 void MultiTestQueryModuleUsageRecords(void)
230 {
231     int32_t maxNum = 1;
232     BundleActiveEvent eventA(g_defaultBundleName, g_defaultMoudleName, g_defaultFormName,
233         DEFAULT_DIMENSION, DEFAULT_FORMID, BundleActiveEvent::FORM_IS_CLICKED);
234     BundleActiveClient::GetInstance().ReportEvent(eventA, DEFAULT_USERID);
235     std::vector<BundleActiveModuleRecord> results;
236     int32_t errCode = BundleActiveClient::GetInstance().QueryModuleUsageRecords(maxNum, results, DEFAULT_USERID);
237     EXPECT_EQ(errCode, 0);
238     EXPECT_EQ(results.size(), 0);
239 
240     results.clear();
241     maxNum = 0;
242     errCode = BundleActiveClient::GetInstance().QueryModuleUsageRecords(maxNum, results, DEFAULT_USERID);
243     EXPECT_NE(errCode, 0);
244 
245     results.clear();
246     maxNum = 1001;
247     errCode = BundleActiveClient::GetInstance().QueryModuleUsageRecords(maxNum, results, DEFAULT_USERID);
248     EXPECT_NE(errCode, 0);
249 }
250 
251 HWTEST_F(DeviceUsageStatisticsMultiTest, DeviceUsageStatisticsMultiTest_QueryModuleUsageRecords_001,
252     Function | MediumTest | Level0)
253 {
254     SET_THREAD_NUM(100);
255     GTEST_RUN_TASK(MultiTestQueryModuleUsageRecords);
256 }
257 
258 /*
259  * @tc.name: DeviceUsageStatisticsMultiTest_QueryAppGroup_001
260  * @tc.desc: QueryAppGroup, no bundleName
261  * @tc.type: FUNC
262  * @tc.require: issuesI5SOZY
263  */
MultiTestAppGroup(void)264 void MultiTestAppGroup(void)
265 {
266     BundleActiveClient::GetInstance().SetAppGroup(g_defaultBundleName, DEFAULT_GROUP, COMMON_USERID);
267     int32_t result = 0;
268     BundleActiveClient::GetInstance().QueryAppGroup(result, g_defaultBundleName, COMMON_USERID);
269     bool flag = false;
270     for (auto item = GROUP_TYPE.begin(); item != GROUP_TYPE.end(); item++) {
271         if (*item == result) {
272             flag = true;
273             break;
274         }
275     }
276     EXPECT_EQ(flag, true);
277 }
278 
279 HWTEST_F(DeviceUsageStatisticsMultiTest, DeviceUsageStatisticsMultiTest_AppGroup_001, Function | MediumTest | Level0)
280 {
281     SET_THREAD_NUM(100);
282     GTEST_RUN_TASK(MultiTestAppGroup);
283 }
284 
285 /*
286  * @tc.name: DeviceUsageStatisticsMultiTest_QueryDeviceEventStats_001
287  * @tc.desc: QueryDeviceEventStats
288  * @tc.type: FUNC
289  * @tc.require: issuesI5SOZY
290  */
MultiTestQueryDeviceEventStats(void)291 void MultiTestQueryDeviceEventStats(void)
292 {
293     std::vector<BundleActiveEventStats> eventStats;
294     int32_t errCode = BundleActiveClient::GetInstance().QueryDeviceEventStats(0, LARGE_NUM, eventStats);
295     EXPECT_EQ(errCode, 0);
296     errCode = BundleActiveClient::GetInstance().QueryDeviceEventStats(0, LARGE_NUM, eventStats, COMMON_USERID);
297     EXPECT_EQ(errCode, 0);
298 }
299 
300 HWTEST_F(DeviceUsageStatisticsMultiTest, DeviceUsageStatisticsMultiTest_QueryDeviceEventStats_001,
301     Function | MediumTest | Level0)
302 {
303     SET_THREAD_NUM(100);
304     GTEST_RUN_TASK(MultiTestQueryDeviceEventStats);
305 }
306 
307 /*
308  * @tc.name: DeviceUsageStatisticsMultiTest_QueryNotificationEventStats_001
309  * @tc.desc: QueryNotificationEventStats
310  * @tc.type: FUNC
311  * @tc.require: issuesI5SOZY
312  */
MultiTestQueryNotificationEventStats(void)313 void MultiTestQueryNotificationEventStats(void)
314 {
315     std::vector<BundleActiveEventStats> eventStats;
316     int32_t errCode = BundleActiveClient::GetInstance().QueryNotificationEventStats(0, LARGE_NUM, eventStats);
317     EXPECT_EQ(errCode, 0);
318     errCode = BundleActiveClient::GetInstance().QueryNotificationEventStats(0, LARGE_NUM, eventStats, COMMON_USERID);
319     EXPECT_EQ(errCode, 0);
320 }
321 
322 HWTEST_F(DeviceUsageStatisticsMultiTest, DeviceUsageStatisticsMultiTest_QueryNotificationEventStats_001,
323     Function | MediumTest | Level0)
324 {
325     SET_THREAD_NUM(100);
326     GTEST_RUN_TASK(MultiTestQueryNotificationEventStats);
327 }
328 
329 /*
330  * @tc.name: DeviceUsageStatisticsMultiTest_BundleActiveGroupMap_001
331  * @tc.desc: BundleActiveGroupMap
332  * @tc.type: FUNC
333  * @tc.require: issuesI5SOZY
334  */
MultiTestDeviceUsageStatsGroupMap(void)335 void MultiTestDeviceUsageStatsGroupMap(void)
336 {
337     int64_t minInterval = DeviceUsageStatsGroupMap::groupIntervalMap_
338         .at(DeviceUsageStatsGroupConst::ACTIVE_GROUP_FORCED_SET);
339     EXPECT_EQ(minInterval, 0);
340     minInterval = DeviceUsageStatsGroupMap::groupIntervalMap_
341         .at(DeviceUsageStatsGroupConst::ACTIVE_GROUP_ALIVE);
342     EXPECT_EQ(minInterval, DeviceUsageStatsGroupConst::TWO_HOUR);
343     minInterval = DeviceUsageStatsGroupMap::groupIntervalMap_
344         .at(DeviceUsageStatsGroupConst::ACTIVE_GROUP_DAILY);
345     EXPECT_EQ(minInterval, 2 * DeviceUsageStatsGroupConst::TWO_HOUR);
346     minInterval = DeviceUsageStatsGroupMap::groupIntervalMap_
347         .at(DeviceUsageStatsGroupConst::ACTIVE_GROUP_FIXED);
348     EXPECT_EQ(minInterval, DeviceUsageStatsGroupConst::TWENTY_FOUR_HOUR);
349     minInterval = DeviceUsageStatsGroupMap::groupIntervalMap_
350         .at(DeviceUsageStatsGroupConst::ACTIVE_GROUP_RARE);
351     EXPECT_EQ(minInterval, DeviceUsageStatsGroupConst::FOURTY_EIGHT_HOUR);
352 }
353 
354 HWTEST_F(DeviceUsageStatisticsMultiTest, DeviceUsageStatisticsMultiTest_BundleActiveGroupMap_001,
355     Function | MediumTest | Level0)
356 {
357     SET_THREAD_NUM(100);
358     GTEST_RUN_TASK(MultiTestDeviceUsageStatsGroupMap);
359 }
360 }  // namespace DeviceUsageStats
361 }  // namespace OHOS
362 
363