• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 <gtest/gtest.h>
17 #include "image_source_util.h"
18 #include "log_tags.h"
19 #include "media_errors.h"
20 
21 using namespace testing::ext;
22 using namespace OHOS::Media;
23 using namespace OHOS::ImageSourceUtil;
24 
25 namespace OHOS {
26 namespace Multimedia {
27 namespace {
28 static const std::string SVG_FORMAT_TYPE = "image/svg+xml";
29 static const std::string INPUT_PATH = "/data/local/tmp/image/";
30 static const std::string OUTPUT_PATH = "/data/local/tmp/image/output_";
31 static const std::string OUTPUT_EXT = ".jpg";
32 static const std::string TEST_FILE_SVG = "test.svg";
33 static const std::string TEST_FILE_LARGE_SVG = "test_large.svg";
34 }
35 
36 class ImageSourceSvgTest : public testing::Test {};
37 
38 /**
39  * @tc.name: SvgImageDecode
40  * @tc.desc: Decode svg image from file source stream
41  * @tc.type: FUNC
42  */
43 HWTEST_F(ImageSourceSvgTest, SvgImageDecode, TestSize.Level3)
44 {
45     GTEST_LOG_(INFO) << "ImageSourceSvgTest: SvgImageDecode start";
46 
47     const std::string testName = TEST_FILE_SVG;
48 
49     /**
50      * @tc.steps: step1. create image source by correct svg file path and svg format hit.
51      * @tc.expected: step1. create image source success.
52      */
53     uint32_t errorCode = 0;
54     SourceOptions opts;
55     opts.formatHint = SVG_FORMAT_TYPE;
56     const std::string inName = INPUT_PATH + testName;
57     auto imageSource = ImageSource::CreateImageSource(inName, opts, errorCode);
58     ASSERT_EQ(errorCode, SUCCESS);
59     ASSERT_NE(imageSource.get(), nullptr);
60 
61     /**
62      * @tc.steps: step2. decode image source to pixel map by default decode options
63      * @tc.expected: step2. decode image source to pixel map success.
64      */
65     DecodeOptions decodeOpts;
66     auto pixelMap = imageSource->CreatePixelMap(decodeOpts, errorCode);
67     ASSERT_EQ(errorCode, SUCCESS);
68     ASSERT_NE(pixelMap.get(), nullptr);
69 
70     /**
71      * @tc.steps: step3. compress the pixel map to jpg file.
72      * @tc.expected: step3. pack pixel map success.
73      */
74     const std::string outName = OUTPUT_PATH + testName + OUTPUT_EXT;
75     auto packSize = PackImage(outName, std::move(pixelMap));
76     ASSERT_NE(packSize, 0);
77 
78     GTEST_LOG_(INFO) << "ImageSourceSvgTest: SvgImageDecode end";
79 }
80 
81 /**
82  * @tc.name: SvgCreateImageSource
83  * @tc.desc: Decode svg image from file source stream
84  * @tc.type: FUNC
85  */
86 HWTEST_F(ImageSourceSvgTest, SvgCreateImageSource, TestSize.Level3)
87 {
88     GTEST_LOG_(INFO) << "ImageSourceSvgTest: SvgCreateImageSource start";
89 
90     const std::string testName = TEST_FILE_LARGE_SVG;
91 
92     /**
93      * @tc.steps: step1. create image source by correct svg file path and svg format hit.
94      * @tc.expected: step1. create image source success.
95      */
96     uint32_t errorCode = 0;
97     SourceOptions opts;
98     opts.formatHint = SVG_FORMAT_TYPE;
99     const std::string inName = INPUT_PATH + testName;
100     auto imageSource = ImageSource::CreateImageSource(inName, opts, errorCode);
101     ASSERT_EQ(errorCode, SUCCESS);
102     ASSERT_NE(imageSource.get(), nullptr);
103 
104     /**
105      * @tc.steps: step2. decode image source to pixel map by default decode options
106      * @tc.expected: step2. decode image source to pixel map success.
107      */
108     DecodeOptions decodeOpts;
109     auto pixelMap = imageSource->CreatePixelMap(decodeOpts, errorCode);
110     ASSERT_EQ(errorCode, SUCCESS);
111     ASSERT_NE(pixelMap.get(), nullptr);
112 
113     /**
114      * @tc.steps: step3. compress the pixel map to jpg file.
115      * @tc.expected: step3. pack pixel map success.
116      */
117     const std::string outName = OUTPUT_PATH + testName + OUTPUT_EXT;
118     auto packSize = PackImage(outName, std::move(pixelMap));
119     ASSERT_NE(packSize, 0);
120 
121     GTEST_LOG_(INFO) << "ImageSourceSvgTest: SvgCreateImageSource end";
122 }
123 
124 /**
125  * @tc.name: SvgImageDecodeWithFillColorChange
126  * @tc.desc: Decode svg image from file source stream
127  * @tc.type: FUNC
128  */
129 HWTEST_F(ImageSourceSvgTest, SvgImageDecodeWithFillColorChange, TestSize.Level3)
130 {
131     GTEST_LOG_(INFO) << "ImageSourceSvgTest: SvgImageDecode start";
132 
133     const std::string testName = TEST_FILE_SVG;
134 
135     /**
136      * @tc.steps: step1. create image source by correct svg file path and svg format hit.
137      * @tc.expected: step1. create image source success.
138      */
139     uint32_t errorCode = 0;
140     SourceOptions opts;
141     opts.formatHint = SVG_FORMAT_TYPE;
142     const std::string inName = INPUT_PATH + testName;
143     auto imageSource = ImageSource::CreateImageSource(inName, opts, errorCode);
144     ASSERT_EQ(errorCode, SUCCESS);
145     ASSERT_NE(imageSource.get(), nullptr);
146 
147     /**
148      * @tc.steps: step2. decode image source to pixel map by default decode options which sets fillColor 0xff00ff
149      * @tc.expected: step2. decode image source to pixel map success.
150      */
151     DecodeOptions decodeOpts;
152     decodeOpts.SVGOpts.fillColor.isValidColor = true;
153     decodeOpts.SVGOpts.fillColor.color = 0xff00ff;
154     auto pixelMap = imageSource->CreatePixelMap(decodeOpts, errorCode);
155     ASSERT_EQ(errorCode, SUCCESS);
156     ASSERT_NE(pixelMap.get(), nullptr);
157 
158     /**
159      * @tc.steps: step3. compress the pixel map to jpg file.
160      * @tc.expected: step3. pack pixel map success and the color is purple.
161      */
162     const std::string outName = OUTPUT_PATH + testName + OUTPUT_EXT;
163     auto packSize = PackImage(outName, std::move(pixelMap));
164     ASSERT_NE(packSize, 0);
165 
166     GTEST_LOG_(INFO) << "ImageSourceSvgTest: SvgImageDecodeWithFillColorChange end";
167 }
168 
169 /**
170  * @tc.name: SvgImageDecodeWithResizePercentage
171  * @tc.desc: Decode svg image from file source stream
172  * @tc.type: FUNC
173  */
174 HWTEST_F(ImageSourceSvgTest, SvgImageDecodeWithResizePercentage, TestSize.Level3)
175 {
176     GTEST_LOG_(INFO) << "ImageSourceSvgTest: SvgImageDecode start";
177 
178     const std::string testName = TEST_FILE_SVG;
179 
180     /**
181      * @tc.steps: step1. create image source by correct svg file path and svg format hit.
182      * @tc.expected: step1. create image source success.
183      */
184     uint32_t errorCode = 0;
185     SourceOptions opts;
186     opts.formatHint = SVG_FORMAT_TYPE;
187     const std::string inName = INPUT_PATH + testName;
188     auto imageSource = ImageSource::CreateImageSource(inName, opts, errorCode);
189     ASSERT_EQ(errorCode, SUCCESS);
190     ASSERT_NE(imageSource.get(), nullptr);
191 
192     /**
193      * @tc.steps: step2. decode image source to pixel map by default decode options  which sets resizePercentage 200
194      * @tc.expected: step2. decode image source to pixel map success.
195      */
196     DecodeOptions decodeOpts;
197     decodeOpts.SVGOpts.SVGResize.isValidPercentage = true;
198     decodeOpts.SVGOpts.SVGResize.resizePercentage = 200;
199     auto pixelMap = imageSource->CreatePixelMap(decodeOpts, errorCode);
200     ASSERT_EQ(errorCode, SUCCESS);
201     ASSERT_NE(pixelMap.get(), nullptr);
202 
203     /**
204      * @tc.steps: step3. compress the pixel map to jpg file.
205      * @tc.expected: step3. pack pixel map success and the size is 200% of the former one.
206      */
207     const std::string outName = OUTPUT_PATH + testName + OUTPUT_EXT;
208     auto packSize = PackImage(outName, std::move(pixelMap));
209     ASSERT_NE(packSize, 0);
210 
211     GTEST_LOG_(INFO) << "ImageSourceSvgTest: SvgImageDecodeWithResizePercentage end";
212 }
213 } // namespace Multimedia
214 } // namespace OHOS