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