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: AddSample001
50 * Type: Function
51 * Rank: Important(2)
52 * EnvConditions: N/A
53 * CaseDescription: 1. call AddSample
54  */
55 HWTEST_F(VSyncSamplerTest, AddSample001, Function | MediumTest| Level3)
56 {
57     ASSERT_EQ(VSyncSamplerTest::vsyncSampler->AddSample(0), true);
58     VSyncSamplerTest::vsyncSampler->Reset();
59 }
60 
61 /*
62 * Function: AddSample002
63 * Type: Function
64 * Rank: Important(2)
65 * EnvConditions: N/A
66 * CaseDescription: 1. call AddSample
67  */
68 HWTEST_F(VSyncSamplerTest, AddSample002, Function | MediumTest| Level3)
69 {
70     bool ret = true;
71     for (int i = 0; i < SAMPLER_NUMBER; i++) {
72         ret = VSyncSamplerTest::vsyncSampler->AddSample(i);
73     }
74     ASSERT_EQ(ret, false);
75     VSyncSamplerTest::vsyncSampler->Reset();
76 }
77 
78 /*
79 * Function: BeginSample001
80 * Type: Function
81 * Rank: Important(2)
82 * EnvConditions: N/A
83 * CaseDescription: 1. call BeginSample
84  */
85 HWTEST_F(VSyncSamplerTest, BeginSample001, Function | MediumTest| Level3)
86 {
87     bool ret = true;
88     for (int i = 0; i < SAMPLER_NUMBER; i++) {
89         ret = VSyncSamplerTest::vsyncSampler->AddSample(i);
90     }
91     ASSERT_EQ(ret, false);
92 
93     VSyncSamplerTest::vsyncSampler->BeginSample();
94     ASSERT_EQ(VSyncSamplerTest::vsyncSampler->AddSample(6), true);
95     VSyncSamplerTest::vsyncSampler->Reset();
96 }
97 
98 /*
99 * Function: GetPeriod001
100 * Type: Function
101 * Rank: Important(2)
102 * EnvConditions: N/A
103 * CaseDescription: 1. call GetPeriod
104  */
105 HWTEST_F(VSyncSamplerTest, GetPeriod001, Function | MediumTest| Level3)
106 {
107     ASSERT_EQ(VSyncSamplerTest::vsyncSampler->GetPeriod(), 0);
108 }
109 
110 /*
111 * Function: GetPeriod002
112 * Type: Function
113 * Rank: Important(2)
114 * EnvConditions: N/A
115 * CaseDescription: 1. call GetPeriod
116  */
117 HWTEST_F(VSyncSamplerTest, GetPeriod002, Function | MediumTest| Level3)
118 {
119     bool ret = true;
120     for (int i = 0; i < SAMPLER_NUMBER; i++) {
121         ret = VSyncSamplerTest::vsyncSampler->AddSample(i);
122     }
123     ASSERT_EQ(ret, false);
124     ASSERT_EQ(VSyncSamplerTest::vsyncSampler->GetPeriod(), 1);
125     VSyncSamplerTest::vsyncSampler->Reset();
126 }
127 
128 /*
129 * Function: GetPhase001
130 * Type: Function
131 * Rank: Important(2)
132 * EnvConditions: N/A
133 * CaseDescription: 1. call GetPhase
134  */
135 HWTEST_F(VSyncSamplerTest, GetPhase001, Function | MediumTest| Level3)
136 {
137     VSyncSamplerTest::vsyncSampler->AddSample(0);
138     ASSERT_EQ(VSyncSamplerTest::vsyncSampler->GetPhase(), 0);
139     VSyncSamplerTest::vsyncSampler->Reset();
140 }
141 
142 /*
143 * Function: GetPhase002
144 * Type: Function
145 * Rank: Important(2)
146 * EnvConditions: N/A
147 * CaseDescription: 1. call GetPhase
148  */
149 HWTEST_F(VSyncSamplerTest, GetPhase002, Function | MediumTest| Level3)
150 {
151     bool ret = true;
152     for (int i = 0; i < SAMPLER_NUMBER; i++) {
153         ret = VSyncSamplerTest::vsyncSampler->AddSample(i);
154     }
155     ASSERT_EQ(ret, false);
156     ASSERT_EQ(VSyncSamplerTest::vsyncSampler->GetPhase(), 0);
157     VSyncSamplerTest::vsyncSampler->Reset();
158 }
159 
160 /*
161 * Function: GetRefrenceTime001
162 * Type: Function
163 * Rank: Important(2)
164 * EnvConditions: N/A
165 * CaseDescription: 1. call GetRefrenceTime
166  */
167 HWTEST_F(VSyncSamplerTest, GetRefrenceTime001, Function | MediumTest| Level3)
168 {
169     ASSERT_EQ(VSyncSamplerTest::vsyncSampler->GetRefrenceTime(), 0);
170 }
171 
172 /*
173 * Function: GetRefrenceTime002
174 * Type: Function
175 * Rank: Important(2)
176 * EnvConditions: N/A
177 * CaseDescription: 1. call GetRefrenceTime
178  */
179 HWTEST_F(VSyncSamplerTest, GetRefrenceTime002, Function | MediumTest| Level3)
180 {
181     bool ret = true;
182     for (int i = 1; i < SAMPLER_NUMBER + 1; i++) {
183         ret = VSyncSamplerTest::vsyncSampler->AddSample(i);
184     }
185     ASSERT_EQ(ret, false);
186     ASSERT_EQ(VSyncSamplerTest::vsyncSampler->GetRefrenceTime(), 1);
187 }
188 
189 /*
190 * Function: GetHardwarePeriod001
191 * Type: Function
192 * Rank: Important(2)
193 * EnvConditions: N/A
194 * CaseDescription: 1. call GetHardwarePeriod
195  */
196 HWTEST_F(VSyncSamplerTest, GetHardwarePeriod001, Function | MediumTest| Level3)
197 {
198     ASSERT_EQ(VSyncSamplerTest::vsyncSampler->GetHardwarePeriod(), 1);
199 }
200 
201 /*
202 * Function: GetHardwarePeriod002
203 * Type: Function
204 * Rank: Important(2)
205 * EnvConditions: N/A
206 * CaseDescription: 1. call GetHardwarePeriod
207  */
208 HWTEST_F(VSyncSamplerTest, GetHardwarePeriod002, Function | MediumTest| Level3)
209 {
210     VSyncSamplerTest::vsyncSampler->SetPendingPeriod(1);
211     bool ret = true;
212     for (int i = 1; i < SAMPLER_NUMBER + 1; i++) {
213         ret = VSyncSamplerTest::vsyncSampler->AddSample(i);
214     }
215     ASSERT_EQ(ret, false);
216     VSyncSamplerTest::vsyncSampler->SetPendingPeriod(1);
217     ASSERT_EQ(VSyncSamplerTest::vsyncSampler->GetHardwarePeriod(), 1);
218     VSyncSamplerTest::vsyncSampler->Reset();
219 }
220 
221 /*
222 * Function: AddPresentFenceTime001
223 * Type: Function
224 * Rank: Important(2)
225 * EnvConditions: N/A
226 * CaseDescription: 1. call AddPresentFenceTime
227  */
228 HWTEST_F(VSyncSamplerTest, AddPresentFenceTime001, Function | MediumTest| Level3)
229 {
230     ASSERT_EQ(VSyncSamplerTest::vsyncSampler->AddPresentFenceTime(6), true);
231     VSyncSamplerTest::vsyncSampler->Reset();
232 }
233 
234 /*
235 * Function: AddPresentFenceTime002
236 * Type: Function
237 * Rank: Important(2)
238 * EnvConditions: N/A
239 * CaseDescription: 1. call AddPresentFenceTime
240  */
241 HWTEST_F(VSyncSamplerTest, AddPresentFenceTime002, Function | MediumTest| Level3)
242 {
243     bool ret = true;
244     for (int i = 1; i < SAMPLER_NUMBER + 1; i++) {
245         ret = VSyncSamplerTest::vsyncSampler->AddSample(i);
246     }
247     ASSERT_EQ(ret, false);
248     ASSERT_EQ(VSyncSamplerTest::vsyncSampler->AddPresentFenceTime(SAMPLER_NUMBER + 1), false);
249     VSyncSamplerTest::vsyncSampler->Reset();
250 }
251 } // namespace
252 } // namespace Rosen
253 } // namespace OHOS