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/notification.h"
16 #include "chrome/browser/notifications/notification_delegate.h"
17 #include "chrome/browser/notifications/notification_ui_manager.h"
18 #include "chrome/browser/ui/browser.h"
19 #include "chrome/test/base/in_process_browser_test.h"
20 #include "content/public/test/test_utils.h"
21 #include "ui/message_center/message_center_style.h"
22 #include "ui/message_center/message_center_tray.h"
23 #include "ui/message_center/notification_list.h"
24 #include "ui/message_center/notification_types.h"
25 #include "ui/message_center/views/message_center_bubble.h"
26 #include "ui/message_center/views/message_popup_collection.h"
27 #include "ui/views/controls/label.h"
28 #include "ui/views/layout/fill_layout.h"
29 #include "ui/views/view.h"
30 #include "ui/views/widget/widget.h"
32 namespace message_center
{
36 class WebNotificationTrayTest
: public InProcessBrowserTest
{
38 WebNotificationTrayTest() {}
39 virtual ~WebNotificationTrayTest() {}
41 virtual void CleanUpOnMainThread() OVERRIDE
{
42 message_center::MessageCenter::Get()->RemoveAllNotifications(false);
46 class TestNotificationDelegate
: public ::NotificationDelegate
{
48 explicit TestNotificationDelegate(std::string id
) : id_(id
) {}
49 virtual void Display() OVERRIDE
{}
50 virtual void Error() OVERRIDE
{}
51 virtual void Close(bool by_user
) OVERRIDE
{}
52 virtual void Click() OVERRIDE
{}
53 virtual std::string
id() const OVERRIDE
{ return id_
; }
54 virtual content::RenderViewHost
* GetRenderViewHost() const OVERRIDE
{
59 virtual ~TestNotificationDelegate() {}
64 void AddNotification(const std::string
& id
, const std::string
& replace_id
) {
65 ::Notification
notification(GURL("chrome-extension://abbccedd"),
67 base::ASCIIToUTF16("Test Web Notification"),
69 "Notification message body."),
70 blink::WebTextDirectionDefault
,
72 base::ASCIIToUTF16(replace_id
),
73 new TestNotificationDelegate(id
));
75 g_browser_process
->notification_ui_manager()->Add(
76 notification
, browser()->profile());
79 void UpdateNotification(const std::string
& replace_id
,
80 const std::string
& new_id
) {
81 ::Notification
notification(GURL("chrome-extension://abbccedd"),
83 base::ASCIIToUTF16("Updated Web Notification"),
84 base::ASCIIToUTF16("Updated message body."),
85 blink::WebTextDirectionDefault
,
87 base::ASCIIToUTF16(replace_id
),
88 new TestNotificationDelegate(new_id
));
90 g_browser_process
->notification_ui_manager()->Add(
91 notification
, browser()->profile());
94 void RemoveNotification(const std::string
& id
) {
95 g_browser_process
->notification_ui_manager()->CancelById(id
);
99 DISALLOW_COPY_AND_ASSIGN(WebNotificationTrayTest
);
105 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA)
106 // TODO(erg): linux_aura bringup: http://crbug.com/163931
107 #define MAYBE_WebNotifications DISABLED_WebNotifications
109 #define MAYBE_WebNotifications WebNotifications
112 // TODO(dewittj): More exhaustive testing.
113 IN_PROC_BROWSER_TEST_F(WebNotificationTrayTest
, MAYBE_WebNotifications
) {
114 message_center::MessageCenter
* message_center
=
115 message_center::MessageCenter::Get();
117 // Add a notification.
118 AddNotification("test_id1", "replace_id1");
119 EXPECT_EQ(1u, message_center
->NotificationCount());
120 EXPECT_TRUE(message_center
->HasNotification("test_id1"));
121 EXPECT_FALSE(message_center
->HasNotification("test_id2"));
122 AddNotification("test_id2", "replace_id2");
123 AddNotification("test_id2", "replace_id2");
124 EXPECT_EQ(2u, message_center
->NotificationCount());
125 EXPECT_TRUE(message_center
->HasNotification("test_id2"));
127 // Ensure that updating a notification does not affect the count.
128 UpdateNotification("replace_id2", "test_id3");
129 UpdateNotification("replace_id2", "test_id3");
130 EXPECT_EQ(2u, message_center
->NotificationCount());
131 EXPECT_FALSE(message_center
->HasNotification("test_id2"));
133 // Ensure that Removing the first notification removes it from the tray.
134 RemoveNotification("test_id1");
135 EXPECT_FALSE(message_center
->HasNotification("test_id1"));
136 EXPECT_EQ(1u, message_center
->NotificationCount());
138 // Remove the remaining notification.
139 RemoveNotification("test_id3");
140 EXPECT_EQ(0u, message_center
->NotificationCount());
141 EXPECT_FALSE(message_center
->HasNotification("test_id3"));
144 IN_PROC_BROWSER_TEST_F(WebNotificationTrayTest
, WebNotificationPopupBubble
) {
145 scoped_ptr
<WebNotificationTray
> tray(new WebNotificationTray());
146 tray
->message_center();
148 // Adding a notification should show the popup bubble.
149 AddNotification("test_id1", "replace_id1");
150 EXPECT_TRUE(tray
->message_center_tray_
->popups_visible());
152 // Updating a notification should not hide the popup bubble.
153 AddNotification("test_id2", "replace_id2");
154 UpdateNotification("replace_id2", "test_id3");
155 EXPECT_TRUE(tray
->message_center_tray_
->popups_visible());
157 // Removing the first notification should not hide the popup bubble.
158 RemoveNotification("test_id1");
159 EXPECT_TRUE(tray
->message_center_tray_
->popups_visible());
161 // Removing the visible notification should hide the popup bubble.
162 RemoveNotification("test_id3");
163 EXPECT_FALSE(tray
->message_center_tray_
->popups_visible());
166 using message_center::NotificationList
;
168 // Flaky, see http://crbug.com/222500 .
169 IN_PROC_BROWSER_TEST_F(WebNotificationTrayTest
,
170 DISABLED_ManyMessageCenterNotifications
) {
171 scoped_ptr
<WebNotificationTray
> tray(new WebNotificationTray());
172 message_center::MessageCenter
* message_center
= tray
->message_center();
174 // Add the max visible notifications +1, ensure the correct visible number.
175 size_t notifications_to_add
= kMaxVisibleMessageCenterNotifications
+ 1;
176 for (size_t i
= 0; i
< notifications_to_add
; ++i
) {
177 std::string id
= base::StringPrintf("test_id%d", static_cast<int>(i
));
178 std::string replace_id
=
179 base::StringPrintf("replace_id%d", static_cast<int>(i
));
180 AddNotification(id
, replace_id
);
182 bool shown
= tray
->message_center_tray_
->ShowMessageCenterBubble();
184 content::RunAllPendingInMessageLoop();
185 EXPECT_TRUE(tray
->message_center_delegate_
!= NULL
);
186 EXPECT_EQ(notifications_to_add
, message_center
->NotificationCount());
187 EXPECT_EQ(kMaxVisibleMessageCenterNotifications
,
188 tray
->message_center_delegate_
->NumMessageViewsForTest());
191 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA)
192 // TODO(erg): linux_aura bringup: http://crbug.com/163931
193 #define MAYBE_ManyPopupNotifications DISABLED_ManyPopupNotifications
195 #define MAYBE_ManyPopupNotifications ManyPopupNotifications
198 IN_PROC_BROWSER_TEST_F(WebNotificationTrayTest
, MAYBE_ManyPopupNotifications
) {
199 scoped_ptr
<WebNotificationTray
> tray(new WebNotificationTray());
200 message_center::MessageCenter
* message_center
= tray
->message_center();
202 // Add the max visible popup notifications +1, ensure the correct num visible.
203 size_t notifications_to_add
= kMaxVisiblePopupNotifications
+ 1;
204 for (size_t i
= 0; i
< notifications_to_add
; ++i
) {
205 std::string id
= base::StringPrintf("test_id%d", static_cast<int>(i
));
206 std::string replace_id
=
207 base::StringPrintf("replace_id%d", static_cast<int>(i
));
208 AddNotification(id
, replace_id
);
210 // Hide and reshow the bubble so that it is updated immediately, not delayed.
211 tray
->message_center_tray_
->HidePopupBubble();
212 tray
->message_center_tray_
->ShowPopupBubble();
213 EXPECT_TRUE(tray
->message_center_tray_
->popups_visible());
214 EXPECT_EQ(notifications_to_add
, message_center
->NotificationCount());
215 NotificationList::PopupNotifications popups
=
216 message_center
->GetPopupNotifications();
217 EXPECT_EQ(kMaxVisiblePopupNotifications
, popups
.size());
220 } // namespace message_center