usb_peripheral_notification_controller_unittest.cc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // Copyright 2021 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/usb_peripheral/usb_peripheral_notification_controller.h"
  5. #include "ash/constants/ash_features.h"
  6. #include "ash/shell.h"
  7. #include "ash/test/ash_test_base.h"
  8. #include "ui/message_center/message_center.h"
  9. using message_center::MessageCenter;
  10. namespace ash {
  11. namespace {
  12. const char kUsbPeripheralInvalidDpCableNotificationId[] =
  13. "cros_usb_peripheral_invalid_dp_cable_notification_id";
  14. const char kUsbPeripheralInvalidUSB4ValidTBTCableNotificationId[] =
  15. "cros_usb_peripheral_invalid_usb4_valid_tbt_cable_notification_id";
  16. const char kUsbPeripheralInvalidUSB4CableNotificationId[] =
  17. "cros_usb_peripheral_invalid_usb4_cable_notification_id";
  18. const char kUsbPeripheralInvalidTBTCableNotificationId[] =
  19. "cros_usb_peripheral_invalid_tbt_cable_notification_id";
  20. const char kUsbPeripheralSpeedLimitingCableNotificationId[] =
  21. "cros_usb_peripheral_speed_limiting_cable_notification_id";
  22. } // namespace
  23. class UsbPeripheralNotificationControllerTest : public AshTestBase {
  24. public:
  25. UsbPeripheralNotificationControllerTest() = default;
  26. UsbPeripheralNotificationControllerTest(
  27. const UsbPeripheralNotificationControllerTest&) = delete;
  28. UsbPeripheralNotificationControllerTest& operator=(
  29. const UsbPeripheralNotificationControllerTest&) = delete;
  30. ~UsbPeripheralNotificationControllerTest() override = default;
  31. UsbPeripheralNotificationController* controller() {
  32. return Shell::Get()->usb_peripheral_notification_controller();
  33. }
  34. private:
  35. };
  36. TEST_F(UsbPeripheralNotificationControllerTest, InvalidDpCableNotification) {
  37. EXPECT_EQ(MessageCenter::Get()->NotificationCount(), 0u);
  38. controller()->OnInvalidDpCableWarning();
  39. EXPECT_EQ(MessageCenter::Get()->NotificationCount(), 1u);
  40. message_center::Notification* notification =
  41. MessageCenter::Get()->FindVisibleNotificationById(
  42. kUsbPeripheralInvalidDpCableNotificationId);
  43. ASSERT_TRUE(notification);
  44. EXPECT_EQ(notification->buttons().size(), 1u);
  45. controller()->OnInvalidDpCableWarning();
  46. EXPECT_EQ(MessageCenter::Get()->NotificationCount(), 1u);
  47. }
  48. TEST_F(UsbPeripheralNotificationControllerTest,
  49. InvalidUSB4ValidTBTCableNotification) {
  50. EXPECT_EQ(MessageCenter::Get()->NotificationCount(), 0u);
  51. controller()->OnInvalidUSB4ValidTBTCableWarning();
  52. EXPECT_EQ(MessageCenter::Get()->NotificationCount(), 1u);
  53. message_center::Notification* notification =
  54. MessageCenter::Get()->FindVisibleNotificationById(
  55. kUsbPeripheralInvalidUSB4ValidTBTCableNotificationId);
  56. ASSERT_TRUE(notification);
  57. EXPECT_EQ(notification->buttons().size(), 1u);
  58. controller()->OnInvalidUSB4ValidTBTCableWarning();
  59. EXPECT_EQ(MessageCenter::Get()->NotificationCount(), 1u);
  60. }
  61. TEST_F(UsbPeripheralNotificationControllerTest, InvalidUSB4CableNotification) {
  62. EXPECT_EQ(MessageCenter::Get()->NotificationCount(), 0u);
  63. controller()->OnInvalidUSB4CableWarning();
  64. EXPECT_EQ(MessageCenter::Get()->NotificationCount(), 1u);
  65. message_center::Notification* notification =
  66. MessageCenter::Get()->FindVisibleNotificationById(
  67. kUsbPeripheralInvalidUSB4CableNotificationId);
  68. ASSERT_TRUE(notification);
  69. EXPECT_EQ(notification->buttons().size(), 1u);
  70. controller()->OnInvalidUSB4CableWarning();
  71. EXPECT_EQ(MessageCenter::Get()->NotificationCount(), 1u);
  72. }
  73. TEST_F(UsbPeripheralNotificationControllerTest, InvalidTBTCableNotification) {
  74. EXPECT_EQ(MessageCenter::Get()->NotificationCount(), 0u);
  75. controller()->OnInvalidTBTCableWarning();
  76. EXPECT_EQ(MessageCenter::Get()->NotificationCount(), 1u);
  77. message_center::Notification* notification =
  78. MessageCenter::Get()->FindVisibleNotificationById(
  79. kUsbPeripheralInvalidTBTCableNotificationId);
  80. ASSERT_TRUE(notification);
  81. EXPECT_EQ(notification->buttons().size(), 1u);
  82. controller()->OnInvalidTBTCableWarning();
  83. EXPECT_EQ(MessageCenter::Get()->NotificationCount(), 1u);
  84. }
  85. TEST_F(UsbPeripheralNotificationControllerTest,
  86. SpeedLimitingCableNotification) {
  87. EXPECT_EQ(MessageCenter::Get()->NotificationCount(), 0u);
  88. controller()->OnSpeedLimitingCableWarning();
  89. EXPECT_EQ(MessageCenter::Get()->NotificationCount(), 1u);
  90. message_center::Notification* notification =
  91. MessageCenter::Get()->FindVisibleNotificationById(
  92. kUsbPeripheralSpeedLimitingCableNotificationId);
  93. ASSERT_TRUE(notification);
  94. EXPECT_EQ(notification->buttons().size(), 1u);
  95. controller()->OnSpeedLimitingCableWarning();
  96. EXPECT_EQ(MessageCenter::Get()->NotificationCount(), 1u);
  97. }
  98. TEST_F(UsbPeripheralNotificationControllerTest,
  99. SpeedLimitingCableNotificationWithClick) {
  100. EXPECT_EQ(MessageCenter::Get()->NotificationCount(), 0u);
  101. controller()->OnSpeedLimitingCableWarning();
  102. EXPECT_EQ(MessageCenter::Get()->NotificationCount(), 1u);
  103. message_center::Notification* notification =
  104. MessageCenter::Get()->FindVisibleNotificationById(
  105. kUsbPeripheralSpeedLimitingCableNotificationId);
  106. ASSERT_TRUE(notification);
  107. // Click the notification to close it.
  108. notification->delegate()->Click(absl::nullopt, absl::nullopt);
  109. // Resend the notification, but expect it not to show after being clicked.
  110. EXPECT_EQ(MessageCenter::Get()->NotificationCount(), 0u);
  111. controller()->OnSpeedLimitingCableWarning();
  112. EXPECT_EQ(MessageCenter::Get()->NotificationCount(), 0u);
  113. }
  114. } // namespace ash