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
16 #include <gtest/gtest.h>
17 #include "vsync_receiver.h"
18 #include "vsync_distributor.h"
19 #include "vsync_controller.h"
20 #include "event_runner.h"
21 #include "event_handler.h"
22 #include "transaction/rs_interfaces.h"
23
24 using namespace testing;
25 using namespace testing::ext;
26
27 namespace OHOS {
28 namespace Rosen {
29 class VsyncReceiverTest : public testing::Test {
30 public:
31 static void SetUpTestCase();
32 static void TearDownTestCase();
33 static inline void OnVSync(int64_t now, void* data);
34 static inline void OnVSync2(int64_t now, void* data);
35
36 static inline sptr<VSyncController> vsyncController = nullptr;
37 static inline sptr<VSyncDistributor> vsyncDistributor = nullptr;
38 static inline sptr<VSyncGenerator> vsyncGenerator = nullptr;
39 static inline sptr<VSyncConnection> conn = nullptr;
40 static inline sptr<VSyncReceiver> vsyncReceiver = nullptr;
41 static inline int32_t onVsyncCount = 0;
42 };
43
OnVSync(int64_t now,void * data)44 void VsyncReceiverTest::OnVSync(int64_t now, void* data)
45 {
46 onVsyncCount ++;
47 }
48
OnVSync2(int64_t now,void * data)49 void VsyncReceiverTest::OnVSync2(int64_t now, void* data)
50 {
51 usleep(100000); // 100000us
52 }
53
SetUpTestCase()54 void VsyncReceiverTest::SetUpTestCase()
55 {
56 vsyncGenerator = CreateVSyncGenerator();
57 vsyncController = new VSyncController(vsyncGenerator, 0);
58 vsyncDistributor = new VSyncDistributor(vsyncController, "VsyncReceiverTest");
59 conn = new VSyncConnection(vsyncDistributor, "VsyncReceiverTest");
60 vsyncReceiver = new VSyncReceiver(conn);
61 }
62
TearDownTestCase()63 void VsyncReceiverTest::TearDownTestCase()
64 {
65 vsyncReceiver->looper_->RemoveFileDescriptorListener(vsyncReceiver->fd_);
66 vsyncReceiver->looper_ = nullptr;
67 vsyncReceiver->fd_ = -1;
68 vsyncReceiver = nullptr;
69 vsyncController = nullptr;
70 vsyncGenerator = nullptr;
71 vsyncDistributor = nullptr;
72 conn = nullptr;
73 DestroyVSyncGenerator();
74 }
75
76 namespace {
77 /*
78 * Function: BeforeInit001
79 * Type: Function
80 * Rank: Important(2)
81 * EnvConditions: N/A
82 * CaseDescription: 1. call RequestNextVSync Before Init
83 2. call SetVSyncRate Before Init
84 3. call the one-parameter method of CreateVSyncReceiver
85 4. call GetVSyncPeriodAndLastTimeStamp Before Init of vSync receiver in 3.
86 5. call the two-parameter method of CreateVSyncReceiver
87 6. call GetVSyncPeriodAndLastTimeStamp Before Init of vSync receiver in 5.
88 7. call the four-parameter method of CreateVSyncReceiver
89 8. call GetVSyncPeriodAndLastTimeStamp Before Init of vSync receiver in 7.
90 */
91 HWTEST_F(VsyncReceiverTest, BeforeInit001, Function | MediumTest| Level3)
92 {
93 VSyncReceiver::FrameCallback fcb = {
94 .userData_ = this,
95 .callback_ = OnVSync,
96 };
97 ASSERT_EQ(VsyncReceiverTest::vsyncReceiver->RequestNextVSync(fcb), VSYNC_ERROR_NOT_INIT);
98 ASSERT_EQ(VsyncReceiverTest::vsyncReceiver->SetVSyncRate(fcb, 0), VSYNC_ERROR_API_FAILED);
99
100 int64_t period;
101 int64_t timeStamp;
102 auto& rsClient = RSInterfaces::GetInstance();
103 auto rsReceiver = rsClient.CreateVSyncReceiver("VsyncReceiverTest");
104 ASSERT_EQ(rsReceiver->GetVSyncPeriodAndLastTimeStamp(period, timeStamp), VSYNC_ERROR_NOT_INIT);
105 rsReceiver = rsClient.CreateVSyncReceiver("VsyncReceiverTest", nullptr);
106 ASSERT_EQ(rsReceiver->GetVSyncPeriodAndLastTimeStamp(period, timeStamp), VSYNC_ERROR_NOT_INIT);
107 rsReceiver = rsClient.CreateVSyncReceiver("VsyncReceiverTest", 0, nullptr, 0);
108 ASSERT_EQ(rsReceiver->GetVSyncPeriodAndLastTimeStamp(period, timeStamp), VSYNC_ERROR_NOT_INIT);
109 }
110
111 /*
112 * Function: RequestNextVSyncWithMultiCallback
113 * Type: Function
114 * Rank: Important(2)
115 * EnvConditions: N/A
116 * CaseDescription: 1. call RequestNextVSyncWithMultiCallback before Init.
117 */
118 HWTEST_F(VsyncReceiverTest, RequestNextVSyncWithMultiCallback001, Function | MediumTest| Level3)
119 {
120 VSyncReceiver::FrameCallback fcb = {
121 .userData_ = this,
122 .callback_ = OnVSync,
123 };
124 vsyncDistributor->AddConnection(conn);
125 ASSERT_EQ(VsyncReceiverTest::vsyncReceiver->RequestNextVSyncWithMultiCallback(fcb), VSYNC_ERROR_NOT_INIT);
126 vsyncDistributor->RemoveConnection(conn);
127 }
128
129 /*
130 * Function: Init001
131 * Type: Function
132 * Rank: Important(2)
133 * EnvConditions: N/A
134 * CaseDescription: 1. call Init
135 */
136 HWTEST_F(VsyncReceiverTest, Init001, Function | MediumTest| Level3)
137 {
138 ASSERT_EQ(VsyncReceiverTest::vsyncReceiver->Init(), VSYNC_ERROR_OK);
139 ASSERT_EQ(VsyncReceiverTest::vsyncReceiver->Init(), VSYNC_ERROR_OK);
140 }
141
142 /*
143 * Function: Init002
144 * Type: Function
145 * Rank: Important(2)
146 * EnvConditions: N/A
147 * CaseDescription: 1. call Init
148 */
149 HWTEST_F(VsyncReceiverTest, Init002, Function | MediumTest| Level3)
150 {
151 sptr<IVSyncConnection> connection_ = nullptr;
152 ASSERT_NE(VsyncReceiverTest::vsyncReceiver->Init(), VSYNC_ERROR_NULLPTR);
153 }
154
155 /*
156 * Function: IsRequestedNextVSync001
157 * Type: Function
158 * Rank: Important(2)
159 * EnvConditions: N/A
160 * CaseDescription: 1. call IsRequestedNextVSync
161 */
162 HWTEST_F(VsyncReceiverTest, IsRequestedNextVSync001, Function | MediumTest| Level3)
163 {
164 ASSERT_EQ(VsyncReceiverTest::vsyncReceiver->IsRequestedNextVSync(), false);
165 }
166
167 /*
168 * Function: RequestNextVSync001
169 * Type: Function
170 * Rank: Important(2)
171 * EnvConditions: N/A
172 * CaseDescription: 1. call RequestNextVSync
173 */
174 HWTEST_F(VsyncReceiverTest, RequestNextVSync001, Function | MediumTest| Level3)
175 {
176 VSyncReceiver::FrameCallback fcb = {
177 .userData_ = this,
178 .callback_ = OnVSync,
179 };
180 vsyncDistributor->AddConnection(conn);
181 ASSERT_EQ(VsyncReceiverTest::vsyncReceiver->RequestNextVSync(fcb), VSYNC_ERROR_OK);
182 vsyncDistributor->RemoveConnection(conn);
183 }
184
185 /*
186 * Function: RequestNextVSync002
187 * Type: Function
188 * Rank: Important(2)
189 * EnvConditions: N/A
190 * CaseDescription: 1. call RequestNextVSync
191 */
192 HWTEST_F(VsyncReceiverTest, RequestNextVSync002, Function | MediumTest| Level3)
193 {
194 VSyncReceiver::FrameCallback fcb = {
195 .userData_ = this,
196 .callback_ = OnVSync,
197 };
198 vsyncDistributor->AddConnection(conn);
199 ASSERT_EQ(VsyncReceiverTest::vsyncReceiver->RequestNextVSync(fcb, "unknown", 0), VSYNC_ERROR_OK);
200 vsyncDistributor->RemoveConnection(conn);
201 }
202
203 /*
204 * Function: RequestNextVSyncWithMultiCallback
205 * Type: Function
206 * Rank: Important(2)
207 * EnvConditions: N/A
208 * CaseDescription: 1. call RequestNextVSyncWithMultiCallback after Init.
209 */
210 HWTEST_F(VsyncReceiverTest, RequestNextVSyncWithMultiCallback002, Function | MediumTest| Level3)
211 {
212 VSyncReceiver::FrameCallback fcb = {
213 .userData_ = this,
214 .callback_ = OnVSync,
215 };
216 vsyncDistributor->AddConnection(conn);
217 ASSERT_EQ(VsyncReceiverTest::vsyncReceiver->RequestNextVSyncWithMultiCallback(fcb), VSYNC_ERROR_OK);
218 vsyncDistributor->RemoveConnection(conn);
219 }
220
221 /*
222 * Function: IsRequestedNextVSync002
223 * Type: Function
224 * Rank: Important(2)
225 * EnvConditions: N/A
226 * CaseDescription: 1. call IsRequestedNextVSync
227 */
228 HWTEST_F(VsyncReceiverTest, IsRequestedNextVSync002, Function | MediumTest| Level3)
229 {
230 ASSERT_EQ(VsyncReceiverTest::vsyncReceiver->IsRequestedNextVSync(), true);
231 }
232
233 /*
234 * Function: GetVSyncPeriodAndLastTimeStamp001
235 * Type: Function
236 * Rank: Important(2)
237 * EnvConditions: N/A
238 * CaseDescription: 1. call CreateVSyncReceiver
239 * 2. call GetVSyncPeriodAndLastTimeStamp and check result
240 */
241 HWTEST_F(VsyncReceiverTest, GetVSyncPeriodAndLastTimeStamp001, Function | MediumTest| Level3)
242 {
243 onVsyncCount = 0;
244 auto& rsClient = RSInterfaces::GetInstance();
245 auto rsReceiver = rsClient.CreateVSyncReceiver("VsyncReceiverTest");
246
247 ASSERT_EQ(rsReceiver->Init(), VSYNC_ERROR_OK);
248 VSyncReceiver::FrameCallback fcb = {
249 .userData_ = this,
250 .callback_ = OnVSync,
251 };
252
253 ASSERT_EQ(rsReceiver->RequestNextVSync(fcb), VSYNC_ERROR_OK);
254 while (onVsyncCount == 0) {
255 sleep(1);
256 std::cout<< "OnVsync called count: " << onVsyncCount << std::endl;
257 }
258
259 int64_t period;
260 int64_t timeStamp;
261 ASSERT_EQ(rsReceiver->GetVSyncPeriodAndLastTimeStamp(period, timeStamp), VSYNC_ERROR_OK);
262 ASSERT_EQ(rsReceiver->GetVSyncPeriodAndLastTimeStamp(period, timeStamp, true), VSYNC_ERROR_UNKOWN);
263 ASSERT_NE(period, 0);
264 ASSERT_NE(timeStamp, 0);
265 rsReceiver->looper_->RemoveFileDescriptorListener(rsReceiver->fd_);
266 rsReceiver->looper_ = nullptr;
267 rsReceiver->fd_ = -1;
268 }
269
270 /*
271 * Function: SetVsyncCallBackForEveryFrame001
272 * Type: Function
273 * Rank: Important(2)
274 * EnvConditions: N/A
275 * CaseDescription: 1. call SetVsyncCallBackForEveryFrame
276 */
277 HWTEST_F(VsyncReceiverTest, SetVsyncCallBackForEveryFrame001, Function | MediumTest| Level3)
278 {
279 onVsyncCount = 0;
280 auto& rsClient = RSInterfaces::GetInstance();
281 auto rsReceiver = rsClient.CreateVSyncReceiver("VsyncReceiverTest");
282
283 ASSERT_EQ(rsReceiver->Init(), VSYNC_ERROR_OK);
284 VSyncReceiver::FrameCallback fcb = {
285 .userData_ = this,
286 .callback_ = OnVSync,
287 };
288
289 ASSERT_EQ(rsReceiver->RequestNextVSync(fcb), VSYNC_ERROR_OK);
290 while (onVsyncCount == 0) {
291 sleep(1);
292 std::cout<< "OnVsync called count: " << onVsyncCount << std::endl;
293 }
294
295 onVsyncCount = 0;
296 ASSERT_EQ(rsReceiver->SetVsyncCallBackForEveryFrame(fcb, true), VSYNC_ERROR_OK);
297 int64_t period = 0;
298 ASSERT_EQ(rsReceiver->GetVSyncPeriod(period), VSYNC_ERROR_OK);
299 usleep(period / 10);
300 ASSERT_EQ(rsReceiver->SetVsyncCallBackForEveryFrame(fcb, false), VSYNC_ERROR_OK);
301 sleep(1);
302 std::cout<< "OnVsync called count: " << onVsyncCount << " period: " << period << std::endl;
303 ASSERT_EQ(abs(onVsyncCount - 100) <= 5, true);
304 rsReceiver->looper_->RemoveFileDescriptorListener(rsReceiver->fd_);
305 rsReceiver->looper_ = nullptr;
306 rsReceiver->fd_ = -1;
307 }
308
309 /*
310 * Function: SetVSyncRate001
311 * Type: Function
312 * Rank: Important(2)
313 * EnvConditions: N/A
314 * CaseDescription: 1. call SetVSyncRate
315 */
316 HWTEST_F(VsyncReceiverTest, SetVSyncRate001, Function | MediumTest| Level3)
317 {
318 VSyncReceiver::FrameCallback fcb = {
319 .userData_ = this,
320 .callback_ = OnVSync,
321 };
322 vsyncDistributor->AddConnection(conn);
323 ASSERT_EQ(VsyncReceiverTest::vsyncReceiver->SetVSyncRate(fcb, 0), VSYNC_ERROR_INVALID_ARGUMENTS);
324 vsyncDistributor->RemoveConnection(conn);
325 }
326
327 /*
328 * Function: SetVSyncRate002
329 * Type: Function
330 * Rank: Important(2)
331 * EnvConditions: N/A
332 * CaseDescription: 1. call SetVSyncRate
333 */
334 HWTEST_F(VsyncReceiverTest, SetVSyncRate002, Function | MediumTest| Level3)
335 {
336 VSyncReceiver::FrameCallback fcb = {
337 .userData_ = this,
338 .callback_ = OnVSync,
339 };
340 vsyncDistributor->AddConnection(conn);
341 ASSERT_EQ(VsyncReceiverTest::vsyncReceiver->SetVSyncRate(fcb, 1), VSYNC_ERROR_OK);
342 ASSERT_EQ(VsyncReceiverTest::vsyncReceiver->SetVSyncRate(fcb, -1), VSYNC_ERROR_OK);
343 vsyncDistributor->RemoveConnection(conn);
344 }
345
346 /*
347 * Function: SendDataFailedTest
348 * Type: Function
349 * Rank: Important(2)
350 * EnvConditions: N/A
351 * CaseDescription: 1. test SendData Failed
352 */
353 HWTEST_F(VsyncReceiverTest, SendDataFailedTest, Function | MediumTest| Level3)
354 {
355 VSyncReceiver::FrameCallback fcb = {
356 .userData_ = this,
357 .callback_ = OnVSync2,
358 };
359 vsyncDistributor->AddConnection(conn);
360 for (int i = 0; i < 10; i++) { // test 10 times
361 ASSERT_EQ(vsyncReceiver->RequestNextVSync(fcb), VSYNC_ERROR_OK);
362 usleep(10000); // 10000us
363 }
364 sleep(1); // 1s
365 vsyncDistributor->RemoveConnection(conn);
366 }
367
368 /*
369 * Function: SetUiDvsyncSwitchTest
370 * Type: Function
371 * Rank: Important(2)
372 * EnvConditions: N/A
373 * CaseDescription: 1. test SetUiDvsyncSwitch
374 */
375 HWTEST_F(VsyncReceiverTest, SetUiDvsyncSwitchTest, Function | MediumTest| Level3)
376 {
377 vsyncDistributor->AddConnection(conn);
378 ASSERT_EQ(vsyncReceiver->SetUiDvsyncSwitch(true), VSYNC_ERROR_OK);
379 ASSERT_EQ(vsyncReceiver->SetUiDvsyncSwitch(false), VSYNC_ERROR_OK);
380 vsyncDistributor->RemoveConnection(conn);
381 }
382
383 /*
384 * Function: SetUiDvsyncConfigTest
385 * Type: Function
386 * Rank: Important(2)
387 * EnvConditions: N/A
388 * CaseDescription: 1. test SetUiDvsyncConfig
389 */
390 HWTEST_F(VsyncReceiverTest, SetUiDvsyncConfigTest, Function | MediumTest| Level3)
391 {
392 vsyncDistributor->AddConnection(conn);
393 ASSERT_EQ(vsyncReceiver->SetUiDvsyncConfig(1), VSYNC_ERROR_OK);
394 vsyncDistributor->RemoveConnection(conn);
395 }
396
397 /*
398 * Function: OnReadable001
399 * Type: Function
400 * Rank: Important(2)
401 * EnvConditions: N/A
402 * CaseDescription: 1. test OnReadable
403 */
404 HWTEST_F(VsyncReceiverTest, OnReadable001, Function | MediumTest| Level3)
405 {
406 onVsyncCount = 0;
407 auto& rsClient = RSInterfaces::GetInstance();
408 auto rsReceiver = rsClient.CreateVSyncReceiver("VsyncReceiverTest");
409
410 ASSERT_EQ(rsReceiver->Init(), VSYNC_ERROR_OK);
411 VSyncReceiver::FrameCallback fcb = {
412 .userData_ = this,
413 .callback_ = OnVSync,
414 };
415
416 ASSERT_EQ(rsReceiver->RequestNextVSync(fcb), VSYNC_ERROR_OK);
417 while (onVsyncCount == 0) {
418 sleep(1);
419 std::cout<< "OnVsync called count: " << onVsyncCount << std::endl;
420 }
421
422 onVsyncCount = 0;
423 ASSERT_EQ(rsReceiver->SetVsyncCallBackForEveryFrame(fcb, true), VSYNC_ERROR_OK);
424 int64_t period = 0;
425 ASSERT_EQ(rsReceiver->GetVSyncPeriod(period), VSYNC_ERROR_OK);
426 usleep(period / 10);
427 ASSERT_EQ(rsReceiver->SetVsyncCallBackForEveryFrame(fcb, false), VSYNC_ERROR_OK);
428 sleep(1);
429 std::cout<< "OnVsync called count: " << onVsyncCount << " period: " << period << std::endl;
430 ASSERT_EQ(abs(onVsyncCount - 100) <= 5, true);
431
432 rsReceiver->listener_->OnReadable(-1);
433 rsReceiver->listener_->OnReadable(999);
434 rsReceiver->looper_->RemoveFileDescriptorListener(rsReceiver->fd_);
435 rsReceiver->looper_ = nullptr;
436 rsReceiver->fd_ = -1;
437 }
438 } // namespace
439 } // namespace Rosen
440 } // namespace OHOS