• 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_button.h"
17 
18 #include "common/screen.h"
19 #include "components/root_view.h"
20 #include "components/ui_digital_clock.h"
21 #include "components/ui_label.h"
22 #include "components/ui_label_button.h"
23 #include "components/ui_radio_button.h"
24 #include "components/ui_toggle_button.h"
25 #include "font/ui_font.h"
26 #include "test_resource_config.h"
27 
28 namespace OHOS {
SetUp()29 void UITestBUTTON::SetUp()
30 {
31     if (container_ == nullptr) {
32         container_ = new UIScrollView();
33         container_->Resize(Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight() - BACK_BUTTON_HEIGHT);
34         container_->SetHorizontalScrollState(false);
35     }
36 }
37 
TearDown()38 void UITestBUTTON::TearDown()
39 {
40     if (checkBoxChangeListener_ != nullptr) {
41         delete checkBoxChangeListener_;
42         checkBoxChangeListener_ = nullptr;
43     }
44     if (checkBoxChangeListener1_ != nullptr) {
45         delete checkBoxChangeListener1_;
46         checkBoxChangeListener1_ = nullptr;
47     }
48     if (checkBoxChangeListener2_ != nullptr) {
49         delete checkBoxChangeListener2_;
50         checkBoxChangeListener2_ = nullptr;
51     }
52     if (radioChangeListener_ != nullptr) {
53         delete radioChangeListener_;
54         radioChangeListener_ = nullptr;
55     }
56     if (radioChangeListener1_ != nullptr) {
57         delete radioChangeListener1_;
58         radioChangeListener1_ = nullptr;
59     }
60     if (radioChangeListener2_ != nullptr) {
61         delete radioChangeListener2_;
62         radioChangeListener2_ = nullptr;
63     }
64     if (toggleChangeListener_ != nullptr) {
65         delete toggleChangeListener_;
66         toggleChangeListener_ = nullptr;
67     }
68     if (toggleChangeListener1_ != nullptr) {
69         delete toggleChangeListener1_;
70         toggleChangeListener1_ = nullptr;
71     }
72     if (toggleChangeListener2_ != nullptr) {
73         delete toggleChangeListener2_;
74         toggleChangeListener2_ = nullptr;
75     }
76     if (clickEnableVisiableListener_ != nullptr) {
77         delete clickEnableVisiableListener_;
78         clickEnableVisiableListener_ = nullptr;
79     }
80     if (clickDisableVisiableListener_ != nullptr) {
81         delete clickDisableVisiableListener_;
82         clickDisableVisiableListener_ = nullptr;
83     }
84     if (clickEnableTouchableListener_ != nullptr) {
85         delete clickEnableTouchableListener_;
86         clickEnableTouchableListener_ = nullptr;
87     }
88     if (clickDisableTouchableListener_ != nullptr) {
89         delete clickDisableTouchableListener_;
90         clickDisableTouchableListener_ = nullptr;
91     }
92     if (clickColorToWhiteListener_ != nullptr) {
93         delete clickColorToWhiteListener_;
94         clickColorToWhiteListener_ = nullptr;
95     }
96     if (clickColorToRedListener_ != nullptr) {
97         delete clickColorToRedListener_;
98         clickColorToRedListener_ = nullptr;
99     }
100     if (clickRevetColorListener_ != nullptr) {
101         delete clickRevetColorListener_;
102         clickRevetColorListener_ = nullptr;
103     }
104     if (clickRevetToOriginListener_ != nullptr) {
105         delete clickRevetToOriginListener_;
106         clickRevetToOriginListener_ = nullptr;
107     }
108     if (clickBigListener_ != nullptr) {
109         delete clickBigListener_;
110         clickBigListener_ = nullptr;
111     }
112     if (clickLeftListener_ != nullptr) {
113         delete clickLeftListener_;
114         clickLeftListener_ = nullptr;
115     }
116     if (clickRightListener_ != nullptr) {
117         delete clickRightListener_;
118         clickRightListener_ = nullptr;
119     }
120     if (clickUpListener_ != nullptr) {
121         delete clickUpListener_;
122         clickUpListener_ = nullptr;
123     }
124     if (clickDownListener_ != nullptr) {
125         delete clickDownListener_;
126         clickDownListener_ = nullptr;
127     }
128     if (clickSmallListener_ != nullptr) {
129         delete clickSmallListener_;
130         clickSmallListener_ = nullptr;
131     }
132     if (enableAnimationListener_ != nullptr) {
133         delete enableAnimationListener_;
134         enableAnimationListener_ = nullptr;
135     }
136     if (disableAnimationListener_ != nullptr) {
137         delete disableAnimationListener_;
138         disableAnimationListener_ = nullptr;
139     }
140     DeleteChildren(container_);
141     container_ = nullptr;
142 }
143 
144 class TestBtnOnStateChangeListener : public OHOS::UICheckBox::OnChangeListener {
145 public:
TestBtnOnStateChangeListener(UILabel * uiLabel)146     explicit TestBtnOnStateChangeListener(UILabel* uiLabel) : uiLabel_(uiLabel) {}
147 
~TestBtnOnStateChangeListener()148     ~TestBtnOnStateChangeListener() {}
149 
OnChange(UICheckBox::UICheckBoxState state)150     bool OnChange(UICheckBox::UICheckBoxState state) override
151     {
152         if (state == UICheckBox::UICheckBoxState::SELECTED) {
153             uiLabel_->SetText("ON");
154         } else {
155             uiLabel_->SetText("OFF");
156         }
157         uiLabel_->Invalidate();
158         return true;
159     }
160 
161 private:
162     UILabel* uiLabel_;
163 };
164 
GetTestView()165 const UIView* UITestBUTTON::GetTestView()
166 {
167     UIKitCheckBoxTest001();
168     UIKitRadioButtonTest001();
169     UIKitToggleButtonTest001();
170     UIKitCheckBoxTest002();
171     UIKitRadioButtonTest002();
172     UIKitToggleButtonTest002();
173     UIKitButtonTest001();
174     return container_;
175 }
176 
CreateButtonGroup(int16_t posX,int16_t posY,int16_t width,int16_t height,UICheckBox::OnChangeListener ** listener,UIViewType type,const char * name)177 UIViewGroup* UITestBUTTON::CreateButtonGroup(int16_t posX,
178                                              int16_t posY,
179                                              int16_t width,
180                                              int16_t height,
181                                              UICheckBox::OnChangeListener** listener,
182                                              UIViewType type,
183                                              const char* name)
184 {
185     UIViewGroup* group = new UIViewGroup();
186     group->SetPosition(posX, posY, 300, 150); // 300: width, 150: height
187     UILabel* label = new UILabel();
188     group->Add(label);
189     label->SetPosition(10, 30, 80, 20); // 10: posX 30 posY 80 width 20 height
190     label->SetText("State: ");
191 
192     UILabel* label1 = new UILabel();
193     group->Add(label1);
194     label1->SetPosition(70, 30, 40, 20); // 70: posX 30 posY 40 width 20 height
195     *listener = static_cast<UICheckBox::OnChangeListener*>(new TestBtnOnStateChangeListener(label1));
196 
197     UICheckBox* checkBox = nullptr;
198     if (type == UIViewType::UI_TOGGLE_BUTTON) {
199         checkBox = static_cast<UICheckBox*>(new UIToggleButton());
200     } else if (type == UIViewType::UI_RADIO_BUTTON) {
201         checkBox = static_cast<UICheckBox*>(new UIRadioButton(name));
202     } else {
203         checkBox = new UICheckBox();
204         checkBox->SetImages("", "");
205     }
206     group->Add(checkBox);
207     checkBox->SetOnChangeListener(*listener);
208     checkBox->SetPosition(100, 0, width, height); // 100: posX 0: posY
209     if (checkBox->GetState() == UICheckBox::SELECTED) {
210         label1->SetText("ON");
211     } else {
212         label1->SetText("OFF");
213     }
214     return group;
215 }
216 
UIKitCheckBoxTest001()217 void UITestBUTTON::UIKitCheckBoxTest001()
218 {
219     if (container_ != nullptr) {
220         UILabel* label = new UILabel();
221         container_->Add(label);
222         label->SetPosition(TEXT_DISTANCE_TO_LEFT_SIDE, 5, // 5: y-coordinate
223                            Screen::GetInstance().GetWidth(), TITLE_LABEL_DEFAULT_HEIGHT);
224         label->SetText("checkbox功能");
225         label->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
226         // 40: posX 35 posY 100 width 100 height
227         UIViewGroup* group = CreateButtonGroup(40, 35, 100, 100, &checkBoxChangeListener_);
228         // 250: posX 35 posY 100 width 100 height
229         UIViewGroup* group1 = CreateButtonGroup(250, 35, 100, 100, &checkBoxChangeListener1_);
230         // 500: posX 35 posY 100 width 100 height
231         UIViewGroup* group2 = CreateButtonGroup(500, 35, 100, 100, &checkBoxChangeListener2_);
232         container_->Add(group);
233         container_->Add(group1);
234         container_->Add(group2);
235     }
236 }
237 
UIKitRadioButtonTest001()238 void UITestBUTTON::UIKitRadioButtonTest001()
239 {
240     if (container_ != nullptr) {
241         UILabel* label = new UILabel();
242         container_->Add(label);
243         // 0:x-coordinate, 130:y-coordinate, 20:new height
244         label->SetPosition(TEXT_DISTANCE_TO_LEFT_SIDE, 130, Screen::GetInstance().GetWidth(),
245                            TITLE_LABEL_DEFAULT_HEIGHT);
246         label->SetText("radiobutton功能");
247         label->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
248         // 40: posX 160 posY 100 width 100 height
249         UIViewGroup* group =
250             CreateButtonGroup(40, 160, 100, 100, &radioChangeListener_, UIViewType::UI_RADIO_BUTTON, "bb");
251         // 250: posX 160 posY 100 width 100 height
252         UIViewGroup* group1 =
253             CreateButtonGroup(250, 160, 100, 100, &radioChangeListener1_, UIViewType::UI_RADIO_BUTTON, "bb");
254         // 500: posX 160 posY 100 width 100 height
255         UIViewGroup* group2 =
256             CreateButtonGroup(500, 160, 100, 100, &radioChangeListener2_, UIViewType::UI_RADIO_BUTTON, "bb");
257         container_->Add(group);
258         container_->Add(group1);
259         container_->Add(group2);
260     }
261 }
262 
UIKitToggleButtonTest001()263 void UITestBUTTON::UIKitToggleButtonTest001()
264 {
265     if (container_ != nullptr) {
266         UILabel* label = new UILabel();
267         container_->Add(label);
268         // 0:x-coordinate, 270:y-coordinate, 20:new height
269         label->SetPosition(TEXT_DISTANCE_TO_LEFT_SIDE, 270, Screen::GetInstance().GetWidth(),
270                            TITLE_LABEL_DEFAULT_HEIGHT);
271         label->SetText("togglebutton功能");
272         label->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
273 
274         // 40: posX 300 posY 100 width 100 height
275         UIViewGroup* group = CreateButtonGroup(40, 300, 100, 100, &toggleChangeListener_, UIViewType::UI_TOGGLE_BUTTON);
276         // 250: posX 300 posY 100 width 100 height
277         UIViewGroup* group1 =
278             CreateButtonGroup(250, 300, 100, 100, &toggleChangeListener1_, UIViewType::UI_TOGGLE_BUTTON);
279         // 500: posX 300 posY 100 width 100 height
280         UIViewGroup* group2 =
281             CreateButtonGroup(500, 300, 100, 100, &toggleChangeListener2_, UIViewType::UI_TOGGLE_BUTTON);
282         container_->Add(group);
283         container_->Add(group1);
284         container_->Add(group2);
285     }
286 }
UIKitCheckBoxTest002() const287 void UITestBUTTON::UIKitCheckBoxTest002() const
288 {
289     if (container_ != nullptr) {
290         UILabel* label = new UILabel();
291         container_->Add(label);
292         // 0:x-coordinate, 400:y-coordinate, 20:new height
293         label->SetPosition(TEXT_DISTANCE_TO_LEFT_SIDE, 400, Screen::GetInstance().GetWidth(),
294                            TITLE_LABEL_DEFAULT_HEIGHT);
295         label->SetText("checkbox SetImage功能");
296         label->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
297 
298         UICheckBox* checkbox = new UICheckBox();
299         checkbox->SetPosition(30, 430); // 30: x-coordinate, 430: y-coordinate
300         checkbox->SetWidth(130);        // 130: width
301         checkbox->SetHeight(130);       // 130: height
302         checkbox->SetImages(BLUE_IMAGE_PATH, YELLOW_IMAGE_PATH);
303 
304         UICheckBox* checkbox2 = new UICheckBox();
305         checkbox2->SetPosition(200, 430); // 200: x-coordinate, 430: y-coordinate
306         checkbox2->SetWidth(130);         // 130: width
307         checkbox2->SetHeight(130);        // 130: height
308         checkbox2->SetImages(GREEN_IMAGE_PATH, RED_IMAGE_PATH);
309 
310         UICheckBox* checkbox3 = new UICheckBox();
311         checkbox3->SetPosition(350, 430); // 350: x-coordinate, 430: y-coordinate
312 
313         UICheckBox* checkbox4 = new UICheckBox();
314         checkbox4->SetPosition(450, 430); // 450: x-coordinate, 430: y-coordinate
315 
316         container_->Add(checkbox);
317         container_->Add(checkbox2);
318         container_->Add(checkbox3);
319         container_->Add(checkbox4);
320     }
321 }
322 
UIKitRadioButtonTest002() const323 void UITestBUTTON::UIKitRadioButtonTest002() const
324 {
325     if (container_ != nullptr) {
326         UILabel* label = new UILabel();
327         container_->Add(label);
328         // 0:x-coordinate, 570:y-coordinate, 20:new height
329         label->SetPosition(TEXT_DISTANCE_TO_LEFT_SIDE, 570, Screen::GetInstance().GetWidth(),
330                            TITLE_LABEL_DEFAULT_HEIGHT);
331         label->SetText("radiobutton SetImage功能");
332         label->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
333 
334         UIRadioButton* radioButton = new UIRadioButton("aaa");
335         radioButton->SetPosition(30, 10); // 30: x-coordinate, 10: y-coordinate
336         radioButton->SetWidth(130);       // 130: width
337         radioButton->SetHeight(130);      // 130: height
338         radioButton->SetImages(BLUE_IMAGE_PATH, YELLOW_IMAGE_PATH);
339 
340         UIRadioButton* radioButton2 = new UIRadioButton("aaa");
341         radioButton2->SetPosition(200, 10); // 200: x-coordinate, 10: y-coordinate
342         radioButton2->SetWidth(130);        // 130: width
343         radioButton2->SetHeight(130);       // 130: height
344         radioButton2->SetImages(GREEN_IMAGE_PATH, RED_IMAGE_PATH);
345 
346         OHOS::UIViewGroup* viewGroup = new UIViewGroup();
347         viewGroup->SetPosition(0, 600);                              // 0: x-coordinate, 600: y-coordinate
348         viewGroup->SetWidth(Screen::GetInstance().GetWidth());       // 2: half width
349         viewGroup->SetHeight(Screen::GetInstance().GetHeight() / 2); // 2: half height
350 
351         viewGroup->Add(radioButton);
352         viewGroup->Add(radioButton2);
353         viewGroup->SetStyle(STYLE_BACKGROUND_OPA, 0);
354 
355         container_->Add(viewGroup);
356     }
357 }
358 
UIKitToggleButtonTest002()359 void UITestBUTTON::UIKitToggleButtonTest002()
360 {
361     if (container_ != nullptr) {
362         UILabel* label = new UILabel();
363         container_->Add(label);
364         // 0:x-coordinate, 740:y-coordinate, 20:new height
365         label->SetPosition(TEXT_DISTANCE_TO_LEFT_SIDE, 740, Screen::GetInstance().GetWidth(),
366                            TITLE_LABEL_DEFAULT_HEIGHT);
367         label->SetText("togglebutton SetImage功能");
368         label->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
369 
370         UIToggleButton* togglebutton = new UIToggleButton();
371         togglebutton->SetPosition(30, 770);              // 30: x-coordinate, 770: y-coordinate
372         togglebutton->SetStyle(STYLE_BACKGROUND_OPA, 0); // 0: opacity
373         togglebutton->SetState(true);
374         togglebutton->SetWidth(130);  // 130: width
375         togglebutton->SetHeight(130); // 130: height
376         togglebutton->SetImages(BLUE_IMAGE_PATH, YELLOW_IMAGE_PATH);
377 
378         UIToggleButton* togglebutton2 = new UIToggleButton();
379         togglebutton2->SetPosition(200, 770);             // 200: x-coordinate, 770: y-coordinate
380         togglebutton2->SetStyle(STYLE_BACKGROUND_OPA, 0); // 0: opacity
381         togglebutton2->SetState(false);
382         togglebutton2->SetWidth(130);  // 130: width
383         togglebutton2->SetHeight(130); // 130: height
384         togglebutton2->SetImages(GREEN_IMAGE_PATH, RED_IMAGE_PATH);
385 
386         container_->Add(togglebutton);
387         container_->Add(togglebutton2);
388     }
389 }
390 
391 class TestBtnOnClickShapeChangeListener : public UIView::OnClickListener {
392 public:
TestBtnOnClickShapeChangeListener(UIView * uiView,int16_t grid)393     TestBtnOnClickShapeChangeListener(UIView* uiView, int16_t grid) : uiView_(uiView), changeGrid_(grid) {}
394 
~TestBtnOnClickShapeChangeListener()395     ~TestBtnOnClickShapeChangeListener() {}
396 
OnClick(UIView & view,const ClickEvent & event)397     bool OnClick(UIView& view, const ClickEvent& event) override
398     {
399         int16_t width = uiView_->GetWidth() + changeGrid_;
400         int16_t height = uiView_->GetHeight() + changeGrid_;
401         if (changeGrid_ < 0) {
402             uiView_->Invalidate();
403         }
404         uiView_->Resize(width, height);
405         uiView_->Invalidate();
406         return true;
407     }
408 
409 private:
410     UIView* uiView_;
411     int16_t changeGrid_;
412 };
413 
414 class TestBtnOnClickPositionChangeListener : public OHOS::UIView::OnClickListener {
415 public:
416     enum class MoveType {
417         MOVE_LEFT,
418         MOVE_RIGHT,
419         MOVE_TOP,
420         MOVE_BOTTOM,
421     };
TestBtnOnClickPositionChangeListener(UIView * uiView,MoveType moveType,uint16_t grid)422     TestBtnOnClickPositionChangeListener(UIView* uiView, MoveType moveType, uint16_t grid)
423         : uiView_(uiView), moveType_(moveType), grid_(grid)
424     {
425     }
426 
~TestBtnOnClickPositionChangeListener()427     ~TestBtnOnClickPositionChangeListener() {}
428 
OnClick(UIView & view,const ClickEvent & event)429     bool OnClick(UIView& view, const ClickEvent& event) override
430     {
431         int16_t xPos = uiView_->GetX();
432         int16_t yPos = uiView_->GetY();
433         uiView_->Invalidate();
434         if (moveType_ == MoveType::MOVE_LEFT) {
435             xPos = xPos - grid_;
436         } else if (moveType_ == MoveType::MOVE_RIGHT) {
437             xPos = xPos + grid_;
438         } else if (moveType_ == MoveType::MOVE_TOP) {
439             yPos = yPos - grid_;
440         } else if (moveType_ == MoveType::MOVE_BOTTOM) {
441             yPos = yPos + grid_;
442         }
443         uiView_->SetX(xPos);
444         uiView_->SetY(yPos);
445         uiView_->Invalidate();
446         return true;
447     }
448 
449 private:
450     UIView* uiView_;
451     MoveType moveType_;
452     uint16_t grid_;
453 };
454 
455 class TestBtnOnClickVisableListener : public OHOS::UIView::OnClickListener {
456 public:
TestBtnOnClickVisableListener(UIView * uiView,bool visible)457     TestBtnOnClickVisableListener(UIView* uiView, bool visible) : uiView_(uiView), visible_(visible) {}
458 
~TestBtnOnClickVisableListener()459     ~TestBtnOnClickVisableListener() {}
460 
OnClick(UIView & view,const ClickEvent & event)461     bool OnClick(UIView& view, const ClickEvent& event) override
462     {
463         uiView_->SetVisible(visible_);
464         uiView_->Invalidate();
465         return true;
466     }
467 
468 private:
469     UIView* uiView_;
470     bool visible_;
471 };
472 
473 class TestBtnOnClickTouchableListener : public UIView::OnClickListener {
474 public:
TestBtnOnClickTouchableListener(UIView * uiView,bool touchable)475     TestBtnOnClickTouchableListener(UIView* uiView, bool touchable) : uiView_(uiView), touchable_(touchable) {}
476 
~TestBtnOnClickTouchableListener()477     ~TestBtnOnClickTouchableListener() {}
478 
OnClick(UIView & view,const ClickEvent & event)479     bool OnClick(UIView& view, const ClickEvent& event) override
480     {
481         uiView_->SetTouchable(touchable_);
482         uiView_->Invalidate();
483         return true;
484     }
485 
486 private:
487     UIView* uiView_;
488     bool touchable_;
489 };
490 
491 class TestBtnOnClickChangeColorListener : public OHOS::UIView::OnClickListener {
492 public:
TestBtnOnClickChangeColorListener(UIView * uiView,uint16_t red,uint16_t green,uint16_t blue)493     TestBtnOnClickChangeColorListener(UIView* uiView, uint16_t red, uint16_t green, uint16_t blue)
494         : uiView_(uiView), red_(red), green_(green), blue_(blue)
495     {
496     }
497 
~TestBtnOnClickChangeColorListener()498     ~TestBtnOnClickChangeColorListener() {}
499 
OnClick(UIView & view,const ClickEvent & event)500     bool OnClick(UIView& view, const ClickEvent& event) override
501     {
502         ColorType color;
503         color.full = uiView_->GetStyle(STYLE_BACKGROUND_COLOR);
504         color.red = red_;
505         color.green = green_;
506         color.blue = blue_;
507         uiView_->SetStyle(STYLE_BACKGROUND_COLOR, color.full);
508         uiView_->Invalidate();
509         return true;
510     }
511 
512 private:
513     UIView* uiView_;
514     uint16_t red_;
515     uint16_t green_;
516     uint16_t blue_;
517 };
518 
519 class TestBtnOnClickRevertColorListener : public UIView::OnClickListener {
520 public:
TestBtnOnClickRevertColorListener(UIView * uiView)521     explicit TestBtnOnClickRevertColorListener(UIView* uiView) : uiView_(uiView), originColor_(0)
522     {
523         if (uiView != nullptr) {
524             originColor_ = uiView->GetStyle(STYLE_BACKGROUND_COLOR);
525         }
526     }
527 
~TestBtnOnClickRevertColorListener()528     ~TestBtnOnClickRevertColorListener() {}
529 
OnClick(UIView & view,const ClickEvent & event)530     bool OnClick(UIView& view, const ClickEvent& event) override
531     {
532         uiView_->SetStyle(STYLE_BACKGROUND_COLOR, originColor_);
533         uiView_->Invalidate();
534         return true;
535     }
536 
537 private:
538     UIView* uiView_;
539     uint32_t originColor_;
540 };
541 
542 class TestBtnOnClickRevertToOriginStateListener : public UIView::OnClickListener {
543 public:
TestBtnOnClickRevertToOriginStateListener(UIView * uiView)544     explicit TestBtnOnClickRevertToOriginStateListener(UIView* uiView)
545         : uiView_(uiView), originColor_(0), width_(0), height_(0), visible_(false), touchable_(false)
546     {
547         if (uiView != nullptr) {
548             originColor_ = uiView->GetStyle(STYLE_BACKGROUND_COLOR);
549             width_ = uiView->GetWidth();
550             height_ = uiView->GetHeight();
551             visible_ = uiView->IsVisible();
552             touchable_ = uiView->IsTouchable();
553         }
554     }
555 
~TestBtnOnClickRevertToOriginStateListener()556     ~TestBtnOnClickRevertToOriginStateListener() {}
557 
OnClick(UIView & view,const ClickEvent & event)558     bool OnClick(UIView& view, const ClickEvent& event) override
559     {
560         uiView_->Invalidate();
561         uiView_->SetStyle(STYLE_BACKGROUND_COLOR, originColor_);
562         uiView_->SetWidth(width_);
563         uiView_->SetHeight(height_);
564         uiView_->SetVisible(visible_);
565         uiView_->SetTouchable(touchable_);
566         (reinterpret_cast<UIButton*>(uiView_))->Invalidate();
567         return true;
568     }
569 
570 private:
571     UIView* uiView_;
572     uint32_t originColor_;
573     uint32_t width_;
574     uint32_t height_;
575     bool visible_;
576     bool touchable_;
577 };
578 
579 #if defined(DEFAULT_ANIMATION) && DEFAULT_ANIMATION
580 class TestBtnAnimationListener : public UIView::OnClickListener {
581 public:
TestBtnAnimationListener(UIView * uiView,bool enableAnimation)582     TestBtnAnimationListener(UIView* uiView, bool enableAnimation) : uiView_(uiView), enableAnimation_(enableAnimation)
583     {
584     }
585 
~TestBtnAnimationListener()586     ~TestBtnAnimationListener() {}
587 
OnClick(UIView & view,const ClickEvent & event)588     bool OnClick(UIView& view, const ClickEvent& event) override
589     {
590         static_cast<UIButton*>(uiView_)->EnableButtonAnimation(enableAnimation_);
591         return true;
592     }
593 
594 private:
595     UIView* uiView_;
596     bool enableAnimation_;
597 };
598 #endif
599 
GetTestUILabel(const char * titlename)600 UILabel* GetTestUILabel(const char* titlename)
601 {
602     if (titlename == nullptr) {
603         return nullptr;
604     }
605 
606     UILabel* label = new UILabel();
607     // 900:y-coordinate
608     label->SetPosition(TEXT_DISTANCE_TO_LEFT_SIDE, 900, Screen::GetInstance().GetWidth(), TITLE_LABEL_DEFAULT_HEIGHT);
609     label->SetText(titlename);
610     label->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
611     return label;
612 }
613 
GetTestUIButton(const char * buttonText,int16_t x,int16_t y,UIButton * button)614 UILabelButton* GetTestUIButton(const char* buttonText, int16_t x, int16_t y, UIButton* button)
615 {
616     if (buttonText == nullptr) {
617         return nullptr;
618     }
619 
620     UILabelButton* labelButton = new UILabelButton();
621     // 150: x-coordinate, 440: y-coordinate
622     labelButton->SetPosition(x, y);
623     labelButton->Resize(BUTTON_WIDHT1, BUTTON_HEIGHT1);
624     labelButton->SetText(buttonText);
625     labelButton->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
626     labelButton->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_STYLE_BORDER_RADIUS_VALUE, UIButton::PRESSED);
627     labelButton->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_STYLE_BORDER_RADIUS_VALUE, UIButton::INACTIVE);
628     labelButton->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::RELEASED);
629     labelButton->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::PRESSED);
630     labelButton->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::INACTIVE);
631     return labelButton;
632 }
633 
UIKitButtonTest002(UIScrollView * container,UIButton * button)634 void UITestBUTTON::UIKitButtonTest002(UIScrollView* container, UIButton* button)
635 {
636     if ((container == nullptr) || (button == nullptr)) {
637         return;
638     }
639     UILabelButton* button8 = GetTestUIButton("隐藏", 430, 940, button); // 430: x-coordinate, 940: y-coordinate
640     if (clickEnableVisiableListener_ == nullptr) {
641         clickEnableVisiableListener_ =
642             static_cast<UIView::OnClickListener*>(
643                 new TestBtnOnClickVisableListener(reinterpret_cast<UIView*>(button), false));
644     }
645     button8->SetOnClickListener(clickEnableVisiableListener_);
646 
647     UILabelButton* button9 = GetTestUIButton("显示", 430, 990, button); // 430: x-coordinate, 990: y-coordinate
648     if (clickDisableVisiableListener_ == nullptr) {
649         clickDisableVisiableListener_ =
650             static_cast<UIView::OnClickListener*>(
651                 new TestBtnOnClickVisableListener(reinterpret_cast<UIView*>(button), true));
652     }
653     button9->SetOnClickListener(clickDisableVisiableListener_);
654 
655     UILabelButton* button10 = GetTestUIButton("可触摸 ", 520, 940, button); // 520: x-coordinate, 940: y-coordinate
656     if (clickEnableTouchableListener_ == nullptr) {
657         clickEnableTouchableListener_ =
658             static_cast<UIView::OnClickListener*>(
659                 new TestBtnOnClickTouchableListener(reinterpret_cast<UIView*>(button), true));
660     }
661     button10->SetOnClickListener(clickEnableTouchableListener_);
662 
663     UILabelButton* button11 = GetTestUIButton("不可触摸", 520, 990, button); // 520: x-coordinate, 990: y-coordinate
664     if (clickDisableTouchableListener_ == nullptr) {
665         clickDisableTouchableListener_ =
666             static_cast<UIView::OnClickListener*>(
667                 new TestBtnOnClickTouchableListener(reinterpret_cast<UIView*>(button), false));
668     }
669     button11->SetOnClickListener(clickDisableTouchableListener_);
670 
671     UILabelButton* button12 = GetTestUIButton("变白", 160, 1040, button); // 160: x-coordinate, 1040: y-coordinate
672     if (clickColorToWhiteListener_ == nullptr) {
673         clickColorToWhiteListener_ = static_cast<UIView::OnClickListener*>(
674             new TestBtnOnClickChangeColorListener(reinterpret_cast<UIView*>(button), 0xFF, 0xFF, 0xFF));
675     }
676     button12->SetOnClickListener(clickColorToWhiteListener_);
677 
678     UILabelButton* button13 = GetTestUIButton("变红", 160, 1090, button); // 160: x-coordinate, 1090: y-coordinate
679     if (clickColorToRedListener_ == nullptr) {
680         clickColorToRedListener_ =
681             static_cast<UIView::OnClickListener*>(
682                 new TestBtnOnClickChangeColorListener(reinterpret_cast<UIView*>(button), 0xFF, 0, 0));
683     }
684     button13->SetOnClickListener(clickColorToRedListener_);
685 
686     UILabelButton* button14 = GetTestUIButton("色彩还原", 250, 1040, button); // 250: x-coordinate, 1040: y-coordinate
687     if (clickRevetColorListener_ == nullptr) {
688         clickRevetColorListener_ =
689             static_cast<UIView::OnClickListener*>(
690                 new TestBtnOnClickRevertColorListener(reinterpret_cast<UIView*>(button)));
691     }
692     button14->SetOnClickListener(clickRevetColorListener_);
693 
694     UILabelButton* button15 = GetTestUIButton("全部还原", 250, 1090, button); // 250: x-coordinate, 1090: y-coordinate
695     if (clickRevetToOriginListener_ == nullptr) {
696         clickRevetToOriginListener_ =
697             static_cast<UIView::OnClickListener*>(
698                 new TestBtnOnClickRevertToOriginStateListener(reinterpret_cast<UIView*>(button)));
699     }
700     button15->SetOnClickListener(clickRevetToOriginListener_);
701 
702     container->Add(button8);
703     container->Add(button9);
704     container->Add(button10);
705     container->Add(button11);
706     container->Add(button12);
707     container->Add(button13);
708     container->Add(button14);
709     container->Add(button15);
710 
711 #if defined(DEFAULT_ANIMATION) && DEFAULT_ANIMATION
712     // 340: x-coordinate, 1040: y-coordinate
713     UILabelButton* button16 = GetTestUIButton("开启动效", 340, 1040, button);
714     if (enableAnimationListener_ == nullptr) {
715         enableAnimationListener_ =
716             static_cast<UIView::OnClickListener*>(
717                 new TestBtnAnimationListener(reinterpret_cast<UIView*>(button), true));
718     }
719     button16->SetOnClickListener(enableAnimationListener_);
720 
721     // 340: x-coordinate, 1090: y-coordinate
722     UILabelButton* button17 = GetTestUIButton("关闭动效", 340, 1090, button);
723     if (disableAnimationListener_ == nullptr) {
724         disableAnimationListener_ = static_cast<UIView::OnClickListener*>(
725             new TestBtnAnimationListener(reinterpret_cast<UIView*>(button), false));
726     }
727     button17->EnableButtonAnimation(false);
728     button17->SetOnClickListener(disableAnimationListener_);
729 
730     container->Add(button16);
731     container->Add(button17);
732 #endif
733 }
734 
UIKitButtonTest001()735 void UITestBUTTON::UIKitButtonTest001()
736 {
737     if (container_ != nullptr) {
738         UILabel* label = GetTestUILabel("普通button功能效果");
739         container_->Add(label);
740 
741         UIButton* button = new UIButton();
742         button->SetPosition(10, 935); // 10: x-coordinate, 935: y-coordinate
743         button->SetWidth(80);         // 80: width
744         button->SetHeight(60);        // 60: height
745         button->SetImageSrc(BLUE_IMAGE_PATH, YELLOW_IMAGE_PATH);
746 
747         UILabelButton* button2 = GetTestUIButton("放大", 160, 940, button); // 160: x-coordinate, 940: y-coordinate
748         if (clickBigListener_ == nullptr) {
749             clickBigListener_ =
750                 static_cast<UIView::OnClickListener*>(new TestBtnOnClickShapeChangeListener(button, CHANGE_SIZE));
751         }
752         button2->SetOnClickListener(clickBigListener_);
753 
754         UILabelButton* button3 = GetTestUIButton("缩小", 160, 990, button); // 160: x-coordinate, 990: y-coordinate
755         if (clickSmallListener_ == nullptr) {
756             clickSmallListener_ =
757                 static_cast<UIView::OnClickListener*>(new TestBtnOnClickShapeChangeListener(button, -CHANGE_SIZE));
758         }
759         button3->SetOnClickListener(clickSmallListener_);
760 
761         UILabelButton* button4 = GetTestUIButton("左移", 250, 940, button); // 250: x-coordinate, 940: y-coordinate
762         if (clickLeftListener_ == nullptr) {
763             clickLeftListener_ = static_cast<UIView::OnClickListener*>(new TestBtnOnClickPositionChangeListener(
764                 reinterpret_cast<UIView*>(button),
765                 TestBtnOnClickPositionChangeListener::MoveType::MOVE_LEFT, CHANGE_SIZE));
766         }
767         button4->SetOnClickListener(clickLeftListener_);
768 
769         UILabelButton* button5 = GetTestUIButton("右移", 250, 990, button); // 250: x-coordinate, 990: y-coordinate
770         button5->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
771         if (clickRightListener_ == nullptr) {
772             clickRightListener_ = static_cast<UIView::OnClickListener*>(new TestBtnOnClickPositionChangeListener(
773                 reinterpret_cast<UIView*>(button),
774                 TestBtnOnClickPositionChangeListener::MoveType::MOVE_RIGHT, CHANGE_SIZE));
775         }
776         button5->SetOnClickListener(clickRightListener_);
777 
778         UILabelButton* button6 = GetTestUIButton("上移", 340, 940, button); // 340: x-coordinate, 940: y-coordinate
779         button6->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
780         if (clickUpListener_ == nullptr) {
781             clickUpListener_ = static_cast<UIView::OnClickListener*>(new TestBtnOnClickPositionChangeListener(
782                 reinterpret_cast<UIView*>(button),
783                 TestBtnOnClickPositionChangeListener::MoveType::MOVE_TOP, CHANGE_SIZE));
784         }
785         button6->SetOnClickListener(clickUpListener_);
786 
787         UILabelButton* button7 = GetTestUIButton("下移", 340, 990, button); // 340: x-coordinate, 990: y-coordinate
788         if (clickDownListener_ == nullptr) {
789             clickDownListener_ = static_cast<UIView::OnClickListener*>(new TestBtnOnClickPositionChangeListener(
790                 reinterpret_cast<UIView*>(button),
791                 TestBtnOnClickPositionChangeListener::MoveType::MOVE_BOTTOM, CHANGE_SIZE));
792         }
793         button7->SetOnClickListener(clickDownListener_);
794 
795         container_->Add(button);
796         container_->Add(button2);
797         container_->Add(button3);
798         container_->Add(button4);
799         container_->Add(button5);
800         container_->Add(button6);
801         container_->Add(button7);
802 
803         UIKitButtonTest002(container_, button);
804     }
805 }
806 } // namespace OHOS
807