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 
18 #include "hgm_energy_consumption_policy.h"
19 
20 #include "common/rs_common_hook.h"
21 
22 using namespace testing;
23 using namespace testing::ext;
24 
25 namespace OHOS {
26 namespace Rosen {
27 constexpr int DEFAULT_MAX_FPS = 120;
28 constexpr int IDLE_FPS = 60;
29 
30 class HgmEnergyConsumptionPolicyTest : public testing::Test {
31 public:
SetUpTestCase()32     static void SetUpTestCase() {}
TearDownTestCase()33     static void TearDownTestCase() {}
SetUp()34     void SetUp() {}
TearDown()35     void TearDown() {}
36     void SetConfigEnable(std::string isEnable);
37     void SetIdleStateEnable(bool isIdle);
38 };
39 
SetConfigEnable(std::string isEnable)40 void HgmEnergyConsumptionPolicyTest::SetConfigEnable(std::string isEnable)
41 {
42     std::unordered_map<std::string, std::string> animationPowerConfig = {
43         { "animation_energy_assurance_enable", isEnable },
44         { "animation_idle_fps", "60" },
45         { "animation_idle_duration", "2000" } };
46     std::unordered_map<std::string, std::string> uiPowerConfig = { { "ui_animation", "30" }, { "display_sync", "31" },
47     { "ace_component", "32" }, { "display_soloist", "33" } };
48 
49     HgmEnergyConsumptionPolicy::Instance().SetEnergyConsumptionConfig(animationPowerConfig);
50     if (isEnable != "true") {
51         uiPowerConfig.clear();
52     }
53     HgmEnergyConsumptionPolicy::Instance().SetUiEnergyConsumptionConfig(uiPowerConfig);
54 }
55 
SetIdleStateEnable(bool isIdle)56 void HgmEnergyConsumptionPolicyTest::SetIdleStateEnable(bool isIdle)
57 {
58     HgmEnergyConsumptionPolicy::Instance().SetAnimationEnergyConsumptionAssuranceMode(isIdle);
59     HgmEnergyConsumptionPolicy::Instance().SetUiEnergyConsumptionAssuranceMode(isIdle);
60 }
61 
62 /**
63  * @tc.name: SetEnergyConsumptionConfigTest1
64  * @tc.desc: test results of SetEnergyConsumptionConfigTest1
65  * @tc.type: FUNC
66  * @tc.require: issuesIA96Q3
67  */
68 HWTEST_F(HgmEnergyConsumptionPolicyTest, SetEnergyConsumptionConfigTest1, TestSize.Level1)
69 {
70     SetConfigEnable("true");
71 }
72 
73 /**
74  * @tc.name: SetEnergyConsumptionConfigTest2
75  * @tc.desc: test results of SetEnergyConsumptionConfigTest2
76  * @tc.type: FUNC
77  * @tc.require: issuesIA96Q3
78  */
79 HWTEST_F(HgmEnergyConsumptionPolicyTest, SetEnergyConsumptionConfigTest2, TestSize.Level1)
80 {
81     SetConfigEnable("false");
82 }
83 
84 /**
85  * @tc.name: SetEnergyConsumptionAssuranceModeTest1
86  * @tc.desc: test results of SetEnergyConsumptionAssuranceModeTest1
87  * @tc.type: FUNC
88  * @tc.require: issuesIA96Q3
89  */
90 HWTEST_F(HgmEnergyConsumptionPolicyTest, SetEnergyConsumptionAssuranceModeTest1, TestSize.Level1)
91 {
92     SetConfigEnable("true");
93     SetIdleStateEnable(true);
94     SetIdleStateEnable(false);
95 }
96 
97 /**
98  * @tc.name: SetEnergyConsumptionAssuranceModeTest2
99  * @tc.desc: test results of SetEnergyConsumptionAssuranceModeTest2
100  * @tc.type: FUNC
101  * @tc.require: issuesIA96Q3
102  */
103 HWTEST_F(HgmEnergyConsumptionPolicyTest, SetEnergyConsumptionAssuranceModeTest2, TestSize.Level1)
104 {
105     SetConfigEnable("true");
106     SetIdleStateEnable(true);
107     SetIdleStateEnable(true);
108 }
109 
110 /**
111  * @tc.name: SetEnergyConsumptionAssuranceModeTest3
112  * @tc.desc: test results of SetEnergyConsumptionAssuranceModeTest3
113  * @tc.type: FUNC
114  * @tc.require: issuesIA96Q3
115  */
116 HWTEST_F(HgmEnergyConsumptionPolicyTest, SetEnergyConsumptionAssuranceModeTest3, TestSize.Level1)
117 {
118     SetConfigEnable("false");
119     SetIdleStateEnable(false);
120     SetIdleStateEnable(true);
121 }
122 
123 /**
124  * @tc.name: StatisticAnimationTimeTest1
125  * @tc.desc: test results of StatisticAnimationTimeTest1
126  * @tc.type: FUNC
127  * @tc.require: issuesIA96Q3
128  */
129 HWTEST_F(HgmEnergyConsumptionPolicyTest, StatisticAnimationTimeTest1, TestSize.Level1)
130 {
131     SetConfigEnable("false");
132     SetIdleStateEnable(false);
133     uint64_t currentTime = 1719544264071;
134     HgmEnergyConsumptionPolicy::Instance().StatisticAnimationTime(currentTime);
135 }
136 
137 /**
138  * @tc.name: StatisticAnimationTimeTest2
139  * @tc.desc: test results of StatisticAnimationTimeTest2
140  * @tc.type: FUNC
141  * @tc.require: issuesIA96Q3
142  */
143 HWTEST_F(HgmEnergyConsumptionPolicyTest, StatisticAnimationTimeTest2, TestSize.Level1)
144 {
145     SetConfigEnable("true");
146     SetIdleStateEnable(false);
147     uint64_t currentTime = 1719544264071;
148     HgmEnergyConsumptionPolicy::Instance().StatisticAnimationTime(currentTime);
149 }
150 
151 /**
152  * @tc.name: StatisticAnimationTimeTest3
153  * @tc.desc: test results of StatisticAnimationTimeTest3
154  * @tc.type: FUNC
155  * @tc.require: issuesIA96Q3
156  */
157 HWTEST_F(HgmEnergyConsumptionPolicyTest, StatisticAnimationTimeTest3, TestSize.Level1)
158 {
159     SetConfigEnable("true");
160     SetIdleStateEnable(true);
161     uint64_t currentTime = 1719544264071;
162     HgmEnergyConsumptionPolicy::Instance().StatisticAnimationTime(currentTime);
163 }
164 
165 /**
166  * @tc.name: StartNewAnimationTest1
167  * @tc.desc: test results of StartNewAnimationTest1
168  * @tc.type: FUNC
169  * @tc.require: issuesIA96Q3
170  */
171 HWTEST_F(HgmEnergyConsumptionPolicyTest, StartNewAnimationTest1, TestSize.Level1)
172 {
173     SetConfigEnable("true");
174     HgmEnergyConsumptionPolicy::Instance().StartNewAnimation();
175 }
176 
177 /**
178  * @tc.name: StartNewAnimationTest2
179  * @tc.desc: test results of StartNewAnimationTest2
180  * @tc.type: FUNC
181  * @tc.require: issuesIA96Q3
182  */
183 HWTEST_F(HgmEnergyConsumptionPolicyTest, StartNewAnimationTest2, TestSize.Level1)
184 {
185     SetConfigEnable("true");
186     RsCommonHook::Instance().OnStartNewAnimation();
187 }
188 
189 /**
190  * @tc.name: GetAnimationIdleFpsTest1
191  * @tc.desc: test results of GetAnimationIdleFpsTest1
192  * @tc.type: FUNC
193  * @tc.require: issuesIA96Q3
194  */
195 HWTEST_F(HgmEnergyConsumptionPolicyTest, GetAnimationIdleFpsTest1, TestSize.Level1)
196 {
197     SetConfigEnable("false");
198     FrameRateRange rsRange = { DEFAULT_MAX_FPS, DEFAULT_MAX_FPS, DEFAULT_MAX_FPS };
199     HgmEnergyConsumptionPolicy::Instance().GetAnimationIdleFps(rsRange);
200     ASSERT_EQ(rsRange.max_, DEFAULT_MAX_FPS);
201     ASSERT_EQ(rsRange.min_, DEFAULT_MAX_FPS);
202     ASSERT_EQ(rsRange.preferred_, DEFAULT_MAX_FPS);
203 }
204 
205 /**
206  * @tc.name: GetAnimationIdleFpsTest2
207  * @tc.desc: test results of GetAnimationIdleFpsTest2
208  * @tc.type: FUNC
209  * @tc.require: issuesIA96Q3
210  */
211 HWTEST_F(HgmEnergyConsumptionPolicyTest, GetAnimationIdleFpsTest2, TestSize.Level1)
212 {
213     SetConfigEnable("true");
214     HgmEnergyConsumptionPolicy::Instance().lastAnimationTimestamp_ = 1719544264071;
215     HgmEnergyConsumptionPolicy::Instance().firstAnimationTimestamp_ = 1719544263071;
216     FrameRateRange rsRange = { DEFAULT_MAX_FPS, DEFAULT_MAX_FPS, DEFAULT_MAX_FPS };
217     HgmEnergyConsumptionPolicy::Instance().GetAnimationIdleFps(rsRange);
218     ASSERT_EQ(rsRange.max_, DEFAULT_MAX_FPS);
219     ASSERT_EQ(rsRange.min_, DEFAULT_MAX_FPS);
220     ASSERT_EQ(rsRange.preferred_, DEFAULT_MAX_FPS);
221 }
222 
223 /**
224  * @tc.name: GetAnimationIdleFpsTest3
225  * @tc.desc: test results of GetAnimationIdleFpsTest3
226  * @tc.type: FUNC
227  * @tc.require: issuesIA96Q3
228  */
229 HWTEST_F(HgmEnergyConsumptionPolicyTest, GetAnimationIdleFpsTest3, TestSize.Level1)
230 {
231     SetConfigEnable("true");
232     HgmEnergyConsumptionPolicy::Instance().lastAnimationTimestamp_ = 1719544264071;
233     HgmEnergyConsumptionPolicy::Instance().firstAnimationTimestamp_ = 1719544261071;
234     FrameRateRange rsRange = { DEFAULT_MAX_FPS, DEFAULT_MAX_FPS, DEFAULT_MAX_FPS };
235     HgmEnergyConsumptionPolicy::Instance().GetAnimationIdleFps(rsRange);
236     ASSERT_EQ(rsRange.max_, IDLE_FPS);
237     ASSERT_EQ(rsRange.min_, IDLE_FPS);
238     ASSERT_EQ(rsRange.preferred_, IDLE_FPS);
239 }
240 
241 /**
242  * @tc.name: GetUiAnimationIdleFpsTest1
243  * @tc.desc: test results of GetUiAnimationIdleFpsTest1
244  * @tc.type: FUNC
245  * @tc.require: issuesIA96Q3
246  */
247 HWTEST_F(HgmEnergyConsumptionPolicyTest, GetUiAnimationIdleFpsTest1, TestSize.Level1)
248 {
249     FrameRateRange rsRange = { DEFAULT_MAX_FPS, DEFAULT_MAX_FPS, DEFAULT_MAX_FPS, UI_ANIMATION_FRAME_RATE_TYPE };
250     SetConfigEnable("false");
251     HgmEnergyConsumptionPolicy::Instance().GetUiIdleFps(rsRange);
252     ASSERT_EQ(rsRange.max_, DEFAULT_MAX_FPS);
253     ASSERT_EQ(rsRange.min_, DEFAULT_MAX_FPS);
254     ASSERT_EQ(rsRange.preferred_, DEFAULT_MAX_FPS);
255 
256     SetConfigEnable("true");
257     HgmEnergyConsumptionPolicy::Instance().GetUiIdleFps(rsRange);
258     ASSERT_EQ(rsRange.max_, 30);
259     ASSERT_EQ(rsRange.min_, 30);
260     ASSERT_EQ(rsRange.preferred_, 30);
261 }
262 
263 /**
264  * @tc.name: GetDisplaySyncIdleFpsTest1
265  * @tc.desc: test results of GetDisplaySyncIdleFpsTest1
266  * @tc.type: FUNC
267  * @tc.require: issuesIA96Q3
268  */
269 HWTEST_F(HgmEnergyConsumptionPolicyTest, GetDisplaySyncIdleFpsTest1, TestSize.Level1)
270 {
271     FrameRateRange rsRange = { DEFAULT_MAX_FPS, DEFAULT_MAX_FPS, DEFAULT_MAX_FPS, DISPLAY_SYNC_FRAME_RATE_TYPE };
272     SetConfigEnable("false");
273     HgmEnergyConsumptionPolicy::Instance().GetUiIdleFps(rsRange);
274     ASSERT_EQ(rsRange.max_, DEFAULT_MAX_FPS);
275     ASSERT_EQ(rsRange.min_, DEFAULT_MAX_FPS);
276     ASSERT_EQ(rsRange.preferred_, DEFAULT_MAX_FPS);
277 
278     SetConfigEnable("true");
279     HgmEnergyConsumptionPolicy::Instance().GetUiIdleFps(rsRange);
280     ASSERT_EQ(rsRange.max_, 31);
281     ASSERT_EQ(rsRange.min_, 31);
282     ASSERT_EQ(rsRange.preferred_, 31);
283 }
284 
285 /**
286  * @tc.name: GetAceComponentIdleFpsTest1
287  * @tc.desc: test results of GetAceComponentIdleFpsTest1
288  * @tc.type: FUNC
289  * @tc.require: issuesIA96Q3
290  */
291 HWTEST_F(HgmEnergyConsumptionPolicyTest, GetAceComponentIdleFpsTest1, TestSize.Level1)
292 {
293     FrameRateRange rsRange = { DEFAULT_MAX_FPS, DEFAULT_MAX_FPS, DEFAULT_MAX_FPS, ACE_COMPONENT_FRAME_RATE_TYPE };
294     SetConfigEnable("false");
295     HgmEnergyConsumptionPolicy::Instance().GetUiIdleFps(rsRange);
296     ASSERT_EQ(rsRange.max_, DEFAULT_MAX_FPS);
297     ASSERT_EQ(rsRange.min_, DEFAULT_MAX_FPS);
298     ASSERT_EQ(rsRange.preferred_, DEFAULT_MAX_FPS);
299 
300     SetConfigEnable("true");
301     HgmEnergyConsumptionPolicy::Instance().GetUiIdleFps(rsRange);
302     ASSERT_EQ(rsRange.max_, 32);
303     ASSERT_EQ(rsRange.min_, 32);
304     ASSERT_EQ(rsRange.preferred_, 32);
305 }
306 
307 /**
308  * @tc.name: GetDisplaySoloistIdleFpsTest1
309  * @tc.desc: test results of GetDisplaySoloistIdleFpsTest1
310  * @tc.type: FUNC
311  * @tc.require: issuesIA96Q3
312  */
313 HWTEST_F(HgmEnergyConsumptionPolicyTest, GetDisplaySoloistIdleFpsTest1, TestSize.Level1)
314 {
315     FrameRateRange rsRange = { DEFAULT_MAX_FPS, DEFAULT_MAX_FPS, DEFAULT_MAX_FPS, DISPLAY_SOLOIST_FRAME_RATE_TYPE };
316     SetConfigEnable("false");
317     HgmEnergyConsumptionPolicy::Instance().GetUiIdleFps(rsRange);
318     ASSERT_EQ(rsRange.max_, DEFAULT_MAX_FPS);
319     ASSERT_EQ(rsRange.min_, DEFAULT_MAX_FPS);
320     ASSERT_EQ(rsRange.preferred_, DEFAULT_MAX_FPS);
321 
322     SetConfigEnable("true");
323     HgmEnergyConsumptionPolicy::Instance().GetUiIdleFps(rsRange);
324     ASSERT_EQ(rsRange.max_, 33);
325     ASSERT_EQ(rsRange.min_, 33);
326     ASSERT_EQ(rsRange.preferred_, 33);
327 }
328 } // namespace Rosen
329 } // namespace OHOS