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