1 /*
2  * Copyright (c) 2022 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 "image_chain.h"
17 #include "output.h"
18 #include "filter_factory.h"
19 #include "output_unittest.h"
20 using namespace testing;
21 using namespace testing::ext;
22 
23 namespace OHOS {
24 namespace Rosen {
25 /**
26  * @tc.name: SetValue001
27  * @tc.desc: Set some parameters required when the program is compiled
28  * @tc.type: FUNC
29  * @tc.require:
30  * @tc.author:
31  */
32 HWTEST_F(OutputUnittest, SetValue001, TestSize.Level1)
33 {
34     GTEST_LOG_(INFO) << "OutputUnittest SetValue001 start";
35     /**
36      * @tc.steps: step1. Create a Filter pointer
37      */
38     auto output = std::make_shared<Output>();
39     bool testResult = output != nullptr;
40     EXPECT_TRUE(testResult);
41     /**
42      * @tc.steps: step2. Call SetValue to set the necessary values
43      */
44     auto outputFormat = std::make_shared<std::string>("jpg");
45     std::weak_ptr<void> vOutputFormat = outputFormat;
46     output->SetValue("format", vOutputFormat.lock(), 1);
47 }
48 
49 /**
50  * @tc.name: SetValue002
51  * @tc.desc: Set some parameters required when the program is compiled
52  * @tc.type: FUNC
53  * @tc.require:
54  * @tc.author:
55  */
56 HWTEST_F(OutputUnittest, SetValue002, TestSize.Level1)
57 {
58     GTEST_LOG_(INFO) << "OutputUnittest SetValue002 start";
59     /**
60      * @tc.steps: step1. Create a Filter pointer
61      */
62     auto output = std::make_shared<Output>();
63     bool testResult = output != nullptr;
64     EXPECT_TRUE(testResult);
65     /**
66      * @tc.steps: step2. Call SetValue to set the necessary values
67      */
68     auto outputFormat = std::make_shared<std::string>("png");
69     std::weak_ptr<void> vOutputFormat = outputFormat;
70     output->SetValue("format", vOutputFormat.lock(), 1);
71 }
72 
73 /**
74  * @tc.name: SetValue003
75  * @tc.desc: Set some parameters required when the program is compiled
76  * @tc.type: FUNC
77  * @tc.require:
78  * @tc.author:
79  */
80 HWTEST_F(OutputUnittest, SetValue003, TestSize.Level1)
81 {
82     GTEST_LOG_(INFO) << "OutputUnittest SetValue003 start";
83     /**
84      * @tc.steps: step1. Create a Filter pointer
85      */
86     auto output = std::make_shared<Output>();
87     bool testResult = output != nullptr;
88     EXPECT_TRUE(testResult);
89     /**
90      * @tc.steps: step2. Call SetValue to set the necessary values
91      */
92     auto outputDST = std::make_shared<std::string>("test");
93     std::weak_ptr<void> vOutputDST = outputDST;
94     output->SetValue("dst", vOutputDST.lock(), 1);
95 }
96 
97 /**
98  * @tc.name: GetVertexShader001
99  * @tc.desc: Get a string used to compile the program
100  * @tc.type: FUNC
101  * @tc.require:
102  * @tc.author:
103  */
104 HWTEST_F(OutputUnittest, GetVertexShader001, TestSize.Level1)
105 {
106     GTEST_LOG_(INFO) << "OutputUnittest GetVertexShader001 start";
107     /**
108      * @tc.steps: step1. Create a Filter pointer
109      */
110     auto output = std::make_shared<Output>();
111     bool testResult = output != nullptr;
112     EXPECT_TRUE(testResult);
113     /**
114      * @tc.steps: step2. Call GetVertexShader to get the strings
115      */
116     std::string result = R"SHADER(#version 320 es
117         precision mediump float;
118         layout (location = 0) in vec3 vertexCoord;
119         layout (location = 1) in vec2 inputTexCoord;
120         out vec2 texCoord;
121 
122         void main()
123         {
124             gl_Position = vec4(vertexCoord, 1.0);
125             texCoord = inputTexCoord;
126         }
127     )SHADER";
128     EXPECT_TRUE(output->GetVertexShader() == result);
129 }
130 
131 /**
132  * @tc.name: GetVertexShader002
133  * @tc.desc: Get a string used to compile the program
134  * @tc.type: FUNC
135  * @tc.require:
136  * @tc.author:
137  */
138 HWTEST_F(OutputUnittest, GetVertexShader002, TestSize.Level1)
139 {
140     GTEST_LOG_(INFO) << "OutputUnittest GetVertexShader002 start";
141     /**
142      * @tc.steps: step1. Create a Filter pointer
143      */
144     auto output = std::make_shared<Output>();
145     bool testResult = output != nullptr;
146     EXPECT_TRUE(testResult);
147     /**
148      * @tc.steps: step2. Call GetVertexShader to get the strings
149      */
150     string result = "";
151     EXPECT_TRUE(output->GetVertexShader() != result);
152 }
153 
154 /**
155  * @tc.name: GetFragmentShader001
156  * @tc.desc: Get a string used to compile the program
157  * @tc.type: FUNC
158  * @tc.require:
159  * @tc.author:
160  */
161 HWTEST_F(OutputUnittest, GetFragmentShader001, TestSize.Level1)
162 {
163     GTEST_LOG_(INFO) << "OutputUnittest GetFragmentShader001 start";
164     /**
165      * @tc.steps: step1. Create a Filter pointer
166      */
167     auto output = std::make_shared<Output>();
168     bool testResult = output != nullptr;
169     EXPECT_TRUE(testResult);
170     /**
171      * @tc.steps: step2. Call GetFragmentShader to get the strings
172      */
173     std::string result = R"SHADER(#version 320 es
174         precision mediump float;
175         in vec2 texCoord;
176         out vec4 fragColor;
177         uniform sampler2D uTexture;
178         void main()
179         {
180             fragColor = texture(uTexture, texCoord);
181         }
182     )SHADER";
183     EXPECT_TRUE(output->GetFragmentShader() == result);
184 }
185 
186 /**
187  * @tc.name: GetFragmentShader002
188  * @tc.desc: Get a string used to compile the program
189  * @tc.type: FUNC
190  * @tc.require:
191  * @tc.author:
192  */
193 HWTEST_F(OutputUnittest, GetFragmentShader002, TestSize.Level1)
194 {
195     GTEST_LOG_(INFO) << "OutputUnittest GetFragmentShader002 start";
196     /**
197      * @tc.steps: step1. Create a Filter pointer
198      */
199     auto output = std::make_shared<Output>();
200     bool testResult = output != nullptr;
201     EXPECT_TRUE(testResult);
202     /**
203      * @tc.steps: step2. Call GetFragmentShader to get the strings
204      */
205     string result = "";
206     EXPECT_TRUE(output->GetFragmentShader() != result);
207 }
208 
209 /**
210  * @tc.name: GetFilterType001
211  * @tc.desc: Obtain the type of filter according to different implementations of the interface
212  * @tc.type: FUNC
213  * @tc.require:
214  * @tc.author:
215  */
216 HWTEST_F(OutputUnittest, GetFilterType001, TestSize.Level1)
217 {
218     GTEST_LOG_(INFO) << "OutputUnittest GetFilterType001 start";
219     /**
220      * @tc.steps: step1. Create a Filter pointer
221      */
222     auto output = std::make_shared<Output>();
223     bool testResult = output != nullptr;
224     EXPECT_TRUE(testResult);
225     /**
226      * @tc.steps: step2. Call GetFilterType to get the type of Filter
227      */
228     EXPECT_TRUE(output->GetFilterType() == FILTER_TYPE::OUTPUT);
229 }
230 
231 /**
232  * @tc.name: GetFilterType002
233  * @tc.desc: Obtain the type of filter according to different implementations of the interface
234  * @tc.type: FUNC
235  * @tc.require:
236  * @tc.author:
237  */
238 HWTEST_F(OutputUnittest, GetFilterType002, TestSize.Level1)
239 {
240     GTEST_LOG_(INFO) << "OutputUnittest GetFilterType002 start";
241     /**
242      * @tc.steps: step1. Create a Filter pointer
243      */
244     auto output = std::make_shared<Output>();
245     bool testResult = output != nullptr;
246     EXPECT_TRUE(testResult);
247     /**
248      * @tc.steps: step2. Call GetFilterType to get the type of Filter
249      */
250     EXPECT_TRUE(output->GetFilterType() != FILTER_TYPE::INPUT);
251 }
252 
253 /**
254  * @tc.name: GetFilterType003
255  * @tc.desc: Obtain the type of filter according to different implementations of the interface
256  * @tc.type: FUNC
257  * @tc.require:
258  * @tc.author:
259  */
260 HWTEST_F(OutputUnittest, GetFilterType003, TestSize.Level1)
261 {
262     GTEST_LOG_(INFO) << "OutputUnittest GetFilterType003 start";
263     /**
264      * @tc.steps: step1. Create a Filter pointer
265      */
266     auto output = std::make_shared<Output>();
267     bool testResult = output != nullptr;
268     EXPECT_TRUE(testResult);
269     /**
270      * @tc.steps: step2. Call GetFilterType to get the type of Filter
271      */
272     EXPECT_TRUE(output->GetFilterType() != FILTER_TYPE::ALGOFILTER);
273 }
274 
275 /**
276  * @tc.name: GetPixelMap001
277  * @tc.desc: Get the rendered PixelMap
278  * @tc.type: FUNC
279  * @tc.require:
280  * @tc.author:
281  */
282 HWTEST_F(OutputUnittest, GetPixelMap001, TestSize.Level1)
283 {
284     GTEST_LOG_(INFO) << "OutputUnittest GetPixelMap001 start";
285     /**
286      * @tc.steps: step1. Create a Filter pointer
287      */
288     auto output = std::make_shared<Output>();
289     bool testResult1 = output != nullptr;
290     EXPECT_TRUE(testResult1);
291     /**
292      * @tc.steps: step2. Call GetPixelMap to get render result
293      */
294     bool testResult2 = output->GetPixelMap() == nullptr;
295     EXPECT_TRUE(testResult2);
296 }
297 
298 /**
299  * @tc.name: GetPixelMap002
300  * @tc.desc: Get the rendered PixelMap
301  * @tc.type: FUNC
302  * @tc.require:
303  * @tc.author:
304  */
305 HWTEST_F(OutputUnittest, GetPixelMap002, TestSize.Level1)
306 {
307     GTEST_LOG_(INFO) << "OutputUnittest GetPixelMap002 start";
308     /**
309      * @tc.steps: step1. Create a Filter list and render
310      */
311     FilterFactory filterFactory;
312     auto input = filterFactory.GetFilter("Input");
313     auto inputFormat = std::make_shared<std::string>("pixelMap");
314     std::weak_ptr<void> vInputFormat = inputFormat;
315     input->SetValue("format", vInputFormat.lock(), 1);
316     Media::InitializationOptions opts;
317     opts.size.width = 512;
318     opts.size.height = 512;
319     opts.editable = true;
320     auto pixelMap = Media::PixelMap::Create(opts);
321     auto shpPixelMap =  std::shared_ptr<Media::PixelMap>(pixelMap.release());
322     std::weak_ptr<void> vPixelMap = shpPixelMap;
323     input->SetValue("src", vPixelMap.lock(), 1);
324     auto output = std::make_shared<Output>();
325     auto outputFormat = std::make_shared<std::string>("pixelMap");
326     std::weak_ptr<void> vOutputFormat = outputFormat;
327     output->SetValue("format", vOutputFormat.lock(), 1);
328     auto castOutput = std::static_pointer_cast<Rosen::Filter>(output);
329     input->AddNextFilter(castOutput);
330     std::vector<std::shared_ptr<Rosen::Input>> inputs;
331     auto castInput = std::static_pointer_cast<Rosen::Input>(input);
332     inputs.push_back(castInput);
333     Rosen::ImageChain imageChain(inputs);
334     imageChain.Render();
335     /**
336      * @tc.steps: step2. Call GetPixelMap to get render result
337      */
338     bool testResult = output->GetPixelMap() != nullptr;
339     EXPECT_TRUE(testResult);
340 }
341 
342 /**
343  * @tc.name: GetColorBuffer001
344  * @tc.desc: Get the rendered ColorBuffer
345  * @tc.type: FUNC
346  * @tc.require:
347  * @tc.author:
348  */
349 HWTEST_F(OutputUnittest, GetColorBuffer001, TestSize.Level1)
350 {
351     GTEST_LOG_(INFO) << "OutputUnittest GetColorBuffer001 start";
352     /**
353      * @tc.steps: step1. Create a Filter pointer
354      */
355     auto output = std::make_shared<Output>();
356     bool testResult1 = output != nullptr;
357     EXPECT_TRUE(testResult1);
358     /**
359      * @tc.steps: step2. Call GetColorBuffer to get render result
360      */
361     bool testResult2 = output->GetColorBuffer().size() == 0;
362     EXPECT_TRUE(testResult2);
363 }
364 
365 /**
366  * @tc.name: GetColorBuffer002
367  * @tc.desc: Get the rendered ColorBuffer
368  * @tc.type: FUNC
369  * @tc.require:
370  * @tc.author:
371  */
372 HWTEST_F(OutputUnittest, GetColorBuffer002, TestSize.Level1)
373 {
374     GTEST_LOG_(INFO) << "OutputUnittest GetColorBuffer002 start";
375     /**
376      * @tc.steps: step1. Create a Filter list and render
377      */
378     FilterFactory filterFactory;
379     auto input = filterFactory.GetFilter("Input");
380     auto inputFormat = std::make_shared<std::string>("pixelMap");
381     std::weak_ptr<void> vInputFormat = inputFormat;
382     input->SetValue("format", vInputFormat.lock(), 1);
383     Media::InitializationOptions opts;
384     opts.size.width = 512;
385     opts.size.height = 512;
386     opts.editable = true;
387     auto pixelMap = Media::PixelMap::Create(opts);
388     auto shpPixelMap =  std::shared_ptr<Media::PixelMap>(pixelMap.release());
389     std::weak_ptr<void> vPixelMap = shpPixelMap;
390     input->SetValue("src", vPixelMap.lock(), 1);
391     auto output = std::make_shared<Output>();
392     auto outputFormat = std::make_shared<std::string>("buffer");
393     std::weak_ptr<void> vOutputFormat = outputFormat;
394     output->SetValue("format", vOutputFormat.lock(), 1);
395     auto castOutput = std::static_pointer_cast<Rosen::Filter>(output);
396     input->AddNextFilter(castOutput);
397     std::vector<std::shared_ptr<Rosen::Input>> inputs;
398     auto castInput = std::static_pointer_cast<Rosen::Input>(input);
399     inputs.push_back(castInput);
400     Rosen::ImageChain imageChain(inputs);
401     imageChain.Render();
402     /**
403      * @tc.steps: step2. Call GetColorBuffer to get render result
404      */
405     bool testResult = output->GetColorBuffer().size() != 0;
406     EXPECT_TRUE(testResult);
407 }
408 } // namespace Rosen
409 } // namespace OHOS
410