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.
7 #include "base/command_line.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/string_util.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "chrome/browser/browser_process.h"
13 #include "chrome/browser/chrome_notification_types.h"
14 #include "chrome/browser/notifications/message_center_notification_manager.h"
15 #include "chrome/browser/notifications/notification.h"
16 #include "chrome/browser/notifications/notification_ui_manager.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/ui/browser.h"
19 #include "chrome/test/base/in_process_browser_test.h"
20 #include "chrome/test/base/test_switches.h"
21 #include "content/public/browser/notification_details.h"
22 #include "content/public/browser/notification_observer.h"
23 #include "content/public/browser/notification_source.h"
24 #include "ui/message_center/message_center.h"
25 #include "ui/message_center/message_center_switches.h"
26 #include "ui/message_center/message_center_types.h"
27 #include "ui/message_center/message_center_util.h"
29 class TestAddObserver : public message_center::MessageCenterObserver {
31 explicit TestAddObserver(message_center::MessageCenter* message_center)
32 : message_center_(message_center) {
33 message_center_->AddObserver(this);
36 virtual ~TestAddObserver() { message_center_->RemoveObserver(this); }
38 virtual void OnNotificationAdded(const std::string& id) OVERRIDE {
44 virtual void OnNotificationUpdated(const std::string& id) OVERRIDE {
47 log_ += "update-" + id;
50 const std::string log() const { return log_; }
51 void reset_log() { log_ = ""; }
55 message_center::MessageCenter* message_center_;
58 class MessageCenterNotificationsTest : public InProcessBrowserTest {
60 MessageCenterNotificationsTest() {}
62 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
63 // This switch enables the new piping of Notifications through Message
65 command_line->AppendSwitch(
66 message_center::switches::kEnableRichNotifications);
69 MessageCenterNotificationManager* manager() {
70 return static_cast<MessageCenterNotificationManager*>(
71 g_browser_process->notification_ui_manager());
74 message_center::MessageCenter* message_center() {
75 return g_browser_process->message_center();
78 Profile* profile() { return browser()->profile(); }
80 class TestDelegate : public NotificationDelegate {
82 explicit TestDelegate(const std::string& id) : id_(id) {}
84 virtual void Display() OVERRIDE { log_ += "Display_"; }
85 virtual void Error() OVERRIDE { log_ += "Error_"; }
86 virtual void Close(bool by_user) OVERRIDE {
88 log_ += ( by_user ? "by_user_" : "programmatically_");
90 virtual void Click() OVERRIDE { log_ += "Click_"; }
91 virtual void ButtonClick(int button_index) OVERRIDE {
92 log_ += "ButtonClick_";
93 log_ += base::IntToString(button_index) + "_";
95 virtual std::string id() const OVERRIDE { return id_; }
96 virtual content::RenderViewHost* GetRenderViewHost() const OVERRIDE {
100 const std::string& log() { return log_; }
103 virtual ~TestDelegate() {}
107 DISALLOW_COPY_AND_ASSIGN(TestDelegate);
110 Notification CreateTestNotification(const std::string& id,
111 TestDelegate** delegate = NULL) {
112 TestDelegate* new_delegate = new TestDelegate(id);
114 *delegate = new_delegate;
115 new_delegate->AddRef();
118 return Notification(GURL("chrome-test://testing/"),
120 ASCIIToUTF16("title"),
121 ASCIIToUTF16("message"),
122 WebKit::WebTextDirectionDefault,
123 UTF8ToUTF16("chrome-test://testing/"),
124 UTF8ToUTF16("REPLACE-ME"),
128 Notification CreateRichTestNotification(const std::string& id,
129 TestDelegate** delegate = NULL) {
130 TestDelegate* new_delegate = new TestDelegate(id);
132 *delegate = new_delegate;
133 new_delegate->AddRef();
136 message_center::RichNotificationData data;
138 return Notification(message_center::NOTIFICATION_TYPE_BASE_FORMAT,
139 GURL("chrome-test://testing/"),
140 ASCIIToUTF16("title"),
141 ASCIIToUTF16("message"),
143 WebKit::WebTextDirectionDefault,
144 message_center::NotifierId(
145 message_center::NotifierId::APPLICATION,
147 UTF8ToUTF16("chrome-test://testing/"),
148 UTF8ToUTF16("REPLACE-ME"),
154 // TODO(rsesek): Implement Message Center on Mac and get these tests passing
155 // for real. http://crbug.com/179904
156 #if !defined(OS_MACOSX)
158 IN_PROC_BROWSER_TEST_F(MessageCenterNotificationsTest, RetrieveBaseParts) {
159 // Make sure comamnd-line switch has an effect.
160 EXPECT_EQ(NotificationUIManager::DelegatesToMessageCenter(),
161 message_center::IsRichNotificationEnabled());
162 EXPECT_TRUE(manager());
163 EXPECT_TRUE(message_center());
166 // MessaceCenter-specific test.
167 #if defined(RUN_MESSAGE_CENTER_TESTS)
168 #define MAYBE_BasicAddCancel BasicAddCancel
170 #define MAYBE_BasicAddCancel DISABLED_BasicAddCancel
173 IN_PROC_BROWSER_TEST_F(MessageCenterNotificationsTest, MAYBE_BasicAddCancel) {
174 #if defined(OS_WIN) && defined(USE_ASH)
175 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
176 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
180 EXPECT_TRUE(NotificationUIManager::DelegatesToMessageCenter());
181 manager()->Add(CreateTestNotification("hey"), profile());
182 EXPECT_EQ(1u, message_center()->NotificationCount());
183 manager()->CancelById("hey");
184 EXPECT_EQ(0u, message_center()->NotificationCount());
187 // MessaceCenter-specific test.
188 #if defined(RUN_MESSAGE_CENTER_TESTS)
189 #define MAYBE_BasicDelegate BasicDelegate
191 #define MAYBE_BasicDelegate DISABLED_BasicDelegate
194 IN_PROC_BROWSER_TEST_F(MessageCenterNotificationsTest, MAYBE_BasicDelegate) {
195 #if defined(OS_WIN) && defined(USE_ASH)
196 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
197 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
201 EXPECT_TRUE(NotificationUIManager::DelegatesToMessageCenter());
202 TestDelegate* delegate;
203 manager()->Add(CreateTestNotification("hey", &delegate), profile());
204 // Verify that delegate accumulated correct log of events.
205 EXPECT_EQ("Display_", delegate->log());
206 manager()->CancelById("hey");
207 // Verify that delegate accumulated correct log of events.
208 EXPECT_EQ("Display_Close_programmatically_", delegate->log());
212 // MessaceCenter-specific test.
213 #if defined(RUN_MESSAGE_CENTER_TESTS)
214 #define MAYBE_ButtonClickedDelegate ButtonClickedDelegate
216 #define MAYBE_ButtonClickedDelegate DISABLED_ButtonClickedDelegate
219 IN_PROC_BROWSER_TEST_F(MessageCenterNotificationsTest,
220 MAYBE_ButtonClickedDelegate) {
221 #if defined(OS_WIN) && defined(USE_ASH)
222 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
223 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
227 EXPECT_TRUE(NotificationUIManager::DelegatesToMessageCenter());
228 TestDelegate* delegate;
229 manager()->Add(CreateTestNotification("n", &delegate), profile());
230 message_center()->ClickOnNotificationButton("n", 1);
231 // Verify that delegate accumulated correct log of events.
232 EXPECT_EQ("Display_ButtonClick_1_", delegate->log());
236 // MessaceCenter-specific test.
237 #if defined(RUN_MESSAGE_CENTER_TESTS)
238 #define MAYBE_UpdateExistingNotification UpdateExistingNotification
240 #define MAYBE_UpdateExistingNotification DISABLED_UpdateExistingNotification
243 IN_PROC_BROWSER_TEST_F(MessageCenterNotificationsTest,
244 MAYBE_UpdateExistingNotification) {
245 #if defined(OS_WIN) && defined(USE_ASH)
246 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
247 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
251 EXPECT_TRUE(NotificationUIManager::DelegatesToMessageCenter());
252 TestDelegate* delegate;
253 manager()->Add(CreateTestNotification("n", &delegate), profile());
254 TestDelegate* delegate2;
255 manager()->Add(CreateRichTestNotification("n", &delegate2), profile());
257 manager()->CancelById("n");
258 EXPECT_EQ("Display_", delegate->log());
259 EXPECT_EQ("Close_programmatically_", delegate2->log());
262 delegate2->Release();
265 // MessaceCenter-specific test.
266 #if defined(RUN_MESSAGE_CENTER_TESTS)
267 #define MAYBE_QueueWhenCenterVisible QueueWhenCenterVisible
269 #define MAYBE_QueueWhenCenterVisible DISABLED_QueueWhenCenterVisible
272 IN_PROC_BROWSER_TEST_F(MessageCenterNotificationsTest,
273 MAYBE_QueueWhenCenterVisible) {
274 #if defined(OS_WIN) && defined(USE_ASH)
275 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
276 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
280 EXPECT_TRUE(NotificationUIManager::DelegatesToMessageCenter());
281 TestAddObserver observer(message_center());
283 TestDelegate* delegate;
284 TestDelegate* delegate2;
286 manager()->Add(CreateTestNotification("n", &delegate), profile());
287 message_center()->SetVisibility(message_center::VISIBILITY_MESSAGE_CENTER);
288 manager()->Add(CreateTestNotification("n2", &delegate2), profile());
290 EXPECT_EQ("add-n", observer.log());
292 message_center()->SetVisibility(message_center::VISIBILITY_TRANSIENT);
294 EXPECT_EQ("add-n_add-n2", observer.log());
297 delegate2->Release();
300 // MessaceCenter-specific test.
301 #if defined(RUN_MESSAGE_CENTER_TESTS)
302 #define MAYBE_UpdateNonProgressNotificationWhenCenterVisible \
303 UpdateNonProgressNotificationWhenCenterVisible
305 #define MAYBE_UpdateNonProgressNotificationWhenCenterVisible \
306 DISABLED_UpdateNonProgressNotificationWhenCenterVisible
309 IN_PROC_BROWSER_TEST_F(MessageCenterNotificationsTest,
310 MAYBE_UpdateNonProgressNotificationWhenCenterVisible) {
311 #if defined(OS_WIN) && defined(USE_ASH)
312 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
313 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
317 EXPECT_TRUE(NotificationUIManager::DelegatesToMessageCenter());
318 TestAddObserver observer(message_center());
320 TestDelegate* delegate;
322 // Add a non-progress notification and update it while the message center
324 Notification notification = CreateTestNotification("n", &delegate);
325 manager()->Add(notification, profile());
326 message_center()->ClickOnNotification("n");
327 message_center()->SetVisibility(message_center::VISIBILITY_MESSAGE_CENTER);
328 observer.reset_log();
329 notification.set_title(ASCIIToUTF16("title2"));
330 manager()->Update(notification, profile());
332 // Expect that the notification update is not done.
333 EXPECT_EQ("", observer.log());
335 message_center()->SetVisibility(message_center::VISIBILITY_TRANSIENT);
336 EXPECT_EQ("update-n", observer.log());
341 // MessaceCenter-specific test.
342 #if defined(RUN_MESSAGE_CENTER_TESTS)
343 #define MAYBE_UpdateNonProgressToProgressNotificationWhenCenterVisible \
344 UpdateNonProgressToProgressNotificationWhenCenterVisible
346 #define MAYBE_UpdateNonProgressToProgressNotificationWhenCenterVisible \
347 DISABLED_UpdateNonProgressToProgressNotificationWhenCenterVisible
350 IN_PROC_BROWSER_TEST_F(
351 MessageCenterNotificationsTest,
352 MAYBE_UpdateNonProgressToProgressNotificationWhenCenterVisible) {
353 #if defined(OS_WIN) && defined(USE_ASH)
354 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
355 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
359 EXPECT_TRUE(NotificationUIManager::DelegatesToMessageCenter());
360 TestAddObserver observer(message_center());
362 TestDelegate* delegate;
364 // Add a non-progress notification and change the type to progress while the
365 // message center is visible.
366 Notification notification = CreateTestNotification("n", &delegate);
367 manager()->Add(notification, profile());
368 message_center()->ClickOnNotification("n");
369 message_center()->SetVisibility(message_center::VISIBILITY_MESSAGE_CENTER);
370 observer.reset_log();
371 notification.set_type(message_center::NOTIFICATION_TYPE_PROGRESS);
372 manager()->Update(notification, profile());
374 // Expect that the notification update is not done.
375 EXPECT_EQ("", observer.log());
377 message_center()->SetVisibility(message_center::VISIBILITY_TRANSIENT);
378 EXPECT_EQ("update-n", observer.log());
383 // MessaceCenter-specific test.
384 #if defined(RUN_MESSAGE_CENTER_TESTS)
385 #define MAYBE_UpdateProgressNotificationWhenCenterVisible \
386 UpdateProgressNotificationWhenCenterVisible
388 #define MAYBE_UpdateProgressNotificationWhenCenterVisible \
389 DISABLED_UpdateProgressNotificationWhenCenterVisible
392 IN_PROC_BROWSER_TEST_F(MessageCenterNotificationsTest,
393 MAYBE_UpdateProgressNotificationWhenCenterVisible) {
394 #if defined(OS_WIN) && defined(USE_ASH)
395 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
396 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
400 EXPECT_TRUE(NotificationUIManager::DelegatesToMessageCenter());
401 TestAddObserver observer(message_center());
403 TestDelegate* delegate;
405 // Add a progress notification and update it while the message center
407 Notification notification = CreateTestNotification("n", &delegate);
408 notification.set_type(message_center::NOTIFICATION_TYPE_PROGRESS);
409 manager()->Add(notification, profile());
410 message_center()->ClickOnNotification("n");
411 message_center()->SetVisibility(message_center::VISIBILITY_MESSAGE_CENTER);
412 observer.reset_log();
413 notification.set_progress(50);
414 manager()->Update(notification, profile());
416 // Expect that the progress notification update is performed.
417 EXPECT_EQ("update-n", observer.log());
422 #if !defined(OS_CHROMEOS) && defined(RUN_MESSAGE_CENTER_TESTS)
423 #define MAYBE_HideWhenFullscreenEnabled HideWhenFullscreenEnabled
425 #define MAYBE_HideWhenFullscreenEnabled DISABLED_HideWhenFullscreenEnabled
428 IN_PROC_BROWSER_TEST_F(MessageCenterNotificationsTest,
429 MAYBE_HideWhenFullscreenEnabled) {
430 EXPECT_TRUE(NotificationUIManager::DelegatesToMessageCenter());
432 TestDelegate* delegate;
433 manager()->Add(CreateTestNotification("n", &delegate), profile());
435 EXPECT_EQ("Display_", delegate->log());
436 EXPECT_TRUE(message_center()->HasPopupNotifications());
437 bool is_fullscreen = true;
438 // Cast so that Observe() is public.
439 content::NotificationObserver* observer =
440 static_cast<content::NotificationObserver*>(manager());
441 observer->Observe(chrome::NOTIFICATION_FULLSCREEN_CHANGED,
442 content::Source<Profile>(profile()),
443 content::Details<bool>(&is_fullscreen));
444 EXPECT_FALSE(message_center()->HasPopupNotifications());
447 #endif // !defined(OS_MACOSX)