1 /* 2 * Copyright (C) 2022 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 <sys/types.h> 18 #include <sys/stat.h> 19 #include <fcntl.h> 20 #include <fstream> 21 #include "image/abs_image_encoder.h" 22 #include "image_packer.h" 23 #include "buffer_packer_stream.h" 24 #include "file_packer_stream.h" 25 #include "image_utils.h" 26 #include "log_tags.h" 27 #include "media_errors.h" 28 #include "ostream_packer_stream.h" 29 #include "plugin_server.h" 30 31 using namespace OHOS::Media; 32 using namespace testing::ext; 33 using namespace OHOS::HiviewDFX; 34 using namespace OHOS::ImagePlugin; 35 using namespace OHOS::MultimediaPlugin; 36 namespace OHOS { 37 namespace Multimedia { 38 constexpr uint32_t NUM_1 = 1; 39 constexpr uint32_t NUM_100 = 100; 40 constexpr int64_t BUFFER_SIZE = 2 * 1024 * 1024; 41 static const std::string IMAGE_INPUT_JPEG_PATH = "/data/local/tmp/image/test_packing.jpg"; 42 class ImagePackerTest : public testing::Test { 43 public: ImagePackerTest()44 ImagePackerTest() {} ~ImagePackerTest()45 ~ImagePackerTest() {} 46 }; 47 48 /** 49 * @tc.name: StartPacking001 50 * @tc.desc: test StartPacking 51 * @tc.type: FUNC 52 */ 53 HWTEST_F(ImagePackerTest, StartPacking001, TestSize.Level3) 54 { 55 GTEST_LOG_(INFO) << "ImagePackerTest: StartPacking001 start"; 56 ImagePacker pack; 57 uint8_t *outputData = nullptr; 58 uint32_t maxSize = 0; 59 const PackOption option; 60 uint32_t startpc = pack.StartPacking(outputData, maxSize, option); 61 ASSERT_EQ(startpc, ERR_IMAGE_INVALID_PARAMETER); 62 GTEST_LOG_(INFO) << "ImagePackerTest: StartPacking001 end"; 63 } 64 65 /** 66 * @tc.name: StartPacking002 67 * @tc.desc: test StartPacking 68 * @tc.type: FUNC 69 */ 70 HWTEST_F(ImagePackerTest, StartPacking002, TestSize.Level3) 71 { 72 GTEST_LOG_(INFO) << "ImagePackerTest: StartPacking002 start"; 73 ImagePacker pack; 74 uint8_t *outputData = nullptr; 75 uint32_t maxSize = 0; 76 PackOption option; 77 option.format = "image/jpeg"; 78 option.quality = NUM_100; 79 option.numberHint = NUM_1; 80 uint32_t startpc = pack.StartPacking(outputData, maxSize, option); 81 ASSERT_EQ(startpc, ERR_IMAGE_INVALID_PARAMETER); 82 GTEST_LOG_(INFO) << "ImagePackerTest: StartPacking002 end"; 83 } 84 85 /** 86 * @tc.name: StartPacking003 87 * @tc.desc: test StartPacking 88 * @tc.type: FUNC 89 */ 90 HWTEST_F(ImagePackerTest, StartPacking003, TestSize.Level3) 91 { 92 GTEST_LOG_(INFO) << "ImagePackerTest: StartPacking003 start"; 93 ImagePacker pack; 94 int64_t bufferSize = BUFFER_SIZE; 95 uint8_t *outputData = static_cast<uint8_t *>(malloc(bufferSize)); 96 uint32_t maxSize = 0; 97 PackOption option; 98 option.format = "image/jpeg"; 99 option.quality = NUM_100; 100 option.numberHint = NUM_1; 101 uint32_t startpc = pack.StartPacking(outputData, maxSize, option); 102 ASSERT_EQ(startpc, 0); 103 GTEST_LOG_(INFO) << "ImagePackerTest: StartPacking003 end"; 104 } 105 106 /** 107 * @tc.name: StartPacking004 108 * @tc.desc: test StartPacking 109 * @tc.type: FUNC 110 */ 111 HWTEST_F(ImagePackerTest, StartPacking004, TestSize.Level3) 112 { 113 GTEST_LOG_(INFO) << "ImagePackerTest: StartPacking004 start"; 114 ImagePacker pack; 115 const std::string filePath; 116 PackOption option; 117 uint32_t startpc = pack.StartPacking(filePath, option); 118 ASSERT_EQ(startpc, ERR_IMAGE_INVALID_PARAMETER); 119 GTEST_LOG_(INFO) << "ImagePackerTest: StartPacking004 end"; 120 } 121 122 /** 123 * @tc.name: StartPacking005 124 * @tc.desc: test StartPacking 125 * @tc.type: FUNC 126 */ 127 HWTEST_F(ImagePackerTest, StartPacking005, TestSize.Level3) 128 { 129 GTEST_LOG_(INFO) << "ImagePackerTest: StartPacking005 start"; 130 ImagePacker pack; 131 const std::string filePath = IMAGE_INPUT_JPEG_PATH; 132 PackOption option; 133 option.format = "image/jpeg"; 134 option.quality = NUM_100; 135 option.numberHint = NUM_1; 136 uint32_t startpc = pack.StartPacking(filePath, option); 137 ASSERT_EQ(startpc, 0); 138 GTEST_LOG_(INFO) << "ImagePackerTest: StartPacking005 end"; 139 } 140 141 /** 142 * @tc.name: StartPacking006 143 * @tc.desc: test StartPacking 144 * @tc.type: FUNC 145 */ 146 HWTEST_F(ImagePackerTest, StartPacking006, TestSize.Level3) 147 { 148 GTEST_LOG_(INFO) << "ImagePackerTest: StartPacking006 start"; 149 ImagePacker pack; 150 const int fd = 0; 151 const PackOption option; 152 uint32_t startpc = pack.StartPacking(fd, option); 153 ASSERT_EQ(startpc, ERR_IMAGE_INVALID_PARAMETER); 154 GTEST_LOG_(INFO) << "ImagePackerTest: StartPacking006 end"; 155 } 156 157 /** 158 * @tc.name: StartPacking007 159 * @tc.desc: test StartPacking 160 * @tc.type: FUNC 161 */ 162 HWTEST_F(ImagePackerTest, StartPacking007, TestSize.Level3) 163 { 164 GTEST_LOG_(INFO) << "ImagePackerTest: StartPacking007 start"; 165 ImagePacker pack; 166 const int fd = 0; 167 const int fd2 = open("/data/local/tmp/image/test.jpg", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); 168 PackOption option; 169 option.format = "image/jpeg"; 170 option.quality = NUM_100; 171 option.numberHint = NUM_1; 172 pack.StartPacking(fd, option); 173 pack.StartPacking(fd2, option); 174 PackOption option2; 175 option2.format = ""; 176 uint32_t ret = pack.StartPacking(fd2, option2); 177 ASSERT_NE(ret, OHOS::Media::SUCCESS); 178 GTEST_LOG_(INFO) << "ImagePackerTest: StartPacking007 end"; 179 } 180 181 /** 182 * @tc.name: StartPacking008 183 * @tc.desc: test StartPacking 184 * @tc.type: FUNC 185 */ 186 HWTEST_F(ImagePackerTest, StartPacking008, TestSize.Level3) 187 { 188 GTEST_LOG_(INFO) << "ImagePackerTest: StartPacking008 start"; 189 ImagePacker pack; 190 std::ostream &outputStream = std::cout; 191 const PackOption option; 192 uint32_t ret = pack.StartPacking(outputStream, option); 193 ASSERT_NE(ret, OHOS::Media::SUCCESS); 194 GTEST_LOG_(INFO) << "ImagePackerTest: StartPacking008 end"; 195 } 196 197 /** 198 * @tc.name: StartPacking009 199 * @tc.desc: test StartPacking 200 * @tc.type: FUNC 201 */ 202 HWTEST_F(ImagePackerTest, StartPacking009, TestSize.Level3) 203 { 204 GTEST_LOG_(INFO) << "ImagePackerTest: StartPacking009 start"; 205 ImagePacker pack; 206 std::ostream &outputStream = std::cout; 207 PackOption option; 208 option.format = "image/jpeg"; 209 option.quality = NUM_100; 210 option.numberHint = NUM_1; 211 uint32_t startpc = pack.StartPacking(outputStream, option); 212 ASSERT_EQ(startpc, 0); 213 GTEST_LOG_(INFO) << "ImagePackerTest: StartPacking009 end"; 214 } 215 216 /** 217 * @tc.name: StartPacking010 218 * @tc.desc: test StartPacking 219 * @tc.type: FUNC 220 */ 221 HWTEST_F(ImagePackerTest, StartPacking010, TestSize.Level3) 222 { 223 GTEST_LOG_(INFO) << "ImagePackerTest: StartPacking010 start"; 224 ImagePacker pack; 225 uint8_t *outPut = nullptr; 226 PackOption option; 227 option.format = ""; 228 option.quality = NUM_100; 229 option.numberHint = NUM_1; 230 pack.StartPacking(outPut, static_cast<uint32_t>(100), option); 231 pack.StartPacking(outPut, static_cast<uint32_t>(-1), option); 232 uint8_t outPut2 = 1; 233 uint32_t ret = pack.StartPacking(&outPut2, static_cast<uint32_t>(-1), option); 234 ASSERT_NE(ret, OHOS::Media::SUCCESS); 235 GTEST_LOG_(INFO) << "ImagePackerTest: StartPacking010 end"; 236 } 237 238 /** 239 * @tc.name: StartPacking012 240 * @tc.desc: test StartPacking 241 * @tc.type: FUNC 242 */ 243 HWTEST_F(ImagePackerTest, StartPacking012, TestSize.Level3) 244 { 245 GTEST_LOG_(INFO) << "ImagePackerTest: StartPacking012 start"; 246 ImagePacker pack; 247 const std::string filePath = IMAGE_INPUT_JPEG_PATH; 248 const std::string filePath2 = "ImagePackerTestNoImage.jpg"; 249 PackOption option; 250 option.format = "image/jpeg"; 251 option.quality = NUM_100; 252 option.numberHint = NUM_1; 253 pack.StartPacking(filePath2, option); 254 option.format = ""; 255 uint32_t ret = pack.StartPacking(filePath, option); 256 ASSERT_NE(ret, OHOS::Media::SUCCESS); 257 GTEST_LOG_(INFO) << "ImagePackerTest: StartPacking012 end"; 258 } 259 260 /** 261 * @tc.name: AddImage001 262 * @tc.desc: test AddImage 263 * @tc.type: FUNC 264 */ 265 HWTEST_F(ImagePackerTest, AddImage001, TestSize.Level3) 266 { 267 GTEST_LOG_(INFO) << "ImagePackerTest: AddImage001 start"; 268 ImagePacker pack; 269 PixelMap pixelMap; 270 pack.AddImage(pixelMap); 271 SourceOptions opts; 272 uint32_t errorCode = 0; 273 std::unique_ptr<ImageSource> imageSource = 274 ImageSource::CreateImageSource(IMAGE_INPUT_JPEG_PATH, opts, errorCode); 275 uint32_t ret = pack.AddImage(*imageSource); 276 ASSERT_NE(ret, OHOS::Media::SUCCESS); 277 GTEST_LOG_(INFO) << "ImagePackerTest: AddImage001 end"; 278 } 279 280 /** 281 * @tc.name: AddImage002 282 * @tc.desc: test AddImage 283 * @tc.type: FUNC 284 */ 285 HWTEST_F(ImagePackerTest, AddImage002, TestSize.Level3) 286 { 287 GTEST_LOG_(INFO) << "ImagePackerTest: AddImage002 start"; 288 ImagePacker pack; 289 uint32_t errorCode = 0; 290 SourceOptions opts; 291 opts.formatHint = -1; 292 std::unique_ptr<ImageSource> imageSource = 293 ImageSource::CreateImageSource(IMAGE_INPUT_JPEG_PATH, opts, errorCode); 294 uint32_t ret = pack.AddImage(*imageSource); 295 ASSERT_NE(ret, OHOS::Media::SUCCESS); 296 GTEST_LOG_(INFO) << "ImagePackerTest: AddImage002 end"; 297 } 298 299 /** 300 * @tc.name: AddImage003 301 * @tc.desc: test AddImage 302 * @tc.type: FUNC 303 */ 304 HWTEST_F(ImagePackerTest, AddImage003, TestSize.Level3) 305 { 306 GTEST_LOG_(INFO) << "ImagePackerTest: AddImage003 start"; 307 ImagePacker pack; 308 uint32_t errorCode = 0; 309 SourceOptions opts; 310 opts.formatHint = "image/jpeg"; 311 std::unique_ptr<ImageSource> imageSource = 312 ImageSource::CreateImageSource(IMAGE_INPUT_JPEG_PATH, opts, errorCode); 313 uint32_t index = 0; 314 uint32_t ret = pack.AddImage(*imageSource, index); 315 ASSERT_NE(ret, OHOS::Media::SUCCESS); 316 GTEST_LOG_(INFO) << "ImagePackerTest: AddImage003 end"; 317 } 318 319 /** 320 * @tc.name: FinalizePacking001 321 * @tc.desc: test FinalizePacking 322 * @tc.type: FUNC 323 */ 324 HWTEST_F(ImagePackerTest, FinalizePacking001, TestSize.Level3) 325 { 326 GTEST_LOG_(INFO) << "ImagePackerTest: FinalizePacking001 start"; 327 ImagePacker pack; 328 uint32_t ret = pack.FinalizePacking(); 329 ASSERT_NE(ret, OHOS::Media::SUCCESS); 330 GTEST_LOG_(INFO) << "ImagePackerTest: FinalizePacking001 end"; 331 } 332 333 /** 334 * @tc.name: FinalizePacking002 335 * @tc.desc: test FinalizePacking 336 * @tc.type: FUNC 337 */ 338 HWTEST_F(ImagePackerTest, FinalizePacking002, TestSize.Level3) 339 { 340 GTEST_LOG_(INFO) << "ImagePackerTest: FinalizePacking002 start"; 341 ImagePacker pack; 342 int64_t packedSize = 0; 343 uint32_t ret = pack.FinalizePacking(packedSize); 344 ASSERT_NE(ret, OHOS::Media::SUCCESS); 345 GTEST_LOG_(INFO) << "ImagePackerTest: FinalizePacking002 end"; 346 } 347 } // namespace Multimedia 348 } // namespace OHOS