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_abstract_progress.h"
17
18 #include <climits>
19 #include <gtest/gtest.h>
20
21 using namespace testing::ext;
22 namespace OHOS {
23 namespace {
24 const int16_t MAX_VALUE = 80;
25 const int16_t MEDIAN_VALUE = 50;
26 const int16_t MIN_VALUE = 20;
27 const char* foreground = "D:/";
28 const char* background = "D:/";
29 }
30 class UIAbsatrctProgressTest : public testing::Test {
31 public:
UIAbsatrctProgressTest()32 UIAbsatrctProgressTest() : abstractProgress_(nullptr) {}
~UIAbsatrctProgressTest()33 virtual ~UIAbsatrctProgressTest() {}
34 static void SetUpTestCase();
35 static void TearDownTestCase();
36 void SetUp();
37 void TearDown();
38 UIAbstractProgress* abstractProgress_;
39 };
40
SetUpTestCase()41 void UIAbsatrctProgressTest::SetUpTestCase()
42 {
43 }
44
TearDownTestCase()45 void UIAbsatrctProgressTest::TearDownTestCase()
46 {
47 }
48
SetUp()49 void UIAbsatrctProgressTest::SetUp()
50 {
51 if (abstractProgress_ == nullptr) {
52 abstractProgress_ = new UIAbstractProgress();
53 }
54 }
55
TearDown()56 void UIAbsatrctProgressTest::TearDown()
57 {
58 if (abstractProgress_ != nullptr) {
59 delete abstractProgress_;
60 abstractProgress_ = nullptr;
61 }
62 }
63 /**
64 * @tc.name: UIAbsatrctProgressGetViewType_001
65 * @tc.desc: Verify GetViewType function, equal.
66 * @tc.type: FUNC
67 * @tc.require: AR000DSMQG
68 */
69 HWTEST_F(UIAbsatrctProgressTest, UIAbsatrctProgressGetViewType_001, TestSize.Level1)
70 {
71 if (abstractProgress_ == nullptr) {
72 EXPECT_NE(0, 0);
73 return;
74 }
75 EXPECT_EQ(abstractProgress_->GetViewType(), UI_ABSTRACT_PROGRESS);
76 }
77
78 /**
79 * @tc.name: UIAbsatrctProgressSetValue_001
80 * @tc.desc: Verify SetValue function, equal.
81 * @tc.type: FUNC
82 * @tc.require: AR000DSMQG
83 */
84 HWTEST_F(UIAbsatrctProgressTest, UIAbsatrctProgressSetValue_001, TestSize.Level0)
85 {
86 if (abstractProgress_ == nullptr) {
87 EXPECT_NE(0, 0);
88 return;
89 }
90 abstractProgress_->SetRange(MAX_VALUE, MIN_VALUE);
91 EXPECT_EQ(abstractProgress_->GetRangeMin(), MIN_VALUE);
92 EXPECT_EQ(abstractProgress_->GetRangeMax(), MAX_VALUE);
93
94 abstractProgress_->SetValue(MEDIAN_VALUE);
95 EXPECT_EQ(abstractProgress_->GetValue(), MEDIAN_VALUE);
96
97 abstractProgress_->SetValue(MAX_VALUE + 1);
98 EXPECT_EQ(abstractProgress_->GetValue(), MAX_VALUE);
99
100 abstractProgress_->SetValue(MIN_VALUE - 1);
101 EXPECT_EQ(abstractProgress_->GetValue(), MIN_VALUE);
102 }
103
104 /**
105 * @tc.name: UIAbsatrctProgressSetValue_002
106 * @tc.desc: Verify SetValue function, equal.
107 * @tc.type: FUNC
108 * @tc.require: AR000DSMQG
109 */
110 HWTEST_F(UIAbsatrctProgressTest, UIAbsatrctProgressSetValue_002, TestSize.Level1)
111 {
112 if (abstractProgress_ == nullptr) {
113 EXPECT_NE(0, 0);
114 return;
115 }
116 abstractProgress_->SetRange(MAX_VALUE, MIN_VALUE);
117 EXPECT_EQ(abstractProgress_->GetRangeMin(), MIN_VALUE);
118 EXPECT_EQ(abstractProgress_->GetRangeMax(), MAX_VALUE);
119
120 abstractProgress_->SetRange(MIN_VALUE, MAX_VALUE);
121 EXPECT_EQ(abstractProgress_->GetRangeMin(), MIN_VALUE);
122 EXPECT_EQ(abstractProgress_->GetRangeMax(), MAX_VALUE);
123 }
124
125 /**
126 * @tc.name: UIAbsatrctProgressSetStep_001
127 * @tc.desc: Verify SetStep function, equal.
128 * @tc.type: FUNC
129 * @tc.require: AR000DSMQG
130 */
131 HWTEST_F(UIAbsatrctProgressTest, UIAbsatrctProgressSetStep_001, TestSize.Level1)
132 {
133 if (abstractProgress_ == nullptr) {
134 EXPECT_NE(0, 0);
135 return;
136 }
137 abstractProgress_->SetStep(MEDIAN_VALUE);
138 EXPECT_EQ(abstractProgress_->GetStep(), MEDIAN_VALUE);
139 }
140
141 /**
142 * @tc.name: UIAbsatrctProgressSetBackgroundStyle_001
143 * @tc.desc: Verify SetBackgroundStyle function, equal.
144 * @tc.type: FUNC
145 * @tc.require: AR000DSMQG
146 */
147 HWTEST_F(UIAbsatrctProgressTest, UIAbsatrctProgressSetBackgroundStyle_001, TestSize.Level1)
148 {
149 if (abstractProgress_ == nullptr) {
150 EXPECT_NE(0, 0);
151 return;
152 }
153 Style style;
154 style.imageOpa_ = OPA_TRANSPARENT;
155 style.lineOpa_ = OPA_TRANSPARENT;
156 style.borderRadius_ = 1;
157
158 abstractProgress_->SetBackgroundStyle(style);
159 EXPECT_EQ(abstractProgress_->GetBackgroundStyle().imageOpa_, OPA_TRANSPARENT);
160 EXPECT_EQ(abstractProgress_->GetBackgroundStyle().lineOpa_, OPA_TRANSPARENT);
161 EXPECT_EQ(abstractProgress_->GetBackgroundStyle().borderRadius_, 1);
162
163 abstractProgress_->SetBackgroundStyle(STYLE_BACKGROUND_COLOR, Color::Silver().full);
164 EXPECT_EQ(abstractProgress_->GetBackgroundStyle(STYLE_BACKGROUND_COLOR), Color::Silver().full);
165 }
166
167 /**
168 * @tc.name: UIAbsatrctProgressSetForegroundStyle_001
169 * @tc.desc: Verify SetForegroundStyle function, equal.
170 * @tc.type: FUNC
171 * @tc.require: AR000DSMQG
172 */
173 HWTEST_F(UIAbsatrctProgressTest, UIAbsatrctProgressSetForegroundStyle_001, TestSize.Level1)
174 {
175 if (abstractProgress_ == nullptr) {
176 EXPECT_NE(0, 0);
177 return;
178 }
179 Style style;
180 style.imageOpa_ = OPA_TRANSPARENT;
181 style.lineOpa_ = OPA_TRANSPARENT;
182 style.borderRadius_ = 1;
183
184 abstractProgress_->SetForegroundStyle(style);
185 EXPECT_EQ(abstractProgress_->GetForegroundStyle().imageOpa_, OPA_TRANSPARENT);
186 EXPECT_EQ(abstractProgress_->GetForegroundStyle().lineOpa_, OPA_TRANSPARENT);
187 EXPECT_EQ(abstractProgress_->GetForegroundStyle().borderRadius_, 1);
188
189 abstractProgress_->SetForegroundStyle(STYLE_BACKGROUND_COLOR, Color::White().full);
190 EXPECT_EQ(abstractProgress_->GetForegroundStyle(STYLE_BACKGROUND_COLOR), Color::White().full);
191 }
192
193 /**
194 * @tc.name: UIAbsatrctProgressSetCapType_001
195 * @tc.desc: Verify SetCapType function, equal.
196 * @tc.type: FUNC
197 * @tc.require: AR000DSMQG
198 */
199 HWTEST_F(UIAbsatrctProgressTest, UIAbsatrctProgressSetCapType_001, TestSize.Level1)
200 {
201 if (abstractProgress_ == nullptr) {
202 EXPECT_NE(0, 0);
203 return;
204 }
205 EXPECT_EQ(abstractProgress_->GetForegroundStyle(STYLE_LINE_CAP), CapType::CAP_NONE);
206 EXPECT_EQ(abstractProgress_->GetBackgroundStyle(STYLE_LINE_CAP), CapType::CAP_NONE);
207 abstractProgress_->SetCapType(CapType::CAP_ROUND);
208 EXPECT_EQ(abstractProgress_->GetForegroundStyle(STYLE_LINE_CAP), CapType::CAP_ROUND);
209 EXPECT_EQ(abstractProgress_->GetBackgroundStyle(STYLE_LINE_CAP), CapType::CAP_ROUND);
210 }
211
212 /**
213 * @tc.name: UIAbsatrctProgressSetImage_001
214 * @tc.desc: Verify SetImage function, equal.
215 */
216 HWTEST_F(UIAbsatrctProgressTest, UIAbsatrctProgressSetImage_001, TestSize.Level1)
217 {
218 if (abstractProgress_ == nullptr) {
219 EXPECT_NE(0, 0);
220 return;
221 }
222 const Image* backgroundImage = new Image();
223 const Image* foregroundImage = new Image();
224 foreground = foregroundImage->GetPath();
225 background = backgroundImage->GetPath();
226 abstractProgress_->SetImage(foreground, background);
227 if (foreground == nullptr) {
228 EXPECT_EQ(foregroundImage->GetSrcType(), 2); // 0 : IMG_SRC_VARIABLE 1 : IMG_SRC_FILE 2 : IMG_SRC_UNKNOWN
229 } else {
230 EXPECT_EQ(foregroundImage->GetSrcType(), 1); // 0 : IMG_SRC_VARIABLE 1 : IMG_SRC_FILE 2 : IMG_SRC_UNKNOWN
231 }
232 if (background == nullptr) {
233 EXPECT_EQ(backgroundImage->GetSrcType(), 2); // 0 : IMG_SRC_VARIABLE 1 : IMG_SRC_FILE 2 : IMG_SRC_UNKNOWN
234 } else {
235 EXPECT_EQ(backgroundImage->GetSrcType(), 1); // 0 : IMG_SRC_VARIABLE 1 : IMG_SRC_FILE 2 : IMG_SRC_UNKNOWN
236 }
237 delete backgroundImage;
238 backgroundImage = nullptr;
239 delete foregroundImage;
240 foregroundImage = nullptr;
241 }
242
243 /**
244 * @tc.name: UIAbsatrctProgressSetImage_002
245 * @tc.desc: Verify SetImage function, equal.
246 */
247 HWTEST_F(UIAbsatrctProgressTest, UIAbsatrctProgressSetImage_002, TestSize.Level1)
248 {
249 if (abstractProgress_ == nullptr) {
250 EXPECT_NE(0, 0);
251 return;
252 }
253 const Image* backgroundImage = new Image();
254 const Image* foregroundImage = new Image();
255 const Image* GroundImage = new Image();
256 ImageInfo* foregroundImageInfo = static_cast<ImageInfo*>(UIMalloc(sizeof(ImageInfo)));
257 ImageInfo* backgroundImageInfo = static_cast<ImageInfo*>(UIMalloc(sizeof(ImageInfo)));
258 ImageInfo* groundImageInfo = static_cast<ImageInfo*>(UIMalloc(sizeof(ImageInfo)));
259 abstractProgress_->SetImage(foregroundImageInfo, backgroundImageInfo);
260 if (foregroundImageInfo == nullptr && GroundImage->GetImageInfo() == nullptr) {
261 EXPECT_EQ(foregroundImage->GetSrcType(), 2); // 0 : IMG_SRC_VARIABLE 1 : IMG_SRC_FILE 2 : IMG_SRC_UNKNOWN
262 } else {
263 if (groundImageInfo == foregroundImageInfo) {
264 EXPECT_EQ(foregroundImage->GetSrcType(), 0); // 0 : IMG_SRC_VARIABLE 1 : IMG_SRC_FILE 2 : IMG_SRC_UNKNOWN
265 }
266 }
267 if (backgroundImageInfo == nullptr && GroundImage->GetImageInfo() == nullptr) {
268 EXPECT_EQ(backgroundImage->GetSrcType(), 2); // 0 : IMG_SRC_VARIABLE 1 : IMG_SRC_FILE 2 : IMG_SRC_UNKNOWN
269 } else {
270 if (groundImageInfo == backgroundImageInfo) {
271 EXPECT_EQ(backgroundImage->GetSrcType(), 0); // 0 : IMG_SRC_VARIABLE 1 : IMG_SRC_FILE 2 : IMG_SRC_UNKNOWN
272 }
273 }
274 delete backgroundImage;
275 backgroundImage = nullptr;
276 delete GroundImage;
277 GroundImage = nullptr;
278 delete foregroundImage;
279 foregroundImage = nullptr;
280 UIFree(foregroundImageInfo);
281 foregroundImageInfo = nullptr;
282 UIFree(backgroundImageInfo);
283 backgroundImageInfo = nullptr;
284 UIFree(groundImageInfo);
285 groundImageInfo = nullptr;
286 }
287 } // namespace OHOS
288