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.
4
5#include "content/browser/battery_status/battery_status_manager_linux.h"
6
7#include "base/values.h"
8#include "testing/gtest/include/gtest/gtest.h"
9
10namespace content {
11
12namespace {
13
14TEST(BatteryStatusManagerLinuxTest, EmptyDictionary) {
15  base::DictionaryValue dictionary;
16  blink::WebBatteryStatus default_status;
17  blink::WebBatteryStatus status = ComputeWebBatteryStatus(dictionary);
18
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);
23}
24
25TEST(BatteryStatusManagerLinuxTest, ChargingHalfFull) {
26  base::DictionaryValue dictionary;
27  dictionary.SetDouble("State", UPOWER_DEVICE_STATE_CHARGING);
28  dictionary.SetDouble("TimeToFull", 0);
29  dictionary.SetDouble("Percentage", 50);
30
31  blink::WebBatteryStatus status = ComputeWebBatteryStatus(dictionary);
32
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);
37}
38
39TEST(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);
44
45  blink::WebBatteryStatus status = ComputeWebBatteryStatus(dictionary);
46
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);
51}
52
53TEST(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);
59
60  blink::WebBatteryStatus status = ComputeWebBatteryStatus(dictionary);
61
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);
66}
67
68TEST(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);
74
75  blink::WebBatteryStatus status = ComputeWebBatteryStatus(dictionary);
76
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);
81}
82
83TEST(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);
89
90  blink::WebBatteryStatus status = ComputeWebBatteryStatus(dictionary);
91
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);
96}
97
98TEST(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);
104
105  blink::WebBatteryStatus status = ComputeWebBatteryStatus(dictionary);
106
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);
111}
112
113TEST(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);
119
120  blink::WebBatteryStatus status = ComputeWebBatteryStatus(dictionary);
121
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);
126}
127
128TEST(BatteryStatusManagerLinuxTest, LevelRoundedToThreeSignificantDigits) {
129  base::DictionaryValue dictionary;
130  dictionary.SetDouble("State", UPOWER_DEVICE_STATE_DISCHARGING);
131  dictionary.SetDouble("Percentage", 14.56);
132
133  blink::WebBatteryStatus status = ComputeWebBatteryStatus(dictionary);
134
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);
139}
140
141}  // namespace
142
143}  // namespace content
144