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         SetTestButtons001();
123         flexController_->LayoutChildren();
124         positionY_ += 250; // 250: position y add
125     }
126 }
127 
SetTestButtons001()128 void UITestAdvancedLayout::SetTestButtons001()
129 {
130     SetUpButton(flexController_, resetfBtn_, "reset");
131     SetUpButton(flexController_, marginfBtn_, "margin");
132     SetUpButton(flexController_, horfBtn_, "hor");
133     SetUpButton(flexController_, horfRBtn_, "hor_r");
134     SetUpButton(flexController_, verfBtn_, "ver");
135     SetUpButton(flexController_, verfRBtn_, "ver_r");
136     SetUpButton(flexController_, wrapBtn_, "wrap");
137     SetUpButton(flexController_, addElefBtn_, "add");
138     SetUpButton(flexController_, addTextElefBtn_, "addLabel");
139     SetUpButton(flexController_, majorStartBtn_, "M_start");
140     SetUpButton(flexController_, majorEndBtn_, "M_end");
141     SetUpButton(flexController_, majorCenterBtn_, "M_center");
142     SetUpButton(flexController_, majorEvenBtn_, "M_even");
143     SetUpButton(flexController_, majorAroundBtn_, "M_around");
144     SetUpButton(flexController_, majorBetBtn_, "M_between");
145     SetUpButton(flexController_, secStartBtn_, "S_start");
146     SetUpButton(flexController_, secEndBtn_, "S_end");
147     SetUpButton(flexController_, secCenterBtn_, "S_center");
148     SetUpButton(flexController_, secInvalidBtn_, "S_invalid");
149     SetUpButton(flexController_, layoutChildrenfBtn_, "布局");
150 }
151 
UIKitLayoutTestGridLayout001()152 void UITestAdvancedLayout::UIKitLayoutTestGridLayout001()
153 {
154     if (container_ != nullptr) {
155         UIViewGroup* uiViewGroup = new UIViewGroup();
156         uiViewGroup->SetPosition(0, positionY_, Screen::GetInstance().GetWidth(), 370); // 370: height
157         container_->Add(uiViewGroup);
158         UILabel* label = new UILabel();
159         uiViewGroup->Add(label);
160         // 288: widht; 48: height
161         label->SetPosition(TEXT_DISTANCE_TO_LEFT_SIDE, TEXT_DISTANCE_TO_TOP_SIDE, 288, 48);
162         label->SetText("Grid布局测试");
163         label->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
164         UIViewGroup* uiViewGroupFrame = new UIViewGroup();
165         uiViewGroup->Add(uiViewGroupFrame);
166         uiViewGroupFrame->SetPosition(VIEW_DISTANCE_TO_LEFT_SIDE2, VIEW_DISTANCE_TO_TOP_SIDE,
167                                       Screen::GetInstance().GetWidth() - VIEW_DISTANCE_TO_LEFT_SIDE,
168                                       100); // 100: height
169         uiViewGroupFrame->SetStyle(STYLE_BORDER_COLOR, Color::White().full);
170         uiViewGroupFrame->SetStyle(STYLE_BORDER_OPA, HALF_OPA_OPAQUE);
171         uiViewGroupFrame->SetStyle(STYLE_BORDER_WIDTH, VIEW_STYLE_BORDER_WIDTH);
172         uiViewGroupFrame->SetStyle(STYLE_BORDER_RADIUS, VIEW_STYLE_BORDER_RADIUS);
173         uiViewGroupFrame->SetStyle(STYLE_BACKGROUND_OPA, 0);
174         uiViewGroupFrame->Add(gTarget_);
175 
176         gTarget_->SetPosition(0, 0, Screen::GetInstance().GetWidth() - VIEW_DISTANCE_TO_LEFT_SIDE, 100); // 100: height
177         uiViewGroup->Add(gridController_);
178         gridController_->SetPosition(0, 160, Screen::GetInstance().GetWidth(), 150); // 160: y-coordinate; 150: height
179         gridController_->SetLayoutDirection(LAYOUT_HOR);
180         gridController_->SetRows(3); // 3: row
181         gridController_->SetCols(4); // 4: col
182 
183         SetUpButton(gridController_, resetgBtn_, "reset");
184         SetUpButton(gridController_, margingBtn_, "matgin");
185         SetUpButton(gridController_, horgBtn_, "hor");
186         SetUpButton(gridController_, horgRBtn_, "hor_r");
187         SetUpButton(gridController_, vergBtn_, "ver");
188         SetUpButton(gridController_, vergRBtn_, "ver_r");
189         SetUpButton(gridController_, addElegBtn_, "add");
190         SetUpButton(gridController_, incRowsBtn_, "incrows");
191         SetUpButton(gridController_, decRowsBtn_, "decrows");
192         SetUpButton(gridController_, incColsBtn_, "inccols");
193         SetUpButton(gridController_, decColsBtn_, "deccols");
194         SetUpButton(gridController_, layoutChildrengBtn_, "布局");
195         gridController_->LayoutChildren();
196     }
197 }
198 
OnClickButton(const UIView & view)199 void UITestAdvancedLayout::OnClickButton(const UIView& view)
200 {
201     if (&view == resetgBtn_) {
202         Clear(gTarget_);
203         gTarget_->SetLayoutDirection(LAYOUT_HOR);
204         rows_ = 0;
205         cols_ = 0;
206         gTarget_->SetRows(rows_);
207         gTarget_->SetCols(cols_);
208         withMargin_ = false;
209     } else if (&view == margingBtn_) {
210         withMargin_ = true;
211     } else if (&view == horgBtn_) {
212         gTarget_->SetLayoutDirection(LAYOUT_HOR);
213     } else if (&view == horgRBtn_) {
214         gTarget_->SetLayoutDirection(LAYOUT_HOR_R);
215     } else if (&view == vergBtn_) {
216         gTarget_->SetLayoutDirection(LAYOUT_VER);
217     } else if (&view == vergRBtn_) {
218         gTarget_->SetLayoutDirection(LAYOUT_VER_R);
219     } else if (&view == addElegBtn_) {
220         AddElement(gTarget_);
221     } else if (&view == incRowsBtn_) {
222         rows_++;
223         gTarget_->SetRows(rows_);
224     } else if (&view == decRowsBtn_) {
225         rows_--;
226         gTarget_->SetRows(rows_);
227     } else if (&view == incColsBtn_) {
228         cols_++;
229         gTarget_->SetCols(cols_);
230     } else if (&view == decColsBtn_) {
231         cols_--;
232         gTarget_->SetCols(cols_);
233     } else if (&view == layoutChildrenfBtn_) {
234         fTarget_->LayoutChildren();
235         fTarget_->Invalidate();
236     } else if (&view == layoutChildrengBtn_) {
237         gTarget_->LayoutChildren();
238         gTarget_->Invalidate();
239     } else {
240     }
241 }
242 
OnClick(UIView & view,const ClickEvent & event)243 bool UITestAdvancedLayout::OnClick(UIView& view, const ClickEvent& event)
244 {
245     fTarget_->Invalidate();
246     gTarget_->Invalidate();
247     if (&view == resetfBtn_) {
248         Clear(fTarget_);
249         fTarget_->SetFlexWrap(false);
250         fTarget_->SetLayoutDirection(LAYOUT_HOR);
251         fTarget_->SetMajorAxisAlign(ALIGN_START);
252         fTarget_->SetSecondaryAxisAlign(ALIGN_CENTER);
253         withMargin_ = false;
254     } else if (&view == marginfBtn_) {
255         withMargin_ = true;
256     } else if (&view == horfBtn_) {
257         fTarget_->SetLayoutDirection(LAYOUT_HOR);
258     } else if (&view == horfRBtn_) {
259         fTarget_->SetLayoutDirection(LAYOUT_HOR_R);
260     } else if (&view == verfBtn_) {
261         fTarget_->SetLayoutDirection(LAYOUT_VER);
262     } else if (&view == verfRBtn_) {
263         fTarget_->SetLayoutDirection(LAYOUT_VER_R);
264     } else if (&view == wrapBtn_) {
265         fTarget_->SetFlexWrap(true);
266     } else if (&view == addElefBtn_) {
267         AddElement(fTarget_);
268     } else if (&view == addTextElefBtn_) {
269         AddTextElement(fTarget_);
270     } else if (&view == majorStartBtn_) {
271         fTarget_->SetMajorAxisAlign(ALIGN_START);
272     } else if (&view == majorEndBtn_) {
273         fTarget_->SetMajorAxisAlign(ALIGN_END);
274     } else if (&view == majorCenterBtn_) {
275         fTarget_->SetMajorAxisAlign(ALIGN_CENTER);
276     } else if (&view == majorEvenBtn_) {
277         fTarget_->SetMajorAxisAlign(ALIGN_EVENLY);
278     } else if (&view == majorAroundBtn_) {
279         fTarget_->SetMajorAxisAlign(ALIGN_AROUND);
280     } else if (&view == majorBetBtn_) {
281         fTarget_->SetMajorAxisAlign(ALIGN_BETWEEN);
282     } else if (&view == secStartBtn_) {
283         fTarget_->SetSecondaryAxisAlign(ALIGN_START);
284     } else if (&view == secEndBtn_) {
285         fTarget_->SetSecondaryAxisAlign(ALIGN_END);
286     } else if (&view == secCenterBtn_) {
287         fTarget_->SetSecondaryAxisAlign(ALIGN_CENTER);
288     } else {
289         OnClickButton(view);
290     }
291     return true;
292 }
293 } // namespace OHOS
294