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

Side by Side Diff: ash/system/chromeos/power/tray_power_unittest.cc

Issue 1014753003: Move low battery notification to Message Center. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Made suggested changes Created 5 years, 9 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
« no previous file with comments | « ash/system/chromeos/power/tray_power.cc ('k') | ash/system/chromeos/settings/tray_settings.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 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/system/chromeos/power/tray_power.h" 5 #include "ash/system/chromeos/power/tray_power.h"
6 6
7 #include <map>
8 #include <string>
9
7 #include "ash/ash_switches.h" 10 #include "ash/ash_switches.h"
8 #include "ash/test/ash_test_base.h" 11 #include "ash/test/ash_test_base.h"
9 #include "base/memory/scoped_ptr.h" 12 #include "base/memory/scoped_ptr.h"
10 #include "chromeos/dbus/power_manager/power_supply_properties.pb.h" 13 #include "chromeos/dbus/power_manager/power_supply_properties.pb.h"
11 #include "ui/message_center/fake_message_center.h" 14 #include "ui/message_center/fake_message_center.h"
12 15
13 using message_center::Notification; 16 using message_center::Notification;
14 using power_manager::PowerSupplyProperties; 17 using power_manager::PowerSupplyProperties;
15 18
16 namespace { 19 namespace {
17 20
18 class MockMessageCenter : public message_center::FakeMessageCenter { 21 class MockMessageCenter : public message_center::FakeMessageCenter {
19 public: 22 public:
20 MockMessageCenter() : add_count_(0), remove_count_(0) {} 23 MockMessageCenter() : add_count_(0), remove_count_(0) {}
21 ~MockMessageCenter() override {} 24 ~MockMessageCenter() override {}
22 25
23 int add_count() const { return add_count_; } 26 int add_count() const { return add_count_; }
24 int remove_count() const { return remove_count_; } 27 int remove_count() const { return remove_count_; }
25 28
26 // message_center::FakeMessageCenter overrides: 29 // message_center::FakeMessageCenter overrides:
27 void AddNotification(scoped_ptr<Notification> notification) override { 30 void AddNotification(scoped_ptr<Notification> notification) override {
28 add_count_++; 31 add_count_++;
32 notifications_[notification->id()] = notification.get();
29 } 33 }
30 void RemoveNotification(const std::string& id, bool by_user) override { 34 void RemoveNotification(const std::string& id, bool by_user) override {
31 remove_count_++; 35 remove_count_++;
36 notifications_.erase(id);
37 }
38
39 Notification* FindVisibleNotificationById(const std::string& id) override {
40 auto it = notifications_.find(id);
41 return it == notifications_.end() ? NULL : it->second;
32 } 42 }
33 43
34 private: 44 private:
35 int add_count_; 45 int add_count_;
36 int remove_count_; 46 int remove_count_;
47 std::map<std::string, Notification*> notifications_;
37 48
38 DISALLOW_COPY_AND_ASSIGN(MockMessageCenter); 49 DISALLOW_COPY_AND_ASSIGN(MockMessageCenter);
39 }; 50 };
40 51
41 } // namespace 52 } // namespace
42 53
43 namespace ash { 54 namespace ash {
44 55
45 class TrayPowerTest : public test::AshTestBase { 56 class TrayPowerTest : public test::AshTestBase {
46 public: 57 public:
(...skipping 18 matching lines...) Expand all
65 76
66 TrayPower::NotificationState notification_state() const { 77 TrayPower::NotificationState notification_state() const {
67 return tray_power_->notification_state_; 78 return tray_power_->notification_state_;
68 } 79 }
69 80
70 bool MaybeShowUsbChargerNotification(const PowerSupplyProperties& proto) { 81 bool MaybeShowUsbChargerNotification(const PowerSupplyProperties& proto) {
71 PowerStatus::Get()->SetProtoForTesting(proto); 82 PowerStatus::Get()->SetProtoForTesting(proto);
72 return tray_power_->MaybeShowUsbChargerNotification(); 83 return tray_power_->MaybeShowUsbChargerNotification();
73 } 84 }
74 85
75 bool UpdateNotificationState(const PowerSupplyProperties& proto) { 86 void UpdateNotificationState(const PowerSupplyProperties& proto,
87 TrayPower::NotificationState expected_state,
88 bool expected_add,
89 bool expected_remove) {
90 int prev_add = message_center_->add_count();
91 int prev_remove = message_center_->remove_count();
76 PowerStatus::Get()->SetProtoForTesting(proto); 92 PowerStatus::Get()->SetProtoForTesting(proto);
77 return tray_power_->UpdateNotificationState(); 93 tray_power_->OnPowerStatusChanged();
94 EXPECT_EQ(expected_state, notification_state());
95 EXPECT_EQ(expected_add, message_center_->add_count() == prev_add + 1);
96 EXPECT_EQ(expected_remove,
97 message_center_->remove_count() == prev_remove + 1);
78 } 98 }
79 99
80 void SetUsbChargerConnected(bool connected) { 100 void SetUsbChargerConnected(bool connected) {
81 tray_power_->usb_charger_was_connected_ = connected; 101 tray_power_->usb_charger_was_connected_ = connected;
82 } 102 }
83 103
84 // Returns a discharging PowerSupplyProperties more appropriate for testing. 104 // Returns a discharging PowerSupplyProperties more appropriate for testing.
85 static PowerSupplyProperties DefaultPowerSupplyProperties() { 105 static PowerSupplyProperties DefaultPowerSupplyProperties() {
86 PowerSupplyProperties proto; 106 PowerSupplyProperties proto;
87 proto.set_external_power( 107 proto.set_external_power(
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
134 EXPECT_EQ(1, message_center()->remove_count()); 154 EXPECT_EQ(1, message_center()->remove_count());
135 } 155 }
136 156
137 TEST_F(TrayPowerTest, UpdateNotificationState) { 157 TEST_F(TrayPowerTest, UpdateNotificationState) {
138 // No notifications when no battery present. 158 // No notifications when no battery present.
139 PowerSupplyProperties no_battery = DefaultPowerSupplyProperties(); 159 PowerSupplyProperties no_battery = DefaultPowerSupplyProperties();
140 no_battery.set_external_power( 160 no_battery.set_external_power(
141 power_manager::PowerSupplyProperties_ExternalPower_AC); 161 power_manager::PowerSupplyProperties_ExternalPower_AC);
142 no_battery.set_battery_state( 162 no_battery.set_battery_state(
143 power_manager::PowerSupplyProperties_BatteryState_NOT_PRESENT); 163 power_manager::PowerSupplyProperties_BatteryState_NOT_PRESENT);
144 EXPECT_FALSE(UpdateNotificationState(no_battery)); 164 {
145 EXPECT_EQ(TrayPower::NOTIFICATION_NONE, notification_state()); 165 SCOPED_TRACE("Expected no notifications when no battery present");
oshima 2015/03/20 00:53:15 you may omit "Expected to" part, but am ok to have
michaelpg 2015/03/20 00:55:47 It seems ambiguous to me. Do you usually write the
michaelpg 2015/03/20 03:57:12 Done.
166 UpdateNotificationState(no_battery, TrayPower::NOTIFICATION_NONE, false,
167 false);
168 }
146 169
147 // No notification when calculating remaining battery time. 170 // No notification when calculating remaining battery time.
148 PowerSupplyProperties calculating = DefaultPowerSupplyProperties(); 171 PowerSupplyProperties calculating = DefaultPowerSupplyProperties();
149 calculating.set_is_calculating_battery_time(true); 172 calculating.set_is_calculating_battery_time(true);
150 EXPECT_FALSE(UpdateNotificationState(calculating)); 173 {
151 EXPECT_EQ(TrayPower::NOTIFICATION_NONE, notification_state()); 174 SCOPED_TRACE(
175 "Expected no notification when calculating remaining battery time");
176 UpdateNotificationState(calculating, TrayPower::NOTIFICATION_NONE, false,
177 false);
178 }
152 179
153 // No notification when charging. 180 // No notification when charging.
154 PowerSupplyProperties charging = DefaultPowerSupplyProperties(); 181 PowerSupplyProperties charging = DefaultPowerSupplyProperties();
155 charging.set_external_power( 182 charging.set_external_power(
156 power_manager::PowerSupplyProperties_ExternalPower_AC); 183 power_manager::PowerSupplyProperties_ExternalPower_AC);
157 charging.set_battery_state( 184 charging.set_battery_state(
158 power_manager::PowerSupplyProperties_BatteryState_CHARGING); 185 power_manager::PowerSupplyProperties_BatteryState_CHARGING);
159 EXPECT_FALSE(UpdateNotificationState(charging)); 186 {
160 EXPECT_EQ(TrayPower::NOTIFICATION_NONE, notification_state()); 187 SCOPED_TRACE("Expected no notification when charging");
188 UpdateNotificationState(charging, TrayPower::NOTIFICATION_NONE, false,
189 false);
190 }
161 191
162 // When the rounded minutes-to-empty are above the threshold, no notification 192 // When the rounded minutes-to-empty are above the threshold, no notification
163 // should be shown. 193 // should be shown.
164 PowerSupplyProperties low = DefaultPowerSupplyProperties(); 194 PowerSupplyProperties low = DefaultPowerSupplyProperties();
165 low.set_battery_time_to_empty_sec(TrayPower::kLowPowerMinutes * 60 + 30); 195 low.set_battery_time_to_empty_sec(TrayPower::kLowPowerMinutes * 60 + 30);
166 EXPECT_FALSE(UpdateNotificationState(low)); 196 {
167 EXPECT_EQ(TrayPower::NOTIFICATION_NONE, notification_state()); 197 SCOPED_TRACE("Expected no notification when time to empty above threshold");
198 UpdateNotificationState(low, TrayPower::NOTIFICATION_NONE, false, false);
199 }
168 200
169 // When the rounded value matches the threshold, the notification should 201 // When the rounded value matches the threshold, the notification should
170 // appear. 202 // appear.
171 low.set_battery_time_to_empty_sec(TrayPower::kLowPowerMinutes * 60 + 29); 203 low.set_battery_time_to_empty_sec(TrayPower::kLowPowerMinutes * 60 + 29);
172 EXPECT_TRUE(UpdateNotificationState(low)); 204 {
173 EXPECT_EQ(TrayPower::NOTIFICATION_LOW_POWER, notification_state()); 205 SCOPED_TRACE("Expected notification when time to empty matches threshold");
206 UpdateNotificationState(low, TrayPower::NOTIFICATION_LOW_POWER, true,
207 false);
208 }
174 209
175 // It should persist at lower values. 210 // It should persist at lower values.
176 low.set_battery_time_to_empty_sec(TrayPower::kLowPowerMinutes * 60 - 20); 211 low.set_battery_time_to_empty_sec(TrayPower::kLowPowerMinutes * 60 - 20);
177 EXPECT_FALSE(UpdateNotificationState(low)); 212 {
178 EXPECT_EQ(TrayPower::NOTIFICATION_LOW_POWER, notification_state()); 213 SCOPED_TRACE("Expected notification to persist at lower values");
214 UpdateNotificationState(low, TrayPower::NOTIFICATION_LOW_POWER, false,
215 false);
216 }
179 217
180 // The critical low battery notification should be shown when the rounded 218 // The critical low battery notification should be shown when the rounded
181 // value is at the lower threshold. 219 // value is at the lower threshold.
182 PowerSupplyProperties critical = DefaultPowerSupplyProperties(); 220 PowerSupplyProperties critical = DefaultPowerSupplyProperties();
183 critical.set_battery_time_to_empty_sec(TrayPower::kCriticalMinutes * 60 + 29); 221 critical.set_battery_time_to_empty_sec(TrayPower::kCriticalMinutes * 60 + 29);
184 EXPECT_TRUE(UpdateNotificationState(critical)); 222 {
185 EXPECT_EQ(TrayPower::NOTIFICATION_CRITICAL, notification_state()); 223 SCOPED_TRACE("Expected critical notification when time to empty critical");
224 UpdateNotificationState(critical, TrayPower::NOTIFICATION_CRITICAL, true,
225 true);
226 }
186 227
187 // The notification should be dismissed when the no-warning threshold is 228 // The notification should be dismissed when the no-warning threshold is
188 // reached. 229 // reached.
189 PowerSupplyProperties safe = DefaultPowerSupplyProperties(); 230 PowerSupplyProperties safe = DefaultPowerSupplyProperties();
190 safe.set_battery_time_to_empty_sec(TrayPower::kNoWarningMinutes * 60 - 29); 231 safe.set_battery_time_to_empty_sec(TrayPower::kNoWarningMinutes * 60 - 29);
191 EXPECT_FALSE(UpdateNotificationState(safe)); 232 {
192 EXPECT_EQ(TrayPower::NOTIFICATION_NONE, notification_state()); 233 SCOPED_TRACE("Expected notification removed when battery not low");
234 UpdateNotificationState(safe, TrayPower::NOTIFICATION_NONE, false, true);
235 }
193 236
194 // Test that rounded percentages are used when a USB charger is connected. 237 // Test that rounded percentages are used when a USB charger is connected.
195 PowerSupplyProperties low_usb = DefaultPowerSupplyProperties(); 238 PowerSupplyProperties low_usb = DefaultPowerSupplyProperties();
196 low_usb.set_external_power( 239 low_usb.set_external_power(
197 power_manager::PowerSupplyProperties_ExternalPower_USB); 240 power_manager::PowerSupplyProperties_ExternalPower_USB);
198 low_usb.set_battery_percent(TrayPower::kLowPowerPercentage + 0.5); 241 low_usb.set_battery_percent(TrayPower::kLowPowerPercentage + 0.5);
199 EXPECT_FALSE(UpdateNotificationState(low_usb)); 242 {
200 EXPECT_EQ(TrayPower::NOTIFICATION_NONE, notification_state()); 243 SCOPED_TRACE("Expected notification removed for percent above threshold");
244 UpdateNotificationState(low_usb, TrayPower::NOTIFICATION_NONE, true, false);
245 }
201 246
202 low_usb.set_battery_percent(TrayPower::kLowPowerPercentage + 0.49); 247 low_usb.set_battery_percent(TrayPower::kLowPowerPercentage + 0.49);
203 EXPECT_TRUE(UpdateNotificationState(low_usb)); 248 {
204 EXPECT_EQ(TrayPower::NOTIFICATION_LOW_POWER, notification_state()); 249 SCOPED_TRACE("Expected battery percent to round down");
250 UpdateNotificationState(low_usb, TrayPower::NOTIFICATION_LOW_POWER, true,
251 false);
252 }
205 253
206 PowerSupplyProperties critical_usb = DefaultPowerSupplyProperties(); 254 PowerSupplyProperties critical_usb = DefaultPowerSupplyProperties();
207 critical_usb.set_external_power( 255 critical_usb.set_external_power(
208 power_manager::PowerSupplyProperties_ExternalPower_USB); 256 power_manager::PowerSupplyProperties_ExternalPower_USB);
209 critical_usb.set_battery_percent(TrayPower::kCriticalPercentage + 0.2); 257 critical_usb.set_battery_percent(TrayPower::kCriticalPercentage + 0.2);
210 EXPECT_TRUE(UpdateNotificationState(critical_usb)); 258 {
211 EXPECT_EQ(TrayPower::NOTIFICATION_CRITICAL, notification_state()); 259 SCOPED_TRACE("Expected critical notification for critical percentage");
260 UpdateNotificationState(critical_usb, TrayPower::NOTIFICATION_CRITICAL,
261 true, true);
262 }
212 263
213 PowerSupplyProperties safe_usb = DefaultPowerSupplyProperties(); 264 PowerSupplyProperties safe_usb = DefaultPowerSupplyProperties();
214 safe_usb.set_external_power( 265 safe_usb.set_external_power(
215 power_manager::PowerSupplyProperties_ExternalPower_USB); 266 power_manager::PowerSupplyProperties_ExternalPower_USB);
216 safe_usb.set_battery_percent(TrayPower::kNoWarningPercentage - 0.1); 267 safe_usb.set_battery_percent(TrayPower::kNoWarningPercentage - 0.1);
217 EXPECT_FALSE(UpdateNotificationState(safe_usb)); 268 {
218 EXPECT_EQ(TrayPower::NOTIFICATION_NONE, notification_state()); 269 SCOPED_TRACE("Expected battery percent to round up");
270 UpdateNotificationState(safe_usb, TrayPower::NOTIFICATION_NONE, false,
271 true);
272 }
219 } 273 }
220 274
221 } // namespace ash 275 } // namespace ash
OLDNEW
« no previous file with comments | « ash/system/chromeos/power/tray_power.cc ('k') | ash/system/chromeos/settings/tray_settings.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698