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_list_layout.h"
17 #include "common/screen.h"
18 #include "components/ui_label.h"
19 #include "components/ui_label_button.h"
20 #include "layout/list_layout.h"
21
22 namespace OHOS {
23 namespace {
24 static int16_t g_buttonH = 80;
25 static int16_t g_buttonW = 200;
26 static int16_t g_blank = 20;
27 static int16_t g_gap = 8;
28 }
29
SetUp()30 void UITestListLayout::SetUp()
31 {
32 if (container_ == nullptr) {
33 container_ = new UIScrollView();
34 container_->Resize(Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight() - BACK_BUTTON_HEIGHT);
35 container_->SetThrowDrag(true);
36 }
37 }
38
TearDown()39 void UITestListLayout::TearDown()
40 {
41 DeleteChildren(container_);
42 container_ = nullptr;
43 positionX_ = 0;
44 positionY_ = 0;
45 listDirect_ = nullptr;
46 }
47
GetTestView()48 const UIView* UITestListLayout::GetTestView()
49 {
50 UIKitListVerticalAddTestAutoSize001();
51 UIKitListVerticalRemoveTestAutoSize001();
52 UIKitListVerticalRemoveTestAutoSize002();
53 UIKitListHorAddTestAutoSize001();
54 UIKitListHorRemoveTestAutoSize001();
55 UIKitListHorRemoveTestAutoSize002();
56 return container_;
57 }
58
UIKitListVerticalAddTestAutoSize001()59 void UITestListLayout::UIKitListVerticalAddTestAutoSize001()
60 {
61 if (container_ == nullptr) {
62 return;
63 }
64 UILabel* label = GetTitleLabel("UILIst高度自适应 Add Insert");
65 container_->Add(label);
66 label->SetPosition(TEXT_DISTANCE_TO_LEFT_SIDE, positionY_ + g_blank);
67
68 ListLayout* list = new ListLayout();
69 list->SetDirection(ListLayout::VERTICAL);
70 list->SetStyle(STYLE_BACKGROUND_COLOR, Color::Red().full);
71 list->SetPosition(VIEW_DISTANCE_TO_LEFT_SIDE, label->GetY() + TITLE_LABEL_DEFAULT_HEIGHT + g_gap);
72 list->Resize(Screen::GetInstance().GetWidth() - 100, 0); // 100: list reduce width
73 container_->Add(list);
74
75 UILabelButton* button1 = new UILabelButton();
76 button1->SetPosition(0, 0, g_buttonW, g_buttonH);
77 button1->SetText("button1");
78 list->Add(button1);
79 UILabelButton* button2 = new UILabelButton();
80 button2->SetPosition(0, 0, g_buttonW, g_buttonH);
81 button2->SetText("button2");
82 list->Insert(button1, button2);
83
84 if (listDirect_ == nullptr) {
85 listDirect_ = new UILabel();
86 }
87 listDirect_->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
88 listDirect_->SetPosition(list->GetX(), list->GetY(), 100, 100); // 100: list dir width and height
89 if (list->GetDirection() == ListLayout::VERTICAL) {
90 listDirect_->SetText("VERTICAL");
91 } else {
92 listDirect_->SetText("HROIZONTAL");
93 }
94
95 container_->Add(listDirect_);
96
97 SetLastPos(list);
98 }
99
UIKitListVerticalRemoveTestAutoSize001()100 void UITestListLayout::UIKitListVerticalRemoveTestAutoSize001()
101 {
102 if (container_ == nullptr) {
103 return;
104 }
105 UILabel* label = GetTitleLabel("UILIst高度自适应 Remove");
106 container_->Add(label);
107 label->SetPosition(TEXT_DISTANCE_TO_LEFT_SIDE, positionY_ + g_blank);
108
109 ListLayout* list = new ListLayout(ListLayout::VERTICAL);
110 list->SetStyle(STYLE_BACKGROUND_COLOR, Color::Red().full);
111
112 list->SetPosition(VIEW_DISTANCE_TO_LEFT_SIDE, label->GetY() + TITLE_LABEL_DEFAULT_HEIGHT + g_gap,
113 Screen::GetInstance().GetWidth() - 100, 0); // 100: list reduce width
114 container_->Add(list);
115 UILabelButton* button1 = new UILabelButton();
116 button1->SetPosition(0, 0, g_buttonW, g_buttonH);
117 button1->SetText("button1");
118 list->Add(button1);
119 UILabelButton* button2 = new UILabelButton();
120 button2->SetPosition(0, 0, g_buttonW, g_buttonH);
121 button2->SetText("button2");
122 list->Add(button2);
123 list->Remove(button1);
124 SetLastPos(list);
125 }
126
UIKitListVerticalRemoveTestAutoSize002()127 void UITestListLayout::UIKitListVerticalRemoveTestAutoSize002()
128 {
129 if (container_ == nullptr) {
130 return;
131 }
132 UILabel* label = GetTitleLabel("UILIst高度自适应 RemoveAll");
133 container_->Add(label);
134 label->SetPosition(TEXT_DISTANCE_TO_LEFT_SIDE, positionY_ + g_blank);
135
136 ListLayout* list = new ListLayout(ListLayout::VERTICAL);
137 list->SetStyle(STYLE_BACKGROUND_COLOR, Color::Red().full);
138
139 list->SetPosition(VIEW_DISTANCE_TO_LEFT_SIDE, label->GetY() + TITLE_LABEL_DEFAULT_HEIGHT + g_gap,
140 Screen::GetInstance().GetWidth() - 100, 0); // 100: list reduce width
141 container_->Add(list);
142 UILabelButton* button1 = new UILabelButton();
143 button1->SetPosition(0, 0, g_buttonW, g_buttonH);
144 button1->SetText("button1");
145 list->Add(button1);
146 UILabelButton* button2 = new UILabelButton();
147 button2->SetPosition(0, 0, g_buttonW, g_buttonH);
148 button2->SetText("button2");
149 list->Add(button2);
150 list->RemoveAll();
151 SetLastPos(list);
152 }
153
UIKitListHorAddTestAutoSize001()154 void UITestListLayout::UIKitListHorAddTestAutoSize001()
155 {
156 if (container_ == nullptr) {
157 return;
158 }
159 UILabel* label = GetTitleLabel("UILIst宽度自适应 Add Insert");
160 container_->Add(label);
161 label->SetPosition(TEXT_DISTANCE_TO_LEFT_SIDE, positionY_ + g_blank);
162
163 ListLayout* list = new ListLayout(ListLayout::HORIZONTAL);
164 list->SetStyle(STYLE_BACKGROUND_COLOR, Color::Red().full);
165
166 list->SetPosition(VIEW_DISTANCE_TO_LEFT_SIDE, label->GetY() + TITLE_LABEL_DEFAULT_HEIGHT + g_gap, 0,
167 Screen::GetInstance().GetHeight());
168 container_->Add(list);
169 UILabelButton* button1 = new UILabelButton();
170 button1->SetPosition(0, 0, g_buttonW, g_buttonH);
171 button1->SetText("button1");
172 list->Add(button1);
173 UILabelButton* button2 = new UILabelButton();
174 button2->SetPosition(0, 0, g_buttonW, g_buttonH);
175 button2->SetText("button2");
176 list->Insert(button1, button2);
177 SetLastPos(list);
178 }
179
UIKitListHorRemoveTestAutoSize001()180 void UITestListLayout::UIKitListHorRemoveTestAutoSize001()
181 {
182 if (container_ == nullptr) {
183 return;
184 }
185 UILabel* label = GetTitleLabel("UILIst宽度自适应 Remove");
186 container_->Add(label);
187 label->SetPosition(TEXT_DISTANCE_TO_LEFT_SIDE, positionY_ + g_blank);
188
189 ListLayout* list = new ListLayout(ListLayout::HORIZONTAL);
190 list->SetStyle(STYLE_BACKGROUND_COLOR, Color::Red().full);
191
192 list->SetPosition(VIEW_DISTANCE_TO_LEFT_SIDE, label->GetY() + TITLE_LABEL_DEFAULT_HEIGHT + g_gap, 0,
193 Screen::GetInstance().GetHeight());
194 container_->Add(list);
195 UILabelButton* button1 = new UILabelButton();
196 button1->SetPosition(0, 0, g_buttonW, g_buttonH);
197 button1->SetText("button1");
198 list->Add(button1);
199 UILabelButton* button2 = new UILabelButton();
200 button2->SetPosition(0, 0, g_buttonW, g_buttonH);
201 button2->SetText("button2");
202 list->Add(button2);
203 list->Remove(button1);
204 SetLastPos(list);
205 }
206
UIKitListHorRemoveTestAutoSize002()207 void UITestListLayout::UIKitListHorRemoveTestAutoSize002()
208 {
209 if (container_ == nullptr) {
210 return;
211 }
212 UILabel* label = GetTitleLabel("UILIst宽度自适应 RemoveAll");
213 container_->Add(label);
214 label->SetPosition(TEXT_DISTANCE_TO_LEFT_SIDE, positionY_ + g_blank);
215
216 ListLayout* list = new ListLayout(ListLayout::HORIZONTAL);
217 list->SetStyle(STYLE_BACKGROUND_COLOR, Color::Red().full);
218
219 list->SetPosition(VIEW_DISTANCE_TO_LEFT_SIDE, label->GetY() + TITLE_LABEL_DEFAULT_HEIGHT + g_gap, 0,
220 Screen::GetInstance().GetHeight());
221 container_->Add(list);
222 UILabelButton* button1 = new UILabelButton();
223 button1->SetPosition(0, 0, g_buttonW, g_buttonH);
224 button1->SetText("button1");
225 list->Add(button1);
226 UILabelButton* button2 = new UILabelButton();
227 button2->SetPosition(0, 0, g_buttonW, g_buttonH);
228 button2->SetText("button2");
229 list->Add(button2);
230 list->RemoveAll();
231 SetLastPos(list);
232 }
233
SetLastPos(UIView * view)234 void UITestListLayout::SetLastPos(UIView* view)
235 {
236 if (view == nullptr) {
237 return;
238 }
239 positionX_ = view->GetX();
240 positionY_ = view->GetY() + view->GetHeight();
241 }
242 } // namespace OHOS