Andre Le | 8d500cb | 2021-01-15 22:58:30 | [diff] [blame] | 1 | // Copyright 2020 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 | |
| 5 | #include "ash/system/unified/notification_icons_controller.h" |
| 6 | |
Andre Le | 7dcbb02 | 2021-02-13 22:07:01 | [diff] [blame] | 7 | #include "ash/media/media_notification_constants.h" |
| 8 | #include "ash/public/cpp/ash_features.h" |
Andre Le | 49c0ca1 | 2021-01-28 08:18:49 | [diff] [blame] | 9 | #include "ash/public/cpp/notification_utils.h" |
Andre Le | 7dcbb02 | 2021-02-13 22:07:01 | [diff] [blame] | 10 | #include "ash/public/cpp/vm_camera_mic_constants.h" |
Andre Le | 8d500cb | 2021-01-15 22:58:30 | [diff] [blame] | 11 | #include "ash/system/tray/tray_item_view.h" |
| 12 | #include "ash/system/unified/unified_system_tray.h" |
| 13 | #include "ash/test/ash_test_base.h" |
Andre Le | 7dcbb02 | 2021-02-13 22:07:01 | [diff] [blame] | 14 | #include "base/test/scoped_feature_list.h" |
Andre Le | 49c0ca1 | 2021-01-28 08:18:49 | [diff] [blame] | 15 | #include "ui/message_center/message_center.h" |
| 16 | #include "ui/message_center/public/cpp/notification.h" |
Andre Le | 8d500cb | 2021-01-15 22:58:30 | [diff] [blame] | 17 | |
| 18 | namespace ash { |
| 19 | |
Andre Le | 09892e2 | 2021-02-24 22:23:31 | [diff] [blame] | 20 | class NotificationIconsControllerTest |
| 21 | : public AshTestBase, |
| 22 | public testing::WithParamInterface<bool> { |
Andre Le | 8d500cb | 2021-01-15 22:58:30 | [diff] [blame] | 23 | public: |
| 24 | NotificationIconsControllerTest() = default; |
| 25 | ~NotificationIconsControllerTest() override = default; |
| 26 | |
| 27 | // AshTestBase: |
| 28 | void SetUp() override { |
| 29 | AshTestBase::SetUp(); |
Andre Le | 09892e2 | 2021-02-24 22:23:31 | [diff] [blame] | 30 | scoped_feature_list_.InitWithFeatureState(features::kScalableStatusArea, |
| 31 | IsScalableStatusAreaEnabled()); |
Andre Le | 8d500cb | 2021-01-15 22:58:30 | [diff] [blame] | 32 | tray_ = std::make_unique<UnifiedSystemTray>(GetPrimaryShelf()); |
| 33 | notification_icons_controller_ = |
| 34 | std::make_unique<NotificationIconsController>(tray_.get()); |
| 35 | notification_icons_controller_->AddNotificationTrayItems( |
| 36 | tray_->tray_container()); |
| 37 | } |
| 38 | |
Andre Le | 09892e2 | 2021-02-24 22:23:31 | [diff] [blame] | 39 | bool IsScalableStatusAreaEnabled() { return GetParam(); } |
| 40 | |
Andre Le | 8d500cb | 2021-01-15 22:58:30 | [diff] [blame] | 41 | void TearDown() override { |
| 42 | notification_icons_controller_.reset(); |
| 43 | tray_.reset(); |
| 44 | AshTestBase::TearDown(); |
| 45 | } |
| 46 | |
Andre Le | 7dcbb02 | 2021-02-13 22:07:01 | [diff] [blame] | 47 | TrayItemView* separator() { |
| 48 | return notification_icons_controller_->separator_; |
| 49 | } |
| 50 | |
Andre Le | 7dcbb02 | 2021-02-13 22:07:01 | [diff] [blame] | 51 | std::string AddNotification(bool is_pinned, |
| 52 | bool is_critical_warning, |
| 53 | const std::string& app_id = "app") { |
Andre Le | 49c0ca1 | 2021-01-28 08:18:49 | [diff] [blame] | 54 | std::string id = base::NumberToString(notification_id_++); |
| 55 | |
| 56 | auto warning_level = |
| 57 | is_critical_warning |
| 58 | ? message_center::SystemNotificationWarningLevel::CRITICAL_WARNING |
| 59 | : message_center::SystemNotificationWarningLevel::NORMAL; |
| 60 | message_center::RichNotificationData rich_notification_data; |
| 61 | rich_notification_data.pinned = is_pinned; |
| 62 | |
| 63 | message_center::MessageCenter::Get()->AddNotification( |
| 64 | CreateSystemNotification( |
| 65 | message_center::NOTIFICATION_TYPE_SIMPLE, id, |
| 66 | base::UTF8ToUTF16("test_title"), base::UTF8ToUTF16("test message"), |
Jan Wilken Dörrie | 85285b0 | 2021-03-11 23:38:47 | [diff] [blame^] | 67 | std::u16string() /*display_source */, GURL() /* origin_url */, |
Andre Le | 49c0ca1 | 2021-01-28 08:18:49 | [diff] [blame] | 68 | message_center::NotifierId( |
Andre Le | 7dcbb02 | 2021-02-13 22:07:01 | [diff] [blame] | 69 | message_center::NotifierType::SYSTEM_COMPONENT, app_id), |
Andre Le | 49c0ca1 | 2021-01-28 08:18:49 | [diff] [blame] | 70 | rich_notification_data, nullptr /* delegate */, gfx::VectorIcon(), |
| 71 | warning_level)); |
| 72 | notification_id_++; |
| 73 | |
| 74 | return id; |
| 75 | } |
| 76 | |
Andre Le | 8d500cb | 2021-01-15 22:58:30 | [diff] [blame] | 77 | protected: |
Andre Le | 49c0ca1 | 2021-01-28 08:18:49 | [diff] [blame] | 78 | int notification_id_ = 0; |
Andre Le | 09892e2 | 2021-02-24 22:23:31 | [diff] [blame] | 79 | base::test::ScopedFeatureList scoped_feature_list_; |
Andre Le | 8d500cb | 2021-01-15 22:58:30 | [diff] [blame] | 80 | std::unique_ptr<UnifiedSystemTray> tray_; |
| 81 | std::unique_ptr<NotificationIconsController> notification_icons_controller_; |
| 82 | }; |
| 83 | |
Andre Le | 09892e2 | 2021-02-24 22:23:31 | [diff] [blame] | 84 | INSTANTIATE_TEST_SUITE_P(All, |
| 85 | NotificationIconsControllerTest, |
| 86 | testing::Bool() /* IsScalableStatusAreaEnabled() */); |
| 87 | |
| 88 | TEST_P(NotificationIconsControllerTest, DisplayChanged) { |
Andre Le | 49c0ca1 | 2021-01-28 08:18:49 | [diff] [blame] | 89 | AddNotification(true /* is_pinned */, false /* is_critical_warning */); |
Andre Le | 7dcbb02 | 2021-02-13 22:07:01 | [diff] [blame] | 90 | AddNotification(false /* is_pinned */, false /* is_critical_warning */); |
Andre Le | 7dcbb02 | 2021-02-13 22:07:01 | [diff] [blame] | 91 | |
| 92 | // Notification icons should be shown in medium screen size. |
| 93 | UpdateDisplay("800x800"); |
Andre Le | 09892e2 | 2021-02-24 22:23:31 | [diff] [blame] | 94 | EXPECT_EQ(IsScalableStatusAreaEnabled(), |
| 95 | notification_icons_controller_->tray_items().front()->GetVisible()); |
| 96 | EXPECT_EQ(IsScalableStatusAreaEnabled(), separator()->GetVisible()); |
Andre Le | 49c0ca1 | 2021-01-28 08:18:49 | [diff] [blame] | 97 | |
Andre Le | 8d500cb | 2021-01-15 22:58:30 | [diff] [blame] | 98 | // Notification icons should not be shown in small screen size. |
| 99 | UpdateDisplay("600x600"); |
| 100 | EXPECT_FALSE( |
| 101 | notification_icons_controller_->tray_items().front()->GetVisible()); |
Andre Le | 7dcbb02 | 2021-02-13 22:07:01 | [diff] [blame] | 102 | EXPECT_FALSE(separator()->GetVisible()); |
Andre Le | 8d500cb | 2021-01-15 22:58:30 | [diff] [blame] | 103 | |
Andre Le | 7dcbb02 | 2021-02-13 22:07:01 | [diff] [blame] | 104 | // Notification icons should be shown in large screen size. |
Andre Le | 8d500cb | 2021-01-15 22:58:30 | [diff] [blame] | 105 | UpdateDisplay("1680x800"); |
Andre Le | 09892e2 | 2021-02-24 22:23:31 | [diff] [blame] | 106 | EXPECT_EQ(IsScalableStatusAreaEnabled(), |
| 107 | notification_icons_controller_->tray_items().front()->GetVisible()); |
| 108 | EXPECT_EQ(IsScalableStatusAreaEnabled(), separator()->GetVisible()); |
Andre Le | 8d500cb | 2021-01-15 22:58:30 | [diff] [blame] | 109 | } |
| 110 | |
Andre Le | 09892e2 | 2021-02-24 22:23:31 | [diff] [blame] | 111 | TEST_P(NotificationIconsControllerTest, ShowNotificationIcons) { |
Andre Le | 49c0ca1 | 2021-01-28 08:18:49 | [diff] [blame] | 112 | UpdateDisplay("800x800"); |
| 113 | |
| 114 | // If there's no notification, no notification icons should be shown. |
| 115 | EXPECT_FALSE(notification_icons_controller_->tray_items()[0]->GetVisible()); |
| 116 | EXPECT_FALSE(notification_icons_controller_->tray_items()[1]->GetVisible()); |
Andre Le | 7dcbb02 | 2021-02-13 22:07:01 | [diff] [blame] | 117 | EXPECT_FALSE(separator()->GetVisible()); |
Andre Le | 49c0ca1 | 2021-01-28 08:18:49 | [diff] [blame] | 118 | |
| 119 | // Same case for non pinned or non critical warning notification. |
| 120 | AddNotification(false /* is_pinned */, false /* is_critical_warning */); |
| 121 | EXPECT_FALSE(notification_icons_controller_->tray_items()[0]->GetVisible()); |
| 122 | EXPECT_FALSE(notification_icons_controller_->tray_items()[1]->GetVisible()); |
Andre Le | 7dcbb02 | 2021-02-13 22:07:01 | [diff] [blame] | 123 | EXPECT_FALSE(separator()->GetVisible()); |
Andre Le | 49c0ca1 | 2021-01-28 08:18:49 | [diff] [blame] | 124 | |
| 125 | // Notification icons should be shown when pinned or critical warning |
| 126 | // notification is added. |
| 127 | std::string id0 = |
| 128 | AddNotification(true /* is_pinned */, false /* is_critical_warning */); |
Andre Le | 09892e2 | 2021-02-24 22:23:31 | [diff] [blame] | 129 | EXPECT_EQ(IsScalableStatusAreaEnabled(), |
| 130 | notification_icons_controller_->tray_items()[0]->GetVisible()); |
Andre Le | 49c0ca1 | 2021-01-28 08:18:49 | [diff] [blame] | 131 | EXPECT_FALSE(notification_icons_controller_->tray_items()[1]->GetVisible()); |
Andre Le | 09892e2 | 2021-02-24 22:23:31 | [diff] [blame] | 132 | EXPECT_EQ(IsScalableStatusAreaEnabled(), separator()->GetVisible()); |
Andre Le | 49c0ca1 | 2021-01-28 08:18:49 | [diff] [blame] | 133 | |
| 134 | std::string id1 = |
| 135 | AddNotification(false /* is_pinned */, true /* is_critical_warning */); |
Andre Le | 09892e2 | 2021-02-24 22:23:31 | [diff] [blame] | 136 | EXPECT_EQ(IsScalableStatusAreaEnabled(), |
| 137 | notification_icons_controller_->tray_items()[0]->GetVisible()); |
| 138 | EXPECT_EQ(IsScalableStatusAreaEnabled(), |
| 139 | notification_icons_controller_->tray_items()[1]->GetVisible()); |
| 140 | EXPECT_EQ(IsScalableStatusAreaEnabled(), separator()->GetVisible()); |
Andre Le | 49c0ca1 | 2021-01-28 08:18:49 | [diff] [blame] | 141 | |
| 142 | // Remove the critical warning notification should make the tray show only one |
| 143 | // icon. |
| 144 | message_center::MessageCenter::Get()->RemoveNotification(id1, |
| 145 | false /* by_user */); |
Andre Le | 09892e2 | 2021-02-24 22:23:31 | [diff] [blame] | 146 | EXPECT_EQ(IsScalableStatusAreaEnabled(), |
| 147 | notification_icons_controller_->tray_items()[0]->GetVisible()); |
Andre Le | 49c0ca1 | 2021-01-28 08:18:49 | [diff] [blame] | 148 | EXPECT_FALSE(notification_icons_controller_->tray_items()[1]->GetVisible()); |
Andre Le | 09892e2 | 2021-02-24 22:23:31 | [diff] [blame] | 149 | EXPECT_EQ(IsScalableStatusAreaEnabled(), separator()->GetVisible()); |
Andre Le | 49c0ca1 | 2021-01-28 08:18:49 | [diff] [blame] | 150 | |
| 151 | // Remove the pinned notification, no icon is shown. |
| 152 | message_center::MessageCenter::Get()->RemoveNotification(id0, |
| 153 | false /* by_user */); |
| 154 | EXPECT_FALSE(notification_icons_controller_->tray_items()[0]->GetVisible()); |
| 155 | EXPECT_FALSE(notification_icons_controller_->tray_items()[1]->GetVisible()); |
Andre Le | 7dcbb02 | 2021-02-13 22:07:01 | [diff] [blame] | 156 | EXPECT_FALSE(separator()->GetVisible()); |
| 157 | } |
| 158 | |
Andre Le | 8d500cb | 2021-01-15 22:58:30 | [diff] [blame] | 159 | } // namespace ash |