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
ash / system / power / peripheral_battery_notifier_unittest.cc [blame]
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/system/power/peripheral_battery_notifier.h"
#include <memory>
#include <string>
#include "ash/constants/ash_features.h"
#include "ash/public/cpp/test/test_system_tray_client.h"
#include "ash/shell.h"
#include "ash/system/power/peripheral_battery_listener.h"
#include "ash/system/power/peripheral_battery_tests.h"
#include "ash/test/ash_test_base.h"
#include "base/memory/raw_ptr.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/scoped_feature_list.h"
#include "ui/events/devices/device_data_manager.h"
#include "ui/events/devices/device_data_manager_test_api.h"
#include "ui/events/devices/touchscreen_device.h"
#include "ui/message_center/message_center.h"
#include "ui/message_center/public/cpp/notification.h"
using BI = ash::PeripheralBatteryListener::BatteryInfo;
namespace {
const std::u16string& NotificationMessagePrefix() {
static const std::u16string prefix(u"Battery low (");
return prefix;
}
const std::u16string& NotificationMessageSuffix() {
static const std::u16string suffix(u"%)");
return suffix;
}
} // namespace
namespace ash {
class PeripheralBatteryNotifierTest : public AshTestBase {
public:
PeripheralBatteryNotifierTest()
: AshTestBase(base::test::TaskEnvironment::TimeSource::MOCK_TIME) {}
PeripheralBatteryNotifierTest(const PeripheralBatteryNotifierTest&) = delete;
PeripheralBatteryNotifierTest& operator=(
const PeripheralBatteryNotifierTest&) = delete;
~PeripheralBatteryNotifierTest() override = default;
void SetUp() override {
chromeos::PowerManagerClient::InitializeFake();
AshTestBase::SetUp();
ASSERT_TRUE(ui::DeviceDataManager::HasInstance());
// Simulate the complete listing of input devices, required by the listener.
ui::DeviceDataManagerTestApi().OnDeviceListsComplete();
message_center_ = message_center::MessageCenter::Get();
system_tray_client_ = GetSystemTrayClient();
battery_listener_ = std::make_unique<PeripheralBatteryListener>();
battery_notifier_ =
std::make_unique<PeripheralBatteryNotifier>(battery_listener_.get());
// No notifications should have been posted yet.
ASSERT_EQ(0u, message_center_->NotificationCount());
}
void TearDown() override {
battery_notifier_.reset();
battery_listener_.reset();
AshTestBase::TearDown();
chromeos::PowerManagerClient::Shutdown();
}
// Extracts the battery percentage from the message of a notification.
uint8_t ExtractBatteryPercentage(message_center::Notification* notification) {
const std::u16string& message = notification->message();
EXPECT_TRUE(base::StartsWith(message, NotificationMessagePrefix(),
base::CompareCase::SENSITIVE));
EXPECT_TRUE(base::EndsWith(message, NotificationMessageSuffix(),
base::CompareCase::SENSITIVE));
int prefix_size = NotificationMessagePrefix().size();
int suffix_size = NotificationMessageSuffix().size();
int key_len = message.size() - prefix_size - suffix_size;
EXPECT_GT(key_len, 0);
int battery_percentage;
EXPECT_TRUE(base::StringToInt(message.substr(prefix_size, key_len),
&battery_percentage));
EXPECT_GE(battery_percentage, 0);
EXPECT_LE(battery_percentage, 100);
return battery_percentage;
}
base::TimeTicks GetTestingClock() { return base::TimeTicks::Now(); }
void ClockAdvance(base::TimeDelta delta) {
task_environment()->AdvanceClock(delta);
}
void UpdateBatteryLevel(bool add_first,
const std::string key,
const std::u16string name,
std::optional<uint8_t> level,
bool battery_report_eligible,
BI::PeripheralType type,
const std::string btaddr) {
BI info(key, name, level, battery_report_eligible, GetTestingClock(), type,
BI::ChargeStatus::kUnknown, btaddr);
if (add_first)
battery_notifier_->OnAddingBattery(info);
battery_notifier_->OnUpdatedBatteryLevel(info);
}
void RemoveBattery(const std::string key,
const std::u16string name,
std::optional<uint8_t> level,
bool battery_report_eligible,
BI::PeripheralType type,
const std::string btaddr) {
BI info(key, name, level, battery_report_eligible, GetTestingClock(), type,
BI::ChargeStatus::kUnknown, btaddr);
battery_notifier_->OnRemovingBattery(info);
}
protected:
raw_ptr<message_center::MessageCenter, DanglingUntriaged> message_center_;
raw_ptr<TestSystemTrayClient, DanglingUntriaged> system_tray_client_;
std::unique_ptr<PeripheralBatteryNotifier> battery_notifier_;
std::unique_ptr<PeripheralBatteryListener> battery_listener_;
void set_complete_devices(bool complete_devices) {
complete_devices_ = complete_devices;
}
// SetUp() doesn't complete devices if this is set to false.
bool complete_devices_ = true;
};
TEST_F(PeripheralBatteryNotifierTest, Basic) {
// Level 50 at time 100, no low-battery notification.
ClockAdvance(base::Seconds(100));
UpdateBatteryLevel(true, kTestBatteryId, kTestDeviceName16, 50,
/*battery_report_eligible=*/true,
BI::PeripheralType::kOther, kTestBatteryAddress);
EXPECT_EQ(1u,
battery_notifier_->battery_notifications_.count(kTestBatteryId));
const PeripheralBatteryNotifier::NotificationInfo& info =
battery_notifier_->battery_notifications_[kTestBatteryId];
EXPECT_EQ(std::nullopt, info.level);
EXPECT_EQ(GetTestingClock(), info.last_notification_timestamp);
EXPECT_FALSE(
message_center_->FindVisibleNotificationById(kTestBatteryNotificationId));
// Level 5 at time 110, low-battery notification.
ClockAdvance(base::Seconds(10));
UpdateBatteryLevel(false, kTestBatteryId, kTestDeviceName16, 5,
/*battery_report_eligible=*/true,
BI::PeripheralType::kOther, kTestBatteryAddress);
EXPECT_EQ(5, info.level);
EXPECT_EQ(GetTestingClock(), info.last_notification_timestamp);
EXPECT_TRUE(
message_center_->FindVisibleNotificationById(kTestBatteryNotificationId));
// Verify that the low-battery notification for stylus does not show up.
EXPECT_FALSE(message_center_->FindVisibleNotificationById(
PeripheralBatteryNotifier::kStylusNotificationId));
// Level -1 at time 115, cancel previous notification.
ClockAdvance(base::Seconds(5));
UpdateBatteryLevel(false, kTestBatteryId, kTestDeviceName16, std::nullopt,
/*battery_report_eligible=*/true,
BI::PeripheralType::kOther, kTestBatteryAddress);
EXPECT_EQ(std::nullopt, info.level);
EXPECT_EQ(GetTestingClock() - base::Seconds(5),
info.last_notification_timestamp);
EXPECT_FALSE(
message_center_->FindVisibleNotificationById(kTestBatteryNotificationId));
// Level 50 at time 120, no low-battery notification.
ClockAdvance(base::Seconds(5));
UpdateBatteryLevel(false, kTestBatteryId, kTestDeviceName16, 50,
/*battery_report_eligible=*/true,
BI::PeripheralType::kOther, kTestBatteryAddress);
EXPECT_EQ(std::nullopt, info.level);
EXPECT_EQ(GetTestingClock() - base::Seconds(10),
info.last_notification_timestamp);
EXPECT_FALSE(
message_center_->FindVisibleNotificationById(kTestBatteryNotificationId));
// Level 5 at time 130, no low-battery notification (throttling).
ClockAdvance(base::Seconds(10));
UpdateBatteryLevel(false, kTestBatteryId, kTestDeviceName16, 5,
/*battery_report_eligible=*/true,
BI::PeripheralType::kOther, kTestBatteryAddress);
EXPECT_EQ(5, info.level);
EXPECT_EQ(GetTestingClock() - base::Seconds(20),
info.last_notification_timestamp);
EXPECT_FALSE(
message_center_->FindVisibleNotificationById(kTestBatteryNotificationId));
}
TEST_F(PeripheralBatteryNotifierTest, EarlyNotification) {
// Level 15 at time 10, low-battery notification.
ClockAdvance(base::Seconds(10));
UpdateBatteryLevel(true, kTestBatteryId, kTestDeviceName16, 15,
/*battery_report_eligible=*/true,
BI::PeripheralType::kOther, kTestBatteryAddress);
EXPECT_EQ(1u,
battery_notifier_->battery_notifications_.count(kTestBatteryId));
const PeripheralBatteryNotifier::NotificationInfo& info =
battery_notifier_->battery_notifications_[kTestBatteryId];
EXPECT_EQ(15, info.level);
EXPECT_EQ(GetTestingClock(), info.last_notification_timestamp);
EXPECT_TRUE(
message_center_->FindVisibleNotificationById(kTestBatteryNotificationId));
}
TEST_F(PeripheralBatteryNotifierTest, StylusNotification) {
const std::string kTestStylusBatteryPath =
"/sys/class/power_supply/hid-AAAA:BBBB:CCCC.DDDD-battery";
const std::string kTestStylusBatteryId =
"???hxxxxid-AAAA:BBBB:CCCC.DDDD-battery";
const std::string kTestStylusName = "test_stylus";
const std::u16string kTestStylusName16 = u"test_stylus";
// Add an external stylus to our test device manager.
ui::TouchscreenDevice stylus(
/*id=*/0, ui::INPUT_DEVICE_USB, kTestStylusName, gfx::Size(),
/*touch_points=*/1,
/*has_stylus=*/true);
stylus.sys_path = base::FilePath(kTestStylusBatteryPath);
ui::DeviceDataManagerTestApi().SetTouchscreenDevices({stylus});
// Verify that when the battery level is 50, no stylus low battery
// notification is shown.
UpdateBatteryLevel(true, kTestStylusBatteryId, kTestStylusName16, 50,
/*battery_report_eligible=*/true,
BI::PeripheralType::kStylusViaScreen, "");
EXPECT_FALSE(message_center_->FindVisibleNotificationById(
PeripheralBatteryNotifier::kStylusNotificationId));
// Verify that when the battery level is 5, a stylus low battery notification
// is shown. Also check that a non stylus device low battery notification will
// not show up.
UpdateBatteryLevel(false, kTestStylusBatteryId, kTestStylusName16, 5,
/*battery_report_eligible=*/true,
BI::PeripheralType::kStylusViaScreen, "");
EXPECT_TRUE(message_center_->FindVisibleNotificationById(
PeripheralBatteryNotifier::kStylusNotificationId));
EXPECT_FALSE(
message_center_->FindVisibleNotificationById(kTestBatteryAddress));
// Verify that when the battery level is -1, the previous stylus low battery
// notification is cancelled.
UpdateBatteryLevel(false, kTestStylusBatteryId, kTestStylusName16,
std::nullopt, /*battery_report_eligible=*/true,
BI::PeripheralType::kStylusViaScreen, "");
EXPECT_FALSE(message_center_->FindVisibleNotificationById(
PeripheralBatteryNotifier::kStylusNotificationId));
}
TEST_F(PeripheralBatteryNotifierTest,
Bluetooth_CreatesANotificationForEachDevice) {
UpdateBatteryLevel(true, kBluetoothDeviceId1, kBluetoothDeviceName116, 5,
/*battery_report_eligible=*/true,
BI::PeripheralType::kOther, kBluetoothDeviceAddress1);
UpdateBatteryLevel(true, kBluetoothDeviceId2, kBluetoothDeviceName216, 0,
/*battery_report_eligible=*/true,
BI::PeripheralType::kOther, kBluetoothDeviceAddress2);
// Verify 2 notifications were posted with the correct values.
EXPECT_EQ(2u, message_center_->NotificationCount());
message_center::Notification* notification_1 =
message_center_->FindVisibleNotificationById(
kBluetoothDeviceNotificationId1);
message_center::Notification* notification_2 =
message_center_->FindVisibleNotificationById(
kBluetoothDeviceNotificationId2);
EXPECT_TRUE(notification_1);
EXPECT_EQ(kBluetoothDeviceName116, notification_1->title());
EXPECT_EQ(5, ExtractBatteryPercentage(notification_1));
EXPECT_TRUE(notification_2);
EXPECT_EQ(kBluetoothDeviceName216, notification_2->title());
EXPECT_EQ(0, ExtractBatteryPercentage(notification_2));
}
TEST_F(PeripheralBatteryNotifierTest,
Bluetooth_RemovesNotificationForDisconnectedDevices) {
UpdateBatteryLevel(true, kBluetoothDeviceId1, kBluetoothDeviceName116, 5,
/*battery_report_eligible=*/true,
BI::PeripheralType::kOther, kBluetoothDeviceAddress1);
UpdateBatteryLevel(true, kBluetoothDeviceId2, kBluetoothDeviceName216, 0,
/*battery_report_eligible=*/true,
BI::PeripheralType::kOther, kBluetoothDeviceAddress2);
// Verify 2 notifications were posted.
EXPECT_EQ(2u, message_center_->NotificationCount());
// Verify only the notification for device 1 gets removed.
RemoveBattery(kBluetoothDeviceId1, kBluetoothDeviceName116, 5,
/*battery_report_eligible=*/true, BI::PeripheralType::kOther,
kBluetoothDeviceAddress1);
EXPECT_EQ(1u, message_center_->NotificationCount());
EXPECT_TRUE(message_center_->FindVisibleNotificationById(
kBluetoothDeviceNotificationId2));
// Remove the second notification.
RemoveBattery(kBluetoothDeviceId2, kBluetoothDeviceName216, 0,
/*battery_report_eligible=*/true, BI::PeripheralType::kOther,
kBluetoothDeviceAddress2);
EXPECT_EQ(0u, message_center_->NotificationCount());
}
TEST_F(PeripheralBatteryNotifierTest,
Bluetooth_CancelNotificationForInvalidBatteryLevel) {
UpdateBatteryLevel(true, kBluetoothDeviceId1, kBluetoothDeviceName116, 1,
/*battery_report_eligible=*/true,
BI::PeripheralType::kOther, kBluetoothDeviceAddress1);
EXPECT_TRUE(message_center_->FindVisibleNotificationById(
kBluetoothDeviceNotificationId1));
// The notification should get canceled.
UpdateBatteryLevel(true, kBluetoothDeviceId1, kBluetoothDeviceName116,
std::nullopt, /*battery_report_eligible=*/true,
BI::PeripheralType::kOther, kBluetoothDeviceAddress1);
EXPECT_FALSE(message_center_->FindVisibleNotificationById(
kBluetoothDeviceNotificationId1));
}
// Don't post a notification if the battery level drops again under the
// threshold before kNotificationInterval is completed.
TEST_F(PeripheralBatteryNotifierTest,
DontShowSecondNotificationWithinASmallTimeInterval) {
ClockAdvance(base::Seconds(100));
// Post a notification.
UpdateBatteryLevel(true, kBluetoothDeviceId1, kBluetoothDeviceName116, 1,
/*battery_report_eligible=*/true,
BI::PeripheralType::kOther, kBluetoothDeviceAddress1);
EXPECT_TRUE(message_center_->FindVisibleNotificationById(
kBluetoothDeviceNotificationId1));
// Cancel the notification.
ClockAdvance(base::Seconds(1));
UpdateBatteryLevel(false, kBluetoothDeviceId1, kBluetoothDeviceName116,
std::nullopt, /*battery_report_eligible=*/true,
BI::PeripheralType::kOther, kBluetoothDeviceAddress1);
EXPECT_FALSE(message_center_->FindVisibleNotificationById(
kBluetoothDeviceNotificationId1));
// The battery level falls below the threshold after a short time period. No
// notification should get posted.
ClockAdvance(base::Seconds(1));
UpdateBatteryLevel(true, kBluetoothDeviceId1, kBluetoothDeviceName116, 1,
/*battery_report_eligible=*/true,
BI::PeripheralType::kOther, kBluetoothDeviceAddress1);
EXPECT_FALSE(message_center_->FindVisibleNotificationById(
kBluetoothDeviceNotificationId1));
}
// Post a notification if the battery is under threshold, then unknown level and
// then is again under the threshold after kNotificationInterval is completed.
TEST_F(PeripheralBatteryNotifierTest,
PostNotificationIfBatteryGoesFromUnknownLevelToBelowThreshold) {
ClockAdvance(base::Seconds(100));
// Post a notification.
UpdateBatteryLevel(true, kBluetoothDeviceId1, kBluetoothDeviceName116, 1,
/*battery_report_eligible=*/true,
BI::PeripheralType::kOther, kBluetoothDeviceAddress1);
EXPECT_TRUE(message_center_->FindVisibleNotificationById(
kBluetoothDeviceNotificationId1));
// Cancel the notification.
ClockAdvance(base::Seconds(1));
UpdateBatteryLevel(true, kBluetoothDeviceId1, kBluetoothDeviceName116,
std::nullopt, /*battery_report_eligible=*/true,
BI::PeripheralType::kOther, kBluetoothDeviceAddress1);
EXPECT_FALSE(message_center_->FindVisibleNotificationById(
kBluetoothDeviceNotificationId1));
// Post notification if we are out of the kNotificationInterval.
ClockAdvance(base::Seconds(100));
UpdateBatteryLevel(true, kBluetoothDeviceId1, kBluetoothDeviceName116, 1,
/*battery_report_eligible=*/true,
BI::PeripheralType::kOther, kBluetoothDeviceAddress1);
EXPECT_TRUE(message_center_->FindVisibleNotificationById(
kBluetoothDeviceNotificationId1));
}
// Don't Post another notification if the battery level keeps low and the user
// dismissed the previous notification.
TEST_F(PeripheralBatteryNotifierTest,
DontRepostNotificationIfUserDismissedPreviousOne) {
ClockAdvance(base::Seconds(100));
UpdateBatteryLevel(true, kBluetoothDeviceId1, kBluetoothDeviceName116, 5,
/*battery_report_eligible=*/true,
BI::PeripheralType::kOther, kBluetoothDeviceAddress1);
EXPECT_EQ(1u, message_center_->NotificationCount());
// Simulate the user clears the notification.
message_center_->RemoveAllNotifications(
/*by_user=*/true, message_center::MessageCenter::RemoveType::ALL);
// The battery level remains low, but shouldn't post a notification.
ClockAdvance(base::Seconds(100));
UpdateBatteryLevel(true, kBluetoothDeviceId1, kBluetoothDeviceName116, 5,
/*battery_report_eligible=*/true,
BI::PeripheralType::kOther, kBluetoothDeviceAddress1);
EXPECT_EQ(0u, message_center_->NotificationCount());
}
// If there is an existing notification and the battery level remains low,
// update its content.
TEST_F(PeripheralBatteryNotifierTest, UpdateNotificationIfVisible) {
ClockAdvance(base::Seconds(100));
UpdateBatteryLevel(true, kBluetoothDeviceId1, kBluetoothDeviceName116, 5,
/*battery_report_eligible=*/true,
BI::PeripheralType::kOther, kBluetoothDeviceAddress1);
EXPECT_EQ(1u, message_center_->NotificationCount());
// The battery level remains low, should update the notification.
ClockAdvance(base::Seconds(100));
UpdateBatteryLevel(true, kBluetoothDeviceId1, kBluetoothDeviceName116, 3,
/*battery_report_eligible=*/true,
BI::PeripheralType::kOther, kBluetoothDeviceAddress1);
message_center::Notification* notification =
message_center_->FindVisibleNotificationById(
kBluetoothDeviceNotificationId1);
EXPECT_TRUE(notification);
EXPECT_EQ(kBluetoothDeviceName116, notification->title());
EXPECT_EQ(3, ExtractBatteryPercentage(notification));
}
TEST_F(PeripheralBatteryNotifierTest, OpenBluetoothSettingsUi) {
ClockAdvance(base::Seconds(100));
UpdateBatteryLevel(true, kBluetoothDeviceId1, kBluetoothDeviceName116, 5,
/*battery_report_eligible=*/true,
BI::PeripheralType::kOther, kBluetoothDeviceAddress1);
EXPECT_EQ(1u, message_center_->NotificationCount());
message_center::Notification* notification =
message_center_->FindVisibleNotificationById(
kBluetoothDeviceNotificationId1);
EXPECT_TRUE(notification);
message_center_->ClickOnNotification(notification->id());
EXPECT_EQ(1, system_tray_client_->show_bluetooth_settings_count());
EXPECT_FALSE(message_center_->FindVisibleNotificationById(
kBluetoothDeviceNotificationId1));
}
} // namespace ash