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/ui_button.h"
17
18 #include <climits>
19 #include <gtest/gtest.h>
20 #include "test_resource_config.h"
21 #include "draw/draw_utils.h"
22
23 using namespace testing::ext;
24
25 namespace OHOS {
26 namespace {
27 const Point INIT_POS = { 10, 12 };
28 }
29 class UIButtonTest : public testing::Test {
30 public:
UIButtonTest()31 UIButtonTest() : button_(nullptr) {}
~UIButtonTest()32 virtual ~UIButtonTest() {}
SetUpTestCase()33 static void SetUpTestCase() {}
TearDownTestCase()34 static void TearDownTestCase() {}
35 void TearDown();
36 void SetUp();
37 UIButton* button_;
38 };
39
SetUp()40 void UIButtonTest::SetUp()
41 {
42 if (button_ == nullptr) {
43 button_ = new UIButton();
44 }
45 }
46
TearDown()47 void UIButtonTest::TearDown()
48 {
49 if (button_ != nullptr) {
50 delete button_;
51 button_ = nullptr;
52 }
53 }
54
55 /**
56 * @tc.name: UIButtonGetViewType_001
57 * @tc.desc: Verify GetViewType function.
58 * @tc.type: FUNC
59 * @tc.require: SR000DRSH1
60 */
61 HWTEST_F(UIButtonTest, UIButtonGetViewType_001, TestSize.Level1)
62 {
63 if (button_ == nullptr) {
64 EXPECT_NE(0, 0);
65 return;
66 }
67 EXPECT_EQ(button_->GetViewType(), UI_BUTTON);
68 }
69
70 /**
71 * @tc.name: UIButtonSetImageSrc_001
72 * @tc.desc: Verify SetImageSrc function.
73 * @tc.type: FUNC
74 * @tc.require: SR000DRSH1
75 */
76 HWTEST_F(UIButtonTest, UIButtonSetImageSrc_001, TestSize.Level0)
77 {
78 if (button_ == nullptr) {
79 EXPECT_NE(0, 0);
80 return;
81 }
82 button_->SetImageSrc(BLUE_RGB888_IMAGE_PATH, BLUE_RGB565_IMAGE_PATH);
83 ASSERT_TRUE(button_->GetCurImageSrc());
84
85 if (button_->GetCurImageSrc()->GetPath() == nullptr) {
86 EXPECT_NE(0, 0);
87 return;
88 }
89 EXPECT_EQ(strcmp(button_->GetCurImageSrc()->GetPath(), BLUE_RGB888_IMAGE_PATH), 0);
90 PressEvent event(INIT_POS);
91 button_->OnPressEvent(event);
92 if (button_->GetCurImageSrc()->GetPath() == nullptr) {
93 EXPECT_NE(0, 0);
94 return;
95 }
96 EXPECT_EQ(strcmp(button_->GetCurImageSrc()->GetPath(), BLUE_RGB565_IMAGE_PATH), 0);
97 }
98
99 /**
100 * @tc.name:UIButtonSetImagePosition_001
101 * @tc.desc: Verify SetImagePosition function.
102 * @tc.type: FUNC
103 * @tc.require: SR000DRSH1
104 */
105 HWTEST_F(UIButtonTest, UIButtonSetImagePosition_001, TestSize.Level1)
106 {
107 if (button_ == nullptr) {
108 EXPECT_NE(0, 0);
109 return;
110 }
111 button_->SetImagePosition(INIT_POS.x, INIT_POS.y);
112 EXPECT_EQ(button_->GetImageX(), INIT_POS.x);
113 EXPECT_EQ(button_->GetImageY(), INIT_POS.y);
114 }
115
116 /**
117 * @tc.name: UIButtonSetStyle_001
118 * @tc.desc: Verify SetStyle function.
119 * @tc.type: FUNC
120 * @tc.require: SR000DRSH1
121 */
122 HWTEST_F(UIButtonTest, UIButtonSetStyle_001, TestSize.Level1)
123 {
124 if (button_ == nullptr) {
125 EXPECT_NE(0, 0);
126 return;
127 }
128 button_->SetStateForStyle(UIButton::ButtonState::PRESSED);
129 button_->SetStyle(STYLE_BACKGROUND_COLOR, Color::Blue().full);
130 EXPECT_EQ(button_->GetStyle(STYLE_BACKGROUND_COLOR), Color::Blue().full);
131 EXPECT_EQ(button_->GetStyleForState(STYLE_BACKGROUND_COLOR, UIButton::ButtonState::PRESSED), Color::Blue().full);
132 }
133
134 /**
135 * @tc.name: UIButtonSetStyle_002
136 * @tc.desc: Verify SetStyle function.
137 * @tc.type: FUNC
138 * @tc.require: SR000DRSH1
139 */
140 HWTEST_F(UIButtonTest, UIButtonSetStyle_002, TestSize.Level1)
141 {
142 if (button_ == nullptr) {
143 EXPECT_NE(0, 0);
144 return;
145 }
146 button_->SetStateForStyle(UIButton::ButtonState::INACTIVE);
147 button_->SetStyle(STYLE_BACKGROUND_COLOR, Color::Red().full);
148 EXPECT_EQ(button_->GetStyle(STYLE_BACKGROUND_COLOR), Color::Red().full);
149 EXPECT_EQ(button_->GetStyleForState(STYLE_BACKGROUND_COLOR, UIButton::ButtonState::INACTIVE), Color::Red().full);
150 }
151
152 /**
153 * @tc.name: UIButtonSetStyle_003
154 * @tc.desc: Verify SetStyle function.
155 * @tc.type: FUNC
156 * @tc.require: SR000DRSH1
157 */
158 HWTEST_F(UIButtonTest, UIButtonSetStyle_003, TestSize.Level1)
159 {
160 if (button_ == nullptr) {
161 EXPECT_NE(0, 0);
162 return;
163 }
164 button_->SetStateForStyle(UIButton::ButtonState::RELEASED);
165 button_->SetStyle(STYLE_BACKGROUND_COLOR, Color::Green().full);
166 EXPECT_EQ(button_->GetStyle(STYLE_BACKGROUND_COLOR), Color::Green().full);
167 EXPECT_EQ(button_->GetStyleForState(STYLE_BACKGROUND_COLOR, UIButton::ButtonState::RELEASED), Color::Green().full);
168 }
169
170 /**
171 * @tc.name: UIButtonIsTouchable_001
172 * @tc.desc: Verify IsTouchable function, equal.
173 * @tc.type: FUNC
174 * @tc.require: SR000DRSH1
175 */
176 HWTEST_F(UIButtonTest, UIButtonIsTouchable_001, TestSize.Level1)
177 {
178 if (button_ == nullptr) {
179 EXPECT_NE(0, 0);
180 return;
181 }
182 button_->Enable();
183 EXPECT_EQ(button_->IsTouchable(), true);
184 button_->Disable();
185 EXPECT_EQ(button_->IsTouchable(), false);
186 }
187
188 /**
189 * @tc.name: UIButtonSetSize_001
190 * @tc.desc: Verify SetSize function, equal.
191 * @tc.type: FUNC
192 * @tc.require: SR000DRSH1
193 */
194 HWTEST_F(UIButtonTest, UIButtonSetSize_001, TestSize.Level0)
195 {
196 if (button_ == nullptr) {
197 EXPECT_NE(0, 0);
198 return;
199 }
200 const int16_t releasedWidth = 5;
201 const int16_t releasedHeight = 25;
202 const int16_t paddingdLeft = 10;
203 const int16_t paddingdTop = 20;
204 const int16_t borderWidth = 2;
205 const int16_t posX = 50;
206 const int16_t posY = 100;
207 button_->SetStyleForState(STYLE_PADDING_LEFT, paddingdLeft, UIButton::ButtonState::RELEASED);
208 button_->SetStyleForState(STYLE_BORDER_WIDTH, borderWidth, UIButton::ButtonState::RELEASED);
209 button_->SetStyleForState(STYLE_PADDING_TOP, paddingdTop, UIButton::ButtonState::RELEASED);
210
211 ReleaseEvent releaseEvent(INIT_POS);
212 button_->OnReleaseEvent(releaseEvent);
213 button_->SetPosition(posX, posY);
214 button_->SetWidth(releasedWidth);
215 button_->SetHeight(releasedHeight);
216 EXPECT_EQ(button_->GetWidth(), releasedWidth);
217 EXPECT_EQ(button_->GetHeight(), releasedHeight);
218 EXPECT_EQ(button_->GetContentRect().GetWidth(), releasedWidth);
219 EXPECT_EQ(button_->GetContentRect().GetHeight(), releasedHeight);
220 EXPECT_EQ(button_->GetContentRect().GetX(), posX + paddingdLeft + borderWidth);
221 EXPECT_EQ(button_->GetContentRect().GetY(), posY + paddingdTop + borderWidth);
222 }
223
224 /**
225 * @tc.name: UIButtonSetSize_002
226 * @tc.desc: Verify SetSize function, equal.
227 * @tc.type: FUNC
228 * @tc.require: SR000DRSH1
229 */
230 HWTEST_F(UIButtonTest, UIButtonSetSize_002, TestSize.Level1)
231 {
232 if (button_ == nullptr) {
233 EXPECT_NE(0, 0);
234 return;
235 }
236 const int16_t pressWidth = 5;
237 const int16_t pressHeight = 25;
238 const int16_t paddingdLeft = 10;
239 const int16_t paddingdTop = 20;
240 const int16_t borderWidth = 2;
241 const int16_t posX = 50;
242 const int16_t posY = 100;
243 button_->SetStyleForState(STYLE_PADDING_LEFT, paddingdLeft, UIButton::ButtonState::PRESSED);
244 button_->SetStyleForState(STYLE_BORDER_WIDTH, borderWidth, UIButton::ButtonState::PRESSED);
245 button_->SetStyleForState(STYLE_PADDING_TOP, paddingdTop, UIButton::ButtonState::PRESSED);
246
247 PressEvent pressEvent(INIT_POS);
248 button_->OnPressEvent(pressEvent);
249 button_->SetPosition(posX, posY);
250 button_->SetWidth(pressWidth);
251 button_->SetHeight(pressHeight);
252 EXPECT_EQ(button_->GetWidth(), pressWidth);
253 EXPECT_EQ(button_->GetHeight(), pressHeight);
254 EXPECT_EQ(button_->GetContentRect().GetWidth(), pressWidth);
255 EXPECT_EQ(button_->GetContentRect().GetHeight(), pressHeight);
256 EXPECT_EQ(button_->GetContentRect().GetX(), posX + paddingdLeft + borderWidth);
257 EXPECT_EQ(button_->GetContentRect().GetY(), posY + paddingdTop + borderWidth);
258 }
259
260 /**
261 * @tc.name: UIButtonSetSize_003
262 * @tc.desc: Verify SetSize function, equal.
263 * @tc.type: FUNC
264 * @tc.require: SR000DRSH1
265 */
266 HWTEST_F(UIButtonTest, UIButtonSetSize_003, TestSize.Level1)
267 {
268 if (button_ == nullptr) {
269 EXPECT_NE(0, 0);
270 return;
271 }
272 const int16_t inactiveWidth = 5;
273 const int16_t inactiveHeight = 25;
274 const int16_t paddingdLeft = 10;
275 const int16_t paddingdTop = 20;
276 const int16_t borderWidth = 2;
277 const int16_t posX = 50;
278 const int16_t posY = 100;
279 button_->SetStyleForState(STYLE_PADDING_LEFT, paddingdLeft, UIButton::ButtonState::INACTIVE);
280 button_->SetStyleForState(STYLE_BORDER_WIDTH, borderWidth, UIButton::ButtonState::INACTIVE);
281 button_->SetStyleForState(STYLE_PADDING_TOP, paddingdTop, UIButton::ButtonState::INACTIVE);
282
283 button_->Disable();
284 button_->SetPosition(posX, posY);
285 button_->SetWidth(inactiveWidth);
286 button_->SetHeight(inactiveHeight);
287 EXPECT_EQ(button_->GetWidth(), inactiveWidth);
288 EXPECT_EQ(button_->GetHeight(), inactiveHeight);
289 EXPECT_EQ(button_->GetContentRect().GetWidth(), inactiveWidth);
290 EXPECT_EQ(button_->GetContentRect().GetHeight(), inactiveHeight);
291 EXPECT_EQ(button_->GetContentRect().GetX(), posX + paddingdLeft + borderWidth);
292 EXPECT_EQ(button_->GetContentRect().GetY(), posY + paddingdTop + borderWidth);
293 }
294
295 /**
296 * @tc.name: UIButtonSetStyleForState_001
297 * @tc.desc: Verify SetStyleForState function, equal.
298 * @tc.type: FUNC
299 * @tc.require: SR000DRSH1
300 */
301 HWTEST_F(UIButtonTest, UIButtonSetStyleForState_001, TestSize.Level1)
302 {
303 if (button_ == nullptr) {
304 EXPECT_NE(0, 0);
305 return;
306 }
307 button_->SetStyleForState(STYLE_BACKGROUND_COLOR, Color::Red().full, UIButton::ButtonState::RELEASED);
308 EXPECT_EQ(button_->GetStyleForState(STYLE_BACKGROUND_COLOR, UIButton::ButtonState::RELEASED),
309 Color::Red().full);
310 button_->SetStyleForState(STYLE_BACKGROUND_COLOR, Color::Green().full, UIButton::ButtonState::PRESSED);
311 EXPECT_EQ(button_->GetStyleForState(STYLE_BACKGROUND_COLOR, UIButton::ButtonState::PRESSED),
312 Color::Green().full);
313 button_->SetStyleForState(STYLE_BACKGROUND_COLOR, Color::Yellow().full, UIButton::ButtonState::INACTIVE);
314 EXPECT_EQ(button_->GetStyleForState(STYLE_BACKGROUND_COLOR, UIButton::ButtonState::INACTIVE),
315 Color::Yellow().full);
316 }
317
318 /**
319 * @tc.name: UIButtonSetStateForStyle_001
320 * @tc.desc: Verify SetStyle function.
321 * @tc.type: FUNC
322 * @tc.require: SR000DRSH1
323 */
324 HWTEST_F(UIButtonTest, UIButtonSetStateForStyle_001, TestSize.Level1)
325 {
326 if (button_ == nullptr) {
327 EXPECT_NE(0, 0);
328 return;
329 }
330 button_->SetStyleForState(STYLE_BACKGROUND_COLOR, Color::Red().full, UIButton::ButtonState::RELEASED);
331 EXPECT_EQ(button_->GetStyleForState(STYLE_BACKGROUND_COLOR, UIButton::ButtonState::RELEASED),
332 Color::Red().full);
333 button_->SetStyleForState(STYLE_BACKGROUND_COLOR, Color::Green().full, UIButton::ButtonState::PRESSED);
334 EXPECT_EQ(button_->GetStyleForState(STYLE_BACKGROUND_COLOR, UIButton::ButtonState::PRESSED),
335 Color::Green().full);
336 button_->SetStateForStyle(UIButton::ButtonState::RELEASED);
337 EXPECT_EQ(button_->GetStyle(STYLE_BACKGROUND_COLOR), Color::Red().full);
338 button_->SetStateForStyle(UIButton::ButtonState::PRESSED);
339 EXPECT_EQ(button_->GetStyle(STYLE_BACKGROUND_COLOR), Color::Green().full);
340 }
341
342 /**
343 * @tc.name: UIButtonSetImageSrc_002
344 * @tc.desc: Verify SetImageSrc function.
345 */
346 HWTEST_F(UIButtonTest, UIButtonSetImageSrc_002, TestSize.Level0)
347 {
348 if (button_ == nullptr) {
349 EXPECT_NE(0, 0);
350 return;
351 }
352 ImageInfo* defaultImgSrc = static_cast<ImageInfo*>(UIMalloc(sizeof(ImageInfo)));
353 ImageInfo* triggeredImgSrc = static_cast<ImageInfo*>(UIMalloc(sizeof(ImageInfo)));
354 button_->SetImageSrc(defaultImgSrc, triggeredImgSrc);
355 ASSERT_TRUE(button_->GetCurImageSrc());
356 EXPECT_EQ(button_->GetCurImageSrc()->GetSrcType(), IMG_SRC_VARIABLE);
357 UIFree(triggeredImgSrc);
358 triggeredImgSrc = nullptr;
359 UIFree(defaultImgSrc);
360 defaultImgSrc = nullptr;
361 }
362
363 /**
364 * @tc.name: UIButtonEnableButtonAnimation_001
365 * @tc.desc: Verify EnableButtonAnimation function.
366 */
367 HWTEST_F(UIButtonTest, UIButtonEnableButtonAnimation_001, TestSize.Level0)
368 {
369 if (button_ == nullptr) {
370 EXPECT_NE(0, 0);
371 return;
372 }
373 bool enable = true;
374 button_->EnableButtonAnimation(enable);
375 EXPECT_EQ(button_->GetEnableButtonAnimation(), enable);
376 }
377 } // namespace OHOS
378