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 "base/test/simple_test_clock.h"
17 #include "base/time/clock.h"
18 #include "chrome/browser/browser_process.h"
19 #include "chrome/browser/chrome_notification_types.h"
20 #include "chrome/browser/infobars/infobar_service.h"
21 #include "chrome/browser/notifications/desktop_notification_profile_util.h"
22 #include "chrome/browser/notifications/notification.h"
23 #include "chrome/browser/profiles/profile.h"
24 #include "chrome/browser/ui/browser.h"
25 #include "chrome/browser/ui/browser_tabstrip.h"
26 #include "chrome/browser/ui/browser_window.h"
27 #include "chrome/browser/ui/tabs/tab_strip_model.h"
28 #include "chrome/test/base/in_process_browser_test.h"
29 #include "chrome/test/base/ui_test_utils.h"
30 #include "components/content_settings/core/browser/host_content_settings_map.h"
31 #include "components/content_settings/core/common/content_settings.h"
32 #include "components/content_settings/core/common/content_settings_pattern.h"
33 #include "components/infobars/core/confirm_infobar_delegate.h"
34 #include "components/infobars/core/infobar.h"
35 #include "content/public/browser/notification_service.h"
36 #include "content/public/browser/notification_source.h"
37 #include "content/public/browser/notification_types.h"
38 #include "content/public/browser/render_view_host.h"
39 #include "content/public/browser/web_contents.h"
40 #include "content/public/test/browser_test_utils.h"
41 #include "content/public/test/test_utils.h"
42 #include "net/base/net_util.h"
43 #include "net/test/embedded_test_server/embedded_test_server.h"
44 #include "testing/gtest/include/gtest/gtest.h"
45 #include "ui/base/window_open_disposition.h"
46 #include "ui/message_center/message_center.h"
47 #include "ui/message_center/message_center_observer.h"
52 const char kExpectedIconUrl
[] = "/notifications/no_such_file.png";
60 class NotificationChangeObserver
{
62 virtual ~NotificationChangeObserver() {}
63 virtual bool Wait() = 0;
66 class MessageCenterChangeObserver
67 : public message_center::MessageCenterObserver
,
68 public NotificationChangeObserver
{
70 MessageCenterChangeObserver()
71 : notification_received_(false) {
72 message_center::MessageCenter::Get()->AddObserver(this);
75 ~MessageCenterChangeObserver() override
{
76 message_center::MessageCenter::Get()->RemoveObserver(this);
79 // NotificationChangeObserver:
80 bool Wait() override
{
81 if (notification_received_
)
84 message_loop_runner_
= new content::MessageLoopRunner
;
85 message_loop_runner_
->Run();
86 return notification_received_
;
89 // message_center::MessageCenterObserver:
90 void OnNotificationAdded(const std::string
& notification_id
) override
{
91 OnMessageCenterChanged();
94 void OnNotificationRemoved(const std::string
& notification_id
,
95 bool by_user
) override
{
96 OnMessageCenterChanged();
99 void OnNotificationUpdated(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();
123 int GetNotificationPopupCount();
125 void CloseBrowserWindow(Browser
* browser
);
126 void CrashTab(Browser
* browser
, int index
);
128 void DenyOrigin(const GURL
& origin
);
129 void AllowOrigin(const GURL
& origin
);
130 void AllowAllOrigins();
131 void SetDefaultContentSetting(ContentSetting setting
);
133 void VerifyInfoBar(const Browser
* browser
, int index
);
134 std::string
CreateNotification(Browser
* browser
,
135 bool wait_for_new_balloon
,
139 const char* replace_id
);
140 std::string
CreateSimpleNotification(Browser
* browser
,
141 bool wait_for_new_balloon
);
142 bool RequestPermissionAndWait(Browser
* browser
);
143 bool CancelNotification(const char* notification_id
, Browser
* browser
);
144 bool PerformActionOnInfoBar(Browser
* browser
,
145 InfobarAction action
,
146 size_t infobar_index
,
148 void GetPrefsByContentSetting(ContentSetting setting
,
149 ContentSettingsForOneType
* settings
);
150 bool CheckOriginInSetting(const ContentSettingsForOneType
& settings
,
153 GURL
GetTestPageURLForFile(const std::string
& file
) const {
154 return embedded_test_server()->GetURL(
155 std::string("/notifications/") + file
);
158 GURL
GetTestPageURL() const {
159 return GetTestPageURLForFile("notification_tester.html");
163 void DropOriginPreference(const GURL
& origin
);
166 int NotificationsTest::GetNotificationCount() {
167 return message_center::MessageCenter::Get()->NotificationCount();
170 int NotificationsTest::GetNotificationPopupCount() {
171 return message_center::MessageCenter::Get()->GetPopupNotifications().size();
174 void NotificationsTest::CloseBrowserWindow(Browser
* browser
) {
175 content::WindowedNotificationObserver
observer(
176 chrome::NOTIFICATION_BROWSER_CLOSED
,
177 content::Source
<Browser
>(browser
));
178 browser
->window()->Close();
182 void NotificationsTest::CrashTab(Browser
* browser
, int index
) {
183 content::CrashTab(browser
->tab_strip_model()->GetWebContentsAt(index
));
186 void NotificationsTest::DenyOrigin(const GURL
& origin
) {
187 DropOriginPreference(origin
);
188 DesktopNotificationProfileUtil::DenyPermission(browser()->profile(), origin
);
191 void NotificationsTest::AllowOrigin(const GURL
& origin
) {
192 DropOriginPreference(origin
);
193 DesktopNotificationProfileUtil::GrantPermission(browser()->profile(), origin
);
196 void NotificationsTest::AllowAllOrigins() {
198 browser()->profile()->GetHostContentSettingsMap()->ClearSettingsForOneType(
199 CONTENT_SETTINGS_TYPE_NOTIFICATIONS
);
200 SetDefaultContentSetting(CONTENT_SETTING_ALLOW
);
203 void NotificationsTest::SetDefaultContentSetting(ContentSetting setting
) {
204 browser()->profile()->GetHostContentSettingsMap()->SetDefaultContentSetting(
205 CONTENT_SETTINGS_TYPE_NOTIFICATIONS
, setting
);
208 void NotificationsTest::VerifyInfoBar(const Browser
* browser
, int index
) {
209 InfoBarService
* infobar_service
= InfoBarService::FromWebContents(
210 browser
->tab_strip_model()->GetWebContentsAt(index
));
212 ASSERT_EQ(1U, infobar_service
->infobar_count());
213 ConfirmInfoBarDelegate
* confirm_infobar
=
214 infobar_service
->infobar_at(0)->delegate()->AsConfirmInfoBarDelegate();
215 ASSERT_TRUE(confirm_infobar
);
216 int buttons
= confirm_infobar
->GetButtons();
217 EXPECT_TRUE(buttons
& ConfirmInfoBarDelegate::BUTTON_OK
);
218 EXPECT_TRUE(buttons
& ConfirmInfoBarDelegate::BUTTON_CANCEL
);
221 std::string
NotificationsTest::CreateNotification(
223 bool wait_for_new_balloon
,
227 const char* replace_id
) {
228 std::string script
= base::StringPrintf(
229 "createNotification('%s', '%s', '%s', '%s');",
230 icon
, title
, body
, replace_id
);
232 MessageCenterChangeObserver observer
;
234 bool success
= content::ExecuteScriptAndExtractString(
235 browser
->tab_strip_model()->GetActiveWebContents(),
238 if (success
&& result
!= "-1" && wait_for_new_balloon
)
239 success
= observer
.Wait();
240 EXPECT_TRUE(success
);
245 std::string
NotificationsTest::CreateSimpleNotification(
247 bool wait_for_new_balloon
) {
248 return CreateNotification(
249 browser
, wait_for_new_balloon
,
250 "no_such_file.png", "My Title", "My Body", "");
253 bool NotificationsTest::RequestPermissionAndWait(Browser
* browser
) {
254 InfoBarService
* infobar_service
= InfoBarService::FromWebContents(
255 browser
->tab_strip_model()->GetActiveWebContents());
256 content::WindowedNotificationObserver
observer(
257 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED
,
258 content::Source
<InfoBarService
>(infobar_service
));
260 bool success
= content::ExecuteScriptAndExtractString(
261 browser
->tab_strip_model()->GetActiveWebContents(),
262 "requestPermission();",
264 if (!success
|| result
!= "1")
270 bool NotificationsTest::CancelNotification(
271 const char* notification_id
,
273 std::string script
= base::StringPrintf(
274 "cancelNotification('%s');",
277 MessageCenterChangeObserver observer
;
279 bool success
= content::ExecuteScriptAndExtractString(
280 browser
->tab_strip_model()->GetActiveWebContents(),
283 if (!success
|| result
!= "1")
285 return observer
.Wait();
288 bool NotificationsTest::PerformActionOnInfoBar(
290 InfobarAction action
,
291 size_t infobar_index
,
293 InfoBarService
* infobar_service
= InfoBarService::FromWebContents(
294 browser
->tab_strip_model()->GetWebContentsAt(tab_index
));
295 if (infobar_index
>= infobar_service
->infobar_count()) {
300 infobars::InfoBar
* infobar
= infobar_service
->infobar_at(infobar_index
);
301 infobars::InfoBarDelegate
* infobar_delegate
= infobar
->delegate();
304 infobar_delegate
->InfoBarDismissed();
305 infobar_service
->RemoveInfoBar(infobar
);
309 ConfirmInfoBarDelegate
* confirm_infobar_delegate
=
310 infobar_delegate
->AsConfirmInfoBarDelegate();
311 if (!confirm_infobar_delegate
) {
313 } else if (confirm_infobar_delegate
->Accept()) {
314 infobar_service
->RemoveInfoBar(infobar
);
320 ConfirmInfoBarDelegate
* confirm_infobar_delegate
=
321 infobar_delegate
->AsConfirmInfoBarDelegate();
322 if (!confirm_infobar_delegate
) {
324 } else if (confirm_infobar_delegate
->Cancel()) {
325 infobar_service
->RemoveInfoBar(infobar
);
334 void NotificationsTest::GetPrefsByContentSetting(
335 ContentSetting setting
,
336 ContentSettingsForOneType
* settings
) {
337 DesktopNotificationProfileUtil::GetNotificationsSettings(
338 browser()->profile(), settings
);
339 for (ContentSettingsForOneType::iterator it
= settings
->begin();
340 it
!= settings
->end(); ) {
341 if (it
->setting
!= setting
|| it
->source
.compare("preference") != 0)
342 it
= settings
->erase(it
);
348 bool NotificationsTest::CheckOriginInSetting(
349 const ContentSettingsForOneType
& settings
,
350 const GURL
& origin
) {
351 ContentSettingsPattern pattern
=
352 ContentSettingsPattern::FromURLNoWildcard(origin
);
353 for (ContentSettingsForOneType::const_iterator it
= settings
.begin();
354 it
!= settings
.end(); ++it
) {
355 if (it
->primary_pattern
== pattern
)
361 void NotificationsTest::DropOriginPreference(const GURL
& origin
) {
362 DesktopNotificationProfileUtil::ClearSetting(browser()->profile(),
363 ContentSettingsPattern::FromURLNoWildcard(origin
));
366 // Flaky on Windows, Mac, Linux: http://crbug.com/437414.
367 IN_PROC_BROWSER_TEST_F(NotificationsTest
, DISABLED_TestUserGestureInfobar
) {
368 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
370 ui_test_utils::NavigateToURL(
372 embedded_test_server()->GetURL(
373 "/notifications/notifications_request_function.html"));
375 // Request permission by calling request() while eval'ing an inline script;
376 // That's considered a user gesture to webkit, and should produce an infobar.
378 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
379 browser()->tab_strip_model()->GetActiveWebContents(),
380 "window.domAutomationController.send(request());",
384 InfoBarService
* infobar_service
= InfoBarService::FromWebContents(
385 browser()->tab_strip_model()->GetWebContentsAt(0));
386 EXPECT_EQ(1U, infobar_service
->infobar_count());
389 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestCreateSimpleNotification
) {
390 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
392 // Creates a simple notification.
394 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
396 std::string result
= CreateSimpleNotification(browser(), true);
397 EXPECT_NE("-1", result
);
399 GURL EXPECTED_ICON_URL
= embedded_test_server()->GetURL(kExpectedIconUrl
);
400 ASSERT_EQ(1, GetNotificationCount());
401 message_center::NotificationList::Notifications notifications
=
402 message_center::MessageCenter::Get()->GetVisibleNotifications();
403 EXPECT_EQ(base::ASCIIToUTF16("My Title"),
404 (*notifications
.rbegin())->title());
405 EXPECT_EQ(base::ASCIIToUTF16("My Body"),
406 (*notifications
.rbegin())->message());
409 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestCloseNotification
) {
410 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
412 // Creates a notification and closes it.
414 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
416 std::string result
= CreateSimpleNotification(browser(), true);
417 EXPECT_NE("-1", result
);
418 ASSERT_EQ(1, GetNotificationCount());
420 message_center::NotificationList::Notifications notifications
=
421 message_center::MessageCenter::Get()->GetVisibleNotifications();
422 message_center::MessageCenter::Get()->RemoveNotification(
423 (*notifications
.rbegin())->id(),
426 ASSERT_EQ(0, GetNotificationCount());
429 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestCancelNotification
) {
430 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
432 // Creates a notification and cancels it in the origin page.
434 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
436 std::string note_id
= CreateSimpleNotification(browser(), true);
437 EXPECT_NE(note_id
, "-1");
439 ASSERT_EQ(1, GetNotificationCount());
440 ASSERT_TRUE(CancelNotification(note_id
.c_str(), browser()));
441 ASSERT_EQ(0, GetNotificationCount());
444 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestPermissionInfobarAppears
) {
445 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
447 // Requests notification privileges and verifies the infobar appears.
448 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
449 ASSERT_TRUE(RequestPermissionAndWait(browser()));
451 ASSERT_EQ(0, GetNotificationCount());
452 ASSERT_NO_FATAL_FAILURE(VerifyInfoBar(browser(), 0));
455 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestAllowOnPermissionInfobar
) {
456 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
458 // Tries to create a notification and clicks allow on the infobar.
459 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
460 // This notification should not be shown because we do not have permission.
461 CreateSimpleNotification(browser(), false);
462 ASSERT_EQ(0, GetNotificationCount());
464 ASSERT_TRUE(RequestPermissionAndWait(browser()));
465 ASSERT_TRUE(PerformActionOnInfoBar(browser(), ALLOW
, 0, 0));
467 CreateSimpleNotification(browser(), true);
468 EXPECT_EQ(1, GetNotificationCount());
471 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestDenyOnPermissionInfobar
) {
472 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
474 // Test that no notification is created
475 // when Deny is chosen from permission infobar.
476 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
477 ASSERT_TRUE(RequestPermissionAndWait(browser()));
478 PerformActionOnInfoBar(browser(), DENY
, 0, 0);
479 CreateSimpleNotification(browser(), false);
480 ASSERT_EQ(0, GetNotificationCount());
481 ContentSettingsForOneType settings
;
482 GetPrefsByContentSetting(CONTENT_SETTING_BLOCK
, &settings
);
483 EXPECT_TRUE(CheckOriginInSetting(settings
, GetTestPageURL()));
486 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestClosePermissionInfobar
) {
487 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
489 // Test that no notification is created when permission infobar is dismissed.
490 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
491 ASSERT_TRUE(RequestPermissionAndWait(browser()));
492 PerformActionOnInfoBar(browser(), DISMISS
, 0, 0);
493 CreateSimpleNotification(browser(), false);
494 ASSERT_EQ(0, GetNotificationCount());
495 ContentSettingsForOneType settings
;
496 GetPrefsByContentSetting(CONTENT_SETTING_BLOCK
, &settings
);
497 EXPECT_EQ(0U, settings
.size());
500 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestAllowNotificationsFromAllSites
) {
501 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
503 // Verify that all domains can be allowed to show notifications.
504 SetDefaultContentSetting(CONTENT_SETTING_ALLOW
);
505 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
507 std::string result
= CreateSimpleNotification(browser(), true);
508 EXPECT_NE("-1", result
);
510 ASSERT_EQ(1, GetNotificationCount());
511 InfoBarService
* infobar_service
= InfoBarService::FromWebContents(
512 browser()->tab_strip_model()->GetWebContentsAt(0));
513 EXPECT_EQ(0U, infobar_service
->infobar_count());
516 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestDenyNotificationsFromAllSites
) {
517 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
519 // Verify that no domain can show notifications.
520 SetDefaultContentSetting(CONTENT_SETTING_BLOCK
);
521 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
523 std::string result
= CreateSimpleNotification(browser(), false);
524 EXPECT_EQ("-1", result
);
526 ASSERT_EQ(0, GetNotificationCount());
529 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestDenyDomainAndAllowAll
) {
530 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
532 // Verify that denying a domain and allowing all shouldn't show
533 // notifications from the denied domain.
534 DenyOrigin(GetTestPageURL().GetOrigin());
535 SetDefaultContentSetting(CONTENT_SETTING_ALLOW
);
537 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
539 std::string result
= CreateSimpleNotification(browser(), false);
540 EXPECT_EQ("-1", result
);
542 ASSERT_EQ(0, GetNotificationCount());
545 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestAllowDomainAndDenyAll
) {
546 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
548 // Verify that allowing a domain and denying all others should show
549 // notifications from the allowed domain.
550 AllowOrigin(GetTestPageURL().GetOrigin());
551 SetDefaultContentSetting(CONTENT_SETTING_BLOCK
);
553 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
555 std::string result
= CreateSimpleNotification(browser(), true);
556 EXPECT_NE("-1", result
);
558 ASSERT_EQ(1, GetNotificationCount());
561 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestDenyAndThenAllowDomain
) {
562 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
564 // Verify that denying and again allowing should show notifications.
565 DenyOrigin(GetTestPageURL().GetOrigin());
567 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
569 std::string result
= CreateSimpleNotification(browser(), false);
570 EXPECT_EQ("-1", result
);
572 ASSERT_EQ(0, GetNotificationCount());
574 AllowOrigin(GetTestPageURL().GetOrigin());
575 result
= CreateSimpleNotification(browser(), true);
576 EXPECT_NE("-1", result
);
578 ASSERT_EQ(1, GetNotificationCount());
579 InfoBarService
* infobar_service
= InfoBarService::FromWebContents(
580 browser()->tab_strip_model()->GetWebContentsAt(0));
581 EXPECT_EQ(0U, infobar_service
->infobar_count());
584 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestCreateDenyCloseNotifications
) {
585 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
587 // Verify able to create, deny, and close the notification.
589 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
590 CreateSimpleNotification(browser(), true);
591 ASSERT_EQ(1, GetNotificationCount());
593 DenyOrigin(GetTestPageURL().GetOrigin());
594 ContentSettingsForOneType settings
;
595 GetPrefsByContentSetting(CONTENT_SETTING_BLOCK
, &settings
);
596 ASSERT_TRUE(CheckOriginInSetting(settings
, GetTestPageURL().GetOrigin()));
598 EXPECT_EQ(1, GetNotificationCount());
599 message_center::NotificationList::Notifications notifications
=
600 message_center::MessageCenter::Get()->GetVisibleNotifications();
601 message_center::MessageCenter::Get()->RemoveNotification(
602 (*notifications
.rbegin())->id(),
604 ASSERT_EQ(0, GetNotificationCount());
607 // Crashes on Linux/Win. See http://crbug.com/160657.
608 IN_PROC_BROWSER_TEST_F(
610 DISABLED_TestOriginPrefsNotSavedInIncognito
) {
611 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
613 // Verify that allow/deny origin preferences are not saved in incognito.
614 Browser
* incognito
= CreateIncognitoBrowser();
615 ui_test_utils::NavigateToURL(incognito
, GetTestPageURL());
616 ASSERT_TRUE(RequestPermissionAndWait(incognito
));
617 PerformActionOnInfoBar(incognito
, DENY
, 0, 0);
618 CloseBrowserWindow(incognito
);
620 incognito
= CreateIncognitoBrowser();
621 ui_test_utils::NavigateToURL(incognito
, GetTestPageURL());
622 ASSERT_TRUE(RequestPermissionAndWait(incognito
));
623 PerformActionOnInfoBar(incognito
, ALLOW
, 0, 0);
624 CreateSimpleNotification(incognito
, true);
625 ASSERT_EQ(1, GetNotificationCount());
626 CloseBrowserWindow(incognito
);
628 incognito
= CreateIncognitoBrowser();
629 ui_test_utils::NavigateToURL(incognito
, GetTestPageURL());
630 ASSERT_TRUE(RequestPermissionAndWait(incognito
));
632 ContentSettingsForOneType settings
;
633 GetPrefsByContentSetting(CONTENT_SETTING_BLOCK
, &settings
);
634 EXPECT_EQ(0U, settings
.size());
635 GetPrefsByContentSetting(CONTENT_SETTING_ALLOW
, &settings
);
636 EXPECT_EQ(0U, settings
.size());
639 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestExitBrowserWithInfobar
) {
640 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
642 // Exit the browser window, when the infobar appears.
643 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
644 ASSERT_TRUE(RequestPermissionAndWait(browser()));
647 // Times out on Windows and Linux. http://crbug.com/168976
648 #if defined(OS_WIN) || defined(OS_LINUX)
649 #define MAYBE_TestCrashTabWithPermissionInfobar \
650 DISABLED_TestCrashTabWithPermissionInfobar
652 #define MAYBE_TestCrashTabWithPermissionInfobar \
653 TestCrashTabWithPermissionInfobar
655 IN_PROC_BROWSER_TEST_F(NotificationsTest
,
656 MAYBE_TestCrashTabWithPermissionInfobar
) {
657 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
659 // Test crashing the tab with permission infobar doesn't crash Chrome.
660 ui_test_utils::NavigateToURLWithDisposition(
662 embedded_test_server()->GetURL("/empty.html"),
664 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB
);
665 browser()->tab_strip_model()->ActivateTabAt(0, true);
666 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
667 ASSERT_TRUE(RequestPermissionAndWait(browser()));
668 CrashTab(browser(), 0);
671 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestIncognitoNotification
) {
672 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
674 // Test notifications in incognito window.
675 Browser
* browser
= CreateIncognitoBrowser();
676 ui_test_utils::NavigateToURL(browser
, GetTestPageURL());
677 browser
->tab_strip_model()->ActivateTabAt(0, true);
678 ASSERT_TRUE(RequestPermissionAndWait(browser
));
679 PerformActionOnInfoBar(browser
, ALLOW
, 0, 0);
680 CreateSimpleNotification(browser
, true);
681 ASSERT_EQ(1, GetNotificationCount());
684 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestCloseTabWithPermissionInfobar
) {
685 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
687 // Test that user can close tab when infobar present.
688 ui_test_utils::NavigateToURLWithDisposition(
692 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB
);
693 browser()->tab_strip_model()->ActivateTabAt(0, true);
694 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
695 ASSERT_TRUE(RequestPermissionAndWait(browser()));
696 content::WebContentsDestroyedWatcher
destroyed_watcher(
697 browser()->tab_strip_model()->GetWebContentsAt(0));
698 browser()->tab_strip_model()->CloseWebContentsAt(0,
699 TabStripModel::CLOSE_NONE
);
700 destroyed_watcher
.Wait();
703 IN_PROC_BROWSER_TEST_F(
705 TestNavigateAwayWithPermissionInfobar
) {
706 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
708 // Test navigating away when an infobar is present,
709 // then trying to create a notification from the same page.
710 ui_test_utils::NavigateToURLWithDisposition(
714 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB
);
715 browser()->tab_strip_model()->ActivateTabAt(0, true);
716 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
717 ASSERT_TRUE(RequestPermissionAndWait(browser()));
718 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
719 ASSERT_TRUE(RequestPermissionAndWait(browser()));
720 PerformActionOnInfoBar(browser(), ALLOW
, 0, 0);
721 CreateSimpleNotification(browser(), true);
722 ASSERT_EQ(1, GetNotificationCount());
725 // See crbug.com/248470
726 #if defined(OS_LINUX)
727 #define MAYBE_TestCrashRendererNotificationRemain \
728 DISABLED_TestCrashRendererNotificationRemain
730 #define MAYBE_TestCrashRendererNotificationRemain \
731 TestCrashRendererNotificationRemain
734 IN_PROC_BROWSER_TEST_F(NotificationsTest
,
735 MAYBE_TestCrashRendererNotificationRemain
) {
736 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
738 // Test crashing renderer does not close or crash notification.
740 ui_test_utils::NavigateToURLWithDisposition(
744 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB
);
745 browser()->tab_strip_model()->ActivateTabAt(0, true);
746 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
747 CreateSimpleNotification(browser(), true);
748 ASSERT_EQ(1, GetNotificationCount());
749 CrashTab(browser(), 0);
750 ASSERT_EQ(1, GetNotificationCount());
753 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestNotificationReplacement
) {
754 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
756 // Test that we can replace a notification using the replaceId.
759 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
761 std::string result
= CreateNotification(
762 browser(), true, "abc.png", "Title1", "Body1", "chat");
763 EXPECT_NE("-1", result
);
765 ASSERT_EQ(1, GetNotificationCount());
767 result
= CreateNotification(
768 browser(), false, "no_such_file.png", "Title2", "Body2", "chat");
769 EXPECT_NE("-1", result
);
771 ASSERT_EQ(1, GetNotificationCount());
772 message_center::NotificationList::Notifications notifications
=
773 message_center::MessageCenter::Get()->GetVisibleNotifications();
774 EXPECT_EQ(base::ASCIIToUTF16("Title2"), (*notifications
.rbegin())->title());
775 EXPECT_EQ(base::ASCIIToUTF16("Body2"),
776 (*notifications
.rbegin())->message());
779 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestLastUsage
) {
780 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
782 HostContentSettingsMap
* settings_map
=
783 browser()->profile()->GetHostContentSettingsMap();
784 base::SimpleTestClock
* clock
= new base::SimpleTestClock();
785 settings_map
->SetPrefClockForTesting(scoped_ptr
<base::Clock
>(clock
));
786 clock
->SetNow(base::Time::UnixEpoch() + base::TimeDelta::FromSeconds(10));
788 // Creates a simple notification.
790 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
792 std::string result
= CreateSimpleNotification(browser(), true);
793 EXPECT_NE("-1", result
);
795 EXPECT_EQ(settings_map
->GetLastUsage(GetTestPageURL().GetOrigin(),
796 GetTestPageURL().GetOrigin(),
797 CONTENT_SETTINGS_TYPE_NOTIFICATIONS
)
801 clock
->Advance(base::TimeDelta::FromSeconds(3));
803 result
= CreateSimpleNotification(browser(), true);
804 EXPECT_NE("-1", result
);
806 EXPECT_EQ(settings_map
->GetLastUsage(GetTestPageURL().GetOrigin(),
807 GetTestPageURL().GetOrigin(),
808 CONTENT_SETTINGS_TYPE_NOTIFICATIONS
)
813 IN_PROC_BROWSER_TEST_F(NotificationsTest
,
814 TestNotificationReplacementReappearance
) {
815 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
817 // Test that we can replace a notification using the tag, and that it will
818 // cause the notification to reappear as a popup again.
821 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
823 ASSERT_EQ(0, GetNotificationPopupCount());
825 std::string result
= CreateNotification(
826 browser(), true, "abc.png", "Title1", "Body1", "chat");
827 EXPECT_NE("-1", result
);
829 ASSERT_EQ(1, GetNotificationPopupCount());
831 message_center::NotificationList::Notifications notifications
=
832 message_center::MessageCenter::Get()->GetVisibleNotifications();
833 message_center::MessageCenter::Get()->ClickOnNotification(
834 (*notifications
.rbegin())->id());
836 ASSERT_EQ(0, GetNotificationPopupCount());
838 result
= CreateNotification(
839 browser(), true, "abc.png", "Title2", "Body2", "chat");
840 EXPECT_NE("-1", result
);
842 ASSERT_EQ(1, GetNotificationPopupCount());
845 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestNotificationValidIcon
) {
846 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
849 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
850 ASSERT_EQ(0, GetNotificationPopupCount());
852 std::string result
= CreateNotification(
853 browser(), true, "icon.png", "Title1", "Body1", "chat");
854 EXPECT_NE("-1", result
);
856 message_center::NotificationList::PopupNotifications notifications
=
857 message_center::MessageCenter::Get()->GetPopupNotifications();
858 ASSERT_EQ(1u, notifications
.size());
860 auto* notification
= *notifications
.rbegin();
862 EXPECT_EQ(100, notification
->icon().Width());
863 EXPECT_EQ(100, notification
->icon().Height());
866 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestNotificationInvalidIcon
) {
867 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
870 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
871 ASSERT_EQ(0, GetNotificationPopupCount());
873 // Not supplying an icon URL.
874 std::string result
= CreateNotification(
875 browser(), true, "", "Title1", "Body1", "chat");
876 EXPECT_NE("-1", result
);
878 message_center::NotificationList::PopupNotifications notifications
=
879 message_center::MessageCenter::Get()->GetPopupNotifications();
880 ASSERT_EQ(1u, notifications
.size());
882 auto* notification
= *notifications
.rbegin();
883 EXPECT_TRUE(notification
->icon().IsEmpty());
885 // Supplying an invalid icon URL.
886 result
= CreateNotification(
887 browser(), true, "invalid.png", "Title1", "Body1", "chat");
888 EXPECT_NE("-1", result
);
890 notifications
= message_center::MessageCenter::Get()->GetPopupNotifications();
891 ASSERT_EQ(1u, notifications
.size());
893 notification
= *notifications
.rbegin();
894 EXPECT_TRUE(notification
->icon().IsEmpty());
897 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestNotificationDoubleClose
) {
898 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
901 ui_test_utils::NavigateToURL(
902 browser(), GetTestPageURLForFile("notification-double-close.html"));
903 ASSERT_EQ(0, GetNotificationPopupCount());
905 std::string result
= CreateNotification(
906 browser(), true, "", "Title1", "Body1", "chat");
907 EXPECT_NE("-1", result
);
909 ASSERT_EQ(1, GetNotificationCount());
910 message_center::NotificationList::Notifications notifications
=
911 message_center::MessageCenter::Get()->GetVisibleNotifications();
912 message_center::MessageCenter::Get()->RemoveNotification(
913 (*notifications
.rbegin())->id(),
916 ASSERT_EQ(0, GetNotificationCount());
918 // Calling WebContents::IsCrashed() will return FALSE here, even if the WC did
919 // crash. Work around this timing issue by creating another notification,
920 // which requires interaction with the renderer process.
921 result
= CreateNotification(browser(), true, "", "Title1", "Body1", "chat");
922 EXPECT_NE("-1", result
);