Upstream version 8.37.180.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / notifications / notification_browsertest.cc
1 // Copyright (c) 2012 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 <deque>
6 #include <string>
7
8 #include "base/bind.h"
9 #include "base/callback.h"
10 #include "base/command_line.h"
11 #include "base/compiler_specific.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/run_loop.h"
14 #include "base/strings/stringprintf.h"
15 #include "base/strings/utf_string_conversions.h"
16 #include "chrome/browser/browser_process.h"
17 #include "chrome/browser/chrome_notification_types.h"
18 #include "chrome/browser/infobars/infobar_service.h"
19 #include "chrome/browser/notifications/desktop_notification_service.h"
20 #include "chrome/browser/notifications/desktop_notification_service_factory.h"
21 #include "chrome/browser/notifications/notification.h"
22 #include "chrome/browser/profiles/profile.h"
23 #include "chrome/browser/ui/browser.h"
24 #include "chrome/browser/ui/browser_tabstrip.h"
25 #include "chrome/browser/ui/browser_window.h"
26 #include "chrome/browser/ui/tabs/tab_strip_model.h"
27 #include "chrome/common/content_settings.h"
28 #include "chrome/common/content_settings_pattern.h"
29 #include "chrome/test/base/in_process_browser_test.h"
30 #include "chrome/test/base/ui_test_utils.h"
31 #include "components/infobars/core/confirm_infobar_delegate.h"
32 #include "components/infobars/core/infobar.h"
33 #include "content/public/browser/notification_service.h"
34 #include "content/public/browser/notification_source.h"
35 #include "content/public/browser/notification_types.h"
36 #include "content/public/browser/render_view_host.h"
37 #include "content/public/browser/web_contents.h"
38 #include "content/public/test/browser_test_utils.h"
39 #include "content/public/test/test_utils.h"
40 #include "net/base/net_util.h"
41 #include "net/test/embedded_test_server/embedded_test_server.h"
42 #include "testing/gtest/include/gtest/gtest.h"
43 #include "ui/base/window_open_disposition.h"
44 #include "ui/message_center/message_center.h"
45 #include "ui/message_center/message_center_observer.h"
46 #include "url/gurl.h"
47
48 namespace {
49
50 const char kExpectedIconUrl[] = "/notifications/no_such_file.png";
51
52 enum InfobarAction {
53   DISMISS = 0,
54   ALLOW,
55   DENY,
56 };
57
58 class NotificationChangeObserver {
59 public:
60   virtual ~NotificationChangeObserver() {}
61   virtual bool Wait() = 0;
62 };
63
64 class MessageCenterChangeObserver
65     : public message_center::MessageCenterObserver,
66       public NotificationChangeObserver {
67  public:
68   MessageCenterChangeObserver()
69       : notification_received_(false) {
70     message_center::MessageCenter::Get()->AddObserver(this);
71   }
72
73   virtual ~MessageCenterChangeObserver() {
74     message_center::MessageCenter::Get()->RemoveObserver(this);
75   }
76
77   // NotificationChangeObserver:
78   virtual bool Wait() OVERRIDE {
79     if (notification_received_)
80       return true;
81
82     message_loop_runner_ = new content::MessageLoopRunner;
83     message_loop_runner_->Run();
84     return notification_received_;
85   }
86
87   // message_center::MessageCenterObserver:
88   virtual void OnNotificationAdded(
89       const std::string& notification_id) OVERRIDE {
90     OnMessageCenterChanged();
91   }
92
93   virtual void OnNotificationRemoved(const std::string& notification_id,
94                                      bool by_user) OVERRIDE {
95     OnMessageCenterChanged();
96   }
97
98   virtual void OnNotificationUpdated(
99       const std::string& notification_id) OVERRIDE {
100     OnMessageCenterChanged();
101   }
102
103   void OnMessageCenterChanged() {
104     notification_received_ = true;
105     if (message_loop_runner_.get())
106       message_loop_runner_->Quit();
107   }
108
109   bool notification_received_;
110   scoped_refptr<content::MessageLoopRunner> message_loop_runner_;
111
112   DISALLOW_COPY_AND_ASSIGN(MessageCenterChangeObserver);
113 };
114
115 }  // namespace
116
117 class NotificationsTest : public InProcessBrowserTest {
118  public:
119   NotificationsTest() {}
120
121  protected:
122   int GetNotificationCount();
123
124   void CloseBrowserWindow(Browser* browser);
125   void CrashTab(Browser* browser, int index);
126
127   void SetDefaultPermissionSetting(ContentSetting setting);
128   void DenyOrigin(const GURL& origin);
129   void AllowOrigin(const GURL& origin);
130   void AllowAllOrigins();
131
132   void VerifyInfoBar(const Browser* browser, int index);
133   std::string CreateNotification(Browser* browser,
134                                  bool wait_for_new_balloon,
135                                  const char* icon,
136                                  const char* title,
137                                  const char* body,
138                                  const char* replace_id);
139   std::string CreateSimpleNotification(Browser* browser,
140                                        bool wait_for_new_balloon);
141   bool RequestPermissionAndWait(Browser* browser);
142   bool CancelNotification(const char* notification_id, Browser* browser);
143   bool PerformActionOnInfoBar(Browser* browser,
144                               InfobarAction action,
145                               size_t infobar_index,
146                               int tab_index);
147   void GetPrefsByContentSetting(ContentSetting setting,
148                                 ContentSettingsForOneType* settings);
149   bool CheckOriginInSetting(const ContentSettingsForOneType& settings,
150                             const GURL& origin);
151
152   GURL GetTestPageURL() const {
153     return embedded_test_server()->GetURL(
154       "/notifications/notification_tester.html");
155   }
156
157  private:
158   void DropOriginPreference(const GURL& origin);
159   DesktopNotificationService* GetDesktopNotificationService();
160 };
161
162 int NotificationsTest::GetNotificationCount() {
163   return message_center::MessageCenter::Get()->NotificationCount();
164 }
165
166 void NotificationsTest::CloseBrowserWindow(Browser* browser) {
167   content::WindowedNotificationObserver observer(
168       chrome::NOTIFICATION_BROWSER_CLOSED,
169       content::Source<Browser>(browser));
170   browser->window()->Close();
171   observer.Wait();
172 }
173
174 void NotificationsTest::CrashTab(Browser* browser, int index) {
175   content::CrashTab(browser->tab_strip_model()->GetWebContentsAt(index));
176 }
177
178 void NotificationsTest::SetDefaultPermissionSetting(ContentSetting setting) {
179   DesktopNotificationService* service = GetDesktopNotificationService();
180   service->SetDefaultContentSetting(setting);
181 }
182
183 void NotificationsTest::DenyOrigin(const GURL& origin) {
184   DropOriginPreference(origin);
185   GetDesktopNotificationService()->DenyPermission(origin);
186 }
187
188 void NotificationsTest::AllowOrigin(const GURL& origin) {
189   DropOriginPreference(origin);
190   GetDesktopNotificationService()->GrantPermission(origin);
191 }
192
193 void NotificationsTest::AllowAllOrigins() {
194   GetDesktopNotificationService()->ResetAllOrigins();
195   GetDesktopNotificationService()->SetDefaultContentSetting(
196       CONTENT_SETTING_ALLOW);
197 }
198
199 void NotificationsTest::VerifyInfoBar(const Browser* browser, int index) {
200   InfoBarService* infobar_service = InfoBarService::FromWebContents(
201       browser->tab_strip_model()->GetWebContentsAt(index));
202
203   ASSERT_EQ(1U, infobar_service->infobar_count());
204   ConfirmInfoBarDelegate* confirm_infobar =
205       infobar_service->infobar_at(0)->delegate()->AsConfirmInfoBarDelegate();
206   ASSERT_TRUE(confirm_infobar);
207   int buttons = confirm_infobar->GetButtons();
208   EXPECT_TRUE(buttons & ConfirmInfoBarDelegate::BUTTON_OK);
209   EXPECT_TRUE(buttons & ConfirmInfoBarDelegate::BUTTON_CANCEL);
210 }
211
212 std::string NotificationsTest::CreateNotification(
213     Browser* browser,
214     bool wait_for_new_balloon,
215     const char* icon,
216     const char* title,
217     const char* body,
218     const char* replace_id) {
219   std::string script = base::StringPrintf(
220       "createNotification('%s', '%s', '%s', '%s');",
221       icon, title, body, replace_id);
222
223   MessageCenterChangeObserver observer;
224   std::string result;
225   bool success = content::ExecuteScriptAndExtractString(
226       browser->tab_strip_model()->GetActiveWebContents(),
227       script,
228       &result);
229   if (success && result != "-1" && wait_for_new_balloon)
230     success = observer.Wait();
231   EXPECT_TRUE(success);
232
233   return result;
234 }
235
236 std::string NotificationsTest::CreateSimpleNotification(
237     Browser* browser,
238     bool wait_for_new_balloon) {
239   return CreateNotification(
240       browser, wait_for_new_balloon,
241       "no_such_file.png", "My Title", "My Body", "");
242 }
243
244 bool NotificationsTest::RequestPermissionAndWait(Browser* browser) {
245   InfoBarService* infobar_service = InfoBarService::FromWebContents(
246       browser->tab_strip_model()->GetActiveWebContents());
247   content::WindowedNotificationObserver observer(
248       chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED,
249       content::Source<InfoBarService>(infobar_service));
250   std::string result;
251   bool success = content::ExecuteScriptAndExtractString(
252       browser->tab_strip_model()->GetActiveWebContents(),
253       "requestPermission();",
254       &result);
255   if (!success || result != "1")
256     return false;
257   observer.Wait();
258   return true;
259 }
260
261 bool NotificationsTest::CancelNotification(
262     const char* notification_id,
263     Browser* browser) {
264   std::string script = base::StringPrintf(
265       "cancelNotification('%s');",
266       notification_id);
267
268   MessageCenterChangeObserver observer;
269   std::string result;
270   bool success = content::ExecuteScriptAndExtractString(
271       browser->tab_strip_model()->GetActiveWebContents(),
272       script,
273       &result);
274   if (!success || result != "1")
275     return false;
276   return observer.Wait();
277 }
278
279 bool NotificationsTest::PerformActionOnInfoBar(
280     Browser* browser,
281     InfobarAction action,
282     size_t infobar_index,
283     int tab_index) {
284   InfoBarService* infobar_service = InfoBarService::FromWebContents(
285       browser->tab_strip_model()->GetWebContentsAt(tab_index));
286   if (infobar_index >= infobar_service->infobar_count()) {
287     ADD_FAILURE();
288     return false;
289   }
290
291   infobars::InfoBar* infobar = infobar_service->infobar_at(infobar_index);
292   infobars::InfoBarDelegate* infobar_delegate = infobar->delegate();
293   switch (action) {
294     case DISMISS:
295       infobar_delegate->InfoBarDismissed();
296       infobar_service->RemoveInfoBar(infobar);
297       return true;
298
299     case ALLOW: {
300       ConfirmInfoBarDelegate* confirm_infobar_delegate =
301           infobar_delegate->AsConfirmInfoBarDelegate();
302       if (!confirm_infobar_delegate) {
303         ADD_FAILURE();
304       } else if (confirm_infobar_delegate->Accept()) {
305         infobar_service->RemoveInfoBar(infobar);
306         return true;
307       }
308     }
309
310     case DENY: {
311       ConfirmInfoBarDelegate* confirm_infobar_delegate =
312           infobar_delegate->AsConfirmInfoBarDelegate();
313       if (!confirm_infobar_delegate) {
314         ADD_FAILURE();
315       } else if (confirm_infobar_delegate->Cancel()) {
316         infobar_service->RemoveInfoBar(infobar);
317         return true;
318       }
319     }
320   }
321
322   return false;
323 }
324
325 void NotificationsTest::GetPrefsByContentSetting(
326     ContentSetting setting,
327     ContentSettingsForOneType* settings) {
328   DesktopNotificationService* service = GetDesktopNotificationService();
329   service->GetNotificationsSettings(settings);
330   for (ContentSettingsForOneType::iterator it = settings->begin();
331        it != settings->end(); ) {
332     if (it->setting != setting || it->source.compare("preference") != 0)
333       it = settings->erase(it);
334     else
335       ++it;
336   }
337 }
338
339 bool NotificationsTest::CheckOriginInSetting(
340     const ContentSettingsForOneType& settings,
341     const GURL& origin) {
342   ContentSettingsPattern pattern =
343       ContentSettingsPattern::FromURLNoWildcard(origin);
344   for (ContentSettingsForOneType::const_iterator it = settings.begin();
345        it != settings.end(); ++it) {
346     if (it->primary_pattern == pattern)
347       return true;
348   }
349   return false;
350 }
351
352 void NotificationsTest::DropOriginPreference(const GURL& origin) {
353   GetDesktopNotificationService()->ClearSetting(
354       ContentSettingsPattern::FromURLNoWildcard(origin));
355 }
356
357 DesktopNotificationService* NotificationsTest::GetDesktopNotificationService() {
358   Profile* profile = browser()->profile();
359   return DesktopNotificationServiceFactory::GetForProfile(profile);
360 }
361
362 // If this flakes, use http://crbug.com/62311 and http://crbug.com/74428.
363 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestUserGestureInfobar) {
364   ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
365
366   ui_test_utils::NavigateToURL(
367       browser(),
368       embedded_test_server()->GetURL(
369           "/notifications/notifications_request_function.html"));
370
371   // Request permission by calling request() while eval'ing an inline script;
372   // That's considered a user gesture to webkit, and should produce an infobar.
373   bool result;
374   ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
375       browser()->tab_strip_model()->GetActiveWebContents(),
376       "window.domAutomationController.send(request());",
377       &result));
378   EXPECT_TRUE(result);
379
380   InfoBarService* infobar_service = InfoBarService::FromWebContents(
381       browser()->tab_strip_model()->GetWebContentsAt(0));
382   EXPECT_EQ(1U, infobar_service->infobar_count());
383 }
384
385 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestCreateSimpleNotification) {
386   ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
387
388   // Creates a simple notification.
389   AllowAllOrigins();
390   ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
391
392   std::string result = CreateSimpleNotification(browser(), true);
393   EXPECT_NE("-1", result);
394
395   GURL EXPECTED_ICON_URL = embedded_test_server()->GetURL(kExpectedIconUrl);
396   ASSERT_EQ(1, GetNotificationCount());
397   message_center::NotificationList::Notifications notifications =
398       message_center::MessageCenter::Get()->GetVisibleNotifications();
399   EXPECT_EQ(base::ASCIIToUTF16("My Title"),
400             (*notifications.rbegin())->title());
401   EXPECT_EQ(base::ASCIIToUTF16("My Body"),
402             (*notifications.rbegin())->message());
403 }
404
405 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestCloseNotification) {
406   ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
407
408   // Creates a notification and closes it.
409   AllowAllOrigins();
410   ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
411
412   std::string result = CreateSimpleNotification(browser(), true);
413   EXPECT_NE("-1", result);
414   ASSERT_EQ(1, GetNotificationCount());
415
416   message_center::NotificationList::Notifications notifications =
417       message_center::MessageCenter::Get()->GetVisibleNotifications();
418   message_center::MessageCenter::Get()->RemoveNotification(
419       (*notifications.rbegin())->id(),
420       true);  // by_user
421
422   ASSERT_EQ(0, GetNotificationCount());
423 }
424
425 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestCancelNotification) {
426   ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
427
428   // Creates a notification and cancels it in the origin page.
429   AllowAllOrigins();
430   ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
431
432   std::string note_id = CreateSimpleNotification(browser(), true);
433   EXPECT_NE(note_id, "-1");
434
435   ASSERT_EQ(1, GetNotificationCount());
436   ASSERT_TRUE(CancelNotification(note_id.c_str(), browser()));
437   ASSERT_EQ(0, GetNotificationCount());
438 }
439
440 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestPermissionInfobarAppears) {
441   ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
442
443   // Requests notification privileges and verifies the infobar appears.
444   ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
445   ASSERT_TRUE(RequestPermissionAndWait(browser()));
446
447   ASSERT_EQ(0, GetNotificationCount());
448   ASSERT_NO_FATAL_FAILURE(VerifyInfoBar(browser(), 0));
449 }
450
451 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestAllowOnPermissionInfobar) {
452   ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
453
454   // Tries to create a notification and clicks allow on the infobar.
455   ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
456   // This notification should not be shown because we do not have permission.
457   CreateSimpleNotification(browser(), false);
458   ASSERT_EQ(0, GetNotificationCount());
459
460   ASSERT_TRUE(RequestPermissionAndWait(browser()));
461   ASSERT_TRUE(PerformActionOnInfoBar(browser(), ALLOW, 0, 0));
462
463   CreateSimpleNotification(browser(), true);
464   EXPECT_EQ(1, GetNotificationCount());
465 }
466
467 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestDenyOnPermissionInfobar) {
468   ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
469
470   // Test that no notification is created
471   // when Deny is chosen from permission infobar.
472   ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
473   ASSERT_TRUE(RequestPermissionAndWait(browser()));
474   PerformActionOnInfoBar(browser(), DENY, 0, 0);
475   CreateSimpleNotification(browser(), false);
476   ASSERT_EQ(0, GetNotificationCount());
477   ContentSettingsForOneType settings;
478   GetPrefsByContentSetting(CONTENT_SETTING_BLOCK, &settings);
479   EXPECT_TRUE(CheckOriginInSetting(settings, GetTestPageURL()));
480 }
481
482 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestClosePermissionInfobar) {
483   ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
484
485   // Test that no notification is created when permission infobar is dismissed.
486   ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
487   ASSERT_TRUE(RequestPermissionAndWait(browser()));
488   PerformActionOnInfoBar(browser(), DISMISS, 0, 0);
489   CreateSimpleNotification(browser(), false);
490   ASSERT_EQ(0, GetNotificationCount());
491   ContentSettingsForOneType settings;
492   GetPrefsByContentSetting(CONTENT_SETTING_BLOCK, &settings);
493   EXPECT_EQ(0U, settings.size());
494 }
495
496 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestAllowNotificationsFromAllSites) {
497   ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
498
499   // Verify that all domains can be allowed to show notifications.
500   SetDefaultPermissionSetting(CONTENT_SETTING_ALLOW);
501   ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
502
503   std::string result = CreateSimpleNotification(browser(), true);
504   EXPECT_NE("-1", result);
505
506   ASSERT_EQ(1, GetNotificationCount());
507   InfoBarService* infobar_service = InfoBarService::FromWebContents(
508       browser()->tab_strip_model()->GetWebContentsAt(0));
509   EXPECT_EQ(0U, infobar_service->infobar_count());
510 }
511
512 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestDenyNotificationsFromAllSites) {
513   ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
514
515   // Verify that no domain can show notifications.
516   SetDefaultPermissionSetting(CONTENT_SETTING_BLOCK);
517   ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
518
519   std::string result = CreateSimpleNotification(browser(), false);
520   EXPECT_EQ("-1", result);
521
522   ASSERT_EQ(0, GetNotificationCount());
523 }
524
525 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestDenyDomainAndAllowAll) {
526   ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
527
528   // Verify that denying a domain and allowing all shouldn't show
529   // notifications from the denied domain.
530   DenyOrigin(GetTestPageURL().GetOrigin());
531   SetDefaultPermissionSetting(CONTENT_SETTING_ALLOW);
532
533   ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
534
535   std::string result = CreateSimpleNotification(browser(), false);
536   EXPECT_EQ("-1", result);
537
538   ASSERT_EQ(0, GetNotificationCount());
539 }
540
541 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestAllowDomainAndDenyAll) {
542   ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
543
544   // Verify that allowing a domain and denying all others should show
545   // notifications from the allowed domain.
546   AllowOrigin(GetTestPageURL().GetOrigin());
547   SetDefaultPermissionSetting(CONTENT_SETTING_BLOCK);
548
549   ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
550
551   std::string result = CreateSimpleNotification(browser(), true);
552   EXPECT_NE("-1", result);
553
554   ASSERT_EQ(1, GetNotificationCount());
555 }
556
557 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestDenyAndThenAllowDomain) {
558   ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
559
560   // Verify that denying and again allowing should show notifications.
561   DenyOrigin(GetTestPageURL().GetOrigin());
562
563   ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
564
565   std::string result = CreateSimpleNotification(browser(), false);
566   EXPECT_EQ("-1", result);
567
568   ASSERT_EQ(0, GetNotificationCount());
569
570   AllowOrigin(GetTestPageURL().GetOrigin());
571   result = CreateSimpleNotification(browser(), true);
572   EXPECT_NE("-1", result);
573
574   ASSERT_EQ(1, GetNotificationCount());
575   InfoBarService* infobar_service = InfoBarService::FromWebContents(
576       browser()->tab_strip_model()->GetWebContentsAt(0));
577   EXPECT_EQ(0U, infobar_service->infobar_count());
578 }
579
580 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestCreateDenyCloseNotifications) {
581   ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
582
583   // Verify able to create, deny, and close the notification.
584   AllowAllOrigins();
585   ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
586   CreateSimpleNotification(browser(), true);
587   ASSERT_EQ(1, GetNotificationCount());
588
589   DenyOrigin(GetTestPageURL().GetOrigin());
590   ContentSettingsForOneType settings;
591   GetPrefsByContentSetting(CONTENT_SETTING_BLOCK, &settings);
592   ASSERT_TRUE(CheckOriginInSetting(settings, GetTestPageURL().GetOrigin()));
593
594   EXPECT_EQ(1, GetNotificationCount());
595   message_center::NotificationList::Notifications notifications =
596       message_center::MessageCenter::Get()->GetVisibleNotifications();
597   message_center::MessageCenter::Get()->RemoveNotification(
598       (*notifications.rbegin())->id(),
599       true);  // by_user
600   ASSERT_EQ(0, GetNotificationCount());
601 }
602
603 // Crashes on Linux/Win. See http://crbug.com/160657.
604 IN_PROC_BROWSER_TEST_F(
605     NotificationsTest,
606     DISABLED_TestOriginPrefsNotSavedInIncognito) {
607   ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
608
609   // Verify that allow/deny origin preferences are not saved in incognito.
610   Browser* incognito = CreateIncognitoBrowser();
611   ui_test_utils::NavigateToURL(incognito, GetTestPageURL());
612   ASSERT_TRUE(RequestPermissionAndWait(incognito));
613   PerformActionOnInfoBar(incognito, DENY, 0, 0);
614   CloseBrowserWindow(incognito);
615
616   incognito = CreateIncognitoBrowser();
617   ui_test_utils::NavigateToURL(incognito, GetTestPageURL());
618   ASSERT_TRUE(RequestPermissionAndWait(incognito));
619   PerformActionOnInfoBar(incognito, ALLOW, 0, 0);
620   CreateSimpleNotification(incognito, true);
621   ASSERT_EQ(1, GetNotificationCount());
622   CloseBrowserWindow(incognito);
623
624   incognito = CreateIncognitoBrowser();
625   ui_test_utils::NavigateToURL(incognito, GetTestPageURL());
626   ASSERT_TRUE(RequestPermissionAndWait(incognito));
627
628   ContentSettingsForOneType settings;
629   GetPrefsByContentSetting(CONTENT_SETTING_BLOCK, &settings);
630   EXPECT_EQ(0U, settings.size());
631   GetPrefsByContentSetting(CONTENT_SETTING_ALLOW, &settings);
632   EXPECT_EQ(0U, settings.size());
633 }
634
635 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestExitBrowserWithInfobar) {
636   ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
637
638   // Exit the browser window, when the infobar appears.
639   ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
640   ASSERT_TRUE(RequestPermissionAndWait(browser()));
641 }
642
643 // Times out on Windows and Linux. http://crbug.com/168976
644 #if defined(OS_WIN) || defined(OS_LINUX)
645 #define MAYBE_TestCrashTabWithPermissionInfobar \
646     DISABLED_TestCrashTabWithPermissionInfobar
647 #else
648 #define MAYBE_TestCrashTabWithPermissionInfobar \
649     TestCrashTabWithPermissionInfobar
650 #endif
651 IN_PROC_BROWSER_TEST_F(NotificationsTest,
652                        MAYBE_TestCrashTabWithPermissionInfobar) {
653   ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
654
655   // Test crashing the tab with permission infobar doesn't crash Chrome.
656   ui_test_utils::NavigateToURLWithDisposition(
657       browser(),
658       embedded_test_server()->GetURL("/empty.html"),
659       NEW_BACKGROUND_TAB,
660       ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB);
661   browser()->tab_strip_model()->ActivateTabAt(0, true);
662   ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
663   ASSERT_TRUE(RequestPermissionAndWait(browser()));
664   CrashTab(browser(), 0);
665 }
666
667 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestIncognitoNotification) {
668   ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
669
670   // Test notifications in incognito window.
671   Browser* browser = CreateIncognitoBrowser();
672   ui_test_utils::NavigateToURL(browser, GetTestPageURL());
673   browser->tab_strip_model()->ActivateTabAt(0, true);
674   ASSERT_TRUE(RequestPermissionAndWait(browser));
675   PerformActionOnInfoBar(browser, ALLOW, 0, 0);
676   CreateSimpleNotification(browser, true);
677   ASSERT_EQ(1, GetNotificationCount());
678 }
679
680 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestCloseTabWithPermissionInfobar) {
681   ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
682
683   // Test that user can close tab when infobar present.
684   ui_test_utils::NavigateToURLWithDisposition(
685       browser(),
686       GURL("about:blank"),
687       NEW_BACKGROUND_TAB,
688       ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB);
689   browser()->tab_strip_model()->ActivateTabAt(0, true);
690   ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
691   ASSERT_TRUE(RequestPermissionAndWait(browser()));
692   content::WebContentsDestroyedWatcher destroyed_watcher(
693       browser()->tab_strip_model()->GetWebContentsAt(0));
694   browser()->tab_strip_model()->CloseWebContentsAt(0,
695                                                    TabStripModel::CLOSE_NONE);
696   destroyed_watcher.Wait();
697 }
698
699 IN_PROC_BROWSER_TEST_F(
700     NotificationsTest,
701     TestNavigateAwayWithPermissionInfobar) {
702   ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
703
704   // Test navigating away when an infobar is present,
705   // then trying to create a notification from the same page.
706   ui_test_utils::NavigateToURLWithDisposition(
707       browser(),
708       GURL("about:blank"),
709       NEW_BACKGROUND_TAB,
710       ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB);
711   browser()->tab_strip_model()->ActivateTabAt(0, true);
712   ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
713   ASSERT_TRUE(RequestPermissionAndWait(browser()));
714   ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
715   ASSERT_TRUE(RequestPermissionAndWait(browser()));
716   PerformActionOnInfoBar(browser(), ALLOW, 0, 0);
717   CreateSimpleNotification(browser(), true);
718   ASSERT_EQ(1, GetNotificationCount());
719 }
720
721 // See crbug.com/248470
722 #if defined(OS_LINUX)
723 #define MAYBE_TestCrashRendererNotificationRemain \
724     DISABLED_TestCrashRendererNotificationRemain
725 #else
726 #define MAYBE_TestCrashRendererNotificationRemain \
727     TestCrashRendererNotificationRemain
728 #endif
729
730 IN_PROC_BROWSER_TEST_F(NotificationsTest,
731                        MAYBE_TestCrashRendererNotificationRemain) {
732   ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
733
734   // Test crashing renderer does not close or crash notification.
735   AllowAllOrigins();
736   ui_test_utils::NavigateToURLWithDisposition(
737       browser(),
738       GURL("about:blank"),
739       NEW_BACKGROUND_TAB,
740       ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB);
741   browser()->tab_strip_model()->ActivateTabAt(0, true);
742   ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
743   CreateSimpleNotification(browser(), true);
744   ASSERT_EQ(1, GetNotificationCount());
745   CrashTab(browser(), 0);
746   ASSERT_EQ(1, GetNotificationCount());
747 }
748
749 // See http://crbug.com/366539
750 #if defined(OS_LINUX)
751 #define MAYBE_TestNotificationReplacement DISABLED_TestNotificationReplacement
752 #else
753 #define MAYBE_TestNotificationReplacement TestNotificationReplacement
754 #endif
755
756 IN_PROC_BROWSER_TEST_F(NotificationsTest, MAYBE_TestNotificationReplacement) {
757   ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
758
759   // Test that we can replace a notification using the replaceId.
760   AllowAllOrigins();
761
762   ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
763
764   std::string result = CreateNotification(
765       browser(), true, "abc.png", "Title1", "Body1", "chat");
766   EXPECT_NE("-1", result);
767
768   ASSERT_EQ(1, GetNotificationCount());
769
770   result = CreateNotification(
771       browser(), false, "no_such_file.png", "Title2", "Body2", "chat");
772   EXPECT_NE("-1", result);
773
774   ASSERT_EQ(1, GetNotificationCount());
775   message_center::NotificationList::Notifications notifications =
776       message_center::MessageCenter::Get()->GetVisibleNotifications();
777   EXPECT_EQ(base::ASCIIToUTF16("Title2"), (*notifications.rbegin())->title());
778   EXPECT_EQ(base::ASCIIToUTF16("Body2"),
779             (*notifications.rbegin())->message());
780 }