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.h"
18 #include "media_errors.h"
19 #include "svg_format_agent.h"
20 
21 using namespace testing::ext;
22 using namespace OHOS::Media;
23 
24 namespace OHOS {
25 namespace Multimedia {
26 static const std::string IMAGE_INPUT_SVG_PATH = "/data/local/tmp/image/test.svg";
27 static const std::string SVG_FORMAT_TYPE = "image/svg+xml";
28 static constexpr uint8_t SVG_HEADER[] = { '<', '?', 'x', 'm', 'l' };
29 
30 class FormatAgentPluginSvgTest : public testing::Test {};
31 
32 /**
33  * @tc.name: SvgGetFormatTypeTest
34  * @tc.desc: svg GetFormatType
35  * @tc.type: FUNC
36  */
37 HWTEST_F(FormatAgentPluginSvgTest, SvgGetFormatTypeTest, TestSize.Level3)
38 {
39     GTEST_LOG_(INFO) << "FormatAgentPluginSvgTest: SvgFormatAgentPluginTest start";
40     ImagePlugin::SvgFormatAgent formatAgent;
41     auto ret = formatAgent.GetFormatType();
42     ASSERT_EQ(ret, SVG_FORMAT_TYPE);
43     GTEST_LOG_(INFO) << "FormatAgentPluginSvgTest: SvgFormatAgentPluginTest end";
44 }
45 
46 /**
47  * @tc.name: SvgGetHeaderSize
48  * @tc.desc: svg GetHeaderSize
49  * @tc.type: FUNC
50  */
51 HWTEST_F(FormatAgentPluginSvgTest, SvgGetHeaderSizeTest, TestSize.Level3)
52 {
53     GTEST_LOG_(INFO) << "FormatAgentPluginSvgTest: SvgFormatAgentPluginTest002 start";
54     ImagePlugin::SvgFormatAgent formatAgent;
55     auto ret = formatAgent.GetHeaderSize();
56     ASSERT_EQ(ret, sizeof(SVG_HEADER));
57     GTEST_LOG_(INFO) << "FormatAgentPluginSvgTest: SvgFormatAgentPluginTest002 end";
58 }
59 
60 /**
61  * @tc.name: SvgCheckFormatTest
62  * @tc.desc: svg CheckFormat
63  * @tc.type: FUNC
64  */
65 HWTEST_F(FormatAgentPluginSvgTest, SvgCheckFormatTest, TestSize.Level3)
66 {
67     GTEST_LOG_(INFO) << "FormatAgentPluginSvgTest: SvgCheckFormatTest start";
68     ImagePlugin::SvgFormatAgent formatAgent;
69     auto datasize = formatAgent.GetHeaderSize();
70     void *headerData = nullptr;
71     auto ret = formatAgent.CheckFormat(headerData, datasize);
72     ASSERT_EQ(ret, false);
73     GTEST_LOG_(INFO) << "FormatAgentPluginSvgTest: SvgCheckFormatTest end";
74 }
75 
76 /**
77  * @tc.name: SvgCreatePixelMapTest
78  * @tc.desc: svg CheckFormat
79  * @tc.type: FUNC
80  */
81 HWTEST_F(FormatAgentPluginSvgTest, SvgCreatePixelMapTest, TestSize.Level3)
82 {
83     GTEST_LOG_(INFO) << "FormatAgentPluginSvgTest: SvgCreatePixelMapTest start";
84     ImagePlugin::SvgFormatAgent formatAgent;
85     auto datasize = formatAgent.GetHeaderSize();
86     uint32_t errorCode = 0;
87     SourceOptions opts;
88     opts.formatHint = SVG_FORMAT_TYPE;
89     auto imageSource = ImageSource::CreateImageSource(IMAGE_INPUT_SVG_PATH, opts, errorCode);
90     ASSERT_EQ(errorCode, SUCCESS);
91     ASSERT_NE(imageSource.get(), nullptr);
92 
93     DecodeOptions decodeOpts;
94     auto pixelMap = imageSource->CreatePixelMap(decodeOpts, errorCode);
95     ASSERT_EQ(errorCode, SUCCESS);
96     ASSERT_NE(pixelMap.get(), nullptr);
97 
98     auto ret = formatAgent.CheckFormat(pixelMap->GetPixels(), datasize);
99     ASSERT_EQ(ret, false);
100     GTEST_LOG_(INFO) << "FormatAgentPluginSvgTest: SvgCreatePixelMapTest end";
101 }
102 
103 /**
104  * @tc.name: SvgCheckFormat
105  * @tc.desc: svg CheckFormat
106  * @tc.type: FUNC
107  */
108 HWTEST_F(FormatAgentPluginSvgTest, SvgCheckFormat, TestSize.Level3)
109 {
110     GTEST_LOG_(INFO) << "FormatAgentPluginSvgTest: SvgCheckFormat start";
111     ImagePlugin::SvgFormatAgent formatAgent;
112     auto datasize = formatAgent.GetHeaderSize();
113     auto ret = formatAgent.CheckFormat(nullptr, datasize);
114     ASSERT_EQ(ret, false);
115     GTEST_LOG_(INFO) << "FormatAgentPluginSvgTest: SvgCheckFormat end";
116 }
117 
118 /**
119  * @tc.name: SvgGetPixelsTest
120  * @tc.desc: svg CheckFormat
121  * @tc.type: FUNC
122  */
123 HWTEST_F(FormatAgentPluginSvgTest, SvgGetPixelsTest, TestSize.Level3)
124 {
125     GTEST_LOG_(INFO) << "FormatAgentPluginSvgTest: SvgGetPixelsTest start";
126     ImagePlugin::SvgFormatAgent formatAgent;
127     auto datasize = formatAgent.GetHeaderSize() - 10;
128     uint32_t errorCode = 0;
129     SourceOptions opts;
130     opts.formatHint = SVG_FORMAT_TYPE;
131     auto imageSource = ImageSource::CreateImageSource(IMAGE_INPUT_SVG_PATH, opts, errorCode);
132     ASSERT_EQ(errorCode, SUCCESS);
133     ASSERT_NE(imageSource.get(), nullptr);
134 
135     DecodeOptions decodeOpts;
136     auto pixelMap = imageSource->CreatePixelMap(decodeOpts, errorCode);
137     ASSERT_EQ(errorCode, SUCCESS);
138     ASSERT_NE(pixelMap.get(), nullptr);
139 
140     auto ret = formatAgent.CheckFormat(pixelMap->GetPixels(), datasize);
141     ASSERT_EQ(ret, false);
142     GTEST_LOG_(INFO) << "FormatAgentPluginSvgTest: SvgGetPixelsTest end";
143 }
144 
145 /**
146  * @tc.name: SvgCheckFormatTest002
147  * @tc.desc: svg CheckFormat
148  * @tc.type: FUNC
149  */
150 HWTEST_F(FormatAgentPluginSvgTest, SvgCheckFormatTest002, TestSize.Level3)
151 {
152     GTEST_LOG_(INFO) << "FormatAgentPluginSvgTest: SvgCheckFormatTest002 start";
153     ImagePlugin::SvgFormatAgent formatAgent;
154     uint32_t data = 0;
155     void *headerData = &data;
156     ASSERT_NE(headerData, nullptr);
157     uint32_t dataSize = 4;
158     auto ret = formatAgent.CheckFormat(headerData, dataSize);
159     ASSERT_EQ(ret, false);
160     GTEST_LOG_(INFO) << "FormatAgentPluginSvgTest: SvgCheckFormatTest002 end";
161 }
162 }
163 }