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