1 // Copyright (c) 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.
8 #include "base/command_line.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/string_util.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_ui_manager.h"
18 #include "chrome/browser/profiles/profile.h"
19 #include "chrome/browser/ui/browser.h"
20 #include "chrome/test/base/in_process_browser_test.h"
21 #include "chrome/test/base/test_switches.h"
22 #include "ui/message_center/message_center.h"
23 #include "ui/message_center/message_center_types.h"
25 class TestAddObserver
: public message_center::MessageCenterObserver
{
27 explicit TestAddObserver(message_center::MessageCenter
* message_center
)
28 : message_center_(message_center
) {
29 message_center_
->AddObserver(this);
32 virtual ~TestAddObserver() { message_center_
->RemoveObserver(this); }
34 virtual void OnNotificationAdded(const std::string
& id
) override
{
35 std::string log
= logs_
[id
];
38 logs_
[id
] = log
+ "add-" + id
;
41 virtual void OnNotificationUpdated(const std::string
& id
) override
{
42 std::string log
= logs_
[id
];
45 logs_
[id
] = log
+ "update-" + id
;
48 const std::string
log(const std::string
& id
) { return logs_
[id
]; }
49 void reset_logs() { logs_
.clear(); }
52 std::map
<std::string
, std::string
> logs_
;
53 message_center::MessageCenter
* message_center_
;
56 class MessageCenterNotificationsTest
: public InProcessBrowserTest
{
58 MessageCenterNotificationsTest() {}
60 MessageCenterNotificationManager
* manager() {
61 return static_cast<MessageCenterNotificationManager
*>(
62 g_browser_process
->notification_ui_manager());
65 message_center::MessageCenter
* message_center() {
66 return g_browser_process
->message_center();
69 Profile
* profile() { return browser()->profile(); }
71 class TestDelegate
: public NotificationDelegate
{
73 explicit TestDelegate(const std::string
& id
) : id_(id
) {}
75 virtual void Display() override
{ log_
+= "Display_"; }
76 virtual void Error() override
{ log_
+= "Error_"; }
77 virtual void Close(bool by_user
) override
{
79 log_
+= ( by_user
? "by_user_" : "programmatically_");
81 virtual void Click() override
{ log_
+= "Click_"; }
82 virtual void ButtonClick(int button_index
) override
{
83 log_
+= "ButtonClick_";
84 log_
+= base::IntToString(button_index
) + "_";
86 virtual std::string
id() const override
{ return id_
; }
88 const std::string
& log() { return log_
; }
91 virtual ~TestDelegate() {}
95 DISALLOW_COPY_AND_ASSIGN(TestDelegate
);
98 Notification
CreateTestNotification(const std::string
& delegate_id
,
99 TestDelegate
** delegate
= NULL
) {
100 TestDelegate
* new_delegate
= new TestDelegate(delegate_id
);
102 *delegate
= new_delegate
;
103 new_delegate
->AddRef();
106 return Notification(GURL("chrome-test://testing/"),
107 base::ASCIIToUTF16("title"),
108 base::ASCIIToUTF16("message"),
110 base::UTF8ToUTF16("chrome-test://testing/"),
111 base::UTF8ToUTF16("REPLACE-ME"),
115 Notification
CreateRichTestNotification(const std::string
& id
,
116 TestDelegate
** delegate
= NULL
) {
117 TestDelegate
* new_delegate
= new TestDelegate(id
);
119 *delegate
= new_delegate
;
120 new_delegate
->AddRef();
123 message_center::RichNotificationData data
;
125 return Notification(message_center::NOTIFICATION_TYPE_BASE_FORMAT
,
126 GURL("chrome-test://testing/"),
127 base::ASCIIToUTF16("title"),
128 base::ASCIIToUTF16("message"),
130 blink::WebTextDirectionDefault
,
131 message_center::NotifierId(
132 message_center::NotifierId::APPLICATION
,
134 base::UTF8ToUTF16("chrome-test://testing/"),
135 base::UTF8ToUTF16("REPLACE-ME"),
141 // TODO(rsesek): Implement Message Center on Mac and get these tests passing
142 // for real. http://crbug.com/179904
143 #if !defined(OS_MACOSX)
145 IN_PROC_BROWSER_TEST_F(MessageCenterNotificationsTest
, RetrieveBaseParts
) {
146 EXPECT_TRUE(manager());
147 EXPECT_TRUE(message_center());
150 IN_PROC_BROWSER_TEST_F(MessageCenterNotificationsTest
, BasicAddCancel
) {
151 #if defined(OS_WIN) && defined(USE_ASH)
152 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
153 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests
))
157 // Someone may create system notifications like "you're in multi-profile
158 // mode..." or something which may change the expectation.
159 // TODO(mukai): move this to SetUpOnMainThread() after fixing the side-effect
160 // of canceling animation which prevents some Displayed() event.
161 manager()->CancelAll();
162 manager()->Add(CreateTestNotification("hey"), profile());
163 EXPECT_EQ(1u, message_center()->NotificationCount());
164 manager()->CancelById("hey", NotificationUIManager::GetProfileID(profile()));
165 EXPECT_EQ(0u, message_center()->NotificationCount());
168 IN_PROC_BROWSER_TEST_F(MessageCenterNotificationsTest
, BasicDelegate
) {
169 #if defined(OS_WIN) && defined(USE_ASH)
170 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
171 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests
))
175 TestDelegate
* delegate
;
176 manager()->Add(CreateTestNotification("hey", &delegate
), profile());
177 // Verify that delegate accumulated correct log of events.
178 EXPECT_EQ("Display_", delegate
->log());
179 manager()->CancelById("hey", NotificationUIManager::GetProfileID(profile()));
180 // Verify that delegate accumulated correct log of events.
181 EXPECT_EQ("Display_Close_programmatically_", delegate
->log());
185 IN_PROC_BROWSER_TEST_F(MessageCenterNotificationsTest
, ButtonClickedDelegate
) {
186 #if defined(OS_WIN) && defined(USE_ASH)
187 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
188 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests
))
192 TestDelegate
* delegate
;
193 manager()->Add(CreateTestNotification("n", &delegate
), profile());
194 const std::string notification_id
=
195 manager()->GetMessageCenterNotificationIdForTest("n", profile());
196 message_center()->ClickOnNotificationButton(notification_id
, 1);
197 // Verify that delegate accumulated correct log of events.
198 EXPECT_EQ("Display_ButtonClick_1_", delegate
->log());
202 IN_PROC_BROWSER_TEST_F(MessageCenterNotificationsTest
,
203 UpdateExistingNotification
) {
204 #if defined(OS_WIN) && defined(USE_ASH)
205 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
206 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests
))
210 TestDelegate
* delegate
;
211 manager()->Add(CreateTestNotification("n", &delegate
), profile());
212 TestDelegate
* delegate2
;
213 manager()->Add(CreateRichTestNotification("n", &delegate2
), profile());
215 manager()->CancelById("n", NotificationUIManager::GetProfileID(profile()));
216 EXPECT_EQ("Display_", delegate
->log());
217 EXPECT_EQ("Close_programmatically_", delegate2
->log());
220 delegate2
->Release();
223 IN_PROC_BROWSER_TEST_F(MessageCenterNotificationsTest
, QueueWhenCenterVisible
) {
224 #if defined(OS_WIN) && defined(USE_ASH)
225 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
226 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests
))
230 TestAddObserver
observer(message_center());
232 TestDelegate
* delegate
;
233 TestDelegate
* delegate2
;
235 manager()->Add(CreateTestNotification("n", &delegate
), profile());
236 const std::string id_n
=
237 manager()->GetMessageCenterNotificationIdForTest("n", profile());
238 message_center()->SetVisibility(message_center::VISIBILITY_MESSAGE_CENTER
);
239 manager()->Add(CreateTestNotification("n2", &delegate2
), profile());
240 const std::string id_n2
=
241 manager()->GetMessageCenterNotificationIdForTest("n2", profile());
243 // 'update-n' should happen since SetVisibility updates is_read status of n.
244 // TODO(mukai): fix event handling to happen update-n just once.
245 EXPECT_EQ(base::StringPrintf("add-%s_update-%s_update-%s",
251 message_center()->SetVisibility(message_center::VISIBILITY_TRANSIENT
);
253 EXPECT_EQ(base::StringPrintf("add-%s", id_n2
.c_str()), observer
.log(id_n2
));
256 delegate2
->Release();
259 IN_PROC_BROWSER_TEST_F(MessageCenterNotificationsTest
,
260 UpdateNonProgressNotificationWhenCenterVisible
) {
261 #if defined(OS_WIN) && defined(USE_ASH)
262 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
263 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests
))
267 TestAddObserver
observer(message_center());
269 TestDelegate
* delegate
;
271 // Add a non-progress notification and update it while the message center
273 Notification notification
= CreateTestNotification("n", &delegate
);
274 manager()->Add(notification
, profile());
275 const std::string notification_id
=
276 manager()->GetMessageCenterNotificationIdForTest("n", profile());
277 message_center()->ClickOnNotification(notification_id
);
278 message_center()->SetVisibility(message_center::VISIBILITY_MESSAGE_CENTER
);
279 observer
.reset_logs();
280 notification
.set_title(base::ASCIIToUTF16("title2"));
281 manager()->Update(notification
, profile());
283 // Expect that the notification update is not done.
284 EXPECT_EQ("", observer
.log(notification_id
));
286 message_center()->SetVisibility(message_center::VISIBILITY_TRANSIENT
);
287 EXPECT_EQ(base::StringPrintf("update-%s", notification_id
.c_str()),
288 observer
.log(notification_id
));
293 IN_PROC_BROWSER_TEST_F(
294 MessageCenterNotificationsTest
,
295 UpdateNonProgressToProgressNotificationWhenCenterVisible
) {
296 #if defined(OS_WIN) && defined(USE_ASH)
297 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
298 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests
))
302 TestAddObserver
observer(message_center());
304 TestDelegate
* delegate
;
306 // Add a non-progress notification and change the type to progress while the
307 // message center is visible.
308 Notification notification
= CreateTestNotification("n", &delegate
);
309 manager()->Add(notification
, profile());
310 const std::string notification_id
=
311 manager()->GetMessageCenterNotificationIdForTest("n", profile());
312 message_center()->ClickOnNotification(notification_id
);
313 message_center()->SetVisibility(message_center::VISIBILITY_MESSAGE_CENTER
);
314 observer
.reset_logs();
315 notification
.set_type(message_center::NOTIFICATION_TYPE_PROGRESS
);
316 manager()->Update(notification
, profile());
318 // Expect that the notification update is not done.
319 EXPECT_EQ("", observer
.log(notification_id
));
321 message_center()->SetVisibility(message_center::VISIBILITY_TRANSIENT
);
322 EXPECT_EQ(base::StringPrintf("update-%s", notification_id
.c_str()),
323 observer
.log(notification_id
));
328 IN_PROC_BROWSER_TEST_F(MessageCenterNotificationsTest
,
329 UpdateProgressNotificationWhenCenterVisible
) {
330 #if defined(OS_WIN) && defined(USE_ASH)
331 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
332 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests
))
336 TestAddObserver
observer(message_center());
338 TestDelegate
* delegate
;
340 // Add a progress notification and update it while the message center
342 Notification notification
= CreateTestNotification("n", &delegate
);
343 notification
.set_type(message_center::NOTIFICATION_TYPE_PROGRESS
);
344 manager()->Add(notification
, profile());
345 const std::string notification_id
=
346 manager()->GetMessageCenterNotificationIdForTest("n", profile());
347 message_center()->ClickOnNotification(notification_id
);
348 message_center()->SetVisibility(message_center::VISIBILITY_MESSAGE_CENTER
);
349 observer
.reset_logs();
350 notification
.set_progress(50);
351 manager()->Update(notification
, profile());
353 // Expect that the progress notification update is performed.
354 EXPECT_EQ(base::StringPrintf("update-%s", notification_id
.c_str()),
355 observer
.log(notification_id
));
360 #endif // !defined(OS_MACOSX)