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_axis.h"
17
18 #include <climits>
19 #include <gtest/gtest.h>
20
21 using namespace testing::ext;
22
23 namespace OHOS {
24 namespace {
25 const int16_t LARGER_VALUE = 10;
26 const int16_t SMALLER_VALUE = 2;
27 const bool ENABLE = true;
28 }
29 class UIAxisTest : public testing::Test {
30 public:
31 static void SetUpTestCase(void);
32 static void TearDownTestCase(void);
33 static UIXAxis* axisX_;
34 static UIYAxis* axisY_;
35 };
36 UIXAxis* UIAxisTest::axisX_ = nullptr;
37 UIYAxis* UIAxisTest::axisY_ = nullptr;
38
SetUpTestCase(void)39 void UIAxisTest::SetUpTestCase(void)
40 {
41 if (axisX_ == nullptr) {
42 axisX_ = new UIXAxis();
43 }
44 if (axisY_ == nullptr) {
45 axisY_ = new UIYAxis();
46 }
47 }
48
TearDownTestCase(void)49 void UIAxisTest::TearDownTestCase(void)
50 {
51 if (axisX_ != nullptr) {
52 delete axisX_;
53 axisX_ = nullptr;
54 }
55 if (axisY_ != nullptr) {
56 delete axisY_;
57 axisY_ = nullptr;
58 }
59 }
60 /**
61 * @tc.name: UIAxisGetViewType_001
62 * @tc.desc: Verify GetViewType function, equal.
63 * @tc.type: FUNC
64 * @tc.require: AR000EEMQ8
65 */
66 HWTEST_F(UIAxisTest, UIAxisGetViewType_001, TestSize.Level1)
67 {
68 if (axisX_ == nullptr) {
69 EXPECT_EQ(1, 0);
70 return;
71 }
72 EXPECT_EQ(axisX_->GetViewType(), UI_AXIS);
73 }
74
75 /**
76 * @tc.name: UIAxisSetLineColor_001
77 * @tc.desc: Verify SetLineColor function, equal.
78 * @tc.type: FUNC
79 * @tc.require: AR000EEMQ8
80 */
81 HWTEST_F(UIAxisTest, UIAxisSetLineColor_001, TestSize.Level1)
82 {
83 if (axisY_ == nullptr) {
84 EXPECT_EQ(1, 0);
85 return;
86 }
87 axisY_->SetLineColor(Color::Gray());
88 EXPECT_EQ(axisY_->GetStyle(STYLE_LINE_COLOR), Color::Gray().full);
89 }
90
91 /**
92 * @tc.name: UIXAxisSetDataRange_001
93 * @tc.desc: Verify SetDataRange function, equal.
94 * @tc.type: FUNC
95 * @tc.require: AR000EEMQ8
96 */
97 HWTEST_F(UIAxisTest, UIXAxisSetDataRange_001, TestSize.Level0)
98 {
99 if (axisX_ == nullptr) {
100 EXPECT_EQ(1, 0);
101 return;
102 }
103 EXPECT_EQ(axisX_->SetDataRange(SMALLER_VALUE, LARGER_VALUE), true);
104 }
105
106 /**
107 * @tc.name: UIXAxisSetDataRange_002
108 * @tc.desc: Verify SetDataRange function, equal.
109 * @tc.type: FUNC
110 * @tc.require: AR000EEMQ8
111 */
112 HWTEST_F(UIAxisTest, UIXAxisSetDataRange_002, TestSize.Level1)
113 {
114 if (axisX_ == nullptr) {
115 EXPECT_EQ(1, 0);
116 return;
117 }
118 EXPECT_EQ(axisX_->SetDataRange(LARGER_VALUE, SMALLER_VALUE), false);
119 }
120
121 /**
122 * @tc.name: UIYAxisSetDataRange_001
123 * @tc.desc: Verify SetDataRange function, equal.
124 * @tc.type: FUNC
125 * @tc.require: AR000EEMQ8
126 */
127 HWTEST_F(UIAxisTest, UIYAxisSetDataRange_001, TestSize.Level1)
128 {
129 if (axisY_ == nullptr) {
130 EXPECT_EQ(1, 0);
131 return;
132 }
133 EXPECT_EQ(axisY_->SetDataRange(SMALLER_VALUE, LARGER_VALUE), true);
134 }
135
136 /**
137 * @tc.name: UIYAxisSetDataRange_002
138 * @tc.desc: Verify SetDataRange function, equal.
139 * @tc.type: FUNC
140 * @tc.require: AR000EEMQ8
141 */
142 HWTEST_F(UIAxisTest, UIYAxisSetDataRange_002, TestSize.Level1)
143 {
144 if (axisY_ == nullptr) {
145 EXPECT_EQ(1, 0);
146 return;
147 }
148 EXPECT_EQ(axisY_->SetDataRange(LARGER_VALUE, SMALLER_VALUE), false);
149 }
150
151 /**
152 * @tc.name: UIYAxisSetMarkNum_001
153 * @tc.desc: Verify SetMarkNum function, equal.
154 */
155 HWTEST_F(UIAxisTest, UIXAxisSetMarkNum_001, TestSize.Level1)
156 {
157 if (axisY_ == nullptr) {
158 EXPECT_EQ(1, 0);
159 return;
160 }
161 int16_t COUNT = 1;
162 axisX_->SetMarkNum(COUNT);
163 EXPECT_EQ(axisX_->GetMarkNum(), COUNT);
164 }
165
166 /**
167 * @tc.name: UIYAxisSetMarkNum_001
168 * @tc.desc: Verify SetMarkNum function, equal.
169 */
170 HWTEST_F(UIAxisTest, UIYAxisSetMarkNum_001, TestSize.Level1)
171 {
172 if (axisY_ == nullptr) {
173 EXPECT_EQ(1, 0);
174 return;
175 }
176 int16_t COUNT = 1;
177 axisY_->SetMarkNum(COUNT);
178 EXPECT_EQ(axisY_->GetMarkNum(), COUNT);
179 COUNT = 2;
180 axisY_->SetMarkNum(COUNT);
181 axisY_->UpdateAxis();
182 EXPECT_EQ(axisY_->GetMarkNum(), COUNT);
183 }
184
185 /**
186 * @tc.name: UIAxisEnableReverse_001
187 * @tc.desc: Verify EnableReverse function, equal.
188 */
189 HWTEST_F(UIAxisTest, UIAxisEnableReverse_001, TestSize.Level1)
190 {
191 if (axisX_ == nullptr) {
192 EXPECT_EQ(1, 0);
193 return;
194 }
195 axisX_->EnableReverse(ENABLE);
196 EXPECT_EQ(axisX_->GetEnableReverse(), ENABLE);
197 }
198
199 /**
200 * @tc.name: UIAxisGetStartPoint_001
201 * @tc.desc: Verify GetStartPoint function, equal.
202 */
203 HWTEST_F(UIAxisTest, UIAxisGetStartPoint_001, TestSize.Level1)
204 {
205 if (axisX_ == nullptr) {
206 EXPECT_EQ(1, 0);
207 return;
208 }
209 Point start_ = {0, 0};
210 EXPECT_EQ(axisX_->GetStartPoint().x, start_.x);
211 EXPECT_EQ(axisX_->GetStartPoint().y, start_.y);
212 }
213
214 /**
215 * @tc.name: UIAxisGetEndPoint_001
216 * @tc.desc: Verify GetEndPoint function, equal.
217 */
218 HWTEST_F(UIAxisTest, UIAxisGetEndPoint_001, TestSize.Level1)
219 {
220 if (axisX_ == nullptr) {
221 EXPECT_EQ(1, 0);
222 return;
223 }
224 Point end_ = {0, 0};
225 EXPECT_EQ(axisX_->GetEndPoint().x, end_.x);
226 EXPECT_EQ(axisX_->GetEndPoint().y, end_.y);
227 }
228
229 /**
230 * @tc.name: UIAxisGetMarkInterval_001
231 * @tc.desc: Verify GetMarkInterval function, equal.
232 */
233 HWTEST_F(UIAxisTest, UIAxisGetMarkInterval_001, TestSize.Level1)
234 {
235 if (axisX_ == nullptr) {
236 EXPECT_EQ(1, 0);
237 return;
238 }
239 float markInterval = 0;
240 axisX_->SetMarkInterval(markInterval);
241 EXPECT_EQ(axisX_->GetMarkInterval(), markInterval);
242 }
243
244 /**
245 * @tc.name: UIAxisOnPreDraw_001
246 * @tc.desc: Verify OnPreDraw function, equal.
247 */
248 HWTEST_F(UIAxisTest, UIAxisOnPreDraw_001, TestSize.Level1)
249 {
250 if (axisX_ == nullptr) {
251 EXPECT_EQ(1, 0);
252 return;
253 }
254 Rect* invalidatedArea = new Rect();
255 EXPECT_EQ(axisX_->OnPreDraw(*invalidatedArea), false);
256 delete invalidatedArea;
257 invalidatedArea = nullptr;
258 }
259
260 /**
261 * @tc.name: UIAxisUpdateAxisPoints_001
262 * @tc.desc: Verify UpdateAxisPoints function, equal.
263 */
264 HWTEST_F(UIAxisTest, UIAxisUpdateAxisPoints_001, TestSize.Level1)
265 {
266 if (axisX_ == nullptr) {
267 EXPECT_EQ(1, 0);
268 return;
269 }
270 axisX_->UpdateAxisPoints();
271 Point endx = axisX_->GetEndPoint();
272 Point startx = axisX_->GetStartPoint();
273 EXPECT_EQ(endx.y, startx.y);
274 }
275
276 /**
277 * @tc.name: UIAxisUpdateAxisPoints_002
278 * @tc.desc: Verify UpdateAxisPoints function, equal.
279 */
280 HWTEST_F(UIAxisTest, UIAxisUpdateAxisPoints_002, TestSize.Level1)
281 {
282 if (axisY_ == nullptr) {
283 EXPECT_EQ(1, 0);
284 return;
285 }
286 axisY_->UpdateAxisPoints();
287 Point endx = axisY_->GetEndPoint();
288 Point startx = axisY_->GetStartPoint();
289 EXPECT_EQ(endx.x, startx.x);
290 }
291
292 /**
293 * @tc.name: UIAxisTranslateToPixel_001
294 * @tc.desc: Verify TranslateToPixel function, equal.
295 */
296 HWTEST_F(UIAxisTest, UIAxisTranslateToPixel_001, TestSize.Level1)
297 {
298 if (axisX_ == nullptr) {
299 EXPECT_EQ(1, 0);
300 return;
301 }
302 int16_t value = 2;
303 float minXStep = 0;
304 Point startx = axisX_->GetStartPoint();
305 axisX_->TranslateToPixel(value);
306 int16_t value2 = startx.x + static_cast<int16_t>((value - 0) * minXStep);
307 EXPECT_EQ(value, value2);
308 }
309
310 /**
311 * @tc.name: UIAxisTranslateToPixel_002
312 * @tc.desc: Verify TranslateToPixel function, equal.
313 */
314 HWTEST_F(UIAxisTest, UIAxisTranslateToPixel_002, TestSize.Level1)
315 {
316 if (axisY_ == nullptr) {
317 EXPECT_EQ(1, 0);
318 return;
319 }
320 int16_t value = 2;
321 float minYStep = 0;
322 Point starty = axisY_->GetStartPoint();
323 axisX_->TranslateToPixel(value);
324 int16_t value2 = starty.y + static_cast<int16_t>((0 - value + 0) * minYStep);
325 EXPECT_EQ(value, value2);
326 }
327
328 /**
329 * @tc.name: UIAxisTranslateToPixel_003
330 * @tc.desc: Verify TranslateToPixel function, equal.
331 */
332 HWTEST_F(UIAxisTest, UIAxisTranslateToPixel_003, TestSize.Level1)
333 {
334 if (axisY_ == nullptr) {
335 EXPECT_EQ(1, 0);
336 return;
337 }
338 int16_t value = 2;
339 float minYStep = 0;
340 Point starty = axisY_->GetStartPoint();
341 axisY_->TranslateToPixel(value);
342 int16_t value2 = starty.y - static_cast<int16_t>((value - 0) * minYStep);
343 EXPECT_EQ(value, value2);
344 }
345 } // namespace OHOS
346