Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1668)

Side by Side Diff: chrome/browser/push_messaging/push_messaging_browsertest.cc

Issue 1701313002: Partial implementation of subscription restrictions. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added exception failure return and updated more tests Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 25 matching lines...) Expand all
36 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h" 36 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h"
37 #include "chrome/common/chrome_switches.h" 37 #include "chrome/common/chrome_switches.h"
38 #include "chrome/test/base/in_process_browser_test.h" 38 #include "chrome/test/base/in_process_browser_test.h"
39 #include "chrome/test/base/ui_test_utils.h" 39 #include "chrome/test/base/ui_test_utils.h"
40 #include "components/content_settings/core/browser/host_content_settings_map.h" 40 #include "components/content_settings/core/browser/host_content_settings_map.h"
41 #include "components/content_settings/core/common/content_settings.h" 41 #include "components/content_settings/core/common/content_settings.h"
42 #include "components/content_settings/core/common/content_settings_types.h" 42 #include "components/content_settings/core/common/content_settings_types.h"
43 #include "components/gcm_driver/common/gcm_messages.h" 43 #include "components/gcm_driver/common/gcm_messages.h"
44 #include "components/gcm_driver/gcm_client.h" 44 #include "components/gcm_driver/gcm_client.h"
45 #include "content/public/browser/web_contents.h" 45 #include "content/public/browser/web_contents.h"
46 #include "content/public/common/content_switches.h"
46 #include "content/public/test/browser_test_utils.h" 47 #include "content/public/test/browser_test_utils.h"
47 #include "content/public/test/test_utils.h" 48 #include "content/public/test/test_utils.h"
48 #include "net/test/embedded_test_server/embedded_test_server.h" 49 #include "net/test/embedded_test_server/embedded_test_server.h"
49 #include "ui/base/window_open_disposition.h" 50 #include "ui/base/window_open_disposition.h"
50 51
51 #if defined(ENABLE_BACKGROUND) 52 #if defined(ENABLE_BACKGROUND)
52 #include "chrome/browser/background/background_mode_manager.h" 53 #include "chrome/browser/background/background_mode_manager.h"
53 #endif 54 #endif
54 55
55 namespace { 56 namespace {
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
91 92
92 #if defined(ENABLE_NOTIFICATIONS) 93 #if defined(ENABLE_NOTIFICATIONS)
93 notification_manager_.reset(new StubNotificationUIManager); 94 notification_manager_.reset(new StubNotificationUIManager);
94 notification_service()->SetNotificationUIManagerForTesting( 95 notification_service()->SetNotificationUIManagerForTesting(
95 notification_manager()); 96 notification_manager());
96 #endif 97 #endif
97 98
98 InProcessBrowserTest::SetUp(); 99 InProcessBrowserTest::SetUp();
99 } 100 }
100 101
102 void SetUpCommandLine(base::CommandLine* command_line) override {
103 command_line->AppendSwitch(
Peter Beverloo 2016/02/26 16:02:33 Please document in a comment for what feature this
harkness 2016/02/26 17:12:04 Done.
104 switches::kEnableExperimentalWebPlatformFeatures);
Peter Beverloo 2016/02/26 16:02:32 nit: InProcessBrowserTest::SetUpCommandLine(comma
harkness 2016/02/26 17:12:04 Done.
105 }
106
101 // InProcessBrowserTest: 107 // InProcessBrowserTest:
102 void SetUpOnMainThread() override { 108 void SetUpOnMainThread() override {
103 gcm_service_ = static_cast<gcm::FakeGCMProfileService*>( 109 gcm_service_ = static_cast<gcm::FakeGCMProfileService*>(
104 gcm::GCMProfileServiceFactory::GetInstance()->SetTestingFactoryAndUse( 110 gcm::GCMProfileServiceFactory::GetInstance()->SetTestingFactoryAndUse(
105 GetBrowser()->profile(), &gcm::FakeGCMProfileService::Build)); 111 GetBrowser()->profile(), &gcm::FakeGCMProfileService::Build));
106 gcm_service_->set_collect(true); 112 gcm_service_->set_collect(true);
107 push_service_ = 113 push_service_ =
108 PushMessagingServiceFactory::GetForProfile(GetBrowser()->profile()); 114 PushMessagingServiceFactory::GetForProfile(GetBrowser()->profile());
109 115
110 LoadTestPage(); 116 LoadTestPage();
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
156 162
157 PermissionBubbleManager* GetPermissionBubbleManager() { 163 PermissionBubbleManager* GetPermissionBubbleManager() {
158 return PermissionBubbleManager::FromWebContents( 164 return PermissionBubbleManager::FromWebContents(
159 GetBrowser()->tab_strip_model()->GetActiveWebContents()); 165 GetBrowser()->tab_strip_model()->GetActiveWebContents());
160 } 166 }
161 167
162 void RequestAndAcceptPermission(); 168 void RequestAndAcceptPermission();
163 void RequestAndDenyPermission(); 169 void RequestAndDenyPermission();
164 170
165 void TryToSubscribeSuccessfully( 171 void TryToSubscribeSuccessfully(
166 const std::string& expected_push_subscription_id); 172 const std::string& expected_push_subscription_info,
173 bool use_key = true);
167 174
168 std::string GetEndpointForSubscriptionId(const std::string& subscription_id) { 175 std::string GetEndpointForSubscriptionId(const std::string& subscription_id) {
169 return std::string(kPushMessagingEndpoint) + "/" + subscription_id; 176 return std::string(kPushMessagingEndpoint) + "/" + subscription_id;
170 } 177 }
171 178
172 PushMessagingAppIdentifier GetAppIdentifierForServiceWorkerRegistration( 179 PushMessagingAppIdentifier GetAppIdentifierForServiceWorkerRegistration(
173 int64_t service_worker_registration_id); 180 int64_t service_worker_registration_id);
174 181
175 void SendMessageAndWaitUntilHandled( 182 void SendMessageAndWaitUntilHandled(
176 const PushMessagingAppIdentifier& app_identifier, 183 const PushMessagingAppIdentifier& app_identifier,
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
238 245
239 void PushMessagingBrowserTest::RequestAndDenyPermission() { 246 void PushMessagingBrowserTest::RequestAndDenyPermission() {
240 std::string script_result; 247 std::string script_result;
241 GetPermissionBubbleManager()->set_auto_response_for_test( 248 GetPermissionBubbleManager()->set_auto_response_for_test(
242 PermissionBubbleManager::DENY_ALL); 249 PermissionBubbleManager::DENY_ALL);
243 EXPECT_TRUE(RunScript("requestNotificationPermission();", &script_result)); 250 EXPECT_TRUE(RunScript("requestNotificationPermission();", &script_result));
244 EXPECT_EQ("permission status - denied", script_result); 251 EXPECT_EQ("permission status - denied", script_result);
245 } 252 }
246 253
247 void PushMessagingBrowserTest::TryToSubscribeSuccessfully( 254 void PushMessagingBrowserTest::TryToSubscribeSuccessfully(
248 const std::string& expected_push_subscription_id) { 255 const std::string& expected_push_subscription_info,
256 bool use_key) {
249 std::string script_result; 257 std::string script_result;
250 258
251 EXPECT_TRUE(RunScript("registerServiceWorker()", &script_result)); 259 EXPECT_TRUE(RunScript("registerServiceWorker()", &script_result));
252 EXPECT_EQ("ok - service worker registered", script_result); 260 EXPECT_EQ("ok - service worker registered", script_result);
253 261
254 RequestAndAcceptPermission(); 262 RequestAndAcceptPermission();
255 263
256 EXPECT_TRUE(RunScript("subscribePush()", &script_result)); 264 if (use_key) {
257 EXPECT_EQ(GetEndpointForSubscriptionId(expected_push_subscription_id), 265 EXPECT_TRUE(RunScript("subscribePush()", &script_result));
266 } else {
267 // Test backwards compatibility with old ID based subscriptions.
268 EXPECT_TRUE(RunScript("subscribePushWithoutKey()", &script_result));
269 }
270
271 EXPECT_EQ(GetEndpointForSubscriptionId(expected_push_subscription_info),
258 script_result); 272 script_result);
259 } 273 }
260 274
261 PushMessagingAppIdentifier 275 PushMessagingAppIdentifier
262 PushMessagingBrowserTest::GetAppIdentifierForServiceWorkerRegistration( 276 PushMessagingBrowserTest::GetAppIdentifierForServiceWorkerRegistration(
263 int64_t service_worker_registration_id) { 277 int64_t service_worker_registration_id) {
264 GURL origin = https_server()->GetURL("/").GetOrigin(); 278 GURL origin = https_server()->GetURL("/").GetOrigin();
265 PushMessagingAppIdentifier app_identifier = 279 PushMessagingAppIdentifier app_identifier =
266 PushMessagingAppIdentifier::FindByServiceWorker( 280 PushMessagingAppIdentifier::FindByServiceWorker(
267 GetBrowser()->profile(), origin, service_worker_registration_id); 281 GetBrowser()->profile(), origin, service_worker_registration_id);
268 EXPECT_FALSE(app_identifier.is_null()); 282 EXPECT_FALSE(app_identifier.is_null());
269 return app_identifier; 283 return app_identifier;
270 } 284 }
271 285
272 void PushMessagingBrowserTest::SendMessageAndWaitUntilHandled( 286 void PushMessagingBrowserTest::SendMessageAndWaitUntilHandled(
273 const PushMessagingAppIdentifier& app_identifier, 287 const PushMessagingAppIdentifier& app_identifier,
274 const gcm::IncomingMessage& message) { 288 const gcm::IncomingMessage& message) {
275 base::RunLoop run_loop; 289 base::RunLoop run_loop;
276 push_service()->SetMessageCallbackForTesting(run_loop.QuitClosure()); 290 push_service()->SetMessageCallbackForTesting(run_loop.QuitClosure());
277 push_service()->OnMessage(app_identifier.app_id(), message); 291 push_service()->OnMessage(app_identifier.app_id(), message);
278 run_loop.Run(); 292 run_loop.Run();
279 } 293 }
280 294
281 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, 295 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
296 SubscribeWithoutKeySuccessNotificationsGranted) {
297 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */, false);
298
299 PushMessagingAppIdentifier app_identifier =
300 GetAppIdentifierForServiceWorkerRegistration(0LL);
301 EXPECT_EQ(app_identifier.app_id(), gcm_service()->last_registered_app_id());
302 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]);
303 }
304
305 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
282 SubscribeSuccessNotificationsGranted) { 306 SubscribeSuccessNotificationsGranted) {
283 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */); 307 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */);
284 308
285 PushMessagingAppIdentifier app_identifier = 309 PushMessagingAppIdentifier app_identifier =
286 GetAppIdentifierForServiceWorkerRegistration(0LL); 310 GetAppIdentifierForServiceWorkerRegistration(0LL);
287 EXPECT_EQ(app_identifier.app_id(), gcm_service()->last_registered_app_id()); 311 EXPECT_EQ(app_identifier.app_id(), gcm_service()->last_registered_app_id());
288 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]); 312 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]);
289 } 313 }
290 314
291 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, 315 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
292 SubscribeSuccessNotificationsPrompt) { 316 SubscribeSuccessNotificationsPrompt) {
293 std::string script_result; 317 std::string script_result;
294 318
295 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); 319 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
296 ASSERT_EQ("ok - service worker registered", script_result); 320 ASSERT_EQ("ok - service worker registered", script_result);
297 321
298 GetPermissionBubbleManager()->set_auto_response_for_test( 322 GetPermissionBubbleManager()->set_auto_response_for_test(
299 PermissionBubbleManager::ACCEPT_ALL); 323 PermissionBubbleManager::ACCEPT_ALL);
300 ASSERT_TRUE(RunScript("subscribePush()", &script_result)); 324 ASSERT_TRUE(RunScript("subscribePush()", &script_result));
301 EXPECT_EQ(GetEndpointForSubscriptionId("1-0"), script_result); 325 EXPECT_EQ(GetEndpointForSubscriptionId("1-0"), script_result);
302 326
303 PushMessagingAppIdentifier app_identifier = 327 PushMessagingAppIdentifier app_identifier =
304 GetAppIdentifierForServiceWorkerRegistration(0LL); 328 GetAppIdentifierForServiceWorkerRegistration(0LL);
305 EXPECT_EQ(app_identifier.app_id(), gcm_service()->last_registered_app_id()); 329 EXPECT_EQ(app_identifier.app_id(), gcm_service()->last_registered_app_id());
306 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]); 330 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]);
307 } 331 }
308 332
333 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, SubscribeFailureBadKey) {
334 std::string script_result;
335
336 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
337 ASSERT_EQ("ok - service worker registered", script_result);
338
339 RequestAndAcceptPermission();
340
341 ASSERT_TRUE(RunScript("subscribePushBadKey()", &script_result));
342 EXPECT_EQ(
343 "InvalidAccessError - Failed to execute 'subscribe' on 'PushManager': "
344 "The provided applicationServerKey is not valid.",
345 script_result);
346 }
347
309 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, 348 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
310 SubscribeFailureNotificationsBlocked) { 349 SubscribeFailureNotificationsBlocked) {
311 std::string script_result; 350 std::string script_result;
312 351
313 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); 352 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
314 ASSERT_EQ("ok - service worker registered", script_result); 353 ASSERT_EQ("ok - service worker registered", script_result);
315 354
316 RequestAndDenyPermission(); 355 RequestAndDenyPermission();
317 356
318 ASSERT_TRUE(RunScript("subscribePush()", &script_result)); 357 ASSERT_TRUE(RunScript("subscribePush()", &script_result));
(...skipping 955 matching lines...) Expand 10 before | Expand all | Expand 10 after
1274 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive()); 1313 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive());
1275 1314
1276 // After dropping the last subscription background mode is still inactive. 1315 // After dropping the last subscription background mode is still inactive.
1277 std::string script_result; 1316 std::string script_result;
1278 gcm_service()->AddExpectedUnregisterResponse(gcm::GCMClient::SUCCESS); 1317 gcm_service()->AddExpectedUnregisterResponse(gcm::GCMClient::SUCCESS);
1279 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result)); 1318 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
1280 EXPECT_EQ("unsubscribe result: true", script_result); 1319 EXPECT_EQ("unsubscribe result: true", script_result);
1281 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive()); 1320 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive());
1282 } 1321 }
1283 #endif // defined(ENABLE_BACKGROUND) && !defined(OS_CHROMEOS) 1322 #endif // defined(ENABLE_BACKGROUND) && !defined(OS_CHROMEOS)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698