• 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 
16 #include "gtest/gtest.h"
17 
18 #define private public
19 #define protected public
20 #include "interfaces/inner_api/drawable_descriptor/drawable_descriptor.h"
21 #include "interfaces/inner_api/drawable_descriptor/image_converter.h"
22 
23 #include "test/mock/core/pipeline/mock_pipeline_context.h"
24 
25 using namespace testing;
26 using namespace testing::ext;
27 
28 namespace OHOS::Ace {
29 namespace {
30 constexpr int32_t ID = 1;
31 const uint32_t DENSITY = 0;
32 const uint32_t ICONTYPE = 0;
33 const std::string PATH_NAME = "";
34 } // namespace
35 class DrawableDescriptorTest : public testing::Test {
36 public:
SetUpTestCase()37     static void SetUpTestCase() {};
TearDownTestCase()38     static void TearDownTestCase() {};
39 };
40 
41 /**
42  * @tc.name: DrawableDescTest001
43  * @tc.desc: test DrawableDescriptor GetPixelMap when pixMap is empty;
44  * @tc.type: FUNC
45  */
46 HWTEST_F(DrawableDescriptorTest, DrawableDescTest001, TestSize.Level1)
47 {
48     Napi::DrawableDescriptor drawableDescriptor;
49     auto res = drawableDescriptor.GetPixelMap();
50     EXPECT_EQ(res, nullptr);
51 }
52 
53 /**
54  * @tc.name: DrawableDescTest002
55  * @tc.desc: test LayeredDrawableDescriptor's member functions;
56  * @tc.type: FUNC
57  */
58 HWTEST_F(DrawableDescriptorTest, DrawableDescTest002, TestSize.Level1)
59 {
60     /**
61      * @tc.steps: step1. create layeredDrawableDescriptor and call GetPixelMap when layeredPixelMap is empty
62      * @tc.expected: return nullptr
63      */
64     std::unique_ptr<uint8_t[]> jsonBuf;
65     size_t len = 0;
66     std::shared_ptr<Global::Resource::ResourceManager> resourceMgr(Global::Resource::CreateResourceManager());
67     auto layeredDrawableDescriptor = Napi::LayeredDrawableDescriptor(std::move(jsonBuf), len, std::move(resourceMgr));
68     auto res = layeredDrawableDescriptor.GetPixelMap();
69     EXPECT_EQ(res, nullptr);
70 
71     /**
72      * @tc.steps: step2. call GetForeground when foreground is empty
73      * @tc.expected: return nullptr
74      */
75     auto res2 = layeredDrawableDescriptor.GetForeground();
76     EXPECT_EQ(res2, nullptr);
77 
78     /**
79      * @tc.steps: step3. call GetBackground when background is empty
80      * @tc.expected: return nullptr
81      */
82     auto res3 = layeredDrawableDescriptor.GetBackground();
83     EXPECT_EQ(res3, nullptr);
84 }
85 
86 /**
87  * @tc.name: ImageConverterTest001
88  * @tc.desc: test ImageConverter's member functions;
89  * @tc.type: FUNC
90  */
91 HWTEST_F(DrawableDescriptorTest, ImageConverterTest001, TestSize.Level1)
92 {
93     /**
94      * @tc.steps: step1. create imageConverter and call PixelFormatToSkColorType
95      * @tc.expected: return rightly
96      */
97     Napi::ImageConverter imageConverter;
98     Media::PixelFormat pixelFormat = Media::PixelFormat::BGRA_8888;
99     auto res = imageConverter.PixelFormatToSkColorType(pixelFormat);
100     EXPECT_EQ(res, SkColorType::kBGRA_8888_SkColorType);
101 
102     /**
103      * @tc.steps: step2. call AlphaTypeToSkAlphaType
104      * @tc.expected: return rightly
105      */
106     Media::AlphaType alphaType = Media::AlphaType::IMAGE_ALPHA_TYPE_OPAQUE;
107     auto res2 = imageConverter.AlphaTypeToSkAlphaType(alphaType);
108     EXPECT_EQ(res2, SkAlphaType::kOpaque_SkAlphaType);
109 
110     /**
111      * @tc.steps: step3. call BitmapToPixelMap
112      * @tc.expected: function exits normally
113      */
114     Media::InitializationOptions opts;
115     SkBitmap skBitmap;
116     auto bitmap = std::make_shared<SkBitmap>(skBitmap);
117     ASSERT_NE(bitmap, nullptr);
118     auto res4 = imageConverter.BitmapToPixelMap(bitmap, opts);
119     EXPECT_EQ(res4, nullptr);
120 }
121 /**
122  * @tc.name: DrawableDescTest003
123  * @tc.desc: test LayeredDrawableDescriptor::GetMask()
124  * @tc.type: FUNC
125  */
126 HWTEST_F(DrawableDescriptorTest, DrawableDescTest003, TestSize.Level1)
127 {
128     /**
129      * @tc.steps: step1. create layeredDrawableDescriptor and call GetMask when layeredPixelMap is empty
130      * @tc.expected: return nullptr
131      */
132     std::unique_ptr<uint8_t[]> jsonBuf;
133     size_t len = 0;
134     std::shared_ptr<Global::Resource::ResourceManager> resourceMgr(Global::Resource::CreateResourceManager());
135     ASSERT_NE(resourceMgr, nullptr);
136     auto layeredDrawableDescriptor = Napi::LayeredDrawableDescriptor(std::move(jsonBuf), len, std::move(resourceMgr));
137     auto res = layeredDrawableDescriptor.GetMask();
138     EXPECT_EQ(res, nullptr);
139     /**
140      * @tc.steps: step2. call GetStaticMaskClipPath
141      * @tc.expected: return rightly
142      */
143     auto str = layeredDrawableDescriptor.GetStaticMaskClipPath();
144     EXPECT_EQ(str, PATH_NAME);
145 }
146 
147 /**
148  * @tc.name: DrawableDescTest004
149  * @tc.desc: test DrawableDescriptorFactory::Create()
150  * @tc.type: FUNC
151  */
152 HWTEST_F(DrawableDescriptorTest, DrawableDescTest004, TestSize.Level1)
153 {
154     /**
155      * @tc.steps: step1. create DrawableDescriptorFactory and call create when RState is not success
156      * @tc.expected: return nullptr
157      */
158     std::unique_ptr<uint8_t[]> jsonBuf;
159     std::shared_ptr<Global::Resource::ResourceManager> resourceMgr(Global::Resource::CreateResourceManager());
160     ASSERT_NE(resourceMgr, nullptr);
161     Napi::DrawableDescriptorFactory drawableDescriptorFactory;
162     Global::Resource::RState state(Global::Resource::INVALID_FORMAT);
163     Napi::DrawableDescriptor::DrawableType drawableType;
164     auto res = drawableDescriptorFactory.Create(ID, resourceMgr, state, drawableType, DENSITY);
165     EXPECT_EQ(res, nullptr);
166 
167     auto res2 = drawableDescriptorFactory.Create(nullptr, resourceMgr, state, drawableType, DENSITY);
168     EXPECT_EQ(res2, nullptr);
169     std::tuple<int32_t, uint32_t, uint32_t> drawableInfo(ID, ICONTYPE, DENSITY);
170     auto res3 = drawableDescriptorFactory.Create(drawableInfo, resourceMgr, state, drawableType);
171     EXPECT_EQ(res3, nullptr);
172 
173     std::tuple<const char *, uint32_t, uint32_t> drawableInfoName(nullptr, ICONTYPE, DENSITY);
174     auto res4 = drawableDescriptorFactory.Create(drawableInfoName, resourceMgr, state, drawableType);
175     EXPECT_EQ(res4, nullptr);
176 }
177 
178 /**
179  * @tc.name: DrawableDescTest005
180  * @tc.desc: test LayeredDrawableDescriptor's member functions;
181  * @tc.type: FUNC
182  */
183 HWTEST_F(DrawableDescriptorTest, DrawableDescTest005, TestSize.Level1)
184 {
185     /**
186      * @tc.steps: step1. create layeredDrawableDescriptor and call SetMaskPath
187      * @tc.expected:return path.
188      */
189     std::unique_ptr<uint8_t[]> jsonBuf;
190     size_t len = 0;
191     std::shared_ptr<Global::Resource::ResourceManager> resourceMgr;
192     std::string path = "path";
193     uint32_t iconType = 1;
194     uint32_t density = 2;
195     auto layeredDrawableDescriptor = Napi::LayeredDrawableDescriptor(
196         std::move(jsonBuf), len, std::move(resourceMgr), path, iconType, density);
197 
198     /**
199      * @tc.steps: step2. check
200      */
201     EXPECT_EQ(layeredDrawableDescriptor.maskPath_, path);
202     EXPECT_EQ(layeredDrawableDescriptor.iconType_, iconType);
203     EXPECT_EQ(layeredDrawableDescriptor.density_, density);
204 }
205 } // namespace OHOS::Ace