common picture: fix invalid size returns from raw image.
[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(EXAMPLE_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)) == Result::Success);
59
60     float w, h;
61     REQUIRE(picture->size(&w, &h) == Result::Success);
62     REQUIRE(w == Approx(1000).epsilon(0.0000001));
63     REQUIRE(h == Approx(1000).epsilon(0.0000001));
64 }
65
66 TEST_CASE("Load RAW Data", "[tvgPicture]")
67 {
68     auto picture = Picture::gen();
69     REQUIRE(picture);
70
71     string path(EXAMPLE_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 == Approx(200).epsilon(0.0000001));
93     REQUIRE(h == Approx(300).epsilon(0.0000001));
94
95     free(data);
96 }
97
98
99 TEST_CASE("Load PNG file", "[tvgPicture]")
100 {
101     auto picture = Picture::gen();
102     REQUIRE(picture);
103
104     //Invalid file
105     REQUIRE(picture->load("invalid.png") == Result::InvalidArguments);
106
107     REQUIRE(picture->load(EXAMPLE_DIR"/logo.png") == Result::Success);
108
109     float w, h;
110     REQUIRE(picture->size(&w, &h) == Result::Success);
111 }
112
113
114 TEST_CASE("Picture Size", "[tvgPicture]")
115 {
116     auto picture = Picture::gen();
117     REQUIRE(picture);
118
119     float w, h;
120     REQUIRE(picture->size(&w, &h) == Result::InsufficientCondition);
121
122     REQUIRE(picture->load(EXAMPLE_DIR"/logo.svg") == Result::Success);
123
124     REQUIRE(picture->size(nullptr, nullptr) == Result::Success);
125     REQUIRE(picture->size(100, 100) == Result::Success);
126     REQUIRE(picture->size(&w, &h) == Result::Success);
127     REQUIRE(w == Approx(100).epsilon(0.0000001));
128     REQUIRE(h == Approx(100).epsilon(0.0000001));
129
130     REQUIRE(picture->load(EXAMPLE_DIR"/tiger.svg") == Result::Success);
131     REQUIRE(picture->size(&w, &h) == Result::Success);
132     REQUIRE(picture->size(w, h) == Result::Success);
133 }