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 #include "gtest/gtest.h"
17 #include "interfaces/inner_api/ace/ui_event_observer.h"
18
19 #include "base/log/log.h"
20 #include "base/memory/ace_type.h"
21 #include "base/utils/utils.h"
22 #include "core/common/recorder/event_config.h"
23 #include "core/common/recorder/event_controller.h"
24 #include "core/common/recorder/event_recorder.h"
25 #include "core/common/recorder/exposure_processor.h"
26 #include "core/common/recorder/node_data_cache.h"
27 #include "core/components_ng/pattern/stage/page_info.h"
28 #include "core/components_ng/pattern/stage/page_pattern.h"
29
30 using namespace testing;
31 using namespace testing::ext;
32
33 namespace OHOS::Ace {
34 class DemoUIEventObserver : public UIEventObserver {
35 public:
36 DemoUIEventObserver() = default;
37 ~DemoUIEventObserver() override = default;
38
GetEventType() const39 int32_t GetEventType() const
40 {
41 return eventType_;
42 }
43
NotifyUIEvent(int32_t eventType,const std::unordered_map<std::string,std::string> & eventParams)44 virtual void NotifyUIEvent(int32_t eventType, const std::unordered_map<std::string, std::string>& eventParams)
45 {
46 LOGI("NotifyUIEvent eventType %{public}d", eventType);
47 eventType_ = eventType;
48 }
49
50 private:
51 int32_t eventType_;
52 };
53
54 class EventRecorderTest : public testing::Test {
55 public:
SetUpTestCase()56 static void SetUpTestCase() {}
TearDownTestCase()57 static void TearDownTestCase() {}
SetUp()58 void SetUp() {}
TearDown()59 void TearDown() {}
60 };
61
GetConfig(std::string & config)62 void GetConfig(std::string& config)
63 {
64 config =
65 "{\"enable\":true,\"switch\":{\"page\":true,\"component\":true,\"exposure\":true},\"config\":[{\"pageUrl\":"
66 "\"pages/"
67 "Index\",\"shareNode\":[\"hahaha\",\"btn_TitleExpand\",\"btn_OpenSelf\",\"btn_Screenshot\",\"btn_inspect\","
68 "\"btn_xxx\",\"\"],\"exposureCfg\":[{\"id\":\"btn_Grid\",\"ratio\":0.75,\"duration\":5000},{\"id\":\"btn_"
69 "TitleExpand\",\"ratio\":0.9,\"duration\":1000}]},{\"pageUrl\":\"pages/"
70 "ScrollPage\",\"shareNode\":[\"scroll_item_1\"],\"exposureCfg\":[{\"id\":\"scroll_item_2\",\"ratio\":0.85,"
71 "\"duration\":5000},{\"id\":\"scroll_item_12\",\"ratio\":0.4,\"duration\":3000}]}]}";
72 }
73
CreatePageNode(const std::string pageUrl)74 RefPtr<NG::FrameNode> CreatePageNode(const std::string pageUrl)
75 {
76 auto pageNodeId = ElementRegister::GetInstance()->MakeUniqueId();
77 return NG::FrameNode::GetOrCreateFrameNode("page", pageNodeId, [pageUrl]() {
78 return AceType::MakeRefPtr<NG::PagePattern>(AceType::MakeRefPtr<NG::PageInfo>(1, pageUrl, pageUrl + ".js"));
79 });
80 }
81
82 /**
83 * @tc.name: EventRecorderTest001
84 * @tc.desc: Test register.
85 * @tc.type: FUNC
86 */
87 HWTEST_F(EventRecorderTest, EventRecorderTest001, TestSize.Level1)
88 {
89 /**
90 * @tc.steps: step1. call the Register first.
91 * @tc.expected: step1. register success.
92 */
93 std::string config;
94 GetConfig(config);
95 auto observer = std::make_shared<DemoUIEventObserver>();
96 Recorder::EventController::Get().Register(config, observer);
97
98 Recorder::NodeDataCache::Get().OnPageShow("pages/Index");
99
100 Recorder::ExposureCfg exposureCfg = { "", 0.0, 0 };
101 Recorder::NodeDataCache::Get().GetExposureCfg("pages/Index", "btn_Grid", exposureCfg);
102 EXPECT_EQ(exposureCfg.id, "btn_Grid");
103 EXPECT_EQ(exposureCfg.duration, 5000);
104
105 exposureCfg = { "", 0.0, 0 };
106 Recorder::NodeDataCache::Get().GetExposureCfg("pages/Index", "", exposureCfg);
107 EXPECT_EQ(exposureCfg.id, "");
108
109 exposureCfg = { "", 0.0, 0 };
110 Recorder::NodeDataCache::Get().GetExposureCfg("pages/Index", "xyz", exposureCfg);
111 EXPECT_EQ(exposureCfg.id, "");
112
113 Recorder::NodeDataCache::Get().OnPageShow("pages/ScrollPage");
114 exposureCfg = { "", 0.0, 0 };
115 Recorder::NodeDataCache::Get().GetExposureCfg("pages/ScrollPage", "btn_Grid", exposureCfg);
116 EXPECT_EQ(exposureCfg.id, "");
117
118 exposureCfg = { "", 0.0, 0 };
119 Recorder::NodeDataCache::Get().GetExposureCfg("pages/ScrollPage", "scroll_item_2", exposureCfg);
120 EXPECT_EQ(exposureCfg.id, "scroll_item_2");
121 EXPECT_NEAR(exposureCfg.ratio, 0.85, 0.00001f);
122
123 /**
124 * @tc.steps: step2. call the Unregister first.
125 * @tc.expected: step2. unregister success.
126 */
127 Recorder::EventController::Get().Unregister(observer);
128 exposureCfg = { "", 0.0, 0 };
129 Recorder::NodeDataCache::Get().GetExposureCfg("pages/ScrollPage", "scroll_item_2", exposureCfg);
130 EXPECT_EQ(exposureCfg.id, "");
131 }
132
133 /**
134 * @tc.name: EventRecorderTest002
135 * @tc.desc: Test node data cache.
136 * @tc.type: FUNC
137 */
138 HWTEST_F(EventRecorderTest, EventRecorderTest002, TestSize.Level1)
139 {
140 std::string config;
141 GetConfig(config);
142 auto observer = std::make_shared<DemoUIEventObserver>();
143 Recorder::EventController::Get().Register(config, observer);
144
145 /**
146 * @tc.steps: step1. test index page.
147 * @tc.expected: step1. get value success.
148 */
149 Recorder::NodeDataCache::Get().OnPageShow("pages/Index");
150 auto pageNode = CreatePageNode("pages/Index");
151
152 Recorder::NodeDataCache::Get().PutString(pageNode, "btn_TitleExpand", "abc");
153 Recorder::NodeDataCache::Get().PutInt(pageNode, "btn_OpenSelf", 2);
154 Recorder::NodeDataCache::Get().PutBool(pageNode, "btn_Screenshot", true);
155 Recorder::NodeDataCache::Get().PutMultiple(pageNode, "btn_inspect", "inspect", 11);
156 Recorder::NodeDataCache::Get().PutMultiple(pageNode, "btn_xxx", "xxx", true);
157 std::vector<std::string> values = { "a", "b", "c" };
158 Recorder::NodeDataCache::Get().PutMultiple(pageNode, "hahaha", "xixi", values);
159 auto nodeValues = std::unordered_map<std::string, std::string>();
160 nodeValues.emplace("btn_TitleExpand", "");
161 nodeValues.emplace("btn_OpenSelf", "");
162 nodeValues.emplace("btn_Screenshot", "");
163 nodeValues.emplace("btn_inspect", "");
164 nodeValues.emplace("btn_xxx", "");
165 nodeValues.emplace("hahaha", "");
166 nodeValues.emplace("hello", "");
167 nodeValues.emplace("world", "xyz");
168 Recorder::NodeDataCache::Get().GetNodeData("pages/Index", nodeValues);
169
170 auto iter = nodeValues.find("btn_TitleExpand");
171 EXPECT_EQ(iter->second, "abc");
172
173 iter = nodeValues.find("btn_OpenSelf");
174 EXPECT_EQ(iter->second, "2");
175
176 iter = nodeValues.find("btn_Screenshot");
177 EXPECT_EQ(iter->second, "true");
178
179 iter = nodeValues.find("btn_inspect");
180 EXPECT_EQ(iter->second, "{\"text\":\"inspect\",\"index\":11}");
181
182 iter = nodeValues.find("btn_xxx");
183 EXPECT_EQ(iter->second, "{\"text\":\"xxx\",\"checked\":true}");
184
185 iter = nodeValues.find("hahaha");
186 EXPECT_EQ(iter->second, "{\"text\":\"xixi\",\"textArray\":[\"a\",\"b\",\"c\"]}");
187
188 iter = nodeValues.find("hello");
189 EXPECT_EQ(iter->second, "");
190
191 iter = nodeValues.find("world");
192 EXPECT_EQ(iter->second, "xyz");
193
194 Recorder::EventController::Get().Unregister(observer);
195 }
196
197 /**
198 * @tc.name: EventRecorderTest003
199 * @tc.desc: Test node data cache.
200 * @tc.type: FUNC
201 */
202 HWTEST_F(EventRecorderTest, EventRecorderTest003, TestSize.Level1)
203 {
204 std::string config;
205 GetConfig(config);
206 auto observer = std::make_shared<DemoUIEventObserver>();
207 Recorder::EventController::Get().Register(config, observer);
208
209 Recorder::NodeDataCache::Get().OnPageShow("pages/Index");
210 auto pageNode = CreatePageNode("pages/Index");
211
212 Recorder::NodeDataCache::Get().PutString(pageNode, "btn_TitleExpand", "abc");
213 Recorder::NodeDataCache::Get().PutInt(pageNode, "btn_OpenSelf", 2);
214 Recorder::NodeDataCache::Get().PutBool(pageNode, "btn_Screenshot", true);
215 Recorder::NodeDataCache::Get().PutMultiple(pageNode, "btn_inspect", "inspect", 11);
216 Recorder::NodeDataCache::Get().PutMultiple(pageNode, "btn_xxx", "xxx", true);
217 std::vector<std::string> values = { "a", "b", "c" };
218 Recorder::NodeDataCache::Get().PutMultiple(pageNode, "hahaha", "xixi", values);
219
220 /**
221 * @tc.steps: step1. test scroll page.
222 * @tc.expected: step1. get value success.
223 */
224 Recorder::NodeDataCache::Get().OnPageShow("pages/ScrollPage");
225 auto pageNode2 = CreatePageNode("pages/ScrollPage");
226
227 std::vector<std::string> values2 = { "x", "y", "z" };
228 Recorder::NodeDataCache::Get().PutStringArray(pageNode2, "scroll_item_1", values2);
229 auto nodeValues2 = std::unordered_map<std::string, std::string>();
230 nodeValues2.emplace("btn_TitleExpand", "");
231 nodeValues2.emplace("scroll_item_1", "");
232 Recorder::NodeDataCache::Get().GetNodeData("pages/ScrollPage", nodeValues2);
233
234 auto iter2 = nodeValues2.find("btn_TitleExpand");
235 EXPECT_NE(iter2->second, "abc");
236
237 iter2 = nodeValues2.find("scroll_item_1");
238 EXPECT_EQ(iter2->second, "[\"x\",\"y\",\"z\"]");
239
240 Recorder::EventController::Get().Unregister(observer);
241 }
242
243 /**
244 * @tc.name: EventRecorderTest004
245 * @tc.desc: Test node data cache.
246 * @tc.type: FUNC
247 */
248 HWTEST_F(EventRecorderTest, EventRecorderTest004, TestSize.Level1)
249 {
250 std::string config;
251 GetConfig(config);
252 auto observer = std::make_shared<DemoUIEventObserver>();
253 Recorder::EventController::Get().Register(config, observer);
254
255 Recorder::NodeDataCache::Get().OnPageShow("pages/Index");
256 auto pageNode = CreatePageNode("pages/Index");
257
258 Recorder::NodeDataCache::Get().PutString(pageNode, "btn_TitleExpand", "abc");
259 Recorder::NodeDataCache::Get().PutInt(pageNode, "btn_OpenSelf", 2);
260 Recorder::NodeDataCache::Get().PutBool(pageNode, "btn_Screenshot", true);
261 Recorder::NodeDataCache::Get().PutMultiple(pageNode, "btn_inspect", "inspect", 11);
262 Recorder::NodeDataCache::Get().PutMultiple(pageNode, "btn_xxx", "xxx", true);
263 std::vector<std::string> values = { "a", "b", "c" };
264 Recorder::NodeDataCache::Get().PutMultiple(pageNode, "hahaha", "xixi", values);
265
266 Recorder::NodeDataCache::Get().OnPageShow("pages/ScrollPage");
267 auto pageNode2 = CreatePageNode("pages/ScrollPage");
268
269 std::vector<std::string> values2 = { "x", "y", "z" };
270 Recorder::NodeDataCache::Get().PutStringArray(pageNode2, "scroll_item_1", values2);
271
272 /**
273 * @tc.steps: step1. test pop scroll page.
274 * @tc.expected: step1. get value success.
275 */
276 Recorder::NodeDataCache::Get().OnBeforePagePop();
277
278 auto nodeValues3 = std::unordered_map<std::string, std::string>();
279 nodeValues3.emplace("btn_TitleExpand", "");
280 nodeValues3.emplace("scroll_item_1", "");
281 Recorder::NodeDataCache::Get().GetNodeData("pages/ScrollPage", nodeValues3);
282
283 auto iter3 = nodeValues3.find("scroll_item_1");
284 EXPECT_EQ(iter3->second, "");
285
286 iter3 = nodeValues3.find("btn_TitleExpand");
287 EXPECT_EQ(iter3->second, "");
288
289 Recorder::EventController::Get().Unregister(observer);
290 }
291
292 /**
293 * @tc.name: EventRecorderTest005
294 * @tc.desc: Test node data cache.
295 * @tc.type: FUNC
296 */
297 HWTEST_F(EventRecorderTest, EventRecorderTest005, TestSize.Level1)
298 {
299 std::string config;
300 GetConfig(config);
301 auto observer = std::make_shared<DemoUIEventObserver>();
302 Recorder::EventController::Get().Register(config, observer);
303
304 /**
305 * @tc.steps: step1. test index page.
306 * @tc.expected: step1. get value success.
307 */
308 Recorder::NodeDataCache::Get().OnPageShow("pages/Index");
309 auto pageNode = CreatePageNode("pages/Index");
310
311 Recorder::NodeDataCache::Get().PutString(pageNode, "btn_TitleExpand", "abc");
312 Recorder::NodeDataCache::Get().PutInt(pageNode, "btn_OpenSelf", 2);
313 Recorder::NodeDataCache::Get().PutBool(pageNode, "btn_Screenshot", true);
314 Recorder::NodeDataCache::Get().PutMultiple(pageNode, "btn_inspect", "inspect", 11);
315 Recorder::NodeDataCache::Get().PutMultiple(pageNode, "btn_xxx", "xxx", true);
316 std::vector<std::string> values = { "a", "b", "c" };
317 Recorder::NodeDataCache::Get().PutMultiple(pageNode, "hahaha", "xixi", values);
318
319 Recorder::NodeDataCache::Get().OnPageShow("pages/ScrollPage");
320 auto pageNode2 = CreatePageNode("pages/ScrollPage");
321
322 std::vector<std::string> values2 = { "x", "y", "z" };
323 Recorder::NodeDataCache::Get().PutStringArray(pageNode2, "scroll_item_1", values2);
324
325 Recorder::NodeDataCache::Get().OnBeforePagePop();
326
327 /**
328 * @tc.steps: step1. test index page.
329 * @tc.expected: step1. get value success.
330 */
331 auto nodeValues4 = std::unordered_map<std::string, std::string>();
332 nodeValues4.emplace("btn_TitleExpand", "");
333 nodeValues4.emplace("btn_OpenSelf", "");
334 nodeValues4.emplace("btn_Screenshot", "");
335 Recorder::NodeDataCache::Get().GetNodeData("pages/Index", nodeValues4);
336
337 auto iter4 = nodeValues4.find("btn_TitleExpand");
338 EXPECT_EQ(iter4->second, "abc");
339
340 iter4 = nodeValues4.find("btn_OpenSelf");
341 EXPECT_EQ(iter4->second, "2");
342
343 iter4 = nodeValues4.find("btn_Screenshot");
344 EXPECT_EQ(iter4->second, "true");
345
346 Recorder::EventController::Get().Unregister(observer);
347 }
348
349 /**
350 * @tc.name: EventRecorderTest006
351 * @tc.desc: Test node data cache.
352 * @tc.type: FUNC
353 */
354 HWTEST_F(EventRecorderTest, EventRecorderTest006, TestSize.Level1)
355 {
356 std::string config;
357 GetConfig(config);
358 auto observer = std::make_shared<DemoUIEventObserver>();
359 Recorder::EventController::Get().Register(config, observer);
360
361 /**
362 * @tc.steps: step1. test index page.
363 * @tc.expected: step1. get value success.
364 */
365 Recorder::NodeDataCache::Get().OnPageShow("pages/Index");
366 auto pageNode = CreatePageNode("pages/Index");
367
368 Recorder::NodeDataCache::Get().PutString(pageNode, "btn_TitleExpand", "abc");
369 Recorder::NodeDataCache::Get().PutInt(pageNode, "btn_OpenSelf", 2);
370 Recorder::NodeDataCache::Get().PutBool(pageNode, "btn_Screenshot", true);
371 Recorder::NodeDataCache::Get().PutMultiple(pageNode, "btn_inspect", "inspect", 11);
372 Recorder::NodeDataCache::Get().PutMultiple(pageNode, "btn_xxx", "xxx", true);
373 std::vector<std::string> values = { "a", "b", "c" };
374 Recorder::NodeDataCache::Get().PutMultiple(pageNode, "hahaha", "xixi", values);
375
376 Recorder::NodeDataCache::Get().OnPageShow("pages/ScrollPage");
377 auto pageNode2 = CreatePageNode("pages/ScrollPage");
378
379 std::vector<std::string> values2 = { "x", "y", "z" };
380 Recorder::NodeDataCache::Get().PutStringArray(pageNode2, "scroll_item_1", values2);
381
382 Recorder::NodeDataCache::Get().OnBeforePagePop();
383 Recorder::NodeDataCache::Get().OnPageShow("pages/Index");
384
385 /**
386 * @tc.steps: step1. test update value.
387 * @tc.expected: step1. get value success.
388 */
389 Recorder::NodeDataCache::Get().PutString(pageNode, "btn_TitleExpand", "hello");
390 auto nodeValues5 = std::unordered_map<std::string, std::string>();
391 nodeValues5.emplace("btn_TitleExpand", "");
392 Recorder::NodeDataCache::Get().GetNodeData("pages/Index", nodeValues5);
393
394 auto iter5 = nodeValues5.find("btn_TitleExpand");
395 EXPECT_EQ(iter5->second, "hello");
396
397 Recorder::EventController::Get().Unregister(observer);
398 }
399
400 /**
401 * @tc.name: EventRecorderTest007
402 * @tc.desc: Test node data cache.
403 * @tc.type: FUNC
404 */
405 HWTEST_F(EventRecorderTest, EventRecorderTest007, TestSize.Level1)
406 {
407 std::string config;
408 GetConfig(config);
409 auto observer = std::make_shared<DemoUIEventObserver>();
410 Recorder::EventController::Get().Register(config, observer);
411
412 Recorder::NodeDataCache::Get().OnPageShow("pages/Index");
413 auto pageNode = CreatePageNode("pages/Index");
414
415 Recorder::NodeDataCache::Get().PutString(pageNode, "btn_TitleExpand", "abc");
416 Recorder::NodeDataCache::Get().PutInt(pageNode, "btn_OpenSelf", 2);
417 Recorder::NodeDataCache::Get().PutBool(pageNode, "btn_Screenshot", true);
418 Recorder::NodeDataCache::Get().PutMultiple(pageNode, "btn_inspect", "inspect", 11);
419 Recorder::NodeDataCache::Get().PutMultiple(pageNode, "btn_xxx", "xxx", true);
420 std::vector<std::string> values = { "a", "b", "c" };
421 Recorder::NodeDataCache::Get().PutMultiple(pageNode, "hahaha", "xixi", values);
422
423 Recorder::NodeDataCache::Get().OnPageShow("pages/ScrollPage");
424 auto pageNode2 = CreatePageNode("pages/ScrollPage");
425
426 std::vector<std::string> values2 = { "x", "y", "z" };
427 Recorder::NodeDataCache::Get().PutStringArray(pageNode2, "scroll_item_1", values2);
428
429 Recorder::NodeDataCache::Get().OnBeforePagePop();
430
431 Recorder::NodeDataCache::Get().PutString(pageNode, "btn_TitleExpand", "hello");
432
433 /**
434 * @tc.steps: step1. test clear.
435 * @tc.expected: step1. get value success.
436 */
437 Recorder::NodeDataCache::Get().Clear("pages/Index");
438 auto nodeValues6 = std::unordered_map<std::string, std::string>();
439 nodeValues6.emplace("btn_TitleExpand", "");
440 auto iter6 = nodeValues6.find("btn_TitleExpand");
441 EXPECT_EQ(iter6->second, "");
442
443 Recorder::EventController::Get().Unregister(observer);
444 }
445
446 /**
447 * @tc.name: EventRecorderTest008
448 * @tc.desc: Test node exposure.
449 * @tc.type: FUNC
450 */
451 HWTEST_F(EventRecorderTest, EventRecorderTest008, TestSize.Level1)
452 {
453 std::string config;
454 GetConfig(config);
455 auto observer = std::make_shared<DemoUIEventObserver>();
456 Recorder::EventController::Get().Register(config, observer);
457 Recorder::NodeDataCache::Get().OnPageShow("pages/Index");
458 auto exposure = AceType::MakeRefPtr<Recorder::ExposureProcessor>("pages/Index", "btn_TitleExpand");
459 EXPECT_TRUE(exposure->IsNeedRecord());
460 EXPECT_NEAR(exposure->GetRatio(), 0.9, 0.00001f);
461 exposure->OnVisibleChange(true);
462 sleep(2);
463 exposure->OnVisibleChange(false);
464
465 sleep(1);
466 EXPECT_EQ(observer->GetEventType(), static_cast<int32_t>(Recorder::EventType::EXPOSURE));
467 Recorder::EventController::Get().Unregister(observer);
468 }
469
470 /**
471 * @tc.name: EventRecorderTest009
472 * @tc.desc: Test node recorder.
473 * @tc.type: FUNC
474 */
475 HWTEST_F(EventRecorderTest, EventRecorderTest009, TestSize.Level1)
476 {
477 std::string config;
478 GetConfig(config);
479 auto observer = std::make_shared<DemoUIEventObserver>();
480 Recorder::EventController::Get().Register(config, observer);
481
482 Recorder::EventParamsBuilder builder1;
483 builder1.SetId("hello").SetPageUrl("pages/Index").SetText("world");
484 LOGI("OnClick");
485 Recorder::EventRecorder::Get().OnClick(std::move(builder1));
486 sleep(1);
487
488 Recorder::EventParamsBuilder builder2;
489 builder2.SetId("hello").SetPageUrl("pages/Index").SetText("world").SetChecked(true);
490 LOGI("OnChange");
491 Recorder::EventRecorder::Get().OnChange(std::move(builder2));
492 sleep(1);
493 EXPECT_EQ(observer->GetEventType(), static_cast<int32_t>(Recorder::EventType::CHANGE));
494
495 Recorder::EventParamsBuilder builder3;
496 builder3.SetId("hello")
497 .SetPageUrl("pages/Index")
498 .SetText("weather")
499 .SetEventType(Recorder::EventType::SEARCH_SUBMIT);
500 LOGI("OnEvent");
501 Recorder::EventRecorder::Get().OnEvent(std::move(builder3));
502 sleep(1);
503 EXPECT_EQ(observer->GetEventType(), static_cast<int32_t>(Recorder::EventType::SEARCH_SUBMIT));
504
505 Recorder::EventRecorder::Get().OnPageShow("pages/Index", "from moon");
506 sleep(1);
507 EXPECT_EQ(observer->GetEventType(), static_cast<int32_t>(Recorder::EventType::PAGE_SHOW));
508
509 Recorder::EventRecorder::Get().OnPageHide("pages/Index", 10000);
510 sleep(1);
511 EXPECT_EQ(observer->GetEventType(), static_cast<int32_t>(Recorder::EventType::PAGE_HIDE));
512
513 Recorder::EventParamsBuilder builder4;
514 builder4.SetText("tom");
515 Recorder::EventRecorder::Get().OnNavDstShow(std::move(builder4));
516 sleep(1);
517 EXPECT_EQ(observer->GetEventType(), static_cast<int32_t>(Recorder::EventType::PAGE_SHOW));
518
519 Recorder::EventParamsBuilder builder5;
520 builder5.SetText("tom");
521 Recorder::EventRecorder::Get().OnNavDstHide(std::move(builder5));
522 sleep(1);
523 EXPECT_EQ(observer->GetEventType(), static_cast<int32_t>(Recorder::EventType::PAGE_HIDE));
524
525 Recorder::EventController::Get().Unregister(observer);
526 }
527
528 /**
529 * @tc.name: EventRecorderTest010
530 * @tc.desc: Test ToString MapToString
531 * @tc.type: FUNC
532 */
533 HWTEST_F(EventRecorderTest, EventRecorderTest010, TestSize.Level1)
534 {
535 Recorder::EventParamsBuilder eventBuilder;
536 std::string ret = eventBuilder.ToString();
537 EXPECT_EQ(ret, "{}");
538 std::string ret2 = eventBuilder.SetEventType(Recorder::EventType::PAGE_SHOW).ToString();
539 EXPECT_EQ(ret2, "{eventType:1, }");
540 const std::shared_ptr<std::unordered_map<std::string, std::string>> input = nullptr;
541 std::string ret3 = Recorder::MapToString(input);
542 EXPECT_EQ(ret3, "");
543 const std::shared_ptr<std::unordered_map<std::string, std::string>> input2 =
544 std::make_shared<std::unordered_map<std::string, std::string>>();
545 input2->emplace("key1", "value1");
546 std::string ret4 = Recorder::MapToString(input2);
547 EXPECT_EQ(ret4, "{key1:value1, }");
548 }
549
550 /**
551 * @tc.name: EventRecorderTest011
552 * @tc.desc: Test SetContainerInfo
553 * @tc.type: FUNC
554 */
555 HWTEST_F(EventRecorderTest, EventRecorderTest011, TestSize.Level1)
556 {
557 // windowName = "$HA_FLOAT_WINDOW$"
558 std::string windowName = "$HA_FLOAT_WINDOW$";
559 int32_t id = 1;
560 bool foreground = true;
561 Recorder::EventRecorder::Get().SetContainerInfo(windowName, id, foreground);
562 EXPECT_EQ(Recorder::EventRecorder::Get().GetContainerId() == -1, true);
563 Recorder::EventRecorder::Get().SetFocusContainerInfo(windowName, id);
564 EXPECT_EQ(Recorder::EventRecorder::Get().GetContainerId() == -1, true);
565 // windowName = "pages/Index",foreground = true
566 windowName = "pages/Index";
567 Recorder::EventRecorder::Get().SetContainerInfo(windowName, id, true);
568 EXPECT_EQ(Recorder::EventRecorder::Get().GetContainerId() == -1, true);
569 Recorder::EventRecorder::Get().SetFocusContainerInfo(windowName, 2);
570 EXPECT_EQ(Recorder::EventRecorder::Get().GetContainerId() == 2, true);
571 // windowName = "pages/Index",foreground = false
572 Recorder::EventRecorder::Get().SetContainerInfo(windowName, id, false);
573 EXPECT_EQ(Recorder::EventRecorder::Get().GetContainerId() == -1, true);
574 }
575
576 /**
577 * @tc.name: EventRecorderTest012
578 * @tc.desc: Test SetDescription/SetNavDst
579 * @tc.type: FUNC
580 */
581 HWTEST_F(EventRecorderTest, EventRecorderTest012, TestSize.Level1)
582 {
583 Recorder::EventParamsBuilder eventBuilder;
584 std::string desc = "desc";
585 std::shared_ptr<std::unordered_map<std::string, std::string>> ret = eventBuilder.SetDescription(desc).build();
586 EXPECT_EQ(ret->size(), 1);
587 std::string dstName = "dstName";
588 std::shared_ptr<std::unordered_map<std::string, std::string>> ret2 = eventBuilder.SetNavDst(dstName).build();
589 EXPECT_EQ(ret2->size(), 2);
590 }
591
592 /**
593 * @tc.name: SetContainerInfo001
594 * @tc.desc: Test SetContainerInfo.
595 * @tc.type: FUNC
596 */
597 HWTEST_F(EventRecorderTest, SetContainerInfo001, TestSize.Level1)
598 {
599 std::string windowName = "$HA_FLOAT_WINDOW$";
600 Recorder::EventRecorder::Get().SetContainerInfo(windowName, 0, true);
601 EXPECT_EQ(Recorder::EventRecorder::Get().GetContainerId(), -1);
602 }
603
604 /**
605 * @tc.name: SetContainerInfo002
606 * @tc.desc: Test SetContainerInfo.
607 * @tc.type: FUNC
608 */
609 HWTEST_F(EventRecorderTest, SetContainerInfo002, TestSize.Level1)
610 {
611 std::string windowName = "";
612 Recorder::EventRecorder::Get().SetContainerInfo(windowName, 0, true);
613 EXPECT_NE(Recorder::EventRecorder::Get().GetContainerId(), 0);
614 }
615
616 /**
617 * @tc.name: SetContainerInfo003
618 * @tc.desc: Test SetContainerInfo.
619 * @tc.type: FUNC
620 */
621 HWTEST_F(EventRecorderTest, SetContainerInfo003, TestSize.Level1)
622 {
623 std::string windowName = "";
624 Recorder::EventRecorder::Get().SetContainerInfo(windowName, 0, false);
625 EXPECT_EQ(Recorder::EventRecorder::Get().GetContainerId(), -1);
626 }
627
628 /**
629 * @tc.name: SetFocusContainerInfo001
630 * @tc.desc: Test SetFocusContainerInfo.
631 * @tc.type: FUNC
632 */
633 HWTEST_F(EventRecorderTest, SetFocusContainerInfo001, TestSize.Level1)
634 {
635 std::string windowName = "$HA_FLOAT_WINDOW$";
636 Recorder::EventRecorder::Get().SetFocusContainerInfo(windowName, 0);
637 EXPECT_EQ(Recorder::EventRecorder::Get().GetContainerId(), -1);
638 }
639
640 /**
641 * @tc.name: SetFocusContainerInfo002
642 * @tc.desc: Test SetFocusContainerInfo.
643 * @tc.type: FUNC
644 */
645 HWTEST_F(EventRecorderTest, SetFocusContainerInfo002, TestSize.Level1)
646 {
647 std::string windowName = "";
648 Recorder::EventRecorder::Get().SetFocusContainerInfo(windowName, 0);
649 EXPECT_NE(Recorder::EventRecorder::Get().GetContainerId(), 0);
650 }
651
652 /**
653 * @tc.name: Init001
654 * @tc.desc: Test Init.
655 * @tc.type: FUNC
656 */
657 HWTEST_F(EventRecorderTest, Init001, TestSize.Level1)
658 {
659 Recorder::EventConfig* config = new Recorder::EventConfig();
660 config->Init("");
661 }
662
663 /**
664 * @tc.name: IsPageRecordEnable001
665 * @tc.desc: Test IsPageRecordEnable.
666 * @tc.type: FUNC
667 */
668 HWTEST_F(EventRecorderTest, IsPageRecordEnable001, TestSize.Level1)
669 {
670 EXPECT_TRUE(Recorder::EventRecorder::Get().IsPageRecordEnable());
671 }
672
673 /**
674 * @tc.name: IsComponentRecordEnable001
675 * @tc.desc: Test IsComponentRecordEnable.
676 * @tc.type: FUNC
677 */
678 HWTEST_F(EventRecorderTest, IsComponentRecordEnable001, TestSize.Level1)
679 {
680 EXPECT_FALSE(Recorder::EventRecorder::Get().IsComponentRecordEnable());
681 }
682
683 /**
684 * @tc.name: IsCacheAvaliable001
685 * @tc.desc: Test IsCacheAvaliable.
686 * @tc.type: FUNC
687 */
688 HWTEST_F(EventRecorderTest, IsCacheAvaliable001, TestSize.Level1)
689 {
690 EXPECT_FALSE(Recorder::IsCacheAvaliable());
691 }
692
693 /**
694 * @tc.name: PutString001
695 * @tc.desc: Test PutString.
696 * @tc.type: FUNC
697 */
698 HWTEST_F(EventRecorderTest, PutString001, TestSize.Level1)
699 {
700 Recorder::ExposureCfg cfg;
701 Recorder::NodeDataCache::Get().Clear("");
702 auto pageNode = CreatePageNode("pages/Index");
703 bool result = Recorder::NodeDataCache::Get().PutString(pageNode, "", "1");
704 std::unordered_map<std::string, std::string> nodes;
705 Recorder::NodeDataCache::Get().GetNodeData("", nodes);
706 EXPECT_FALSE(result);
707 }
708
709 /**
710 * @tc.name: PutString002
711 * @tc.desc: Test PutString.
712 * @tc.type: FUNC
713 */
714 HWTEST_F(EventRecorderTest, PutString002, TestSize.Level1)
715 {
716 auto pageNode = CreatePageNode("pages/Index");
717 std::string value;
718 bool result = Recorder::NodeDataCache::Get().PutString(pageNode, "", value);
719 EXPECT_FALSE(result);
720 }
721
722 /**
723 * @tc.name: OnBeforePagePop001
724 * @tc.desc: Test OnBeforePagePop.
725 * @tc.type: FUNC
726 */
727 HWTEST_F(EventRecorderTest, OnBeforePagePop001, TestSize.Level1)
728 {
729 Recorder::NodeDataCache::Get().OnBeforePagePop(true);
730 EXPECT_FALSE(Recorder::NodeDataCache::Get().ShouldCollectData());
731 }
732
733 /**
734 * @tc.name: GetExposureCfg001
735 * @tc.desc: Test GetExposureCfg.
736 * @tc.type: FUNC
737 */
738 HWTEST_F(EventRecorderTest, GetExposureCfg001, TestSize.Level1)
739 {
740 Recorder::ExposureCfg cfg;
741 string pageUrl = "";
742 Recorder::NodeDataCache::Get().GetExposureCfg(pageUrl, "", cfg);
743 EXPECT_TRUE(pageUrl.empty());
744 }
745 } // namespace OHOS::Ace
746