forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpower_notification_controller_unittest.cc
506 lines (441 loc) · 19.9 KB
/
power_notification_controller_unittest.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/system/power/power_notification_controller.h"
#include <map>
#include <memory>
#include <string>
#include "ash/test/ash_test_base.h"
#include "chromeos/dbus/power_manager/power_supply_properties.pb.h"
#include "ui/message_center/fake_message_center.h"
using message_center::Notification;
using power_manager::PowerSupplyProperties;
namespace {
class MockMessageCenter : public message_center::FakeMessageCenter {
public:
MockMessageCenter() : add_count_(0), remove_count_(0), update_count_(0) {}
~MockMessageCenter() override = default;
int add_count() const { return add_count_; }
int remove_count() const { return remove_count_; }
int update_count() const { return update_count_; }
// message_center::FakeMessageCenter overrides:
void AddNotification(std::unique_ptr<Notification> notification) override {
add_count_++;
notifications_.insert(
std::make_pair(notification->id(), std::move(notification)));
}
void RemoveNotification(const std::string& id, bool by_user) override {
Notification* notification = FindVisibleNotificationById(id);
if (notification && notification->delegate())
notification->delegate()->Close(by_user);
remove_count_++;
notifications_.erase(id);
}
void UpdateNotification(
const std::string& id,
std::unique_ptr<Notification> new_notification) override {
update_count_++;
Notification* notification = FindVisibleNotificationById(id);
if (notification)
notifications_.erase(id);
notifications_.insert(
std::make_pair(new_notification->id(), std::move(new_notification)));
}
Notification* FindVisibleNotificationById(const std::string& id) override {
auto it = notifications_.find(id);
return it == notifications_.end() ? NULL : it->second.get();
}
private:
int add_count_;
int remove_count_;
int update_count_;
std::map<std::string, std::unique_ptr<Notification>> notifications_;
DISALLOW_COPY_AND_ASSIGN(MockMessageCenter);
};
} // namespace
namespace ash {
class PowerNotificationControllerTest : public AshTestBase {
public:
PowerNotificationControllerTest() = default;
~PowerNotificationControllerTest() override = default;
MockMessageCenter* message_center() { return message_center_.get(); }
PowerNotificationController* controller() { return controller_.get(); }
// AshTestBase:
void SetUp() override {
AshTestBase::SetUp();
message_center_.reset(new MockMessageCenter());
controller_.reset(new PowerNotificationController(message_center_.get()));
}
void TearDown() override {
controller_.reset();
message_center_.reset();
AshTestBase::TearDown();
}
PowerNotificationController::NotificationState notification_state() const {
return controller_->notification_state_;
}
bool MaybeShowUsbChargerNotification(const PowerSupplyProperties& proto) {
PowerStatus::Get()->SetProtoForTesting(proto);
return controller_->MaybeShowUsbChargerNotification();
}
void MaybeShowDualRoleNotification(const PowerSupplyProperties& proto) {
PowerStatus::Get()->SetProtoForTesting(proto);
controller_->MaybeShowDualRoleNotification();
}
void UpdateNotificationState(
const PowerSupplyProperties& proto,
PowerNotificationController::NotificationState expected_state,
bool expected_add,
bool expected_remove) {
int prev_add = message_center_->add_count();
int prev_remove = message_center_->remove_count();
PowerStatus::Get()->SetProtoForTesting(proto);
controller_->OnPowerStatusChanged();
EXPECT_EQ(expected_state, notification_state());
EXPECT_EQ(expected_add, message_center_->add_count() == prev_add + 1);
EXPECT_EQ(expected_remove,
message_center_->remove_count() == prev_remove + 1);
}
void SetUsbChargerWasConnected(bool connected) {
controller_->usb_charger_was_connected_ = connected;
}
void SetBatteryWasFull(bool full) { controller_->battery_was_full_ = full; }
// Returns a discharging PowerSupplyProperties more appropriate for testing.
static PowerSupplyProperties DefaultPowerSupplyProperties() {
PowerSupplyProperties proto;
proto.set_external_power(
power_manager::PowerSupplyProperties_ExternalPower_DISCONNECTED);
proto.set_battery_state(
power_manager::PowerSupplyProperties_BatteryState_DISCHARGING);
proto.set_battery_percent(50.0);
proto.set_battery_time_to_empty_sec(3 * 60 * 60);
proto.set_battery_time_to_full_sec(2 * 60 * 60);
proto.set_is_calculating_battery_time(false);
return proto;
}
private:
std::unique_ptr<MockMessageCenter> message_center_;
std::unique_ptr<PowerNotificationController> controller_;
DISALLOW_COPY_AND_ASSIGN(PowerNotificationControllerTest);
};
TEST_F(PowerNotificationControllerTest, MaybeShowUsbChargerNotification) {
PowerSupplyProperties discharging = DefaultPowerSupplyProperties();
EXPECT_FALSE(MaybeShowUsbChargerNotification(discharging));
EXPECT_EQ(0, message_center()->add_count());
EXPECT_EQ(0, message_center()->remove_count());
// Notification shows when connecting a USB charger.
PowerSupplyProperties usb_connected = DefaultPowerSupplyProperties();
usb_connected.set_external_power(
power_manager::PowerSupplyProperties_ExternalPower_USB);
EXPECT_TRUE(MaybeShowUsbChargerNotification(usb_connected));
EXPECT_EQ(1, message_center()->add_count());
EXPECT_EQ(0, message_center()->remove_count());
SetUsbChargerWasConnected(true);
// Change in charge does not trigger the notification again.
PowerSupplyProperties more_charge = DefaultPowerSupplyProperties();
more_charge.set_external_power(
power_manager::PowerSupplyProperties_ExternalPower_USB);
more_charge.set_battery_time_to_full_sec(60 * 60);
more_charge.set_battery_percent(75.0);
EXPECT_FALSE(MaybeShowUsbChargerNotification(more_charge));
EXPECT_EQ(1, message_center()->add_count());
EXPECT_EQ(0, message_center()->remove_count());
// Disconnecting a USB charger with the notification showing should close
// the notification.
EXPECT_TRUE(MaybeShowUsbChargerNotification(discharging));
EXPECT_EQ(1, message_center()->add_count());
EXPECT_EQ(1, message_center()->remove_count());
SetUsbChargerWasConnected(false);
// Notification shows when connecting a USB charger again.
EXPECT_TRUE(MaybeShowUsbChargerNotification(usb_connected));
EXPECT_EQ(2, message_center()->add_count());
EXPECT_EQ(1, message_center()->remove_count());
SetUsbChargerWasConnected(true);
// Notification hides when external power switches to AC.
PowerSupplyProperties ac_charger = DefaultPowerSupplyProperties();
ac_charger.set_external_power(
power_manager::PowerSupplyProperties_ExternalPower_AC);
EXPECT_TRUE(MaybeShowUsbChargerNotification(ac_charger));
EXPECT_EQ(2, message_center()->add_count());
EXPECT_EQ(2, message_center()->remove_count());
SetUsbChargerWasConnected(false);
// Notification shows when external power switches back to USB.
EXPECT_TRUE(MaybeShowUsbChargerNotification(usb_connected));
EXPECT_EQ(3, message_center()->add_count());
EXPECT_EQ(2, message_center()->remove_count());
SetUsbChargerWasConnected(true);
// Notification does not re-appear after being manually dismissed if
// power supply flickers between AC and USB charger.
message_center()->RemoveNotification(
PowerNotificationController::kUsbNotificationId, true);
EXPECT_EQ(3, message_center()->remove_count());
EXPECT_TRUE(MaybeShowUsbChargerNotification(ac_charger));
SetUsbChargerWasConnected(false);
EXPECT_FALSE(MaybeShowUsbChargerNotification(usb_connected));
EXPECT_EQ(3, message_center()->add_count());
SetUsbChargerWasConnected(true);
// Notification appears again after being manually dismissed if the charger
// is removed, and then a USB charger is attached.
MaybeShowUsbChargerNotification(discharging);
EXPECT_EQ(3, message_center()->add_count());
SetUsbChargerWasConnected(false);
MaybeShowUsbChargerNotification(usb_connected);
EXPECT_EQ(4, message_center()->add_count());
SetUsbChargerWasConnected(true);
}
TEST_F(PowerNotificationControllerTest,
AvoidUsbChargerNotificationWhenBatteryFull) {
PowerSupplyProperties full_proto;
full_proto.set_external_power(
power_manager::PowerSupplyProperties_ExternalPower_USB);
full_proto.set_battery_state(
power_manager::PowerSupplyProperties_BatteryState_FULL);
full_proto.set_battery_percent(100.0);
full_proto.set_is_calculating_battery_time(false);
PowerSupplyProperties not_full_proto;
not_full_proto.set_external_power(
power_manager::PowerSupplyProperties_ExternalPower_USB);
not_full_proto.set_battery_state(
power_manager::PowerSupplyProperties_BatteryState_CHARGING);
full_proto.set_battery_percent(90.0);
full_proto.set_is_calculating_battery_time(false);
// When the battery is reported as full, a notification shouldn't be displayed
// for a low-power charger: http://b/64913617
SetUsbChargerWasConnected(false);
SetBatteryWasFull(false);
EXPECT_FALSE(MaybeShowUsbChargerNotification(full_proto));
EXPECT_EQ(0, message_center()->add_count());
EXPECT_EQ(0, message_center()->remove_count());
// The notification should be displayed if the battery isn't full, though.
SetUsbChargerWasConnected(false);
SetBatteryWasFull(false);
EXPECT_TRUE(MaybeShowUsbChargerNotification(not_full_proto));
EXPECT_EQ(1, message_center()->add_count());
EXPECT_EQ(0, message_center()->remove_count());
// It should be dismissed if the battery becomes full again while the charger
// is still connected.
SetUsbChargerWasConnected(true);
SetBatteryWasFull(false);
EXPECT_TRUE(MaybeShowUsbChargerNotification(full_proto));
EXPECT_EQ(1, message_center()->add_count());
EXPECT_EQ(1, message_center()->remove_count());
}
TEST_F(PowerNotificationControllerTest, MaybeShowDualRoleNotification) {
PowerSupplyProperties discharging = DefaultPowerSupplyProperties();
discharging.set_supports_dual_role_devices(true);
MaybeShowDualRoleNotification(discharging);
EXPECT_EQ(0, message_center()->add_count());
EXPECT_EQ(0, message_center()->update_count());
EXPECT_EQ(0, message_center()->remove_count());
// Notification shows when connecting a dual-role device.
PowerSupplyProperties dual_role = DefaultPowerSupplyProperties();
dual_role.set_supports_dual_role_devices(true);
power_manager::PowerSupplyProperties_PowerSource* source =
dual_role.add_available_external_power_source();
source->set_id("dual-role1");
source->set_active_by_default(false);
MaybeShowDualRoleNotification(dual_role);
EXPECT_EQ(1, message_center()->add_count());
EXPECT_EQ(0, message_center()->update_count());
EXPECT_EQ(0, message_center()->remove_count());
// Connecting another dual-role device updates the notification to be plural.
source = dual_role.add_available_external_power_source();
source->set_id("dual-role2");
source->set_active_by_default(false);
MaybeShowDualRoleNotification(dual_role);
EXPECT_EQ(1, message_center()->add_count());
EXPECT_EQ(1, message_center()->update_count());
EXPECT_EQ(0, message_center()->remove_count());
// Connecting a 3rd dual-role device doesn't affect the notification.
source = dual_role.add_available_external_power_source();
source->set_id("dual-role3");
source->set_active_by_default(false);
MaybeShowDualRoleNotification(dual_role);
EXPECT_EQ(1, message_center()->add_count());
EXPECT_EQ(1, message_center()->update_count());
EXPECT_EQ(0, message_center()->remove_count());
// Connecting a legacy USB device removes the notification.
PowerSupplyProperties legacy(dual_role);
power_manager::PowerSupplyProperties_PowerSource* legacy_source =
legacy.add_available_external_power_source();
legacy_source->set_id("legacy");
legacy_source->set_active_by_default(true);
legacy.set_external_power_source_id("legacy");
legacy.set_external_power(
power_manager::PowerSupplyProperties_ExternalPower_USB);
MaybeShowDualRoleNotification(legacy);
EXPECT_EQ(1, message_center()->add_count());
EXPECT_EQ(1, message_center()->update_count());
EXPECT_EQ(1, message_center()->remove_count());
// Removing the legacy USB device adds the notification again.
MaybeShowDualRoleNotification(dual_role);
EXPECT_EQ(2, message_center()->add_count());
EXPECT_EQ(1, message_center()->update_count());
EXPECT_EQ(1, message_center()->remove_count());
// Charging from the device updates the notification.
dual_role.set_external_power_source_id("dual-role1");
dual_role.set_external_power(
power_manager::PowerSupplyProperties_ExternalPower_USB);
MaybeShowDualRoleNotification(dual_role);
EXPECT_EQ(2, message_center()->add_count());
EXPECT_EQ(2, message_center()->update_count());
EXPECT_EQ(1, message_center()->remove_count());
// Adding a device as a sink doesn't change the notification, because the
// notification exposes the source.
source = dual_role.add_available_external_power_source();
source->set_active_by_default(false);
MaybeShowDualRoleNotification(dual_role);
EXPECT_EQ(2, message_center()->add_count());
EXPECT_EQ(2, message_center()->update_count());
EXPECT_EQ(1, message_center()->remove_count());
// Changing the source to a sink changes the notification.
dual_role.set_external_power_source_id("");
dual_role.set_external_power(
power_manager::PowerSupplyProperties_ExternalPower_DISCONNECTED);
MaybeShowDualRoleNotification(dual_role);
EXPECT_EQ(2, message_center()->add_count());
EXPECT_EQ(3, message_center()->update_count());
EXPECT_EQ(1, message_center()->remove_count());
// An unrelated change has no effect.
dual_role.set_battery_time_to_empty_sec(2 * 60 * 60);
MaybeShowDualRoleNotification(dual_role);
EXPECT_EQ(2, message_center()->add_count());
EXPECT_EQ(3, message_center()->update_count());
EXPECT_EQ(1, message_center()->remove_count());
// Removing devices hides the notification.
MaybeShowDualRoleNotification(discharging);
EXPECT_EQ(2, message_center()->add_count());
EXPECT_EQ(3, message_center()->update_count());
EXPECT_EQ(2, message_center()->remove_count());
}
TEST_F(PowerNotificationControllerTest, UpdateNotificationState) {
// No notifications when no battery present.
PowerSupplyProperties no_battery = DefaultPowerSupplyProperties();
no_battery.set_external_power(
power_manager::PowerSupplyProperties_ExternalPower_AC);
no_battery.set_battery_state(
power_manager::PowerSupplyProperties_BatteryState_NOT_PRESENT);
{
SCOPED_TRACE("No notifications when no battery present");
UpdateNotificationState(no_battery,
PowerNotificationController::NOTIFICATION_NONE,
false, false);
}
// No notification when calculating remaining battery time.
PowerSupplyProperties calculating = DefaultPowerSupplyProperties();
calculating.set_is_calculating_battery_time(true);
{
SCOPED_TRACE("No notification when calculating remaining battery time");
UpdateNotificationState(calculating,
PowerNotificationController::NOTIFICATION_NONE,
false, false);
}
// No notification when charging.
PowerSupplyProperties charging = DefaultPowerSupplyProperties();
charging.set_external_power(
power_manager::PowerSupplyProperties_ExternalPower_AC);
charging.set_battery_state(
power_manager::PowerSupplyProperties_BatteryState_CHARGING);
{
SCOPED_TRACE("No notification when charging");
UpdateNotificationState(
charging, PowerNotificationController::NOTIFICATION_NONE, false, false);
}
// When the rounded minutes-to-empty are above the threshold, no notification
// should be shown.
PowerSupplyProperties low = DefaultPowerSupplyProperties();
low.set_battery_time_to_empty_sec(
PowerNotificationController::kLowPowerMinutes * 60 + 30);
{
SCOPED_TRACE("No notification when time to empty above threshold");
UpdateNotificationState(low, PowerNotificationController::NOTIFICATION_NONE,
false, false);
}
// When the rounded value matches the threshold, the notification should
// appear.
low.set_battery_time_to_empty_sec(
PowerNotificationController::kLowPowerMinutes * 60 + 29);
{
SCOPED_TRACE("Notification when time to empty matches threshold");
UpdateNotificationState(
low, PowerNotificationController::NOTIFICATION_LOW_POWER, true, false);
}
// It should persist at lower values.
low.set_battery_time_to_empty_sec(
PowerNotificationController::kLowPowerMinutes * 60 - 20);
{
SCOPED_TRACE("Notification persists at lower values");
UpdateNotificationState(
low, PowerNotificationController::NOTIFICATION_LOW_POWER, false, false);
}
// The critical low battery notification should be shown when the rounded
// value is at the lower threshold.
PowerSupplyProperties critical = DefaultPowerSupplyProperties();
critical.set_battery_time_to_empty_sec(
PowerNotificationController::kCriticalMinutes * 60 + 29);
{
SCOPED_TRACE("Critical notification when time to empty is critical");
UpdateNotificationState(critical,
PowerNotificationController::NOTIFICATION_CRITICAL,
true, true);
}
// The notification should be dismissed when the no-warning threshold is
// reached.
PowerSupplyProperties safe = DefaultPowerSupplyProperties();
safe.set_battery_time_to_empty_sec(
PowerNotificationController::kNoWarningMinutes * 60 - 29);
{
SCOPED_TRACE("Notification removed when battery not low");
UpdateNotificationState(
safe, PowerNotificationController::NOTIFICATION_NONE, false, true);
}
// Test that rounded percentages are used when a USB charger is connected.
PowerSupplyProperties low_usb = DefaultPowerSupplyProperties();
low_usb.set_external_power(
power_manager::PowerSupplyProperties_ExternalPower_USB);
low_usb.set_battery_percent(PowerNotificationController::kLowPowerPercentage +
0.5);
{
SCOPED_TRACE("No notification for rounded battery percent");
UpdateNotificationState(
low_usb, PowerNotificationController::NOTIFICATION_NONE, true, false);
}
low_usb.set_battery_percent(PowerNotificationController::kLowPowerPercentage +
0.49);
{
SCOPED_TRACE("Notification for rounded low power percent");
UpdateNotificationState(low_usb,
PowerNotificationController::NOTIFICATION_LOW_POWER,
true, false);
}
PowerSupplyProperties critical_usb = DefaultPowerSupplyProperties();
critical_usb.set_external_power(
power_manager::PowerSupplyProperties_ExternalPower_USB);
critical_usb.set_battery_percent(
PowerNotificationController::kCriticalPercentage + 0.2);
{
SCOPED_TRACE("Notification for rounded critical power percent");
UpdateNotificationState(critical_usb,
PowerNotificationController::NOTIFICATION_CRITICAL,
true, true);
}
PowerSupplyProperties safe_usb = DefaultPowerSupplyProperties();
safe_usb.set_external_power(
power_manager::PowerSupplyProperties_ExternalPower_USB);
safe_usb.set_battery_percent(
PowerNotificationController::kNoWarningPercentage - 0.1);
{
SCOPED_TRACE("Notification removed for rounded percent above threshold");
UpdateNotificationState(
safe_usb, PowerNotificationController::NOTIFICATION_NONE, false, true);
}
}
// Test that a notification isn't shown if powerd sends a -1 time-to-empty value
// to indicate that it couldn't produce an estimate: https://crbug.com/930358
TEST_F(PowerNotificationControllerTest, IgnoreMissingBatteryEstimates) {
PowerSupplyProperties proto = DefaultPowerSupplyProperties();
proto.set_battery_time_to_empty_sec(-1);
UpdateNotificationState(proto, PowerNotificationController::NOTIFICATION_NONE,
false, false);
}
} // namespace ash