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 "components/root_view.h"
17 #include "components/ui_dialog.h"
18 
19 #include <climits>
20 #include <gtest/gtest.h>
21 
22 #if ENABLE_WINDOW
23 #if ENABLE_DEBUG
24 using namespace testing::ext;
25 namespace OHOS {
26 class UIDialogTest : public testing::Test {
27 public:
28     static void SetUpTestCase(void);
29     static void TearDownTestCase(void);
30     static UIDialog* dialog_;
31     void SetUp(void);
32     void TearDown(void);
33 };
34 
35 UIDialog* UIDialogTest::dialog_ = nullptr;
36 UIView::OnClickListener* listener_ = nullptr;
37 
SetUpTestCase(void)38 void UIDialogTest::SetUpTestCase(void)
39 {
40     listener_ = new UIView::OnClickListener();
41 }
42 
SetUp(void)43 void UIDialogTest::SetUp(void)
44 {
45     if (dialog_ != nullptr) {
46         delete dialog_;
47     }
48     dialog_ = new UIDialog();
49 }
50 
51 
TearDown(void)52 void UIDialogTest::TearDown(void)
53 {
54     if (dialog_ != nullptr) {
55         delete dialog_;
56         dialog_ = nullptr;
57     }
58 }
59 
TearDownTestCase(void)60 void UIDialogTest::TearDownTestCase(void)
61 {
62     if (listener_ != nullptr) {
63         delete listener_;
64         listener_ = nullptr;
65     }
66 }
67 
68 /**
69  * @tc.name: UIDialogSetTitle_001
70  * @tc.desc: Verify SetTitle function, equal.
71  * @tc.type: FUNC
72  * @tc.require: AR000F4E5F
73  */
74 HWTEST_F(UIDialogTest, UIDialogSetTitle_001, TestSize.Level0)
75 {
76     const char* title1 = "title1";
77     dialog_->SetTitle(title1);
78     EXPECT_EQ(dialog_->GetTitle(), title1);
79     const char* title2 = "title2";
80     dialog_->SetTitle(title2);
81     EXPECT_EQ(dialog_->GetTitle(), title2);
82 }
83 
84 /**
85  * @tc.name: UIDialogSetTitle_002
86  * @tc.desc: Verify SetTitle function, equal.
87  * @tc.type: FUNC
88  * @tc.require: AR000F4E5F
89  */
90 HWTEST_F(UIDialogTest, UIDialogSetTitle_002, TestSize.Level1)
91 {
92     dialog_->SetTitle(nullptr);
93     EXPECT_EQ(dialog_->GetTitle(), nullptr);
94     const char* title = "title";
95     dialog_->SetTitle(title);
96     dialog_->SetTitle(nullptr);
97     EXPECT_EQ(dialog_->GetTitle(), title);
98 }
99 
100 /**
101  * @tc.name: UIDialogSetText_001
102  * @tc.desc: Verify SetText function, equal.
103  * @tc.type: FUNC
104  * @tc.require: AR000F4E5F
105  */
106 HWTEST_F(UIDialogTest, UIDialogSetText_001, TestSize.Level0)
107 {
108     const char* text1 = "text1";
109     dialog_->SetText(text1);
110     EXPECT_EQ(dialog_->GetText(), text1);
111     const char* text2 = "text2";
112     dialog_->SetText(text2);
113     EXPECT_EQ(dialog_->GetText(), text2);
114 }
115 
116 /**
117  * @tc.name: UIDialogSetText_002
118  * @tc.desc: Verify SetText function, equal.
119  * @tc.type: FUNC
120  * @tc.require: AR000F4E5F
121  */
122 HWTEST_F(UIDialogTest, UIDialogSetText_002, TestSize.Level1)
123 {
124     dialog_->SetText(nullptr);
125     EXPECT_EQ(dialog_->GetText(), nullptr);
126     const char* text = "text";
127     dialog_->SetText(text);
128     dialog_->SetText(nullptr);
129     EXPECT_EQ(dialog_->GetText(), text);
130 }
131 
132 /**
133  * @tc.name: UIDialogSetButton_001
134  * @tc.desc: Verify SetButton function, equal.
135  * @tc.type: FUNC
136  * @tc.require: AR000F4E5F
137  */
138 HWTEST_F(UIDialogTest, UIDialogSetButton_001, TestSize.Level0)
139 {
140     const char* buttonText = "button";
141     dialog_->SetButton(UIDialog::DialogButtonType::BUTTON_LEFT, buttonText, listener_);
142     const char* buttonText0 = dialog_->GetButtonText(UIDialog::DialogButtonType::BUTTON_LEFT);
143     ASSERT_TRUE(buttonText0);
144     if (strcmp(buttonText0, buttonText) != 0) {
145         EXPECT_EQ(1, 0);
146     }
147     EXPECT_EQ(dialog_->GetButtonListener(UIDialog::DialogButtonType::BUTTON_LEFT), listener_);
148 }
149 
150 /**
151  * @tc.name: UIDialogSetButtonColor_001
152  * @tc.desc: Verify SetButtonColor function, equal.
153  * @tc.type: FUNC
154  * @tc.require: AR000F4E5F
155  */
156 HWTEST_F(UIDialogTest, UIDialogSetButtonColor_001, TestSize.Level1)
157 {
158     dialog_->SetButton(UIDialog::DialogButtonType::BUTTON_LEFT, "button", listener_);
159     ColorType color = Color::Red();
160     dialog_->SetButtonColor(UIDialog::DialogButtonType::BUTTON_LEFT, color);
161     EXPECT_EQ(dialog_->GetButtonColor(UIDialog::DialogButtonType::BUTTON_LEFT).full, color.full);
162 }
163 
164 /**
165  * @tc.name: UIDialogSetOnCancelListener_001
166  * @tc.desc: Verify SetOnCancelListener function, equal.
167  * @tc.type: FUNC
168  * @tc.require: AR000F4E5F
169  */
170 HWTEST_F(UIDialogTest, UIDialogSetOnCancelListener_001, TestSize.Level1)
171 {
172     dialog_->SetOnCancelListener(nullptr);
173     EXPECT_EQ(dialog_->GetOnCancelListener(), nullptr);
174     dialog_->SetOnCancelListener(listener_);
175     EXPECT_EQ(dialog_->GetOnCancelListener(), listener_);
176 }
177 
178 /**
179  * @tc.name: UIDialogEnableAutoCancel_001
180  * @tc.desc: Verify EnableAutoCancel function, equal.
181  * @tc.type: FUNC
182  * @tc.require: AR000F4E5F
183  */
184 HWTEST_F(UIDialogTest, UIDialogEnableAutoCancel_001, TestSize.Level1)
185 {
186     dialog_->EnableAutoCancel(true);
187     EXPECT_EQ(dialog_->GetEnableAutoCancel(), true);
188     dialog_->EnableAutoCancel(false);
189     EXPECT_EQ(dialog_->GetEnableAutoCancel(), false);
190 }
191 
192 /**
193  * @tc.name: UIDialog_001
194  * @tc.desc: Verify UIDialog function, equal.
195  * @tc.type: FUNC
196  * @tc.require: SR000F3PED
197  */
198 HWTEST_F(UIDialogTest, UIDialog_001, TestSize.Level0)
199 {
200     const char* title = "title";
201     dialog_->SetTitle(title);
202     EXPECT_EQ(dialog_->GetTitle(), title);
203     const char* text = "text";
204     dialog_->SetText(text);
205     EXPECT_EQ(dialog_->GetText(), text);
206     const char* buttonText = "button";
207     dialog_->SetButton(UIDialog::DialogButtonType::BUTTON_LEFT, buttonText, listener_);
208     const char* buttonText0 = dialog_->GetButtonText(UIDialog::DialogButtonType::BUTTON_LEFT);
209     ASSERT_TRUE(buttonText0);
210     if (strcmp(buttonText0, buttonText) != 0) {
211         EXPECT_EQ(1, 0);
212     }
213     EXPECT_EQ(dialog_->GetButtonListener(UIDialog::DialogButtonType::BUTTON_LEFT), listener_);
214     dialog_->SetOnCancelListener(listener_);
215     EXPECT_EQ(dialog_->GetOnCancelListener(), listener_);
216     dialog_->EnableAutoCancel(true);
217     EXPECT_EQ(dialog_->GetEnableAutoCancel(), true);
218 }
219 } // namespace OHOS
220 #endif // ENABLE_DEBUG
221 #endif // ENABLE_WINDOW
222