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

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: Fix module export build issue 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
« no previous file with comments | « no previous file | chrome/browser/push_messaging/push_messaging_service_impl.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 26 matching lines...) Expand all
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"
47 #include "content/public/test/browser_test_utils.h" 48 #include "content/public/test/browser_test_utils.h"
48 #include "content/public/test/test_utils.h" 49 #include "content/public/test/test_utils.h"
49 #include "net/test/embedded_test_server/embedded_test_server.h" 50 #include "net/test/embedded_test_server/embedded_test_server.h"
50 #include "ui/base/window_open_disposition.h" 51 #include "ui/base/window_open_disposition.h"
51 52
52 #if BUILDFLAG(ENABLE_BACKGROUND) 53 #if BUILDFLAG(ENABLE_BACKGROUND)
53 #include "chrome/browser/background/background_mode_manager.h" 54 #include "chrome/browser/background/background_mode_manager.h"
54 #endif 55 #endif
55 56
56 namespace { 57 namespace {
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
92 93
93 #if defined(ENABLE_NOTIFICATIONS) 94 #if defined(ENABLE_NOTIFICATIONS)
94 notification_manager_.reset(new StubNotificationUIManager); 95 notification_manager_.reset(new StubNotificationUIManager);
95 notification_service()->SetNotificationUIManagerForTesting( 96 notification_service()->SetNotificationUIManagerForTesting(
96 notification_manager()); 97 notification_manager());
97 #endif 98 #endif
98 99
99 InProcessBrowserTest::SetUp(); 100 InProcessBrowserTest::SetUp();
100 } 101 }
101 102
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
102 // InProcessBrowserTest: 110 // InProcessBrowserTest:
103 void SetUpOnMainThread() override { 111 void SetUpOnMainThread() override {
104 gcm_service_ = static_cast<gcm::FakeGCMProfileService*>( 112 gcm_service_ = static_cast<gcm::FakeGCMProfileService*>(
105 gcm::GCMProfileServiceFactory::GetInstance()->SetTestingFactoryAndUse( 113 gcm::GCMProfileServiceFactory::GetInstance()->SetTestingFactoryAndUse(
106 GetBrowser()->profile(), &gcm::FakeGCMProfileService::Build)); 114 GetBrowser()->profile(), &gcm::FakeGCMProfileService::Build));
107 gcm_service_->set_collect(true); 115 gcm_service_->set_collect(true);
108 push_service_ = 116 push_service_ =
109 PushMessagingServiceFactory::GetForProfile(GetBrowser()->profile()); 117 PushMessagingServiceFactory::GetForProfile(GetBrowser()->profile());
110 118
111 LoadTestPage(); 119 LoadTestPage();
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
157 165
158 PermissionBubbleManager* GetPermissionBubbleManager() { 166 PermissionBubbleManager* GetPermissionBubbleManager() {
159 return PermissionBubbleManager::FromWebContents( 167 return PermissionBubbleManager::FromWebContents(
160 GetBrowser()->tab_strip_model()->GetActiveWebContents()); 168 GetBrowser()->tab_strip_model()->GetActiveWebContents());
161 } 169 }
162 170
163 void RequestAndAcceptPermission(); 171 void RequestAndAcceptPermission();
164 void RequestAndDenyPermission(); 172 void RequestAndDenyPermission();
165 173
166 void TryToSubscribeSuccessfully( 174 void TryToSubscribeSuccessfully(
167 const std::string& expected_push_subscription_id); 175 const std::string& expected_push_subscription_info,
176 bool use_key = true);
168 177
169 std::string GetEndpointForSubscriptionId(const std::string& subscription_id) { 178 std::string GetEndpointForSubscriptionId(const std::string& subscription_id) {
170 return std::string(kPushMessagingEndpoint) + "/" + subscription_id; 179 return std::string(kPushMessagingEndpoint) + "/" + subscription_id;
171 } 180 }
172 181
173 PushMessagingAppIdentifier GetAppIdentifierForServiceWorkerRegistration( 182 PushMessagingAppIdentifier GetAppIdentifierForServiceWorkerRegistration(
174 int64_t service_worker_registration_id); 183 int64_t service_worker_registration_id);
175 184
176 void SendMessageAndWaitUntilHandled( 185 void SendMessageAndWaitUntilHandled(
177 const PushMessagingAppIdentifier& app_identifier, 186 const PushMessagingAppIdentifier& app_identifier,
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
239 248
240 void PushMessagingBrowserTest::RequestAndDenyPermission() { 249 void PushMessagingBrowserTest::RequestAndDenyPermission() {
241 std::string script_result; 250 std::string script_result;
242 GetPermissionBubbleManager()->set_auto_response_for_test( 251 GetPermissionBubbleManager()->set_auto_response_for_test(
243 PermissionBubbleManager::DENY_ALL); 252 PermissionBubbleManager::DENY_ALL);
244 EXPECT_TRUE(RunScript("requestNotificationPermission();", &script_result)); 253 EXPECT_TRUE(RunScript("requestNotificationPermission();", &script_result));
245 EXPECT_EQ("permission status - denied", script_result); 254 EXPECT_EQ("permission status - denied", script_result);
246 } 255 }
247 256
248 void PushMessagingBrowserTest::TryToSubscribeSuccessfully( 257 void PushMessagingBrowserTest::TryToSubscribeSuccessfully(
249 const std::string& expected_push_subscription_id) { 258 const std::string& expected_push_subscription_info,
259 bool use_key) {
250 std::string script_result; 260 std::string script_result;
251 261
252 EXPECT_TRUE(RunScript("registerServiceWorker()", &script_result)); 262 EXPECT_TRUE(RunScript("registerServiceWorker()", &script_result));
253 EXPECT_EQ("ok - service worker registered", script_result); 263 EXPECT_EQ("ok - service worker registered", script_result);
254 264
255 RequestAndAcceptPermission(); 265 RequestAndAcceptPermission();
256 266
257 EXPECT_TRUE(RunScript("subscribePush()", &script_result)); 267 if (use_key) {
258 EXPECT_EQ(GetEndpointForSubscriptionId(expected_push_subscription_id), 268 EXPECT_TRUE(RunScript("subscribePush()", &script_result));
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),
259 script_result); 275 script_result);
260 } 276 }
261 277
262 PushMessagingAppIdentifier 278 PushMessagingAppIdentifier
263 PushMessagingBrowserTest::GetAppIdentifierForServiceWorkerRegistration( 279 PushMessagingBrowserTest::GetAppIdentifierForServiceWorkerRegistration(
264 int64_t service_worker_registration_id) { 280 int64_t service_worker_registration_id) {
265 GURL origin = https_server()->GetURL("/").GetOrigin(); 281 GURL origin = https_server()->GetURL("/").GetOrigin();
266 PushMessagingAppIdentifier app_identifier = 282 PushMessagingAppIdentifier app_identifier =
267 PushMessagingAppIdentifier::FindByServiceWorker( 283 PushMessagingAppIdentifier::FindByServiceWorker(
268 GetBrowser()->profile(), origin, service_worker_registration_id); 284 GetBrowser()->profile(), origin, service_worker_registration_id);
269 EXPECT_FALSE(app_identifier.is_null()); 285 EXPECT_FALSE(app_identifier.is_null());
270 return app_identifier; 286 return app_identifier;
271 } 287 }
272 288
273 void PushMessagingBrowserTest::SendMessageAndWaitUntilHandled( 289 void PushMessagingBrowserTest::SendMessageAndWaitUntilHandled(
274 const PushMessagingAppIdentifier& app_identifier, 290 const PushMessagingAppIdentifier& app_identifier,
275 const gcm::IncomingMessage& message) { 291 const gcm::IncomingMessage& message) {
276 base::RunLoop run_loop; 292 base::RunLoop run_loop;
277 push_service()->SetMessageCallbackForTesting(run_loop.QuitClosure()); 293 push_service()->SetMessageCallbackForTesting(run_loop.QuitClosure());
278 push_service()->OnMessage(app_identifier.app_id(), message); 294 push_service()->OnMessage(app_identifier.app_id(), message);
279 run_loop.Run(); 295 run_loop.Run();
280 } 296 }
281 297
282 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, 298 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
299 SubscribeWithoutKeySuccessNotificationsGranted) {
300 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */, false);
301
302 PushMessagingAppIdentifier app_identifier =
303 GetAppIdentifierForServiceWorkerRegistration(0LL);
304 EXPECT_EQ(app_identifier.app_id(), gcm_service()->last_registered_app_id());
305 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]);
306 }
307
308 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
283 SubscribeSuccessNotificationsGranted) { 309 SubscribeSuccessNotificationsGranted) {
284 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */); 310 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */);
285 311
286 PushMessagingAppIdentifier app_identifier = 312 PushMessagingAppIdentifier app_identifier =
287 GetAppIdentifierForServiceWorkerRegistration(0LL); 313 GetAppIdentifierForServiceWorkerRegistration(0LL);
288 EXPECT_EQ(app_identifier.app_id(), gcm_service()->last_registered_app_id()); 314 EXPECT_EQ(app_identifier.app_id(), gcm_service()->last_registered_app_id());
289 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]); 315 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]);
290 } 316 }
291 317
292 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, 318 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
293 SubscribeSuccessNotificationsPrompt) { 319 SubscribeSuccessNotificationsPrompt) {
294 std::string script_result; 320 std::string script_result;
295 321
296 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); 322 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
297 ASSERT_EQ("ok - service worker registered", script_result); 323 ASSERT_EQ("ok - service worker registered", script_result);
298 324
299 GetPermissionBubbleManager()->set_auto_response_for_test( 325 GetPermissionBubbleManager()->set_auto_response_for_test(
300 PermissionBubbleManager::ACCEPT_ALL); 326 PermissionBubbleManager::ACCEPT_ALL);
301 ASSERT_TRUE(RunScript("subscribePush()", &script_result)); 327 ASSERT_TRUE(RunScript("subscribePush()", &script_result));
302 EXPECT_EQ(GetEndpointForSubscriptionId("1-0"), script_result); 328 EXPECT_EQ(GetEndpointForSubscriptionId("1-0"), script_result);
303 329
304 PushMessagingAppIdentifier app_identifier = 330 PushMessagingAppIdentifier app_identifier =
305 GetAppIdentifierForServiceWorkerRegistration(0LL); 331 GetAppIdentifierForServiceWorkerRegistration(0LL);
306 EXPECT_EQ(app_identifier.app_id(), gcm_service()->last_registered_app_id()); 332 EXPECT_EQ(app_identifier.app_id(), gcm_service()->last_registered_app_id());
307 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]); 333 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]);
308 } 334 }
309 335
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
310 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, 351 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
311 SubscribeFailureNotificationsBlocked) { 352 SubscribeFailureNotificationsBlocked) {
312 std::string script_result; 353 std::string script_result;
313 354
314 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); 355 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
315 ASSERT_EQ("ok - service worker registered", script_result); 356 ASSERT_EQ("ok - service worker registered", script_result);
316 357
317 RequestAndDenyPermission(); 358 RequestAndDenyPermission();
318 359
319 ASSERT_TRUE(RunScript("subscribePush()", &script_result)); 360 ASSERT_TRUE(RunScript("subscribePush()", &script_result));
(...skipping 955 matching lines...) Expand 10 before | Expand all | Expand 10 after
1275 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive()); 1316 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive());
1276 1317
1277 // After dropping the last subscription background mode is still inactive. 1318 // After dropping the last subscription background mode is still inactive.
1278 std::string script_result; 1319 std::string script_result;
1279 gcm_service()->AddExpectedUnregisterResponse(gcm::GCMClient::SUCCESS); 1320 gcm_service()->AddExpectedUnregisterResponse(gcm::GCMClient::SUCCESS);
1280 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result)); 1321 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
1281 EXPECT_EQ("unsubscribe result: true", script_result); 1322 EXPECT_EQ("unsubscribe result: true", script_result);
1282 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive()); 1323 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive());
1283 } 1324 }
1284 #endif // BUILDFLAG(ENABLE_BACKGROUND) && !defined(OS_CHROMEOS) 1325 #endif // BUILDFLAG(ENABLE_BACKGROUND) && !defined(OS_CHROMEOS)
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/push_messaging/push_messaging_service_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698