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 // If this flakes, use http://crbug.com/62311 and http://crbug.com/74428.
367 // Flaky on Windows: http://crbug.com/437414.
369 #define MAYBE_TestUserGestureInfobar DISABLED_TestUserGestureInfobar
371 #define MAYBE_TestUserGestureInfobar TestUserGestureInfobar
374 IN_PROC_BROWSER_TEST_F(NotificationsTest
, MAYBE_TestUserGestureInfobar
) {
375 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
377 ui_test_utils::NavigateToURL(
379 embedded_test_server()->GetURL(
380 "/notifications/notifications_request_function.html"));
382 // Request permission by calling request() while eval'ing an inline script;
383 // That's considered a user gesture to webkit, and should produce an infobar.
385 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
386 browser()->tab_strip_model()->GetActiveWebContents(),
387 "window.domAutomationController.send(request());",
391 InfoBarService
* infobar_service
= InfoBarService::FromWebContents(
392 browser()->tab_strip_model()->GetWebContentsAt(0));
393 EXPECT_EQ(1U, infobar_service
->infobar_count());
396 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestCreateSimpleNotification
) {
397 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
399 // Creates a simple notification.
401 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
403 std::string result
= CreateSimpleNotification(browser(), true);
404 EXPECT_NE("-1", result
);
406 GURL EXPECTED_ICON_URL
= embedded_test_server()->GetURL(kExpectedIconUrl
);
407 ASSERT_EQ(1, GetNotificationCount());
408 message_center::NotificationList::Notifications notifications
=
409 message_center::MessageCenter::Get()->GetVisibleNotifications();
410 EXPECT_EQ(base::ASCIIToUTF16("My Title"),
411 (*notifications
.rbegin())->title());
412 EXPECT_EQ(base::ASCIIToUTF16("My Body"),
413 (*notifications
.rbegin())->message());
416 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestCloseNotification
) {
417 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
419 // Creates a notification and closes it.
421 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
423 std::string result
= CreateSimpleNotification(browser(), true);
424 EXPECT_NE("-1", result
);
425 ASSERT_EQ(1, GetNotificationCount());
427 message_center::NotificationList::Notifications notifications
=
428 message_center::MessageCenter::Get()->GetVisibleNotifications();
429 message_center::MessageCenter::Get()->RemoveNotification(
430 (*notifications
.rbegin())->id(),
433 ASSERT_EQ(0, GetNotificationCount());
436 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestCancelNotification
) {
437 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
439 // Creates a notification and cancels it in the origin page.
441 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
443 std::string note_id
= CreateSimpleNotification(browser(), true);
444 EXPECT_NE(note_id
, "-1");
446 ASSERT_EQ(1, GetNotificationCount());
447 ASSERT_TRUE(CancelNotification(note_id
.c_str(), browser()));
448 ASSERT_EQ(0, GetNotificationCount());
451 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestPermissionInfobarAppears
) {
452 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
454 // Requests notification privileges and verifies the infobar appears.
455 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
456 ASSERT_TRUE(RequestPermissionAndWait(browser()));
458 ASSERT_EQ(0, GetNotificationCount());
459 ASSERT_NO_FATAL_FAILURE(VerifyInfoBar(browser(), 0));
462 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestAllowOnPermissionInfobar
) {
463 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
465 // Tries to create a notification and clicks allow on the infobar.
466 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
467 // This notification should not be shown because we do not have permission.
468 CreateSimpleNotification(browser(), false);
469 ASSERT_EQ(0, GetNotificationCount());
471 ASSERT_TRUE(RequestPermissionAndWait(browser()));
472 ASSERT_TRUE(PerformActionOnInfoBar(browser(), ALLOW
, 0, 0));
474 CreateSimpleNotification(browser(), true);
475 EXPECT_EQ(1, GetNotificationCount());
478 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestDenyOnPermissionInfobar
) {
479 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
481 // Test that no notification is created
482 // when Deny is chosen from permission infobar.
483 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
484 ASSERT_TRUE(RequestPermissionAndWait(browser()));
485 PerformActionOnInfoBar(browser(), DENY
, 0, 0);
486 CreateSimpleNotification(browser(), false);
487 ASSERT_EQ(0, GetNotificationCount());
488 ContentSettingsForOneType settings
;
489 GetPrefsByContentSetting(CONTENT_SETTING_BLOCK
, &settings
);
490 EXPECT_TRUE(CheckOriginInSetting(settings
, GetTestPageURL()));
493 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestClosePermissionInfobar
) {
494 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
496 // Test that no notification is created when permission infobar is dismissed.
497 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
498 ASSERT_TRUE(RequestPermissionAndWait(browser()));
499 PerformActionOnInfoBar(browser(), DISMISS
, 0, 0);
500 CreateSimpleNotification(browser(), false);
501 ASSERT_EQ(0, GetNotificationCount());
502 ContentSettingsForOneType settings
;
503 GetPrefsByContentSetting(CONTENT_SETTING_BLOCK
, &settings
);
504 EXPECT_EQ(0U, settings
.size());
507 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestAllowNotificationsFromAllSites
) {
508 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
510 // Verify that all domains can be allowed to show notifications.
511 SetDefaultContentSetting(CONTENT_SETTING_ALLOW
);
512 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
514 std::string result
= CreateSimpleNotification(browser(), true);
515 EXPECT_NE("-1", result
);
517 ASSERT_EQ(1, GetNotificationCount());
518 InfoBarService
* infobar_service
= InfoBarService::FromWebContents(
519 browser()->tab_strip_model()->GetWebContentsAt(0));
520 EXPECT_EQ(0U, infobar_service
->infobar_count());
523 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestDenyNotificationsFromAllSites
) {
524 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
526 // Verify that no domain can show notifications.
527 SetDefaultContentSetting(CONTENT_SETTING_BLOCK
);
528 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
530 std::string result
= CreateSimpleNotification(browser(), false);
531 EXPECT_EQ("-1", result
);
533 ASSERT_EQ(0, GetNotificationCount());
536 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestDenyDomainAndAllowAll
) {
537 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
539 // Verify that denying a domain and allowing all shouldn't show
540 // notifications from the denied domain.
541 DenyOrigin(GetTestPageURL().GetOrigin());
542 SetDefaultContentSetting(CONTENT_SETTING_ALLOW
);
544 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
546 std::string result
= CreateSimpleNotification(browser(), false);
547 EXPECT_EQ("-1", result
);
549 ASSERT_EQ(0, GetNotificationCount());
552 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestAllowDomainAndDenyAll
) {
553 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
555 // Verify that allowing a domain and denying all others should show
556 // notifications from the allowed domain.
557 AllowOrigin(GetTestPageURL().GetOrigin());
558 SetDefaultContentSetting(CONTENT_SETTING_BLOCK
);
560 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
562 std::string result
= CreateSimpleNotification(browser(), true);
563 EXPECT_NE("-1", result
);
565 ASSERT_EQ(1, GetNotificationCount());
568 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestDenyAndThenAllowDomain
) {
569 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
571 // Verify that denying and again allowing should show notifications.
572 DenyOrigin(GetTestPageURL().GetOrigin());
574 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
576 std::string result
= CreateSimpleNotification(browser(), false);
577 EXPECT_EQ("-1", result
);
579 ASSERT_EQ(0, GetNotificationCount());
581 AllowOrigin(GetTestPageURL().GetOrigin());
582 result
= CreateSimpleNotification(browser(), true);
583 EXPECT_NE("-1", result
);
585 ASSERT_EQ(1, GetNotificationCount());
586 InfoBarService
* infobar_service
= InfoBarService::FromWebContents(
587 browser()->tab_strip_model()->GetWebContentsAt(0));
588 EXPECT_EQ(0U, infobar_service
->infobar_count());
591 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestCreateDenyCloseNotifications
) {
592 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
594 // Verify able to create, deny, and close the notification.
596 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
597 CreateSimpleNotification(browser(), true);
598 ASSERT_EQ(1, GetNotificationCount());
600 DenyOrigin(GetTestPageURL().GetOrigin());
601 ContentSettingsForOneType settings
;
602 GetPrefsByContentSetting(CONTENT_SETTING_BLOCK
, &settings
);
603 ASSERT_TRUE(CheckOriginInSetting(settings
, GetTestPageURL().GetOrigin()));
605 EXPECT_EQ(1, GetNotificationCount());
606 message_center::NotificationList::Notifications notifications
=
607 message_center::MessageCenter::Get()->GetVisibleNotifications();
608 message_center::MessageCenter::Get()->RemoveNotification(
609 (*notifications
.rbegin())->id(),
611 ASSERT_EQ(0, GetNotificationCount());
614 // Crashes on Linux/Win. See http://crbug.com/160657.
615 IN_PROC_BROWSER_TEST_F(
617 DISABLED_TestOriginPrefsNotSavedInIncognito
) {
618 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
620 // Verify that allow/deny origin preferences are not saved in incognito.
621 Browser
* incognito
= CreateIncognitoBrowser();
622 ui_test_utils::NavigateToURL(incognito
, GetTestPageURL());
623 ASSERT_TRUE(RequestPermissionAndWait(incognito
));
624 PerformActionOnInfoBar(incognito
, DENY
, 0, 0);
625 CloseBrowserWindow(incognito
);
627 incognito
= CreateIncognitoBrowser();
628 ui_test_utils::NavigateToURL(incognito
, GetTestPageURL());
629 ASSERT_TRUE(RequestPermissionAndWait(incognito
));
630 PerformActionOnInfoBar(incognito
, ALLOW
, 0, 0);
631 CreateSimpleNotification(incognito
, true);
632 ASSERT_EQ(1, GetNotificationCount());
633 CloseBrowserWindow(incognito
);
635 incognito
= CreateIncognitoBrowser();
636 ui_test_utils::NavigateToURL(incognito
, GetTestPageURL());
637 ASSERT_TRUE(RequestPermissionAndWait(incognito
));
639 ContentSettingsForOneType settings
;
640 GetPrefsByContentSetting(CONTENT_SETTING_BLOCK
, &settings
);
641 EXPECT_EQ(0U, settings
.size());
642 GetPrefsByContentSetting(CONTENT_SETTING_ALLOW
, &settings
);
643 EXPECT_EQ(0U, settings
.size());
646 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestExitBrowserWithInfobar
) {
647 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
649 // Exit the browser window, when the infobar appears.
650 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
651 ASSERT_TRUE(RequestPermissionAndWait(browser()));
654 // Times out on Windows and Linux. http://crbug.com/168976
655 #if defined(OS_WIN) || defined(OS_LINUX)
656 #define MAYBE_TestCrashTabWithPermissionInfobar \
657 DISABLED_TestCrashTabWithPermissionInfobar
659 #define MAYBE_TestCrashTabWithPermissionInfobar \
660 TestCrashTabWithPermissionInfobar
662 IN_PROC_BROWSER_TEST_F(NotificationsTest
,
663 MAYBE_TestCrashTabWithPermissionInfobar
) {
664 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
666 // Test crashing the tab with permission infobar doesn't crash Chrome.
667 ui_test_utils::NavigateToURLWithDisposition(
669 embedded_test_server()->GetURL("/empty.html"),
671 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB
);
672 browser()->tab_strip_model()->ActivateTabAt(0, true);
673 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
674 ASSERT_TRUE(RequestPermissionAndWait(browser()));
675 CrashTab(browser(), 0);
678 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestIncognitoNotification
) {
679 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
681 // Test notifications in incognito window.
682 Browser
* browser
= CreateIncognitoBrowser();
683 ui_test_utils::NavigateToURL(browser
, GetTestPageURL());
684 browser
->tab_strip_model()->ActivateTabAt(0, true);
685 ASSERT_TRUE(RequestPermissionAndWait(browser
));
686 PerformActionOnInfoBar(browser
, ALLOW
, 0, 0);
687 CreateSimpleNotification(browser
, true);
688 ASSERT_EQ(1, GetNotificationCount());
691 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestCloseTabWithPermissionInfobar
) {
692 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
694 // Test that user can close tab when infobar present.
695 ui_test_utils::NavigateToURLWithDisposition(
699 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB
);
700 browser()->tab_strip_model()->ActivateTabAt(0, true);
701 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
702 ASSERT_TRUE(RequestPermissionAndWait(browser()));
703 content::WebContentsDestroyedWatcher
destroyed_watcher(
704 browser()->tab_strip_model()->GetWebContentsAt(0));
705 browser()->tab_strip_model()->CloseWebContentsAt(0,
706 TabStripModel::CLOSE_NONE
);
707 destroyed_watcher
.Wait();
710 IN_PROC_BROWSER_TEST_F(
712 TestNavigateAwayWithPermissionInfobar
) {
713 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
715 // Test navigating away when an infobar is present,
716 // then trying to create a notification from the same page.
717 ui_test_utils::NavigateToURLWithDisposition(
721 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB
);
722 browser()->tab_strip_model()->ActivateTabAt(0, true);
723 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
724 ASSERT_TRUE(RequestPermissionAndWait(browser()));
725 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
726 ASSERT_TRUE(RequestPermissionAndWait(browser()));
727 PerformActionOnInfoBar(browser(), ALLOW
, 0, 0);
728 CreateSimpleNotification(browser(), true);
729 ASSERT_EQ(1, GetNotificationCount());
732 // See crbug.com/248470
733 #if defined(OS_LINUX)
734 #define MAYBE_TestCrashRendererNotificationRemain \
735 DISABLED_TestCrashRendererNotificationRemain
737 #define MAYBE_TestCrashRendererNotificationRemain \
738 TestCrashRendererNotificationRemain
741 IN_PROC_BROWSER_TEST_F(NotificationsTest
,
742 MAYBE_TestCrashRendererNotificationRemain
) {
743 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
745 // Test crashing renderer does not close or crash notification.
747 ui_test_utils::NavigateToURLWithDisposition(
751 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB
);
752 browser()->tab_strip_model()->ActivateTabAt(0, true);
753 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
754 CreateSimpleNotification(browser(), true);
755 ASSERT_EQ(1, GetNotificationCount());
756 CrashTab(browser(), 0);
757 ASSERT_EQ(1, GetNotificationCount());
760 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestNotificationReplacement
) {
761 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
763 // Test that we can replace a notification using the replaceId.
766 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
768 std::string result
= CreateNotification(
769 browser(), true, "abc.png", "Title1", "Body1", "chat");
770 EXPECT_NE("-1", result
);
772 ASSERT_EQ(1, GetNotificationCount());
774 result
= CreateNotification(
775 browser(), false, "no_such_file.png", "Title2", "Body2", "chat");
776 EXPECT_NE("-1", result
);
778 ASSERT_EQ(1, GetNotificationCount());
779 message_center::NotificationList::Notifications notifications
=
780 message_center::MessageCenter::Get()->GetVisibleNotifications();
781 EXPECT_EQ(base::ASCIIToUTF16("Title2"), (*notifications
.rbegin())->title());
782 EXPECT_EQ(base::ASCIIToUTF16("Body2"),
783 (*notifications
.rbegin())->message());
786 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestLastUsage
) {
787 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
789 HostContentSettingsMap
* settings_map
=
790 browser()->profile()->GetHostContentSettingsMap();
791 base::SimpleTestClock
* clock
= new base::SimpleTestClock();
792 settings_map
->SetPrefClockForTesting(scoped_ptr
<base::Clock
>(clock
));
793 clock
->SetNow(base::Time::UnixEpoch() + base::TimeDelta::FromSeconds(10));
795 // Creates a simple notification.
797 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
799 std::string result
= CreateSimpleNotification(browser(), true);
800 EXPECT_NE("-1", result
);
802 EXPECT_EQ(settings_map
->GetLastUsage(GetTestPageURL().GetOrigin(),
803 GetTestPageURL().GetOrigin(),
804 CONTENT_SETTINGS_TYPE_NOTIFICATIONS
)
808 clock
->Advance(base::TimeDelta::FromSeconds(3));
810 result
= CreateSimpleNotification(browser(), true);
811 EXPECT_NE("-1", result
);
813 EXPECT_EQ(settings_map
->GetLastUsage(GetTestPageURL().GetOrigin(),
814 GetTestPageURL().GetOrigin(),
815 CONTENT_SETTINGS_TYPE_NOTIFICATIONS
)
820 IN_PROC_BROWSER_TEST_F(NotificationsTest
,
821 TestNotificationReplacementReappearance
) {
822 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
824 // Test that we can replace a notification using the tag, and that it will
825 // cause the notification to reappear as a popup again.
828 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
830 ASSERT_EQ(0, GetNotificationPopupCount());
832 std::string result
= CreateNotification(
833 browser(), true, "abc.png", "Title1", "Body1", "chat");
834 EXPECT_NE("-1", result
);
836 ASSERT_EQ(1, GetNotificationPopupCount());
838 message_center::NotificationList::Notifications notifications
=
839 message_center::MessageCenter::Get()->GetVisibleNotifications();
840 message_center::MessageCenter::Get()->ClickOnNotification(
841 (*notifications
.rbegin())->id());
843 ASSERT_EQ(0, GetNotificationPopupCount());
845 result
= CreateNotification(
846 browser(), true, "abc.png", "Title2", "Body2", "chat");
847 EXPECT_NE("-1", result
);
849 ASSERT_EQ(1, GetNotificationPopupCount());
852 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestNotificationValidIcon
) {
853 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
856 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
857 ASSERT_EQ(0, GetNotificationPopupCount());
859 std::string result
= CreateNotification(
860 browser(), true, "icon.png", "Title1", "Body1", "chat");
861 EXPECT_NE("-1", result
);
863 message_center::NotificationList::PopupNotifications notifications
=
864 message_center::MessageCenter::Get()->GetPopupNotifications();
865 ASSERT_EQ(1u, notifications
.size());
867 auto* notification
= *notifications
.rbegin();
869 EXPECT_EQ(100, notification
->icon().Width());
870 EXPECT_EQ(100, notification
->icon().Height());
873 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestNotificationInvalidIcon
) {
874 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
877 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
878 ASSERT_EQ(0, GetNotificationPopupCount());
880 // Not supplying an icon URL.
881 std::string result
= CreateNotification(
882 browser(), true, "", "Title1", "Body1", "chat");
883 EXPECT_NE("-1", result
);
885 message_center::NotificationList::PopupNotifications notifications
=
886 message_center::MessageCenter::Get()->GetPopupNotifications();
887 ASSERT_EQ(1u, notifications
.size());
889 auto* notification
= *notifications
.rbegin();
890 EXPECT_TRUE(notification
->icon().IsEmpty());
892 // Supplying an invalid icon URL.
893 result
= CreateNotification(
894 browser(), true, "invalid.png", "Title1", "Body1", "chat");
895 EXPECT_NE("-1", result
);
897 notifications
= message_center::MessageCenter::Get()->GetPopupNotifications();
898 ASSERT_EQ(1u, notifications
.size());
900 notification
= *notifications
.rbegin();
901 EXPECT_TRUE(notification
->icon().IsEmpty());
904 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestNotificationDoubleClose
) {
905 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
908 ui_test_utils::NavigateToURL(
909 browser(), GetTestPageURLForFile("notification-double-close.html"));
910 ASSERT_EQ(0, GetNotificationPopupCount());
912 std::string result
= CreateNotification(
913 browser(), true, "", "Title1", "Body1", "chat");
914 EXPECT_NE("-1", result
);
916 ASSERT_EQ(1, GetNotificationCount());
917 message_center::NotificationList::Notifications notifications
=
918 message_center::MessageCenter::Get()->GetVisibleNotifications();
919 message_center::MessageCenter::Get()->RemoveNotification(
920 (*notifications
.rbegin())->id(),
923 ASSERT_EQ(0, GetNotificationCount());
925 // Calling WebContents::IsCrashed() will return FALSE here, even if the WC did
926 // crash. Work around this timing issue by creating another notification,
927 // which requires interaction with the renderer process.
928 result
= CreateNotification(browser(), true, "", "Title1", "Body1", "chat");
929 EXPECT_NE("-1", result
);