• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_canvas.h"
18 #include "components/ui_view_group.h"
19 
20 #include <climits>
21 #include <gtest/gtest.h>
22 
23 using namespace testing::ext;
24 namespace OHOS {
25 namespace {
26     const int16_t POS_X = 50;
27     const int16_t POS_Y = 100;
28     const uint16_t WIDTH = 100;
29     const uint16_t HEIGHT = 100;
30     const int16_t START1_X = 10;
31     const int16_t START1_Y = 20;
32     const int16_t LINE1_X = 40;
33     const int16_t LINE1_Y = 100;
34     const int16_t LINE2_X = 50;
35     const int16_t LINE2_Y = 120;
36     const int16_t CENTER_X = 150;
37     const int16_t CENTER_Y = 150;
38     const int16_t RADIUS = 50;
39     const int16_t START_ANGLE = 30;
40     const int16_t END_ANGLE = 250;
41     const int16_t RECT_X = 250;
42     const int16_t RECT_Y = 50;
43     const int16_t RECT_WIDTH = 100;
44     const int16_t RECT_HEIGHT = 50;
45 }
46 
47 class TestUICanvas : public UICanvas {
48 public:
TestUICanvas()49     TestUICanvas() {}
~TestUICanvas()50     virtual ~TestUICanvas() {}
51 
GetPath() const52     const UICanvasPath* GetPath() const
53     {
54         return path_;
55     }
56 
GetStartPos() const57     Point GetStartPos() const
58     {
59         if (path_ != nullptr) {
60             return path_->startPos_;
61         } else {
62             return { COORD_MIN, COORD_MIN };
63         }
64     }
65 
GetEndPos() const66     Point GetEndPos() const
67     {
68         if (path_ != nullptr) {
69             return path_->points_.Tail()->data_;
70         } else {
71             return { COORD_MIN, COORD_MIN };
72         }
73     }
74 };
75 
76 class UICanvasTest : public testing::Test {
77 public:
SetUpTestCase()78     static void SetUpTestCase() {}
TearDownTestCase()79     static void TearDownTestCase() {}
80     void SetUp();
81     void TearDown();
82     static Paint* paint_;
83     static TestUICanvas* canvas_;
84 };
85 
86 Paint* UICanvasTest::paint_ = nullptr;
87 TestUICanvas* UICanvasTest::canvas_ = nullptr;
88 
SetUp()89 void UICanvasTest::SetUp()
90 {
91     if (paint_ == nullptr) {
92         paint_ = new Paint();
93     }
94     if (canvas_ == nullptr) {
95         canvas_ = new TestUICanvas();
96     }
97 }
98 
TearDown()99 void UICanvasTest::TearDown()
100 {
101     if (paint_ != nullptr) {
102         delete paint_;
103         paint_ = nullptr;
104     }
105     if (canvas_ != nullptr) {
106         delete canvas_;
107         canvas_ = nullptr;
108     }
109 }
110 /**
111  * @tc.name: UICanvasSetPaintStyle_001
112  * @tc.desc: Verify SetStyle function, equal.
113  * @tc.type: FUNC
114  * @tc.require: AR000DSMPV
115  */
116 HWTEST_F(UICanvasTest, UICanvasSetPaintStyle_001, TestSize.Level1)
117 {
118     if (paint_ == nullptr) {
119         EXPECT_EQ(1, 0);
120         return;
121     }
122     Paint::PaintStyle paintStyle = Paint::PaintStyle::FILL_STYLE;
123 
124     paint_->SetStyle(Paint::PaintStyle::FILL_STYLE);
125     EXPECT_EQ(paint_->GetStyle(), paintStyle);
126 }
127 
128 /**
129  * @tc.name: UICanvasSetStrokeWidth_001
130  * @tc.desc: Verify SetStrokeWidth function, equal.
131  * @tc.type: FUNC
132  * @tc.require: AR000DSMPV
133  */
134 HWTEST_F(UICanvasTest, UICanvasSetStrokeWidth_001, TestSize.Level1)
135 {
136     if (paint_ == nullptr) {
137         EXPECT_EQ(1, 0);
138         return;
139     }
140     paint_->SetStrokeWidth(WIDTH);
141     EXPECT_EQ(paint_->GetStrokeWidth(), WIDTH);
142 }
143 
144 /**
145  * @tc.name: UICanvasSetStrokeColor_001
146  * @tc.desc: Verify SetStrokeColor function, equal.
147  * @tc.type: FUNC
148  * @tc.require: AR000DSMPV
149  */
150 HWTEST_F(UICanvasTest, UICanvasSetStrokeColor_001, TestSize.Level1)
151 {
152     if (paint_ == nullptr) {
153         EXPECT_EQ(1, 0);
154         return;
155     }
156     ColorType color = Color::Red();
157 
158     paint_->SetStrokeColor(color);
159     EXPECT_EQ(paint_->GetStrokeColor().full, color.full);
160 }
161 
162 /**
163  * @tc.name: UICanvasSetFillColor_001
164  * @tc.desc: Verify SetFillColor function, equal.
165  * @tc.type: FUNC
166  * @tc.require: AR000DSMPV
167  */
168 HWTEST_F(UICanvasTest, UICanvasSetFillColor_001, TestSize.Level1)
169 {
170     if (paint_ == nullptr) {
171         EXPECT_EQ(1, 0);
172         return;
173     }
174     ColorType color = Color::Red();
175 
176     paint_->SetFillColor(color);
177     EXPECT_EQ(paint_->GetFillColor().full, color.full);
178 }
179 
180 /**
181  * @tc.name: UICanvasSetOpacity_001
182  * @tc.desc: Verify SetOpacity function, equal.
183  * @tc.type: FUNC
184  * @tc.require: AR000DSMPV
185  */
186 HWTEST_F(UICanvasTest, UICanvasSetOpacity_001, TestSize.Level1)
187 {
188     if (paint_ == nullptr) {
189         EXPECT_EQ(1, 0);
190         return;
191     }
192     paint_->SetOpacity(OPA_OPAQUE);
193     EXPECT_EQ(paint_->GetOpacity(), OPA_OPAQUE);
194 }
195 
196 /**
197  * @tc.name: UICanvasSetStartPosition_001
198  * @tc.desc: Verify SetStartPosition function, equal.
199  * @tc.type: FUNC
200  * @tc.require: AR000DSMPV
201  */
202 HWTEST_F(UICanvasTest, UICanvasSetStartPosition_001, TestSize.Level1)
203 {
204     if (canvas_ == nullptr) {
205         EXPECT_EQ(1, 0);
206         return;
207     }
208     canvas_->SetStartPosition({ POS_X, POS_Y });
209     EXPECT_EQ(canvas_->GetStartPosition().x, POS_X);
210     EXPECT_EQ(canvas_->GetStartPosition().y, POS_Y);
211 }
212 
213 /**
214  * @tc.name: UICanvasDrawLine_001
215  * @tc.desc: Verify DrawLine function, equal.
216  * @tc.type: FUNC
217  * @tc.require: AR000DSMPV
218  */
219 HWTEST_F(UICanvasTest, UICanvasDrawLine_001, TestSize.Level0)
220 {
221     if (canvas_ == nullptr) {
222         EXPECT_EQ(1, 0);
223         return;
224     }
225     RootView* rootView = RootView::GetInstance();
226     UIViewGroup* viewGroup = static_cast<UIViewGroup*>(rootView);
227     Paint paint;
228     Point startPoint = { 0, 50 };
229     Point endPoint = { 50, 100 };
230 
231     viewGroup->SetPosition(0, 0);
232     viewGroup->SetWidth(WIDTH);
233     viewGroup->SetHeight(HEIGHT);
234     canvas_->DrawLine(startPoint, endPoint, paint);
235     viewGroup->Add(canvas_);
236     EXPECT_EQ(canvas_->GetStartPosition().x, endPoint.x);
237     EXPECT_EQ(canvas_->GetStartPosition().y, endPoint.y);
238     viewGroup->Remove(canvas_);
239 }
240 
241 /**
242  * @tc.name: UICanvasDrawCurve_001
243  * @tc.desc: Verify DrawCurve function, equal.
244  * @tc.type: FUNC
245  * @tc.require: AR000DSMPV
246  */
247 HWTEST_F(UICanvasTest, UICanvasDrawCurve_001, TestSize.Level0)
248 {
249     if (canvas_ == nullptr) {
250         EXPECT_EQ(1, 0);
251         return;
252     }
253     RootView* rootView = RootView::GetInstance();
254     UIViewGroup* viewGroup = static_cast<UIViewGroup*>(rootView);
255     Paint paint;
256     Point startPoint = { 100, 100 };
257     Point endPoint = { 150, 200 };
258     Point control1Point = { 150, 100 };
259     Point control2Point = { 150, 100 };
260 
261     viewGroup->SetPosition(0, 0);
262     viewGroup->SetWidth(WIDTH);
263     viewGroup->SetHeight(HEIGHT);
264     canvas_->DrawCurve(startPoint, control1Point, control2Point, endPoint, paint);
265     viewGroup->Add(canvas_);
266     EXPECT_EQ(canvas_->GetStartPosition().x, endPoint.x);
267     EXPECT_EQ(canvas_->GetStartPosition().y, endPoint.y);
268     viewGroup->Remove(canvas_);
269 }
270 
271 /**
272  * @tc.name: UICanvasBeginPath_001
273  * @tc.desc: Verify BeginPath function, equal.
274  * @tc.type: FUNC
275  * @tc.require: AR000EVN8V
276  */
277 HWTEST_F(UICanvasTest, UICanvasBeginPath_001, TestSize.Level0)
278 {
279     if (canvas_ == nullptr) {
280         EXPECT_EQ(1, 0);
281         return;
282     }
283 
284     EXPECT_EQ(canvas_->GetPath(), nullptr);
285     canvas_->BeginPath();
286     EXPECT_NE(canvas_->GetPath(), nullptr);
287 }
288 
289 /**
290  * @tc.name: UICanvasMoveTo_001
291  * @tc.desc: Verify MoveTo function, equal.
292  * @tc.type: FUNC
293  * @tc.require: AR000EVN8V
294  */
295 HWTEST_F(UICanvasTest, UICanvasMoveTo_001, TestSize.Level0)
296 {
297     if (canvas_ == nullptr) {
298         EXPECT_EQ(1, 0);
299         return;
300     }
301     canvas_->BeginPath();
302     canvas_->MoveTo({ START1_X, START1_Y });
303     Point start = canvas_->GetStartPos();
304     EXPECT_EQ(start.x, START1_X);
305     EXPECT_EQ(start.y, START1_Y);
306 
307     Point end = canvas_->GetEndPos();
308     EXPECT_EQ(end.x, START1_X);
309     EXPECT_EQ(end.y, START1_Y);
310 }
311 
312 /**
313  * @tc.name: UICanvasLineTo_001
314  * @tc.desc: Verify LineTo function, equal.
315  * @tc.type: FUNC
316  * @tc.require: AR000EVN8V
317  */
318 HWTEST_F(UICanvasTest, UICanvasLineTo_001, TestSize.Level0)
319 {
320     if (canvas_ == nullptr) {
321         EXPECT_EQ(1, 0);
322         return;
323     }
324     canvas_->BeginPath();
325     canvas_->LineTo({ LINE1_X, LINE1_Y });
326     Point start = canvas_->GetStartPos();
327     EXPECT_EQ(start.x, LINE1_X);
328     EXPECT_EQ(start.y, LINE1_Y);
329 
330     Point end = canvas_->GetEndPos();
331     EXPECT_EQ(end.x, LINE1_X);
332     EXPECT_EQ(end.y, LINE1_Y);
333 }
334 
335 /**
336  * @tc.name: UICanvasLineTo_002
337  * @tc.desc: Verify LineTo function, equal.
338  * @tc.type: FUNC
339  * @tc.require: AR000EVN8V
340  */
341 HWTEST_F(UICanvasTest, UICanvasLineTo_002, TestSize.Level1)
342 {
343     if (canvas_ == nullptr) {
344         EXPECT_EQ(1, 0);
345         return;
346     }
347     canvas_->BeginPath();
348     canvas_->MoveTo({ START1_X, START1_Y });
349     canvas_->LineTo({ LINE1_X, LINE1_Y });
350     Point start = canvas_->GetStartPos();
351     EXPECT_EQ(start.x, START1_X);
352     EXPECT_EQ(start.y, START1_Y);
353 
354     Point end = canvas_->GetEndPos();
355     EXPECT_EQ(end.x, LINE1_X);
356     EXPECT_EQ(end.y, LINE1_Y);
357 
358     canvas_->LineTo({ LINE2_X, LINE2_Y });
359     end = canvas_->GetEndPos();
360     EXPECT_EQ(end.x, LINE2_X);
361     EXPECT_EQ(end.y, LINE2_Y);
362 }
363 
364 /**
365  * @tc.name: UICanvasArcTo_001
366  * @tc.desc: Verify ArcTo function, equal.
367  * @tc.type: FUNC
368  * @tc.require: AR000EVN8V
369  */
370 HWTEST_F(UICanvasTest, UICanvasArcTo_001, TestSize.Level0)
371 {
372     if (canvas_ == nullptr) {
373         EXPECT_EQ(1, 0);
374         return;
375     }
376     canvas_->BeginPath();
377     canvas_->ArcTo({ CENTER_X, CENTER_Y }, RADIUS, START_ANGLE, END_ANGLE);
378 
379     float sinma = RADIUS * Sin(START_ANGLE);
380     float cosma = RADIUS * Sin(QUARTER_IN_DEGREE - START_ANGLE);
381     Point start = canvas_->GetStartPos();
382     EXPECT_EQ(start.x, MATH_ROUND(CENTER_X + sinma));
383     EXPECT_EQ(start.y,  MATH_ROUND(CENTER_Y - cosma));
384 
385     sinma = RADIUS * Sin(END_ANGLE);
386     cosma = RADIUS * Sin(QUARTER_IN_DEGREE - END_ANGLE);
387     Point end = canvas_->GetEndPos();
388     EXPECT_EQ(end.x, MATH_ROUND(CENTER_X + sinma));
389     EXPECT_EQ(end.y, MATH_ROUND(CENTER_Y - cosma));
390 }
391 
392 /**
393  * @tc.name: UICanvasArcTo_002
394  * @tc.desc: Verify ArcTo function, equal.
395  * @tc.type: FUNC
396  * @tc.require: AR000EVN8V
397  */
398 HWTEST_F(UICanvasTest, UICanvasArcTo_002, TestSize.Level1)
399 {
400     if (canvas_ == nullptr) {
401         EXPECT_EQ(1, 0);
402         return;
403     }
404     canvas_->BeginPath();
405     canvas_->ArcTo({ CENTER_X, CENTER_Y }, RADIUS, START_ANGLE, START_ANGLE + CIRCLE_IN_DEGREE + QUARTER_IN_DEGREE);
406 
407     float sinma = RADIUS * Sin(START_ANGLE);
408     float cosma = RADIUS * Sin(QUARTER_IN_DEGREE - START_ANGLE);
409     Point start = canvas_->GetStartPos();
410     EXPECT_EQ(start.x, MATH_ROUND(CENTER_X + sinma));
411     EXPECT_EQ(start.y,  MATH_ROUND(CENTER_Y - cosma));
412 
413     sinma = RADIUS * Sin(END_ANGLE);
414     cosma = RADIUS * Sin(QUARTER_IN_DEGREE - END_ANGLE);
415     Point end = canvas_->GetEndPos();
416     EXPECT_EQ(end.x, start.x);
417     EXPECT_EQ(end.y, start.y);
418 }
419 
420 /**
421  * @tc.name: UICanvasArcTo_003
422  * @tc.desc: Verify ArcTo function, equal.
423  * @tc.type: FUNC
424  * @tc.require: AR000EVN8V
425  */
426 HWTEST_F(UICanvasTest, UICanvasArcTo_003, TestSize.Level1)
427 {
428     if (canvas_ == nullptr) {
429         EXPECT_EQ(1, 0);
430         return;
431     }
432     canvas_->BeginPath();
433     canvas_->MoveTo({ START1_X, START1_Y });
434     canvas_->ArcTo({ CENTER_X, CENTER_Y }, RADIUS, START_ANGLE, END_ANGLE);
435 
436     Point start = canvas_->GetStartPos();
437     EXPECT_EQ(start.x, START1_X);
438     EXPECT_EQ(start.y, START1_Y);
439 
440     float sinma = RADIUS * Sin(END_ANGLE);
441     float cosma = RADIUS * Sin(QUARTER_IN_DEGREE - END_ANGLE);
442     Point end = canvas_->GetEndPos();
443     EXPECT_EQ(end.x, MATH_ROUND(CENTER_X + sinma));
444     EXPECT_EQ(end.y, MATH_ROUND(CENTER_Y - cosma));
445 }
446 
447 /**
448  * @tc.name: UICanvasAddRect_001
449  * @tc.desc: Verify AddRect function, equal.
450  * @tc.type: FUNC
451  * @tc.require: AR000EVN8V
452  */
453 HWTEST_F(UICanvasTest, UICanvasAddRect_001, TestSize.Level0)
454 {
455     if (canvas_ == nullptr) {
456         EXPECT_EQ(1, 0);
457         return;
458     }
459     canvas_->BeginPath();
460     canvas_->AddRect({ RECT_X, RECT_Y }, RECT_HEIGHT, RECT_WIDTH);
461 
462     Point start = canvas_->GetStartPos();
463     EXPECT_EQ(start.x, RECT_X);
464     EXPECT_EQ(start.y, RECT_Y);
465 
466     Point end = canvas_->GetEndPos();
467     EXPECT_EQ(end.x, RECT_X);
468     EXPECT_EQ(end.y, RECT_Y);
469 }
470 
471 /**
472  * @tc.name: UICanvasAddRect_002
473  * @tc.desc: Verify AddRect function, equal.
474  * @tc.type: FUNC
475  * @tc.require: AR000EVN8V
476  */
477 HWTEST_F(UICanvasTest, UICanvasAddRect_002, TestSize.Level1)
478 {
479     if (canvas_ == nullptr) {
480         EXPECT_EQ(1, 0);
481         return;
482     }
483     canvas_->BeginPath();
484     canvas_->MoveTo({ START1_X, START1_Y });
485     canvas_->LineTo({ LINE1_X, LINE1_Y });
486     canvas_->AddRect({ RECT_X, RECT_Y }, RECT_HEIGHT, RECT_WIDTH);
487 
488     Point start = canvas_->GetStartPos();
489     EXPECT_EQ(start.x, RECT_X);
490     EXPECT_EQ(start.y, RECT_Y);
491 
492     Point end = canvas_->GetEndPos();
493     EXPECT_EQ(end.x, RECT_X);
494     EXPECT_EQ(end.y, RECT_Y);
495 }
496 
497 /**
498  * @tc.name: UICanvasClosePath_001
499  * @tc.desc: Verify ClosePath function, equal.
500  * @tc.type: FUNC
501  * @tc.require: AR000EVN8V
502  */
503 HWTEST_F(UICanvasTest, UICanvasClosePath_001, TestSize.Level0)
504 {
505     if (canvas_ == nullptr) {
506         EXPECT_EQ(1, 0);
507         return;
508     }
509     canvas_->BeginPath();
510     canvas_->MoveTo({ START1_X, START1_Y });
511     canvas_->LineTo({ LINE1_X, LINE1_Y });
512     canvas_->ClosePath();
513 
514     Point start = canvas_->GetStartPos();
515     EXPECT_EQ(start.x, START1_X);
516     EXPECT_EQ(start.y, START1_Y);
517 
518     Point end = canvas_->GetEndPos();
519     EXPECT_EQ(end.x, START1_X);
520     EXPECT_EQ(end.y, START1_Y);
521 }
522 
523 /**
524  * @tc.name: UICanvasClosePath_002
525  * @tc.desc: Verify ClosePath function, equal.
526  * @tc.type: FUNC
527  * @tc.require: SR000EVN8R
528  */
529 HWTEST_F(UICanvasTest, UICanvasClosePath_002, TestSize.Level1)
530 {
531     if (canvas_ == nullptr) {
532         EXPECT_EQ(1, 0);
533         return;
534     }
535 
536     if (paint_ == nullptr) {
537         EXPECT_EQ(1, 0);
538         return;
539     }
540 
541     canvas_->BeginPath();
542     canvas_->MoveTo({ START1_X, START1_Y });
543     canvas_->LineTo({ LINE1_X, LINE1_Y });
544     canvas_->ClosePath();
545     canvas_->DrawPath(*paint_);
546 
547     Point start = canvas_->GetStartPos();
548     EXPECT_EQ(start.x, START1_X);
549     EXPECT_EQ(start.y, START1_Y);
550 
551     Point end = canvas_->GetEndPos();
552     EXPECT_EQ(end.x, START1_X);
553     EXPECT_EQ(end.y, START1_Y);
554 
555     canvas_->BeginPath();
556     canvas_->LineTo({ LINE2_X, LINE2_Y });
557     canvas_->ClosePath();
558 
559     start = canvas_->GetStartPos();
560     EXPECT_EQ(start.x, LINE2_X);
561     EXPECT_EQ(start.y, LINE2_Y);
562 
563     end = canvas_->GetEndPos();
564     EXPECT_EQ(end.x, LINE2_X);
565     EXPECT_EQ(end.y, LINE2_Y);
566 }
567 } // namespace OHOS