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 
35     static inline sptr<VSyncController> vsyncController = nullptr;
36     static inline sptr<VSyncDistributor> vsyncDistributor = nullptr;
37     static inline sptr<VSyncGenerator> vsyncGenerator = nullptr;
38     static inline sptr<VSyncConnection> conn = nullptr;
39     static inline sptr<VSyncReceiver> vsyncReceiver = nullptr;
40     static inline int32_t onVsyncCount = 0;
41 };
42 
OnVSync(int64_t now,void * data)43 void VsyncReceiverTest::OnVSync(int64_t now, void* data)
44 {
45     onVsyncCount ++;
46 }
47 
SetUpTestCase()48 void VsyncReceiverTest::SetUpTestCase()
49 {
50     vsyncGenerator = CreateVSyncGenerator();
51     vsyncController = new VSyncController(vsyncGenerator, 0);
52     vsyncDistributor = new VSyncDistributor(vsyncController, "VsyncReceiverTest");
53     conn = new VSyncConnection(vsyncDistributor, "VsyncReceiverTest");
54     vsyncReceiver = new VSyncReceiver(conn);
55 }
56 
TearDownTestCase()57 void VsyncReceiverTest::TearDownTestCase()
58 {
59     vsyncReceiver = nullptr;
60     vsyncController = nullptr;
61     vsyncGenerator = nullptr;
62     vsyncDistributor = nullptr;
63     conn = nullptr;
64     DestroyVSyncGenerator();
65 }
66 
67 namespace {
68 /*
69 * Function: BeforeInit001
70 * Type: Function
71 * Rank: Important(2)
72 * EnvConditions: N/A
73 * CaseDescription: 1. call RequestNextVSync Before Init
74                    2. call SetVSyncRate Before Init
75                    3. call the one-parameter method of CreateVSyncReceiver
76                    4. call GetVSyncPeriodAndLastTimeStamp Before Init of vSync receiver in 3.
77                    5. call the two-parameter method of CreateVSyncReceiver
78                    6. call GetVSyncPeriodAndLastTimeStamp Before Init of vSync receiver in 5.
79                    7. call the four-parameter method of CreateVSyncReceiver
80                    8. call GetVSyncPeriodAndLastTimeStamp Before Init of vSync receiver in 7.
81  */
82 HWTEST_F(VsyncReceiverTest, BeforeInit001, Function | MediumTest| Level3)
83 {
84     VSyncReceiver::FrameCallback fcb = {
85         .userData_ = this,
86         .callback_ = OnVSync,
87     };
88     ASSERT_EQ(VsyncReceiverTest::vsyncReceiver->RequestNextVSync(fcb), VSYNC_ERROR_NOT_INIT);
89     ASSERT_EQ(VsyncReceiverTest::vsyncReceiver->SetVSyncRate(fcb, 0), VSYNC_ERROR_API_FAILED);
90 
91     int64_t period;
92     int64_t timeStamp;
93     auto& rsClient = RSInterfaces::GetInstance();
94     auto rsReceiver = rsClient.CreateVSyncReceiver("VsyncReceiverTest");
95     ASSERT_EQ(rsReceiver->GetVSyncPeriodAndLastTimeStamp(period, timeStamp), VSYNC_ERROR_NOT_INIT);
96     rsReceiver = rsClient.CreateVSyncReceiver("VsyncReceiverTest", nullptr);
97     ASSERT_EQ(rsReceiver->GetVSyncPeriodAndLastTimeStamp(period, timeStamp), VSYNC_ERROR_NOT_INIT);
98     rsReceiver = rsClient.CreateVSyncReceiver("VsyncReceiverTest", 0, nullptr, 0);
99     ASSERT_EQ(rsReceiver->GetVSyncPeriodAndLastTimeStamp(period, timeStamp), VSYNC_ERROR_NOT_INIT);
100 }
101 
102 /*
103 * Function: RequestNextVSyncWithMultiCallback
104 * Type: Function
105 * Rank: Important(2)
106 * EnvConditions: N/A
107 * CaseDescription: 1. call RequestNextVSyncWithMultiCallback before Init.
108  */
109 HWTEST_F(VsyncReceiverTest, RequestNextVSyncWithMultiCallback001, Function | MediumTest| Level3)
110 {
111     VSyncReceiver::FrameCallback fcb = {
112         .userData_ = this,
113         .callback_ = OnVSync,
114     };
115     vsyncDistributor->AddConnection(conn);
116     ASSERT_EQ(VsyncReceiverTest::vsyncReceiver->RequestNextVSyncWithMultiCallback(fcb), VSYNC_ERROR_NOT_INIT);
117     vsyncDistributor->RemoveConnection(conn);
118 }
119 
120 /*
121 * Function: Init001
122 * Type: Function
123 * Rank: Important(2)
124 * EnvConditions: N/A
125 * CaseDescription: 1. call Init
126  */
127 HWTEST_F(VsyncReceiverTest, Init001, Function | MediumTest| Level3)
128 {
129     ASSERT_EQ(VsyncReceiverTest::vsyncReceiver->Init(), VSYNC_ERROR_OK);
130     ASSERT_EQ(VsyncReceiverTest::vsyncReceiver->Init(), VSYNC_ERROR_OK);
131 }
132 
133 /*
134 * Function: Init002
135 * Type: Function
136 * Rank: Important(2)
137 * EnvConditions: N/A
138 * CaseDescription: 1. call Init
139  */
140 HWTEST_F(VsyncReceiverTest, Init002, Function | MediumTest| Level3)
141 {
142     sptr<IVSyncConnection> connection_ = nullptr;
143     ASSERT_NE(VsyncReceiverTest::vsyncReceiver->Init(), VSYNC_ERROR_NULLPTR);
144 }
145 
146 /*
147 * Function: IsRequestedNextVSync001
148 * Type: Function
149 * Rank: Important(2)
150 * EnvConditions: N/A
151 * CaseDescription: 1. call IsRequestedNextVSync
152  */
153 HWTEST_F(VsyncReceiverTest, IsRequestedNextVSync001, Function | MediumTest| Level3)
154 {
155     ASSERT_EQ(VsyncReceiverTest::vsyncReceiver->IsRequestedNextVSync(), false);
156 }
157 
158 /*
159 * Function: RequestNextVSync001
160 * Type: Function
161 * Rank: Important(2)
162 * EnvConditions: N/A
163 * CaseDescription: 1. call RequestNextVSync
164  */
165 HWTEST_F(VsyncReceiverTest, RequestNextVSync001, Function | MediumTest| Level3)
166 {
167     VSyncReceiver::FrameCallback fcb = {
168         .userData_ = this,
169         .callback_ = OnVSync,
170     };
171     vsyncDistributor->AddConnection(conn);
172     ASSERT_EQ(VsyncReceiverTest::vsyncReceiver->RequestNextVSync(fcb), VSYNC_ERROR_OK);
173     vsyncDistributor->RemoveConnection(conn);
174 }
175 
176 /*
177 * Function: RequestNextVSync002
178 * Type: Function
179 * Rank: Important(2)
180 * EnvConditions: N/A
181 * CaseDescription: 1. call RequestNextVSync
182  */
183 HWTEST_F(VsyncReceiverTest, RequestNextVSync002, Function | MediumTest| Level3)
184 {
185     VSyncReceiver::FrameCallback fcb = {
186         .userData_ = this,
187         .callback_ = OnVSync,
188     };
189     vsyncDistributor->AddConnection(conn);
190     ASSERT_EQ(VsyncReceiverTest::vsyncReceiver->RequestNextVSync(fcb, "unknown", 0), VSYNC_ERROR_OK);
191     vsyncDistributor->RemoveConnection(conn);
192 }
193 
194 /*
195 * Function: RequestNextVSyncWithMultiCallback
196 * Type: Function
197 * Rank: Important(2)
198 * EnvConditions: N/A
199 * CaseDescription: 1. call RequestNextVSyncWithMultiCallback after Init.
200  */
201 HWTEST_F(VsyncReceiverTest, RequestNextVSyncWithMultiCallback002, Function | MediumTest| Level3)
202 {
203     VSyncReceiver::FrameCallback fcb = {
204         .userData_ = this,
205         .callback_ = OnVSync,
206     };
207     vsyncDistributor->AddConnection(conn);
208     ASSERT_EQ(VsyncReceiverTest::vsyncReceiver->RequestNextVSyncWithMultiCallback(fcb), VSYNC_ERROR_OK);
209     vsyncDistributor->RemoveConnection(conn);
210 }
211 
212 /*
213 * Function: IsRequestedNextVSync002
214 * Type: Function
215 * Rank: Important(2)
216 * EnvConditions: N/A
217 * CaseDescription: 1. call IsRequestedNextVSync
218  */
219 HWTEST_F(VsyncReceiverTest, IsRequestedNextVSync002, Function | MediumTest| Level3)
220 {
221     ASSERT_EQ(VsyncReceiverTest::vsyncReceiver->IsRequestedNextVSync(), true);
222 }
223 
224 /*
225 * Function: GetVSyncPeriodAndLastTimeStamp001
226 * Type: Function
227 * Rank: Important(2)
228 * EnvConditions: N/A
229 * CaseDescription: 1. call CreateVSyncReceiver
230 *                  2. call GetVSyncPeriodAndLastTimeStamp and check result
231  */
232 HWTEST_F(VsyncReceiverTest, GetVSyncPeriodAndLastTimeStamp001, Function | MediumTest| Level3)
233 {
234     onVsyncCount = 0;
235     auto& rsClient = RSInterfaces::GetInstance();
236     auto rsReceiver = rsClient.CreateVSyncReceiver("VsyncReceiverTest");
237 
238     ASSERT_EQ(rsReceiver->Init(), VSYNC_ERROR_OK);
239     VSyncReceiver::FrameCallback fcb = {
240         .userData_ = this,
241         .callback_ = OnVSync,
242     };
243 
244     ASSERT_EQ(rsReceiver->RequestNextVSync(fcb), VSYNC_ERROR_OK);
245     while (onVsyncCount == 0) {
246         sleep(1);
247         std::cout<< "OnVsync called count: " << onVsyncCount << std::endl;
248     }
249 
250     int64_t period;
251     int64_t timeStamp;
252     ASSERT_EQ(rsReceiver->GetVSyncPeriodAndLastTimeStamp(period, timeStamp), VSYNC_ERROR_OK);
253     ASSERT_EQ(rsReceiver->GetVSyncPeriodAndLastTimeStamp(period, timeStamp, true), VSYNC_ERROR_UNKOWN);
254     ASSERT_NE(period, 0);
255     ASSERT_NE(timeStamp, 0);
256 }
257 
258 /*
259 * Function: SetVsyncCallBackForEveryFrame001
260 * Type: Function
261 * Rank: Important(2)
262 * EnvConditions: N/A
263 * CaseDescription: 1. call SetVsyncCallBackForEveryFrame
264  */
265 HWTEST_F(VsyncReceiverTest, SetVsyncCallBackForEveryFrame001, Function | MediumTest| Level3)
266 {
267     onVsyncCount = 0;
268     auto& rsClient = RSInterfaces::GetInstance();
269     auto rsReceiver = rsClient.CreateVSyncReceiver("VsyncReceiverTest");
270 
271     ASSERT_EQ(rsReceiver->Init(), VSYNC_ERROR_OK);
272     VSyncReceiver::FrameCallback fcb = {
273         .userData_ = this,
274         .callback_ = OnVSync,
275     };
276 
277     ASSERT_EQ(rsReceiver->RequestNextVSync(fcb), VSYNC_ERROR_OK);
278     while (onVsyncCount == 0) {
279         sleep(1);
280         std::cout<< "OnVsync called count: " << onVsyncCount << std::endl;
281     }
282 
283     onVsyncCount = 0;
284     ASSERT_EQ(rsReceiver->SetVsyncCallBackForEveryFrame(fcb, true), VSYNC_ERROR_OK);
285     int64_t period = 0;
286     ASSERT_EQ(rsReceiver->GetVSyncPeriod(period), VSYNC_ERROR_OK);
287     usleep(period / 10);
288     ASSERT_EQ(rsReceiver->SetVsyncCallBackForEveryFrame(fcb, false), VSYNC_ERROR_OK);
289     sleep(1);
290     std::cout<< "OnVsync called count: " << onVsyncCount << " period: " << period << std::endl;
291     ASSERT_EQ(abs(onVsyncCount - 100) <= 5, true);
292 }
293 
294 /*
295 * Function: SetVSyncRate001
296 * Type: Function
297 * Rank: Important(2)
298 * EnvConditions: N/A
299 * CaseDescription: 1. call SetVSyncRate
300  */
301 HWTEST_F(VsyncReceiverTest, SetVSyncRate001, Function | MediumTest| Level3)
302 {
303     VSyncReceiver::FrameCallback fcb = {
304         .userData_ = this,
305         .callback_ = OnVSync,
306     };
307     vsyncDistributor->AddConnection(conn);
308     ASSERT_EQ(VsyncReceiverTest::vsyncReceiver->SetVSyncRate(fcb, 0), VSYNC_ERROR_INVALID_ARGUMENTS);
309     vsyncDistributor->RemoveConnection(conn);
310 }
311 
312 /*
313 * Function: SetVSyncRate002
314 * Type: Function
315 * Rank: Important(2)
316 * EnvConditions: N/A
317 * CaseDescription: 1. call SetVSyncRate
318  */
319 HWTEST_F(VsyncReceiverTest, SetVSyncRate002, Function | MediumTest| Level3)
320 {
321     VSyncReceiver::FrameCallback fcb = {
322         .userData_ = this,
323         .callback_ = OnVSync,
324     };
325     vsyncDistributor->AddConnection(conn);
326     ASSERT_EQ(VsyncReceiverTest::vsyncReceiver->SetVSyncRate(fcb, 1), VSYNC_ERROR_OK);
327     vsyncDistributor->RemoveConnection(conn);
328 }
329 } // namespace
330 } // namespace Rosen
331 } // namespace OHOS