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/confirm_infobar_delegate.h"
19 #include "chrome/browser/infobars/infobar.h"
20 #include "chrome/browser/infobars/infobar_service.h"
21 #include "chrome/browser/notifications/balloon.h"
22 #include "chrome/browser/notifications/balloon_collection.h"
23 #include "chrome/browser/notifications/balloon_host.h"
24 #include "chrome/browser/notifications/balloon_notification_ui_manager.h"
25 #include "chrome/browser/notifications/desktop_notification_service.h"
26 #include "chrome/browser/notifications/desktop_notification_service_factory.h"
27 #include "chrome/browser/notifications/notification.h"
28 #include "chrome/browser/profiles/profile.h"
29 #include "chrome/browser/ui/browser.h"
30 #include "chrome/browser/ui/browser_tabstrip.h"
31 #include "chrome/browser/ui/browser_window.h"
32 #include "chrome/browser/ui/tabs/tab_strip_model.h"
33 #include "chrome/common/content_settings.h"
34 #include "chrome/common/content_settings_pattern.h"
35 #include "chrome/test/base/in_process_browser_test.h"
36 #include "chrome/test/base/ui_test_utils.h"
37 #include "content/public/browser/notification_service.h"
38 #include "content/public/browser/notification_source.h"
39 #include "content/public/browser/notification_types.h"
40 #include "content/public/browser/render_view_host.h"
41 #include "content/public/browser/web_contents.h"
42 #include "content/public/test/browser_test_utils.h"
43 #include "content/public/test/test_utils.h"
44 #include "net/base/net_util.h"
45 #include "net/test/embedded_test_server/embedded_test_server.h"
46 #include "testing/gtest/include/gtest/gtest.h"
47 #include "ui/base/window_open_disposition.h"
48 #include "ui/message_center/message_center.h"
49 #include "ui/message_center/message_center_observer.h"
50 #include "ui/message_center/message_center_switches.h"
51 #include "ui/message_center/message_center_util.h"
56 const char kExpectedIconUrl
[] = "/notifications/no_such_file.png";
64 class NotificationChangeObserver
{
66 virtual ~NotificationChangeObserver() {}
67 virtual bool Wait() = 0;
70 class MessageCenterChangeObserver
71 : public message_center::MessageCenterObserver
,
72 public NotificationChangeObserver
{
74 MessageCenterChangeObserver()
75 : notification_received_(false) {
76 message_center::MessageCenter::Get()->AddObserver(this);
79 virtual ~MessageCenterChangeObserver() {
80 message_center::MessageCenter::Get()->RemoveObserver(this);
83 // NotificationChangeObserver:
84 virtual bool Wait() OVERRIDE
{
85 if (notification_received_
)
88 message_loop_runner_
= new content::MessageLoopRunner
;
89 message_loop_runner_
->Run();
90 return notification_received_
;
93 // message_center::MessageCenterObserver:
94 virtual void OnNotificationAdded(
95 const std::string
& notification_id
) OVERRIDE
{
96 OnMessageCenterChanged();
99 virtual void OnNotificationRemoved(const std::string
& notification_id
,
100 bool by_user
) OVERRIDE
{
101 OnMessageCenterChanged();
104 virtual void OnNotificationUpdated(
105 const std::string
& notification_id
) OVERRIDE
{
106 OnMessageCenterChanged();
109 void OnMessageCenterChanged() {
110 notification_received_
= true;
111 if (message_loop_runner_
.get())
112 message_loop_runner_
->Quit();
115 bool notification_received_
;
116 scoped_refptr
<content::MessageLoopRunner
> message_loop_runner_
;
118 DISALLOW_COPY_AND_ASSIGN(MessageCenterChangeObserver
);
121 class NotificationBalloonChangeObserver
122 : public content::NotificationObserver
,
123 public NotificationChangeObserver
{
125 NotificationBalloonChangeObserver()
126 : collection_(BalloonNotificationUIManager::GetInstanceForTesting()->
127 balloon_collection()),
128 collection_changed_(false),
129 notification_received_(false),
132 registrar_
.Add(this, chrome::NOTIFICATION_NOTIFY_BALLOON_CONNECTED
,
133 content::NotificationService::AllSources());
134 registrar_
.Add(this, chrome::NOTIFICATION_NOTIFY_BALLOON_DISCONNECTED
,
135 content::NotificationService::AllSources());
136 collection_
->set_on_collection_changed_callback(
137 base::Bind(&NotificationBalloonChangeObserver::OnCollectionChanged
,
138 base::Unretained(this)));
141 virtual ~NotificationBalloonChangeObserver() {
142 collection_
->set_on_collection_changed_callback(base::Closure());
145 // NotificationChangeObserver:
146 virtual bool Wait() OVERRIDE
{
149 message_loop_runner_
= new content::MessageLoopRunner
;
150 message_loop_runner_
->Run();
160 if (collection_changed_
&& notification_received_
) {
163 message_loop_runner_
->Quit();
170 void OnCollectionChanged() {
171 collection_changed_
= true;
175 // content::NotificationObserver:
176 virtual void Observe(int type
,
177 const content::NotificationSource
& source
,
178 const content::NotificationDetails
& details
) OVERRIDE
{
179 DCHECK(type
== chrome::NOTIFICATION_NOTIFY_BALLOON_DISCONNECTED
||
180 type
== chrome::NOTIFICATION_NOTIFY_BALLOON_CONNECTED
);
181 notification_received_
= true;
186 content::NotificationRegistrar registrar_
;
187 BalloonCollection
* collection_
;
189 bool collection_changed_
;
190 bool notification_received_
;
193 scoped_refptr
<content::MessageLoopRunner
> message_loop_runner_
;
195 DISALLOW_COPY_AND_ASSIGN(NotificationBalloonChangeObserver
);
200 class NotificationsTest
: public InProcessBrowserTest
{
202 NotificationsTest() {}
205 int GetNotificationCount();
207 NotificationChangeObserver
* CreateObserver();
209 void CloseBrowserWindow(Browser
* browser
);
210 void CrashTab(Browser
* browser
, int index
);
211 const std::deque
<Balloon
*>& GetActiveBalloons();
212 void CrashNotification(Balloon
* balloon
);
213 bool CloseNotificationAndWait(const Notification
& notification
);
215 void SetDefaultPermissionSetting(ContentSetting setting
);
216 void DenyOrigin(const GURL
& origin
);
217 void AllowOrigin(const GURL
& origin
);
218 void AllowAllOrigins();
220 void VerifyInfoBar(const Browser
* browser
, int index
);
221 std::string
CreateNotification(Browser
* browser
,
222 bool wait_for_new_balloon
,
226 const char* replace_id
);
227 std::string
CreateSimpleNotification(Browser
* browser
,
228 bool wait_for_new_balloon
);
229 bool RequestPermissionAndWait(Browser
* browser
);
230 bool CancelNotification(const char* notification_id
, Browser
* browser
);
231 bool PerformActionOnInfoBar(Browser
* browser
,
232 InfobarAction action
,
233 size_t infobar_index
,
235 void GetPrefsByContentSetting(ContentSetting setting
,
236 ContentSettingsForOneType
* settings
);
237 bool CheckOriginInSetting(const ContentSettingsForOneType
& settings
,
240 GURL
GetTestPageURL() const {
241 return embedded_test_server()->GetURL(
242 "/notifications/notification_tester.html");
246 void DropOriginPreference(const GURL
& origin
);
247 DesktopNotificationService
* GetDesktopNotificationService();
250 int NotificationsTest::GetNotificationCount() {
251 if (message_center::IsRichNotificationEnabled()) {
252 return message_center::MessageCenter::Get()->NotificationCount();
254 return BalloonNotificationUIManager::GetInstanceForTesting()->
255 balloon_collection()->GetActiveBalloons().size();
259 NotificationChangeObserver
* NotificationsTest::CreateObserver() {
260 if (message_center::IsRichNotificationEnabled())
261 return new MessageCenterChangeObserver();
263 return new NotificationBalloonChangeObserver();
266 void NotificationsTest::CloseBrowserWindow(Browser
* browser
) {
267 content::WindowedNotificationObserver
observer(
268 chrome::NOTIFICATION_BROWSER_CLOSED
,
269 content::Source
<Browser
>(browser
));
270 browser
->window()->Close();
274 void NotificationsTest::CrashTab(Browser
* browser
, int index
) {
275 content::CrashTab(browser
->tab_strip_model()->GetWebContentsAt(index
));
278 const std::deque
<Balloon
*>& NotificationsTest::GetActiveBalloons() {
279 return BalloonNotificationUIManager::GetInstanceForTesting()->
280 balloon_collection()->GetActiveBalloons();
283 void NotificationsTest::CrashNotification(Balloon
* balloon
) {
284 content::CrashTab(balloon
->balloon_view()->GetHost()->web_contents());
287 bool NotificationsTest::CloseNotificationAndWait(
288 const Notification
& notification
) {
289 scoped_ptr
<NotificationChangeObserver
> observer(CreateObserver());
290 bool success
= g_browser_process
->notification_ui_manager()->
291 CancelById(notification
.notification_id());
293 return observer
->Wait();
297 void NotificationsTest::SetDefaultPermissionSetting(ContentSetting setting
) {
298 DesktopNotificationService
* service
= GetDesktopNotificationService();
299 service
->SetDefaultContentSetting(setting
);
302 void NotificationsTest::DenyOrigin(const GURL
& origin
) {
303 DropOriginPreference(origin
);
304 GetDesktopNotificationService()->DenyPermission(origin
);
307 void NotificationsTest::AllowOrigin(const GURL
& origin
) {
308 DropOriginPreference(origin
);
309 GetDesktopNotificationService()->GrantPermission(origin
);
312 void NotificationsTest::AllowAllOrigins() {
313 GetDesktopNotificationService()->ResetAllOrigins();
314 GetDesktopNotificationService()->SetDefaultContentSetting(
315 CONTENT_SETTING_ALLOW
);
318 void NotificationsTest::VerifyInfoBar(const Browser
* browser
, int index
) {
319 InfoBarService
* infobar_service
= InfoBarService::FromWebContents(
320 browser
->tab_strip_model()->GetWebContentsAt(index
));
322 ASSERT_EQ(1U, infobar_service
->infobar_count());
323 ConfirmInfoBarDelegate
* confirm_infobar
=
324 infobar_service
->infobar_at(0)->delegate()->AsConfirmInfoBarDelegate();
325 ASSERT_TRUE(confirm_infobar
);
326 int buttons
= confirm_infobar
->GetButtons();
327 EXPECT_TRUE(buttons
& ConfirmInfoBarDelegate::BUTTON_OK
);
328 EXPECT_TRUE(buttons
& ConfirmInfoBarDelegate::BUTTON_CANCEL
);
331 std::string
NotificationsTest::CreateNotification(
333 bool wait_for_new_balloon
,
337 const char* replace_id
) {
338 std::string script
= base::StringPrintf(
339 "createNotification('%s', '%s', '%s', '%s');",
340 icon
, title
, body
, replace_id
);
342 scoped_ptr
<NotificationChangeObserver
> observer(CreateObserver());
344 bool success
= content::ExecuteScriptAndExtractString(
345 browser
->tab_strip_model()->GetActiveWebContents(),
348 if (success
&& result
!= "-1" && wait_for_new_balloon
)
349 success
= observer
->Wait();
350 EXPECT_TRUE(success
);
355 std::string
NotificationsTest::CreateSimpleNotification(
357 bool wait_for_new_balloon
) {
358 return CreateNotification(
359 browser
, wait_for_new_balloon
,
360 "no_such_file.png", "My Title", "My Body", "");
363 bool NotificationsTest::RequestPermissionAndWait(Browser
* browser
) {
364 InfoBarService
* infobar_service
= InfoBarService::FromWebContents(
365 browser
->tab_strip_model()->GetActiveWebContents());
366 content::WindowedNotificationObserver
observer(
367 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED
,
368 content::Source
<InfoBarService
>(infobar_service
));
370 bool success
= content::ExecuteScriptAndExtractString(
371 browser
->tab_strip_model()->GetActiveWebContents(),
372 "requestPermission();",
374 if (!success
|| result
!= "1")
380 bool NotificationsTest::CancelNotification(
381 const char* notification_id
,
383 std::string script
= base::StringPrintf(
384 "cancelNotification('%s');",
387 scoped_ptr
<NotificationChangeObserver
> observer(CreateObserver());
389 bool success
= content::ExecuteScriptAndExtractString(
390 browser
->tab_strip_model()->GetActiveWebContents(),
393 if (!success
|| result
!= "1")
395 return observer
->Wait();
398 bool NotificationsTest::PerformActionOnInfoBar(
400 InfobarAction action
,
401 size_t infobar_index
,
403 InfoBarService
* infobar_service
= InfoBarService::FromWebContents(
404 browser
->tab_strip_model()->GetWebContentsAt(tab_index
));
405 if (infobar_index
>= infobar_service
->infobar_count()) {
410 InfoBar
* infobar
= infobar_service
->infobar_at(infobar_index
);
411 InfoBarDelegate
* infobar_delegate
= infobar
->delegate();
414 infobar_delegate
->InfoBarDismissed();
415 infobar_service
->RemoveInfoBar(infobar
);
419 ConfirmInfoBarDelegate
* confirm_infobar_delegate
=
420 infobar_delegate
->AsConfirmInfoBarDelegate();
421 if (!confirm_infobar_delegate
) {
423 } else if (confirm_infobar_delegate
->Accept()) {
424 infobar_service
->RemoveInfoBar(infobar
);
430 ConfirmInfoBarDelegate
* confirm_infobar_delegate
=
431 infobar_delegate
->AsConfirmInfoBarDelegate();
432 if (!confirm_infobar_delegate
) {
434 } else if (confirm_infobar_delegate
->Cancel()) {
435 infobar_service
->RemoveInfoBar(infobar
);
444 void NotificationsTest::GetPrefsByContentSetting(
445 ContentSetting setting
,
446 ContentSettingsForOneType
* settings
) {
447 DesktopNotificationService
* service
= GetDesktopNotificationService();
448 service
->GetNotificationsSettings(settings
);
449 for (ContentSettingsForOneType::iterator it
= settings
->begin();
450 it
!= settings
->end(); ) {
451 if (it
->setting
!= setting
|| it
->source
.compare("preference") != 0)
452 it
= settings
->erase(it
);
458 bool NotificationsTest::CheckOriginInSetting(
459 const ContentSettingsForOneType
& settings
,
460 const GURL
& origin
) {
461 ContentSettingsPattern pattern
=
462 ContentSettingsPattern::FromURLNoWildcard(origin
);
463 for (ContentSettingsForOneType::const_iterator it
= settings
.begin();
464 it
!= settings
.end(); ++it
) {
465 if (it
->primary_pattern
== pattern
)
471 void NotificationsTest::DropOriginPreference(const GURL
& origin
) {
472 GetDesktopNotificationService()->ClearSetting(
473 ContentSettingsPattern::FromURLNoWildcard(origin
));
476 DesktopNotificationService
* NotificationsTest::GetDesktopNotificationService() {
477 Profile
* profile
= browser()->profile();
478 return DesktopNotificationServiceFactory::GetForProfile(profile
);
481 // If this flakes, use http://crbug.com/62311 and http://crbug.com/74428.
482 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestUserGestureInfobar
) {
483 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
485 ui_test_utils::NavigateToURL(
487 embedded_test_server()->GetURL(
488 "/notifications/notifications_request_function.html"));
490 // Request permission by calling request() while eval'ing an inline script;
491 // That's considered a user gesture to webkit, and should produce an infobar.
493 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
494 browser()->tab_strip_model()->GetActiveWebContents(),
495 "window.domAutomationController.send(request());",
499 EXPECT_EQ(1U, InfoBarService::FromWebContents(
500 browser()->tab_strip_model()->GetWebContentsAt(0))->infobar_count());
503 // If this flakes, use http://crbug.com/62311.
504 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestNoUserGestureInfobar
) {
505 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
507 // Load a page which just does a request; no user gesture should result
509 ui_test_utils::NavigateToURL(
511 embedded_test_server()->GetURL(
512 "/notifications/notifications_request_inline.html"));
514 EXPECT_EQ(0U, InfoBarService::FromWebContents(
515 browser()->tab_strip_model()->GetWebContentsAt(0))->infobar_count());
518 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestCreateSimpleNotification
) {
519 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
521 // Creates a simple notification.
523 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
525 std::string result
= CreateSimpleNotification(browser(), true);
526 EXPECT_NE("-1", result
);
528 GURL EXPECTED_ICON_URL
= embedded_test_server()->GetURL(kExpectedIconUrl
);
529 ASSERT_EQ(1, GetNotificationCount());
530 if (message_center::IsRichNotificationEnabled()) {
531 message_center::NotificationList::Notifications notifications
=
532 message_center::MessageCenter::Get()->GetVisibleNotifications();
533 EXPECT_EQ(base::ASCIIToUTF16("My Title"),
534 (*notifications
.rbegin())->title());
535 EXPECT_EQ(base::ASCIIToUTF16("My Body"),
536 (*notifications
.rbegin())->message());
538 const std::deque
<Balloon
*>& balloons
= GetActiveBalloons();
539 ASSERT_EQ(1U, balloons
.size());
540 Balloon
* balloon
= balloons
[0];
541 const Notification
& notification
= balloon
->notification();
542 EXPECT_EQ(EXPECTED_ICON_URL
, notification
.icon_url());
543 EXPECT_EQ(base::ASCIIToUTF16("My Title"), notification
.title());
544 EXPECT_EQ(base::ASCIIToUTF16("My Body"), notification
.message());
548 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestCloseNotification
) {
549 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
551 // Creates a notification and closes it.
553 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
555 std::string result
= CreateSimpleNotification(browser(), true);
556 EXPECT_NE("-1", result
);
557 ASSERT_EQ(1, GetNotificationCount());
559 if (message_center::IsRichNotificationEnabled()) {
560 message_center::NotificationList::Notifications notifications
=
561 message_center::MessageCenter::Get()->GetVisibleNotifications();
562 message_center::MessageCenter::Get()->RemoveNotification(
563 (*notifications
.rbegin())->id(),
566 const std::deque
<Balloon
*>& balloons
= GetActiveBalloons();
567 EXPECT_TRUE(CloseNotificationAndWait(balloons
[0]->notification()));
570 ASSERT_EQ(0, GetNotificationCount());
573 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestCancelNotification
) {
574 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
576 // Creates a notification and cancels it in the origin page.
578 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
580 std::string note_id
= CreateSimpleNotification(browser(), true);
581 EXPECT_NE(note_id
, "-1");
583 ASSERT_EQ(1, GetNotificationCount());
584 ASSERT_TRUE(CancelNotification(note_id
.c_str(), browser()));
585 ASSERT_EQ(0, GetNotificationCount());
588 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestPermissionInfobarAppears
) {
589 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
591 // Requests notification privileges and verifies the infobar appears.
592 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
593 ASSERT_TRUE(RequestPermissionAndWait(browser()));
595 ASSERT_EQ(0, GetNotificationCount());
596 ASSERT_NO_FATAL_FAILURE(VerifyInfoBar(browser(), 0));
599 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestAllowOnPermissionInfobar
) {
600 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
602 // Tries to create a notification and clicks allow on the infobar.
603 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
604 // This notification should not be shown because we do not have permission.
605 CreateSimpleNotification(browser(), false);
606 ASSERT_EQ(0, GetNotificationCount());
608 ASSERT_TRUE(RequestPermissionAndWait(browser()));
609 ASSERT_TRUE(PerformActionOnInfoBar(browser(), ALLOW
, 0, 0));
611 CreateSimpleNotification(browser(), true);
612 EXPECT_EQ(1, GetNotificationCount());
615 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestDenyOnPermissionInfobar
) {
616 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
618 // Test that no notification is created
619 // when Deny is chosen from permission infobar.
620 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
621 ASSERT_TRUE(RequestPermissionAndWait(browser()));
622 PerformActionOnInfoBar(browser(), DENY
, 0, 0);
623 CreateSimpleNotification(browser(), false);
624 ASSERT_EQ(0, GetNotificationCount());
625 ContentSettingsForOneType settings
;
626 GetPrefsByContentSetting(CONTENT_SETTING_BLOCK
, &settings
);
627 EXPECT_TRUE(CheckOriginInSetting(settings
, GetTestPageURL()));
630 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestClosePermissionInfobar
) {
631 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
633 // Test that no notification is created when permission infobar is dismissed.
634 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
635 ASSERT_TRUE(RequestPermissionAndWait(browser()));
636 PerformActionOnInfoBar(browser(), DISMISS
, 0, 0);
637 CreateSimpleNotification(browser(), false);
638 ASSERT_EQ(0, GetNotificationCount());
639 ContentSettingsForOneType settings
;
640 GetPrefsByContentSetting(CONTENT_SETTING_BLOCK
, &settings
);
641 EXPECT_EQ(0U, settings
.size());
644 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestAllowNotificationsFromAllSites
) {
645 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
647 // Verify that all domains can be allowed to show notifications.
648 SetDefaultPermissionSetting(CONTENT_SETTING_ALLOW
);
649 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
651 std::string result
= CreateSimpleNotification(browser(), true);
652 EXPECT_NE("-1", result
);
654 ASSERT_EQ(1, GetNotificationCount());
655 EXPECT_EQ(0U, InfoBarService::FromWebContents(
656 browser()->tab_strip_model()->GetWebContentsAt(0))->infobar_count());
659 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestDenyNotificationsFromAllSites
) {
660 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
662 // Verify that no domain can show notifications.
663 SetDefaultPermissionSetting(CONTENT_SETTING_BLOCK
);
664 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
666 std::string result
= CreateSimpleNotification(browser(), false);
667 EXPECT_EQ("-1", result
);
669 ASSERT_EQ(0, GetNotificationCount());
672 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestDenyDomainAndAllowAll
) {
673 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
675 // Verify that denying a domain and allowing all shouldn't show
676 // notifications from the denied domain.
677 DenyOrigin(GetTestPageURL().GetOrigin());
678 SetDefaultPermissionSetting(CONTENT_SETTING_ALLOW
);
680 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
682 std::string result
= CreateSimpleNotification(browser(), false);
683 EXPECT_EQ("-1", result
);
685 ASSERT_EQ(0, GetNotificationCount());
688 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestAllowDomainAndDenyAll
) {
689 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
691 // Verify that allowing a domain and denying all others should show
692 // notifications from the allowed domain.
693 AllowOrigin(GetTestPageURL().GetOrigin());
694 SetDefaultPermissionSetting(CONTENT_SETTING_BLOCK
);
696 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
698 std::string result
= CreateSimpleNotification(browser(), true);
699 EXPECT_NE("-1", result
);
701 ASSERT_EQ(1, GetNotificationCount());
704 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestDenyAndThenAllowDomain
) {
705 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
707 // Verify that denying and again allowing should show notifications.
708 DenyOrigin(GetTestPageURL().GetOrigin());
710 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
712 std::string result
= CreateSimpleNotification(browser(), false);
713 EXPECT_EQ("-1", result
);
715 ASSERT_EQ(0, GetNotificationCount());
717 AllowOrigin(GetTestPageURL().GetOrigin());
718 result
= CreateSimpleNotification(browser(), true);
719 EXPECT_NE("-1", result
);
721 ASSERT_EQ(1, GetNotificationCount());
722 EXPECT_EQ(0U, InfoBarService::FromWebContents(
723 browser()->tab_strip_model()->GetWebContentsAt(0))->infobar_count());
726 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestCreateDenyCloseNotifications
) {
727 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
729 // Verify able to create, deny, and close the notification.
731 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
732 CreateSimpleNotification(browser(), true);
733 ASSERT_EQ(1, GetNotificationCount());
735 DenyOrigin(GetTestPageURL().GetOrigin());
736 ContentSettingsForOneType settings
;
737 GetPrefsByContentSetting(CONTENT_SETTING_BLOCK
, &settings
);
738 ASSERT_TRUE(CheckOriginInSetting(settings
, GetTestPageURL().GetOrigin()));
740 EXPECT_EQ(1, GetNotificationCount());
741 if (message_center::IsRichNotificationEnabled()) {
742 message_center::NotificationList::Notifications notifications
=
743 message_center::MessageCenter::Get()->GetVisibleNotifications();
744 message_center::MessageCenter::Get()->RemoveNotification(
745 (*notifications
.rbegin())->id(),
748 const std::deque
<Balloon
*>& balloons
= GetActiveBalloons();
749 ASSERT_TRUE(CloseNotificationAndWait(balloons
[0]->notification()));
751 ASSERT_EQ(0, GetNotificationCount());
754 // Crashes on Linux/Win. See http://crbug.com/160657.
755 IN_PROC_BROWSER_TEST_F(
757 DISABLED_TestOriginPrefsNotSavedInIncognito
) {
758 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
760 // Verify that allow/deny origin preferences are not saved in incognito.
761 Browser
* incognito
= CreateIncognitoBrowser();
762 ui_test_utils::NavigateToURL(incognito
, GetTestPageURL());
763 ASSERT_TRUE(RequestPermissionAndWait(incognito
));
764 PerformActionOnInfoBar(incognito
, DENY
, 0, 0);
765 CloseBrowserWindow(incognito
);
767 incognito
= CreateIncognitoBrowser();
768 ui_test_utils::NavigateToURL(incognito
, GetTestPageURL());
769 ASSERT_TRUE(RequestPermissionAndWait(incognito
));
770 PerformActionOnInfoBar(incognito
, ALLOW
, 0, 0);
771 CreateSimpleNotification(incognito
, true);
772 ASSERT_EQ(1, GetNotificationCount());
773 CloseBrowserWindow(incognito
);
775 incognito
= CreateIncognitoBrowser();
776 ui_test_utils::NavigateToURL(incognito
, GetTestPageURL());
777 ASSERT_TRUE(RequestPermissionAndWait(incognito
));
779 ContentSettingsForOneType settings
;
780 GetPrefsByContentSetting(CONTENT_SETTING_BLOCK
, &settings
);
781 EXPECT_EQ(0U, settings
.size());
782 GetPrefsByContentSetting(CONTENT_SETTING_ALLOW
, &settings
);
783 EXPECT_EQ(0U, settings
.size());
786 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestExitBrowserWithInfobar
) {
787 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
789 // Exit the browser window, when the infobar appears.
790 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
791 ASSERT_TRUE(RequestPermissionAndWait(browser()));
794 // Times out on Windows and Linux. http://crbug.com/168976
795 #if defined(OS_WIN) || defined(OS_LINUX)
796 #define MAYBE_TestCrashTabWithPermissionInfobar \
797 DISABLED_TestCrashTabWithPermissionInfobar
799 #define MAYBE_TestCrashTabWithPermissionInfobar \
800 TestCrashTabWithPermissionInfobar
802 IN_PROC_BROWSER_TEST_F(NotificationsTest
,
803 MAYBE_TestCrashTabWithPermissionInfobar
) {
804 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
806 // Test crashing the tab with permission infobar doesn't crash Chrome.
807 ui_test_utils::NavigateToURLWithDisposition(
809 embedded_test_server()->GetURL("/empty.html"),
811 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB
);
812 browser()->tab_strip_model()->ActivateTabAt(0, true);
813 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
814 ASSERT_TRUE(RequestPermissionAndWait(browser()));
815 CrashTab(browser(), 0);
818 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestKillNotificationProcess
) {
819 // Notifications don't have their own process with the message center.
820 if (message_center::IsRichNotificationEnabled())
823 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
825 // Test killing a notification doesn't crash Chrome.
827 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
828 CreateSimpleNotification(browser(), true);
829 ASSERT_EQ(1, GetNotificationCount());
831 const std::deque
<Balloon
*>& balloons
= GetActiveBalloons();
832 ASSERT_EQ(1U, balloons
.size());
833 CrashNotification(balloons
[0]);
834 ASSERT_EQ(0, GetNotificationCount());
837 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestIncognitoNotification
) {
838 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
840 // Test notifications in incognito window.
841 Browser
* browser
= CreateIncognitoBrowser();
842 ui_test_utils::NavigateToURL(browser
, GetTestPageURL());
843 browser
->tab_strip_model()->ActivateTabAt(0, true);
844 ASSERT_TRUE(RequestPermissionAndWait(browser
));
845 PerformActionOnInfoBar(browser
, ALLOW
, 0, 0);
846 CreateSimpleNotification(browser
, true);
847 ASSERT_EQ(1, GetNotificationCount());
850 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestCloseTabWithPermissionInfobar
) {
851 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
853 // Test that user can close tab when infobar present.
854 ui_test_utils::NavigateToURLWithDisposition(
858 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB
);
859 browser()->tab_strip_model()->ActivateTabAt(0, true);
860 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
861 ASSERT_TRUE(RequestPermissionAndWait(browser()));
862 content::WebContentsDestroyedWatcher
destroyed_watcher(
863 browser()->tab_strip_model()->GetWebContentsAt(0));
864 browser()->tab_strip_model()->CloseWebContentsAt(0,
865 TabStripModel::CLOSE_NONE
);
866 destroyed_watcher
.Wait();
869 IN_PROC_BROWSER_TEST_F(
871 TestNavigateAwayWithPermissionInfobar
) {
872 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
874 // Test navigating away when an infobar is present,
875 // then trying to create a notification from the same page.
876 ui_test_utils::NavigateToURLWithDisposition(
880 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB
);
881 browser()->tab_strip_model()->ActivateTabAt(0, true);
882 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
883 ASSERT_TRUE(RequestPermissionAndWait(browser()));
884 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
885 ASSERT_TRUE(RequestPermissionAndWait(browser()));
886 PerformActionOnInfoBar(browser(), ALLOW
, 0, 0);
887 CreateSimpleNotification(browser(), true);
888 ASSERT_EQ(1, GetNotificationCount());
891 // See crbug.com/248470
892 #if defined(OS_LINUX)
893 #define MAYBE_TestCrashRendererNotificationRemain \
894 DISABLED_TestCrashRendererNotificationRemain
896 #define MAYBE_TestCrashRendererNotificationRemain \
897 TestCrashRendererNotificationRemain
900 IN_PROC_BROWSER_TEST_F(NotificationsTest
,
901 MAYBE_TestCrashRendererNotificationRemain
) {
902 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
904 // Test crashing renderer does not close or crash notification.
906 ui_test_utils::NavigateToURLWithDisposition(
910 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB
);
911 browser()->tab_strip_model()->ActivateTabAt(0, true);
912 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
913 CreateSimpleNotification(browser(), true);
914 ASSERT_EQ(1, GetNotificationCount());
915 CrashTab(browser(), 0);
916 ASSERT_EQ(1, GetNotificationCount());
919 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestNotificationReplacement
) {
920 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
922 // Test that we can replace a notification using the replaceId.
925 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
927 std::string result
= CreateNotification(
928 browser(), true, "abc.png", "Title1", "Body1", "chat");
929 EXPECT_NE("-1", result
);
931 ASSERT_EQ(1, GetNotificationCount());
933 result
= CreateNotification(
934 browser(), false, "no_such_file.png", "Title2", "Body2", "chat");
935 EXPECT_NE("-1", result
);
937 if (message_center::IsRichNotificationEnabled()) {
938 ASSERT_EQ(1, GetNotificationCount());
939 message_center::NotificationList::Notifications notifications
=
940 message_center::MessageCenter::Get()->GetVisibleNotifications();
941 EXPECT_EQ(base::ASCIIToUTF16("Title2"), (*notifications
.rbegin())->title());
942 EXPECT_EQ(base::ASCIIToUTF16("Body2"),
943 (*notifications
.rbegin())->message());
945 const std::deque
<Balloon
*>& balloons
= GetActiveBalloons();
946 ASSERT_EQ(1U, balloons
.size());
947 Balloon
* balloon
= balloons
[0];
948 const Notification
& notification
= balloon
->notification();
949 GURL EXPECTED_ICON_URL
= embedded_test_server()->GetURL(kExpectedIconUrl
);
950 EXPECT_EQ(EXPECTED_ICON_URL
, notification
.icon_url());
951 EXPECT_EQ(base::ASCIIToUTF16("Title2"), notification
.title());
952 EXPECT_EQ(base::ASCIIToUTF16("Body2"), notification
.message());