Roll src/third_party/WebKit bf18a82:a9cee16 (svn 185297:185304)
[chromium-blink-merge.git] / chrome / browser / notifications / notification_browsertest.cc
blob3cead0397846f9fe3da2ecc7e6a9c8d9371f65d9
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.
5 #include <deque>
6 #include <string>
8 #include "base/bind.h"
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"
48 #include "url/gurl.h"
50 namespace {
52 const char kExpectedIconUrl[] = "/notifications/no_such_file.png";
54 enum InfobarAction {
55 DISMISS = 0,
56 ALLOW,
57 DENY,
60 class NotificationChangeObserver {
61 public:
62 virtual ~NotificationChangeObserver() {}
63 virtual bool Wait() = 0;
66 class MessageCenterChangeObserver
67 : public message_center::MessageCenterObserver,
68 public NotificationChangeObserver {
69 public:
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_)
82 return true;
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);
115 } // namespace
117 class NotificationsTest : public InProcessBrowserTest {
118 public:
119 NotificationsTest() {}
121 protected:
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,
136 const char* icon,
137 const char* title,
138 const char* body,
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,
147 int tab_index);
148 void GetPrefsByContentSetting(ContentSetting setting,
149 ContentSettingsForOneType* settings);
150 bool CheckOriginInSetting(const ContentSettingsForOneType& settings,
151 const GURL& origin);
153 GURL GetTestPageURL() const {
154 return embedded_test_server()->GetURL(
155 "/notifications/notification_tester.html");
158 private:
159 void DropOriginPreference(const GURL& origin);
162 int NotificationsTest::GetNotificationCount() {
163 return message_center::MessageCenter::Get()->NotificationCount();
166 int NotificationsTest::GetNotificationPopupCount() {
167 return message_center::MessageCenter::Get()->GetPopupNotifications().size();
170 void NotificationsTest::CloseBrowserWindow(Browser* browser) {
171 content::WindowedNotificationObserver observer(
172 chrome::NOTIFICATION_BROWSER_CLOSED,
173 content::Source<Browser>(browser));
174 browser->window()->Close();
175 observer.Wait();
178 void NotificationsTest::CrashTab(Browser* browser, int index) {
179 content::CrashTab(browser->tab_strip_model()->GetWebContentsAt(index));
182 void NotificationsTest::DenyOrigin(const GURL& origin) {
183 DropOriginPreference(origin);
184 DesktopNotificationProfileUtil::DenyPermission(browser()->profile(), origin);
187 void NotificationsTest::AllowOrigin(const GURL& origin) {
188 DropOriginPreference(origin);
189 DesktopNotificationProfileUtil::GrantPermission(browser()->profile(), origin);
192 void NotificationsTest::AllowAllOrigins() {
193 // Reset all origins
194 browser()->profile()->GetHostContentSettingsMap()->ClearSettingsForOneType(
195 CONTENT_SETTINGS_TYPE_NOTIFICATIONS);
196 SetDefaultContentSetting(CONTENT_SETTING_ALLOW);
199 void NotificationsTest::SetDefaultContentSetting(ContentSetting setting) {
200 browser()->profile()->GetHostContentSettingsMap()->SetDefaultContentSetting(
201 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, setting);
204 void NotificationsTest::VerifyInfoBar(const Browser* browser, int index) {
205 InfoBarService* infobar_service = InfoBarService::FromWebContents(
206 browser->tab_strip_model()->GetWebContentsAt(index));
208 ASSERT_EQ(1U, infobar_service->infobar_count());
209 ConfirmInfoBarDelegate* confirm_infobar =
210 infobar_service->infobar_at(0)->delegate()->AsConfirmInfoBarDelegate();
211 ASSERT_TRUE(confirm_infobar);
212 int buttons = confirm_infobar->GetButtons();
213 EXPECT_TRUE(buttons & ConfirmInfoBarDelegate::BUTTON_OK);
214 EXPECT_TRUE(buttons & ConfirmInfoBarDelegate::BUTTON_CANCEL);
217 std::string NotificationsTest::CreateNotification(
218 Browser* browser,
219 bool wait_for_new_balloon,
220 const char* icon,
221 const char* title,
222 const char* body,
223 const char* replace_id) {
224 std::string script = base::StringPrintf(
225 "createNotification('%s', '%s', '%s', '%s');",
226 icon, title, body, replace_id);
228 MessageCenterChangeObserver observer;
229 std::string result;
230 bool success = content::ExecuteScriptAndExtractString(
231 browser->tab_strip_model()->GetActiveWebContents(),
232 script,
233 &result);
234 if (success && result != "-1" && wait_for_new_balloon)
235 success = observer.Wait();
236 EXPECT_TRUE(success);
238 return result;
241 std::string NotificationsTest::CreateSimpleNotification(
242 Browser* browser,
243 bool wait_for_new_balloon) {
244 return CreateNotification(
245 browser, wait_for_new_balloon,
246 "no_such_file.png", "My Title", "My Body", "");
249 bool NotificationsTest::RequestPermissionAndWait(Browser* browser) {
250 InfoBarService* infobar_service = InfoBarService::FromWebContents(
251 browser->tab_strip_model()->GetActiveWebContents());
252 content::WindowedNotificationObserver observer(
253 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED,
254 content::Source<InfoBarService>(infobar_service));
255 std::string result;
256 bool success = content::ExecuteScriptAndExtractString(
257 browser->tab_strip_model()->GetActiveWebContents(),
258 "requestPermission();",
259 &result);
260 if (!success || result != "1")
261 return false;
262 observer.Wait();
263 return true;
266 bool NotificationsTest::CancelNotification(
267 const char* notification_id,
268 Browser* browser) {
269 std::string script = base::StringPrintf(
270 "cancelNotification('%s');",
271 notification_id);
273 MessageCenterChangeObserver observer;
274 std::string result;
275 bool success = content::ExecuteScriptAndExtractString(
276 browser->tab_strip_model()->GetActiveWebContents(),
277 script,
278 &result);
279 if (!success || result != "1")
280 return false;
281 return observer.Wait();
284 bool NotificationsTest::PerformActionOnInfoBar(
285 Browser* browser,
286 InfobarAction action,
287 size_t infobar_index,
288 int tab_index) {
289 InfoBarService* infobar_service = InfoBarService::FromWebContents(
290 browser->tab_strip_model()->GetWebContentsAt(tab_index));
291 if (infobar_index >= infobar_service->infobar_count()) {
292 ADD_FAILURE();
293 return false;
296 infobars::InfoBar* infobar = infobar_service->infobar_at(infobar_index);
297 infobars::InfoBarDelegate* infobar_delegate = infobar->delegate();
298 switch (action) {
299 case DISMISS:
300 infobar_delegate->InfoBarDismissed();
301 infobar_service->RemoveInfoBar(infobar);
302 return true;
304 case ALLOW: {
305 ConfirmInfoBarDelegate* confirm_infobar_delegate =
306 infobar_delegate->AsConfirmInfoBarDelegate();
307 if (!confirm_infobar_delegate) {
308 ADD_FAILURE();
309 } else if (confirm_infobar_delegate->Accept()) {
310 infobar_service->RemoveInfoBar(infobar);
311 return true;
315 case DENY: {
316 ConfirmInfoBarDelegate* confirm_infobar_delegate =
317 infobar_delegate->AsConfirmInfoBarDelegate();
318 if (!confirm_infobar_delegate) {
319 ADD_FAILURE();
320 } else if (confirm_infobar_delegate->Cancel()) {
321 infobar_service->RemoveInfoBar(infobar);
322 return true;
327 return false;
330 void NotificationsTest::GetPrefsByContentSetting(
331 ContentSetting setting,
332 ContentSettingsForOneType* settings) {
333 DesktopNotificationProfileUtil::GetNotificationsSettings(
334 browser()->profile(), settings);
335 for (ContentSettingsForOneType::iterator it = settings->begin();
336 it != settings->end(); ) {
337 if (it->setting != setting || it->source.compare("preference") != 0)
338 it = settings->erase(it);
339 else
340 ++it;
344 bool NotificationsTest::CheckOriginInSetting(
345 const ContentSettingsForOneType& settings,
346 const GURL& origin) {
347 ContentSettingsPattern pattern =
348 ContentSettingsPattern::FromURLNoWildcard(origin);
349 for (ContentSettingsForOneType::const_iterator it = settings.begin();
350 it != settings.end(); ++it) {
351 if (it->primary_pattern == pattern)
352 return true;
354 return false;
357 void NotificationsTest::DropOriginPreference(const GURL& origin) {
358 DesktopNotificationProfileUtil::ClearSetting(browser()->profile(),
359 ContentSettingsPattern::FromURLNoWildcard(origin));
362 // If this flakes, use http://crbug.com/62311 and http://crbug.com/74428.
363 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestUserGestureInfobar) {
364 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
366 ui_test_utils::NavigateToURL(
367 browser(),
368 embedded_test_server()->GetURL(
369 "/notifications/notifications_request_function.html"));
371 // Request permission by calling request() while eval'ing an inline script;
372 // That's considered a user gesture to webkit, and should produce an infobar.
373 bool result;
374 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
375 browser()->tab_strip_model()->GetActiveWebContents(),
376 "window.domAutomationController.send(request());",
377 &result));
378 EXPECT_TRUE(result);
380 InfoBarService* infobar_service = InfoBarService::FromWebContents(
381 browser()->tab_strip_model()->GetWebContentsAt(0));
382 EXPECT_EQ(1U, infobar_service->infobar_count());
385 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestCreateSimpleNotification) {
386 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
388 // Creates a simple notification.
389 AllowAllOrigins();
390 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
392 std::string result = CreateSimpleNotification(browser(), true);
393 EXPECT_NE("-1", result);
395 GURL EXPECTED_ICON_URL = embedded_test_server()->GetURL(kExpectedIconUrl);
396 ASSERT_EQ(1, GetNotificationCount());
397 message_center::NotificationList::Notifications notifications =
398 message_center::MessageCenter::Get()->GetVisibleNotifications();
399 EXPECT_EQ(base::ASCIIToUTF16("My Title"),
400 (*notifications.rbegin())->title());
401 EXPECT_EQ(base::ASCIIToUTF16("My Body"),
402 (*notifications.rbegin())->message());
405 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestCloseNotification) {
406 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
408 // Creates a notification and closes it.
409 AllowAllOrigins();
410 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
412 std::string result = CreateSimpleNotification(browser(), true);
413 EXPECT_NE("-1", result);
414 ASSERT_EQ(1, GetNotificationCount());
416 message_center::NotificationList::Notifications notifications =
417 message_center::MessageCenter::Get()->GetVisibleNotifications();
418 message_center::MessageCenter::Get()->RemoveNotification(
419 (*notifications.rbegin())->id(),
420 true); // by_user
422 ASSERT_EQ(0, GetNotificationCount());
425 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestCancelNotification) {
426 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
428 // Creates a notification and cancels it in the origin page.
429 AllowAllOrigins();
430 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
432 std::string note_id = CreateSimpleNotification(browser(), true);
433 EXPECT_NE(note_id, "-1");
435 ASSERT_EQ(1, GetNotificationCount());
436 ASSERT_TRUE(CancelNotification(note_id.c_str(), browser()));
437 ASSERT_EQ(0, GetNotificationCount());
440 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestPermissionInfobarAppears) {
441 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
443 // Requests notification privileges and verifies the infobar appears.
444 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
445 ASSERT_TRUE(RequestPermissionAndWait(browser()));
447 ASSERT_EQ(0, GetNotificationCount());
448 ASSERT_NO_FATAL_FAILURE(VerifyInfoBar(browser(), 0));
451 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestAllowOnPermissionInfobar) {
452 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
454 // Tries to create a notification and clicks allow on the infobar.
455 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
456 // This notification should not be shown because we do not have permission.
457 CreateSimpleNotification(browser(), false);
458 ASSERT_EQ(0, GetNotificationCount());
460 ASSERT_TRUE(RequestPermissionAndWait(browser()));
461 ASSERT_TRUE(PerformActionOnInfoBar(browser(), ALLOW, 0, 0));
463 CreateSimpleNotification(browser(), true);
464 EXPECT_EQ(1, GetNotificationCount());
467 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestDenyOnPermissionInfobar) {
468 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
470 // Test that no notification is created
471 // when Deny is chosen from permission infobar.
472 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
473 ASSERT_TRUE(RequestPermissionAndWait(browser()));
474 PerformActionOnInfoBar(browser(), DENY, 0, 0);
475 CreateSimpleNotification(browser(), false);
476 ASSERT_EQ(0, GetNotificationCount());
477 ContentSettingsForOneType settings;
478 GetPrefsByContentSetting(CONTENT_SETTING_BLOCK, &settings);
479 EXPECT_TRUE(CheckOriginInSetting(settings, GetTestPageURL()));
482 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestClosePermissionInfobar) {
483 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
485 // Test that no notification is created when permission infobar is dismissed.
486 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
487 ASSERT_TRUE(RequestPermissionAndWait(browser()));
488 PerformActionOnInfoBar(browser(), DISMISS, 0, 0);
489 CreateSimpleNotification(browser(), false);
490 ASSERT_EQ(0, GetNotificationCount());
491 ContentSettingsForOneType settings;
492 GetPrefsByContentSetting(CONTENT_SETTING_BLOCK, &settings);
493 EXPECT_EQ(0U, settings.size());
496 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestAllowNotificationsFromAllSites) {
497 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
499 // Verify that all domains can be allowed to show notifications.
500 SetDefaultContentSetting(CONTENT_SETTING_ALLOW);
501 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
503 std::string result = CreateSimpleNotification(browser(), true);
504 EXPECT_NE("-1", result);
506 ASSERT_EQ(1, GetNotificationCount());
507 InfoBarService* infobar_service = InfoBarService::FromWebContents(
508 browser()->tab_strip_model()->GetWebContentsAt(0));
509 EXPECT_EQ(0U, infobar_service->infobar_count());
512 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestDenyNotificationsFromAllSites) {
513 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
515 // Verify that no domain can show notifications.
516 SetDefaultContentSetting(CONTENT_SETTING_BLOCK);
517 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
519 std::string result = CreateSimpleNotification(browser(), false);
520 EXPECT_EQ("-1", result);
522 ASSERT_EQ(0, GetNotificationCount());
525 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestDenyDomainAndAllowAll) {
526 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
528 // Verify that denying a domain and allowing all shouldn't show
529 // notifications from the denied domain.
530 DenyOrigin(GetTestPageURL().GetOrigin());
531 SetDefaultContentSetting(CONTENT_SETTING_ALLOW);
533 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
535 std::string result = CreateSimpleNotification(browser(), false);
536 EXPECT_EQ("-1", result);
538 ASSERT_EQ(0, GetNotificationCount());
541 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestAllowDomainAndDenyAll) {
542 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
544 // Verify that allowing a domain and denying all others should show
545 // notifications from the allowed domain.
546 AllowOrigin(GetTestPageURL().GetOrigin());
547 SetDefaultContentSetting(CONTENT_SETTING_BLOCK);
549 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
551 std::string result = CreateSimpleNotification(browser(), true);
552 EXPECT_NE("-1", result);
554 ASSERT_EQ(1, GetNotificationCount());
557 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestDenyAndThenAllowDomain) {
558 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
560 // Verify that denying and again allowing should show notifications.
561 DenyOrigin(GetTestPageURL().GetOrigin());
563 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
565 std::string result = CreateSimpleNotification(browser(), false);
566 EXPECT_EQ("-1", result);
568 ASSERT_EQ(0, GetNotificationCount());
570 AllowOrigin(GetTestPageURL().GetOrigin());
571 result = CreateSimpleNotification(browser(), true);
572 EXPECT_NE("-1", result);
574 ASSERT_EQ(1, GetNotificationCount());
575 InfoBarService* infobar_service = InfoBarService::FromWebContents(
576 browser()->tab_strip_model()->GetWebContentsAt(0));
577 EXPECT_EQ(0U, infobar_service->infobar_count());
580 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestCreateDenyCloseNotifications) {
581 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
583 // Verify able to create, deny, and close the notification.
584 AllowAllOrigins();
585 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
586 CreateSimpleNotification(browser(), true);
587 ASSERT_EQ(1, GetNotificationCount());
589 DenyOrigin(GetTestPageURL().GetOrigin());
590 ContentSettingsForOneType settings;
591 GetPrefsByContentSetting(CONTENT_SETTING_BLOCK, &settings);
592 ASSERT_TRUE(CheckOriginInSetting(settings, GetTestPageURL().GetOrigin()));
594 EXPECT_EQ(1, GetNotificationCount());
595 message_center::NotificationList::Notifications notifications =
596 message_center::MessageCenter::Get()->GetVisibleNotifications();
597 message_center::MessageCenter::Get()->RemoveNotification(
598 (*notifications.rbegin())->id(),
599 true); // by_user
600 ASSERT_EQ(0, GetNotificationCount());
603 // Crashes on Linux/Win. See http://crbug.com/160657.
604 IN_PROC_BROWSER_TEST_F(
605 NotificationsTest,
606 DISABLED_TestOriginPrefsNotSavedInIncognito) {
607 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
609 // Verify that allow/deny origin preferences are not saved in incognito.
610 Browser* incognito = CreateIncognitoBrowser();
611 ui_test_utils::NavigateToURL(incognito, GetTestPageURL());
612 ASSERT_TRUE(RequestPermissionAndWait(incognito));
613 PerformActionOnInfoBar(incognito, DENY, 0, 0);
614 CloseBrowserWindow(incognito);
616 incognito = CreateIncognitoBrowser();
617 ui_test_utils::NavigateToURL(incognito, GetTestPageURL());
618 ASSERT_TRUE(RequestPermissionAndWait(incognito));
619 PerformActionOnInfoBar(incognito, ALLOW, 0, 0);
620 CreateSimpleNotification(incognito, true);
621 ASSERT_EQ(1, GetNotificationCount());
622 CloseBrowserWindow(incognito);
624 incognito = CreateIncognitoBrowser();
625 ui_test_utils::NavigateToURL(incognito, GetTestPageURL());
626 ASSERT_TRUE(RequestPermissionAndWait(incognito));
628 ContentSettingsForOneType settings;
629 GetPrefsByContentSetting(CONTENT_SETTING_BLOCK, &settings);
630 EXPECT_EQ(0U, settings.size());
631 GetPrefsByContentSetting(CONTENT_SETTING_ALLOW, &settings);
632 EXPECT_EQ(0U, settings.size());
635 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestExitBrowserWithInfobar) {
636 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
638 // Exit the browser window, when the infobar appears.
639 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
640 ASSERT_TRUE(RequestPermissionAndWait(browser()));
643 // Times out on Windows and Linux. http://crbug.com/168976
644 #if defined(OS_WIN) || defined(OS_LINUX)
645 #define MAYBE_TestCrashTabWithPermissionInfobar \
646 DISABLED_TestCrashTabWithPermissionInfobar
647 #else
648 #define MAYBE_TestCrashTabWithPermissionInfobar \
649 TestCrashTabWithPermissionInfobar
650 #endif
651 IN_PROC_BROWSER_TEST_F(NotificationsTest,
652 MAYBE_TestCrashTabWithPermissionInfobar) {
653 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
655 // Test crashing the tab with permission infobar doesn't crash Chrome.
656 ui_test_utils::NavigateToURLWithDisposition(
657 browser(),
658 embedded_test_server()->GetURL("/empty.html"),
659 NEW_BACKGROUND_TAB,
660 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB);
661 browser()->tab_strip_model()->ActivateTabAt(0, true);
662 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
663 ASSERT_TRUE(RequestPermissionAndWait(browser()));
664 CrashTab(browser(), 0);
667 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestIncognitoNotification) {
668 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
670 // Test notifications in incognito window.
671 Browser* browser = CreateIncognitoBrowser();
672 ui_test_utils::NavigateToURL(browser, GetTestPageURL());
673 browser->tab_strip_model()->ActivateTabAt(0, true);
674 ASSERT_TRUE(RequestPermissionAndWait(browser));
675 PerformActionOnInfoBar(browser, ALLOW, 0, 0);
676 CreateSimpleNotification(browser, true);
677 ASSERT_EQ(1, GetNotificationCount());
680 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestCloseTabWithPermissionInfobar) {
681 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
683 // Test that user can close tab when infobar present.
684 ui_test_utils::NavigateToURLWithDisposition(
685 browser(),
686 GURL("about:blank"),
687 NEW_BACKGROUND_TAB,
688 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB);
689 browser()->tab_strip_model()->ActivateTabAt(0, true);
690 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
691 ASSERT_TRUE(RequestPermissionAndWait(browser()));
692 content::WebContentsDestroyedWatcher destroyed_watcher(
693 browser()->tab_strip_model()->GetWebContentsAt(0));
694 browser()->tab_strip_model()->CloseWebContentsAt(0,
695 TabStripModel::CLOSE_NONE);
696 destroyed_watcher.Wait();
699 IN_PROC_BROWSER_TEST_F(
700 NotificationsTest,
701 TestNavigateAwayWithPermissionInfobar) {
702 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
704 // Test navigating away when an infobar is present,
705 // then trying to create a notification from the same page.
706 ui_test_utils::NavigateToURLWithDisposition(
707 browser(),
708 GURL("about:blank"),
709 NEW_BACKGROUND_TAB,
710 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB);
711 browser()->tab_strip_model()->ActivateTabAt(0, true);
712 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
713 ASSERT_TRUE(RequestPermissionAndWait(browser()));
714 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
715 ASSERT_TRUE(RequestPermissionAndWait(browser()));
716 PerformActionOnInfoBar(browser(), ALLOW, 0, 0);
717 CreateSimpleNotification(browser(), true);
718 ASSERT_EQ(1, GetNotificationCount());
721 // See crbug.com/248470
722 #if defined(OS_LINUX)
723 #define MAYBE_TestCrashRendererNotificationRemain \
724 DISABLED_TestCrashRendererNotificationRemain
725 #else
726 #define MAYBE_TestCrashRendererNotificationRemain \
727 TestCrashRendererNotificationRemain
728 #endif
730 IN_PROC_BROWSER_TEST_F(NotificationsTest,
731 MAYBE_TestCrashRendererNotificationRemain) {
732 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
734 // Test crashing renderer does not close or crash notification.
735 AllowAllOrigins();
736 ui_test_utils::NavigateToURLWithDisposition(
737 browser(),
738 GURL("about:blank"),
739 NEW_BACKGROUND_TAB,
740 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB);
741 browser()->tab_strip_model()->ActivateTabAt(0, true);
742 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
743 CreateSimpleNotification(browser(), true);
744 ASSERT_EQ(1, GetNotificationCount());
745 CrashTab(browser(), 0);
746 ASSERT_EQ(1, GetNotificationCount());
749 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestNotificationReplacement) {
750 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
752 // Test that we can replace a notification using the replaceId.
753 AllowAllOrigins();
755 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
757 std::string result = CreateNotification(
758 browser(), true, "abc.png", "Title1", "Body1", "chat");
759 EXPECT_NE("-1", result);
761 ASSERT_EQ(1, GetNotificationCount());
763 result = CreateNotification(
764 browser(), false, "no_such_file.png", "Title2", "Body2", "chat");
765 EXPECT_NE("-1", result);
767 ASSERT_EQ(1, GetNotificationCount());
768 message_center::NotificationList::Notifications notifications =
769 message_center::MessageCenter::Get()->GetVisibleNotifications();
770 EXPECT_EQ(base::ASCIIToUTF16("Title2"), (*notifications.rbegin())->title());
771 EXPECT_EQ(base::ASCIIToUTF16("Body2"),
772 (*notifications.rbegin())->message());
775 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestLastUsage) {
776 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
778 HostContentSettingsMap* settings_map =
779 browser()->profile()->GetHostContentSettingsMap();
780 base::SimpleTestClock* clock = new base::SimpleTestClock();
781 settings_map->SetPrefClockForTesting(scoped_ptr<base::Clock>(clock));
782 clock->SetNow(base::Time::UnixEpoch() + base::TimeDelta::FromSeconds(10));
784 // Creates a simple notification.
785 AllowAllOrigins();
786 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
788 std::string result = CreateSimpleNotification(browser(), true);
789 EXPECT_NE("-1", result);
791 EXPECT_EQ(settings_map->GetLastUsage(GetTestPageURL().GetOrigin(),
792 GetTestPageURL().GetOrigin(),
793 CONTENT_SETTINGS_TYPE_NOTIFICATIONS)
794 .ToDoubleT(),
795 10);
797 clock->Advance(base::TimeDelta::FromSeconds(3));
799 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)
805 .ToDoubleT(),
806 13);
809 IN_PROC_BROWSER_TEST_F(NotificationsTest,
810 TestNotificationReplacementReappearance) {
811 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
813 // Test that we can replace a notification using the tag, and that it will
814 // cause the notification to reappear as a popup again.
815 AllowAllOrigins();
817 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
819 ASSERT_EQ(0, GetNotificationPopupCount());
821 std::string result = CreateNotification(
822 browser(), true, "abc.png", "Title1", "Body1", "chat");
823 EXPECT_NE("-1", result);
825 ASSERT_EQ(1, GetNotificationPopupCount());
827 message_center::NotificationList::Notifications notifications =
828 message_center::MessageCenter::Get()->GetVisibleNotifications();
829 message_center::MessageCenter::Get()->ClickOnNotification(
830 (*notifications.rbegin())->id());
832 ASSERT_EQ(0, GetNotificationPopupCount());
834 result = CreateNotification(
835 browser(), true, "abc.png", "Title2", "Body2", "chat");
836 EXPECT_NE("-1", result);
838 ASSERT_EQ(1, GetNotificationPopupCount());
841 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestNotificationValidIcon) {
842 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
843 AllowAllOrigins();
845 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
846 ASSERT_EQ(0, GetNotificationPopupCount());
848 std::string result = CreateNotification(
849 browser(), true, "icon.png", "Title1", "Body1", "chat");
850 EXPECT_NE("-1", result);
852 message_center::NotificationList::PopupNotifications notifications =
853 message_center::MessageCenter::Get()->GetPopupNotifications();
854 ASSERT_EQ(1u, notifications.size());
856 auto* notification = *notifications.rbegin();
858 EXPECT_EQ(100, notification->icon().Width());
859 EXPECT_EQ(100, notification->icon().Height());
862 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestNotificationInvalidIcon) {
863 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
864 AllowAllOrigins();
866 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
867 ASSERT_EQ(0, GetNotificationPopupCount());
869 // Not supplying an icon URL.
870 std::string result = CreateNotification(
871 browser(), true, "", "Title1", "Body1", "chat");
872 EXPECT_NE("-1", result);
874 message_center::NotificationList::PopupNotifications notifications =
875 message_center::MessageCenter::Get()->GetPopupNotifications();
876 ASSERT_EQ(1u, notifications.size());
878 auto* notification = *notifications.rbegin();
879 EXPECT_TRUE(notification->icon().IsEmpty());
881 // Supplying an invalid icon URL.
882 result = CreateNotification(
883 browser(), true, "invalid.png", "Title1", "Body1", "chat");
884 EXPECT_NE("-1", result);
886 notifications = message_center::MessageCenter::Get()->GetPopupNotifications();
887 ASSERT_EQ(1u, notifications.size());
889 notification = *notifications.rbegin();
890 EXPECT_TRUE(notification->icon().IsEmpty());