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_dialog.h"
17 #include "common/screen.h"
18 #include "components/ui_dialog.h"
19 #include "components/ui_label.h"
20 #include "graphic_config.h"
21 #include "test_resource_config.h"
22 
23 #if defined(ENABLE_WINDOW) && ENABLE_WINDOW
24 namespace OHOS {
25 namespace {
26 const int16_t GAP = 5;
27 const int16_t TITLE_HEIGHT = 20;
28 const uint16_t LABEL_WIDTH = 350;
29 const uint16_t LABEL_HEIGHT = 50;
30 const uint16_t BUTTON_WIDTH = 100;
31 const uint16_t BUTTON_HEIGHT = 50;
32 } // namespace
33 
34 class TestUIDialogButtonListener : public UIView::OnClickListener {
35 public:
TestUIDialogButtonListener(UIDialog::DialogButtonType buttonType,UILabel * label)36     TestUIDialogButtonListener(UIDialog::DialogButtonType buttonType, UILabel* label)
37     {
38         dialog_ = nullptr;
39         buttonType_ = buttonType;
40         label_ = label;
41     }
~TestUIDialogButtonListener()42     virtual ~TestUIDialogButtonListener() {}
43 
SetDialog(UIDialog ** dialog)44     void SetDialog(UIDialog** dialog)
45     {
46         dialog_ = dialog;
47     }
48 
OnClick(UIView & view,const ClickEvent & event)49     bool OnClick(UIView &view, const ClickEvent& event) override
50     {
51         switch (buttonType_) {
52             case UIDialog::DialogButtonType::BUTTON_LEFT:
53                 label_->SetText("button left click!");
54                 break;
55             case UIDialog::DialogButtonType::BUTTON_MID:
56                 label_->SetText("button mid click!");
57                 break;
58             case UIDialog::DialogButtonType::BUTTON_RIGHT:
59                 label_->SetText("button right click!");
60                 break;
61             default:
62                 break;
63         }
64         if (*dialog_ != nullptr) {
65             delete *dialog_;
66             *dialog_ = nullptr;
67         }
68         return true;
69     }
70 
71 private:
72     UIDialog** dialog_;
73     UIDialog::DialogButtonType buttonType_;
74     UILabel* label_;
75 };
76 
77 class TestUIDialogOnCancelListener : public UIView::OnClickListener {
78 public:
TestUIDialogOnCancelListener(UILabel * label)79     explicit TestUIDialogOnCancelListener(UILabel* label)
80     {
81         dialog_ = nullptr;
82         label_ = label;
83     }
~TestUIDialogOnCancelListener()84     virtual ~TestUIDialogOnCancelListener() {}
85 
SetDialog(UIDialog ** dialog)86     void SetDialog(UIDialog** dialog)
87     {
88         dialog_ = dialog;
89     }
90 
OnClick(UIView & view,const ClickEvent & event)91     bool OnClick(UIView &view, const ClickEvent& event) override
92     {
93         label_->SetText("Click outside the dialog.");
94         if (*dialog_ != nullptr) {
95             delete *dialog_;
96             *dialog_ = nullptr;
97         }
98         return true;
99     }
100 
101 private:
102     UIDialog** dialog_;
103     UILabel* label_;
104 };
105 
SetUp()106 void UITestDialog::SetUp()
107 {
108     if (container_ == nullptr) {
109         container_ = new UIScrollView();
110         container_->SetThrowDrag(true);
111         container_->SetHorizontalScrollState(false);
112         container_->Resize(Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight() - LABEL_HEIGHT);
113         positionX_ = 50; // 50: init position x0
114         positionY_ = 5;  // 5: init position y
115     }
116 
117     if (label_ == nullptr) {
118         label_ = new UILabel();
119         label_->SetPosition(0, positionY_);
120         label_->Resize(LABEL_WIDTH, LABEL_HEIGHT);
121         label_->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 18); // 18: font size
122         label_->SetText("label");
123         container_->Add(label_);
124         positionY_ += LABEL_HEIGHT + GAP;
125     }
126     if (listener1_ == nullptr) {
127         listener1_ = new TestUIDialogButtonListener(UIDialog::DialogButtonType::BUTTON_LEFT, label_);
128     }
129     if (listener2_ == nullptr) {
130         listener2_ = new TestUIDialogButtonListener(UIDialog::DialogButtonType::BUTTON_MID, label_);
131     }
132     if (listener3_ == nullptr) {
133         listener3_ = new TestUIDialogButtonListener(UIDialog::DialogButtonType::BUTTON_RIGHT, label_);
134     }
135     if (listener_ == nullptr) {
136         listener_ = new TestUIDialogOnCancelListener(label_);
137     }
138 }
139 
TearDown()140 void UITestDialog::TearDown()
141 {
142     DeleteChildren(container_);
143     if (listener1_ != nullptr) {
144         delete listener1_;
145         listener1_ = nullptr;
146     }
147     if (listener2_ != nullptr) {
148         delete listener2_;
149         listener2_ = nullptr;
150     }
151     if (listener3_ != nullptr) {
152         delete listener3_;
153         listener3_ = nullptr;
154     }
155     if (listener_ != nullptr) {
156         delete listener_;
157         listener_ = nullptr;
158     }
159     if (dialog_ != nullptr) {
160         delete dialog_;
161         dialog_ = nullptr;
162     }
163     label_ = nullptr;
164     container_ = nullptr;
165 }
166 
InnerTestTitle(const char * title)167 void UITestDialog::InnerTestTitle(const char* title)
168 {
169     UILabel* titleLabel = new UILabel();
170     titleLabel->SetPosition(0, positionY_, Screen::GetInstance().GetWidth(), TITLE_HEIGHT);
171     titleLabel->SetStyle(STYLE_TEXT_COLOR, Color::Black().full);
172     titleLabel->SetStyle(STYLE_BACKGROUND_COLOR, Color::White().full);
173     titleLabel->SetStyle(STYLE_BACKGROUND_OPA, OPA_OPAQUE);
174     titleLabel->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
175     titleLabel->SetText(title);
176 
177     container_->Add(titleLabel);
178     positionY_ += TITLE_HEIGHT + GAP;
179 }
180 
GetTestView()181 const UIView* UITestDialog::GetTestView()
182 {
183     UIKitDialogTest001();
184     UIKitDialogTest002();
185     UIKitDialogTest003();
186     UIKitDialogTest004();
187     UIKitDialogTest005();
188     UIKitDialogTest006();
189     UIKitDialogTest007();
190     UIKitDialogTest008();
191     UIKitDialogTest009();
192     UIKitDialogTest010();
193     return container_;
194 }
195 
UIKitDialogTest001()196 void UITestDialog::UIKitDialogTest001()
197 {
198     if (container_ == nullptr) {
199         return;
200     }
201     InnerTestTitle("测试设置较长标题、较短正文、单个较短按钮");
202     button1_ = new UILabelButton();
203     button1_->SetPosition(0, positionY_, BUTTON_WIDTH, BUTTON_HEIGHT);
204     button1_->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
205     button1_->SetText("Dialog1");
206     button1_->SetOnClickListener(this);
207     container_->Add(button1_);
208     positionY_ += BUTTON_HEIGHT + GAP;
209 }
210 
UIKitDialogTest002()211 void UITestDialog::UIKitDialogTest002()
212 {
213     if (container_ == nullptr) {
214         return;
215     }
216     InnerTestTitle("测试设置较短标题、较长正文、两个较短按钮");
217     button2_ = new UILabelButton();
218     button2_->SetPosition(0, positionY_, BUTTON_WIDTH, BUTTON_HEIGHT);
219     button2_->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
220     button2_->SetText("Dialog2");
221     button2_->SetOnClickListener(this);
222     container_->Add(button2_);
223     positionY_ += BUTTON_HEIGHT + GAP;
224 }
225 
UIKitDialogTest003()226 void UITestDialog::UIKitDialogTest003()
227 {
228     if (container_ == nullptr) {
229         return;
230     }
231     InnerTestTitle("测试设置较短标题、较短正文、单个较长按钮");
232     button3_ = new UILabelButton();
233     button3_->SetPosition(0, positionY_, BUTTON_WIDTH, BUTTON_HEIGHT);
234     button3_->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
235     button3_->SetText("Dialog3");
236     button3_->SetOnClickListener(this);
237     container_->Add(button3_);
238     positionY_ += BUTTON_HEIGHT + GAP;
239 }
240 
UIKitDialogTest004()241 void UITestDialog::UIKitDialogTest004()
242 {
243     if (container_ == nullptr) {
244         return;
245     }
246     InnerTestTitle("测试设置较短标题、较短正文、三个长按钮");
247     button4_ = new UILabelButton();
248     button4_->SetPosition(0, positionY_, BUTTON_WIDTH, BUTTON_HEIGHT);
249     button4_->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
250     button4_->SetText("Dialog4");
251     button4_->SetOnClickListener(this);
252     container_->Add(button4_);
253     positionY_ += BUTTON_HEIGHT + GAP;
254 }
255 
UIKitDialogTest005()256 void UITestDialog::UIKitDialogTest005()
257 {
258     if (container_ == nullptr) {
259         return;
260     }
261     InnerTestTitle("测试设置较短正文、自动关闭、弹框外点击事件监听");
262     button5_ = new UILabelButton();
263     button5_->SetPosition(0, positionY_, BUTTON_WIDTH, BUTTON_HEIGHT);
264     button5_->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
265     button5_->SetText("Dialog5");
266     button5_->SetOnClickListener(this);
267     container_->Add(button5_);
268     positionY_ += BUTTON_HEIGHT + GAP;
269 }
270 
UIKitDialogTest006()271 void UITestDialog::UIKitDialogTest006()
272 {
273     if (container_ == nullptr) {
274         return;
275     }
276     InnerTestTitle("测试只设置较长正文,点击弹框外自动关闭");
277     button6_ = new UILabelButton();
278     button6_->SetPosition(0, positionY_, BUTTON_WIDTH, BUTTON_HEIGHT);
279     button6_->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
280     button6_->SetText("Dialog6");
281     button6_->SetOnClickListener(this);
282     container_->Add(button6_);
283     positionY_ += BUTTON_HEIGHT + GAP;
284 }
285 
UIKitDialogTest007()286 void UITestDialog::UIKitDialogTest007()
287 {
288     if (container_ == nullptr) {
289         return;
290     }
291     InnerTestTitle("测试设置长标题,长正文,三个长按钮");
292     button7_ = new UILabelButton();
293     button7_->SetPosition(0, positionY_, BUTTON_WIDTH, BUTTON_HEIGHT);
294     button7_->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
295     button7_->SetText("Dialog7");
296     button7_->SetOnClickListener(this);
297     container_->Add(button7_);
298     positionY_ += BUTTON_HEIGHT + GAP;
299 }
300 
UIKitDialogTest008()301 void UITestDialog::UIKitDialogTest008()
302 {
303     if (container_ == nullptr) {
304         return;
305     }
306     InnerTestTitle("测试设置按钮颜色不带透明度");
307     button8_ = new UILabelButton();
308     button8_->SetPosition(0, positionY_, BUTTON_WIDTH, BUTTON_HEIGHT);
309     button8_->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
310     button8_->SetText("Dialog8");
311     button8_->SetOnClickListener(this);
312     container_->Add(button8_);
313     positionY_ += BUTTON_HEIGHT + GAP;
314 }
315 
UIKitDialogTest009()316 void UITestDialog::UIKitDialogTest009()
317 {
318     if (container_ == nullptr) {
319         return;
320     }
321     InnerTestTitle("测试设置按钮颜色带透明度");
322     button9_ = new UILabelButton();
323     button9_->SetPosition(0, positionY_, BUTTON_WIDTH, BUTTON_HEIGHT);
324     button9_->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
325     button9_->SetText("Dialog9");
326     button9_->SetOnClickListener(this);
327     container_->Add(button9_);
328     positionY_ += BUTTON_HEIGHT + GAP;
329 }
330 
UIKitDialogTest010()331 void UITestDialog::UIKitDialogTest010()
332 {
333     if (container_ == nullptr) {
334         return;
335     }
336     InnerTestTitle("测试连续多次设置标题、正文、按钮");
337     button10_ = new UILabelButton();
338     button10_->SetPosition(0, positionY_, BUTTON_WIDTH, BUTTON_HEIGHT);
339     button10_->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
340     button10_->SetText("Dialog10");
341     button10_->SetOnClickListener(this);
342     container_->Add(button10_);
343     positionY_ += BUTTON_HEIGHT + GAP;
344 }
345 
OnClick(UIView & view,const ClickEvent & event)346 bool UITestDialog::OnClick(UIView &view, const ClickEvent& event)
347 {
348     if (&view == button1_) {
349         SetShowInfo001();
350     } else if (&view == button2_) {
351         SetShowInfo002();
352     } else if (&view == button3_) {
353         SetShowInfo003();
354     } else if (&view == button4_) {
355         if (dialog_ != nullptr) {
356             delete dialog_;
357             dialog_ = nullptr;
358         }
359         dialog_ = new UIDialog();
360         dialog_->SetTitle("标题");
361         dialog_->SetText("段落文本");
362         listener1_->SetDialog(&dialog_);
363         listener2_->SetDialog(&dialog_);
364         listener3_->SetDialog(&dialog_);
365         dialog_->SetButton(UIDialog::DialogButtonType::BUTTON_LEFT, "Button111111111111111111", listener1_);
366         dialog_->SetButton(UIDialog::DialogButtonType::BUTTON_MID, "Button22222222222222222", listener2_);
367         dialog_->SetButton(UIDialog::DialogButtonType::BUTTON_RIGHT, "Button33333333333333333", listener3_);
368         dialog_->Show();
369     } else if (&view == button5_) {
370         if (dialog_ != nullptr) {
371             delete dialog_;
372             dialog_ = nullptr;
373         }
374         dialog_ = new UIDialog();
375         dialog_->SetText("段落文本");
376         dialog_->EnableAutoCancel(true);
377         listener_->SetDialog(&dialog_);
378         dialog_->SetOnCancelListener(listener_);
379         dialog_->Show();
380     } else {
381         return ClickExpand(view, event);
382     }
383     return true;
384 }
385 
SetShowInfo001()386 void UITestDialog::SetShowInfo001()
387 {
388     if (dialog_ != nullptr) {
389         delete dialog_;
390         dialog_ = nullptr;
391     }
392     dialog_ = new UIDialog();
393     dialog_->SetTitle("标题标题标题标题标题");
394     dialog_->SetText("段落文本");
395     listener1_->SetDialog(&dialog_);
396     dialog_->SetButton(UIDialog::DialogButtonType::BUTTON_LEFT, "Button1", listener1_);
397     dialog_->Show();
398 }
399 
SetShowInfo002()400 void UITestDialog::SetShowInfo002()
401 {
402     if (dialog_ != nullptr) {
403         delete dialog_;
404         dialog_ = nullptr;
405     }
406     dialog_ = new UIDialog();
407     dialog_->SetTitle("标题");
408     dialog_->SetText("段落文本段落文本段落文本段落文本段落文本段落文本段落文本");
409     listener1_->SetDialog(&dialog_);
410     listener3_->SetDialog(&dialog_);
411     dialog_->SetButton(UIDialog::DialogButtonType::BUTTON_LEFT, "Button1", listener1_);
412     dialog_->SetButton(UIDialog::DialogButtonType::BUTTON_RIGHT, "Button2", listener3_);
413     dialog_->Show();
414 }
415 
SetShowInfo003()416 void UITestDialog::SetShowInfo003()
417 {
418     if (dialog_ != nullptr) {
419         delete dialog_;
420         dialog_ = nullptr;
421     }
422     dialog_ = new UIDialog();
423     dialog_->SetTitle("标题");
424     dialog_->SetText("段落文本");
425     listener2_->SetDialog(&dialog_);
426     dialog_->SetButton(UIDialog::DialogButtonType::BUTTON_MID, "Button1Button1Button1Button1", listener2_);
427     dialog_->Show();
428 }
429 
SetShowInfo006()430 void UITestDialog::SetShowInfo006()
431 {
432     if (dialog_ != nullptr) {
433         delete dialog_;
434         dialog_ = nullptr;
435     }
436     dialog_ = new UIDialog();
437     dialog_->SetText("段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本");
438     dialog_->EnableAutoCancel(true);
439     listener_->SetDialog(&dialog_);
440     dialog_->SetOnCancelListener(listener_);
441     dialog_->Show();
442 }
443 
SetShowInfo007()444 void UITestDialog::SetShowInfo007()
445 {
446     if (dialog_ != nullptr) {
447         delete dialog_;
448         dialog_ = nullptr;
449     }
450     dialog_ = new UIDialog();
451     dialog_->SetTitle("段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本");
452     dialog_->SetText(
453         "段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本 \
454         段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本 \
455         段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本 \
456         段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本 \
457         段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本 \
458         段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本 \
459         段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文 \
460         段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文");
461     listener1_->SetDialog(&dialog_);
462     listener2_->SetDialog(&dialog_);
463     listener3_->SetDialog(&dialog_);
464     dialog_->SetButton(UIDialog::DialogButtonType::BUTTON_LEFT, "Button111111111111111111111", listener1_);
465     dialog_->SetButton(UIDialog::DialogButtonType::BUTTON_MID, "Button22222222222222222222", listener2_);
466     dialog_->SetButton(UIDialog::DialogButtonType::BUTTON_RIGHT, "Button33333333333333333333", listener3_);
467     dialog_->Show();
468 }
469 
ClickExpand(UIView & view,const ClickEvent & event)470 bool UITestDialog::ClickExpand(UIView &view, const ClickEvent& event)
471 {
472     if (&view == button6_) {
473         SetShowInfo006();
474     } else if (&view == button7_) {
475         SetShowInfo007();
476     } else if (&view == button8_) {
477         if (dialog_ != nullptr) {
478             delete dialog_;
479             dialog_ = nullptr;
480         }
481         dialog_ = new UIDialog();
482         dialog_->SetText("段落正文");
483         listener2_->SetDialog(&dialog_);
484         dialog_->SetButton(UIDialog::DialogButtonType::BUTTON_MID, "button", listener2_);
485         dialog_->SetButtonColor(UIDialog::DialogButtonType::BUTTON_MID, Color::Red());
486         dialog_->Show();
487     } else if (&view == button9_) {
488         if (dialog_ != nullptr) {
489             delete dialog_;
490             dialog_ = nullptr;
491         }
492         dialog_ = new UIDialog();
493         dialog_->SetText("段落正文");
494         listener2_->SetDialog(&dialog_);
495         dialog_->SetButton(UIDialog::DialogButtonType::BUTTON_MID, "button", listener2_);
496         // 0xFF, 0x00, 0x00, 0x7F: color red with 50% opacity
497         ColorType color = Color::GetColorFromRGBA(0xFF, 0x00, 0x00, 0x7F);
498         dialog_->SetButtonColor(UIDialog::DialogButtonType::BUTTON_MID, color);
499         dialog_->Show();
500     } else if (&view == button10_) {
501         if (dialog_ != nullptr) {
502             delete dialog_;
503             dialog_ = nullptr;
504         }
505         dialog_ = new UIDialog();
506         dialog_->SetTitle("标题1");
507         dialog_->SetText("段落正文1");
508         listener2_->SetDialog(&dialog_);
509         dialog_->SetButton(UIDialog::DialogButtonType::BUTTON_MID, "按钮1", listener2_);
510         dialog_->SetTitle("标题2");
511         dialog_->SetText("段落正文2");
512         dialog_->SetButton(UIDialog::DialogButtonType::BUTTON_MID, "按钮2", listener2_);
513         dialog_->Show();
514     }
515     return true;
516 }
517 } // namespace OHOS
518 #endif // ENABLE_WINDOW
519