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         label = GetTitleLabel("UISwipeView 子组件左对齐");
316     } else if (alignMode == UISwipeView::ALIGN_RIGHT) {
317         const char* rightTitle = "UISwipeView 子组件右对齐";
318         label = GetTitleLabel(rightTitle);
319     } else {
320         const char* centerTitle = "UISwipeView 子组件Center对齐";
321         label = GetTitleLabel(centerTitle);
322     }
323 
324     container_->Add(label);
325     label->SetPosition(positionX_, lastY_ + g_blank);
326 
327     UISwipeView* swipe = new UISwipeView(UISwipeView::HORIZONTAL);
328     swipe->SetIntercept(true);
329     swipe->SetStyle(STYLE_BACKGROUND_COLOR, Color::Red().full);
330     // 100: offset
331     swipe->SetPosition(positionX_, label->GetY() + g_blank + g_deltaCoordinateY, g_swipeW - 100, g_swipeH);
332     swipe->SetLoopState(true);
333     swipe->SetAnimatorTime(100); // 100: mean animator drag time(ms)
334     swipe->SetAlignMode(alignMode);
335     container_->Add(swipe);
336     UILabelButton* button1 = new UILabelButton();
337     button1->SetPosition(0, 0, g_buttonW / divNum, g_buttonH / 2); // 2: half
338     button1->SetText("button1");
339     swipe->Add(button1);
340     UILabelButton* button2 = new UILabelButton();
341     button2->SetPosition(0, 0, g_buttonW / divNum, g_buttonH / 2); // 2: half
342     button2->SetText("button2");
343     swipe->Add(button2);
344     UILabelButton* button3 = new UILabelButton();
345     button3->SetPosition(0, 0, g_buttonW / divNum, g_buttonH / 2); // 2: half
346     button3->SetText("button3");
347     swipe->Add(button3);
348 
349     UILabel* tmpLabel = nullptr;
350     uint8_t tmpAlignMode = swipe->GetAlignMode();
351     if (tmpAlignMode == alignMode) {
352         const char* tmpOkTitle = "The result of GetAlignMode is OK.";
353         tmpLabel = GetTitleLabel(tmpOkTitle);
354     } else {
355         const char* tmpErrTitle = "The result of GetAlignMode is Error.";
356         tmpLabel = GetTitleLabel(tmpErrTitle);
357     }
358     tmpLabel->SetPosition(positionX_, 100, 250, 25); // 100: y, 250:width, 25:height
359     swipe->Add(tmpLabel);
360 
361     SetLastPos(swipe);
362 }
363 
UIKitSwipeViewTestSetCurrentPage()364 void UITestUISwipeView::UIKitSwipeViewTestSetCurrentPage()
365 {
366     if (container_ == nullptr) {
367         return;
368     }
369     positionY_ += g_deltaCoordinateY;
370     UILabel* label = GetTitleLabel("UISwipeView切换页面");
371     container_->Add(label);
372     positionY_ += g_deltaCoordinateY;
373     label->SetPosition(TEXT_DISTANCE_TO_LEFT_SIDE, positionY_);
374     positionY_ += g_deltaCoordinateY2;
375 
376     UISwipeView* swipe = new UISwipeView(UISwipeView::HORIZONTAL);
377     swipe->SetIntercept(true);
378     swipe->SetStyle(STYLE_BACKGROUND_COLOR, Color::Red().full);
379     swipe->SetPosition(TEXT_DISTANCE_TO_LEFT_SIDE, positionY_, g_swipeW, g_swipeH);
380     swipe->SetLoopState(loop_);
381     swipe->SetAnimatorTime(1000); // 1000: mean animator drag time(ms)
382     currentSwipe_ = swipe;
383     container_->Add(swipe);
384     UIView* view1 = new UIView();
385     view1->SetStyle(STYLE_BACKGROUND_COLOR, Color::Red().full);
386     view1->Resize(g_swipeW, g_swipeH);
387     swipe->Add(view1);
388     UIView* view2 = new UIView();
389     view2->SetStyle(STYLE_BACKGROUND_COLOR, Color::White().full);
390     view2->Resize(g_swipeW, g_swipeH);
391     swipe->Add(view2);
392     UIView* view3 = new UIView();
393     view3->SetStyle(STYLE_BACKGROUND_COLOR, Color::Blue().full);
394     view3->Resize(g_swipeW, g_swipeH);
395     swipe->Add(view3);
396     UIView* view4 = new UIView();
397     view4->SetStyle(STYLE_BACKGROUND_COLOR, Color::Yellow().full);
398     view4->Resize(g_swipeW, g_swipeH);
399     swipe->Add(view4);
400     if (loopBtn_ == nullptr) {
401         loopBtn_ = new UILabelButton();
402     }
403     if (changePageBtn_ == nullptr) {
404         changePageBtn_ = new UILabelButton();
405     }
406 
407     positionX_ = TEXT_DISTANCE_TO_LEFT_SIDE + swipe->GetWidth() + 20; // 20: is interval between button and swipe
408     positionY_ += g_deltaCoordinateY2;
409     SetUpButton(loopBtn_, "设置循环 关 ", UI_TEST_SET_CYCLE);
410     SetUpButton(changePageBtn_, "切换页面 ", UI_TEST_SWITCH_PAGES);
411 }
412 
OnClick(UIView & view,const ClickEvent & event)413 bool UITestUISwipeView::OnClick(UIView& view, const ClickEvent& event)
414 {
415     if (currentSwipe_ == nullptr) {
416         return true;
417     }
418     if (&view == addBtnInHead_) {
419         UILabelButton* btn = new UILabelButton();
420         btn->SetPosition(0, 0, g_buttonH, g_buttonW);
421         btn->SetText(std::to_string(btnNum_).c_str());
422         currentSwipe_->Insert(nullptr, btn);
423     } else if (&view == addBtnInTail_) {
424         UILabelButton* btn = new UILabelButton();
425         btn->SetPosition(0, 0, g_buttonH, g_buttonW);
426         btn->SetText(std::to_string(btnNum_).c_str());
427         currentSwipe_->Add(btn);
428     } else if (&view == addBtnInMid_) {
429         UILabelButton* btn = new UILabelButton();
430         btn->SetPosition(0, 0, g_buttonH, g_buttonW);
431         btn->SetText(std::to_string(btnNum_).c_str());
432         currentSwipe_->Insert(currentSwipe_->GetChildrenHead(), btn);
433     } else if (&view == removeHeadBtn_) {
434         currentSwipe_->Remove(currentSwipe_->GetChildrenHead());
435     } else if (&view == removeMidBtn_) {
436         UIView* viewToBeRemoved = currentSwipe_->GetViewByIndex(1);
437         currentSwipe_->Remove(viewToBeRemoved);
438     } else if (&view == removeAllBtn_) {
439         currentSwipe_->RemoveAll();
440     } else if (&view == loopBtn_) {
441         loop_ = !loop_;
442         currentSwipe_->SetLoopState(loop_);
443         if (!loop_) {
444             loopBtn_->SetText("设置循环 关 ");
445         } else {
446             loopBtn_->SetText("设置循环 开 ");
447         }
448     } else if (&view == changePageBtn_) {
449         uint16_t currentIndex = currentSwipe_->GetCurrentPage();
450         if (currentIndex < g_MaxIndex) {
451             currentSwipe_->SetCurrentPage(++currentIndex, true);
452         } else {
453             currentSwipe_->SetCurrentPage(0, true);
454         }
455     }
456     currentSwipe_->Invalidate();
457     btnNum_++;
458     return true;
459 }
460 
SetUpButton(UILabelButton * btn,const char * title,const char * id)461 void UITestUISwipeView::SetUpButton(UILabelButton* btn, const char* title, const char* id)
462 {
463     if (btn == nullptr || title == nullptr || id == nullptr) {
464         return;
465     }
466     container_->Add(btn);
467     btn->SetPosition(positionX_, positionY_, BUTTON_WIDHT2, BUTTON_HEIGHT2);
468     positionY_ += btn->GetHeight() + 10; // 10: increase y-coordinate
469     btn->SetText(title);
470     btn->SetFont(DEFAULT_VECTOR_FONT_FILENAME, BUTTON_LABEL_SIZE);
471     btn->SetOnClickListener(this);
472     btn->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_STYLE_BORDER_RADIUS_VALUE, UIButton::RELEASED);
473     btn->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_STYLE_BORDER_RADIUS_VALUE, UIButton::PRESSED);
474     btn->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_STYLE_BORDER_RADIUS_VALUE, UIButton::INACTIVE);
475     btn->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::RELEASED);
476     btn->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::PRESSED);
477     btn->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::INACTIVE);
478     btn->SetViewId(id);
479     container_->Invalidate();
480 }
481 
SetLastPos(UIView * view)482 void UITestUISwipeView::SetLastPos(UIView* view)
483 {
484     if (view == nullptr) {
485         return;
486     }
487     lastX_ = view->GetX();
488     lastY_ = view->GetY() + view->GetHeight();
489 }
490 } // namespace OHOS
491