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