Switch global error menu icon to vectorized MD asset
[chromium-blink-merge.git] / chrome / browser / notifications / notification_browsertest.cc
blobab807de981d93ef6248213306eef409d5560fbc7
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/browser/ui/website_settings/permission_bubble_manager.h"
29 #include "chrome/test/base/in_process_browser_test.h"
30 #include "chrome/test/base/ui_test_utils.h"
31 #include "components/content_settings/core/browser/host_content_settings_map.h"
32 #include "components/content_settings/core/common/content_settings.h"
33 #include "components/content_settings/core/common/content_settings_pattern.h"
34 #include "content/public/browser/notification_service.h"
35 #include "content/public/browser/notification_source.h"
36 #include "content/public/browser/notification_types.h"
37 #include "content/public/browser/render_view_host.h"
38 #include "content/public/browser/web_contents.h"
39 #include "content/public/test/browser_test_utils.h"
40 #include "content/public/test/test_utils.h"
41 #include "net/base/net_util.h"
42 #include "net/test/embedded_test_server/embedded_test_server.h"
43 #include "testing/gtest/include/gtest/gtest.h"
44 #include "ui/base/window_open_disposition.h"
45 #include "ui/message_center/message_center.h"
46 #include "ui/message_center/message_center_observer.h"
47 #include "url/gurl.h"
49 namespace {
51 const char kExpectedIconUrl[] = "/notifications/no_such_file.png";
53 class NotificationChangeObserver {
54 public:
55 virtual ~NotificationChangeObserver() {}
56 virtual bool Wait() = 0;
59 class MessageCenterChangeObserver
60 : public message_center::MessageCenterObserver,
61 public NotificationChangeObserver {
62 public:
63 MessageCenterChangeObserver()
64 : notification_received_(false) {
65 message_center::MessageCenter::Get()->AddObserver(this);
68 ~MessageCenterChangeObserver() override {
69 message_center::MessageCenter::Get()->RemoveObserver(this);
72 // NotificationChangeObserver:
73 bool Wait() override {
74 if (notification_received_)
75 return true;
77 message_loop_runner_ = new content::MessageLoopRunner;
78 message_loop_runner_->Run();
79 return notification_received_;
82 // message_center::MessageCenterObserver:
83 void OnNotificationAdded(const std::string& notification_id) override {
84 OnMessageCenterChanged();
87 void OnNotificationRemoved(const std::string& notification_id,
88 bool by_user) override {
89 OnMessageCenterChanged();
92 void OnNotificationUpdated(const std::string& notification_id) override {
93 OnMessageCenterChanged();
96 void OnMessageCenterChanged() {
97 notification_received_ = true;
98 if (message_loop_runner_.get())
99 message_loop_runner_->Quit();
102 bool notification_received_;
103 scoped_refptr<content::MessageLoopRunner> message_loop_runner_;
105 DISALLOW_COPY_AND_ASSIGN(MessageCenterChangeObserver);
108 // Used to observe the creation of permission prompt without responding.
109 class PermissionRequestObserver : public PermissionBubbleManager::Observer {
110 public:
111 explicit PermissionRequestObserver(content::WebContents* web_contents)
112 : bubble_manager_(PermissionBubbleManager::FromWebContents(web_contents)),
113 request_shown_(false),
114 message_loop_runner_(new content::MessageLoopRunner) {
115 bubble_manager_->AddObserver(this);
117 ~PermissionRequestObserver() override {
118 // Safe to remove twice if it happens.
119 bubble_manager_->RemoveObserver(this);
122 void Wait() { message_loop_runner_->Run(); }
124 bool request_shown() { return request_shown_; }
126 private:
127 // PermissionBubbleManager::Observer
128 void OnBubbleAdded() override {
129 request_shown_ = true;
130 bubble_manager_->RemoveObserver(this);
131 message_loop_runner_->Quit();
134 PermissionBubbleManager* bubble_manager_;
135 bool request_shown_;
136 scoped_refptr<content::MessageLoopRunner> message_loop_runner_;
138 DISALLOW_COPY_AND_ASSIGN(PermissionRequestObserver);
141 } // namespace
143 class NotificationsTest : public InProcessBrowserTest {
144 public:
145 NotificationsTest() {}
147 protected:
148 int GetNotificationCount();
149 int GetNotificationPopupCount();
151 void CloseBrowserWindow(Browser* browser);
152 void CrashTab(Browser* browser, int index);
154 void DenyOrigin(const GURL& origin);
155 void AllowOrigin(const GURL& origin);
156 void AllowAllOrigins();
157 void SetDefaultContentSetting(ContentSetting setting);
159 std::string CreateNotification(Browser* browser,
160 bool wait_for_new_balloon,
161 const char* icon,
162 const char* title,
163 const char* body,
164 const char* replace_id);
165 std::string CreateSimpleNotification(Browser* browser,
166 bool wait_for_new_balloon);
167 bool RequestAndAcceptPermission(Browser* browser);
168 bool RequestAndDenyPermission(Browser* browser);
169 bool RequestAndDismissPermission(Browser* browser);
170 bool RequestPermissionAndWait(Browser* browser);
171 bool CancelNotification(const char* notification_id, Browser* browser);
172 void GetPrefsByContentSetting(ContentSetting setting,
173 ContentSettingsForOneType* settings);
174 bool CheckOriginInSetting(const ContentSettingsForOneType& settings,
175 const GURL& origin);
177 GURL GetTestPageURLForFile(const std::string& file) const {
178 return embedded_test_server()->GetURL(
179 std::string("/notifications/") + file);
182 GURL GetTestPageURL() const {
183 return GetTestPageURLForFile("notification_tester.html");
186 content::WebContents* GetActiveWebContents(Browser* browser) {
187 return browser->tab_strip_model()->GetActiveWebContents();
190 private:
191 void DropOriginPreference(const GURL& origin);
192 std::string RequestAndRespondToPermission(
193 Browser* browser,
194 PermissionBubbleManager::AutoResponseType bubble_response);
197 int NotificationsTest::GetNotificationCount() {
198 return message_center::MessageCenter::Get()->NotificationCount();
201 int NotificationsTest::GetNotificationPopupCount() {
202 return message_center::MessageCenter::Get()->GetPopupNotifications().size();
205 void NotificationsTest::CloseBrowserWindow(Browser* browser) {
206 content::WindowedNotificationObserver observer(
207 chrome::NOTIFICATION_BROWSER_CLOSED,
208 content::Source<Browser>(browser));
209 browser->window()->Close();
210 observer.Wait();
213 void NotificationsTest::CrashTab(Browser* browser, int index) {
214 content::CrashTab(browser->tab_strip_model()->GetWebContentsAt(index));
217 void NotificationsTest::DenyOrigin(const GURL& origin) {
218 DropOriginPreference(origin);
219 DesktopNotificationProfileUtil::DenyPermission(browser()->profile(), origin);
222 void NotificationsTest::AllowOrigin(const GURL& origin) {
223 DropOriginPreference(origin);
224 DesktopNotificationProfileUtil::GrantPermission(browser()->profile(), origin);
227 void NotificationsTest::AllowAllOrigins() {
228 // Reset all origins
229 browser()->profile()->GetHostContentSettingsMap()->ClearSettingsForOneType(
230 CONTENT_SETTINGS_TYPE_NOTIFICATIONS);
231 SetDefaultContentSetting(CONTENT_SETTING_ALLOW);
234 void NotificationsTest::SetDefaultContentSetting(ContentSetting setting) {
235 browser()->profile()->GetHostContentSettingsMap()->SetDefaultContentSetting(
236 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, setting);
239 std::string NotificationsTest::CreateNotification(
240 Browser* browser,
241 bool wait_for_new_balloon,
242 const char* icon,
243 const char* title,
244 const char* body,
245 const char* replace_id) {
246 std::string script = base::StringPrintf(
247 "createNotification('%s', '%s', '%s', '%s');",
248 icon, title, body, replace_id);
250 MessageCenterChangeObserver observer;
251 std::string result;
252 bool success = content::ExecuteScriptAndExtractString(
253 GetActiveWebContents(browser), script, &result);
254 if (success && result != "-1" && wait_for_new_balloon)
255 success = observer.Wait();
256 EXPECT_TRUE(success);
258 return result;
261 std::string NotificationsTest::CreateSimpleNotification(
262 Browser* browser,
263 bool wait_for_new_balloon) {
264 return CreateNotification(
265 browser, wait_for_new_balloon,
266 "no_such_file.png", "My Title", "My Body", "");
269 std::string NotificationsTest::RequestAndRespondToPermission(
270 Browser* browser,
271 PermissionBubbleManager::AutoResponseType bubble_response) {
272 std::string result;
273 content::WebContents* web_contents = GetActiveWebContents(browser);
274 PermissionBubbleManager::FromWebContents(web_contents)
275 ->set_auto_response_for_test(bubble_response);
276 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
277 web_contents, "requestPermission();", &result));
278 return result;
281 bool NotificationsTest::RequestAndAcceptPermission(Browser* browser) {
282 std::string result = RequestAndRespondToPermission(
283 browser, PermissionBubbleManager::ACCEPT_ALL);
284 return "request-callback-granted" == result;
287 bool NotificationsTest::RequestAndDenyPermission(Browser* browser) {
288 std::string result =
289 RequestAndRespondToPermission(browser, PermissionBubbleManager::DENY_ALL);
290 return "request-callback-denied" == result;
293 bool NotificationsTest::RequestAndDismissPermission(Browser* browser) {
294 std::string result =
295 RequestAndRespondToPermission(browser, PermissionBubbleManager::DISMISS);
296 return "request-callback-default" == result;
299 bool NotificationsTest::RequestPermissionAndWait(Browser* browser) {
300 content::WebContents* web_contents = GetActiveWebContents(browser);
301 ui_test_utils::NavigateToURL(browser, GetTestPageURL());
302 PermissionRequestObserver observer(web_contents);
303 std::string result;
304 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
305 web_contents, "requestPermissionAndRespond();", &result));
306 EXPECT_EQ("requested", result);
307 observer.Wait();
308 return observer.request_shown();
311 bool NotificationsTest::CancelNotification(
312 const char* notification_id,
313 Browser* browser) {
314 std::string script = base::StringPrintf(
315 "cancelNotification('%s');",
316 notification_id);
318 MessageCenterChangeObserver observer;
319 std::string result;
320 bool success = content::ExecuteScriptAndExtractString(
321 GetActiveWebContents(browser), script, &result);
322 if (!success || result != "1")
323 return false;
324 return observer.Wait();
327 void NotificationsTest::GetPrefsByContentSetting(
328 ContentSetting setting,
329 ContentSettingsForOneType* settings) {
330 DesktopNotificationProfileUtil::GetNotificationsSettings(
331 browser()->profile(), settings);
332 for (ContentSettingsForOneType::iterator it = settings->begin();
333 it != settings->end(); ) {
334 if (it->setting != setting || it->source.compare("preference") != 0)
335 it = settings->erase(it);
336 else
337 ++it;
341 bool NotificationsTest::CheckOriginInSetting(
342 const ContentSettingsForOneType& settings,
343 const GURL& origin) {
344 ContentSettingsPattern pattern =
345 ContentSettingsPattern::FromURLNoWildcard(origin);
346 for (ContentSettingsForOneType::const_iterator it = settings.begin();
347 it != settings.end(); ++it) {
348 if (it->primary_pattern == pattern)
349 return true;
351 return false;
354 void NotificationsTest::DropOriginPreference(const GURL& origin) {
355 DesktopNotificationProfileUtil::ClearSetting(browser()->profile(),
356 ContentSettingsPattern::FromURLNoWildcard(origin));
359 // Flaky on Windows, Mac, Linux: http://crbug.com/437414.
360 IN_PROC_BROWSER_TEST_F(NotificationsTest, DISABLED_TestUserGestureInfobar) {
361 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
363 ui_test_utils::NavigateToURL(
364 browser(),
365 embedded_test_server()->GetURL(
366 "/notifications/notifications_request_function.html"));
368 // Request permission by calling request() while eval'ing an inline script;
369 // That's considered a user gesture to webkit, and should produce an infobar.
370 bool result;
371 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
372 GetActiveWebContents(browser()),
373 "window.domAutomationController.send(request());", &result));
374 EXPECT_TRUE(result);
376 InfoBarService* infobar_service = InfoBarService::FromWebContents(
377 browser()->tab_strip_model()->GetWebContentsAt(0));
378 EXPECT_EQ(1U, infobar_service->infobar_count());
381 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestCreateSimpleNotification) {
382 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
384 // Creates a simple notification.
385 AllowAllOrigins();
386 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
388 std::string result = CreateSimpleNotification(browser(), true);
389 EXPECT_NE("-1", result);
391 GURL EXPECTED_ICON_URL = embedded_test_server()->GetURL(kExpectedIconUrl);
392 ASSERT_EQ(1, GetNotificationCount());
393 message_center::NotificationList::Notifications notifications =
394 message_center::MessageCenter::Get()->GetVisibleNotifications();
395 EXPECT_EQ(base::ASCIIToUTF16("My Title"),
396 (*notifications.rbegin())->title());
397 EXPECT_EQ(base::ASCIIToUTF16("My Body"),
398 (*notifications.rbegin())->message());
401 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestCloseNotification) {
402 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
404 // Creates a notification and closes it.
405 AllowAllOrigins();
406 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
408 std::string result = CreateSimpleNotification(browser(), true);
409 EXPECT_NE("-1", result);
410 ASSERT_EQ(1, GetNotificationCount());
412 message_center::NotificationList::Notifications notifications =
413 message_center::MessageCenter::Get()->GetVisibleNotifications();
414 message_center::MessageCenter::Get()->RemoveNotification(
415 (*notifications.rbegin())->id(),
416 true); // by_user
418 ASSERT_EQ(0, GetNotificationCount());
421 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestCancelNotification) {
422 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
424 // Creates a notification and cancels it in the origin page.
425 AllowAllOrigins();
426 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
428 std::string note_id = CreateSimpleNotification(browser(), true);
429 EXPECT_NE(note_id, "-1");
431 ASSERT_EQ(1, GetNotificationCount());
432 ASSERT_TRUE(CancelNotification(note_id.c_str(), browser()));
433 ASSERT_EQ(0, GetNotificationCount());
436 // Requests notification privileges and verifies the prompt appears.
437 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestPermissionRequestUIAppears) {
438 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
440 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
441 EXPECT_TRUE(RequestPermissionAndWait(browser()));
442 ASSERT_EQ(0, GetNotificationCount());
445 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestAllowOnPermissionRequestUI) {
446 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
448 // Tries to create a notification & clicks 'allow' on the prompt.
449 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
450 // This notification should not be shown because we do not have permission.
451 CreateSimpleNotification(browser(), false);
452 ASSERT_EQ(0, GetNotificationCount());
454 ASSERT_TRUE(RequestAndAcceptPermission(browser()));
456 CreateSimpleNotification(browser(), true);
457 EXPECT_EQ(1, GetNotificationCount());
460 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestDenyOnPermissionRequestUI) {
461 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
463 // Test that no notification is created when Deny is chosen from prompt.
464 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
465 ASSERT_TRUE(RequestAndDenyPermission(browser()));
466 CreateSimpleNotification(browser(), false);
467 ASSERT_EQ(0, GetNotificationCount());
468 ContentSettingsForOneType settings;
469 GetPrefsByContentSetting(CONTENT_SETTING_BLOCK, &settings);
470 EXPECT_TRUE(CheckOriginInSetting(settings, GetTestPageURL()));
473 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestClosePermissionRequestUI) {
474 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
476 // Test that no notification is created when prompt is dismissed.
477 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
478 ASSERT_TRUE(RequestAndDismissPermission(browser()));
479 CreateSimpleNotification(browser(), false);
480 ASSERT_EQ(0, GetNotificationCount());
481 ContentSettingsForOneType settings;
482 GetPrefsByContentSetting(CONTENT_SETTING_BLOCK, &settings);
483 EXPECT_EQ(0U, settings.size());
486 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestAllowNotificationsFromAllSites) {
487 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
489 // Verify that all domains can be allowed to show notifications.
490 SetDefaultContentSetting(CONTENT_SETTING_ALLOW);
491 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
493 std::string result = CreateSimpleNotification(browser(), true);
494 EXPECT_NE("-1", result);
496 ASSERT_EQ(1, GetNotificationCount());
499 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestDenyNotificationsFromAllSites) {
500 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
502 // Verify that no domain can show notifications.
503 SetDefaultContentSetting(CONTENT_SETTING_BLOCK);
504 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
506 std::string result = CreateSimpleNotification(browser(), false);
507 EXPECT_EQ("-1", result);
509 ASSERT_EQ(0, GetNotificationCount());
512 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestDenyDomainAndAllowAll) {
513 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
515 // Verify that denying a domain and allowing all shouldn't show
516 // notifications from the denied domain.
517 DenyOrigin(GetTestPageURL().GetOrigin());
518 SetDefaultContentSetting(CONTENT_SETTING_ALLOW);
520 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
522 std::string result = CreateSimpleNotification(browser(), false);
523 EXPECT_EQ("-1", result);
525 ASSERT_EQ(0, GetNotificationCount());
528 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestAllowDomainAndDenyAll) {
529 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
531 // Verify that allowing a domain and denying all others should show
532 // notifications from the allowed domain.
533 AllowOrigin(GetTestPageURL().GetOrigin());
534 SetDefaultContentSetting(CONTENT_SETTING_BLOCK);
536 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
538 std::string result = CreateSimpleNotification(browser(), true);
539 EXPECT_NE("-1", result);
541 ASSERT_EQ(1, GetNotificationCount());
544 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestDenyAndThenAllowDomain) {
545 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
547 // Verify that denying and again allowing should show notifications.
548 DenyOrigin(GetTestPageURL().GetOrigin());
550 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
552 std::string result = CreateSimpleNotification(browser(), false);
553 EXPECT_EQ("-1", result);
555 ASSERT_EQ(0, GetNotificationCount());
557 AllowOrigin(GetTestPageURL().GetOrigin());
558 result = CreateSimpleNotification(browser(), true);
559 EXPECT_NE("-1", result);
561 ASSERT_EQ(1, GetNotificationCount());
564 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestCreateDenyCloseNotifications) {
565 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
567 // Verify able to create, deny, and close the notification.
568 AllowAllOrigins();
569 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
570 CreateSimpleNotification(browser(), true);
571 ASSERT_EQ(1, GetNotificationCount());
573 DenyOrigin(GetTestPageURL().GetOrigin());
574 ContentSettingsForOneType settings;
575 GetPrefsByContentSetting(CONTENT_SETTING_BLOCK, &settings);
576 ASSERT_TRUE(CheckOriginInSetting(settings, GetTestPageURL().GetOrigin()));
578 EXPECT_EQ(1, GetNotificationCount());
579 message_center::NotificationList::Notifications notifications =
580 message_center::MessageCenter::Get()->GetVisibleNotifications();
581 message_center::MessageCenter::Get()->RemoveNotification(
582 (*notifications.rbegin())->id(),
583 true); // by_user
584 ASSERT_EQ(0, GetNotificationCount());
587 // Crashes on Linux/Win. See http://crbug.com/160657.
588 IN_PROC_BROWSER_TEST_F(
589 NotificationsTest,
590 DISABLED_TestOriginPrefsNotSavedInIncognito) {
591 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
593 // Verify that allow/deny origin preferences are not saved in incognito.
594 Browser* incognito = CreateIncognitoBrowser();
595 ui_test_utils::NavigateToURL(incognito, GetTestPageURL());
596 ASSERT_TRUE(RequestAndDenyPermission(incognito));
597 CloseBrowserWindow(incognito);
599 incognito = CreateIncognitoBrowser();
600 ui_test_utils::NavigateToURL(incognito, GetTestPageURL());
601 ASSERT_TRUE(RequestAndAcceptPermission(incognito));
602 CreateSimpleNotification(incognito, true);
603 ASSERT_EQ(1, GetNotificationCount());
604 CloseBrowserWindow(incognito);
606 incognito = CreateIncognitoBrowser();
607 ui_test_utils::NavigateToURL(incognito, GetTestPageURL());
608 ASSERT_TRUE(RequestPermissionAndWait(incognito));
610 ContentSettingsForOneType settings;
611 GetPrefsByContentSetting(CONTENT_SETTING_BLOCK, &settings);
612 EXPECT_EQ(0U, settings.size());
613 GetPrefsByContentSetting(CONTENT_SETTING_ALLOW, &settings);
614 EXPECT_EQ(0U, settings.size());
617 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestIncognitoNotification) {
618 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
620 // Test notifications in incognito window.
621 Browser* browser = CreateIncognitoBrowser();
622 ui_test_utils::NavigateToURL(browser, GetTestPageURL());
623 browser->tab_strip_model()->ActivateTabAt(0, true);
624 ASSERT_TRUE(RequestAndAcceptPermission(browser));
625 CreateSimpleNotification(browser, true);
626 ASSERT_EQ(1, GetNotificationCount());
629 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestCloseTabWithPermissionRequestUI) {
630 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
632 // Test that user can close tab when bubble present.
633 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
634 EXPECT_TRUE(RequestPermissionAndWait(browser()));
635 content::WebContentsDestroyedWatcher destroyed_watcher(
636 browser()->tab_strip_model()->GetWebContentsAt(0));
637 browser()->tab_strip_model()->CloseWebContentsAt(0,
638 TabStripModel::CLOSE_NONE);
639 destroyed_watcher.Wait();
642 // See crbug.com/248470
643 #if defined(OS_LINUX)
644 #define MAYBE_TestCrashRendererNotificationRemain \
645 DISABLED_TestCrashRendererNotificationRemain
646 #else
647 #define MAYBE_TestCrashRendererNotificationRemain \
648 TestCrashRendererNotificationRemain
649 #endif
651 IN_PROC_BROWSER_TEST_F(NotificationsTest,
652 MAYBE_TestCrashRendererNotificationRemain) {
653 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
655 // Test crashing renderer does not close or crash notification.
656 AllowAllOrigins();
657 ui_test_utils::NavigateToURLWithDisposition(
658 browser(),
659 GURL("about:blank"),
660 NEW_BACKGROUND_TAB,
661 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB);
662 browser()->tab_strip_model()->ActivateTabAt(0, true);
663 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
664 CreateSimpleNotification(browser(), true);
665 ASSERT_EQ(1, GetNotificationCount());
666 CrashTab(browser(), 0);
667 ASSERT_EQ(1, GetNotificationCount());
670 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestNotificationReplacement) {
671 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
673 // Test that we can replace a notification using the replaceId.
674 AllowAllOrigins();
676 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
678 std::string result = CreateNotification(
679 browser(), true, "abc.png", "Title1", "Body1", "chat");
680 EXPECT_NE("-1", result);
682 ASSERT_EQ(1, GetNotificationCount());
684 result = CreateNotification(
685 browser(), false, "no_such_file.png", "Title2", "Body2", "chat");
686 EXPECT_NE("-1", result);
688 ASSERT_EQ(1, GetNotificationCount());
689 message_center::NotificationList::Notifications notifications =
690 message_center::MessageCenter::Get()->GetVisibleNotifications();
691 EXPECT_EQ(base::ASCIIToUTF16("Title2"), (*notifications.rbegin())->title());
692 EXPECT_EQ(base::ASCIIToUTF16("Body2"),
693 (*notifications.rbegin())->message());
696 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestLastUsage) {
697 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
699 HostContentSettingsMap* settings_map =
700 browser()->profile()->GetHostContentSettingsMap();
701 base::SimpleTestClock* clock = new base::SimpleTestClock();
702 settings_map->SetPrefClockForTesting(scoped_ptr<base::Clock>(clock));
703 clock->SetNow(base::Time::UnixEpoch() + base::TimeDelta::FromSeconds(10));
705 // Creates a simple notification.
706 AllowAllOrigins();
707 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
709 std::string result = CreateSimpleNotification(browser(), true);
710 EXPECT_NE("-1", result);
712 EXPECT_EQ(settings_map->GetLastUsage(GetTestPageURL().GetOrigin(),
713 GetTestPageURL().GetOrigin(),
714 CONTENT_SETTINGS_TYPE_NOTIFICATIONS)
715 .ToDoubleT(),
716 10);
718 clock->Advance(base::TimeDelta::FromSeconds(3));
720 result = CreateSimpleNotification(browser(), true);
721 EXPECT_NE("-1", result);
723 EXPECT_EQ(settings_map->GetLastUsage(GetTestPageURL().GetOrigin(),
724 GetTestPageURL().GetOrigin(),
725 CONTENT_SETTINGS_TYPE_NOTIFICATIONS)
726 .ToDoubleT(),
727 13);
730 IN_PROC_BROWSER_TEST_F(NotificationsTest,
731 TestNotificationReplacementReappearance) {
732 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
734 // Test that we can replace a notification using the tag, and that it will
735 // cause the notification to reappear as a popup again.
736 AllowAllOrigins();
738 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
740 ASSERT_EQ(0, GetNotificationPopupCount());
742 std::string result = CreateNotification(
743 browser(), true, "abc.png", "Title1", "Body1", "chat");
744 EXPECT_NE("-1", result);
746 ASSERT_EQ(1, GetNotificationPopupCount());
748 message_center::NotificationList::Notifications notifications =
749 message_center::MessageCenter::Get()->GetVisibleNotifications();
750 message_center::MessageCenter::Get()->ClickOnNotification(
751 (*notifications.rbegin())->id());
753 ASSERT_EQ(0, GetNotificationPopupCount());
755 result = CreateNotification(
756 browser(), true, "abc.png", "Title2", "Body2", "chat");
757 EXPECT_NE("-1", result);
759 ASSERT_EQ(1, GetNotificationPopupCount());
762 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestNotificationValidIcon) {
763 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
764 AllowAllOrigins();
766 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
767 ASSERT_EQ(0, GetNotificationPopupCount());
769 std::string result = CreateNotification(
770 browser(), true, "icon.png", "Title1", "Body1", "chat");
771 EXPECT_NE("-1", result);
773 message_center::NotificationList::PopupNotifications notifications =
774 message_center::MessageCenter::Get()->GetPopupNotifications();
775 ASSERT_EQ(1u, notifications.size());
777 auto* notification = *notifications.rbegin();
779 EXPECT_EQ(100, notification->icon().Width());
780 EXPECT_EQ(100, notification->icon().Height());
783 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestNotificationInvalidIcon) {
784 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
785 AllowAllOrigins();
787 ui_test_utils::NavigateToURL(browser(), GetTestPageURL());
788 ASSERT_EQ(0, GetNotificationPopupCount());
790 // Not supplying an icon URL.
791 std::string result = CreateNotification(
792 browser(), true, "", "Title1", "Body1", "chat");
793 EXPECT_NE("-1", result);
795 message_center::NotificationList::PopupNotifications notifications =
796 message_center::MessageCenter::Get()->GetPopupNotifications();
797 ASSERT_EQ(1u, notifications.size());
799 auto* notification = *notifications.rbegin();
800 EXPECT_TRUE(notification->icon().IsEmpty());
802 // Supplying an invalid icon URL.
803 result = CreateNotification(
804 browser(), true, "invalid.png", "Title1", "Body1", "chat");
805 EXPECT_NE("-1", result);
807 notifications = message_center::MessageCenter::Get()->GetPopupNotifications();
808 ASSERT_EQ(1u, notifications.size());
810 notification = *notifications.rbegin();
811 EXPECT_TRUE(notification->icon().IsEmpty());
814 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestNotificationDoubleClose) {
815 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
816 AllowAllOrigins();
818 ui_test_utils::NavigateToURL(
819 browser(), GetTestPageURLForFile("notification-double-close.html"));
820 ASSERT_EQ(0, GetNotificationPopupCount());
822 std::string result = CreateNotification(
823 browser(), true, "", "Title1", "Body1", "chat");
824 EXPECT_NE("-1", result);
826 ASSERT_EQ(1, GetNotificationCount());
827 message_center::NotificationList::Notifications notifications =
828 message_center::MessageCenter::Get()->GetVisibleNotifications();
829 message_center::MessageCenter::Get()->RemoveNotification(
830 (*notifications.rbegin())->id(),
831 true); // by_user
833 ASSERT_EQ(0, GetNotificationCount());
835 // Calling WebContents::IsCrashed() will return FALSE here, even if the WC did
836 // crash. Work around this timing issue by creating another notification,
837 // which requires interaction with the renderer process.
838 result = CreateNotification(browser(), true, "", "Title1", "Body1", "chat");
839 EXPECT_NE("-1", result);