• 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 #include <optional>
17 #include "gtest/gtest.h"
18 #define private public
19 #define protected public
20 #include "interfaces/inner_api/ace/ui_event_observer.h"
21 
22 #include "base/log/log.h"
23 #include "base/memory/ace_type.h"
24 #include "base/utils/utils.h"
25 #include "core/common/recorder/event_config.h"
26 #include "core/common/recorder/event_controller.h"
27 #include "core/common/recorder/event_definition.h"
28 #include "core/common/recorder/event_recorder.h"
29 #include "core/common/recorder/exposure_processor.h"
30 #include "core/common/recorder/inspector_tree_collector.h"
31 #include "core/common/recorder/node_data_cache.h"
32 #include "core/components_ng/pattern/stage/page_info.h"
33 #include "core/components_ng/pattern/stage/page_pattern.h"
34 
35 using namespace testing;
36 using namespace testing::ext;
37 using namespace OHOS::Ace::Recorder;
38 
39 namespace OHOS::Ace {
40 class DemoUIEventObserver : public UIEventObserver {
41 public:
42     DemoUIEventObserver() = default;
43     ~DemoUIEventObserver() override = default;
44 
GetEventType() const45     int32_t GetEventType() const
46     {
47         return eventType_;
48     }
49 
NotifyUIEvent(int32_t eventType,const std::unordered_map<std::string,std::string> & eventParams)50     virtual void NotifyUIEvent(int32_t eventType, const std::unordered_map<std::string, std::string>& eventParams)
51     {
52         LOGI("NotifyUIEvent eventType %{public}d", eventType);
53         eventType_ = eventType;
54     }
55 
56 private:
57     int32_t eventType_;
58 };
59 
60 class EventRecorderTest : public testing::Test {
61 public:
SetUpTestCase()62     static void SetUpTestCase() {}
TearDownTestCase()63     static void TearDownTestCase() {}
SetUp()64     void SetUp() {}
TearDown()65     void TearDown() {}
66 };
67 
GetConfig(std::string & config)68 void GetConfig(std::string& config)
69 {
70     config =
71         "{\"enable\":true,\"switch\":{\"page\":true,\"component\":true,\"exposure\":true},\"config\":[{\"pageUrl\":"
72         "\"pages/"
73         "Index\",\"shareNode\":[\"hahaha\",\"btn_TitleExpand\",\"btn_OpenSelf\",\"btn_Screenshot\",\"btn_inspect\","
74         "\"btn_xxx\",\"\"],\"exposureCfg\":[{\"id\":\"btn_Grid\",\"ratio\":0.75,\"duration\":5000},{\"id\":\"btn_"
75         "TitleExpand\",\"ratio\":0.9,\"duration\":1000}]},{\"pageUrl\":\"pages/"
76         "ScrollPage\",\"shareNode\":[\"scroll_item_1\"],\"exposureCfg\":[{\"id\":\"scroll_item_2\",\"ratio\":0.85,"
77         "\"duration\":5000},{\"id\":\"scroll_item_12\",\"ratio\":0.4,\"duration\":3000}]}]}";
78 }
79 
GetConfigDisable(std::string & config)80 void GetConfigDisable(std::string& config)
81 {
82     config =
83         "{\"enable\":false,\"switch\":{\"page\":true,\"component\":true,\"exposure\":true},\"config\":[{\"pageUrl\":"
84         "\"pages/"
85         "Index\",\"shareNode\":[\"hahaha\",\"btn_TitleExpand\",\"btn_OpenSelf\",\"btn_Screenshot\",\"btn_inspect\","
86         "\"btn_xxx\",\"\"],\"exposureCfg\":[{\"id\":\"btn_Grid\",\"ratio\":0.75,\"duration\":5000},{\"id\":\"btn_"
87         "TitleExpand\",\"ratio\":0.9,\"duration\":1000}]},{\"pageUrl\":\"pages/"
88         "ScrollPage\",\"shareNode\":[\"scroll_item_1\"],\"exposureCfg\":[{\"id\":\"scroll_item_2\",\"ratio\":0.85,"
89         "\"duration\":5000},{\"id\":\"scroll_item_12\",\"ratio\":0.4,\"duration\":3000}]}]}";
90 }
91 
GetConfigTest(std::string & config)92 void GetConfigTest(std::string& config)
93 {
94     config =
95         "{\"enable\":false,\"switch\":{\"page\":true,\"component\":true,\"exposure\":true},\"x\":[{\"pageUrl\":"
96         "\"pages/"
97         "Index\",\"shareNode\":[\"hahaha\",\"btn_TitleExpand\",\"btn_OpenSelf\",\"btn_Screenshot\",\"btn_inspect\","
98         "\"btn_xxx\",\"\"],\"exposureCfg\":[{\"id\":\"btn_Grid\",\"ratio\":0.75,\"duration\":5000},{\"id\":\"btn_"
99         "TitleExpand\",\"ratio\":0.9,\"duration\":1000}]},{\"pageUrl\":\"pages/"
100         "ScrollPage\",\"shareNode\":[\"scroll_item_1\"],\"exposureCfg\":[{\"id\":\"scroll_item_2\",\"ratio\":0.85,"
101         "\"duration\":5000},{\"id\":\"scroll_item_12\",\"ratio\":0.4,\"duration\":3000}]}]}";
102 }
103 
GetConfigTest2(std::string & config)104 void GetConfigTest2(std::string& config)
105 {
106     config = "{\"enable\":true,\"switch\":{\"page\":true,\"component\":true,\"exposure\":true},\"config\":[{"
107              "\"shareNode\":[\"hahaha\",\"btn_TitleExpand\",\"btn_OpenSelf\",\"btn_Screenshot\",\"btn_inspect\","
108              "\"btn_xxx\",\"\"],\"exposureCfg\":[{\"id\":\"btn_Grid\",\"ratio\":0.75,\"duration\":5000},{\"id\":\"btn_"
109              "TitleExpand\",\"ratio\":0.9,\"duration\":1000}]},{\"shareNode\":[\"scroll_item_1\"],\"exposureCfg\":[{"
110              "\"id\":\"scroll_item_2\",\"ratio\":0.85,"
111              "\"duration\":5000},{\"id\":\"scroll_item_12\",\"ratio\":0.4,\"duration\":3000}]}]}";
112 }
113 
GetConfigTest3(std::string & config)114 void GetConfigTest3(std::string& config)
115 {
116     config =
117         "{\"enable\":true,\"switch\":{\"page\":true,\"component\":true,\"exposure\":true},\"config\":[{\"pageUrl\":"
118         "\"pages/"
119         "Index\"},{\"pageUrl\":\"pages/"
120         "ScrollPage\"}]}";
121 }
122 
GetConfigTest4(std::string & config)123 void GetConfigTest4(std::string& config)
124 {
125     config =
126         "{\"enable\":true,\"globalSwitch\":{\"page\":true,\"component\":true,\"exposure\":true},\"config\":[{"
127         "\"pageUrl\":"
128         "\"pages/"
129         "Index\",\"shareNode\":[\"hahaha\",\"btn_TitleExpand\",\"btn_OpenSelf\",\"btn_Screenshot\",\"btn_inspect\","
130         "\"btn_xxx\",\"\"],\"exposureCfg\":[{\"id\":\"btn_Grid\",\"ratio\":0.75,\"duration\":5000},{\"id\":\"btn_"
131         "TitleExpand\",\"ratio\":0.9,\"duration\":1000}]},{\"pageUrl\":\"pages/"
132         "ScrollPage\",\"shareNode\":[\"scroll_item_1\"],\"exposureCfg\":[{\"id\":\"scroll_item_2\",\"ratio\":0.85,"
133         "\"duration\":5000},{\"id\":\"scroll_item_12\",\"ratio\":0.4,\"duration\":3000}]}]}";
134 }
135 
GetConfigTest5(std::string & config)136 void GetConfigTest5(std::string& config)
137 {
138     config = "{\"enable\":true,\"switch\":{\"page\":true,\"component\":true,\"exposure\":true,\"pageParam\":true,"
139              "\"scroll\":true,\"animation\":true,\"rect\":true,\"web\":true,\"textInput\":true,\"clickGesture\":true},"
140              "\"webCategory\":\"test\",\"webIdentifier\":\"abc\",\"webActionJs\":\"hello\"}";
141 }
142 
CreatePageNode(const std::string pageUrl)143 RefPtr<NG::FrameNode> CreatePageNode(const std::string pageUrl)
144 {
145     auto pageNodeId = ElementRegister::GetInstance()->MakeUniqueId();
146     return NG::FrameNode::GetOrCreateFrameNode("page", pageNodeId, [pageUrl]() {
147         return AceType::MakeRefPtr<NG::PagePattern>(AceType::MakeRefPtr<NG::PageInfo>(1, pageUrl, pageUrl + ".js"));
148     });
149 }
150 
151 /**
152  * @tc.name: EventRecorderTest001
153  * @tc.desc: Test register.
154  * @tc.type: FUNC
155  */
156 HWTEST_F(EventRecorderTest, EventRecorderTest001, TestSize.Level1)
157 {
158     /**
159      * @tc.steps: step1. call the Register first.
160      * @tc.expected: step1. register success.
161      */
162     std::string config;
163     GetConfig(config);
164     auto observer = std::make_shared<DemoUIEventObserver>();
165     Recorder::EventController::Get().Register(config, observer);
166 
167     Recorder::NodeDataCache::Get().OnPageShow("pages/Index");
168 
169     Recorder::ExposureCfg exposureCfg = { "", 0.0, 0 };
170     Recorder::NodeDataCache::Get().GetExposureCfg("pages/Index", "btn_Grid", exposureCfg);
171     EXPECT_EQ(exposureCfg.id, "btn_Grid");
172     EXPECT_EQ(exposureCfg.duration, 5000);
173 
174     exposureCfg = { "", 0.0, 0 };
175     Recorder::NodeDataCache::Get().GetExposureCfg("pages/Index", "", exposureCfg);
176     EXPECT_EQ(exposureCfg.id, "");
177 
178     exposureCfg = { "", 0.0, 0 };
179     Recorder::NodeDataCache::Get().GetExposureCfg("pages/Index", "xyz", exposureCfg);
180     EXPECT_EQ(exposureCfg.id, "");
181 
182     Recorder::NodeDataCache::Get().OnPageShow("pages/ScrollPage");
183     exposureCfg = { "", 0.0, 0 };
184     Recorder::NodeDataCache::Get().GetExposureCfg("pages/ScrollPage", "btn_Grid", exposureCfg);
185     EXPECT_EQ(exposureCfg.id, "");
186 
187     exposureCfg = { "", 0.0, 0 };
188     Recorder::NodeDataCache::Get().GetExposureCfg("pages/ScrollPage", "scroll_item_2", exposureCfg);
189     EXPECT_EQ(exposureCfg.id, "scroll_item_2");
190     EXPECT_NEAR(exposureCfg.ratio, 0.85, 0.00001f);
191 
192     /**
193      * @tc.steps: step2. call the Unregister first.
194      * @tc.expected: step2. unregister success.
195      */
196     Recorder::EventController::Get().Unregister(observer);
197     exposureCfg = { "", 0.0, 0 };
198     Recorder::NodeDataCache::Get().GetExposureCfg("pages/ScrollPage", "scroll_item_2", exposureCfg);
199     EXPECT_EQ(exposureCfg.id, "");
200 }
201 
202 /**
203  * @tc.name: EventRecorderTest002
204  * @tc.desc: Test node data cache.
205  * @tc.type: FUNC
206  */
207 HWTEST_F(EventRecorderTest, EventRecorderTest002, TestSize.Level1)
208 {
209     std::string config;
210     GetConfig(config);
211     auto observer = std::make_shared<DemoUIEventObserver>();
212     Recorder::EventController::Get().Register(config, observer);
213 
214     /**
215      * @tc.steps: step1. test index page.
216      * @tc.expected: step1. get value success.
217      */
218     Recorder::NodeDataCache::Get().OnPageShow("pages/Index");
219     auto pageNode = CreatePageNode("pages/Index");
220 
221     Recorder::NodeDataCache::Get().PutString(pageNode, "btn_TitleExpand", "abc");
222     Recorder::NodeDataCache::Get().PutInt(pageNode, "btn_OpenSelf", 2);
223     Recorder::NodeDataCache::Get().PutBool(pageNode, "btn_Screenshot", true);
224     Recorder::NodeDataCache::Get().PutMultiple(pageNode, "btn_inspect", "inspect", 11);
225     Recorder::NodeDataCache::Get().PutMultiple(pageNode, "btn_xxx", "xxx", true);
226     std::vector<std::string> values = { "a", "b", "c" };
227     Recorder::NodeDataCache::Get().PutMultiple(pageNode, "hahaha", "xixi", values);
228     auto nodeValues = std::unordered_map<std::string, std::string>();
229     nodeValues.emplace("btn_TitleExpand", "");
230     nodeValues.emplace("btn_OpenSelf", "");
231     nodeValues.emplace("btn_Screenshot", "");
232     nodeValues.emplace("btn_inspect", "");
233     nodeValues.emplace("btn_xxx", "");
234     nodeValues.emplace("hahaha", "");
235     nodeValues.emplace("hello", "");
236     nodeValues.emplace("world", "xyz");
237     Recorder::NodeDataCache::Get().GetNodeData("pages/Index", nodeValues);
238 
239     auto iter = nodeValues.find("btn_TitleExpand");
240     EXPECT_EQ(iter->second, "abc");
241 
242     iter = nodeValues.find("btn_OpenSelf");
243     EXPECT_EQ(iter->second, "2");
244 
245     iter = nodeValues.find("btn_Screenshot");
246     EXPECT_EQ(iter->second, "true");
247 
248     iter = nodeValues.find("btn_inspect");
249     EXPECT_EQ(iter->second, "{\"text\":\"inspect\",\"index\":11}");
250 
251     iter = nodeValues.find("btn_xxx");
252     EXPECT_EQ(iter->second, "{\"text\":\"xxx\",\"checked\":true}");
253 
254     iter = nodeValues.find("hahaha");
255     EXPECT_EQ(iter->second, "{\"text\":\"xixi\",\"textArray\":[\"a\",\"b\",\"c\"]}");
256 
257     iter = nodeValues.find("hello");
258     EXPECT_EQ(iter->second, "");
259 
260     iter = nodeValues.find("world");
261     EXPECT_EQ(iter->second, "xyz");
262 
263     Recorder::EventController::Get().Unregister(observer);
264 }
265 
266 /**
267  * @tc.name: EventRecorderTest003
268  * @tc.desc: Test node data cache.
269  * @tc.type: FUNC
270  */
271 HWTEST_F(EventRecorderTest, EventRecorderTest003, TestSize.Level1)
272 {
273     std::string config;
274     GetConfig(config);
275     auto observer = std::make_shared<DemoUIEventObserver>();
276     Recorder::EventController::Get().Register(config, observer);
277 
278     Recorder::NodeDataCache::Get().OnPageShow("pages/Index");
279     auto pageNode = CreatePageNode("pages/Index");
280 
281     Recorder::NodeDataCache::Get().PutString(pageNode, "btn_TitleExpand", "abc");
282     Recorder::NodeDataCache::Get().PutInt(pageNode, "btn_OpenSelf", 2);
283     Recorder::NodeDataCache::Get().PutBool(pageNode, "btn_Screenshot", true);
284     Recorder::NodeDataCache::Get().PutMultiple(pageNode, "btn_inspect", "inspect", 11);
285     Recorder::NodeDataCache::Get().PutMultiple(pageNode, "btn_xxx", "xxx", true);
286     std::vector<std::string> values = { "a", "b", "c" };
287     Recorder::NodeDataCache::Get().PutMultiple(pageNode, "hahaha", "xixi", values);
288 
289     /**
290      * @tc.steps: step1. test scroll page.
291      * @tc.expected: step1. get value success.
292      */
293     Recorder::NodeDataCache::Get().OnPageShow("pages/ScrollPage");
294     auto pageNode2 = CreatePageNode("pages/ScrollPage");
295 
296     std::vector<std::string> values2 = { "x", "y", "z" };
297     Recorder::NodeDataCache::Get().PutStringArray(pageNode2, "scroll_item_1", values2);
298     auto nodeValues2 = std::unordered_map<std::string, std::string>();
299     nodeValues2.emplace("btn_TitleExpand", "");
300     nodeValues2.emplace("scroll_item_1", "");
301     Recorder::NodeDataCache::Get().GetNodeData("pages/ScrollPage", nodeValues2);
302 
303     auto iter2 = nodeValues2.find("btn_TitleExpand");
304     EXPECT_NE(iter2->second, "abc");
305 
306     iter2 = nodeValues2.find("scroll_item_1");
307     EXPECT_EQ(iter2->second, "[\"x\",\"y\",\"z\"]");
308 
309     Recorder::EventController::Get().Unregister(observer);
310 }
311 
312 /**
313  * @tc.name: EventRecorderTest004
314  * @tc.desc: Test node data cache.
315  * @tc.type: FUNC
316  */
317 HWTEST_F(EventRecorderTest, EventRecorderTest004, TestSize.Level1)
318 {
319     std::string config;
320     GetConfig(config);
321     auto observer = std::make_shared<DemoUIEventObserver>();
322     Recorder::EventController::Get().Register(config, observer);
323 
324     Recorder::NodeDataCache::Get().OnPageShow("pages/Index");
325     auto pageNode = CreatePageNode("pages/Index");
326 
327     Recorder::NodeDataCache::Get().PutString(pageNode, "btn_TitleExpand", "abc");
328     Recorder::NodeDataCache::Get().PutInt(pageNode, "btn_OpenSelf", 2);
329     Recorder::NodeDataCache::Get().PutBool(pageNode, "btn_Screenshot", true);
330     Recorder::NodeDataCache::Get().PutMultiple(pageNode, "btn_inspect", "inspect", 11);
331     Recorder::NodeDataCache::Get().PutMultiple(pageNode, "btn_xxx", "xxx", true);
332     std::vector<std::string> values = { "a", "b", "c" };
333     Recorder::NodeDataCache::Get().PutMultiple(pageNode, "hahaha", "xixi", values);
334 
335     Recorder::NodeDataCache::Get().OnPageShow("pages/ScrollPage");
336     auto pageNode2 = CreatePageNode("pages/ScrollPage");
337 
338     std::vector<std::string> values2 = { "x", "y", "z" };
339     Recorder::NodeDataCache::Get().PutStringArray(pageNode2, "scroll_item_1", values2);
340 
341     /**
342      * @tc.steps: step1. test pop scroll page.
343      * @tc.expected: step1. get value success.
344      */
345     Recorder::NodeDataCache::Get().OnBeforePagePop();
346 
347     auto nodeValues3 = std::unordered_map<std::string, std::string>();
348     nodeValues3.emplace("btn_TitleExpand", "");
349     nodeValues3.emplace("scroll_item_1", "");
350     Recorder::NodeDataCache::Get().GetNodeData("pages/ScrollPage", nodeValues3);
351 
352     auto iter3 = nodeValues3.find("scroll_item_1");
353     EXPECT_EQ(iter3->second, "");
354 
355     iter3 = nodeValues3.find("btn_TitleExpand");
356     EXPECT_EQ(iter3->second, "");
357 
358     Recorder::EventController::Get().Unregister(observer);
359 }
360 
361 /**
362  * @tc.name: EventRecorderTest005
363  * @tc.desc: Test node data cache.
364  * @tc.type: FUNC
365  */
366 HWTEST_F(EventRecorderTest, EventRecorderTest005, TestSize.Level1)
367 {
368     std::string config;
369     GetConfig(config);
370     auto observer = std::make_shared<DemoUIEventObserver>();
371     Recorder::EventController::Get().Register(config, observer);
372 
373     /**
374      * @tc.steps: step1. test index page.
375      * @tc.expected: step1. get value success.
376      */
377     Recorder::NodeDataCache::Get().OnPageShow("pages/Index");
378     auto pageNode = CreatePageNode("pages/Index");
379 
380     Recorder::NodeDataCache::Get().PutString(pageNode, "btn_TitleExpand", "abc");
381     Recorder::NodeDataCache::Get().PutInt(pageNode, "btn_OpenSelf", 2);
382     Recorder::NodeDataCache::Get().PutBool(pageNode, "btn_Screenshot", true);
383     Recorder::NodeDataCache::Get().PutMultiple(pageNode, "btn_inspect", "inspect", 11);
384     Recorder::NodeDataCache::Get().PutMultiple(pageNode, "btn_xxx", "xxx", true);
385     std::vector<std::string> values = { "a", "b", "c" };
386     Recorder::NodeDataCache::Get().PutMultiple(pageNode, "hahaha", "xixi", values);
387 
388     Recorder::NodeDataCache::Get().OnPageShow("pages/ScrollPage");
389     auto pageNode2 = CreatePageNode("pages/ScrollPage");
390 
391     std::vector<std::string> values2 = { "x", "y", "z" };
392     Recorder::NodeDataCache::Get().PutStringArray(pageNode2, "scroll_item_1", values2);
393 
394     Recorder::NodeDataCache::Get().OnBeforePagePop();
395 
396     /**
397      * @tc.steps: step1. test index page.
398      * @tc.expected: step1. get value success.
399      */
400     auto nodeValues4 = std::unordered_map<std::string, std::string>();
401     nodeValues4.emplace("btn_TitleExpand", "");
402     nodeValues4.emplace("btn_OpenSelf", "");
403     nodeValues4.emplace("btn_Screenshot", "");
404     Recorder::NodeDataCache::Get().GetNodeData("pages/Index", nodeValues4);
405 
406     auto iter4 = nodeValues4.find("btn_TitleExpand");
407     EXPECT_EQ(iter4->second, "abc");
408 
409     iter4 = nodeValues4.find("btn_OpenSelf");
410     EXPECT_EQ(iter4->second, "2");
411 
412     iter4 = nodeValues4.find("btn_Screenshot");
413     EXPECT_EQ(iter4->second, "true");
414 
415     Recorder::EventController::Get().Unregister(observer);
416 }
417 
418 /**
419  * @tc.name: EventRecorderTest006
420  * @tc.desc: Test node data cache.
421  * @tc.type: FUNC
422  */
423 HWTEST_F(EventRecorderTest, EventRecorderTest006, TestSize.Level1)
424 {
425     std::string config;
426     GetConfig(config);
427     auto observer = std::make_shared<DemoUIEventObserver>();
428     Recorder::EventController::Get().Register(config, observer);
429 
430     /**
431      * @tc.steps: step1. test index page.
432      * @tc.expected: step1. get value success.
433      */
434     Recorder::NodeDataCache::Get().OnPageShow("pages/Index");
435     auto pageNode = CreatePageNode("pages/Index");
436 
437     Recorder::NodeDataCache::Get().PutString(pageNode, "btn_TitleExpand", "abc");
438     Recorder::NodeDataCache::Get().PutInt(pageNode, "btn_OpenSelf", 2);
439     Recorder::NodeDataCache::Get().PutBool(pageNode, "btn_Screenshot", true);
440     Recorder::NodeDataCache::Get().PutMultiple(pageNode, "btn_inspect", "inspect", 11);
441     Recorder::NodeDataCache::Get().PutMultiple(pageNode, "btn_xxx", "xxx", true);
442     std::vector<std::string> values = { "a", "b", "c" };
443     Recorder::NodeDataCache::Get().PutMultiple(pageNode, "hahaha", "xixi", values);
444 
445     Recorder::NodeDataCache::Get().OnPageShow("pages/ScrollPage");
446     auto pageNode2 = CreatePageNode("pages/ScrollPage");
447 
448     std::vector<std::string> values2 = { "x", "y", "z" };
449     Recorder::NodeDataCache::Get().PutStringArray(pageNode2, "scroll_item_1", values2);
450 
451     Recorder::NodeDataCache::Get().OnBeforePagePop();
452     Recorder::NodeDataCache::Get().OnPageShow("pages/Index");
453 
454     /**
455      * @tc.steps: step1. test update value.
456      * @tc.expected: step1. get value success.
457      */
458     Recorder::NodeDataCache::Get().PutString(pageNode, "btn_TitleExpand", "hello");
459     auto nodeValues5 = std::unordered_map<std::string, std::string>();
460     nodeValues5.emplace("btn_TitleExpand", "");
461     Recorder::NodeDataCache::Get().GetNodeData("pages/Index", nodeValues5);
462 
463     auto iter5 = nodeValues5.find("btn_TitleExpand");
464     EXPECT_EQ(iter5->second, "hello");
465 
466     Recorder::EventController::Get().Unregister(observer);
467 }
468 
469 /**
470  * @tc.name: EventRecorderTest007
471  * @tc.desc: Test node data cache.
472  * @tc.type: FUNC
473  */
474 HWTEST_F(EventRecorderTest, EventRecorderTest007, TestSize.Level1)
475 {
476     std::string config;
477     GetConfig(config);
478     auto observer = std::make_shared<DemoUIEventObserver>();
479     Recorder::EventController::Get().Register(config, observer);
480 
481     Recorder::NodeDataCache::Get().OnPageShow("pages/Index");
482     auto pageNode = CreatePageNode("pages/Index");
483 
484     Recorder::NodeDataCache::Get().PutString(pageNode, "btn_TitleExpand", "abc");
485     Recorder::NodeDataCache::Get().PutInt(pageNode, "btn_OpenSelf", 2);
486     Recorder::NodeDataCache::Get().PutBool(pageNode, "btn_Screenshot", true);
487     Recorder::NodeDataCache::Get().PutMultiple(pageNode, "btn_inspect", "inspect", 11);
488     Recorder::NodeDataCache::Get().PutMultiple(pageNode, "btn_xxx", "xxx", true);
489     std::vector<std::string> values = { "a", "b", "c" };
490     Recorder::NodeDataCache::Get().PutMultiple(pageNode, "hahaha", "xixi", values);
491 
492     Recorder::NodeDataCache::Get().OnPageShow("pages/ScrollPage");
493     auto pageNode2 = CreatePageNode("pages/ScrollPage");
494 
495     std::vector<std::string> values2 = { "x", "y", "z" };
496     Recorder::NodeDataCache::Get().PutStringArray(pageNode2, "scroll_item_1", values2);
497 
498     Recorder::NodeDataCache::Get().OnBeforePagePop();
499 
500     Recorder::NodeDataCache::Get().PutString(pageNode, "btn_TitleExpand", "hello");
501 
502     /**
503      * @tc.steps: step1. test clear.
504      * @tc.expected: step1. get value success.
505      */
506     Recorder::NodeDataCache::Get().Clear("pages/Index");
507     auto nodeValues6 = std::unordered_map<std::string, std::string>();
508     nodeValues6.emplace("btn_TitleExpand", "");
509     auto iter6 = nodeValues6.find("btn_TitleExpand");
510     EXPECT_EQ(iter6->second, "");
511 
512     Recorder::EventController::Get().Unregister(observer);
513 }
514 
515 /**
516  * @tc.name: EventRecorderTest008
517  * @tc.desc: Test node exposure.
518  * @tc.type: FUNC
519  */
520 HWTEST_F(EventRecorderTest, EventRecorderTest008, TestSize.Level1)
521 {
522     std::string config;
523     GetConfig(config);
524     auto observer = std::make_shared<DemoUIEventObserver>();
525     Recorder::EventController::Get().Register(config, observer);
526     Recorder::NodeDataCache::Get().OnPageShow("pages/Index");
527     auto exposure = AceType::MakeRefPtr<Recorder::ExposureProcessor>("pages/Index", "btn_TitleExpand");
528     EXPECT_TRUE(exposure->IsNeedRecord());
529     EXPECT_NEAR(exposure->GetRatio(), 0.9, 0.00001f);
530     exposure->OnVisibleChange(true);
531     sleep(2);
532     exposure->OnVisibleChange(false);
533 
534     sleep(1);
535     EXPECT_EQ(observer->GetEventType(), static_cast<int32_t>(Recorder::EventType::EXPOSURE));
536     Recorder::EventController::Get().Unregister(observer);
537 }
538 
539 /**
540  * @tc.name: EventRecorderTest009
541  * @tc.desc: Test node recorder.
542  * @tc.type: FUNC
543  */
544 HWTEST_F(EventRecorderTest, EventRecorderTest009, TestSize.Level1)
545 {
546     std::string config;
547     GetConfig(config);
548     auto observer = std::make_shared<DemoUIEventObserver>();
549     Recorder::EventController::Get().Register(config, observer);
550 
551     Recorder::EventParamsBuilder builder1;
552     builder1.SetId("hello").SetPageUrl("pages/Index").SetText("world");
553     LOGI("OnClick");
554     Recorder::EventRecorder::Get().OnClick(std::move(builder1));
555     sleep(1);
556 
557     Recorder::EventParamsBuilder builder2;
558     builder2.SetId("hello").SetPageUrl("pages/Index").SetText("world").SetChecked(true);
559     LOGI("OnChange");
560     Recorder::EventRecorder::Get().OnChange(std::move(builder2));
561     sleep(1);
562     EXPECT_EQ(observer->GetEventType(), static_cast<int32_t>(Recorder::EventType::CHANGE));
563 
564     Recorder::EventParamsBuilder builder3;
565     builder3.SetId("hello")
566         .SetPageUrl("pages/Index")
567         .SetText("weather")
568         .SetEventType(Recorder::EventType::SEARCH_SUBMIT);
569     LOGI("OnEvent");
570     Recorder::EventRecorder::Get().OnEvent(std::move(builder3));
571     sleep(1);
572     EXPECT_EQ(observer->GetEventType(), static_cast<int32_t>(Recorder::EventType::SEARCH_SUBMIT));
573 
574     Recorder::EventRecorder::Get().OnPageShow("pages/Index", "from moon");
575     sleep(1);
576     EXPECT_EQ(observer->GetEventType(), static_cast<int32_t>(Recorder::EventType::PAGE_SHOW));
577 
578     Recorder::EventRecorder::Get().OnPageHide("pages/Index", 10000);
579     sleep(1);
580     EXPECT_EQ(observer->GetEventType(), static_cast<int32_t>(Recorder::EventType::PAGE_HIDE));
581 
582     Recorder::EventParamsBuilder builder4;
583     builder4.SetText("tom");
584     Recorder::EventRecorder::Get().OnNavDstShow(std::move(builder4));
585     sleep(1);
586     EXPECT_EQ(observer->GetEventType(), static_cast<int32_t>(Recorder::EventType::PAGE_SHOW));
587 
588     Recorder::EventParamsBuilder builder5;
589     builder5.SetText("");
590     builder5.SetText("tom");
591     Recorder::EventRecorder::Get().OnNavDstHide(std::move(builder5));
592     sleep(1);
593     EXPECT_EQ(observer->GetEventType(), static_cast<int32_t>(Recorder::EventType::PAGE_HIDE));
594 
595     Recorder::EventController::Get().Unregister(observer);
596 }
597 
598 /**
599  * @tc.name: EventRecorderTest010
600  * @tc.desc: Test ToString MapToString
601  * @tc.type: FUNC
602  */
603 HWTEST_F(EventRecorderTest, EventRecorderTest010, TestSize.Level1)
604 {
605     Recorder::EventParamsBuilder eventBuilder;
606     std::string ret = eventBuilder.ToString();
607     EXPECT_EQ(ret, "{}");
608     std::string ret2 = eventBuilder.SetEventType(Recorder::EventType::PAGE_SHOW).ToString();
609     EXPECT_EQ(ret2, "{eventType:1, }");
610     const std::shared_ptr<std::unordered_map<std::string, std::string>> input = nullptr;
611     std::string ret3 = Recorder::MapToString(input);
612     EXPECT_EQ(ret3, "");
613     const std::shared_ptr<std::unordered_map<std::string, std::string>> input2 =
614         std::make_shared<std::unordered_map<std::string, std::string>>();
615     input2->emplace("key1", "value1");
616     std::string ret4 = Recorder::MapToString(input2);
617     EXPECT_EQ(ret4, "{key1:value1, }");
618 }
619 
620 /**
621  * @tc.name: EventRecorderTest011
622  * @tc.desc: Test SetContainerInfo
623  * @tc.type: FUNC
624  */
625 HWTEST_F(EventRecorderTest, EventRecorderTest011, TestSize.Level1)
626 {
627     // windowName = "$HA_FLOAT_WINDOW$"
628     std::string windowName = "$HA_FLOAT_WINDOW$";
629     int32_t id = 1;
630     bool foreground = true;
631     Recorder::EventRecorder::Get().SetContainerInfo(windowName, id, foreground);
632     EXPECT_EQ(Recorder::EventRecorder::Get().GetContainerId() == -1, true);
633     Recorder::EventRecorder::Get().SetFocusContainerInfo(windowName, id);
634     EXPECT_EQ(Recorder::EventRecorder::Get().GetContainerId() == -1, true);
635     // windowName = "pages/Index",foreground = true
636     windowName = "pages/Index";
637     Recorder::EventRecorder::Get().SetContainerInfo(windowName, id, true);
638     EXPECT_EQ(Recorder::EventRecorder::Get().GetContainerId() == -1, true);
639     Recorder::EventRecorder::Get().SetFocusContainerInfo(windowName, 2);
640     EXPECT_EQ(Recorder::EventRecorder::Get().GetContainerId() == 2, true);
641     // windowName = "pages/Index",foreground = false
642     Recorder::EventRecorder::Get().SetContainerInfo(windowName, id, false);
643     EXPECT_EQ(Recorder::EventRecorder::Get().GetContainerId() == 2, true);
644 }
645 
646 /**
647  * @tc.name: EventRecorderTest012
648  * @tc.desc: Test SetDescription/SetNavDst
649  * @tc.type: FUNC
650  */
651 HWTEST_F(EventRecorderTest, EventRecorderTest012, TestSize.Level1)
652 {
653     Recorder::EventParamsBuilder eventBuilder;
654     std::string desc = "desc";
655     std::shared_ptr<std::unordered_map<std::string, std::string>> ret = eventBuilder.SetDescription(desc).build();
656     EXPECT_EQ(ret->size(), 2);
657     std::string dstName = "dstName";
658     std::shared_ptr<std::unordered_map<std::string, std::string>> ret2 = eventBuilder.SetNavDst(dstName).build();
659     EXPECT_EQ(ret2->size(), 3);
660 }
661 
662 /**
663  * @tc.name: SetContainerInfo001
664  * @tc.desc: Test SetContainerInfo.
665  * @tc.type: FUNC
666  */
667 HWTEST_F(EventRecorderTest, SetContainerInfo001, TestSize.Level1)
668 {
669     Recorder::EventRecorder::Get().containerId_ = 1;
670     Recorder::EventRecorder::Get().focusContainerId_ = 2;
671     std::string windowName = "$HA_FLOAT_WINDOW$";
672     Recorder::EventRecorder::Get().SetContainerInfo(windowName, 0, true);
673     EXPECT_EQ(Recorder::EventRecorder::Get().GetContainerId(), 2);
674     EXPECT_EQ(Recorder::EventRecorder::Get().GetContainerId(false), 1);
675 }
676 
677 /**
678  * @tc.name: SetContainerInfo002
679  * @tc.desc: Test SetContainerInfo.
680  * @tc.type: FUNC
681  */
682 HWTEST_F(EventRecorderTest, SetContainerInfo002, TestSize.Level1)
683 {
684     std::string windowName = "";
685     Recorder::EventRecorder::Get().SetContainerInfo(windowName, 0, true);
686     EXPECT_NE(Recorder::EventRecorder::Get().GetContainerId(), 0);
687 }
688 
689 /**
690  * @tc.name: SetContainerInfo003
691  * @tc.desc: Test SetContainerInfo.
692  * @tc.type: FUNC
693  */
694 HWTEST_F(EventRecorderTest, SetContainerInfo003, TestSize.Level1)
695 {
696     Recorder::EventRecorder::Get().containerId_ = 1;
697     Recorder::EventRecorder::Get().focusContainerId_ = 2;
698     std::string windowName = "";
699     Recorder::EventRecorder::Get().SetContainerInfo(windowName, 0, false);
700     EXPECT_EQ(Recorder::EventRecorder::Get().GetContainerId(), 2);
701 }
702 
703 /**
704  * @tc.name: SetFocusContainerInfo001
705  * @tc.desc: Test SetFocusContainerInfo.
706  * @tc.type: FUNC
707  */
708 HWTEST_F(EventRecorderTest, SetFocusContainerInfo001, TestSize.Level1)
709 {
710     Recorder::EventRecorder::Get().containerId_ = 1;
711     Recorder::EventRecorder::Get().focusContainerId_ = 2;
712     std::string windowName = "$HA_FLOAT_WINDOW$";
713     Recorder::EventRecorder::Get().SetFocusContainerInfo(windowName, 0);
714     EXPECT_EQ(Recorder::EventRecorder::Get().GetContainerId(), 2);
715 }
716 
717 /**
718  * @tc.name: SetFocusContainerInfo002
719  * @tc.desc: Test SetFocusContainerInfo.
720  * @tc.type: FUNC
721  */
722 HWTEST_F(EventRecorderTest, SetFocusContainerInfo002, TestSize.Level1)
723 {
724     Recorder::EventRecorder::Get().containerId_ = 1;
725     Recorder::EventRecorder::Get().focusContainerId_ = 2;
726     std::string windowName = "";
727     Recorder::EventRecorder::Get().SetFocusContainerInfo(windowName, 0);
728     EXPECT_EQ(Recorder::EventRecorder::Get().GetContainerId(), 0);
729 }
730 
731 /**
732  * @tc.name: Init001
733  * @tc.desc: Test Init.
734  * @tc.type: FUNC
735  */
736 HWTEST_F(EventRecorderTest, Init001, TestSize.Level1)
737 {
738     std::string str = "";
739     Recorder::EventConfig* config = new Recorder::EventConfig();
740     config->Init(str);
741     EXPECT_EQ(str, "");
742     delete config;
743 }
744 
745 /**
746  * @tc.name: Init002
747  * @tc.desc: Test Init.
748  * @tc.type: FUNC
749  */
750 HWTEST_F(EventRecorderTest, Init002, TestSize.Level1)
751 {
752     std::string str;
753     GetConfigTest(str);
754     Recorder::EventConfig* config = new Recorder::EventConfig();
755     config->Init(str);
756     EXPECT_NE(str, "");
757     delete config;
758 }
759 
760 /**
761  * @tc.name: Init003
762  * @tc.desc: Test Init.
763  * @tc.type: FUNC
764  */
765 HWTEST_F(EventRecorderTest, Init003, TestSize.Level1)
766 {
767     std::string str;
768     GetConfigTest2(str);
769     Recorder::EventConfig* config = new Recorder::EventConfig();
770     config->Init(str);
771     EXPECT_NE(str, "");
772     delete config;
773 }
774 
775 /**
776  * @tc.name: Init004
777  * @tc.desc: Test Init.
778  * @tc.type: FUNC
779  */
780 HWTEST_F(EventRecorderTest, Init004, TestSize.Level1)
781 {
782     std::string str;
783     GetConfigTest3(str);
784     Recorder::EventConfig* config = new Recorder::EventConfig();
785     config->Init(str);
786     EXPECT_NE(str, "");
787     delete config;
788 }
789 
790 /**
791  * @tc.name: Init005
792  * @tc.desc: Test Init.
793  * @tc.type: FUNC
794  */
795 HWTEST_F(EventRecorderTest, Init005, TestSize.Level1)
796 {
797     std::string str;
798     GetConfigTest4(str);
799     Recorder::EventConfig* config = new Recorder::EventConfig();
800     config->Init(str);
801     EXPECT_NE(str, "");
802     delete config;
803 }
804 
805 /**
806  * @tc.name: IsPageRecordEnable001
807  * @tc.desc: Test IsPageRecordEnable.
808  * @tc.type: FUNC
809  */
810 HWTEST_F(EventRecorderTest, IsPageRecordEnable001, TestSize.Level1)
811 {
812     EXPECT_TRUE(Recorder::EventRecorder::Get().IsPageRecordEnable());
813 }
814 
815 /**
816  * @tc.name: IsPageRecordEnable002
817  * @tc.desc: Test IsPageRecordEnable.
818  * @tc.type: FUNC
819  */
820 HWTEST_F(EventRecorderTest, IsPageRecordEnable002, TestSize.Level1)
821 {
822     int32_t index = static_cast<int32_t>(Recorder::EventCategory::CATEGORY_PAGE);
823     Recorder::EventRecorder::Get().globalSwitch_[index] = false;
824     Recorder::EventRecorder::Get().eventSwitch_[index] = true;
825     EXPECT_FALSE(Recorder::EventRecorder::Get().IsPageRecordEnable());
826 }
827 
828 /**
829  * @tc.name: IsPageRecordEnable003
830  * @tc.desc: Test IsPageRecordEnable.
831  * @tc.type: FUNC
832  */
833 HWTEST_F(EventRecorderTest, IsPageRecordEnable003, TestSize.Level1)
834 {
835     int32_t index = static_cast<int32_t>(Recorder::EventCategory::CATEGORY_PAGE);
836     Recorder::EventRecorder::Get().globalSwitch_[index] = true;
837     Recorder::EventRecorder::Get().eventSwitch_[index] = false;
838     EXPECT_FALSE(Recorder::EventRecorder::Get().IsPageRecordEnable());
839 }
840 
841 /**
842  * @tc.name: IsPageRecordEnable004
843  * @tc.desc: Test IsPageRecordEnable.
844  * @tc.type: FUNC
845  */
846 HWTEST_F(EventRecorderTest, IsPageRecordEnable004, TestSize.Level1)
847 {
848     int32_t index = static_cast<int32_t>(Recorder::EventCategory::CATEGORY_PAGE);
849     Recorder::EventRecorder::Get().globalSwitch_[index] = false;
850     Recorder::EventRecorder::Get().eventSwitch_[index] = false;
851     EXPECT_FALSE(Recorder::EventRecorder::Get().IsPageRecordEnable());
852 }
853 
854 /**
855  * @tc.name: IsComponentRecordEnable001
856  * @tc.desc: Test IsComponentRecordEnable.
857  * @tc.type: FUNC
858  */
859 HWTEST_F(EventRecorderTest, IsComponentRecordEnable001, TestSize.Level1)
860 {
861     EXPECT_FALSE(Recorder::EventRecorder::Get().IsComponentRecordEnable());
862 }
863 
864 /**
865  * @tc.name: IsComponentRecordEnable002
866  * @tc.desc: Test IsComponentRecordEnable.
867  * @tc.type: FUNC
868  */
869 HWTEST_F(EventRecorderTest, IsComponentRecordEnable002, TestSize.Level1)
870 {
871     int32_t index = static_cast<int32_t>(Recorder::EventCategory::CATEGORY_COMPONENT);
872     Recorder::EventRecorder::Get().globalSwitch_[index] = true;
873     Recorder::EventRecorder::Get().eventSwitch_[index] = false;
874     EXPECT_FALSE(Recorder::EventRecorder::Get().IsComponentRecordEnable());
875 }
876 
877 /**
878  * @tc.name: IsComponentRecordEnable003
879  * @tc.desc: Test IsComponentRecordEnable.
880  * @tc.type: FUNC
881  */
882 HWTEST_F(EventRecorderTest, IsComponentRecordEnable003, TestSize.Level1)
883 {
884     int32_t index = static_cast<int32_t>(Recorder::EventCategory::CATEGORY_COMPONENT);
885     Recorder::EventRecorder::Get().globalSwitch_[index] = false;
886     Recorder::EventRecorder::Get().eventSwitch_[index] = true;
887     EXPECT_FALSE(Recorder::EventRecorder::Get().IsComponentRecordEnable());
888 }
889 
890 /**
891  * @tc.name: IsComponentRecordEnable004
892  * @tc.desc: Test IsComponentRecordEnable.
893  * @tc.type: FUNC
894  */
895 HWTEST_F(EventRecorderTest, IsComponentRecordEnable004, TestSize.Level1)
896 {
897     int32_t index = static_cast<int32_t>(Recorder::EventCategory::CATEGORY_COMPONENT);
898     Recorder::EventRecorder::Get().globalSwitch_[index] = false;
899     Recorder::EventRecorder::Get().eventSwitch_[index] = false;
900     EXPECT_FALSE(Recorder::EventRecorder::Get().IsComponentRecordEnable());
901 }
902 
903 /**
904  * @tc.name: IsRecordEnable001
905  * @tc.desc: Test IsRecordEnable.
906  * @tc.type: FUNC
907  */
908 HWTEST_F(EventRecorderTest, IsRecordEnable001, TestSize.Level1)
909 {
910     int32_t index = static_cast<int32_t>(Recorder::EventCategory::CATEGORY_WEB);
911     Recorder::EventRecorder::Get().globalSwitch_[index] = false;
912     Recorder::EventRecorder::Get().eventSwitch_[index] = false;
913     EXPECT_FALSE(Recorder::EventRecorder::Get().IsRecordEnable(Recorder::EventCategory::CATEGORY_WEB));
914 }
915 
916 /**
917  * @tc.name: IsCacheAvaliable001
918  * @tc.desc: Test IsCacheAvaliable.
919  * @tc.type: FUNC
920  */
921 HWTEST_F(EventRecorderTest, IsCacheAvaliable001, TestSize.Level1)
922 {
923     EXPECT_FALSE(Recorder::IsCacheAvaliable());
924 }
925 
926 /**
927  * @tc.name: IsCacheAvaliable002
928  * @tc.desc: Test IsCacheAvaliable.
929  * @tc.type: FUNC
930  */
931 HWTEST_F(EventRecorderTest, IsCacheAvaliable002, TestSize.Level1)
932 {
933     int32_t index = static_cast<int32_t>(Recorder::EventCategory::CATEGORY_COMPONENT);
934     Recorder::EventRecorder::Get().globalSwitch_[index] = true;
935     Recorder::EventRecorder::Get().eventSwitch_[index] = true;
936     Recorder::NodeDataCache::Get().mergedConfig_->shareNodes["element"] = { "element1", "element2", "element3" };
937     Recorder::IsCacheAvaliable();
938     Recorder::EventRecorder::Get().globalSwitch_[index] = false;
939     Recorder::EventRecorder::Get().eventSwitch_[index] = false;
940     Recorder::NodeDataCache::Get().mergedConfig_->shareNodes.clear();
941     EXPECT_FALSE(Recorder::IsCacheAvaliable());
942 }
943 
944 /**
945  * @tc.name: PutString001
946  * @tc.desc: Test PutString.
947  * @tc.type: FUNC
948  */
949 HWTEST_F(EventRecorderTest, PutString001, TestSize.Level1)
950 {
951     Recorder::ExposureCfg cfg;
952     Recorder::NodeDataCache::Get().Clear("");
953     auto pageNode = CreatePageNode("pages/Index");
954     bool result = Recorder::NodeDataCache::Get().PutString(pageNode, "", "1");
955     std::unordered_map<std::string, std::string> nodes;
956     Recorder::NodeDataCache::Get().GetNodeData("", nodes);
957     EXPECT_FALSE(result);
958 }
959 
960 /**
961  * @tc.name: PutString002
962  * @tc.desc: Test PutString.
963  * @tc.type: FUNC
964  */
965 HWTEST_F(EventRecorderTest, PutString002, TestSize.Level1)
966 {
967     auto pageNode = CreatePageNode("pages/Index");
968     std::string value;
969     bool result = Recorder::NodeDataCache::Get().PutString(pageNode, "", value);
970     EXPECT_FALSE(result);
971 }
972 
973 /**
974  * @tc.name: PutString003
975  * @tc.desc: Test PutString.
976  * @tc.type: FUNC
977  */
978 HWTEST_F(EventRecorderTest, PutString003, TestSize.Level1)
979 {
980     std::string config;
981     GetConfig(config);
982     auto observer = std::make_shared<DemoUIEventObserver>();
983     Recorder::EventController::Get().Register(config, observer);
984     Recorder::NodeDataCache::Get().OnPageShow("pages/Index");
985     auto pageNode = CreatePageNode("pages/Index");
986     Recorder::NodeDataCache::Get().mergedConfig_->shareNodes["element"] = { "element1", "element2", "element3" };
987     bool result = Recorder::NodeDataCache::Get().PutString(pageNode, "1", "1");
988     EXPECT_TRUE(result);
989 }
990 
991 /**
992  * @tc.name: PutString004
993  * @tc.desc: Test PutString.
994  * @tc.type: FUNC
995  */
996 HWTEST_F(EventRecorderTest, PutString004, TestSize.Level1)
997 {
998     std::string config;
999     GetConfig(config);
1000     auto observer = std::make_shared<DemoUIEventObserver>();
1001     Recorder::EventController::Get().Register(config, observer);
1002     Recorder::NodeDataCache::Get().OnPageShow("pages/Index");
1003     auto pageNode = CreatePageNode("pages/Index");
1004     Recorder::NodeDataCache::Get().mergedConfig_->shareNodes.clear();
1005     bool result = Recorder::NodeDataCache::Get().PutString(pageNode, "1", "1");
1006     EXPECT_FALSE(result);
1007 }
1008 
1009 /**
1010  * @tc.name: OnBeforePagePop001
1011  * @tc.desc: Test OnBeforePagePop.
1012  * @tc.type: FUNC
1013  */
1014 HWTEST_F(EventRecorderTest, OnBeforePagePop001, TestSize.Level1)
1015 {
1016     Recorder::NodeDataCache::Get().OnBeforePagePop(true);
1017     EXPECT_FALSE(Recorder::NodeDataCache::Get().ShouldCollectData());
1018 }
1019 
1020 /**
1021  * @tc.name: GetExposureCfg001
1022  * @tc.desc: Test GetExposureCfg.
1023  * @tc.type: FUNC
1024  */
1025 HWTEST_F(EventRecorderTest, GetExposureCfg001, TestSize.Level1)
1026 {
1027     Recorder::ExposureCfg cfg;
1028     std::string pageUrl = "";
1029     Recorder::NodeDataCache::Get().GetExposureCfg(pageUrl, "", cfg);
1030     EXPECT_TRUE(pageUrl.empty());
1031 }
1032 
1033 /**
1034  * @tc.name: SetExtra001
1035  * @tc.desc: Test SetExtra.
1036  * @tc.type: FUNC
1037  */
1038 HWTEST_F(EventRecorderTest, SetExtra001, TestSize.Level1)
1039 {
1040     std::string key = "";
1041     std::string value = "";
1042     Recorder::EventParamsBuilder builder1;
1043     builder1.SetExtra(key, value);
1044     std::string result = builder1.GetValue("text");
1045     EXPECT_EQ(result, "");
1046 }
1047 
1048 /**
1049  * @tc.name: SetExtra002
1050  * @tc.desc: Test SetExtra.
1051  * @tc.type: FUNC
1052  */
1053 HWTEST_F(EventRecorderTest, SetExtra002, TestSize.Level1)
1054 {
1055     std::string key = "123";
1056     std::string value = "";
1057     Recorder::EventParamsBuilder builder1;
1058     builder1.SetExtra(key, value);
1059     EXPECT_TRUE(builder1.params_->empty());
1060 }
1061 
1062 /**
1063  * @tc.name: SetExtra003
1064  * @tc.desc: Test SetExtra.
1065  * @tc.type: FUNC
1066  */
1067 HWTEST_F(EventRecorderTest, SetExtra003, TestSize.Level1)
1068 {
1069     std::string key = "";
1070     std::string value = "123";
1071     Recorder::EventParamsBuilder builder1;
1072     builder1.SetExtra(key, value);
1073     EXPECT_TRUE(builder1.params_->empty());
1074 }
1075 
1076 /**
1077  * @tc.name: SetExtra004
1078  * @tc.desc: Test SetExtra.
1079  * @tc.type: FUNC
1080  */
1081 HWTEST_F(EventRecorderTest, SetExtra004, TestSize.Level1)
1082 {
1083     std::string key = "123";
1084     std::string value = "123";
1085     Recorder::EventParamsBuilder builder1;
1086     builder1.SetExtra(key, value);
1087     std::string result = builder1.ToString();
1088     EXPECT_NE(result, "");
1089 }
1090 
1091 /**
1092  * @tc.name: SetTextArray001
1093  * @tc.desc: Test SetTextArray.
1094  * @tc.type: FUNC
1095  */
1096 HWTEST_F(EventRecorderTest, SetTextArray001, TestSize.Level1)
1097 {
1098     std::vector<std::string> value = { "50", "100", "200", "300" };
1099     Recorder::EventParamsBuilder builder1;
1100     builder1.SetTextArray(value);
1101     EXPECT_FALSE(builder1.params_->empty());
1102 }
1103 
1104 /**
1105  * @tc.name: SetId001
1106  * @tc.desc: Test SetId.
1107  * @tc.type: FUNC
1108  */
1109 HWTEST_F(EventRecorderTest, SetId001, TestSize.Level1)
1110 {
1111     std::vector<std::string> value = { "50", "100", "200", "300" };
1112     Recorder::EventParamsBuilder builder1;
1113     builder1.SetId("");
1114     EXPECT_TRUE(builder1.params_->empty());
1115 }
1116 
1117 /**
1118  * @tc.name: OnNavDstHide001
1119  * @tc.desc: Test OnNavDstHide.
1120  * @tc.type: FUNC
1121  */
1122 HWTEST_F(EventRecorderTest, OnNavDstHide001, TestSize.Level1)
1123 {
1124     Recorder::EventParamsBuilder builder1;
1125     builder1.SetExtra("1", "1");
1126     builder1.SetNavDst("1");
1127     Recorder::EventRecorder::Get().navShowTime_ = 0;
1128     Recorder::EventRecorder::Get().OnNavDstHide(std::move(builder1));
1129     EXPECT_EQ(builder1.GetValue("1"), "1");
1130     EXPECT_EQ(Recorder::EventRecorder::Get().GetNavDstName(), "");
1131 }
1132 
1133 /**
1134  * @tc.name: OnNavDstHide002
1135  * @tc.desc: Test OnNavDstHide.
1136  * @tc.type: FUNC
1137  */
1138 HWTEST_F(EventRecorderTest, OnNavDstHide002, TestSize.Level1)
1139 {
1140     Recorder::EventParamsBuilder builder1;
1141     builder1.SetText("1");
1142     Recorder::EventRecorder::Get().navDstName_ = "";
1143     Recorder::EventRecorder::Get().OnNavDstHide(std::move(builder1));
1144     EXPECT_NE(Recorder::EventRecorder::Get().GetNavDstName(), builder1.GetValue("text"));
1145 }
1146 
1147 /**
1148  * @tc.name: IsCategoryEnable001
1149  * @tc.desc: Test IsCategoryEnable
1150  * @tc.type: FUNC
1151  */
1152 HWTEST_F(EventRecorderTest, IsCategoryEnable001, TestSize.Level1)
1153 {
1154     int32_t index = static_cast<int32_t>(Recorder::EventCategory::CATEGORY_COMPONENT);
1155     Recorder::EventConfig* config = new Recorder::EventConfig();
1156     config->switches_[index] = false;
1157     index = static_cast<int32_t>(Recorder::EventCategory::CATEGORY_PAGE);
1158     bool result = config->IsCategoryEnable(index);
1159     EXPECT_FALSE(result);
1160     delete config;
1161 }
1162 
1163 /**
1164  * @tc.name: Register001
1165  * @tc.desc: Test register.
1166  * @tc.type: FUNC
1167  */
1168 HWTEST_F(EventRecorderTest, Register001, TestSize.Level1)
1169 {
1170     std::string config;
1171     GetConfigDisable(config);
1172     auto observer = std::make_shared<DemoUIEventObserver>();
1173     Recorder::EventController::Get().Register(config, observer);
1174     Recorder::EventController::Get().NotifyConfigChange();
1175     EXPECT_FALSE(Recorder::EventController::Get().clientList_.empty());
1176 }
1177 
1178 /**
1179  * @tc.name: ApplyNewestConfig001
1180  * @tc.desc: Test ApplyNewestConfig.
1181  * @tc.type: FUNC
1182  */
1183 HWTEST_F(EventRecorderTest, ApplyNewestConfig001, TestSize.Level1)
1184 {
1185     Recorder::EventParamsBuilder builder1;
1186     builder1.SetId("hello").SetPageUrl("pages/Index").SetText("world");
1187     auto params = builder1.build();
1188     Recorder::EventController::Get().clientList_.clear();
1189     Recorder::EventController::Get().ApplyNewestConfig(false, false);
1190     Recorder::EventController::Get().NotifyEvent(
1191         Recorder::EventCategory::CATEGORY_COMPONENT, Recorder::EventType::CHANGE, std::move(params));
1192     EXPECT_TRUE(Recorder::EventController::Get().clientList_.empty());
1193 }
1194 
1195 /**
1196  * @tc.name: IsExposureRecordEnable001
1197  * @tc.desc: Test IsExposureRecordEnable.
1198  * @tc.type: FUNC
1199  */
1200 HWTEST_F(EventRecorderTest, IsExposureRecordEnable001, TestSize.Level1)
1201 {
1202     int32_t index = static_cast<int32_t>(Recorder::EventCategory::CATEGORY_EXPOSURE);
1203     Recorder::EventRecorder::Get().globalSwitch_[index] = true;
1204     Recorder::EventRecorder::Get().eventSwitch_[index] = true;
1205     EXPECT_TRUE(Recorder::EventRecorder::Get().IsExposureRecordEnable());
1206 }
1207 
1208 /**
1209  * @tc.name: IsExposureRecordEnable002
1210  * @tc.desc: Test IsExposureRecordEnable.
1211  * @tc.type: FUNC
1212  */
1213 HWTEST_F(EventRecorderTest, IsExposureRecordEnable002, TestSize.Level1)
1214 {
1215     int32_t index = static_cast<int32_t>(Recorder::EventCategory::CATEGORY_EXPOSURE);
1216     Recorder::EventRecorder::Get().globalSwitch_[index] = false;
1217     Recorder::EventRecorder::Get().eventSwitch_[index] = true;
1218     EXPECT_FALSE(Recorder::EventRecorder::Get().IsExposureRecordEnable());
1219 }
1220 
1221 /**
1222  * @tc.name: IsExposureRecordEnable003
1223  * @tc.desc: Test IsExposureRecordEnable.
1224  * @tc.type: FUNC
1225  */
1226 HWTEST_F(EventRecorderTest, IsExposureRecordEnable003, TestSize.Level1)
1227 {
1228     int32_t index = static_cast<int32_t>(Recorder::EventCategory::CATEGORY_EXPOSURE);
1229     Recorder::EventRecorder::Get().globalSwitch_[index] = true;
1230     Recorder::EventRecorder::Get().eventSwitch_[index] = false;
1231     EXPECT_FALSE(Recorder::EventRecorder::Get().IsExposureRecordEnable());
1232 }
1233 
1234 /**
1235  * @tc.name: IsExposureRecordEnable004
1236  * @tc.desc: Test IsExposureRecordEnable.
1237  * @tc.type: FUNC
1238  */
1239 HWTEST_F(EventRecorderTest, IsExposureRecordEnable004, TestSize.Level1)
1240 {
1241     int32_t index = static_cast<int32_t>(Recorder::EventCategory::CATEGORY_EXPOSURE);
1242     Recorder::EventRecorder::Get().globalSwitch_[index] = false;
1243     Recorder::EventRecorder::Get().eventSwitch_[index] = false;
1244     EXPECT_FALSE(Recorder::EventRecorder::Get().IsExposureRecordEnable());
1245 }
1246 
1247 /**
1248  * @tc.name: GetPageUrl001
1249  * @tc.desc: Test GetPageUrl.
1250  * @tc.type: FUNC
1251  */
1252 HWTEST_F(EventRecorderTest, GetPageUrl001, TestSize.Level1)
1253 {
1254     Recorder::EventRecorder::Get().pageUrl_ = "url";
1255     Recorder::EventRecorder::Get().isFocusContainerChanged_ = false;
1256     std::string url = Recorder::EventRecorder::Get().GetPageUrl();
1257     EXPECT_NE(url, "");
1258 }
1259 
1260 /**
1261  * @tc.name: IsPageParamRecordEnable001
1262  * @tc.desc: Test IsPageParamRecordEnable.
1263  * @tc.type: FUNC
1264  */
1265 HWTEST_F(EventRecorderTest, IsPageParamRecordEnable001, TestSize.Level1)
1266 {
1267     int32_t index = static_cast<int32_t>(EventCategory::CATEGORY_PAGE_PARAM);
1268     EventRecorder::Get().globalSwitch_[index] = true;
1269     EventRecorder::Get().eventSwitch_[index] = false;
1270     EXPECT_FALSE(EventRecorder::Get().IsPageParamRecordEnable());
1271     index = static_cast<int32_t>(EventCategory::CATEGORY_WEB);
1272     EventRecorder::Get().globalSwitch_[index] = true;
1273     EventRecorder::Get().eventSwitch_[index] = true;
1274     EXPECT_FALSE(EventRecorder::Get().IsComponentRecordEnable());
1275 
1276     std::string str;
1277     GetConfigTest5(str);
1278     EventConfig* config = new EventConfig();
1279     config->Init(str);
1280     EXPECT_FALSE(config->GetWebJsCode().empty());
1281     EXPECT_FALSE(config->GetWebCategory().empty());
1282     EXPECT_TRUE(config->IsCategoryEnable(index));
1283     delete config;
1284 }
1285 
1286 /**
1287  * @tc.name: SaveJavascriptItems001
1288  * @tc.desc: Test SaveJavascriptItems.
1289  * @tc.type: FUNC
1290  */
1291 HWTEST_F(EventRecorderTest, SaveJavascriptItems001, TestSize.Level1)
1292 {
1293     std::map<std::string, std::vector<std::string>> items;
1294     EventRecorder::Get().SaveJavascriptItems(items);
1295     std::optional<std::map<std::string, std::vector<std::string>>> input;
1296     std::optional<std::vector<std::string>> orderItems;
1297     EventRecorder::Get().HandleJavascriptItems(input, orderItems);
1298 
1299     input = std::make_optional<std::map<std::string, std::vector<std::string>>>();
1300     EventRecorder::Get().HandleJavascriptItems(input, orderItems);
1301     EXPECT_TRUE(input.has_value());
1302 
1303     input = std::nullopt;
1304     items["hello"] = { "world" };
1305     EventController::Get().hasCached_ = false;
1306     EventController::Get().hasWebProcessed_ = true;
1307     EventRecorder::Get().SaveJavascriptItems(items);
1308     EXPECT_FALSE(Recorder::EventRecorder::Get().cacheScriptItems_.has_value());
1309     EventRecorder::Get().HandleJavascriptItems(input, orderItems);
1310     EXPECT_FALSE(input.has_value());
1311 
1312     EventController::Get().hasCached_ = false;
1313     EventController::Get().hasWebProcessed_ = false;
1314     EventRecorder::Get().SaveJavascriptItems(items);
1315     EXPECT_TRUE(EventRecorder::Get().cacheScriptItems_.has_value());
1316     input = std::nullopt;
1317     EventRecorder::Get().HandleJavascriptItems(input, orderItems);
1318     EXPECT_TRUE(input.has_value());
1319     EXPECT_FALSE(EventRecorder::Get().cacheScriptItems_.has_value());
1320 
1321     int32_t index = static_cast<int32_t>(EventCategory::CATEGORY_WEB);
1322     EventRecorder::Get().globalSwitch_[index] = true;
1323     EventRecorder::Get().eventSwitch_[index] = true;
1324     std::string config;
1325     GetConfigTest5(config);
1326     auto observer = std::make_shared<DemoUIEventObserver>();
1327     EventController::Get().Register(config, observer);
1328     std::optional<WebJsItem> scriptItems = std::nullopt;
1329     EventRecorder::Get().FillWebJsCode(scriptItems);
1330     EXPECT_TRUE(scriptItems.has_value());
1331     scriptItems = std::make_optional<WebJsItem>();
1332     EventRecorder::Get().FillWebJsCode(scriptItems);
1333     EXPECT_TRUE(scriptItems.has_value());
1334 
1335     EventController::Get().cacheJsCode_ = "hello";
1336     EXPECT_FALSE(EventRecorder::Get().GetCacheJsCode().empty());
1337 
1338     EXPECT_TRUE(EventRecorder::Get().IsMessageValid("test", "abc"));
1339     EXPECT_FALSE(EventRecorder::Get().IsMessageValid("", "abc"));
1340     EXPECT_FALSE(EventRecorder::Get().IsMessageValid("ttt", "abc"));
1341 }
1342 
1343 /**
1344  * @tc.name: InspectorTreeCollectorTest001
1345  * @tc.desc: Test register.
1346  * @tc.type: FUNC
1347  */
1348 HWTEST_F(EventRecorderTest, InspectorTreeCollectorTest001, TestSize.Level1)
1349 {
1350     /**
1351      * @tc.steps: step1. call the Constructor.
1352      * @tc.expected: step1. Constructor success.
1353      */
__anon84befa010202(const std::shared_ptr<std::string> tree) 1354     InspectorTreeCollector collector([](const std::shared_ptr<std::string> tree) {});
1355     EXPECT_TRUE(collector.root_ != nullptr);
1356 }
1357 
1358 /**
1359  * @tc.name: InspectorTreeCollectorTest002
1360  * @tc.desc: Test node data cache.
1361  * @tc.type: FUNC
1362  */
1363 HWTEST_F(EventRecorderTest, InspectorTreeCollectorTest002, TestSize.Level1)
1364 {
__anon84befa010302(const std::shared_ptr<std::string> tree) 1365     InspectorTreeCollector collector([](const std::shared_ptr<std::string> tree) {});
1366     collector.taskNum_ = 0;
1367     /**
1368      * @tc.steps: step1. call the IncreaseTaskNum.
1369      * @tc.expected: step1. IncreaseTaskNum success.
1370      */
1371     collector.IncreaseTaskNum();
1372     EXPECT_EQ(collector.taskNum_, 1);
1373     /**
1374      * @tc.steps: step2. call the DecreaseTaskNum.
1375      * @tc.expected: step2. DecreaseTaskNum success.
1376      */
1377     collector.DecreaseTaskNum();
1378     EXPECT_EQ(collector.taskNum_, 0);
1379     /**
1380      * @tc.steps: step3. call the UpdateTaskNum.
1381      * @tc.expected: step3. UpdateTaskNum success.
1382      */
1383     collector.UpdateTaskNum(2);
1384     EXPECT_EQ(collector.taskNum_, 2);
1385 }
1386 } // namespace OHOS::Ace
1387