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

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
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) {
76 PowerStatus::Get()->SetProtoForTesting(proto); 87 PowerStatus::Get()->SetProtoForTesting(proto);
77 return tray_power_->UpdateNotificationState(); 88 tray_power_->OnPowerStatusChanged();
78 } 89 }
79 90
80 void SetUsbChargerConnected(bool connected) { 91 void SetUsbChargerConnected(bool connected) {
81 tray_power_->usb_charger_was_connected_ = connected; 92 tray_power_->usb_charger_was_connected_ = connected;
82 } 93 }
83 94
84 // Returns a discharging PowerSupplyProperties more appropriate for testing. 95 // Returns a discharging PowerSupplyProperties more appropriate for testing.
85 static PowerSupplyProperties DefaultPowerSupplyProperties() { 96 static PowerSupplyProperties DefaultPowerSupplyProperties() {
86 PowerSupplyProperties proto; 97 PowerSupplyProperties proto;
87 proto.set_external_power( 98 proto.set_external_power(
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
134 EXPECT_EQ(1, message_center()->remove_count()); 145 EXPECT_EQ(1, message_center()->remove_count());
135 } 146 }
136 147
137 TEST_F(TrayPowerTest, UpdateNotificationState) { 148 TEST_F(TrayPowerTest, UpdateNotificationState) {
138 // No notifications when no battery present. 149 // No notifications when no battery present.
139 PowerSupplyProperties no_battery = DefaultPowerSupplyProperties(); 150 PowerSupplyProperties no_battery = DefaultPowerSupplyProperties();
140 no_battery.set_external_power( 151 no_battery.set_external_power(
141 power_manager::PowerSupplyProperties_ExternalPower_AC); 152 power_manager::PowerSupplyProperties_ExternalPower_AC);
142 no_battery.set_battery_state( 153 no_battery.set_battery_state(
143 power_manager::PowerSupplyProperties_BatteryState_NOT_PRESENT); 154 power_manager::PowerSupplyProperties_BatteryState_NOT_PRESENT);
144 EXPECT_FALSE(UpdateNotificationState(no_battery)); 155 UpdateNotificationState(no_battery);
145 EXPECT_EQ(TrayPower::NOTIFICATION_NONE, notification_state()); 156 EXPECT_EQ(TrayPower::NOTIFICATION_NONE, notification_state());
Mr4D (OOO till 08-26) 2015/03/17 17:04:37 Instead of having here lots of EXPECT_EQ's - could
michaelpg 2015/03/17 18:32:46 Will do, good idea.
michaelpg 2015/03/17 23:10:39 Done. Added SCOPED_TRACE so the failures will at l
157 EXPECT_EQ(0, message_center()->add_count());
158 EXPECT_EQ(0, message_center()->remove_count());
146 159
147 // No notification when calculating remaining battery time. 160 // No notification when calculating remaining battery time.
148 PowerSupplyProperties calculating = DefaultPowerSupplyProperties(); 161 PowerSupplyProperties calculating = DefaultPowerSupplyProperties();
149 calculating.set_is_calculating_battery_time(true); 162 calculating.set_is_calculating_battery_time(true);
150 EXPECT_FALSE(UpdateNotificationState(calculating)); 163 UpdateNotificationState(calculating);
151 EXPECT_EQ(TrayPower::NOTIFICATION_NONE, notification_state()); 164 EXPECT_EQ(TrayPower::NOTIFICATION_NONE, notification_state());
165 EXPECT_EQ(0, message_center()->add_count());
166 EXPECT_EQ(0, message_center()->remove_count());
152 167
153 // No notification when charging. 168 // No notification when charging.
154 PowerSupplyProperties charging = DefaultPowerSupplyProperties(); 169 PowerSupplyProperties charging = DefaultPowerSupplyProperties();
155 charging.set_external_power( 170 charging.set_external_power(
156 power_manager::PowerSupplyProperties_ExternalPower_AC); 171 power_manager::PowerSupplyProperties_ExternalPower_AC);
157 charging.set_battery_state( 172 charging.set_battery_state(
158 power_manager::PowerSupplyProperties_BatteryState_CHARGING); 173 power_manager::PowerSupplyProperties_BatteryState_CHARGING);
159 EXPECT_FALSE(UpdateNotificationState(charging)); 174 UpdateNotificationState(charging);
160 EXPECT_EQ(TrayPower::NOTIFICATION_NONE, notification_state()); 175 EXPECT_EQ(TrayPower::NOTIFICATION_NONE, notification_state());
176 EXPECT_EQ(0, message_center()->add_count());
177 EXPECT_EQ(0, message_center()->remove_count());
161 178
162 // When the rounded minutes-to-empty are above the threshold, no notification 179 // When the rounded minutes-to-empty are above the threshold, no notification
163 // should be shown. 180 // should be shown.
164 PowerSupplyProperties low = DefaultPowerSupplyProperties(); 181 PowerSupplyProperties low = DefaultPowerSupplyProperties();
165 low.set_battery_time_to_empty_sec(TrayPower::kLowPowerMinutes * 60 + 30); 182 low.set_battery_time_to_empty_sec(TrayPower::kLowPowerMinutes * 60 + 30);
166 EXPECT_FALSE(UpdateNotificationState(low)); 183 UpdateNotificationState(low);
167 EXPECT_EQ(TrayPower::NOTIFICATION_NONE, notification_state()); 184 EXPECT_EQ(TrayPower::NOTIFICATION_NONE, notification_state());
185 EXPECT_EQ(0, message_center()->add_count());
186 EXPECT_EQ(0, message_center()->remove_count());
168 187
169 // When the rounded value matches the threshold, the notification should 188 // When the rounded value matches the threshold, the notification should
170 // appear. 189 // appear.
171 low.set_battery_time_to_empty_sec(TrayPower::kLowPowerMinutes * 60 + 29); 190 low.set_battery_time_to_empty_sec(TrayPower::kLowPowerMinutes * 60 + 29);
172 EXPECT_TRUE(UpdateNotificationState(low)); 191 UpdateNotificationState(low);
173 EXPECT_EQ(TrayPower::NOTIFICATION_LOW_POWER, notification_state()); 192 EXPECT_EQ(TrayPower::NOTIFICATION_LOW_POWER, notification_state());
193 EXPECT_EQ(1, message_center()->add_count());
194 EXPECT_EQ(0, message_center()->remove_count());
174 195
175 // It should persist at lower values. 196 // It should persist at lower values.
176 low.set_battery_time_to_empty_sec(TrayPower::kLowPowerMinutes * 60 - 20); 197 low.set_battery_time_to_empty_sec(TrayPower::kLowPowerMinutes * 60 - 20);
177 EXPECT_FALSE(UpdateNotificationState(low)); 198 UpdateNotificationState(low);
178 EXPECT_EQ(TrayPower::NOTIFICATION_LOW_POWER, notification_state()); 199 EXPECT_EQ(TrayPower::NOTIFICATION_LOW_POWER, notification_state());
200 EXPECT_EQ(1, message_center()->add_count());
201 EXPECT_EQ(0, message_center()->remove_count());
179 202
180 // The critical low battery notification should be shown when the rounded 203 // The critical low battery notification should be shown when the rounded
181 // value is at the lower threshold. 204 // value is at the lower threshold.
182 PowerSupplyProperties critical = DefaultPowerSupplyProperties(); 205 PowerSupplyProperties critical = DefaultPowerSupplyProperties();
183 critical.set_battery_time_to_empty_sec(TrayPower::kCriticalMinutes * 60 + 29); 206 critical.set_battery_time_to_empty_sec(TrayPower::kCriticalMinutes * 60 + 29);
184 EXPECT_TRUE(UpdateNotificationState(critical)); 207 UpdateNotificationState(critical);
185 EXPECT_EQ(TrayPower::NOTIFICATION_CRITICAL, notification_state()); 208 EXPECT_EQ(TrayPower::NOTIFICATION_CRITICAL, notification_state());
209 EXPECT_EQ(2, message_center()->add_count());
210 EXPECT_EQ(1, message_center()->remove_count());
186 211
187 // The notification should be dismissed when the no-warning threshold is 212 // The notification should be dismissed when the no-warning threshold is
188 // reached. 213 // reached.
189 PowerSupplyProperties safe = DefaultPowerSupplyProperties(); 214 PowerSupplyProperties safe = DefaultPowerSupplyProperties();
190 safe.set_battery_time_to_empty_sec(TrayPower::kNoWarningMinutes * 60 - 29); 215 safe.set_battery_time_to_empty_sec(TrayPower::kNoWarningMinutes * 60 - 29);
191 EXPECT_FALSE(UpdateNotificationState(safe)); 216 UpdateNotificationState(safe);
192 EXPECT_EQ(TrayPower::NOTIFICATION_NONE, notification_state()); 217 EXPECT_EQ(TrayPower::NOTIFICATION_NONE, notification_state());
218 EXPECT_EQ(2, message_center()->add_count());
219 EXPECT_EQ(2, message_center()->remove_count());
193 220
194 // Test that rounded percentages are used when a USB charger is connected. 221 // Test that rounded percentages are used when a USB charger is connected.
195 PowerSupplyProperties low_usb = DefaultPowerSupplyProperties(); 222 PowerSupplyProperties low_usb = DefaultPowerSupplyProperties();
196 low_usb.set_external_power( 223 low_usb.set_external_power(
197 power_manager::PowerSupplyProperties_ExternalPower_USB); 224 power_manager::PowerSupplyProperties_ExternalPower_USB);
198 low_usb.set_battery_percent(TrayPower::kLowPowerPercentage + 0.5); 225 low_usb.set_battery_percent(TrayPower::kLowPowerPercentage + 0.5);
199 EXPECT_FALSE(UpdateNotificationState(low_usb)); 226 UpdateNotificationState(low_usb);
200 EXPECT_EQ(TrayPower::NOTIFICATION_NONE, notification_state()); 227 EXPECT_EQ(TrayPower::NOTIFICATION_NONE, notification_state());
228 EXPECT_EQ(3, message_center()->add_count());
229 EXPECT_EQ(2, message_center()->remove_count());
201 230
202 low_usb.set_battery_percent(TrayPower::kLowPowerPercentage + 0.49); 231 low_usb.set_battery_percent(TrayPower::kLowPowerPercentage + 0.49);
203 EXPECT_TRUE(UpdateNotificationState(low_usb)); 232 UpdateNotificationState(low_usb);
204 EXPECT_EQ(TrayPower::NOTIFICATION_LOW_POWER, notification_state()); 233 EXPECT_EQ(TrayPower::NOTIFICATION_LOW_POWER, notification_state());
234 EXPECT_EQ(4, message_center()->add_count());
235 EXPECT_EQ(2, message_center()->remove_count());
205 236
206 PowerSupplyProperties critical_usb = DefaultPowerSupplyProperties(); 237 PowerSupplyProperties critical_usb = DefaultPowerSupplyProperties();
207 critical_usb.set_external_power( 238 critical_usb.set_external_power(
208 power_manager::PowerSupplyProperties_ExternalPower_USB); 239 power_manager::PowerSupplyProperties_ExternalPower_USB);
209 critical_usb.set_battery_percent(TrayPower::kCriticalPercentage + 0.2); 240 critical_usb.set_battery_percent(TrayPower::kCriticalPercentage + 0.2);
210 EXPECT_TRUE(UpdateNotificationState(critical_usb)); 241 UpdateNotificationState(critical_usb);
211 EXPECT_EQ(TrayPower::NOTIFICATION_CRITICAL, notification_state()); 242 EXPECT_EQ(TrayPower::NOTIFICATION_CRITICAL, notification_state());
243 EXPECT_EQ(5, message_center()->add_count());
244 EXPECT_EQ(3, message_center()->remove_count());
212 245
213 PowerSupplyProperties safe_usb = DefaultPowerSupplyProperties(); 246 PowerSupplyProperties safe_usb = DefaultPowerSupplyProperties();
214 safe_usb.set_external_power( 247 safe_usb.set_external_power(
215 power_manager::PowerSupplyProperties_ExternalPower_USB); 248 power_manager::PowerSupplyProperties_ExternalPower_USB);
216 safe_usb.set_battery_percent(TrayPower::kNoWarningPercentage - 0.1); 249 safe_usb.set_battery_percent(TrayPower::kNoWarningPercentage - 0.1);
217 EXPECT_FALSE(UpdateNotificationState(safe_usb)); 250 UpdateNotificationState(safe_usb);
218 EXPECT_EQ(TrayPower::NOTIFICATION_NONE, notification_state()); 251 EXPECT_EQ(TrayPower::NOTIFICATION_NONE, notification_state());
252 EXPECT_EQ(5, message_center()->add_count());
253 EXPECT_EQ(4, message_center()->remove_count());
219 } 254 }
220 255
221 } // namespace ash 256 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698