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