1 /* 2 * Copyright (C) 2021 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 <fstream> 18 #include <fcntl.h> 19 #define private public 20 #include "directory_ex.h" 21 #include "graphic_common.h" 22 #include "image_packer.h" 23 #include "image_source.h" 24 #include "image_type.h" 25 #include "image_utils.h" 26 #include "image_receiver.h" 27 #include "image_receiver_manager.h" 28 #include "image_source_util.h" 29 #include "incremental_pixel_map.h" 30 #include "incremental_source_stream.h" 31 #include "media_errors.h" 32 #include "pixel_map.h" 33 #include "pixel_map_manager.h" 34 35 36 using namespace testing::ext; 37 using namespace OHOS::Media; 38 namespace OHOS { 39 namespace Multimedia { 40 static const std::string IMAGE_INPUT_JPEG_PATH = "/data/local/tmp/image/test.jpg"; 41 static const std::string OPTION_FORMAT_TEST = "image/jpeg"; 42 static const std::int32_t OPTION_QUALITY_TEST = 100; 43 static const std::int32_t OPTION_NUMBERHINT_TEST = 1; 44 45 class InterfaceTest : public testing::Test { 46 public: InterfaceTest()47 InterfaceTest() {} ~InterfaceTest()48 ~InterfaceTest() {} 49 }; 50 51 /** 52 * @tc.name: InterfaceTest001 53 * @tc.desc: PromoteDecoding 54 * @tc.type: FUNC 55 */ 56 HWTEST_F(InterfaceTest, InterfaceTest001, TestSize.Level3) 57 { 58 GTEST_LOG_(INFO) << "InterfaceTest: InterfaceTest001 start"; 59 uint32_t errorCode = 0; 60 IncrementalSourceOptions incOpts; 61 incOpts.incrementalMode = IncrementalMode::INCREMENTAL_DATA; 62 std::unique_ptr<ImageSource> imageSource = ImageSource::CreateIncrementalImageSource(incOpts, errorCode); 63 DecodeOptions decodeOpts; 64 std::unique_ptr<IncrementalPixelMap> incPixelMap = imageSource->CreateIncrementalPixelMap(0, decodeOpts, 65 errorCode); 66 uint8_t decodeProgress = 0; 67 uint32_t ret = incPixelMap->PromoteDecoding(decodeProgress); 68 ASSERT_EQ(ret, ERR_IMAGE_SOURCE_DATA); 69 GTEST_LOG_(INFO) << "InterfaceTest: InterfaceTest001 end"; 70 } 71 72 /** 73 * @tc.name: InterfaceTest002 74 * @tc.desc: DetachFromDecoding 75 * @tc.type: FUNC 76 */ 77 HWTEST_F(InterfaceTest, InterfaceTest002, TestSize.Level3) 78 { 79 GTEST_LOG_(INFO) << "InterfaceTest: InterfaceTest002 start"; 80 uint32_t errorCode = 0; 81 IncrementalSourceOptions incOpts; 82 incOpts.incrementalMode = IncrementalMode::INCREMENTAL_DATA; 83 std::unique_ptr<ImageSource> imageSource = ImageSource::CreateIncrementalImageSource(incOpts, errorCode); 84 DecodeOptions decodeOpts; 85 std::unique_ptr<IncrementalPixelMap> incPixelMap = imageSource->CreateIncrementalPixelMap(0, decodeOpts, 86 errorCode); 87 incPixelMap->DetachFromDecoding(); 88 ASSERT_NE(incPixelMap, nullptr); 89 GTEST_LOG_(INFO) << "InterfaceTest: InterfaceTest002 end"; 90 } 91 92 /** 93 * @tc.name: InterfaceTest003 94 * @tc.desc: GetDecodingStatus 95 * @tc.type: FUNC 96 */ 97 HWTEST_F(InterfaceTest, InterfaceTest003, TestSize.Level3) 98 { 99 GTEST_LOG_(INFO) << "InterfaceTest: InterfaceTest003 start"; 100 uint32_t errorCode = 0; 101 IncrementalSourceOptions incOpts; 102 incOpts.incrementalMode = IncrementalMode::INCREMENTAL_DATA; 103 std::unique_ptr<ImageSource> imageSource = ImageSource::CreateIncrementalImageSource(incOpts, errorCode); 104 DecodeOptions decodeOpts; 105 std::unique_ptr<IncrementalPixelMap> incPixelMap = imageSource->CreateIncrementalPixelMap(0, decodeOpts, 106 errorCode); 107 incPixelMap->DetachFromDecoding(); 108 IncrementalDecodingStatus status = incPixelMap->GetDecodingStatus(); 109 ASSERT_EQ(status.decodingProgress, 0); 110 GTEST_LOG_(INFO) << "InterfaceTest: InterfaceTest003 end"; 111 } 112 113 /** 114 * @tc.name: InterfaceTest004 115 * @tc.desc: FreePixels 116 * @tc.type: FUNC 117 */ 118 HWTEST_F(InterfaceTest, InterfaceTest004, TestSize.Level3) 119 { 120 GTEST_LOG_(INFO) << "InterfaceTest: InterfaceTest004 start"; 121 PixelMap *pixelMap = nullptr; 122 PixelMapManager pixelMapManager(pixelMap); 123 pixelMapManager.FreePixels(); 124 ASSERT_EQ(&pixelMapManager.GetPixelMap(), nullptr); 125 GTEST_LOG_(INFO) << "InterfaceTest: InterfaceTest004 end"; 126 } 127 128 /** 129 * @tc.name: InterfaceTest005 130 * @tc.desc: Invalid 131 * @tc.type: FUNC 132 */ 133 HWTEST_F(InterfaceTest, InterfaceTest005, TestSize.Level3) 134 { 135 GTEST_LOG_(INFO) << "InterfaceTest: InterfaceTest005 start"; 136 PixelMap *pixelMap = nullptr; 137 PixelMapManager pixelMapManager(pixelMap); 138 bool flag = pixelMapManager.Invalid(); 139 ASSERT_EQ(flag, true); 140 GTEST_LOG_(INFO) << "InterfaceTest: InterfaceTest005 end"; 141 } 142 143 /** 144 * @tc.name: InterfaceTest006 145 * @tc.desc: GetPixelMap 146 * @tc.type: FUNC 147 */ 148 HWTEST_F(InterfaceTest, InterfaceTest006, TestSize.Level3) 149 { 150 GTEST_LOG_(INFO) << "InterfaceTest: InterfaceTest006 start"; 151 PixelMap *pixelMap = nullptr; 152 PixelMapManager pixelMapManager(pixelMap); 153 ASSERT_EQ(&pixelMapManager.GetPixelMap(), nullptr); 154 GTEST_LOG_(INFO) << "InterfaceTest: InterfaceTest006 end"; 155 } 156 157 /** 158 * @tc.name: InterfaceTest007 159 * @tc.desc: GetByteCount 160 * @tc.type: FUNC 161 */ 162 HWTEST_F(InterfaceTest, InterfaceTest007, TestSize.Level3) 163 { 164 GTEST_LOG_(INFO) << "InterfaceTest: InterfaceTest007 start"; 165 PixelMap *pixelMap = nullptr; 166 PixelMapManager pixelMapManager(pixelMap); 167 int32_t count = pixelMapManager.GetByteCount(); 168 ASSERT_EQ(count, 0); 169 GTEST_LOG_(INFO) << "InterfaceTest: InterfaceTest007 end"; 170 } 171 172 /** 173 * @tc.name: InterfaceTest008 174 * @tc.desc: Ref 175 * @tc.type: FUNC 176 */ 177 HWTEST_F(InterfaceTest, InterfaceTest008, TestSize.Level3) 178 { 179 GTEST_LOG_(INFO) << "InterfaceTest: InterfaceTest008 start"; 180 PixelMap *pixelMap = nullptr; 181 PixelMapManager pixelMapManager(pixelMap); 182 pixelMapManager.Ref(); 183 ASSERT_EQ(pixelMap, nullptr); 184 GTEST_LOG_(INFO) << "InterfaceTest: InterfaceTest008 end"; 185 } 186 187 /** 188 * @tc.name: InterfaceTest009 189 * @tc.desc: UnRef 190 * @tc.type: FUNC 191 */ 192 HWTEST_F(InterfaceTest, InterfaceTest009, TestSize.Level3) 193 { 194 GTEST_LOG_(INFO) << "InterfaceTest: InterfaceTest009 start"; 195 PixelMap *pixelMap = nullptr; 196 PixelMapManager pixelMapManager(pixelMap); 197 pixelMapManager.UnRef(); 198 ASSERT_EQ(pixelMap, nullptr); 199 GTEST_LOG_(INFO) << "InterfaceTest: InterfaceTest009 end"; 200 } 201 202 /** 203 * @tc.name: InterfaceTest0010 204 * @tc.desc: ImagePacker StartPacking 205 * @tc.type: FUNC 206 */ 207 HWTEST_F(InterfaceTest, InterfaceTest0010, TestSize.Level3) 208 { 209 GTEST_LOG_(INFO) << "InterfaceTest: InterfaceTest0010 start"; 210 ImagePacker imagePacker; 211 PackOption option; 212 option.format = OPTION_FORMAT_TEST; 213 option.quality = OPTION_QUALITY_TEST; 214 option.numberHint = OPTION_NUMBERHINT_TEST; 215 size_t bufferSize = 0; 216 bool ret = ImageUtils::GetFileSize(IMAGE_INPUT_JPEG_PATH, bufferSize); 217 ASSERT_EQ(ret, true); 218 uint8_t *buffer = reinterpret_cast<uint8_t *>(malloc(bufferSize)); 219 imagePacker.StartPacking(buffer, bufferSize, option); 220 GTEST_LOG_(INFO) << "InterfaceTest: InterfaceTest0010 end"; 221 } 222 223 /** 224 * @tc.name: InterfaceTest0011 225 * @tc.desc: ImagePacker StartPacking 226 * @tc.type: FUNC 227 */ 228 HWTEST_F(InterfaceTest, InterfaceTest0011, TestSize.Level3) 229 { 230 GTEST_LOG_(INFO) << "InterfaceTest: InterfaceTest0011 start"; 231 ImagePacker imagePacker; 232 PackOption option; 233 option.format = OPTION_FORMAT_TEST; 234 option.quality = OPTION_QUALITY_TEST; 235 option.numberHint = OPTION_NUMBERHINT_TEST; 236 size_t bufferSize = 0; 237 bool ret = ImageUtils::GetFileSize(IMAGE_INPUT_JPEG_PATH, bufferSize); 238 ASSERT_EQ(ret, true); 239 uint8_t *buffer = nullptr; 240 imagePacker.StartPacking(buffer, bufferSize, option); 241 GTEST_LOG_(INFO) << "InterfaceTest: InterfaceTest0011 end"; 242 } 243 244 /** 245 * @tc.name: InterfaceTest0012 246 * @tc.desc: ImagePacker StartPacking 247 * @tc.type: FUNC 248 */ 249 HWTEST_F(InterfaceTest, InterfaceTest0012, TestSize.Level3) 250 { 251 GTEST_LOG_(INFO) << "InterfaceTest: InterfaceTest0012 start"; 252 ImagePacker imagePacker; 253 PackOption option; 254 option.format = OPTION_FORMAT_TEST; 255 option.quality = OPTION_QUALITY_TEST; 256 option.numberHint = OPTION_NUMBERHINT_TEST; 257 size_t bufferSize = 0; 258 uint8_t *buffer = reinterpret_cast<uint8_t *>(malloc(bufferSize)); 259 ASSERT_NE(buffer, nullptr); 260 uint32_t tmp = imagePacker.StartPacking(buffer, bufferSize, option); 261 ASSERT_EQ(tmp, SUCCESS); 262 GTEST_LOG_(INFO) << "InterfaceTest: InterfaceTest0012 end"; 263 } 264 265 /** 266 * @tc.name: PromoteDecodingTest001 267 * @tc.desc: Test PromoteDecoding when imageSource is nullptr and decodingState is 268 * BASE_INFO_ERROR or IMAGE_ERROR or others. 269 * @tc.type: FUNC 270 */ 271 HWTEST_F(InterfaceTest, PromoteDecodingTest001, TestSize.Level3) 272 { 273 GTEST_LOG_(INFO) << "InterfaceTest: PromoteDecodingTest001 start"; 274 unique_ptr<IncrementalSourceStream> incrementalSourceStream = 275 std::make_unique<IncrementalSourceStream>(IncrementalMode::INCREMENTAL_DATA); 276 unique_ptr<SourceStream> sourceStream = std::move(incrementalSourceStream); 277 SourceOptions sourceOpts; 278 DecodeOptions decodeOpts; 279 ImageSource imageSource(std::move(sourceStream), sourceOpts); 280 IncrementalPixelMap incrementalPixelMap(0, decodeOpts, &imageSource); 281 incrementalPixelMap.imageSource_ = nullptr; 282 incrementalPixelMap.decodingStatus_.state = IncrementalDecodingState::UNRESOLVED; 283 uint8_t decodeProgress = 0; 284 285 uint32_t errCode = incrementalPixelMap.PromoteDecoding(decodeProgress); 286 EXPECT_EQ(errCode, ERR_IMAGE_SOURCE_DATA); 287 288 incrementalPixelMap.decodingStatus_.state = IncrementalDecodingState::BASE_INFO_ERROR; 289 errCode = incrementalPixelMap.PromoteDecoding(decodeProgress); 290 EXPECT_EQ(errCode, 0); 291 292 incrementalPixelMap.decodingStatus_.state = IncrementalDecodingState::IMAGE_ERROR; 293 errCode = incrementalPixelMap.PromoteDecoding(decodeProgress); 294 EXPECT_EQ(errCode, 0); 295 GTEST_LOG_(INFO) << "InterfaceTest: PromoteDecodingTest001 end"; 296 } 297 } 298 }