• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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/text/span_node.h"
37 #include "core/components_v2/inspector/inspector_constants.h"
38 #include "core/pipeline_ng/pipeline_context.h"
39 
40 using namespace testing;
41 using namespace testing::ext;
42 namespace OHOS::Ace::NG {
43 namespace {
44 std::string key = "key";
45 }; // namespace
46 
47 class InspectorTestNg : public testing::Test {
48 public:
SetUpTestSuite()49     static void SetUpTestSuite()
50     {
51         MockPipelineContext::SetUp();
52     }
TearDownTestSuite()53     static void TearDownTestSuite()
54     {
55         MockPipelineContext::TearDown();
56     }
57 };
58 
59 /**
60  * @tc.name: InspectorTestNg001
61  * @tc.desc: Test the operation of Inspector
62  * @tc.type: FUNC
63  */
64 HWTEST_F(InspectorTestNg, InspectorTestNg001, TestSize.Level1)
65 {
66     auto id = ElementRegister::GetInstance()->MakeUniqueId();
67     RefPtr<FrameNode> ONE = FrameNode::CreateFrameNode("one", id, AceType::MakeRefPtr<Pattern>(), true);
68     auto id2 = ElementRegister::GetInstance()->MakeUniqueId();
69     const RefPtr<FrameNode> TWO = FrameNode::CreateFrameNode("two", id2, AceType::MakeRefPtr<Pattern>());
70     /**
71      * @tc.steps: step1. callback GetFrameNodeByKey
72      * @tc.expected: expect the function is run ok
73      */
74     auto nodePtr = Inspector::GetFrameNodeByKey(key);
75     EXPECT_EQ(nodePtr, nullptr);
76 
77     /**
78      * @tc.steps: step2. callback SetUp
79      * @tc.expected: expect context1 is not null
80      */
81     auto context1 = PipelineContext::GetCurrentContext();
82     ASSERT_NE(context1, nullptr);
83     auto nodePtr1 = Inspector::GetFrameNodeByKey(key);
84     EXPECT_EQ(nodePtr1, nullptr);
85 
86     /**
87      * @tc.steps: step3. callback GetFrameNodeByKey
88      * @tc.expected: expect nodePtr2 not null
89      */
90     context1->rootNode_ = ONE;
91     context1->rootNode_->AddChild(TWO);
92     auto rootNode = context1->GetRootElement();
93     ASSERT_NE(rootNode, nullptr);
94     auto nodePtr2 = Inspector::GetFrameNodeByKey(key);
95     EXPECT_EQ(nodePtr2, nullptr);
96 
97     auto nodePtr3 = Inspector::GetFrameNodeByKey("");
98     ASSERT_NE(nodePtr3, nullptr);
99 
100     context1->rootNode_ = nullptr;
101 }
102 
103 /**
104  * @tc.name: InspectorTestNg002
105  * @tc.desc: Test the operation of Inspector
106  * @tc.type: FUNC
107  */
108 HWTEST_F(InspectorTestNg, InspectorTestNg002, TestSize.Level1)
109 {
110     /**
111      * @tc.steps: step1. callback GetInspectorNodeByKey
112      * @tc.expected: expect the function is run ok
113      */
114     auto test = Inspector::GetInspectorNodeByKey(key);
115     EXPECT_EQ(test, "");
116 
117     /**
118      * @tc.steps: step2. callback SetUp
119      * @tc.expected: expect test1 is "".
120      */
121     auto context1 = PipelineContext::GetCurrentContext();
122     ASSERT_NE(context1, nullptr);
123     auto test1 = Inspector::GetInspectorNodeByKey(key);
124     EXPECT_EQ(test1, "");
125 
126     /**
127      * @tc.steps: step1. callback rootNode_
128      * @tc.expected: expect the function GetInspectorNodeByKey is return null
129      */
130     auto id = ElementRegister::GetInstance()->MakeUniqueId();
131     RefPtr<FrameNode> ONE = FrameNode::CreateFrameNode("one", id, AceType::MakeRefPtr<Pattern>(), true);
132     context1->rootNode_ = ONE;
133     ASSERT_NE(context1, nullptr);
134     auto test2 = Inspector::GetInspectorNodeByKey(key);
135     EXPECT_EQ(test2, "");
136     /**
137      * @tc.steps: step3. callback GetInspectorNodeByKey
138      * @tc.expected: expect nodePtr2 not null
139      */
140     auto test3 = Inspector::GetInspectorNodeByKey("");
141 
142     context1->rootNode_ = nullptr;
143 }
144 
145 /**
146  * @tc.name: InspectorTestNg003
147  * @tc.desc: Test the operation of Inspector
148  * @tc.type: FUNC
149  */
150 HWTEST_F(InspectorTestNg, InspectorTestNg003, TestSize.Level1)
151 {
152     /**
153      * @tc.steps: step1. callback GetInspector
154      * @tc.expected: expect the function is run ok
155      */
156     auto id = ElementRegister::GetInstance()->MakeUniqueId();
157     RefPtr<FrameNode> ONE = FrameNode::CreateFrameNode("one", id, AceType::MakeRefPtr<Pattern>(), true);
158     auto context1 = PipelineContext::GetCurrentContext();
159     ASSERT_NE(context1, nullptr);
160     context1->stageManager_ = AceType::MakeRefPtr<StageManager>(ONE);
161     auto id2 = ElementRegister::GetInstance()->MakeUniqueId();
162     const RefPtr<FrameNode> TWO = FrameNode::CreateFrameNode("stage", id2, AceType::MakeRefPtr<Pattern>());
163     ONE->AddChild(TWO);
164     auto id3 = ElementRegister::GetInstance()->MakeUniqueId();
165     const RefPtr<FrameNode> THREE = FrameNode::CreateFrameNode("three", id3, AceType::MakeRefPtr<Pattern>());
166     THREE->AddChild(ONE);
167 
168     /**
169      * @tc.steps: step2. call GetInspector
170      * @tc.expected: the return value is empty string
171      */
172     auto test1 = Inspector::GetInspector(false);
173     EXPECT_NE(test1, "");
174     auto test3 = Inspector::GetInspector(true);
175     EXPECT_NE(test3, "");
176 
177     context1->stageManager_ = nullptr;
178 }
179 
180 /**
181  * @tc.name: InspectorTestNg004
182  * @tc.desc: Test the operation of View Inspector
183  * @tc.type: FUNC
184  */
185 HWTEST_F(InspectorTestNg, InspectorTestNg004, TestSize.Level1)
186 {
187     /**
188      * @tc.steps: step1. set rootNode and taskExecutor call SendEventByKey
189      * @tc.expected: expect return true
190      */
191     auto context = NG::PipelineContext::GetCurrentContext();
192     context->rootNode_ = FrameNode::CreateFrameNode("frameNode", 60, AceType::MakeRefPtr<Pattern>(), true);
193     context->rootNode_->SetGeometryNode(AceType::MakeRefPtr<GeometryNode>());
194     context->taskExecutor_ = AceType::MakeRefPtr<MockTaskExecutor>();
195     auto test3 = Inspector::SendEventByKey("", static_cast<int>(AceAction::ACTION_CLICK), "params");
196     EXPECT_EQ(test3, true);
197     test3 = Inspector::SendEventByKey("", static_cast<int>(AceAction::ACTION_LONG_CLICK), "params");
198     EXPECT_EQ(test3, true);
199     test3 = Inspector::SendEventByKey("", 31, "params");
200     EXPECT_EQ(test3, true);
201 
202     context->rootNode_ = nullptr;
203 }
204 
205 /**
206  * @tc.name: InspectorTestNg005
207  * @tc.desc: Test the operation of View Inspector
208  * @tc.type: FUNC
209  */
210 HWTEST_F(InspectorTestNg, InspectorTestNg005, TestSize.Level1)
211 {
212     /**
213      * @tc.steps: step1. callback HideAllMenus
214      * @tc.expected: expect the function is run ok
215      */
216     Inspector::HideAllMenus();
217     auto context = PipelineContext::GetCurrentContext();
218 
219     /**
220      * @tc.steps: step2. callback SetUp
221      * @tc.expected: step2. expect context1 is return null.
222      */
223     auto context1 = PipelineContext::GetCurrentContext();
224     ASSERT_NE(context1, nullptr);
225     Inspector::HideAllMenus();
226 }
227 
228 /**
229  * @tc.name: InspectorTestNg006
230  * @tc.desc: Test the operation of Inspector
231  * @tc.type: FUNC
232  */
233 HWTEST_F(InspectorTestNg, InspectorTestNg006, TestSize.Level1)
234 {
235     /**
236      * @tc.steps: step1. callback GetInspector
237      * @tc.expected: expect the function is run ok
238      */
239     auto context1 = PipelineContext::GetCurrentContext();
240     ASSERT_NE(context1, nullptr);
241 
242     auto id = ElementRegister::GetInstance()->MakeUniqueId();
243     RefPtr<FrameNode> ONE = FrameNode::CreateFrameNode("one", id, AceType::MakeRefPtr<Pattern>(), true);
244     context1->stageManager_ = AceType::MakeRefPtr<StageManager>(ONE);
245 
246     /**
247      * @tc.steps: step2 add child to two and create a temp node with tag "temp"
248                 call GetInspector
249      * @tc.expected: the return value is empty string
250      */
251     auto id2 = ElementRegister::GetInstance()->MakeUniqueId();
252     const RefPtr<FrameNode> TWO = FrameNode::CreateFrameNode("two", id2, AceType::MakeRefPtr<Pattern>());
253     ONE->children_.clear();
254     ONE->AddChild(TWO);
255     auto stageParent = FrameNode::CreateFrameNode("stageParent", 5, AceType::MakeRefPtr<Pattern>(), true);
256     stageParent->AddChild(ONE);
257 
258     const RefPtr<FrameNode> THREE = FrameNode::CreateFrameNode(
259         "three", ElementRegister::GetInstance()->MakeUniqueId(), AceType::MakeRefPtr<Pattern>());
260     const RefPtr<FrameNode> STAGE = FrameNode::CreateFrameNode(
261         "stage", ElementRegister::GetInstance()->MakeUniqueId(), AceType::MakeRefPtr<Pattern>(), true);
262     const RefPtr<FrameNode> PAGE = FrameNode::CreateFrameNode(
263         "page", ElementRegister::GetInstance()->MakeUniqueId(), AceType::MakeRefPtr<Pattern>(), true);
264     THREE->isInternal_ = true;
265     TWO->AddChild(PAGE);
266     TWO->AddChild(THREE);
267     TWO->AddChild(STAGE);
268     auto temp = FrameNode::CreateFrameNode("temp", 5, AceType::MakeRefPtr<Pattern>(), true);
269     STAGE->AddChild(temp);
270     ONE->tag_ = "stage";
271     auto test1 = Inspector::GetInspector(false);
272     EXPECT_NE(test1, "");
273 
274     PAGE->hostPageId_ = TWO->GetPageId();
275     auto test2 = Inspector::GetInspector(false);
276     EXPECT_NE(test2, "");
277 
278     PAGE->hostPageId_ = TWO->GetPageId() + 1;
279     test2 = Inspector::GetInspector(false);
280     EXPECT_NE(test2, "");
281 
282     TWO->children_.clear();
283     auto test3 = Inspector::GetInspector(false);
284     EXPECT_NE(test3, "");
285 
286     /**
287      *  ONE(stageNode)--TWO(GetLastPage())--PAGE
288      *                   |--THREE
289      *                   |--STAGE--temp
290      *                   |--frameNode
291      *                   |--frameNode2--frameNode3
292      */
293     /**
294      * @tc.steps: step3 add child to two and create a frame node tree
295             call GetInspector
296      * @tc.expected: the return value is not empty string
297      */
298     auto spanNode = SpanNode::GetOrCreateSpanNode(int32_t(191));
299     TWO->AddChild(spanNode);
300 
301     auto frameNode = FrameNode::CreateFrameNode("frameNode", 6, AceType::MakeRefPtr<Pattern>(), true);
302     TWO->AddChild(frameNode);
303     frameNode->isActive_ = true;
304     auto frameNode2 = FrameNode::CreateFrameNode("frameNode2", 62, AceType::MakeRefPtr<Pattern>(), true);
305     TWO->AddChild(frameNode2);
306     frameNode2->isActive_ = false;
307     auto frameNode3 = FrameNode::CreateFrameNode("frameNode3", 63, AceType::MakeRefPtr<Pattern>(), true);
308     frameNode2->AddChild(frameNode3);
309     test3 = Inspector::GetInspector(false);
310     EXPECT_NE(test3, "");
311     context1->stageManager_ = nullptr;
312 }
313 
314 /**
315  * @tc.name: InspectorTestNg007
316  * @tc.desc: Test the operation of Inspector
317  * @tc.type: FUNC
318  */
319 HWTEST_F(InspectorTestNg, InspectorTestNg007, TestSize.Level1)
320 {
321     /**
322      * @tc.steps: step1. callback GetInspector
323      * @tc.expected: expect the function is run ok
324      */
325     auto id = ElementRegister::GetInstance()->MakeUniqueId();
326     RefPtr<FrameNode> ONE = FrameNode::CreateFrameNode("one", id, AceType::MakeRefPtr<Pattern>(), true);
327     auto context1 = PipelineContext::GetCurrentContext();
328     ASSERT_NE(context1, nullptr);
329     context1->stageManager_ = AceType::MakeRefPtr<StageManager>(ONE);
330 
331     /**
332      * @tc.steps: step2. callback pop_back
333      * @tc.expected: expect clean children and pageRootNode is noll
334      */
335     auto pageRootNode = context1->GetStageManager()->GetLastPage();
336     auto test = Inspector::GetInspector(false);
337     auto str = "{\"$type\":\"root\",\"width\":\"720.000000\",\"height\":\"1280.000000\",\"$resolution\":\"1.000000\"}";
338     EXPECT_EQ(test, str);
339 
340     context1->stageManager_ = nullptr;
341 }
342 
343 /**
344  * @tc.name: InspectorTestNg008
345  * @tc.desc: Test the GetRectangleById
346  * @tc.type: FUNC
347  */
348 HWTEST_F(InspectorTestNg, InspectorTestNg008, TestSize.Level1)
349 {
350     /**
351      * @tc.steps: step1. creat frameNode
352      * @tc.expected: expect the function is run ok
353      */
354     auto id = ElementRegister::GetInstance()->MakeUniqueId();
355     RefPtr<FrameNode> ONE = FrameNode::CreateFrameNode("one", id, AceType::MakeRefPtr<Pattern>(), true);
356     auto context = NG::PipelineContext::GetCurrentContext();
357     ASSERT_NE(context, nullptr);
358 
359     /**
360      * @tc.steps: step2. assign value to rootNode_
361      * @tc.expected: rootNode_ pass non-null check.
362      */
363     context->rootNode_ = ONE;
364     context->rootNode_->SetGeometryNode(AceType::MakeRefPtr<GeometryNode>());
365 
366     /**
367      * @tc.steps: step3. construct assignments and call GetRectangleById.
368      * @tc.expected: expect the GetRectangleById is run ok and result is expected.
369      */
370     OHOS::Ace::NG::Rectangle rect;
371     string key = "";
372     Inspector::GetRectangleById(key, rect);
373     float zResult = 1.0f;
374     EXPECT_EQ(rect.scale.z, zResult);
375     context->rootNode_ = nullptr;
376 }
377 } // namespace OHOS::Ace::NG
378