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