1 /*
2 * Copyright (c) 2024 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_common_unittest.h"
17
18 #include "color.h"
19 #include "color_picker.h"
20 #include "effect_errors.h"
21 #include "hilog/log.h"
22 #include "image_source.h"
23 #include "pixel_map.h"
24 #include "test_picture_files.h"
25
26 using namespace testing;
27 using namespace testing::ext;
28 using namespace OHOS::Media;
29 using namespace OHOS::HiviewDFX;
30
31 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL_TEST = { LOG_CORE, LOG_DOMAIN, "ColorPickerCommonTest" };
32
33 namespace OHOS {
34 namespace Rosen {
35
CreateColorPicker()36 std::shared_ptr<ColorPickerCommon> ColorPickerCommonUnittest::CreateColorPicker()
37 {
38 size_t bufferSize = 0;
39 uint8_t* buffer = GetJpgBuffer(bufferSize);
40 if (buffer == nullptr) {
41 return nullptr;
42 }
43
44 uint32_t errorCode = 0;
45 SourceOptions opts;
46 opts.formatHint = "image/jpeg";
47 std::unique_ptr<ImageSource> imageSource = ImageSource::CreateImageSource(buffer, bufferSize, opts, errorCode);
48 if ((errorCode != SUCCESS) || (imageSource == nullptr)) {
49 return nullptr;
50 }
51
52 DecodeOptions decodeOpts;
53 std::unique_ptr<PixelMap> pixmap = imageSource->CreatePixelMap(decodeOpts, errorCode);
54 if ((errorCode != SUCCESS) || (pixmap == nullptr)) {
55 return nullptr;
56 }
57
58 return ColorPickerCommon::CreateColorPicker(std::move(pixmap), errorCode);
59 }
60 /**
61 * @tc.name: CreateColorPickerFromPixelmapTest001
62 * @tc.desc: Ensure the ability of creating color picker from pixelmap.
63 * @tc.type: FUNC
64 * @tc.require:
65 * @tc.author:
66 */
67 HWTEST_F(ColorPickerCommonUnittest, CreateColorPickerFromPixelmapTest001, TestSize.Level1)
68 {
69 GTEST_LOG_(INFO) << "ColorPickerCommonUnittest CreateColorPickerFromPixelmapTest001 start";
70 /**
71 * @tc.steps: step1. Create a pixelmap
72 */
73 Media::InitializationOptions opts;
74 opts.size.width = 200;
75 opts.size.height = 150;
76 opts.editable = true;
77 std::unique_ptr<Media::PixelMap> pixmap = Media::PixelMap::Create(opts);
78
79 /**
80 * @tc.steps: step2. Call create From pixelMap
81 */
82 uint32_t errorCode = SUCCESS;
83 std::shared_ptr<ColorPickerCommon> pColorPicker =
84 ColorPickerCommon::CreateColorPicker(std::move(pixmap), errorCode);
85 ASSERT_EQ(errorCode, SUCCESS);
86 EXPECT_NE(pColorPicker, nullptr);
87 }
88
89 /**
90 * @tc.name: CreateColorPickerFromPixelmapTest002
91 * @tc.desc: Ensure the ability of creating color picker from pixelmap.
92 * @tc.type: FUNC
93 * @tc.require:
94 * @tc.author:
95 */
96 HWTEST_F(ColorPickerCommonUnittest, CreateColorPickerFromPixelmapTest002, TestSize.Level1)
97 {
98 GTEST_LOG_(INFO) << "ColorPickerCommonUnittest CreateColorPickerFromPixelmapTest002 start";
99 size_t bufferSize = 0;
100 uint8_t* buffer = GetPngBuffer(bufferSize);
101 ASSERT_NE(buffer, nullptr);
102
103 /**
104 * @tc.steps: step1. Create a ImageSource
105 */
106 uint32_t errorCode = 0;
107 SourceOptions opts;
108 opts.formatHint = "image/png";
109 std::unique_ptr<ImageSource> imageSource = ImageSource::CreateImageSource(buffer, bufferSize, opts, errorCode);
110 ASSERT_EQ(errorCode, SUCCESS);
111
112 /**
113 * @tc.steps: step2. decode image source to pixel map by default decode options
114 * @tc.expected: step2. decode image source to pixel map success.
115 */
116 DecodeOptions decodeOpts;
117 std::unique_ptr<PixelMap> pixmap = imageSource->CreatePixelMap(decodeOpts, errorCode);
118 HiLog::Debug(LABEL_TEST, "create pixel map error code=%{public}u.", errorCode);
119 ASSERT_EQ(errorCode, SUCCESS);
120 ASSERT_NE(pixmap.get(), nullptr);
121
122 /**
123 * @tc.steps: step3. Call create From pixelMap
124 */
125 std::shared_ptr<ColorPickerCommon> pColorPicker =
126 ColorPickerCommon::CreateColorPicker(std::move(pixmap), errorCode);
127 EXPECT_NE(pColorPicker, nullptr);
128 }
129
130 /**
131 * @tc.name: CreateColorPickerFromPixelmapTest003
132 * @tc.desc: Ensure the ability of creating effect chain from config file.
133 * @tc.type: FUNC
134 * @tc.require:
135 * @tc.author:
136 */
137 HWTEST_F(ColorPickerCommonUnittest, CreateColorPickerFromPixelmapTest003, TestSize.Level1)
138 {
139 GTEST_LOG_(INFO) << "ColorPickerCommonUnittest CreateColorPickerFromPixelmapTest003 start";
140 /**
141 * @tc.steps: step1. Create a pixelMap
142 */
143 std::unique_ptr<Media::PixelMap> pixmap = nullptr;
144
145 /**
146 * @tc.steps: step2. Call create From pixelMap
147 */
148 uint32_t errorCode = SUCCESS;
149 std::shared_ptr<ColorPickerCommon> pColorPicker =
150 ColorPickerCommon::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(ColorPickerCommonUnittest, CreateColorPickerFromPixelmapTest004, TestSize.Level1)
163 {
164 GTEST_LOG_(INFO) << "ColorPickerCommonUnittest 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 std::vector<double> region = { 0, 0, 0.5, 0.5 };
179 std::shared_ptr<ColorPickerCommon> pColorPicker =
180 ColorPickerCommon::CreateColorPicker(std::move(pixmap), region, errorCode);
181 ASSERT_EQ(errorCode, SUCCESS);
182 EXPECT_NE(pColorPicker, nullptr);
183 }
184
185 /**
186 * @tc.name: CreateColorPickerFromPixelmapTest005
187 * @tc.desc: Ensure the ability of creating color picker from pixelmap.
188 * @tc.type: FUNC
189 * @tc.require:
190 * @tc.author:
191 */
192 HWTEST_F(ColorPickerCommonUnittest, CreateColorPickerFromPixelmapTest005, TestSize.Level1)
193 {
194 GTEST_LOG_(INFO) << "ColorPickerCommonUnittest CreateColorPickerFromPixelmapTest005 start";
195 /**
196 * @tc.steps: step1. Create a pixelmap
197 */
198 Media::InitializationOptions opts;
199 opts.size.width = 200;
200 opts.size.height = 150;
201 opts.editable = true;
202 std::unique_ptr<Media::PixelMap> pixmap = Media::PixelMap::Create(opts);
203
204 /**
205 * @tc.steps: step2. Call create From pixelMap
206 */
207 uint32_t errorCode = SUCCESS;
208 std::vector<double> region = { 0, 0, 0.5, 0.5 };
209 std::shared_ptr<ColorPickerCommon> pColorPicker =
210 ColorPickerCommon::CreateColorPicker(std::move(pixmap), region, errorCode);
211 EXPECT_NE(pColorPicker, nullptr);
212 }
213
214 /**
215 * @tc.name: GetMainColorTest001
216 * @tc.desc: Ensure the ability of creating effect chain from config file.
217 * @tc.type: FUNC
218 * @tc.require:
219 * @tc.author:
220 */
221 HWTEST_F(ColorPickerCommonUnittest, GetMainColorTest001, TestSize.Level1)
222 {
223 GTEST_LOG_(INFO) << "ColorPickerCommonUnittest GetMainColorTest001 start";
224 size_t bufferSize = 0;
225 uint8_t* buffer = GetJpgBuffer(bufferSize);
226 ASSERT_NE(buffer, nullptr);
227
228 /**
229 * @tc.steps: step1. create image source by correct jpeg file path and jpeg format hit.
230 * @tc.expected: step1. create image source success.
231 */
232 uint32_t errorCode = 0;
233 SourceOptions opts;
234 opts.formatHint = "image/jpeg";
235 std::unique_ptr<ImageSource> imageSource = 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<ColorPickerCommon> pColorPicker =
253 ColorPickerCommon::CreateColorPicker(std::move(pixmap), errorCode);
254 ASSERT_EQ(errorCode, SUCCESS);
255 EXPECT_NE(pColorPicker, nullptr);
256
257 /**
258 * @tc.steps: step3. Get main color from pixmap
259 */
260 ColorManager::Color color;
261 errorCode = pColorPicker->GetMainColor(color);
262 HiLog::Info(LABEL_TEST, "get main color t1[rgba]=%{public}f,%{public}f,%{public}f,%{public}f", color.r, color.g,
263 color.b, color.a);
264 ASSERT_EQ(errorCode, SUCCESS);
265 bool ret = color.ColorEqual(ColorManager::Color(1.f, 0.788235f, 0.050980f, 1.f));
266 EXPECT_EQ(true, ret);
267 }
268
269 /**
270 * @tc.name: GetMainColorTest002
271 * @tc.desc: Ensure the ability of creating effect chain from config file.
272 * @tc.type: FUNC
273 * @tc.require:
274 * @tc.author:
275 */
276 HWTEST_F(ColorPickerCommonUnittest, GetMainColorTest002, TestSize.Level1)
277 {
278 GTEST_LOG_(INFO) << "ColorPickerCommonUnittest GetMainColorTest002 start";
279 size_t bufferSize = 0;
280 uint8_t* buffer = GetPngBuffer(bufferSize);
281 ASSERT_NE(buffer, nullptr);
282
283 /**
284 * @tc.steps: step1. Create a ImageSource
285 */
286 uint32_t errorCode = 0;
287 SourceOptions opts;
288 opts.formatHint = "image/png";
289 std::unique_ptr<ImageSource> imageSource = 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<ColorPickerCommon> pColorPicker =
307 ColorPickerCommon::CreateColorPicker(std::move(pixmap), errorCode);
308 ASSERT_EQ(errorCode, SUCCESS);
309 ASSERT_NE(pColorPicker, nullptr);
310
311 /**
312 * @tc.steps: step4. Get main color from pixmap
313 */
314 ColorManager::Color color;
315 errorCode = pColorPicker->GetMainColor(color);
316 HiLog::Info(LABEL_TEST, "get main color t2[rgba]=%{public}f,%{public}f,%{public}f,%{public}f", color.r, color.g,
317 color.b, color.a);
318 ASSERT_EQ(errorCode, SUCCESS);
319 bool ret = color.ColorEqual(ColorManager::Color(1.f, 1.f, 1.f, 1.f));
320 EXPECT_EQ(true, ret);
321 }
322
323 /**
324 * @tc.name: GetMainColorTest003
325 * @tc.desc: Ensure the ability of creating effect chain from config file.
326 * @tc.type: FUNC
327 * @tc.require:
328 * @tc.author:
329 */
330 HWTEST_F(ColorPickerCommonUnittest, GetMainColorTest003, TestSize.Level1)
331 {
332 GTEST_LOG_(INFO) << "ColorPickerCommonUnittest GetMainColorTest003 start";
333 /**
334 * @tc.steps: step1. Create a pixelMap
335 */
336 Media::InitializationOptions opts;
337 opts.size.width = 200;
338 opts.size.height = 100;
339 opts.editable = true;
340 std::unique_ptr<Media::PixelMap> pixmap = Media::PixelMap::Create(opts);
341
342 /**
343 * @tc.steps: step2. Call create From pixelMap
344 */
345 uint32_t errorCode = SUCCESS;
346 std::shared_ptr<ColorPickerCommon> pColorPicker =
347 ColorPickerCommon::CreateColorPicker(std::move(pixmap), errorCode);
348 ASSERT_EQ(errorCode, SUCCESS);
349 ASSERT_NE(pColorPicker, nullptr);
350
351 /**
352 * @tc.steps: step3. Get main color from pixmap
353 */
354 ColorManager::Color color;
355 errorCode = pColorPicker->GetMainColor(color);
356 HiLog::Info(LABEL_TEST, "get main color t3[rgba]=%{public}f,%{public}f,%{public}f,%{public}f", color.r, color.g,
357 color.b, color.a);
358 ASSERT_EQ(errorCode, SUCCESS);
359 bool ret = color.ColorEqual(ColorManager::Color(0x00000000U));
360 EXPECT_EQ(true, ret);
361 }
362
363 /**
364 * @tc.name: GetLargestProportionColor
365 * @tc.desc: Ensure the ability of creating effect chain from config file.
366 * @tc.type: FUNC
367 * @tc.require:
368 * @tc.author:
369 */
370 HWTEST_F(ColorPickerCommonUnittest, GetLargestProportionColor, TestSize.Level1)
371 {
372 GTEST_LOG_(INFO) << "ColorPickerCommonUnittest GetLargestProportionColor start";
373 size_t bufferSize = 0;
374 uint8_t* buffer = GetJpgBuffer(bufferSize);
375 ASSERT_NE(buffer, nullptr);
376
377 uint32_t errorCode = 0;
378 SourceOptions opts;
379 opts.formatHint = "image/jpeg";
380 std::unique_ptr<ImageSource> imageSource = ImageSource::CreateImageSource(buffer, bufferSize, opts, errorCode);
381 ASSERT_EQ(errorCode, SUCCESS);
382 ASSERT_NE(imageSource.get(), nullptr);
383
384 DecodeOptions decodeOpts;
385 std::unique_ptr<PixelMap> pixmap = imageSource->CreatePixelMap(decodeOpts, errorCode);
386 HiLog::Debug(LABEL_TEST, "create pixel map error code=%{public}u.", errorCode);
387 ASSERT_EQ(errorCode, SUCCESS);
388 ASSERT_NE(pixmap.get(), nullptr);
389
390 std::shared_ptr<ColorPickerCommon> pColorPicker =
391 ColorPickerCommon::CreateColorPicker(std::move(pixmap), errorCode);
392 ASSERT_EQ(errorCode, SUCCESS);
393 EXPECT_NE(pColorPicker, nullptr);
394
395 ColorManager::Color color;
396 errorCode = pColorPicker->GetLargestProportionColor(color);
397 HiLog::Info(LABEL_TEST, "get largest proportion color [rgba]=%{public}f,%{public}f,%{public}f,%{public}f", color.r,
398 color.g, color.b, color.a);
399 ASSERT_EQ(errorCode, SUCCESS);
400 bool ret = color.ColorEqual(ColorManager::Color(0.972549f, 0.784314f, 0.0313726f, 1.f));
401 EXPECT_EQ(true, ret);
402 }
403
404 /**
405 * @tc.name: GetHighestSaturationColor
406 * @tc.desc: Ensure the ability of creating effect chain from config file.
407 * @tc.type: FUNC
408 * @tc.require:
409 * @tc.author:
410 */
411 HWTEST_F(ColorPickerCommonUnittest, GetHighestSaturationColor, TestSize.Level1)
412 {
413 GTEST_LOG_(INFO) << "ColorPickerCommonUnittest GetHighestSaturationColor start";
414 size_t bufferSize = 0;
415 uint8_t* buffer = GetJpgBuffer(bufferSize);
416 ASSERT_NE(buffer, nullptr);
417
418 uint32_t errorCode = 0;
419 SourceOptions opts;
420 opts.formatHint = "image/jpeg";
421 std::unique_ptr<ImageSource> imageSource = ImageSource::CreateImageSource(buffer, bufferSize, opts, errorCode);
422 ASSERT_EQ(errorCode, SUCCESS);
423 ASSERT_NE(imageSource.get(), nullptr);
424
425 DecodeOptions decodeOpts;
426 std::unique_ptr<PixelMap> pixmap = imageSource->CreatePixelMap(decodeOpts, errorCode);
427 HiLog::Debug(LABEL_TEST, "create pixel map error code=%{public}u.", errorCode);
428 ASSERT_EQ(errorCode, SUCCESS);
429 ASSERT_NE(pixmap.get(), nullptr);
430
431 std::shared_ptr<ColorPickerCommon> pColorPicker =
432 ColorPickerCommon::CreateColorPicker(std::move(pixmap), errorCode);
433 ASSERT_EQ(errorCode, SUCCESS);
434 EXPECT_NE(pColorPicker, nullptr);
435
436 ColorManager::Color color;
437 errorCode = pColorPicker->GetHighestSaturationColor(color);
438 HiLog::Info(LABEL_TEST, "get highest saturation color [rgba]=%{public}f,%{public}f,%{public}f,%{public}f", color.r,
439 color.g, color.b, color.a);
440 ASSERT_EQ(errorCode, SUCCESS);
441 bool ret = color.ColorEqual(ColorManager::Color(0.972549f, 0.784314f, 0.0313726f, 1.f));
442 EXPECT_EQ(true, ret);
443 }
444
445 /**
446 * @tc.name: GetAverageColor
447 * @tc.desc: Ensure the ability of creating effect chain from config file.
448 * @tc.type: FUNC
449 * @tc.require:
450 * @tc.author:
451 */
452 HWTEST_F(ColorPickerCommonUnittest, GetAverageColor, TestSize.Level1)
453 {
454 GTEST_LOG_(INFO) << "ColorPickerCommonUnittest GetAverageColor start";
455 size_t bufferSize = 0;
456 uint8_t* buffer = GetJpgBuffer(bufferSize);
457 ASSERT_NE(buffer, nullptr);
458
459 uint32_t errorCode = 0;
460 SourceOptions opts;
461 opts.formatHint = "image/jpeg";
462 std::unique_ptr<ImageSource> imageSource = ImageSource::CreateImageSource(buffer, bufferSize, opts, errorCode);
463 ASSERT_EQ(errorCode, SUCCESS);
464 ASSERT_NE(imageSource.get(), nullptr);
465
466 DecodeOptions decodeOpts;
467 std::unique_ptr<PixelMap> pixmap = imageSource->CreatePixelMap(decodeOpts, errorCode);
468 HiLog::Debug(LABEL_TEST, "create pixel map error code=%{public}u.", errorCode);
469 ASSERT_EQ(errorCode, SUCCESS);
470 ASSERT_NE(pixmap.get(), nullptr);
471
472 std::shared_ptr<ColorPickerCommon> pColorPicker =
473 ColorPickerCommon::CreateColorPicker(std::move(pixmap), errorCode);
474 ASSERT_EQ(errorCode, SUCCESS);
475 EXPECT_NE(pColorPicker, nullptr);
476
477 ColorManager::Color color;
478 errorCode = pColorPicker->GetAverageColor(color);
479 HiLog::Info(LABEL_TEST, "get average color [rgba]=%{public}f,%{public}f,%{public}f,%{public}f", color.r, color.g,
480 color.b, color.a);
481 ASSERT_EQ(errorCode, SUCCESS);
482 bool ret = color.ColorEqual(ColorManager::Color(0.972549f, 0.784314f, 0.0313726f, 1.f));
483 EXPECT_EQ(true, ret);
484 }
485
486 /**
487 * @tc.name: IsBlackOrWhiteOrGrayColor
488 * @tc.desc: Ensure the ability of creating effect chain from config file.
489 * @tc.type: FUNC
490 * @tc.require:
491 * @tc.author:
492 */
493 HWTEST_F(ColorPickerCommonUnittest, IsBlackOrWhiteOrGrayColor, TestSize.Level1)
494 {
495 GTEST_LOG_(INFO) << "ColorPickerCommonUnittest IsBlackOrWhiteOrGrayColor start";
496 size_t bufferSize = 0;
497 uint8_t* buffer = GetJpgBuffer(bufferSize);
498 ASSERT_NE(buffer, nullptr);
499
500 uint32_t errorCode = 0;
501 SourceOptions opts;
502 opts.formatHint = "image/jpeg";
503 std::unique_ptr<ImageSource> imageSource = ImageSource::CreateImageSource(buffer, bufferSize, opts, errorCode);
504 ASSERT_EQ(errorCode, SUCCESS);
505 ASSERT_NE(imageSource.get(), nullptr);
506
507 DecodeOptions decodeOpts;
508 std::unique_ptr<PixelMap> pixmap = imageSource->CreatePixelMap(decodeOpts, errorCode);
509 HiLog::Debug(LABEL_TEST, "create pixel map error code=%{public}u.", errorCode);
510 ASSERT_EQ(errorCode, SUCCESS);
511 ASSERT_NE(pixmap.get(), nullptr);
512
513 std::shared_ptr<ColorPickerCommon> pColorPicker =
514 ColorPickerCommon::CreateColorPicker(std::move(pixmap), errorCode);
515 ASSERT_EQ(errorCode, SUCCESS);
516 EXPECT_NE(pColorPicker, nullptr);
517
518 bool judgeRst = pColorPicker->IsBlackOrWhiteOrGrayColor(0xFFFFFFFF, errorCode);
519 ASSERT_EQ(errorCode, SUCCESS);
520 HiLog::Info(LABEL_TEST, "get largest proportion color result=%{public}d", judgeRst);
521 ASSERT_EQ(judgeRst, true);
522 }
523
524 /**
525 * @tc.name: GetTopProportionColors
526 * @tc.desc: Ensure the ability of creating effect chain from config file.
527 * @tc.type: FUNC
528 * @tc.require:
529 * @tc.author:
530 */
531 HWTEST_F(ColorPickerCommonUnittest, GetTopProportionColors, TestSize.Level1)
532 {
533 GTEST_LOG_(INFO) << "ColorPickerCommonUnittest GetTopProportionColors start";
534 size_t bufferSize = 0;
535 uint8_t* buffer = GetJpgBuffer(bufferSize);
536 ASSERT_NE(buffer, nullptr);
537
538 uint32_t errorCode = 0;
539 SourceOptions opts;
540 opts.formatHint = "image/jpeg";
541 std::unique_ptr<ImageSource> imageSource = ImageSource::CreateImageSource(buffer, bufferSize, opts, errorCode);
542 ASSERT_EQ(errorCode, SUCCESS);
543 ASSERT_NE(imageSource.get(), nullptr);
544
545 DecodeOptions decodeOpts;
546 std::unique_ptr<PixelMap> pixmap = imageSource->CreatePixelMap(decodeOpts, errorCode);
547 HiLog::Debug(LABEL_TEST, "create pixel map error code=%{public}u.", errorCode);
548 ASSERT_EQ(errorCode, SUCCESS);
549 ASSERT_NE(pixmap.get(), nullptr);
550
551 std::shared_ptr<ColorPickerCommon> pColorPicker =
552 ColorPickerCommon::CreateColorPicker(std::move(pixmap), errorCode);
553 ASSERT_EQ(errorCode, SUCCESS);
554 EXPECT_NE(pColorPicker, nullptr);
555
556 std::vector<ColorManager::Color> colors =
557 pColorPicker->GetTopProportionColors(10, errorCode); // the color num limit is 10
558 ASSERT_EQ(errorCode, SUCCESS);
559 HiLog::Info(LABEL_TEST, "get top proportion colors[0][rgba]=%{public}f,%{public}f,%{public}f,%{public}f",
560 colors[0].r, colors[0].g, colors[0].b, colors[0].a);
561 ASSERT_EQ(colors.size(), 1);
562 bool ret =
563 colors[0].ColorEqual(ColorManager::Color(0.972549f, 0.784314f, 0.0313726f, 1.f)); // the top 1 proportion color
564 EXPECT_EQ(true, ret);
565
566 std::vector<ColorManager::Color> colors1 = pColorPicker->GetTopProportionColors(1, errorCode);
567 ASSERT_EQ(errorCode, SUCCESS);
568 HiLog::Info(LABEL_TEST, "get top proportion colors[0][rgba]=%{public}f,%{public}f,%{public}f,%{public}f",
569 colors1[0].r, colors1[0].g, colors1[0].b, colors1[0].a);
570 ASSERT_EQ(colors1.size(), 1);
571 ret =
572 colors1[0].ColorEqual(ColorManager::Color(0.972549f, 0.784314f, 0.0313726f, 1.f)); // the top 1 proportion color
573 EXPECT_EQ(true, ret);
574
575 std::vector<ColorManager::Color> colors2 = pColorPicker->GetTopProportionColors(0, errorCode);
576 ASSERT_EQ(errorCode, SUCCESS);
577 ASSERT_EQ(colors2.size(), 0);
578 }
579
580 } // namespace Rosen
581 } // namespace OHOS