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