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 "color_picker_unittest.h" 17 #include "color_picker.h" 18 #include "color.h" 19 #include "image_source.h" 20 #include "pixel_map.h" 21 #include "effect_errors.h" 22 #include "hilog/log.h" 23 #include "test_picture_files.h" 24 25 using namespace testing; 26 using namespace testing::ext; 27 using namespace OHOS::Media; 28 using namespace OHOS::HiviewDFX; 29 30 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL_TEST = { 31 LOG_CORE, LOG_DOMAIN, "ColorPickerTest" 32 }; 33 34 namespace OHOS { 35 namespace Rosen { 36 /** 37 * @tc.name: CreateColorPickerFromPixelmapTest001 38 * @tc.desc: Ensure the ability of creating color picker from pixelmap. 39 * @tc.type: FUNC 40 * @tc.require: 41 * @tc.author: 42 */ 43 HWTEST_F(ColorPickerUnittest, CreateColorPickerFromPixelmapTest001, TestSize.Level1) 44 { 45 GTEST_LOG_(INFO) << "ColorPickerUnittest CreateColorPickerFromPixelmapTest001 start"; 46 /** 47 * @tc.steps: step1. Create a pixelmap 48 */ 49 Media::InitializationOptions opts; 50 opts.size.width = 200; 51 opts.size.height = 150; 52 opts.editable = true; 53 std::unique_ptr<Media::PixelMap> pixmap = Media::PixelMap::Create(opts); 54 55 /** 56 * @tc.steps: step2. Call create From pixelMap 57 */ 58 uint32_t errorCode = SUCCESS; 59 std::shared_ptr<ColorPicker> pColorPicker = ColorPicker::CreateColorPicker(std::move(pixmap), errorCode); 60 ASSERT_EQ(errorCode, SUCCESS); 61 EXPECT_NE(pColorPicker, nullptr); 62 } 63 64 /** 65 * @tc.name: CreateColorPickerFromPixelmapTest002 66 * @tc.desc: Ensure the ability of creating color picker from pixelmap. 67 * @tc.type: FUNC 68 * @tc.require: 69 * @tc.author: 70 */ 71 HWTEST_F(ColorPickerUnittest, CreateColorPickerFromPixelmapTest002, TestSize.Level1) 72 { 73 GTEST_LOG_(INFO) << "ColorPickerUnittest CreateColorPickerFromPixelmapTest002 start"; 74 size_t bufferSize = 0; 75 uint8_t *buffer = GetPngBuffer(bufferSize); 76 ASSERT_NE(buffer, nullptr); 77 78 /** 79 * @tc.steps: step1. Create a ImageSource 80 */ 81 uint32_t errorCode = 0; 82 SourceOptions opts; 83 opts.formatHint = "image/png"; 84 std::unique_ptr<ImageSource> imageSource = 85 ImageSource::CreateImageSource(buffer, bufferSize, opts, errorCode); 86 ASSERT_EQ(errorCode, SUCCESS); 87 88 /** 89 * @tc.steps: step2. decode image source to pixel map by default decode options 90 * @tc.expected: step2. decode image source to pixel map success. 91 */ 92 DecodeOptions decodeOpts; 93 std::unique_ptr<PixelMap> pixmap = imageSource->CreatePixelMap(decodeOpts, errorCode); 94 HiLog::Debug(LABEL_TEST, "create pixel map error code=%{public}u.", errorCode); 95 ASSERT_EQ(errorCode, SUCCESS); 96 ASSERT_NE(pixmap.get(), nullptr); 97 98 /** 99 * @tc.steps: step3. Call create From pixelMap 100 */ 101 std::shared_ptr<ColorPicker> pColorPicker = ColorPicker::CreateColorPicker(std::move(pixmap), errorCode); 102 EXPECT_NE(pColorPicker, nullptr); 103 } 104 105 /** 106 * @tc.name: CreateColorPickerFromPixelmapTest003 107 * @tc.desc: Ensure the ability of creating effect chain from config file. 108 * @tc.type: FUNC 109 * @tc.require: 110 * @tc.author: 111 */ 112 HWTEST_F(ColorPickerUnittest, CreateColorPickerFromPixelmapTest003, TestSize.Level1) 113 { 114 GTEST_LOG_(INFO) << "ColorPickerUnittest CreateColorPickerFromPixelmapTest003 start"; 115 /** 116 * @tc.steps: step1. Create a pixelMap 117 */ 118 std::unique_ptr<Media::PixelMap> pixmap = nullptr; 119 120 /** 121 * @tc.steps: step2. Call create From pixelMap 122 */ 123 uint32_t errorCode = SUCCESS; 124 std::shared_ptr<ColorPicker> pColorPicker = ColorPicker::CreateColorPicker(std::move(pixmap), errorCode); 125 ASSERT_EQ(errorCode, ERR_EFFECT_INVALID_VALUE); 126 EXPECT_EQ(pColorPicker, nullptr); 127 } 128 129 /** 130 * @tc.name: GetMainColorTest001 131 * @tc.desc: Ensure the ability of creating effect chain from config file. 132 * @tc.type: FUNC 133 * @tc.require: 134 * @tc.author: 135 */ 136 HWTEST_F(ColorPickerUnittest, GetMainColorTest001, TestSize.Level1) 137 { 138 GTEST_LOG_(INFO) << "ColorPickerUnittest GetMainColorTest001 start"; 139 size_t bufferSize = 0; 140 uint8_t *buffer = GetJpgBuffer(bufferSize); 141 ASSERT_NE(buffer, nullptr); 142 143 /** 144 * @tc.steps: step1. create image source by correct jpeg file path and jpeg format hit. 145 * @tc.expected: step1. create image source success. 146 */ 147 uint32_t errorCode = 0; 148 SourceOptions opts; 149 opts.formatHint = "image/jpeg"; 150 std::unique_ptr<ImageSource> imageSource = 151 ImageSource::CreateImageSource(buffer, bufferSize, opts, errorCode); 152 ASSERT_EQ(errorCode, SUCCESS); 153 ASSERT_NE(imageSource.get(), nullptr); 154 155 /** 156 * @tc.steps: step2. decode image source to pixel map by default decode options 157 * @tc.expected: step2. decode image source to pixel map success. 158 */ 159 DecodeOptions decodeOpts; 160 std::unique_ptr<PixelMap> pixmap = imageSource->CreatePixelMap(decodeOpts, errorCode); 161 HiLog::Debug(LABEL_TEST, "create pixel map error code=%{public}u.", errorCode); 162 ASSERT_EQ(errorCode, SUCCESS); 163 ASSERT_NE(pixmap.get(), nullptr); 164 165 /** 166 * @tc.steps: step2. Call create From pixelMap 167 */ 168 std::shared_ptr<ColorPicker> pColorPicker = ColorPicker::CreateColorPicker(std::move(pixmap), errorCode); 169 ASSERT_EQ(errorCode, SUCCESS); 170 EXPECT_NE(pColorPicker, nullptr); 171 172 /** 173 * @tc.steps: step3. Get main color from pixmap 174 */ 175 ColorManager::Color color; 176 errorCode = pColorPicker->GetMainColor(color); 177 HiLog::Info(LABEL_TEST, "get main color t1[rgba]=%{public}f,%{public}f,%{public}f,%{public}f", 178 color.r, color.g, color.b, color.a); 179 ASSERT_EQ(errorCode, SUCCESS); 180 bool ret = color.ColorEqual(ColorManager::Color(1.f, 0.788235f, 0.050980f, 1.f)); 181 EXPECT_EQ(true, ret); 182 } 183 184 /** 185 * @tc.name: GetMainColorTest002 186 * @tc.desc: Ensure the ability of creating effect chain from config file. 187 * @tc.type: FUNC 188 * @tc.require: 189 * @tc.author: 190 */ 191 HWTEST_F(ColorPickerUnittest, GetMainColorTest002, TestSize.Level1) 192 { 193 GTEST_LOG_(INFO) << "ColorPickerUnittest GetMainColorTest002 start"; 194 size_t bufferSize = 0; 195 uint8_t *buffer = GetPngBuffer(bufferSize); 196 ASSERT_NE(buffer, nullptr); 197 198 /** 199 * @tc.steps: step1. Create a ImageSource 200 */ 201 uint32_t errorCode = 0; 202 SourceOptions opts; 203 opts.formatHint = "image/png"; 204 std::unique_ptr<ImageSource> imageSource = 205 ImageSource::CreateImageSource(buffer, bufferSize, opts, errorCode); 206 ASSERT_EQ(errorCode, SUCCESS); 207 ASSERT_NE(imageSource.get(), nullptr); 208 209 /** 210 * @tc.steps: step2. decode image source to pixel map by default decode options 211 * @tc.expected: step2. decode image source to pixel map success. 212 */ 213 DecodeOptions decodeOpts; 214 std::unique_ptr<PixelMap> pixmap = imageSource->CreatePixelMap(decodeOpts, errorCode); 215 HiLog::Debug(LABEL_TEST, "create pixel map error code=%{public}u.", errorCode); 216 ASSERT_EQ(errorCode, SUCCESS); 217 ASSERT_NE(pixmap.get(), nullptr); 218 219 /** 220 * @tc.steps: step3. Call create From pixelMap 221 */ 222 std::shared_ptr<ColorPicker> pColorPicker = ColorPicker::CreateColorPicker(std::move(pixmap), errorCode); 223 ASSERT_EQ(errorCode, SUCCESS); 224 ASSERT_NE(pColorPicker, nullptr); 225 226 /** 227 * @tc.steps: step4. Get main color from pixmap 228 */ 229 ColorManager::Color color; 230 errorCode = pColorPicker->GetMainColor(color); 231 HiLog::Info(LABEL_TEST, "get main color t2[rgba]=%{public}f,%{public}f,%{public}f,%{public}f", 232 color.r, color.g, color.b, color.a); 233 ASSERT_EQ(errorCode, SUCCESS); 234 bool ret = color.ColorEqual(ColorManager::Color(1.f, 1.f, 1.f, 1.f)); 235 EXPECT_EQ(true, ret); 236 } 237 238 /** 239 * @tc.name: GetMainColorTest003 240 * @tc.desc: Ensure the ability of creating effect chain from config file. 241 * @tc.type: FUNC 242 * @tc.require: 243 * @tc.author: 244 */ 245 HWTEST_F(ColorPickerUnittest, GetMainColorTest003, TestSize.Level1) 246 { 247 GTEST_LOG_(INFO) << "ColorPickerUnittest GetMainColorTest003 start"; 248 /** 249 * @tc.steps: step1. Create a pixelMap 250 */ 251 Media::InitializationOptions opts; 252 opts.size.width = 200; 253 opts.size.height = 100; 254 opts.editable = true; 255 std::unique_ptr<Media::PixelMap> pixmap = Media::PixelMap::Create(opts); 256 257 /** 258 * @tc.steps: step2. Call create From pixelMap 259 */ 260 uint32_t errorCode = SUCCESS; 261 std::shared_ptr<ColorPicker> pColorPicker = ColorPicker::CreateColorPicker(std::move(pixmap), errorCode); 262 ASSERT_EQ(errorCode, SUCCESS); 263 ASSERT_NE(pColorPicker, nullptr); 264 265 /** 266 * @tc.steps: step3. Get main color from pixmap 267 */ 268 ColorManager::Color color; 269 errorCode = pColorPicker->GetMainColor(color); 270 HiLog::Info(LABEL_TEST, "get main color t3[rgba]=%{public}f,%{public}f,%{public}f,%{public}f", 271 color.r, color.g, color.b, color.a); 272 ASSERT_EQ(errorCode, SUCCESS); 273 bool ret = color.ColorEqual(ColorManager::Color(0x00000000U)); 274 EXPECT_EQ(true, ret); 275 } 276 } // namespace Rosen 277 } // namespace OHOS 278