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