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.
5 #include "chrome/browser/ui/views/message_center/web_notification_tray.h"
9 #include "ash/root_window_controller.h"
10 #include "ash/system/status_area_widget.h"
11 #include "ash/system/tray/system_tray_item.h"
12 #include "base/strings/stringprintf.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "chrome/browser/browser_process.h"
15 #include "chrome/browser/notifications/message_center_notification_manager.h"
16 #include "chrome/browser/notifications/notification.h"
17 #include "chrome/browser/notifications/notification_delegate.h"
18 #include "chrome/browser/notifications/notification_ui_manager.h"
19 #include "chrome/browser/ui/browser.h"
20 #include "chrome/test/base/in_process_browser_test.h"
21 #include "content/public/test/test_utils.h"
22 #include "ui/message_center/message_center_style.h"
23 #include "ui/message_center/message_center_tray.h"
24 #include "ui/message_center/notification_list.h"
25 #include "ui/message_center/notification_types.h"
26 #include "ui/message_center/views/message_center_bubble.h"
27 #include "ui/message_center/views/message_popup_collection.h"
28 #include "ui/views/controls/label.h"
29 #include "ui/views/layout/fill_layout.h"
30 #include "ui/views/view.h"
31 #include "ui/views/widget/widget.h"
33 namespace message_center
{
37 class WebNotificationTrayTest
: public InProcessBrowserTest
{
39 WebNotificationTrayTest() {}
40 ~WebNotificationTrayTest() override
{}
42 void TearDownOnMainThread() override
{
43 message_center::MessageCenter::Get()->RemoveAllNotifications(false);
47 class TestNotificationDelegate
: public ::NotificationDelegate
{
49 explicit TestNotificationDelegate(std::string id
) : id_(id
) {}
50 std::string
id() const override
{ return id_
; }
53 ~TestNotificationDelegate() override
{}
58 void AddNotification(const std::string
& delegate_id
,
59 const std::string
& replace_id
) {
60 ::Notification
notification(
61 GURL("chrome-extension://abbccedd"),
62 base::ASCIIToUTF16("Test Web Notification"),
63 base::ASCIIToUTF16("Notification message body."),
67 new TestNotificationDelegate(delegate_id
));
69 g_browser_process
->notification_ui_manager()->Add(notification
,
70 browser()->profile());
73 std::string
FindNotificationIdByDelegateId(const std::string
& delegate_id
) {
74 const ::Notification
* notification
=
75 g_browser_process
->notification_ui_manager()->FindById(
77 NotificationUIManager::GetProfileID(browser()->profile()));
78 EXPECT_TRUE(notification
);
79 return notification
->id();
82 void UpdateNotification(const std::string
& replace_id
,
83 const std::string
& new_id
) {
84 ::Notification
notification(GURL("chrome-extension://abbccedd"),
85 base::ASCIIToUTF16("Updated Web Notification"),
86 base::ASCIIToUTF16("Updated message body."),
90 new TestNotificationDelegate(new_id
));
92 g_browser_process
->notification_ui_manager()->Add(notification
,
93 browser()->profile());
96 void RemoveNotification(const std::string
& id
) {
97 g_browser_process
->notification_ui_manager()->CancelById(
98 id
, NotificationUIManager::GetProfileID(browser()->profile()));
101 bool HasNotification(message_center::MessageCenter
* message_center
,
102 const std::string
& id
) {
103 return message_center
->FindVisibleNotificationById(id
) != NULL
;
107 DISALLOW_COPY_AND_ASSIGN(WebNotificationTrayTest
);
113 // TODO(dewittj): More exhaustive testing.
114 IN_PROC_BROWSER_TEST_F(WebNotificationTrayTest
, WebNotifications
) {
115 message_center::MessageCenter
* message_center
=
116 message_center::MessageCenter::Get();
118 // Add a notification.
119 AddNotification("test_id1", "replace_id1");
120 EXPECT_EQ(1u, message_center
->NotificationCount());
121 EXPECT_TRUE(HasNotification(message_center
,
122 FindNotificationIdByDelegateId("test_id1")));
123 EXPECT_FALSE(HasNotification(message_center
, "test_id2"));
124 AddNotification("test_id2", "replace_id2");
125 AddNotification("test_id2", "replace_id2");
126 EXPECT_EQ(2u, message_center
->NotificationCount());
127 EXPECT_TRUE(HasNotification(message_center
,
128 FindNotificationIdByDelegateId("test_id1")));
130 // Ensure that updating a notification does not affect the count.
131 UpdateNotification("replace_id2", "test_id3");
132 UpdateNotification("replace_id2", "test_id3");
133 EXPECT_EQ(2u, message_center
->NotificationCount());
134 EXPECT_FALSE(HasNotification(message_center
, "test_id2"));
136 // Ensure that Removing the first notification removes it from the tray.
137 RemoveNotification("test_id1");
138 EXPECT_FALSE(HasNotification(message_center
, "test_id1"));
139 EXPECT_EQ(1u, message_center
->NotificationCount());
141 // Remove the remaining notification.
142 RemoveNotification("test_id3");
143 EXPECT_EQ(0u, message_center
->NotificationCount());
144 EXPECT_FALSE(HasNotification(message_center
, "test_id1"));
147 IN_PROC_BROWSER_TEST_F(WebNotificationTrayTest
, WebNotificationPopupBubble
) {
148 scoped_ptr
<WebNotificationTray
> tray(new WebNotificationTray());
149 tray
->message_center();
151 // Adding a notification should show the popup bubble.
152 AddNotification("test_id1", "replace_id1");
153 EXPECT_TRUE(tray
->message_center_tray_
->popups_visible());
155 // Updating a notification should not hide the popup bubble.
156 AddNotification("test_id2", "replace_id2");
157 UpdateNotification("replace_id2", "test_id3");
158 EXPECT_TRUE(tray
->message_center_tray_
->popups_visible());
160 // Removing the first notification should not hide the popup bubble.
161 RemoveNotification("test_id1");
162 EXPECT_TRUE(tray
->message_center_tray_
->popups_visible());
164 // Removing the visible notification should hide the popup bubble.
165 RemoveNotification("test_id3");
166 EXPECT_FALSE(tray
->message_center_tray_
->popups_visible());
169 using message_center::NotificationList
;
171 IN_PROC_BROWSER_TEST_F(WebNotificationTrayTest
, ManyPopupNotifications
) {
172 scoped_ptr
<WebNotificationTray
> tray(new WebNotificationTray());
173 message_center::MessageCenter
* message_center
= tray
->message_center();
175 // Add the max visible popup notifications +1, ensure the correct num visible.
176 size_t notifications_to_add
= kMaxVisiblePopupNotifications
+ 1;
177 for (size_t i
= 0; i
< notifications_to_add
; ++i
) {
178 std::string id
= base::StringPrintf("test_id%d", static_cast<int>(i
));
179 std::string replace_id
=
180 base::StringPrintf("replace_id%d", static_cast<int>(i
));
181 AddNotification(id
, replace_id
);
183 // Hide and reshow the bubble so that it is updated immediately, not delayed.
184 tray
->message_center_tray_
->HidePopupBubble();
185 tray
->message_center_tray_
->ShowPopupBubble();
186 EXPECT_TRUE(tray
->message_center_tray_
->popups_visible());
187 EXPECT_EQ(notifications_to_add
, message_center
->NotificationCount());
188 NotificationList::PopupNotifications popups
=
189 message_center
->GetPopupNotifications();
190 EXPECT_EQ(kMaxVisiblePopupNotifications
, popups
.size());
193 } // namespace message_center