Check USB device path access when prompting users to select a device.
[chromium-blink-merge.git] / chrome / browser / notifications / notification_browsertest.cc
blob3feb8ec315c28b6a21647756e4759e953b782bdb
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 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");
162 private:
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();
179 observer.Wait();
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() {
197 // Reset all origins
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(
222 Browser* browser,
223 bool wait_for_new_balloon,
224 const char* icon,
225 const char* title,
226 const char* body,
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;
233 std::string result;
234 bool success = content::ExecuteScriptAndExtractString(
235 browser->tab_strip_model()->GetActiveWebContents(),
236 script,
237 &result);
238 if (success && result != "-1" && wait_for_new_balloon)
239 success = observer.Wait();
240 EXPECT_TRUE(success);
242 return result;
245 std::string NotificationsTest::CreateSimpleNotification(
246 Browser* browser,
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));
259 std::string result;
260 bool success = content::ExecuteScriptAndExtractString(
261 browser->tab_strip_model()->GetActiveWebContents(),
262 "requestPermission();",
263 &result);
264 if (!success || result != "1")
265 return false;
266 observer.Wait();
267 return true;
270 bool NotificationsTest::CancelNotification(
271 const char* notification_id,
272 Browser* browser) {
273 std::string script = base::StringPrintf(
274 "cancelNotification('%s');",
275 notification_id);
277 MessageCenterChangeObserver observer;
278 std::string result;
279 bool success = content::ExecuteScriptAndExtractString(
280 browser->tab_strip_model()->GetActiveWebContents(),
281 script,
282 &result);
283 if (!success || result != "1")
284 return false;
285 return observer.Wait();
288 bool NotificationsTest::PerformActionOnInfoBar(
289 Browser* browser,
290 InfobarAction action,
291 size_t infobar_index,
292 int tab_index) {
293 InfoBarService* infobar_service = InfoBarService::FromWebContents(
294 browser->tab_strip_model()->GetWebContentsAt(tab_index));
295 if (infobar_index >= infobar_service->infobar_count()) {
296 ADD_FAILURE();
297 return false;
300 infobars::InfoBar* infobar = infobar_service->infobar_at(infobar_index);
301 infobars::InfoBarDelegate* infobar_delegate = infobar->delegate();
302 switch (action) {
303 case DISMISS:
304 infobar_delegate->InfoBarDismissed();
305 infobar_service->RemoveInfoBar(infobar);
306 return true;
308 case ALLOW: {
309 ConfirmInfoBarDelegate* confirm_infobar_delegate =
310 infobar_delegate->AsConfirmInfoBarDelegate();
311 if (!confirm_infobar_delegate) {
312 ADD_FAILURE();
313 } else if (confirm_infobar_delegate->Accept()) {
314 infobar_service->RemoveInfoBar(infobar);
315 return true;
319 case DENY: {
320 ConfirmInfoBarDelegate* confirm_infobar_delegate =
321 infobar_delegate->AsConfirmInfoBarDelegate();
322 if (!confirm_infobar_delegate) {
323 ADD_FAILURE();
324 } else if (confirm_infobar_delegate->Cancel()) {
325 infobar_service->RemoveInfoBar(infobar);
326 return true;
331 return false;
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);
343 else
344 ++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)
356 return true;
358 return false;
361 void NotificationsTest::DropOriginPreference(const GURL& origin) {
362 DesktopNotificationProfileUtil::ClearSetting(browser()->profile(),
363 ContentSettingsPattern::FromURLNoWildcard(origin));
366 // Flaky on Windows, Mac, Linux: http://crbug.com/437414.
367 IN_PROC_BROWSER_TEST_F(NotificationsTest, DISABLED_TestUserGestureInfobar) {
368 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
370 ui_test_utils::NavigateToURL(
371 browser(),
372 embedded_test_server()->GetURL(
373 "/notifications/notifications_request_function.html"));
375 // Request permission by calling request() while eval'ing an inline script;
376 // That's considered a user gesture to webkit, and should produce an infobar.
377 bool result;
378 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
379 browser()->tab_strip_model()->GetActiveWebContents(),
380 "window.domAutomationController.send(request());",
381 &result));
382 EXPECT_TRUE(result);
384 InfoBarService* infobar_service = InfoBarService::FromWebContents(
385 browser()->tab_strip_model()->GetWebContentsAt(0));
386 EXPECT_EQ(1U, infobar_service->infobar_count());
389 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestCreateSimpleNotification) {
390 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
392 // Creates a simple notification.
393 AllowAllOrigins();
394 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
396 std::string result = CreateSimpleNotification(browser(), true);
397 EXPECT_NE("-1", result);
399 GURL EXPECTED_ICON_URL = embedded_test_server()->GetURL(kExpectedIconUrl);
400 ASSERT_EQ(1, GetNotificationCount());
401 message_center::NotificationList::Notifications notifications =
402 message_center::MessageCenter::Get()->GetVisibleNotifications();
403 EXPECT_EQ(base::ASCIIToUTF16("My Title"),
404 (*notifications.rbegin())->title());
405 EXPECT_EQ(base::ASCIIToUTF16("My Body"),
406 (*notifications.rbegin())->message());
409 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestCloseNotification) {
410 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
412 // Creates a notification and closes it.
413 AllowAllOrigins();
414 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
416 std::string result = CreateSimpleNotification(browser(), true);
417 EXPECT_NE("-1", result);
418 ASSERT_EQ(1, GetNotificationCount());
420 message_center::NotificationList::Notifications notifications =
421 message_center::MessageCenter::Get()->GetVisibleNotifications();
422 message_center::MessageCenter::Get()->RemoveNotification(
423 (*notifications.rbegin())->id(),
424 true); // by_user
426 ASSERT_EQ(0, GetNotificationCount());
429 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestCancelNotification) {
430 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
432 // Creates a notification and cancels it in the origin page.
433 AllowAllOrigins();
434 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
436 std::string note_id = CreateSimpleNotification(browser(), true);
437 EXPECT_NE(note_id, "-1");
439 ASSERT_EQ(1, GetNotificationCount());
440 ASSERT_TRUE(CancelNotification(note_id.c_str(), browser()));
441 ASSERT_EQ(0, GetNotificationCount());
444 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestPermissionInfobarAppears) {
445 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
447 // Requests notification privileges and verifies the infobar appears.
448 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
449 ASSERT_TRUE(RequestPermissionAndWait(browser()));
451 ASSERT_EQ(0, GetNotificationCount());
452 ASSERT_NO_FATAL_FAILURE(VerifyInfoBar(browser(), 0));
455 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestAllowOnPermissionInfobar) {
456 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
458 // Tries to create a notification and clicks allow on the infobar.
459 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
460 // This notification should not be shown because we do not have permission.
461 CreateSimpleNotification(browser(), false);
462 ASSERT_EQ(0, GetNotificationCount());
464 ASSERT_TRUE(RequestPermissionAndWait(browser()));
465 ASSERT_TRUE(PerformActionOnInfoBar(browser(), ALLOW, 0, 0));
467 CreateSimpleNotification(browser(), true);
468 EXPECT_EQ(1, GetNotificationCount());
471 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestDenyOnPermissionInfobar) {
472 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
474 // Test that no notification is created
475 // when Deny is chosen from permission infobar.
476 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
477 ASSERT_TRUE(RequestPermissionAndWait(browser()));
478 PerformActionOnInfoBar(browser(), DENY, 0, 0);
479 CreateSimpleNotification(browser(), false);
480 ASSERT_EQ(0, GetNotificationCount());
481 ContentSettingsForOneType settings;
482 GetPrefsByContentSetting(CONTENT_SETTING_BLOCK, &settings);
483 EXPECT_TRUE(CheckOriginInSetting(settings, GetTestPageURL()));
486 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestClosePermissionInfobar) {
487 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
489 // Test that no notification is created when permission infobar is dismissed.
490 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
491 ASSERT_TRUE(RequestPermissionAndWait(browser()));
492 PerformActionOnInfoBar(browser(), DISMISS, 0, 0);
493 CreateSimpleNotification(browser(), false);
494 ASSERT_EQ(0, GetNotificationCount());
495 ContentSettingsForOneType settings;
496 GetPrefsByContentSetting(CONTENT_SETTING_BLOCK, &settings);
497 EXPECT_EQ(0U, settings.size());
500 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestAllowNotificationsFromAllSites) {
501 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
503 // Verify that all domains can be allowed to show notifications.
504 SetDefaultContentSetting(CONTENT_SETTING_ALLOW);
505 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
507 std::string result = CreateSimpleNotification(browser(), true);
508 EXPECT_NE("-1", result);
510 ASSERT_EQ(1, GetNotificationCount());
511 InfoBarService* infobar_service = InfoBarService::FromWebContents(
512 browser()->tab_strip_model()->GetWebContentsAt(0));
513 EXPECT_EQ(0U, infobar_service->infobar_count());
516 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestDenyNotificationsFromAllSites) {
517 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
519 // Verify that no domain can show notifications.
520 SetDefaultContentSetting(CONTENT_SETTING_BLOCK);
521 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
523 std::string result = CreateSimpleNotification(browser(), false);
524 EXPECT_EQ("-1", result);
526 ASSERT_EQ(0, GetNotificationCount());
529 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestDenyDomainAndAllowAll) {
530 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
532 // Verify that denying a domain and allowing all shouldn't show
533 // notifications from the denied domain.
534 DenyOrigin(GetTestPageURL().GetOrigin());
535 SetDefaultContentSetting(CONTENT_SETTING_ALLOW);
537 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
539 std::string result = CreateSimpleNotification(browser(), false);
540 EXPECT_EQ("-1", result);
542 ASSERT_EQ(0, GetNotificationCount());
545 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestAllowDomainAndDenyAll) {
546 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
548 // Verify that allowing a domain and denying all others should show
549 // notifications from the allowed domain.
550 AllowOrigin(GetTestPageURL().GetOrigin());
551 SetDefaultContentSetting(CONTENT_SETTING_BLOCK);
553 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
555 std::string result = CreateSimpleNotification(browser(), true);
556 EXPECT_NE("-1", result);
558 ASSERT_EQ(1, GetNotificationCount());
561 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestDenyAndThenAllowDomain) {
562 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
564 // Verify that denying and again allowing should show notifications.
565 DenyOrigin(GetTestPageURL().GetOrigin());
567 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
569 std::string result = CreateSimpleNotification(browser(), false);
570 EXPECT_EQ("-1", result);
572 ASSERT_EQ(0, GetNotificationCount());
574 AllowOrigin(GetTestPageURL().GetOrigin());
575 result = CreateSimpleNotification(browser(), true);
576 EXPECT_NE("-1", result);
578 ASSERT_EQ(1, GetNotificationCount());
579 InfoBarService* infobar_service = InfoBarService::FromWebContents(
580 browser()->tab_strip_model()->GetWebContentsAt(0));
581 EXPECT_EQ(0U, infobar_service->infobar_count());
584 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestCreateDenyCloseNotifications) {
585 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
587 // Verify able to create, deny, and close the notification.
588 AllowAllOrigins();
589 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
590 CreateSimpleNotification(browser(), true);
591 ASSERT_EQ(1, GetNotificationCount());
593 DenyOrigin(GetTestPageURL().GetOrigin());
594 ContentSettingsForOneType settings;
595 GetPrefsByContentSetting(CONTENT_SETTING_BLOCK, &settings);
596 ASSERT_TRUE(CheckOriginInSetting(settings, GetTestPageURL().GetOrigin()));
598 EXPECT_EQ(1, GetNotificationCount());
599 message_center::NotificationList::Notifications notifications =
600 message_center::MessageCenter::Get()->GetVisibleNotifications();
601 message_center::MessageCenter::Get()->RemoveNotification(
602 (*notifications.rbegin())->id(),
603 true); // by_user
604 ASSERT_EQ(0, GetNotificationCount());
607 // Crashes on Linux/Win. See http://crbug.com/160657.
608 IN_PROC_BROWSER_TEST_F(
609 NotificationsTest,
610 DISABLED_TestOriginPrefsNotSavedInIncognito) {
611 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
613 // Verify that allow/deny origin preferences are not saved in incognito.
614 Browser* incognito = CreateIncognitoBrowser();
615 ui_test_utils::NavigateToURL(incognito, GetTestPageURL());
616 ASSERT_TRUE(RequestPermissionAndWait(incognito));
617 PerformActionOnInfoBar(incognito, DENY, 0, 0);
618 CloseBrowserWindow(incognito);
620 incognito = CreateIncognitoBrowser();
621 ui_test_utils::NavigateToURL(incognito, GetTestPageURL());
622 ASSERT_TRUE(RequestPermissionAndWait(incognito));
623 PerformActionOnInfoBar(incognito, ALLOW, 0, 0);
624 CreateSimpleNotification(incognito, true);
625 ASSERT_EQ(1, GetNotificationCount());
626 CloseBrowserWindow(incognito);
628 incognito = CreateIncognitoBrowser();
629 ui_test_utils::NavigateToURL(incognito, GetTestPageURL());
630 ASSERT_TRUE(RequestPermissionAndWait(incognito));
632 ContentSettingsForOneType settings;
633 GetPrefsByContentSetting(CONTENT_SETTING_BLOCK, &settings);
634 EXPECT_EQ(0U, settings.size());
635 GetPrefsByContentSetting(CONTENT_SETTING_ALLOW, &settings);
636 EXPECT_EQ(0U, settings.size());
639 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestExitBrowserWithInfobar) {
640 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
642 // Exit the browser window, when the infobar appears.
643 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
644 ASSERT_TRUE(RequestPermissionAndWait(browser()));
647 // Times out. http://crbug.com/168976
648 IN_PROC_BROWSER_TEST_F(NotificationsTest,
649 DISABLED_TestCrashTabWithPermissionInfobar) {
650 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
652 // Test crashing the tab with permission infobar doesn't crash Chrome.
653 ui_test_utils::NavigateToURLWithDisposition(
654 browser(),
655 embedded_test_server()->GetURL("/empty.html"),
656 NEW_BACKGROUND_TAB,
657 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB);
658 browser()->tab_strip_model()->ActivateTabAt(0, true);
659 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
660 ASSERT_TRUE(RequestPermissionAndWait(browser()));
661 CrashTab(browser(), 0);
664 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestIncognitoNotification) {
665 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
667 // Test notifications in incognito window.
668 Browser* browser = CreateIncognitoBrowser();
669 ui_test_utils::NavigateToURL(browser, GetTestPageURL());
670 browser->tab_strip_model()->ActivateTabAt(0, true);
671 ASSERT_TRUE(RequestPermissionAndWait(browser));
672 PerformActionOnInfoBar(browser, ALLOW, 0, 0);
673 CreateSimpleNotification(browser, true);
674 ASSERT_EQ(1, GetNotificationCount());
677 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestCloseTabWithPermissionInfobar) {
678 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
680 // Test that user can close tab when infobar present.
681 ui_test_utils::NavigateToURLWithDisposition(
682 browser(),
683 GURL("about:blank"),
684 NEW_BACKGROUND_TAB,
685 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB);
686 browser()->tab_strip_model()->ActivateTabAt(0, true);
687 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
688 ASSERT_TRUE(RequestPermissionAndWait(browser()));
689 content::WebContentsDestroyedWatcher destroyed_watcher(
690 browser()->tab_strip_model()->GetWebContentsAt(0));
691 browser()->tab_strip_model()->CloseWebContentsAt(0,
692 TabStripModel::CLOSE_NONE);
693 destroyed_watcher.Wait();
696 IN_PROC_BROWSER_TEST_F(
697 NotificationsTest,
698 TestNavigateAwayWithPermissionInfobar) {
699 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
701 // Test navigating away when an infobar is present,
702 // then trying to create a notification from the same page.
703 ui_test_utils::NavigateToURLWithDisposition(
704 browser(),
705 GURL("about:blank"),
706 NEW_BACKGROUND_TAB,
707 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB);
708 browser()->tab_strip_model()->ActivateTabAt(0, true);
709 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
710 ASSERT_TRUE(RequestPermissionAndWait(browser()));
711 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
712 ASSERT_TRUE(RequestPermissionAndWait(browser()));
713 PerformActionOnInfoBar(browser(), ALLOW, 0, 0);
714 CreateSimpleNotification(browser(), true);
715 ASSERT_EQ(1, GetNotificationCount());
718 // See crbug.com/248470
719 #if defined(OS_LINUX)
720 #define MAYBE_TestCrashRendererNotificationRemain \
721 DISABLED_TestCrashRendererNotificationRemain
722 #else
723 #define MAYBE_TestCrashRendererNotificationRemain \
724 TestCrashRendererNotificationRemain
725 #endif
727 IN_PROC_BROWSER_TEST_F(NotificationsTest,
728 MAYBE_TestCrashRendererNotificationRemain) {
729 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
731 // Test crashing renderer does not close or crash notification.
732 AllowAllOrigins();
733 ui_test_utils::NavigateToURLWithDisposition(
734 browser(),
735 GURL("about:blank"),
736 NEW_BACKGROUND_TAB,
737 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB);
738 browser()->tab_strip_model()->ActivateTabAt(0, true);
739 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
740 CreateSimpleNotification(browser(), true);
741 ASSERT_EQ(1, GetNotificationCount());
742 CrashTab(browser(), 0);
743 ASSERT_EQ(1, GetNotificationCount());
746 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestNotificationReplacement) {
747 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
749 // Test that we can replace a notification using the replaceId.
750 AllowAllOrigins();
752 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
754 std::string result = CreateNotification(
755 browser(), true, "abc.png", "Title1", "Body1", "chat");
756 EXPECT_NE("-1", result);
758 ASSERT_EQ(1, GetNotificationCount());
760 result = CreateNotification(
761 browser(), false, "no_such_file.png", "Title2", "Body2", "chat");
762 EXPECT_NE("-1", result);
764 ASSERT_EQ(1, GetNotificationCount());
765 message_center::NotificationList::Notifications notifications =
766 message_center::MessageCenter::Get()->GetVisibleNotifications();
767 EXPECT_EQ(base::ASCIIToUTF16("Title2"), (*notifications.rbegin())->title());
768 EXPECT_EQ(base::ASCIIToUTF16("Body2"),
769 (*notifications.rbegin())->message());
772 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestLastUsage) {
773 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
775 HostContentSettingsMap* settings_map =
776 browser()->profile()->GetHostContentSettingsMap();
777 base::SimpleTestClock* clock = new base::SimpleTestClock();
778 settings_map->SetPrefClockForTesting(scoped_ptr<base::Clock>(clock));
779 clock->SetNow(base::Time::UnixEpoch() + base::TimeDelta::FromSeconds(10));
781 // Creates a simple notification.
782 AllowAllOrigins();
783 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
785 std::string result = CreateSimpleNotification(browser(), true);
786 EXPECT_NE("-1", result);
788 EXPECT_EQ(settings_map->GetLastUsage(GetTestPageURL().GetOrigin(),
789 GetTestPageURL().GetOrigin(),
790 CONTENT_SETTINGS_TYPE_NOTIFICATIONS)
791 .ToDoubleT(),
792 10);
794 clock->Advance(base::TimeDelta::FromSeconds(3));
796 result = CreateSimpleNotification(browser(), true);
797 EXPECT_NE("-1", result);
799 EXPECT_EQ(settings_map->GetLastUsage(GetTestPageURL().GetOrigin(),
800 GetTestPageURL().GetOrigin(),
801 CONTENT_SETTINGS_TYPE_NOTIFICATIONS)
802 .ToDoubleT(),
803 13);
806 IN_PROC_BROWSER_TEST_F(NotificationsTest,
807 TestNotificationReplacementReappearance) {
808 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
810 // Test that we can replace a notification using the tag, and that it will
811 // cause the notification to reappear as a popup again.
812 AllowAllOrigins();
814 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
816 ASSERT_EQ(0, GetNotificationPopupCount());
818 std::string result = CreateNotification(
819 browser(), true, "abc.png", "Title1", "Body1", "chat");
820 EXPECT_NE("-1", result);
822 ASSERT_EQ(1, GetNotificationPopupCount());
824 message_center::NotificationList::Notifications notifications =
825 message_center::MessageCenter::Get()->GetVisibleNotifications();
826 message_center::MessageCenter::Get()->ClickOnNotification(
827 (*notifications.rbegin())->id());
829 ASSERT_EQ(0, GetNotificationPopupCount());
831 result = CreateNotification(
832 browser(), true, "abc.png", "Title2", "Body2", "chat");
833 EXPECT_NE("-1", result);
835 ASSERT_EQ(1, GetNotificationPopupCount());
838 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestNotificationValidIcon) {
839 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
840 AllowAllOrigins();
842 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
843 ASSERT_EQ(0, GetNotificationPopupCount());
845 std::string result = CreateNotification(
846 browser(), true, "icon.png", "Title1", "Body1", "chat");
847 EXPECT_NE("-1", result);
849 message_center::NotificationList::PopupNotifications notifications =
850 message_center::MessageCenter::Get()->GetPopupNotifications();
851 ASSERT_EQ(1u, notifications.size());
853 auto* notification = *notifications.rbegin();
855 EXPECT_EQ(100, notification->icon().Width());
856 EXPECT_EQ(100, notification->icon().Height());
859 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestNotificationInvalidIcon) {
860 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
861 AllowAllOrigins();
863 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
864 ASSERT_EQ(0, GetNotificationPopupCount());
866 // Not supplying an icon URL.
867 std::string result = CreateNotification(
868 browser(), true, "", "Title1", "Body1", "chat");
869 EXPECT_NE("-1", result);
871 message_center::NotificationList::PopupNotifications notifications =
872 message_center::MessageCenter::Get()->GetPopupNotifications();
873 ASSERT_EQ(1u, notifications.size());
875 auto* notification = *notifications.rbegin();
876 EXPECT_TRUE(notification->icon().IsEmpty());
878 // Supplying an invalid icon URL.
879 result = CreateNotification(
880 browser(), true, "invalid.png", "Title1", "Body1", "chat");
881 EXPECT_NE("-1", result);
883 notifications = message_center::MessageCenter::Get()->GetPopupNotifications();
884 ASSERT_EQ(1u, notifications.size());
886 notification = *notifications.rbegin();
887 EXPECT_TRUE(notification->icon().IsEmpty());
890 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestNotificationDoubleClose) {
891 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
892 AllowAllOrigins();
894 ui_test_utils::NavigateToURL(
895 browser(), GetTestPageURLForFile("notification-double-close.html"));
896 ASSERT_EQ(0, GetNotificationPopupCount());
898 std::string result = CreateNotification(
899 browser(), true, "", "Title1", "Body1", "chat");
900 EXPECT_NE("-1", result);
902 ASSERT_EQ(1, GetNotificationCount());
903 message_center::NotificationList::Notifications notifications =
904 message_center::MessageCenter::Get()->GetVisibleNotifications();
905 message_center::MessageCenter::Get()->RemoveNotification(
906 (*notifications.rbegin())->id(),
907 true); // by_user
909 ASSERT_EQ(0, GetNotificationCount());
911 // Calling WebContents::IsCrashed() will return FALSE here, even if the WC did
912 // crash. Work around this timing issue by creating another notification,
913 // which requires interaction with the renderer process.
914 result = CreateNotification(browser(), true, "", "Title1", "Body1", "chat");
915 EXPECT_NE("-1", result);