1 /*
2  * Copyright (c) 2023-2024 iSoftStone Information Technology (Group) 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 #include <cstdint>
16 #include <fcntl.h>
17 #include <functional>
18 #include <memory>
19 #include <optional>
20 #include <utility>
21 #include <vector>
22 
23 #include "gtest/gtest.h"
24 
25 #define protected public
26 #define private public
27 #include "test/mock/base/mock_task_executor.h"
28 #include "test/mock/core/pipeline/mock_pipeline_context.h"
29 
30 #include "base/utils/utils.h"
31 #include "core/common/ace_application_info.h"
32 #include "core/components_ng/base/inspector.h"
33 #include "core/components_ng/base/ui_node.h"
34 #include "core/components_ng/pattern/stage/stage_manager.h"
35 #include "core/components_ng/pattern/stage/stage_pattern.h"
36 #include "core/components_ng/pattern/stage/page_pattern.h"
37 #include "core/components_ng/pattern/text/span_node.h"
38 #include "core/components_v2/inspector/inspector_constants.h"
39 #include "core/pipeline_ng/pipeline_context.h"
40 
41 using namespace testing;
42 using namespace testing::ext;
43 namespace OHOS::Ace::NG {
44 namespace {
45 std::string key = "key";
46 }; // namespace
47 
48 class InspectorTestNg : public testing::Test {
49 public:
SetUpTestSuite()50     static void SetUpTestSuite()
51     {
52         MockPipelineContext::SetUp();
53     }
TearDownTestSuite()54     static void TearDownTestSuite()
55     {
56         MockPipelineContext::TearDown();
57     }
58 };
59 
60 /**
61  * @tc.name: InspectorTestNg001
62  * @tc.desc: Test the operation of Inspector
63  * @tc.type: FUNC
64  */
65 HWTEST_F(InspectorTestNg, InspectorTestNg001, TestSize.Level1)
66 {
67     auto id = ElementRegister::GetInstance()->MakeUniqueId();
68     RefPtr<FrameNode> ONE = FrameNode::CreateFrameNode("one", id, AceType::MakeRefPtr<Pattern>(), true);
69     auto id2 = ElementRegister::GetInstance()->MakeUniqueId();
70     const RefPtr<FrameNode> TWO = FrameNode::CreateFrameNode("two", id2, AceType::MakeRefPtr<Pattern>());
71     /**
72      * @tc.steps: step1. callback GetFrameNodeByKey
73      * @tc.expected: expect the function is run ok
74      */
75     auto nodePtr = Inspector::GetFrameNodeByKey(key);
76     EXPECT_EQ(nodePtr, nullptr);
77 
78     /**
79      * @tc.steps: step2. callback SetUp
80      * @tc.expected: expect context1 is not null
81      */
82     auto context1 = PipelineContext::GetCurrentContext();
83     ASSERT_NE(context1, nullptr);
84     auto nodePtr1 = Inspector::GetFrameNodeByKey(key);
85     EXPECT_EQ(nodePtr1, nullptr);
86 
87     /**
88      * @tc.steps: step3. callback GetFrameNodeByKey
89      * @tc.expected: expect nodePtr2 not null
90      */
91     context1->rootNode_ = ONE;
92     context1->rootNode_->AddChild(TWO);
93     auto rootNode = context1->GetRootElement();
94     ASSERT_NE(rootNode, nullptr);
95     auto nodePtr2 = Inspector::GetFrameNodeByKey(key);
96     EXPECT_EQ(nodePtr2, nullptr);
97 
98     auto nodePtr3 = Inspector::GetFrameNodeByKey("");
99     ASSERT_NE(nodePtr3, nullptr);
100 }
101 
102 /**
103  * @tc.name: InspectorTestNg002
104  * @tc.desc: Test the operation of Inspector
105  * @tc.type: FUNC
106  */
107 HWTEST_F(InspectorTestNg, InspectorTestNg002, TestSize.Level1)
108 {
109     /**
110      * @tc.steps: step1. callback GetInspectorNodeByKey
111      * @tc.expected: expect the function is run ok
112      */
113     auto test = Inspector::GetInspectorNodeByKey(key);
114     EXPECT_EQ(test, "");
115 
116     /**
117      * @tc.steps: step2. callback SetUp
118      * @tc.expected: expect test1 is "".
119      */
120     auto context1 = PipelineContext::GetCurrentContext();
121     ASSERT_NE(context1, nullptr);
122     auto test1 = Inspector::GetInspectorNodeByKey(key);
123     EXPECT_EQ(test1, "");
124 
125     /**
126      * @tc.steps: step1. callback rootNode_
127      * @tc.expected: expect the function GetInspectorNodeByKey is return null
128      */
129     auto id = ElementRegister::GetInstance()->MakeUniqueId();
130     RefPtr<FrameNode> ONE = FrameNode::CreateFrameNode("one", id, AceType::MakeRefPtr<Pattern>(), true);
131     context1->rootNode_ = ONE;
132     ASSERT_NE(context1, nullptr);
133     auto test2 = Inspector::GetInspectorNodeByKey(key);
134     EXPECT_EQ(test2, "");
135     /**
136      * @tc.steps: step3. callback GetInspectorNodeByKey
137      * @tc.expected: expect nodePtr2 not null
138      */
139     auto test3 = Inspector::GetInspectorNodeByKey("");
140 }
141 
142 /**
143  * @tc.name: InspectorTestNg003
144  * @tc.desc: Test the operation of Inspector
145  * @tc.type: FUNC
146  */
147 HWTEST_F(InspectorTestNg, InspectorTestNg003, TestSize.Level1)
148 {
149     /**
150      * @tc.steps: step1. callback GetInspector
151      * @tc.expected: expect the function is run ok
152      */
153     auto id = ElementRegister::GetInstance()->MakeUniqueId();
154     RefPtr<FrameNode> ONE = FrameNode::CreateFrameNode("one", id, AceType::MakeRefPtr<Pattern>(), true);
155     auto context1 = PipelineContext::GetCurrentContext();
156     ASSERT_NE(context1, nullptr);
157     context1->stageManager_ = AceType::MakeRefPtr<StageManager>(ONE);
158     auto id2 = ElementRegister::GetInstance()->MakeUniqueId();
159     const RefPtr<FrameNode> TWO = FrameNode::CreateFrameNode("stage", id2, AceType::MakeRefPtr<Pattern>());
160     ONE->AddChild(TWO);
161     auto id3 = ElementRegister::GetInstance()->MakeUniqueId();
162     const RefPtr<FrameNode> THREE = FrameNode::CreateFrameNode("three", id3, AceType::MakeRefPtr<Pattern>());
163     THREE->AddChild(ONE);
164 
165     /**
166      * @tc.steps: step2. call GetInspector
167      * @tc.expected: the return value is empty string
168      */
169     auto test1 = Inspector::GetInspector(false);
170     EXPECT_NE(test1, "");
171     auto test3 = Inspector::GetInspector(true);
172     EXPECT_NE(test3, "");
173     auto test4 = Inspector::GetInspectorOfNode(TWO);
174     EXPECT_NE(test4, "");
175 
176     RefPtr<MockPipelineContext> pipeline_bak = MockPipelineContext::pipeline_;
177     MockPipelineContext::pipeline_ = nullptr;
178     auto jsonRoot = JsonUtil::Create(true);
179     const char inspectorType[] = "$type";
180     const char inspectorRoot[] = "root";
181     jsonRoot->Put(inspectorType, inspectorRoot);
182     auto test5 = Inspector::GetInspector(false);
183     EXPECT_EQ(test5, jsonRoot->ToString());
184     MockPipelineContext::pipeline_ = pipeline_bak;
185 
186     InspectorFilter filter;
187     std::string testId = "test";
188     filter.SetFilterID(testId);
189     bool needThrow = false;
190     auto test6 = Inspector::GetInspector(false, filter, needThrow);
191     auto rootNode = context1->GetStageManager()->GetLastPage();
192     if (rootNode == nullptr) {
193         EXPECT_EQ(test6, jsonRoot->ToString());
194     } else {
195         EXPECT_NE(test6, "");
196     }
197     context1->stageManager_ = nullptr;
198 }
199 
200 /**
201  * @tc.name: InspectorTestNg004
202  * @tc.desc: Test the operation of View Inspector
203  * @tc.type: FUNC
204  */
205 HWTEST_F(InspectorTestNg, InspectorTestNg004, TestSize.Level1)
206 {
207     /**
208      * @tc.steps: step1. set rootNode and taskExecutor call SendEventByKey
209      * @tc.expected: expect return true
210      */
211     auto context = NG::PipelineContext::GetCurrentContext();
212     context->rootNode_ = FrameNode::CreateFrameNode("frameNode", 60, AceType::MakeRefPtr<Pattern>(), true);
213     context->rootNode_->SetGeometryNode(AceType::MakeRefPtr<GeometryNode>());
214     context->taskExecutor_ = AceType::MakeRefPtr<MockTaskExecutor>();
215     auto test3 = Inspector::SendEventByKey("", static_cast<int>(AceAction::ACTION_CLICK), "params");
216     EXPECT_EQ(test3, true);
217     test3 = Inspector::SendEventByKey("", static_cast<int>(AceAction::ACTION_LONG_CLICK), "params");
218     EXPECT_EQ(test3, true);
219     test3 = Inspector::SendEventByKey("", 31, "params");
220     EXPECT_EQ(test3, true);
221 }
222 
223 /**
224  * @tc.name: InspectorTestNg005
225  * @tc.desc: Test the operation of View Inspector
226  * @tc.type: FUNC
227  */
228 HWTEST_F(InspectorTestNg, InspectorTestNg005, TestSize.Level1)
229 {
230     /**
231      * @tc.steps: step1. callback HideAllMenus
232      * @tc.expected: expect the function is run ok
233      */
234     Inspector::HideAllMenus();
235     auto context = PipelineContext::GetCurrentContext();
236 
237     /**
238      * @tc.steps: step2. callback SetUp
239      * @tc.expected: step2. expect context1 is return null.
240      */
241     auto context1 = PipelineContext::GetCurrentContext();
242     ASSERT_NE(context1, nullptr);
243     Inspector::HideAllMenus();
244 }
245 
246 /**
247  * @tc.name: InspectorTestNg006
248  * @tc.desc: Test the operation of Inspector
249  * @tc.type: FUNC
250  */
251 HWTEST_F(InspectorTestNg, InspectorTestNg006, TestSize.Level1)
252 {
253     /**
254      * @tc.steps: step1. callback GetInspector
255      * @tc.expected: expect the function is run ok
256      */
257     auto context1 = PipelineContext::GetCurrentContext();
258     ASSERT_NE(context1, nullptr);
259 
260     auto id = ElementRegister::GetInstance()->MakeUniqueId();
261     RefPtr<FrameNode> ONE = FrameNode::CreateFrameNode("one", id, AceType::MakeRefPtr<Pattern>(), true);
262     context1->stageManager_ = AceType::MakeRefPtr<StageManager>(ONE);
263 
264     /**
265      * @tc.steps: step2 add child to two and create a temp node with tag "temp"
266                 call GetInspector
267      * @tc.expected: the return value is empty string
268      */
269     auto id2 = ElementRegister::GetInstance()->MakeUniqueId();
270     const RefPtr<FrameNode> TWO = FrameNode::CreateFrameNode("two", id2, AceType::MakeRefPtr<Pattern>());
271     ONE->children_.clear();
272     ONE->AddChild(TWO);
273     auto stageParent = FrameNode::CreateFrameNode("stageParent", 5, AceType::MakeRefPtr<Pattern>(), true);
274     stageParent->AddChild(ONE);
275 
276     const RefPtr<FrameNode> THREE = FrameNode::CreateFrameNode(
277         "three", ElementRegister::GetInstance()->MakeUniqueId(), AceType::MakeRefPtr<Pattern>());
278     const RefPtr<FrameNode> STAGE = FrameNode::CreateFrameNode(
279         "stage", ElementRegister::GetInstance()->MakeUniqueId(), AceType::MakeRefPtr<Pattern>(), true);
280     const RefPtr<FrameNode> PAGE = FrameNode::CreateFrameNode(
281         "page", ElementRegister::GetInstance()->MakeUniqueId(), AceType::MakeRefPtr<Pattern>(), true);
282     THREE->isInternal_ = true;
283     TWO->AddChild(PAGE);
284     TWO->AddChild(THREE);
285     TWO->AddChild(STAGE);
286     auto temp = FrameNode::CreateFrameNode("temp", 5, AceType::MakeRefPtr<Pattern>(), true);
287     STAGE->AddChild(temp);
288     ONE->tag_ = "stage";
289     auto test1 = Inspector::GetInspector(false);
290     EXPECT_NE(test1, "");
291 
292     PAGE->hostPageId_ = TWO->GetPageId();
293     auto test2 = Inspector::GetInspector(false);
294     EXPECT_NE(test2, "");
295 
296     PAGE->hostPageId_ = TWO->GetPageId() + 1;
297     test2 = Inspector::GetInspector(false);
298     EXPECT_NE(test2, "");
299 
300     TWO->children_.clear();
301     auto test3 = Inspector::GetInspector(false);
302     EXPECT_NE(test3, "");
303 
304     /**
305      *  ONE(stageNode)--TWO(GetLastPage())--PAGE
306      *                   |--THREE
307      *                   |--STAGE--temp
308      *                   |--frameNode
309      *                   |--frameNode2--frameNode3
310      */
311     /**
312      * @tc.steps: step3 add child to two and create a frame node tree
313             call GetInspector
314      * @tc.expected: the return value is not empty string
315      */
316     auto spanNode = SpanNode::GetOrCreateSpanNode(int32_t(191));
317     TWO->AddChild(spanNode);
318 
319     auto frameNode = FrameNode::CreateFrameNode("frameNode", 6, AceType::MakeRefPtr<Pattern>(), true);
320     TWO->AddChild(frameNode);
321     frameNode->isActive_ = true;
322     auto frameNode2 = FrameNode::CreateFrameNode("frameNode2", 62, AceType::MakeRefPtr<Pattern>(), true);
323     TWO->AddChild(frameNode2);
324     frameNode2->isActive_ = false;
325     auto frameNode3 = FrameNode::CreateFrameNode("frameNode3", 63, AceType::MakeRefPtr<Pattern>(), true);
326     frameNode2->AddChild(frameNode3);
327     test3 = Inspector::GetInspector(false);
328     EXPECT_NE(test3, "");
329     context1->stageManager_ = nullptr;
330 }
331 
332 /**
333  * @tc.name: InspectorTestNg007
334  * @tc.desc: Test the operation of Inspector
335  * @tc.type: FUNC
336  */
337 HWTEST_F(InspectorTestNg, InspectorTestNg007, TestSize.Level1)
338 {
339     /**
340      * @tc.steps: step1. callback GetInspector
341      * @tc.expected: expect the function is run ok
342      */
343     auto id = ElementRegister::GetInstance()->MakeUniqueId();
344     RefPtr<FrameNode> ONE = FrameNode::CreateFrameNode("one", id, AceType::MakeRefPtr<Pattern>(), true);
345     auto context1 = PipelineContext::GetCurrentContext();
346     ASSERT_NE(context1, nullptr);
347     context1->stageManager_ = AceType::MakeRefPtr<StageManager>(ONE);
348 
349     /**
350      * @tc.steps: step2. callback pop_back
351      * @tc.expected: expect clean children and pageRootNode is noll
352      */
353     auto pageRootNode = context1->GetStageManager()->GetLastPage();
354     auto test = Inspector::GetInspector(false);
355     auto str = "{\"$type\":\"root\",\"width\":\"720.000000\",\"height\":\"1280.000000\",\"$resolution\":\"1.000000\"}";
356     EXPECT_EQ(test, str);
357     context1->stageManager_ = nullptr;
358 }
359 
360 /**
361  * @tc.name: InspectorTestNg008
362  * @tc.desc: Test the GetRectangleById
363  * @tc.type: FUNC
364  */
365 HWTEST_F(InspectorTestNg, InspectorTestNg008, TestSize.Level1)
366 {
367     /**
368      * @tc.steps: step1. creat frameNode
369      * @tc.expected: expect the function is run ok
370      */
371     auto id = ElementRegister::GetInstance()->MakeUniqueId();
372     RefPtr<FrameNode> ONE = FrameNode::CreateFrameNode("one", id, AceType::MakeRefPtr<Pattern>(), true);
373     auto context = NG::PipelineContext::GetCurrentContext();
374     ASSERT_NE(context, nullptr);
375 
376     /**
377      * @tc.steps: step2. assign value to rootNode_
378      * @tc.expected: rootNode_ pass non-null check.
379      */
380     context->rootNode_ = ONE;
381     context->rootNode_->SetGeometryNode(AceType::MakeRefPtr<GeometryNode>());
382 
383     /**
384      * @tc.steps: step3. construct assignments and call GetRectangleById.
385      * @tc.expected: expect the GetRectangleById is run ok and result is expected.
386      */
387     OHOS::Ace::NG::Rectangle rect;
388     string key = "";
389     Inspector::GetRectangleById(key, rect);
390     float zResult = 1.0f;
391     EXPECT_EQ(rect.scale.z, zResult);
392     context->rootNode_ = nullptr;
393 }
394 
395 /**
396  * @tc.name: InspectorTestNg009
397  * @tc.desc: Test the GetFrameNodeByKey
398  * @tc.type: FUNC
399  */
400 HWTEST_F(InspectorTestNg, InspectorTestNg009, TestSize.Level1)
401 {
402     auto id = ElementRegister::GetInstance()->MakeUniqueId();
403     RefPtr<FrameNode> ONE = FrameNode::CreateFrameNode("one", id, AceType::MakeRefPtr<Pattern>(), true);
404     auto id2 = ElementRegister::GetInstance()->MakeUniqueId();
405     const RefPtr<FrameNode> TWO = FrameNode::CreateFrameNode("two", id2, AceType::MakeRefPtr<Pattern>());
406     auto context = PipelineContext::GetCurrentContext();
407     ASSERT_NE(context, nullptr);
408     auto nodePtr = Inspector::GetFrameNodeByKey(key);
409     EXPECT_EQ(nodePtr, nullptr);
410 
411     Inspector::AddOffscreenNode(ONE);
412     nodePtr = Inspector::GetFrameNodeByKey("one");
413     EXPECT_EQ(nodePtr, 0);
414 
415     RefPtr<MockPipelineContext> pipeline_bak = MockPipelineContext::pipeline_;
416     MockPipelineContext::pipeline_ = nullptr;
417     context = PipelineContext::GetCurrentContext();
418     EXPECT_EQ(context, nullptr);
419     nodePtr = Inspector::GetFrameNodeByKey("two");
420     EXPECT_EQ(nodePtr, nullptr);
421     MockPipelineContext::pipeline_ = pipeline_bak;
422 }
423 
424 /**
425  * @tc.name: InspectorTestNg010
426  * @tc.desc: Test the GetRectangleById
427  * @tc.type: FUNC
428  */
429 HWTEST_F(InspectorTestNg, InspectorTestNg010, TestSize.Level1)
430 {
431     auto id = ElementRegister::GetInstance()->MakeUniqueId();
432     RefPtr<FrameNode> ONE = FrameNode::CreateFrameNode("one", id, AceType::MakeRefPtr<Pattern>(), true);
433     auto context = NG::PipelineContext::GetCurrentContext();
434     ASSERT_NE(context, nullptr);
435     context->rootNode_ = ONE;
436     context->rootNode_->SetGeometryNode(AceType::MakeRefPtr<GeometryNode>());
437     std::string key = "key";
438     OHOS::Ace::NG::Rectangle rect;
439     Inspector::GetRectangleById(key, rect);
440     EXPECT_EQ(rect.size.Width(), 0.0f);
441 
442     auto frameNode = Inspector::GetFrameNodeByKey("one");
443     RefPtr<RenderContext> renderContextBak = ONE->renderContext_;
444     ONE->renderContext_ = nullptr;
445     Inspector::GetRectangleById("one", rect);
446     EXPECT_EQ(rect.screenRect.Width(), 0.0f);
447     ONE->renderContext_ = renderContextBak;
448     EXPECT_EQ(rect.size.Width(), 0.0f);
449 }
450 
451 /**
452  * @tc.name: InspectorTestNg011
453  * @tc.desc: Test the operation of GetSubWindowInspector
454  * @tc.type: FUNC
455  */
456 HWTEST_F(InspectorTestNg, InspectorTestNg011, TestSize.Level1)
457 {
458     auto id = ElementRegister::GetInstance()->MakeUniqueId();
459     RefPtr<FrameNode> ONE = FrameNode::CreateFrameNode("one", id, AceType::MakeRefPtr<Pattern>(), true);
460     auto context = PipelineContext::GetCurrentContext();
461     ASSERT_NE(context, nullptr);
462     context->stageManager_ = AceType::MakeRefPtr<StageManager>(ONE);
463     auto pageRootNode = context->GetStageManager()->GetLastPage();
464     auto test = Inspector::GetSubWindowInspector(false);
465     auto str = "";
466     EXPECT_NE(test, str);
467     context->stageManager_ = nullptr;
468 }
469 
470 /**
471  * @tc.name: InspectorTestNg012
472  * @tc.desc: Test the operation of GetSimplifiedInspector
473  * @tc.type: FUNC
474  */
475 HWTEST_F(InspectorTestNg, InspectorTestNg012, TestSize.Level1)
476 {
477     auto id = ElementRegister::GetInstance()->MakeUniqueId();
478     RefPtr<FrameNode> ONE = FrameNode::CreateFrameNode("one", id, AceType::MakeRefPtr<Pattern>(), true);
479     auto context = PipelineContext::GetCurrentContext();
480     ASSERT_NE(context, nullptr);
481     context->stageManager_ = AceType::MakeRefPtr<StageManager>(ONE);
482     int32_t containerId = 1;
483     std::string result = Inspector::GetSimplifiedInspector(containerId);
484     EXPECT_NE(result, "");
485     context->stageManager_ = nullptr;
486 }
487 
488 /**
489  * @tc.name: InspectorTestNg013
490  * @tc.desc: Test the function of InspectorFilter
491  * @tc.type: FUNC
492  */
493 HWTEST_F(InspectorTestNg, InspectorTestNg013, TestSize.Level1)
494 {
495     const char* hello = "hi";
496     InspectorFilter testFilter;
497     EXPECT_EQ(testFilter.CheckFilterAttr(FixedAttrBit::FIXED_ATTR_CONTENT, hello), true);
498     testFilter.SetFilterDepth(1);
499     std::string id = "id";
500     testFilter.SetFilterID(id);
501     testFilter.filterExt.emplace_back("abc");
502     testFilter.AddFilterAttr("focusable");
503     testFilter.AddFilterAttr("abc");
504     testFilter.AddFilterAttr("def");
505     EXPECT_EQ(testFilter.CheckFixedAttr(FixedAttrBit::FIXED_ATTR_CONTENT), false);
506     EXPECT_EQ(testFilter.CheckExtAttr("zzz"), false);
507     EXPECT_EQ(testFilter.CheckFilterAttr(FixedAttrBit::FIXED_ATTR_CONTENT, hello), false);
508     EXPECT_EQ(testFilter.IsFastFilter(), false);
509 }
510 
511 /**
512  * @tc.name: InspectorTestNg014
513  * @tc.desc: Test the function of InspectorFilter
514  * @tc.type: FUNC
515  */
516 HWTEST_F(InspectorTestNg, InspectorTestNg014, TestSize.Level1)
517 {
518     const char* hello = "hi";
519     InspectorFilter testFilter;
520     testFilter.AddFilterAttr("focusable");
521     EXPECT_EQ(testFilter.CheckFilterAttr(FixedAttrBit::FIXED_ATTR_FOCUSABLE, hello), true);
522 }
523 
524 /**
525  * @tc.name: InspectorTestNg015
526  * @tc.desc: Test the function of InspectorFilter
527  * @tc.type: FUNC
528  */
529 HWTEST_F(InspectorTestNg, InspectorTestNg015, TestSize.Level1)
530 {
531     const char* hello = "abc";
532     InspectorFilter testFilter;
533     testFilter.filterExt.emplace_back("abc");
534     testFilter.AddFilterAttr("focusable");
535     EXPECT_EQ(testFilter.CheckFilterAttr(FixedAttrBit::FIXED_ATTR_CONTENT, hello), true);
536 }
537 
538 /**
539  * @tc.name: InspectorTestNg016
540  * @tc.desc: Test the operation of GetInspectorTree
541  * @tc.type: FUNC
542  */
543 HWTEST_F(InspectorTestNg, InspectorTestNg016, TestSize.Level1)
544 {
545     // tc.steps: step1. callback GetInspectorTree
546     // tc.expected: expect the function is run ok
547     auto context1 = PipelineContext::GetCurrentContext();
548     ASSERT_NE(context1, nullptr);
549 
550     auto id = ElementRegister::GetInstance()->MakeUniqueId();
551     RefPtr<FrameNode> stageNode = FrameNode::CreateFrameNode("stage", id, AceType::MakeRefPtr<Pattern>(), true);
552     context1->stageManager_ = AceType::MakeRefPtr<StageManager>(stageNode);
553 
554     // tc.steps: step2 add lastPage and create a frame node tree to lastPage
555     auto id2 = ElementRegister::GetInstance()->MakeUniqueId();
556     const RefPtr<FrameNode> lastPage = FrameNode::CreateFrameNode("two", id2, AceType::MakeRefPtr<Pattern>());
557     stageNode->children_.clear();
558     stageNode->AddChild(lastPage);
559     auto stageParent = FrameNode::CreateFrameNode("stageParent", 5, AceType::MakeRefPtr<Pattern>(), true);
560     stageParent->AddChild(stageNode);
561     auto frameNode = FrameNode::CreateFrameNode("frameNode", 6, AceType::MakeRefPtr<Pattern>(), true);
562     lastPage->AddChild(frameNode);
563     frameNode->isActive_ = true;
564     auto frameNode2 = FrameNode::CreateFrameNode("frameNode2", 62, AceType::MakeRefPtr<Pattern>(), true);
565     lastPage->AddChild(frameNode2);
566     frameNode2->isActive_ = false;
567     auto frameNode3 = FrameNode::CreateFrameNode("frameNode3", 63, AceType::MakeRefPtr<Pattern>(), true);
568     frameNode2->AddChild(frameNode3);
569     NG::InspectorTreeMap treesInfos;
570     Inspector::GetInspectorTree(treesInfos);
571     EXPECT_TRUE(!treesInfos.empty());
572     context1->stageManager_ = nullptr;
573 }
574 
575 /**
576  * @tc.name: AddOffscreenNode_001
577  * @tc.desc: Test the operation of AddOffscreenNode
578  * @tc.type: FUNC
579  */
580 HWTEST_F(InspectorTestNg, AddOffscreenNode_001, TestSize.Level1)
581 {
582     int32_t num = Inspector::offscreenNodes.size();
583     RefPtr<FrameNode> one = nullptr;
584     Inspector::AddOffscreenNode(one);
585     EXPECT_EQ(Inspector::offscreenNodes.size(), num);
586 
587     auto id = ElementRegister::GetInstance()->MakeUniqueId();
588     one = FrameNode::CreateFrameNode("one", id, AceType::MakeRefPtr<Pattern>(), true);
589     auto context = PipelineContext::GetCurrentContext();
590     ASSERT_NE(context, nullptr);
591 
592     context->stageManager_ = AceType::MakeRefPtr<StageManager>(one);
593     num = Inspector::offscreenNodes.size();
594     Inspector::AddOffscreenNode(one);
595     EXPECT_EQ(Inspector::offscreenNodes.size(), num + 1);
596     context->stageManager_ = nullptr;
597 }
598 
599 /**
600  * @tc.name: RemoveOffscreenNode_001
601  * @tc.desc: Test the operation of RemoveOffscreenNode
602  * @tc.type: FUNC
603  */
604 HWTEST_F(InspectorTestNg, RemoveOffscreenNode_001, TestSize.Level1)
605 {
606     int32_t num = Inspector::offscreenNodes.size();
607     RefPtr<FrameNode> one = nullptr;
608     Inspector::RemoveOffscreenNode(one);
609     EXPECT_EQ(Inspector::offscreenNodes.size(), num);
610 
611     auto id = ElementRegister::GetInstance()->MakeUniqueId();
612     one = FrameNode::CreateFrameNode("one", id, AceType::MakeRefPtr<Pattern>(), true);
613     auto context = PipelineContext::GetCurrentContext();
614     ASSERT_NE(context, nullptr);
615     context->stageManager_ = AceType::MakeRefPtr<StageManager>(one);
616     Inspector::AddOffscreenNode(one);
617     num = Inspector::offscreenNodes.size();
618 
619     Inspector::RemoveOffscreenNode(one);
620     EXPECT_EQ(Inspector::offscreenNodes.size(), num - 1);
621     context->stageManager_ = nullptr;
622 }
623 
624 /**
625  * @tc.name: GetOffScreenTreeNodes_001
626  * @tc.desc: Test the operation of GetOffScreenTreeNodes
627  * @tc.type: FUNC
628  */
629 HWTEST_F(InspectorTestNg, GetOffScreenTreeNodes_001, TestSize.Level1)
630 {
631     auto id = ElementRegister::GetInstance()->MakeUniqueId();
632     RefPtr<FrameNode> one = FrameNode::CreateFrameNode("one", id, AceType::MakeRefPtr<Pattern>(), true);
633     auto context = PipelineContext::GetCurrentContext();
634     ASSERT_NE(context, nullptr);
635     context->stageManager_ = AceType::MakeRefPtr<StageManager>(one);
636     Inspector::AddOffscreenNode(one);
637     int32_t num = Inspector::offscreenNodes.size();
638     NG::InspectorTreeMap offNodes;
639     Inspector::GetOffScreenTreeNodes(offNodes);
640     EXPECT_EQ(offNodes.size(), num);
641     context->stageManager_ = nullptr;
642 }
643 
644 /**
645  * @tc.name: GetRecordAllPagesNodes_001
646  * @tc.desc: Test the operation of GetRecordAllPagesNodes
647  * (stageNode)--PageA--PageB--PageC
648  *                              |--frameNode
649  *                              |--frameNode1--frameNode3
650  *                              |--frameNode2
651  * @tc.type: FUNC
652  */
653 HWTEST_F(InspectorTestNg, GetRecordAllPagesNodes_001, TestSize.Level1)
654 {
655     // tc.steps: step1. call GetRecordAllPagesNodes
656     // tc.expected: expect the function is run ok
657     auto context1 = PipelineContext::GetCurrentContext();
658     ASSERT_NE(context1, nullptr);
659 
660     auto id = ElementRegister::GetInstance()->MakeUniqueId();
661     RefPtr<FrameNode> stageNode = FrameNode::CreateFrameNode("sageNode", id, AceType::MakeRefPtr<Pattern>(), true);
662     context1->stageManager_ = AceType::MakeRefPtr<StageManager>(stageNode);
663     stageNode->children_.clear();
664 
665     // tc.steps: step2 add lastPage and create a frame node tree to lastPage
666     auto id2 = ElementRegister::GetInstance()->MakeUniqueId();
667     const RefPtr<FrameNode> pageA = FrameNode::CreateFrameNode("PageA", id2,
668         AceType::MakeRefPtr<PagePattern>(AceType::MakeRefPtr<PageInfo>()));
669     stageNode->AddChild(pageA);
670     auto id3 = ElementRegister::GetInstance()->MakeUniqueId();
671     const RefPtr<FrameNode> pageB = FrameNode::CreateFrameNode("PageB", id3,
672         AceType::MakeRefPtr<PagePattern>(AceType::MakeRefPtr<PageInfo>()));
673     stageNode->AddChild(pageB);
674     auto id4 = ElementRegister::GetInstance()->MakeUniqueId();
675     const RefPtr<FrameNode> pageC = FrameNode::CreateFrameNode("PageC", id4,
676         AceType::MakeRefPtr<PagePattern>(AceType::MakeRefPtr<PageInfo>()));
677     stageNode->AddChild(pageC);
678 
679     auto frameNode = FrameNode::CreateFrameNode("frameNode0", 5, AceType::MakeRefPtr<Pattern>(), true);
680     pageC->AddChild(frameNode);
681     auto frameNode1 = FrameNode::CreateFrameNode("frameNode1", 6, AceType::MakeRefPtr<Pattern>(), true);
682     pageC->AddChild(frameNode1);
683     auto frameNode2 = FrameNode::CreateFrameNode("frameNode2", 62, AceType::MakeRefPtr<Pattern>(), true);
684     pageC->AddChild(frameNode2);
685     frameNode2->isActive_ = false;
686     auto frameNode3 = FrameNode::CreateFrameNode("frameNode3", 63, AceType::MakeRefPtr<Pattern>(), true);
687     frameNode1->AddChild(frameNode3);
688     frameNode3->isActive_ = true;
689     NG::InspectorTreeMap treesInfos;
690     Inspector::GetRecordAllPagesNodes(treesInfos);
691     EXPECT_TRUE(!treesInfos.empty());
692     context1->stageManager_ = nullptr;
693 }
694 } // namespace OHOS::Ace::NG
695