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 #define private public
22 #define protected public
23 #include "test/mock/core/common/mock_container.h"
24
25 #include "core/components_ng/base/view_stack_processor.h"
26 #undef private
27 #undef protected
28
29 namespace OHOS::Ace::NG {
SetUpTestSuite()30 void TestNG::SetUpTestSuite()
31 {
32 MockContainer::SetUp();
33 MockContainer::Current()->taskExecutor_ = AceType::MakeRefPtr<MockTaskExecutor>();
34 MockPipelineContext::SetUp();
35 }
36
TearDownTestSuite()37 void TestNG::TearDownTestSuite()
38 {
39 MockPipelineContext::TearDown();
40 MockContainer::TearDown();
41 MockAnimationManager::Enable(false);
42 }
43
FlushLayoutTask(const RefPtr<FrameNode> & frameNode,bool markDirty)44 RefPtr<PaintWrapper> TestNG::FlushLayoutTask(const RefPtr<FrameNode>& frameNode, bool markDirty)
45 {
46 if (markDirty) {
47 frameNode->MarkDirtyNode(PROPERTY_UPDATE_MEASURE);
48 }
49 frameNode->SetActive();
50 frameNode->isLayoutDirtyMarked_ = true;
51 frameNode->CreateLayoutTask();
52 auto paintProperty = frameNode->GetPaintProperty<PaintProperty>();
53 auto wrapper = frameNode->CreatePaintWrapper();
54 if (wrapper != nullptr) {
55 wrapper->FlushRender();
56 }
57 paintProperty->CleanDirty();
58 frameNode->SetActive(false);
59 return wrapper;
60 }
61
FlushExpandSafeAreaTask()62 void TestNG::FlushExpandSafeAreaTask()
63 {
64 auto pipeline = MockPipelineContext::GetCurrent();
65 CHECK_NULL_VOID(pipeline);
66 auto safeAreaManager = pipeline->GetSafeAreaManager();
67 CHECK_NULL_VOID(safeAreaManager);
68 safeAreaManager->ExpandSafeArea();
69 }
70
CreateDone(const RefPtr<FrameNode> & frameNode)71 RefPtr<PaintWrapper> TestNG::CreateDone(const RefPtr<FrameNode>& frameNode)
72 {
73 auto& elementsStack = ViewStackProcessor::GetInstance()->elementsStack_;
74 while (elementsStack.size() > 1) {
75 ViewStackProcessor::GetInstance()->Pop();
76 ViewStackProcessor::GetInstance()->StopGetAccessRecording();
77 }
78 RefPtr<UINode> element = ViewStackProcessor::GetInstance()->Finish();
79 auto rootNode = AceType::DynamicCast<FrameNode>(element);
80 ViewStackProcessor::GetInstance()->StopGetAccessRecording();
81 auto layoutNode = frameNode ? frameNode : rootNode;
82 layoutNode->MarkModifyDone();
83 return FlushLayoutTask(layoutNode);
84 }
85
CreateLayoutTask(const RefPtr<FrameNode> & frameNode)86 void TestNG::CreateLayoutTask(const RefPtr<FrameNode>& frameNode)
87 {
88 frameNode->SetActive();
89 frameNode->SetLayoutDirtyMarked(true);
90 frameNode->CreateLayoutTask();
91 }
92
GetActions(const RefPtr<AccessibilityProperty> & accessibilityProperty)93 uint64_t TestNG::GetActions(const RefPtr<AccessibilityProperty>& accessibilityProperty)
94 {
95 std::unordered_set<AceAction> supportAceActions = accessibilityProperty->GetSupportAction();
96 uint64_t actions = 0;
97 for (auto action : supportAceActions) {
98 actions |= 1UL << static_cast<uint32_t>(action);
99 }
100 return actions;
101 }
102
CreateTouchEventInfo(TouchType touchType,Offset location)103 TouchEventInfo TestNG::CreateTouchEventInfo(TouchType touchType, Offset location)
104 {
105 TouchLocationInfo touchLocationInfo(1);
106 touchLocationInfo.SetTouchType(touchType);
107 touchLocationInfo.SetLocalLocation(location);
108 TouchEventInfo touchEventInfo("touch");
109 touchEventInfo.AddTouchLocationInfo(std::move(touchLocationInfo));
110 return touchEventInfo;
111 }
112
CreateThemeConstants(const std::string & patternName)113 RefPtr<ThemeConstants> TestNG::CreateThemeConstants(const std::string& patternName)
114 {
115 auto resAdapter = RefPtr<ResourceAdapter>();
116 auto themeConstants = AceType::MakeRefPtr<ThemeConstants>(resAdapter);
117 std::unordered_map<std::string, ResValueWrapper> attributes;
118 ResValueWrapper resValueWrapper;
119 resValueWrapper.type = ThemeConstantsType::THEME;
120 resValueWrapper.value = AceType::MakeRefPtr<ThemeStyle>();
121 attributes.insert(std::pair<std::string, ResValueWrapper>(patternName, resValueWrapper));
122 themeConstants->currentThemeStyle_ = AceType::MakeRefPtr<ThemeStyle>();
123 themeConstants->currentThemeStyle_->SetAttributes(attributes);
124 return themeConstants;
125 }
126
CreateText(const std::string & content,const std::function<void (TextModelNG)> & callback)127 RefPtr<FrameNode> TestNG::CreateText(const std::string& content, const std::function<void(TextModelNG)>& callback)
128 {
129 TextModelNG model;
130 model.Create(content);
131 if (callback) {
132 callback(model);
133 }
134 RefPtr<UINode> element = ViewStackProcessor::GetInstance()->GetMainElementNode();
135 ViewStackProcessor::GetInstance()->PopContainer();
136 return AceType::DynamicCast<FrameNode>(element);
137 }
138
CreateRow(const std::function<void (RowModelNG)> & callback)139 RefPtr<FrameNode> TestNG::CreateRow(const std::function<void(RowModelNG)>& callback)
140 {
141 RowModelNG model;
142 model.Create(std::nullopt, nullptr, "");
143 if (callback) {
144 callback(model);
145 }
146 RefPtr<UINode> element = ViewStackProcessor::GetInstance()->GetMainElementNode();
147 ViewStackProcessor::GetInstance()->PopContainer();
148 return AceType::DynamicCast<FrameNode>(element);
149 }
150
CreateColumn(const std::function<void (ColumnModelNG)> & callback)151 RefPtr<FrameNode> TestNG::CreateColumn(const std::function<void(ColumnModelNG)>& callback)
152 {
153 ColumnModelNG model;
154 model.Create(std::nullopt, nullptr, "");
155 if (callback) {
156 callback(model);
157 }
158 RefPtr<UINode> element = ViewStackProcessor::GetInstance()->GetMainElementNode();
159 ViewStackProcessor::GetInstance()->PopContainer();
160 return AceType::DynamicCast<FrameNode>(element);
161 }
162
SetSize(Axis axis,const CalcLength & crossSize,const CalcLength & mainSize)163 void TestNG::SetSize(Axis axis, const CalcLength& crossSize, const CalcLength& mainSize)
164 {
165 if (axis == Axis::VERTICAL) {
166 ViewAbstract::SetWidth(crossSize);
167 ViewAbstract::SetHeight(mainSize);
168 } else {
169 ViewAbstract::SetWidth(mainSize);
170 ViewAbstract::SetHeight(crossSize);
171 }
172 }
173 } // namespace OHOS::Ace::NG