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_basic_layout.h"
17 #include "common/screen.h"
18
19 namespace OHOS {
20 namespace {
21 const int16_t ITEM_H = 50;
22 const int16_t TEXT_H = 20;
23 const int16_t TEXT_W = 250;
24 const int16_t TEST_VIEW_H = 40;
25 const int16_t TEST_VIEW_W = 40;
26 const int16_t GAP = 5;
27 const int16_t TEST_VIEW_GAP = 80;
28 } // namespace
29
SetUp()30 void UITestBasicLayout::SetUp()
31 {
32 container_ = new UIScrollView();
33 layout_ = new GridLayout();
34 uiViewGroupFrame_ = new UIViewGroup();
35 testTarget_ = new UIViewGroup();
36 testTargetSmall1_ = new UIView();
37 testTargetSmall2_ = new UIView();
38 refTarget_ = new UIView();
39 resetBtn_ = new UILabelButton();
40 marginBtn_ = new UILabelButton();
41 padBtn_ = new UILabelButton();
42
43 lCenterOfParentBtn_ = new UILabelButton();
44 lLeftOfParentBtn_ = new UILabelButton();
45 lRightOfParentBtn_ = new UILabelButton();
46 lTopOfParentBtn_ = new UILabelButton();
47 lBottomOfParentBtn_ = new UILabelButton();
48
49 aLeftToSiblingBtn_ = new UILabelButton();
50 aRightToSiblingBtn_ = new UILabelButton();
51 aTopToSiblingBtn_ = new UILabelButton();
52 aBottomToSiblingBtn_ = new UILabelButton();
53 aHorCenterToSiblingBtn_ = new UILabelButton();
54 aVerCenterToSiblingBtn_ = new UILabelButton();
55
56 lLeftToSiblingBtn_ = new UILabelButton();
57 lRightToSiblingBtn_ = new UILabelButton();
58 lTopToSiblingBtn_ = new UILabelButton();
59 lBottomToSiblingBtn_ = new UILabelButton();
60 lInvalidLayoutBtn_ = new UILabelButton();
61 container_->Resize(Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight() - BACK_BUTTON_HEIGHT);
62 }
63
TearDown()64 void UITestBasicLayout::TearDown()
65 {
66 DeleteChildren(container_);
67 container_ = nullptr;
68 layout_ = nullptr;
69 testTarget_ = nullptr;
70 uiViewGroupFrame_ = nullptr;
71 testTargetSmall1_ = nullptr;
72 testTargetSmall2_ = nullptr;
73 refTarget_ = nullptr;
74 resetBtn_ = nullptr;
75 lCenterOfParentBtn_ = nullptr;
76 lLeftOfParentBtn_ = nullptr;
77 lRightOfParentBtn_ = nullptr;
78 lTopOfParentBtn_ = nullptr;
79 lBottomOfParentBtn_ = nullptr;
80 aLeftToSiblingBtn_ = nullptr;
81 aRightToSiblingBtn_ = nullptr;
82 aTopToSiblingBtn_ = nullptr;
83 aBottomToSiblingBtn_ = nullptr;
84 aHorCenterToSiblingBtn_ = nullptr;
85 aVerCenterToSiblingBtn_ = nullptr;
86 lLeftToSiblingBtn_ = nullptr;
87 lRightToSiblingBtn_ = nullptr;
88 lTopToSiblingBtn_ = nullptr;
89 lBottomToSiblingBtn_ = nullptr;
90 lInvalidLayoutBtn_ = nullptr;
91 marginBtn_ = nullptr;
92 padBtn_ = nullptr;
93 setStyle_ = nullptr;
94 positionX_ = 20; // 20: position x
95 positionY_ = 5; // 5 position y
96 }
97
GetTestView()98 const UIView* UITestBasicLayout::GetTestView()
99 {
100 UIKitLayoutTestBasicLayout001();
101 return container_;
102 }
103
UIKitLayoutTestBasicLayout001()104 void UITestBasicLayout::UIKitLayoutTestBasicLayout001()
105 {
106 if (container_ != nullptr) {
107 UIViewGroup* uiViewGroup = new UIViewGroup();
108 uiViewGroup->SetPosition(0, 0, 320, 390); // 320: width; 390: height
109 container_->Add(uiViewGroup);
110 UILabel* label = new UILabel();
111 uiViewGroup->Add(label);
112 label->SetPosition(TEXT_DISTANCE_TO_LEFT_SIDE, TEXT_DISTANCE_TO_TOP_SIDE, 288, 48); // 288: width; 48: height
113 label->SetText("布局效果");
114 label->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
115 uiViewGroup->Add(uiViewGroupFrame_);
116 // 288: width; 336: height
117 uiViewGroupFrame_->SetPosition(VIEW_DISTANCE_TO_LEFT_SIDE2, VIEW_DISTANCE_TO_TOP_SIDE, 288, 336);
118 uiViewGroupFrame_->SetStyle(STYLE_BORDER_COLOR, Color::White().full);
119 uiViewGroupFrame_->SetStyle(STYLE_BORDER_OPA, HALF_OPA_OPAQUE);
120 uiViewGroupFrame_->SetStyle(STYLE_BORDER_WIDTH, VIEW_STYLE_BORDER_WIDTH);
121 uiViewGroupFrame_->SetStyle(STYLE_BORDER_RADIUS, VIEW_STYLE_BORDER_RADIUS);
122 uiViewGroupFrame_->SetStyle(STYLE_BACKGROUND_OPA, 0);
123 uiViewGroupFrame_->Add(testTarget_);
124 testTarget_->SetPosition(0, 0, 96, 96); // 96: width 96:height
125 testTargetSmall1_->Resize(40, 40); // 40:width 40:height
126 testTargetSmall2_->Resize(20, 20); // 20:width 20:height
127 testTargetSmall1_->LayoutCenterOfParent();
128 testTargetSmall2_->LayoutCenterOfParent();
129 testTargetSmall1_->SetStyle(STYLE_BACKGROUND_COLOR, Color::Purple().full);
130 testTargetSmall2_->SetStyle(STYLE_BACKGROUND_COLOR, Color::Cyan().full);
131 testTarget_->SetStyle(STYLE_BACKGROUND_COLOR, Color::GetColorFromRGB(0, 182, 191).full); // 182, 191: color
132 uiViewGroupFrame_->Add(refTarget_);
133 refTarget_->Resize(VIEW_DISTANCE_TO_LEFT_SIDE, VIEW_DISTANCE_TO_TOP_SIDE);
134 refTarget_->SetViewId("refView");
135 refTarget_->LayoutCenterOfParent();
136 refTarget_->SetStyle(STYLE_BACKGROUND_COLOR, Color::GetColorFromRGB(255, 152, 0).full); // 255, 152: color
137
138 UIViewGroup* uiViewGroup2 = new UIViewGroup();
139 uiViewGroup2->SetPosition(336, 0, 600, 384); // 336: x-coordinate; 600: width; 384: height
140 container_->Add(uiViewGroup2);
141 UILabel* label2 = new UILabel();
142 uiViewGroup2->Add(label2);
143 // 288: width; 48: height
144 label2->SetPosition(VIEW_DISTANCE_TO_LEFT_SIDE2, TEXT_DISTANCE_TO_TOP_SIDE, 288, 48);
145 label2->SetText("布局效果");
146 label2->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
147 uiViewGroup2->Add(layout_);
148 layout_->SetPosition(0, VIEW_DISTANCE_TO_TOP_SIDE, 516, 296); // 516: width; 296: height
149 layout_->SetLayoutDirection(LAYOUT_HOR);
150 layout_->SetRows(5); // 5: row
151 layout_->SetCols(4); // 4: col
152 InitSetUpButton();
153 layout_->LayoutChildren();
154 positionY_ += ITEM_H;
155 }
156 }
157
InitSetUpButton()158 void UITestBasicLayout::InitSetUpButton()
159 {
160 SetUpButton(resetBtn_, "重置", UI_TEST_BASIC_LAYOUT_RESET_BTN_ID);
161 SetUpButton(marginBtn_, "margin", UI_TEST_BASIC_LAYOUT_MARGIN_BTN_ID);
162 SetUpButton(padBtn_, "padding", UI_TEST_BASIC_LAYOUT_PADDING_BTN_ID);
163 SetUpButton(lCenterOfParentBtn_, "居中", UI_TEST_BASIC_LAYOUT_CENTEROFPARENT_BTN_ID);
164 SetUpButton(lLeftOfParentBtn_, "居左", UI_TEST_BASIC_LAYOUT_LEFTOFPARENT_BTN_ID);
165 SetUpButton(lRightOfParentBtn_, "居右", UI_TEST_BASIC_LAYOUT_RIGHTOFPARENT_BTN_ID);
166 SetUpButton(lTopOfParentBtn_, "居上", UI_TEST_BASIC_LAYOUT_TOPOFPARENT_BTN_ID);
167 SetUpButton(lBottomOfParentBtn_, "居下", UI_TEST_BASIC_LAYOUT_BOTTOMOFPARENT_BTN_ID);
168 SetUpButton(aLeftToSiblingBtn_, "左侧对齐兄弟", UI_TEST_BASIC_LAYOUT_LEFTOFSIBLING_BTN_ID);
169 SetUpButton(aRightToSiblingBtn_, "右侧对齐兄弟", UI_TEST_BASIC_LAYOUT_RIGHTOFSIBLING_BTN_ID);
170 SetUpButton(aTopToSiblingBtn_, "上侧对齐兄弟", UI_TEST_BASIC_LAYOUT_TOPOFSIBLING_BTN_ID);
171 SetUpButton(aBottomToSiblingBtn_, "下侧对齐兄弟", UI_TEST_BASIC_LAYOUT_BOTTOMOFSIBLING_BTN_ID);
172 SetUpButton(aHorCenterToSiblingBtn_, "水平居中对齐", UI_TEST_BASIC_LAYOUT_HORCENTEROFSIBLING_BTN_ID);
173 SetUpButton(aVerCenterToSiblingBtn_, "竖直居中对齐", UI_TEST_BASIC_LAYOUT_VERCENTEROFSIBLING_BTN_ID);
174 SetUpButton(lLeftToSiblingBtn_, "放至兄弟左侧", UI_TEST_BASIC_LAYOUT_LEFTTOSIBLING_BTN_ID);
175 SetUpButton(lRightToSiblingBtn_, "放至兄弟右侧", UI_TEST_BASIC_LAYOUT_RIGHTTOSIBLING_BTN_ID);
176 SetUpButton(lTopToSiblingBtn_, "放至兄弟上侧", UI_TEST_BASIC_LAYOUT_TOPTOSIBLING_BTN_ID);
177 SetUpButton(lBottomToSiblingBtn_, "放至兄弟下侧", UI_TEST_BASIC_LAYOUT_BOTTOMTOSIBLING_BTN_ID);
178 SetUpButton(lInvalidLayoutBtn_, "非法节点布局", UI_TEST_BASIC_LAYOUT_INVALIDLAYOUT_BTN_ID);
179 }
180
OnClickButton(UIView & view)181 void UITestBasicLayout::OnClickButton(UIView& view)
182 {
183 if (&view == aLeftToSiblingBtn_) {
184 testTarget_->AlignLeftToSibling("refView");
185 } else if (&view == aRightToSiblingBtn_) {
186 testTarget_->AlignRightToSibling("refView");
187 } else if (&view == aTopToSiblingBtn_) {
188 testTarget_->AlignTopToSibling("refView");
189 } else if (&view == aBottomToSiblingBtn_) {
190 testTarget_->AlignBottomToSibling("refView");
191 } else if (&view == aHorCenterToSiblingBtn_) {
192 testTarget_->AlignHorCenterToSibling("refView");
193 } else if (&view == aVerCenterToSiblingBtn_) {
194 testTarget_->AlignVerCenterToSibling("refView");
195 } else if (&view == lLeftToSiblingBtn_) {
196 testTarget_->LayoutLeftToSibling("refView");
197 } else if (&view == lRightToSiblingBtn_) {
198 testTarget_->LayoutRightToSibling("refView");
199 } else if (&view == lTopToSiblingBtn_) {
200 testTarget_->LayoutTopToSibling("refView");
201 } else if (&view == lBottomToSiblingBtn_) {
202 testTarget_->LayoutBottomToSibling("refView");
203 } else if (&view == lInvalidLayoutBtn_) {
204 testTarget_->AlignLeftToSibling("invalid");
205 } else {
206 }
207 }
208
OnClick(UIView & view,const ClickEvent & event)209 bool UITestBasicLayout::OnClick(UIView& view, const ClickEvent& event)
210 {
211 uiViewGroupFrame_->Invalidate();
212 if (&view == resetBtn_) {
213 testTarget_->SetPosition(0, 0);
214 testTarget_->SetStyle(STYLE_MARGIN_LEFT, 0);
215 testTarget_->SetStyle(STYLE_MARGIN_RIGHT, 0);
216 testTarget_->SetStyle(STYLE_MARGIN_TOP, 0);
217 testTarget_->SetStyle(STYLE_MARGIN_BOTTOM, 0);
218 uiViewGroupFrame_->SetStyle(STYLE_PADDING_LEFT, 0);
219 uiViewGroupFrame_->SetStyle(STYLE_PADDING_RIGHT, 0);
220 uiViewGroupFrame_->SetStyle(STYLE_PADDING_TOP, 0);
221 uiViewGroupFrame_->SetStyle(STYLE_PADDING_BOTTOM, 0);
222 } else if (&view == marginBtn_) {
223 testTarget_->SetStyle(STYLE_MARGIN_LEFT, 10); // 10: margin left
224 testTarget_->SetStyle(STYLE_MARGIN_RIGHT, 10); // 10: margin right
225 testTarget_->SetStyle(STYLE_MARGIN_TOP, 10); // 10: margin top
226 testTarget_->SetStyle(STYLE_MARGIN_BOTTOM, 10); // 10: margin bottom
227 } else if (&view == padBtn_) {
228 uiViewGroupFrame_->SetStyle(STYLE_PADDING_LEFT, 15); // 15: padding left
229 uiViewGroupFrame_->SetStyle(STYLE_PADDING_RIGHT, 15); // 15: padding right
230 uiViewGroupFrame_->SetStyle(STYLE_PADDING_TOP, 15); // 15: padding top
231 uiViewGroupFrame_->SetStyle(STYLE_PADDING_BOTTOM, 15); // 15: padding bottom
232 refTarget_->LayoutCenterOfParent();
233 } else if (&view == lCenterOfParentBtn_) {
234 testTarget_->LayoutCenterOfParent();
235 } else if (&view == lLeftOfParentBtn_) {
236 testTarget_->LayoutLeftOfParent();
237 } else if (&view == lRightOfParentBtn_) {
238 testTarget_->LayoutRightOfParent();
239 } else if (&view == lTopOfParentBtn_) {
240 testTarget_->LayoutTopOfParent();
241 } else if (&view == lBottomOfParentBtn_) {
242 testTarget_->LayoutBottomOfParent();
243 } else {
244 OnClickButton(view);
245 }
246 uiViewGroupFrame_->Invalidate();
247 return true;
248 }
249 } // namespace OHOS