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_list.h"
17 #include "common/screen.h"
18 #include "components/ui_label.h"
19 #include "components/ui_label_button.h"
20 #include "font/ui_font.h"
21 
22 namespace OHOS {
23 namespace {
24 static int16_t g_blank = 20;
25 static int16_t g_listW = 200;
26 static int16_t g_listH = 400;
27 static int16_t g_selectPos = 150;
28 static int16_t g_blankSize = 250;
29 static int16_t g_padding = 10;
30 static int16_t g_border = 10;
31 static int16_t g_reboundSize = 50;
32 static int16_t g_itemHeight = 100;
33 } // namespace
34 
SetUp()35 void UITestUIList::SetUp()
36 {
37     if (container_ == nullptr) {
38         container_ = new UIScrollView();
39         container_->Resize(Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight() - BACK_BUTTON_HEIGHT);
40         container_->SetThrowDrag(true);
41     }
42     if (adapterData1_ == nullptr) {
43         adapterData1_ = new List<const char*>();
44         adapterData1_->PushBack("abcd0");
45         adapterData1_->PushBack("abcd1");
46         adapterData1_->PushBack("abcd2");
47         adapterData1_->PushBack("abcd3");
48     }
49     if (adapterData2_ == nullptr) {
50         adapterData2_ = new List<const char*>();
51         adapterData2_->PushBack("000");
52         adapterData2_->PushBack("111");
53         adapterData2_->PushBack("222");
54         adapterData2_->PushBack("333");
55         adapterData2_->PushBack("444");
56         adapterData2_->PushBack("555");
57         adapterData2_->PushBack("666");
58         adapterData2_->PushBack("777");
59         adapterData2_->PushBack("888");
60         adapterData2_->PushBack("999");
61     }
62 }
63 
TearDown()64 void UITestUIList::TearDown()
65 {
66     DeleteChildren(container_);
67     container_ = nullptr;
68     TearDown001();
69     setBlankBtn_ = nullptr;
70     setBlankOffBtn_ = nullptr;
71     setThrowDragBtn_ = nullptr;
72     setThrowDragOffBtn_ = nullptr;
73     scrollStateLabel_ = nullptr;
74     scrollSelectLabel_ = nullptr;
75     setRefreshBtn_ = nullptr;
76     setLoopBtn_ = nullptr;
77     setLoopOffBtn_ = nullptr;
78     setListPaddingBtn_ = nullptr;
79     setListBorderBtn_ = nullptr;
80     setRebondSizeBtn_ = nullptr;
81     setSelectBtn_ = nullptr;
82     setSelectOffBtn_ = nullptr;
83     setAutoAlignBtn_ = nullptr;
84     setAutoAlignOffBtn_ = nullptr;
85     setBackAlignBtn_ = nullptr;
86     setBackAlignOffBtn_ = nullptr;
87     setAutoAlignACCIncBtn_ = nullptr;
88     setAutoAlignACCDncBtn_ = nullptr;
89     setYScrollBarVisableBtn_ = nullptr;
90     setYScrollBarInvisableBtn_ = nullptr;
91     setItemHeightMinBtn_ = nullptr;
92     setItemHeightMaxBtn_ = nullptr;
93     setAdapterEmptyBtn_ = nullptr;
94     setSwitchDirectionBtn_ = nullptr;
95     lastX_ = 0;
96     lastY_ = 0;
97 }
98 
TearDown001()99 void UITestUIList::TearDown001()
100 {
101     if (adapterData1_ != nullptr) {
102         adapterData1_->Clear();
103         delete adapterData1_;
104         adapterData1_ = nullptr;
105     }
106     if (adapterData2_ != nullptr) {
107         adapterData2_->Clear();
108         delete adapterData2_;
109         adapterData2_ = nullptr;
110     }
111     if (adapter1_ != nullptr) {
112         delete adapter1_;
113         adapter1_ = nullptr;
114     }
115     if (adapter2_ != nullptr) {
116         delete adapter2_;
117         adapter2_ = nullptr;
118     }
119     if (adapter3_ != nullptr) {
120         delete adapter3_;
121         adapter3_ = nullptr;
122     }
123     if (adapter4_ != nullptr) {
124         delete adapter4_;
125         adapter4_ = nullptr;
126     }
127 }
128 
GetTestView()129 const UIView* UITestUIList::GetTestView()
130 {
131     UIKitListInitTestFullScreen001();
132     UIKitListInitTesthalhScreen001();
133     UIKitListScrollTestBlankSet001();
134     return container_;
135 }
136 
UIKitListInitTestFullScreen001()137 void UITestUIList::UIKitListInitTestFullScreen001()
138 {
139     if (container_ == nullptr) {
140         return;
141     }
142     UILabel* label = GetTitleLabel("UILIst设置adapter数据填满list");
143     container_->Add(label);
144     label->SetPosition(TEXT_DISTANCE_TO_LEFT_SIDE, TEXT_DISTANCE_TO_TOP_SIDE);
145 
146     if (adapter1_ == nullptr) {
147         adapter1_ = new TextAdapter();
148     }
149     adapter1_->SetData(adapterData2_);
150 
151     UIList* list = new UIList(UIList::VERTICAL);
152     list->SetIntercept(true);
153     list->SetStyle(STYLE_BACKGROUND_COLOR, Color::Red().full);
154     list->SetPosition(VIEW_DISTANCE_TO_LEFT_SIDE, VIEW_DISTANCE_TO_TOP_SIDE, g_listW,
155                       g_listH - 200); // 200: mean list reduce height
156     list->SetStartIndex(5);           // 5: list start index
157     list->GetStartIndex();
158     list->SetAdapter(adapter1_);
159     list->SetYScrollBarVisible(true);
160     container_->Add(list);
161 }
162 
UIKitListInitTesthalhScreen001()163 void UITestUIList::UIKitListInitTesthalhScreen001()
164 {
165     if (container_ == nullptr) {
166         return;
167     }
168     UILabel* label = GetTitleLabel("UILIst设置adapter数据不填满list");
169     container_->Add(label);
170     // 2: half of screen width
171     label->SetPosition(Screen::GetInstance().GetWidth() / 2 + TEXT_DISTANCE_TO_LEFT_SIDE, TEXT_DISTANCE_TO_TOP_SIDE);
172 
173     if (adapter2_ == nullptr) {
174         adapter2_ = new TextAdapter();
175     }
176     adapter2_->SetData(adapterData1_);
177 
178     UIList* list = new UIList(UIList::VERTICAL);
179     list->SetIntercept(true);
180     list->SetStyle(STYLE_BACKGROUND_COLOR, Color::Red().full);
181     // 2: half of screen width
182     list->SetPosition(Screen::GetInstance().GetWidth() / 2 + VIEW_DISTANCE_TO_LEFT_SIDE, VIEW_DISTANCE_TO_TOP_SIDE,
183                       g_listW, g_listH - 150); // 150: decrease height
184     list->SetAdapter(adapter2_);
185     list->SetYScrollBarVisible(true);
186     container_->Add(list);
187 }
188 
SetControlButton()189 void UITestUIList::SetControlButton()
190 {
191     if (setBlankBtn_ == nullptr) {
192         setBlankBtn_ = new UILabelButton();
193     }
194     if (setBlankOffBtn_ == nullptr) {
195         setBlankOffBtn_ = new UILabelButton();
196     }
197     if (setThrowDragBtn_ == nullptr) {
198         setThrowDragBtn_ = new UILabelButton();
199     }
200     if (setThrowDragOffBtn_ == nullptr) {
201         setThrowDragOffBtn_ = new UILabelButton();
202     }
203     if (setRefreshBtn_ == nullptr) {
204         setRefreshBtn_ = new UILabelButton();
205     }
206     if (setLoopBtn_ == nullptr) {
207         setLoopBtn_ = new UILabelButton();
208     }
209     if (setLoopOffBtn_ == nullptr) {
210         setLoopOffBtn_ = new UILabelButton();
211     }
212     if (setListPaddingBtn_ == nullptr) {
213         setListPaddingBtn_ = new UILabelButton();
214     }
215     if (setListBorderBtn_ == nullptr) {
216         setListBorderBtn_ = new UILabelButton();
217     }
218     if (setRebondSizeBtn_ == nullptr) {
219         setRebondSizeBtn_ = new UILabelButton();
220     }
221     if (setSelectBtn_ == nullptr) {
222         setSelectBtn_ = new UILabelButton();
223     }
224     if (setSelectOffBtn_ == nullptr) {
225         setSelectOffBtn_ = new UILabelButton();
226     }
227     InitButtons();
228     SetUpButtons001();
229 }
230 
InitButtons()231 void UITestUIList::InitButtons()
232 {
233     if (setAutoAlignBtn_ == nullptr) {
234         setAutoAlignBtn_ = new UILabelButton();
235     }
236     if (setAutoAlignOffBtn_ == nullptr) {
237         setAutoAlignOffBtn_ = new UILabelButton();
238     }
239     if (setBackAlignBtn_ == nullptr) {
240         setBackAlignBtn_ = new UILabelButton();
241     }
242     if (setBackAlignOffBtn_ == nullptr) {
243         setBackAlignOffBtn_ = new UILabelButton();
244     }
245     if (setAutoAlignACCIncBtn_ == nullptr) {
246         setAutoAlignACCIncBtn_ = new UILabelButton();
247     }
248     if (setAutoAlignACCDncBtn_ == nullptr) {
249         setAutoAlignACCDncBtn_ = new UILabelButton();
250     }
251     if (setYScrollBarVisableBtn_ == nullptr) {
252         setYScrollBarVisableBtn_ = new UILabelButton();
253     }
254     if (setYScrollBarInvisableBtn_ == nullptr) {
255         setYScrollBarInvisableBtn_ = new UILabelButton();
256     }
257     if (setItemHeightMinBtn_ == nullptr) {
258         setItemHeightMinBtn_ = new UILabelButton();
259     }
260     if (setItemHeightMaxBtn_ == nullptr) {
261         setItemHeightMaxBtn_ = new UILabelButton();
262     }
263     if (setAdapterEmptyBtn_ == nullptr) {
264         setAdapterEmptyBtn_ = new UILabelButton();
265     }
266     if (setSwitchDirectionBtn_ == nullptr) {
267         setSwitchDirectionBtn_ = new UILabelButton();
268     }
269 }
270 
SetUpButtons001()271 void UITestUIList::SetUpButtons001()
272 {
273     positionX_ += 5; // 5: increase y-coordinate
274     SetUpButton(setBlankBtn_, "开启blank");
275     SetUpButton(setBlankOffBtn_, "关闭blank");
276     SetUpButton(setThrowDragBtn_, "开启throwDrag");
277     SetUpButton(setThrowDragOffBtn_, "关闭throwDrag");
278     SetUpButton(setRefreshBtn_, "刷新list");
279     SetUpButton(setLoopBtn_, "开启loop模式");
280     SetUpButton(setLoopOffBtn_, "关闭loop模式");
281     SetUpButton(setListPaddingBtn_, "设置Padding");
282     SetUpButton(setListBorderBtn_, "设置Border");
283     SetUpButton(setRebondSizeBtn_, "设置回弹值");
284     positionX_ = setBlankBtn_->GetX() + setBlankBtn_->GetWidth() + g_blank - VIEW_DISTANCE_TO_LEFT_SIDE;
285     positionY_ = setBlankBtn_->GetY();
286     SetUpButton(setSelectBtn_, "select 150");
287     SetUpButton(setSelectOffBtn_, "关闭select");
288     SetUpButton(setAutoAlignBtn_, "开启自动对齐 ");
289     SetUpButton(setAutoAlignOffBtn_, "关闭自动对齐 ");
290     SetUpButton(setBackAlignBtn_, "开启弹回对齐 ");
291     SetUpButton(setBackAlignOffBtn_, "关闭弹回对齐 ");
292     SetUpButton(setAutoAlignACCIncBtn_, "增加自动对齐时间 ");
293     SetUpButton(setAutoAlignACCDncBtn_, "减少自动对齐时间 ");
294     SetUpButton(setYScrollBarVisableBtn_, "显示纵向滚动条");
295     SetUpButton(setYScrollBarInvisableBtn_, "不显示纵向滚动条");
296     positionX_ = setBlankBtn_->GetX() + setBlankBtn_->GetWidth() + setBlankBtn_->GetWidth() + g_blank + g_blank -
297                  VIEW_DISTANCE_TO_LEFT_SIDE;
298     positionY_ = setBlankBtn_->GetY();
299     SetUpButton(setItemHeightMinBtn_, "设置Item高度为1");
300     SetUpButton(setItemHeightMaxBtn_, "设置Item高度为100");
301     SetUpButton(setAdapterEmptyBtn_, "清空List的元素");
302     SetUpButton(setSwitchDirectionBtn_, "切换List方向");
303 }
304 
UIKitListScrollTestBlankSet001()305 void UITestUIList::UIKitListScrollTestBlankSet001()
306 {
307     if (container_ == nullptr) {
308         return;
309     }
310     UILabel* label = GetTitleLabel("UILIst设置blank和throwDrag");
311     container_->Add(label);
312     label->SetPosition(TEXT_DISTANCE_TO_LEFT_SIDE, g_listH - 132); // 132: decrease y-coordinate
313 
314     InitAdapter4();
315 
316     UIList* list = new UIList(UIList::VERTICAL);
317     list->SetIntercept(true);
318     list->SetStyle(STYLE_BACKGROUND_COLOR, Color::Red().full);
319     list->SetPosition(VIEW_DISTANCE_TO_LEFT_SIDE, g_listH - 95, g_listW, g_listH); // 95: decrease y-coordinate
320     list->SetYScrollBarVisible(true);
321     list->SetAdapter(adapter4_);
322     list->SetScrollStateListener(this);
323     currentList_ = list;
324 
325     if (scrollStateLabel_ == nullptr) {
326         scrollStateLabel_ = new UILabel();
327     }
328     if (scrollSelectLabel_ == nullptr) {
329         scrollSelectLabel_ = new UILabel();
330     }
331 
332     scrollStateLabel_->SetLineBreakMode(UILabel::LINE_BREAK_ADAPT);
333     scrollSelectLabel_->SetLineBreakMode(UILabel::LINE_BREAK_ADAPT);
334     scrollStateLabel_->SetText("STOP");
335     scrollSelectLabel_->SetText("NULL");
336 
337     scrollStateLabel_->SetPosition(list->GetWidth() + 53, list->GetY()); // 53: increase x-coordinate
338     // 53: increase x-coordinate
339     scrollSelectLabel_->SetPosition(list->GetWidth() + scrollStateLabel_->GetWidth() + g_blank + 53, list->GetY());
340     if (list->GetSelectView() != nullptr) {
341         scrollSelectLabel_->SetText(static_cast<UILabel*>(list->GetSelectView())->GetText());
342     }
343     positionX_ = list->GetWidth();
344     positionY_ = scrollStateLabel_->GetY() + scrollStateLabel_->GetHeight();
345     SetControlButton();
346 
347     container_->Add(list);
348     UIView* selectBtnPos = new UIView();
349     selectBtnPos->SetStyle(STYLE_BACKGROUND_OPA, OPA_OPAQUE);
350     selectBtnPos->SetStyle(STYLE_BACKGROUND_COLOR, Color::White().full);
351     selectBtnPos->SetStyle(STYLE_BORDER_WIDTH, 0);
352     selectBtnPos->SetStyle(STYLE_BORDER_RADIUS, 0);
353     selectBtnPos->SetPosition(VIEW_DISTANCE_TO_LEFT_SIDE, list->GetY() + g_selectPos, 100, 1); // 100: mean view width
354     container_->Add(selectBtnPos);
355     container_->Add(scrollStateLabel_);
356     container_->Add(scrollSelectLabel_);
357     SetLastPos(list);
358 }
359 
InitAdapter4()360 void UITestUIList::InitAdapter4()
361 {
362     if (adapter4_ == nullptr) {
363         adapter4_ = new TextAdapter();
364     }
365     adapter4_->SetLineBreakMode(UILabel::LINE_BREAK_CLIP);
366     adapter4_->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
367     adapter4_->SetHeight(100); // 100: mean adapter height
368     adapter4_->SetWidth(100);  // 100: mean adapter width
369     adapter4_->SetData(adapterData2_);
370 }
371 
OnClick(UIView & view,const ClickEvent & event)372 bool UITestUIList::OnClick(UIView& view, const ClickEvent& event)
373 {
374     if (currentList_ == nullptr) {
375         return true;
376     }
377     if (&view == setBlankBtn_) {
378         currentList_->SetScrollBlankSize(g_blankSize);
379     } else if (&view == setBlankOffBtn_) {
380         currentList_->SetScrollBlankSize(0);
381     } else if (&view == setThrowDragBtn_) {
382         currentList_->SetThrowDrag(true);
383     } else if (&view == setThrowDragOffBtn_) {
384         currentList_->SetThrowDrag(false);
385     } else if (&view == setRefreshBtn_) {
386         adapter4_->SetData(adapterData1_);
387         currentList_->RefreshList();
388     } else if (&view == setLoopBtn_) {
389         currentList_->SetLoopState(true);
390     } else if (&view == setLoopOffBtn_) {
391         currentList_->SetLoopState(false);
392     } else if (&view == setListPaddingBtn_) {
393         currentList_->SetStyle(STYLE_PADDING_LEFT, g_padding);
394         currentList_->SetStyle(STYLE_PADDING_RIGHT, g_padding);
395         currentList_->SetStyle(STYLE_PADDING_TOP, g_padding);
396         currentList_->SetStyle(STYLE_PADDING_BOTTOM, g_padding);
397         currentList_->Invalidate();
398     } else if (&view == setListBorderBtn_) {
399         currentList_->SetStyle(STYLE_BORDER_WIDTH, g_border);
400         currentList_->Invalidate();
401     } else if (&view == setRebondSizeBtn_) {
402         currentList_->SetReboundSize(g_reboundSize);
403     } else if (&view == setSelectBtn_) {
404         currentList_->SetSelectPosition(g_selectPos);
405         currentList_->RefreshList();
406     } else if (&view == setSelectOffBtn_) {
407         currentList_->SetSelectPosition(0);
408         currentList_->RefreshList();
409     } else if (&view == setAutoAlignBtn_) {
410         currentList_->EnableAutoAlign(true);
411     } else if (&view == setAutoAlignOffBtn_) {
412         currentList_->EnableAutoAlign(false);
413     } else if (&view == setBackAlignBtn_) {
414         currentList_->EnableCrossDragBack(true);
415     } else if (&view == setBackAlignOffBtn_) {
416         currentList_->EnableCrossDragBack(false);
417     } else if (&view == setAutoAlignACCIncBtn_) {
418         autoAlignTime_ += ALINE_TIME_CHANGE_VALUE;
419         currentList_->SetAutoAlignTime(autoAlignTime_);
420     } else if (&view == setAutoAlignACCDncBtn_) {
421         autoAlignTime_ -= ALINE_TIME_CHANGE_VALUE;
422         currentList_->SetAutoAlignTime(autoAlignTime_);
423     } else if (&view == setYScrollBarVisableBtn_) {
424         currentList_->SetYScrollBarVisible(true);
425     } else if (&view == setYScrollBarInvisableBtn_) {
426         currentList_->SetYScrollBarVisible(false);
427     } else if (&view == setItemHeightMinBtn_) {
428         adapter4_->SetHeight(1);
429         currentList_->RefreshList();
430     } else if (&view == setItemHeightMaxBtn_) {
431         adapter4_->SetHeight(g_itemHeight);
432         currentList_->RefreshList();
433     } else if (&view == setAdapterEmptyBtn_) {
434         adapterData2_->Clear();
435         adapter4_->SetData(adapterData2_);
436         currentList_->RefreshList();
437     } else if (&view == setSwitchDirectionBtn_) {
438         currentList_->SetDirection((currentList_->GetDirection() == UIList::HORIZONTAL) ? 1 : 0);
439         currentList_->RefreshList();
440     }
441     return true;
442 }
443 
OnScrollStart(int16_t index,UIView * view)444 void UITestUIList::OnScrollStart(int16_t index, UIView* view)
445 {
446     if (GetScrollState() == SCROLL_STATE_STOP) {
447         scrollStateLabel_->SetText("STOP");
448     } else {
449         scrollStateLabel_->SetText("MOVE");
450     }
451 
452     scrollStateLabel_->Invalidate();
453 }
454 
OnScrollEnd(int16_t index,UIView * view)455 void UITestUIList::OnScrollEnd(int16_t index, UIView* view)
456 {
457     if (GetScrollState() == SCROLL_STATE_STOP) {
458         scrollStateLabel_->SetText("STOP");
459     } else {
460         scrollStateLabel_->SetText("MOVE");
461     }
462     scrollStateLabel_->Invalidate();
463 }
464 
OnScrollTop(int16_t index,UIView * view)465 void UITestUIList::OnScrollTop(int16_t index, UIView* view)
466 {
467     scrollStateLabel_->SetText("OnTop");
468     scrollStateLabel_->Invalidate();
469 }
470 
OnScrollBottom(int16_t index,UIView * view)471 void UITestUIList::OnScrollBottom(int16_t index, UIView* view)
472 {
473     scrollStateLabel_->SetText("OnBottom");
474     scrollStateLabel_->Invalidate();
475 }
476 
OnItemSelected(int16_t index,UIView * view)477 void UITestUIList::OnItemSelected(int16_t index, UIView* view)
478 {
479     if (view != nullptr) {
480         scrollSelectLabel_->SetText(static_cast<UILabel*>(view)->GetText());
481         view->Invalidate();
482     } else {
483         scrollSelectLabel_->SetText("NULL");
484     }
485 }
486 
SetUpButton(UILabelButton * btn,const char * title)487 void UITestUIList::SetUpButton(UILabelButton* btn, const char* title)
488 {
489     if (btn == nullptr) {
490         return;
491     }
492     container_->Add(btn);
493     btn->SetPosition(positionX_ + VIEW_DISTANCE_TO_LEFT_SIDE, positionY_, BUTTON_WIDHT3, BUTTON_HEIGHT3);
494     positionY_ += btn->GetHeight() + 10; // 10: button interval
495     btn->SetText(title);
496     btn->SetFont(DEFAULT_VECTOR_FONT_FILENAME, BUTTON_LABEL_SIZE);
497     btn->SetOnClickListener(this);
498     btn->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_STYLE_BORDER_RADIUS_VALUE, UIButton::RELEASED);
499     btn->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_STYLE_BORDER_RADIUS_VALUE, UIButton::PRESSED);
500     btn->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_STYLE_BORDER_RADIUS_VALUE, UIButton::INACTIVE);
501     btn->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::RELEASED);
502     btn->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::PRESSED);
503     btn->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::INACTIVE);
504     container_->Invalidate();
505 }
506 
SetLastPos(UIView * view)507 void UITestUIList::SetLastPos(UIView* view)
508 {
509     if (view == nullptr) {
510         return;
511     }
512     lastX_ = view->GetX();
513     lastY_ = view->GetY() + view->GetHeight();
514 }
515 } // namespace OHOS
516