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