| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 <stddef.h> | 5 #include <stddef.h> |
| 6 #include <stdint.h> | 6 #include <stdint.h> |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 #include "chrome/common/chrome_switches.h" | 37 #include "chrome/common/chrome_switches.h" |
| 38 #include "chrome/common/features.h" | 38 #include "chrome/common/features.h" |
| 39 #include "chrome/test/base/in_process_browser_test.h" | 39 #include "chrome/test/base/in_process_browser_test.h" |
| 40 #include "chrome/test/base/ui_test_utils.h" | 40 #include "chrome/test/base/ui_test_utils.h" |
| 41 #include "components/content_settings/core/browser/host_content_settings_map.h" | 41 #include "components/content_settings/core/browser/host_content_settings_map.h" |
| 42 #include "components/content_settings/core/common/content_settings.h" | 42 #include "components/content_settings/core/common/content_settings.h" |
| 43 #include "components/content_settings/core/common/content_settings_types.h" | 43 #include "components/content_settings/core/common/content_settings_types.h" |
| 44 #include "components/gcm_driver/common/gcm_messages.h" | 44 #include "components/gcm_driver/common/gcm_messages.h" |
| 45 #include "components/gcm_driver/gcm_client.h" | 45 #include "components/gcm_driver/gcm_client.h" |
| 46 #include "content/public/browser/web_contents.h" | 46 #include "content/public/browser/web_contents.h" |
| 47 #include "content/public/common/content_switches.h" | |
| 48 #include "content/public/test/browser_test_utils.h" | 47 #include "content/public/test/browser_test_utils.h" |
| 49 #include "content/public/test/test_utils.h" | 48 #include "content/public/test/test_utils.h" |
| 50 #include "net/test/embedded_test_server/embedded_test_server.h" | 49 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 51 #include "ui/base/window_open_disposition.h" | 50 #include "ui/base/window_open_disposition.h" |
| 52 | 51 |
| 53 #if BUILDFLAG(ENABLE_BACKGROUND) | 52 #if BUILDFLAG(ENABLE_BACKGROUND) |
| 54 #include "chrome/browser/background/background_mode_manager.h" | 53 #include "chrome/browser/background/background_mode_manager.h" |
| 55 #endif | 54 #endif |
| 56 | 55 |
| 57 namespace { | 56 namespace { |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 | 92 |
| 94 #if defined(ENABLE_NOTIFICATIONS) | 93 #if defined(ENABLE_NOTIFICATIONS) |
| 95 notification_manager_.reset(new StubNotificationUIManager); | 94 notification_manager_.reset(new StubNotificationUIManager); |
| 96 notification_service()->SetNotificationUIManagerForTesting( | 95 notification_service()->SetNotificationUIManagerForTesting( |
| 97 notification_manager()); | 96 notification_manager()); |
| 98 #endif | 97 #endif |
| 99 | 98 |
| 100 InProcessBrowserTest::SetUp(); | 99 InProcessBrowserTest::SetUp(); |
| 101 } | 100 } |
| 102 | 101 |
| 103 void SetUpCommandLine(base::CommandLine* command_line) override { | |
| 104 // Enable experiemntal features for subscription restrictions. | |
| 105 command_line->AppendSwitch( | |
| 106 switches::kEnableExperimentalWebPlatformFeatures); | |
| 107 InProcessBrowserTest::SetUpCommandLine(command_line); | |
| 108 } | |
| 109 | |
| 110 // InProcessBrowserTest: | 102 // InProcessBrowserTest: |
| 111 void SetUpOnMainThread() override { | 103 void SetUpOnMainThread() override { |
| 112 gcm_service_ = static_cast<gcm::FakeGCMProfileService*>( | 104 gcm_service_ = static_cast<gcm::FakeGCMProfileService*>( |
| 113 gcm::GCMProfileServiceFactory::GetInstance()->SetTestingFactoryAndUse( | 105 gcm::GCMProfileServiceFactory::GetInstance()->SetTestingFactoryAndUse( |
| 114 GetBrowser()->profile(), &gcm::FakeGCMProfileService::Build)); | 106 GetBrowser()->profile(), &gcm::FakeGCMProfileService::Build)); |
| 115 gcm_service_->set_collect(true); | 107 gcm_service_->set_collect(true); |
| 116 push_service_ = | 108 push_service_ = |
| 117 PushMessagingServiceFactory::GetForProfile(GetBrowser()->profile()); | 109 PushMessagingServiceFactory::GetForProfile(GetBrowser()->profile()); |
| 118 | 110 |
| 119 LoadTestPage(); | 111 LoadTestPage(); |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 | 157 |
| 166 PermissionBubbleManager* GetPermissionBubbleManager() { | 158 PermissionBubbleManager* GetPermissionBubbleManager() { |
| 167 return PermissionBubbleManager::FromWebContents( | 159 return PermissionBubbleManager::FromWebContents( |
| 168 GetBrowser()->tab_strip_model()->GetActiveWebContents()); | 160 GetBrowser()->tab_strip_model()->GetActiveWebContents()); |
| 169 } | 161 } |
| 170 | 162 |
| 171 void RequestAndAcceptPermission(); | 163 void RequestAndAcceptPermission(); |
| 172 void RequestAndDenyPermission(); | 164 void RequestAndDenyPermission(); |
| 173 | 165 |
| 174 void TryToSubscribeSuccessfully( | 166 void TryToSubscribeSuccessfully( |
| 175 const std::string& expected_push_subscription_info, | 167 const std::string& expected_push_subscription_id); |
| 176 bool use_key = true); | |
| 177 | 168 |
| 178 std::string GetEndpointForSubscriptionId(const std::string& subscription_id) { | 169 std::string GetEndpointForSubscriptionId(const std::string& subscription_id) { |
| 179 return std::string(kPushMessagingEndpoint) + "/" + subscription_id; | 170 return std::string(kPushMessagingEndpoint) + "/" + subscription_id; |
| 180 } | 171 } |
| 181 | 172 |
| 182 PushMessagingAppIdentifier GetAppIdentifierForServiceWorkerRegistration( | 173 PushMessagingAppIdentifier GetAppIdentifierForServiceWorkerRegistration( |
| 183 int64_t service_worker_registration_id); | 174 int64_t service_worker_registration_id); |
| 184 | 175 |
| 185 void SendMessageAndWaitUntilHandled( | 176 void SendMessageAndWaitUntilHandled( |
| 186 const PushMessagingAppIdentifier& app_identifier, | 177 const PushMessagingAppIdentifier& app_identifier, |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 248 | 239 |
| 249 void PushMessagingBrowserTest::RequestAndDenyPermission() { | 240 void PushMessagingBrowserTest::RequestAndDenyPermission() { |
| 250 std::string script_result; | 241 std::string script_result; |
| 251 GetPermissionBubbleManager()->set_auto_response_for_test( | 242 GetPermissionBubbleManager()->set_auto_response_for_test( |
| 252 PermissionBubbleManager::DENY_ALL); | 243 PermissionBubbleManager::DENY_ALL); |
| 253 EXPECT_TRUE(RunScript("requestNotificationPermission();", &script_result)); | 244 EXPECT_TRUE(RunScript("requestNotificationPermission();", &script_result)); |
| 254 EXPECT_EQ("permission status - denied", script_result); | 245 EXPECT_EQ("permission status - denied", script_result); |
| 255 } | 246 } |
| 256 | 247 |
| 257 void PushMessagingBrowserTest::TryToSubscribeSuccessfully( | 248 void PushMessagingBrowserTest::TryToSubscribeSuccessfully( |
| 258 const std::string& expected_push_subscription_info, | 249 const std::string& expected_push_subscription_id) { |
| 259 bool use_key) { | |
| 260 std::string script_result; | 250 std::string script_result; |
| 261 | 251 |
| 262 EXPECT_TRUE(RunScript("registerServiceWorker()", &script_result)); | 252 EXPECT_TRUE(RunScript("registerServiceWorker()", &script_result)); |
| 263 EXPECT_EQ("ok - service worker registered", script_result); | 253 EXPECT_EQ("ok - service worker registered", script_result); |
| 264 | 254 |
| 265 RequestAndAcceptPermission(); | 255 RequestAndAcceptPermission(); |
| 266 | 256 |
| 267 if (use_key) { | 257 EXPECT_TRUE(RunScript("subscribePush()", &script_result)); |
| 268 EXPECT_TRUE(RunScript("subscribePush()", &script_result)); | 258 EXPECT_EQ(GetEndpointForSubscriptionId(expected_push_subscription_id), |
| 269 } else { | |
| 270 // Test backwards compatibility with old ID based subscriptions. | |
| 271 EXPECT_TRUE(RunScript("subscribePushWithoutKey()", &script_result)); | |
| 272 } | |
| 273 | |
| 274 EXPECT_EQ(GetEndpointForSubscriptionId(expected_push_subscription_info), | |
| 275 script_result); | 259 script_result); |
| 276 } | 260 } |
| 277 | 261 |
| 278 PushMessagingAppIdentifier | 262 PushMessagingAppIdentifier |
| 279 PushMessagingBrowserTest::GetAppIdentifierForServiceWorkerRegistration( | 263 PushMessagingBrowserTest::GetAppIdentifierForServiceWorkerRegistration( |
| 280 int64_t service_worker_registration_id) { | 264 int64_t service_worker_registration_id) { |
| 281 GURL origin = https_server()->GetURL("/").GetOrigin(); | 265 GURL origin = https_server()->GetURL("/").GetOrigin(); |
| 282 PushMessagingAppIdentifier app_identifier = | 266 PushMessagingAppIdentifier app_identifier = |
| 283 PushMessagingAppIdentifier::FindByServiceWorker( | 267 PushMessagingAppIdentifier::FindByServiceWorker( |
| 284 GetBrowser()->profile(), origin, service_worker_registration_id); | 268 GetBrowser()->profile(), origin, service_worker_registration_id); |
| 285 EXPECT_FALSE(app_identifier.is_null()); | 269 EXPECT_FALSE(app_identifier.is_null()); |
| 286 return app_identifier; | 270 return app_identifier; |
| 287 } | 271 } |
| 288 | 272 |
| 289 void PushMessagingBrowserTest::SendMessageAndWaitUntilHandled( | 273 void PushMessagingBrowserTest::SendMessageAndWaitUntilHandled( |
| 290 const PushMessagingAppIdentifier& app_identifier, | 274 const PushMessagingAppIdentifier& app_identifier, |
| 291 const gcm::IncomingMessage& message) { | 275 const gcm::IncomingMessage& message) { |
| 292 base::RunLoop run_loop; | 276 base::RunLoop run_loop; |
| 293 push_service()->SetMessageCallbackForTesting(run_loop.QuitClosure()); | 277 push_service()->SetMessageCallbackForTesting(run_loop.QuitClosure()); |
| 294 push_service()->OnMessage(app_identifier.app_id(), message); | 278 push_service()->OnMessage(app_identifier.app_id(), message); |
| 295 run_loop.Run(); | 279 run_loop.Run(); |
| 296 } | 280 } |
| 297 | 281 |
| 298 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, | 282 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, |
| 299 SubscribeWithoutKeySuccessNotificationsGranted) { | 283 SubscribeSuccessNotificationsGranted) { |
| 300 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */, false); | 284 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */); |
| 301 | 285 |
| 302 PushMessagingAppIdentifier app_identifier = | 286 PushMessagingAppIdentifier app_identifier = |
| 303 GetAppIdentifierForServiceWorkerRegistration(0LL); | 287 GetAppIdentifierForServiceWorkerRegistration(0LL); |
| 304 EXPECT_EQ(app_identifier.app_id(), gcm_service()->last_registered_app_id()); | 288 EXPECT_EQ(app_identifier.app_id(), gcm_service()->last_registered_app_id()); |
| 305 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]); | 289 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]); |
| 306 } | 290 } |
| 307 | 291 |
| 308 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, | 292 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, |
| 309 SubscribeSuccessNotificationsGranted) { | |
| 310 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */); | |
| 311 | |
| 312 PushMessagingAppIdentifier app_identifier = | |
| 313 GetAppIdentifierForServiceWorkerRegistration(0LL); | |
| 314 EXPECT_EQ(app_identifier.app_id(), gcm_service()->last_registered_app_id()); | |
| 315 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]); | |
| 316 } | |
| 317 | |
| 318 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, | |
| 319 SubscribeSuccessNotificationsPrompt) { | 293 SubscribeSuccessNotificationsPrompt) { |
| 320 std::string script_result; | 294 std::string script_result; |
| 321 | 295 |
| 322 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); | 296 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); |
| 323 ASSERT_EQ("ok - service worker registered", script_result); | 297 ASSERT_EQ("ok - service worker registered", script_result); |
| 324 | 298 |
| 325 GetPermissionBubbleManager()->set_auto_response_for_test( | 299 GetPermissionBubbleManager()->set_auto_response_for_test( |
| 326 PermissionBubbleManager::ACCEPT_ALL); | 300 PermissionBubbleManager::ACCEPT_ALL); |
| 327 ASSERT_TRUE(RunScript("subscribePush()", &script_result)); | 301 ASSERT_TRUE(RunScript("subscribePush()", &script_result)); |
| 328 EXPECT_EQ(GetEndpointForSubscriptionId("1-0"), script_result); | 302 EXPECT_EQ(GetEndpointForSubscriptionId("1-0"), script_result); |
| 329 | 303 |
| 330 PushMessagingAppIdentifier app_identifier = | 304 PushMessagingAppIdentifier app_identifier = |
| 331 GetAppIdentifierForServiceWorkerRegistration(0LL); | 305 GetAppIdentifierForServiceWorkerRegistration(0LL); |
| 332 EXPECT_EQ(app_identifier.app_id(), gcm_service()->last_registered_app_id()); | 306 EXPECT_EQ(app_identifier.app_id(), gcm_service()->last_registered_app_id()); |
| 333 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]); | 307 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]); |
| 334 } | 308 } |
| 335 | 309 |
| 336 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, SubscribeFailureBadKey) { | |
| 337 std::string script_result; | |
| 338 | |
| 339 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); | |
| 340 ASSERT_EQ("ok - service worker registered", script_result); | |
| 341 | |
| 342 RequestAndAcceptPermission(); | |
| 343 | |
| 344 ASSERT_TRUE(RunScript("subscribePushBadKey()", &script_result)); | |
| 345 EXPECT_EQ( | |
| 346 "InvalidAccessError - Failed to execute 'subscribe' on 'PushManager': " | |
| 347 "The provided applicationServerKey is not valid.", | |
| 348 script_result); | |
| 349 } | |
| 350 | |
| 351 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, | 310 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, |
| 352 SubscribeFailureNotificationsBlocked) { | 311 SubscribeFailureNotificationsBlocked) { |
| 353 std::string script_result; | 312 std::string script_result; |
| 354 | 313 |
| 355 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); | 314 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); |
| 356 ASSERT_EQ("ok - service worker registered", script_result); | 315 ASSERT_EQ("ok - service worker registered", script_result); |
| 357 | 316 |
| 358 RequestAndDenyPermission(); | 317 RequestAndDenyPermission(); |
| 359 | 318 |
| 360 ASSERT_TRUE(RunScript("subscribePush()", &script_result)); | 319 ASSERT_TRUE(RunScript("subscribePush()", &script_result)); |
| (...skipping 955 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1316 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive()); | 1275 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive()); |
| 1317 | 1276 |
| 1318 // After dropping the last subscription background mode is still inactive. | 1277 // After dropping the last subscription background mode is still inactive. |
| 1319 std::string script_result; | 1278 std::string script_result; |
| 1320 gcm_service()->AddExpectedUnregisterResponse(gcm::GCMClient::SUCCESS); | 1279 gcm_service()->AddExpectedUnregisterResponse(gcm::GCMClient::SUCCESS); |
| 1321 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result)); | 1280 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result)); |
| 1322 EXPECT_EQ("unsubscribe result: true", script_result); | 1281 EXPECT_EQ("unsubscribe result: true", script_result); |
| 1323 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive()); | 1282 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive()); |
| 1324 } | 1283 } |
| 1325 #endif // BUILDFLAG(ENABLE_BACKGROUND) && !defined(OS_CHROMEOS) | 1284 #endif // BUILDFLAG(ENABLE_BACKGROUND) && !defined(OS_CHROMEOS) |
| OLD | NEW |