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/content_settings/host_content_settings_map_factory.h"
21 #include "chrome/browser/infobars/infobar_service.h"
22 #include "chrome/browser/notifications/desktop_notification_profile_util.h"
23 #include "chrome/browser/notifications/notification.h"
24 #include "chrome/browser/profiles/profile.h"
25 #include "chrome/browser/ui/browser.h"
26 #include "chrome/browser/ui/browser_tabstrip.h"
27 #include "chrome/browser/ui/browser_window.h"
28 #include "chrome/browser/ui/tabs/tab_strip_model.h"
29 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h"
30 #include "chrome/test/base/in_process_browser_test.h"
31 #include "chrome/test/base/ui_test_utils.h"
32 #include "components/content_settings/core/browser/host_content_settings_map.h"
33 #include "components/content_settings/core/common/content_settings.h"
34 #include "components/content_settings/core/common/content_settings_pattern.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";
54 class NotificationChangeObserver
{
56 virtual ~NotificationChangeObserver() {}
57 virtual bool Wait() = 0;
60 class MessageCenterChangeObserver
61 : public message_center::MessageCenterObserver
,
62 public NotificationChangeObserver
{
64 MessageCenterChangeObserver()
65 : notification_received_(false) {
66 message_center::MessageCenter::Get()->AddObserver(this);
69 ~MessageCenterChangeObserver() override
{
70 message_center::MessageCenter::Get()->RemoveObserver(this);
73 // NotificationChangeObserver:
74 bool Wait() override
{
75 if (notification_received_
)
78 message_loop_runner_
= new content::MessageLoopRunner
;
79 message_loop_runner_
->Run();
80 return notification_received_
;
83 // message_center::MessageCenterObserver:
84 void OnNotificationAdded(const std::string
& notification_id
) override
{
85 OnMessageCenterChanged();
88 void OnNotificationRemoved(const std::string
& notification_id
,
89 bool by_user
) override
{
90 OnMessageCenterChanged();
93 void OnNotificationUpdated(const std::string
& notification_id
) override
{
94 OnMessageCenterChanged();
97 void OnMessageCenterChanged() {
98 notification_received_
= true;
99 if (message_loop_runner_
.get())
100 message_loop_runner_
->Quit();
103 bool notification_received_
;
104 scoped_refptr
<content::MessageLoopRunner
> message_loop_runner_
;
106 DISALLOW_COPY_AND_ASSIGN(MessageCenterChangeObserver
);
109 // Used to observe the creation of permission prompt without responding.
110 class PermissionRequestObserver
: public PermissionBubbleManager::Observer
{
112 explicit PermissionRequestObserver(content::WebContents
* web_contents
)
113 : bubble_manager_(PermissionBubbleManager::FromWebContents(web_contents
)),
114 request_shown_(false),
115 message_loop_runner_(new content::MessageLoopRunner
) {
116 bubble_manager_
->AddObserver(this);
118 ~PermissionRequestObserver() override
{
119 // Safe to remove twice if it happens.
120 bubble_manager_
->RemoveObserver(this);
123 void Wait() { message_loop_runner_
->Run(); }
125 bool request_shown() { return request_shown_
; }
128 // PermissionBubbleManager::Observer
129 void OnBubbleAdded() override
{
130 request_shown_
= true;
131 bubble_manager_
->RemoveObserver(this);
132 message_loop_runner_
->Quit();
135 PermissionBubbleManager
* bubble_manager_
;
137 scoped_refptr
<content::MessageLoopRunner
> message_loop_runner_
;
139 DISALLOW_COPY_AND_ASSIGN(PermissionRequestObserver
);
144 class NotificationsTest
: public InProcessBrowserTest
{
146 NotificationsTest() {}
149 int GetNotificationCount();
150 int GetNotificationPopupCount();
152 void CloseBrowserWindow(Browser
* browser
);
153 void CrashTab(Browser
* browser
, int index
);
155 void DenyOrigin(const GURL
& origin
);
156 void AllowOrigin(const GURL
& origin
);
157 void AllowAllOrigins();
158 void SetDefaultContentSetting(ContentSetting setting
);
160 std::string
CreateNotification(Browser
* browser
,
161 bool wait_for_new_balloon
,
165 const char* replace_id
);
166 std::string
CreateSimpleNotification(Browser
* browser
,
167 bool wait_for_new_balloon
);
168 bool RequestAndAcceptPermission(Browser
* browser
);
169 bool RequestAndDenyPermission(Browser
* browser
);
170 bool RequestAndDismissPermission(Browser
* browser
);
171 bool RequestPermissionAndWait(Browser
* browser
);
172 bool CancelNotification(const char* notification_id
, Browser
* browser
);
173 void GetPrefsByContentSetting(ContentSetting setting
,
174 ContentSettingsForOneType
* settings
);
175 bool CheckOriginInSetting(const ContentSettingsForOneType
& settings
,
178 GURL
GetTestPageURLForFile(const std::string
& file
) const {
179 return embedded_test_server()->GetURL(
180 std::string("/notifications/") + file
);
183 GURL
GetTestPageURL() const {
184 return GetTestPageURLForFile("notification_tester.html");
187 content::WebContents
* GetActiveWebContents(Browser
* browser
) {
188 return browser
->tab_strip_model()->GetActiveWebContents();
192 void DropOriginPreference(const GURL
& origin
);
193 std::string
RequestAndRespondToPermission(
195 PermissionBubbleManager::AutoResponseType bubble_response
);
198 int NotificationsTest::GetNotificationCount() {
199 return message_center::MessageCenter::Get()->NotificationCount();
202 int NotificationsTest::GetNotificationPopupCount() {
203 return message_center::MessageCenter::Get()->GetPopupNotifications().size();
206 void NotificationsTest::CloseBrowserWindow(Browser
* browser
) {
207 content::WindowedNotificationObserver
observer(
208 chrome::NOTIFICATION_BROWSER_CLOSED
,
209 content::Source
<Browser
>(browser
));
210 browser
->window()->Close();
214 void NotificationsTest::CrashTab(Browser
* browser
, int index
) {
215 content::CrashTab(browser
->tab_strip_model()->GetWebContentsAt(index
));
218 void NotificationsTest::DenyOrigin(const GURL
& origin
) {
219 DropOriginPreference(origin
);
220 DesktopNotificationProfileUtil::DenyPermission(browser()->profile(), origin
);
223 void NotificationsTest::AllowOrigin(const GURL
& origin
) {
224 DropOriginPreference(origin
);
225 DesktopNotificationProfileUtil::GrantPermission(browser()->profile(), origin
);
228 void NotificationsTest::AllowAllOrigins() {
230 HostContentSettingsMapFactory::GetForProfile(browser()->profile())
231 ->ClearSettingsForOneType(CONTENT_SETTINGS_TYPE_NOTIFICATIONS
);
232 SetDefaultContentSetting(CONTENT_SETTING_ALLOW
);
235 void NotificationsTest::SetDefaultContentSetting(ContentSetting setting
) {
236 HostContentSettingsMapFactory::GetForProfile(browser()->profile())
237 ->SetDefaultContentSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS
, setting
);
240 std::string
NotificationsTest::CreateNotification(
242 bool wait_for_new_balloon
,
246 const char* replace_id
) {
247 std::string script
= base::StringPrintf(
248 "createNotification('%s', '%s', '%s', '%s');",
249 icon
, title
, body
, replace_id
);
251 MessageCenterChangeObserver observer
;
253 bool success
= content::ExecuteScriptAndExtractString(
254 GetActiveWebContents(browser
), script
, &result
);
255 if (success
&& result
!= "-1" && wait_for_new_balloon
)
256 success
= observer
.Wait();
257 EXPECT_TRUE(success
);
262 std::string
NotificationsTest::CreateSimpleNotification(
264 bool wait_for_new_balloon
) {
265 return CreateNotification(
266 browser
, wait_for_new_balloon
,
267 "no_such_file.png", "My Title", "My Body", "");
270 std::string
NotificationsTest::RequestAndRespondToPermission(
272 PermissionBubbleManager::AutoResponseType bubble_response
) {
274 content::WebContents
* web_contents
= GetActiveWebContents(browser
);
275 PermissionBubbleManager::FromWebContents(web_contents
)
276 ->set_auto_response_for_test(bubble_response
);
277 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
278 web_contents
, "requestPermission();", &result
));
282 bool NotificationsTest::RequestAndAcceptPermission(Browser
* browser
) {
283 std::string result
= RequestAndRespondToPermission(
284 browser
, PermissionBubbleManager::ACCEPT_ALL
);
285 return "request-callback-granted" == result
;
288 bool NotificationsTest::RequestAndDenyPermission(Browser
* browser
) {
290 RequestAndRespondToPermission(browser
, PermissionBubbleManager::DENY_ALL
);
291 return "request-callback-denied" == result
;
294 bool NotificationsTest::RequestAndDismissPermission(Browser
* browser
) {
296 RequestAndRespondToPermission(browser
, PermissionBubbleManager::DISMISS
);
297 return "request-callback-default" == result
;
300 bool NotificationsTest::RequestPermissionAndWait(Browser
* browser
) {
301 content::WebContents
* web_contents
= GetActiveWebContents(browser
);
302 ui_test_utils::NavigateToURL(browser
, GetTestPageURL());
303 PermissionRequestObserver
observer(web_contents
);
305 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
306 web_contents
, "requestPermissionAndRespond();", &result
));
307 EXPECT_EQ("requested", result
);
309 return observer
.request_shown();
312 bool NotificationsTest::CancelNotification(
313 const char* notification_id
,
315 std::string script
= base::StringPrintf(
316 "cancelNotification('%s');",
319 MessageCenterChangeObserver observer
;
321 bool success
= content::ExecuteScriptAndExtractString(
322 GetActiveWebContents(browser
), script
, &result
);
323 if (!success
|| result
!= "1")
325 return observer
.Wait();
328 void NotificationsTest::GetPrefsByContentSetting(
329 ContentSetting setting
,
330 ContentSettingsForOneType
* settings
) {
331 DesktopNotificationProfileUtil::GetNotificationsSettings(
332 browser()->profile(), settings
);
333 for (ContentSettingsForOneType::iterator it
= settings
->begin();
334 it
!= settings
->end(); ) {
335 if (it
->setting
!= setting
|| it
->source
.compare("preference") != 0)
336 it
= settings
->erase(it
);
342 bool NotificationsTest::CheckOriginInSetting(
343 const ContentSettingsForOneType
& settings
,
344 const GURL
& origin
) {
345 ContentSettingsPattern pattern
=
346 ContentSettingsPattern::FromURLNoWildcard(origin
);
347 for (ContentSettingsForOneType::const_iterator it
= settings
.begin();
348 it
!= settings
.end(); ++it
) {
349 if (it
->primary_pattern
== pattern
)
355 void NotificationsTest::DropOriginPreference(const GURL
& origin
) {
356 DesktopNotificationProfileUtil::ClearSetting(browser()->profile(),
357 ContentSettingsPattern::FromURLNoWildcard(origin
));
360 // Flaky on Windows, Mac, Linux: http://crbug.com/437414.
361 IN_PROC_BROWSER_TEST_F(NotificationsTest
, DISABLED_TestUserGestureInfobar
) {
362 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
364 ui_test_utils::NavigateToURL(
366 embedded_test_server()->GetURL(
367 "/notifications/notifications_request_function.html"));
369 // Request permission by calling request() while eval'ing an inline script;
370 // That's considered a user gesture to webkit, and should produce an infobar.
372 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
373 GetActiveWebContents(browser()),
374 "window.domAutomationController.send(request());", &result
));
377 InfoBarService
* infobar_service
= InfoBarService::FromWebContents(
378 browser()->tab_strip_model()->GetWebContentsAt(0));
379 EXPECT_EQ(1U, infobar_service
->infobar_count());
382 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestCreateSimpleNotification
) {
383 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
385 // Creates a simple notification.
387 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
389 std::string result
= CreateSimpleNotification(browser(), true);
390 EXPECT_NE("-1", result
);
392 GURL EXPECTED_ICON_URL
= embedded_test_server()->GetURL(kExpectedIconUrl
);
393 ASSERT_EQ(1, GetNotificationCount());
394 message_center::NotificationList::Notifications notifications
=
395 message_center::MessageCenter::Get()->GetVisibleNotifications();
396 EXPECT_EQ(base::ASCIIToUTF16("My Title"),
397 (*notifications
.rbegin())->title());
398 EXPECT_EQ(base::ASCIIToUTF16("My Body"),
399 (*notifications
.rbegin())->message());
402 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestCloseNotification
) {
403 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
405 // Creates a notification and closes it.
407 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
409 std::string result
= CreateSimpleNotification(browser(), true);
410 EXPECT_NE("-1", result
);
411 ASSERT_EQ(1, GetNotificationCount());
413 message_center::NotificationList::Notifications notifications
=
414 message_center::MessageCenter::Get()->GetVisibleNotifications();
415 message_center::MessageCenter::Get()->RemoveNotification(
416 (*notifications
.rbegin())->id(),
419 ASSERT_EQ(0, GetNotificationCount());
422 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestCancelNotification
) {
423 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
425 // Creates a notification and cancels it in the origin page.
427 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
429 std::string note_id
= CreateSimpleNotification(browser(), true);
430 EXPECT_NE(note_id
, "-1");
432 ASSERT_EQ(1, GetNotificationCount());
433 ASSERT_TRUE(CancelNotification(note_id
.c_str(), browser()));
434 ASSERT_EQ(0, GetNotificationCount());
437 // Requests notification privileges and verifies the prompt appears.
438 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestPermissionRequestUIAppears
) {
439 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
441 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
442 EXPECT_TRUE(RequestPermissionAndWait(browser()));
443 ASSERT_EQ(0, GetNotificationCount());
446 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestAllowOnPermissionRequestUI
) {
447 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
449 // Tries to create a notification & clicks 'allow' on the prompt.
450 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
451 // This notification should not be shown because we do not have permission.
452 CreateSimpleNotification(browser(), false);
453 ASSERT_EQ(0, GetNotificationCount());
455 ASSERT_TRUE(RequestAndAcceptPermission(browser()));
457 CreateSimpleNotification(browser(), true);
458 EXPECT_EQ(1, GetNotificationCount());
461 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestDenyOnPermissionRequestUI
) {
462 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
464 // Test that no notification is created when Deny is chosen from prompt.
465 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
466 ASSERT_TRUE(RequestAndDenyPermission(browser()));
467 CreateSimpleNotification(browser(), false);
468 ASSERT_EQ(0, GetNotificationCount());
469 ContentSettingsForOneType settings
;
470 GetPrefsByContentSetting(CONTENT_SETTING_BLOCK
, &settings
);
471 EXPECT_TRUE(CheckOriginInSetting(settings
, GetTestPageURL()));
474 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestClosePermissionRequestUI
) {
475 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
477 // Test that no notification is created when prompt is dismissed.
478 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
479 ASSERT_TRUE(RequestAndDismissPermission(browser()));
480 CreateSimpleNotification(browser(), false);
481 ASSERT_EQ(0, GetNotificationCount());
482 ContentSettingsForOneType settings
;
483 GetPrefsByContentSetting(CONTENT_SETTING_BLOCK
, &settings
);
484 EXPECT_EQ(0U, settings
.size());
487 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestAllowNotificationsFromAllSites
) {
488 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
490 // Verify that all domains can be allowed to show notifications.
491 SetDefaultContentSetting(CONTENT_SETTING_ALLOW
);
492 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
494 std::string result
= CreateSimpleNotification(browser(), true);
495 EXPECT_NE("-1", result
);
497 ASSERT_EQ(1, GetNotificationCount());
500 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestDenyNotificationsFromAllSites
) {
501 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
503 // Verify that no domain can show notifications.
504 SetDefaultContentSetting(CONTENT_SETTING_BLOCK
);
505 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
507 std::string result
= CreateSimpleNotification(browser(), false);
508 EXPECT_EQ("-1", result
);
510 ASSERT_EQ(0, GetNotificationCount());
513 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestDenyDomainAndAllowAll
) {
514 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
516 // Verify that denying a domain and allowing all shouldn't show
517 // notifications from the denied domain.
518 DenyOrigin(GetTestPageURL().GetOrigin());
519 SetDefaultContentSetting(CONTENT_SETTING_ALLOW
);
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
, TestAllowDomainAndDenyAll
) {
530 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
532 // Verify that allowing a domain and denying all others should show
533 // notifications from the allowed domain.
534 AllowOrigin(GetTestPageURL().GetOrigin());
535 SetDefaultContentSetting(CONTENT_SETTING_BLOCK
);
537 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
539 std::string result
= CreateSimpleNotification(browser(), true);
540 EXPECT_NE("-1", result
);
542 ASSERT_EQ(1, GetNotificationCount());
545 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestDenyAndThenAllowDomain
) {
546 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
548 // Verify that denying and again allowing should show notifications.
549 DenyOrigin(GetTestPageURL().GetOrigin());
551 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
553 std::string result
= CreateSimpleNotification(browser(), false);
554 EXPECT_EQ("-1", result
);
556 ASSERT_EQ(0, GetNotificationCount());
558 AllowOrigin(GetTestPageURL().GetOrigin());
559 result
= CreateSimpleNotification(browser(), true);
560 EXPECT_NE("-1", result
);
562 ASSERT_EQ(1, GetNotificationCount());
565 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestCreateDenyCloseNotifications
) {
566 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
568 // Verify able to create, deny, and close the notification.
570 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
571 CreateSimpleNotification(browser(), true);
572 ASSERT_EQ(1, GetNotificationCount());
574 DenyOrigin(GetTestPageURL().GetOrigin());
575 ContentSettingsForOneType settings
;
576 GetPrefsByContentSetting(CONTENT_SETTING_BLOCK
, &settings
);
577 ASSERT_TRUE(CheckOriginInSetting(settings
, GetTestPageURL().GetOrigin()));
579 EXPECT_EQ(1, GetNotificationCount());
580 message_center::NotificationList::Notifications notifications
=
581 message_center::MessageCenter::Get()->GetVisibleNotifications();
582 message_center::MessageCenter::Get()->RemoveNotification(
583 (*notifications
.rbegin())->id(),
585 ASSERT_EQ(0, GetNotificationCount());
588 // Crashes on Linux/Win. See http://crbug.com/160657.
589 IN_PROC_BROWSER_TEST_F(
591 DISABLED_TestOriginPrefsNotSavedInIncognito
) {
592 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
594 // Verify that allow/deny origin preferences are not saved in incognito.
595 Browser
* incognito
= CreateIncognitoBrowser();
596 ui_test_utils::NavigateToURL(incognito
, GetTestPageURL());
597 ASSERT_TRUE(RequestAndDenyPermission(incognito
));
598 CloseBrowserWindow(incognito
);
600 incognito
= CreateIncognitoBrowser();
601 ui_test_utils::NavigateToURL(incognito
, GetTestPageURL());
602 ASSERT_TRUE(RequestAndAcceptPermission(incognito
));
603 CreateSimpleNotification(incognito
, true);
604 ASSERT_EQ(1, GetNotificationCount());
605 CloseBrowserWindow(incognito
);
607 incognito
= CreateIncognitoBrowser();
608 ui_test_utils::NavigateToURL(incognito
, GetTestPageURL());
609 ASSERT_TRUE(RequestPermissionAndWait(incognito
));
611 ContentSettingsForOneType settings
;
612 GetPrefsByContentSetting(CONTENT_SETTING_BLOCK
, &settings
);
613 EXPECT_EQ(0U, settings
.size());
614 GetPrefsByContentSetting(CONTENT_SETTING_ALLOW
, &settings
);
615 EXPECT_EQ(0U, settings
.size());
618 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestIncognitoNotification
) {
619 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
621 // Test notifications in incognito window.
622 Browser
* browser
= CreateIncognitoBrowser();
623 ui_test_utils::NavigateToURL(browser
, GetTestPageURL());
624 browser
->tab_strip_model()->ActivateTabAt(0, true);
625 ASSERT_TRUE(RequestAndAcceptPermission(browser
));
626 CreateSimpleNotification(browser
, true);
627 ASSERT_EQ(1, GetNotificationCount());
630 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestCloseTabWithPermissionRequestUI
) {
631 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
633 // Test that user can close tab when bubble present.
634 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
635 EXPECT_TRUE(RequestPermissionAndWait(browser()));
636 content::WebContentsDestroyedWatcher
destroyed_watcher(
637 browser()->tab_strip_model()->GetWebContentsAt(0));
638 browser()->tab_strip_model()->CloseWebContentsAt(0,
639 TabStripModel::CLOSE_NONE
);
640 destroyed_watcher
.Wait();
643 // See crbug.com/248470
644 #if defined(OS_LINUX)
645 #define MAYBE_TestCrashRendererNotificationRemain \
646 DISABLED_TestCrashRendererNotificationRemain
648 #define MAYBE_TestCrashRendererNotificationRemain \
649 TestCrashRendererNotificationRemain
652 IN_PROC_BROWSER_TEST_F(NotificationsTest
,
653 MAYBE_TestCrashRendererNotificationRemain
) {
654 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
656 // Test crashing renderer does not close or crash notification.
658 ui_test_utils::NavigateToURLWithDisposition(
662 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB
);
663 browser()->tab_strip_model()->ActivateTabAt(0, true);
664 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
665 CreateSimpleNotification(browser(), true);
666 ASSERT_EQ(1, GetNotificationCount());
667 CrashTab(browser(), 0);
668 ASSERT_EQ(1, GetNotificationCount());
671 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestNotificationReplacement
) {
672 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
674 // Test that we can replace a notification using the replaceId.
677 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
679 std::string result
= CreateNotification(
680 browser(), true, "abc.png", "Title1", "Body1", "chat");
681 EXPECT_NE("-1", result
);
683 ASSERT_EQ(1, GetNotificationCount());
685 result
= CreateNotification(
686 browser(), false, "no_such_file.png", "Title2", "Body2", "chat");
687 EXPECT_NE("-1", result
);
689 ASSERT_EQ(1, GetNotificationCount());
690 message_center::NotificationList::Notifications notifications
=
691 message_center::MessageCenter::Get()->GetVisibleNotifications();
692 EXPECT_EQ(base::ASCIIToUTF16("Title2"), (*notifications
.rbegin())->title());
693 EXPECT_EQ(base::ASCIIToUTF16("Body2"),
694 (*notifications
.rbegin())->message());
697 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestLastUsage
) {
698 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
700 HostContentSettingsMap
* settings_map
=
701 HostContentSettingsMapFactory::GetForProfile(browser()->profile());
702 base::SimpleTestClock
* clock
= new base::SimpleTestClock();
703 settings_map
->SetPrefClockForTesting(scoped_ptr
<base::Clock
>(clock
));
704 clock
->SetNow(base::Time::UnixEpoch() + base::TimeDelta::FromSeconds(10));
706 // Creates a simple notification.
708 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
710 std::string result
= CreateSimpleNotification(browser(), true);
711 EXPECT_NE("-1", result
);
713 EXPECT_EQ(settings_map
->GetLastUsage(GetTestPageURL().GetOrigin(),
714 GetTestPageURL().GetOrigin(),
715 CONTENT_SETTINGS_TYPE_NOTIFICATIONS
)
719 clock
->Advance(base::TimeDelta::FromSeconds(3));
721 result
= CreateSimpleNotification(browser(), true);
722 EXPECT_NE("-1", result
);
724 EXPECT_EQ(settings_map
->GetLastUsage(GetTestPageURL().GetOrigin(),
725 GetTestPageURL().GetOrigin(),
726 CONTENT_SETTINGS_TYPE_NOTIFICATIONS
)
731 IN_PROC_BROWSER_TEST_F(NotificationsTest
,
732 TestNotificationReplacementReappearance
) {
733 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
735 // Test that we can replace a notification using the tag, and that it will
736 // cause the notification to reappear as a popup again.
739 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
741 ASSERT_EQ(0, GetNotificationPopupCount());
743 std::string result
= CreateNotification(
744 browser(), true, "abc.png", "Title1", "Body1", "chat");
745 EXPECT_NE("-1", result
);
747 ASSERT_EQ(1, GetNotificationPopupCount());
749 message_center::NotificationList::Notifications notifications
=
750 message_center::MessageCenter::Get()->GetVisibleNotifications();
751 message_center::MessageCenter::Get()->ClickOnNotification(
752 (*notifications
.rbegin())->id());
754 ASSERT_EQ(0, GetNotificationPopupCount());
756 result
= CreateNotification(
757 browser(), true, "abc.png", "Title2", "Body2", "chat");
758 EXPECT_NE("-1", result
);
760 ASSERT_EQ(1, GetNotificationPopupCount());
763 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestNotificationValidIcon
) {
764 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
767 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
768 ASSERT_EQ(0, GetNotificationPopupCount());
770 std::string result
= CreateNotification(
771 browser(), true, "icon.png", "Title1", "Body1", "chat");
772 EXPECT_NE("-1", result
);
774 message_center::NotificationList::PopupNotifications notifications
=
775 message_center::MessageCenter::Get()->GetPopupNotifications();
776 ASSERT_EQ(1u, notifications
.size());
778 auto* notification
= *notifications
.rbegin();
780 EXPECT_EQ(100, notification
->icon().Width());
781 EXPECT_EQ(100, notification
->icon().Height());
784 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestNotificationInvalidIcon
) {
785 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
788 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
789 ASSERT_EQ(0, GetNotificationPopupCount());
791 // Not supplying an icon URL.
792 std::string result
= CreateNotification(
793 browser(), true, "", "Title1", "Body1", "chat");
794 EXPECT_NE("-1", result
);
796 message_center::NotificationList::PopupNotifications notifications
=
797 message_center::MessageCenter::Get()->GetPopupNotifications();
798 ASSERT_EQ(1u, notifications
.size());
800 auto* notification
= *notifications
.rbegin();
801 EXPECT_TRUE(notification
->icon().IsEmpty());
803 // Supplying an invalid icon URL.
804 result
= CreateNotification(
805 browser(), true, "invalid.png", "Title1", "Body1", "chat");
806 EXPECT_NE("-1", result
);
808 notifications
= message_center::MessageCenter::Get()->GetPopupNotifications();
809 ASSERT_EQ(1u, notifications
.size());
811 notification
= *notifications
.rbegin();
812 EXPECT_TRUE(notification
->icon().IsEmpty());
815 IN_PROC_BROWSER_TEST_F(NotificationsTest
, TestNotificationDoubleClose
) {
816 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
819 ui_test_utils::NavigateToURL(
820 browser(), GetTestPageURLForFile("notification-double-close.html"));
821 ASSERT_EQ(0, GetNotificationPopupCount());
823 std::string result
= CreateNotification(
824 browser(), true, "", "Title1", "Body1", "chat");
825 EXPECT_NE("-1", result
);
827 ASSERT_EQ(1, GetNotificationCount());
828 message_center::NotificationList::Notifications notifications
=
829 message_center::MessageCenter::Get()->GetVisibleNotifications();
830 message_center::MessageCenter::Get()->RemoveNotification(
831 (*notifications
.rbegin())->id(),
834 ASSERT_EQ(0, GetNotificationCount());
836 // Calling WebContents::IsCrashed() will return FALSE here, even if the WC did
837 // crash. Work around this timing issue by creating another notification,
838 // which requires interaction with the renderer process.
839 result
= CreateNotification(browser(), true, "", "Title1", "Body1", "chat");
840 EXPECT_NE("-1", result
);