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