• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 #define private public
18 #include "hardware/jpeg_hw_decoder.h"
19 #include "mock_jpeg_hw_decode_flow.h"
20 #include "image_system_properties.h"
21 #include "media_errors.h"
22 
23 namespace OHOS::Media {
24 using namespace testing::ext;
25 using namespace OHOS::ImagePlugin;
26 using namespace OHOS::HDI::Codec::Image::V2_1;
27 
28 class JpegHwDecoderTest : public testing::Test {
29 public:
30     static constexpr char JPEG_FORMAT[] = "image/jpeg";
31     static constexpr char HEIF_FORMAT[] = "image/heif";
32     static constexpr char TEST_JPEG_IMG[] = "/data/local/tmp/image/test_hw1.jpg";
33 };
34 
35 HWTEST_F(JpegHwDecoderTest, unsupported_img_empty_format, TestSize.Level1)
36 {
37     JpegHardwareDecoder testObj;
38     Size srcImgSize = {
39         .width = 8192,
40         .height = 8192
41     };
42     bool ret = testObj.IsHardwareDecodeSupported("", srcImgSize);
43     ASSERT_FALSE(ret);
44 }
45 
46 HWTEST_F(JpegHwDecoderTest, unsupported_img_unknown_format, TestSize.Level1)
47 {
48     JpegHardwareDecoder testObj;
49     Size srcImgSize = {
50         .width = 512,
51         .height = 512
52     };
53     bool ret = testObj.IsHardwareDecodeSupported(HEIF_FORMAT, srcImgSize);
54     ASSERT_FALSE(ret);
55 }
56 
57 HWTEST_F(JpegHwDecoderTest, unsupported_img_size_too_small, TestSize.Level1)
58 {
59     JpegHardwareDecoder testObj;
60     Size srcImgSize = {
61         .width = 140,
62         .height = 512
63     };
64     bool ret = testObj.IsHardwareDecodeSupported(JPEG_FORMAT, srcImgSize);
65     ASSERT_FALSE(ret);
66 }
67 
68 HWTEST_F(JpegHwDecoderTest, unsupported_img_size_too_big, TestSize.Level1)
69 {
70     JpegHardwareDecoder testObj;
71     Size srcImgSize = {
72         .width = 15000,
73         .height = 15001
74     };
75     bool ret = testObj.IsHardwareDecodeSupported(JPEG_FORMAT, srcImgSize);
76     ASSERT_FALSE(ret);
77 }
78 
79 HWTEST_F(JpegHwDecoderTest, decode_ok, TestSize.Level1)
80 {
81     CommandOpt opt;
82     opt.width = 1280;
83     opt.height = 768;
84     opt.sampleSize = 1;
85     opt.inputFile = TEST_JPEG_IMG;
86     JpegHwDecoderFlow demo;
87     bool ret = true;
88     if (ImageSystemProperties::GetHardWareDecodeEnabled()) {
89         ret = demo.Run(opt, false);
90     }
91     ASSERT_TRUE(ret);
92 }
93 
94 /**
95  * @tc.name: IsHardwareDecodeSupportedTest001
96  * @tc.desc: test the IsHardwareDecodeSupported
97              when hwDecoder_ is nullptr,return false
98  * @tc.type: FUNC
99  */
100 HWTEST_F(JpegHwDecoderTest, IsHardwareDecodeSupportedTest001, TestSize.Level3)
101 {
102     GTEST_LOG_(INFO) << "JpegHwDecoderTest: IsHardwareDecodeSupportedTest001 start";
103     JpegHardwareDecoder jpegHardwareDecoder;
104     jpegHardwareDecoder.hwDecoder_ = nullptr;
105     const std::string srcImgFormat = "jpeg";
106     Size srcImgSize = {1, 1};
107     bool ret = jpegHardwareDecoder.IsHardwareDecodeSupported(srcImgFormat, srcImgSize);
108     ASSERT_EQ(ret, false);
109     GTEST_LOG_(INFO) << "JpegHwDecoderTest: IsHardwareDecodeSupportedTest001 end";
110 }
111 
112 /**
113  * @tc.name: IsHardwareDecodeSupportedTest002
114  * @tc.desc: test the IsHardwareDecodeSupported
115              when hwDecoder_->GetImageCapability is empty,return false
116  * @tc.type: FUNC
117  */
118 HWTEST_F(JpegHwDecoderTest, IsHardwareDecodeSupportedTest002, TestSize.Level3)
119 {
120     GTEST_LOG_(INFO) << "JpegHwDecoderTest: IsHardwareDecodeSupportedTest002 start";
121     JpegHardwareDecoder jpegHardwareDecoder;
122     const std::string srcImgFormat = "jpeg";
123     Size srcImgSize = {1, 1};
124     bool ret = jpegHardwareDecoder.IsHardwareDecodeSupported(srcImgFormat, srcImgSize);
125     ASSERT_EQ(ret, false);
126     GTEST_LOG_(INFO) << "JpegHwDecoderTest: IsHardwareDecodeSupportedTest002 end";
127 }
128 
129 /**
130  * @tc.name: IsHardwareDecodeSupportedTest003
131  * @tc.desc: Verify hardware decode support check returns false for empty file path, zero size, and null decoder cases.
132  * @tc.type: FUNC
133  */
134 HWTEST_F(JpegHwDecoderTest, IsHardwareDecodeSupportedTest003, TestSize.Level3)
135 {
136     GTEST_LOG_(INFO) << "JpegHwDecoderTest: IsHardwareDecodeSupportedTest003 start";
137     JpegHardwareDecoder jpegHardwareDecoder;
138     ASSERT_NE(jpegHardwareDecoder.hwDecoder_, nullptr);
139     Media::Size size = {0, 0};
140     bool ret = jpegHardwareDecoder.IsHardwareDecodeSupported("", size);
141     EXPECT_FALSE(ret);
142     jpegHardwareDecoder.hwDecoder_ = nullptr;
143     ret = jpegHardwareDecoder.IsHardwareDecodeSupported("", size);
144     EXPECT_FALSE(ret);
145     GTEST_LOG_(INFO) << "JpegHwDecoderTest: IsHardwareDecodeSupportedTest003 end";
146 }
147 
148 /**
149  * @tc.name: CheckInputColorFmtTest001
150  * @tc.desc: test the CheckInputColorFmt
151              when codec is nullptr,return false
152  * @tc.type: FUNC
153  */
154 HWTEST_F(JpegHwDecoderTest, CheckInputColorFmtTest001, TestSize.Level3)
155 {
156     GTEST_LOG_(INFO) << "JpegHwDecoderTest: CheckInputColorFmtTest001 start";
157     JpegHardwareDecoder jpegHardwareDecoder;
158     SkCodec *codec = nullptr;
159     bool ret = jpegHardwareDecoder.CheckInputColorFmt(codec);
160     ASSERT_EQ(ret, false);
161     GTEST_LOG_(INFO) << "JpegHwDecoderTest: CheckInputColorFmtTest001 end";
162 }
163 
164 /**
165  * @tc.name: DecodeTest001
166  * @tc.desc: test the Decode
167              when hwDecoder_ is nullptr,return ERR_IMAGE_DECODE_ABNORMAL
168  * @tc.type: FUNC
169  */
170 HWTEST_F(JpegHwDecoderTest, DecodeTest001, TestSize.Level3)
171 {
172     GTEST_LOG_(INFO) << "JpegHwDecoderTest: DecodeTest001 start";
173     JpegHardwareDecoder jpegHardwareDecoder;
174     jpegHardwareDecoder.hwDecoder_ = nullptr;
175     SkCodec *codec = nullptr;
176     ImagePlugin::InputDataStream *srcStream = nullptr;
177     Size srcImgSize = {1, 1};
178     uint32_t sampleSize = 0;
179     CodecImageBuffer outputBuffer;
180     uint32_t ret = jpegHardwareDecoder.Decode(codec, srcStream, srcImgSize, sampleSize, outputBuffer);
181     ASSERT_EQ(ret, ERR_IMAGE_DECODE_ABNORMAL);
182     GTEST_LOG_(INFO) << "JpegHwDecoderTest: DecodeTest001 end";
183 }
184 
185 /**
186  * @tc.name: DecodeTest002
187  * @tc.desc: test the Decode
188              when codec is nullptr,return ERR_IMAGE_DATA_UNSUPPORT
189  * @tc.type: FUNC
190  */
191 HWTEST_F(JpegHwDecoderTest, DecodeTest002, TestSize.Level3)
192 {
193     GTEST_LOG_(INFO) << "JpegHwDecoderTest: DecodeTest002 start";
194     JpegHardwareDecoder jpegHardwareDecoder;
195     SkCodec *codec = nullptr;
196     ImagePlugin::InputDataStream *srcStream = nullptr;
197     Size srcImgSize = {1, 1};
198     uint32_t sampleSize = 0;
199     CodecImageBuffer outputBuffer;
200     uint32_t ret = jpegHardwareDecoder.Decode(codec, srcStream, srcImgSize, sampleSize, outputBuffer);
201     ASSERT_EQ(ret, ERR_IMAGE_DATA_UNSUPPORT);
202     GTEST_LOG_(INFO) << "JpegHwDecoderTest: DecodeTest002 end";
203 }
204 
205 /**
206  * @tc.name: AssembleComponentInfoTest001
207  * @tc.desc: test the AssembleComponentInfo
208              when num_components is not euqal 1 or 3,return false
209  * @tc.type: FUNC
210  */
211 HWTEST_F(JpegHwDecoderTest, AssembleComponentInfoTest001, TestSize.Level3)
212 {
213     GTEST_LOG_(INFO) << "JpegHwDecoderTest: AssembleComponentInfoTest001 start";
214     JpegHardwareDecoder jpegHardwareDecoder;
215     jpeg_decompress_struct jpegCompressInfo;
216     jpegCompressInfo.num_components = 2;
217     bool ret = jpegHardwareDecoder.AssembleComponentInfo(&jpegCompressInfo);
218     ASSERT_EQ(ret, false);
219     GTEST_LOG_(INFO) << "JpegHwDecoderTest: AssembleComponentInfoTest001 end";
220 }
221 
222 /**
223  * @tc.name: JumpOverCurrentJpegMarkerTest001
224  * @tc.desc: test the JumpOverCurrentJpegMarker
225              when curPos + JpegMarker::MARKER_LEN > totalLen,return false
226  * @tc.type: FUNC
227  */
228 HWTEST_F(JpegHwDecoderTest, JumpOverCurrentJpegMarkerTest001, TestSize.Level3)
229 {
230     GTEST_LOG_(INFO) << "JpegHwDecoderTest: JumpOverCurrentJpegMarkerTest001 start";
231     JpegHardwareDecoder jpegHardwareDecoder;
232     ImagePlugin::InputDataStream* srcStream = nullptr;
233     unsigned int curPos = 1;
234     unsigned int totalLen = 1;
235     uint16_t marker = 0;
236     bool ret = jpegHardwareDecoder.JumpOverCurrentJpegMarker(srcStream, curPos, totalLen, marker);
237     ASSERT_EQ(ret, false);
238     GTEST_LOG_(INFO) << "JpegHwDecoderTest: JumpOverCurrentJpegMarkerTest001 end";
239 }
240 
241 /**
242  * @tc.name: PrepareInputDataTest001
243  * @tc.desc: test the PrepareInputData
244              when srcStream is nullptr,return false
245  * @tc.type: FUNC
246  */
247 HWTEST_F(JpegHwDecoderTest, PrepareInputDataTest001, TestSize.Level3)
248 {
249     GTEST_LOG_(INFO) << "JpegHwDecoderTest: PrepareInputDataTest001 start";
250     JpegHardwareDecoder jpegHardwareDecoder;
251     SkCodec *codec = nullptr;
252     ImagePlugin::InputDataStream *srcStream = nullptr;
253     bool ret = jpegHardwareDecoder.PrepareInputData(codec, srcStream);
254     ASSERT_EQ(ret, false);
255     GTEST_LOG_(INFO) << "JpegHwDecoderTest: PrepareInputDataTest001 end";
256 }
257 } // namespace OHOS::Media