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  */
HWTEST_F(OutputUnittest, SetValue001, TestSize.Level1)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  */
HWTEST_F(OutputUnittest, SetValue002, TestSize.Level1)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  */
HWTEST_F(OutputUnittest, SetValue003, TestSize.Level1)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: SetValue004
99  * @tc.desc: Set some parameters required when the program is compiled
100  * @tc.type: FUNC
101  * @tc.require:
102  * @tc.author:
103  */
HWTEST_F(OutputUnittest, SetValue004, TestSize.Level1)104 HWTEST_F(OutputUnittest, SetValue004, TestSize.Level1)
105 {
106     GTEST_LOG_(INFO) << "OutputUnittest SetValue004 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 SetValue to set the necessary values
115      */
116     auto outputDST = std::make_shared<std::string>("test");
117     std::weak_ptr<void> vOutputDST = outputDST;
118     output->SetValue("format", vOutputDST.lock(), 1);
119     ProcessData data;
120     output->DoProcess(data);
121 
122     EXPECT_TRUE(output->GetFilterType() != FILTER_TYPE::ALGOFILTER);
123 }
124 
125 /**
126  * @tc.name: SetValue005
127  * @tc.desc: Set some parameters required when the program is compiled
128  * @tc.type: FUNC
129  * @tc.require:
130  * @tc.author:
131  */
HWTEST_F(OutputUnittest, SetValue005, TestSize.Level1)132 HWTEST_F(OutputUnittest, SetValue005, TestSize.Level1)
133 {
134     GTEST_LOG_(INFO) << "OutputUnittest SetValue005 start";
135     /**
136      * @tc.steps: step1. Create a Filter pointer
137      */
138     auto output = std::make_shared<Output>();
139     bool testResult = output != nullptr;
140     EXPECT_TRUE(testResult);
141     /**
142      * @tc.steps: step2. Call SetValue to set the necessary values
143      */
144     auto outputDST = std::make_shared<std::string>("jpg");
145     std::weak_ptr<void> vOutputDST = outputDST;
146     output->SetValue("format", vOutputDST.lock(), 1);
147     output->SetValue("dst", vOutputDST.lock(), 1);
148 
149     EXPECT_TRUE(output->GetFilterType() != FILTER_TYPE::INPUT);
150 }
151 
152 /**
153  * @tc.name: SetValue006
154  * @tc.desc: Set some parameters required when the program is compiled
155  * @tc.type: FUNC
156  * @tc.require:
157  * @tc.author:
158  */
HWTEST_F(OutputUnittest, SetValue006, TestSize.Level1)159 HWTEST_F(OutputUnittest, SetValue006, TestSize.Level1)
160 {
161     GTEST_LOG_(INFO) << "OutputUnittest SetValue006 start";
162     /**
163      * @tc.steps: step1. Create a Filter pointer
164      */
165     auto output = std::make_shared<Output>();
166     bool testResult = output != nullptr;
167     EXPECT_TRUE(testResult);
168     /**
169      * @tc.steps: step2. Call SetValue to set the necessary values
170      */
171     auto outputDST = std::make_shared<std::string>("pixelMap");
172     std::weak_ptr<void> vOutputDST = outputDST;
173     output->SetValue("format", vOutputDST.lock(), 1);
174 
175     EXPECT_TRUE(output->GetFilterType() == FILTER_TYPE::OUTPUT);
176 }
177 
178 /**
179  * @tc.name: GetVertexShader001
180  * @tc.desc: Get a string used to compile the program
181  * @tc.type: FUNC
182  * @tc.require:
183  * @tc.author:
184  */
HWTEST_F(OutputUnittest, GetVertexShader001, TestSize.Level1)185 HWTEST_F(OutputUnittest, GetVertexShader001, TestSize.Level1)
186 {
187     GTEST_LOG_(INFO) << "OutputUnittest GetVertexShader001 start";
188     /**
189      * @tc.steps: step1. Create a Filter pointer
190      */
191     auto output = std::make_shared<Output>();
192     bool testResult = output != nullptr;
193     EXPECT_TRUE(testResult);
194     /**
195      * @tc.steps: step2. Call GetVertexShader to get the strings
196      */
197     std::string result = R"SHADER(#version 320 es
198         precision mediump float;
199         layout (location = 0) in vec3 vertexCoord;
200         layout (location = 1) in vec2 inputTexCoord;
201         out vec2 texCoord;
202 
203         void main()
204         {
205             gl_Position = vec4(vertexCoord, 1.0);
206             texCoord = inputTexCoord;
207         }
208     )SHADER";
209     EXPECT_TRUE(output->GetVertexShader() == result);
210 }
211 
212 /**
213  * @tc.name: GetVertexShader002
214  * @tc.desc: Get a string used to compile the program
215  * @tc.type: FUNC
216  * @tc.require:
217  * @tc.author:
218  */
HWTEST_F(OutputUnittest, GetVertexShader002, TestSize.Level1)219 HWTEST_F(OutputUnittest, GetVertexShader002, TestSize.Level1)
220 {
221     GTEST_LOG_(INFO) << "OutputUnittest GetVertexShader002 start";
222     /**
223      * @tc.steps: step1. Create a Filter pointer
224      */
225     auto output = std::make_shared<Output>();
226     bool testResult = output != nullptr;
227     EXPECT_TRUE(testResult);
228     /**
229      * @tc.steps: step2. Call GetVertexShader to get the strings
230      */
231     string result = "";
232     EXPECT_TRUE(output->GetVertexShader() != result);
233 }
234 
235 /**
236  * @tc.name: GetFragmentShader001
237  * @tc.desc: Get a string used to compile the program
238  * @tc.type: FUNC
239  * @tc.require:
240  * @tc.author:
241  */
HWTEST_F(OutputUnittest, GetFragmentShader001, TestSize.Level1)242 HWTEST_F(OutputUnittest, GetFragmentShader001, TestSize.Level1)
243 {
244     GTEST_LOG_(INFO) << "OutputUnittest GetFragmentShader001 start";
245     /**
246      * @tc.steps: step1. Create a Filter pointer
247      */
248     auto output = std::make_shared<Output>();
249     bool testResult = output != nullptr;
250     EXPECT_TRUE(testResult);
251     /**
252      * @tc.steps: step2. Call GetFragmentShader to get the strings
253      */
254     std::string result = R"SHADER(#version 320 es
255         precision mediump float;
256         in vec2 texCoord;
257         out vec4 fragColor;
258         uniform sampler2D uTexture;
259         void main()
260         {
261             fragColor = texture(uTexture, texCoord);
262         }
263     )SHADER";
264     EXPECT_TRUE(output->GetFragmentShader() == result);
265 }
266 
267 /**
268  * @tc.name: GetFragmentShader002
269  * @tc.desc: Get a string used to compile the program
270  * @tc.type: FUNC
271  * @tc.require:
272  * @tc.author:
273  */
HWTEST_F(OutputUnittest, GetFragmentShader002, TestSize.Level1)274 HWTEST_F(OutputUnittest, GetFragmentShader002, TestSize.Level1)
275 {
276     GTEST_LOG_(INFO) << "OutputUnittest GetFragmentShader002 start";
277     /**
278      * @tc.steps: step1. Create a Filter pointer
279      */
280     auto output = std::make_shared<Output>();
281     bool testResult = output != nullptr;
282     EXPECT_TRUE(testResult);
283     /**
284      * @tc.steps: step2. Call GetFragmentShader to get the strings
285      */
286     string result = "";
287     EXPECT_TRUE(output->GetFragmentShader() != result);
288 }
289 
290 /**
291  * @tc.name: GetFilterType001
292  * @tc.desc: Obtain the type of filter according to different implementations of the interface
293  * @tc.type: FUNC
294  * @tc.require:
295  * @tc.author:
296  */
HWTEST_F(OutputUnittest, GetFilterType001, TestSize.Level1)297 HWTEST_F(OutputUnittest, GetFilterType001, TestSize.Level1)
298 {
299     GTEST_LOG_(INFO) << "OutputUnittest GetFilterType001 start";
300     /**
301      * @tc.steps: step1. Create a Filter pointer
302      */
303     auto output = std::make_shared<Output>();
304     bool testResult = output != nullptr;
305     EXPECT_TRUE(testResult);
306     /**
307      * @tc.steps: step2. Call GetFilterType to get the type of Filter
308      */
309     EXPECT_TRUE(output->GetFilterType() == FILTER_TYPE::OUTPUT);
310 }
311 
312 /**
313  * @tc.name: GetFilterType002
314  * @tc.desc: Obtain the type of filter according to different implementations of the interface
315  * @tc.type: FUNC
316  * @tc.require:
317  * @tc.author:
318  */
HWTEST_F(OutputUnittest, GetFilterType002, TestSize.Level1)319 HWTEST_F(OutputUnittest, GetFilterType002, TestSize.Level1)
320 {
321     GTEST_LOG_(INFO) << "OutputUnittest GetFilterType002 start";
322     /**
323      * @tc.steps: step1. Create a Filter pointer
324      */
325     auto output = std::make_shared<Output>();
326     bool testResult = output != nullptr;
327     EXPECT_TRUE(testResult);
328     /**
329      * @tc.steps: step2. Call GetFilterType to get the type of Filter
330      */
331     EXPECT_TRUE(output->GetFilterType() != FILTER_TYPE::INPUT);
332 }
333 
334 /**
335  * @tc.name: GetFilterType003
336  * @tc.desc: Obtain the type of filter according to different implementations of the interface
337  * @tc.type: FUNC
338  * @tc.require:
339  * @tc.author:
340  */
HWTEST_F(OutputUnittest, GetFilterType003, TestSize.Level1)341 HWTEST_F(OutputUnittest, GetFilterType003, TestSize.Level1)
342 {
343     GTEST_LOG_(INFO) << "OutputUnittest GetFilterType003 start";
344     /**
345      * @tc.steps: step1. Create a Filter pointer
346      */
347     auto output = std::make_shared<Output>();
348     bool testResult = output != nullptr;
349     EXPECT_TRUE(testResult);
350     /**
351      * @tc.steps: step2. Call GetFilterType to get the type of Filter
352      */
353     EXPECT_TRUE(output->GetFilterType() != FILTER_TYPE::ALGOFILTER);
354 }
355 
356 /**
357  * @tc.name: GetPixelMap001
358  * @tc.desc: Get the rendered PixelMap
359  * @tc.type: FUNC
360  * @tc.require:
361  * @tc.author:
362  */
HWTEST_F(OutputUnittest, GetPixelMap001, TestSize.Level1)363 HWTEST_F(OutputUnittest, GetPixelMap001, TestSize.Level1)
364 {
365     GTEST_LOG_(INFO) << "OutputUnittest GetPixelMap001 start";
366     /**
367      * @tc.steps: step1. Create a Filter pointer
368      */
369     auto output = std::make_shared<Output>();
370     bool testResult1 = output != nullptr;
371     EXPECT_TRUE(testResult1);
372     /**
373      * @tc.steps: step2. Call GetPixelMap to get render result
374      */
375     bool testResult2 = output->GetPixelMap() == nullptr;
376     EXPECT_TRUE(testResult2);
377 }
378 
379 /**
380  * @tc.name: GetPixelMap002
381  * @tc.desc: Get the rendered PixelMap
382  * @tc.type: FUNC
383  * @tc.require:
384  * @tc.author:
385  */
HWTEST_F(OutputUnittest, GetPixelMap002, TestSize.Level1)386 HWTEST_F(OutputUnittest, GetPixelMap002, TestSize.Level1)
387 {
388     GTEST_LOG_(INFO) << "OutputUnittest GetPixelMap002 start";
389     /**
390      * @tc.steps: step1. Create a Filter list and render
391      */
392     FilterFactory filterFactory;
393     auto input = filterFactory.GetFilter("Input");
394     auto inputFormat = std::make_shared<std::string>("pixelMap");
395     std::weak_ptr<void> vInputFormat = inputFormat;
396     input->SetValue("format", vInputFormat.lock(), 1);
397     Media::InitializationOptions opts;
398     opts.size.width = 512;
399     opts.size.height = 512;
400     opts.editable = true;
401     auto pixelMap = Media::PixelMap::Create(opts);
402     auto shpPixelMap =  std::shared_ptr<Media::PixelMap>(pixelMap.release());
403     std::weak_ptr<void> vPixelMap = shpPixelMap;
404     input->SetValue("src", vPixelMap.lock(), 1);
405     auto output = std::make_shared<Output>();
406     auto outputFormat = std::make_shared<std::string>("pixelMap");
407     std::weak_ptr<void> vOutputFormat = outputFormat;
408     output->SetValue("format", vOutputFormat.lock(), 1);
409     auto castOutput = std::static_pointer_cast<Rosen::Filter>(output);
410     input->AddNextFilter(castOutput);
411     std::vector<std::shared_ptr<Rosen::Input>> inputs;
412     auto castInput = std::static_pointer_cast<Rosen::Input>(input);
413     inputs.push_back(castInput);
414     Rosen::ImageChain imageChain(inputs);
415     imageChain.Render();
416     /**
417      * @tc.steps: step2. Call GetPixelMap to get render result
418      */
419     bool testResult = output->GetPixelMap() != nullptr;
420     EXPECT_TRUE(testResult);
421 }
422 
423 /**
424  * @tc.name: GetColorBuffer001
425  * @tc.desc: Get the rendered ColorBuffer
426  * @tc.type: FUNC
427  * @tc.require:
428  * @tc.author:
429  */
HWTEST_F(OutputUnittest, GetColorBuffer001, TestSize.Level1)430 HWTEST_F(OutputUnittest, GetColorBuffer001, TestSize.Level1)
431 {
432     GTEST_LOG_(INFO) << "OutputUnittest GetColorBuffer001 start";
433     /**
434      * @tc.steps: step1. Create a Filter pointer
435      */
436     auto output = std::make_shared<Output>();
437     bool testResult1 = output != nullptr;
438     EXPECT_TRUE(testResult1);
439     /**
440      * @tc.steps: step2. Call GetColorBuffer to get render result
441      */
442     bool testResult2 = output->GetColorBuffer().size() == 0;
443     EXPECT_TRUE(testResult2);
444 }
445 
446 /**
447  * @tc.name: GetColorBuffer002
448  * @tc.desc: Get the rendered ColorBuffer
449  * @tc.type: FUNC
450  * @tc.require:
451  * @tc.author:
452  */
HWTEST_F(OutputUnittest, GetColorBuffer002, TestSize.Level1)453 HWTEST_F(OutputUnittest, GetColorBuffer002, TestSize.Level1)
454 {
455     GTEST_LOG_(INFO) << "OutputUnittest GetColorBuffer002 start";
456     /**
457      * @tc.steps: step1. Create a Filter list and render
458      */
459     FilterFactory filterFactory;
460     auto input = filterFactory.GetFilter("Input");
461     auto inputFormat = std::make_shared<std::string>("pixelMap");
462     std::weak_ptr<void> vInputFormat = inputFormat;
463     input->SetValue("format", vInputFormat.lock(), 1);
464     Media::InitializationOptions opts;
465     opts.size.width = 512;
466     opts.size.height = 512;
467     opts.editable = true;
468     auto pixelMap = Media::PixelMap::Create(opts);
469     auto shpPixelMap =  std::shared_ptr<Media::PixelMap>(pixelMap.release());
470     std::weak_ptr<void> vPixelMap = shpPixelMap;
471     input->SetValue("src", vPixelMap.lock(), 1);
472     auto output = std::make_shared<Output>();
473     auto outputFormat = std::make_shared<std::string>("buffer");
474     std::weak_ptr<void> vOutputFormat = outputFormat;
475     output->SetValue("format", vOutputFormat.lock(), 1);
476     auto castOutput = std::static_pointer_cast<Rosen::Filter>(output);
477     input->AddNextFilter(castOutput);
478     std::vector<std::shared_ptr<Rosen::Input>> inputs;
479     auto castInput = std::static_pointer_cast<Rosen::Input>(input);
480     inputs.push_back(castInput);
481     Rosen::ImageChain imageChain(inputs);
482     imageChain.Render();
483     /**
484      * @tc.steps: step2. Call GetColorBuffer to get render result
485      */
486     bool testResult = output->GetColorBuffer().size() != 0;
487     EXPECT_TRUE(testResult);
488 }
489 } // namespace Rosen
490 } // namespace OHOS
491