• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "ui/view_stack/view_stack_processor.h"
17 
18 #include "gtest/gtest.h"
19 #include "test/unittest/interfaces/ace_kit/mock/mock_ace_kit_pattern.h"
20 #include "ui/view_factory/abstract_view_factory.h"
21 #include "frameworks/core/components_ng/base/view_stack_model.h"
22 #include "frameworks/core/components_ng/base/view_stack_model_ng.h"
23 
24 using namespace testing;
25 using namespace testing::ext;
26 using namespace OHOS::Ace::Kit;
27 using namespace OHOS::Ace::NG;
28 
29 namespace OHOS::Ace {
GetInstance()30 ViewStackModel* ViewStackModel::GetInstance()
31 {
32     static NG::ViewStackModelNG instance;
33     return &instance;
34 }
35 } // namespace OHOS::Ace
36 
37 namespace OHOS::Ace::Kit {
38 class ViewStackProcessorTest : public testing::Test {};
39 
40 /**
41  * @tc.name: ViewStackProcessorTest001
42  * @tc.desc:
43  * @tc.type: FUNC
44  */
45 HWTEST_F(ViewStackProcessorTest, ViewStackProcessorTest001, TestSize.Level1)
46 {
47     const std::string tag = "TEST";
48     auto framenode = AbstractViewFactory::CreateFrameNode(tag, 0, AceType::MakeRefPtr<MockAceKitPattern>());
49     EXPECT_NE(framenode, nullptr);
50     ViewStackProcessor::Push(framenode);
51     auto topNode = ViewStackProcessor::GetTopNode();
52     EXPECT_EQ(topNode, framenode);
53 }
54 
55 /**
56  * @tc.name: ViewStackProcessorTest002
57  * @tc.desc: Test NewScope method functionality
58  * @tc.type: FUNC
59  */
60 HWTEST_F(ViewStackProcessorTest, ViewStackProcessorTest002, TestSize.Level1)
61 {
62     /**
63      * @tc.steps: step1. Create first FrameNode
64      * @tc.expected: step1. Node created successfully and not null
65      */
66     const std::string tag1 = "FIRST";
67     auto node1 = AbstractViewFactory::CreateFrameNode(tag1, 0, AceType::MakeRefPtr<MockAceKitPattern>());
68     EXPECT_NE(node1, nullptr);
69 
70     /**
71      * @tc.steps: step2. Start first scope and push node
72      * @tc.expected: step2. Finish returns correct node
73      */
74     ViewStackProcessor::NewScope();
75     ViewStackProcessor::Push(node1);
76     auto finish1 = ViewStackProcessor::Finish();
77     EXPECT_EQ(finish1, node1);
78 
79     /**
80      * @tc.steps: step3. Create second FrameNode
81      * @tc.expected: step3. Node created successfully and not null
82      */
83     const std::string tag2 = "SECOND";
84     auto node2 = AbstractViewFactory::CreateFrameNode(tag2, 0, AceType::MakeRefPtr<MockAceKitPattern>());
85     EXPECT_NE(node2, nullptr);
86 
87     /**
88      * @tc.steps: step4. Start second scope and push node
89      * @tc.expected: step4. Finish returns new node different from first
90      */
91     ViewStackProcessor::NewScope();
92     ViewStackProcessor::Push(node2);
93     auto finish2 = ViewStackProcessor::Finish();
94 
95     EXPECT_EQ(finish2, node2);
96     EXPECT_NE(finish2, finish1);
97 }
98 
99 /**
100  * @tc.name: ViewStackProcessorTest003
101  * @tc.desc: Test single node build in NewScope
102  * @tc.type: FUNC
103  */
104 HWTEST_F(ViewStackProcessorTest, ViewStackProcessorTest003, TestSize.Level1)
105 {
106     /**
107      * @tc.steps: step1. Start new scope
108      */
109     ViewStackProcessor::NewScope();
110 
111     /**
112      * @tc.steps: step2. Create and push FrameNode
113      * @tc.expected: step2. Node created successfully
114      */
115     const std::string tag = "SINGLE_NODE";
116     auto node = AbstractViewFactory::CreateFrameNode(tag, 0, AceType::MakeRefPtr<MockAceKitPattern>());
117     ViewStackProcessor::Push(node);
118 
119     /**
120      * @tc.steps: step3. Finish scope and get result
121      * @tc.expected: step3. Returned node matches pushed node
122      */
123     auto result = ViewStackProcessor::Finish();
124     EXPECT_EQ(result, node);
125 }
126 
127 /**
128  * @tc.name: ViewStackProcessorTest004
129  * @tc.desc: Test nested scopes
130  * @tc.type: FUNC
131  */
132 HWTEST_F(ViewStackProcessorTest, ViewStackProcessorTest004, TestSize.Level1)
133 {
134     /**
135      * @tc.steps: step1. Start outer scope and push outer node
136      */
137     ViewStackProcessor::NewScope();
138     const std::string outerTag = "OUTER";
139     auto outerNode = AbstractViewFactory::CreateFrameNode(outerTag, 0, AceType::MakeRefPtr<MockAceKitPattern>());
140     ViewStackProcessor::Push(outerNode);
141 
142     /**
143      * @tc.steps: step2. Create and push inner node in nested scope
144      * @tc.expected: step2. Inner scope finish returns correct inner node
145      */
146     ViewStackProcessor::NewScope();
147     const std::string innerTag = "INNER";
148     auto innerNode = AbstractViewFactory::CreateFrameNode(innerTag, 0, AceType::MakeRefPtr<MockAceKitPattern>());
149     ViewStackProcessor::Push(innerNode);
150     auto innerResult = ViewStackProcessor::Finish();
151     EXPECT_EQ(innerResult, innerNode);
152 
153     /**
154      * @tc.steps: step3. Finish outer scope
155      * @tc.expected: step3. Returns outer node different from inner node
156      */
157     auto outerResult = ViewStackProcessor::Finish();
158     EXPECT_EQ(outerResult, outerNode);
159 }
160 
161 /**
162 • @tc.name: ViewStackProcessorTest005
163 • @tc.desc: Test Finish with FrameNode that already has kitNode
164 • @tc.type: FUNC
165 */
166 HWTEST_F(ViewStackProcessorTest, ViewStackProcessorTest005, TestSize.Level1)
167 {
168     /**
169      * @tc.steps: step1. Start new scope
170      */
171     ViewStackProcessor::NewScope();
172 
173     /**
174      * @tc.steps: step2. Create FrameNode with kitNode
175      * @tc.expected: step2. Node created successfully
176      */
177     const std::string tag = "NORMAL_FRAME";
178     auto node = AbstractViewFactory::CreateFrameNode(tag, 0, AceType::MakeRefPtr<MockAceKitPattern>());
179     ASSERT_NE(node, nullptr);
180 
181     /**
182      * @tc.steps: step3. Push node and record top node
183      * @tc.expected: step3. Top node matches created node
184      */
185     ViewStackProcessor::Push(node);
186     auto preTop = ViewStackProcessor::GetTopNode();
187 
188     /**
189      * @tc.steps: step4. Finish scope
190      * @tc.expected: step4. Returned node has same ID as created node
191      */
192     auto result = ViewStackProcessor::Finish();
193     EXPECT_EQ(result, preTop);
194     EXPECT_EQ(result->GetId(), node->GetId());
195 }
196 } // namespace OHOS::Ace::Kit
197