• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2024 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 "test_ng.h"
17 
18 #include "test/mock/base/mock_task_executor.h"
19 #include "test/mock/core/animation/mock_animation_manager.h"
20 #include "test/mock/core/pipeline/mock_pipeline_context.h"
21 
22 #include "core/components_ng/pattern/scrollable/scrollable_pattern.h"
23 #define private public
24 #define protected public
25 #include "test/mock/core/common/mock_container.h"
26 
27 #include "core/components_ng/base/view_stack_processor.h"
28 #undef private
29 #undef protected
30 
31 namespace OHOS::Ace::NG {
SetUpTestSuite()32 void TestNG::SetUpTestSuite()
33 {
34     MockContainer::SetUp();
35     MockContainer::Current()->taskExecutor_ = AceType::MakeRefPtr<MockTaskExecutor>();
36     MockContainer::Current()->SetUseNewPipeline();
37     MockPipelineContext::SetUp();
38     testing::FLAGS_gmock_verbose = "error";
39     auto rootNode = MockPipelineContext::GetCurrent()->GetRootElement();
40     auto stageNode = AceType::DynamicCast<FrameNode>(rootNode->GetChildAtIndex(0));
41     stageNode->GetLayoutProperty()->UpdateAlignment(Alignment::TOP_LEFT);
42 }
43 
TearDownTestSuite()44 void TestNG::TearDownTestSuite()
45 {
46     MockPipelineContext::TearDown();
47     MockContainer::TearDown();
48     MockAnimationManager::Enable(false);
49 }
50 
FlushUITasks()51 void TestNG::FlushUITasks()
52 {
53     auto currentNode = GetStageNode()->GetChildAtIndex(0);
54     currentNode->MarkDirtyNode(PROPERTY_UPDATE_MEASURE);
55     MockPipelineContext::GetCurrent()->FlushUITasks();
56 }
57 
FlushUITasks(const RefPtr<FrameNode> & frameNode)58 void TestNG::FlushUITasks(const RefPtr<FrameNode>& frameNode)
59 {
60     frameNode->SetActive();
61     frameNode->MarkDirtyNode(PROPERTY_UPDATE_MEASURE);
62     MockPipelineContext::GetCurrent()->FlushUITasks();
63 }
64 
FlushExpandSafeAreaTask()65 void TestNG::FlushExpandSafeAreaTask()
66 {
67     auto pipeline = MockPipelineContext::GetCurrent();
68     CHECK_NULL_VOID(pipeline);
69     auto safeAreaManager = pipeline->GetSafeAreaManager();
70     CHECK_NULL_VOID(safeAreaManager);
71     safeAreaManager->ExpandSafeArea();
72 }
73 
CreateDone()74 void TestNG::CreateDone()
75 {
76     auto& elementsStack = ViewStackProcessor::GetInstance()->elementsStack_;
77     while (elementsStack.size() > 1) {
78         ViewStackProcessor::GetInstance()->Pop();
79         ViewStackProcessor::GetInstance()->StopGetAccessRecording();
80     }
81     RefPtr<UINode> element = ViewStackProcessor::GetInstance()->Finish();
82     auto currentNode = AceType::DynamicCast<FrameNode>(element);
83     ViewStackProcessor::GetInstance()->StopGetAccessRecording();
84     // rootNode > stageNode > currentNode
85     MountToStageNode(currentNode);
86     FlushUITasks();
87 }
88 
CreateLayoutTask(const RefPtr<FrameNode> & frameNode)89 void TestNG::CreateLayoutTask(const RefPtr<FrameNode>& frameNode)
90 {
91     frameNode->SetActive();
92     frameNode->SetLayoutDirtyMarked(true);
93     frameNode->CreateLayoutTask();
94 }
95 
GetStageNode()96 RefPtr<FrameNode> TestNG::GetStageNode()
97 {
98     auto rootNode = MockPipelineContext::GetCurrent()->GetRootElement();
99     auto stageNode = rootNode->GetChildAtIndex(0);
100     return AceType::DynamicCast<FrameNode>(stageNode);
101 }
102 
MountToStageNode(const RefPtr<FrameNode> & currentNode)103 void TestNG::MountToStageNode(const RefPtr<FrameNode>& currentNode)
104 {
105     auto stageNode = GetStageNode();
106     auto oldNode = stageNode->GetChildAtIndex(0);
107     stageNode->ReplaceChild(oldNode, currentNode);
108 }
109 
RemoveFromStageNode()110 void TestNG::RemoveFromStageNode()
111 {
112     GetStageNode()->RemoveChildAtIndex(0);
113 }
114 
GetActions(const RefPtr<AccessibilityProperty> & accessibilityProperty)115 uint64_t TestNG::GetActions(const RefPtr<AccessibilityProperty>& accessibilityProperty)
116 {
117     std::unordered_set<AceAction> supportAceActions = accessibilityProperty->GetSupportAction();
118     uint64_t actions = 0;
119     for (auto action : supportAceActions) {
120         actions |= 1UL << static_cast<uint32_t>(action);
121     }
122     return actions;
123 }
124 
CreateTouchEventInfo(TouchType touchType,Offset location)125 TouchEventInfo TestNG::CreateTouchEventInfo(TouchType touchType, Offset location)
126 {
127     TouchLocationInfo touchLocationInfo(1);
128     touchLocationInfo.SetTouchType(touchType);
129     touchLocationInfo.SetLocalLocation(location);
130     TouchEventInfo touchEventInfo("touch");
131     touchEventInfo.AddTouchLocationInfo(std::move(touchLocationInfo));
132     return touchEventInfo;
133 }
134 
CreateThemeConstants(const std::string & patternName)135 RefPtr<ThemeConstants> TestNG::CreateThemeConstants(const std::string& patternName)
136 {
137     auto resAdapter = RefPtr<ResourceAdapter>();
138     auto themeConstants = AceType::MakeRefPtr<ThemeConstants>(resAdapter);
139     std::unordered_map<std::string, ResValueWrapper> attributes;
140     ResValueWrapper resValueWrapper;
141     resValueWrapper.type = ThemeConstantsType::THEME;
142     resValueWrapper.value = AceType::MakeRefPtr<ThemeStyle>();
143     attributes.insert(std::pair<std::string, ResValueWrapper>(patternName, resValueWrapper));
144     themeConstants->currentThemeStyle_ = AceType::MakeRefPtr<ThemeStyle>();
145     themeConstants->currentThemeStyle_->SetAttributes(attributes);
146     return themeConstants;
147 }
148 
CreateText(const std::u16string & content,const std::function<void (TextModelNG)> & callback)149 RefPtr<FrameNode> TestNG::CreateText(const std::u16string& content, const std::function<void(TextModelNG)>& callback)
150 {
151     TextModelNG model;
152     model.Create(content);
153     if (callback) {
154         callback(model);
155     }
156     RefPtr<UINode> element = ViewStackProcessor::GetInstance()->GetMainElementNode();
157     ViewStackProcessor::GetInstance()->PopContainer();
158     return AceType::DynamicCast<FrameNode>(element);
159 }
160 
CreateRow(const std::function<void (RowModelNG)> & callback)161 RefPtr<FrameNode> TestNG::CreateRow(const std::function<void(RowModelNG)>& callback)
162 {
163     RowModelNG model;
164     model.Create(std::nullopt, nullptr, "");
165     if (callback) {
166         callback(model);
167     }
168     RefPtr<UINode> element = ViewStackProcessor::GetInstance()->GetMainElementNode();
169     ViewStackProcessor::GetInstance()->PopContainer();
170     return AceType::DynamicCast<FrameNode>(element);
171 }
172 
CreateColumn(const std::function<void (ColumnModelNG)> & callback)173 RefPtr<FrameNode> TestNG::CreateColumn(const std::function<void(ColumnModelNG)>& callback)
174 {
175     ColumnModelNG model;
176     model.Create(std::nullopt, nullptr, "");
177     if (callback) {
178         callback(model);
179     }
180     RefPtr<UINode> element = ViewStackProcessor::GetInstance()->GetMainElementNode();
181     ViewStackProcessor::GetInstance()->PopContainer();
182     return AceType::DynamicCast<FrameNode>(element);
183 }
184 
CreateStack(const std::function<void (StackModelNG)> & callback)185 RefPtr<FrameNode> TestNG::CreateStack(const std::function<void(StackModelNG)>& callback)
186 {
187     StackModelNG model;
188     model.Create();
189     if (callback) {
190         callback(model);
191     }
192     RefPtr<UINode> element = ViewStackProcessor::GetInstance()->GetMainElementNode();
193     ViewStackProcessor::GetInstance()->PopContainer();
194     return AceType::DynamicCast<FrameNode>(element);
195 }
196 
SetSize(std::optional<Axis> axis,const CalcLength & crossSize,const CalcLength & mainSize)197 void TestNG::SetSize(std::optional<Axis> axis, const CalcLength& crossSize, const CalcLength& mainSize)
198 {
199     if (axis.has_value() && axis.value() == Axis::HORIZONTAL) {
200         ViewAbstract::SetWidth(mainSize);
201         ViewAbstract::SetHeight(crossSize);
202     } else {
203         ViewAbstract::SetWidth(crossSize);
204         ViewAbstract::SetHeight(mainSize);
205     }
206 }
207 
IsExist(const RefPtr<FrameNode> & frameNode,int32_t index)208 AssertionResult TestNG::IsExist(const RefPtr<FrameNode>& frameNode, int32_t index)
209 {
210     auto childNode = AceType::DynamicCast<FrameNode>(frameNode->GetChildByIndex(index, true));
211     if (childNode) {
212         return AssertionSuccess();
213     }
214     return AssertionFailure();
215 }
216 
IsExistAndActive(const RefPtr<FrameNode> & frameNode,int32_t index)217 AssertionResult TestNG::IsExistAndActive(const RefPtr<FrameNode>& frameNode, int32_t index)
218 {
219     auto childNode = AceType::DynamicCast<FrameNode>(frameNode->GetChildByIndex(index, true));
220     if (!childNode) {
221         return AssertionFailure();
222     }
223     if (childNode->IsActive()) {
224         return AssertionSuccess();
225     }
226     return AssertionFailure();
227 }
228 
IsExistAndInActive(const RefPtr<FrameNode> & frameNode,int32_t index)229 AssertionResult TestNG::IsExistAndInActive(const RefPtr<FrameNode>& frameNode, int32_t index)
230 {
231     auto childNode = AceType::DynamicCast<FrameNode>(frameNode->GetChildByIndex(index, true));
232     if (!childNode) {
233         return AssertionFailure();
234     }
235     if (childNode->IsActive()) {
236         return AssertionFailure();
237     }
238     return AssertionSuccess();
239 }
240 } // namespace OHOS::Ace::NG
241