| 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 "chrome/browser/browser_process.h" | 16 #include "chrome/browser/browser_process.h" |
| 17 #include "chrome/browser/chrome_notification_types.h" | 17 #include "chrome/browser/chrome_notification_types.h" |
| 18 #include "chrome/browser/infobars/infobar_service.h" | 18 #include "chrome/browser/infobars/infobar_service.h" |
| 19 #include "chrome/browser/notifications/desktop_notification_service.h" | 19 #include "chrome/browser/notifications/desktop_notification_profile_util.h" |
| 20 #include "chrome/browser/notifications/desktop_notification_service_factory.h" | |
| 21 #include "chrome/browser/notifications/notification.h" | 20 #include "chrome/browser/notifications/notification.h" |
| 22 #include "chrome/browser/profiles/profile.h" | 21 #include "chrome/browser/profiles/profile.h" |
| 23 #include "chrome/browser/ui/browser.h" | 22 #include "chrome/browser/ui/browser.h" |
| 24 #include "chrome/browser/ui/browser_tabstrip.h" | 23 #include "chrome/browser/ui/browser_tabstrip.h" |
| 25 #include "chrome/browser/ui/browser_window.h" | 24 #include "chrome/browser/ui/browser_window.h" |
| 26 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 25 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 27 #include "chrome/common/content_settings.h" | 26 #include "chrome/common/content_settings.h" |
| 28 #include "chrome/common/content_settings_pattern.h" | 27 #include "chrome/common/content_settings_pattern.h" |
| 29 #include "chrome/test/base/in_process_browser_test.h" | 28 #include "chrome/test/base/in_process_browser_test.h" |
| 30 #include "chrome/test/base/ui_test_utils.h" | 29 #include "chrome/test/base/ui_test_utils.h" |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 class NotificationsTest : public InProcessBrowserTest { | 116 class NotificationsTest : public InProcessBrowserTest { |
| 118 public: | 117 public: |
| 119 NotificationsTest() {} | 118 NotificationsTest() {} |
| 120 | 119 |
| 121 protected: | 120 protected: |
| 122 int GetNotificationCount(); | 121 int GetNotificationCount(); |
| 123 | 122 |
| 124 void CloseBrowserWindow(Browser* browser); | 123 void CloseBrowserWindow(Browser* browser); |
| 125 void CrashTab(Browser* browser, int index); | 124 void CrashTab(Browser* browser, int index); |
| 126 | 125 |
| 127 void SetDefaultPermissionSetting(ContentSetting setting); | |
| 128 void DenyOrigin(const GURL& origin); | 126 void DenyOrigin(const GURL& origin); |
| 129 void AllowOrigin(const GURL& origin); | 127 void AllowOrigin(const GURL& origin); |
| 130 void AllowAllOrigins(); | 128 void AllowAllOrigins(); |
| 131 | 129 |
| 132 void VerifyInfoBar(const Browser* browser, int index); | 130 void VerifyInfoBar(const Browser* browser, int index); |
| 133 std::string CreateNotification(Browser* browser, | 131 std::string CreateNotification(Browser* browser, |
| 134 bool wait_for_new_balloon, | 132 bool wait_for_new_balloon, |
| 135 const char* icon, | 133 const char* icon, |
| 136 const char* title, | 134 const char* title, |
| 137 const char* body, | 135 const char* body, |
| (...skipping 11 matching lines...) Expand all Loading... |
| 149 bool CheckOriginInSetting(const ContentSettingsForOneType& settings, | 147 bool CheckOriginInSetting(const ContentSettingsForOneType& settings, |
| 150 const GURL& origin); | 148 const GURL& origin); |
| 151 | 149 |
| 152 GURL GetTestPageURL() const { | 150 GURL GetTestPageURL() const { |
| 153 return embedded_test_server()->GetURL( | 151 return embedded_test_server()->GetURL( |
| 154 "/notifications/notification_tester.html"); | 152 "/notifications/notification_tester.html"); |
| 155 } | 153 } |
| 156 | 154 |
| 157 private: | 155 private: |
| 158 void DropOriginPreference(const GURL& origin); | 156 void DropOriginPreference(const GURL& origin); |
| 159 DesktopNotificationService* GetDesktopNotificationService(); | |
| 160 }; | 157 }; |
| 161 | 158 |
| 162 int NotificationsTest::GetNotificationCount() { | 159 int NotificationsTest::GetNotificationCount() { |
| 163 return message_center::MessageCenter::Get()->NotificationCount(); | 160 return message_center::MessageCenter::Get()->NotificationCount(); |
| 164 } | 161 } |
| 165 | 162 |
| 166 void NotificationsTest::CloseBrowserWindow(Browser* browser) { | 163 void NotificationsTest::CloseBrowserWindow(Browser* browser) { |
| 167 content::WindowedNotificationObserver observer( | 164 content::WindowedNotificationObserver observer( |
| 168 chrome::NOTIFICATION_BROWSER_CLOSED, | 165 chrome::NOTIFICATION_BROWSER_CLOSED, |
| 169 content::Source<Browser>(browser)); | 166 content::Source<Browser>(browser)); |
| 170 browser->window()->Close(); | 167 browser->window()->Close(); |
| 171 observer.Wait(); | 168 observer.Wait(); |
| 172 } | 169 } |
| 173 | 170 |
| 174 void NotificationsTest::CrashTab(Browser* browser, int index) { | 171 void NotificationsTest::CrashTab(Browser* browser, int index) { |
| 175 content::CrashTab(browser->tab_strip_model()->GetWebContentsAt(index)); | 172 content::CrashTab(browser->tab_strip_model()->GetWebContentsAt(index)); |
| 176 } | 173 } |
| 177 | 174 |
| 178 void NotificationsTest::SetDefaultPermissionSetting(ContentSetting setting) { | |
| 179 DesktopNotificationService* service = GetDesktopNotificationService(); | |
| 180 service->SetDefaultContentSetting(setting); | |
| 181 } | |
| 182 | |
| 183 void NotificationsTest::DenyOrigin(const GURL& origin) { | 175 void NotificationsTest::DenyOrigin(const GURL& origin) { |
| 184 DropOriginPreference(origin); | 176 DropOriginPreference(origin); |
| 185 GetDesktopNotificationService()->DenyPermission(origin); | 177 DesktopNotificationProfileUtil::DenyPermission(browser()->profile(), origin); |
| 186 } | 178 } |
| 187 | 179 |
| 188 void NotificationsTest::AllowOrigin(const GURL& origin) { | 180 void NotificationsTest::AllowOrigin(const GURL& origin) { |
| 189 DropOriginPreference(origin); | 181 DropOriginPreference(origin); |
| 190 GetDesktopNotificationService()->GrantPermission(origin); | 182 DesktopNotificationProfileUtil::GrantPermission(browser()->profile(), origin); |
| 191 } | 183 } |
| 192 | 184 |
| 193 void NotificationsTest::AllowAllOrigins() { | 185 void NotificationsTest::AllowAllOrigins() { |
| 194 GetDesktopNotificationService()->ResetAllOrigins(); | 186 DesktopNotificationProfileUtil::ResetAllOrigins(browser()->profile()); |
| 195 GetDesktopNotificationService()->SetDefaultContentSetting( | 187 DesktopNotificationProfileUtil::SetDefaultContentSetting(browser()->profile(), |
| 196 CONTENT_SETTING_ALLOW); | 188 CONTENT_SETTING_ALLOW); |
| 197 } | 189 } |
| 198 | 190 |
| 199 void NotificationsTest::VerifyInfoBar(const Browser* browser, int index) { | 191 void NotificationsTest::VerifyInfoBar(const Browser* browser, int index) { |
| 200 InfoBarService* infobar_service = InfoBarService::FromWebContents( | 192 InfoBarService* infobar_service = InfoBarService::FromWebContents( |
| 201 browser->tab_strip_model()->GetWebContentsAt(index)); | 193 browser->tab_strip_model()->GetWebContentsAt(index)); |
| 202 | 194 |
| 203 ASSERT_EQ(1U, infobar_service->infobar_count()); | 195 ASSERT_EQ(1U, infobar_service->infobar_count()); |
| 204 ConfirmInfoBarDelegate* confirm_infobar = | 196 ConfirmInfoBarDelegate* confirm_infobar = |
| 205 infobar_service->infobar_at(0)->delegate()->AsConfirmInfoBarDelegate(); | 197 infobar_service->infobar_at(0)->delegate()->AsConfirmInfoBarDelegate(); |
| 206 ASSERT_TRUE(confirm_infobar); | 198 ASSERT_TRUE(confirm_infobar); |
| 207 int buttons = confirm_infobar->GetButtons(); | 199 int buttons = confirm_infobar->GetButtons(); |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 318 } | 310 } |
| 319 } | 311 } |
| 320 } | 312 } |
| 321 | 313 |
| 322 return false; | 314 return false; |
| 323 } | 315 } |
| 324 | 316 |
| 325 void NotificationsTest::GetPrefsByContentSetting( | 317 void NotificationsTest::GetPrefsByContentSetting( |
| 326 ContentSetting setting, | 318 ContentSetting setting, |
| 327 ContentSettingsForOneType* settings) { | 319 ContentSettingsForOneType* settings) { |
| 328 DesktopNotificationService* service = GetDesktopNotificationService(); | 320 DesktopNotificationProfileUtil::GetNotificationsSettings( |
| 329 service->GetNotificationsSettings(settings); | 321 browser()->profile(), settings); |
| 330 for (ContentSettingsForOneType::iterator it = settings->begin(); | 322 for (ContentSettingsForOneType::iterator it = settings->begin(); |
| 331 it != settings->end(); ) { | 323 it != settings->end(); ) { |
| 332 if (it->setting != setting || it->source.compare("preference") != 0) | 324 if (it->setting != setting || it->source.compare("preference") != 0) |
| 333 it = settings->erase(it); | 325 it = settings->erase(it); |
| 334 else | 326 else |
| 335 ++it; | 327 ++it; |
| 336 } | 328 } |
| 337 } | 329 } |
| 338 | 330 |
| 339 bool NotificationsTest::CheckOriginInSetting( | 331 bool NotificationsTest::CheckOriginInSetting( |
| 340 const ContentSettingsForOneType& settings, | 332 const ContentSettingsForOneType& settings, |
| 341 const GURL& origin) { | 333 const GURL& origin) { |
| 342 ContentSettingsPattern pattern = | 334 ContentSettingsPattern pattern = |
| 343 ContentSettingsPattern::FromURLNoWildcard(origin); | 335 ContentSettingsPattern::FromURLNoWildcard(origin); |
| 344 for (ContentSettingsForOneType::const_iterator it = settings.begin(); | 336 for (ContentSettingsForOneType::const_iterator it = settings.begin(); |
| 345 it != settings.end(); ++it) { | 337 it != settings.end(); ++it) { |
| 346 if (it->primary_pattern == pattern) | 338 if (it->primary_pattern == pattern) |
| 347 return true; | 339 return true; |
| 348 } | 340 } |
| 349 return false; | 341 return false; |
| 350 } | 342 } |
| 351 | 343 |
| 352 void NotificationsTest::DropOriginPreference(const GURL& origin) { | 344 void NotificationsTest::DropOriginPreference(const GURL& origin) { |
| 353 GetDesktopNotificationService()->ClearSetting( | 345 DesktopNotificationProfileUtil::ClearSetting(browser()->profile(), |
| 354 ContentSettingsPattern::FromURLNoWildcard(origin)); | 346 ContentSettingsPattern::FromURLNoWildcard(origin)); |
| 355 } | 347 } |
| 356 | 348 |
| 357 DesktopNotificationService* NotificationsTest::GetDesktopNotificationService() { | |
| 358 Profile* profile = browser()->profile(); | |
| 359 return DesktopNotificationServiceFactory::GetForProfile(profile); | |
| 360 } | |
| 361 | |
| 362 // If this flakes, use http://crbug.com/62311 and http://crbug.com/74428. | 349 // If this flakes, use http://crbug.com/62311 and http://crbug.com/74428. |
| 363 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestUserGestureInfobar) { | 350 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestUserGestureInfobar) { |
| 364 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); | 351 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
| 365 | 352 |
| 366 ui_test_utils::NavigateToURL( | 353 ui_test_utils::NavigateToURL( |
| 367 browser(), | 354 browser(), |
| 368 embedded_test_server()->GetURL( | 355 embedded_test_server()->GetURL( |
| 369 "/notifications/notifications_request_function.html")); | 356 "/notifications/notifications_request_function.html")); |
| 370 | 357 |
| 371 // Request permission by calling request() while eval'ing an inline script; | 358 // Request permission by calling request() while eval'ing an inline script; |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 490 ASSERT_EQ(0, GetNotificationCount()); | 477 ASSERT_EQ(0, GetNotificationCount()); |
| 491 ContentSettingsForOneType settings; | 478 ContentSettingsForOneType settings; |
| 492 GetPrefsByContentSetting(CONTENT_SETTING_BLOCK, &settings); | 479 GetPrefsByContentSetting(CONTENT_SETTING_BLOCK, &settings); |
| 493 EXPECT_EQ(0U, settings.size()); | 480 EXPECT_EQ(0U, settings.size()); |
| 494 } | 481 } |
| 495 | 482 |
| 496 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestAllowNotificationsFromAllSites) { | 483 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestAllowNotificationsFromAllSites) { |
| 497 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); | 484 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
| 498 | 485 |
| 499 // Verify that all domains can be allowed to show notifications. | 486 // Verify that all domains can be allowed to show notifications. |
| 500 SetDefaultPermissionSetting(CONTENT_SETTING_ALLOW); | 487 DesktopNotificationProfileUtil::SetDefaultContentSetting(browser()->profile(), |
| 488 CONTENT_SETTING_ALLOW); |
| 501 ui_test_utils::NavigateToURL(browser(), GetTestPageURL()); | 489 ui_test_utils::NavigateToURL(browser(), GetTestPageURL()); |
| 502 | 490 |
| 503 std::string result = CreateSimpleNotification(browser(), true); | 491 std::string result = CreateSimpleNotification(browser(), true); |
| 504 EXPECT_NE("-1", result); | 492 EXPECT_NE("-1", result); |
| 505 | 493 |
| 506 ASSERT_EQ(1, GetNotificationCount()); | 494 ASSERT_EQ(1, GetNotificationCount()); |
| 507 InfoBarService* infobar_service = InfoBarService::FromWebContents( | 495 InfoBarService* infobar_service = InfoBarService::FromWebContents( |
| 508 browser()->tab_strip_model()->GetWebContentsAt(0)); | 496 browser()->tab_strip_model()->GetWebContentsAt(0)); |
| 509 EXPECT_EQ(0U, infobar_service->infobar_count()); | 497 EXPECT_EQ(0U, infobar_service->infobar_count()); |
| 510 } | 498 } |
| 511 | 499 |
| 512 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestDenyNotificationsFromAllSites) { | 500 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestDenyNotificationsFromAllSites) { |
| 513 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); | 501 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
| 514 | 502 |
| 515 // Verify that no domain can show notifications. | 503 // Verify that no domain can show notifications. |
| 516 SetDefaultPermissionSetting(CONTENT_SETTING_BLOCK); | 504 DesktopNotificationProfileUtil::SetDefaultContentSetting( |
| 505 browser()->profile(), CONTENT_SETTING_BLOCK); |
| 517 ui_test_utils::NavigateToURL(browser(), GetTestPageURL()); | 506 ui_test_utils::NavigateToURL(browser(), GetTestPageURL()); |
| 518 | 507 |
| 519 std::string result = CreateSimpleNotification(browser(), false); | 508 std::string result = CreateSimpleNotification(browser(), false); |
| 520 EXPECT_EQ("-1", result); | 509 EXPECT_EQ("-1", result); |
| 521 | 510 |
| 522 ASSERT_EQ(0, GetNotificationCount()); | 511 ASSERT_EQ(0, GetNotificationCount()); |
| 523 } | 512 } |
| 524 | 513 |
| 525 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestDenyDomainAndAllowAll) { | 514 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestDenyDomainAndAllowAll) { |
| 526 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); | 515 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
| 527 | 516 |
| 528 // Verify that denying a domain and allowing all shouldn't show | 517 // Verify that denying a domain and allowing all shouldn't show |
| 529 // notifications from the denied domain. | 518 // notifications from the denied domain. |
| 530 DenyOrigin(GetTestPageURL().GetOrigin()); | 519 DenyOrigin(GetTestPageURL().GetOrigin()); |
| 531 SetDefaultPermissionSetting(CONTENT_SETTING_ALLOW); | 520 DesktopNotificationProfileUtil::SetDefaultContentSetting( |
| 521 browser()->profile(), CONTENT_SETTING_ALLOW); |
| 532 | 522 |
| 533 ui_test_utils::NavigateToURL(browser(), GetTestPageURL()); | 523 ui_test_utils::NavigateToURL(browser(), GetTestPageURL()); |
| 534 | 524 |
| 535 std::string result = CreateSimpleNotification(browser(), false); | 525 std::string result = CreateSimpleNotification(browser(), false); |
| 536 EXPECT_EQ("-1", result); | 526 EXPECT_EQ("-1", result); |
| 537 | 527 |
| 538 ASSERT_EQ(0, GetNotificationCount()); | 528 ASSERT_EQ(0, GetNotificationCount()); |
| 539 } | 529 } |
| 540 | 530 |
| 541 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestAllowDomainAndDenyAll) { | 531 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestAllowDomainAndDenyAll) { |
| 542 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); | 532 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
| 543 | 533 |
| 544 // Verify that allowing a domain and denying all others should show | 534 // Verify that allowing a domain and denying all others should show |
| 545 // notifications from the allowed domain. | 535 // notifications from the allowed domain. |
| 546 AllowOrigin(GetTestPageURL().GetOrigin()); | 536 AllowOrigin(GetTestPageURL().GetOrigin()); |
| 547 SetDefaultPermissionSetting(CONTENT_SETTING_BLOCK); | 537 DesktopNotificationProfileUtil::SetDefaultContentSetting( |
| 538 browser()->profile(), CONTENT_SETTING_BLOCK); |
| 548 | 539 |
| 549 ui_test_utils::NavigateToURL(browser(), GetTestPageURL()); | 540 ui_test_utils::NavigateToURL(browser(), GetTestPageURL()); |
| 550 | 541 |
| 551 std::string result = CreateSimpleNotification(browser(), true); | 542 std::string result = CreateSimpleNotification(browser(), true); |
| 552 EXPECT_NE("-1", result); | 543 EXPECT_NE("-1", result); |
| 553 | 544 |
| 554 ASSERT_EQ(1, GetNotificationCount()); | 545 ASSERT_EQ(1, GetNotificationCount()); |
| 555 } | 546 } |
| 556 | 547 |
| 557 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestDenyAndThenAllowDomain) { | 548 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestDenyAndThenAllowDomain) { |
| (...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 771 browser(), false, "no_such_file.png", "Title2", "Body2", "chat"); | 762 browser(), false, "no_such_file.png", "Title2", "Body2", "chat"); |
| 772 EXPECT_NE("-1", result); | 763 EXPECT_NE("-1", result); |
| 773 | 764 |
| 774 ASSERT_EQ(1, GetNotificationCount()); | 765 ASSERT_EQ(1, GetNotificationCount()); |
| 775 message_center::NotificationList::Notifications notifications = | 766 message_center::NotificationList::Notifications notifications = |
| 776 message_center::MessageCenter::Get()->GetVisibleNotifications(); | 767 message_center::MessageCenter::Get()->GetVisibleNotifications(); |
| 777 EXPECT_EQ(base::ASCIIToUTF16("Title2"), (*notifications.rbegin())->title()); | 768 EXPECT_EQ(base::ASCIIToUTF16("Title2"), (*notifications.rbegin())->title()); |
| 778 EXPECT_EQ(base::ASCIIToUTF16("Body2"), | 769 EXPECT_EQ(base::ASCIIToUTF16("Body2"), |
| 779 (*notifications.rbegin())->message()); | 770 (*notifications.rbegin())->message()); |
| 780 } | 771 } |
| OLD | NEW |