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 "vsync_sampler.h"
17
18 #include <gtest/gtest.h>
19
20 using namespace testing;
21 using namespace testing::ext;
22
23 namespace OHOS {
24 namespace Rosen {
25 namespace {
26 constexpr int32_t SAMPLER_NUMBER = 6;
27 }
28 class VSyncSamplerTest : public testing::Test {
29 public:
30 static void SetUpTestCase();
31 static void TearDownTestCase();
32
33 static inline sptr<VSyncSampler> vsyncSampler = nullptr;
34 static constexpr const int32_t WAIT_SYSTEM_ABILITY_REPORT_DATA_SECONDS = 5;
35 };
36
SetUpTestCase()37 void VSyncSamplerTest::SetUpTestCase()
38 {
39 vsyncSampler = CreateVSyncSampler();
40 }
41
TearDownTestCase()42 void VSyncSamplerTest::TearDownTestCase()
43 {
44 sleep(WAIT_SYSTEM_ABILITY_REPORT_DATA_SECONDS);
45 }
46
47 namespace {
48 /*
49 * Function: GetHardwarePeriodTest
50 * Type: Function
51 * Rank: Important(2)
52 * EnvConditions: N/A
53 * CaseDescription: 1. call GetHardwarePeriod
54 */
55 HWTEST_F(VSyncSamplerTest, GetHardwarePeriodTest, Function | MediumTest| Level3)
56 {
57 ASSERT_EQ(VSyncSamplerTest::vsyncSampler->GetHardwarePeriod(), 0);
58 }
59
60 /*
61 * Function: AddSample001
62 * Type: Function
63 * Rank: Important(2)
64 * EnvConditions: N/A
65 * CaseDescription: 1. call AddSample
66 */
67 HWTEST_F(VSyncSamplerTest, AddSample001, Function | MediumTest| Level3)
68 {
69 ASSERT_EQ(VSyncSamplerTest::vsyncSampler->AddSample(0), true);
70 VSyncSamplerTest::vsyncSampler->Reset();
71 }
72
73 /*
74 * Function: AddSample002
75 * Type: Function
76 * Rank: Important(2)
77 * EnvConditions: N/A
78 * CaseDescription: 1. call AddSample
79 */
80 HWTEST_F(VSyncSamplerTest, AddSample002, Function | MediumTest| Level3)
81 {
82 bool ret = true;
83 for (int i = 0; i < SAMPLER_NUMBER; i++) {
84 ret = VSyncSamplerTest::vsyncSampler->AddSample(i);
85 }
86 ASSERT_EQ(ret, false);
87 VSyncSamplerTest::vsyncSampler->Reset();
88 }
89
90 /*
91 * Function: BeginSample001
92 * Type: Function
93 * Rank: Important(2)
94 * EnvConditions: N/A
95 * CaseDescription: 1. call BeginSample
96 */
97 HWTEST_F(VSyncSamplerTest, BeginSample001, Function | MediumTest| Level3)
98 {
99 bool ret = true;
100 for (int i = 0; i < SAMPLER_NUMBER; i++) {
101 ret = VSyncSamplerTest::vsyncSampler->AddSample(i);
102 }
103 ASSERT_EQ(ret, false);
104
105 VSyncSamplerTest::vsyncSampler->BeginSample();
106 ASSERT_EQ(VSyncSamplerTest::vsyncSampler->AddSample(6), true);
107 VSyncSamplerTest::vsyncSampler->Reset();
108 }
109
110 /*
111 * Function: GetPeriod001
112 * Type: Function
113 * Rank: Important(2)
114 * EnvConditions: N/A
115 * CaseDescription: 1. call GetPeriod
116 */
117 HWTEST_F(VSyncSamplerTest, GetPeriod001, Function | MediumTest| Level3)
118 {
119 ASSERT_EQ(VSyncSamplerTest::vsyncSampler->GetPeriod(), 0);
120 }
121
122 /*
123 * Function: GetPeriod002
124 * Type: Function
125 * Rank: Important(2)
126 * EnvConditions: N/A
127 * CaseDescription: 1. call GetPeriod
128 */
129 HWTEST_F(VSyncSamplerTest, GetPeriod002, Function | MediumTest| Level3)
130 {
131 bool ret = true;
132 for (int i = 0; i < SAMPLER_NUMBER; i++) {
133 ret = VSyncSamplerTest::vsyncSampler->AddSample(i);
134 }
135 ASSERT_EQ(ret, false);
136 ASSERT_EQ(VSyncSamplerTest::vsyncSampler->GetPeriod(), 1);
137 VSyncSamplerTest::vsyncSampler->Reset();
138 }
139
140 /*
141 * Function: GetPhase001
142 * Type: Function
143 * Rank: Important(2)
144 * EnvConditions: N/A
145 * CaseDescription: 1. call GetPhase
146 */
147 HWTEST_F(VSyncSamplerTest, GetPhase001, Function | MediumTest| Level3)
148 {
149 VSyncSamplerTest::vsyncSampler->AddSample(0);
150 ASSERT_EQ(VSyncSamplerTest::vsyncSampler->GetPhase(), 0);
151 VSyncSamplerTest::vsyncSampler->Reset();
152 }
153
154 /*
155 * Function: GetPhase002
156 * Type: Function
157 * Rank: Important(2)
158 * EnvConditions: N/A
159 * CaseDescription: 1. call GetPhase
160 */
161 HWTEST_F(VSyncSamplerTest, GetPhase002, Function | MediumTest| Level3)
162 {
163 bool ret = true;
164 for (int i = 0; i < SAMPLER_NUMBER; i++) {
165 ret = VSyncSamplerTest::vsyncSampler->AddSample(i);
166 }
167 ASSERT_EQ(ret, false);
168 ASSERT_EQ(VSyncSamplerTest::vsyncSampler->GetPhase(), 0);
169 VSyncSamplerTest::vsyncSampler->Reset();
170 }
171
172 /*
173 * Function: GetRefrenceTime001
174 * Type: Function
175 * Rank: Important(2)
176 * EnvConditions: N/A
177 * CaseDescription: 1. call GetRefrenceTime
178 */
179 HWTEST_F(VSyncSamplerTest, GetRefrenceTime001, Function | MediumTest| Level3)
180 {
181 ASSERT_EQ(VSyncSamplerTest::vsyncSampler->GetRefrenceTime(), 0);
182 }
183
184 /*
185 * Function: GetRefrenceTime002
186 * Type: Function
187 * Rank: Important(2)
188 * EnvConditions: N/A
189 * CaseDescription: 1. call GetRefrenceTime
190 */
191 HWTEST_F(VSyncSamplerTest, GetRefrenceTime002, Function | MediumTest| Level3)
192 {
193 bool ret = true;
194 for (int i = 1; i < SAMPLER_NUMBER + 1; i++) {
195 ret = VSyncSamplerTest::vsyncSampler->AddSample(i);
196 }
197 ASSERT_EQ(ret, false);
198 ASSERT_EQ(VSyncSamplerTest::vsyncSampler->GetRefrenceTime(), 1);
199 }
200
201 /*
202 * Function: GetHardwarePeriod001
203 * Type: Function
204 * Rank: Important(2)
205 * EnvConditions: N/A
206 * CaseDescription: 1. call GetHardwarePeriod
207 */
208 HWTEST_F(VSyncSamplerTest, GetHardwarePeriod001, Function | MediumTest| Level3)
209 {
210 ASSERT_EQ(VSyncSamplerTest::vsyncSampler->GetHardwarePeriod(), 1);
211 }
212
213 /*
214 * Function: GetHardwarePeriod002
215 * Type: Function
216 * Rank: Important(2)
217 * EnvConditions: N/A
218 * CaseDescription: 1. call GetHardwarePeriod
219 */
220 HWTEST_F(VSyncSamplerTest, GetHardwarePeriod002, Function | MediumTest| Level3)
221 {
222 VSyncSamplerTest::vsyncSampler->SetPendingPeriod(1);
223 bool ret = true;
224 for (int i = 1; i < SAMPLER_NUMBER + 1; i++) {
225 ret = VSyncSamplerTest::vsyncSampler->AddSample(i);
226 }
227 ASSERT_EQ(ret, false);
228 VSyncSamplerTest::vsyncSampler->SetPendingPeriod(1);
229 ASSERT_EQ(VSyncSamplerTest::vsyncSampler->GetHardwarePeriod(), 1);
230 VSyncSamplerTest::vsyncSampler->Reset();
231 }
232
233 /*
234 * Function: GetHardwarePeriod003
235 * Type: Function
236 * Rank: Important(2)
237 * EnvConditions: N/A
238 * CaseDescription: 1. call GetHardwarePeriod
239 */
240 HWTEST_F(VSyncSamplerTest, GetHardwarePeriod003, Function | MediumTest| Level3)
241 {
242 VSyncSamplerTest::vsyncSampler->SetPendingPeriod(16666667); // 16666667ns
243 bool ret = true;
244 for (int i = 1; i <= 50; i++) { // add 50 samples
245 ret = VSyncSamplerTest::vsyncSampler->AddSample(i * 16666667); // 16666667ns
246 }
247 ASSERT_EQ(ret, false);
248 VSyncSamplerTest::vsyncSampler->SetPendingPeriod(8333333); // 8333333ns
249 ASSERT_EQ(VSyncSamplerTest::vsyncSampler->GetHardwarePeriod(), 16666667); // 16666667ns
250 VSyncSamplerTest::vsyncSampler->BeginSample();
251 VSyncSamplerTest::vsyncSampler->SetPendingPeriod(8333333); // 8333333ns
252 ASSERT_EQ(VSyncSamplerTest::vsyncSampler->GetHardwarePeriod(), 8333333); // 8333333ns
253 VSyncSamplerTest::vsyncSampler->SetPendingPeriod(0);
254 ASSERT_EQ(VSyncSamplerTest::vsyncSampler->GetHardwarePeriod(), 8333333); // 8333333ns
255 VSyncSamplerTest::vsyncSampler->SetPendingPeriod(-1);
256 ASSERT_EQ(VSyncSamplerTest::vsyncSampler->GetHardwarePeriod(), 8333333); // 8333333ns
257 VSyncSamplerTest::vsyncSampler->Reset();
258 }
259
260 /*
261 * Function: AddPresentFenceTime001
262 * Type: Function
263 * Rank: Important(2)
264 * EnvConditions: N/A
265 * CaseDescription: 1. call AddPresentFenceTime
266 */
267 HWTEST_F(VSyncSamplerTest, AddPresentFenceTime001, Function | MediumTest| Level3)
268 {
269 VSyncSamplerTest::vsyncSampler->SetVsyncEnabledScreenId(0);
270 ASSERT_EQ(VSyncSamplerTest::vsyncSampler->AddPresentFenceTime(0, 6), true);
271 VSyncSamplerTest::vsyncSampler->Reset();
272 }
273
274 /*
275 * Function: AddPresentFenceTime002
276 * Type: Function
277 * Rank: Important(2)
278 * EnvConditions: N/A
279 * CaseDescription: 1. call AddPresentFenceTime
280 */
281 HWTEST_F(VSyncSamplerTest, AddPresentFenceTime002, Function | MediumTest| Level3)
282 {
283 bool ret = true;
284 for (int i = 1; i < SAMPLER_NUMBER + 1; i++) {
285 ret = VSyncSamplerTest::vsyncSampler->AddSample(i);
286 }
287 ASSERT_EQ(ret, false);
288 VSyncSamplerTest::vsyncSampler->SetVsyncEnabledScreenId(0);
289 ASSERT_EQ(VSyncSamplerTest::vsyncSampler->AddPresentFenceTime(0, SAMPLER_NUMBER + 1), false);
290 VSyncSamplerTest::vsyncSampler->Reset();
291 }
292
293 /*
294 * Function: AddPresentFenceTime003
295 * Type: Function
296 * Rank: Important(2)
297 * EnvConditions: N/A
298 * CaseDescription: 1. preSetup: set vsyncEnabledScreenId 0
299 * 2. operation: test AddPresentFenceTime with screenId 0
300 * 3. result: AddPresentFenceTime function return true
301 */
302 HWTEST_F(VSyncSamplerTest, AddPresentFenceTime003, Function | MediumTest| Level3)
303 {
304 VSyncSamplerTest::vsyncSampler->SetVsyncEnabledScreenId(0);
305 ASSERT_EQ(VSyncSamplerTest::vsyncSampler->AddPresentFenceTime(0, 0), true); // screenId 0
306 VSyncSamplerTest::vsyncSampler->Reset();
307 }
308
309 /*
310 * Function: AddPresentFenceTime004
311 * Type: Function
312 * Rank: Important(2)
313 * EnvConditions: N/A
314 * CaseDescription: 1. preSetup: set vsyncEnabledScreenId 0
315 * 2. operation: test AddPresentFenceTime with screenId 1
316 * 3. result: AddPresentFenceTime function return false
317 */
318 HWTEST_F(VSyncSamplerTest, AddPresentFenceTime004, Function | MediumTest| Level3)
319 {
320 VSyncSamplerTest::vsyncSampler->SetVsyncEnabledScreenId(0);
321 ASSERT_EQ(VSyncSamplerTest::vsyncSampler->AddPresentFenceTime(1, 0), false); // screenId 1
322 VSyncSamplerTest::vsyncSampler->Reset();
323 }
324
325 /*
326 * Function: DumpTest
327 * Type: Function
328 * Rank: Important(2)
329 * EnvConditions: N/A
330 * CaseDescription: 1. call Dump
331 */
332 HWTEST_F(VSyncSamplerTest, DumpTest, Function | MediumTest| Level3)
333 {
334 for (int i = 1; i <= 10; i++) { // add 10 samples
335 VSyncSamplerTest::vsyncSampler->AddSample(i * 16666667); // 16666667ns
336 }
337 std::string result = "";
338 VSyncSamplerTest::vsyncSampler->Dump(result);
339 ASSERT_NE(result.find("VSyncSampler"), std::string::npos);
340 }
341
342 /*
343 * Function: RegSetScreenVsyncEnabledCallbackTest
344 * Type: Function
345 * Rank: Important(2)
346 * EnvConditions: N/A
347 * CaseDescription: 1. call RegSetScreenVsyncEnabledCallback
348 */
349 HWTEST_F(VSyncSamplerTest, RegSetScreenVsyncEnabledCallbackTest, Function | MediumTest| Level3)
350 {
351 bool result = false;
__anoned72ebc90302(bool enabled) 352 VSyncSampler::SetScreenVsyncEnabledCallback cb = [&result](bool enabled) {
353 result = enabled;
354 };
355 VSyncSamplerTest::vsyncSampler->RegSetScreenVsyncEnabledCallback(cb);
356 VSyncSamplerTest::vsyncSampler->SetScreenVsyncEnabledInRSMainThread(true);
357 ASSERT_EQ(result, true);
358 VSyncSamplerTest::vsyncSampler->SetScreenVsyncEnabledInRSMainThread(false);
359 ASSERT_EQ(result, false);
360 }
361
362 /*
363 * Function: AddNegativeSamplesTest
364 * Type: Function
365 * Rank: Important(2)
366 * EnvConditions: N/A
367 * CaseDescription: 1. test add negative samples
368 */
369 HWTEST_F(VSyncSamplerTest, AddNegativeSamplesTest, Function | MediumTest| Level3)
370 {
371 VSyncSamplerTest::vsyncSampler->BeginSample();
372 bool ret = true;
373 for (int i = 1; i < SAMPLER_NUMBER + 1; i++) { // add 10 samples
374 ret = VSyncSamplerTest::vsyncSampler->AddSample(i * -16666667); // 16666667ns
375 }
376 ASSERT_EQ(ret, true);
377 }
378
379 /*
380 * Function: AddSamplesVarianceOversizeTest
381 * Type: Function
382 * Rank: Important(2)
383 * EnvConditions: N/A
384 * CaseDescription: 1. test samples variance oversize
385 */
386 HWTEST_F(VSyncSamplerTest, AddSamplesVarianceOversizeTest, Function | MediumTest| Level3)
387 {
388 VSyncSamplerTest::vsyncSampler->BeginSample();
389 int64_t floatingScope = 1000000; // 1000000ms
390 bool ret = true;
391 for (int i = 1; i <= 50; i++) { // add 50 samples
392 floatingScope *= -1;
393 ret = VSyncSamplerTest::vsyncSampler->AddSample(i * 16666667 + floatingScope); // 16666667ns
394 }
395 ASSERT_EQ(ret, true);
396 }
397 } // namespace
398 } // namespace Rosen
399 } // namespace OHOS