• 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 "core/pipeline_ng/test/mock/mock_pipeline_base.h"
24 
25 using namespace testing;
26 using namespace testing::ext;
27 
28 namespace OHOS::Ace {
29 namespace {} // namespace
30 class DrawableDescriptorTest : public testing::Test {
31 public:
SetUpTestCase()32     static void SetUpTestCase() {};
TearDownTestCase()33     static void TearDownTestCase() {};
34 };
35 
36 /**
37  * @tc.name: DrawableDescTest001
38  * @tc.desc: test DrawableDescriptor GetPixelMap when pixMap is empty;
39  * @tc.type: FUNC
40  */
41 HWTEST_F(DrawableDescriptorTest, DrawableDescTest001, TestSize.Level1)
42 {
43     Napi::DrawableDescriptor drawableDescriptor;
44     auto res = drawableDescriptor.GetPixelMap();
45     EXPECT_EQ(res, nullptr);
46 }
47 
48 /**
49  * @tc.name: DrawableDescTest002
50  * @tc.desc: test LayeredDrawableDescriptor's member functions;
51  * @tc.type: FUNC
52  */
53 HWTEST_F(DrawableDescriptorTest, DrawableDescTest002, TestSize.Level1)
54 {
55     /**
56      * @tc.steps: step1. create layeredDrawableDescriptor and call GetPixelMap when layeredPixelMap is empty
57      * @tc.expected: return nullptr
58      */
59     std::unique_ptr<uint8_t[]> jsonBuf;
60     size_t len = 0;
61     std::shared_ptr<Global::Resource::ResourceManager> resourceMgr;
62     auto layeredDrawableDescriptor = Napi::LayeredDrawableDescriptor(std::move(jsonBuf), len, std::move(resourceMgr));
63     auto res = layeredDrawableDescriptor.GetPixelMap();
64     EXPECT_EQ(res, nullptr);
65 
66     /**
67      * @tc.steps: step2. call GetForeground when foreground is empty
68      * @tc.expected: return nullptr
69      */
70     auto res2 = layeredDrawableDescriptor.GetForeground();
71     EXPECT_EQ(res2, nullptr);
72 
73     /**
74      * @tc.steps: step3. call GetBackground when background is empty
75      * @tc.expected: return nullptr
76      */
77     auto res3 = layeredDrawableDescriptor.GetBackground();
78     EXPECT_EQ(res3, nullptr);
79 }
80 
81 /**
82  * @tc.name: ImageConverterTest001
83  * @tc.desc: test ImageConverter's member functions;
84  * @tc.type: FUNC
85  */
86 HWTEST_F(DrawableDescriptorTest, ImageConverterTest001, TestSize.Level1)
87 {
88     /**
89      * @tc.steps: step1. create imageConverter and call PixelFormatToSkColorType
90      * @tc.expected: return rightly
91      */
92     Napi::ImageConverter imageConverter;
93     Media::PixelFormat pixelFormat = Media::PixelFormat::BGRA_8888;
94     auto res = imageConverter.PixelFormatToSkColorType(pixelFormat);
95     EXPECT_EQ(res, SkColorType::kBGRA_8888_SkColorType);
96 
97     /**
98      * @tc.steps: step2. call AlphaTypeToSkAlphaType
99      * @tc.expected: return rightly
100      */
101     Media::AlphaType alphaType = Media::AlphaType::IMAGE_ALPHA_TYPE_OPAQUE;
102     auto res2 = imageConverter.AlphaTypeToSkAlphaType(alphaType);
103     EXPECT_EQ(res2, SkAlphaType::kOpaque_SkAlphaType);
104 
105     /**
106      * @tc.steps: step3. call PixelMapToBitmap
107      * @tc.expected: created successfully
108      */
109     Media::PixelMap pixel;
110     auto pixelMap = std::make_shared<Media::PixelMap>(pixel);
111     ASSERT_NE(pixelMap, nullptr);
112     auto res3 = imageConverter.PixelMapToBitmap(pixelMap);
113     EXPECT_NE(res3, nullptr);
114 
115     /**
116      * @tc.steps: step4. call BitmapToPixelMap
117      * @tc.expected: function exits normally
118      */
119     Media::InitializationOptions opts;
120     SkBitmap skBitmap;
121     auto bitmap = std::make_shared<SkBitmap>(skBitmap);
122     ASSERT_NE(bitmap, nullptr);
123     auto res4 = imageConverter.BitmapToPixelMap(bitmap, opts);
124     EXPECT_EQ(res4, nullptr);
125 }
126 } // namespace OHOS::Ace