• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 <gtest/gtest.h>
16 #include "vsync_sampler.h"
17 #include <iostream>
18 
19 using namespace testing;
20 using namespace testing::ext;
21 
22 namespace OHOS::Rosen {
23 namespace {
24 int64_t g_incrementalHardwareVsyncTimestamp = 0;
25 constexpr int32_t RANGE_PERCENT = 3; // 3%
26 constexpr int32_t FULL_PERCENT = 100; // 100%
27 }
28 class VSyncSampleTest : public testing::Test {
29 public:
30     int64_t GetHardwareVsyncTimestamp(int32_t refreshRate);
31     sptr<VSyncSampler> vsyncSampler = CreateVSyncSampler();
32 };
33 
GetHardwareVsyncTimestamp(int32_t refreshRate)34 int64_t VSyncSampleTest::GetHardwareVsyncTimestamp(int32_t refreshRate)
35 {
36     if (refreshRate == 0) {
37         return 0;
38     }
39     srand(time(0));
40     int64_t period = 1000000000 / refreshRate; // 1000000000ns
41     int randNum = rand() % FULL_PERCENT;
42     int64_t diff = period * RANGE_PERCENT / FULL_PERCENT;
43     int64_t timestampInterval = period - diff + (diff * 2) * randNum / FULL_PERCENT;
44     g_incrementalHardwareVsyncTimestamp += timestampInterval;
45     return g_incrementalHardwareVsyncTimestamp;
46 }
47 
48 /*
49 * Function: VSyncSampleTest
50 * Type: Function
51 * Rank: Important(2)
52 * EnvConditions: N/A
53 * CaseDescription: VSyncSample_30Hz_Test
54  */
55 HWTEST_F(VSyncSampleTest, VSyncSample_30Hz_Test, Function | MediumTest | Level2)
56 {
57     vsyncSampler->BeginSample();
58     for (int i = 0; i < 20; i++) { // test 20 times
59         bool keepSampling = vsyncSampler->AddSample(GetHardwareVsyncTimestamp(30));
60         if (!keepSampling) {
61             break;
62         }
63     }
64     int64_t period = vsyncSampler->GetPeriod();
65     EXPECT_GT(period, 32000000); // 32000000ns
66     EXPECT_LT(period, 35000000); // 35000000ns
67 }
68 
69 /*
70 * Function: VSyncSampleTest
71 * Type: Function
72 * Rank: Important(2)
73 * EnvConditions: N/A
74 * CaseDescription: VSyncSample_60Hz_Test
75  */
76 HWTEST_F(VSyncSampleTest, VSyncSample_60Hz_Test, Function | MediumTest | Level2)
77 {
78     vsyncSampler->BeginSample();
79     for (int i = 0; i < 20; i++) { // test 20 times
80         bool keepSampling = vsyncSampler->AddSample(GetHardwareVsyncTimestamp(60));
81         if (!keepSampling) {
82             break;
83         }
84     }
85     int64_t period = vsyncSampler->GetPeriod();
86     EXPECT_GT(period, 16000000); // 16000000ns
87     EXPECT_LT(period, 18000000); // 18000000ns
88 }
89 
90 /*
91 * Function: VSyncSampleTest
92 * Type: Function
93 * Rank: Important(2)
94 * EnvConditions: N/A
95 * CaseDescription: VSyncSample_90Hz_Test
96  */
97 HWTEST_F(VSyncSampleTest, VSyncSample_90Hz_Test, Function | MediumTest | Level2)
98 {
99     vsyncSampler->BeginSample();
100     for (int i = 0; i < 20; i++) { // test 20 times
101         bool keepSampling = vsyncSampler->AddSample(GetHardwareVsyncTimestamp(90));
102         if (!keepSampling) {
103             break;
104         }
105     }
106     int64_t period = vsyncSampler->GetPeriod();
107     EXPECT_GT(period, 10000000); // 10000000ns
108     EXPECT_LT(period, 12000000); // 12000000ns
109 }
110 
111 /*
112 * Function: VSyncSampleTest
113 * Type: Function
114 * Rank: Important(2)
115 * EnvConditions: N/A
116 * CaseDescription: VSyncSample_120Hz_Test
117  */
118 HWTEST_F(VSyncSampleTest, VSyncSample_120Hz_Test, Function | MediumTest | Level2)
119 {
120     vsyncSampler->BeginSample();
121     for (int i = 0; i < 20; i++) { // test 20 times
122         bool keepSampling = vsyncSampler->AddSample(GetHardwareVsyncTimestamp(120));
123         if (!keepSampling) {
124             break;
125         }
126     }
127     int64_t period = vsyncSampler->GetPeriod();
128     EXPECT_GT(period, 7000000); // 7000000ns
129     EXPECT_LT(period, 9000000); // 9000000ns
130 }
131 }
132