[go: nahoru, domu]

blob: 78edc33e4854f370e1357bde5efe1e08f76bc160 [file] [log] [blame]
Andre Le8d500cb2021-01-15 22:58:301// 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 Le7dcbb022021-02-13 22:07:017#include "ash/media/media_notification_constants.h"
8#include "ash/public/cpp/ash_features.h"
Andre Le49c0ca12021-01-28 08:18:499#include "ash/public/cpp/notification_utils.h"
Andre Le7dcbb022021-02-13 22:07:0110#include "ash/public/cpp/vm_camera_mic_constants.h"
Andre Le8d500cb2021-01-15 22:58:3011#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 Le7dcbb022021-02-13 22:07:0114#include "base/test/scoped_feature_list.h"
Andre Le49c0ca12021-01-28 08:18:4915#include "ui/message_center/message_center.h"
16#include "ui/message_center/public/cpp/notification.h"
Andre Le8d500cb2021-01-15 22:58:3017
18namespace ash {
19
Andre Le09892e22021-02-24 22:23:3120class NotificationIconsControllerTest
21 : public AshTestBase,
22 public testing::WithParamInterface<bool> {
Andre Le8d500cb2021-01-15 22:58:3023 public:
24 NotificationIconsControllerTest() = default;
25 ~NotificationIconsControllerTest() override = default;
26
27 // AshTestBase:
28 void SetUp() override {
29 AshTestBase::SetUp();
Andre Le09892e22021-02-24 22:23:3130 scoped_feature_list_.InitWithFeatureState(features::kScalableStatusArea,
31 IsScalableStatusAreaEnabled());
Andre Le8d500cb2021-01-15 22:58:3032 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 Le09892e22021-02-24 22:23:3139 bool IsScalableStatusAreaEnabled() { return GetParam(); }
40
Andre Le8d500cb2021-01-15 22:58:3041 void TearDown() override {
42 notification_icons_controller_.reset();
43 tray_.reset();
44 AshTestBase::TearDown();
45 }
46
Andre Le7dcbb022021-02-13 22:07:0147 TrayItemView* separator() {
48 return notification_icons_controller_->separator_;
49 }
50
Andre Le7dcbb022021-02-13 22:07:0151 std::string AddNotification(bool is_pinned,
52 bool is_critical_warning,
53 const std::string& app_id = "app") {
Andre Le49c0ca12021-01-28 08:18:4954 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örrie85285b02021-03-11 23:38:4767 std::u16string() /*display_source */, GURL() /* origin_url */,
Andre Le49c0ca12021-01-28 08:18:4968 message_center::NotifierId(
Andre Le7dcbb022021-02-13 22:07:0169 message_center::NotifierType::SYSTEM_COMPONENT, app_id),
Andre Le49c0ca12021-01-28 08:18:4970 rich_notification_data, nullptr /* delegate */, gfx::VectorIcon(),
71 warning_level));
72 notification_id_++;
73
74 return id;
75 }
76
Andre Le8d500cb2021-01-15 22:58:3077 protected:
Andre Le49c0ca12021-01-28 08:18:4978 int notification_id_ = 0;
Andre Le09892e22021-02-24 22:23:3179 base::test::ScopedFeatureList scoped_feature_list_;
Andre Le8d500cb2021-01-15 22:58:3080 std::unique_ptr<UnifiedSystemTray> tray_;
81 std::unique_ptr<NotificationIconsController> notification_icons_controller_;
82};
83
Andre Le09892e22021-02-24 22:23:3184INSTANTIATE_TEST_SUITE_P(All,
85 NotificationIconsControllerTest,
86 testing::Bool() /* IsScalableStatusAreaEnabled() */);
87
88TEST_P(NotificationIconsControllerTest, DisplayChanged) {
Andre Le49c0ca12021-01-28 08:18:4989 AddNotification(true /* is_pinned */, false /* is_critical_warning */);
Andre Le7dcbb022021-02-13 22:07:0190 AddNotification(false /* is_pinned */, false /* is_critical_warning */);
Andre Le7dcbb022021-02-13 22:07:0191
92 // Notification icons should be shown in medium screen size.
93 UpdateDisplay("800x800");
Andre Le09892e22021-02-24 22:23:3194 EXPECT_EQ(IsScalableStatusAreaEnabled(),
95 notification_icons_controller_->tray_items().front()->GetVisible());
96 EXPECT_EQ(IsScalableStatusAreaEnabled(), separator()->GetVisible());
Andre Le49c0ca12021-01-28 08:18:4997
Andre Le8d500cb2021-01-15 22:58:3098 // 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 Le7dcbb022021-02-13 22:07:01102 EXPECT_FALSE(separator()->GetVisible());
Andre Le8d500cb2021-01-15 22:58:30103
Andre Le7dcbb022021-02-13 22:07:01104 // Notification icons should be shown in large screen size.
Andre Le8d500cb2021-01-15 22:58:30105 UpdateDisplay("1680x800");
Andre Le09892e22021-02-24 22:23:31106 EXPECT_EQ(IsScalableStatusAreaEnabled(),
107 notification_icons_controller_->tray_items().front()->GetVisible());
108 EXPECT_EQ(IsScalableStatusAreaEnabled(), separator()->GetVisible());
Andre Le8d500cb2021-01-15 22:58:30109}
110
Andre Le09892e22021-02-24 22:23:31111TEST_P(NotificationIconsControllerTest, ShowNotificationIcons) {
Andre Le49c0ca12021-01-28 08:18:49112 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 Le7dcbb022021-02-13 22:07:01117 EXPECT_FALSE(separator()->GetVisible());
Andre Le49c0ca12021-01-28 08:18:49118
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 Le7dcbb022021-02-13 22:07:01123 EXPECT_FALSE(separator()->GetVisible());
Andre Le49c0ca12021-01-28 08:18:49124
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 Le09892e22021-02-24 22:23:31129 EXPECT_EQ(IsScalableStatusAreaEnabled(),
130 notification_icons_controller_->tray_items()[0]->GetVisible());
Andre Le49c0ca12021-01-28 08:18:49131 EXPECT_FALSE(notification_icons_controller_->tray_items()[1]->GetVisible());
Andre Le09892e22021-02-24 22:23:31132 EXPECT_EQ(IsScalableStatusAreaEnabled(), separator()->GetVisible());
Andre Le49c0ca12021-01-28 08:18:49133
134 std::string id1 =
135 AddNotification(false /* is_pinned */, true /* is_critical_warning */);
Andre Le09892e22021-02-24 22:23:31136 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 Le49c0ca12021-01-28 08:18:49141
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 Le09892e22021-02-24 22:23:31146 EXPECT_EQ(IsScalableStatusAreaEnabled(),
147 notification_icons_controller_->tray_items()[0]->GetVisible());
Andre Le49c0ca12021-01-28 08:18:49148 EXPECT_FALSE(notification_icons_controller_->tray_items()[1]->GetVisible());
Andre Le09892e22021-02-24 22:23:31149 EXPECT_EQ(IsScalableStatusAreaEnabled(), separator()->GetVisible());
Andre Le49c0ca12021-01-28 08:18:49150
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 Le7dcbb022021-02-13 22:07:01156 EXPECT_FALSE(separator()->GetVisible());
157}
158
Andre Le8d500cb2021-01-15 22:58:30159} // namespace ash