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 "gtest/gtest.h"
16 
17 #define protected public
18 #define private public
19 
20 #include "core/components_ng/base/view_partial_update_model_ng.h"
21 
22 #include "base/log/ace_trace.h"
23 #include "core/components_ng/base/view_stack_processor.h"
24 #include "core/components_ng/pattern/custom/custom_measure_layout_node.h"
25 #include "core/components_ng/pattern/custom/custom_title_node.h"
26 
27 using namespace testing;
28 using namespace testing::ext;
29 
30 namespace OHOS::Ace::NG {
31 class ViewPartialUpdateModelTestNg : public testing::Test {
32 public:
SetUpTestCase()33     static void SetUpTestCase() {}
TearDownTestCase()34     static void TearDownTestCase() {}
35 };
36 
37 /**
38  * @tc.name: ViewPartialUpdateModelTestNg001
39  * @tc.desc: Test the operation of view partial update model ng
40  * @tc.type: FUNC
41  */
42 HWTEST_F(ViewPartialUpdateModelTestNg, ViewPartialUpdateModelTestNg001, TestSize.Level1)
43 {
44     /**
45      * @tc.steps: step1. call the CreateNode function and set input is default value.
46      * @tc.expected: step1. info is default value and return expect value.
47      */
48     ViewPartialUpdateModelNG TestViewPartialUpdateModelNG;
49     NodeInfoPU info;
50 
51     RefPtr<AceType> customNode = TestViewPartialUpdateModelNG.CreateNode(std::move(info));
52     EXPECT_FALSE(info.isStatic);
53     EXPECT_TRUE(customNode);
54 }
55 
56 /**
57  * @tc.name: ViewPartialUpdateModelTestNg002
58  * @tc.desc: Test the operation of view partial update model ng
59  * @tc.type: FUNC
60  */
61 HWTEST_F(ViewPartialUpdateModelTestNg, ViewPartialUpdateModelTestNg002, TestSize.Level1)
62 {
63     /**
64      * @tc.steps: step1. call the CreateNode function and set info.updateViewIdFunc.
65      * @tc.expected: step1. info.updateViewIdFunc is not null and return expect value.
66      */
67     std::string viewIdStr;
68     ViewPartialUpdateModelNG TestViewPartialUpdateModelNG;
69 
__anon5736251c0102(std::string viewId = "") 70     auto updateViewNodeFunction = [&viewIdStr](std::string viewId = "testViewIdStr") {
71         viewIdStr = viewId;
72     };
73 
74     NodeInfoPU info = {
75         .updateViewIdFunc = std::move(updateViewNodeFunction),
76     };
77 
78     RefPtr<AceType> customNode = TestViewPartialUpdateModelNG.CreateNode(std::move(info));
79     EXPECT_NE(info.updateViewIdFunc, nullptr);
80     EXPECT_TRUE(customNode);
81 }
82 
83 /**
84  * @tc.name: ViewPartialUpdateModelTestNg003
85  * @tc.desc: Test the operation of view partial update model ng
86  * @tc.type: FUNC
87  */
88 HWTEST_F(ViewPartialUpdateModelTestNg, ViewPartialUpdateModelTestNg003, TestSize.Level1)
89 {
90     /**
91      * @tc.steps: step1. call the CreateNode function and set info.hasMeasureOrLayout is true.
92      * @tc.expected: step1. info.hasMeasureOrLayout is true and return expect value.
93      */
94     std::string viewIdStr;
95 
96     ViewPartialUpdateModelNG TestViewPartialUpdateModelNG;
97 
__anon5736251c0202(std::string viewId = "") 98     auto updateViewNodeFunction = [&viewIdStr](std::string viewId = "testViewIdStr") {
99         viewIdStr = viewId;
100     };
101 
102     NodeInfoPU info = {
103         .updateViewIdFunc = std::move(updateViewNodeFunction),
104         .hasMeasureOrLayout = true,
105     };
106 
107     RefPtr<AceType> customNode = TestViewPartialUpdateModelNG.CreateNode(std::move(info));
108     EXPECT_TRUE(info.hasMeasureOrLayout);
109     EXPECT_TRUE(customNode);
110 }
111 
112 /**
113  * @tc.name: ViewPartialUpdateModelTestNg004
114  * @tc.desc: Test the operation of view partial update model ng
115  * @tc.type: FUNC
116  */
117 HWTEST_F(ViewPartialUpdateModelTestNg, ViewPartialUpdateModelTestNg004, TestSize.Level1)
118 {
119     /**
120      * @tc.steps: step1. call the CreateNode function and set info.measureFunc.
121      * @tc.expected: step1. info.measureFunc is not null and return expect value.
122      */
123     std::string viewIdStr;
124     NG::LayoutWrapper* testMeasureFun;
125     ViewPartialUpdateModelNG TestViewPartialUpdateModelNG;
126 
__anon5736251c0302(std::string viewId = "") 127     auto updateViewNodeFunction = [&viewIdStr](std::string viewId = "testViewIdStr") {
128         viewIdStr = viewId;
129     };
__anon5736251c0402(NG::LayoutWrapper* layoutWrapper = nullptr) 130     auto measureFuncation = [&testMeasureFun](NG::LayoutWrapper* layoutWrapper = nullptr) {
131         testMeasureFun = layoutWrapper;
132     };
133 
134     NodeInfoPU info = {
135         .updateViewIdFunc = std::move(updateViewNodeFunction),
136         .measureFunc = std::move(measureFuncation),
137         .hasMeasureOrLayout = true,
138     };
139 
140     RefPtr<AceType> customNode = TestViewPartialUpdateModelNG.CreateNode(std::move(info));
141     EXPECT_NE(info.measureFunc, nullptr);
142     EXPECT_TRUE(customNode);
143 }
144 
145 /**
146  * @tc.name: ViewPartialUpdateModelTestNg005
147  * @tc.desc: Test the operation of view partial update model ng
148  * @tc.type: FUNC
149  */
150 HWTEST_F(ViewPartialUpdateModelTestNg, ViewPartialUpdateModelTestNg005, TestSize.Level1)
151 {
152     /**
153      * @tc.steps: step1. call the CreateNode function and set info.layoutFunc.
154      * @tc.expected: step1. info.layoutFunc is not null and return expect value.
155      */
156     std::string viewIdStr;
157     NG::LayoutWrapper* testMeasureFun;
158     NG::LayoutWrapper* testLayoutFunc;
159 
160     ViewPartialUpdateModelNG TestViewPartialUpdateModelNG;
161 
__anon5736251c0502(std::string viewId = "") 162     auto updateViewNodeFunction = [&viewIdStr](std::string viewId = "testViewIdStr") {
163         viewIdStr = viewId;
164     };
__anon5736251c0602(NG::LayoutWrapper* layoutWrapper = nullptr) 165     auto measureFuncation = [&testMeasureFun](NG::LayoutWrapper* layoutWrapper = nullptr) {
166         testMeasureFun = layoutWrapper;
167     };
__anon5736251c0702(NG::LayoutWrapper* layoutWrapper = nullptr) 168     auto layoutFuncation = [&testLayoutFunc](NG::LayoutWrapper* layoutWrapper = nullptr) {
169         testLayoutFunc = layoutWrapper;
170     };
171 
172     NodeInfoPU info = {
173         .updateViewIdFunc = std::move(updateViewNodeFunction),
174         .measureFunc = std::move(measureFuncation),
175         .layoutFunc = std::move(layoutFuncation),
176         .hasMeasureOrLayout = true,
177     };
178 
179     RefPtr<AceType> customNode = TestViewPartialUpdateModelNG.CreateNode(std::move(info));
180     EXPECT_NE(info.layoutFunc, nullptr);
181     EXPECT_TRUE(customNode);
182 }
183 
184 /**
185  * @tc.name: ViewPartialUpdateModelTestNg006
186  * @tc.desc: Test the operation of view partial update model ng
187  * @tc.type: FUNC
188  */
189 HWTEST_F(ViewPartialUpdateModelTestNg, ViewPartialUpdateModelTestNg006, TestSize.Level1)
190 {
191     /**
192      * @tc.steps: step1. call the CreateNode function and set info.updateNodeFunc.
193      * @tc.expected: step1. info.updateNodeFunc is not null and return expect value.
194      */
195     std::string viewIdStr;
196     NG::LayoutWrapper* testMeasureFun;
197     NG::LayoutWrapper* testLayoutFunc;
198     WeakPtr<AceType> testUpdateNodeFunc;
199 
200     ViewPartialUpdateModelNG TestViewPartialUpdateModelNG;
201 
__anon5736251c0802(std::string viewId = "") 202     auto updateViewNodeFunction = [&viewIdStr](std::string viewId = "testViewIdStr") {
203         viewIdStr = viewId;
204     };
__anon5736251c0902(NG::LayoutWrapper* layoutWrapper = nullptr) 205     auto measureFuncation = [&testMeasureFun](NG::LayoutWrapper* layoutWrapper = nullptr) {
206         testMeasureFun = layoutWrapper;
207     };
__anon5736251c0a02(NG::LayoutWrapper* layoutWrapper = nullptr) 208     auto layoutFuncation = [&testLayoutFunc](NG::LayoutWrapper* layoutWrapper = nullptr) {
209         testLayoutFunc = layoutWrapper;
210     };
__anon5736251c0b02(const RefPtr<AceType>& type = nullptr) 211     auto updateNodeFuncation = [&testUpdateNodeFunc](const RefPtr<AceType>& type = nullptr) {
212         testUpdateNodeFunc = type;
213     };
214 
215     NodeInfoPU info = {
216         .updateNodeFunc = std::move(updateNodeFuncation),
217         .updateViewIdFunc = std::move(updateViewNodeFunction),
218         .measureFunc = std::move(measureFuncation),
219         .layoutFunc = std::move(layoutFuncation),
220         .hasMeasureOrLayout = true,
221     };
222 
223     RefPtr<AceType> customNode = TestViewPartialUpdateModelNG.CreateNode(std::move(info));
224     EXPECT_NE(info.updateNodeFunc, nullptr);
225     EXPECT_TRUE(customNode);
226 }
227 
228 /**
229  * @tc.name: ViewPartialUpdateModelTestNg007
230  * @tc.desc: Test the operation of view partial update model ng
231  * @tc.type: FUNC
232  */
233 HWTEST_F(ViewPartialUpdateModelTestNg, ViewPartialUpdateModelTestNg007, TestSize.Level1)
234 {
235     /**
236      * @tc.steps: step1. call the CreateNode function and set info.updateNodeFunc.
237      * @tc.expected: step1. info.updateNodeFunc is not null and return expect value.
238      */
239     std::string viewIdStr;
240     NG::LayoutWrapper* testMeasureFun;
241     NG::LayoutWrapper* testLayoutFunc;
242     WeakPtr<AceType> testUpdateNodeFunc;
243     WeakPtr<AceType> testRenderfunc;
244 
245     ViewPartialUpdateModelNG TestViewPartialUpdateModelNG;
246 
__anon5736251c0c02(std::string viewId = "") 247     auto updateViewNodeFunction = [&viewIdStr](std::string viewId = "testViewIdStr") {
248         viewIdStr = viewId;
249     };
__anon5736251c0d02(NG::LayoutWrapper* layoutWrapper = nullptr) 250     auto measureFuncation = [&testMeasureFun](NG::LayoutWrapper* layoutWrapper = nullptr) {
251         testMeasureFun = layoutWrapper;
252     };
__anon5736251c0e02(NG::LayoutWrapper* layoutWrapper = nullptr) 253     auto layoutFuncation = [&testLayoutFunc](NG::LayoutWrapper* layoutWrapper = nullptr) {
254         testLayoutFunc = layoutWrapper;
255     };
__anon5736251c0f02(const RefPtr<AceType>& type = nullptr) 256     auto updateNodeFuncation = [&testUpdateNodeFunc](const RefPtr<AceType>& type = nullptr) {
257         testUpdateNodeFunc = type;
258     };
__anon5736251c1002() 259     auto renderFunction = [&testRenderfunc]() -> RefPtr<AceType> {
260         return nullptr;
261     };
262 
263     NodeInfoPU info = {
264         .renderFunc = renderFunction,
265         .updateNodeFunc = std::move(updateNodeFuncation),
266         .updateViewIdFunc = std::move(updateViewNodeFunction),
267         .measureFunc = std::move(measureFuncation),
268         .layoutFunc = std::move(layoutFuncation),
269         .hasMeasureOrLayout = true,
270     };
271 
272     RefPtr<AceType> customNode = TestViewPartialUpdateModelNG.CreateNode(std::move(info));
273     EXPECT_NE(info.renderFunc, nullptr);
274     EXPECT_TRUE(customNode);
275 }
276 
277 /**
278  * @tc.name: ViewPartialUpdateModelTestNg008
279  * @tc.desc: Test the operation of view partial update model ng
280  * @tc.type: FUNC
281  */
282 HWTEST_F(ViewPartialUpdateModelTestNg, ViewPartialUpdateModelTestNg008, TestSize.Level1)
283 {
284     /**
285      * @tc.steps: step1. call the MarkNeedUpdate function first and set input is null.
286      * @tc.expected: step1. the return value is false.
287      */
288     const WeakPtr<AceType>& nullNode = nullptr;
289     ViewPartialUpdateModelNG TestViewPartialUpdateModelNG;
290 
291     bool testcustomNode =  TestViewPartialUpdateModelNG.MarkNeedUpdate(nullNode);
292     EXPECT_FALSE(testcustomNode);
293 
294     /**
295      * @tc.steps: step2. call the MarkNeedUpdate function again and set input is not null.
296      * @tc.expected: step2. the return value is true.
297      */
298     NodeInfoPU info;
299     RefPtr<AceType> customNode = TestViewPartialUpdateModelNG.CreateNode(std::move(info));
300 
301     testcustomNode = TestViewPartialUpdateModelNG.MarkNeedUpdate(customNode);
302     EXPECT_TRUE(testcustomNode);
303 }
304 
305 /**
306  * @tc.name: ViewPartialUpdateModelTestNg009
307  * @tc.desc: Test the operation of view partial update model ng
308  * @tc.type: FUNC
309  */
310 HWTEST_F(ViewPartialUpdateModelTestNg, ViewPartialUpdateModelTestNg009, TestSize.Level1)
311 {
312     /**
313      * @tc.steps: step1. call the FinishUpdate function.
314      * @tc.expected: step1. the FinishUpdate function is run ok.
315      */
316     const WeakPtr<AceType>& viewNode = nullptr;
317     ViewPartialUpdateModelNG TestViewPartialUpdateModelNG;
318 
__anon5736251c1102(const UpdateTask& task) 319     TestViewPartialUpdateModelNG.FinishUpdate(viewNode, 1, [] (const UpdateTask& task) {});
320     auto stack = NG::ViewStackProcessor::GetInstance()->elementsStack_;
321     EXPECT_TRUE(stack.empty());
322 }
323 } // namespace OHOS::Ace::NG