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

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: tests added 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 INSTANTIATE_TEST_CASE_P(
76 ,
James Cook 2016/06/21 23:19:00 nit: comment why this is blank
tdanderson 2016/06/22 15:25:00 Done.
77 PowerStatusTest,
78 testing::Values(MaterialDesignController::NON_MATERIAL,
79 MaterialDesignController::MATERIAL_NORMAL,
80 MaterialDesignController::MATERIAL_EXPERIMENTAL));
81
82 TEST_P(PowerStatusTest, InitializeAndUpdate) {
67 // Test that the initial power supply state should be acquired after 83 // Test that the initial power supply state should be acquired after
68 // PowerStatus is instantiated. This depends on 84 // PowerStatus is instantiated. This depends on
69 // PowerManagerClientStubImpl, which responds to power status update 85 // PowerManagerClientStubImpl, which responds to power status update
70 // requests, pretends there is a battery present, and generates some valid 86 // requests, pretends there is a battery present, and generates some valid
71 // power supply status data. 87 // power supply status data.
72 message_loop_.RunUntilIdle(); 88 message_loop_.RunUntilIdle();
73 EXPECT_EQ(1, test_observer_->power_changed_count()); 89 EXPECT_EQ(1, test_observer_->power_changed_count());
74 90
75 // Test RequestUpdate, test_obsever_ should be notified for power suuply 91 // Test RequestUpdate, test_obsever_ should be notified for power suuply
76 // status change. 92 // status change.
77 power_status_->RequestStatusUpdate(); 93 power_status_->RequestStatusUpdate();
78 message_loop_.RunUntilIdle(); 94 message_loop_.RunUntilIdle();
79 EXPECT_EQ(2, test_observer_->power_changed_count()); 95 EXPECT_EQ(2, test_observer_->power_changed_count());
80 } 96 }
81 97
82 TEST_F(PowerStatusTest, ShouldDisplayBatteryTime) { 98 TEST_P(PowerStatusTest, ShouldDisplayBatteryTime) {
83 EXPECT_FALSE(PowerStatus::ShouldDisplayBatteryTime( 99 EXPECT_FALSE(PowerStatus::ShouldDisplayBatteryTime(
84 base::TimeDelta::FromSeconds(-1))); 100 base::TimeDelta::FromSeconds(-1)));
85 EXPECT_FALSE(PowerStatus::ShouldDisplayBatteryTime( 101 EXPECT_FALSE(PowerStatus::ShouldDisplayBatteryTime(
86 base::TimeDelta::FromSeconds(0))); 102 base::TimeDelta::FromSeconds(0)));
87 EXPECT_FALSE(PowerStatus::ShouldDisplayBatteryTime( 103 EXPECT_FALSE(PowerStatus::ShouldDisplayBatteryTime(
88 base::TimeDelta::FromSeconds(59))); 104 base::TimeDelta::FromSeconds(59)));
89 EXPECT_TRUE(PowerStatus::ShouldDisplayBatteryTime( 105 EXPECT_TRUE(PowerStatus::ShouldDisplayBatteryTime(
90 base::TimeDelta::FromSeconds(60))); 106 base::TimeDelta::FromSeconds(60)));
91 EXPECT_TRUE(PowerStatus::ShouldDisplayBatteryTime( 107 EXPECT_TRUE(PowerStatus::ShouldDisplayBatteryTime(
92 base::TimeDelta::FromSeconds(600))); 108 base::TimeDelta::FromSeconds(600)));
93 EXPECT_TRUE(PowerStatus::ShouldDisplayBatteryTime( 109 EXPECT_TRUE(PowerStatus::ShouldDisplayBatteryTime(
94 base::TimeDelta::FromSeconds(3600))); 110 base::TimeDelta::FromSeconds(3600)));
95 EXPECT_TRUE(PowerStatus::ShouldDisplayBatteryTime( 111 EXPECT_TRUE(PowerStatus::ShouldDisplayBatteryTime(
96 base::TimeDelta::FromSeconds( 112 base::TimeDelta::FromSeconds(
97 PowerStatus::kMaxBatteryTimeToDisplaySec))); 113 PowerStatus::kMaxBatteryTimeToDisplaySec)));
98 EXPECT_FALSE(PowerStatus::ShouldDisplayBatteryTime( 114 EXPECT_FALSE(PowerStatus::ShouldDisplayBatteryTime(
99 base::TimeDelta::FromSeconds( 115 base::TimeDelta::FromSeconds(
100 PowerStatus::kMaxBatteryTimeToDisplaySec + 1))); 116 PowerStatus::kMaxBatteryTimeToDisplaySec + 1)));
101 } 117 }
102 118
103 TEST_F(PowerStatusTest, SplitTimeIntoHoursAndMinutes) { 119 TEST_P(PowerStatusTest, SplitTimeIntoHoursAndMinutes) {
104 int hours = 0, minutes = 0; 120 int hours = 0, minutes = 0;
105 PowerStatus::SplitTimeIntoHoursAndMinutes( 121 PowerStatus::SplitTimeIntoHoursAndMinutes(
106 base::TimeDelta::FromSeconds(0), &hours, &minutes); 122 base::TimeDelta::FromSeconds(0), &hours, &minutes);
107 EXPECT_EQ(0, hours); 123 EXPECT_EQ(0, hours);
108 EXPECT_EQ(0, minutes); 124 EXPECT_EQ(0, minutes);
109 125
110 PowerStatus::SplitTimeIntoHoursAndMinutes( 126 PowerStatus::SplitTimeIntoHoursAndMinutes(
111 base::TimeDelta::FromSeconds(60), &hours, &minutes); 127 base::TimeDelta::FromSeconds(60), &hours, &minutes);
112 EXPECT_EQ(0, hours); 128 EXPECT_EQ(0, hours);
113 EXPECT_EQ(1, minutes); 129 EXPECT_EQ(1, minutes);
(...skipping 30 matching lines...) Expand all
144 base::TimeDelta::FromSecondsD(3599.9), &hours, &minutes); 160 base::TimeDelta::FromSecondsD(3599.9), &hours, &minutes);
145 EXPECT_EQ(1, hours); 161 EXPECT_EQ(1, hours);
146 EXPECT_EQ(0, minutes); 162 EXPECT_EQ(0, minutes);
147 163
148 PowerStatus::SplitTimeIntoHoursAndMinutes( 164 PowerStatus::SplitTimeIntoHoursAndMinutes(
149 base::TimeDelta::FromSecondsD(3600.1), &hours, &minutes); 165 base::TimeDelta::FromSecondsD(3600.1), &hours, &minutes);
150 EXPECT_EQ(1, hours); 166 EXPECT_EQ(1, hours);
151 EXPECT_EQ(0, minutes); 167 EXPECT_EQ(0, minutes);
152 } 168 }
153 169
154 TEST_F(PowerStatusTest, GetBatteryImageInfo) { 170 TEST_P(PowerStatusTest, GetBatteryImageInfo) {
171 const bool use_md_icon =
172 ash::MaterialDesignController::UseMaterialDesignSystemIcons();
173
155 PowerSupplyProperties prop; 174 PowerSupplyProperties prop;
156 prop.set_external_power(PowerSupplyProperties::AC); 175 prop.set_external_power(PowerSupplyProperties::AC);
157 prop.set_battery_state(PowerSupplyProperties::CHARGING); 176 prop.set_battery_state(PowerSupplyProperties::CHARGING);
158 prop.set_battery_percent(98.0); 177 prop.set_battery_percent(98.0);
159 power_status_->SetProtoForTesting(prop); 178 power_status_->SetProtoForTesting(prop);
160 const PowerStatus::BatteryImageInfo info_charging_98 = 179 const PowerStatus::BatteryImageInfo info_charging_98 =
161 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT); 180 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT);
162 181
163 // 99% should use the same icon as 98%. 182 // 99% should use the same icon as 98%.
164 prop.set_battery_percent(99.0); 183 prop.set_battery_percent(99.0);
165 power_status_->SetProtoForTesting(prop); 184 power_status_->SetProtoForTesting(prop);
166 EXPECT_EQ(info_charging_98, 185 EXPECT_EQ(info_charging_98,
167 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT)); 186 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT));
168 187
169 // The dark icon set should use a different image. 188 // The dark icon set should use a different image for non-MD, but the
189 // same image for MD.
170 prop.set_battery_percent(98.0); 190 prop.set_battery_percent(98.0);
171 EXPECT_NE(info_charging_98, 191 if (use_md_icon) {
172 power_status_->GetBatteryImageInfo(PowerStatus::ICON_DARK)); 192 EXPECT_EQ(info_charging_98,
193 power_status_->GetBatteryImageInfo(PowerStatus::ICON_DARK));
194 } else {
195 EXPECT_NE(info_charging_98,
196 power_status_->GetBatteryImageInfo(PowerStatus::ICON_DARK));
197 }
173 198
174 // A different icon should be used when the battery is full, too. 199 // A different icon should be used when the battery is full, too.
175 prop.set_battery_state(PowerSupplyProperties::FULL); 200 prop.set_battery_state(PowerSupplyProperties::FULL);
176 prop.set_battery_percent(100.0); 201 prop.set_battery_percent(100.0);
177 power_status_->SetProtoForTesting(prop); 202 power_status_->SetProtoForTesting(prop);
178 EXPECT_NE(info_charging_98, 203 EXPECT_NE(info_charging_98,
179 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT)); 204 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT));
180 205
181 // A much-lower battery level should use a different icon. 206 // A much-lower battery level should use a different icon.
182 prop.set_battery_state(PowerSupplyProperties::CHARGING); 207 prop.set_battery_state(PowerSupplyProperties::CHARGING);
183 prop.set_battery_percent(20.0); 208 prop.set_battery_percent(20.0);
184 power_status_->SetProtoForTesting(prop); 209 power_status_->SetProtoForTesting(prop);
185 EXPECT_NE(info_charging_98, 210 EXPECT_NE(info_charging_98,
186 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT)); 211 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT));
187 212
188 // Ditto for 98%, but on USB instead of AC. 213 // Ditto for 98%, but on USB instead of AC.
189 prop.set_external_power(PowerSupplyProperties::USB); 214 prop.set_external_power(PowerSupplyProperties::USB);
190 prop.set_battery_percent(98.0); 215 prop.set_battery_percent(98.0);
191 power_status_->SetProtoForTesting(prop); 216 power_status_->SetProtoForTesting(prop);
192 EXPECT_NE(info_charging_98, 217 EXPECT_NE(info_charging_98,
193 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT)); 218 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT));
194 } 219 }
195 220
221 // Tests that the |icon_badge| member of BatteryImageInfo is set correctly
222 // with various power supply property values.
223 TEST_P(PowerStatusTest, BatteryImageInfoIconBadge) {
224 PowerSupplyProperties prop;
225
226 // A charging battery connected to AC power should have an ICON_BADGE_BOLT.
227 prop.set_external_power(PowerSupplyProperties::AC);
228 prop.set_battery_state(PowerSupplyProperties::CHARGING);
229 prop.set_battery_percent(98.0);
230 power_status_->SetProtoForTesting(prop);
231 EXPECT_EQ(
232 PowerStatus::ICON_BADGE_BOLT,
233 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT).icon_badge);
234
235 // A discharging battery connected to AC should also have an ICON_BADGE_BOLT.
236 prop.set_battery_state(PowerSupplyProperties::DISCHARGING);
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 charging battery connected to USB power should have an
243 // ICON_BADGE_UNRELIABLE.
244 prop.set_external_power(PowerSupplyProperties::USB);
245 prop.set_battery_state(PowerSupplyProperties::CHARGING);
246 power_status_->SetProtoForTesting(prop);
247 EXPECT_EQ(
248 PowerStatus::ICON_BADGE_UNRELIABLE,
249 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT).icon_badge);
250
251 // A discharging battery connected to USB power should also have an
252 // ICON_BADGE_UNRELIABLE.
253 prop.set_battery_state(PowerSupplyProperties::DISCHARGING);
254 power_status_->SetProtoForTesting(prop);
255 EXPECT_EQ(
256 PowerStatus::ICON_BADGE_UNRELIABLE,
257 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT).icon_badge);
258
259 // Show an ICON_BADGE_X when no battery is present.
260 prop.set_external_power(PowerSupplyProperties::DISCONNECTED);
261 prop.set_battery_state(PowerSupplyProperties::NOT_PRESENT);
262 power_status_->SetProtoForTesting(prop);
263 EXPECT_EQ(
264 PowerStatus::ICON_BADGE_X,
265 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT).icon_badge);
266
267 // Do not show a badge when the battery is discharging.
268 prop.set_battery_state(PowerSupplyProperties::DISCHARGING);
269 power_status_->SetProtoForTesting(prop);
270 EXPECT_EQ(
271 PowerStatus::ICON_BADGE_NONE,
272 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT).icon_badge);
273
274 // Show ICON_BADGE_ALERT for a discharging battery when it falls below
275 // a charge level of 9%.
276 prop.set_battery_percent(9.0);
277 power_status_->SetProtoForTesting(prop);
278 EXPECT_EQ(
279 PowerStatus::ICON_BADGE_NONE,
280 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT).icon_badge);
281 prop.set_battery_percent(8.0);
282 power_status_->SetProtoForTesting(prop);
283 EXPECT_EQ(
284 PowerStatus::ICON_BADGE_ALERT,
285 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT).icon_badge);
286 }
287
288 // Tests that the |charge_level| member of BatteryImageInfo is set correctly
289 // with various power supply property values.
290 TEST_P(PowerStatusTest, BatteryImageInfoChargeLevel) {
291 PowerSupplyProperties prop;
292
293 // No charge level is drawn when the battery is not present.
294 prop.set_external_power(PowerSupplyProperties::DISCONNECTED);
295 prop.set_battery_state(PowerSupplyProperties::NOT_PRESENT);
296 power_status_->SetProtoForTesting(prop);
297 EXPECT_EQ(
298 0,
299 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT).charge_level);
300
301 // A charge level of 1 when the battery is up to 16% full, and a level of 2
James Cook 2016/06/21 23:19:00 optional: maybe also test battery at 0%? I think w
tdanderson 2016/06/22 15:25:01 Done.
302 // for 17% full.
303 prop.set_external_power(PowerSupplyProperties::AC);
304 prop.set_battery_state(PowerSupplyProperties::CHARGING);
305 prop.set_battery_percent(16.0);
306 power_status_->SetProtoForTesting(prop);
307 EXPECT_EQ(
308 1,
309 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT).charge_level);
310 prop.set_battery_percent(17.0);
311 power_status_->SetProtoForTesting(prop);
312 EXPECT_EQ(
313 2,
314 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT).charge_level);
315
316 // A charge level of 6 when the battery is 50% full.
317 prop.set_battery_percent(50.0);
318 power_status_->SetProtoForTesting(prop);
319 EXPECT_EQ(
320 6,
321 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT).charge_level);
322
323 // A charge level of 11 when the battery is 99% full, and a level of 12 when
324 // the battery is 100% full.
325 prop.set_battery_percent(99.0);
326 power_status_->SetProtoForTesting(prop);
327 EXPECT_EQ(
328 11,
329 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT).charge_level);
330 prop.set_battery_percent(100.0);
331 power_status_->SetProtoForTesting(prop);
332 EXPECT_EQ(
333 12,
334 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT).charge_level);
335 }
James Cook 2016/06/21 23:19:00 Nice test suite, very thorough.
tdanderson 2016/06/22 15:25:01 Thank you
336
196 } // namespace ash 337 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698