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

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: 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("No notifications when no battery present");
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("No notification when calculating remaining battery time");
175 UpdateNotificationState(calculating, TrayPower::NOTIFICATION_NONE, false,
176 false);
177 }
152 178
153 // No notification when charging. 179 // No notification when charging.
154 PowerSupplyProperties charging = DefaultPowerSupplyProperties(); 180 PowerSupplyProperties charging = DefaultPowerSupplyProperties();
155 charging.set_external_power( 181 charging.set_external_power(
156 power_manager::PowerSupplyProperties_ExternalPower_AC); 182 power_manager::PowerSupplyProperties_ExternalPower_AC);
157 charging.set_battery_state( 183 charging.set_battery_state(
158 power_manager::PowerSupplyProperties_BatteryState_CHARGING); 184 power_manager::PowerSupplyProperties_BatteryState_CHARGING);
159 EXPECT_FALSE(UpdateNotificationState(charging)); 185 {
160 EXPECT_EQ(TrayPower::NOTIFICATION_NONE, notification_state()); 186 SCOPED_TRACE("No notification when charging");
187 UpdateNotificationState(charging, TrayPower::NOTIFICATION_NONE, false,
188 false);
189 }
161 190
162 // When the rounded minutes-to-empty are above the threshold, no notification 191 // When the rounded minutes-to-empty are above the threshold, no notification
163 // should be shown. 192 // should be shown.
164 PowerSupplyProperties low = DefaultPowerSupplyProperties(); 193 PowerSupplyProperties low = DefaultPowerSupplyProperties();
165 low.set_battery_time_to_empty_sec(TrayPower::kLowPowerMinutes * 60 + 30); 194 low.set_battery_time_to_empty_sec(TrayPower::kLowPowerMinutes * 60 + 30);
166 EXPECT_FALSE(UpdateNotificationState(low)); 195 {
167 EXPECT_EQ(TrayPower::NOTIFICATION_NONE, notification_state()); 196 SCOPED_TRACE("No notification when time to empty above threshold");
197 UpdateNotificationState(low, TrayPower::NOTIFICATION_NONE, false, false);
198 }
168 199
169 // When the rounded value matches the threshold, the notification should 200 // When the rounded value matches the threshold, the notification should
170 // appear. 201 // appear.
171 low.set_battery_time_to_empty_sec(TrayPower::kLowPowerMinutes * 60 + 29); 202 low.set_battery_time_to_empty_sec(TrayPower::kLowPowerMinutes * 60 + 29);
172 EXPECT_TRUE(UpdateNotificationState(low)); 203 {
173 EXPECT_EQ(TrayPower::NOTIFICATION_LOW_POWER, notification_state()); 204 SCOPED_TRACE("Notification when time to empty matches threshold");
205 UpdateNotificationState(low, TrayPower::NOTIFICATION_LOW_POWER, true,
206 false);
207 }
174 208
175 // It should persist at lower values. 209 // It should persist at lower values.
176 low.set_battery_time_to_empty_sec(TrayPower::kLowPowerMinutes * 60 - 20); 210 low.set_battery_time_to_empty_sec(TrayPower::kLowPowerMinutes * 60 - 20);
177 EXPECT_FALSE(UpdateNotificationState(low)); 211 {
178 EXPECT_EQ(TrayPower::NOTIFICATION_LOW_POWER, notification_state()); 212 SCOPED_TRACE("Notification persists at lower values");
213 UpdateNotificationState(low, TrayPower::NOTIFICATION_LOW_POWER, false,
214 false);
215 }
179 216
180 // The critical low battery notification should be shown when the rounded 217 // The critical low battery notification should be shown when the rounded
181 // value is at the lower threshold. 218 // value is at the lower threshold.
182 PowerSupplyProperties critical = DefaultPowerSupplyProperties(); 219 PowerSupplyProperties critical = DefaultPowerSupplyProperties();
183 critical.set_battery_time_to_empty_sec(TrayPower::kCriticalMinutes * 60 + 29); 220 critical.set_battery_time_to_empty_sec(TrayPower::kCriticalMinutes * 60 + 29);
184 EXPECT_TRUE(UpdateNotificationState(critical)); 221 {
185 EXPECT_EQ(TrayPower::NOTIFICATION_CRITICAL, notification_state()); 222 SCOPED_TRACE("Critical notification when time to empty is critical");
223 UpdateNotificationState(critical, TrayPower::NOTIFICATION_CRITICAL, true,
224 true);
225 }
186 226
187 // The notification should be dismissed when the no-warning threshold is 227 // The notification should be dismissed when the no-warning threshold is
188 // reached. 228 // reached.
189 PowerSupplyProperties safe = DefaultPowerSupplyProperties(); 229 PowerSupplyProperties safe = DefaultPowerSupplyProperties();
190 safe.set_battery_time_to_empty_sec(TrayPower::kNoWarningMinutes * 60 - 29); 230 safe.set_battery_time_to_empty_sec(TrayPower::kNoWarningMinutes * 60 - 29);
191 EXPECT_FALSE(UpdateNotificationState(safe)); 231 {
192 EXPECT_EQ(TrayPower::NOTIFICATION_NONE, notification_state()); 232 SCOPED_TRACE("Notification removed when battery not low");
233 UpdateNotificationState(safe, TrayPower::NOTIFICATION_NONE, false, true);
234 }
193 235
194 // Test that rounded percentages are used when a USB charger is connected. 236 // Test that rounded percentages are used when a USB charger is connected.
195 PowerSupplyProperties low_usb = DefaultPowerSupplyProperties(); 237 PowerSupplyProperties low_usb = DefaultPowerSupplyProperties();
196 low_usb.set_external_power( 238 low_usb.set_external_power(
197 power_manager::PowerSupplyProperties_ExternalPower_USB); 239 power_manager::PowerSupplyProperties_ExternalPower_USB);
198 low_usb.set_battery_percent(TrayPower::kLowPowerPercentage + 0.5); 240 low_usb.set_battery_percent(TrayPower::kLowPowerPercentage + 0.5);
199 EXPECT_FALSE(UpdateNotificationState(low_usb)); 241 {
200 EXPECT_EQ(TrayPower::NOTIFICATION_NONE, notification_state()); 242 SCOPED_TRACE("No notification for rounded battery percent");
243 UpdateNotificationState(low_usb, TrayPower::NOTIFICATION_NONE, true, false);
244 }
201 245
202 low_usb.set_battery_percent(TrayPower::kLowPowerPercentage + 0.49); 246 low_usb.set_battery_percent(TrayPower::kLowPowerPercentage + 0.49);
203 EXPECT_TRUE(UpdateNotificationState(low_usb)); 247 {
204 EXPECT_EQ(TrayPower::NOTIFICATION_LOW_POWER, notification_state()); 248 SCOPED_TRACE("Notification for rounded low power percent");
249 UpdateNotificationState(low_usb, TrayPower::NOTIFICATION_LOW_POWER, true,
250 false);
251 }
205 252
206 PowerSupplyProperties critical_usb = DefaultPowerSupplyProperties(); 253 PowerSupplyProperties critical_usb = DefaultPowerSupplyProperties();
207 critical_usb.set_external_power( 254 critical_usb.set_external_power(
208 power_manager::PowerSupplyProperties_ExternalPower_USB); 255 power_manager::PowerSupplyProperties_ExternalPower_USB);
209 critical_usb.set_battery_percent(TrayPower::kCriticalPercentage + 0.2); 256 critical_usb.set_battery_percent(TrayPower::kCriticalPercentage + 0.2);
210 EXPECT_TRUE(UpdateNotificationState(critical_usb)); 257 {
211 EXPECT_EQ(TrayPower::NOTIFICATION_CRITICAL, notification_state()); 258 SCOPED_TRACE("Notification for rounded critical power percent");
259 UpdateNotificationState(critical_usb, TrayPower::NOTIFICATION_CRITICAL,
260 true, true);
261 }
212 262
213 PowerSupplyProperties safe_usb = DefaultPowerSupplyProperties(); 263 PowerSupplyProperties safe_usb = DefaultPowerSupplyProperties();
214 safe_usb.set_external_power( 264 safe_usb.set_external_power(
215 power_manager::PowerSupplyProperties_ExternalPower_USB); 265 power_manager::PowerSupplyProperties_ExternalPower_USB);
216 safe_usb.set_battery_percent(TrayPower::kNoWarningPercentage - 0.1); 266 safe_usb.set_battery_percent(TrayPower::kNoWarningPercentage - 0.1);
217 EXPECT_FALSE(UpdateNotificationState(safe_usb)); 267 {
218 EXPECT_EQ(TrayPower::NOTIFICATION_NONE, notification_state()); 268 SCOPED_TRACE("Notification removed for rounded percent above threshold");
269 UpdateNotificationState(safe_usb, TrayPower::NOTIFICATION_NONE, false,
270 true);
271 }
219 } 272 }
220 273
221 } // namespace ash 274 } // 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