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
CreateColorPicker()37 std::shared_ptr<ColorPicker> ColorPickerUnittest::CreateColorPicker()
38 {
39 size_t bufferSize = 0;
40 uint8_t *buffer = GetJpgBuffer(bufferSize);
41 if (buffer == nullptr) {
42 return nullptr;
43 }
44
45 uint32_t errorCode = 0;
46 SourceOptions opts;
47 opts.formatHint = "image/jpeg";
48 std::unique_ptr<ImageSource> imageSource =
49 ImageSource::CreateImageSource(buffer, bufferSize, opts, errorCode);
50 if ((errorCode != SUCCESS) || (imageSource == nullptr)) {
51 return nullptr;
52 }
53
54 DecodeOptions decodeOpts;
55 std::unique_ptr<PixelMap> pixmap = imageSource->CreatePixelMap(decodeOpts, errorCode);
56 if ((errorCode != SUCCESS) || (pixmap == nullptr)) {
57 return nullptr;
58 }
59
60 return ColorPicker::CreateColorPicker(std::move(pixmap), errorCode);
61 }
62 /**
63 * @tc.name: CreateColorPickerFromPixelmapTest001
64 * @tc.desc: Ensure the ability of creating color picker from pixelmap.
65 * @tc.type: FUNC
66 * @tc.require:
67 * @tc.author:
68 */
69 HWTEST_F(ColorPickerUnittest, CreateColorPickerFromPixelmapTest001, TestSize.Level1)
70 {
71 GTEST_LOG_(INFO) << "ColorPickerUnittest CreateColorPickerFromPixelmapTest001 start";
72 /**
73 * @tc.steps: step1. Create a pixelmap
74 */
75 Media::InitializationOptions opts;
76 opts.size.width = 200;
77 opts.size.height = 150;
78 opts.editable = true;
79 std::unique_ptr<Media::PixelMap> pixmap = Media::PixelMap::Create(opts);
80
81 /**
82 * @tc.steps: step2. Call create From pixelMap
83 */
84 uint32_t errorCode = SUCCESS;
85 std::shared_ptr<ColorPicker> pColorPicker = ColorPicker::CreateColorPicker(std::move(pixmap), errorCode);
86 ASSERT_EQ(errorCode, SUCCESS);
87 EXPECT_NE(pColorPicker, nullptr);
88 }
89
90 /**
91 * @tc.name: CreateColorPickerFromPixelmapTest002
92 * @tc.desc: Ensure the ability of creating color picker from pixelmap.
93 * @tc.type: FUNC
94 * @tc.require:
95 * @tc.author:
96 */
97 HWTEST_F(ColorPickerUnittest, CreateColorPickerFromPixelmapTest002, TestSize.Level1)
98 {
99 GTEST_LOG_(INFO) << "ColorPickerUnittest CreateColorPickerFromPixelmapTest002 start";
100 size_t bufferSize = 0;
101 uint8_t *buffer = GetPngBuffer(bufferSize);
102 ASSERT_NE(buffer, nullptr);
103
104 /**
105 * @tc.steps: step1. Create a ImageSource
106 */
107 uint32_t errorCode = 0;
108 SourceOptions opts;
109 opts.formatHint = "image/png";
110 std::unique_ptr<ImageSource> imageSource =
111 ImageSource::CreateImageSource(buffer, bufferSize, opts, errorCode);
112 ASSERT_EQ(errorCode, SUCCESS);
113
114 /**
115 * @tc.steps: step2. decode image source to pixel map by default decode options
116 * @tc.expected: step2. decode image source to pixel map success.
117 */
118 DecodeOptions decodeOpts;
119 std::unique_ptr<PixelMap> pixmap = imageSource->CreatePixelMap(decodeOpts, errorCode);
120 HiLog::Debug(LABEL_TEST, "create pixel map error code=%{public}u.", errorCode);
121 ASSERT_EQ(errorCode, SUCCESS);
122 ASSERT_NE(pixmap.get(), nullptr);
123
124 /**
125 * @tc.steps: step3. Call create From pixelMap
126 */
127 std::shared_ptr<ColorPicker> pColorPicker = ColorPicker::CreateColorPicker(std::move(pixmap), errorCode);
128 EXPECT_NE(pColorPicker, nullptr);
129 }
130
131 /**
132 * @tc.name: CreateColorPickerFromPixelmapTest003
133 * @tc.desc: Ensure the ability of creating effect chain from config file.
134 * @tc.type: FUNC
135 * @tc.require:
136 * @tc.author:
137 */
138 HWTEST_F(ColorPickerUnittest, CreateColorPickerFromPixelmapTest003, TestSize.Level1)
139 {
140 GTEST_LOG_(INFO) << "ColorPickerUnittest CreateColorPickerFromPixelmapTest003 start";
141 /**
142 * @tc.steps: step1. Create a pixelMap
143 */
144 std::unique_ptr<Media::PixelMap> pixmap = nullptr;
145
146 /**
147 * @tc.steps: step2. Call create From pixelMap
148 */
149 uint32_t errorCode = SUCCESS;
150 std::shared_ptr<ColorPicker> pColorPicker = ColorPicker::CreateColorPicker(std::move(pixmap), errorCode);
151 ASSERT_EQ(errorCode, ERR_EFFECT_INVALID_VALUE);
152 EXPECT_EQ(pColorPicker, nullptr);
153 }
154
155 /**
156 * @tc.name: CreateColorPickerFromPixelmapTest004
157 * @tc.desc: Ensure the ability of creating color picker from pixelmap.
158 * @tc.type: FUNC
159 * @tc.require:
160 * @tc.author:
161 */
162 HWTEST_F(ColorPickerUnittest, CreateColorPickerFromPixelmapTest004, TestSize.Level1)
163 {
164 GTEST_LOG_(INFO) << "ColorPickerUnittest CreateColorPickerFromPixelmapTest004 start";
165 /**
166 * @tc.steps: step1. Create a pixelmap
167 */
168 Media::InitializationOptions opts;
169 opts.size.width = 200;
170 opts.size.height = 150;
171 opts.editable = true;
172 std::unique_ptr<Media::PixelMap> pixmap = Media::PixelMap::Create(opts);
173
174 /**
175 * @tc.steps: step2. Call create From pixelMap
176 */
177 uint32_t errorCode = SUCCESS;
178 double region[4] = {0, 0, 0.5, 0.5};
179 std::shared_ptr<ColorPicker> pColorPicker = ColorPicker::CreateColorPicker(std::move(pixmap), region, errorCode);
180 ASSERT_EQ(errorCode, SUCCESS);
181 EXPECT_NE(pColorPicker, nullptr);
182 }
183
184 /**
185 * @tc.name: CreateColorPickerFromPixelmapTest005
186 * @tc.desc: Ensure the ability of creating color picker from pixelmap.
187 * @tc.type: FUNC
188 * @tc.require:
189 * @tc.author:
190 */
191 HWTEST_F(ColorPickerUnittest, CreateColorPickerFromPixelmapTest005, TestSize.Level1)
192 {
193 GTEST_LOG_(INFO) << "ColorPickerUnittest CreateColorPickerFromPixelmapTest005 start";
194 /**
195 * @tc.steps: step1. Create a pixelmap
196 */
197 Media::InitializationOptions opts;
198 opts.size.width = 200;
199 opts.size.height = 150;
200 opts.editable = true;
201 std::unique_ptr<Media::PixelMap> pixmap = Media::PixelMap::Create(opts);
202
203 /**
204 * @tc.steps: step2. Call create From pixelMap
205 */
206 uint32_t errorCode = SUCCESS;
207 double region[4] = {0, 0.5, 0.5, 0.5};
208 std::shared_ptr<ColorPicker> pColorPicker = ColorPicker::CreateColorPicker(std::move(pixmap), region, errorCode);
209 ASSERT_EQ(pColorPicker->colorValLen_, 0);
210 EXPECT_NE(pColorPicker, nullptr);
211 }
212
213 /**
214 * @tc.name: GetMainColorTest001
215 * @tc.desc: Ensure the ability of creating effect chain from config file.
216 * @tc.type: FUNC
217 * @tc.require:
218 * @tc.author:
219 */
220 HWTEST_F(ColorPickerUnittest, GetMainColorTest001, TestSize.Level1)
221 {
222 GTEST_LOG_(INFO) << "ColorPickerUnittest GetMainColorTest001 start";
223 size_t bufferSize = 0;
224 uint8_t *buffer = GetJpgBuffer(bufferSize);
225 ASSERT_NE(buffer, nullptr);
226
227 /**
228 * @tc.steps: step1. create image source by correct jpeg file path and jpeg format hit.
229 * @tc.expected: step1. create image source success.
230 */
231 uint32_t errorCode = 0;
232 SourceOptions opts;
233 opts.formatHint = "image/jpeg";
234 std::unique_ptr<ImageSource> imageSource =
235 ImageSource::CreateImageSource(buffer, bufferSize, opts, errorCode);
236 ASSERT_EQ(errorCode, SUCCESS);
237 ASSERT_NE(imageSource.get(), nullptr);
238
239 /**
240 * @tc.steps: step2. decode image source to pixel map by default decode options
241 * @tc.expected: step2. decode image source to pixel map success.
242 */
243 DecodeOptions decodeOpts;
244 std::unique_ptr<PixelMap> pixmap = imageSource->CreatePixelMap(decodeOpts, errorCode);
245 HiLog::Debug(LABEL_TEST, "create pixel map error code=%{public}u.", errorCode);
246 ASSERT_EQ(errorCode, SUCCESS);
247 ASSERT_NE(pixmap.get(), nullptr);
248
249 /**
250 * @tc.steps: step2. Call create From pixelMap
251 */
252 std::shared_ptr<ColorPicker> pColorPicker = ColorPicker::CreateColorPicker(std::move(pixmap), errorCode);
253 ASSERT_EQ(errorCode, SUCCESS);
254 EXPECT_NE(pColorPicker, nullptr);
255
256 /**
257 * @tc.steps: step3. Get main color from pixmap
258 */
259 ColorManager::Color color;
260 errorCode = pColorPicker->GetMainColor(color);
261 HiLog::Info(LABEL_TEST, "get main color t1[rgba]=%{public}f,%{public}f,%{public}f,%{public}f",
262 color.r, color.g, color.b, color.a);
263 ASSERT_EQ(errorCode, SUCCESS);
264 bool ret = color.ColorEqual(ColorManager::Color(1.f, 0.788235f, 0.050980f, 1.f));
265 EXPECT_EQ(true, ret);
266 }
267
268 /**
269 * @tc.name: GetMainColorTest002
270 * @tc.desc: Ensure the ability of creating effect chain from config file.
271 * @tc.type: FUNC
272 * @tc.require:
273 * @tc.author:
274 */
275 HWTEST_F(ColorPickerUnittest, GetMainColorTest002, TestSize.Level1)
276 {
277 GTEST_LOG_(INFO) << "ColorPickerUnittest GetMainColorTest002 start";
278 size_t bufferSize = 0;
279 uint8_t *buffer = GetPngBuffer(bufferSize);
280 ASSERT_NE(buffer, nullptr);
281
282 /**
283 * @tc.steps: step1. Create a ImageSource
284 */
285 uint32_t errorCode = 0;
286 SourceOptions opts;
287 opts.formatHint = "image/png";
288 std::unique_ptr<ImageSource> imageSource =
289 ImageSource::CreateImageSource(buffer, bufferSize, opts, errorCode);
290 ASSERT_EQ(errorCode, SUCCESS);
291 ASSERT_NE(imageSource.get(), nullptr);
292
293 /**
294 * @tc.steps: step2. decode image source to pixel map by default decode options
295 * @tc.expected: step2. decode image source to pixel map success.
296 */
297 DecodeOptions decodeOpts;
298 std::unique_ptr<PixelMap> pixmap = imageSource->CreatePixelMap(decodeOpts, errorCode);
299 HiLog::Debug(LABEL_TEST, "create pixel map error code=%{public}u.", errorCode);
300 ASSERT_EQ(errorCode, SUCCESS);
301 ASSERT_NE(pixmap.get(), nullptr);
302
303 /**
304 * @tc.steps: step3. Call create From pixelMap
305 */
306 std::shared_ptr<ColorPicker> pColorPicker = ColorPicker::CreateColorPicker(std::move(pixmap), errorCode);
307 ASSERT_EQ(errorCode, SUCCESS);
308 ASSERT_NE(pColorPicker, nullptr);
309
310 /**
311 * @tc.steps: step4. Get main color from pixmap
312 */
313 ColorManager::Color color;
314 errorCode = pColorPicker->GetMainColor(color);
315 HiLog::Info(LABEL_TEST, "get main color t2[rgba]=%{public}f,%{public}f,%{public}f,%{public}f",
316 color.r, color.g, color.b, color.a);
317 ASSERT_EQ(errorCode, SUCCESS);
318 bool ret = color.ColorEqual(ColorManager::Color(1.f, 1.f, 1.f, 1.f));
319 EXPECT_EQ(true, ret);
320 }
321
322 /**
323 * @tc.name: GetMainColorTest003
324 * @tc.desc: Ensure the ability of creating effect chain from config file.
325 * @tc.type: FUNC
326 * @tc.require:
327 * @tc.author:
328 */
329 HWTEST_F(ColorPickerUnittest, GetMainColorTest003, TestSize.Level1)
330 {
331 GTEST_LOG_(INFO) << "ColorPickerUnittest GetMainColorTest003 start";
332 /**
333 * @tc.steps: step1. Create a pixelMap
334 */
335 Media::InitializationOptions opts;
336 opts.size.width = 200;
337 opts.size.height = 100;
338 opts.editable = true;
339 std::unique_ptr<Media::PixelMap> pixmap = Media::PixelMap::Create(opts);
340
341 /**
342 * @tc.steps: step2. Call create From pixelMap
343 */
344 uint32_t errorCode = SUCCESS;
345 std::shared_ptr<ColorPicker> pColorPicker = ColorPicker::CreateColorPicker(std::move(pixmap), errorCode);
346 ASSERT_EQ(errorCode, SUCCESS);
347 ASSERT_NE(pColorPicker, nullptr);
348
349 /**
350 * @tc.steps: step3. Get main color from pixmap
351 */
352 ColorManager::Color color;
353 errorCode = pColorPicker->GetMainColor(color);
354 HiLog::Info(LABEL_TEST, "get main color t3[rgba]=%{public}f,%{public}f,%{public}f,%{public}f",
355 color.r, color.g, color.b, color.a);
356 ASSERT_EQ(errorCode, SUCCESS);
357 bool ret = color.ColorEqual(ColorManager::Color(0x00000000U));
358 EXPECT_EQ(true, ret);
359 }
360
361 /**
362 * @tc.name: GetLargestProportionColor
363 * @tc.desc: Ensure the ability of creating effect chain from config file.
364 * @tc.type: FUNC
365 * @tc.require:
366 * @tc.author:
367 */
368 HWTEST_F(ColorPickerUnittest, GetLargestProportionColor, TestSize.Level1)
369 {
370 GTEST_LOG_(INFO) << "ColorPickerUnittest GetLargestProportionColor start";
371 size_t bufferSize = 0;
372 uint8_t *buffer = GetJpgBuffer(bufferSize);
373 ASSERT_NE(buffer, nullptr);
374
375 uint32_t errorCode = 0;
376 SourceOptions opts;
377 opts.formatHint = "image/jpeg";
378 std::unique_ptr<ImageSource> imageSource =
379 ImageSource::CreateImageSource(buffer, bufferSize, opts, errorCode);
380 ASSERT_EQ(errorCode, SUCCESS);
381 ASSERT_NE(imageSource.get(), nullptr);
382
383 DecodeOptions decodeOpts;
384 std::unique_ptr<PixelMap> pixmap = imageSource->CreatePixelMap(decodeOpts, errorCode);
385 HiLog::Debug(LABEL_TEST, "create pixel map error code=%{public}u.", errorCode);
386 ASSERT_EQ(errorCode, SUCCESS);
387 ASSERT_NE(pixmap.get(), nullptr);
388
389 std::shared_ptr<ColorPicker> pColorPicker = ColorPicker::CreateColorPicker(std::move(pixmap), errorCode);
390 ASSERT_EQ(errorCode, SUCCESS);
391 EXPECT_NE(pColorPicker, nullptr);
392
393 ColorManager::Color color;
394 errorCode = pColorPicker->GetLargestProportionColor(color);
395 HiLog::Info(LABEL_TEST, "get largest proportion color [rgba]=%{public}f,%{public}f,%{public}f,%{public}f",
396 color.r, color.g, color.b, color.a);
397 ASSERT_EQ(errorCode, SUCCESS);
398 bool ret = color.ColorEqual(ColorManager::Color(0.972549f, 0.784314f, 0.0313726f, 1.f));
399 EXPECT_EQ(true, ret);
400 }
401
402 /**
403 * @tc.name: GetHighestSaturationColor
404 * @tc.desc: Ensure the ability of creating effect chain from config file.
405 * @tc.type: FUNC
406 * @tc.require:
407 * @tc.author:
408 */
409 HWTEST_F(ColorPickerUnittest, GetHighestSaturationColor, TestSize.Level1)
410 {
411 GTEST_LOG_(INFO) << "ColorPickerUnittest GetHighestSaturationColor start";
412 size_t bufferSize = 0;
413 uint8_t *buffer = GetJpgBuffer(bufferSize);
414 ASSERT_NE(buffer, nullptr);
415
416 uint32_t errorCode = 0;
417 SourceOptions opts;
418 opts.formatHint = "image/jpeg";
419 std::unique_ptr<ImageSource> imageSource =
420 ImageSource::CreateImageSource(buffer, bufferSize, opts, errorCode);
421 ASSERT_EQ(errorCode, SUCCESS);
422 ASSERT_NE(imageSource.get(), nullptr);
423
424 DecodeOptions decodeOpts;
425 std::unique_ptr<PixelMap> pixmap = imageSource->CreatePixelMap(decodeOpts, errorCode);
426 HiLog::Debug(LABEL_TEST, "create pixel map error code=%{public}u.", errorCode);
427 ASSERT_EQ(errorCode, SUCCESS);
428 ASSERT_NE(pixmap.get(), nullptr);
429
430 std::shared_ptr<ColorPicker> pColorPicker = ColorPicker::CreateColorPicker(std::move(pixmap), errorCode);
431 ASSERT_EQ(errorCode, SUCCESS);
432 EXPECT_NE(pColorPicker, nullptr);
433
434 ColorManager::Color color;
435 errorCode = pColorPicker->GetHighestSaturationColor(color);
436 HiLog::Info(LABEL_TEST, "get highest saturation color [rgba]=%{public}f,%{public}f,%{public}f,%{public}f",
437 color.r, color.g, color.b, color.a);
438 ASSERT_EQ(errorCode, SUCCESS);
439 bool ret = color.ColorEqual(ColorManager::Color(0.972549f, 0.784314f, 0.0313726f, 1.f));
440 EXPECT_EQ(true, ret);
441 }
442
443 /**
444 * @tc.name: GetAverageColor
445 * @tc.desc: Ensure the ability of creating effect chain from config file.
446 * @tc.type: FUNC
447 * @tc.require:
448 * @tc.author:
449 */
450 HWTEST_F(ColorPickerUnittest, GetAverageColor, TestSize.Level1)
451 {
452 GTEST_LOG_(INFO) << "ColorPickerUnittest GetAverageColor start";
453 size_t bufferSize = 0;
454 uint8_t *buffer = GetJpgBuffer(bufferSize);
455 ASSERT_NE(buffer, nullptr);
456
457 uint32_t errorCode = 0;
458 SourceOptions opts;
459 opts.formatHint = "image/jpeg";
460 std::unique_ptr<ImageSource> imageSource =
461 ImageSource::CreateImageSource(buffer, bufferSize, opts, errorCode);
462 ASSERT_EQ(errorCode, SUCCESS);
463 ASSERT_NE(imageSource.get(), nullptr);
464
465 DecodeOptions decodeOpts;
466 std::unique_ptr<PixelMap> pixmap = imageSource->CreatePixelMap(decodeOpts, errorCode);
467 HiLog::Debug(LABEL_TEST, "create pixel map error code=%{public}u.", errorCode);
468 ASSERT_EQ(errorCode, SUCCESS);
469 ASSERT_NE(pixmap.get(), nullptr);
470
471 std::shared_ptr<ColorPicker> pColorPicker = ColorPicker::CreateColorPicker(std::move(pixmap), errorCode);
472 ASSERT_EQ(errorCode, SUCCESS);
473 EXPECT_NE(pColorPicker, nullptr);
474
475 ColorManager::Color color;
476 errorCode = pColorPicker->GetAverageColor(color);
477 HiLog::Info(LABEL_TEST, "get average color [rgba]=%{public}f,%{public}f,%{public}f,%{public}f",
478 color.r, color.g, color.b, color.a);
479 ASSERT_EQ(errorCode, SUCCESS);
480 bool ret = color.ColorEqual(ColorManager::Color(0.972549f, 0.784314f, 0.0313726f, 1.f));
481 EXPECT_EQ(true, ret);
482 }
483
484 /**
485 * @tc.name: GetAverageColor001
486 * @tc.desc: GetAverageColor is ERR_EFFECT_INVALID_VALUE
487 * @tc.type: FUNC
488 * @tc.require:
489 * @tc.author:
490 */
491 HWTEST_F(ColorPickerUnittest, GetAverageColor001, TestSize.Level1)
492 {
493 size_t bufferSize = 0;
494 uint8_t* buffer = GetJpgBuffer(bufferSize);
495 ASSERT_NE(buffer, nullptr);
496
497 uint32_t errorCode = 0;
498 SourceOptions opts;
499 opts.formatHint = "image/jpeg";
500 std::unique_ptr<ImageSource> imageSource = ImageSource::CreateImageSource(buffer, bufferSize, opts, errorCode);
501 ASSERT_EQ(errorCode, SUCCESS);
502 ASSERT_NE(imageSource.get(), nullptr);
503
504 DecodeOptions decodeOpts;
505 std::unique_ptr<PixelMap> pixmap = imageSource->CreatePixelMap(decodeOpts, errorCode);
506 ASSERT_NE(pixmap.get(), nullptr);
507
508 std::shared_ptr<ColorPicker> pColorPicker = ColorPicker::CreateColorPicker(std::move(pixmap), errorCode);
509 ASSERT_EQ(errorCode, SUCCESS);
510 EXPECT_NE(pColorPicker, nullptr);
511
512 ColorManager::Color color;
513 std::vector<std::pair<uint32_t, uint32_t>> featureColors;
514 featureColors.emplace_back(std::pair<uint32_t, uint32_t>(1, 0));
515 pColorPicker->featureColors_ = std::move(featureColors);
516 errorCode = pColorPicker->GetAverageColor(color);
517 ASSERT_EQ(errorCode, ERR_EFFECT_INVALID_VALUE);
518 }
519
520 /**
521 * @tc.name: CalcGrayVariance001
522 * @tc.desc: CalcGrayVariance is ERR_EFFECT_INVALID_VALUE
523 * @tc.type: FUNC
524 * @tc.require:
525 * @tc.author:
526 */
527 HWTEST_F(ColorPickerUnittest, CalcGrayVariance001, TestSize.Level1)
528 {
529 size_t bufferSize = 0;
530 uint8_t* buffer = GetJpgBuffer(bufferSize);
531 ASSERT_NE(buffer, nullptr);
532
533 uint32_t errorCode = 0;
534 SourceOptions opts;
535 opts.formatHint = "image/jpeg";
536 std::unique_ptr<ImageSource> imageSource = ImageSource::CreateImageSource(buffer, bufferSize, opts, errorCode);
537 ASSERT_EQ(errorCode, SUCCESS);
538 ASSERT_NE(imageSource.get(), nullptr);
539
540 DecodeOptions decodeOpts;
541 std::unique_ptr<PixelMap> pixmap = imageSource->CreatePixelMap(decodeOpts, errorCode);
542 HiLog::Debug(LABEL_TEST, "create pixel map error code=%{public}u.", errorCode);
543 ASSERT_EQ(errorCode, SUCCESS);
544 ASSERT_NE(pixmap.get(), nullptr);
545
546 std::shared_ptr<ColorPicker> pColorPicker = ColorPicker::CreateColorPicker(std::move(pixmap), errorCode);
547 ASSERT_EQ(errorCode, SUCCESS);
548 EXPECT_NE(pColorPicker, nullptr);
549
550 ColorManager::Color color;
551 errorCode = pColorPicker->GetAverageColor(color);
552 EXPECT_EQ(errorCode, SUCCESS);
553
554 uint32_t ret = pColorPicker->CalcGrayVariance();
555 EXPECT_NE(ret, ERR_EFFECT_INVALID_VALUE);
556 }
557
558 /**
559 * @tc.name: CalcContrastRatioWithWhite001
560 * @tc.desc: lightColorDegree is 0
561 * @tc.type: FUNC
562 * @tc.require:
563 * @tc.author:
564 */
565 HWTEST_F(ColorPickerUnittest, CalcContrastRatioWithWhite001, TestSize.Level1)
566 {
567 size_t bufferSize = 0;
568 uint8_t* buffer = GetJpgBuffer(bufferSize);
569 ASSERT_NE(buffer, nullptr);
570
571 uint32_t errorCode = 0;
572 SourceOptions opts;
573 opts.formatHint = "image/jpeg";
574 std::unique_ptr<ImageSource> imageSource = ImageSource::CreateImageSource(buffer, bufferSize, opts, errorCode);
575 ASSERT_EQ(errorCode, SUCCESS);
576 ASSERT_NE(imageSource.get(), nullptr);
577
578 DecodeOptions decodeOpts;
579 std::unique_ptr<PixelMap> pixmap = imageSource->CreatePixelMap(decodeOpts, errorCode);
580 HiLog::Debug(LABEL_TEST, "create pixel map error code=%{public}u.", errorCode);
581 ASSERT_EQ(errorCode, SUCCESS);
582 ASSERT_NE(pixmap.get(), nullptr);
583
584 std::shared_ptr<ColorPicker> pColorPicker = ColorPicker::CreateColorPicker(std::move(pixmap), errorCode);
585 ASSERT_EQ(errorCode, SUCCESS);
586 EXPECT_NE(pColorPicker, nullptr);
587
588 std::vector<std::pair<uint32_t, uint32_t>> featureColors;
589 featureColors.emplace_back(std::pair<uint32_t, uint32_t>(1, 0));
590 pColorPicker->featureColors_ = std::move(featureColors);
591 pColorPicker->colorValLen_ = 1;
592 int ret = pColorPicker->CalcContrastRatioWithWhite();
593 EXPECT_EQ(ret, 0);
594 }
595
596 /**
597 * @tc.name: DiscriminatePitureLightDegreee001
598 * @tc.desc: DiscriminatePitureLightDegree degree is DARK_COLOR_PICTURE
599 * @tc.type: FUNC
600 * @tc.require:
601 * @tc.author:
602 */
603 HWTEST_F(ColorPickerUnittest, DiscriminatePitureLightDegreee001, TestSize.Level1)
604 {
605 size_t bufferSize = 0;
606 uint8_t* buffer = GetJpgBuffer(bufferSize);
607 ASSERT_NE(buffer, nullptr);
608
609 uint32_t errorCode = 0;
610 SourceOptions opts;
611 opts.formatHint = "image/jpeg";
612 std::unique_ptr<ImageSource> imageSource = ImageSource::CreateImageSource(buffer, bufferSize, opts, errorCode);
613 ASSERT_EQ(errorCode, SUCCESS);
614 ASSERT_NE(imageSource.get(), nullptr);
615
616 DecodeOptions decodeOpts;
617 std::unique_ptr<PixelMap> pixmap = imageSource->CreatePixelMap(decodeOpts, errorCode);
618 HiLog::Debug(LABEL_TEST, "create pixel map error code=%{public}u.", errorCode);
619 ASSERT_EQ(errorCode, SUCCESS);
620 ASSERT_NE(pixmap.get(), nullptr);
621
622 std::shared_ptr<ColorPicker> pColorPicker = ColorPicker::CreateColorPicker(std::move(pixmap), errorCode);
623 ASSERT_EQ(errorCode, SUCCESS);
624 EXPECT_NE(pColorPicker, nullptr);
625
626 pColorPicker->grayMsd_ = 5000;
627 pColorPicker->contrastToWhite_ = 9;
628 PictureLightColorDegree degree = EXTREMELY_LIGHT_COLOR_PICTURE;
629 pColorPicker->DiscriminatePitureLightDegree(degree);
630 EXPECT_EQ(degree, DARK_COLOR_PICTURE);
631 }
632
633 /**
634 * @tc.name: DiscriminatePitureLightDegreee002
635 * @tc.desc: DiscriminatePitureLightDegree degree is ERR_EFFECT_INVALID_VALUE
636 * @tc.type: FUNC
637 * @tc.require:
638 * @tc.author:
639 */
640 HWTEST_F(ColorPickerUnittest, DiscriminatePitureLightDegreee002, TestSize.Level1)
641 {
642 size_t bufferSize = 0;
643 uint8_t* buffer = GetJpgBuffer(bufferSize);
644 ASSERT_NE(buffer, nullptr);
645
646 uint32_t errorCode = 0;
647 SourceOptions opts;
648 opts.formatHint = "image/jpeg";
649 std::unique_ptr<ImageSource> imageSource = ImageSource::CreateImageSource(buffer, bufferSize, opts, errorCode);
650 ASSERT_EQ(errorCode, SUCCESS);
651 ASSERT_NE(imageSource.get(), nullptr);
652
653 DecodeOptions decodeOpts;
654 std::unique_ptr<PixelMap> pixmap = imageSource->CreatePixelMap(decodeOpts, errorCode);
655 HiLog::Debug(LABEL_TEST, "create pixel map error code=%{public}u.", errorCode);
656 ASSERT_EQ(errorCode, SUCCESS);
657 ASSERT_NE(pixmap.get(), nullptr);
658
659 std::shared_ptr<ColorPicker> pColorPicker = ColorPicker::CreateColorPicker(std::move(pixmap), errorCode);
660 ASSERT_EQ(errorCode, SUCCESS);
661 EXPECT_NE(pColorPicker, nullptr);
662
663 pColorPicker->featureColors_.clear();
664 pColorPicker->grayMsd_ = 5000;
665 pColorPicker->contrastToWhite_ = 9;
666 PictureLightColorDegree degree = EXTREMELY_LIGHT_COLOR_PICTURE;
667 uint32_t ret = pColorPicker->DiscriminatePitureLightDegree(degree);
668 EXPECT_EQ(ret, ERR_EFFECT_INVALID_VALUE);
669 }
670
671 /**
672 * @tc.name: GetReverseColor001
673 * @tc.desc: GetReverseColor is ERR_EFFECT_INVALID_VALUE
674 * @tc.type: FUNC
675 * @tc.require:
676 * @tc.author:
677 */
678 HWTEST_F(ColorPickerUnittest, GetReverseColor001, TestSize.Level1)
679 {
680 size_t bufferSize = 0;
681 uint8_t* buffer = GetJpgBuffer(bufferSize);
682 ASSERT_NE(buffer, nullptr);
683
684 uint32_t errorCode = 0;
685 SourceOptions opts;
686 opts.formatHint = "image/jpeg";
687 std::unique_ptr<ImageSource> imageSource = ImageSource::CreateImageSource(buffer, bufferSize, opts, errorCode);
688 ASSERT_EQ(errorCode, SUCCESS);
689 ASSERT_NE(imageSource.get(), nullptr);
690
691 DecodeOptions decodeOpts;
692 std::unique_ptr<PixelMap> pixmap = imageSource->CreatePixelMap(decodeOpts, errorCode);
693 HiLog::Debug(LABEL_TEST, "create pixel map error code=%{public}u.", errorCode);
694 ASSERT_EQ(errorCode, SUCCESS);
695 ASSERT_NE(pixmap.get(), nullptr);
696
697 std::shared_ptr<ColorPicker> pColorPicker = ColorPicker::CreateColorPicker(std::move(pixmap), errorCode);
698 ASSERT_EQ(errorCode, SUCCESS);
699 EXPECT_NE(pColorPicker, nullptr);
700
701 pColorPicker->featureColors_.clear();
702 ColorManager::Color color;
703 uint32_t ret = pColorPicker->GetReverseColor(color);
704 EXPECT_EQ(ret, ERR_EFFECT_INVALID_VALUE);
705 }
706
707 /**
708 * @tc.name: GetMorandiShadowColor001
709 * @tc.desc: GetMorandiBackgroundColor is ERR_EFFECT_INVALID_VALUE.
710 * @tc.type: FUNC
711 * @tc.require:
712 * @tc.author:
713 */
714 HWTEST_F(ColorPickerUnittest, GetMorandiShadowColor001, TestSize.Level1)
715 {
716 size_t bufferSize = 0;
717 uint8_t* buffer = GetJpgBuffer(bufferSize);
718 ASSERT_NE(buffer, nullptr);
719
720 uint32_t errorCode = 0;
721 SourceOptions opts;
722 opts.formatHint = "image/jpeg";
723 std::unique_ptr<ImageSource> imageSource = ImageSource::CreateImageSource(buffer, bufferSize, opts, errorCode);
724 ASSERT_EQ(errorCode, SUCCESS);
725 ASSERT_NE(imageSource.get(), nullptr);
726
727 DecodeOptions decodeOpts;
728 std::unique_ptr<PixelMap> pixmap = imageSource->CreatePixelMap(decodeOpts, errorCode);
729 HiLog::Debug(LABEL_TEST, "create pixel map error code=%{public}u.", errorCode);
730 ASSERT_EQ(errorCode, SUCCESS);
731 ASSERT_NE(pixmap.get(), nullptr);
732
733 std::shared_ptr<ColorPicker> pColorPicker = ColorPicker::CreateColorPicker(std::move(pixmap), errorCode);
734 ASSERT_EQ(errorCode, SUCCESS);
735 EXPECT_NE(pColorPicker, nullptr);
736
737 std::vector<std::pair<uint32_t, uint32_t>> featureColors;
738 featureColors.emplace_back(std::pair<uint32_t, uint32_t>(0, 1));
739 pColorPicker->featureColors_ = std::move(featureColors);
740 for (int r = 0; r <= 255; ++r) {
741 for (int g = 0; g <= 255; ++g) {
742 for (int b = 0; b <= 255; ++b) {
743 ColorManager::Color cl(r, g, b, 0.f);
744 pColorPicker->GetMorandiBackgroundColor(cl);
745 }
746 }
747 }
748
749 for (int r = 0; r <= 10; ++r) {
750 for (int g = 0; g <= 10; ++g) {
751 for (int b = 0; b <= 10; ++b) {
752 ColorManager::Color cl(r / 10, g / 10, b / 10, 1);
753 pColorPicker->GetMorandiBackgroundColor(cl);
754 }
755 }
756 }
757
758 pColorPicker->featureColors_.clear();
759 ColorManager::Color cl;
760 uint32_t ret = pColorPicker->GetMorandiBackgroundColor(cl);
761 EXPECT_EQ(ret, ERR_EFFECT_INVALID_VALUE);
762 }
763
764 /**
765 * @tc.name: GenerateMorandiShadowColor002
766 * @tc.desc: hsv.h > 60 && hsv.h <= 190
767 * @tc.type: FUNC
768 * @tc.require:
769 * @tc.author:
770 */
771 HWTEST_F(ColorPickerUnittest, GenerateMorandiShadowColor002, TestSize.Level1)
772 {
773 size_t bufferSize = 0;
774 uint8_t* buffer = GetJpgBuffer(bufferSize);
775 ASSERT_NE(buffer, nullptr);
776
777 uint32_t errorCode = 0;
778 SourceOptions opts;
779 opts.formatHint = "image/jpeg";
780 std::unique_ptr<ImageSource> imageSource = ImageSource::CreateImageSource(buffer, bufferSize, opts, errorCode);
781 ASSERT_EQ(errorCode, SUCCESS);
782 ASSERT_NE(imageSource.get(), nullptr);
783
784 DecodeOptions decodeOpts;
785 std::unique_ptr<PixelMap> pixmap = imageSource->CreatePixelMap(decodeOpts, errorCode);
786 HiLog::Debug(LABEL_TEST, "create pixel map error code=%{public}u.", errorCode);
787 ASSERT_EQ(errorCode, SUCCESS);
788 ASSERT_NE(pixmap.get(), nullptr);
789
790 std::shared_ptr<ColorPicker> pColorPicker = ColorPicker::CreateColorPicker(std::move(pixmap), errorCode);
791 ASSERT_EQ(errorCode, SUCCESS);
792 EXPECT_NE(pColorPicker, nullptr);
793
794 HSV hsv2 = { 100, 0, 0 };
795 pColorPicker->GenerateMorandiShadowColor(hsv2);
796 }
797
798 /**
799 * @tc.name: GenerateMorandiShadowColor003
800 * @tc.desc: hsv.h > 190 && hsv.h <= 270
801 * @tc.type: FUNC
802 * @tc.require:
803 * @tc.author:
804 */
805 HWTEST_F(ColorPickerUnittest, GenerateMorandiShadowColor003, TestSize.Level1)
806 {
807 size_t bufferSize = 0;
808 uint8_t* buffer = GetJpgBuffer(bufferSize);
809 ASSERT_NE(buffer, nullptr);
810
811 uint32_t errorCode = 0;
812 SourceOptions opts;
813 opts.formatHint = "image/jpeg";
814 std::unique_ptr<ImageSource> imageSource = ImageSource::CreateImageSource(buffer, bufferSize, opts, errorCode);
815 ASSERT_EQ(errorCode, SUCCESS);
816 ASSERT_NE(imageSource.get(), nullptr);
817
818 DecodeOptions decodeOpts;
819 std::unique_ptr<PixelMap> pixmap = imageSource->CreatePixelMap(decodeOpts, errorCode);
820 HiLog::Debug(LABEL_TEST, "create pixel map error code=%{public}u.", errorCode);
821 ASSERT_EQ(errorCode, SUCCESS);
822 ASSERT_NE(pixmap.get(), nullptr);
823
824 std::shared_ptr<ColorPicker> pColorPicker = ColorPicker::CreateColorPicker(std::move(pixmap), errorCode);
825 ASSERT_EQ(errorCode, SUCCESS);
826 EXPECT_NE(pColorPicker, nullptr);
827
828 HSV hsv2 = { 200, 0, 0 };
829 pColorPicker->GenerateMorandiShadowColor(hsv2);
830 }
831
832 /**
833 * @tc.name: GenerateMorandiShadowColor004
834 * @tc.desc: else
835 * @tc.type: FUNC
836 * @tc.require:
837 * @tc.author:
838 */
839 HWTEST_F(ColorPickerUnittest, GenerateMorandiShadowColor004, TestSize.Level1)
840 {
841 size_t bufferSize = 0;
842 uint8_t* buffer = GetJpgBuffer(bufferSize);
843 ASSERT_NE(buffer, nullptr);
844
845 uint32_t errorCode = 0;
846 SourceOptions opts;
847 opts.formatHint = "image/jpeg";
848 std::unique_ptr<ImageSource> imageSource = ImageSource::CreateImageSource(buffer, bufferSize, opts, errorCode);
849 ASSERT_EQ(errorCode, SUCCESS);
850 ASSERT_NE(imageSource.get(), nullptr);
851
852 DecodeOptions decodeOpts;
853 std::unique_ptr<PixelMap> pixmap = imageSource->CreatePixelMap(decodeOpts, errorCode);
854 HiLog::Debug(LABEL_TEST, "create pixel map error code=%{public}u.", errorCode);
855 ASSERT_EQ(errorCode, SUCCESS);
856 ASSERT_NE(pixmap.get(), nullptr);
857
858 std::shared_ptr<ColorPicker> pColorPicker = ColorPicker::CreateColorPicker(std::move(pixmap), errorCode);
859 ASSERT_EQ(errorCode, SUCCESS);
860 EXPECT_NE(pColorPicker, nullptr);
861
862 HSV hsv2 = { 300, 0, 0 };
863 pColorPicker->GenerateMorandiShadowColor(hsv2);
864 }
865
866 /**
867 * @tc.name: DiscriminateDarkOrBrightColor001
868 * @tc.desc: DiscriminateDarkOrBrightColor mode is LOW_SATURATION_BRIGHT_COLOR
869 * @tc.type: FUNC
870 * @tc.require:
871 * @tc.author:
872 */
873 HWTEST_F(ColorPickerUnittest, DiscriminateDarkOrBrightColor001, TestSize.Level1)
874 {
875 size_t bufferSize = 0;
876 uint8_t* buffer = GetJpgBuffer(bufferSize);
877 ASSERT_NE(buffer, nullptr);
878
879 uint32_t errorCode = 0;
880 SourceOptions opts;
881 opts.formatHint = "image/jpeg";
882 std::unique_ptr<ImageSource> imageSource = ImageSource::CreateImageSource(buffer, bufferSize, opts, errorCode);
883 ASSERT_EQ(errorCode, SUCCESS);
884 ASSERT_NE(imageSource.get(), nullptr);
885
886 DecodeOptions decodeOpts;
887 std::unique_ptr<PixelMap> pixmap = imageSource->CreatePixelMap(decodeOpts, errorCode);
888 HiLog::Debug(LABEL_TEST, "create pixel map error code=%{public}u.", errorCode);
889 ASSERT_EQ(errorCode, SUCCESS);
890 ASSERT_NE(pixmap.get(), nullptr);
891
892 std::shared_ptr<ColorPicker> pColorPicker = ColorPicker::CreateColorPicker(std::move(pixmap), errorCode);
893 ASSERT_EQ(errorCode, SUCCESS);
894 EXPECT_NE(pColorPicker, nullptr);
895
896 HSV hsv2 = { 30, 30, 90 };
897 ColorBrightnessMode mode = pColorPicker->DiscriminateDarkOrBrightColor(hsv2);
898 EXPECT_EQ(mode, ColorBrightnessMode::LOW_SATURATION_BRIGHT_COLOR);
899 }
900
901 /**
902 * @tc.name: DiscriminateDarkOrBrightColor002
903 * @tc.desc: DiscriminateDarkOrBrightColor mode is LOW_SATURATION_BRIGHT_COLOR
904 * @tc.type: FUNC
905 * @tc.require:
906 * @tc.author:
907 */
908 HWTEST_F(ColorPickerUnittest, DiscriminateDarkOrBrightColor002, TestSize.Level1)
909 {
910 size_t bufferSize = 0;
911 uint8_t* buffer = GetJpgBuffer(bufferSize);
912 ASSERT_NE(buffer, nullptr);
913
914 uint32_t errorCode = 0;
915 SourceOptions opts;
916 opts.formatHint = "image/jpeg";
917 std::unique_ptr<ImageSource> imageSource = ImageSource::CreateImageSource(buffer, bufferSize, opts, errorCode);
918 ASSERT_EQ(errorCode, SUCCESS);
919 ASSERT_NE(imageSource.get(), nullptr);
920
921 DecodeOptions decodeOpts;
922 std::unique_ptr<PixelMap> pixmap = imageSource->CreatePixelMap(decodeOpts, errorCode);
923 HiLog::Debug(LABEL_TEST, "create pixel map error code=%{public}u.", errorCode);
924 ASSERT_EQ(errorCode, SUCCESS);
925 ASSERT_NE(pixmap.get(), nullptr);
926
927 std::shared_ptr<ColorPicker> pColorPicker = ColorPicker::CreateColorPicker(std::move(pixmap), errorCode);
928 ASSERT_EQ(errorCode, SUCCESS);
929 EXPECT_NE(pColorPicker, nullptr);
930
931 HSV hsv2 = { 100, 20, 90 };
932 ColorBrightnessMode mode = pColorPicker->DiscriminateDarkOrBrightColor(hsv2);
933 EXPECT_EQ(mode, ColorBrightnessMode::LOW_SATURATION_BRIGHT_COLOR);
934 }
935
936 /**
937 * @tc.name: DiscriminateDarkOrBrightColor003
938 * @tc.desc: DiscriminateDarkOrBrightColor mode is DARK_COLOR
939 * @tc.type: FUNC
940 * @tc.require:
941 * @tc.author:
942 */
943 HWTEST_F(ColorPickerUnittest, DiscriminateDarkOrBrightColor003, TestSize.Level1)
944 {
945 size_t bufferSize = 0;
946 uint8_t* buffer = GetJpgBuffer(bufferSize);
947 ASSERT_NE(buffer, nullptr);
948
949 uint32_t errorCode = 0;
950 SourceOptions opts;
951 opts.formatHint = "image/jpeg";
952 std::unique_ptr<ImageSource> imageSource = ImageSource::CreateImageSource(buffer, bufferSize, opts, errorCode);
953 ASSERT_EQ(errorCode, SUCCESS);
954 ASSERT_NE(imageSource.get(), nullptr);
955
956 DecodeOptions decodeOpts;
957 std::unique_ptr<PixelMap> pixmap = imageSource->CreatePixelMap(decodeOpts, errorCode);
958 HiLog::Debug(LABEL_TEST, "create pixel map error code=%{public}u.", errorCode);
959 ASSERT_EQ(errorCode, SUCCESS);
960 ASSERT_NE(pixmap.get(), nullptr);
961
962 std::shared_ptr<ColorPicker> pColorPicker = ColorPicker::CreateColorPicker(std::move(pixmap), errorCode);
963 ASSERT_EQ(errorCode, SUCCESS);
964 EXPECT_NE(pColorPicker, nullptr);
965
966 HSV hsv2 = { 100, 50, 90 };
967 ColorBrightnessMode mode = pColorPicker->DiscriminateDarkOrBrightColor(hsv2);
968 EXPECT_EQ(mode, ColorBrightnessMode::DARK_COLOR);
969 }
970
971 /**
972 * @tc.name: DiscriminateDarkOrBrightColor004
973 * @tc.desc: DiscriminateDarkOrBrightColor mode is LOW_SATURATION_BRIGHT_COLOR
974 * @tc.type: FUNC
975 * @tc.require:
976 * @tc.author:
977 */
978 HWTEST_F(ColorPickerUnittest, DiscriminateDarkOrBrightColor004, TestSize.Level1)
979 {
980 size_t bufferSize = 0;
981 uint8_t* buffer = GetJpgBuffer(bufferSize);
982 ASSERT_NE(buffer, nullptr);
983
984 uint32_t errorCode = 0;
985 SourceOptions opts;
986 opts.formatHint = "image/jpeg";
987 std::unique_ptr<ImageSource> imageSource = ImageSource::CreateImageSource(buffer, bufferSize, opts, errorCode);
988 ASSERT_EQ(errorCode, SUCCESS);
989 ASSERT_NE(imageSource.get(), nullptr);
990
991 DecodeOptions decodeOpts;
992 std::unique_ptr<PixelMap> pixmap = imageSource->CreatePixelMap(decodeOpts, errorCode);
993 HiLog::Debug(LABEL_TEST, "create pixel map error code=%{public}u.", errorCode);
994 ASSERT_EQ(errorCode, SUCCESS);
995 ASSERT_NE(pixmap.get(), nullptr);
996
997 std::shared_ptr<ColorPicker> pColorPicker = ColorPicker::CreateColorPicker(std::move(pixmap), errorCode);
998 ASSERT_EQ(errorCode, SUCCESS);
999 EXPECT_NE(pColorPicker, nullptr);
1000
1001 HSV hsv2 = { 100, 20, 90 };
1002 ColorBrightnessMode mode = pColorPicker->DiscriminateDarkOrBrightColor(hsv2);
1003 EXPECT_EQ(mode, ColorBrightnessMode::LOW_SATURATION_BRIGHT_COLOR);
1004 }
1005
1006 /**
1007 * @tc.name: DiscriminateDarkOrBrightColor005
1008 * @tc.desc: Ensure the ability of creating effect chain from config file.
1009 * @tc.type: FUNC
1010 * @tc.require:
1011 * @tc.author:
1012 */
1013 HWTEST_F(ColorPickerUnittest, DiscriminateDarkOrBrightColor005, TestSize.Level1)
1014 {
1015 size_t bufferSize = 0;
1016 uint8_t* buffer = GetJpgBuffer(bufferSize);
1017 ASSERT_NE(buffer, nullptr);
1018
1019 uint32_t errorCode = 0;
1020 SourceOptions opts;
1021 opts.formatHint = "image/jpeg";
1022 std::unique_ptr<ImageSource> imageSource = ImageSource::CreateImageSource(buffer, bufferSize, opts, errorCode);
1023 ASSERT_EQ(errorCode, SUCCESS);
1024 ASSERT_NE(imageSource.get(), nullptr);
1025
1026 DecodeOptions decodeOpts;
1027 std::unique_ptr<PixelMap> pixmap = imageSource->CreatePixelMap(decodeOpts, errorCode);
1028 HiLog::Debug(LABEL_TEST, "create pixel map error code=%{public}u.", errorCode);
1029 ASSERT_EQ(errorCode, SUCCESS);
1030 ASSERT_NE(pixmap.get(), nullptr);
1031
1032 std::shared_ptr<ColorPicker> pColorPicker = ColorPicker::CreateColorPicker(std::move(pixmap), errorCode);
1033 ASSERT_EQ(errorCode, SUCCESS);
1034 EXPECT_NE(pColorPicker, nullptr);
1035
1036 HSV hsv2 = { 10, 60, 90 };
1037 ColorBrightnessMode mode = pColorPicker->DiscriminateDarkOrBrightColor(hsv2);
1038 EXPECT_EQ(mode, ColorBrightnessMode::HIGH_SATURATION_BRIGHT_COLOR);
1039 }
1040
1041 /**
1042 * @tc.name: AdjustToBasicColor001
1043 * @tc.desc: AdjustToBasicColor x <= y
1044 * @tc.type: FUNC
1045 * @tc.require:
1046 * @tc.author:
1047 */
1048 HWTEST_F(ColorPickerUnittest, AdjustToBasicColor001, TestSize.Level1)
1049 {
1050 size_t bufferSize = 0;
1051 uint8_t* buffer = GetJpgBuffer(bufferSize);
1052 ASSERT_NE(buffer, nullptr);
1053
1054 uint32_t errorCode = 0;
1055 SourceOptions opts;
1056 opts.formatHint = "image/jpeg";
1057 std::unique_ptr<ImageSource> imageSource = ImageSource::CreateImageSource(buffer, bufferSize, opts, errorCode);
1058 ASSERT_EQ(errorCode, SUCCESS);
1059 ASSERT_NE(imageSource.get(), nullptr);
1060
1061 DecodeOptions decodeOpts;
1062 std::unique_ptr<PixelMap> pixmap = imageSource->CreatePixelMap(decodeOpts, errorCode);
1063 HiLog::Debug(LABEL_TEST, "create pixel map error code=%{public}u.", errorCode);
1064 ASSERT_EQ(errorCode, SUCCESS);
1065 ASSERT_NE(pixmap.get(), nullptr);
1066
1067 std::shared_ptr<ColorPicker> pColorPicker = ColorPicker::CreateColorPicker(std::move(pixmap), errorCode);
1068 ASSERT_EQ(errorCode, SUCCESS);
1069 EXPECT_NE(pColorPicker, nullptr);
1070
1071 HSV hsv2 = { 10, 10, 10 };
1072 double basicS = 20.f;
1073 double basicV = 20.f;
1074 pColorPicker->AdjustToBasicColor(hsv2, basicS, basicV);
1075 }
1076
1077 /**
1078 * @tc.name: ProcessToDarkColor001
1079 * @tc.desc: hsv.h > 69 && hsv.h <= 189
1080 * @tc.type: FUNC
1081 * @tc.require:
1082 * @tc.author:
1083 */
1084 HWTEST_F(ColorPickerUnittest, ProcessToDarkColor001, TestSize.Level1)
1085 {
1086 size_t bufferSize = 0;
1087 uint8_t* buffer = GetJpgBuffer(bufferSize);
1088 ASSERT_NE(buffer, nullptr);
1089
1090 uint32_t errorCode = 0;
1091 SourceOptions opts;
1092 opts.formatHint = "image/jpeg";
1093 std::unique_ptr<ImageSource> imageSource = ImageSource::CreateImageSource(buffer, bufferSize, opts, errorCode);
1094 ASSERT_EQ(errorCode, SUCCESS);
1095 ASSERT_NE(imageSource.get(), nullptr);
1096
1097 DecodeOptions decodeOpts;
1098 std::unique_ptr<PixelMap> pixmap = imageSource->CreatePixelMap(decodeOpts, errorCode);
1099 HiLog::Debug(LABEL_TEST, "create pixel map error code=%{public}u.", errorCode);
1100 ASSERT_EQ(errorCode, SUCCESS);
1101 ASSERT_NE(pixmap.get(), nullptr);
1102
1103 std::shared_ptr<ColorPicker> pColorPicker = ColorPicker::CreateColorPicker(std::move(pixmap), errorCode);
1104 ASSERT_EQ(errorCode, SUCCESS);
1105 EXPECT_NE(pColorPicker, nullptr);
1106
1107 HSV hsv2 = { 80, 10, 10 };
1108 pColorPicker->ProcessToDarkColor(hsv2);
1109 }
1110
1111 /**
1112 * @tc.name: ProcessToDarkColor002
1113 * @tc.desc: hsv.h > 189 && hsv.h <= 269
1114 * @tc.type: FUNC
1115 * @tc.require:
1116 * @tc.author:
1117 */
1118 HWTEST_F(ColorPickerUnittest, ProcessToDarkColor002, TestSize.Level1)
1119 {
1120 size_t bufferSize = 0;
1121 uint8_t* buffer = GetJpgBuffer(bufferSize);
1122 ASSERT_NE(buffer, nullptr);
1123
1124 uint32_t errorCode = 0;
1125 SourceOptions opts;
1126 opts.formatHint = "image/jpeg";
1127 std::unique_ptr<ImageSource> imageSource = ImageSource::CreateImageSource(buffer, bufferSize, opts, errorCode);
1128 ASSERT_EQ(errorCode, SUCCESS);
1129 ASSERT_NE(imageSource.get(), nullptr);
1130
1131 DecodeOptions decodeOpts;
1132 std::unique_ptr<PixelMap> pixmap = imageSource->CreatePixelMap(decodeOpts, errorCode);
1133 HiLog::Debug(LABEL_TEST, "create pixel map error code=%{public}u.", errorCode);
1134 ASSERT_EQ(errorCode, SUCCESS);
1135 ASSERT_NE(pixmap.get(), nullptr);
1136
1137 std::shared_ptr<ColorPicker> pColorPicker = ColorPicker::CreateColorPicker(std::move(pixmap), errorCode);
1138 ASSERT_EQ(errorCode, SUCCESS);
1139 EXPECT_NE(pColorPicker, nullptr);
1140
1141 HSV hsv2 = { 190, 10, 10 };
1142 pColorPicker->ProcessToDarkColor(hsv2);
1143 }
1144
1145 /**
1146 * @tc.name: ProcessToDarkColor003
1147 * @tc.desc:else
1148 * @tc.type: FUNC
1149 * @tc.require:
1150 * @tc.author:
1151 */
1152 HWTEST_F(ColorPickerUnittest, ProcessToDarkColor003, TestSize.Level1)
1153 {
1154 size_t bufferSize = 0;
1155 uint8_t* buffer = GetJpgBuffer(bufferSize);
1156 ASSERT_NE(buffer, nullptr);
1157
1158 uint32_t errorCode = 0;
1159 SourceOptions opts;
1160 opts.formatHint = "image/jpeg";
1161 std::unique_ptr<ImageSource> imageSource = ImageSource::CreateImageSource(buffer, bufferSize, opts, errorCode);
1162 ASSERT_EQ(errorCode, SUCCESS);
1163 ASSERT_NE(imageSource.get(), nullptr);
1164
1165 DecodeOptions decodeOpts;
1166 std::unique_ptr<PixelMap> pixmap = imageSource->CreatePixelMap(decodeOpts, errorCode);
1167 ASSERT_EQ(errorCode, SUCCESS);
1168 ASSERT_NE(pixmap.get(), nullptr);
1169
1170 std::shared_ptr<ColorPicker> pColorPicker = ColorPicker::CreateColorPicker(std::move(pixmap), errorCode);
1171 ASSERT_EQ(errorCode, SUCCESS);
1172 EXPECT_NE(pColorPicker, nullptr);
1173
1174 HSV hsv2 = { 280, 10, 10 };
1175 pColorPicker->ProcessToDarkColor(hsv2);
1176 }
1177
1178 /**
1179 * @tc.name: AdjustLowSaturationBrightColor_001
1180 * @tc.desc:
1181 * @tc.type: FUNC
1182 * @tc.require:
1183 * @tc.author:
1184 */
1185 HWTEST_F(ColorPickerUnittest, AdjustLowSaturationBrightColor_001, TestSize.Level1)
1186 {
1187 size_t bufferSize = 0;
1188 uint8_t* buffer = GetJpgBuffer(bufferSize);
1189 ASSERT_NE(buffer, nullptr);
1190
1191 uint32_t errorCode = 0;
1192 SourceOptions opts;
1193 opts.formatHint = "image/jpeg";
1194 std::unique_ptr<ImageSource> imageSource = ImageSource::CreateImageSource(buffer, bufferSize, opts, errorCode);
1195 ASSERT_EQ(errorCode, SUCCESS);
1196 ASSERT_NE(imageSource.get(), nullptr);
1197
1198 DecodeOptions decodeOpts;
1199 std::unique_ptr<PixelMap> pixmap = imageSource->CreatePixelMap(decodeOpts, errorCode);
1200 HiLog::Debug(LABEL_TEST, "create pixel map error code=%{public}u.", errorCode);
1201 ASSERT_EQ(errorCode, SUCCESS);
1202 ASSERT_NE(pixmap.get(), nullptr);
1203
1204 std::shared_ptr<ColorPicker> pColorPicker = ColorPicker::CreateColorPicker(std::move(pixmap), errorCode);
1205 ASSERT_EQ(errorCode, SUCCESS);
1206 EXPECT_NE(pColorPicker, nullptr);
1207
1208 HSV colorHsv = { 30, 5, 90 };
1209 HSV mainHsv = { 30, 30, 90 };
1210 HSV secondaryHsv = { 30, 30, 90 };
1211
1212 const std::pair<uint32_t, uint32_t> primaryColor;
1213 const std::pair<uint32_t, uint32_t> secondaryColor;
1214 pColorPicker->AdjustLowSaturationBrightColor(colorHsv, mainHsv, secondaryHsv, primaryColor, secondaryColor);
1215 }
1216
1217 /**
1218 * @tc.name: AdjustLowSaturationBrightColor002
1219 * @tc.desc: primaryColor.second - secondaryColor.second < colorValLen_ / 10.
1220 * @tc.type: FUNC
1221 * @tc.require:
1222 * @tc.author:
1223 */
1224 HWTEST_F(ColorPickerUnittest, AdjustLowSaturationBrightColor002, TestSize.Level1)
1225 {
1226 size_t bufferSize = 0;
1227 uint8_t* buffer = GetJpgBuffer(bufferSize);
1228 ASSERT_NE(buffer, nullptr);
1229
1230 uint32_t errorCode = 0;
1231 SourceOptions opts;
1232 opts.formatHint = "image/jpeg";
1233 std::unique_ptr<ImageSource> imageSource = ImageSource::CreateImageSource(buffer, bufferSize, opts, errorCode);
1234 ASSERT_EQ(errorCode, SUCCESS);
1235 ASSERT_NE(imageSource.get(), nullptr);
1236
1237 DecodeOptions decodeOpts;
1238 std::unique_ptr<PixelMap> pixmap = imageSource->CreatePixelMap(decodeOpts, errorCode);
1239 HiLog::Debug(LABEL_TEST, "create pixel map error code=%{public}u.", errorCode);
1240 ASSERT_EQ(errorCode, SUCCESS);
1241 ASSERT_NE(pixmap.get(), nullptr);
1242
1243 std::shared_ptr<ColorPicker> pColorPicker = ColorPicker::CreateColorPicker(std::move(pixmap), errorCode);
1244 ASSERT_EQ(errorCode, SUCCESS);
1245 EXPECT_NE(pColorPicker, nullptr);
1246
1247 HSV colorHsv = { 30, 5, 90 };
1248 HSV mainHsv = { 30, 30, 90 };
1249 HSV secondaryHsv = { 100, 50, 90 };
1250
1251 std::pair<uint32_t, uint32_t> primaryColor(0, 20);
1252 std::pair<uint32_t, uint32_t> secondaryColor(1, 10);
1253 pColorPicker->colorValLen_ = 200;
1254 pColorPicker->AdjustLowSaturationBrightColor(colorHsv, mainHsv, secondaryHsv, primaryColor, secondaryColor);
1255 }
1256
1257 /**
1258 * @tc.name: GetImmersiveBackgroundColor001
1259 * @tc.desc: featureColors_ is empty.
1260 * @tc.type: FUNC
1261 * @tc.require:
1262 * @tc.author:
1263 */
1264 HWTEST_F(ColorPickerUnittest, GetImmersiveBackgroundColor001, TestSize.Level1)
1265 {
1266 size_t bufferSize = 0;
1267 uint8_t* buffer = GetJpgBuffer(bufferSize);
1268 ASSERT_NE(buffer, nullptr);
1269
1270 uint32_t errorCode = 0;
1271 SourceOptions opts;
1272 opts.formatHint = "image/jpeg";
1273 std::unique_ptr<ImageSource> imageSource = ImageSource::CreateImageSource(buffer, bufferSize, opts, errorCode);
1274 ASSERT_EQ(errorCode, SUCCESS);
1275 ASSERT_NE(imageSource.get(), nullptr);
1276
1277 DecodeOptions decodeOpts;
1278 std::unique_ptr<PixelMap> pixmap = imageSource->CreatePixelMap(decodeOpts, errorCode);
1279 HiLog::Debug(LABEL_TEST, "create pixel map error code=%{public}u.", errorCode);
1280 ASSERT_EQ(errorCode, SUCCESS);
1281 ASSERT_NE(pixmap.get(), nullptr);
1282
1283 std::shared_ptr<ColorPicker> pColorPicker = ColorPicker::CreateColorPicker(std::move(pixmap), errorCode);
1284 ASSERT_EQ(errorCode, SUCCESS);
1285 EXPECT_NE(pColorPicker, nullptr);
1286
1287 ColorManager::Color color;
1288 pColorPicker->featureColors_.clear();
1289 uint32_t ret = pColorPicker->GetImmersiveBackgroundColor(color);
1290 EXPECT_EQ(ret, ERR_EFFECT_INVALID_VALUE);
1291 }
1292
1293 /**
1294 * @tc.name: GetImmersiveForegroundColor001
1295 * @tc.desc: featureColors_ is empty.
1296 * @tc.type: FUNC
1297 * @tc.require:
1298 * @tc.author:
1299 */
1300 HWTEST_F(ColorPickerUnittest, GetImmersiveForegroundColor001, TestSize.Level1)
1301 {
1302 size_t bufferSize = 0;
1303 uint8_t* buffer = GetJpgBuffer(bufferSize);
1304 ASSERT_NE(buffer, nullptr);
1305
1306 uint32_t errorCode = 0;
1307 SourceOptions opts;
1308 opts.formatHint = "image/jpeg";
1309 std::unique_ptr<ImageSource> imageSource = ImageSource::CreateImageSource(buffer, bufferSize, opts, errorCode);
1310 ASSERT_EQ(errorCode, SUCCESS);
1311 ASSERT_NE(imageSource.get(), nullptr);
1312
1313 DecodeOptions decodeOpts;
1314 std::unique_ptr<PixelMap> pixmap = imageSource->CreatePixelMap(decodeOpts, errorCode);
1315 HiLog::Debug(LABEL_TEST, "create pixel map error code=%{public}u.", errorCode);
1316 ASSERT_EQ(errorCode, SUCCESS);
1317 ASSERT_NE(pixmap.get(), nullptr);
1318
1319 std::shared_ptr<ColorPicker> pColorPicker = ColorPicker::CreateColorPicker(std::move(pixmap), errorCode);
1320 ASSERT_EQ(errorCode, SUCCESS);
1321 EXPECT_NE(pColorPicker, nullptr);
1322
1323 ColorManager::Color color;
1324 pColorPicker->featureColors_.clear();
1325 uint32_t ret = pColorPicker->GetImmersiveForegroundColor(color);
1326 EXPECT_EQ(ret, ERR_EFFECT_INVALID_VALUE);
1327 }
1328
1329 /**
1330 * @tc.name: GetDominantColor001
1331 * @tc.desc: featureColors_ is empty.
1332 * @tc.type: FUNC
1333 * @tc.require:
1334 * @tc.author:
1335 */
1336 HWTEST_F(ColorPickerUnittest, GetDominantColor001, TestSize.Level1)
1337 {
1338 size_t bufferSize = 0;
1339 uint8_t* buffer = GetJpgBuffer(bufferSize);
1340 ASSERT_NE(buffer, nullptr);
1341
1342 uint32_t errorCode = 0;
1343 SourceOptions opts;
1344 opts.formatHint = "image/jpeg";
1345 std::unique_ptr<ImageSource> imageSource = ImageSource::CreateImageSource(buffer, bufferSize, opts, errorCode);
1346 ASSERT_EQ(errorCode, SUCCESS);
1347 ASSERT_NE(imageSource.get(), nullptr);
1348
1349 DecodeOptions decodeOpts;
1350 std::unique_ptr<PixelMap> pixmap = imageSource->CreatePixelMap(decodeOpts, errorCode);
1351 HiLog::Debug(LABEL_TEST, "create pixel map error code=%{public}u.", errorCode);
1352 ASSERT_EQ(errorCode, SUCCESS);
1353 ASSERT_NE(pixmap.get(), nullptr);
1354
1355 std::shared_ptr<ColorPicker> pColorPicker = ColorPicker::CreateColorPicker(std::move(pixmap), errorCode);
1356 ASSERT_EQ(errorCode, SUCCESS);
1357 EXPECT_NE(pColorPicker, nullptr);
1358
1359 pColorPicker->featureColors_.clear();
1360 std::pair<uint32_t, uint32_t> primaryColor;
1361 std::pair<uint32_t, uint32_t> secondaryColor;
1362 bool ret = pColorPicker->GetDominantColor(primaryColor, secondaryColor);
1363 EXPECT_EQ(ret, false);
1364 }
1365
1366 /**
1367 * @tc.name: GetDominantColor002
1368 * @tc.desc: featureColors_ size is 2.
1369 * @tc.type: FUNC
1370 * @tc.require:
1371 * @tc.author:
1372 */
1373 HWTEST_F(ColorPickerUnittest, GetDominantColor002, TestSize.Level1)
1374 {
1375 size_t bufferSize = 0;
1376 uint8_t* buffer = GetJpgBuffer(bufferSize);
1377 ASSERT_NE(buffer, nullptr);
1378
1379 uint32_t errorCode = 0;
1380 SourceOptions opts;
1381 opts.formatHint = "image/jpeg";
1382 std::unique_ptr<ImageSource> imageSource = ImageSource::CreateImageSource(buffer, bufferSize, opts, errorCode);
1383 ASSERT_EQ(errorCode, SUCCESS);
1384 ASSERT_NE(imageSource.get(), nullptr);
1385
1386 DecodeOptions decodeOpts;
1387 std::unique_ptr<PixelMap> pixmap = imageSource->CreatePixelMap(decodeOpts, errorCode);
1388 HiLog::Debug(LABEL_TEST, "create pixel map error code=%{public}u.", errorCode);
1389 ASSERT_EQ(errorCode, SUCCESS);
1390 ASSERT_NE(pixmap.get(), nullptr);
1391
1392 std::shared_ptr<ColorPicker> pColorPicker = ColorPicker::CreateColorPicker(std::move(pixmap), errorCode);
1393 ASSERT_EQ(errorCode, SUCCESS);
1394 EXPECT_NE(pColorPicker, nullptr);
1395
1396 std::pair<uint32_t, uint32_t> featureColor;
1397 featureColor.first = 0;
1398 featureColor.second = 20;
1399 pColorPicker->featureColors_.emplace_back(featureColor);
1400 featureColor.first = 1;
1401 featureColor.second = 10;
1402 pColorPicker->featureColors_.emplace_back(featureColor);
1403 pColorPicker->colorValLen_ = 20;
1404 std::pair<uint32_t, uint32_t> primaryColor;
1405 std::pair<uint32_t, uint32_t> secondaryColor;
1406 bool ret = pColorPicker->GetDominantColor(primaryColor, secondaryColor);
1407 EXPECT_EQ(ret, true);
1408
1409 pColorPicker->featureColors_.clear();
1410 featureColor.first = 0;
1411 featureColor.second = 10;
1412 pColorPicker->featureColors_.emplace_back(featureColor);
1413 featureColor.first = 1;
1414 featureColor.second = 10;
1415 pColorPicker->featureColors_.emplace_back(featureColor);
1416 ret = pColorPicker->GetDominantColor(primaryColor, secondaryColor);
1417 EXPECT_EQ(ret, false);
1418 }
1419
1420 /**
1421 * @tc.name: GetDeepenImmersionColor001
1422 * @tc.desc: featureColors_ is empty.
1423 * @tc.type: FUNC
1424 * @tc.require:
1425 * @tc.author:
1426 */
1427 HWTEST_F(ColorPickerUnittest, GetDeepenImmersionColor001, TestSize.Level1)
1428 {
1429 size_t bufferSize = 0;
1430 uint8_t* buffer = GetJpgBuffer(bufferSize);
1431 ASSERT_NE(buffer, nullptr);
1432
1433 uint32_t errorCode = 0;
1434 SourceOptions opts;
1435 opts.formatHint = "image/jpeg";
1436 std::unique_ptr<ImageSource> imageSource = ImageSource::CreateImageSource(buffer, bufferSize, opts, errorCode);
1437 ASSERT_EQ(errorCode, SUCCESS);
1438 ASSERT_NE(imageSource.get(), nullptr);
1439
1440 DecodeOptions decodeOpts;
1441 std::unique_ptr<PixelMap> pixmap = imageSource->CreatePixelMap(decodeOpts, errorCode);
1442 HiLog::Debug(LABEL_TEST, "create pixel map error code=%{public}u.", errorCode);
1443 ASSERT_EQ(errorCode, SUCCESS);
1444 ASSERT_NE(pixmap.get(), nullptr);
1445
1446 std::shared_ptr<ColorPicker> pColorPicker = ColorPicker::CreateColorPicker(std::move(pixmap), errorCode);
1447 ASSERT_EQ(errorCode, SUCCESS);
1448 EXPECT_NE(pColorPicker, nullptr);
1449
1450 pColorPicker->featureColors_.clear();
1451 ColorManager::Color color;
1452 uint32_t ret = pColorPicker->GetDeepenImmersionColor(color);
1453 EXPECT_EQ(ret, ERR_EFFECT_INVALID_VALUE);
1454 }
1455
1456 /**
1457 * @tc.name: IsBlackOrWhiteOrGrayColor
1458 * @tc.desc: Ensure the ability of creating effect chain from config file.
1459 * @tc.type: FUNC
1460 * @tc.require:
1461 * @tc.author:
1462 */
1463 HWTEST_F(ColorPickerUnittest, IsBlackOrWhiteOrGrayColor, TestSize.Level1)
1464 {
1465 GTEST_LOG_(INFO) << "ColorPickerUnittest IsBlackOrWhiteOrGrayColor start";
1466 size_t bufferSize = 0;
1467 uint8_t *buffer = GetJpgBuffer(bufferSize);
1468 ASSERT_NE(buffer, nullptr);
1469
1470 uint32_t errorCode = 0;
1471 SourceOptions opts;
1472 opts.formatHint = "image/jpeg";
1473 std::unique_ptr<ImageSource> imageSource =
1474 ImageSource::CreateImageSource(buffer, bufferSize, opts, errorCode);
1475 ASSERT_EQ(errorCode, SUCCESS);
1476 ASSERT_NE(imageSource.get(), nullptr);
1477
1478 DecodeOptions decodeOpts;
1479 std::unique_ptr<PixelMap> pixmap = imageSource->CreatePixelMap(decodeOpts, errorCode);
1480 HiLog::Debug(LABEL_TEST, "create pixel map error code=%{public}u.", errorCode);
1481 ASSERT_EQ(errorCode, SUCCESS);
1482 ASSERT_NE(pixmap.get(), nullptr);
1483
1484 std::shared_ptr<ColorPicker> pColorPicker = ColorPicker::CreateColorPicker(std::move(pixmap), errorCode);
1485 ASSERT_EQ(errorCode, SUCCESS);
1486 EXPECT_NE(pColorPicker, nullptr);
1487
1488 bool judgeRst = pColorPicker->IsBlackOrWhiteOrGrayColor(0xFFFFFFFF);
1489 HiLog::Info(LABEL_TEST, "get largest proportion color result=%{public}d", judgeRst);
1490 ASSERT_EQ(judgeRst, true);
1491 }
1492
1493 /**
1494 * @tc.name: GetTopProportionColors
1495 * @tc.desc: Ensure the ability of creating effect chain from config file.
1496 * @tc.type: FUNC
1497 * @tc.require:
1498 * @tc.author:
1499 */
1500 HWTEST_F(ColorPickerUnittest, GetTopProportionColors, TestSize.Level1)
1501 {
1502 GTEST_LOG_(INFO) << "ColorPickerUnittest GetTopProportionColors start";
1503 size_t bufferSize = 0;
1504 uint8_t *buffer = GetJpgBuffer(bufferSize);
1505 ASSERT_NE(buffer, nullptr);
1506
1507 uint32_t errorCode = 0;
1508 SourceOptions opts;
1509 opts.formatHint = "image/jpeg";
1510 std::unique_ptr<ImageSource> imageSource =
1511 ImageSource::CreateImageSource(buffer, bufferSize, opts, errorCode);
1512 ASSERT_EQ(errorCode, SUCCESS);
1513 ASSERT_NE(imageSource.get(), nullptr);
1514
1515 DecodeOptions decodeOpts;
1516 std::unique_ptr<PixelMap> pixmap = imageSource->CreatePixelMap(decodeOpts, errorCode);
1517 HiLog::Debug(LABEL_TEST, "create pixel map error code=%{public}u.", errorCode);
1518 ASSERT_EQ(errorCode, SUCCESS);
1519 ASSERT_NE(pixmap.get(), nullptr);
1520
1521 std::shared_ptr<ColorPicker> pColorPicker = ColorPicker::CreateColorPicker(std::move(pixmap), errorCode);
1522 ASSERT_EQ(errorCode, SUCCESS);
1523 EXPECT_NE(pColorPicker, nullptr);
1524
1525 std::vector<ColorManager::Color> colors = pColorPicker->GetTopProportionColors(10); // the color num limit is 10
1526 HiLog::Info(LABEL_TEST, "get top proportion colors[0][rgba]=%{public}f,%{public}f,%{public}f,%{public}f",
1527 colors[0].r, colors[0].g, colors[0].b, colors[0].a);
1528 ASSERT_EQ(colors.size(), 1);
1529 bool ret = colors[0].ColorEqual(
1530 ColorManager::Color(0.972549f, 0.784314f, 0.0313726f, 1.f)); // the top 1 proportion color
1531 EXPECT_EQ(true, ret);
1532
1533 std::vector<ColorManager::Color> colors1 = pColorPicker->GetTopProportionColors(1);
1534 HiLog::Info(LABEL_TEST, "get top proportion colors[0][rgba]=%{public}f,%{public}f,%{public}f,%{public}f",
1535 colors1[0].r, colors1[0].g, colors1[0].b, colors1[0].a);
1536 ASSERT_EQ(colors1.size(), 1);
1537 ret = colors1[0].ColorEqual(
1538 ColorManager::Color(0.972549f, 0.784314f, 0.0313726f, 1.f)); // the top 1 proportion color
1539 EXPECT_EQ(true, ret);
1540
1541 std::vector<ColorManager::Color> colors2 = pColorPicker->GetTopProportionColors(0);
1542 ASSERT_EQ(colors2.size(), 0);
1543 }
1544
1545 /**
1546 * @tc.name: AdjustHSVToDefinedInterval
1547 * @tc.desc: check hsv to defined interval.
1548 * @tc.type: FUNC
1549 * @tc.require:
1550 * @tc.author:
1551 */
1552 HWTEST_F(ColorPickerUnittest, AdjustHSVToDefinedInterval, TestSize.Level1)
1553 {
1554 GTEST_LOG_(INFO) << "ColorPickerUnittest AdjustHSVToDefinedInterval start";
1555
1556 std::shared_ptr<ColorPicker> pColorPicker = CreateColorPicker();
1557 ASSERT_NE(pColorPicker, nullptr);
1558
1559 HSV gHsv {361, 101, 101}; // 361,101,101 invalid hsv
1560 pColorPicker->AdjustHSVToDefinedInterval(gHsv);
1561 EXPECT_EQ(gHsv.h, 360); // 360 is valid hue
1562 EXPECT_EQ(gHsv.s, 100); // 100 is valid saturation
1563 EXPECT_EQ(gHsv.v, 100); // 100 is valid value
1564
1565 HSV lHsv {-1, -1, -1}; // -1, -1, -1 invalid hsv
1566 pColorPicker->AdjustHSVToDefinedInterval(lHsv);
1567 EXPECT_EQ(lHsv.h, 0); // 0 is valid hue
1568 EXPECT_EQ(lHsv.s, 0); // 0 is valid saturation
1569 EXPECT_EQ(lHsv.v, 0); // 0 is valid value
1570
1571 GTEST_LOG_(INFO) << "ColorPickerUnittest AdjustHSVToDefinedInterval end";
1572 }
1573 } // namespace Rosen
1574 } // namespace OHOS
1575