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_ui_swipe_view.h"
17
18 #include <string>
19
20 #include "common/screen.h"
21 #include "components/ui_label.h"
22 #include "components/ui_swipe_view.h"
23
24 namespace OHOS {
25 namespace {
26 static int16_t g_buttonH = 80;
27 static int16_t g_buttonW = 400;
28 static int16_t g_blank = 20;
29 static int16_t g_swipeH = 200;
30 static int16_t g_swipeW = 400;
31 static int16_t g_swipeHorH = 110;
32 static int16_t g_deltaCoordinateY = 19;
33 static int16_t g_deltaCoordinateY2 = 37;
34 static uint16_t g_MaxIndex = 3;
35 } // namespace
36
SetUp()37 void UITestUISwipeView::SetUp()
38 {
39 if (container_ == nullptr) {
40 container_ = new UIScrollView();
41 container_->Resize(Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight() - BACK_BUTTON_HEIGHT);
42 container_->SetThrowDrag(true);
43 container_->SetHorizontalScrollState(false);
44 }
45 }
46
TearDown()47 void UITestUISwipeView::TearDown()
48 {
49 DeleteChildren(container_);
50 container_ = nullptr;
51 addBtnInHead_ = nullptr;
52 addBtnInTail_ = nullptr;
53 addBtnInMid_ = nullptr;
54 removeHeadBtn_ = nullptr;
55 removeMidBtn_ = nullptr;
56 removeAllBtn_ = nullptr;
57 loopBtn_ = nullptr;
58 changePageBtn_ = nullptr;
59 lastX_ = 0;
60 lastY_ = 0;
61 }
62
GetTestView()63 const UIView* UITestUISwipeView::GetTestView()
64 {
65 UIKitSwipeViewTestHorizontal001();
66 UIKitSwipeViewTestHorizontal002();
67 UIKitSwipeViewTestHorizontal003();
68 UIKitSwipeViewTestAlign001(UISwipeView::ALIGN_LEFT);
69 UIKitSwipeViewTestAlign001(UISwipeView::ALIGN_CENTER);
70 UIKitSwipeViewTestAlign001(UISwipeView::ALIGN_RIGHT);
71
72 UIKitSwipeViewTestVer001();
73 UIKitSwipeViewTestVer002();
74 UIKitSwipeViewTestRemove001();
75
76 UIKitSwipeViewTestSetCurrentPage();
77
78 return container_;
79 }
80
UIKitSwipeViewTestHorizontal001()81 void UITestUISwipeView::UIKitSwipeViewTestHorizontal001()
82 {
83 if (container_ == nullptr) {
84 return;
85 }
86 UILabel* label = GetTitleLabel("两个子元素UISwipeView循环水平滑动");
87 container_->Add(label);
88 positionX_ = TEXT_DISTANCE_TO_LEFT_SIDE;
89 positionY_ = TEXT_DISTANCE_TO_TOP_SIDE;
90 label->SetPosition(positionX_, positionY_);
91 positionY_ += g_deltaCoordinateY2;
92
93 UISwipeView* swipe = new UISwipeView(UISwipeView::HORIZONTAL);
94 swipe->SetIntercept(true);
95 swipe->SetLoopState(true);
96 swipe->SetStyle(STYLE_BACKGROUND_COLOR, Color::Red().full);
97 swipe->SetPosition(positionX_, positionY_, g_swipeW, g_swipeHorH);
98 swipe->SetBlankSize(100); // 100: is blank size
99 container_->Add(swipe);
100 UILabelButton* button1 = new UILabelButton();
101 button1->SetPosition(0, 0, g_buttonW, g_buttonH);
102 button1->SetText("button1");
103 button1->SetViewId(UI_TEST_HOR_001_BUTTON1);
104 swipe->Add(button1);
105 UILabelButton* button2 = new UILabelButton();
106 button2->SetPosition(0, 0, g_buttonW, g_buttonH);
107 button2->SetText("button2");
108 button2->SetViewId(UI_TEST_HOR_001_BUTTON2);
109 swipe->Add(button2);
110 SetLastPos(swipe);
111 positionY_ += g_swipeHorH;
112 }
113
UIKitSwipeViewTestHorizontal002()114 void UITestUISwipeView::UIKitSwipeViewTestHorizontal002()
115 {
116 if (container_ == nullptr) {
117 return;
118 }
119 UILabel* label = GetTitleLabel("UISwipeView水平滑动");
120 container_->Add(label);
121 positionY_ += g_deltaCoordinateY;
122 label->SetPosition(positionX_, positionY_);
123 positionY_ += g_deltaCoordinateY2;
124
125 UISwipeView* swipe = new UISwipeView(UISwipeView::HORIZONTAL);
126 swipe->SetIntercept(true);
127 swipe->SetStyle(STYLE_BACKGROUND_COLOR, Color::Red().full);
128 swipe->SetPosition(positionX_, positionY_, g_swipeW, g_swipeHorH);
129 swipe->SetAnimatorTime(100); // 100: mean animator drag time(ms)
130 container_->Add(swipe);
131 UILabelButton* button1 = new UILabelButton();
132 button1->SetPosition(0, 0, g_buttonW, g_buttonH);
133 button1->SetText("button1");
134 button1->SetViewId(UI_TEST_HOR_002_BUTTON1);
135 swipe->Add(button1);
136 UILabelButton* button2 = new UILabelButton();
137 button2->SetPosition(0, 0, g_buttonW, g_buttonH);
138 button2->SetText("button2");
139 button2->SetViewId(UI_TEST_HOR_002_BUTTON2);
140 swipe->Add(button2);
141 UILabelButton* button3 = new UILabelButton();
142 button3->SetPosition(0, 0, g_buttonW, g_buttonH);
143 button3->SetText("button3");
144 button3->SetViewId(UI_TEST_HOR_002_BUTTON3);
145 swipe->Add(button3);
146 SetLastPos(swipe);
147 positionY_ += g_swipeHorH;
148 }
149
UIKitSwipeViewTestHorizontal003()150 void UITestUISwipeView::UIKitSwipeViewTestHorizontal003()
151 {
152 if (container_ == nullptr) {
153 return;
154 }
155 UILabel* label = GetTitleLabel("UISwipeView循环水平滑动");
156 container_->Add(label);
157 positionY_ += g_deltaCoordinateY;
158 label->SetPosition(positionX_, positionY_);
159 positionY_ += g_deltaCoordinateY2;
160
161 UISwipeView* swipe = new UISwipeView(UISwipeView::HORIZONTAL);
162 swipe->SetIntercept(true);
163 swipe->SetStyle(STYLE_BACKGROUND_COLOR, Color::Red().full);
164 swipe->SetPosition(positionX_, positionY_, g_swipeW, g_swipeHorH);
165 swipe->SetLoopState(true);
166 swipe->SetAnimatorTime(100); // 100: mean animator drag time(ms)
167 container_->Add(swipe);
168 UILabelButton* button1 = new UILabelButton();
169 button1->SetPosition(0, 0, g_buttonW, g_buttonH);
170 button1->SetText("button1");
171 button1->SetViewId(UI_TEST_HOR_003_BUTTON1);
172 swipe->Add(button1);
173 UILabelButton* button2 = new UILabelButton();
174 button2->SetPosition(0, 0, g_buttonW, g_buttonH);
175 button2->SetText("button2");
176 button2->SetViewId(UI_TEST_HOR_003_BUTTON2);
177 swipe->Add(button2);
178 UILabelButton* button3 = new UILabelButton();
179 button3->SetPosition(0, 0, g_buttonW, g_buttonH);
180 button3->SetText("button3");
181 button3->SetViewId(UI_TEST_HOR_003_BUTTON3);
182 swipe->Add(button3);
183 SetLastPos(swipe);
184 positionY_ += g_swipeHorH;
185 }
186
UIKitSwipeViewTestVer001()187 void UITestUISwipeView::UIKitSwipeViewTestVer001()
188 {
189 if (container_ == nullptr) {
190 return;
191 }
192 positionX_ = Screen::GetInstance().GetWidth() / 2 + TEXT_DISTANCE_TO_LEFT_SIDE; // 2: half of screen width
193 positionY_ = TEXT_DISTANCE_TO_TOP_SIDE;
194 UILabel* label = GetTitleLabel("UISwipeView垂直滑动");
195 container_->Add(label);
196 positionY_ += g_deltaCoordinateY;
197 label->SetPosition(positionX_, positionY_);
198 positionY_ += g_deltaCoordinateY2;
199
200 UISwipeView* swipe = new UISwipeView(UISwipeView::VERTICAL);
201 swipe->SetIntercept(true);
202 swipe->SetStyle(STYLE_BACKGROUND_COLOR, Color::Red().full);
203 swipe->SetPosition(positionX_, positionY_, g_swipeH, g_swipeW);
204 container_->Add(swipe);
205 UILabelButton* button1 = new UILabelButton();
206 button1->SetPosition(0, 0, g_buttonH, g_buttonW);
207 button1->SetText("button1");
208 button1->SetViewId(UI_TEST_VER_001_BUTTON1);
209 swipe->Add(button1);
210 UILabelButton* button2 = new UILabelButton();
211 button2->SetPosition(0, 0, g_buttonH, g_buttonW);
212 button2->SetText("button2");
213 button2->SetViewId(UI_TEST_VER_001_BUTTON2);
214 swipe->Add(button2);
215 SetLastPos(swipe);
216 positionY_ += g_swipeW + g_deltaCoordinateY;
217 }
218
UIKitSwipeViewTestVer002()219 void UITestUISwipeView::UIKitSwipeViewTestVer002()
220 {
221 if (container_ == nullptr) {
222 return;
223 }
224
225 UILabel* label = GetTitleLabel("UISwipeView循环垂直滑动");
226 container_->Add(label);
227 label->SetPosition(positionX_, positionY_);
228 positionY_ += g_deltaCoordinateY2;
229
230 UISwipeView* swipe = new UISwipeView(UISwipeView::VERTICAL);
231 swipe->SetIntercept(true);
232 swipe->SetStyle(STYLE_BACKGROUND_COLOR, Color::Red().full);
233 swipe->SetPosition(positionX_, positionY_, g_swipeH, g_swipeW);
234 swipe->SetLoopState(true);
235 swipe->SetAnimatorTime(100); // 100: is animator drag time(ms)
236 container_->Add(swipe);
237 UILabelButton* button1 = new UILabelButton();
238 button1->SetPosition(0, 0, g_buttonH, g_buttonW);
239 button1->SetText("button1");
240 button1->SetViewId(UI_TEST_VER_002_BUTTON1);
241 swipe->Add(button1);
242 UILabelButton* button2 = new UILabelButton();
243 button2->SetPosition(0, 0, g_buttonH, g_buttonW);
244 button2->SetText("button2");
245 button2->SetViewId(UI_TEST_VER_002_BUTTON2);
246 swipe->Add(button2);
247 UILabelButton* button3 = new UILabelButton();
248 button3->SetPosition(0, 0, g_buttonH, g_buttonW);
249 button3->SetText("button3");
250 button2->SetViewId(UI_TEST_VER_003_BUTTON3);
251 swipe->Add(button3);
252 SetLastPos(swipe);
253 positionY_ += g_swipeW;
254 }
255
UIKitSwipeViewTestRemove001()256 void UITestUISwipeView::UIKitSwipeViewTestRemove001()
257 {
258 if (container_ == nullptr) {
259 return;
260 }
261 UILabel* label = GetTitleLabel("UISwipeView增加和删除子节点");
262 container_->Add(label);
263 positionY_ += g_deltaCoordinateY;
264 label->SetPosition(positionX_, positionY_);
265 positionY_ += g_deltaCoordinateY2;
266
267 UISwipeView* swipe = new UISwipeView(UISwipeView::HORIZONTAL);
268 swipe->SetIntercept(true);
269 swipe->SetStyle(STYLE_BACKGROUND_COLOR, Color::Red().full);
270 swipe->SetPosition(positionX_, positionY_, g_swipeH, g_swipeW);
271 swipe->SetLoopState(true);
272 swipe->SetAnimatorTime(100); // 100: mean animator drag time(ms)
273 currentSwipe_ = swipe;
274 container_->Add(swipe);
275
276 if (addBtnInHead_ == nullptr) {
277 addBtnInHead_ = new UILabelButton();
278 }
279 if (addBtnInTail_ == nullptr) {
280 addBtnInTail_ = new UILabelButton();
281 }
282 if (addBtnInMid_ == nullptr) {
283 addBtnInMid_ = new UILabelButton();
284 }
285 if (removeHeadBtn_ == nullptr) {
286 removeHeadBtn_ = new UILabelButton();
287 }
288 if (removeMidBtn_ == nullptr) {
289 removeMidBtn_ = new UILabelButton();
290 }
291 if (removeAllBtn_ == nullptr) {
292 removeAllBtn_ = new UILabelButton();
293 }
294
295 positionX_ = positionX_ + swipe->GetWidth() + 20; // 20: is interval between button and swipe
296 SetUpButton(addBtnInHead_, "增加至头部 ", UI_TEST_ADD_TO_HEAD);
297 SetUpButton(addBtnInTail_, "增加至尾部 ", UI_TEST_ADD_TO_TAIL);
298 SetUpButton(addBtnInMid_, "增加至头部后 ", UI_TEST_ADD_TO_BACK_OF_HEAD);
299 SetUpButton(removeHeadBtn_, "删除头部节点 ", UI_TEST_DELETE_HEAD);
300 SetUpButton(removeMidBtn_, "删除中间节点 ", UI_TEST_DELETE_MIDDLE);
301 SetUpButton(removeAllBtn_, "删除全部节点 ", UI_TEST_DELETE_ALL);
302
303 SetLastPos(swipe);
304 }
305
UIKitSwipeViewTestAlign001(UISwipeView::AlignMode alignMode)306 void UITestUISwipeView::UIKitSwipeViewTestAlign001(UISwipeView::AlignMode alignMode)
307 {
308 static uint8_t divNum = 4;
309 if (container_ == nullptr) {
310 return;
311 }
312
313 UILabel* label;
314 if (alignMode == UISwipeView::ALIGN_LEFT) {
315 const char* leftTitle = "UISwipeView 子组件左对齐";
316 label = GetTitleLabel(leftTitle);
317 } else if (alignMode == UISwipeView::ALIGN_RIGHT) {
318 const char* rightTitle = "UISwipeView 子组件右对齐";
319 label = GetTitleLabel(rightTitle);
320 } else {
321 const char* centerTitle = "UISwipeView 子组件Center对齐";
322 label = GetTitleLabel(centerTitle);
323 }
324
325 container_->Add(label);
326 label->SetPosition(positionX_, lastY_ + g_blank);
327
328 UISwipeView* swipe = new UISwipeView(UISwipeView::HORIZONTAL);
329 swipe->SetIntercept(true);
330 swipe->SetStyle(STYLE_BACKGROUND_COLOR, Color::Red().full);
331 // 100: offset
332 swipe->SetPosition(positionX_, label->GetY() + g_blank + g_deltaCoordinateY, g_swipeW - 100, g_swipeH);
333 swipe->SetLoopState(true);
334 swipe->SetAnimatorTime(100); // 100: mean animator drag time(ms)
335 swipe->SetAlignMode(alignMode);
336 container_->Add(swipe);
337 UILabelButton* button1 = new UILabelButton();
338 button1->SetPosition(0, 0, g_buttonW / divNum, g_buttonH / 2); // 2: half
339 button1->SetText("button1");
340 swipe->Add(button1);
341 UILabelButton* button2 = new UILabelButton();
342 button2->SetPosition(0, 0, g_buttonW / divNum, g_buttonH / 2); // 2: half
343 button2->SetText("button2");
344 swipe->Add(button2);
345 UILabelButton* button3 = new UILabelButton();
346 button3->SetPosition(0, 0, g_buttonW / divNum, g_buttonH / 2); // 2: half
347 button3->SetText("button3");
348 swipe->Add(button3);
349
350 UILabel* tmpLabel = nullptr;
351 uint8_t tmpAlignMode = swipe->GetAlignMode();
352 if (tmpAlignMode == alignMode) {
353 const char* tmpOkTitle = "The result of GetAlignMode is OK.";
354 tmpLabel = GetTitleLabel(tmpOkTitle);
355 } else {
356 const char* tmpErrTitle = "The result of GetAlignMode is Error.";
357 tmpLabel = GetTitleLabel(tmpErrTitle);
358 }
359 tmpLabel->SetPosition(positionX_, 100, 250, 25); // 100: y, 250:width, 25:height
360 swipe->Add(tmpLabel);
361
362 SetLastPos(swipe);
363 }
364
UIKitSwipeViewTestSetCurrentPage()365 void UITestUISwipeView::UIKitSwipeViewTestSetCurrentPage()
366 {
367 if (container_ == nullptr) {
368 return;
369 }
370 positionY_ += g_deltaCoordinateY;
371 UILabel* label = GetTitleLabel("UISwipeView切换页面");
372 container_->Add(label);
373 positionY_ += g_deltaCoordinateY;
374 label->SetPosition(TEXT_DISTANCE_TO_LEFT_SIDE, positionY_);
375 positionY_ += g_deltaCoordinateY2;
376
377 UISwipeView* swipe = new UISwipeView(UISwipeView::HORIZONTAL);
378 swipe->SetIntercept(true);
379 swipe->SetStyle(STYLE_BACKGROUND_COLOR, Color::Red().full);
380 swipe->SetPosition(TEXT_DISTANCE_TO_LEFT_SIDE, positionY_, g_swipeW, g_swipeH);
381 swipe->SetLoopState(loop_);
382 swipe->SetAnimatorTime(1000); // 1000: mean animator drag time(ms)
383 currentSwipe_ = swipe;
384 container_->Add(swipe);
385 UIView* view1 = new UIView();
386 view1->SetStyle(STYLE_BACKGROUND_COLOR, Color::Red().full);
387 view1->Resize(g_swipeW, g_swipeH);
388 swipe->Add(view1);
389 UIView* view2 = new UIView();
390 view2->SetStyle(STYLE_BACKGROUND_COLOR, Color::White().full);
391 view2->Resize(g_swipeW, g_swipeH);
392 swipe->Add(view2);
393 UIView* view3 = new UIView();
394 view3->SetStyle(STYLE_BACKGROUND_COLOR, Color::Blue().full);
395 view3->Resize(g_swipeW, g_swipeH);
396 swipe->Add(view3);
397 UIView* view4 = new UIView();
398 view4->SetStyle(STYLE_BACKGROUND_COLOR, Color::Yellow().full);
399 view4->Resize(g_swipeW, g_swipeH);
400 swipe->Add(view4);
401 if (loopBtn_ == nullptr) {
402 loopBtn_ = new UILabelButton();
403 }
404 if (changePageBtn_ == nullptr) {
405 changePageBtn_ = new UILabelButton();
406 }
407
408 positionX_ = TEXT_DISTANCE_TO_LEFT_SIDE + swipe->GetWidth() + 20; // 20: is interval between button and swipe
409 positionY_ += g_deltaCoordinateY2;
410 SetUpButton(loopBtn_, "设置循环 关 ", UI_TEST_SET_CYCLE);
411 SetUpButton(changePageBtn_, "切换页面 ", UI_TEST_SWITCH_PAGES);
412 }
413
OnClick(UIView & view,const ClickEvent & event)414 bool UITestUISwipeView::OnClick(UIView& view, const ClickEvent& event)
415 {
416 if (currentSwipe_ == nullptr) {
417 return true;
418 }
419 if (&view == addBtnInHead_) {
420 UILabelButton* btn = new UILabelButton();
421 btn->SetPosition(0, 0, g_buttonH, g_buttonW);
422 btn->SetText(std::to_string(btnNum_).c_str());
423 currentSwipe_->Insert(nullptr, btn);
424 } else if (&view == addBtnInTail_) {
425 UILabelButton* btn = new UILabelButton();
426 btn->SetPosition(0, 0, g_buttonH, g_buttonW);
427 btn->SetText(std::to_string(btnNum_).c_str());
428 currentSwipe_->Add(btn);
429 } else if (&view == addBtnInMid_) {
430 UILabelButton* btn = new UILabelButton();
431 btn->SetPosition(0, 0, g_buttonH, g_buttonW);
432 btn->SetText(std::to_string(btnNum_).c_str());
433 currentSwipe_->Insert(currentSwipe_->GetChildrenHead(), btn);
434 } else if (&view == removeHeadBtn_) {
435 currentSwipe_->Remove(currentSwipe_->GetChildrenHead());
436 } else if (&view == removeMidBtn_) {
437 UIView* viewToBeRemoved = currentSwipe_->GetViewByIndex(1);
438 currentSwipe_->Remove(viewToBeRemoved);
439 } else if (&view == removeAllBtn_) {
440 currentSwipe_->RemoveAll();
441 } else if (&view == loopBtn_) {
442 loop_ = !loop_;
443 currentSwipe_->SetLoopState(loop_);
444 if (!loop_) {
445 loopBtn_->SetText("设置循环 关 ");
446 } else {
447 loopBtn_->SetText("设置循环 开 ");
448 }
449 } else if (&view == changePageBtn_) {
450 uint16_t currentIndex = currentSwipe_->GetCurrentPage();
451 if (currentIndex < g_MaxIndex) {
452 currentSwipe_->SetCurrentPage(++currentIndex, true);
453 } else {
454 currentSwipe_->SetCurrentPage(0, true);
455 }
456 }
457 currentSwipe_->Invalidate();
458 btnNum_++;
459 return true;
460 }
461
SetUpButton(UILabelButton * btn,const char * title,const char * id)462 void UITestUISwipeView::SetUpButton(UILabelButton* btn, const char* title, const char* id)
463 {
464 if (btn == nullptr || title == nullptr || id == nullptr) {
465 return;
466 }
467 container_->Add(btn);
468 btn->SetPosition(positionX_, positionY_, BUTTON_WIDHT2, BUTTON_HEIGHT2);
469 positionY_ += btn->GetHeight() + 10; // 10: increase y-coordinate
470 btn->SetText(title);
471 btn->SetFont(DEFAULT_VECTOR_FONT_FILENAME, BUTTON_LABEL_SIZE);
472 btn->SetOnClickListener(this);
473 btn->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_STYLE_BORDER_RADIUS_VALUE, UIButton::RELEASED);
474 btn->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_STYLE_BORDER_RADIUS_VALUE, UIButton::PRESSED);
475 btn->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_STYLE_BORDER_RADIUS_VALUE, UIButton::INACTIVE);
476 btn->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::RELEASED);
477 btn->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::PRESSED);
478 btn->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::INACTIVE);
479 btn->SetViewId(id);
480 container_->Invalidate();
481 }
482
SetLastPos(UIView * view)483 void UITestUISwipeView::SetLastPos(UIView* view)
484 {
485 if (view == nullptr) {
486 return;
487 }
488 lastX_ = view->GetX();
489 lastY_ = view->GetY() + view->GetHeight();
490 }
491 } // namespace OHOS
492