peripheral_battery_notifier_unittest.cc 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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
  3. // found in the LICENSE file.
  4. #include "ash/system/power/peripheral_battery_notifier.h"
  5. #include <memory>
  6. #include <string>
  7. #include "ash/constants/ash_features.h"
  8. #include "ash/public/cpp/test/test_system_tray_client.h"
  9. #include "ash/shell.h"
  10. #include "ash/system/power/peripheral_battery_listener.h"
  11. #include "ash/system/power/peripheral_battery_tests.h"
  12. #include "ash/test/ash_test_base.h"
  13. #include "base/strings/string_number_conversions.h"
  14. #include "base/strings/string_piece.h"
  15. #include "base/strings/string_util.h"
  16. #include "base/strings/utf_string_conversions.h"
  17. #include "base/test/scoped_feature_list.h"
  18. #include "ui/events/devices/device_data_manager.h"
  19. #include "ui/events/devices/device_data_manager_test_api.h"
  20. #include "ui/events/devices/touchscreen_device.h"
  21. #include "ui/message_center/message_center.h"
  22. #include "ui/message_center/public/cpp/notification.h"
  23. using BI = ash::PeripheralBatteryListener::BatteryInfo;
  24. namespace {
  25. const std::u16string& NotificationMessagePrefix() {
  26. static const std::u16string prefix(u"Battery low (");
  27. return prefix;
  28. }
  29. const std::u16string& NotificationMessageSuffix() {
  30. static const std::u16string suffix(u"%)");
  31. return suffix;
  32. }
  33. } // namespace
  34. namespace ash {
  35. class PeripheralBatteryNotifierTest : public AshTestBase {
  36. public:
  37. PeripheralBatteryNotifierTest()
  38. : AshTestBase(base::test::TaskEnvironment::TimeSource::MOCK_TIME) {}
  39. PeripheralBatteryNotifierTest(const PeripheralBatteryNotifierTest&) = delete;
  40. PeripheralBatteryNotifierTest& operator=(
  41. const PeripheralBatteryNotifierTest&) = delete;
  42. ~PeripheralBatteryNotifierTest() override = default;
  43. void SetUp() override {
  44. chromeos::PowerManagerClient::InitializeFake();
  45. AshTestBase::SetUp();
  46. ASSERT_TRUE(ui::DeviceDataManager::HasInstance());
  47. // Simulate the complete listing of input devices, required by the listener.
  48. ui::DeviceDataManagerTestApi().OnDeviceListsComplete();
  49. message_center_ = message_center::MessageCenter::Get();
  50. system_tray_client_ = GetSystemTrayClient();
  51. battery_listener_ = std::make_unique<PeripheralBatteryListener>();
  52. battery_notifier_ =
  53. std::make_unique<PeripheralBatteryNotifier>(battery_listener_.get());
  54. // No notifications should have been posted yet.
  55. ASSERT_EQ(0u, message_center_->NotificationCount());
  56. }
  57. void TearDown() override {
  58. battery_notifier_.reset();
  59. battery_listener_.reset();
  60. AshTestBase::TearDown();
  61. chromeos::PowerManagerClient::Shutdown();
  62. }
  63. // Extracts the battery percentage from the message of a notification.
  64. uint8_t ExtractBatteryPercentage(message_center::Notification* notification) {
  65. const std::u16string& message = notification->message();
  66. EXPECT_TRUE(base::StartsWith(message, NotificationMessagePrefix(),
  67. base::CompareCase::SENSITIVE));
  68. EXPECT_TRUE(base::EndsWith(message, NotificationMessageSuffix(),
  69. base::CompareCase::SENSITIVE));
  70. int prefix_size = NotificationMessagePrefix().size();
  71. int suffix_size = NotificationMessageSuffix().size();
  72. int key_len = message.size() - prefix_size - suffix_size;
  73. EXPECT_GT(key_len, 0);
  74. int battery_percentage;
  75. EXPECT_TRUE(base::StringToInt(message.substr(prefix_size, key_len),
  76. &battery_percentage));
  77. EXPECT_GE(battery_percentage, 0);
  78. EXPECT_LE(battery_percentage, 100);
  79. return battery_percentage;
  80. }
  81. base::TimeTicks GetTestingClock() { return base::TimeTicks::Now(); }
  82. void ClockAdvance(base::TimeDelta delta) {
  83. task_environment()->AdvanceClock(delta);
  84. }
  85. void UpdateBatteryLevel(bool add_first,
  86. const std::string key,
  87. const std::u16string name,
  88. absl::optional<uint8_t> level,
  89. bool battery_report_eligible,
  90. BI::PeripheralType type,
  91. const std::string btaddr) {
  92. BI info(key, name, level, battery_report_eligible, GetTestingClock(), type,
  93. BI::ChargeStatus::kUnknown, btaddr);
  94. if (add_first)
  95. battery_notifier_->OnAddingBattery(info);
  96. battery_notifier_->OnUpdatedBatteryLevel(info);
  97. }
  98. void RemoveBattery(const std::string key,
  99. const std::u16string name,
  100. absl::optional<uint8_t> level,
  101. bool battery_report_eligible,
  102. BI::PeripheralType type,
  103. const std::string btaddr) {
  104. BI info(key, name, level, battery_report_eligible, GetTestingClock(), type,
  105. BI::ChargeStatus::kUnknown, btaddr);
  106. battery_notifier_->OnRemovingBattery(info);
  107. }
  108. protected:
  109. message_center::MessageCenter* message_center_;
  110. TestSystemTrayClient* system_tray_client_;
  111. std::unique_ptr<PeripheralBatteryNotifier> battery_notifier_;
  112. std::unique_ptr<PeripheralBatteryListener> battery_listener_;
  113. void set_complete_devices(bool complete_devices) {
  114. complete_devices_ = complete_devices;
  115. }
  116. // SetUp() doesn't complete devices if this is set to false.
  117. bool complete_devices_ = true;
  118. };
  119. TEST_F(PeripheralBatteryNotifierTest, Basic) {
  120. // Level 50 at time 100, no low-battery notification.
  121. ClockAdvance(base::Seconds(100));
  122. UpdateBatteryLevel(true, kTestBatteryId, kTestDeviceName16, 50,
  123. /*battery_report_eligible=*/true,
  124. BI::PeripheralType::kOther, kTestBatteryAddress);
  125. EXPECT_EQ(1u,
  126. battery_notifier_->battery_notifications_.count(kTestBatteryId));
  127. const PeripheralBatteryNotifier::NotificationInfo& info =
  128. battery_notifier_->battery_notifications_[kTestBatteryId];
  129. EXPECT_EQ(absl::nullopt, info.level);
  130. EXPECT_EQ(GetTestingClock(), info.last_notification_timestamp);
  131. EXPECT_FALSE(
  132. message_center_->FindVisibleNotificationById(kTestBatteryNotificationId));
  133. // Level 5 at time 110, low-battery notification.
  134. ClockAdvance(base::Seconds(10));
  135. UpdateBatteryLevel(false, kTestBatteryId, kTestDeviceName16, 5,
  136. /*battery_report_eligible=*/true,
  137. BI::PeripheralType::kOther, kTestBatteryAddress);
  138. EXPECT_EQ(5, info.level);
  139. EXPECT_EQ(GetTestingClock(), info.last_notification_timestamp);
  140. EXPECT_TRUE(
  141. message_center_->FindVisibleNotificationById(kTestBatteryNotificationId));
  142. // Verify that the low-battery notification for stylus does not show up.
  143. EXPECT_FALSE(message_center_->FindVisibleNotificationById(
  144. PeripheralBatteryNotifier::kStylusNotificationId));
  145. // Level -1 at time 115, cancel previous notification.
  146. ClockAdvance(base::Seconds(5));
  147. UpdateBatteryLevel(false, kTestBatteryId, kTestDeviceName16, absl::nullopt,
  148. /*battery_report_eligible=*/true,
  149. BI::PeripheralType::kOther, kTestBatteryAddress);
  150. EXPECT_EQ(absl::nullopt, info.level);
  151. EXPECT_EQ(GetTestingClock() - base::Seconds(5),
  152. info.last_notification_timestamp);
  153. EXPECT_FALSE(
  154. message_center_->FindVisibleNotificationById(kTestBatteryNotificationId));
  155. // Level 50 at time 120, no low-battery notification.
  156. ClockAdvance(base::Seconds(5));
  157. UpdateBatteryLevel(false, kTestBatteryId, kTestDeviceName16, 50,
  158. /*battery_report_eligible=*/true,
  159. BI::PeripheralType::kOther, kTestBatteryAddress);
  160. EXPECT_EQ(absl::nullopt, info.level);
  161. EXPECT_EQ(GetTestingClock() - base::Seconds(10),
  162. info.last_notification_timestamp);
  163. EXPECT_FALSE(
  164. message_center_->FindVisibleNotificationById(kTestBatteryNotificationId));
  165. // Level 5 at time 130, no low-battery notification (throttling).
  166. ClockAdvance(base::Seconds(10));
  167. UpdateBatteryLevel(false, kTestBatteryId, kTestDeviceName16, 5,
  168. /*battery_report_eligible=*/true,
  169. BI::PeripheralType::kOther, kTestBatteryAddress);
  170. EXPECT_EQ(5, info.level);
  171. EXPECT_EQ(GetTestingClock() - base::Seconds(20),
  172. info.last_notification_timestamp);
  173. EXPECT_FALSE(
  174. message_center_->FindVisibleNotificationById(kTestBatteryNotificationId));
  175. }
  176. TEST_F(PeripheralBatteryNotifierTest, EarlyNotification) {
  177. // Level 15 at time 10, low-battery notification.
  178. ClockAdvance(base::Seconds(10));
  179. UpdateBatteryLevel(true, kTestBatteryId, kTestDeviceName16, 15,
  180. /*battery_report_eligible=*/true,
  181. BI::PeripheralType::kOther, kTestBatteryAddress);
  182. EXPECT_EQ(1u,
  183. battery_notifier_->battery_notifications_.count(kTestBatteryId));
  184. const PeripheralBatteryNotifier::NotificationInfo& info =
  185. battery_notifier_->battery_notifications_[kTestBatteryId];
  186. EXPECT_EQ(15, info.level);
  187. EXPECT_EQ(GetTestingClock(), info.last_notification_timestamp);
  188. EXPECT_TRUE(
  189. message_center_->FindVisibleNotificationById(kTestBatteryNotificationId));
  190. }
  191. TEST_F(PeripheralBatteryNotifierTest, StylusNotification) {
  192. const std::string kTestStylusBatteryPath =
  193. "/sys/class/power_supply/hid-AAAA:BBBB:CCCC.DDDD-battery";
  194. const std::string kTestStylusBatteryId =
  195. "???hxxxxid-AAAA:BBBB:CCCC.DDDD-battery";
  196. const std::string kTestStylusName = "test_stylus";
  197. const std::u16string kTestStylusName16 = u"test_stylus";
  198. base::test::ScopedFeatureList flags;
  199. flags.InitAndEnableFeature(features::kStylusBatteryStatus);
  200. // Add an external stylus to our test device manager.
  201. ui::TouchscreenDevice stylus(
  202. /*id=*/0, ui::INPUT_DEVICE_USB, kTestStylusName, gfx::Size(),
  203. /*touch_points=*/1,
  204. /*has_stylus=*/true);
  205. stylus.sys_path = base::FilePath(kTestStylusBatteryPath);
  206. ui::DeviceDataManagerTestApi().SetTouchscreenDevices({stylus});
  207. // Verify that when the battery level is 50, no stylus low battery
  208. // notification is shown.
  209. UpdateBatteryLevel(true, kTestStylusBatteryId, kTestStylusName16, 50,
  210. /*battery_report_eligible=*/true,
  211. BI::PeripheralType::kStylusViaScreen, "");
  212. EXPECT_FALSE(message_center_->FindVisibleNotificationById(
  213. PeripheralBatteryNotifier::kStylusNotificationId));
  214. // Verify that when the battery level is 5, a stylus low battery notification
  215. // is shown. Also check that a non stylus device low battery notification will
  216. // not show up.
  217. UpdateBatteryLevel(false, kTestStylusBatteryId, kTestStylusName16, 5,
  218. /*battery_report_eligible=*/true,
  219. BI::PeripheralType::kStylusViaScreen, "");
  220. EXPECT_TRUE(message_center_->FindVisibleNotificationById(
  221. PeripheralBatteryNotifier::kStylusNotificationId));
  222. EXPECT_FALSE(
  223. message_center_->FindVisibleNotificationById(kTestBatteryAddress));
  224. // Verify that when the battery level is -1, the previous stylus low battery
  225. // notification is cancelled.
  226. UpdateBatteryLevel(false, kTestStylusBatteryId, kTestStylusName16,
  227. absl::nullopt, /*battery_report_eligible=*/true,
  228. BI::PeripheralType::kStylusViaScreen, "");
  229. EXPECT_FALSE(message_center_->FindVisibleNotificationById(
  230. PeripheralBatteryNotifier::kStylusNotificationId));
  231. }
  232. TEST_F(PeripheralBatteryNotifierTest, StylusNotificationDisabled) {
  233. const std::string kTestStylusBatteryPath =
  234. "/sys/class/power_supply/hid-AAAA:BBBB:CCCC.DDDD-battery";
  235. const std::string kTestStylusBatteryId =
  236. "???hxxxxid-AAAA:BBBB:CCCC.DDDD-battery";
  237. const std::string kTestStylusName = "test_stylus";
  238. const std::u16string kTestStylusName16 = u"test_stylus";
  239. base::test::ScopedFeatureList flags;
  240. flags.InitAndDisableFeature(features::kStylusBatteryStatus);
  241. // Add an external stylus to our test device manager.
  242. ui::TouchscreenDevice stylus(
  243. /*id=*/0, ui::INPUT_DEVICE_USB, kTestStylusName, gfx::Size(),
  244. /*touch_points=*/1,
  245. /*has_stylus=*/true);
  246. stylus.sys_path = base::FilePath(kTestStylusBatteryPath);
  247. ui::DeviceDataManagerTestApi().SetTouchscreenDevices({stylus});
  248. // Verify that when the battery level is 50, no stylus low battery
  249. // notification is shown.
  250. UpdateBatteryLevel(true, kTestStylusBatteryId, kTestStylusName16, 50,
  251. /*battery_report_eligible=*/true,
  252. BI::PeripheralType::kStylusViaScreen, "");
  253. EXPECT_FALSE(message_center_->FindVisibleNotificationById(
  254. PeripheralBatteryNotifier::kStylusNotificationId));
  255. // Verify that when the battery level is 5, a stylus low battery notification
  256. // is shown. Also check that a non stylus device low battery notification will
  257. // not show up.
  258. UpdateBatteryLevel(false, kTestStylusBatteryId, kTestStylusName16, 5,
  259. /*battery_report_eligible=*/true,
  260. BI::PeripheralType::kStylusViaScreen, "");
  261. EXPECT_FALSE(message_center_->FindVisibleNotificationById(
  262. PeripheralBatteryNotifier::kStylusNotificationId));
  263. EXPECT_FALSE(
  264. message_center_->FindVisibleNotificationById(kTestBatteryAddress));
  265. // Verify that when the battery level is -1, the previous stylus low battery
  266. // notification is cancelled.
  267. UpdateBatteryLevel(false, kTestStylusBatteryId, kTestStylusName16,
  268. absl::nullopt, /*battery_report_eligible=*/true,
  269. BI::PeripheralType::kStylusViaScreen, "");
  270. EXPECT_FALSE(message_center_->FindVisibleNotificationById(
  271. PeripheralBatteryNotifier::kStylusNotificationId));
  272. }
  273. TEST_F(PeripheralBatteryNotifierTest,
  274. Bluetooth_CreatesANotificationForEachDevice) {
  275. UpdateBatteryLevel(true, kBluetoothDeviceId1, kBluetoothDeviceName116, 5,
  276. /*battery_report_eligible=*/true,
  277. BI::PeripheralType::kOther, kBluetoothDeviceAddress1);
  278. UpdateBatteryLevel(true, kBluetoothDeviceId2, kBluetoothDeviceName216, 0,
  279. /*battery_report_eligible=*/true,
  280. BI::PeripheralType::kOther, kBluetoothDeviceAddress2);
  281. // Verify 2 notifications were posted with the correct values.
  282. EXPECT_EQ(2u, message_center_->NotificationCount());
  283. message_center::Notification* notification_1 =
  284. message_center_->FindVisibleNotificationById(
  285. kBluetoothDeviceNotificationId1);
  286. message_center::Notification* notification_2 =
  287. message_center_->FindVisibleNotificationById(
  288. kBluetoothDeviceNotificationId2);
  289. EXPECT_TRUE(notification_1);
  290. EXPECT_EQ(kBluetoothDeviceName116, notification_1->title());
  291. EXPECT_EQ(5, ExtractBatteryPercentage(notification_1));
  292. EXPECT_TRUE(notification_2);
  293. EXPECT_EQ(kBluetoothDeviceName216, notification_2->title());
  294. EXPECT_EQ(0, ExtractBatteryPercentage(notification_2));
  295. }
  296. TEST_F(PeripheralBatteryNotifierTest,
  297. Bluetooth_RemovesNotificationForDisconnectedDevices) {
  298. UpdateBatteryLevel(true, kBluetoothDeviceId1, kBluetoothDeviceName116, 5,
  299. /*battery_report_eligible=*/true,
  300. BI::PeripheralType::kOther, kBluetoothDeviceAddress1);
  301. UpdateBatteryLevel(true, kBluetoothDeviceId2, kBluetoothDeviceName216, 0,
  302. /*battery_report_eligible=*/true,
  303. BI::PeripheralType::kOther, kBluetoothDeviceAddress2);
  304. // Verify 2 notifications were posted.
  305. EXPECT_EQ(2u, message_center_->NotificationCount());
  306. // Verify only the notification for device 1 gets removed.
  307. RemoveBattery(kBluetoothDeviceId1, kBluetoothDeviceName116, 5,
  308. /*battery_report_eligible=*/true, BI::PeripheralType::kOther,
  309. kBluetoothDeviceAddress1);
  310. EXPECT_EQ(1u, message_center_->NotificationCount());
  311. EXPECT_TRUE(message_center_->FindVisibleNotificationById(
  312. kBluetoothDeviceNotificationId2));
  313. // Remove the second notification.
  314. RemoveBattery(kBluetoothDeviceId2, kBluetoothDeviceName216, 0,
  315. /*battery_report_eligible=*/true, BI::PeripheralType::kOther,
  316. kBluetoothDeviceAddress2);
  317. EXPECT_EQ(0u, message_center_->NotificationCount());
  318. }
  319. TEST_F(PeripheralBatteryNotifierTest,
  320. Bluetooth_CancelNotificationForInvalidBatteryLevel) {
  321. UpdateBatteryLevel(true, kBluetoothDeviceId1, kBluetoothDeviceName116, 1,
  322. /*battery_report_eligible=*/true,
  323. BI::PeripheralType::kOther, kBluetoothDeviceAddress1);
  324. EXPECT_TRUE(message_center_->FindVisibleNotificationById(
  325. kBluetoothDeviceNotificationId1));
  326. // The notification should get canceled.
  327. UpdateBatteryLevel(true, kBluetoothDeviceId1, kBluetoothDeviceName116,
  328. absl::nullopt, /*battery_report_eligible=*/true,
  329. BI::PeripheralType::kOther, kBluetoothDeviceAddress1);
  330. EXPECT_FALSE(message_center_->FindVisibleNotificationById(
  331. kBluetoothDeviceNotificationId1));
  332. }
  333. // Don't post a notification if the battery level drops again under the
  334. // threshold before kNotificationInterval is completed.
  335. TEST_F(PeripheralBatteryNotifierTest,
  336. DontShowSecondNotificationWithinASmallTimeInterval) {
  337. ClockAdvance(base::Seconds(100));
  338. // Post a notification.
  339. UpdateBatteryLevel(true, kBluetoothDeviceId1, kBluetoothDeviceName116, 1,
  340. /*battery_report_eligible=*/true,
  341. BI::PeripheralType::kOther, kBluetoothDeviceAddress1);
  342. EXPECT_TRUE(message_center_->FindVisibleNotificationById(
  343. kBluetoothDeviceNotificationId1));
  344. // Cancel the notification.
  345. ClockAdvance(base::Seconds(1));
  346. UpdateBatteryLevel(false, kBluetoothDeviceId1, kBluetoothDeviceName116,
  347. absl::nullopt, /*battery_report_eligible=*/true,
  348. BI::PeripheralType::kOther, kBluetoothDeviceAddress1);
  349. EXPECT_FALSE(message_center_->FindVisibleNotificationById(
  350. kBluetoothDeviceNotificationId1));
  351. // The battery level falls below the threshold after a short time period. No
  352. // notification should get posted.
  353. ClockAdvance(base::Seconds(1));
  354. UpdateBatteryLevel(true, kBluetoothDeviceId1, kBluetoothDeviceName116, 1,
  355. /*battery_report_eligible=*/true,
  356. BI::PeripheralType::kOther, kBluetoothDeviceAddress1);
  357. EXPECT_FALSE(message_center_->FindVisibleNotificationById(
  358. kBluetoothDeviceNotificationId1));
  359. }
  360. // Post a notification if the battery is under threshold, then unknown level and
  361. // then is again under the threshold after kNotificationInterval is completed.
  362. TEST_F(PeripheralBatteryNotifierTest,
  363. PostNotificationIfBatteryGoesFromUnknownLevelToBelowThreshold) {
  364. ClockAdvance(base::Seconds(100));
  365. // Post a notification.
  366. UpdateBatteryLevel(true, kBluetoothDeviceId1, kBluetoothDeviceName116, 1,
  367. /*battery_report_eligible=*/true,
  368. BI::PeripheralType::kOther, kBluetoothDeviceAddress1);
  369. EXPECT_TRUE(message_center_->FindVisibleNotificationById(
  370. kBluetoothDeviceNotificationId1));
  371. // Cancel the notification.
  372. ClockAdvance(base::Seconds(1));
  373. UpdateBatteryLevel(true, kBluetoothDeviceId1, kBluetoothDeviceName116,
  374. absl::nullopt, /*battery_report_eligible=*/true,
  375. BI::PeripheralType::kOther, kBluetoothDeviceAddress1);
  376. EXPECT_FALSE(message_center_->FindVisibleNotificationById(
  377. kBluetoothDeviceNotificationId1));
  378. // Post notification if we are out of the kNotificationInterval.
  379. ClockAdvance(base::Seconds(100));
  380. UpdateBatteryLevel(true, kBluetoothDeviceId1, kBluetoothDeviceName116, 1,
  381. /*battery_report_eligible=*/true,
  382. BI::PeripheralType::kOther, kBluetoothDeviceAddress1);
  383. EXPECT_TRUE(message_center_->FindVisibleNotificationById(
  384. kBluetoothDeviceNotificationId1));
  385. }
  386. // Don't Post another notification if the battery level keeps low and the user
  387. // dismissed the previous notification.
  388. TEST_F(PeripheralBatteryNotifierTest,
  389. DontRepostNotificationIfUserDismissedPreviousOne) {
  390. ClockAdvance(base::Seconds(100));
  391. UpdateBatteryLevel(true, kBluetoothDeviceId1, kBluetoothDeviceName116, 5,
  392. /*battery_report_eligible=*/true,
  393. BI::PeripheralType::kOther, kBluetoothDeviceAddress1);
  394. EXPECT_EQ(1u, message_center_->NotificationCount());
  395. // Simulate the user clears the notification.
  396. message_center_->RemoveAllNotifications(
  397. /*by_user=*/true, message_center::MessageCenter::RemoveType::ALL);
  398. // The battery level remains low, but shouldn't post a notification.
  399. ClockAdvance(base::Seconds(100));
  400. UpdateBatteryLevel(true, kBluetoothDeviceId1, kBluetoothDeviceName116, 5,
  401. /*battery_report_eligible=*/true,
  402. BI::PeripheralType::kOther, kBluetoothDeviceAddress1);
  403. EXPECT_EQ(0u, message_center_->NotificationCount());
  404. }
  405. // If there is an existing notification and the battery level remains low,
  406. // update its content.
  407. TEST_F(PeripheralBatteryNotifierTest, UpdateNotificationIfVisible) {
  408. ClockAdvance(base::Seconds(100));
  409. UpdateBatteryLevel(true, kBluetoothDeviceId1, kBluetoothDeviceName116, 5,
  410. /*battery_report_eligible=*/true,
  411. BI::PeripheralType::kOther, kBluetoothDeviceAddress1);
  412. EXPECT_EQ(1u, message_center_->NotificationCount());
  413. // The battery level remains low, should update the notification.
  414. ClockAdvance(base::Seconds(100));
  415. UpdateBatteryLevel(true, kBluetoothDeviceId1, kBluetoothDeviceName116, 3,
  416. /*battery_report_eligible=*/true,
  417. BI::PeripheralType::kOther, kBluetoothDeviceAddress1);
  418. message_center::Notification* notification =
  419. message_center_->FindVisibleNotificationById(
  420. kBluetoothDeviceNotificationId1);
  421. EXPECT_TRUE(notification);
  422. EXPECT_EQ(kBluetoothDeviceName116, notification->title());
  423. EXPECT_EQ(3, ExtractBatteryPercentage(notification));
  424. }
  425. TEST_F(PeripheralBatteryNotifierTest, OpenBluetoothSettingsUi) {
  426. ClockAdvance(base::Seconds(100));
  427. UpdateBatteryLevel(true, kBluetoothDeviceId1, kBluetoothDeviceName116, 5,
  428. /*battery_report_eligible=*/true,
  429. BI::PeripheralType::kOther, kBluetoothDeviceAddress1);
  430. EXPECT_EQ(1u, message_center_->NotificationCount());
  431. message_center::Notification* notification =
  432. message_center_->FindVisibleNotificationById(
  433. kBluetoothDeviceNotificationId1);
  434. EXPECT_TRUE(notification);
  435. message_center_->ClickOnNotification(notification->id());
  436. EXPECT_EQ(1, system_tray_client_->show_bluetooth_settings_count());
  437. EXPECT_FALSE(message_center_->FindVisibleNotificationById(
  438. kBluetoothDeviceNotificationId1));
  439. }
  440. } // namespace ash