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 "image_log.h" 18 #include "image_source_util.h" 19 #include "media_errors.h" 20 #include "image_source.h" 21 22 using namespace testing::ext; 23 using namespace OHOS::Media; 24 using namespace OHOS::HiviewDFX; 25 using namespace OHOS::ImageSourceUtil; 26 namespace OHOS { 27 namespace Media { 28 namespace { 29 static constexpr HiLogLabel LABEL_TEST = { LOG_CORE, LOG_TAG_DOMAIN_ID_IMAGE, "ImageSourceGifExTest" }; 30 static const std::string INPUT_PATH = "/data/local/tmp/image/"; 31 static const std::string OUTPUT_PATH = "/data/local/tmp/image/output_"; 32 static const std::string OUTPUT_EXT = ".jpg"; 33 static const std::string TEST_FILE_SINGLE_FRAME_GIF = "test.gif"; 34 static const size_t TEST_FILE_SINGLE_FRAME_GIF_FRAME_COUNT = 1; 35 static const std::string TEST_FILE_MULTI_FRAME_GIF = "moving_test.gif"; 36 static const size_t TEST_FILE_MULTI_FRAME_GIF_FRAME_COUNT = 3; 37 static const std::string TEST_FILE_JPG = "test.jpg"; 38 static const size_t TEST_FILE_JPG_FRAME_COUNT = 1; 39 } 40 41 class ImageSourceGifExTest : public testing::Test { 42 public: ImageSourceGifExTest()43 ImageSourceGifExTest() {} ~ImageSourceGifExTest()44 ~ImageSourceGifExTest() {} 45 }; 46 47 /** 48 * @tc.name: CreatePixelMapList001 49 * @tc.desc: test CreatePixelMapList 50 * @tc.type: FUNC 51 */ 52 HWTEST_F(ImageSourceGifExTest, CreatePixelMapList001, TestSize.Level3) 53 { 54 GTEST_LOG_(INFO) << "ImageSourceGifExTest: CreatePixelMapList001 start"; 55 56 const std::string testName = TEST_FILE_SINGLE_FRAME_GIF; 57 58 uint32_t errorCode = 0; 59 const SourceOptions opts; 60 const std::string inputName = INPUT_PATH + testName; 61 auto imageSource = ImageSource::CreateImageSource(inputName, opts, errorCode); 62 63 const DecodeOptions decodeOpts; 64 auto pixelMaps = imageSource->CreatePixelMapList(decodeOpts, errorCode); 65 ASSERT_EQ(errorCode, SUCCESS); 66 ASSERT_NE(pixelMaps, nullptr); 67 ASSERT_EQ(pixelMaps->size(), TEST_FILE_SINGLE_FRAME_GIF_FRAME_COUNT); 68 69 int32_t index = 0; 70 for (auto &pixelMap : *pixelMaps) { 71 ASSERT_NE(pixelMap, nullptr); 72 const std::string outputName = OUTPUT_PATH + testName + "." + std::to_string(index) + OUTPUT_EXT; 73 int64_t packSize = PackImage(outputName, std::move(pixelMap)); 74 ASSERT_NE(packSize, 0); 75 index++; 76 } 77 78 GTEST_LOG_(INFO) << "ImageSourceGifExTest: CreatePixelMapList001 end"; 79 } 80 81 /** 82 * @tc.name: CreatePixelMapList002 83 * @tc.desc: test CreatePixelMapList 84 * @tc.type: FUNC 85 */ 86 HWTEST_F(ImageSourceGifExTest, CreatePixelMapList002, TestSize.Level3) 87 { 88 GTEST_LOG_(INFO) << "ImageSourceGifExTest: CreatePixelMapList002 start"; 89 90 const std::string testName = TEST_FILE_MULTI_FRAME_GIF; 91 92 uint32_t errorCode = 0; 93 const SourceOptions opts; 94 const std::string inputName = INPUT_PATH + testName; 95 auto imageSource = ImageSource::CreateImageSource(inputName, opts, errorCode); 96 97 const DecodeOptions decodeOpts; 98 auto pixelMaps = imageSource->CreatePixelMapList(decodeOpts, errorCode); 99 ASSERT_EQ(errorCode, SUCCESS); 100 ASSERT_NE(pixelMaps, nullptr); 101 ASSERT_EQ(pixelMaps->size(), TEST_FILE_MULTI_FRAME_GIF_FRAME_COUNT); 102 103 int32_t index = 0; 104 for (auto &pixelMap : *pixelMaps) { 105 ASSERT_NE(pixelMap, nullptr); 106 const std::string outputName = OUTPUT_PATH + testName + "." + std::to_string(index) + OUTPUT_EXT; 107 int64_t packSize = PackImage(outputName, std::move(pixelMap)); 108 ASSERT_NE(packSize, 0); 109 index++; 110 } 111 112 GTEST_LOG_(INFO) << "ImageSourceGifExTest: CreatePixelMapList002 end"; 113 } 114 115 /** 116 * @tc.name: CreatePixelMapList003 117 * @tc.desc: test CreatePixelMapList 118 * @tc.type: FUNC 119 */ 120 HWTEST_F(ImageSourceGifExTest, CreatePixelMapList003, TestSize.Level3) 121 { 122 GTEST_LOG_(INFO) << "ImageSourceGifExTest: CreatePixelMapList003 start"; 123 124 const std::string testName = TEST_FILE_JPG; 125 126 uint32_t errorCode = 0; 127 const SourceOptions opts; 128 const std::string inputName = INPUT_PATH + testName; 129 auto imageSource = ImageSource::CreateImageSource(inputName, opts, errorCode); 130 131 const DecodeOptions decodeOpts; 132 auto pixelMaps = imageSource->CreatePixelMapList(decodeOpts, errorCode); 133 ASSERT_EQ(errorCode, SUCCESS); 134 ASSERT_NE(pixelMaps, nullptr); 135 ASSERT_EQ(pixelMaps->size(), TEST_FILE_JPG_FRAME_COUNT); 136 137 int32_t index = 0; 138 for (auto &pixelMap : *pixelMaps) { 139 ASSERT_NE(pixelMap, nullptr); 140 const std::string outputName = OUTPUT_PATH + testName + "." + std::to_string(index) + OUTPUT_EXT; 141 int64_t packSize = PackImage(outputName, std::move(pixelMap)); 142 ASSERT_NE(packSize, 0); 143 index++; 144 } 145 146 GTEST_LOG_(INFO) << "ImageSourceGifExTest: CreatePixelMapList003 end"; 147 } 148 149 /** 150 * @tc.name: GetDelayTime001 151 * @tc.desc: test GetDelayTime 152 * @tc.type: FUNC 153 */ 154 HWTEST_F(ImageSourceGifExTest, GetDelayTime001, TestSize.Level3) 155 { 156 GTEST_LOG_(INFO) << "ImageSourceGifExTest: GetDelayTime001 start"; 157 158 const std::string testName = TEST_FILE_SINGLE_FRAME_GIF; 159 160 uint32_t errorCode = 0; 161 const SourceOptions opts; 162 const std::string inputName = INPUT_PATH + testName; 163 auto imageSource = ImageSource::CreateImageSource(inputName, opts, errorCode); 164 165 auto delayTimes = imageSource->GetDelayTime(errorCode); 166 ASSERT_EQ(errorCode, SUCCESS); 167 ASSERT_NE(delayTimes, nullptr); 168 ASSERT_EQ(delayTimes->size(), TEST_FILE_SINGLE_FRAME_GIF_FRAME_COUNT); 169 170 for (auto delayTime : *delayTimes) { 171 HiLog::Debug(LABEL_TEST, "delay time is %{public}u.", delayTime); 172 } 173 174 GTEST_LOG_(INFO) << "ImageSourceGifExTest: GetDelayTime001 end"; 175 } 176 177 /** 178 * @tc.name: GetDelayTime002 179 * @tc.desc: test GetDelayTime 180 * @tc.type: FUNC 181 */ 182 HWTEST_F(ImageSourceGifExTest, GetDelayTime002, TestSize.Level3) 183 { 184 GTEST_LOG_(INFO) << "ImageSourceGifExTest: GetDelayTime002 start"; 185 186 const std::string testName = TEST_FILE_MULTI_FRAME_GIF; 187 188 uint32_t errorCode = 0; 189 const SourceOptions opts; 190 const std::string inputName = INPUT_PATH + testName; 191 auto imageSource = ImageSource::CreateImageSource(inputName, opts, errorCode); 192 193 auto delayTimes = imageSource->GetDelayTime(errorCode); 194 ASSERT_EQ(errorCode, SUCCESS); 195 ASSERT_NE(delayTimes, nullptr); 196 ASSERT_EQ(delayTimes->size(), TEST_FILE_MULTI_FRAME_GIF_FRAME_COUNT); 197 198 for (auto delayTime : *delayTimes) { 199 HiLog::Debug(LABEL_TEST, "delay time is %{public}u.", delayTime); 200 } 201 202 GTEST_LOG_(INFO) << "ImageSourceGifExTest: GetDelayTime002 end"; 203 } 204 205 /** 206 * @tc.name: GetDelayTime003 207 * @tc.desc: test GetDelayTime 208 * @tc.type: FUNC 209 */ 210 HWTEST_F(ImageSourceGifExTest, GetDelayTime003, TestSize.Level3) 211 { 212 GTEST_LOG_(INFO) << "ImageSourceGifExTest: GetDelayTime003 start"; 213 214 const std::string testName = TEST_FILE_JPG; 215 216 uint32_t errorCode = 0; 217 const SourceOptions opts; 218 const std::string inputName = INPUT_PATH + testName; 219 auto imageSource = ImageSource::CreateImageSource(inputName, opts, errorCode); 220 221 auto delayTimes = imageSource->GetDelayTime(errorCode); 222 ASSERT_NE(errorCode, SUCCESS); 223 ASSERT_EQ(delayTimes, nullptr); 224 225 GTEST_LOG_(INFO) << "ImageSourceGifExTest: GetDelayTime003 end"; 226 } 227 228 /** 229 * @tc.name: GetFrameCount001 230 * @tc.desc: test GetFrameCount 231 * @tc.type: FUNC 232 */ 233 HWTEST_F(ImageSourceGifExTest, GetFrameCount001, TestSize.Level3) 234 { 235 GTEST_LOG_(INFO) << "ImageSourceGifExTest: GetFrameCount001 start"; 236 237 const std::string testName = TEST_FILE_SINGLE_FRAME_GIF; 238 239 uint32_t errorCode = 0; 240 const SourceOptions opts; 241 const std::string inputName = INPUT_PATH + testName; 242 auto imageSource = ImageSource::CreateImageSource(inputName, opts, errorCode); 243 244 auto frameCount = imageSource->GetFrameCount(errorCode); 245 ASSERT_EQ(errorCode, SUCCESS); 246 ASSERT_EQ(frameCount, TEST_FILE_SINGLE_FRAME_GIF_FRAME_COUNT); 247 248 GTEST_LOG_(INFO) << "ImageSourceGifExTest: GetFrameCount001 end"; 249 } 250 251 /** 252 * @tc.name: GetFrameCount002 253 * @tc.desc: test GetFrameCount 254 * @tc.type: FUNC 255 */ 256 HWTEST_F(ImageSourceGifExTest, GetFrameCount002, TestSize.Level3) 257 { 258 GTEST_LOG_(INFO) << "ImageSourceGifExTest: GetFrameCount002 start"; 259 260 const std::string testName = TEST_FILE_MULTI_FRAME_GIF; 261 262 uint32_t errorCode = 0; 263 const SourceOptions opts; 264 const std::string inputName = INPUT_PATH + testName; 265 auto imageSource = ImageSource::CreateImageSource(inputName, opts, errorCode); 266 267 auto frameCount = imageSource->GetFrameCount(errorCode); 268 ASSERT_EQ(frameCount, TEST_FILE_MULTI_FRAME_GIF_FRAME_COUNT); 269 270 GTEST_LOG_(INFO) << "ImageSourceGifExTest: GetFrameCount002 end"; 271 } 272 273 /** 274 * @tc.name: GetFrameCount003 275 * @tc.desc: test GetFrameCount 276 * @tc.type: FUNC 277 */ 278 HWTEST_F(ImageSourceGifExTest, GetFrameCount003, TestSize.Level3) 279 { 280 GTEST_LOG_(INFO) << "ImageSourceGifExTest: GetFrameCount003 start"; 281 282 const std::string testName = TEST_FILE_JPG; 283 284 uint32_t errorCode = 0; 285 const SourceOptions opts; 286 const std::string inputName = INPUT_PATH + testName; 287 auto imageSource = ImageSource::CreateImageSource(inputName, opts, errorCode); 288 289 auto frameCount = imageSource->GetFrameCount(errorCode); 290 ASSERT_EQ(frameCount, TEST_FILE_JPG_FRAME_COUNT); 291 292 GTEST_LOG_(INFO) << "ImageSourceGifExTest: GetFrameCount003 end"; 293 } 294 } // namespace Multimedia 295 } // namespace OHOS