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