1 /*
2 * Copyright (c) 2020-2021 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_test_advanced_layout.h"
17 #include "common/screen.h"
18
19 namespace OHOS {
20 namespace {
21 const int16_t TEXT_H = 20;
22 const int16_t TEXT_W = 250;
23 const int16_t TEST_VIEW_H = 40;
24 const int16_t TEST_VIEW_W = 40;
25 const int16_t GAP = 5;
26 const int16_t TEST_VIEW_GAP = 80;
27 } // namespace
28
SetUp()29 void UITestAdvancedLayout::SetUp()
30 {
31 container_ = new UIScrollView();
32 flexController_ = new GridLayout();
33 fTarget_ = new FlexLayout();
34 resetfBtn_ = new UILabelButton();
35 marginfBtn_ = new UILabelButton();
36
37 horfBtn_ = new UILabelButton();
38 horfRBtn_ = new UILabelButton();
39 verfBtn_ = new UILabelButton();
40 verfRBtn_ = new UILabelButton();
41 wrapBtn_ = new UILabelButton();
42 addElefBtn_ = new UILabelButton();
43 addTextElefBtn_ = new UILabelButton();
44 majorStartBtn_ = new UILabelButton();
45 majorEndBtn_ = new UILabelButton();
46 majorCenterBtn_ = new UILabelButton();
47 majorEvenBtn_ = new UILabelButton();
48 majorAroundBtn_ = new UILabelButton();
49 majorBetBtn_ = new UILabelButton();
50 secStartBtn_ = new UILabelButton();
51 secEndBtn_ = new UILabelButton();
52 secCenterBtn_ = new UILabelButton();
53 secInvalidBtn_ = new UILabelButton();
54 layoutChildrenfBtn_ = new UILabelButton();
55
56 gridController_ = new GridLayout();
57 gTarget_ = new GridLayout();
58 horgBtn_ = new UILabelButton();
59 horgRBtn_ = new UILabelButton();
60 vergBtn_ = new UILabelButton();
61 vergRBtn_ = new UILabelButton();
62 incRowsBtn_ = new UILabelButton();
63 decRowsBtn_ = new UILabelButton();
64 incColsBtn_ = new UILabelButton();
65 decColsBtn_ = new UILabelButton();
66 addElegBtn_ = new UILabelButton();
67 resetgBtn_ = new UILabelButton();
68 margingBtn_ = new UILabelButton();
69 layoutChildrengBtn_ = new UILabelButton();
70 container_->Resize(Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight() - BACK_BUTTON_HEIGHT);
71 positionX_ = 20; // 20: position x
72 positionY_ = 5; // 5: position y
73 }
74
TearDown()75 void UITestAdvancedLayout::TearDown()
76 {
77 DeleteChildren(container_);
78 container_ = nullptr;
79 }
80
GetTestView()81 const UIView* UITestAdvancedLayout::GetTestView()
82 {
83 UIKitLayoutTestFlexLayout001();
84 UIKitLayoutTestGridLayout001();
85 return container_;
86 }
87
UIKitLayoutTestFlexLayout001()88 void UITestAdvancedLayout::UIKitLayoutTestFlexLayout001()
89 {
90 if (container_ != nullptr) {
91 UIViewGroup* uiViewGroup = new UIViewGroup();
92 uiViewGroup->SetPosition(0, 0, Screen::GetInstance().GetWidth(), 420); // 420: height
93 container_->Add(uiViewGroup);
94 UILabel* label = new UILabel();
95 uiViewGroup->Add(label);
96 // 288: width; 48: height
97 label->SetPosition(TEXT_DISTANCE_TO_LEFT_SIDE, TEXT_DISTANCE_TO_TOP_SIDE, 288, 48);
98 label->SetText("Flex布局效果");
99 label->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
100 UIViewGroup* uiViewGroupFrame = new UIViewGroup();
101 uiViewGroup->Add(uiViewGroupFrame);
102 uiViewGroupFrame->SetPosition(VIEW_DISTANCE_TO_LEFT_SIDE2, VIEW_DISTANCE_TO_TOP_SIDE,
103 Screen::GetInstance().GetWidth() - VIEW_DISTANCE_TO_LEFT_SIDE,
104 100); // 100: height
105 uiViewGroupFrame->SetStyle(STYLE_BORDER_COLOR, Color::White().full);
106 uiViewGroupFrame->SetStyle(STYLE_BORDER_OPA, HALF_OPA_OPAQUE);
107 uiViewGroupFrame->SetStyle(STYLE_BORDER_WIDTH, VIEW_STYLE_BORDER_WIDTH);
108 uiViewGroupFrame->SetStyle(STYLE_BORDER_RADIUS, VIEW_STYLE_BORDER_RADIUS);
109 uiViewGroupFrame->SetStyle(STYLE_BACKGROUND_OPA, 0);
110 uiViewGroupFrame->Add(fTarget_);
111
112 fTarget_->SetPosition(0, 0, Screen::GetInstance().GetWidth() - VIEW_DISTANCE_TO_LEFT_SIDE, 100); // 100: height
113 fTarget_->SetStyle(STYLE_BACKGROUND_OPA, 100); // 100: opacity
114
115 positionY_ += 160; // 160: position y add
116 uiViewGroup->Add(flexController_);
117 flexController_->SetPosition(0, positionY_, Screen::GetInstance().GetWidth(), 250); // 250: height
118 flexController_->SetLayoutDirection(LAYOUT_HOR);
119 flexController_->SetRows(5); // 5: row
120 flexController_->SetCols(4); // 4: col
121
122 SetUpButton(flexController_, resetfBtn_, "reset");
123 SetUpButton(flexController_, marginfBtn_, "margin");
124 SetUpButton(flexController_, horfBtn_, "hor");
125 SetUpButton(flexController_, horfRBtn_, "hor_r");
126 SetUpButton(flexController_, verfBtn_, "ver");
127 SetUpButton(flexController_, verfRBtn_, "ver_r");
128 SetUpButton(flexController_, wrapBtn_, "wrap");
129 SetUpButton(flexController_, addElefBtn_, "add");
130 SetUpButton(flexController_, addTextElefBtn_, "addLabel");
131 SetUpButton(flexController_, majorStartBtn_, "M_start");
132 SetUpButton(flexController_, majorEndBtn_, "M_end");
133 SetUpButton(flexController_, majorCenterBtn_, "M_center");
134 SetUpButton(flexController_, majorEvenBtn_, "M_even");
135 SetUpButton(flexController_, majorAroundBtn_, "M_around");
136 SetUpButton(flexController_, majorBetBtn_, "M_between");
137 SetUpButton(flexController_, secStartBtn_, "S_start");
138 SetUpButton(flexController_, secEndBtn_, "S_end");
139 SetUpButton(flexController_, secCenterBtn_, "S_center");
140 SetUpButton(flexController_, secInvalidBtn_, "S_invalid");
141 SetUpButton(flexController_, layoutChildrenfBtn_, "布局");
142 flexController_->LayoutChildren();
143 positionY_ += 250; // 250: position y add
144 }
145 }
146
UIKitLayoutTestGridLayout001()147 void UITestAdvancedLayout::UIKitLayoutTestGridLayout001()
148 {
149 if (container_ != nullptr) {
150 UIViewGroup* uiViewGroup = new UIViewGroup();
151 uiViewGroup->SetPosition(0, positionY_, Screen::GetInstance().GetWidth(), 370); // 370: height
152 container_->Add(uiViewGroup);
153 UILabel* label = new UILabel();
154 uiViewGroup->Add(label);
155 // 288: widht; 48: height
156 label->SetPosition(TEXT_DISTANCE_TO_LEFT_SIDE, TEXT_DISTANCE_TO_TOP_SIDE, 288, 48);
157 label->SetText("Grid布局测试");
158 label->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
159 UIViewGroup* uiViewGroupFrame = new UIViewGroup();
160 uiViewGroup->Add(uiViewGroupFrame);
161 uiViewGroupFrame->SetPosition(VIEW_DISTANCE_TO_LEFT_SIDE2, VIEW_DISTANCE_TO_TOP_SIDE,
162 Screen::GetInstance().GetWidth() - VIEW_DISTANCE_TO_LEFT_SIDE,
163 100); // 100: height
164 uiViewGroupFrame->SetStyle(STYLE_BORDER_COLOR, Color::White().full);
165 uiViewGroupFrame->SetStyle(STYLE_BORDER_OPA, HALF_OPA_OPAQUE);
166 uiViewGroupFrame->SetStyle(STYLE_BORDER_WIDTH, VIEW_STYLE_BORDER_WIDTH);
167 uiViewGroupFrame->SetStyle(STYLE_BORDER_RADIUS, VIEW_STYLE_BORDER_RADIUS);
168 uiViewGroupFrame->SetStyle(STYLE_BACKGROUND_OPA, 0);
169 uiViewGroupFrame->Add(gTarget_);
170
171 gTarget_->SetPosition(0, 0, Screen::GetInstance().GetWidth() - VIEW_DISTANCE_TO_LEFT_SIDE, 100); // 100: height
172 uiViewGroup->Add(gridController_);
173 gridController_->SetPosition(0, 160, Screen::GetInstance().GetWidth(), 150); // 160: y-coordinate; 150: height
174 gridController_->SetLayoutDirection(LAYOUT_HOR);
175 gridController_->SetRows(3); // 3: row
176 gridController_->SetCols(4); // 4: col
177
178 SetUpButton(gridController_, resetgBtn_, "reset");
179 SetUpButton(gridController_, margingBtn_, "matgin");
180 SetUpButton(gridController_, horgBtn_, "hor");
181 SetUpButton(gridController_, horgRBtn_, "hor_r");
182 SetUpButton(gridController_, vergBtn_, "ver");
183 SetUpButton(gridController_, vergRBtn_, "ver_r");
184 SetUpButton(gridController_, addElegBtn_, "add");
185 SetUpButton(gridController_, incRowsBtn_, "incrows");
186 SetUpButton(gridController_, decRowsBtn_, "decrows");
187 SetUpButton(gridController_, incColsBtn_, "inccols");
188 SetUpButton(gridController_, decColsBtn_, "deccols");
189 SetUpButton(gridController_, layoutChildrengBtn_, "布局");
190 gridController_->LayoutChildren();
191 }
192 }
193
OnClickButton(const UIView & view)194 void UITestAdvancedLayout::OnClickButton(const UIView& view)
195 {
196 if (&view == resetgBtn_) {
197 Clear(gTarget_);
198 gTarget_->SetLayoutDirection(LAYOUT_HOR);
199 rows_ = 0;
200 cols_ = 0;
201 gTarget_->SetRows(rows_);
202 gTarget_->SetCols(cols_);
203 withMargin_ = false;
204 } else if (&view == margingBtn_) {
205 withMargin_ = true;
206 } else if (&view == horgBtn_) {
207 gTarget_->SetLayoutDirection(LAYOUT_HOR);
208 } else if (&view == horgRBtn_) {
209 gTarget_->SetLayoutDirection(LAYOUT_HOR_R);
210 } else if (&view == vergBtn_) {
211 gTarget_->SetLayoutDirection(LAYOUT_VER);
212 } else if (&view == vergRBtn_) {
213 gTarget_->SetLayoutDirection(LAYOUT_VER_R);
214 } else if (&view == addElegBtn_) {
215 AddElement(gTarget_);
216 } else if (&view == incRowsBtn_) {
217 rows_++;
218 gTarget_->SetRows(rows_);
219 } else if (&view == decRowsBtn_) {
220 rows_--;
221 gTarget_->SetRows(rows_);
222 } else if (&view == incColsBtn_) {
223 cols_++;
224 gTarget_->SetCols(cols_);
225 } else if (&view == decColsBtn_) {
226 cols_--;
227 gTarget_->SetCols(cols_);
228 } else if (&view == layoutChildrenfBtn_) {
229 fTarget_->LayoutChildren();
230 fTarget_->Invalidate();
231 } else if (&view == layoutChildrengBtn_) {
232 gTarget_->LayoutChildren();
233 gTarget_->Invalidate();
234 } else {
235 }
236 }
237
OnClick(UIView & view,const ClickEvent & event)238 bool UITestAdvancedLayout::OnClick(UIView& view, const ClickEvent& event)
239 {
240 fTarget_->Invalidate();
241 gTarget_->Invalidate();
242 if (&view == resetfBtn_) {
243 Clear(fTarget_);
244 fTarget_->SetFlexWrap(false);
245 fTarget_->SetLayoutDirection(LAYOUT_HOR);
246 fTarget_->SetMajorAxisAlign(ALIGN_START);
247 fTarget_->SetSecondaryAxisAlign(ALIGN_CENTER);
248 withMargin_ = false;
249 } else if (&view == marginfBtn_) {
250 withMargin_ = true;
251 } else if (&view == horfBtn_) {
252 fTarget_->SetLayoutDirection(LAYOUT_HOR);
253 } else if (&view == horfRBtn_) {
254 fTarget_->SetLayoutDirection(LAYOUT_HOR_R);
255 } else if (&view == verfBtn_) {
256 fTarget_->SetLayoutDirection(LAYOUT_VER);
257 } else if (&view == verfRBtn_) {
258 fTarget_->SetLayoutDirection(LAYOUT_VER_R);
259 } else if (&view == wrapBtn_) {
260 fTarget_->SetFlexWrap(true);
261 } else if (&view == addElefBtn_) {
262 AddElement(fTarget_);
263 } else if (&view == addTextElefBtn_) {
264 AddTextElement(fTarget_);
265 } else if (&view == majorStartBtn_) {
266 fTarget_->SetMajorAxisAlign(ALIGN_START);
267 } else if (&view == majorEndBtn_) {
268 fTarget_->SetMajorAxisAlign(ALIGN_END);
269 } else if (&view == majorCenterBtn_) {
270 fTarget_->SetMajorAxisAlign(ALIGN_CENTER);
271 } else if (&view == majorEvenBtn_) {
272 fTarget_->SetMajorAxisAlign(ALIGN_EVENLY);
273 } else if (&view == majorAroundBtn_) {
274 fTarget_->SetMajorAxisAlign(ALIGN_AROUND);
275 } else if (&view == majorBetBtn_) {
276 fTarget_->SetMajorAxisAlign(ALIGN_BETWEEN);
277 } else if (&view == secStartBtn_) {
278 fTarget_->SetSecondaryAxisAlign(ALIGN_START);
279 } else if (&view == secEndBtn_) {
280 fTarget_->SetSecondaryAxisAlign(ALIGN_END);
281 } else if (&view == secCenterBtn_) {
282 fTarget_->SetSecondaryAxisAlign(ALIGN_CENTER);
283 } else {
284 OnClickButton(view);
285 }
286 return true;
287 }
288 } // namespace OHOS
289