• 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_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     if (adapterData1_ != nullptr) {
69         adapterData1_->Clear();
70         delete adapterData1_;
71         adapterData1_ = nullptr;
72     }
73     if (adapterData2_ != nullptr) {
74         adapterData2_->Clear();
75         delete adapterData2_;
76         adapterData2_ = nullptr;
77     }
78     if (adapter1_ != nullptr) {
79         delete adapter1_;
80         adapter1_ = nullptr;
81     }
82     if (adapter2_ != nullptr) {
83         delete adapter2_;
84         adapter2_ = nullptr;
85     }
86     if (adapter3_ != nullptr) {
87         delete adapter3_;
88         adapter3_ = nullptr;
89     }
90     if (adapter4_ != nullptr) {
91         delete adapter4_;
92         adapter4_ = nullptr;
93     }
94     setBlankBtn_ = nullptr;
95     setBlankOffBtn_ = nullptr;
96     setThrowDragBtn_ = nullptr;
97     setThrowDragOffBtn_ = nullptr;
98     scrollStateLabel_ = nullptr;
99     scrollSelectLabel_ = nullptr;
100     setRefreshBtn_ = nullptr;
101     setLoopBtn_ = nullptr;
102     setLoopOffBtn_ = nullptr;
103     setListPaddingBtn_ = nullptr;
104     setListBorderBtn_ = nullptr;
105     setRebondSizeBtn_ = nullptr;
106     setSelectBtn_ = nullptr;
107     setSelectOffBtn_ = nullptr;
108     setAutoAlignBtn_ = nullptr;
109     setAutoAlignOffBtn_ = nullptr;
110     setBackAlignBtn_ = nullptr;
111     setBackAlignOffBtn_ = nullptr;
112     setAutoAlignACCIncBtn_ = nullptr;
113     setAutoAlignACCDncBtn_ = nullptr;
114     setYScrollBarVisableBtn_ = nullptr;
115     setYScrollBarInvisableBtn_ = nullptr;
116     setItemHeightMinBtn_ = nullptr;
117     setItemHeightMaxBtn_ = nullptr;
118     setAdapterEmptyBtn_ = nullptr;
119     setSwitchDirectionBtn_ = nullptr;
120     lastX_ = 0;
121     lastY_ = 0;
122 }
123 
GetTestView()124 const UIView* UITestUIList::GetTestView()
125 {
126     UIKitListInitTestFullScreen001();
127     UIKitListInitTesthalhScreen001();
128     UIKitListScrollTestBlankSet001();
129     return container_;
130 }
131 
UIKitListInitTestFullScreen001()132 void UITestUIList::UIKitListInitTestFullScreen001()
133 {
134     if (container_ == nullptr) {
135         return;
136     }
137     UILabel* label = GetTitleLabel("UILIst设置adapter数据填满list");
138     container_->Add(label);
139     label->SetPosition(TEXT_DISTANCE_TO_LEFT_SIDE, TEXT_DISTANCE_TO_TOP_SIDE);
140 
141     if (adapter1_ == nullptr) {
142         adapter1_ = new TextAdapter();
143     }
144     adapter1_->SetData(adapterData2_);
145 
146     UIList* list = new UIList(UIList::VERTICAL);
147     list->SetIntercept(true);
148     list->SetStyle(STYLE_BACKGROUND_COLOR, Color::Red().full);
149     list->SetPosition(VIEW_DISTANCE_TO_LEFT_SIDE, VIEW_DISTANCE_TO_TOP_SIDE, g_listW,
150                       g_listH - 200); // 200: mean list reduce height
151     list->SetStartIndex(5);           // 5: list start index
152     list->GetStartIndex();
153     list->SetAdapter(adapter1_);
154     list->SetYScrollBarVisible(true);
155     container_->Add(list);
156 }
157 
UIKitListInitTesthalhScreen001()158 void UITestUIList::UIKitListInitTesthalhScreen001()
159 {
160     if (container_ == nullptr) {
161         return;
162     }
163     UILabel* label = GetTitleLabel("UILIst设置adapter数据不填满list");
164     container_->Add(label);
165     // 2: half of screen width
166     label->SetPosition(Screen::GetInstance().GetWidth() / 2 + TEXT_DISTANCE_TO_LEFT_SIDE, TEXT_DISTANCE_TO_TOP_SIDE);
167 
168     if (adapter2_ == nullptr) {
169         adapter2_ = new TextAdapter();
170     }
171     adapter2_->SetData(adapterData1_);
172 
173     UIList* list = new UIList(UIList::VERTICAL);
174     list->SetIntercept(true);
175     list->SetStyle(STYLE_BACKGROUND_COLOR, Color::Red().full);
176     // 2: half of screen width
177     list->SetPosition(Screen::GetInstance().GetWidth() / 2 + VIEW_DISTANCE_TO_LEFT_SIDE, VIEW_DISTANCE_TO_TOP_SIDE,
178                       g_listW, g_listH - 150); // 150: decrease height
179     list->SetAdapter(adapter2_);
180     list->SetYScrollBarVisible(true);
181     container_->Add(list);
182 }
183 
SetControlButton()184 void UITestUIList::SetControlButton()
185 {
186     if (setBlankBtn_ == nullptr) {
187         setBlankBtn_ = new UILabelButton();
188     }
189     if (setBlankOffBtn_ == nullptr) {
190         setBlankOffBtn_ = new UILabelButton();
191     }
192     if (setThrowDragBtn_ == nullptr) {
193         setThrowDragBtn_ = new UILabelButton();
194     }
195     if (setThrowDragOffBtn_ == nullptr) {
196         setThrowDragOffBtn_ = new UILabelButton();
197     }
198     if (setRefreshBtn_ == nullptr) {
199         setRefreshBtn_ = new UILabelButton();
200     }
201     if (setLoopBtn_ == nullptr) {
202         setLoopBtn_ = new UILabelButton();
203     }
204     if (setLoopOffBtn_ == nullptr) {
205         setLoopOffBtn_ = new UILabelButton();
206     }
207     if (setListPaddingBtn_ == nullptr) {
208         setListPaddingBtn_ = new UILabelButton();
209     }
210     if (setListBorderBtn_ == nullptr) {
211         setListBorderBtn_ = new UILabelButton();
212     }
213     if (setRebondSizeBtn_ == nullptr) {
214         setRebondSizeBtn_ = new UILabelButton();
215     }
216     if (setSelectBtn_ == nullptr) {
217         setSelectBtn_ = new UILabelButton();
218     }
219     if (setSelectOffBtn_ == nullptr) {
220         setSelectOffBtn_ = new UILabelButton();
221     }
222     if (setAutoAlignBtn_ == nullptr) {
223         setAutoAlignBtn_ = new UILabelButton();
224     }
225     if (setAutoAlignOffBtn_ == nullptr) {
226         setAutoAlignOffBtn_ = new UILabelButton();
227     }
228     if (setBackAlignBtn_ == nullptr) {
229         setBackAlignBtn_ = new UILabelButton();
230     }
231     if (setBackAlignOffBtn_ == nullptr) {
232         setBackAlignOffBtn_ = new UILabelButton();
233     }
234     if (setAutoAlignACCIncBtn_ == nullptr) {
235         setAutoAlignACCIncBtn_ = new UILabelButton();
236     }
237     if (setAutoAlignACCDncBtn_ == nullptr) {
238         setAutoAlignACCDncBtn_ = new UILabelButton();
239     }
240     if (setYScrollBarVisableBtn_ == nullptr) {
241         setYScrollBarVisableBtn_ = new UILabelButton();
242     }
243     if (setYScrollBarInvisableBtn_ == nullptr) {
244         setYScrollBarInvisableBtn_ = new UILabelButton();
245     }
246     if (setItemHeightMinBtn_ == nullptr) {
247         setItemHeightMinBtn_ = new UILabelButton();
248     }
249     if (setItemHeightMaxBtn_ == nullptr) {
250         setItemHeightMaxBtn_ = new UILabelButton();
251     }
252     if (setAdapterEmptyBtn_ == nullptr) {
253         setAdapterEmptyBtn_ = new UILabelButton();
254     }
255     if (setSwitchDirectionBtn_ == nullptr) {
256         setSwitchDirectionBtn_ = new UILabelButton();
257     }
258     positionX_ += 5; // 5: increase y-coordinate
259     SetUpButton(setBlankBtn_, "开启blank");
260     SetUpButton(setBlankOffBtn_, "关闭blank");
261     SetUpButton(setThrowDragBtn_, "开启throwDrag");
262     SetUpButton(setThrowDragOffBtn_, "关闭throwDrag");
263     SetUpButton(setRefreshBtn_, "刷新list");
264     SetUpButton(setLoopBtn_, "开启loop模式");
265     SetUpButton(setLoopOffBtn_, "关闭loop模式");
266     SetUpButton(setListPaddingBtn_, "设置Padding");
267     SetUpButton(setListBorderBtn_, "设置Border");
268     SetUpButton(setRebondSizeBtn_, "设置回弹值");
269     positionX_ = setBlankBtn_->GetX() + setBlankBtn_->GetWidth() + g_blank - VIEW_DISTANCE_TO_LEFT_SIDE;
270     positionY_ = setBlankBtn_->GetY();
271     SetUpButton(setSelectBtn_, "select 150");
272     SetUpButton(setSelectOffBtn_, "关闭select");
273     SetUpButton(setAutoAlignBtn_, "开启自动对齐 ");
274     SetUpButton(setAutoAlignOffBtn_, "关闭自动对齐 ");
275     SetUpButton(setBackAlignBtn_, "开启弹回对齐 ");
276     SetUpButton(setBackAlignOffBtn_, "关闭弹回对齐 ");
277     SetUpButton(setAutoAlignACCIncBtn_, "增加自动对齐时间 ");
278     SetUpButton(setAutoAlignACCDncBtn_, "减少自动对齐时间 ");
279     SetUpButton(setYScrollBarVisableBtn_, "显示纵向滚动条");
280     SetUpButton(setYScrollBarInvisableBtn_, "不显示纵向滚动条");
281     positionX_ = setBlankBtn_->GetX() + setBlankBtn_->GetWidth() + setBlankBtn_->GetWidth() + g_blank + g_blank -
282                  VIEW_DISTANCE_TO_LEFT_SIDE;
283     positionY_ = setBlankBtn_->GetY();
284     SetUpButton(setItemHeightMinBtn_, "设置Item高度为1");
285     SetUpButton(setItemHeightMaxBtn_, "设置Item高度为100");
286     SetUpButton(setAdapterEmptyBtn_, "清空List的元素");
287     SetUpButton(setSwitchDirectionBtn_, "切换List方向");
288 }
289 
UIKitListScrollTestBlankSet001()290 void UITestUIList::UIKitListScrollTestBlankSet001()
291 {
292     if (container_ == nullptr) {
293         return;
294     }
295     UILabel* label = GetTitleLabel("UILIst设置blank和throwDrag");
296     container_->Add(label);
297     label->SetPosition(TEXT_DISTANCE_TO_LEFT_SIDE, g_listH - 132); // 132: decrease y-coordinate
298 
299     if (adapter4_ == nullptr) {
300         adapter4_ = new TextAdapter();
301     }
302     adapter4_->SetLineBreakMode(UILabel::LINE_BREAK_CLIP);
303     adapter4_->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
304     adapter4_->SetHeight(100); // 100: mean adapter height
305     adapter4_->SetWidth(100);  // 100: mean adapter width
306     adapter4_->SetData(adapterData2_);
307 
308     UIList* list = new UIList(UIList::VERTICAL);
309     list->SetIntercept(true);
310     list->SetStyle(STYLE_BACKGROUND_COLOR, Color::Red().full);
311     list->SetPosition(VIEW_DISTANCE_TO_LEFT_SIDE, g_listH - 95, g_listW, g_listH); // 95: decrease y-coordinate
312     list->SetYScrollBarVisible(true);
313     list->SetAdapter(adapter4_);
314     list->SetScrollStateListener(this);
315     currentList_ = list;
316 
317     if (scrollStateLabel_ == nullptr) {
318         scrollStateLabel_ = new UILabel();
319     }
320     if (scrollSelectLabel_ == nullptr) {
321         scrollSelectLabel_ = new UILabel();
322     }
323 
324     scrollStateLabel_->SetLineBreakMode(UILabel::LINE_BREAK_ADAPT);
325     scrollSelectLabel_->SetLineBreakMode(UILabel::LINE_BREAK_ADAPT);
326     scrollStateLabel_->SetText("STOP");
327     scrollSelectLabel_->SetText("NULL");
328 
329     scrollStateLabel_->SetPosition(list->GetWidth() + 53, list->GetY()); // 53: increase x-coordinate
330     // 53: increase x-coordinate
331     scrollSelectLabel_->SetPosition(list->GetWidth() + scrollStateLabel_->GetWidth() + g_blank + 53, list->GetY());
332     if (list->GetSelectView() != nullptr) {
333         scrollSelectLabel_->SetText(static_cast<UILabel*>(list->GetSelectView())->GetText());
334     }
335     positionX_ = list->GetWidth();
336     positionY_ = scrollStateLabel_->GetY() + scrollStateLabel_->GetHeight();
337     SetControlButton();
338 
339     container_->Add(list);
340     UIView* selectBtnPos = new UIView();
341     selectBtnPos->SetStyle(STYLE_BACKGROUND_OPA, OPA_OPAQUE);
342     selectBtnPos->SetStyle(STYLE_BACKGROUND_COLOR, Color::White().full);
343     selectBtnPos->SetStyle(STYLE_BORDER_WIDTH, 0);
344     selectBtnPos->SetStyle(STYLE_BORDER_RADIUS, 0);
345     selectBtnPos->SetPosition(VIEW_DISTANCE_TO_LEFT_SIDE, list->GetY() + g_selectPos, 100, 1); // 100: mean view width
346     container_->Add(selectBtnPos);
347     container_->Add(scrollStateLabel_);
348     container_->Add(scrollSelectLabel_);
349     SetLastPos(list);
350 }
351 
OnClick(UIView & view,const ClickEvent & event)352 bool UITestUIList::OnClick(UIView& view, const ClickEvent& event)
353 {
354     if (currentList_ == nullptr) {
355         return true;
356     }
357     if (&view == setBlankBtn_) {
358         currentList_->SetScrollBlankSize(g_blankSize);
359     } else if (&view == setBlankOffBtn_) {
360         currentList_->SetScrollBlankSize(0);
361     } else if (&view == setThrowDragBtn_) {
362         currentList_->SetThrowDrag(true);
363     } else if (&view == setThrowDragOffBtn_) {
364         currentList_->SetThrowDrag(false);
365     } else if (&view == setRefreshBtn_) {
366         adapter4_->SetData(adapterData1_);
367         currentList_->RefreshList();
368     } else if (&view == setLoopBtn_) {
369         currentList_->SetLoopState(true);
370     } else if (&view == setLoopOffBtn_) {
371         currentList_->SetLoopState(false);
372     } else if (&view == setListPaddingBtn_) {
373         currentList_->SetStyle(STYLE_PADDING_LEFT, g_padding);
374         currentList_->SetStyle(STYLE_PADDING_RIGHT, g_padding);
375         currentList_->SetStyle(STYLE_PADDING_TOP, g_padding);
376         currentList_->SetStyle(STYLE_PADDING_BOTTOM, g_padding);
377         currentList_->Invalidate();
378     } else if (&view == setListBorderBtn_) {
379         currentList_->SetStyle(STYLE_BORDER_WIDTH, g_border);
380         currentList_->Invalidate();
381     } else if (&view == setRebondSizeBtn_) {
382         currentList_->SetReboundSize(g_reboundSize);
383     } else if (&view == setSelectBtn_) {
384         currentList_->SetSelectPosition(g_selectPos);
385         currentList_->RefreshList();
386     } else if (&view == setSelectOffBtn_) {
387         currentList_->SetSelectPosition(0);
388         currentList_->RefreshList();
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
496