• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2023 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 "stats_display_test.h"
17 
18 #include <display_power_info.h>
19 #include <hisysevent.h>
20 
21 #include "stats_log.h"
22 #include "stats_hisysevent.h"
23 #include "battery_stats_client.h"
24 #include "power_mgr_client.h"
25 
26 using namespace testing::ext;
27 using namespace OHOS::HiviewDFX;
28 using namespace OHOS::PowerMgr;
29 using namespace std;
30 
31 
SetLastBrightness(int32_t lastBrightness)32 static void SetLastBrightness(int32_t lastBrightness)
33 {
34     int32_t stateOn = static_cast<int32_t>(OHOS::DisplayPowerMgr::DisplayState::DISPLAY_ON);
35     int32_t stateOff = static_cast<int32_t>(OHOS::DisplayPowerMgr::DisplayState::DISPLAY_OFF);
36 
37     GTEST_LOG_(INFO) << __func__ << ": Set last screen brightness value = " << lastBrightness;
38     HiSysEventWrite(HiSysEvent::Domain::DISPLAY, StatsHiSysEvent::SCREEN_STATE,
39         HiSysEvent::EventType::STATISTIC, "STATE", stateOn);
40     HiSysEventWrite(HiSysEvent::Domain::DISPLAY, StatsHiSysEvent::BRIGHTNESS_NIT,
41         HiSysEvent::EventType::STATISTIC, "BRIGHTNESS", lastBrightness);
42     usleep(StatsTest::POWER_CONSUMPTION_DURATION_US);
43     HiSysEventWrite(HiSysEvent::Domain::DISPLAY, StatsHiSysEvent::SCREEN_STATE,
44         HiSysEvent::EventType::STATISTIC, "STATE", stateOff);
45 
46     auto& statsClient = BatteryStatsClient::GetInstance();
47     GTEST_LOG_(INFO) << __func__ << ": Battery stats client reset";
48     statsClient.Reset();
49 }
50 
SetUpTestCase()51 void StatsDisplayTest::SetUpTestCase()
52 {
53     ParserAveragePowerFile();
54     system("hidumper -s 3302 -a -u");
55     auto& pms = PowerMgrClient::GetInstance();
56     pms.SuspendDevice();
57 }
58 
TearDownTestCase()59 void StatsDisplayTest::TearDownTestCase()
60 {
61     system("hidumper -s 3302 -a -r");
62 }
63 
SetUp()64 void StatsDisplayTest::SetUp()
65 {
66     auto& statsClient = BatteryStatsClient::GetInstance();
67     statsClient.SetOnBattery(true);
68 }
69 
TearDown()70 void StatsDisplayTest::TearDown()
71 {
72     auto& statsClient = BatteryStatsClient::GetInstance();
73     statsClient.SetOnBattery(false);
74 }
75 
76 namespace {
77 /**
78  * @tc.name: StatsDisplayTest_001
79  * @tc.desc: test Reset function(Screen)
80  * @tc.type: FUNC
81  */
82 HWTEST_F (StatsDisplayTest, StatsDisplayTest_001, TestSize.Level0)
83 {
84     auto& statsClient = BatteryStatsClient::GetInstance();
85     statsClient.Reset();
86 
87     int32_t stateOn = static_cast<int32_t>(OHOS::DisplayPowerMgr::DisplayState::DISPLAY_ON);
88     int32_t stateOff = static_cast<int32_t>(OHOS::DisplayPowerMgr::DisplayState::DISPLAY_OFF);
89     int32_t brightness = 100;
90 
91     HiSysEventWrite(HiSysEvent::Domain::DISPLAY, StatsHiSysEvent::SCREEN_STATE,
92         HiSysEvent::EventType::STATISTIC, "STATE", stateOn);
93     HiSysEventWrite(HiSysEvent::Domain::DISPLAY, StatsHiSysEvent::BRIGHTNESS_NIT,
94         HiSysEvent::EventType::STATISTIC, "BRIGHTNESS", brightness);
95     usleep(POWER_CONSUMPTION_DURATION_US);
96     HiSysEventWrite(HiSysEvent::Domain::DISPLAY, StatsHiSysEvent::SCREEN_STATE,
97         HiSysEvent::EventType::STATISTIC, "STATE", stateOff);
98 
99     double powerMahBefore = statsClient.GetPartStatsMah(BatteryStatsInfo::CONSUMPTION_TYPE_SCREEN);
100     statsClient.Reset();
101     double powerMahAfter = statsClient.GetPartStatsMah(BatteryStatsInfo::CONSUMPTION_TYPE_SCREEN);
102     GTEST_LOG_(INFO) << __func__ << ": before consumption = " << powerMahBefore << " mAh";
103     GTEST_LOG_(INFO) << __func__ << ": after consumption = " << powerMahAfter << " mAh";
104     EXPECT_TRUE(powerMahBefore > StatsUtils::DEFAULT_VALUE && powerMahAfter == StatsUtils::DEFAULT_VALUE);
105 }
106 
107 /**
108  * @tc.name: StatsDisplayTest_002
109  * @tc.desc: test SetOnBattery function(Screen)
110  * @tc.type: FUNC
111  */
112 HWTEST_F (StatsDisplayTest, StatsDisplayTest_002, TestSize.Level0)
113 {
114     auto& statsClient = BatteryStatsClient::GetInstance();
115     statsClient.Reset();
116     statsClient.SetOnBattery(false);
117 
118     int32_t stateOn = static_cast<int32_t>(OHOS::DisplayPowerMgr::DisplayState::DISPLAY_ON);
119     int32_t stateOff = static_cast<int32_t>(OHOS::DisplayPowerMgr::DisplayState::DISPLAY_OFF);
120     int32_t brightness = 120;
121 
122     HiSysEventWrite(HiSysEvent::Domain::DISPLAY, StatsHiSysEvent::SCREEN_STATE,
123         HiSysEvent::EventType::STATISTIC, "STATE", stateOn);
124     HiSysEventWrite(HiSysEvent::Domain::DISPLAY, StatsHiSysEvent::BRIGHTNESS_NIT,
125         HiSysEvent::EventType::STATISTIC, "BRIGHTNESS", brightness);
126     usleep(POWER_CONSUMPTION_DURATION_US);
127     HiSysEventWrite(HiSysEvent::Domain::DISPLAY, StatsHiSysEvent::SCREEN_STATE,
128         HiSysEvent::EventType::STATISTIC, "STATE", stateOff);
129 
130     double expectedPower = StatsUtils::DEFAULT_VALUE;
131     double actualPower = statsClient.GetPartStatsMah(BatteryStatsInfo::CONSUMPTION_TYPE_SCREEN);
132     GTEST_LOG_(INFO) << __func__ << ": expected consumption = " << expectedPower << " mAh";
133     GTEST_LOG_(INFO) << __func__ << ": actual consumption = " << actualPower << " mAh";
134     EXPECT_EQ(expectedPower, actualPower);
135     statsClient.SetOnBattery(true);
136 }
137 
138 /**
139  * @tc.name: StatsDisplayTest_003
140  * @tc.desc: test GetPartStatsMah function(Screen)
141  * @tc.type: FUNC
142  */
143 HWTEST_F (StatsDisplayTest, StatsDisplayTest_003, TestSize.Level0)
144 {
145     auto& statsClient = BatteryStatsClient::GetInstance();
146     statsClient.Reset();
147 
148     int32_t stateOn = static_cast<int32_t>(OHOS::DisplayPowerMgr::DisplayState::DISPLAY_ON);
149     int32_t stateOff = static_cast<int32_t>(OHOS::DisplayPowerMgr::DisplayState::DISPLAY_OFF);
150     int32_t brightness = 100;
151     double screenOnAverage = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_SCREEN_ON);
152     double screenBrightnessAverage = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_SCREEN_BRIGHTNESS);
153 
154     HiSysEventWrite(HiSysEvent::Domain::DISPLAY, StatsHiSysEvent::SCREEN_STATE,
155         HiSysEvent::EventType::STATISTIC, "STATE", stateOn);
156     HiSysEventWrite(HiSysEvent::Domain::DISPLAY, StatsHiSysEvent::BRIGHTNESS_NIT,
157         HiSysEvent::EventType::STATISTIC, "BRIGHTNESS", brightness);
158     usleep(POWER_CONSUMPTION_DURATION_US);
159     HiSysEventWrite(HiSysEvent::Domain::DISPLAY, StatsHiSysEvent::SCREEN_STATE,
160         HiSysEvent::EventType::STATISTIC, "STATE", stateOff);
161 
162     double average = screenBrightnessAverage * brightness + screenOnAverage;
163 
164     double expectedPower = average * POWER_CONSUMPTION_DURATION_US / US_PER_HOUR;
165     double actualPower = statsClient.GetPartStatsMah(BatteryStatsInfo::CONSUMPTION_TYPE_SCREEN);
166     double devPrecent = abs(expectedPower - actualPower) / expectedPower;
167     GTEST_LOG_(INFO) << __func__ << ": expected consumption = " << expectedPower << " mAh";
168     GTEST_LOG_(INFO) << __func__ << ": actual consumption = " << actualPower << " mAh";
169     EXPECT_LE(devPrecent, DEVIATION_PERCENT_THRESHOLD);
170 }
171 
172 /**
173  * @tc.name: StatsDisplayTest_004
174  * @tc.desc: test GetPartStatsPercent function(Screen)
175  * @tc.type: FUNC
176  */
177 HWTEST_F (StatsDisplayTest, StatsDisplayTest_004, TestSize.Level0)
178 {
179     auto& statsClient = BatteryStatsClient::GetInstance();
180     statsClient.Reset();
181 
182     int32_t stateOn = static_cast<int32_t>(OHOS::DisplayPowerMgr::DisplayState::DISPLAY_ON);
183     int32_t stateOff = static_cast<int32_t>(OHOS::DisplayPowerMgr::DisplayState::DISPLAY_OFF);
184     int32_t brightness = 100;
185     double fullPercent = 1;
186     double zeroPercent = 0;
187 
188     HiSysEventWrite(HiSysEvent::Domain::DISPLAY, StatsHiSysEvent::SCREEN_STATE,
189         HiSysEvent::EventType::STATISTIC, "STATE", stateOn);
190     HiSysEventWrite(HiSysEvent::Domain::DISPLAY, StatsHiSysEvent::BRIGHTNESS_NIT,
191         HiSysEvent::EventType::STATISTIC, "BRIGHTNESS", brightness);
192     usleep(POWER_CONSUMPTION_DURATION_US);
193     HiSysEventWrite(HiSysEvent::Domain::DISPLAY, StatsHiSysEvent::SCREEN_STATE,
194         HiSysEvent::EventType::STATISTIC, "STATE", stateOff);
195 
196     double actualPercent = statsClient.GetPartStatsPercent(BatteryStatsInfo::CONSUMPTION_TYPE_SCREEN);
197     GTEST_LOG_(INFO) << __func__ << ": actual percent = " << actualPercent;
198     EXPECT_TRUE(actualPercent >= zeroPercent && actualPercent <= fullPercent);
199 }
200 
201 /**
202  * @tc.name: StatsDisplayTest_005
203  * @tc.desc: test GetBatteryStats function(Screen)
204  * @tc.type: FUNC
205  */
206 HWTEST_F (StatsDisplayTest, StatsDisplayTest_005, TestSize.Level0)
207 {
208     auto& statsClient = BatteryStatsClient::GetInstance();
209     statsClient.Reset();
210 
211     int32_t stateOn = static_cast<int32_t>(OHOS::DisplayPowerMgr::DisplayState::DISPLAY_ON);
212     int32_t stateOff = static_cast<int32_t>(OHOS::DisplayPowerMgr::DisplayState::DISPLAY_OFF);
213     int32_t brightness = 100;
214     double screenOnAverage = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_SCREEN_ON);
215     double screenBrightnessAverage = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_SCREEN_BRIGHTNESS);
216 
217     HiSysEventWrite(HiSysEvent::Domain::DISPLAY, StatsHiSysEvent::SCREEN_STATE,
218         HiSysEvent::EventType::STATISTIC, "STATE", stateOn);
219     HiSysEventWrite(HiSysEvent::Domain::DISPLAY, StatsHiSysEvent::BRIGHTNESS_NIT,
220         HiSysEvent::EventType::STATISTIC, "BRIGHTNESS", brightness);
221     usleep(POWER_CONSUMPTION_DURATION_US);
222     HiSysEventWrite(HiSysEvent::Domain::DISPLAY, StatsHiSysEvent::SCREEN_STATE,
223         HiSysEvent::EventType::STATISTIC, "STATE", stateOff);
224 
225     double average = screenBrightnessAverage * brightness + screenOnAverage;
226 
227     double expectedPower = average * POWER_CONSUMPTION_DURATION_US / US_PER_HOUR;
228     double actualPower = StatsUtils::DEFAULT_VALUE;
229     auto list = statsClient.GetBatteryStats();
230     for (auto it : list) {
231         if ((*it).GetConsumptionType() == BatteryStatsInfo::CONSUMPTION_TYPE_SCREEN) {
232             actualPower = (*it).GetPower();
233         }
234     }
235     double devPrecent = abs(expectedPower - actualPower) / expectedPower;
236     GTEST_LOG_(INFO) << __func__ << ": expected consumption = " << expectedPower << " mAh";
237     GTEST_LOG_(INFO) << __func__ << ": actual consumption = " << actualPower << " mAh";
238     EXPECT_LE(devPrecent, DEVIATION_PERCENT_THRESHOLD);
239 }
240 
241 /**
242  * @tc.name: StatsDisplayTest_006
243  * @tc.desc: test Last brightness value exists, but no BRIGHTNESS_NIT event notification, screen power consumption
244  * @tc.type: FUNC
245  */
246 HWTEST_F (StatsDisplayTest, StatsDisplayTest_006, TestSize.Level0)
247 {
248     auto& statsClient = BatteryStatsClient::GetInstance();
249     statsClient.Reset();
250 
251     int32_t stateOn = static_cast<int32_t>(OHOS::DisplayPowerMgr::DisplayState::DISPLAY_ON);
252     int32_t stateOff = static_cast<int32_t>(OHOS::DisplayPowerMgr::DisplayState::DISPLAY_OFF);
253     int32_t lastBrightness = 100;
254     double screenOnAverage = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_SCREEN_ON);
255     double screenBrightnessAverage = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_SCREEN_BRIGHTNESS);
256 
257     SetLastBrightness(lastBrightness);
258 
259     HiSysEventWrite(HiSysEvent::Domain::DISPLAY, StatsHiSysEvent::SCREEN_STATE,
260         HiSysEvent::EventType::STATISTIC, "STATE", stateOn);
261     usleep(POWER_CONSUMPTION_DURATION_US);
262     HiSysEventWrite(HiSysEvent::Domain::DISPLAY, StatsHiSysEvent::SCREEN_STATE,
263         HiSysEvent::EventType::STATISTIC, "STATE", stateOff);
264 
265     double average = screenBrightnessAverage * lastBrightness + screenOnAverage;
266 
267     double expectedPower = average * POWER_CONSUMPTION_DURATION_US / US_PER_HOUR;
268     double actualPower = statsClient.GetPartStatsMah(BatteryStatsInfo::CONSUMPTION_TYPE_SCREEN);
269     double devPrecent = abs(expectedPower - actualPower) / expectedPower;
270     GTEST_LOG_(INFO) << __func__ << ": expected consumption = " << expectedPower << " mAh";
271     GTEST_LOG_(INFO) << __func__ << ": actual consumption = " << actualPower << " mAh";
272     EXPECT_LE(devPrecent, DEVIATION_PERCENT_THRESHOLD);
273 }
274 
275 /**
276  * @tc.name: StatsDisplayTest_007
277  * @tc.desc: test SCREEN_STATE event are sent repeatedly, screen power consumption
278  * @tc.type: FUNC
279  */
280 HWTEST_F (StatsDisplayTest, StatsDisplayTest_007, TestSize.Level0)
281 {
282     auto& statsClient = BatteryStatsClient::GetInstance();
283     statsClient.Reset();
284 
285     int32_t stateOn = static_cast<int32_t>(OHOS::DisplayPowerMgr::DisplayState::DISPLAY_ON);
286     int32_t stateOff = static_cast<int32_t>(OHOS::DisplayPowerMgr::DisplayState::DISPLAY_OFF);
287     int32_t lastBrightness = 100;
288     double screenOnAverage = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_SCREEN_ON);
289     double screenBrightnessAverage = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_SCREEN_BRIGHTNESS);
290 
291     SetLastBrightness(lastBrightness);
292 
293     HiSysEventWrite(HiSysEvent::Domain::DISPLAY, StatsHiSysEvent::SCREEN_STATE,
294         HiSysEvent::EventType::STATISTIC, "STATE", stateOn);
295     usleep(POWER_CONSUMPTION_DURATION_US);
296     HiSysEventWrite(HiSysEvent::Domain::DISPLAY, StatsHiSysEvent::SCREEN_STATE,
297         HiSysEvent::EventType::STATISTIC, "STATE", stateOn);
298     usleep(POWER_CONSUMPTION_DURATION_US);
299     HiSysEventWrite(HiSysEvent::Domain::DISPLAY, StatsHiSysEvent::SCREEN_STATE,
300         HiSysEvent::EventType::STATISTIC, "STATE", stateOff);
301     usleep(POWER_CONSUMPTION_DURATION_US);
302     HiSysEventWrite(HiSysEvent::Domain::DISPLAY, StatsHiSysEvent::SCREEN_STATE,
303         HiSysEvent::EventType::STATISTIC, "STATE", stateOff);
304 
305     double average = screenBrightnessAverage * lastBrightness + screenOnAverage;
306 
307     double expectedPower = average * 2 * POWER_CONSUMPTION_DURATION_US / US_PER_HOUR;
308     double actualPower = statsClient.GetPartStatsMah(BatteryStatsInfo::CONSUMPTION_TYPE_SCREEN);
309     double devPrecent = abs(expectedPower - actualPower) / expectedPower;
310     GTEST_LOG_(INFO) << __func__ << ": expected consumption = " << expectedPower << " mAh";
311     GTEST_LOG_(INFO) << __func__ << ": actual consumption = " << actualPower << " mAh";
312     EXPECT_LE(devPrecent, DEVIATION_PERCENT_THRESHOLD);
313 }
314 
315 /**
316  * @tc.name: StatsDisplayTest_008
317  * @tc.desc: test Screen is off, BRIGHTNESS_NIT event is invalid
318  * @tc.type: FUNC
319  */
320 HWTEST_F (StatsDisplayTest, StatsDisplayTest_008, TestSize.Level0)
321 {
322     auto& statsClient = BatteryStatsClient::GetInstance();
323     statsClient.Reset();
324 
325     int32_t brightness = 100;
326 
327     HiSysEventWrite(HiSysEvent::Domain::DISPLAY, StatsHiSysEvent::BRIGHTNESS_NIT,
328         HiSysEvent::EventType::STATISTIC, "BRIGHTNESS", brightness);
329     usleep(POWER_CONSUMPTION_DURATION_US);
330 
331     double expectedPower = StatsUtils::DEFAULT_VALUE;
332     double actualPower = statsClient.GetPartStatsMah(BatteryStatsInfo::CONSUMPTION_TYPE_SCREEN);
333     GTEST_LOG_(INFO) << __func__ << ": expected consumption = " << expectedPower << " mAh";
334     GTEST_LOG_(INFO) << __func__ << ": actual consumption = " << actualPower << " mAh";
335     EXPECT_EQ(expectedPower, actualPower);
336 }
337 
338 /**
339  * @tc.name: StatsDisplayTest_009
340  * @tc.desc: test Screen is off, BRIGHTNESS_NIT event is invalid
341  * @tc.type: FUNC
342  */
343 HWTEST_F (StatsDisplayTest, StatsDisplayTest_009, TestSize.Level0)
344 {
345     auto& statsClient = BatteryStatsClient::GetInstance();
346     statsClient.Reset();
347 
348     int32_t stateOn = static_cast<int32_t>(OHOS::DisplayPowerMgr::DisplayState::DISPLAY_ON);
349     int32_t stateOff = static_cast<int32_t>(OHOS::DisplayPowerMgr::DisplayState::DISPLAY_OFF);
350     int32_t lastBrightness = 100;
351     int32_t currentBrightness = 200;
352     double screenOnAverage = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_SCREEN_ON);
353     double screenBrightnessAverage = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_SCREEN_BRIGHTNESS);
354 
355     SetLastBrightness(lastBrightness);
356 
357     HiSysEventWrite(HiSysEvent::Domain::DISPLAY, StatsHiSysEvent::BRIGHTNESS_NIT,
358         HiSysEvent::EventType::STATISTIC, "BRIGHTNESS", currentBrightness);
359     usleep(POWER_CONSUMPTION_DURATION_US);
360 
361     HiSysEventWrite(HiSysEvent::Domain::DISPLAY, StatsHiSysEvent::SCREEN_STATE,
362         HiSysEvent::EventType::STATISTIC, "STATE", stateOn);
363     usleep(POWER_CONSUMPTION_DURATION_US);
364     HiSysEventWrite(HiSysEvent::Domain::DISPLAY, StatsHiSysEvent::SCREEN_STATE,
365         HiSysEvent::EventType::STATISTIC, "STATE", stateOff);
366 
367     double average = screenBrightnessAverage * lastBrightness + screenOnAverage;
368 
369     double expectedPower = average * POWER_CONSUMPTION_DURATION_US / US_PER_HOUR;
370     double actualPower = statsClient.GetPartStatsMah(BatteryStatsInfo::CONSUMPTION_TYPE_SCREEN);
371     double devPrecent = abs(expectedPower - actualPower) / expectedPower;
372     GTEST_LOG_(INFO) << __func__ << ": expected consumption = " << expectedPower << " mAh";
373     GTEST_LOG_(INFO) << __func__ << ": actual consumption = " << actualPower << " mAh";
374     EXPECT_LE(devPrecent, DEVIATION_PERCENT_THRESHOLD);
375 }
376 
377 /**
378  * @tc.name: StatsDisplayTest_010
379  * @tc.desc: test Screen is on, BRIGHTNESS_NIT event are sent delay
380  * @tc.type: FUNC
381  */
382 HWTEST_F (StatsDisplayTest, StatsDisplayTest_010, TestSize.Level0)
383 {
384     auto& statsClient = BatteryStatsClient::GetInstance();
385     statsClient.Reset();
386 
387     int32_t stateOn = static_cast<int32_t>(OHOS::DisplayPowerMgr::DisplayState::DISPLAY_ON);
388     int32_t stateOff = static_cast<int32_t>(OHOS::DisplayPowerMgr::DisplayState::DISPLAY_OFF);
389     int32_t lastBrightness = 100;
390     int32_t currentBrightness = 200;
391     double screenOnAverage = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_SCREEN_ON);
392     double screenBrightnessAverage = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_SCREEN_BRIGHTNESS);
393 
394     SetLastBrightness(lastBrightness);
395 
396     HiSysEventWrite(HiSysEvent::Domain::DISPLAY, StatsHiSysEvent::SCREEN_STATE,
397         HiSysEvent::EventType::STATISTIC, "STATE", stateOn);
398     usleep(POWER_CONSUMPTION_DURATION_US);
399     HiSysEventWrite(HiSysEvent::Domain::DISPLAY, StatsHiSysEvent::BRIGHTNESS_NIT,
400         HiSysEvent::EventType::STATISTIC, "BRIGHTNESS", currentBrightness);
401     usleep(POWER_CONSUMPTION_DURATION_US);
402     HiSysEventWrite(HiSysEvent::Domain::DISPLAY, StatsHiSysEvent::SCREEN_STATE,
403         HiSysEvent::EventType::STATISTIC, "STATE", stateOff);
404 
405     double screenOnPower = screenOnAverage * 2 * POWER_CONSUMPTION_DURATION_US;
406     double lastBrightnessPower = screenBrightnessAverage * lastBrightness * POWER_CONSUMPTION_DURATION_US;
407     double curBrightnessPower = screenBrightnessAverage * currentBrightness * POWER_CONSUMPTION_DURATION_US;
408 
409     double expectedPower = (screenOnPower + lastBrightnessPower + curBrightnessPower) / US_PER_HOUR;
410     double actualPower = statsClient.GetPartStatsMah(BatteryStatsInfo::CONSUMPTION_TYPE_SCREEN);
411     double devPrecent = abs(expectedPower - actualPower) / expectedPower;
412     GTEST_LOG_(INFO) << __func__ << ": expected consumption = " << expectedPower << " mAh";
413     GTEST_LOG_(INFO) << __func__ << ": actual consumption = " << actualPower << " mAh";
414     EXPECT_LE(devPrecent, DEVIATION_PERCENT_THRESHOLD);
415 }
416 
417 /**
418  * @tc.name: StatsDisplayTest_011
419  * @tc.desc: test Screen is on, BRIGHTNESS_NIT event are sent with invalid value
420  * @tc.type: FUNC
421  */
422 HWTEST_F (StatsDisplayTest, StatsDisplayTest_011, TestSize.Level0)
423 {
424     auto& statsClient = BatteryStatsClient::GetInstance();
425     statsClient.Reset();
426 
427     int32_t stateOn = static_cast<int32_t>(OHOS::DisplayPowerMgr::DisplayState::DISPLAY_ON);
428     int32_t stateOff = static_cast<int32_t>(OHOS::DisplayPowerMgr::DisplayState::DISPLAY_OFF);
429     int32_t lastBrightness = 100;
430     int32_t invalidBrightness1 = -1;
431     int32_t invalidBrightness2 = 300;
432     double screenOnAverage = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_SCREEN_ON);
433     double screenBrightnessAverage = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_SCREEN_BRIGHTNESS);
434 
435     SetLastBrightness(lastBrightness);
436 
437     HiSysEventWrite(HiSysEvent::Domain::DISPLAY, StatsHiSysEvent::SCREEN_STATE,
438         HiSysEvent::EventType::STATISTIC, "STATE", stateOn);
439     usleep(POWER_CONSUMPTION_DURATION_US);
440     HiSysEventWrite(HiSysEvent::Domain::DISPLAY, StatsHiSysEvent::BRIGHTNESS_NIT,
441         HiSysEvent::EventType::STATISTIC, "BRIGHTNESS", invalidBrightness1);
442     usleep(POWER_CONSUMPTION_DURATION_US);
443     HiSysEventWrite(HiSysEvent::Domain::DISPLAY, StatsHiSysEvent::BRIGHTNESS_NIT,
444         HiSysEvent::EventType::STATISTIC, "BRIGHTNESS", invalidBrightness2);
445     usleep(POWER_CONSUMPTION_DURATION_US);
446     HiSysEventWrite(HiSysEvent::Domain::DISPLAY, StatsHiSysEvent::SCREEN_STATE,
447         HiSysEvent::EventType::STATISTIC, "STATE", stateOff);
448 
449     double average = screenBrightnessAverage * lastBrightness + screenOnAverage;
450 
451     double expectedPower = average * 3 * POWER_CONSUMPTION_DURATION_US / US_PER_HOUR;
452     double actualPower = statsClient.GetPartStatsMah(BatteryStatsInfo::CONSUMPTION_TYPE_SCREEN);
453     double devPrecent = abs(expectedPower - actualPower) / expectedPower;
454     GTEST_LOG_(INFO) << __func__ << ": expected consumption = " << expectedPower << " mAh";
455     GTEST_LOG_(INFO) << __func__ << ": actual consumption = " << actualPower << " mAh";
456     EXPECT_LE(devPrecent, DEVIATION_PERCENT_THRESHOLD);
457 }
458 
459 /**
460  * @tc.name: StatsDisplayTest_012
461  * @tc.desc: test GetTotalTimeSecond function(CURRENT_SCREEN_ON & CURRENT_SCREEN_BRIGHTNESS)
462  * @tc.type: FUNC
463  */
464 HWTEST_F (StatsDisplayTest, StatsDisplayTest_012, TestSize.Level0)
465 {
466     STATS_HILOGD(LABEL_TEST, "StatsDisplayTest_012 is start");
467     auto& statsClient = BatteryStatsClient::GetInstance();
468     statsClient.Reset();
469 
470     int32_t stateOn = static_cast<int32_t>(OHOS::DisplayPowerMgr::DisplayState::DISPLAY_ON);
471     int32_t stateOff = static_cast<int32_t>(OHOS::DisplayPowerMgr::DisplayState::DISPLAY_OFF);
472     int32_t lastBrightness = 100;
473     int32_t brightnessBegin = 0;
474     int32_t brightness = 0;
475     int32_t count = 50;
476     int32_t step = 3;
477 
478     SetLastBrightness(lastBrightness);
479     HiSysEventWrite(HiSysEvent::Domain::DISPLAY, StatsHiSysEvent::SCREEN_STATE,
480         HiSysEvent::EventType::STATISTIC, "STATE", stateOn);
481     usleep(POWER_CONSUMPTION_DURATION_US);
482     for (int32_t i = 0; i < count; i++) {
483         brightness = brightnessBegin + step * i;
484         auto ret = HiSysEventWrite(HiSysEvent::Domain::DISPLAY, StatsHiSysEvent::BRIGHTNESS_NIT,
485             HiSysEvent::EventType::STATISTIC, "BRIGHTNESS", brightness);
486         STATS_HILOGD(LABEL_TEST, "HiSysEventWrite return: %{public}d", ret);
487         usleep(POWER_CONSUMPTION_DURATION_US);
488     }
489     HiSysEventWrite(HiSysEvent::Domain::DISPLAY, StatsHiSysEvent::SCREEN_STATE,
490         HiSysEvent::EventType::STATISTIC, "STATE", stateOff);
491 
492     long expectScreenOnTime = POWER_CONSUMPTION_DURATION_US * (count + 1) / US_PER_SECOND;
493     long screenOnTime = statsClient.GetTotalTimeSecond(StatsUtils::STATS_TYPE_SCREEN_ON);
494     GTEST_LOG_(INFO) << __func__ << ": expected screen on time = " << expectScreenOnTime << " seconds";
495     GTEST_LOG_(INFO) << __func__ << ": actual screen on time = " <<  screenOnTime << " seconds";
496     EXPECT_EQ(expectScreenOnTime, screenOnTime);
497 
498     long expectBrightnessTime = POWER_CONSUMPTION_DURATION_US * (count + 1) / US_PER_SECOND;
499     long brightnessTime = statsClient.GetTotalTimeSecond(StatsUtils::STATS_TYPE_SCREEN_BRIGHTNESS);
500     GTEST_LOG_(INFO) << __func__ << ": expected screen on time = " << expectBrightnessTime << " seconds";
501     GTEST_LOG_(INFO) << __func__ << ": actual screen on time = " <<  brightnessTime << " seconds";
502     EXPECT_EQ(expectBrightnessTime, brightnessTime);
503     STATS_HILOGD(LABEL_TEST, "StatsDisplayTest_012 is end");
504 }
505 }