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

ash / system / usb_peripheral / usb_peripheral_notification_controller_unittest.cc [blame]

// Copyright 2021 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/usb_peripheral/usb_peripheral_notification_controller.h"

#include "ash/constants/ash_features.h"
#include "ash/shell.h"
#include "ash/test/ash_test_base.h"
#include "ui/message_center/message_center.h"

using message_center::MessageCenter;

namespace ash {

namespace {

const char kUsbPeripheralInvalidDpCableNotificationId[] =
    "cros_usb_peripheral_invalid_dp_cable_notification_id";
const char kUsbPeripheralInvalidUSB4ValidTBTCableNotificationId[] =
    "cros_usb_peripheral_invalid_usb4_valid_tbt_cable_notification_id";
const char kUsbPeripheralInvalidUSB4CableNotificationId[] =
    "cros_usb_peripheral_invalid_usb4_cable_notification_id";
const char kUsbPeripheralInvalidTBTCableNotificationId[] =
    "cros_usb_peripheral_invalid_tbt_cable_notification_id";
const char kUsbPeripheralSpeedLimitingCableNotificationId[] =
    "cros_usb_peripheral_speed_limiting_cable_notification_id";

}  // namespace

class UsbPeripheralNotificationControllerTest : public AshTestBase {
 public:
  UsbPeripheralNotificationControllerTest() = default;
  UsbPeripheralNotificationControllerTest(
      const UsbPeripheralNotificationControllerTest&) = delete;
  UsbPeripheralNotificationControllerTest& operator=(
      const UsbPeripheralNotificationControllerTest&) = delete;
  ~UsbPeripheralNotificationControllerTest() override = default;

  UsbPeripheralNotificationController* controller() {
    return Shell::Get()->usb_peripheral_notification_controller();
  }

 private:
};

TEST_F(UsbPeripheralNotificationControllerTest, InvalidDpCableNotification) {
  EXPECT_EQ(MessageCenter::Get()->NotificationCount(), 0u);
  controller()->OnInvalidDpCableWarning();
  EXPECT_EQ(MessageCenter::Get()->NotificationCount(), 1u);

  message_center::Notification* notification =
      MessageCenter::Get()->FindVisibleNotificationById(
          kUsbPeripheralInvalidDpCableNotificationId);
  ASSERT_TRUE(notification);

  EXPECT_EQ(notification->buttons().size(), 1u);
  controller()->OnInvalidDpCableWarning();
  EXPECT_EQ(MessageCenter::Get()->NotificationCount(), 1u);
}

TEST_F(UsbPeripheralNotificationControllerTest,
       InvalidUSB4ValidTBTCableNotification) {
  EXPECT_EQ(MessageCenter::Get()->NotificationCount(), 0u);
  controller()->OnInvalidUSB4ValidTBTCableWarning();
  EXPECT_EQ(MessageCenter::Get()->NotificationCount(), 1u);

  message_center::Notification* notification =
      MessageCenter::Get()->FindVisibleNotificationById(
          kUsbPeripheralInvalidUSB4ValidTBTCableNotificationId);
  ASSERT_TRUE(notification);

  EXPECT_EQ(notification->buttons().size(), 1u);
  controller()->OnInvalidUSB4ValidTBTCableWarning();
  EXPECT_EQ(MessageCenter::Get()->NotificationCount(), 1u);
}

TEST_F(UsbPeripheralNotificationControllerTest, InvalidUSB4CableNotification) {
  EXPECT_EQ(MessageCenter::Get()->NotificationCount(), 0u);
  controller()->OnInvalidUSB4CableWarning();
  EXPECT_EQ(MessageCenter::Get()->NotificationCount(), 1u);

  message_center::Notification* notification =
      MessageCenter::Get()->FindVisibleNotificationById(
          kUsbPeripheralInvalidUSB4CableNotificationId);
  ASSERT_TRUE(notification);

  EXPECT_EQ(notification->buttons().size(), 1u);
  controller()->OnInvalidUSB4CableWarning();
  EXPECT_EQ(MessageCenter::Get()->NotificationCount(), 1u);
}

TEST_F(UsbPeripheralNotificationControllerTest, InvalidTBTCableNotification) {
  EXPECT_EQ(MessageCenter::Get()->NotificationCount(), 0u);
  controller()->OnInvalidTBTCableWarning();
  EXPECT_EQ(MessageCenter::Get()->NotificationCount(), 1u);

  message_center::Notification* notification =
      MessageCenter::Get()->FindVisibleNotificationById(
          kUsbPeripheralInvalidTBTCableNotificationId);
  ASSERT_TRUE(notification);

  EXPECT_EQ(notification->buttons().size(), 1u);
  controller()->OnInvalidTBTCableWarning();
  EXPECT_EQ(MessageCenter::Get()->NotificationCount(), 1u);
}

TEST_F(UsbPeripheralNotificationControllerTest,
       SpeedLimitingCableNotification) {
  EXPECT_EQ(MessageCenter::Get()->NotificationCount(), 0u);
  controller()->OnSpeedLimitingCableWarning();
  EXPECT_EQ(MessageCenter::Get()->NotificationCount(), 1u);

  message_center::Notification* notification =
      MessageCenter::Get()->FindVisibleNotificationById(
          kUsbPeripheralSpeedLimitingCableNotificationId);
  ASSERT_TRUE(notification);

  EXPECT_EQ(notification->buttons().size(), 1u);
  controller()->OnSpeedLimitingCableWarning();
  EXPECT_EQ(MessageCenter::Get()->NotificationCount(), 1u);
}

TEST_F(UsbPeripheralNotificationControllerTest,
       SpeedLimitingCableNotificationWithClick) {
  EXPECT_EQ(MessageCenter::Get()->NotificationCount(), 0u);
  controller()->OnSpeedLimitingCableWarning();
  EXPECT_EQ(MessageCenter::Get()->NotificationCount(), 1u);

  message_center::Notification* notification =
      MessageCenter::Get()->FindVisibleNotificationById(
          kUsbPeripheralSpeedLimitingCableNotificationId);
  ASSERT_TRUE(notification);

  // Click the notification to close it.
  notification->delegate()->Click(std::nullopt, std::nullopt);

  // Resend the notification, but expect it not to show after being clicked.
  EXPECT_EQ(MessageCenter::Get()->NotificationCount(), 0u);
  controller()->OnSpeedLimitingCableWarning();
  EXPECT_EQ(MessageCenter::Get()->NotificationCount(), 0u);
}

}  // namespace ash