1 /*
2 * Copyright (c) 2022-2023 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 "grid_test_ng.h"
17
18 #include "test/mock/base/mock_drag_window.h"
19 #include "test/mock/core/common/mock_container.h"
20 #include "test/mock/core/common/mock_theme_manager.h"
21 #include "test/mock/core/pipeline/mock_pipeline_context.h"
22 #include "test/mock/core/animation/mock_animation_manager.h"
23
24 #include "core/components/button/button_theme.h"
25 #include "core/components_ng/pattern/grid/grid_item_pattern.h"
26 #include "core/components_ng/syntax/repeat_virtual_scroll_model_ng.h"
27 #include "core/components_ng/pattern/button/button_model_ng.h"
28
29 #ifndef TEST_IRREGULAR_GRID
30 #include "test/mock/base/mock_system_properties.h"
31 #endif
32 namespace OHOS::Ace::NG {
SetUpTestSuite()33 void GridTestNg::SetUpTestSuite()
34 {
35 TestNG::SetUpTestSuite();
36 auto themeManager = AceType::MakeRefPtr<MockThemeManager>();
37 MockPipelineContext::GetCurrent()->SetThemeManager(themeManager);
38 auto buttonTheme = AceType::MakeRefPtr<ButtonTheme>();
39 EXPECT_CALL(*themeManager, GetTheme(_)).WillRepeatedly(Return(buttonTheme));
40 auto themeConstants = CreateThemeConstants(THEME_PATTERN_GRID);
41 auto gridItemTheme = GridItemTheme::Builder().Build(themeConstants);
42 EXPECT_CALL(*themeManager, GetTheme(GridItemTheme::TypeId())).WillRepeatedly(Return(gridItemTheme));
43 RefPtr<DragWindow> dragWindow = DragWindow::CreateDragWindow({"", 0, 0, 0, 0, 0});
44 EXPECT_CALL(*(AceType::DynamicCast<MockDragWindow>(dragWindow)), DrawFrameNode(_)).Times(AnyNumber());
45 EXPECT_CALL(*(AceType::DynamicCast<MockDragWindow>(dragWindow)), MoveTo(_, _)).Times(AnyNumber());
46 EXPECT_CALL(*(AceType::DynamicCast<MockDragWindow>(dragWindow)), Destroy()).Times(AnyNumber());
47 EXPECT_CALL(*MockPipelineContext::pipeline_, FlushUITasks).Times(AnyNumber());
48 auto container = Container::GetContainer(CONTAINER_ID_DIVIDE_SIZE);
49 EXPECT_CALL(*(AceType::DynamicCast<MockContainer>(container)), GetWindowId()).Times(AnyNumber());
50 MockAnimationManager::Enable(true);
51 #ifndef TEST_IRREGULAR_GRID
52 g_irregularGrid = false;
53 #endif
54 }
55
CheckPreloadListEqual(const std::list<int32_t> & expectedList) const56 void GridTestNg::CheckPreloadListEqual(const std::list<int32_t>& expectedList) const
57 {
58 ASSERT_EQ(expectedList.size(), pattern_->preloadItemList_.size());
59 auto it = expectedList.begin();
60 for (auto&& item : pattern_->preloadItemList_) {
61 EXPECT_EQ(*it, item.idx);
62 ++it;
63 }
64 }
65
TearDownTestSuite()66 void GridTestNg::TearDownTestSuite()
67 {
68 TestNG::TearDownTestSuite();
69 }
70
SetUp()71 void GridTestNg::SetUp()
72 {
73 MockAnimationManager::GetInstance().Reset();
74 }
75
TearDown()76 void GridTestNg::TearDown()
77 {
78 frameNode_ = nullptr;
79 pattern_ = nullptr;
80 eventHub_ = nullptr;
81 layoutProperty_ = nullptr;
82 accessibilityProperty_ = nullptr;
83 ClearOldNodes(); // Each testcase will create new list at begin
84 }
85
GetGrid()86 void GridTestNg::GetGrid()
87 {
88 RefPtr<UINode> element = ViewStackProcessor::GetInstance()->GetMainElementNode();
89 frameNode_ = AceType::DynamicCast<FrameNode>(element);
90 pattern_ = frameNode_->GetPattern<GridPattern>();
91 eventHub_ = frameNode_->GetEventHub<GridEventHub>();
92 layoutProperty_ = frameNode_->GetLayoutProperty<GridLayoutProperty>();
93 accessibilityProperty_ = frameNode_->GetAccessibilityProperty<GridAccessibilityProperty>();
94 }
95
CreateGrid()96 GridModelNG GridTestNg::CreateGrid()
97 {
98 ResetElmtId();
99 ViewStackProcessor::GetInstance()->StartGetAccessRecordingFor(GetElmtId());
100 GridModelNG model;
101 RefPtr<ScrollControllerBase> positionController = model.CreatePositionController();
102 RefPtr<ScrollProxy> scrollBarProxy = model.CreateScrollBarProxy();
103 model.Create(positionController, scrollBarProxy);
104 ViewAbstract::SetWidth(CalcLength(GRID_WIDTH));
105 ViewAbstract::SetHeight(CalcLength(GRID_HEIGHT));
106 GetGrid();
107 return model;
108 }
109
CreateGridItem(float width,float height,GridItemStyle gridItemStyle)110 GridItemModelNG GridTestNg::CreateGridItem(float width, float height, GridItemStyle gridItemStyle)
111 {
112 ViewStackProcessor::GetInstance()->StartGetAccessRecordingFor(GetElmtId());
113 GridItemModelNG itemModel;
114 itemModel.Create(gridItemStyle);
115 if (width == FILL_VALUE) {
116 ViewAbstract::SetWidth(CalcLength(FILL_LENGTH));
117 } else if (width != NULL_VALUE) {
118 ViewAbstract::SetWidth(CalcLength(width));
119 }
120 if (height == FILL_VALUE) {
121 ViewAbstract::SetHeight(CalcLength(FILL_LENGTH));
122 } else if (height != NULL_VALUE) {
123 ViewAbstract::SetHeight(CalcLength(height));
124 }
125 return itemModel;
126 }
127
CreateGridItems(int32_t itemNumber,float width,float height,GridItemStyle gridItemStyle)128 void GridTestNg::CreateGridItems(int32_t itemNumber, float width, float height, GridItemStyle gridItemStyle)
129 {
130 for (int32_t i = 0; i < itemNumber; i++) {
131 CreateGridItem(width, height, gridItemStyle);
132 ViewStackProcessor::GetInstance()->Pop();
133 ViewStackProcessor::GetInstance()->StopGetAccessRecording();
134 }
135 }
136
CreateFocusableGridItems(int32_t itemNumber,float width,float height,GridItemStyle gridItemStyle)137 void GridTestNg::CreateFocusableGridItems(int32_t itemNumber, float width, float height, GridItemStyle gridItemStyle)
138 {
139 for (int32_t i = 0; i < itemNumber; i++) {
140 CreateGridItem(width, height, gridItemStyle);
141 {
142 ButtonModelNG buttonModelNG;
143 buttonModelNG.CreateWithLabel("label");
144 ViewStackProcessor::GetInstance()->GetMainElementNode()->onMainTree_ = true;
145 ViewStackProcessor::GetInstance()->Pop();
146 }
147 ViewStackProcessor::GetInstance()->GetMainElementNode()->onMainTree_ = true;
148 ViewStackProcessor::GetInstance()->Pop();
149 ViewStackProcessor::GetInstance()->StopGetAccessRecording();
150 }
151 }
152
CreateFixedItems(int32_t itemNumber,GridItemStyle gridItemStyle)153 void GridTestNg::CreateFixedItems(int32_t itemNumber, GridItemStyle gridItemStyle)
154 {
155 CreateGridItems(itemNumber, ITEM_WIDTH, ITEM_HEIGHT, gridItemStyle);
156 }
157
CreateFixedHeightItems(int32_t itemNumber,float height,GridItemStyle gridItemStyle)158 void GridTestNg::CreateFixedHeightItems(int32_t itemNumber, float height, GridItemStyle gridItemStyle)
159 {
160 CreateGridItems(itemNumber, FILL_VALUE, height, gridItemStyle);
161 }
162
CreateFixedWidthItems(int32_t itemNumber,float width,GridItemStyle gridItemStyle)163 void GridTestNg::CreateFixedWidthItems(int32_t itemNumber, float width, GridItemStyle gridItemStyle)
164 {
165 CreateGridItems(itemNumber, width, FILL_VALUE, gridItemStyle);
166 }
167
CreateBigItem(int32_t rowStart,int32_t rowEnd,int32_t colStart,int32_t colEnd,float width,float height)168 void GridTestNg::CreateBigItem(
169 int32_t rowStart, int32_t rowEnd, int32_t colStart, int32_t colEnd, float width, float height)
170 {
171 ViewStackProcessor::GetInstance()->StartGetAccessRecordingFor(GetElmtId());
172 GridItemModelNG itemModel;
173 itemModel.Create(GridItemStyle::NONE);
174 if (rowStart != NULL_VALUE) {
175 itemModel.SetRowStart(rowStart);
176 }
177 if (rowEnd != NULL_VALUE) {
178 itemModel.SetRowEnd(rowEnd);
179 }
180 if (colStart != NULL_VALUE) {
181 itemModel.SetColumnStart(colStart);
182 }
183 if (colEnd != NULL_VALUE) {
184 itemModel.SetColumnEnd(colEnd);
185 }
186 if (width != NULL_VALUE) {
187 ViewAbstract::SetWidth(CalcLength(width));
188 }
189 if (height != NULL_VALUE) {
190 ViewAbstract::SetHeight(CalcLength(height));
191 }
192 ViewStackProcessor::GetInstance()->Pop();
193 ViewStackProcessor::GetInstance()->StopGetAccessRecording();
194 }
195
CreateBigColItem(int32_t colStart,int32_t colEnd)196 void GridTestNg::CreateBigColItem(int32_t colStart, int32_t colEnd)
197 {
198 CreateBigItem(NULL_VALUE, NULL_VALUE, colStart, colEnd, NULL_VALUE, ITEM_HEIGHT);
199 }
200
CreateBigRowItem(int32_t rowStart,int32_t rowEnd)201 void GridTestNg::CreateBigRowItem(int32_t rowStart, int32_t rowEnd)
202 {
203 CreateBigItem(rowStart, rowEnd, NULL_VALUE, NULL_VALUE, ITEM_WIDTH, NULL_VALUE);
204 }
205
AddFixedHeightItems(int32_t cnt,float height)206 void GridTestNg::AddFixedHeightItems(int32_t cnt, float height)
207 {
208 for (int i = 0; i < cnt; ++i) {
209 auto child = FrameNode::GetOrCreateFrameNode(
210 V2::GRID_ITEM_ETS_TAG, -1, []() { return AceType::MakeRefPtr<GridItemPattern>(nullptr); });
211 child->GetLayoutProperty()->UpdateUserDefinedIdealSize(
212 CalcSize(CalcLength(FILL_LENGTH), CalcLength(Dimension(height))));
213 frameNode_->AddChild(child);
214 }
215 }
216
ScrollTo(float position)217 void GridTestNg::ScrollTo(float position)
218 {
219 pattern_->ScrollTo(position);
220 FlushLayoutTask(frameNode_);
221 }
222
UpdateCurrentOffset(float offset,int32_t source)223 void GridTestNg::UpdateCurrentOffset(float offset, int32_t source)
224 {
225 pattern_->UpdateCurrentOffset(offset, source);
226 FlushLayoutTask(frameNode_);
227 }
228
CreateRepeatGrid(int32_t itemNumber,std::function<float (uint32_t)> && getSize)229 GridModelNG GridTestNg::CreateRepeatGrid(int32_t itemNumber, std::function<float(uint32_t)>&& getSize)
230 {
231 auto model = CreateGrid();
232
233 RepeatVirtualScrollModelNG repeatModel;
234 std::function<void(uint32_t)> createFunc = [this, getSize](
235 uint32_t idx) { CreateGridItem(FILL_VALUE, getSize(idx)); };
236 std::function<void(const std::string&, uint32_t)> updateFunc =
237 [this, getSize](const std::string& value, uint32_t idx) { CreateGridItem(FILL_VALUE, getSize(idx)); };
238 std::function<std::list<std::string>(uint32_t, uint32_t)> getKeys = [](uint32_t start, uint32_t end) {
239 std::list<std::string> keys;
240 for (uint32_t i = start; i <= end; ++i) {
241 keys.emplace_back(std::to_string(i));
242 }
243 return keys;
244 };
245 std::function<std::list<std::string>(uint32_t, uint32_t)> getTypes = [](uint32_t start, uint32_t end) {
246 std::list<std::string> keys;
247 for (uint32_t i = start; i <= end; ++i) {
248 keys.emplace_back("0");
249 }
250 return keys;
251 };
252 std::function<void(uint32_t, uint32_t)> setActiveRange = [](uint32_t start, uint32_t end) {};
253 repeatModel.Create(itemNumber, {}, createFunc, updateFunc, getKeys, getTypes, setActiveRange);
254 return model;
255 }
256
CreateAdaptChildSizeGridItems(int32_t itemNumber,GridItemStyle gridItemStyle)257 void GridTestNg::CreateAdaptChildSizeGridItems(int32_t itemNumber, GridItemStyle gridItemStyle)
258 {
259 for (int32_t i = 0; i < itemNumber; i++) {
260 ViewStackProcessor::GetInstance()->StartGetAccessRecordingFor(GetElmtId());
261 GridItemModelNG itemModel;
262 itemModel.Create(gridItemStyle);
263 {
264 auto columnFrameNode = FrameNode::CreateFrameNode(
265 V2::COLUMN_ETS_TAG, GetElmtId(), AceType::MakeRefPtr<LinearLayoutPattern>(true));
266 ViewStackProcessor::GetInstance()->Pop();
267 }
268 ViewStackProcessor::GetInstance()->Pop();
269 ViewStackProcessor::GetInstance()->StopGetAccessRecording();
270 }
271 }
272 } // namespace OHOS::Ace::NG
273