1 /*
2 * Copyright (c) 2023 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 #include <securec.h>
16
17 #include "gtest/gtest.h"
18
19 #define private public
20 #define protected public
21 #include "interfaces/inner_api/drawable_descriptor/drawable_descriptor.h"
22 #include "interfaces/inner_api/drawable_descriptor/image_converter.h"
23 #include "node_extened.h"
24 #include "native_drawable_descriptor.h"
25
26 #include "test/mock/core/pipeline/mock_pipeline_context.h"
27
28 using namespace testing;
29 using namespace testing::ext;
30
31 namespace OHOS::Ace {
32 namespace {
33 constexpr int32_t ID = 1;
34 const uint32_t DENSITY = 0;
35 const uint32_t ICONTYPE = 0;
36 const std::string PATH_NAME = "";
37 } // namespace
38 class DrawableDescriptorTest : public testing::Test {
39 public:
SetUpTestCase()40 static void SetUpTestCase() {};
TearDownTestCase()41 static void TearDownTestCase() {};
42 };
43
44 /**
45 * @tc.name: DrawableDescTest001
46 * @tc.desc: test DrawableDescriptor GetPixelMap when pixMap is empty;
47 * @tc.type: FUNC
48 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest001, TestSize.Level1)49 HWTEST_F(DrawableDescriptorTest, DrawableDescTest001, TestSize.Level1)
50 {
51 Napi::DrawableDescriptor drawableDescriptor;
52 auto res = drawableDescriptor.GetPixelMap();
53 EXPECT_EQ(res, nullptr);
54 ArkUI_DrawableDescriptor *drawDes = OH_ArkUI_CreateFromNapiDrawable(&drawableDescriptor);
55 EXPECT_EQ(drawDes->size, 0);
56 delete drawDes;
57 }
58
59 /**
60 * @tc.name: DrawableDescTest002
61 * @tc.desc: test LayeredDrawableDescriptor's member functions;
62 * @tc.type: FUNC
63 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest002, TestSize.Level1)64 HWTEST_F(DrawableDescriptorTest, DrawableDescTest002, TestSize.Level1)
65 {
66 /**
67 * @tc.steps: step1. create layeredDrawableDescriptor and call GetPixelMap when layeredPixelMap is empty
68 * @tc.expected: return nullptr
69 */
70 std::unique_ptr<uint8_t[]> jsonBuf;
71 size_t len = 0;
72 std::shared_ptr<Global::Resource::ResourceManager> resourceMgr(Global::Resource::CreateResourceManager());
73 auto layeredDrawableDescriptor = Napi::LayeredDrawableDescriptor(std::move(jsonBuf), len, std::move(resourceMgr));
74 auto res = layeredDrawableDescriptor.GetPixelMap();
75 EXPECT_EQ(res, nullptr);
76
77 /**
78 * @tc.steps: step2. call GetForeground when foreground is empty
79 * @tc.expected: return nullptr
80 */
81 auto res2 = layeredDrawableDescriptor.GetForeground();
82 EXPECT_EQ(res2, nullptr);
83
84 /**
85 * @tc.steps: step3. call GetBackground when background is empty
86 * @tc.expected: return nullptr
87 */
88 auto res3 = layeredDrawableDescriptor.GetBackground();
89 EXPECT_EQ(res3, nullptr);
90 }
91
92 /**
93 * @tc.name: ImageConverterTest001
94 * @tc.desc: test ImageConverter's member functions;
95 * @tc.type: FUNC
96 */
HWTEST_F(DrawableDescriptorTest, ImageConverterTest001, TestSize.Level1)97 HWTEST_F(DrawableDescriptorTest, ImageConverterTest001, TestSize.Level1)
98 {
99 /**
100 * @tc.steps: step1. create imageConverter and call PixelFormatToSkColorType
101 * @tc.expected: return rightly
102 */
103 Napi::ImageConverter imageConverter;
104 Media::PixelFormat pixelFormat = Media::PixelFormat::BGRA_8888;
105 auto res = imageConverter.PixelFormatToSkColorType(pixelFormat);
106 EXPECT_EQ(res, SkColorType::kBGRA_8888_SkColorType);
107
108 /**
109 * @tc.steps: step2. call AlphaTypeToSkAlphaType
110 * @tc.expected: return rightly
111 */
112 Media::AlphaType alphaType = Media::AlphaType::IMAGE_ALPHA_TYPE_OPAQUE;
113 auto res2 = imageConverter.AlphaTypeToSkAlphaType(alphaType);
114 EXPECT_EQ(res2, SkAlphaType::kOpaque_SkAlphaType);
115
116 /**
117 * @tc.steps: step3. call BitmapToPixelMap
118 * @tc.expected: function exits normally
119 */
120 Media::InitializationOptions opts;
121 SkBitmap skBitmap;
122 auto bitmap = std::make_shared<SkBitmap>(skBitmap);
123 ASSERT_NE(bitmap, nullptr);
124 auto res4 = imageConverter.BitmapToPixelMap(bitmap, opts);
125 EXPECT_EQ(res4, nullptr);
126 }
127 /**
128 * @tc.name: DrawableDescTest003
129 * @tc.desc: test LayeredDrawableDescriptor::GetMask()
130 * @tc.type: FUNC
131 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest003, TestSize.Level1)132 HWTEST_F(DrawableDescriptorTest, DrawableDescTest003, TestSize.Level1)
133 {
134 /**
135 * @tc.steps: step1. create layeredDrawableDescriptor and call GetMask when layeredPixelMap is empty
136 * @tc.expected: return nullptr
137 */
138 std::unique_ptr<uint8_t[]> jsonBuf;
139 size_t len = 0;
140 std::shared_ptr<Global::Resource::ResourceManager> resourceMgr(Global::Resource::CreateResourceManager());
141 ASSERT_NE(resourceMgr, nullptr);
142 auto layeredDrawableDescriptor = Napi::LayeredDrawableDescriptor(std::move(jsonBuf), len, std::move(resourceMgr));
143 auto res = layeredDrawableDescriptor.GetMask();
144 EXPECT_EQ(res, nullptr);
145 /**
146 * @tc.steps: step2. call GetStaticMaskClipPath
147 * @tc.expected: return rightly
148 */
149 auto str = layeredDrawableDescriptor.GetStaticMaskClipPath();
150 EXPECT_EQ(str, PATH_NAME);
151 }
152
153 /**
154 * @tc.name: DrawableDescTest004
155 * @tc.desc: test DrawableDescriptorFactory::Create()
156 * @tc.type: FUNC
157 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest004, TestSize.Level1)158 HWTEST_F(DrawableDescriptorTest, DrawableDescTest004, TestSize.Level1)
159 {
160 /**
161 * @tc.steps: step1. create DrawableDescriptorFactory and call create when RState is not success
162 * @tc.expected: return nullptr
163 */
164 std::unique_ptr<uint8_t[]> jsonBuf;
165 std::shared_ptr<Global::Resource::ResourceManager> resourceMgr(Global::Resource::CreateResourceManager());
166 ASSERT_NE(resourceMgr, nullptr);
167 Napi::DrawableDescriptorFactory drawableDescriptorFactory;
168 Global::Resource::RState state(Global::Resource::INVALID_FORMAT);
169 Napi::DrawableDescriptor::DrawableType drawableType;
170 auto res = drawableDescriptorFactory.Create(ID, resourceMgr, state, drawableType, DENSITY);
171 EXPECT_EQ(res, nullptr);
172
173 auto res2 = drawableDescriptorFactory.Create(nullptr, resourceMgr, state, drawableType, DENSITY);
174 EXPECT_EQ(res2, nullptr);
175 std::tuple<int32_t, uint32_t, uint32_t> drawableInfo(ID, ICONTYPE, DENSITY);
176 auto res3 = drawableDescriptorFactory.Create(drawableInfo, resourceMgr, state, drawableType);
177 EXPECT_EQ(res3, nullptr);
178
179 std::tuple<const char *, uint32_t, uint32_t> drawableInfoName(nullptr, ICONTYPE, DENSITY);
180 auto res4 = drawableDescriptorFactory.Create(drawableInfoName, resourceMgr, state, drawableType);
181 EXPECT_EQ(res4, nullptr);
182
183 std::pair<std::unique_ptr<uint8_t[]>, size_t> foregroundInfo = { nullptr, 0 };
184 std::pair<std::unique_ptr<uint8_t[]>, size_t> backgroundInfo = { nullptr, 0 };
185 std::string path = "path";
186 auto res5 = drawableDescriptorFactory.Create(foregroundInfo, backgroundInfo, path, drawableType, resourceMgr);
187 ASSERT_NE(res5, nullptr);
188 }
189
190 /**
191 * @tc.name: DrawableDescTest005
192 * @tc.desc: test LayeredDrawableDescriptor's member functions;
193 * @tc.type: FUNC
194 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest005, TestSize.Level1)195 HWTEST_F(DrawableDescriptorTest, DrawableDescTest005, TestSize.Level1)
196 {
197 /**
198 * @tc.steps: step1. create layeredDrawableDescriptor and call SetMaskPath
199 * @tc.expected:return path.
200 */
201 std::unique_ptr<uint8_t[]> jsonBuf;
202 size_t len = 0;
203 std::shared_ptr<Global::Resource::ResourceManager> resourceMgr;
204 std::string path = "path";
205 uint32_t iconType = 1;
206 uint32_t density = 2;
207 auto layeredDrawableDescriptor = Napi::LayeredDrawableDescriptor(
208 std::move(jsonBuf), len, std::move(resourceMgr), path, iconType, density);
209
210 /**
211 * @tc.steps: step2. check
212 */
213 EXPECT_EQ(layeredDrawableDescriptor.maskPath_, path);
214 EXPECT_EQ(layeredDrawableDescriptor.iconType_, iconType);
215 EXPECT_EQ(layeredDrawableDescriptor.density_, density);
216 }
217
218 /**
219 * @tc.name: DrawableDescTest006
220 * @tc.desc: test LayeredDrawableDescriptor's member functions;
221 * @tc.type: FUNC
222 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest006, TestSize.Level1)223 HWTEST_F(DrawableDescriptorTest, DrawableDescTest006, TestSize.Level1)
224 {
225 /**
226 * @tc.steps: step1. create layeredDrawableDescriptor and call SetMaskPath
227 * @tc.expected:return path.
228 */
229 std::unique_ptr<uint8_t[]> jsonBuf;
230 size_t len = 0;
231 std::shared_ptr<Global::Resource::ResourceManager> resourceMgr;
232 std::string path = "path";
233 uint32_t iconType = 1;
234 uint32_t density = 2;
235 auto layeredDrawableDescriptor =
236 Napi::LayeredDrawableDescriptor(std::move(jsonBuf), len, std::move(resourceMgr), path, iconType, density);
237
238 /**
239 * @tc.steps: step2. check
240 */
241 std::pair<std::unique_ptr<uint8_t[]>, size_t> foregroundInfo = { nullptr, 0 };
242 std::pair<std::unique_ptr<uint8_t[]>, size_t> backgroundInfo = { nullptr, 0 };
243 layeredDrawableDescriptor.InitLayeredParam(foregroundInfo, backgroundInfo);
244
245 /**
246 * @tc.steps: step2. check
247 */
248 EXPECT_EQ(layeredDrawableDescriptor.foreground_, std::nullopt);
249 EXPECT_EQ(layeredDrawableDescriptor.background_, std::nullopt);
250 }
251
252 /**
253 * @tc.name: DrawableDescTest007
254 * @tc.desc: test DrawableDescriptor's member functions;
255 * @tc.type: FUNC
256 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest007, TestSize.Level1)257 HWTEST_F(DrawableDescriptorTest, DrawableDescTest007, TestSize.Level1)
258 {
259 /**
260 * @tc.steps: step1. create DrawableDescriptor and call GetDrawableType()
261 * @tc.expected:return BASE.
262 */
263 Napi::DrawableDescriptor drawableDescriptor;
264 auto res = drawableDescriptor.GetDrawableType();
265 EXPECT_EQ(res, Napi::DrawableDescriptor::DrawableType::BASE);
266 }
267
268 /**
269 * @tc.name: DrawableDescTest008
270 * @tc.desc: test LayeredDrawableDescriptor's member functions;
271 * @tc.type: FUNC
272 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest008, TestSize.Level1)273 HWTEST_F(DrawableDescriptorTest, DrawableDescTest008, TestSize.Level1)
274 {
275 /**
276 * @tc.steps: step1. create layeredDrawableDescriptor and call GetDrawableType()
277 * @tc.expected:return LAYERED.
278 */
279 std::unique_ptr<uint8_t[]> jsonBuf;
280 size_t len = 0;
281 std::shared_ptr<Global::Resource::ResourceManager> resourceMgr;
282 std::string path = "path";
283 uint32_t iconType = 1;
284 uint32_t density = 2;
285 auto layeredDrawableDescriptor = Napi::LayeredDrawableDescriptor(
286 std::move(jsonBuf), len, std::move(resourceMgr), path, iconType, density);
287
288 /**
289 * @tc.steps: step2. check
290 */
291 auto res = layeredDrawableDescriptor.GetDrawableType();
292 EXPECT_EQ(res, Napi::DrawableDescriptor::DrawableType::LAYERED);
293 }
294
295 /**
296 * @tc.name: DrawableDescTest009
297 * @tc.desc: test AnimatedDrawableDescriptor's member functions;
298 * @tc.type: FUNC
299 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest009, TestSize.Level1)300 HWTEST_F(DrawableDescriptorTest, DrawableDescTest009, TestSize.Level1)
301 {
302 /**
303 * @tc.steps: step1. create AnimatedDrawableDescriptor and call GetDrawableType()
304 * @tc.expected:return ANIMATED.
305 */
306 std::vector<std::shared_ptr<Media::PixelMap>> pixelMaps;
307 int32_t duration = -1;
308 int32_t iterations = 2;
309 auto* animatedDrawable = new Napi::AnimatedDrawableDescriptor(pixelMaps, duration, iterations);
310 auto res = animatedDrawable->GetDrawableType();
311 EXPECT_EQ(res, Napi::DrawableDescriptor::DrawableType::ANIMATED);
312
313 /**
314 * @tc.steps: step2. call GetPixelMap()
315 * @tc.expected:return nullptr.
316 */
317 auto pixelMap = animatedDrawable->GetPixelMap();
318 EXPECT_EQ(pixelMap, nullptr);
319
320 /**
321 * @tc.steps: step3. call GetPixelMapList()
322 * @tc.expected: pixelMaps.size().
323 */
324 auto pixelMapList = animatedDrawable->GetPixelMapList();
325 EXPECT_EQ(pixelMapList.size(), pixelMaps.size());
326
327 /**
328 * @tc.steps: step4. create AnimatedDrawableDescriptor and call GetDuration()
329 * @tc.expected:return 1000.
330 */
331 duration = 1000;
332 iterations = 1;
333 animatedDrawable = new Napi::AnimatedDrawableDescriptor(pixelMaps, duration, iterations);
334 EXPECT_EQ(animatedDrawable->GetDuration(), 1000);
335
336 /**
337 * @tc.steps: step5. create AnimatedDrawableDescriptor and call GetIterations()
338 * @tc.expected:return 2.
339 */
340 EXPECT_EQ(animatedDrawable->GetIterations(), 1);
341
342 /**
343 * @tc.steps: step6. create AnimatedDrawableDescriptor and call GetDuration()
344 * @tc.expected:return 0.
345 */
346 duration = -1;
347 iterations = -2;
348 animatedDrawable = new Napi::AnimatedDrawableDescriptor(pixelMaps, duration, iterations);
349 EXPECT_EQ(animatedDrawable->GetDuration(), 0);
350
351 /**
352 * @tc.steps: step7. create AnimatedDrawableDescriptor and call GetIterations()
353 * @tc.expected:return 1.
354 */
355 EXPECT_EQ(animatedDrawable->GetIterations(), 1);
356 }
357
358 /**
359 * @tc.name: DrawableDescTest0010
360 * @tc.desc: test LayeredDrawableDescriptor's member functions;
361 * @tc.type: FUNC
362 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest0010, TestSize.Level1)363 HWTEST_F(DrawableDescriptorTest, DrawableDescTest0010, TestSize.Level1)
364 {
365 /**
366 * @tc.steps: step1. create layeredDrawableDescriptor and init mask only
367 */
368 std::shared_ptr<Global::Resource::ResourceManager> resMgr(Global::Resource::CreateResourceManager());
369 auto layeredDrawable = Napi::LayeredDrawableDescriptor();
370 /**
371 * @tc.steps: step2. init resource name and data
372 */
373 layeredDrawable.InitialMask(resMgr);
374 /**
375 * @tc.steps: step2. check creating mask ok
376 */
377 EXPECT_FALSE(layeredDrawable.GetDefaultMask());
378 }
379
380 /**
381 * @tc.name: DrawableDescTest0011
382 * @tc.desc: test LayeredDrawableDescriptor's member functions;
383 * @tc.type: FUNC
384 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest0011, TestSize.Level1)385 HWTEST_F(DrawableDescriptorTest, DrawableDescTest0011, TestSize.Level1)
386 {
387 /**
388 * @tc.steps: step1. pixelMap param not exist in pixelMapDrawable
389 */
390 std::shared_ptr<Global::Resource::ResourceManager> resMgr(Global::Resource::CreateResourceManager());
391 auto layeredDrawable = Napi::LayeredDrawableDescriptor();
392 /**
393 * @tc.steps: step2. init resource name and data
394 */
395 layeredDrawable.InitialMask(resMgr);
396 /**
397 * @tc.steps: step3. update foreground into layeredDrawable
398 */
399 std::shared_ptr<Media::PixelMap> foreground = std::make_shared<Media::PixelMap>();
400 layeredDrawable.SetForeground(foreground);
401 layeredDrawable.layeredPixelMap_ = foreground;
402 auto composedResult = layeredDrawable.GetPixelMap();
403 /**
404 * @tc.steps: step3. check pixelMap should not be null since this layeredDrawable is customized
405 */
406 EXPECT_EQ(composedResult, nullptr);
407 }
408
409 /**
410 * @tc.name: DrawableDescTest0012
411 * @tc.desc: test LayeredDrawableDescriptor's member functions;
412 * @tc.type: FUNC
413 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest0012, TestSize.Level1)414 HWTEST_F(DrawableDescriptorTest, DrawableDescTest0012, TestSize.Level1)
415 {
416 /**
417 * @tc.steps: step1. init drawble
418 */
419 auto layeredDrawable = Napi::LayeredDrawableDescriptor();
420 /**
421 * @tc.steps: step2. get pixelMap directly from layeredDrawable
422 */
423 auto composedResult = layeredDrawable.GetPixelMap();
424 /**
425 * @tc.steps: step3. check pixelMap should be null since this layeredDrawable is not customized
426 * therefore foreground, background does not exist when create
427 */
428 EXPECT_EQ(composedResult, nullptr);
429 }
430
431 /**
432 * @tc.name: DrawableDescTest0013
433 * @tc.desc: test LayeredDrawableDescriptor's member functions;
434 * @tc.type: FUNC
435 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest0013, TestSize.Level1)436 HWTEST_F(DrawableDescriptorTest, DrawableDescTest0013, TestSize.Level1)
437 {
438 /**
439 * @tc.steps: step1. init layeredDrawble
440 */
441 auto drawable = Napi::DrawableDescriptor();
442
443 /**
444 * @tc.steps: step2. set pixelMap to drawable
445 */
446 drawable.SetPixelMap(std::make_shared<Media::PixelMap>());
447 /**
448 * @tc.steps: step3. check drawable has pixelMap
449 */
450 EXPECT_TRUE(drawable.HasPixelMap());
451 drawable.ResetPixelMap();
452 EXPECT_FALSE(drawable.HasPixelMap());
453 }
454
455 /**
456 * @tc.name: DrawableDescTest0014
457 * @tc.desc: test LayeredDrawableDescriptor's member functions;
458 * @tc.type: FUNC
459 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest0014, TestSize.Level1)460 HWTEST_F(DrawableDescriptorTest, DrawableDescTest0014, TestSize.Level1)
461 {
462 /**
463 * @tc.steps: step1. init layeredDrawble
464 */
465 auto drawable = Napi::LayeredDrawableDescriptor();
466
467 /**
468 * @tc.steps: step2. set param to layeredDrawable
469 */
470 drawable.SetForeground(std::make_shared<Media::PixelMap>());
471 drawable.SetBackground(std::make_shared<Media::PixelMap>());
472 drawable.SetMask(std::make_shared<Media::PixelMap>());
473 /**
474 * @tc.steps: step3. check layeredDrawable is customized
475 */
476 EXPECT_TRUE(drawable.Customized());
477 }
478
479 /**
480 * @tc.name: DrawableDescTest0015
481 * @tc.desc: test AnimatedDrawableDescriptor's member functions;
482 * @tc.type: FUNC
483 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest0015, TestSize.Level1)484 HWTEST_F(DrawableDescriptorTest, DrawableDescTest0015, TestSize.Level1)
485 {
486 /**
487 * @tc.steps: step1. create AnimatedDrawableDescriptor
488 * @tc.expected:return ANIMATED.
489 */
490 std::vector<std::shared_ptr<Media::PixelMap>> pixelMaps;
491 int32_t duration = -1;
492 int32_t iterations = 2;
493 auto* animatedDrawable = new Napi::AnimatedDrawableDescriptor(pixelMaps, duration, iterations);
494 auto res = animatedDrawable->GetDrawableType();
495 EXPECT_EQ(res, Napi::DrawableDescriptor::DrawableType::ANIMATED);
496
497 /**
498 * @tc.steps: step2. set value
499 */
500 animatedDrawable->SetDuration(1000);
501
502 /**
503 * @tc.steps: step3. check duration should be the value set.
504 */
505 EXPECT_EQ(animatedDrawable->GetDuration(), 1000);
506 }
507
508 /**
509 * @tc.name: DrawableDescTest0016
510 * @tc.desc: test AnimatedDrawableDescriptor's member functions;
511 * @tc.type: FUNC
512 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest0016, TestSize.Level1)513 HWTEST_F(DrawableDescriptorTest, DrawableDescTest0016, TestSize.Level1)
514 {
515 /**
516 * @tc.steps: step1. create AnimatedDrawableDescriptor
517 * @tc.expected:return ANIMATED.
518 */
519 std::vector<std::shared_ptr<Media::PixelMap>> pixelMaps;
520 int32_t duration = -1;
521 int32_t iterations = 2;
522 auto* animatedDrawable = new Napi::AnimatedDrawableDescriptor(pixelMaps, duration, iterations);
523 auto res = animatedDrawable->GetDrawableType();
524 EXPECT_EQ(res, Napi::DrawableDescriptor::DrawableType::ANIMATED);
525
526 /**
527 * @tc.steps: step2. set value
528 */
529 animatedDrawable->SetIterations(1);
530
531 /**
532 * @tc.steps: step3. check duration should be the value set.
533 */
534 EXPECT_EQ(animatedDrawable->GetIterations(), 1);
535 }
536
537 /**
538 * @tc.name: DrawableDescTest0017
539 * @tc.desc: test LayeredDrawableDescriptor's member functions;
540 * @tc.type: FUNC
541 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest0017, TestSize.Level1)542 HWTEST_F(DrawableDescriptorTest, DrawableDescTest0017, TestSize.Level1)
543 {
544 /**
545 * @tc.steps: step1. init layeredDrawable
546 */
547 auto drawable = Napi::LayeredDrawableDescriptor();
548
549 /**
550 * @tc.steps: step2. set param to layeredDrawable
551 */
552 std::shared_ptr<Media::PixelMap> layeredPixelMap;
553 std::shared_ptr<Media::PixelMap> badgedPixelMap;
554 std::shared_ptr<Media::PixelMap> compositePixelMap;
555 bool ret = drawable.GetCompositePixelMapWithBadge(layeredPixelMap, badgedPixelMap, compositePixelMap);
556 /**
557 * @tc.steps: step3. check layeredDrawable result
558 */
559 EXPECT_FALSE(ret);
560 }
561
562 /**
563 * @tc.name: DrawableDescTest0018
564 * @tc.desc: test LayeredDrawableDescriptor's member functions;
565 * @tc.type: FUNC
566 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest0018, TestSize.Level1)567 HWTEST_F(DrawableDescriptorTest, DrawableDescTest0018, TestSize.Level1)
568 {
569 auto drawable = Napi::LayeredDrawableDescriptor();;
570 const char* item = "item";
571 Napi::DrawableItem resItem = drawable.PreGetDrawableItem(nullptr, item);
572 EXPECT_EQ(resItem.len_, 0);
573 const char* item1 = "";
574 resItem = drawable.PreGetDrawableItem(nullptr, item1);
575 EXPECT_EQ(resItem.len_, 0);
576 }
577
578 /**
579 * @tc.name: DrawableDescTest0019
580 * @tc.desc: test LayeredDrawableDescriptor's member functions;
581 * @tc.type: FUNC
582 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest0019, TestSize.Level1)583 HWTEST_F(DrawableDescriptorTest, DrawableDescTest0019, TestSize.Level1)
584 {
585 auto drawable = Napi::LayeredDrawableDescriptor();
586 const char* jsonBuf = "{\"background\":\"background\"}";
587 drawable.len_ = strlen(jsonBuf) + 1;
588 drawable.jsonBuf_ = std::make_unique<uint8_t[]>(drawable.len_);
589 memcpy_s(drawable.jsonBuf_.get(), strlen(jsonBuf), jsonBuf, strlen(jsonBuf));
590 auto ret = drawable.PreGetPixelMapFromJsonBuf(nullptr, true);
591 EXPECT_FALSE(ret);
592 }
593
594 /**
595 * @tc.name: DrawableDescTest0020
596 * @tc.desc: test LayeredDrawableDescriptor's member functions;
597 * @tc.type: FUNC
598 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest0020, TestSize.Level1)599 HWTEST_F(DrawableDescriptorTest, DrawableDescTest0020, TestSize.Level1)
600 {
601 auto drawable = Napi::LayeredDrawableDescriptor();
602 const char* jsonBuf = "{\"foreground\": \"foreground\"}";
603 drawable.len_ = strlen(jsonBuf) + 1;
604 drawable.jsonBuf_ = std::make_unique<uint8_t[]>(drawable.len_);
605 memcpy_s(drawable.jsonBuf_.get(), strlen(jsonBuf), jsonBuf, strlen(jsonBuf));
606 auto ret = drawable.PreGetPixelMapFromJsonBuf(nullptr, false);
607 EXPECT_FALSE(ret);
608 }
609
610 /**
611 * @tc.name: DrawableDescTest0021
612 * @tc.desc: test LayeredDrawableDescriptor's member functions;
613 * @tc.type: FUNC
614 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest0021, TestSize.Level1)615 HWTEST_F(DrawableDescriptorTest, DrawableDescTest0021, TestSize.Level1)
616 {
617 auto drawable = Napi::LayeredDrawableDescriptor();
618 auto ret = drawable.GetPixelMapFromBuffer();
619 EXPECT_FALSE(ret);
620 }
621
622 /**
623 * @tc.name: DrawableDescTest0022
624 * @tc.desc: test LayeredDrawableDescriptor's member functions;
625 * @tc.type: FUNC
626 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest0022, TestSize.Level1)627 HWTEST_F(DrawableDescriptorTest, DrawableDescTest0022, TestSize.Level1)
628 {
629 auto drawable = Napi::LayeredDrawableDescriptor();
630 Napi::DrawableItem drawableItem;
631 drawableItem.state_ = Global::Resource::ERROR;
632 uint32_t errorCode = 0;
633 auto ret = drawable.CreateImageSource(drawableItem, errorCode);
634 EXPECT_TRUE(ret == nullptr);
635 drawableItem.state_ = Global::Resource::SUCCESS;
636 ret = drawable.CreateImageSource(drawableItem, errorCode);
637 EXPECT_TRUE(ret == nullptr);
638 }
639
640 /**
641 * @tc.name: DrawableDescTest0023
642 * @tc.desc: test LayeredDrawableDescriptor's member functions;
643 * @tc.type: FUNC
644 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest0023, TestSize.Level1)645 HWTEST_F(DrawableDescriptorTest, DrawableDescTest0023, TestSize.Level1)
646 {
647 auto drawable = Napi::LayeredDrawableDescriptor();
648 drawable.backgroundItem_.state_ = Global::Resource::SUCCESS;
649 auto ret = drawable.GetPixelMapFromJsonBuf(true);
650 EXPECT_FALSE(ret);
651 }
652
653 /**
654 * @tc.name: DrawableDescTest0024
655 * @tc.desc: test LayeredDrawableDescriptor's member functions;
656 * @tc.type: FUNC
657 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest0024, TestSize.Level1)658 HWTEST_F(DrawableDescriptorTest, DrawableDescTest0024, TestSize.Level1)
659 {
660 auto drawable = Napi::LayeredDrawableDescriptor();
661 drawable.maskPath_ = "/data/local/tmp/image/123.jpg";
662 auto ret = drawable.GetMaskByPath();
663 EXPECT_FALSE(ret);
664 std::shared_ptr<Media::PixelMap> pixelMap = std::make_shared<Media::PixelMap>();
665 drawable.mask_ = pixelMap;
666 ret = drawable.GetMaskByPath();
667 EXPECT_FALSE(ret);
668 }
669
670 /**
671 * @tc.name: DrawableDescTest0025
672 * @tc.desc: test LayeredDrawableDescriptor's member functions;
673 * @tc.type: FUNC
674 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest0025, TestSize.Level1)675 HWTEST_F(DrawableDescriptorTest, DrawableDescTest0025, TestSize.Level1)
676 {
677 auto drawable = Napi::LayeredDrawableDescriptor();
678 std::shared_ptr<Global::Resource::ResourceManager> resourceMgr(Global::Resource::CreateResourceManager());
679 auto ret = drawable.GetMaskByName(resourceMgr, "name");
680 EXPECT_FALSE(ret);
681 }
682
683 /**
684 * @tc.name: DrawableDescTest0026
685 * @tc.desc: test LayeredDrawableDescriptor's member functions;
686 * @tc.type: FUNC
687 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest0026, TestSize.Level1)688 HWTEST_F(DrawableDescriptorTest, DrawableDescTest0026, TestSize.Level1)
689 {
690 auto drawable = Napi::LayeredDrawableDescriptor();
691 std::shared_ptr<Media::PixelMap> pixelMap = std::make_shared<Media::PixelMap>();
692 drawable.foreground_ = pixelMap;
693 auto ret = drawable.GetForeground();
694 EXPECT_FALSE(ret == nullptr);
695 }
696
697 /**
698 * @tc.name: DrawableDescTest0027
699 * @tc.desc: test LayeredDrawableDescriptor's member functions;
700 * @tc.type: FUNC
701 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest0027, TestSize.Level1)702 HWTEST_F(DrawableDescriptorTest, DrawableDescTest0027, TestSize.Level1)
703 {
704 auto drawable = Napi::LayeredDrawableDescriptor();
705 std::shared_ptr<Media::PixelMap> pixelMap = std::make_shared<Media::PixelMap>();
706 drawable.background_ = pixelMap;
707 auto ret = drawable.GetBackground();
708 EXPECT_FALSE(ret == nullptr);
709 }
710
711 /**
712 * @tc.name: DrawableDescTest0028
713 * @tc.desc: test LayeredDrawableDescriptor's member functions;
714 * @tc.type: FUNC
715 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest0028, TestSize.Level1)716 HWTEST_F(DrawableDescriptorTest, DrawableDescTest0028, TestSize.Level1)
717 {
718 auto drawable = Napi::LayeredDrawableDescriptor();
719 std::shared_ptr<Media::PixelMap> pixelMap = std::make_shared<Media::PixelMap>();
720 drawable.mask_ = pixelMap;
721 auto ret = drawable.GetMask();
722 EXPECT_FALSE(ret == nullptr);
723 }
724
725 /**
726 * @tc.name: DrawableDescTest0029
727 * @tc.desc: test LayeredDrawableDescriptor's member functions;
728 * @tc.type: FUNC
729 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest0029, TestSize.Level1)730 HWTEST_F(DrawableDescriptorTest, DrawableDescTest0029, TestSize.Level1)
731 {
732 auto drawable = Napi::LayeredDrawableDescriptor();
733 std::shared_ptr<Media::PixelMap> pixelMap = std::make_shared<Media::PixelMap>();
734 drawable.layeredPixelMap_ = pixelMap;
735 auto ret = drawable.GetPixelMap();
736 EXPECT_FALSE(ret == nullptr);
737 }
738
739 /**
740 * @tc.name: DrawableDescTest0030
741 * @tc.desc: test AnimatedDrawableDescriptor's member functions;
742 * @tc.type: FUNC
743 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest0030, TestSize.Level1)744 HWTEST_F(DrawableDescriptorTest, DrawableDescTest0030, TestSize.Level1)
745 {
746 std::vector<std::shared_ptr<Media::PixelMap>> pixelMaps;
747 int32_t duration = -1;
748 int32_t iterations = 2;
749 auto* animatedDrawable = new Napi::AnimatedDrawableDescriptor(pixelMaps, duration, iterations);
750 auto ret = animatedDrawable->GetDuration();
751 EXPECT_EQ(ret, 0);
752 animatedDrawable->duration_ = 10;
753 ret = animatedDrawable->GetDuration();
754 EXPECT_EQ(ret, 10);
755 ret = animatedDrawable->GetIterations();
756 EXPECT_EQ(ret, 2);
757 animatedDrawable->iterations_ = -10;
758 ret = animatedDrawable->GetIterations();
759 EXPECT_EQ(ret, 1);
760 }
761
762 /**
763 * @tc.name: DrawableDescTest031
764 * @tc.desc: test AnimatedDrawableDescriptor's member functions;
765 * @tc.type: FUNC
766 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest031, TestSize.Level1)767 HWTEST_F(DrawableDescriptorTest, DrawableDescTest031, TestSize.Level1)
768 {
769 /**
770 * @tc.steps: step1. create AnimatedDrawableDescriptor
771 * @tc.expected:return ANIMATED.
772 */
773 std::vector<std::shared_ptr<Media::PixelMap>> pixelMaps;
774 int32_t duration = -1;
775 int32_t iterations = 2;
776 auto* animatedDrawable = new Napi::AnimatedDrawableDescriptor(pixelMaps, duration, iterations);
777 auto res = animatedDrawable->GetPixelMap();
778 EXPECT_EQ(res, nullptr);
779 }
780
781 /**
782 * @tc.name: DrawableDescTest0031
783 * @tc.desc: test LayeredDrawableDescriptor's member functions;
784 * @tc.type: FUNC
785 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest0031, TestSize.Level1)786 HWTEST_F(DrawableDescriptorTest, DrawableDescTest0031, TestSize.Level1)
787 {
788 std::shared_ptr<Global::Resource::ResourceManager> resourceMgr(Global::Resource::CreateResourceManager());
789 auto drawable = Napi::LayeredDrawableDescriptor();
790 const char* item = "i:1";
791 Napi::DrawableItem resItem = drawable.PreGetDrawableItem(resourceMgr, item);
792 EXPECT_EQ(resItem.len_, 0);
793 }
794
795 /**
796 * @tc.name: DrawableDescTest0032
797 * @tc.desc: test AnimatedDrawableDescriptor's member functions;
798 * @tc.type: FUNC
799 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest0032, TestSize.Level1)800 HWTEST_F(DrawableDescriptorTest, DrawableDescTest0032, TestSize.Level1)
801 {
802 std::vector<std::shared_ptr<Media::PixelMap>> pixelMaps;
803 int32_t duration = -1;
804 int32_t iterations = 2;
805 auto* animatedDrawable = new Napi::AnimatedDrawableDescriptor(pixelMaps, duration, iterations);
806 animatedDrawable->SetIterations(-2);
807 EXPECT_EQ(animatedDrawable->iterations_, 1);
808 }
809
810 /**
811 * @tc.name: DrawableDescTest0033
812 * @tc.desc: test AnimatedDrawableDescriptor's member functions;
813 * @tc.type: FUNC
814 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest0033, TestSize.Level1)815 HWTEST_F(DrawableDescriptorTest, DrawableDescTest0033, TestSize.Level1)
816 {
817 std::vector<std::shared_ptr<Media::PixelMap>> pixelMaps;
818 int32_t duration = -1;
819 int32_t iterations = 2;
820 auto* animatedDrawable = new Napi::AnimatedDrawableDescriptor(pixelMaps, duration, iterations);
821 animatedDrawable->SetDuration(0);
822 EXPECT_EQ(animatedDrawable->duration_, 0);
823 }
824 } // namespace OHOS::Ace
825