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 #include <gtest/gtest.h>
17 #include <thread>
18 
19 #include "perform_reporter.h"
20 #include "window_manager_hilog.h"
21 
22 using namespace testing;
23 using namespace testing::ext;
24 
25 namespace OHOS {
26 namespace Rosen {
27 namespace {
28 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "PerformReporterTest"};
29 }
30 class PerformReporterTest : public testing::Test {
31 public:
32     static void SetUpTestCase();
33     static void TearDownTestCase();
34     virtual void SetUp() override;
35     virtual void TearDown() override;
36     void SimuReportProcess(PerformReporter& pr, const std::vector<uint32_t>& durations);
37     bool PerformDataCmp(const PerformReporter& pr, const uint32_t totalCount, const std::vector<uint32_t>& splitCount);
38 };
39 
SetUpTestCase()40 void PerformReporterTest::SetUpTestCase()
41 {
42 }
43 
TearDownTestCase()44 void PerformReporterTest::TearDownTestCase()
45 {
46 }
47 
SetUp()48 void PerformReporterTest::SetUp()
49 {
50 }
51 
TearDown()52 void PerformReporterTest::TearDown()
53 {
54 }
55 
SimuReportProcess(PerformReporter & pr,const std::vector<uint32_t> & durations)56 void PerformReporterTest::SimuReportProcess(PerformReporter& pr, const std::vector<uint32_t>& durations)
57 {
58     for (auto duration : durations) {
59         pr.start();
60         std::this_thread::sleep_for(std::chrono::milliseconds(duration));
61         pr.end();
62     }
63 }
64 
PerformDataCmp(const PerformReporter & pr,const uint32_t totalCount,const std::vector<uint32_t> & splitCount)65 bool PerformReporterTest::PerformDataCmp(const PerformReporter& pr,
66     const uint32_t totalCount, const std::vector<uint32_t>& splitCount)
67 {
68     if (pr.totalCount_ != totalCount) {
69         WLOGFE("pr.totalCount_=%{public}u, expect=%{public}u", pr.totalCount_.load(), totalCount);
70         return false;
71     }
72 
73     size_t i = 0;
74     for (auto& iter: pr.timeSplitCount_) {
75         if (iter.second != splitCount[i]) {
76             std::ostringstream oss;
77             oss << "pr.timeSplitCount_[" << iter.first << "]=" << iter.second << ", but expect=" << splitCount[i];
78             WLOGI("%{public}s", oss.str().c_str());
79             return false;
80         }
81         i++;
82     }
83 
84     return true;
85 }
86 
87 namespace {
88 /**
89  * @tc.name: StartEnd
90  * @tc.desc: StartEnd test
91  * @tc.type: FUNC
92  */
93 HWTEST_F(PerformReporterTest, StartEnd, Function | SmallTest | Level2)
94 {
95     PerformReporter pr = PerformReporter("TestTag", {100, 200, 300}, 10);
96     SimuReportProcess(pr, {50, 150, 250, 350, 450});
97     ASSERT_EQ(true, PerformDataCmp(pr, 5, {1, 1, 1, 2}));
98 }
99 
100 /**
101  * @tc.name: StartEndClear
102  * @tc.desc: StartEndClear test
103  * @tc.type: FUNC
104  */
105 HWTEST_F(PerformReporterTest, StartEndClear, Function | SmallTest | Level2)
106 {
107     PerformReporter pr = PerformReporter("TestTag", {100, 200, 300}, 3);
108     SimuReportProcess(pr, {50, 150, 250});
109     ASSERT_EQ(true, PerformDataCmp(pr, 0, {0, 0, 0, 0}));
110 }
111 
112 /**
113  * @tc.name: StartEndInvSeq
114  * @tc.desc: StartEndInvSeq test
115  * @tc.type: FUNC
116  */
117 HWTEST_F(PerformReporterTest, StartEndInvSeq, Function | SmallTest | Level2)
118 {
119     PerformReporter pr = PerformReporter("TestTag", {100, 200, 300}, 4);
120     SimuReportProcess(pr, {250, 150, 50});
121     ASSERT_EQ(true, PerformDataCmp(pr, 3, {1, 1, 1, 0}));
122 }
123 
124 /**
125  * @tc.name: PrivateClear
126  * @tc.desc: PrivateClear test
127  * @tc.type: FUNC
128  */
129 HWTEST_F(PerformReporterTest, PrivateClear, Function | SmallTest | Level2)
130 {
131     PerformReporter pr = PerformReporter("TestTag", {100, 200, 300}, 10);
132     SimuReportProcess(pr, {50, 150, 250, 350, 450});
133     ASSERT_EQ(true, PerformDataCmp(pr, 5, {1, 1, 1, 2}));
134 
135     pr.clear();
136     ASSERT_EQ(true, PerformDataCmp(pr, 0, {0, 0, 0, 0}));
137 }
138 
139 /**
140  * @tc.name: GetMsgString001
141  * @tc.desc: GetMsgString test
142  * @tc.type: FUNC
143  */
144 HWTEST_F(PerformReporterTest, GetMsgString001, Function | SmallTest | Level2)
145 {
146     WindowInfoReporter windowInfoReporter;
147     FullInfoMap infoMap;
148     std::string res = windowInfoReporter.GetMsgString(infoMap);
149     ASSERT_EQ(res, "");
150 }
151 
152 /**
153  * @tc.name: GetMsgString002
154  * @tc.desc: GetMsgString test
155  * @tc.type: FUNC
156  */
157 HWTEST_F(PerformReporterTest, GetMsgString002, Function | SmallTest | Level2)
158 {
159     WindowInfoReporter windowInfoReporter;
160     FullInfoMap infoMap;
161     infoMap["bundleName"]["packageName"] = 0;
162     std::string res = windowInfoReporter.GetMsgString(infoMap);
163     std::ostringstream oss;
164     oss << "{";
165     for (auto& bundleInfos : infoMap) {
166         if (bundleInfos.second.empty()) {
167             continue;
168         }
169         oss << "{";
170         for (auto& packageInfo : bundleInfos.second) {
171             oss << "BUNDLE_NAME:" << bundleInfos.first << ",";
172             oss << "ABILITY_NAME:" << packageInfo.first << ",";
173             oss << "COUNT:" << packageInfo.second;
174         }
175         oss << "},";
176     }
177     oss << "};";
178     ASSERT_EQ(res, oss.str());
179 }
180 
181 /**
182  * @tc.name: GetMsgString003
183  * @tc.desc: GetMsgString test
184  * @tc.type: FUNC
185  */
186 HWTEST_F(PerformReporterTest, GetMsgString003, Function | SmallTest | Level2)
187 {
188     WindowInfoReporter windowInfoReporter;
189     BundleNameMap infoMap;
190     std::string res = windowInfoReporter.GetMsgString(infoMap);
191     ASSERT_EQ(res, "");
192 }
193 
194 /**
195  * @tc.name: GetMsgString004
196  * @tc.desc: GetMsgString test
197  * @tc.type: FUNC
198  */
199 HWTEST_F(PerformReporterTest, GetMsgString004, Function | SmallTest | Level2)
200 {
201     WindowInfoReporter windowInfoReporter;
202     BundleNameMap infoMap;
203     infoMap["bundleName"] = 0;
204     std::string res = windowInfoReporter.GetMsgString(infoMap);
205     std::ostringstream oss;
206     oss << "{";
207     for (auto& bundleInfo : infoMap) {
208         oss << "{";
209         oss << "BUNDLE_NAME:" << bundleInfo.first << ",";
210         oss << "COUNT:" << bundleInfo.second;
211         oss << "},";
212     }
213     oss << "};";
214     ASSERT_EQ(res, oss.str());
215 }
216 
217 /**
218  * @tc.name: InsertCreateReportInfo005
219  * @tc.desc: InsertCreateReportInfo test
220  * @tc.type: FUNC
221  */
222 HWTEST_F(PerformReporterTest, InsertCreateReportInfo005, Function | SmallTest | Level2)
223 {
224     int res = 0;
225     WindowInfoReporter windowInfoReporter;
226     std::string bundleName = "bundleName";
227     std::string packageName = "packageName";
228     windowInfoReporter.InsertCreateReportInfo(bundleName);
229     windowInfoReporter.InsertShowReportInfo(bundleName);
230     windowInfoReporter.InsertHideReportInfo(bundleName);
231     windowInfoReporter.InsertDestroyReportInfo(bundleName);
232     windowInfoReporter.InsertNavigationBarReportInfo(bundleName, packageName);
233     ASSERT_EQ(res, 0);
234 }
235 
236 /**
237  * @tc.name: UpdateReportInfo006
238  * @tc.desc: UpdateReportInfo test
239  * @tc.type: FUNC
240  */
241 HWTEST_F(PerformReporterTest, UpdateReportInfo006, Function | SmallTest | Level2)
242 {
243     WindowInfoReporter windowInfoReporter;
244     FullInfoMap infoMap;
245     std::string bundleName;
246     std::string packageName;
247     windowInfoReporter.UpdateReportInfo(infoMap, bundleName, packageName);
248     std::string res = windowInfoReporter.GetMsgString(infoMap);
249     ASSERT_EQ(res, "");
250 }
251 
252 /**
253  * @tc.name: UpdateReportInfo007
254  * @tc.desc: UpdateReportInfo test
255  * @tc.type: FUNC
256  */
257 HWTEST_F(PerformReporterTest, UpdateReportInfo007, Function | SmallTest | Level2)
258 {
259     int res = 0;
260     WindowInfoReporter windowInfoReporter;
261     FullInfoMap infoMap_1;
262     std::string bundleName = "bundleName";
263     std::string packageName = "packageName";
264     infoMap_1["bundleName"]["packageName"] = 0;
265     windowInfoReporter.UpdateReportInfo(infoMap_1, bundleName, packageName);
266     FullInfoMap infoMap_2;
267     infoMap_2["Name"]["packageName"] = 0;
268     windowInfoReporter.UpdateReportInfo(infoMap_2, bundleName, packageName);
269     ASSERT_EQ(res, 0);
270 }
271 
272 /**
273  * @tc.name: UpdateReportInfo008
274  * @tc.desc: UpdateReportInfo test
275  * @tc.type: FUNC
276  */
277 HWTEST_F(PerformReporterTest, UpdateReportInfo008, Function | SmallTest | Level2)
278 {
279     WindowInfoReporter windowInfoReporter;
280     BundleNameMap infoMap;
281     std::string bundleName;
282     windowInfoReporter.UpdateReportInfo(infoMap, bundleName);
283     std::string res = windowInfoReporter.GetMsgString(infoMap);
284     ASSERT_EQ(res, "");
285 }
286 
287 /**
288  * @tc.name: UpdateReportInfo009
289  * @tc.desc: UpdateReportInfo test
290  * @tc.type: FUNC
291  */
292 HWTEST_F(PerformReporterTest, UpdateReportInfo009, Function | SmallTest | Level2)
293 {
294     int res = 0;
295     WindowInfoReporter windowInfoReporter;
296     BundleNameMap infoMap_1;
297     std::string bundleName = "bundleName";
298     infoMap_1["bundleName"] = 0;
299     windowInfoReporter.UpdateReportInfo(infoMap_1, bundleName);
300     BundleNameMap infoMap_2;
301     infoMap_2["Name"] = 0;
302     windowInfoReporter.UpdateReportInfo(infoMap_2, bundleName);
303     ASSERT_EQ(res, 0);
304 }
305 
306 /**
307  * @tc.name: ReportBackButtonInfoImmediately010
308  * @tc.desc: ReportBackButtonInfoImmediately test
309  * @tc.type: FUNC
310  */
311 HWTEST_F(PerformReporterTest, ReportBackButtonInfoImmediately010, Function | SmallTest | Level2)
312 {
313     int res = 0;
314     WindowInfoReporter windowInfoReporter;
315     windowInfoReporter.ReportBackButtonInfoImmediately();
316     ASSERT_EQ(res, 0);
317 }
318 
319 /**
320  * @tc.name: ReportZeroOpacityInfoImmediately011
321  * @tc.desc: ReportZeroOpacityInfoImmediately test
322  * @tc.type: FUNC
323  */
324 HWTEST_F(PerformReporterTest, ReportZeroOpacityInfoImmediately011, Function | SmallTest | Level2)
325 {
326     int res = 0;
327     std::string bundleName;
328     std::string packageName = "packageName";
329     WindowInfoReporter windowInfoReporter;
330     windowInfoReporter.ReportZeroOpacityInfoImmediately(bundleName, packageName);
331     bundleName = "bundleName";
332     windowInfoReporter.ReportZeroOpacityInfoImmediately(bundleName, packageName);
333     ASSERT_EQ(res, 0);
334 }
335 
336 /**
337  * @tc.name: ReportStartWindow012
338  * @tc.desc: ReportStartWindow test
339  * @tc.type: FUNC
340  */
341 HWTEST_F(PerformReporterTest, ReportStartWindow012, Function | SmallTest | Level2)
342 {
343     int res = 0;
344     std::string bundleName = "bundleName";
345     std::string windowName = "windowName";
346     WindowInfoReporter windowInfoReporter;
347     windowInfoReporter.ReportStartWindow(bundleName, windowName);
348     ASSERT_EQ(res, 0);
349 }
350 
351 /**
352  * @tc.name: ReportRecordedInfos013
353  * @tc.desc: ReportRecordedInfos test
354  * @tc.type: FUNC
355  */
356 HWTEST_F(PerformReporterTest, ReportRecordedInfos013, Function | SmallTest | Level2)
357 {
358     int res = 0;
359     WindowInfoReporter windowInfoReporter;
360     windowInfoReporter.ReportRecordedInfos();
361     ASSERT_EQ(res, 0);
362 }
363 
364 /**
365  * @tc.name: ReportContainerStartBegin014
366  * @tc.desc: ReportContainerStartBegin test
367  * @tc.type: FUNC
368  */
369 HWTEST_F(PerformReporterTest, ReportContainerStartBegin014, Function | SmallTest | Level2)
370 {
371     int res = 0;
372     int32_t missionId = 1;
373     std::string bundleName = "bundleName";
374     int64_t timestamp = 1;
375     WindowInfoReporter windowInfoReporter;
376     windowInfoReporter.ReportContainerStartBegin(missionId, bundleName, timestamp);
377     ASSERT_EQ(res, 0);
378 }
379 
380 /**
381  * @tc.name: Report015
382  * @tc.desc: Report test
383  * @tc.type: FUNC
384  */
385 HWTEST_F(PerformReporterTest, Report015, Function | SmallTest | Level2)
386 {
387     int res = 0;
388     std::string reportTag = "reportTag";
389     std::string msg;
390     WindowInfoReporter windowInfoReporter;
391     windowInfoReporter.Report(reportTag, msg);
392     msg = "msg";
393     windowInfoReporter.Report(reportTag, msg);
394     ASSERT_EQ(res, 0);
395 }
396 
397 /**
398  * @tc.name: ClearRecordedInfos016
399  * @tc.desc: ClearRecordedInfos test
400  * @tc.type: FUNC
401  */
402 HWTEST_F(PerformReporterTest, ClearRecordedInfos016, Function | SmallTest | Level2)
403 {
404     int res = 0;
405     WindowInfoReporter windowInfoReporter;
406     windowInfoReporter.ClearRecordedInfos();
407     ASSERT_EQ(res, 0);
408 }
409 
410 /**
411  * @tc.name: ReportWindowProfileInfo017
412  * @tc.desc: ReportWindowProfileInfo test
413  * @tc.type: FUNC
414  */
415 HWTEST_F(PerformReporterTest, ReportWindowProfileInfo017, Function | SmallTest | Level2)
416 {
417     int32_t res = 0;
418     WindowProfileInfo windowProfileInfo;
419     windowProfileInfo.bundleName = "bundleName";
420     windowProfileInfo.windowLocatedScreen = 0;
421     windowProfileInfo.windowSceneMode = 102;
422     windowProfileInfo.windowVisibleState = 2;
423     WindowInfoReporter windowInfoReporter;
424     res = windowInfoReporter.ReportWindowProfileInfo(windowProfileInfo);
425     ASSERT_EQ(res, 0);
426 }
427 }
428 } // namespace Rosen
429 } // namespace OHOS