• 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_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 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         if (dialog_ != nullptr) {
350             delete dialog_;
351             dialog_ = nullptr;
352         }
353         dialog_ = new UIDialog();
354         dialog_->SetTitle("标题标题标题标题标题");
355         dialog_->SetText("段落文本");
356         listener1_->SetDialog(&dialog_);
357         dialog_->SetButton(UIDialog::DialogButtonType::BUTTON_LEFT, "Button1", listener1_);
358         dialog_->Show();
359     } else if (&view == button2_) {
360         if (dialog_ != nullptr) {
361             delete dialog_;
362             dialog_ = nullptr;
363         }
364         dialog_ = new UIDialog();
365         dialog_->SetTitle("标题");
366         dialog_->SetText("段落文本段落文本段落文本段落文本段落文本段落文本段落文本");
367         listener1_->SetDialog(&dialog_);
368         listener3_->SetDialog(&dialog_);
369         dialog_->SetButton(UIDialog::DialogButtonType::BUTTON_LEFT, "Button1", listener1_);
370         dialog_->SetButton(UIDialog::DialogButtonType::BUTTON_RIGHT, "Button2", listener3_);
371         dialog_->Show();
372     } else if (&view == button3_) {
373         if (dialog_ != nullptr) {
374             delete dialog_;
375             dialog_ = nullptr;
376         }
377         dialog_ = new UIDialog();
378         dialog_->SetTitle("标题");
379         dialog_->SetText("段落文本");
380         listener2_->SetDialog(&dialog_);
381         dialog_->SetButton(UIDialog::DialogButtonType::BUTTON_MID, "Button1Button1Button1Button1", listener2_);
382         dialog_->Show();
383     } else if (&view == button4_) {
384         if (dialog_ != nullptr) {
385             delete dialog_;
386             dialog_ = nullptr;
387         }
388         dialog_ = new UIDialog();
389         dialog_->SetTitle("标题");
390         dialog_->SetText("段落文本");
391         listener1_->SetDialog(&dialog_);
392         listener2_->SetDialog(&dialog_);
393         listener3_->SetDialog(&dialog_);
394         dialog_->SetButton(UIDialog::DialogButtonType::BUTTON_LEFT, "Button111111111111111111", listener1_);
395         dialog_->SetButton(UIDialog::DialogButtonType::BUTTON_MID, "Button22222222222222222", listener2_);
396         dialog_->SetButton(UIDialog::DialogButtonType::BUTTON_RIGHT, "Button33333333333333333", listener3_);
397         dialog_->Show();
398     } else if (&view == button5_) {
399         if (dialog_ != nullptr) {
400             delete dialog_;
401             dialog_ = nullptr;
402         }
403         dialog_ = new UIDialog();
404         dialog_->SetText("段落文本");
405         dialog_->EnableAutoCancel(true);
406         listener_->SetDialog(&dialog_);
407         dialog_->SetOnCancelListener(listener_);
408         dialog_->Show();
409     } else {
410         return ClickExpand(view, event);
411     }
412     return true;
413 }
414 
ClickExpand(UIView & view,const ClickEvent & event)415 bool UITestDialog::ClickExpand(UIView &view, const ClickEvent& event)
416 {
417     if (&view == button6_) {
418         if (dialog_ != nullptr) {
419             delete dialog_;
420             dialog_ = nullptr;
421         }
422         dialog_ = new UIDialog();
423         dialog_->SetText("段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本");
424         dialog_->EnableAutoCancel(true);
425         listener_->SetDialog(&dialog_);
426         dialog_->SetOnCancelListener(listener_);
427         dialog_->Show();
428     } else if (&view == button7_) {
429         if (dialog_ != nullptr) {
430             delete dialog_;
431             dialog_ = nullptr;
432         }
433         dialog_ = new UIDialog();
434         dialog_->SetTitle("段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本");
435         dialog_->SetText(
436             "段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本 \
437             段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本 \
438             段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本 \
439             段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本 \
440             段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本 \
441             段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本 \
442             段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文 \
443             段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文本段落文");
444         listener1_->SetDialog(&dialog_);
445         listener2_->SetDialog(&dialog_);
446         listener3_->SetDialog(&dialog_);
447         dialog_->SetButton(UIDialog::DialogButtonType::BUTTON_LEFT, "Button111111111111111111111", listener1_);
448         dialog_->SetButton(UIDialog::DialogButtonType::BUTTON_MID, "Button22222222222222222222", listener2_);
449         dialog_->SetButton(UIDialog::DialogButtonType::BUTTON_RIGHT, "Button33333333333333333333", listener3_);
450         dialog_->Show();
451     } else if (&view == button8_) {
452         if (dialog_ != nullptr) {
453             delete dialog_;
454             dialog_ = nullptr;
455         }
456         dialog_ = new UIDialog();
457         dialog_->SetText("段落正文");
458         listener2_->SetDialog(&dialog_);
459         dialog_->SetButton(UIDialog::DialogButtonType::BUTTON_MID, "button", listener2_);
460         dialog_->SetButtonColor(UIDialog::DialogButtonType::BUTTON_MID, Color::Red());
461         dialog_->Show();
462     } else if (&view == button9_) {
463         if (dialog_ != nullptr) {
464             delete dialog_;
465             dialog_ = nullptr;
466         }
467         dialog_ = new UIDialog();
468         dialog_->SetText("段落正文");
469         listener2_->SetDialog(&dialog_);
470         dialog_->SetButton(UIDialog::DialogButtonType::BUTTON_MID, "button", listener2_);
471         // 0xFF, 0x00, 0x00, 0x7F: color red with 50% opacity
472         ColorType color = Color::GetColorFromRGBA(0xFF, 0x00, 0x00, 0x7F);
473         dialog_->SetButtonColor(UIDialog::DialogButtonType::BUTTON_MID, color);
474         dialog_->Show();
475     } else if (&view == button10_) {
476         if (dialog_ != nullptr) {
477             delete dialog_;
478             dialog_ = nullptr;
479         }
480         dialog_ = new UIDialog();
481         dialog_->SetTitle("标题1");
482         dialog_->SetText("段落正文1");
483         listener2_->SetDialog(&dialog_);
484         dialog_->SetButton(UIDialog::DialogButtonType::BUTTON_MID, "按钮1", listener2_);
485         dialog_->SetTitle("标题2");
486         dialog_->SetText("段落正文2");
487         dialog_->SetButton(UIDialog::DialogButtonType::BUTTON_MID, "按钮2", listener2_);
488         dialog_->Show();
489     }
490     return true;
491 }
492 } // namespace OHOS
493 #endif // ENABLE_WINDOW
494