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