| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <deque> | 5 #include <deque> |
| 6 #include <string> | 6 #include <string> |
| 7 | 7 |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/callback.h" | 9 #include "base/callback.h" |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
| 12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
| 14 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
| 15 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
| 16 #include "base/test/simple_test_clock.h" | 16 #include "base/test/simple_test_clock.h" |
| 17 #include "base/time/clock.h" | 17 #include "base/time/clock.h" |
| 18 #include "chrome/browser/browser_process.h" | 18 #include "chrome/browser/browser_process.h" |
| 19 #include "chrome/browser/chrome_notification_types.h" | 19 #include "chrome/browser/chrome_notification_types.h" |
| 20 #include "chrome/browser/content_settings/host_content_settings_map.h" | 20 #include "chrome/browser/content_settings/host_content_settings_map.h" |
| 21 #include "chrome/browser/infobars/infobar_service.h" | 21 #include "chrome/browser/infobars/infobar_service.h" |
| 22 #include "chrome/browser/notifications/desktop_notification_service.h" | 22 #include "chrome/browser/notifications/desktop_notification_profile_util.h" |
| 23 #include "chrome/browser/notifications/desktop_notification_service_factory.h" | |
| 24 #include "chrome/browser/notifications/notification.h" | 23 #include "chrome/browser/notifications/notification.h" |
| 25 #include "chrome/browser/profiles/profile.h" | 24 #include "chrome/browser/profiles/profile.h" |
| 26 #include "chrome/browser/ui/browser.h" | 25 #include "chrome/browser/ui/browser.h" |
| 27 #include "chrome/browser/ui/browser_tabstrip.h" | 26 #include "chrome/browser/ui/browser_tabstrip.h" |
| 28 #include "chrome/browser/ui/browser_window.h" | 27 #include "chrome/browser/ui/browser_window.h" |
| 29 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 28 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 30 #include "chrome/common/content_settings.h" | 29 #include "chrome/common/content_settings.h" |
| 31 #include "chrome/common/content_settings_pattern.h" | 30 #include "chrome/common/content_settings_pattern.h" |
| 32 #include "chrome/test/base/in_process_browser_test.h" | 31 #include "chrome/test/base/in_process_browser_test.h" |
| 33 #include "chrome/test/base/ui_test_utils.h" | 32 #include "chrome/test/base/ui_test_utils.h" |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 120 class NotificationsTest : public InProcessBrowserTest { | 119 class NotificationsTest : public InProcessBrowserTest { |
| 121 public: | 120 public: |
| 122 NotificationsTest() {} | 121 NotificationsTest() {} |
| 123 | 122 |
| 124 protected: | 123 protected: |
| 125 int GetNotificationCount(); | 124 int GetNotificationCount(); |
| 126 | 125 |
| 127 void CloseBrowserWindow(Browser* browser); | 126 void CloseBrowserWindow(Browser* browser); |
| 128 void CrashTab(Browser* browser, int index); | 127 void CrashTab(Browser* browser, int index); |
| 129 | 128 |
| 130 void SetDefaultPermissionSetting(ContentSetting setting); | |
| 131 void DenyOrigin(const GURL& origin); | 129 void DenyOrigin(const GURL& origin); |
| 132 void AllowOrigin(const GURL& origin); | 130 void AllowOrigin(const GURL& origin); |
| 133 void AllowAllOrigins(); | 131 void AllowAllOrigins(); |
| 132 void SetDefaultContentSetting(ContentSetting setting); |
| 134 | 133 |
| 135 void VerifyInfoBar(const Browser* browser, int index); | 134 void VerifyInfoBar(const Browser* browser, int index); |
| 136 std::string CreateNotification(Browser* browser, | 135 std::string CreateNotification(Browser* browser, |
| 137 bool wait_for_new_balloon, | 136 bool wait_for_new_balloon, |
| 138 const char* icon, | 137 const char* icon, |
| 139 const char* title, | 138 const char* title, |
| 140 const char* body, | 139 const char* body, |
| 141 const char* replace_id); | 140 const char* replace_id); |
| 142 std::string CreateSimpleNotification(Browser* browser, | 141 std::string CreateSimpleNotification(Browser* browser, |
| 143 bool wait_for_new_balloon); | 142 bool wait_for_new_balloon); |
| 144 bool RequestPermissionAndWait(Browser* browser); | 143 bool RequestPermissionAndWait(Browser* browser); |
| 145 bool CancelNotification(const char* notification_id, Browser* browser); | 144 bool CancelNotification(const char* notification_id, Browser* browser); |
| 146 bool PerformActionOnInfoBar(Browser* browser, | 145 bool PerformActionOnInfoBar(Browser* browser, |
| 147 InfobarAction action, | 146 InfobarAction action, |
| 148 size_t infobar_index, | 147 size_t infobar_index, |
| 149 int tab_index); | 148 int tab_index); |
| 150 void GetPrefsByContentSetting(ContentSetting setting, | 149 void GetPrefsByContentSetting(ContentSetting setting, |
| 151 ContentSettingsForOneType* settings); | 150 ContentSettingsForOneType* settings); |
| 152 bool CheckOriginInSetting(const ContentSettingsForOneType& settings, | 151 bool CheckOriginInSetting(const ContentSettingsForOneType& settings, |
| 153 const GURL& origin); | 152 const GURL& origin); |
| 154 | 153 |
| 155 GURL GetTestPageURL() const { | 154 GURL GetTestPageURL() const { |
| 156 return embedded_test_server()->GetURL( | 155 return embedded_test_server()->GetURL( |
| 157 "/notifications/notification_tester.html"); | 156 "/notifications/notification_tester.html"); |
| 158 } | 157 } |
| 159 | 158 |
| 160 private: | 159 private: |
| 161 void DropOriginPreference(const GURL& origin); | 160 void DropOriginPreference(const GURL& origin); |
| 162 DesktopNotificationService* GetDesktopNotificationService(); | |
| 163 }; | 161 }; |
| 164 | 162 |
| 165 int NotificationsTest::GetNotificationCount() { | 163 int NotificationsTest::GetNotificationCount() { |
| 166 return message_center::MessageCenter::Get()->NotificationCount(); | 164 return message_center::MessageCenter::Get()->NotificationCount(); |
| 167 } | 165 } |
| 168 | 166 |
| 169 void NotificationsTest::CloseBrowserWindow(Browser* browser) { | 167 void NotificationsTest::CloseBrowserWindow(Browser* browser) { |
| 170 content::WindowedNotificationObserver observer( | 168 content::WindowedNotificationObserver observer( |
| 171 chrome::NOTIFICATION_BROWSER_CLOSED, | 169 chrome::NOTIFICATION_BROWSER_CLOSED, |
| 172 content::Source<Browser>(browser)); | 170 content::Source<Browser>(browser)); |
| 173 browser->window()->Close(); | 171 browser->window()->Close(); |
| 174 observer.Wait(); | 172 observer.Wait(); |
| 175 } | 173 } |
| 176 | 174 |
| 177 void NotificationsTest::CrashTab(Browser* browser, int index) { | 175 void NotificationsTest::CrashTab(Browser* browser, int index) { |
| 178 content::CrashTab(browser->tab_strip_model()->GetWebContentsAt(index)); | 176 content::CrashTab(browser->tab_strip_model()->GetWebContentsAt(index)); |
| 179 } | 177 } |
| 180 | 178 |
| 181 void NotificationsTest::SetDefaultPermissionSetting(ContentSetting setting) { | |
| 182 DesktopNotificationService* service = GetDesktopNotificationService(); | |
| 183 service->SetDefaultContentSetting(setting); | |
| 184 } | |
| 185 | |
| 186 void NotificationsTest::DenyOrigin(const GURL& origin) { | 179 void NotificationsTest::DenyOrigin(const GURL& origin) { |
| 187 DropOriginPreference(origin); | 180 DropOriginPreference(origin); |
| 188 GetDesktopNotificationService()->DenyPermission(origin); | 181 DesktopNotificationProfileUtil::DenyPermission(browser()->profile(), origin); |
| 189 } | 182 } |
| 190 | 183 |
| 191 void NotificationsTest::AllowOrigin(const GURL& origin) { | 184 void NotificationsTest::AllowOrigin(const GURL& origin) { |
| 192 DropOriginPreference(origin); | 185 DropOriginPreference(origin); |
| 193 GetDesktopNotificationService()->GrantPermission(origin); | 186 DesktopNotificationProfileUtil::GrantPermission(browser()->profile(), origin); |
| 194 } | 187 } |
| 195 | 188 |
| 196 void NotificationsTest::AllowAllOrigins() { | 189 void NotificationsTest::AllowAllOrigins() { |
| 197 GetDesktopNotificationService()->ResetAllOrigins(); | 190 // Reset all origins |
| 198 GetDesktopNotificationService()->SetDefaultContentSetting( | 191 browser()->profile()->GetHostContentSettingsMap()->ClearSettingsForOneType( |
| 199 CONTENT_SETTING_ALLOW); | 192 CONTENT_SETTINGS_TYPE_NOTIFICATIONS); |
| 193 SetDefaultContentSetting(CONTENT_SETTING_ALLOW); |
| 194 } |
| 195 |
| 196 void NotificationsTest::SetDefaultContentSetting(ContentSetting setting) { |
| 197 browser()->profile()->GetHostContentSettingsMap()->SetDefaultContentSetting( |
| 198 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, setting); |
| 200 } | 199 } |
| 201 | 200 |
| 202 void NotificationsTest::VerifyInfoBar(const Browser* browser, int index) { | 201 void NotificationsTest::VerifyInfoBar(const Browser* browser, int index) { |
| 203 InfoBarService* infobar_service = InfoBarService::FromWebContents( | 202 InfoBarService* infobar_service = InfoBarService::FromWebContents( |
| 204 browser->tab_strip_model()->GetWebContentsAt(index)); | 203 browser->tab_strip_model()->GetWebContentsAt(index)); |
| 205 | 204 |
| 206 ASSERT_EQ(1U, infobar_service->infobar_count()); | 205 ASSERT_EQ(1U, infobar_service->infobar_count()); |
| 207 ConfirmInfoBarDelegate* confirm_infobar = | 206 ConfirmInfoBarDelegate* confirm_infobar = |
| 208 infobar_service->infobar_at(0)->delegate()->AsConfirmInfoBarDelegate(); | 207 infobar_service->infobar_at(0)->delegate()->AsConfirmInfoBarDelegate(); |
| 209 ASSERT_TRUE(confirm_infobar); | 208 ASSERT_TRUE(confirm_infobar); |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 321 } | 320 } |
| 322 } | 321 } |
| 323 } | 322 } |
| 324 | 323 |
| 325 return false; | 324 return false; |
| 326 } | 325 } |
| 327 | 326 |
| 328 void NotificationsTest::GetPrefsByContentSetting( | 327 void NotificationsTest::GetPrefsByContentSetting( |
| 329 ContentSetting setting, | 328 ContentSetting setting, |
| 330 ContentSettingsForOneType* settings) { | 329 ContentSettingsForOneType* settings) { |
| 331 DesktopNotificationService* service = GetDesktopNotificationService(); | 330 DesktopNotificationProfileUtil::GetNotificationsSettings( |
| 332 service->GetNotificationsSettings(settings); | 331 browser()->profile(), settings); |
| 333 for (ContentSettingsForOneType::iterator it = settings->begin(); | 332 for (ContentSettingsForOneType::iterator it = settings->begin(); |
| 334 it != settings->end(); ) { | 333 it != settings->end(); ) { |
| 335 if (it->setting != setting || it->source.compare("preference") != 0) | 334 if (it->setting != setting || it->source.compare("preference") != 0) |
| 336 it = settings->erase(it); | 335 it = settings->erase(it); |
| 337 else | 336 else |
| 338 ++it; | 337 ++it; |
| 339 } | 338 } |
| 340 } | 339 } |
| 341 | 340 |
| 342 bool NotificationsTest::CheckOriginInSetting( | 341 bool NotificationsTest::CheckOriginInSetting( |
| 343 const ContentSettingsForOneType& settings, | 342 const ContentSettingsForOneType& settings, |
| 344 const GURL& origin) { | 343 const GURL& origin) { |
| 345 ContentSettingsPattern pattern = | 344 ContentSettingsPattern pattern = |
| 346 ContentSettingsPattern::FromURLNoWildcard(origin); | 345 ContentSettingsPattern::FromURLNoWildcard(origin); |
| 347 for (ContentSettingsForOneType::const_iterator it = settings.begin(); | 346 for (ContentSettingsForOneType::const_iterator it = settings.begin(); |
| 348 it != settings.end(); ++it) { | 347 it != settings.end(); ++it) { |
| 349 if (it->primary_pattern == pattern) | 348 if (it->primary_pattern == pattern) |
| 350 return true; | 349 return true; |
| 351 } | 350 } |
| 352 return false; | 351 return false; |
| 353 } | 352 } |
| 354 | 353 |
| 355 void NotificationsTest::DropOriginPreference(const GURL& origin) { | 354 void NotificationsTest::DropOriginPreference(const GURL& origin) { |
| 356 GetDesktopNotificationService()->ClearSetting( | 355 DesktopNotificationProfileUtil::ClearSetting(browser()->profile(), |
| 357 ContentSettingsPattern::FromURLNoWildcard(origin)); | 356 ContentSettingsPattern::FromURLNoWildcard(origin)); |
| 358 } | 357 } |
| 359 | 358 |
| 360 DesktopNotificationService* NotificationsTest::GetDesktopNotificationService() { | |
| 361 Profile* profile = browser()->profile(); | |
| 362 return DesktopNotificationServiceFactory::GetForProfile(profile); | |
| 363 } | |
| 364 | |
| 365 // If this flakes, use http://crbug.com/62311 and http://crbug.com/74428. | 359 // If this flakes, use http://crbug.com/62311 and http://crbug.com/74428. |
| 366 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestUserGestureInfobar) { | 360 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestUserGestureInfobar) { |
| 367 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); | 361 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
| 368 | 362 |
| 369 ui_test_utils::NavigateToURL( | 363 ui_test_utils::NavigateToURL( |
| 370 browser(), | 364 browser(), |
| 371 embedded_test_server()->GetURL( | 365 embedded_test_server()->GetURL( |
| 372 "/notifications/notifications_request_function.html")); | 366 "/notifications/notifications_request_function.html")); |
| 373 | 367 |
| 374 // Request permission by calling request() while eval'ing an inline script; | 368 // Request permission by calling request() while eval'ing an inline script; |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 493 ASSERT_EQ(0, GetNotificationCount()); | 487 ASSERT_EQ(0, GetNotificationCount()); |
| 494 ContentSettingsForOneType settings; | 488 ContentSettingsForOneType settings; |
| 495 GetPrefsByContentSetting(CONTENT_SETTING_BLOCK, &settings); | 489 GetPrefsByContentSetting(CONTENT_SETTING_BLOCK, &settings); |
| 496 EXPECT_EQ(0U, settings.size()); | 490 EXPECT_EQ(0U, settings.size()); |
| 497 } | 491 } |
| 498 | 492 |
| 499 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestAllowNotificationsFromAllSites) { | 493 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestAllowNotificationsFromAllSites) { |
| 500 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); | 494 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
| 501 | 495 |
| 502 // Verify that all domains can be allowed to show notifications. | 496 // Verify that all domains can be allowed to show notifications. |
| 503 SetDefaultPermissionSetting(CONTENT_SETTING_ALLOW); | 497 SetDefaultContentSetting(CONTENT_SETTING_ALLOW); |
| 504 ui_test_utils::NavigateToURL(browser(), GetTestPageURL()); | 498 ui_test_utils::NavigateToURL(browser(), GetTestPageURL()); |
| 505 | 499 |
| 506 std::string result = CreateSimpleNotification(browser(), true); | 500 std::string result = CreateSimpleNotification(browser(), true); |
| 507 EXPECT_NE("-1", result); | 501 EXPECT_NE("-1", result); |
| 508 | 502 |
| 509 ASSERT_EQ(1, GetNotificationCount()); | 503 ASSERT_EQ(1, GetNotificationCount()); |
| 510 InfoBarService* infobar_service = InfoBarService::FromWebContents( | 504 InfoBarService* infobar_service = InfoBarService::FromWebContents( |
| 511 browser()->tab_strip_model()->GetWebContentsAt(0)); | 505 browser()->tab_strip_model()->GetWebContentsAt(0)); |
| 512 EXPECT_EQ(0U, infobar_service->infobar_count()); | 506 EXPECT_EQ(0U, infobar_service->infobar_count()); |
| 513 } | 507 } |
| 514 | 508 |
| 515 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestDenyNotificationsFromAllSites) { | 509 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestDenyNotificationsFromAllSites) { |
| 516 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); | 510 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
| 517 | 511 |
| 518 // Verify that no domain can show notifications. | 512 // Verify that no domain can show notifications. |
| 519 SetDefaultPermissionSetting(CONTENT_SETTING_BLOCK); | 513 SetDefaultContentSetting(CONTENT_SETTING_BLOCK); |
| 520 ui_test_utils::NavigateToURL(browser(), GetTestPageURL()); | 514 ui_test_utils::NavigateToURL(browser(), GetTestPageURL()); |
| 521 | 515 |
| 522 std::string result = CreateSimpleNotification(browser(), false); | 516 std::string result = CreateSimpleNotification(browser(), false); |
| 523 EXPECT_EQ("-1", result); | 517 EXPECT_EQ("-1", result); |
| 524 | 518 |
| 525 ASSERT_EQ(0, GetNotificationCount()); | 519 ASSERT_EQ(0, GetNotificationCount()); |
| 526 } | 520 } |
| 527 | 521 |
| 528 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestDenyDomainAndAllowAll) { | 522 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestDenyDomainAndAllowAll) { |
| 529 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); | 523 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
| 530 | 524 |
| 531 // Verify that denying a domain and allowing all shouldn't show | 525 // Verify that denying a domain and allowing all shouldn't show |
| 532 // notifications from the denied domain. | 526 // notifications from the denied domain. |
| 533 DenyOrigin(GetTestPageURL().GetOrigin()); | 527 DenyOrigin(GetTestPageURL().GetOrigin()); |
| 534 SetDefaultPermissionSetting(CONTENT_SETTING_ALLOW); | 528 SetDefaultContentSetting(CONTENT_SETTING_ALLOW); |
| 535 | 529 |
| 536 ui_test_utils::NavigateToURL(browser(), GetTestPageURL()); | 530 ui_test_utils::NavigateToURL(browser(), GetTestPageURL()); |
| 537 | 531 |
| 538 std::string result = CreateSimpleNotification(browser(), false); | 532 std::string result = CreateSimpleNotification(browser(), false); |
| 539 EXPECT_EQ("-1", result); | 533 EXPECT_EQ("-1", result); |
| 540 | 534 |
| 541 ASSERT_EQ(0, GetNotificationCount()); | 535 ASSERT_EQ(0, GetNotificationCount()); |
| 542 } | 536 } |
| 543 | 537 |
| 544 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestAllowDomainAndDenyAll) { | 538 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestAllowDomainAndDenyAll) { |
| 545 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); | 539 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
| 546 | 540 |
| 547 // Verify that allowing a domain and denying all others should show | 541 // Verify that allowing a domain and denying all others should show |
| 548 // notifications from the allowed domain. | 542 // notifications from the allowed domain. |
| 549 AllowOrigin(GetTestPageURL().GetOrigin()); | 543 AllowOrigin(GetTestPageURL().GetOrigin()); |
| 550 SetDefaultPermissionSetting(CONTENT_SETTING_BLOCK); | 544 SetDefaultContentSetting(CONTENT_SETTING_BLOCK); |
| 551 | 545 |
| 552 ui_test_utils::NavigateToURL(browser(), GetTestPageURL()); | 546 ui_test_utils::NavigateToURL(browser(), GetTestPageURL()); |
| 553 | 547 |
| 554 std::string result = CreateSimpleNotification(browser(), true); | 548 std::string result = CreateSimpleNotification(browser(), true); |
| 555 EXPECT_NE("-1", result); | 549 EXPECT_NE("-1", result); |
| 556 | 550 |
| 557 ASSERT_EQ(1, GetNotificationCount()); | 551 ASSERT_EQ(1, GetNotificationCount()); |
| 558 } | 552 } |
| 559 | 553 |
| 560 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestDenyAndThenAllowDomain) { | 554 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestDenyAndThenAllowDomain) { |
| (...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 808 | 802 |
| 809 result = CreateSimpleNotification(browser(), true); | 803 result = CreateSimpleNotification(browser(), true); |
| 810 EXPECT_NE("-1", result); | 804 EXPECT_NE("-1", result); |
| 811 | 805 |
| 812 EXPECT_EQ(settings_map->GetLastUsage(GetTestPageURL().GetOrigin(), | 806 EXPECT_EQ(settings_map->GetLastUsage(GetTestPageURL().GetOrigin(), |
| 813 GetTestPageURL().GetOrigin(), | 807 GetTestPageURL().GetOrigin(), |
| 814 CONTENT_SETTINGS_TYPE_NOTIFICATIONS) | 808 CONTENT_SETTINGS_TYPE_NOTIFICATIONS) |
| 815 .ToDoubleT(), | 809 .ToDoubleT(), |
| 816 13); | 810 13); |
| 817 } | 811 } |
| OLD | NEW |