Format code files with dos2unix, ensure newline at EOF
[platform/core/graphics/tizenvg.git] / test / testShape.cpp
1 /*
2  * Copyright (c) 2021 - 2022 Samsung Electronics Co., Ltd. All rights reserved.
3
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10
11  * The above copyright notice and this permission notice shall be included in all
12  * copies or substantial portions of the Software.
13
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20  * SOFTWARE.
21  */
22
23 #include <thorvg.h>
24 #include "catch.hpp"
25
26 using namespace tvg;
27
28 TEST_CASE("Shape Creation", "[tvgShape]")
29 {
30     auto shape = Shape::gen();
31     REQUIRE(shape);
32
33     REQUIRE(shape->identifier() == Shape::identifier());
34     REQUIRE(shape->identifier() != Picture::identifier());
35     REQUIRE(shape->identifier() != Scene::identifier());
36 }
37
38 TEST_CASE("Appending Commands", "[tvgShape]")
39 {
40     auto shape = Shape::gen();
41     REQUIRE(shape);
42
43     REQUIRE(shape->close() == Result::Success);
44
45     REQUIRE(shape->moveTo(100, 100) == Result::Success);
46     REQUIRE(shape->moveTo(99999999.0f, -99999999.0f) == Result::Success);
47     REQUIRE(shape->moveTo(0, 0) == Result::Success);
48
49     REQUIRE(shape->lineTo(120, 140) == Result::Success);
50     REQUIRE(shape->lineTo(99999999.0f, -99999999.0f) == Result::Success);
51     REQUIRE(shape->lineTo(0, 0) == Result::Success);
52
53     REQUIRE(shape->cubicTo(0, 0, 0, 0, 0, 0) == Result::Success);
54     REQUIRE(shape->cubicTo(0, 0, 99999999.0f, -99999999.0f, 0, 0) == Result::Success);
55     REQUIRE(shape->cubicTo(0, 0, 99999999.0f, -99999999.0f, 99999999.0f, -99999999.0f) == Result::Success);
56     REQUIRE(shape->cubicTo(99999999.0f, -99999999.0f, 99999999.0f, -99999999.0f, 99999999.0f, -99999999.0f) == Result::Success);
57
58     REQUIRE(shape->close() == Result::Success);
59
60     REQUIRE(shape->reset() == Result::Success);
61     REQUIRE(shape->reset() == Result::Success);
62 }
63
64 TEST_CASE("Appending Shapes", "[tvgShape]")
65 {
66     auto shape = Shape::gen();
67     REQUIRE(shape);
68
69     REQUIRE(shape->moveTo(100, 100) == Result::Success);
70     REQUIRE(shape->lineTo(120, 140) == Result::Success);
71
72     REQUIRE(shape->appendRect(0, 0, 0, 0, 0, 0) == Result::Success);
73     REQUIRE(shape->appendRect(0, 0,99999999.0f, -99999999.0f, 0, 0) == Result::Success);
74     REQUIRE(shape->appendRect(0, 0, 0, 0, -99999999.0f, 99999999.0f) == Result::Success);
75     REQUIRE(shape->appendRect(99999999.0f, -99999999.0f, 99999999.0f, -99999999.0f, 99999999.0f, -99999999.0f) == Result::Success);
76
77     REQUIRE(shape->appendCircle(0, 0, 0, 0) == Result::Success);
78     REQUIRE(shape->appendCircle(-99999999.0f, 99999999.0f, 0, 0) == Result::Success);
79     REQUIRE(shape->appendCircle(-99999999.0f, 99999999.0f, -99999999.0f, 99999999.0f) == Result::Success);
80
81     REQUIRE(shape->appendArc(0, 0, 0, 0, 0, false) == Result::Success);
82     REQUIRE(shape->appendArc(0, 0, 0, 0, 0, true) == Result::Success);
83     REQUIRE(shape->appendArc(-99999999.0f, 99999999.0f, 0, 0, 0, false) == Result::Success);
84     REQUIRE(shape->appendArc(-99999999.0f, 99999999.0f, 0, 0, 0, true) == Result::Success);
85     REQUIRE(shape->appendArc(-99999999.0f, 99999999.0f, -99999999.0f, 99999999.0f, 0, false) == Result::Success);
86     REQUIRE(shape->appendArc(-99999999.0f, 99999999.0f, -99999999.0f, 99999999.0f, 0, true) == Result::Success);
87     REQUIRE(shape->appendArc(-99999999.0f, 99999999.0f, -99999999.0f, 99999999.0f, -400, false) == Result::Success);
88     REQUIRE(shape->appendArc(-99999999.0f, 99999999.0f, -99999999.0f, 99999999.0f, 400, true) == Result::Success);
89 }
90
91 TEST_CASE("Appending Pathes", "[tvgShape]")
92 {
93     auto shape = Shape::gen();
94     REQUIRE(shape);
95
96     //Negative cases
97     REQUIRE(shape->appendPath(nullptr, 0, nullptr, 0) == Result::InvalidArguments);
98     REQUIRE(shape->appendPath(nullptr, 100, nullptr, 0) == Result::InvalidArguments);
99     REQUIRE(shape->appendPath(nullptr, 0, nullptr, 100) == Result::InvalidArguments);
100
101     PathCommand cmds[5] = {
102         PathCommand::Close,
103         PathCommand::MoveTo,
104         PathCommand::LineTo,
105         PathCommand::CubicTo,
106         PathCommand::Close
107     };
108
109     Point pts[5] = {
110         {100, 100},
111         {200, 200},
112         {10, 10},
113         {20, 20},
114         {30, 30}
115     };
116
117     REQUIRE(shape->appendPath(cmds, 0, pts, 5) == Result::InvalidArguments);
118     REQUIRE(shape->appendPath(cmds, 5, pts, 0) == Result::InvalidArguments);
119     REQUIRE(shape->appendPath(cmds, 5, pts, 5) == Result::Success);
120
121     const PathCommand* cmds2;
122     REQUIRE(shape->pathCommands(&cmds2) == 5);
123
124     const Point* pts2;
125     REQUIRE(shape->pathCoords(&pts2) == 5);
126
127     for (int i = 0; i < 5; ++i) {
128         REQUIRE(cmds2[i] == cmds[i]);
129         REQUIRE(pts[i].x == pts2[i].x);
130         REQUIRE(pts[i].y == pts2[i].y);
131     }
132
133     shape->reset();
134     REQUIRE(shape->pathCommands(nullptr) == 0);
135     REQUIRE(shape->pathCoords(nullptr) == 0);
136 }
137
138 TEST_CASE("Stroking", "[tvgShape]")
139 {
140     auto shape = Shape::gen();
141     REQUIRE(shape);
142
143     //Stroke Width
144     REQUIRE(shape->stroke(0) == Result::Success);
145     REQUIRE(shape->strokeWidth() == 0);
146     REQUIRE(shape->stroke(300) == Result::Success);
147     REQUIRE(shape->strokeWidth() == 300);
148
149     //Stroke Color
150     uint8_t r, g, b, a;
151     REQUIRE(shape->stroke(0, 50, 100, 200) == Result::Success);
152     REQUIRE(shape->strokeColor(nullptr, nullptr, &b, nullptr) == Result::Success);
153     REQUIRE(b == 100);
154     REQUIRE(shape->strokeColor(&r, &g, &b, &a) == Result::Success);
155     REQUIRE(r == 0);
156     REQUIRE(g == 50);
157     REQUIRE(b == 100);
158     REQUIRE(a == 200);
159     REQUIRE(shape->strokeColor(nullptr, nullptr, nullptr, nullptr) == Result::Success);
160
161     //Stroke Dash
162     float dashPattern[3] = {0, 1.5f, 2.22f};
163     REQUIRE(shape->stroke(dashPattern, 3) == Result::InvalidArguments);
164
165     float dashPattern2[3] = {1.0f, 1.5f, 2.22f};
166     REQUIRE(shape->stroke(dashPattern2, 3) == Result::Success);
167
168     const float* dashPattern3;
169     REQUIRE(shape->strokeDash(nullptr) == 3);
170     REQUIRE(shape->strokeDash(&dashPattern3) == 3);
171     REQUIRE(dashPattern3[0] == 1.0f);
172     REQUIRE(dashPattern3[1] == 1.5f);
173     REQUIRE(dashPattern3[2] == 2.22f);
174
175     REQUIRE(shape->stroke(nullptr, 0) == Result::Success);
176
177     //Stroke Cap
178     REQUIRE(shape->strokeCap() == StrokeCap::Square);
179     REQUIRE(shape->stroke(StrokeCap::Round) == Result::Success);
180     REQUIRE(shape->stroke(StrokeCap::Butt) == Result::Success);
181     REQUIRE(shape->strokeCap() == StrokeCap::Butt);
182
183     //Stroke Join
184     REQUIRE(shape->strokeJoin() == StrokeJoin::Bevel);
185     REQUIRE(shape->stroke(StrokeJoin::Miter) == Result::Success);
186     REQUIRE(shape->stroke(StrokeJoin::Round) == Result::Success);
187     REQUIRE(shape->strokeJoin() == StrokeJoin::Round);
188 }
189
190 TEST_CASE("Shape Filling", "[tvgShape]")
191 {
192     auto shape = Shape::gen();
193     REQUIRE(shape);
194
195     //Fill Color
196     uint8_t r, g, b, a;
197     REQUIRE(shape->fill(255, 100, 50, 5) == Result::Success);
198     REQUIRE(shape->fillColor(&r, nullptr, &b, nullptr) == Result::Success);
199     REQUIRE(r == 255);
200     REQUIRE(b == 50);
201     REQUIRE(shape->fillColor(&r, &g, &b, &a) == Result::Success);
202     REQUIRE(g == 100);
203     REQUIRE(a == 5);
204
205     //Fill Rule
206     REQUIRE(shape->fillRule() == FillRule::Winding);
207     REQUIRE(shape->fill(FillRule::EvenOdd) == Result::Success);
208     REQUIRE(shape->fillRule() == FillRule::EvenOdd);
209 }