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 
16 #include <gtest/gtest.h>
17 #include <test_header.h>
18 
19 #include "hgm_idle_detector.h"
20 
21 using namespace testing;
22 using namespace testing::ext;
23 
24 namespace OHOS {
25 namespace Rosen {
26 namespace {
27     const std::string bufferName = "frameBuffer";
28     const std::string aceAnimator = "AceAnimato";
29     const std::string rosenWeb = "RosenWeb";
30     const std::string flutterBuffer = "oh_flutter";
31     const std::string otherSurface = "Other_SF";
32     constexpr uint64_t  currTime = 100000000;
33     constexpr uint64_t  lastTime = 200000000;
34     constexpr uint32_t  fps30HZ = 30;
35     constexpr uint32_t  fps60HZ = 60;
36     constexpr uint32_t  fps90HZ = 90;
37     constexpr uint32_t  fps120HZ = 120;
38     constexpr pid_t Pid = 0;
39 }
40 class HgmIdleDetectorTest : public testing::Test {
41 public:
42     static void SetUpTestCase();
43     static void TearDownTestCase();
44     void SetUp();
45     void TearDown();
46 };
47 
SetUpTestCase()48 void HgmIdleDetectorTest::SetUpTestCase() {}
TearDownTestCase()49 void HgmIdleDetectorTest::TearDownTestCase() {}
SetUp()50 void HgmIdleDetectorTest::SetUp() {}
TearDown()51 void HgmIdleDetectorTest::TearDown() {}
52 
53 /**
54  * @tc.name: SetAndGetAppSupportState
55  * @tc.desc: Verify the result of SetAndGetAppSupportState function
56  * @tc.type: FUNC
57  * @tc.require: IAFG2V
58  */
59 HWTEST_F(HgmIdleDetectorTest, SetAndGetAppSupportState, Function | SmallTest | Level1)
60 {
61     std::unique_ptr<HgmIdleDetector> idleDetector = std::make_unique<HgmIdleDetector>();
62 
63     PART("CaseDescription") {
64         STEP("1. get an idleDetector") {
65             STEP_ASSERT_NE(idleDetector, nullptr);
66         }
67         STEP("2. set app support status") {
68             idleDetector->SetAppSupportedState(false);
69         }
70         STEP("3. get app support status") {
71             bool ret = idleDetector->GetAppSupportedState();
72             STEP_ASSERT_EQ(ret, false);
73         }
74     }
75 }
76 
77 /**
78  * @tc.name: SetAndGetAceAnimatorIdleState
79  * @tc.desc: Verify the result of SetAndGetAceAnimatorIdleState function
80  * @tc.type: FUNC
81  * @tc.require: IAFG2V
82  */
83 HWTEST_F(HgmIdleDetectorTest, SetAndGetAceAnimatorIdleState, Function | SmallTest | Level1)
84 {
85     std::unique_ptr<HgmIdleDetector> idleDetector = std::make_unique<HgmIdleDetector>();
86 
87     PART("CaseDescription") {
88         STEP("1. get an idleDetector") {
89             STEP_ASSERT_NE(idleDetector, nullptr);
90         }
91         STEP("2. set aceAnimator idle status") {
92             idleDetector->SetAceAnimatorIdleState(false);
93         }
94         STEP("3. get aceAnimator idle status") {
95             bool ret = idleDetector->GetAceAnimatorIdleState();
96             STEP_ASSERT_EQ(ret, false);
97         }
98     }
99 }
100 
101 /**
102  * @tc.name: SetAndGetSurfaceTimeState
103  * @tc.desc: Verify the result of SetAndGetSurfaceTimeState function
104  * @tc.type: FUNC
105  * @tc.require: IAFG2V
106  */
107 HWTEST_F(HgmIdleDetectorTest, SetAndGetSurfaceTimeState, Function | SmallTest | Level1)
108 {
109     std::unique_ptr<HgmIdleDetector> idleDetector = std::make_unique<HgmIdleDetector>();
110 
111     PART("CaseDescription") {
112         STEP("1. get an idleDetector") {
113             STEP_ASSERT_NE(idleDetector, nullptr);
114         }
115         STEP("2. set app support status") {
116             idleDetector->SetAppSupportedState(true);
117             idleDetector->supportAppBufferList_.insert(idleDetector->supportAppBufferList_.begin(), otherSurface);
118         }
119         STEP("3. set buffer renew time") {
120             idleDetector->UpdateSurfaceTime(bufferName, currTime, Pid);
121         }
122         STEP("4. get buffer idle state") {
123             bool ret = idleDetector->GetSurfaceIdleState(lastTime);
124             STEP_ASSERT_EQ(ret, false);
125         }
126     }
127 }
128 
129 /**
130  * @tc.name: ThirdFrameNeedHighRefresh
131  * @tc.desc: Verify the result of ThirdFrameNeedHighRefresh function
132  * @tc.type: FUNC
133  * @tc.require: IAFG2V
134  */
135 HWTEST_F(HgmIdleDetectorTest, ThirdFrameNeedHighRefresh, Function | SmallTest | Level1)
136 {
137     std::unique_ptr<HgmIdleDetector> idleDetector = std::make_unique<HgmIdleDetector>();
138 
139     PART("CaseDescription") {
140         STEP("1. get an idleDetector") {
141             STEP_ASSERT_NE(idleDetector, nullptr);
142         }
143         STEP("2. set surface date") {
144             idleDetector->SetAppSupportedState(true);
145             idleDetector->ClearAppBufferBlackList();
146             bool ret = idleDetector->ThirdFrameNeedHighRefresh();
147             STEP_ASSERT_EQ(ret, true);
148 
149             idleDetector->appBufferBlackList_.push_back(aceAnimator);
150             ret = idleDetector->ThirdFrameNeedHighRefresh();
151             STEP_ASSERT_EQ(ret, false);
152 
153             idleDetector->frameTimeMap_[bufferName] = currTime;
154             ret = idleDetector->ThirdFrameNeedHighRefresh();
155             STEP_ASSERT_EQ(ret, true);
156 
157             idleDetector->appBufferBlackList_.push_back(bufferName);
158             ret = idleDetector->ThirdFrameNeedHighRefresh();
159             STEP_ASSERT_EQ(ret, false);
160 
161             idleDetector->appBufferBlackList_.push_back(otherSurface);
162             ret = idleDetector->ThirdFrameNeedHighRefresh();
163             STEP_ASSERT_EQ(ret, false);
164         }
165     }
166 }
167 
168 /**
169  * @tc.name: GetTouchUpExpectedFPS001
170  * @tc.desc: Verify the result of GetTouchUpExpectedFPS001 function
171  * @tc.type: FUNC
172  * @tc.require: IAFG2V
173  */
174 HWTEST_F(HgmIdleDetectorTest, GetTouchUpExpectedFPS001, Function | SmallTest | Level1)
175 {
176     std::unique_ptr<HgmIdleDetector> idleDetector = std::make_unique<HgmIdleDetector>();
177 
178     PART("CaseDescription") {
179         STEP("1. get an idleDetector") {
180             STEP_ASSERT_NE(idleDetector, nullptr);
181         }
182         STEP("2. get surface up expect fps") {
183             idleDetector->SetAppSupportedState(true);
184             idleDetector->ClearAppBufferList();
185             int32_t ret = idleDetector->GetTouchUpExpectedFPS();
186             STEP_ASSERT_EQ(ret, fps120HZ);
187 
188             idleDetector->SetAceAnimatorIdleState(false);
189             ret = idleDetector->GetTouchUpExpectedFPS();
190             STEP_ASSERT_EQ(ret, fps120HZ);
191 
192             idleDetector->appBufferList_.push_back(std::make_pair(aceAnimator, fps90HZ));
193             ret = idleDetector->GetTouchUpExpectedFPS();
194             STEP_ASSERT_EQ(ret, fps90HZ);
195 
196             idleDetector->frameTimeMap_[bufferName] = currTime;
197             ret = idleDetector->GetTouchUpExpectedFPS();
198             STEP_ASSERT_EQ(ret, fps120HZ);
199 
200             idleDetector->frameTimeMap_[flutterBuffer] = currTime;
201             idleDetector->appBufferList_.push_back(std::make_pair(flutterBuffer, fps90HZ));
202             ret = idleDetector->GetTouchUpExpectedFPS();
203             STEP_ASSERT_EQ(ret, fps120HZ);
204 
205             idleDetector->frameTimeMap_[rosenWeb] = currTime;
206             ret = idleDetector->GetTouchUpExpectedFPS();
207             STEP_ASSERT_EQ(ret, fps120HZ);
208         }
209     }
210 }
211 
212 /**
213  * @tc.name: GetTouchUpExpectFPS002
214  * @tc.desc: Verify the result of GetTouchUpExpectFPS002 function
215  * @tc.type: FUNC
216  * @tc.require: IAFG2V
217  */
218 HWTEST_F(HgmIdleDetectorTest, GetTouchUpExpectFPS002, Function | SmallTest | Level1)
219 {
220     std::unique_ptr<HgmIdleDetector> idleDetector = std::make_unique<HgmIdleDetector>();
221 
222     PART("CaseDescription") {
223         STEP("1. get an idleDetector") {
224             STEP_ASSERT_NE(idleDetector, nullptr);
225         }
226         STEP("2. get surface up expect fps") {
227             idleDetector->SetAppSupportedState(true);
228 
229             idleDetector->frameTimeMap_[rosenWeb] = currTime;
230             idleDetector->frameTimeMap_[flutterBuffer] = currTime;
231             idleDetector->frameTimeMap_[bufferName] = currTime;
232 
233             idleDetector->ClearAppBufferList();
234             idleDetector->appBufferList_.push_back(std::make_pair(bufferName, fps120HZ));
235             idleDetector->appBufferList_.push_back(std::make_pair(rosenWeb, fps90HZ));
236             idleDetector->appBufferList_.push_back(std::make_pair(flutterBuffer, fps90HZ));
237             idleDetector->appBufferList_.push_back(std::make_pair(aceAnimator, fps60HZ));
238             int32_t ret = idleDetector->GetTouchUpExpectedFPS();
239             STEP_ASSERT_EQ(ret, fps120HZ);
240 
241             idleDetector->ClearAppBufferList();
242             idleDetector->SetAceAnimatorIdleState(true);
243             idleDetector->appBufferList_.push_back(std::make_pair(aceAnimator, fps120HZ));
244             idleDetector->appBufferList_.push_back(std::make_pair(rosenWeb, fps90HZ));
245             idleDetector->appBufferList_.push_back(std::make_pair(flutterBuffer, fps90HZ));
246             idleDetector->appBufferList_.push_back(std::make_pair(bufferName, fps60HZ));
247             ret = idleDetector->GetTouchUpExpectedFPS();
248             STEP_ASSERT_EQ(ret, fps90HZ);
249 
250             idleDetector->ClearAppBufferList();
251             idleDetector->SetAceAnimatorIdleState(false);
252             idleDetector->appBufferList_.push_back(std::make_pair(aceAnimator, fps120HZ));
253             idleDetector->appBufferList_.push_back(std::make_pair(rosenWeb, fps90HZ));
254             idleDetector->appBufferList_.push_back(std::make_pair(flutterBuffer, fps90HZ));
255             idleDetector->appBufferList_.push_back(std::make_pair(bufferName, fps60HZ));
256             ret = idleDetector->GetTouchUpExpectedFPS();
257             STEP_ASSERT_EQ(ret, fps120HZ);
258         }
259     }
260 }
261 
262 /**
263  * @tc.name: UpdateAndGetAceAnimatorExpectedFrameRate001
264  * @tc.desc: Verify the result of UpdateAndGetAceAnimatorExpectedFrameRate001 function
265  * @tc.type: FUNC
266  * @tc.require: IAW09K
267  */
268 HWTEST_F(HgmIdleDetectorTest, UpdateAndGetAceAnimatorExpectedFrameRate001, Function | SmallTest | Level1)
269 {
270     std::unique_ptr<HgmIdleDetector> idleDetector = std::make_unique<HgmIdleDetector>();
271 
272     PART("CaseDescription") {
273         STEP("1. get an idleDetector") {
274             STEP_ASSERT_NE(idleDetector, nullptr);
275         }
276         STEP("2. update ace animator expected frame rate") {
277             // verify out of range conditions
278             idleDetector->UpdateAceAnimatorExpectedFrameRate(-2);
279         }
280         STEP("3. get ace animator expected frame rate") {
281             int32_t ret = idleDetector->GetAceAnimatorExpectedFrameRate();
282             STEP_ASSERT_EQ(ret, HgmIdleDetector::ANIMATOR_NOT_RUNNING);
283         }
284     }
285 }
286 
287 /**
288  * @tc.name: UpdateAndGetAceAnimatorExpectedFrameRate002
289  * @tc.desc: Verify the result of UpdateAndGetAceAnimatorExpectedFrameRate002 function
290  * @tc.type: FUNC
291  * @tc.require: IAW09K
292  */
293 HWTEST_F(HgmIdleDetectorTest, UpdateAndGetAceAnimatorExpectedFrameRate002, Function | SmallTest | Level1)
294 {
295     std::unique_ptr<HgmIdleDetector> idleDetector = std::make_unique<HgmIdleDetector>();
296 
297     PART("CaseDescription") {
298         STEP("1. get an idleDetector") {
299             STEP_ASSERT_NE(idleDetector, nullptr);
300         }
301         STEP("2. update ace animator expected frame rate") {
302             idleDetector->UpdateAceAnimatorExpectedFrameRate(60);
303         }
304         STEP("3. get ace animator expected frame rate") {
305             int32_t ret = idleDetector->GetAceAnimatorExpectedFrameRate();
306             STEP_ASSERT_EQ(ret, 60);
307         }
308     }
309 }
310 
311 /**
312  * @tc.name: ResetAceAnimatorExpectedFrameRate
313  * @tc.desc: Verify the result of ResetAceAnimatorExpectedFrameRate function
314  * @tc.type: FUNC
315  * @tc.require: IAW09K
316  */
317 HWTEST_F(HgmIdleDetectorTest, ResetAceAnimatorExpectedFrameRate, Function | SmallTest | Level1)
318 {
319     std::unique_ptr<HgmIdleDetector> idleDetector = std::make_unique<HgmIdleDetector>();
320 
321     PART("CaseDescription") {
322         STEP("1. get an idleDetector") {
323             STEP_ASSERT_NE(idleDetector, nullptr);
324         }
325         STEP("2. update ace animator expected frame rate") {
326             idleDetector->UpdateAceAnimatorExpectedFrameRate(120);
327         }
328         STEP("3. reset ace animator expected frame rate") {
329             idleDetector->ResetAceAnimatorExpectedFrameRate();
330         }
331         STEP("4. get ace animator expected frame rate") {
332             int32_t ret = idleDetector->GetAceAnimatorExpectedFrameRate();
333             STEP_ASSERT_EQ(ret, HgmIdleDetector::ANIMATOR_NOT_RUNNING);
334         }
335     }
336 }
337 } // namespace Rosen
338 } // namespace OHOS