• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 #include <securec.h>
16 #include <gtest/gtest.h>
17 #include <surface.h>
18 #include <consumer_surface.h>
19 #include <surface_utils.h>
20 #include "buffer_consumer_listener.h"
21 
22 using namespace testing;
23 using namespace testing::ext;
24 
25 namespace OHOS::Rosen {
26 class SurfaceUtilsTest : public testing::Test {
27 public:
28     static void SetUpTestCase();
29     static void TearDownTestCase();
30 
31     static inline sptr<IConsumerSurface> csurface1 = nullptr;
32     static inline sptr<IBufferProducer> producer1 = nullptr;
33     static inline sptr<Surface> psurface1 = nullptr;
34 
35     static inline sptr<IConsumerSurface> csurface2 = nullptr;
36     static inline sptr<IBufferProducer> producer2 = nullptr;
37     static inline sptr<Surface> psurface2 = nullptr;
38 
39     static inline SurfaceUtils *utils = nullptr;
40 };
41 
SetUpTestCase()42 void SurfaceUtilsTest::SetUpTestCase()
43 {
44     csurface1 = IConsumerSurface::Create();
45     sptr<IBufferConsumerListener> listener1 = new BufferConsumerListener();
46     csurface1->RegisterConsumerListener(listener1);
47     producer1 = csurface1->GetProducer();
48     psurface1 = Surface::CreateSurfaceAsProducer(producer1);
49 
50     csurface2 = IConsumerSurface::Create();
51     sptr<IBufferConsumerListener> listener2 = new BufferConsumerListener();
52     csurface2->RegisterConsumerListener(listener2);
53     producer2 = csurface2->GetProducer();
54     psurface2 = Surface::CreateSurfaceAsProducer(producer2);
55 }
56 
TearDownTestCase()57 void SurfaceUtilsTest::TearDownTestCase()
58 {
59     csurface1 = nullptr;
60     producer1 = nullptr;
61     psurface1 = nullptr;
62 
63     csurface2 = nullptr;
64     producer2 = nullptr;
65     psurface2 = nullptr;
66     utils = nullptr;
67 }
68 
69 /*
70 * Function: GetInstance
71 * Type: Function
72 * Rank: Important(2)
73 * EnvConditions: N/A
74 * CaseDescription: 1. call GetInstance
75 *                  2. check ret
76  */
77 HWTEST_F(SurfaceUtilsTest, GetInstance001, Function | MediumTest | Level2)
78 {
79     utils = SurfaceUtils::GetInstance();
80     ASSERT_NE(utils, nullptr);
81 }
82 
83 /*
84 * Function: GetInstance
85 * Type: Function
86 * Rank: Important(2)
87 * EnvConditions: N/A
88 * CaseDescription: 1. call Add
89 *                  2. check ret
90  */
91 HWTEST_F(SurfaceUtilsTest, Add001, Function | MediumTest | Level2)
92 {
93     GSError ret = utils->Add(psurface1->GetUniqueId(), nullptr);
94     ASSERT_EQ(ret, OHOS::GSERROR_INVALID_ARGUMENTS);
95 }
96 
97 /*
98 * Function: GetInstance
99 * Type: Function
100 * Rank: Important(2)
101 * EnvConditions: N/A
102 * CaseDescription: 1. call Add 2 times
103 *                  2. check ret
104  */
105 HWTEST_F(SurfaceUtilsTest, Add002, Function | MediumTest | Level2)
106 {
107     GSError ret = utils->Add(psurface1->GetUniqueId(), psurface1);
108     ASSERT_EQ(ret, OHOS::GSERROR_OK);
109 
110     ret = utils->Add(psurface1->GetUniqueId(), psurface1);
111     ASSERT_EQ(ret, OHOS::GSERROR_OK);
112 }
113 
114 /*
115 * Function: GetSurface
116 * Type: Function
117 * Rank: Important(2)
118 * EnvConditions: N/A
119 * CaseDescription: 1. call GetSurface by abnormal uniqueId
120 *                  2. check ret
121  */
122 HWTEST_F(SurfaceUtilsTest, GetSurface001, Function | MediumTest | Level2)
123 {
124     sptr<Surface> surface = utils->GetSurface(0);
125     ASSERT_EQ(surface, nullptr);
126 }
127 
128 /*
129 * Function: GetSurface
130 * Type: Function
131 * Rank: Important(2)
132 * EnvConditions: N/A
133 * CaseDescription: 1. call GetSurface
134 *                  2. check ret
135  */
136 HWTEST_F(SurfaceUtilsTest, GetSurface002, Function | MediumTest | Level2)
137 {
138     sptr<Surface> surface1 = utils->GetSurface(psurface1->GetUniqueId());
139     ASSERT_NE(surface1, nullptr);
140 }
141 
142 /*
143 * Function: GetSurface
144 * Type: Function
145 * Rank: Important(2)
146 * EnvConditions: N/A
147 * CaseDescription: 1. call GetSurface
148 *                  2. call Add
149 *                  3. call GetSurface again
150 *                  4. check ret
151  */
152 HWTEST_F(SurfaceUtilsTest, GetSurface003, Function | MediumTest | Level2)
153 {
154     sptr<Surface> surface2 = utils->GetSurface(psurface2->GetUniqueId());
155     ASSERT_EQ(surface2, nullptr);
156 
157     GSError ret = utils->Add(psurface2->GetUniqueId(), psurface2);
158     ASSERT_EQ(ret, OHOS::GSERROR_OK);
159 
160     surface2 = utils->GetSurface(psurface2->GetUniqueId());
161     ASSERT_NE(surface2, nullptr);
162 }
163 
164 /*
165 * Function: Remove
166 * Type: Function
167 * Rank: Important(2)
168 * EnvConditions: N/A
169 * CaseDescription: 1. call Remove
170 *                  2. check ret
171  */
172 HWTEST_F(SurfaceUtilsTest, Remove001, Function | MediumTest | Level2)
173 {
174     GSError ret = utils->Remove(0);
175     ASSERT_EQ(ret, GSERROR_INVALID_OPERATING);
176 }
177 
178 /*
179 * Function: Remove
180 * Type: Function
181 * Rank: Important(2)
182 * EnvConditions: N/A
183 * CaseDescription: 1. call Remove 2 times
184 *                  2. check ret
185  */
186 HWTEST_F(SurfaceUtilsTest, Remove002, Function | MediumTest | Level2)
187 {
188     GSError ret = utils->Remove(psurface1->GetUniqueId());
189     ASSERT_EQ(ret, OHOS::GSERROR_OK);
190 
191     ret = utils->Remove(psurface1->GetUniqueId());
192     ASSERT_EQ(ret, SURFACE_ERROR_INVALID_OPERATING);
193 }
194 }