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 "content/browser/battery_status/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 blink::WebBatteryStatus default_status
;
17 blink::WebBatteryStatus status
= ComputeWebBatteryStatus(dictionary
);
19 EXPECT_EQ(default_status
.charging
, status
.charging
);
20 EXPECT_EQ(default_status
.chargingTime
, status
.chargingTime
);
21 EXPECT_EQ(default_status
.dischargingTime
, status
.dischargingTime
);
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 blink::WebBatteryStatus status
= ComputeWebBatteryStatus(dictionary
);
33 EXPECT_TRUE(status
.charging
);
34 EXPECT_EQ(std::numeric_limits
<double>::infinity(), status
.chargingTime
);
35 EXPECT_EQ(std::numeric_limits
<double>::infinity(), status
.dischargingTime
);
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 blink::WebBatteryStatus status
= ComputeWebBatteryStatus(dictionary
);
47 EXPECT_TRUE(status
.charging
);
48 EXPECT_EQ(100, status
.chargingTime
);
49 EXPECT_EQ(std::numeric_limits
<double>::infinity(), status
.dischargingTime
);
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 blink::WebBatteryStatus status
= ComputeWebBatteryStatus(dictionary
);
62 EXPECT_TRUE(status
.charging
);
63 EXPECT_EQ(0, status
.chargingTime
);
64 EXPECT_EQ(std::numeric_limits
<double>::infinity(), status
.dischargingTime
);
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 blink::WebBatteryStatus status
= ComputeWebBatteryStatus(dictionary
);
77 EXPECT_FALSE(status
.charging
);
78 EXPECT_EQ(std::numeric_limits
<double>::infinity(), status
.chargingTime
);
79 EXPECT_EQ(200, status
.dischargingTime
);
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 blink::WebBatteryStatus status
= ComputeWebBatteryStatus(dictionary
);
92 EXPECT_FALSE(status
.charging
);
93 EXPECT_EQ(std::numeric_limits
<double>::infinity(), status
.chargingTime
);
94 EXPECT_EQ(std::numeric_limits
<double>::infinity(), status
.dischargingTime
);
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 blink::WebBatteryStatus status
= ComputeWebBatteryStatus(dictionary
);
107 EXPECT_TRUE(status
.charging
);
108 EXPECT_EQ(std::numeric_limits
<double>::infinity(), status
.chargingTime
);
109 EXPECT_EQ(std::numeric_limits
<double>::infinity(), status
.dischargingTime
);
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 blink::WebBatteryStatus status
= ComputeWebBatteryStatus(dictionary
);
122 EXPECT_FALSE(status
.charging
);
123 EXPECT_EQ(std::numeric_limits
<double>::infinity(), status
.chargingTime
);
124 EXPECT_EQ(std::numeric_limits
<double>::infinity(), status
.dischargingTime
);
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 blink::WebBatteryStatus status
= ComputeWebBatteryStatus(dictionary
);
135 EXPECT_FALSE(status
.charging
);
136 EXPECT_EQ(std::numeric_limits
<double>::infinity(), status
.chargingTime
);
137 EXPECT_EQ(std::numeric_limits
<double>::infinity(), status
.dischargingTime
);
138 EXPECT_EQ(0.15, status
.level
);
143 } // namespace content