• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "filter_common_unittest.h"
17 
18 #include "effect_errors.h"
19 #include "filter_common.h"
20 #include "image_source.h"
21 #include "pixel_map.h"
22 #include "test_picture_files.h"
23 
24 using namespace testing;
25 using namespace testing::ext;
26 using namespace OHOS::Media;
27 
28 namespace OHOS {
29 namespace Rosen {
30 
CreatePixelMap()31 std::unique_ptr<PixelMap> CreatePixelMap()
32 {
33     size_t bufferSize = 0;
34     uint8_t* buffer = GetJpgBuffer(bufferSize);
35     if (buffer == nullptr) {
36         return nullptr;
37     }
38 
39     uint32_t errorCode = 0;
40     SourceOptions opts;
41     opts.formatHint = "image/jpeg";
42     std::unique_ptr<ImageSource> imageSource = ImageSource::CreateImageSource(buffer, bufferSize, opts, errorCode);
43     if ((errorCode != SUCCESS) || (imageSource == nullptr)) {
44         return nullptr;
45     }
46 
47     DecodeOptions decodeOpts;
48     std::unique_ptr<PixelMap> pixmap = imageSource->CreatePixelMap(decodeOpts, errorCode);
49     if ((errorCode != SUCCESS) || (pixmap == nullptr)) {
50         return nullptr;
51     }
52 
53     return pixmap;
54 }
55 
56 /**
57  * @tc.name: FilterCommon_CreateEffect
58  * @tc.desc: Create a effect filter with pixelmap.
59  * @tc.type: FUNC
60  * @tc.require: I9CSQ0
61  * @tc.author:
62  */
63 HWTEST_F(FilterCommonUnittest, FilterCommon_CreateEffect001, TestSize.Level1)
64 {
65     GTEST_LOG_(INFO) << "FilterCommonUnittest FilterCommon_CreateEffect001 start";
66     std::unique_ptr<PixelMap> pixMap = CreatePixelMap();
67     ASSERT_TRUE(pixMap != nullptr);
68     uint32_t errorCode = SUCCESS;
69     std::shared_ptr<FilterCommon> filter = FilterCommon::CreateEffect(nullptr, errorCode);
70     ASSERT_TRUE(errorCode != SUCCESS);
71     ASSERT_TRUE(filter == nullptr);
72     errorCode = SUCCESS;
73     filter = FilterCommon::CreateEffect(std::move(pixMap), errorCode);
74     ASSERT_TRUE(errorCode == SUCCESS);
75     ASSERT_TRUE(filter != nullptr);
76 }
77 
78 /**
79  * @tc.name: FilterCommon_Invert
80  * @tc.desc: Create a invert effect filter.
81  * @tc.type: FUNC
82  * @tc.require: I9CSQ0
83  * @tc.author:
84  */
85 HWTEST_F(FilterCommonUnittest, FilterCommon_Invert001, TestSize.Level1)
86 {
87     GTEST_LOG_(INFO) << "FilterCommonUnittest FilterCommon_Filter_Invert001 start";
88     std::unique_ptr<PixelMap> pixMap = CreatePixelMap();
89     uint32_t errorCode = SUCCESS;
90     std::shared_ptr<FilterCommon> filter = FilterCommon::CreateEffect(std::move(pixMap), errorCode);
91     ASSERT_TRUE(errorCode == SUCCESS);
92     ASSERT_TRUE(filter != nullptr);
93     ASSERT_TRUE(filter->Invert() == true);
94 }
95 
96 /**
97  * @tc.name: FilterCommon_Blur
98  * @tc.desc: Create a  blur effect filter with pixelmap.
99  * @tc.type: FUNC
100  * @tc.require: I9CSQ0
101  * @tc.author:
102  */
103 HWTEST_F(FilterCommonUnittest, FilterCommon_Blur001, TestSize.Level1)
104 {
105     GTEST_LOG_(INFO) << "FilterCommonUnittest FilterCommon_Filter_Blur001 start";
106     std::unique_ptr<PixelMap> pixMap = CreatePixelMap();
107     uint32_t errorCode = SUCCESS;
108     std::shared_ptr<FilterCommon> filter = FilterCommon::CreateEffect(std::move(pixMap), errorCode);
109     ASSERT_TRUE(errorCode == SUCCESS);
110     ASSERT_TRUE(filter != nullptr);
111     ASSERT_TRUE(filter->Blur(5.0f) == true);
112 }
113 
114 /**
115  * @tc.name: FilterCommon_Grayscale
116  * @tc.desc: Create a gray scale effect filter with pixelmap.
117  * @tc.type: FUNC
118  * @tc.require: I9CSQ0
119  * @tc.author:
120  */
121 HWTEST_F(FilterCommonUnittest, FilterCommon_Grayscale001, TestSize.Level1)
122 {
123     GTEST_LOG_(INFO) << "FilterCommonUnittest FilterCommon_Grayscale001 start";
124     std::unique_ptr<PixelMap> pixMap = CreatePixelMap();
125     uint32_t errorCode = SUCCESS;
126     std::shared_ptr<FilterCommon> filter = FilterCommon::CreateEffect(std::move(pixMap), errorCode);
127     ASSERT_TRUE(errorCode == SUCCESS);
128     ASSERT_TRUE(filter != nullptr);
129     ASSERT_TRUE(filter->Grayscale() == true);
130 }
131 
132 /**
133  * @tc.name: FilterCommon_Brighten
134  * @tc.desc: Create a brighten effect filter with pixelmap.
135  * @tc.type: FUNC
136  * @tc.require: I9CSQ0
137  * @tc.author:
138  */
139 HWTEST_F(FilterCommonUnittest, FilterCommon_Brighten001, TestSize.Level1)
140 {
141     GTEST_LOG_(INFO) << "FilterCommonUnittest FilterCommon_Brighten001 start";
142     std::unique_ptr<PixelMap> pixMap = CreatePixelMap();
143     uint32_t errorCode = SUCCESS;
144     std::shared_ptr<FilterCommon> filter = FilterCommon::CreateEffect(std::move(pixMap), errorCode);
145     ASSERT_TRUE(errorCode == SUCCESS);
146     ASSERT_TRUE(filter != nullptr);
147     ASSERT_TRUE(filter->Brightness(0.5f) == true);
148 }
149 
150 /**
151  * @tc.name: FilterCommon_SetColorMatrix
152  * @tc.desc: Create a effect filter with pixelmap.
153  * @tc.type: FUNC
154  * @tc.require: I9CSQ0
155  * @tc.author:
156  */
157 HWTEST_F(FilterCommonUnittest, OH_Filter_SetColorMatrix001, TestSize.Level1)
158 {
159     GTEST_LOG_(INFO) << "FilterCommonUnittest FilterCommon_SetColorMatrix001 start";
160     std::unique_ptr<PixelMap> pixMap = CreatePixelMap();
161     uint32_t errorCode = SUCCESS;
162     std::shared_ptr<FilterCommon> filter = FilterCommon::CreateEffect(std::move(pixMap), errorCode);
163     ASSERT_TRUE(errorCode == SUCCESS);
164     ASSERT_TRUE(filter != nullptr);
165     std::vector<float> matrix { -1.0, 0, 0, 0, 1, 0, -1.0, 0, 0, 1, 0, 0, -1.0, 0, 1, 0, 0, 0, 1, 0 };
166     ASSERT_TRUE(filter->SetColorMatrix(matrix, errorCode) == true);
167     ASSERT_TRUE(errorCode == SUCCESS);
168 }
169 
170 /**
171  * @tc.name: FilterCommon_GetEffectPixelMap
172  * @tc.desc: Get a pixelmap created by the filter.
173  * @tc.type: FUNC
174  * @tc.require: I9CSQ0
175  * @tc.author:
176  */
177 HWTEST_F(FilterCommonUnittest, FilterCommon_GetEffectPixelMap001, TestSize.Level1)
178 {
179     GTEST_LOG_(INFO) << "FilterCommonUnittest FilterCommon_Filter_Invert001 start";
180     std::unique_ptr<PixelMap> pixMap = CreatePixelMap();
181     uint32_t errorCode = SUCCESS;
182     std::shared_ptr<FilterCommon> filter = FilterCommon::CreateEffect(std::move(pixMap), errorCode);
183     ASSERT_TRUE(errorCode == SUCCESS);
184     ASSERT_TRUE(filter != nullptr);
185     ASSERT_TRUE(filter->Invert() == true);
186     ASSERT_TRUE(filter->GetEffectPixelMap() != nullptr);
187 }
188 } // namespace Rosen
189 } // namespace OHOS
190