1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "device/battery/battery_status_manager_linux.h"
7 #include "base/values.h"
8 #include "testing/gtest/include/gtest/gtest.h"
14 TEST(BatteryStatusManagerLinuxTest
, EmptyDictionary
) {
15 base::DictionaryValue dictionary
;
16 BatteryStatus default_status
;
17 BatteryStatus status
= ComputeWebBatteryStatus(dictionary
);
19 EXPECT_EQ(default_status
.charging
, status
.charging
);
20 EXPECT_EQ(default_status
.charging_time
, status
.charging_time
);
21 EXPECT_EQ(default_status
.discharging_time
, status
.discharging_time
);
22 EXPECT_EQ(default_status
.level
, status
.level
);
25 TEST(BatteryStatusManagerLinuxTest
, ChargingHalfFull
) {
26 base::DictionaryValue dictionary
;
27 dictionary
.SetDouble("State", UPOWER_DEVICE_STATE_CHARGING
);
28 dictionary
.SetDouble("TimeToFull", 0);
29 dictionary
.SetDouble("Percentage", 50);
31 BatteryStatus status
= ComputeWebBatteryStatus(dictionary
);
33 EXPECT_TRUE(status
.charging
);
34 EXPECT_EQ(std::numeric_limits
<double>::infinity(), status
.charging_time
);
35 EXPECT_EQ(std::numeric_limits
<double>::infinity(), status
.discharging_time
);
36 EXPECT_EQ(0.5, status
.level
);
39 TEST(BatteryStatusManagerLinuxTest
, ChargingTimeToFull
) {
40 base::DictionaryValue dictionary
;
41 dictionary
.SetDouble("State", UPOWER_DEVICE_STATE_CHARGING
);
42 dictionary
.SetDouble("TimeToFull", 100.f
);
43 dictionary
.SetDouble("Percentage", 1);
45 BatteryStatus status
= ComputeWebBatteryStatus(dictionary
);
47 EXPECT_TRUE(status
.charging
);
48 EXPECT_EQ(100, status
.charging_time
);
49 EXPECT_EQ(std::numeric_limits
<double>::infinity(), status
.discharging_time
);
50 EXPECT_EQ(.01, status
.level
);
53 TEST(BatteryStatusManagerLinuxTest
, FullyCharged
) {
54 base::DictionaryValue dictionary
;
55 dictionary
.SetDouble("State", UPOWER_DEVICE_STATE_FULL
);
56 dictionary
.SetDouble("TimeToFull", 100);
57 dictionary
.SetDouble("TimeToEmpty", 200);
58 dictionary
.SetDouble("Percentage", 100);
60 BatteryStatus status
= ComputeWebBatteryStatus(dictionary
);
62 EXPECT_TRUE(status
.charging
);
63 EXPECT_EQ(0, status
.charging_time
);
64 EXPECT_EQ(std::numeric_limits
<double>::infinity(), status
.discharging_time
);
65 EXPECT_EQ(1, status
.level
);
68 TEST(BatteryStatusManagerLinuxTest
, Discharging
) {
69 base::DictionaryValue dictionary
;
70 dictionary
.SetDouble("State", UPOWER_DEVICE_STATE_DISCHARGING
);
71 dictionary
.SetDouble("TimeToFull", 0);
72 dictionary
.SetDouble("TimeToEmpty", 200);
73 dictionary
.SetDouble("Percentage", 90);
75 BatteryStatus status
= ComputeWebBatteryStatus(dictionary
);
77 EXPECT_FALSE(status
.charging
);
78 EXPECT_EQ(std::numeric_limits
<double>::infinity(), status
.charging_time
);
79 EXPECT_EQ(200, status
.discharging_time
);
80 EXPECT_EQ(.9, status
.level
);
83 TEST(BatteryStatusManagerLinuxTest
, DischargingTimeToEmptyUnknown
) {
84 base::DictionaryValue dictionary
;
85 dictionary
.SetDouble("State", UPOWER_DEVICE_STATE_DISCHARGING
);
86 dictionary
.SetDouble("TimeToFull", 0);
87 dictionary
.SetDouble("TimeToEmpty", 0);
88 dictionary
.SetDouble("Percentage", 90);
90 BatteryStatus status
= ComputeWebBatteryStatus(dictionary
);
92 EXPECT_FALSE(status
.charging
);
93 EXPECT_EQ(std::numeric_limits
<double>::infinity(), status
.charging_time
);
94 EXPECT_EQ(std::numeric_limits
<double>::infinity(), status
.discharging_time
);
95 EXPECT_EQ(.9, status
.level
);
98 TEST(BatteryStatusManagerLinuxTest
, DeviceStateUnknown
) {
99 base::DictionaryValue dictionary
;
100 dictionary
.SetDouble("State", UPOWER_DEVICE_STATE_UNKNOWN
);
101 dictionary
.SetDouble("TimeToFull", 0);
102 dictionary
.SetDouble("TimeToEmpty", 0);
103 dictionary
.SetDouble("Percentage", 50);
105 BatteryStatus status
= ComputeWebBatteryStatus(dictionary
);
107 EXPECT_TRUE(status
.charging
);
108 EXPECT_EQ(std::numeric_limits
<double>::infinity(), status
.charging_time
);
109 EXPECT_EQ(std::numeric_limits
<double>::infinity(), status
.discharging_time
);
110 EXPECT_EQ(.5, status
.level
);
113 TEST(BatteryStatusManagerLinuxTest
, DeviceStateEmpty
) {
114 base::DictionaryValue dictionary
;
115 dictionary
.SetDouble("State", UPOWER_DEVICE_STATE_EMPTY
);
116 dictionary
.SetDouble("TimeToFull", 0);
117 dictionary
.SetDouble("TimeToEmpty", 0);
118 dictionary
.SetDouble("Percentage", 0);
120 BatteryStatus status
= ComputeWebBatteryStatus(dictionary
);
122 EXPECT_FALSE(status
.charging
);
123 EXPECT_EQ(std::numeric_limits
<double>::infinity(), status
.charging_time
);
124 EXPECT_EQ(std::numeric_limits
<double>::infinity(), status
.discharging_time
);
125 EXPECT_EQ(0, status
.level
);
128 TEST(BatteryStatusManagerLinuxTest
, LevelRoundedToThreeSignificantDigits
) {
129 base::DictionaryValue dictionary
;
130 dictionary
.SetDouble("State", UPOWER_DEVICE_STATE_DISCHARGING
);
131 dictionary
.SetDouble("Percentage", 14.56);
133 BatteryStatus status
= ComputeWebBatteryStatus(dictionary
);
135 EXPECT_FALSE(status
.charging
);
136 EXPECT_EQ(std::numeric_limits
<double>::infinity(), status
.charging_time
);
137 EXPECT_EQ(std::numeric_limits
<double>::infinity(), status
.discharging_time
);
138 EXPECT_EQ(0.15, status
.level
);
143 } // namespace device