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