Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(644)

Side by Side Diff: ash/common/system/chromeos/power/power_status_unittest.cc

Issue 2063633002: Render Ash material design battery image icon without PNGs (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: add battery percentage constant Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ash/common/system/chromeos/power/power_status.h" 5 #include "ash/common/system/chromeos/power/power_status.h"
6 6
7 #include <memory> 7 #include <memory>
8 8
9 #include "ash/common/material_design/material_design_controller.h"
10 #include "ash/test/material_design_controller_test_api.h"
9 #include "base/message_loop/message_loop.h" 11 #include "base/message_loop/message_loop.h"
10 #include "chromeos/dbus/dbus_thread_manager.h" 12 #include "chromeos/dbus/dbus_thread_manager.h"
11 #include "testing/gtest/include/gtest/gtest.h" 13 #include "testing/gtest/include/gtest/gtest.h"
12 #include "third_party/cros_system_api/dbus/service_constants.h" 14 #include "third_party/cros_system_api/dbus/service_constants.h"
13 15
14 using power_manager::PowerSupplyProperties; 16 using power_manager::PowerSupplyProperties;
15 17
16 namespace ash { 18 namespace ash {
17 namespace { 19 namespace {
18 20
19 class TestObserver : public PowerStatus::Observer { 21 class TestObserver : public PowerStatus::Observer {
20 public: 22 public:
21 TestObserver() : power_changed_count_(0) {} 23 TestObserver() : power_changed_count_(0) {}
22 ~TestObserver() override {} 24 ~TestObserver() override {}
23 25
24 int power_changed_count() const { return power_changed_count_; } 26 int power_changed_count() const { return power_changed_count_; }
25 27
26 // PowerStatus::Observer overrides: 28 // PowerStatus::Observer overrides:
27 void OnPowerStatusChanged() override { ++power_changed_count_; } 29 void OnPowerStatusChanged() override { ++power_changed_count_; }
28 30
29 private: 31 private:
30 int power_changed_count_; 32 int power_changed_count_;
31 33
32 DISALLOW_COPY_AND_ASSIGN(TestObserver); 34 DISALLOW_COPY_AND_ASSIGN(TestObserver);
33 }; 35 };
34 36
35 } // namespace 37 } // namespace
36 38
37 class PowerStatusTest : public testing::Test { 39 class PowerStatusTest
40 : public testing::Test,
41 public testing::WithParamInterface<MaterialDesignController::Mode> {
38 public: 42 public:
39 PowerStatusTest() : power_status_(NULL) {} 43 PowerStatusTest() : power_status_(NULL) {}
40 ~PowerStatusTest() override {} 44 ~PowerStatusTest() override {}
41 45
42 void SetUp() override { 46 void SetUp() override {
43 chromeos::DBusThreadManager::Initialize(); 47 chromeos::DBusThreadManager::Initialize();
44 PowerStatus::Initialize(); 48 PowerStatus::Initialize();
45 power_status_ = PowerStatus::Get(); 49 power_status_ = PowerStatus::Get();
46 test_observer_.reset(new TestObserver); 50 test_observer_.reset(new TestObserver);
47 power_status_->AddObserver(test_observer_.get()); 51 power_status_->AddObserver(test_observer_.get());
52 material_design_state_.reset(
53 new test::MaterialDesignControllerTestAPI(GetParam()));
48 } 54 }
49 55
50 void TearDown() override { 56 void TearDown() override {
57 material_design_state_.reset();
51 power_status_->RemoveObserver(test_observer_.get()); 58 power_status_->RemoveObserver(test_observer_.get());
52 test_observer_.reset(); 59 test_observer_.reset();
53 PowerStatus::Shutdown(); 60 PowerStatus::Shutdown();
54 chromeos::DBusThreadManager::Shutdown(); 61 chromeos::DBusThreadManager::Shutdown();
55 } 62 }
56 63
57 protected: 64 protected:
58 base::MessageLoopForUI message_loop_; 65 base::MessageLoopForUI message_loop_;
59 PowerStatus* power_status_; // Not owned. 66 PowerStatus* power_status_; // Not owned.
60 std::unique_ptr<TestObserver> test_observer_; 67 std::unique_ptr<TestObserver> test_observer_;
61 68
62 private: 69 private:
70 std::unique_ptr<test::MaterialDesignControllerTestAPI> material_design_state_;
71
63 DISALLOW_COPY_AND_ASSIGN(PowerStatusTest); 72 DISALLOW_COPY_AND_ASSIGN(PowerStatusTest);
64 }; 73 };
65 74
66 TEST_F(PowerStatusTest, InitializeAndUpdate) { 75 // The prefix has intentionally been left blank since there is only one
76 // parameterization of this test fixture.
77 INSTANTIATE_TEST_CASE_P(
78 /* prefix intentionally left blank */,
79 PowerStatusTest,
80 testing::Values(MaterialDesignController::NON_MATERIAL,
81 MaterialDesignController::MATERIAL_NORMAL,
82 MaterialDesignController::MATERIAL_EXPERIMENTAL));
83
84 TEST_P(PowerStatusTest, InitializeAndUpdate) {
67 // Test that the initial power supply state should be acquired after 85 // Test that the initial power supply state should be acquired after
68 // PowerStatus is instantiated. This depends on 86 // PowerStatus is instantiated. This depends on
69 // PowerManagerClientStubImpl, which responds to power status update 87 // PowerManagerClientStubImpl, which responds to power status update
70 // requests, pretends there is a battery present, and generates some valid 88 // requests, pretends there is a battery present, and generates some valid
71 // power supply status data. 89 // power supply status data.
72 message_loop_.RunUntilIdle(); 90 message_loop_.RunUntilIdle();
73 EXPECT_EQ(1, test_observer_->power_changed_count()); 91 EXPECT_EQ(1, test_observer_->power_changed_count());
74 92
75 // Test RequestUpdate, test_obsever_ should be notified for power suuply 93 // Test RequestUpdate, test_obsever_ should be notified for power suuply
76 // status change. 94 // status change.
77 power_status_->RequestStatusUpdate(); 95 power_status_->RequestStatusUpdate();
78 message_loop_.RunUntilIdle(); 96 message_loop_.RunUntilIdle();
79 EXPECT_EQ(2, test_observer_->power_changed_count()); 97 EXPECT_EQ(2, test_observer_->power_changed_count());
80 } 98 }
81 99
82 TEST_F(PowerStatusTest, ShouldDisplayBatteryTime) { 100 TEST_P(PowerStatusTest, ShouldDisplayBatteryTime) {
83 EXPECT_FALSE(PowerStatus::ShouldDisplayBatteryTime( 101 EXPECT_FALSE(PowerStatus::ShouldDisplayBatteryTime(
84 base::TimeDelta::FromSeconds(-1))); 102 base::TimeDelta::FromSeconds(-1)));
85 EXPECT_FALSE(PowerStatus::ShouldDisplayBatteryTime( 103 EXPECT_FALSE(PowerStatus::ShouldDisplayBatteryTime(
86 base::TimeDelta::FromSeconds(0))); 104 base::TimeDelta::FromSeconds(0)));
87 EXPECT_FALSE(PowerStatus::ShouldDisplayBatteryTime( 105 EXPECT_FALSE(PowerStatus::ShouldDisplayBatteryTime(
88 base::TimeDelta::FromSeconds(59))); 106 base::TimeDelta::FromSeconds(59)));
89 EXPECT_TRUE(PowerStatus::ShouldDisplayBatteryTime( 107 EXPECT_TRUE(PowerStatus::ShouldDisplayBatteryTime(
90 base::TimeDelta::FromSeconds(60))); 108 base::TimeDelta::FromSeconds(60)));
91 EXPECT_TRUE(PowerStatus::ShouldDisplayBatteryTime( 109 EXPECT_TRUE(PowerStatus::ShouldDisplayBatteryTime(
92 base::TimeDelta::FromSeconds(600))); 110 base::TimeDelta::FromSeconds(600)));
93 EXPECT_TRUE(PowerStatus::ShouldDisplayBatteryTime( 111 EXPECT_TRUE(PowerStatus::ShouldDisplayBatteryTime(
94 base::TimeDelta::FromSeconds(3600))); 112 base::TimeDelta::FromSeconds(3600)));
95 EXPECT_TRUE(PowerStatus::ShouldDisplayBatteryTime( 113 EXPECT_TRUE(PowerStatus::ShouldDisplayBatteryTime(
96 base::TimeDelta::FromSeconds( 114 base::TimeDelta::FromSeconds(
97 PowerStatus::kMaxBatteryTimeToDisplaySec))); 115 PowerStatus::kMaxBatteryTimeToDisplaySec)));
98 EXPECT_FALSE(PowerStatus::ShouldDisplayBatteryTime( 116 EXPECT_FALSE(PowerStatus::ShouldDisplayBatteryTime(
99 base::TimeDelta::FromSeconds( 117 base::TimeDelta::FromSeconds(
100 PowerStatus::kMaxBatteryTimeToDisplaySec + 1))); 118 PowerStatus::kMaxBatteryTimeToDisplaySec + 1)));
101 } 119 }
102 120
103 TEST_F(PowerStatusTest, SplitTimeIntoHoursAndMinutes) { 121 TEST_P(PowerStatusTest, SplitTimeIntoHoursAndMinutes) {
104 int hours = 0, minutes = 0; 122 int hours = 0, minutes = 0;
105 PowerStatus::SplitTimeIntoHoursAndMinutes( 123 PowerStatus::SplitTimeIntoHoursAndMinutes(
106 base::TimeDelta::FromSeconds(0), &hours, &minutes); 124 base::TimeDelta::FromSeconds(0), &hours, &minutes);
107 EXPECT_EQ(0, hours); 125 EXPECT_EQ(0, hours);
108 EXPECT_EQ(0, minutes); 126 EXPECT_EQ(0, minutes);
109 127
110 PowerStatus::SplitTimeIntoHoursAndMinutes( 128 PowerStatus::SplitTimeIntoHoursAndMinutes(
111 base::TimeDelta::FromSeconds(60), &hours, &minutes); 129 base::TimeDelta::FromSeconds(60), &hours, &minutes);
112 EXPECT_EQ(0, hours); 130 EXPECT_EQ(0, hours);
113 EXPECT_EQ(1, minutes); 131 EXPECT_EQ(1, minutes);
(...skipping 30 matching lines...) Expand all
144 base::TimeDelta::FromSecondsD(3599.9), &hours, &minutes); 162 base::TimeDelta::FromSecondsD(3599.9), &hours, &minutes);
145 EXPECT_EQ(1, hours); 163 EXPECT_EQ(1, hours);
146 EXPECT_EQ(0, minutes); 164 EXPECT_EQ(0, minutes);
147 165
148 PowerStatus::SplitTimeIntoHoursAndMinutes( 166 PowerStatus::SplitTimeIntoHoursAndMinutes(
149 base::TimeDelta::FromSecondsD(3600.1), &hours, &minutes); 167 base::TimeDelta::FromSecondsD(3600.1), &hours, &minutes);
150 EXPECT_EQ(1, hours); 168 EXPECT_EQ(1, hours);
151 EXPECT_EQ(0, minutes); 169 EXPECT_EQ(0, minutes);
152 } 170 }
153 171
154 TEST_F(PowerStatusTest, GetBatteryImageInfo) { 172 TEST_P(PowerStatusTest, GetBatteryImageInfo) {
173 const bool use_md_icon =
174 ash::MaterialDesignController::UseMaterialDesignSystemIcons();
175
155 PowerSupplyProperties prop; 176 PowerSupplyProperties prop;
156 prop.set_external_power(PowerSupplyProperties::AC); 177 prop.set_external_power(PowerSupplyProperties::AC);
157 prop.set_battery_state(PowerSupplyProperties::CHARGING); 178 prop.set_battery_state(PowerSupplyProperties::CHARGING);
158 prop.set_battery_percent(98.0); 179 prop.set_battery_percent(98.0);
159 power_status_->SetProtoForTesting(prop); 180 power_status_->SetProtoForTesting(prop);
160 const PowerStatus::BatteryImageInfo info_charging_98 = 181 const PowerStatus::BatteryImageInfo info_charging_98 =
161 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT); 182 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT);
162 183
163 // 99% should use the same icon as 98%. 184 // 99% should use the same icon as 98%.
164 prop.set_battery_percent(99.0); 185 prop.set_battery_percent(99.0);
165 power_status_->SetProtoForTesting(prop); 186 power_status_->SetProtoForTesting(prop);
166 EXPECT_EQ(info_charging_98, 187 EXPECT_EQ(info_charging_98,
167 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT)); 188 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT));
168 189
169 // The dark icon set should use a different image. 190 // The dark icon set should use a different image for non-MD, but the
191 // same image for MD.
170 prop.set_battery_percent(98.0); 192 prop.set_battery_percent(98.0);
171 EXPECT_NE(info_charging_98, 193 if (use_md_icon) {
172 power_status_->GetBatteryImageInfo(PowerStatus::ICON_DARK)); 194 EXPECT_EQ(info_charging_98,
195 power_status_->GetBatteryImageInfo(PowerStatus::ICON_DARK));
196 } else {
197 EXPECT_NE(info_charging_98,
198 power_status_->GetBatteryImageInfo(PowerStatus::ICON_DARK));
199 }
173 200
174 // A different icon should be used when the battery is full, too. 201 // A different icon should be used when the battery is full, too.
175 prop.set_battery_state(PowerSupplyProperties::FULL); 202 prop.set_battery_state(PowerSupplyProperties::FULL);
176 prop.set_battery_percent(100.0); 203 prop.set_battery_percent(100.0);
177 power_status_->SetProtoForTesting(prop); 204 power_status_->SetProtoForTesting(prop);
178 EXPECT_NE(info_charging_98, 205 EXPECT_NE(info_charging_98,
179 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT)); 206 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT));
180 207
181 // A much-lower battery level should use a different icon. 208 // A much-lower battery level should use a different icon.
182 prop.set_battery_state(PowerSupplyProperties::CHARGING); 209 prop.set_battery_state(PowerSupplyProperties::CHARGING);
183 prop.set_battery_percent(20.0); 210 prop.set_battery_percent(20.0);
184 power_status_->SetProtoForTesting(prop); 211 power_status_->SetProtoForTesting(prop);
185 EXPECT_NE(info_charging_98, 212 EXPECT_NE(info_charging_98,
186 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT)); 213 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT));
187 214
188 // Ditto for 98%, but on USB instead of AC. 215 // Ditto for 98%, but on USB instead of AC.
189 prop.set_external_power(PowerSupplyProperties::USB); 216 prop.set_external_power(PowerSupplyProperties::USB);
190 prop.set_battery_percent(98.0); 217 prop.set_battery_percent(98.0);
191 power_status_->SetProtoForTesting(prop); 218 power_status_->SetProtoForTesting(prop);
192 EXPECT_NE(info_charging_98, 219 EXPECT_NE(info_charging_98,
193 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT)); 220 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT));
194 } 221 }
195 222
223 // Tests that the |icon_badge| member of BatteryImageInfo is set correctly
224 // with various power supply property values.
225 TEST_P(PowerStatusTest, BatteryImageInfoIconBadge) {
226 // The |icon_badge| member is only populated for the material design
227 // battery icon.
228 if (!ash::MaterialDesignController::UseMaterialDesignSystemIcons())
229 return;
230
231 PowerSupplyProperties prop;
232
233 // A charging battery connected to AC power should have an ICON_BADGE_BOLT.
234 prop.set_external_power(PowerSupplyProperties::AC);
235 prop.set_battery_state(PowerSupplyProperties::CHARGING);
236 prop.set_battery_percent(98.0);
237 power_status_->SetProtoForTesting(prop);
238 EXPECT_EQ(
239 PowerStatus::ICON_BADGE_BOLT,
240 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT).icon_badge);
241
242 // A discharging battery connected to AC should also have an ICON_BADGE_BOLT.
243 prop.set_battery_state(PowerSupplyProperties::DISCHARGING);
244 power_status_->SetProtoForTesting(prop);
245 EXPECT_EQ(
246 PowerStatus::ICON_BADGE_BOLT,
247 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT).icon_badge);
248
249 // A charging battery connected to USB power should have an
250 // ICON_BADGE_UNRELIABLE.
251 prop.set_external_power(PowerSupplyProperties::USB);
252 prop.set_battery_state(PowerSupplyProperties::CHARGING);
253 power_status_->SetProtoForTesting(prop);
254 EXPECT_EQ(
255 PowerStatus::ICON_BADGE_UNRELIABLE,
256 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT).icon_badge);
257
258 // A discharging battery connected to USB power should also have an
259 // ICON_BADGE_UNRELIABLE.
260 prop.set_battery_state(PowerSupplyProperties::DISCHARGING);
261 power_status_->SetProtoForTesting(prop);
262 EXPECT_EQ(
263 PowerStatus::ICON_BADGE_UNRELIABLE,
264 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT).icon_badge);
265
266 // Show an ICON_BADGE_X when no battery is present.
267 prop.set_external_power(PowerSupplyProperties::DISCONNECTED);
268 prop.set_battery_state(PowerSupplyProperties::NOT_PRESENT);
269 power_status_->SetProtoForTesting(prop);
270 EXPECT_EQ(
271 PowerStatus::ICON_BADGE_X,
272 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT).icon_badge);
273
274 // Do not show a badge when the battery is discharging.
275 prop.set_battery_state(PowerSupplyProperties::DISCHARGING);
276 power_status_->SetProtoForTesting(prop);
277 EXPECT_EQ(
278 PowerStatus::ICON_BADGE_NONE,
279 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT).icon_badge);
280
281 // Show ICON_BADGE_ALERT for a discharging battery when it falls below
282 // a charge level of PowerStatus::kCriticalBatteryChargePercentageMd.
283 prop.set_battery_percent(PowerStatus::kCriticalBatteryChargePercentageMd);
284 power_status_->SetProtoForTesting(prop);
285 EXPECT_EQ(
286 PowerStatus::ICON_BADGE_NONE,
287 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT).icon_badge);
288 prop.set_battery_percent(PowerStatus::kCriticalBatteryChargePercentageMd - 1);
289 power_status_->SetProtoForTesting(prop);
290 EXPECT_EQ(
291 PowerStatus::ICON_BADGE_ALERT,
292 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT).icon_badge);
293 }
294
295 // Tests that the |charge_level| member of BatteryImageInfo is set correctly
296 // with various power supply property values.
297 TEST_P(PowerStatusTest, BatteryImageInfoChargeLevel) {
298 // The |charge_level| member is only populated for the material design
299 // battery icon.
300 if (!ash::MaterialDesignController::UseMaterialDesignSystemIcons())
301 return;
302
303 PowerSupplyProperties prop;
304
305 // No charge level is drawn when the battery is not present.
306 prop.set_external_power(PowerSupplyProperties::DISCONNECTED);
307 prop.set_battery_state(PowerSupplyProperties::NOT_PRESENT);
308 power_status_->SetProtoForTesting(prop);
309 EXPECT_EQ(
310 0,
311 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT).charge_level);
312
313 // A charge level of 0 when the battery is 0% full.
314 prop.set_external_power(PowerSupplyProperties::AC);
315 prop.set_battery_state(PowerSupplyProperties::CHARGING);
316 prop.set_battery_percent(0.0);
317 EXPECT_EQ(
318 0,
319 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT).charge_level);
320
321 // A charge level of 1 when the battery is up to 16% full, and a level of 2
322 // for 17% full.
323 prop.set_battery_percent(16.0);
324 power_status_->SetProtoForTesting(prop);
325 EXPECT_EQ(
326 1,
327 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT).charge_level);
328 prop.set_battery_percent(17.0);
329 power_status_->SetProtoForTesting(prop);
330 EXPECT_EQ(
331 2,
332 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT).charge_level);
333
334 // A charge level of 6 when the battery is 50% full.
335 prop.set_battery_percent(50.0);
336 power_status_->SetProtoForTesting(prop);
337 EXPECT_EQ(
338 6,
339 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT).charge_level);
340
341 // A charge level of 11 when the battery is 99% full, and a level of 12 when
342 // the battery is 100% full.
343 prop.set_battery_percent(99.0);
344 power_status_->SetProtoForTesting(prop);
345 EXPECT_EQ(
346 11,
347 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT).charge_level);
348 prop.set_battery_percent(100.0);
349 power_status_->SetProtoForTesting(prop);
350 EXPECT_EQ(
351 12,
352 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT).charge_level);
353 }
354
196 } // namespace ash 355 } // namespace ash
OLDNEW
« no previous file with comments | « ash/common/system/chromeos/power/power_status.cc ('k') | ash/common/system/chromeos/power/power_status_view.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698