test: ++safety.
[platform/core/graphics/tizenvg.git] / test / testPicture.cpp
1 /*
2  * Copyright (c) 2021 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 <string.h>
25 #include <fstream>
26 #include "catch.hpp"
27
28 using namespace tvg;
29 using namespace std;
30
31 TEST_CASE("Load SVG file", "[tvgPicture]")
32 {
33     auto picture = Picture::gen();
34     REQUIRE(picture);
35
36     //Invalid file
37     REQUIRE(picture->load("invalid.svg") == Result::InvalidArguments);
38
39     //Load Svg file
40     REQUIRE(picture->load(TEST_DIR"/logo.svg") == Result::Success);
41
42     float w, h;
43     REQUIRE(picture->size(&w, &h) == Result::Success);
44 }
45
46 TEST_CASE("Load SVG Data", "[tvgPicture]")
47 {
48     static const char* svg = "<svg height=\"1000\" viewBox=\"0 0 1000 1000\" width=\"1000\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M.10681413.09784845 1000.0527.01592069V1000.0851L.06005738 999.9983Z\" fill=\"#ffffff\" stroke-width=\"3.910218\"/><g fill=\"#252f35\"><g stroke-width=\"3.864492\"><path d=\"M256.61221 100.51736H752.8963V386.99554H256.61221Z\"/><path d=\"M201.875 100.51736H238.366478V386.99554H201.875Z\"/><path d=\"M771.14203 100.51736H807.633508V386.99554H771.14203Z\"/></g><path d=\"M420.82388 380H588.68467V422.805317H420.82388Z\" stroke-width=\"3.227\"/><path d=\"m420.82403 440.7101v63.94623l167.86079 25.5782V440.7101Z\"/><path d=\"M420.82403 523.07258V673.47362L588.68482 612.59701V548.13942Z\"/></g><g fill=\"#222f35\"><path d=\"M420.82403 691.37851 588.68482 630.5019 589 834H421Z\"/><path d=\"m420.82403 852.52249h167.86079v28.64782H420.82403v-28.64782 0 0\"/><path d=\"m439.06977 879.17031c0 0-14.90282 8.49429-18.24574 15.8161-4.3792 9.59153 0 31.63185 0 31.63185h167.86079c0 0 4.3792-22.04032 0-31.63185-3.34292-7.32181-18.24574-15.8161-18.24574-15.8161z\"/></g><g fill=\"#ffffff\"><path d=\"m280 140h15v55l8 10 8-10v-55h15v60l-23 25-23-25z\"/><path d=\"m335 140v80h45v-50h-25v10h10v30h-15v-57h18v-13z\"/></g></svg>";
49
50     auto picture = Picture::gen();
51     REQUIRE(picture);
52
53     //Negative cases
54     REQUIRE(picture->load(nullptr, 100, "") == Result::InvalidArguments);
55     REQUIRE(picture->load(svg, 0, "") == Result::InvalidArguments);
56
57     //Positive cases
58     REQUIRE(picture->load(svg, strlen(svg), "svg") == Result::Success);
59
60     float w, h;
61     REQUIRE(picture->size(&w, &h) == Result::Success);
62     REQUIRE(w == 1000);
63     REQUIRE(h == 1000);
64 }
65
66 TEST_CASE("Load RAW Data", "[tvgPicture]")
67 {
68     auto picture = Picture::gen();
69     REQUIRE(picture);
70
71     string path(TEST_DIR"/rawimage_200x300.raw");
72
73     ifstream file(path);
74     if (!file.is_open()) return;
75     auto data = (uint32_t*)malloc(sizeof(uint32_t) * (200*300));
76     file.read(reinterpret_cast<char *>(data), sizeof (uint32_t) * 200 * 300);
77     file.close();
78
79     //Negative cases
80     REQUIRE(picture->load(nullptr, 200, 300, false) == Result::InvalidArguments);
81     REQUIRE(picture->load(data, 0, 0, false) == Result::InvalidArguments);
82     REQUIRE(picture->load(data, 200, 0, false) == Result::InvalidArguments);
83     REQUIRE(picture->load(data, 0, 300, false) == Result::InvalidArguments);
84
85     //Positive cases
86     REQUIRE(picture->load(data, 200, 300, false) == Result::Success);
87     REQUIRE(picture->load(data, 200, 300, true) == Result::Success);
88
89     float w, h;
90     REQUIRE(picture->size(&w, &h) == Result::Success);
91
92     REQUIRE(w == 200);
93     REQUIRE(h == 300);
94
95     free(data);
96 }
97
98 TEST_CASE("Load PNG file from path", "[tvgPicture]")
99 {
100     auto picture = Picture::gen();
101     REQUIRE(picture);
102
103     //Invalid file
104     REQUIRE(picture->load("invalid.png") == Result::InvalidArguments);
105
106     REQUIRE(picture->load(TEST_DIR"/test.png") == Result::Success);
107
108     float w, h;
109     REQUIRE(picture->size(&w, &h) == Result::Success);
110
111     REQUIRE(w == 512);
112     REQUIRE(h == 512);
113 }
114
115 TEST_CASE("Load PNG file from data", "[tvgPicture]")
116 {
117     auto picture = Picture::gen();
118     REQUIRE(picture);
119
120     //Open file
121     ifstream file(TEST_DIR"/test.png");
122     REQUIRE(file.is_open());
123     auto size = sizeof(uint32_t) * (1000*1000);
124     auto data = (char*)malloc(size);
125     file.read(data, size);
126     file.close();
127
128     REQUIRE(picture->load(data, size, "", false) == Result::Success);
129     REQUIRE(picture->load(data, size, "png", true) == Result::Success);
130
131     float w, h;
132     REQUIRE(picture->size(&w, &h) == Result::Success);
133     REQUIRE(w == 512);
134     REQUIRE(h == 512);
135
136     free(data);
137 }
138
139 TEST_CASE("Load JPG file from path", "[tvgPicture]")
140 {
141     auto picture = Picture::gen();
142     REQUIRE(picture);
143
144     //Invalid file
145     REQUIRE(picture->load("invalid.jpg") == Result::InvalidArguments);
146
147     REQUIRE(picture->load(TEST_DIR"/test.jpg") == Result::Success);
148
149     float w, h;
150     REQUIRE(picture->size(&w, &h) == Result::Success);
151
152     REQUIRE(w == 512);
153     REQUIRE(h == 512);
154 }
155
156 TEST_CASE("Load JPG file from data", "[tvgPicture]")
157 {
158     auto picture = Picture::gen();
159     REQUIRE(picture);
160
161     //Open file
162     ifstream file(TEST_DIR"/test.jpg");
163     REQUIRE(file.is_open());
164     auto begin = file.tellg();
165     file.seekg(0, std::ios::end);
166     auto size = file.tellg() - begin;
167     auto data = (char*)malloc(size);
168     file.seekg(0, std::ios::beg);
169     file.read(data, size);
170     file.close();
171
172     REQUIRE(picture->load(data, size, "", false) == Result::Success);
173     REQUIRE(picture->load(data, size, "jpg", true) == Result::Success);
174
175     float w, h;
176     REQUIRE(picture->size(&w, &h) == Result::Success);
177     REQUIRE(w == 512);
178     REQUIRE(h == 512);
179
180     free(data);
181 }
182
183 TEST_CASE("Load TVG file from path", "[tvgPicture]")
184 {
185     auto picture = Picture::gen();
186     REQUIRE(picture);
187
188     //Invalid file
189     REQUIRE(picture->load("invalid.tvg") == Result::InvalidArguments);
190
191     REQUIRE(picture->load(TEST_DIR"/tag.tvg") == Result::Success);
192
193     float w, h;
194     REQUIRE(picture->size(&w, &h) == Result::Success);
195
196     REQUIRE(w == 1000);
197     REQUIRE(h == 1000);
198 }
199
200 TEST_CASE("Load TVG file from data", "[tvgPicture]")
201 {
202     auto picture = Picture::gen();
203     REQUIRE(picture);
204
205     //Open file
206     ifstream file(TEST_DIR"/tag.tvg");
207     REQUIRE(file.is_open());
208     auto begin = file.tellg();
209     file.seekg(0, std::ios::end);
210     auto size = file.tellg() - begin;
211     auto data = (char*)malloc(size);
212     file.seekg(0, std::ios::beg);
213     file.read(data, size);
214     file.close();
215
216     REQUIRE(picture->load(data, size, "", false) == Result::Success);
217     REQUIRE(picture->load(data, size, "tvg", true) == Result::Success);
218
219     float w, h;
220     REQUIRE(picture->size(&w, &h) == Result::Success);
221     REQUIRE(w == 1000);
222     REQUIRE(h == 1000);
223
224     free(data);
225 }
226
227 TEST_CASE("Picture Size", "[tvgPicture]")
228 {
229     auto picture = Picture::gen();
230     REQUIRE(picture);
231
232     float w, h;
233     REQUIRE(picture->size(&w, &h) == Result::InsufficientCondition);
234
235     REQUIRE(picture->load(TEST_DIR"/logo.svg") == Result::Success);
236
237     REQUIRE(picture->size(nullptr, nullptr) == Result::Success);
238     REQUIRE(picture->size(100, 100) == Result::Success);
239     REQUIRE(picture->size(&w, &h) == Result::Success);
240     REQUIRE(w == 100);
241     REQUIRE(h == 100);
242
243     REQUIRE(picture->load(TEST_DIR"/tiger.svg") == Result::Success);
244     REQUIRE(picture->size(&w, &h) == Result::Success);
245     REQUIRE(picture->size(w, h) == Result::Success);
246 }
247
248 TEST_CASE("Picture Duplication", "[tvgPicture]")
249 {
250     auto picture = Picture::gen();
251     REQUIRE(picture);
252
253     REQUIRE(picture->load(TEST_DIR"/logo.svg") == Result::Success);
254     REQUIRE(picture->size(100, 100) == Result::Success);
255
256     auto dup = picture->duplicate();
257     REQUIRE(dup);
258
259     float w, h;
260     REQUIRE(picture->size(&w, &h) == Result::Success);
261     REQUIRE(w == 100);
262     REQUIRE(h == 100);
263 }
264
265 TEST_CASE("Load SVG file and render", "[tvgPicture]")
266 {
267     REQUIRE(Initializer::init(CanvasEngine::Sw, 0) == Result::Success);
268
269     auto canvas = SwCanvas::gen();
270     REQUIRE(canvas);
271
272     auto buffer = new uint32_t[1000*1000];
273     if (!buffer) return;
274
275     REQUIRE(canvas->target(buffer, 1000, 1000, 1000, SwCanvas::Colorspace::ABGR8888) == Result::Success);
276
277     auto picture = Picture::gen();
278     REQUIRE(picture);
279
280     REQUIRE(picture->load(TEST_DIR"/tag.svg") == Result::Success);
281     REQUIRE(picture->size(100, 100) == Result::Success);
282
283     REQUIRE(canvas->push(move(picture)) == Result::Success);
284     REQUIRE(canvas->draw() == Result::Success);
285     REQUIRE(canvas->sync() == Result::Success);
286
287     REQUIRE(Initializer::term(CanvasEngine::Sw) == Result::Success);
288
289     delete buffer;
290 }
291
292 TEST_CASE("Load PNG file and render", "[tvgPicture]")
293 {
294     REQUIRE(Initializer::init(CanvasEngine::Sw, 0) == Result::Success);
295
296     auto canvas = SwCanvas::gen();
297     REQUIRE(canvas);
298
299     uint32_t buffer[100*100];
300     REQUIRE(canvas->target(buffer, 100, 100, 100, SwCanvas::Colorspace::ABGR8888) == Result::Success);
301
302     auto picture = Picture::gen();
303     REQUIRE(picture);
304
305     REQUIRE(picture->load(TEST_DIR"/test.png") == Result::Success);
306     REQUIRE(picture->opacity(192) == Result::Success);
307     REQUIRE(picture->scale(5.0) == Result::Success);
308
309     REQUIRE(canvas->push(move(picture)) == Result::Success);
310
311     REQUIRE(Initializer::term(CanvasEngine::Sw) == Result::Success);
312 }
313
314 TEST_CASE("Load JPG file and render", "[tvgPicture]")
315 {
316     REQUIRE(Initializer::init(CanvasEngine::Sw, 0) == Result::Success);
317
318     auto canvas = SwCanvas::gen();
319     REQUIRE(canvas);
320
321     uint32_t buffer[100*100];
322     REQUIRE(canvas->target(buffer, 100, 100, 100, SwCanvas::Colorspace::ABGR8888) == Result::Success);
323
324     auto picture = Picture::gen();
325     REQUIRE(picture);
326
327     REQUIRE(picture->load(TEST_DIR"/test.jpg") == Result::Success);
328
329     REQUIRE(canvas->push(move(picture)) == Result::Success);
330
331     REQUIRE(Initializer::term(CanvasEngine::Sw) == Result::Success);
332 }
333
334 TEST_CASE("Load TVG file and render", "[tvgPicture]")
335 {
336     REQUIRE(Initializer::init(CanvasEngine::Sw, 0) == Result::Success);
337
338     auto canvas = SwCanvas::gen();
339     REQUIRE(canvas);
340
341     auto buffer = new uint32_t[1000*1000];
342     if (!buffer) return;
343
344     REQUIRE(canvas->target(buffer, 1000, 1000, 1000, SwCanvas::Colorspace::ABGR8888) == Result::Success);
345
346     auto pictureTag = Picture::gen();
347     REQUIRE(pictureTag);
348     REQUIRE(pictureTag->load(TEST_DIR"/tag.tvg") == Result::Success);
349     REQUIRE(canvas->push(move(pictureTag)) == Result::Success);
350
351     auto pictureTest = Picture::gen();
352     REQUIRE(pictureTest);
353     REQUIRE(pictureTest->load(TEST_DIR"/test.tvg") == Result::Success);
354     REQUIRE(canvas->push(move(pictureTest)) == Result::Success);
355
356     REQUIRE(Initializer::term(CanvasEngine::Sw) == Result::Success);
357
358     delete buffer;
359 }
360
361 TEST_CASE("Load RAW file and render", "[tvgPicture]")
362 {
363     REQUIRE(Initializer::init(CanvasEngine::Sw, 0) == Result::Success);
364
365     auto canvas = SwCanvas::gen();
366     REQUIRE(canvas);
367
368     uint32_t buffer[100*100];
369     REQUIRE(canvas->target(buffer, 100, 100, 100, SwCanvas::Colorspace::ABGR8888) == Result::Success);
370
371     auto picture = Picture::gen();
372     REQUIRE(picture);
373
374     string path(TEST_DIR"/rawimage_200x300.raw");
375
376     ifstream file(path);
377     if (!file.is_open()) return;
378     auto data = (uint32_t*)malloc(sizeof(uint32_t) * (200*300));
379     if (!data) return;
380     file.read(reinterpret_cast<char *>(data), sizeof (uint32_t) * 200 * 300);
381     file.close();
382
383     REQUIRE(picture->load(data, 200, 300, false) == Result::Success);
384     REQUIRE(picture->size(100, 150) == Result::Success);
385
386     REQUIRE(canvas->push(move(picture)) == Result::Success);
387
388     REQUIRE(Initializer::term(CanvasEngine::Sw) == Result::Success);
389
390     free(data);
391 }