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.
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"
50 const char kExpectedIconUrl[] = "/notifications/no_such_file.png";
58 class NotificationChangeObserver {
60 virtual ~NotificationChangeObserver() {}
61 virtual bool Wait() = 0;
64 class MessageCenterChangeObserver
65 : public message_center::MessageCenterObserver,
66 public NotificationChangeObserver {
68 MessageCenterChangeObserver()
69 : notification_received_(false) {
70 message_center::MessageCenter::Get()->AddObserver(this);
73 virtual ~MessageCenterChangeObserver() {
74 message_center::MessageCenter::Get()->RemoveObserver(this);
77 // NotificationChangeObserver:
78 virtual bool Wait() OVERRIDE {
79 if (notification_received_)
82 message_loop_runner_ = new content::MessageLoopRunner;
83 message_loop_runner_->Run();
84 return notification_received_;
87 // message_center::MessageCenterObserver:
88 virtual void OnNotificationAdded(
89 const std::string& notification_id) OVERRIDE {
90 OnMessageCenterChanged();
93 virtual void OnNotificationRemoved(const std::string& notification_id,
94 bool by_user) OVERRIDE {
95 OnMessageCenterChanged();
98 virtual void OnNotificationUpdated(
99 const std::string& notification_id) OVERRIDE {
100 OnMessageCenterChanged();
103 void OnMessageCenterChanged() {
104 notification_received_ = true;
105 if (message_loop_runner_.get())
106 message_loop_runner_->Quit();
109 bool notification_received_;
110 scoped_refptr<content::MessageLoopRunner> message_loop_runner_;
112 DISALLOW_COPY_AND_ASSIGN(MessageCenterChangeObserver);
117 class NotificationsTest : public InProcessBrowserTest {
119 NotificationsTest() {}
122 int GetNotificationCount();
124 void CloseBrowserWindow(Browser* browser);
125 void CrashTab(Browser* browser, int index);
127 void SetDefaultPermissionSetting(ContentSetting setting);
128 void DenyOrigin(const GURL& origin);
129 void AllowOrigin(const GURL& origin);
130 void AllowAllOrigins();
132 void VerifyInfoBar(const Browser* browser, int index);
133 std::string CreateNotification(Browser* browser,
134 bool wait_for_new_balloon,
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,
147 void GetPrefsByContentSetting(ContentSetting setting,
148 ContentSettingsForOneType* settings);
149 bool CheckOriginInSetting(const ContentSettingsForOneType& settings,
152 GURL GetTestPageURL() const {
153 return embedded_test_server()->GetURL(
154 "/notifications/notification_tester.html");
158 void DropOriginPreference(const GURL& origin);
159 DesktopNotificationService* GetDesktopNotificationService();
162 int NotificationsTest::GetNotificationCount() {
163 return message_center::MessageCenter::Get()->NotificationCount();
166 void NotificationsTest::CloseBrowserWindow(Browser* browser) {
167 content::WindowedNotificationObserver observer(
168 chrome::NOTIFICATION_BROWSER_CLOSED,
169 content::Source<Browser>(browser));
170 browser->window()->Close();
174 void NotificationsTest::CrashTab(Browser* browser, int index) {
175 content::CrashTab(browser->tab_strip_model()->GetWebContentsAt(index));
178 void NotificationsTest::SetDefaultPermissionSetting(ContentSetting setting) {
179 DesktopNotificationService* service = GetDesktopNotificationService();
180 service->SetDefaultContentSetting(setting);
183 void NotificationsTest::DenyOrigin(const GURL& origin) {
184 DropOriginPreference(origin);
185 GetDesktopNotificationService()->DenyPermission(origin);
188 void NotificationsTest::AllowOrigin(const GURL& origin) {
189 DropOriginPreference(origin);
190 GetDesktopNotificationService()->GrantPermission(origin);
193 void NotificationsTest::AllowAllOrigins() {
194 GetDesktopNotificationService()->ResetAllOrigins();
195 GetDesktopNotificationService()->SetDefaultContentSetting(
196 CONTENT_SETTING_ALLOW);
199 void NotificationsTest::VerifyInfoBar(const Browser* browser, int index) {
200 InfoBarService* infobar_service = InfoBarService::FromWebContents(
201 browser->tab_strip_model()->GetWebContentsAt(index));
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);
212 std::string NotificationsTest::CreateNotification(
214 bool wait_for_new_balloon,
218 const char* replace_id) {
219 std::string script = base::StringPrintf(
220 "createNotification('%s', '%s', '%s', '%s');",
221 icon, title, body, replace_id);
223 MessageCenterChangeObserver observer;
225 bool success = content::ExecuteScriptAndExtractString(
226 browser->tab_strip_model()->GetActiveWebContents(),
229 if (success && result != "-1" && wait_for_new_balloon)
230 success = observer.Wait();
231 EXPECT_TRUE(success);
236 std::string NotificationsTest::CreateSimpleNotification(
238 bool wait_for_new_balloon) {
239 return CreateNotification(
240 browser, wait_for_new_balloon,
241 "no_such_file.png", "My Title", "My Body", "");
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));
251 bool success = content::ExecuteScriptAndExtractString(
252 browser->tab_strip_model()->GetActiveWebContents(),
253 "requestPermission();",
255 if (!success || result != "1")
261 bool NotificationsTest::CancelNotification(
262 const char* notification_id,
264 std::string script = base::StringPrintf(
265 "cancelNotification('%s');",
268 MessageCenterChangeObserver observer;
270 bool success = content::ExecuteScriptAndExtractString(
271 browser->tab_strip_model()->GetActiveWebContents(),
274 if (!success || result != "1")
276 return observer.Wait();
279 bool NotificationsTest::PerformActionOnInfoBar(
281 InfobarAction action,
282 size_t infobar_index,
284 InfoBarService* infobar_service = InfoBarService::FromWebContents(
285 browser->tab_strip_model()->GetWebContentsAt(tab_index));
286 if (infobar_index >= infobar_service->infobar_count()) {
291 infobars::InfoBar* infobar = infobar_service->infobar_at(infobar_index);
292 infobars::InfoBarDelegate* infobar_delegate = infobar->delegate();
295 infobar_delegate->InfoBarDismissed();
296 infobar_service->RemoveInfoBar(infobar);
300 ConfirmInfoBarDelegate* confirm_infobar_delegate =
301 infobar_delegate->AsConfirmInfoBarDelegate();
302 if (!confirm_infobar_delegate) {
304 } else if (confirm_infobar_delegate->Accept()) {
305 infobar_service->RemoveInfoBar(infobar);
311 ConfirmInfoBarDelegate* confirm_infobar_delegate =
312 infobar_delegate->AsConfirmInfoBarDelegate();
313 if (!confirm_infobar_delegate) {
315 } else if (confirm_infobar_delegate->Cancel()) {
316 infobar_service->RemoveInfoBar(infobar);
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);
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)
352 void NotificationsTest::DropOriginPreference(const GURL& origin) {
353 GetDesktopNotificationService()->ClearSetting(
354 ContentSettingsPattern::FromURLNoWildcard(origin));
357 DesktopNotificationService* NotificationsTest::GetDesktopNotificationService() {
358 Profile* profile = browser()->profile();
359 return DesktopNotificationServiceFactory::GetForProfile(profile);
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());
366 ui_test_utils::NavigateToURL(
368 embedded_test_server()->GetURL(
369 "/notifications/notifications_request_function.html"));
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.
374 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
375 browser()->tab_strip_model()->GetActiveWebContents(),
376 "window.domAutomationController.send(request());",
380 InfoBarService* infobar_service = InfoBarService::FromWebContents(
381 browser()->tab_strip_model()->GetWebContentsAt(0));
382 EXPECT_EQ(1U, infobar_service->infobar_count());
385 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestCreateSimpleNotification) {
386 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
388 // Creates a simple notification.
390 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
392 std::string result = CreateSimpleNotification(browser(), true);
393 EXPECT_NE("-1", result);
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());
405 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestCloseNotification) {
406 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
408 // Creates a notification and closes it.
410 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
412 std::string result = CreateSimpleNotification(browser(), true);
413 EXPECT_NE("-1", result);
414 ASSERT_EQ(1, GetNotificationCount());
416 message_center::NotificationList::Notifications notifications =
417 message_center::MessageCenter::Get()->GetVisibleNotifications();
418 message_center::MessageCenter::Get()->RemoveNotification(
419 (*notifications.rbegin())->id(),
422 ASSERT_EQ(0, GetNotificationCount());
425 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestCancelNotification) {
426 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
428 // Creates a notification and cancels it in the origin page.
430 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
432 std::string note_id = CreateSimpleNotification(browser(), true);
433 EXPECT_NE(note_id, "-1");
435 ASSERT_EQ(1, GetNotificationCount());
436 ASSERT_TRUE(CancelNotification(note_id.c_str(), browser()));
437 ASSERT_EQ(0, GetNotificationCount());
440 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestPermissionInfobarAppears) {
441 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
443 // Requests notification privileges and verifies the infobar appears.
444 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
445 ASSERT_TRUE(RequestPermissionAndWait(browser()));
447 ASSERT_EQ(0, GetNotificationCount());
448 ASSERT_NO_FATAL_FAILURE(VerifyInfoBar(browser(), 0));
451 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestAllowOnPermissionInfobar) {
452 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
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());
460 ASSERT_TRUE(RequestPermissionAndWait(browser()));
461 ASSERT_TRUE(PerformActionOnInfoBar(browser(), ALLOW, 0, 0));
463 CreateSimpleNotification(browser(), true);
464 EXPECT_EQ(1, GetNotificationCount());
467 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestDenyOnPermissionInfobar) {
468 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
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()));
482 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestClosePermissionInfobar) {
483 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
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());
496 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestAllowNotificationsFromAllSites) {
497 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
499 // Verify that all domains can be allowed to show notifications.
500 SetDefaultPermissionSetting(CONTENT_SETTING_ALLOW);
501 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
503 std::string result = CreateSimpleNotification(browser(), true);
504 EXPECT_NE("-1", result);
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());
512 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestDenyNotificationsFromAllSites) {
513 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
515 // Verify that no domain can show notifications.
516 SetDefaultPermissionSetting(CONTENT_SETTING_BLOCK);
517 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
519 std::string result = CreateSimpleNotification(browser(), false);
520 EXPECT_EQ("-1", result);
522 ASSERT_EQ(0, GetNotificationCount());
525 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestDenyDomainAndAllowAll) {
526 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
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);
533 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
535 std::string result = CreateSimpleNotification(browser(), false);
536 EXPECT_EQ("-1", result);
538 ASSERT_EQ(0, GetNotificationCount());
541 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestAllowDomainAndDenyAll) {
542 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
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);
549 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
551 std::string result = CreateSimpleNotification(browser(), true);
552 EXPECT_NE("-1", result);
554 ASSERT_EQ(1, GetNotificationCount());
557 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestDenyAndThenAllowDomain) {
558 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
560 // Verify that denying and again allowing should show notifications.
561 DenyOrigin(GetTestPageURL().GetOrigin());
563 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
565 std::string result = CreateSimpleNotification(browser(), false);
566 EXPECT_EQ("-1", result);
568 ASSERT_EQ(0, GetNotificationCount());
570 AllowOrigin(GetTestPageURL().GetOrigin());
571 result = CreateSimpleNotification(browser(), true);
572 EXPECT_NE("-1", result);
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());
580 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestCreateDenyCloseNotifications) {
581 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
583 // Verify able to create, deny, and close the notification.
585 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
586 CreateSimpleNotification(browser(), true);
587 ASSERT_EQ(1, GetNotificationCount());
589 DenyOrigin(GetTestPageURL().GetOrigin());
590 ContentSettingsForOneType settings;
591 GetPrefsByContentSetting(CONTENT_SETTING_BLOCK, &settings);
592 ASSERT_TRUE(CheckOriginInSetting(settings, GetTestPageURL().GetOrigin()));
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(),
600 ASSERT_EQ(0, GetNotificationCount());
603 // Crashes on Linux/Win. See http://crbug.com/160657.
604 IN_PROC_BROWSER_TEST_F(
606 DISABLED_TestOriginPrefsNotSavedInIncognito) {
607 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
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);
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);
624 incognito = CreateIncognitoBrowser();
625 ui_test_utils::NavigateToURL(incognito, GetTestPageURL());
626 ASSERT_TRUE(RequestPermissionAndWait(incognito));
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());
635 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestExitBrowserWithInfobar) {
636 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
638 // Exit the browser window, when the infobar appears.
639 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
640 ASSERT_TRUE(RequestPermissionAndWait(browser()));
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
648 #define MAYBE_TestCrashTabWithPermissionInfobar \
649 TestCrashTabWithPermissionInfobar
651 IN_PROC_BROWSER_TEST_F(NotificationsTest,
652 MAYBE_TestCrashTabWithPermissionInfobar) {
653 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
655 // Test crashing the tab with permission infobar doesn't crash Chrome.
656 ui_test_utils::NavigateToURLWithDisposition(
658 embedded_test_server()->GetURL("/empty.html"),
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);
667 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestIncognitoNotification) {
668 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
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());
680 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestCloseTabWithPermissionInfobar) {
681 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
683 // Test that user can close tab when infobar present.
684 ui_test_utils::NavigateToURLWithDisposition(
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();
699 IN_PROC_BROWSER_TEST_F(
701 TestNavigateAwayWithPermissionInfobar) {
702 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
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(
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());
721 // See crbug.com/248470
722 #if defined(OS_LINUX)
723 #define MAYBE_TestCrashRendererNotificationRemain \
724 DISABLED_TestCrashRendererNotificationRemain
726 #define MAYBE_TestCrashRendererNotificationRemain \
727 TestCrashRendererNotificationRemain
730 IN_PROC_BROWSER_TEST_F(NotificationsTest,
731 MAYBE_TestCrashRendererNotificationRemain) {
732 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
734 // Test crashing renderer does not close or crash notification.
736 ui_test_utils::NavigateToURLWithDisposition(
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());
749 // See http://crbug.com/366539
750 #if defined(OS_LINUX)
751 #define MAYBE_TestNotificationReplacement DISABLED_TestNotificationReplacement
753 #define MAYBE_TestNotificationReplacement TestNotificationReplacement
756 IN_PROC_BROWSER_TEST_F(NotificationsTest, MAYBE_TestNotificationReplacement) {
757 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
759 // Test that we can replace a notification using the replaceId.
762 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
764 std::string result = CreateNotification(
765 browser(), true, "abc.png", "Title1", "Body1", "chat");
766 EXPECT_NE("-1", result);
768 ASSERT_EQ(1, GetNotificationCount());
770 result = CreateNotification(
771 browser(), false, "no_such_file.png", "Title2", "Body2", "chat");
772 EXPECT_NE("-1", result);
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());