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

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

Issue 1851423003: Make Web Push use InstanceID tokens instead of GCM registrations (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@iid4default
Patch Set: Rebase Created 4 years, 3 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 29 matching lines...) Expand all
40 #include "chrome/browser/ui/tabs/tab_strip_model.h" 40 #include "chrome/browser/ui/tabs/tab_strip_model.h"
41 #include "chrome/common/chrome_switches.h" 41 #include "chrome/common/chrome_switches.h"
42 #include "chrome/common/features.h" 42 #include "chrome/common/features.h"
43 #include "chrome/test/base/in_process_browser_test.h" 43 #include "chrome/test/base/in_process_browser_test.h"
44 #include "chrome/test/base/ui_test_utils.h" 44 #include "chrome/test/base/ui_test_utils.h"
45 #include "components/content_settings/core/browser/host_content_settings_map.h" 45 #include "components/content_settings/core/browser/host_content_settings_map.h"
46 #include "components/content_settings/core/common/content_settings.h" 46 #include "components/content_settings/core/common/content_settings.h"
47 #include "components/content_settings/core/common/content_settings_types.h" 47 #include "components/content_settings/core/common/content_settings_types.h"
48 #include "components/gcm_driver/common/gcm_messages.h" 48 #include "components/gcm_driver/common/gcm_messages.h"
49 #include "components/gcm_driver/gcm_client.h" 49 #include "components/gcm_driver/gcm_client.h"
50 #include "components/gcm_driver/instance_id/fake_gcm_driver_for_instance_id.h"
50 #include "content/public/browser/web_contents.h" 51 #include "content/public/browser/web_contents.h"
51 #include "content/public/common/content_switches.h" 52 #include "content/public/common/content_switches.h"
52 #include "content/public/test/browser_test_utils.h" 53 #include "content/public/test/browser_test_utils.h"
53 #include "content/public/test/test_utils.h" 54 #include "content/public/test/test_utils.h"
54 #include "net/test/embedded_test_server/embedded_test_server.h" 55 #include "net/test/embedded_test_server/embedded_test_server.h"
56 #include "testing/gmock/include/gmock/gmock.h"
55 #include "ui/base/window_open_disposition.h" 57 #include "ui/base/window_open_disposition.h"
56 58
57 #if BUILDFLAG(ENABLE_BACKGROUND) 59 #if BUILDFLAG(ENABLE_BACKGROUND)
58 #include "chrome/browser/background/background_mode_manager.h" 60 #include "chrome/browser/background/background_mode_manager.h"
59 #endif 61 #endif
60 62
61 namespace { 63 namespace {
62 64
65 const char kManifestSenderId[] = "1234567890";
66
63 // NIST P-256 public key made available to tests. Must be an uncompressed 67 // NIST P-256 public key made available to tests. Must be an uncompressed
64 // point in accordance with SEC1 2.3.3. 68 // point in accordance with SEC1 2.3.3.
65 const uint8_t kApplicationServerKey[65] = { 69 const uint8_t kApplicationServerKey[65] = {
66 0x04, 0x55, 0x52, 0x6A, 0xA5, 0x6E, 0x8E, 0xAA, 0x47, 0x97, 0x36, 70 0x04, 0x55, 0x52, 0x6A, 0xA5, 0x6E, 0x8E, 0xAA, 0x47, 0x97, 0x36,
67 0x10, 0xC1, 0x66, 0x3C, 0x1E, 0x65, 0xBF, 0xA1, 0x7B, 0xEE, 0x48, 71 0x10, 0xC1, 0x66, 0x3C, 0x1E, 0x65, 0xBF, 0xA1, 0x7B, 0xEE, 0x48,
68 0xC9, 0xC6, 0xBB, 0xBF, 0x02, 0x18, 0x53, 0x72, 0x1D, 0x0C, 0x7B, 72 0xC9, 0xC6, 0xBB, 0xBF, 0x02, 0x18, 0x53, 0x72, 0x1D, 0x0C, 0x7B,
69 0xA9, 0xE3, 0x11, 0xB7, 0x03, 0x52, 0x21, 0xD3, 0x71, 0x90, 0x13, 73 0xA9, 0xE3, 0x11, 0xB7, 0x03, 0x52, 0x21, 0xD3, 0x71, 0x90, 0x13,
70 0xA8, 0xC1, 0xCF, 0xED, 0x20, 0xF7, 0x1F, 0xD1, 0x7F, 0xF2, 0x76, 74 0xA8, 0xC1, 0xCF, 0xED, 0x20, 0xF7, 0x1F, 0xD1, 0x7F, 0xF2, 0x76,
71 0xB6, 0x01, 0x20, 0xD8, 0x35, 0xA5, 0xD9, 0x3C, 0x43, 0xFD}; 75 0xB6, 0x01, 0x20, 0xD8, 0x35, 0xA5, 0xD9, 0x3C, 0x43, 0xFD};
72 76
73 // URL-safe base64 encoded version of the |kApplicationServerKey|. 77 // URL-safe base64 encoded version of the |kApplicationServerKey|.
74 const char kEncodedApplicationServerKey[] = 78 const char kEncodedApplicationServerKey[] =
75 "BFVSaqVujqpHlzYQwWY8HmW_oXvuSMnGu78CGFNyHQx7qeMRtwNSIdNxkBOowc_tIPcf0X_ydr" 79 "BFVSaqVujqpHlzYQwWY8HmW_oXvuSMnGu78CGFNyHQx7qeMRtwNSIdNxkBOowc_tIPcf0X_ydr"
76 "YBINg1pdk8Q_0"; 80 "YBINg1pdk8Q_0";
77 81
78 std::string GetTestApplicationServerKey() { 82 std::string GetTestApplicationServerKey() {
79 return std::string(kApplicationServerKey, 83 return std::string(kApplicationServerKey,
80 kApplicationServerKey + arraysize(kApplicationServerKey)); 84 kApplicationServerKey + arraysize(kApplicationServerKey));
81 } 85 }
82 86
83 // Class to instantiate on the stack that is meant to be used with 87 void LegacyRegisterCallback(const base::Closure& done_callback,
84 // FakeGCMProfileService. The ::Run() method follows the signature of 88 std::string* out_registration_id,
85 // FakeGCMProfileService::UnregisterCallback. 89 gcm::GCMClient::Result* out_result,
86 class UnregistrationCallback { 90 const std::string& registration_id,
87 public: 91 gcm::GCMClient::Result result) {
88 UnregistrationCallback() 92 if (out_registration_id)
89 : message_loop_runner_(new content::MessageLoopRunner) {} 93 *out_registration_id = registration_id;
90 94 if (out_result)
91 void Run(const std::string& app_id) { 95 *out_result = result;
92 app_id_ = app_id; 96 done_callback.Run();
93 message_loop_runner_->Quit(); 97 }
94 }
95
96 void WaitUntilSatisfied() { message_loop_runner_->Run(); }
97
98 const std::string& app_id() { return app_id_; }
99
100 private:
101 scoped_refptr<content::MessageLoopRunner> message_loop_runner_;
102 std::string app_id_;
103 };
104 98
105 } // namespace 99 } // namespace
106 100
107 class PushMessagingBrowserTest : public InProcessBrowserTest { 101 class PushMessagingBrowserTest : public InProcessBrowserTest {
108 public: 102 public:
109 PushMessagingBrowserTest() : gcm_service_(nullptr) {} 103 PushMessagingBrowserTest() : gcm_service_(nullptr), gcm_driver_(nullptr) {}
110 ~PushMessagingBrowserTest() override {} 104 ~PushMessagingBrowserTest() override {}
111 105
112 // InProcessBrowserTest: 106 // InProcessBrowserTest:
113 void SetUp() override { 107 void SetUp() override {
114 https_server_.reset( 108 https_server_.reset(
115 new net::EmbeddedTestServer(net::EmbeddedTestServer::TYPE_HTTPS)); 109 new net::EmbeddedTestServer(net::EmbeddedTestServer::TYPE_HTTPS));
116 https_server_->ServeFilesFromSourceDirectory("chrome/test/data"); 110 https_server_->ServeFilesFromSourceDirectory("chrome/test/data");
117 ASSERT_TRUE(https_server_->Start()); 111 ASSERT_TRUE(https_server_->Start());
118 112
119 #if defined(ENABLE_NOTIFICATIONS) 113 #if defined(ENABLE_NOTIFICATIONS)
120 notification_manager_.reset(new StubNotificationUIManager); 114 notification_manager_.reset(new StubNotificationUIManager);
121 #endif 115 #endif
122 116
123 InProcessBrowserTest::SetUp(); 117 InProcessBrowserTest::SetUp();
124 } 118 }
125 119
126 void SetUpCommandLine(base::CommandLine* command_line) override { 120 void SetUpCommandLine(base::CommandLine* command_line) override {
127 // Enable experimental features for subscription restrictions. 121 // Enable experimental features for subscription restrictions.
128 command_line->AppendSwitch( 122 command_line->AppendSwitch(
129 switches::kEnableExperimentalWebPlatformFeatures); 123 switches::kEnableExperimentalWebPlatformFeatures);
130 InProcessBrowserTest::SetUpCommandLine(command_line); 124 InProcessBrowserTest::SetUpCommandLine(command_line);
131 } 125 }
132 126
133 // InProcessBrowserTest: 127 // InProcessBrowserTest:
134 void SetUpOnMainThread() override { 128 void SetUpOnMainThread() override {
135 gcm_service_ = static_cast<gcm::FakeGCMProfileService*>( 129 gcm_service_ = static_cast<gcm::FakeGCMProfileService*>(
136 gcm::GCMProfileServiceFactory::GetInstance()->SetTestingFactoryAndUse( 130 gcm::GCMProfileServiceFactory::GetInstance()->SetTestingFactoryAndUse(
137 GetBrowser()->profile(), &gcm::FakeGCMProfileService::Build)); 131 GetBrowser()->profile(), &gcm::FakeGCMProfileService::Build));
138 gcm_service_->set_collect(true); 132 gcm_driver_ = static_cast<instance_id::FakeGCMDriverForInstanceID*>(
133 gcm_service_->driver());
134
139 push_service_ = 135 push_service_ =
140 PushMessagingServiceFactory::GetForProfile(GetBrowser()->profile()); 136 PushMessagingServiceFactory::GetForProfile(GetBrowser()->profile());
141 #if defined(ENABLE_NOTIFICATIONS) 137 #if defined(ENABLE_NOTIFICATIONS)
142 display_service_.reset(new MessageCenterDisplayService( 138 display_service_.reset(new MessageCenterDisplayService(
143 GetBrowser()->profile(), notification_manager_.get())); 139 GetBrowser()->profile(), notification_manager_.get()));
144 notification_service()->SetNotificationDisplayServiceForTesting( 140 notification_service()->SetNotificationDisplayServiceForTesting(
145 display_service_.get()); 141 display_service_.get());
146 #endif 142 #endif
147 143
148 LoadTestPage(); 144 LoadTestPage();
149 InProcessBrowserTest::SetUpOnMainThread(); 145 InProcessBrowserTest::SetUpOnMainThread();
150 } 146 }
151 147
148 // Calls should be wrapped in the ASSERT_NO_FATAL_FAILURE() macro.
152 void RestartPushService() { 149 void RestartPushService() {
153 Profile* profile = GetBrowser()->profile(); 150 Profile* profile = GetBrowser()->profile();
154 PushMessagingServiceFactory::GetInstance()->SetTestingFactory(profile, 151 PushMessagingServiceFactory::GetInstance()->SetTestingFactory(profile,
155 nullptr); 152 nullptr);
156 ASSERT_EQ(nullptr, PushMessagingServiceFactory::GetForProfile(profile)); 153 ASSERT_EQ(nullptr, PushMessagingServiceFactory::GetForProfile(profile));
157 PushMessagingServiceFactory::GetInstance()->RestoreFactoryForTests(profile); 154 PushMessagingServiceFactory::GetInstance()->RestoreFactoryForTests(profile);
158 PushMessagingServiceImpl::InitializeForProfile(profile); 155 PushMessagingServiceImpl::InitializeForProfile(profile);
159 push_service_ = PushMessagingServiceFactory::GetForProfile(profile); 156 push_service_ = PushMessagingServiceFactory::GetForProfile(profile);
160 } 157 }
161 158
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
195 192
196 bool RunScript(const std::string& script, std::string* result, 193 bool RunScript(const std::string& script, std::string* result,
197 content::WebContents* web_contents) { 194 content::WebContents* web_contents) {
198 if (!web_contents) 195 if (!web_contents)
199 web_contents = GetBrowser()->tab_strip_model()->GetActiveWebContents(); 196 web_contents = GetBrowser()->tab_strip_model()->GetActiveWebContents();
200 return content::ExecuteScriptAndExtractString(web_contents->GetMainFrame(), 197 return content::ExecuteScriptAndExtractString(web_contents->GetMainFrame(),
201 script, result); 198 script, result);
202 } 199 }
203 200
204 gcm::GCMAppHandler* GetAppHandler() { 201 gcm::GCMAppHandler* GetAppHandler() {
205 return gcm_service()->driver()->GetAppHandler( 202 return gcm_driver_->GetAppHandler(kPushMessagingAppIdentifierPrefix);
206 kPushMessagingAppIdentifierPrefix);
207 } 203 }
208 204
209 PermissionRequestManager* GetPermissionRequestManager() { 205 PermissionRequestManager* GetPermissionRequestManager() {
210 return PermissionRequestManager::FromWebContents( 206 return PermissionRequestManager::FromWebContents(
211 GetBrowser()->tab_strip_model()->GetActiveWebContents()); 207 GetBrowser()->tab_strip_model()->GetActiveWebContents());
212 } 208 }
213 209
210 // Calls should be wrapped in the ASSERT_NO_FATAL_FAILURE() macro.
214 void RequestAndAcceptPermission(); 211 void RequestAndAcceptPermission();
212 // Calls should be wrapped in the ASSERT_NO_FATAL_FAILURE() macro.
215 void RequestAndDenyPermission(); 213 void RequestAndDenyPermission();
216 214
217 void TryToSubscribeSuccessfully( 215 // Sets out_token to the subscription token (not including server URL).
218 const std::string& expected_push_subscription_info, 216 // Calls should be wrapped in the ASSERT_NO_FATAL_FAILURE() macro.
219 bool use_key = true); 217 void SubscribeSuccessfully(bool use_key = true,
218 std::string* out_token = nullptr);
220 219
221 std::string GetEndpointForSubscriptionId(const std::string& subscription_id, 220 // Legacy subscribe path using GCMDriver rather than Instance IDs. Only
222 bool standard_protocol = true) { 221 // for testing that we maintain support for existing stored registrations.
223 return push_service()->GetEndpoint(standard_protocol).spec() + 222 // Calls should be wrapped in the ASSERT_NO_FATAL_FAILURE() macro.
224 subscription_id; 223 void LegacySubscribeSuccessfully(std::string* out_subscription_id = nullptr);
225 } 224
225 // Strips server URL from a registration endpoint to get subscription token.
226 // Calls should be wrapped in the ASSERT_NO_FATAL_FAILURE() macro.
227 void EndpointToToken(const std::string& endpoint,
228 bool standard_protocol = true,
229 std::string* out_token = nullptr);
226 230
227 PushMessagingAppIdentifier GetAppIdentifierForServiceWorkerRegistration( 231 PushMessagingAppIdentifier GetAppIdentifierForServiceWorkerRegistration(
228 int64_t service_worker_registration_id); 232 int64_t service_worker_registration_id);
229 233
230 void SendMessageAndWaitUntilHandled( 234 void SendMessageAndWaitUntilHandled(
231 const PushMessagingAppIdentifier& app_identifier, 235 const PushMessagingAppIdentifier& app_identifier,
232 const gcm::IncomingMessage& message); 236 const gcm::IncomingMessage& message);
233 237
234 net::EmbeddedTestServer* https_server() const { return https_server_.get(); } 238 net::EmbeddedTestServer* https_server() const { return https_server_.get(); }
235 239
236 gcm::FakeGCMProfileService* gcm_service() const { return gcm_service_; }
237
238 #if defined(ENABLE_NOTIFICATIONS) 240 #if defined(ENABLE_NOTIFICATIONS)
239 // To be called when delivery of a push message has finished. The |run_loop| 241 // To be called when delivery of a push message has finished. The |run_loop|
240 // will be told to quit after |messages_required| messages were received. 242 // will be told to quit after |messages_required| messages were received.
241 void OnDeliveryFinished(std::vector<size_t>* number_of_notifications_shown, 243 void OnDeliveryFinished(std::vector<size_t>* number_of_notifications_shown,
242 const base::Closure& done_closure) { 244 const base::Closure& done_closure) {
243 DCHECK(number_of_notifications_shown); 245 DCHECK(number_of_notifications_shown);
244 246
245 number_of_notifications_shown->push_back( 247 number_of_notifications_shown->push_back(
246 notification_manager_->GetNotificationCount()); 248 notification_manager_->GetNotificationCount());
247 249
(...skipping 15 matching lines...) Expand all
263 SiteEngagementService* service = 265 SiteEngagementService* service =
264 SiteEngagementService::Get(GetBrowser()->profile()); 266 SiteEngagementService::Get(GetBrowser()->profile());
265 service->ResetScoreForURL(url, score); 267 service->ResetScoreForURL(url, score);
266 } 268 }
267 269
268 protected: 270 protected:
269 virtual std::string GetTestURL() { return "/push_messaging/test.html"; } 271 virtual std::string GetTestURL() { return "/push_messaging/test.html"; }
270 272
271 virtual Browser* GetBrowser() const { return browser(); } 273 virtual Browser* GetBrowser() const { return browser(); }
272 274
273 private: 275 protected:
274 std::unique_ptr<net::EmbeddedTestServer> https_server_; 276 std::unique_ptr<net::EmbeddedTestServer> https_server_;
275 gcm::FakeGCMProfileService* gcm_service_; 277 gcm::FakeGCMProfileService* gcm_service_;
278 instance_id::FakeGCMDriverForInstanceID* gcm_driver_;
276 PushMessagingServiceImpl* push_service_; 279 PushMessagingServiceImpl* push_service_;
277 280
278 #if defined(ENABLE_NOTIFICATIONS) 281 #if defined(ENABLE_NOTIFICATIONS)
279 std::unique_ptr<StubNotificationUIManager> notification_manager_; 282 std::unique_ptr<StubNotificationUIManager> notification_manager_;
280 std::unique_ptr<MessageCenterDisplayService> display_service_; 283 std::unique_ptr<MessageCenterDisplayService> display_service_;
281 #endif 284 #endif
282 285
283 DISALLOW_COPY_AND_ASSIGN(PushMessagingBrowserTest); 286 DISALLOW_COPY_AND_ASSIGN(PushMessagingBrowserTest);
284 }; 287 };
285 288
286 class PushMessagingBrowserTestEmptySubscriptionOptions 289 class PushMessagingBrowserTestEmptySubscriptionOptions
287 : public PushMessagingBrowserTest { 290 : public PushMessagingBrowserTest {
288 std::string GetTestURL() override { 291 std::string GetTestURL() override {
289 return "/push_messaging/test_no_subscription_options.html"; 292 return "/push_messaging/test_no_subscription_options.html";
290 } 293 }
291 }; 294 };
292 295
293 void PushMessagingBrowserTest::RequestAndAcceptPermission() { 296 void PushMessagingBrowserTest::RequestAndAcceptPermission() {
294 std::string script_result; 297 std::string script_result;
295 GetPermissionRequestManager()->set_auto_response_for_test( 298 GetPermissionRequestManager()->set_auto_response_for_test(
296 PermissionRequestManager::ACCEPT_ALL); 299 PermissionRequestManager::ACCEPT_ALL);
297 EXPECT_TRUE(RunScript("requestNotificationPermission();", &script_result)); 300 ASSERT_TRUE(RunScript("requestNotificationPermission();", &script_result));
298 EXPECT_EQ("permission status - granted", script_result); 301 ASSERT_EQ("permission status - granted", script_result);
299 } 302 }
300 303
301 void PushMessagingBrowserTest::RequestAndDenyPermission() { 304 void PushMessagingBrowserTest::RequestAndDenyPermission() {
302 std::string script_result; 305 std::string script_result;
303 GetPermissionRequestManager()->set_auto_response_for_test( 306 GetPermissionRequestManager()->set_auto_response_for_test(
304 PermissionRequestManager::DENY_ALL); 307 PermissionRequestManager::DENY_ALL);
305 EXPECT_TRUE(RunScript("requestNotificationPermission();", &script_result)); 308 ASSERT_TRUE(RunScript("requestNotificationPermission();", &script_result));
306 EXPECT_EQ("permission status - denied", script_result); 309 ASSERT_EQ("permission status - denied", script_result);
307 } 310 }
308 311
309 void PushMessagingBrowserTest::TryToSubscribeSuccessfully( 312 void PushMessagingBrowserTest::SubscribeSuccessfully(bool use_key,
310 const std::string& expected_push_subscription_info, 313 std::string* out_token) {
311 bool use_key) {
312 std::string script_result; 314 std::string script_result;
313 315
314 EXPECT_TRUE(RunScript("registerServiceWorker()", &script_result)); 316 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
315 EXPECT_EQ("ok - service worker registered", script_result); 317 ASSERT_EQ("ok - service worker registered", script_result);
316 318
317 RequestAndAcceptPermission(); 319 ASSERT_NO_FATAL_FAILURE(RequestAndAcceptPermission());
318 320
319 if (use_key) { 321 if (use_key) {
320 ASSERT_TRUE(RunScript("removeManifest()", &script_result)); 322 ASSERT_TRUE(RunScript("removeManifest()", &script_result));
321 ASSERT_EQ("manifest removed", script_result); 323 ASSERT_EQ("manifest removed", script_result);
322 324
323 EXPECT_TRUE(RunScript("documentSubscribePush()", &script_result)); 325 ASSERT_TRUE(RunScript("documentSubscribePush()", &script_result));
324 } else { 326 } else {
325 // Test backwards compatibility with old ID based subscriptions. 327 // Test backwards compatibility with old ID based subscriptions.
326 EXPECT_TRUE(RunScript("documentSubscribePushWithoutKey()", &script_result)); 328 ASSERT_TRUE(RunScript("documentSubscribePushWithoutKey()", &script_result));
327 } 329 }
328 330
329 EXPECT_EQ( 331 ASSERT_NO_FATAL_FAILURE(EndpointToToken(script_result, use_key, out_token));
330 GetEndpointForSubscriptionId(expected_push_subscription_info, use_key), 332 }
331 script_result); 333
334 void PushMessagingBrowserTest::LegacySubscribeSuccessfully(
335 std::string* out_subscription_id) {
336 // Create a non-InstanceID GCM registration. Have to directly access
337 // GCMDriver, since this codepath has been deleted from Push.
338
339 std::string script_result;
340 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
341 ASSERT_EQ("ok - service worker registered", script_result);
342
343 ASSERT_NO_FATAL_FAILURE(RequestAndAcceptPermission());
344
345 GURL requesting_origin = https_server()->GetURL("/").GetOrigin();
346 int64_t service_worker_registration_id = 0LL;
347 PushMessagingAppIdentifier app_identifier =
348 PushMessagingAppIdentifier::Generate(requesting_origin,
349 service_worker_registration_id);
350 push_service_->IncreasePushSubscriptionCount(1, true /* is_pending */);
351
352 std::string subscription_id;
353 {
354 base::RunLoop run_loop;
355 gcm::GCMClient::Result register_result = gcm::GCMClient::UNKNOWN_ERROR;
356 gcm_driver_->Register(
357 app_identifier.app_id(), {kManifestSenderId},
358 base::Bind(&LegacyRegisterCallback, run_loop.QuitClosure(),
359 &subscription_id, &register_result));
360 run_loop.Run();
361 ASSERT_EQ(gcm::GCMClient::SUCCESS, register_result);
362 }
363
364 app_identifier.PersistToPrefs(GetBrowser()->profile());
365 push_service_->IncreasePushSubscriptionCount(1, false /* is_pending */);
366 push_service_->DecreasePushSubscriptionCount(1, true /* was_pending */);
367
368 {
369 base::RunLoop run_loop;
370 push_service_->StorePushSubscriptionForTesting(
371 GetBrowser()->profile(), requesting_origin,
372 service_worker_registration_id, subscription_id, kManifestSenderId,
373 run_loop.QuitClosure());
374 run_loop.Run();
375 }
376
377 if (out_subscription_id)
378 *out_subscription_id = subscription_id;
379 }
380
381 void PushMessagingBrowserTest::EndpointToToken(const std::string& endpoint,
382 bool standard_protocol,
383 std::string* out_token) {
384 size_t last_slash = endpoint.rfind('/');
385
386 ASSERT_EQ(endpoint.substr(0, last_slash + 1),
387 push_service()->GetEndpoint(standard_protocol).spec());
388
389 ASSERT_LT(last_slash + 1, endpoint.length()); // Token must not be empty.
390
391 if (out_token)
392 *out_token = endpoint.substr(last_slash + 1);
332 } 393 }
333 394
334 PushMessagingAppIdentifier 395 PushMessagingAppIdentifier
335 PushMessagingBrowserTest::GetAppIdentifierForServiceWorkerRegistration( 396 PushMessagingBrowserTest::GetAppIdentifierForServiceWorkerRegistration(
336 int64_t service_worker_registration_id) { 397 int64_t service_worker_registration_id) {
337 GURL origin = https_server()->GetURL("/").GetOrigin(); 398 GURL origin = https_server()->GetURL("/").GetOrigin();
338 PushMessagingAppIdentifier app_identifier = 399 PushMessagingAppIdentifier app_identifier =
339 PushMessagingAppIdentifier::FindByServiceWorker( 400 PushMessagingAppIdentifier::FindByServiceWorker(
340 GetBrowser()->profile(), origin, service_worker_registration_id); 401 GetBrowser()->profile(), origin, service_worker_registration_id);
341 EXPECT_FALSE(app_identifier.is_null()); 402 EXPECT_FALSE(app_identifier.is_null());
342 return app_identifier; 403 return app_identifier;
343 } 404 }
344 405
345 void PushMessagingBrowserTest::SendMessageAndWaitUntilHandled( 406 void PushMessagingBrowserTest::SendMessageAndWaitUntilHandled(
346 const PushMessagingAppIdentifier& app_identifier, 407 const PushMessagingAppIdentifier& app_identifier,
347 const gcm::IncomingMessage& message) { 408 const gcm::IncomingMessage& message) {
348 base::RunLoop run_loop; 409 base::RunLoop run_loop;
349 push_service()->SetMessageCallbackForTesting(run_loop.QuitClosure()); 410 push_service()->SetMessageCallbackForTesting(run_loop.QuitClosure());
350 push_service()->OnMessage(app_identifier.app_id(), message); 411 push_service()->OnMessage(app_identifier.app_id(), message);
351 run_loop.Run(); 412 run_loop.Run();
352 } 413 }
353 414
354 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, 415 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
355 SubscribeWithoutKeySuccessNotificationsGranted) { 416 SubscribeWithoutKeySuccessNotificationsGranted) {
356 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */, false); 417 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully(false /* use_key */));
357 418 EXPECT_EQ(kManifestSenderId, gcm_driver_->last_gettoken_authorized_entity());
358 PushMessagingAppIdentifier app_identifier = 419 EXPECT_EQ(GetAppIdentifierForServiceWorkerRegistration(0LL).app_id(),
359 GetAppIdentifierForServiceWorkerRegistration(0LL); 420 gcm_driver_->last_gettoken_app_id());
360 EXPECT_EQ(app_identifier.app_id(), gcm_service()->last_registered_app_id());
361 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]);
362 } 421 }
363 422
364 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, 423 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
365 SubscribeSuccessNotificationsGranted) { 424 SubscribeSuccessNotificationsGranted) {
366 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */); 425 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully());
367
368 PushMessagingAppIdentifier app_identifier =
369 GetAppIdentifierForServiceWorkerRegistration(0LL);
370 EXPECT_EQ(app_identifier.app_id(), gcm_service()->last_registered_app_id());
371 EXPECT_EQ(kEncodedApplicationServerKey, 426 EXPECT_EQ(kEncodedApplicationServerKey,
372 gcm_service()->last_registered_sender_ids()[0]); 427 gcm_driver_->last_gettoken_authorized_entity());
428 EXPECT_EQ(GetAppIdentifierForServiceWorkerRegistration(0LL).app_id(),
429 gcm_driver_->last_gettoken_app_id());
373 } 430 }
374 431
375 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, 432 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
376 SubscribeSuccessNotificationsPrompt) { 433 SubscribeSuccessNotificationsPrompt) {
377 std::string script_result; 434 std::string script_result;
378 435
379 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); 436 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
380 ASSERT_EQ("ok - service worker registered", script_result); 437 ASSERT_EQ("ok - service worker registered", script_result);
381 438
382 GetPermissionRequestManager()->set_auto_response_for_test( 439 GetPermissionRequestManager()->set_auto_response_for_test(
383 PermissionRequestManager::ACCEPT_ALL); 440 PermissionRequestManager::ACCEPT_ALL);
384 ASSERT_TRUE(RunScript("documentSubscribePush()", &script_result)); 441 ASSERT_TRUE(RunScript("documentSubscribePush()", &script_result));
385 EXPECT_EQ(GetEndpointForSubscriptionId("1-0"), script_result); 442 // Both of these methods EXPECT that they succeed.
386 443 ASSERT_NO_FATAL_FAILURE(EndpointToToken(script_result));
387 PushMessagingAppIdentifier app_identifier = 444 GetAppIdentifierForServiceWorkerRegistration(0LL);
388 GetAppIdentifierForServiceWorkerRegistration(0LL);
389 EXPECT_EQ(app_identifier.app_id(), gcm_service()->last_registered_app_id());
390 EXPECT_EQ(kEncodedApplicationServerKey,
391 gcm_service()->last_registered_sender_ids()[0]);
392 } 445 }
393 446
394 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, SubscribeFailureBadKey) { 447 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, SubscribeFailureBadKey) {
395 std::string script_result; 448 std::string script_result;
396 449
397 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); 450 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
398 ASSERT_EQ("ok - service worker registered", script_result); 451 ASSERT_EQ("ok - service worker registered", script_result);
399 452
400 RequestAndAcceptPermission(); 453 ASSERT_NO_FATAL_FAILURE(RequestAndAcceptPermission());
401 454
402 ASSERT_TRUE(RunScript("documentSubscribePushBadKey()", &script_result)); 455 ASSERT_TRUE(RunScript("documentSubscribePushBadKey()", &script_result));
403 EXPECT_EQ( 456 EXPECT_EQ(
404 "InvalidAccessError - Failed to execute 'subscribe' on 'PushManager': " 457 "InvalidAccessError - Failed to execute 'subscribe' on 'PushManager': "
405 "The provided applicationServerKey is not valid.", 458 "The provided applicationServerKey is not valid.",
406 script_result); 459 script_result);
407 } 460 }
408 461
409 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, 462 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
410 SubscribeFailureNotificationsBlocked) { 463 SubscribeFailureNotificationsBlocked) {
411 std::string script_result; 464 std::string script_result;
412 465
413 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); 466 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
414 ASSERT_EQ("ok - service worker registered", script_result); 467 ASSERT_EQ("ok - service worker registered", script_result);
415 468
416 RequestAndDenyPermission(); 469 ASSERT_NO_FATAL_FAILURE(RequestAndDenyPermission());
417 470
418 ASSERT_TRUE(RunScript("documentSubscribePush()", &script_result)); 471 ASSERT_TRUE(RunScript("documentSubscribePush()", &script_result));
419 EXPECT_EQ("NotAllowedError - Registration failed - permission denied", 472 EXPECT_EQ("NotAllowedError - Registration failed - permission denied",
420 script_result); 473 script_result);
421 } 474 }
422 475
423 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, SubscribeFailureNoManifest) { 476 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, SubscribeFailureNoManifest) {
424 std::string script_result; 477 std::string script_result;
425 478
426 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); 479 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
427 ASSERT_EQ("ok - service worker registered", script_result); 480 ASSERT_EQ("ok - service worker registered", script_result);
428 481
429 RequestAndAcceptPermission(); 482 ASSERT_NO_FATAL_FAILURE(RequestAndAcceptPermission());
430 483
431 ASSERT_TRUE(RunScript("removeManifest()", &script_result)); 484 ASSERT_TRUE(RunScript("removeManifest()", &script_result));
432 ASSERT_EQ("manifest removed", script_result); 485 ASSERT_EQ("manifest removed", script_result);
433 486
434 ASSERT_TRUE(RunScript("documentSubscribePushWithoutKey()", &script_result)); 487 ASSERT_TRUE(RunScript("documentSubscribePushWithoutKey()", &script_result));
435 EXPECT_EQ( 488 EXPECT_EQ(
436 "AbortError - Registration failed - missing applicationServerKey, and " 489 "AbortError - Registration failed - missing applicationServerKey, and "
437 "manifest empty or missing", 490 "manifest empty or missing",
438 script_result); 491 script_result);
439 } 492 }
440 493
441 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, SubscribeFailureNoSenderId) { 494 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, SubscribeFailureNoSenderId) {
442 std::string script_result; 495 std::string script_result;
443 496
444 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); 497 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
445 ASSERT_EQ("ok - service worker registered", script_result); 498 ASSERT_EQ("ok - service worker registered", script_result);
446 499
447 RequestAndAcceptPermission(); 500 ASSERT_NO_FATAL_FAILURE(RequestAndAcceptPermission());
448 501
449 ASSERT_TRUE(RunScript("swapManifestNoSenderId()", &script_result)); 502 ASSERT_TRUE(RunScript("swapManifestNoSenderId()", &script_result));
450 ASSERT_EQ("sender id removed from manifest", script_result); 503 ASSERT_EQ("sender id removed from manifest", script_result);
451 504
452 ASSERT_TRUE(RunScript("documentSubscribePushWithoutKey()", &script_result)); 505 ASSERT_TRUE(RunScript("documentSubscribePushWithoutKey()", &script_result));
453 EXPECT_EQ( 506 EXPECT_EQ(
454 "AbortError - Registration failed - missing applicationServerKey, and " 507 "AbortError - Registration failed - missing applicationServerKey, and "
455 "gcm_sender_id not found in manifest", 508 "gcm_sender_id not found in manifest",
456 script_result); 509 script_result);
457 } 510 }
458 511
459 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTestEmptySubscriptionOptions, 512 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTestEmptySubscriptionOptions,
460 RegisterFailureEmptyPushSubscriptionOptions) { 513 RegisterFailureEmptyPushSubscriptionOptions) {
461 std::string script_result; 514 std::string script_result;
462 515
463 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); 516 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
464 ASSERT_EQ("ok - service worker registered", script_result); 517 ASSERT_EQ("ok - service worker registered", script_result);
465 518
466 RequestAndAcceptPermission(); 519 ASSERT_NO_FATAL_FAILURE(RequestAndAcceptPermission());
467 520
468 ASSERT_TRUE(RunScript("documentSubscribePush()", &script_result)); 521 ASSERT_TRUE(RunScript("documentSubscribePush()", &script_result));
469 EXPECT_EQ("NotAllowedError - Registration failed - permission denied", 522 EXPECT_EQ("NotAllowedError - Registration failed - permission denied",
470 script_result); 523 script_result);
471 } 524 }
472 525
473 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, SubscribeWorker) { 526 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, SubscribeWorker) {
474 std::string script_result; 527 std::string script_result;
475 528
476 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); 529 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
477 ASSERT_EQ("ok - service worker registered", script_result); 530 ASSERT_EQ("ok - service worker registered", script_result);
478 531
479 RequestAndAcceptPermission(); 532 ASSERT_NO_FATAL_FAILURE(RequestAndAcceptPermission());
480 533
481 LoadTestPage(); // Reload to become controlled. 534 LoadTestPage(); // Reload to become controlled.
482 535
483 ASSERT_TRUE(RunScript("isControlled()", &script_result)); 536 ASSERT_TRUE(RunScript("isControlled()", &script_result));
484 ASSERT_EQ("true - is controlled", script_result); 537 ASSERT_EQ("true - is controlled", script_result);
485 538
486 // Try to subscribe from a worker without a key. This should fail. 539 // Try to subscribe from a worker without a key. This should fail.
487 ASSERT_TRUE(RunScript("workerSubscribePushNoKey()", &script_result)); 540 ASSERT_TRUE(RunScript("workerSubscribePushNoKey()", &script_result));
488 EXPECT_EQ( 541 EXPECT_EQ(
489 "AbortError - Registration failed - missing applicationServerKey, and " 542 "AbortError - Registration failed - missing applicationServerKey, and "
490 "gcm_sender_id not found in manifest", 543 "gcm_sender_id not found in manifest",
491 script_result); 544 script_result);
492 // Now run the subscribe from the service worker with a key. This 545 // Now run the subscribe from the service worker with a key. This
493 // should succeed, and write the key to the datastore. 546 // should succeed, and write the key to the datastore.
494 ASSERT_TRUE(RunScript("workerSubscribePush()", &script_result)); 547 ASSERT_TRUE(RunScript("workerSubscribePush()", &script_result));
495 EXPECT_EQ(GetEndpointForSubscriptionId("1-0"), script_result); 548 std::string token1;
549 ASSERT_NO_FATAL_FAILURE(
550 EndpointToToken(script_result, true /* standard_protocol */, &token1));
496 551
497 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result)); 552 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
498 EXPECT_EQ("unsubscribe result: true", script_result); 553 EXPECT_EQ("unsubscribe result: true", script_result);
499 EXPECT_NE(push_service(), GetAppHandler()); 554 EXPECT_NE(push_service(), GetAppHandler());
500 555
501 // Now run the subscribe from the service worker without a key. 556 // Now run the subscribe from the service worker without a key.
502 // In this case, the key will be read from the datastore. 557 // In this case, the key will be read from the datastore.
503 ASSERT_TRUE(RunScript("workerSubscribePushNoKey()", &script_result)); 558 ASSERT_TRUE(RunScript("workerSubscribePushNoKey()", &script_result));
504 EXPECT_EQ(GetEndpointForSubscriptionId("1-1"), script_result); 559 std::string token2;
560 ASSERT_NO_FATAL_FAILURE(
561 EndpointToToken(script_result, true /* standard_protocol */, &token2));
562 EXPECT_NE(token1, token2);
505 563
506 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result)); 564 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
507 EXPECT_EQ("unsubscribe result: true", script_result); 565 EXPECT_EQ("unsubscribe result: true", script_result);
508 EXPECT_NE(push_service(), GetAppHandler()); 566 EXPECT_NE(push_service(), GetAppHandler());
509 } 567 }
510 568
511 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, SubscribeWorkerUsingManifest) { 569 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, SubscribeWorkerUsingManifest) {
512 std::string script_result; 570 std::string script_result;
513 571
514 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); 572 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
515 ASSERT_EQ("ok - service worker registered", script_result); 573 ASSERT_EQ("ok - service worker registered", script_result);
516 574
517 RequestAndAcceptPermission(); 575 ASSERT_NO_FATAL_FAILURE(RequestAndAcceptPermission());
518 576
519 LoadTestPage(); // Reload to become controlled. 577 LoadTestPage(); // Reload to become controlled.
520 578
521 ASSERT_TRUE(RunScript("isControlled()", &script_result)); 579 ASSERT_TRUE(RunScript("isControlled()", &script_result));
522 ASSERT_EQ("true - is controlled", script_result); 580 ASSERT_EQ("true - is controlled", script_result);
523 581
524 // Try to subscribe from a worker without a key. This should fail. 582 // Try to subscribe from a worker without a key. This should fail.
525 ASSERT_TRUE(RunScript("workerSubscribePushNoKey()", &script_result)); 583 ASSERT_TRUE(RunScript("workerSubscribePushNoKey()", &script_result));
526 EXPECT_EQ( 584 EXPECT_EQ(
527 "AbortError - Registration failed - missing applicationServerKey, and " 585 "AbortError - Registration failed - missing applicationServerKey, and "
528 "gcm_sender_id not found in manifest", 586 "gcm_sender_id not found in manifest",
529 script_result); 587 script_result);
530 EXPECT_NE(push_service(), GetAppHandler()); 588 EXPECT_NE(push_service(), GetAppHandler());
531 589
532 // Run the subscription from the document without a key, this will trigger 590 // Run the subscription from the document without a key, this will trigger
533 // the code to read sender id from the manifest and will write it to the 591 // the code to read sender id from the manifest and will write it to the
534 // datastore. 592 // datastore.
535 ASSERT_TRUE(RunScript("documentSubscribePushWithoutKey()", &script_result)); 593 ASSERT_TRUE(RunScript("documentSubscribePushWithoutKey()", &script_result));
536 EXPECT_EQ(GetEndpointForSubscriptionId("1-0", false), script_result); 594 std::string token1;
595 ASSERT_NO_FATAL_FAILURE(
596 EndpointToToken(script_result, false /* standard_protocol */, &token1));
537 597
538 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result)); 598 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
539 EXPECT_EQ("unsubscribe result: true", script_result); 599 EXPECT_EQ("unsubscribe result: true", script_result);
540 EXPECT_NE(push_service(), GetAppHandler()); 600 EXPECT_NE(push_service(), GetAppHandler());
541 601
542 // Now run the subscribe from the service worker without a key. 602 // Now run the subscribe from the service worker without a key.
543 // In this case, the sender id will be read from the datastore. 603 // In this case, the sender id will be read from the datastore.
544 ASSERT_TRUE(RunScript("workerSubscribePushNoKey()", &script_result)); 604 ASSERT_TRUE(RunScript("workerSubscribePushNoKey()", &script_result));
545 EXPECT_EQ(GetEndpointForSubscriptionId("1-1", false), script_result); 605 std::string token2;
606 ASSERT_NO_FATAL_FAILURE(
607 EndpointToToken(script_result, false /* standard_protocol */, &token2));
608 EXPECT_NE(token1, token2);
546 609
547 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result)); 610 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
548 EXPECT_EQ("unsubscribe result: true", script_result); 611 EXPECT_EQ("unsubscribe result: true", script_result);
549 EXPECT_NE(push_service(), GetAppHandler()); 612 EXPECT_NE(push_service(), GetAppHandler());
550 } 613 }
551 614
552 // Disabled on Windows and Linux due to flakiness (http://crbug.com/554003). 615 // Disabled on Windows and Linux due to flakiness (http://crbug.com/554003).
553 #if defined(OS_WIN) || defined(OS_LINUX) 616 #if defined(OS_WIN) || defined(OS_LINUX)
554 #define MAYBE_SubscribePersisted DISABLED_SubscribePersisted 617 #define MAYBE_SubscribePersisted DISABLED_SubscribePersisted
555 #else 618 #else
556 #define MAYBE_SubscribePersisted SubscribePersisted 619 #define MAYBE_SubscribePersisted SubscribePersisted
557 #endif 620 #endif
558 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, MAYBE_SubscribePersisted) { 621 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, MAYBE_SubscribePersisted) {
559 std::string script_result; 622 std::string script_result;
560 623
561 // First, test that Service Worker registration IDs are assigned in order of 624 // First, test that Service Worker registration IDs are assigned in order of
562 // registering the Service Workers, and the (fake) push subscription ids are 625 // registering the Service Workers, and the (fake) push subscription ids are
563 // assigned in order of push subscription (even when these orders are 626 // assigned in order of push subscription (even when these orders are
564 // different). 627 // different).
565 628
566 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */); 629 std::string token1;
630 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully(true /* use_key */, &token1));
567 PushMessagingAppIdentifier sw0_identifier = 631 PushMessagingAppIdentifier sw0_identifier =
568 GetAppIdentifierForServiceWorkerRegistration(0LL); 632 GetAppIdentifierForServiceWorkerRegistration(0LL);
569 EXPECT_EQ(sw0_identifier.app_id(), gcm_service()->last_registered_app_id()); 633 EXPECT_EQ(sw0_identifier.app_id(), gcm_driver_->last_gettoken_app_id());
570 634
571 LoadTestPage("/push_messaging/subscope1/test.html"); 635 LoadTestPage("/push_messaging/subscope1/test.html");
572 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); 636 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
573 ASSERT_EQ("ok - service worker registered", script_result); 637 ASSERT_EQ("ok - service worker registered", script_result);
574 638
575 LoadTestPage("/push_messaging/subscope2/test.html"); 639 LoadTestPage("/push_messaging/subscope2/test.html");
576 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); 640 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
577 ASSERT_EQ("ok - service worker registered", script_result); 641 ASSERT_EQ("ok - service worker registered", script_result);
578 642
579 // Note that we need to reload the page after registering, otherwise 643 // Note that we need to reload the page after registering, otherwise
580 // navigator.serviceWorker.ready is going to be resolved with the parent 644 // navigator.serviceWorker.ready is going to be resolved with the parent
581 // Service Worker which still controls the page. 645 // Service Worker which still controls the page.
582 LoadTestPage("/push_messaging/subscope2/test.html"); 646 LoadTestPage("/push_messaging/subscope2/test.html");
583 TryToSubscribeSuccessfully("1-1" /* expected_push_subscription_id */); 647 std::string token2;
648 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully(true /* use_key */, &token2));
649 EXPECT_NE(token1, token2);
584 PushMessagingAppIdentifier sw2_identifier = 650 PushMessagingAppIdentifier sw2_identifier =
585 GetAppIdentifierForServiceWorkerRegistration(2LL); 651 GetAppIdentifierForServiceWorkerRegistration(2LL);
586 EXPECT_EQ(sw2_identifier.app_id(), gcm_service()->last_registered_app_id()); 652 EXPECT_EQ(sw2_identifier.app_id(), gcm_driver_->last_gettoken_app_id());
587 653
588 LoadTestPage("/push_messaging/subscope1/test.html"); 654 LoadTestPage("/push_messaging/subscope1/test.html");
589 TryToSubscribeSuccessfully("1-2" /* expected_push_subscription_id */); 655 std::string token3;
656 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully(true /* use_key */, &token3));
657 EXPECT_NE(token1, token3);
658 EXPECT_NE(token2, token3);
590 PushMessagingAppIdentifier sw1_identifier = 659 PushMessagingAppIdentifier sw1_identifier =
591 GetAppIdentifierForServiceWorkerRegistration(1LL); 660 GetAppIdentifierForServiceWorkerRegistration(1LL);
592 EXPECT_EQ(sw1_identifier.app_id(), gcm_service()->last_registered_app_id()); 661 EXPECT_EQ(sw1_identifier.app_id(), gcm_driver_->last_gettoken_app_id());
593 662
594 // Now test that the Service Worker registration IDs and push subscription IDs 663 // Now test that the Service Worker registration IDs and push subscription IDs
595 // generated above were persisted to SW storage, by checking that they are 664 // generated above were persisted to SW storage, by checking that they are
596 // unchanged despite requesting them in a different order. 665 // unchanged despite requesting them in a different order.
597 // TODO(johnme): Ideally we would restart the browser at this point to check 666 // TODO(johnme): Ideally we would restart the browser at this point to check
598 // they were persisted to disk, but that's not currently possible since the 667 // they were persisted to disk, but that's not currently possible since the
599 // test server uses random port numbers for each test (even PRE_Foo and Foo), 668 // test server uses random port numbers for each test (even PRE_Foo and Foo),
600 // so we wouldn't be able to load the test pages with the same origin. 669 // so we wouldn't be able to load the test pages with the same origin.
601 670
602 LoadTestPage("/push_messaging/subscope1/test.html"); 671 LoadTestPage("/push_messaging/subscope1/test.html");
603 TryToSubscribeSuccessfully("1-2" /* expected_push_subscription_id */); 672 std::string token4;
604 EXPECT_EQ(sw1_identifier.app_id(), gcm_service()->last_registered_app_id()); 673 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully(true /* use_key */, &token4));
674 EXPECT_EQ(token3, token4);
675 EXPECT_EQ(sw1_identifier.app_id(), gcm_driver_->last_gettoken_app_id());
605 676
606 LoadTestPage("/push_messaging/subscope2/test.html"); 677 LoadTestPage("/push_messaging/subscope2/test.html");
607 TryToSubscribeSuccessfully("1-1" /* expected_push_subscription_id */); 678 std::string token5;
608 EXPECT_EQ(sw1_identifier.app_id(), gcm_service()->last_registered_app_id()); 679 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully(true /* use_key */, &token5));
680 EXPECT_EQ(token2, token5);
681 EXPECT_EQ(sw1_identifier.app_id(), gcm_driver_->last_gettoken_app_id());
609 682
610 LoadTestPage(); 683 LoadTestPage();
611 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */); 684 std::string token6;
612 EXPECT_EQ(sw1_identifier.app_id(), gcm_service()->last_registered_app_id()); 685 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully(true /* use_key */, &token6));
686 EXPECT_EQ(token1, token6);
687 EXPECT_EQ(sw1_identifier.app_id(), gcm_driver_->last_gettoken_app_id());
613 } 688 }
614 689
615 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, AppHandlerOnlyIfSubscribed) { 690 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, AppHandlerOnlyIfSubscribed) {
616 // This test restarts the push service to simulate restarting the browser. 691 // This test restarts the push service to simulate restarting the browser.
617 692
618 EXPECT_NE(push_service(), GetAppHandler()); 693 EXPECT_NE(push_service(), GetAppHandler());
619 ASSERT_NO_FATAL_FAILURE(RestartPushService()); 694 ASSERT_NO_FATAL_FAILURE(RestartPushService());
620 EXPECT_NE(push_service(), GetAppHandler()); 695 EXPECT_NE(push_service(), GetAppHandler());
621 696
622 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */); 697 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully());
623 698
624 EXPECT_EQ(push_service(), GetAppHandler()); 699 EXPECT_EQ(push_service(), GetAppHandler());
625 ASSERT_NO_FATAL_FAILURE(RestartPushService()); 700 ASSERT_NO_FATAL_FAILURE(RestartPushService());
626 EXPECT_EQ(push_service(), GetAppHandler()); 701 EXPECT_EQ(push_service(), GetAppHandler());
627 702
628 // Unsubscribe. 703 // Unsubscribe.
629 std::string script_result; 704 std::string script_result;
630 gcm_service()->AddExpectedUnregisterResponse(gcm::GCMClient::SUCCESS);
631 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result)); 705 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
632 EXPECT_EQ("unsubscribe result: true", script_result); 706 EXPECT_EQ("unsubscribe result: true", script_result);
633 707
634 EXPECT_NE(push_service(), GetAppHandler()); 708 EXPECT_NE(push_service(), GetAppHandler());
635 ASSERT_NO_FATAL_FAILURE(RestartPushService()); 709 ASSERT_NO_FATAL_FAILURE(RestartPushService());
636 EXPECT_NE(push_service(), GetAppHandler()); 710 EXPECT_NE(push_service(), GetAppHandler());
637 } 711 }
638 712
639 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, PushEventSuccess) { 713 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, PushEventSuccess) {
640 std::string script_result; 714 std::string script_result;
641 715
642 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */); 716 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully());
643
644 PushMessagingAppIdentifier app_identifier = 717 PushMessagingAppIdentifier app_identifier =
645 GetAppIdentifierForServiceWorkerRegistration(0LL); 718 GetAppIdentifierForServiceWorkerRegistration(0LL);
646 EXPECT_EQ(app_identifier.app_id(), gcm_service()->last_registered_app_id());
647 EXPECT_EQ(kEncodedApplicationServerKey,
648 gcm_service()->last_registered_sender_ids()[0]);
649 719
650 ASSERT_TRUE(RunScript("isControlled()", &script_result)); 720 ASSERT_TRUE(RunScript("isControlled()", &script_result));
651 ASSERT_EQ("false - is not controlled", script_result); 721 ASSERT_EQ("false - is not controlled", script_result);
652 722
653 LoadTestPage(); // Reload to become controlled. 723 LoadTestPage(); // Reload to become controlled.
654 724
655 ASSERT_TRUE(RunScript("isControlled()", &script_result)); 725 ASSERT_TRUE(RunScript("isControlled()", &script_result));
656 ASSERT_EQ("true - is controlled", script_result); 726 ASSERT_EQ("true - is controlled", script_result);
657 727
658 EXPECT_TRUE(IsRegisteredKeepAliveEqualTo(false)); 728 EXPECT_TRUE(IsRegisteredKeepAliveEqualTo(false));
659 gcm::IncomingMessage message; 729 gcm::IncomingMessage message;
660 message.sender_id = GetTestApplicationServerKey(); 730 message.sender_id = GetTestApplicationServerKey();
661 message.raw_data = "testdata"; 731 message.raw_data = "testdata";
662 message.decrypted = true; 732 message.decrypted = true;
663 push_service()->OnMessage(app_identifier.app_id(), message); 733 push_service()->OnMessage(app_identifier.app_id(), message);
664 EXPECT_TRUE(IsRegisteredKeepAliveEqualTo(true)); 734 EXPECT_TRUE(IsRegisteredKeepAliveEqualTo(true));
665 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result)); 735 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result));
666 EXPECT_EQ("testdata", script_result); 736 EXPECT_EQ("testdata", script_result);
667 } 737 }
668 738
669 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, PushEventWithoutPayload) { 739 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, PushEventWithoutPayload) {
670 std::string script_result; 740 std::string script_result;
671 741
672 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */); 742 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully());
673
674 PushMessagingAppIdentifier app_identifier = 743 PushMessagingAppIdentifier app_identifier =
675 GetAppIdentifierForServiceWorkerRegistration(0LL); 744 GetAppIdentifierForServiceWorkerRegistration(0LL);
676 EXPECT_EQ(app_identifier.app_id(), gcm_service()->last_registered_app_id());
677 EXPECT_EQ(kEncodedApplicationServerKey,
678 gcm_service()->last_registered_sender_ids()[0]);
679 745
680 ASSERT_TRUE(RunScript("isControlled()", &script_result)); 746 ASSERT_TRUE(RunScript("isControlled()", &script_result));
681 ASSERT_EQ("false - is not controlled", script_result); 747 ASSERT_EQ("false - is not controlled", script_result);
682 748
683 LoadTestPage(); // Reload to become controlled. 749 LoadTestPage(); // Reload to become controlled.
684 750
685 ASSERT_TRUE(RunScript("isControlled()", &script_result)); 751 ASSERT_TRUE(RunScript("isControlled()", &script_result));
686 ASSERT_EQ("true - is controlled", script_result); 752 ASSERT_EQ("true - is controlled", script_result);
687 753
688 gcm::IncomingMessage message; 754 gcm::IncomingMessage message;
689 message.sender_id = GetTestApplicationServerKey(); 755 message.sender_id = GetTestApplicationServerKey();
690 message.decrypted = false; 756 message.decrypted = false;
691 757
692 push_service()->OnMessage(app_identifier.app_id(), message); 758 push_service()->OnMessage(app_identifier.app_id(), message);
693 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result)); 759 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result));
694 EXPECT_EQ("[NULL]", script_result); 760 EXPECT_EQ("[NULL]", script_result);
695 } 761 }
696 762
763 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, LegacyPushEvent) {
764 std::string script_result;
765
766 ASSERT_NO_FATAL_FAILURE(LegacySubscribeSuccessfully());
767 PushMessagingAppIdentifier app_identifier =
768 GetAppIdentifierForServiceWorkerRegistration(0LL);
769
770 ASSERT_TRUE(RunScript("isControlled()", &script_result));
771 ASSERT_EQ("false - is not controlled", script_result);
772
773 LoadTestPage(); // Reload to become controlled.
774
775 ASSERT_TRUE(RunScript("isControlled()", &script_result));
776 ASSERT_EQ("true - is controlled", script_result);
777
778 gcm::IncomingMessage message;
779 message.sender_id = kManifestSenderId;
780 message.decrypted = false;
781
782 push_service()->OnMessage(app_identifier.app_id(), message);
783 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result));
784 EXPECT_EQ("[NULL]", script_result);
785 }
786
697 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, PushEventNoServiceWorker) { 787 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, PushEventNoServiceWorker) {
698 std::string script_result; 788 std::string script_result;
699 789
700 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */); 790 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully());
701
702 PushMessagingAppIdentifier app_identifier = 791 PushMessagingAppIdentifier app_identifier =
703 GetAppIdentifierForServiceWorkerRegistration(0LL); 792 GetAppIdentifierForServiceWorkerRegistration(0LL);
704 EXPECT_EQ(app_identifier.app_id(), gcm_service()->last_registered_app_id());
705 EXPECT_EQ(kEncodedApplicationServerKey,
706 gcm_service()->last_registered_sender_ids()[0]);
707 793
708 ASSERT_TRUE(RunScript("isControlled()", &script_result)); 794 ASSERT_TRUE(RunScript("isControlled()", &script_result));
709 ASSERT_EQ("false - is not controlled", script_result); 795 ASSERT_EQ("false - is not controlled", script_result);
710 796
711 LoadTestPage(); // Reload to become controlled. 797 LoadTestPage(); // Reload to become controlled.
712 798
713 ASSERT_TRUE(RunScript("isControlled()", &script_result)); 799 ASSERT_TRUE(RunScript("isControlled()", &script_result));
714 ASSERT_EQ("true - is controlled", script_result); 800 ASSERT_EQ("true - is controlled", script_result);
715 801
716 // Unregister service worker. Sending a message should now fail. 802 // Unregister service worker. Sending a message should now fail.
717 ASSERT_TRUE(RunScript("unregisterServiceWorker()", &script_result)); 803 ASSERT_TRUE(RunScript("unregisterServiceWorker()", &script_result));
718 ASSERT_EQ("service worker unregistration status: true", script_result); 804 ASSERT_EQ("service worker unregistration status: true", script_result);
719 805
720 // When the push service will receive it next message, given that there is no 806 // Unregistering the service worker doesn't yet unsubscribe from push (though
721 // SW available, it should unregister |app_identifier.app_id()|. 807 // it should), and FindByServiceWorker doesn't require a live SW.
722 UnregistrationCallback callback; 808 GURL origin = https_server()->GetURL("/").GetOrigin();
723 gcm_service()->SetUnregisterCallback( 809 PushMessagingAppIdentifier app_identifier2 =
724 base::Bind(&UnregistrationCallback::Run, base::Unretained(&callback))); 810 PushMessagingAppIdentifier::FindByServiceWorker(
811 GetBrowser()->profile(), origin,
812 0LL /* service_worker_registration_id */);
813 EXPECT_FALSE(app_identifier2.is_null());
814 EXPECT_EQ(app_identifier.app_id(), app_identifier2.app_id());
725 815
726 gcm::IncomingMessage message; 816 gcm::IncomingMessage message;
727 message.sender_id = GetTestApplicationServerKey(); 817 message.sender_id = GetTestApplicationServerKey();
728 message.raw_data = "testdata"; 818 message.raw_data = "testdata";
729 message.decrypted = true; 819 message.decrypted = true;
820
821 base::RunLoop run_loop;
822 push_service()->SetMessageCallbackForTesting(run_loop.QuitClosure());
730 EXPECT_TRUE(IsRegisteredKeepAliveEqualTo(false)); 823 EXPECT_TRUE(IsRegisteredKeepAliveEqualTo(false));
731 push_service()->OnMessage(app_identifier.app_id(), message); 824 push_service()->OnMessage(app_identifier.app_id(), message);
732 EXPECT_TRUE(IsRegisteredKeepAliveEqualTo(true)); 825 EXPECT_TRUE(IsRegisteredKeepAliveEqualTo(true));
733 callback.WaitUntilSatisfied(); 826 run_loop.Run();
734 EXPECT_TRUE(IsRegisteredKeepAliveEqualTo(false)); 827 EXPECT_TRUE(IsRegisteredKeepAliveEqualTo(false));
735 EXPECT_EQ(app_identifier.app_id(), callback.app_id()); 828
829 // Now the push service has received a message and failed to find its service
830 // worker, it should have automatically unsubscribed app_identifier.app_id().
831 PushMessagingAppIdentifier app_identifier3 =
832 PushMessagingAppIdentifier::FindByServiceWorker(
833 GetBrowser()->profile(), origin,
834 0LL /* service_worker_registration_id */);
835 EXPECT_TRUE(app_identifier3.is_null());
736 836
737 // No push data should have been received. 837 // No push data should have been received.
738 ASSERT_TRUE(RunScript("resultQueue.popImmediately()", &script_result)); 838 ASSERT_TRUE(RunScript("resultQueue.popImmediately()", &script_result));
739 EXPECT_EQ("null", script_result); 839 EXPECT_EQ("null", script_result);
740 } 840 }
741 841
742 #if defined(ENABLE_NOTIFICATIONS) 842 #if defined(ENABLE_NOTIFICATIONS)
743 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, 843 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
744 PushEventEnforcesUserVisibleNotification) { 844 PushEventEnforcesUserVisibleNotification) {
745 std::string script_result; 845 std::string script_result;
746 846
747 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */); 847 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully());
748
749 PushMessagingAppIdentifier app_identifier = 848 PushMessagingAppIdentifier app_identifier =
750 GetAppIdentifierForServiceWorkerRegistration(0LL); 849 GetAppIdentifierForServiceWorkerRegistration(0LL);
751 EXPECT_EQ(app_identifier.app_id(), gcm_service()->last_registered_app_id());
752 EXPECT_EQ(kEncodedApplicationServerKey,
753 gcm_service()->last_registered_sender_ids()[0]);
754 850
755 ASSERT_TRUE(RunScript("isControlled()", &script_result)); 851 ASSERT_TRUE(RunScript("isControlled()", &script_result));
756 ASSERT_EQ("false - is not controlled", script_result); 852 ASSERT_EQ("false - is not controlled", script_result);
757 853
758 LoadTestPage(); // Reload to become controlled. 854 LoadTestPage(); // Reload to become controlled.
759 855
760 ASSERT_TRUE(RunScript("isControlled()", &script_result)); 856 ASSERT_TRUE(RunScript("isControlled()", &script_result));
761 ASSERT_EQ("true - is controlled", script_result); 857 ASSERT_EQ("true - is controlled", script_result);
762 858
763 notification_manager()->CancelAll(); 859 notification_manager()->CancelAll();
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
842 notification_manager()->GetNotificationAt(0); 938 notification_manager()->GetNotificationAt(0);
843 939
844 EXPECT_NE(kPushMessagingForcedNotificationTag, first_notification.tag()); 940 EXPECT_NE(kPushMessagingForcedNotificationTag, first_notification.tag());
845 } 941 }
846 } 942 }
847 943
848 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, 944 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
849 PushEventAllowSilentPushCommandLineFlag) { 945 PushEventAllowSilentPushCommandLineFlag) {
850 std::string script_result; 946 std::string script_result;
851 947
852 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */); 948 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully());
853
854 PushMessagingAppIdentifier app_identifier = 949 PushMessagingAppIdentifier app_identifier =
855 GetAppIdentifierForServiceWorkerRegistration(0LL); 950 GetAppIdentifierForServiceWorkerRegistration(0LL);
856 EXPECT_EQ(app_identifier.app_id(), gcm_service()->last_registered_app_id()); 951 EXPECT_EQ(app_identifier.app_id(), gcm_driver_->last_gettoken_app_id());
857 EXPECT_EQ(kEncodedApplicationServerKey, 952 EXPECT_EQ(kEncodedApplicationServerKey,
858 gcm_service()->last_registered_sender_ids()[0]); 953 gcm_driver_->last_gettoken_authorized_entity());
859 954
860 ASSERT_TRUE(RunScript("isControlled()", &script_result)); 955 ASSERT_TRUE(RunScript("isControlled()", &script_result));
861 ASSERT_EQ("false - is not controlled", script_result); 956 ASSERT_EQ("false - is not controlled", script_result);
862 957
863 LoadTestPage(); // Reload to become controlled. 958 LoadTestPage(); // Reload to become controlled.
864 959
865 ASSERT_TRUE(RunScript("isControlled()", &script_result)); 960 ASSERT_TRUE(RunScript("isControlled()", &script_result));
866 ASSERT_EQ("true - is controlled", script_result); 961 ASSERT_EQ("true - is controlled", script_result);
867 962
868 notification_manager()->CancelAll(); 963 notification_manager()->CancelAll();
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
914 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result, web_contents)); 1009 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result, web_contents));
915 EXPECT_EQ("testdata", script_result); 1010 EXPECT_EQ("testdata", script_result);
916 1011
917 ASSERT_EQ(0u, notification_manager()->GetNotificationCount()); 1012 ASSERT_EQ(0u, notification_manager()->GetNotificationCount());
918 } 1013 }
919 1014
920 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, 1015 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
921 PushEventEnforcesUserVisibleNotificationAfterQueue) { 1016 PushEventEnforcesUserVisibleNotificationAfterQueue) {
922 std::string script_result; 1017 std::string script_result;
923 1018
924 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */); 1019 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully());
925
926 PushMessagingAppIdentifier app_identifier = 1020 PushMessagingAppIdentifier app_identifier =
927 GetAppIdentifierForServiceWorkerRegistration(0LL); 1021 GetAppIdentifierForServiceWorkerRegistration(0LL);
928 EXPECT_EQ(app_identifier.app_id(), gcm_service()->last_registered_app_id());
929 EXPECT_EQ(kEncodedApplicationServerKey,
930 gcm_service()->last_registered_sender_ids()[0]);
931 1022
932 ASSERT_TRUE(RunScript("isControlled()", &script_result)); 1023 ASSERT_TRUE(RunScript("isControlled()", &script_result));
933 ASSERT_EQ("false - is not controlled", script_result); 1024 ASSERT_EQ("false - is not controlled", script_result);
934 1025
935 LoadTestPage(); // Reload to become controlled. 1026 LoadTestPage(); // Reload to become controlled.
936 1027
937 ASSERT_TRUE(RunScript("isControlled()", &script_result)); 1028 ASSERT_TRUE(RunScript("isControlled()", &script_result));
938 ASSERT_EQ("true - is controlled", script_result); 1029 ASSERT_EQ("true - is controlled", script_result);
939 1030
940 // Fire off two push messages in sequence, only the second one of which will 1031 // Fire off two push messages in sequence, only the second one of which will
(...skipping 27 matching lines...) Expand all
968 EXPECT_EQ(0u, number_of_notifications_shown[0]); 1059 EXPECT_EQ(0u, number_of_notifications_shown[0]);
969 EXPECT_EQ(1u, number_of_notifications_shown[1]); 1060 EXPECT_EQ(1u, number_of_notifications_shown[1]);
970 } 1061 }
971 1062
972 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, 1063 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
973 PushEventNotificationWithoutEventWaitUntil) { 1064 PushEventNotificationWithoutEventWaitUntil) {
974 std::string script_result; 1065 std::string script_result;
975 content::WebContents* web_contents = 1066 content::WebContents* web_contents =
976 GetBrowser()->tab_strip_model()->GetActiveWebContents(); 1067 GetBrowser()->tab_strip_model()->GetActiveWebContents();
977 1068
978 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */); 1069 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully());
979
980 PushMessagingAppIdentifier app_identifier = 1070 PushMessagingAppIdentifier app_identifier =
981 GetAppIdentifierForServiceWorkerRegistration(0LL); 1071 GetAppIdentifierForServiceWorkerRegistration(0LL);
982 EXPECT_EQ(app_identifier.app_id(), gcm_service()->last_registered_app_id());
983 EXPECT_EQ(kEncodedApplicationServerKey,
984 gcm_service()->last_registered_sender_ids()[0]);
985 1072
986 ASSERT_TRUE(RunScript("isControlled()", &script_result)); 1073 ASSERT_TRUE(RunScript("isControlled()", &script_result));
987 ASSERT_EQ("false - is not controlled", script_result); 1074 ASSERT_EQ("false - is not controlled", script_result);
988 1075
989 LoadTestPage(); // Reload to become controlled. 1076 LoadTestPage(); // Reload to become controlled.
990 1077
991 ASSERT_TRUE(RunScript("isControlled()", &script_result)); 1078 ASSERT_TRUE(RunScript("isControlled()", &script_result));
992 ASSERT_EQ("true - is controlled", script_result); 1079 ASSERT_EQ("true - is controlled", script_result);
993 1080
994 scoped_refptr<content::MessageLoopRunner> message_loop_runner = 1081 scoped_refptr<content::MessageLoopRunner> message_loop_runner =
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
1028 ASSERT_TRUE(RunScript("permissionState()", &script_result)); 1115 ASSERT_TRUE(RunScript("permissionState()", &script_result));
1029 ASSERT_EQ("permission status - prompt", script_result); 1116 ASSERT_EQ("permission status - prompt", script_result);
1030 } 1117 }
1031 1118
1032 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, PermissionStateSaysGranted) { 1119 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, PermissionStateSaysGranted) {
1033 std::string script_result; 1120 std::string script_result;
1034 1121
1035 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); 1122 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
1036 ASSERT_EQ("ok - service worker registered", script_result); 1123 ASSERT_EQ("ok - service worker registered", script_result);
1037 1124
1038 RequestAndAcceptPermission(); 1125 ASSERT_NO_FATAL_FAILURE(RequestAndAcceptPermission());
1039 1126
1040 ASSERT_TRUE(RunScript("documentSubscribePush()", &script_result)); 1127 ASSERT_TRUE(RunScript("documentSubscribePush()", &script_result));
1041 EXPECT_EQ(GetEndpointForSubscriptionId("1-0"), script_result); 1128 ASSERT_NO_FATAL_FAILURE(EndpointToToken(script_result));
1042 1129
1043 ASSERT_TRUE(RunScript("permissionState()", &script_result)); 1130 ASSERT_TRUE(RunScript("permissionState()", &script_result));
1044 EXPECT_EQ("permission status - granted", script_result); 1131 EXPECT_EQ("permission status - granted", script_result);
1045 } 1132 }
1046 1133
1047 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, PermissionStateSaysDenied) { 1134 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, PermissionStateSaysDenied) {
1048 std::string script_result; 1135 std::string script_result;
1049 1136
1050 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); 1137 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
1051 ASSERT_EQ("ok - service worker registered", script_result); 1138 ASSERT_EQ("ok - service worker registered", script_result);
1052 1139
1053 RequestAndDenyPermission(); 1140 ASSERT_NO_FATAL_FAILURE(RequestAndDenyPermission());
1054 1141
1055 ASSERT_TRUE(RunScript("documentSubscribePush()", &script_result)); 1142 ASSERT_TRUE(RunScript("documentSubscribePush()", &script_result));
1056 EXPECT_EQ("NotAllowedError - Registration failed - permission denied", 1143 EXPECT_EQ("NotAllowedError - Registration failed - permission denied",
1057 script_result); 1144 script_result);
1058 1145
1059 ASSERT_TRUE(RunScript("permissionState()", &script_result)); 1146 ASSERT_TRUE(RunScript("permissionState()", &script_result));
1060 EXPECT_EQ("permission status - denied", script_result); 1147 EXPECT_EQ("permission status - denied", script_result);
1061 } 1148 }
1062 1149
1063 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, UnsubscribeSuccess) { 1150 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, UnsubscribeSuccess) {
1064 std::string script_result; 1151 std::string script_result;
1065 1152
1066 EXPECT_TRUE(RunScript("registerServiceWorker()", &script_result)); 1153 EXPECT_TRUE(RunScript("registerServiceWorker()", &script_result));
1067 EXPECT_EQ("ok - service worker registered", script_result); 1154 EXPECT_EQ("ok - service worker registered", script_result);
1068 1155
1069 // Resolves true if there was a subscription. 1156 // Resolves true if there was a subscription.
1070 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */, false); 1157 std::string token1;
1071 gcm_service()->AddExpectedUnregisterResponse(gcm::GCMClient::SUCCESS); 1158 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully(false /* use_key */, &token1));
1072 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result)); 1159 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
1073 EXPECT_EQ("unsubscribe result: true", script_result); 1160 EXPECT_EQ("unsubscribe result: true", script_result);
1074 1161
1162 // Resolves false if there was no longer a subscription.
1163 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
1164 EXPECT_EQ("unsubscribe result: false", script_result);
1165
1166 // TODO(johnme): Test that doesn't reject if there was a network error (should
1167 // deactivate subscription locally anyway).
1168 // TODO(johnme): Test that doesn't reject if there were other push service
1169 // errors (should deactivate subscription locally anyway).
1170
1171 // Unsubscribing (with an existing reference to a PushSubscription), after
1172 // unregistering the Service Worker, just means push subscription isn't found.
1173 std::string token2;
1174 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully(false /* use_key */, &token2));
1175 EXPECT_NE(token1, token2);
1176 ASSERT_TRUE(RunScript("unregisterServiceWorker()", &script_result));
1177 ASSERT_EQ("service worker unregistration status: true", script_result);
1178 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
1179 EXPECT_EQ("unsubscribe result: false", script_result);
1180 }
1181
1182 // Push subscriptions used to be non-InstanceID GCM registrations. Still need
1183 // to be able to unsubscribe these, even though new ones are no longer created.
1184 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, LegacyUnsubscribeSuccess) {
1185 std::string script_result;
1186
1187 // Resolves true if there was a subscription.
1188 std::string subscription_id1;
1189 ASSERT_NO_FATAL_FAILURE(LegacySubscribeSuccessfully(&subscription_id1));
1190 gcm_service_->AddExpectedUnregisterResponse(gcm::GCMClient::SUCCESS);
1191 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
1192 EXPECT_EQ("unsubscribe result: true", script_result);
1193
1075 // Resolves false if there was no longer a subscription. 1194 // Resolves false if there was no longer a subscription.
1076 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result)); 1195 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
1077 EXPECT_EQ("unsubscribe result: false", script_result); 1196 EXPECT_EQ("unsubscribe result: false", script_result);
1078 1197
1079 // Doesn't reject if there was a network error (deactivates subscription 1198 // Doesn't reject if there was a network error (deactivates subscription
1080 // locally anyway). 1199 // locally anyway).
1081 TryToSubscribeSuccessfully("1-1" /* expected_push_subscription_id */, false); 1200 std::string subscription_id2;
1082 gcm_service()->AddExpectedUnregisterResponse(gcm::GCMClient::NETWORK_ERROR); 1201 ASSERT_NO_FATAL_FAILURE(LegacySubscribeSuccessfully(&subscription_id2));
1202 EXPECT_NE(subscription_id1, subscription_id2);
1203 gcm_service_->AddExpectedUnregisterResponse(gcm::GCMClient::NETWORK_ERROR);
1083 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result)); 1204 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
1084 EXPECT_EQ("unsubscribe result: true", script_result); 1205 EXPECT_EQ("unsubscribe result: true", script_result);
1085 ASSERT_TRUE(RunScript("hasSubscription()", &script_result)); 1206 ASSERT_TRUE(RunScript("hasSubscription()", &script_result));
1086 EXPECT_EQ("false - not subscribed", script_result); 1207 EXPECT_EQ("false - not subscribed", script_result);
1087 1208
1088 // Doesn't reject if there were other push service errors (deactivates 1209 // Doesn't reject if there were other push service errors (deactivates
1089 // subscription locally anyway). 1210 // subscription locally anyway).
1090 TryToSubscribeSuccessfully("1-2" /* expected_push_subscription_id */, false); 1211 std::string subscription_id3;
1091 gcm_service()->AddExpectedUnregisterResponse( 1212 ASSERT_NO_FATAL_FAILURE(LegacySubscribeSuccessfully(&subscription_id3));
1213 EXPECT_NE(subscription_id1, subscription_id3);
1214 EXPECT_NE(subscription_id2, subscription_id3);
1215 gcm_service_->AddExpectedUnregisterResponse(
1092 gcm::GCMClient::INVALID_PARAMETER); 1216 gcm::GCMClient::INVALID_PARAMETER);
1093 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result)); 1217 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
1094 EXPECT_EQ("unsubscribe result: true", script_result); 1218 EXPECT_EQ("unsubscribe result: true", script_result);
1095 1219
1096 // Unsubscribing (with an existing reference to a PushSubscription), after 1220 // Unsubscribing (with an existing reference to a PushSubscription), after
1097 // unregistering the Service Worker, just means push subscription isn't found. 1221 // unregistering the Service Worker, just means push subscription isn't found.
1098 TryToSubscribeSuccessfully("1-3" /* expected_push_subscription_id */, false); 1222 std::string subscription_id4;
1223 ASSERT_NO_FATAL_FAILURE(LegacySubscribeSuccessfully(&subscription_id4));
1224 EXPECT_NE(subscription_id1, subscription_id4);
1225 EXPECT_NE(subscription_id2, subscription_id4);
1226 EXPECT_NE(subscription_id3, subscription_id4);
1099 ASSERT_TRUE(RunScript("unregisterServiceWorker()", &script_result)); 1227 ASSERT_TRUE(RunScript("unregisterServiceWorker()", &script_result));
1100 ASSERT_EQ("service worker unregistration status: true", script_result); 1228 ASSERT_EQ("service worker unregistration status: true", script_result);
1101 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result)); 1229 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
1102 EXPECT_EQ("unsubscribe result: false", script_result); 1230 EXPECT_EQ("unsubscribe result: false", script_result);
1103 } 1231 }
1104 1232
1105 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, 1233 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
1106 GlobalResetPushPermissionUnsubscribes) { 1234 GlobalResetPushPermissionUnsubscribes) {
1107 std::string script_result; 1235 std::string script_result;
1108 1236
1109 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */); 1237 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully());
1110 1238
1111 ASSERT_TRUE(RunScript("hasSubscription()", &script_result)); 1239 ASSERT_TRUE(RunScript("hasSubscription()", &script_result));
1112 EXPECT_EQ("true - subscribed", script_result); 1240 EXPECT_EQ("true - subscribed", script_result);
1113 1241
1114 ASSERT_TRUE(RunScript("permissionState()", &script_result)); 1242 ASSERT_TRUE(RunScript("permissionState()", &script_result));
1115 EXPECT_EQ("permission status - granted", script_result); 1243 EXPECT_EQ("permission status - granted", script_result);
1116 1244
1117 scoped_refptr<content::MessageLoopRunner> message_loop_runner = 1245 scoped_refptr<content::MessageLoopRunner> message_loop_runner =
1118 new content::MessageLoopRunner; 1246 new content::MessageLoopRunner;
1119 push_service()->SetContentSettingChangedCallbackForTesting( 1247 push_service()->SetContentSettingChangedCallbackForTesting(
1120 message_loop_runner->QuitClosure()); 1248 message_loop_runner->QuitClosure());
1121 1249
1122 HostContentSettingsMapFactory::GetForProfile(GetBrowser()->profile()) 1250 HostContentSettingsMapFactory::GetForProfile(GetBrowser()->profile())
1123 ->ClearSettingsForOneType(CONTENT_SETTINGS_TYPE_NOTIFICATIONS); 1251 ->ClearSettingsForOneType(CONTENT_SETTINGS_TYPE_NOTIFICATIONS);
1124 1252
1125 message_loop_runner->Run(); 1253 message_loop_runner->Run();
1126 1254
1127 ASSERT_TRUE(RunScript("permissionState()", &script_result)); 1255 ASSERT_TRUE(RunScript("permissionState()", &script_result));
1128 EXPECT_EQ("permission status - prompt", script_result); 1256 EXPECT_EQ("permission status - prompt", script_result);
1129 1257
1130 ASSERT_TRUE(RunScript("hasSubscription()", &script_result)); 1258 ASSERT_TRUE(RunScript("hasSubscription()", &script_result));
1131 EXPECT_EQ("false - not subscribed", script_result); 1259 EXPECT_EQ("false - not subscribed", script_result);
1132 } 1260 }
1133 1261
1134 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, 1262 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
1135 LocalResetPushPermissionUnsubscribes) { 1263 LocalResetPushPermissionUnsubscribes) {
1136 std::string script_result; 1264 std::string script_result;
1137 1265
1138 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */); 1266 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully());
1139 1267
1140 ASSERT_TRUE(RunScript("hasSubscription()", &script_result)); 1268 ASSERT_TRUE(RunScript("hasSubscription()", &script_result));
1141 EXPECT_EQ("true - subscribed", script_result); 1269 EXPECT_EQ("true - subscribed", script_result);
1142 1270
1143 ASSERT_TRUE(RunScript("permissionState()", &script_result)); 1271 ASSERT_TRUE(RunScript("permissionState()", &script_result));
1144 EXPECT_EQ("permission status - granted", script_result); 1272 EXPECT_EQ("permission status - granted", script_result);
1145 1273
1146 scoped_refptr<content::MessageLoopRunner> message_loop_runner = 1274 scoped_refptr<content::MessageLoopRunner> message_loop_runner =
1147 new content::MessageLoopRunner; 1275 new content::MessageLoopRunner;
1148 push_service()->SetContentSettingChangedCallbackForTesting( 1276 push_service()->SetContentSettingChangedCallbackForTesting(
(...skipping 11 matching lines...) Expand all
1160 EXPECT_EQ("permission status - prompt", script_result); 1288 EXPECT_EQ("permission status - prompt", script_result);
1161 1289
1162 ASSERT_TRUE(RunScript("hasSubscription()", &script_result)); 1290 ASSERT_TRUE(RunScript("hasSubscription()", &script_result));
1163 EXPECT_EQ("false - not subscribed", script_result); 1291 EXPECT_EQ("false - not subscribed", script_result);
1164 } 1292 }
1165 1293
1166 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, 1294 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
1167 DenyPushPermissionUnsubscribes) { 1295 DenyPushPermissionUnsubscribes) {
1168 std::string script_result; 1296 std::string script_result;
1169 1297
1170 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */); 1298 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully());
1171 1299
1172 ASSERT_TRUE(RunScript("hasSubscription()", &script_result)); 1300 ASSERT_TRUE(RunScript("hasSubscription()", &script_result));
1173 EXPECT_EQ("true - subscribed", script_result); 1301 EXPECT_EQ("true - subscribed", script_result);
1174 1302
1175 ASSERT_TRUE(RunScript("permissionState()", &script_result)); 1303 ASSERT_TRUE(RunScript("permissionState()", &script_result));
1176 EXPECT_EQ("permission status - granted", script_result); 1304 EXPECT_EQ("permission status - granted", script_result);
1177 1305
1178 scoped_refptr<content::MessageLoopRunner> message_loop_runner = 1306 scoped_refptr<content::MessageLoopRunner> message_loop_runner =
1179 new content::MessageLoopRunner; 1307 new content::MessageLoopRunner;
1180 push_service()->SetContentSettingChangedCallbackForTesting( 1308 push_service()->SetContentSettingChangedCallbackForTesting(
(...skipping 11 matching lines...) Expand all
1192 EXPECT_EQ("permission status - denied", script_result); 1320 EXPECT_EQ("permission status - denied", script_result);
1193 1321
1194 ASSERT_TRUE(RunScript("hasSubscription()", &script_result)); 1322 ASSERT_TRUE(RunScript("hasSubscription()", &script_result));
1195 EXPECT_EQ("false - not subscribed", script_result); 1323 EXPECT_EQ("false - not subscribed", script_result);
1196 } 1324 }
1197 1325
1198 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, 1326 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
1199 GlobalResetNotificationsPermissionUnsubscribes) { 1327 GlobalResetNotificationsPermissionUnsubscribes) {
1200 std::string script_result; 1328 std::string script_result;
1201 1329
1202 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */); 1330 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully());
1203 1331
1204 ASSERT_TRUE(RunScript("hasSubscription()", &script_result)); 1332 ASSERT_TRUE(RunScript("hasSubscription()", &script_result));
1205 EXPECT_EQ("true - subscribed", script_result); 1333 EXPECT_EQ("true - subscribed", script_result);
1206 1334
1207 ASSERT_TRUE(RunScript("permissionState()", &script_result)); 1335 ASSERT_TRUE(RunScript("permissionState()", &script_result));
1208 EXPECT_EQ("permission status - granted", script_result); 1336 EXPECT_EQ("permission status - granted", script_result);
1209 1337
1210 scoped_refptr<content::MessageLoopRunner> message_loop_runner = 1338 scoped_refptr<content::MessageLoopRunner> message_loop_runner =
1211 new content::MessageLoopRunner; 1339 new content::MessageLoopRunner;
1212 push_service()->SetContentSettingChangedCallbackForTesting( 1340 push_service()->SetContentSettingChangedCallbackForTesting(
1213 message_loop_runner->QuitClosure()); 1341 message_loop_runner->QuitClosure());
1214 1342
1215 HostContentSettingsMapFactory::GetForProfile(GetBrowser()->profile()) 1343 HostContentSettingsMapFactory::GetForProfile(GetBrowser()->profile())
1216 ->ClearSettingsForOneType(CONTENT_SETTINGS_TYPE_NOTIFICATIONS); 1344 ->ClearSettingsForOneType(CONTENT_SETTINGS_TYPE_NOTIFICATIONS);
1217 1345
1218 message_loop_runner->Run(); 1346 message_loop_runner->Run();
1219 1347
1220 ASSERT_TRUE(RunScript("permissionState()", &script_result)); 1348 ASSERT_TRUE(RunScript("permissionState()", &script_result));
1221 EXPECT_EQ("permission status - prompt", script_result); 1349 EXPECT_EQ("permission status - prompt", script_result);
1222 1350
1223 ASSERT_TRUE(RunScript("hasSubscription()", &script_result)); 1351 ASSERT_TRUE(RunScript("hasSubscription()", &script_result));
1224 EXPECT_EQ("false - not subscribed", script_result); 1352 EXPECT_EQ("false - not subscribed", script_result);
1225 } 1353 }
1226 1354
1227 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, 1355 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
1228 LocalResetNotificationsPermissionUnsubscribes) { 1356 LocalResetNotificationsPermissionUnsubscribes) {
1229 std::string script_result; 1357 std::string script_result;
1230 1358
1231 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */); 1359 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully());
1232 1360
1233 ASSERT_TRUE(RunScript("hasSubscription()", &script_result)); 1361 ASSERT_TRUE(RunScript("hasSubscription()", &script_result));
1234 EXPECT_EQ("true - subscribed", script_result); 1362 EXPECT_EQ("true - subscribed", script_result);
1235 1363
1236 ASSERT_TRUE(RunScript("permissionState()", &script_result)); 1364 ASSERT_TRUE(RunScript("permissionState()", &script_result));
1237 EXPECT_EQ("permission status - granted", script_result); 1365 EXPECT_EQ("permission status - granted", script_result);
1238 1366
1239 scoped_refptr<content::MessageLoopRunner> message_loop_runner = 1367 scoped_refptr<content::MessageLoopRunner> message_loop_runner =
1240 new content::MessageLoopRunner; 1368 new content::MessageLoopRunner;
1241 push_service()->SetContentSettingChangedCallbackForTesting( 1369 push_service()->SetContentSettingChangedCallbackForTesting(
(...skipping 11 matching lines...) Expand all
1253 EXPECT_EQ("permission status - prompt", script_result); 1381 EXPECT_EQ("permission status - prompt", script_result);
1254 1382
1255 ASSERT_TRUE(RunScript("hasSubscription()", &script_result)); 1383 ASSERT_TRUE(RunScript("hasSubscription()", &script_result));
1256 EXPECT_EQ("false - not subscribed", script_result); 1384 EXPECT_EQ("false - not subscribed", script_result);
1257 } 1385 }
1258 1386
1259 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, 1387 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
1260 DenyNotificationsPermissionUnsubscribes) { 1388 DenyNotificationsPermissionUnsubscribes) {
1261 std::string script_result; 1389 std::string script_result;
1262 1390
1263 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */); 1391 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully());
1264 1392
1265 ASSERT_TRUE(RunScript("hasSubscription()", &script_result)); 1393 ASSERT_TRUE(RunScript("hasSubscription()", &script_result));
1266 EXPECT_EQ("true - subscribed", script_result); 1394 EXPECT_EQ("true - subscribed", script_result);
1267 1395
1268 ASSERT_TRUE(RunScript("permissionState()", &script_result)); 1396 ASSERT_TRUE(RunScript("permissionState()", &script_result));
1269 EXPECT_EQ("permission status - granted", script_result); 1397 EXPECT_EQ("permission status - granted", script_result);
1270 1398
1271 scoped_refptr<content::MessageLoopRunner> message_loop_runner = 1399 scoped_refptr<content::MessageLoopRunner> message_loop_runner =
1272 new content::MessageLoopRunner; 1400 new content::MessageLoopRunner;
1273 push_service()->SetContentSettingChangedCallbackForTesting( 1401 push_service()->SetContentSettingChangedCallbackForTesting(
(...skipping 11 matching lines...) Expand all
1285 EXPECT_EQ("permission status - denied", script_result); 1413 EXPECT_EQ("permission status - denied", script_result);
1286 1414
1287 ASSERT_TRUE(RunScript("hasSubscription()", &script_result)); 1415 ASSERT_TRUE(RunScript("hasSubscription()", &script_result));
1288 EXPECT_EQ("false - not subscribed", script_result); 1416 EXPECT_EQ("false - not subscribed", script_result);
1289 } 1417 }
1290 1418
1291 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, 1419 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
1292 GrantAlreadyGrantedPermissionDoesNotUnsubscribe) { 1420 GrantAlreadyGrantedPermissionDoesNotUnsubscribe) {
1293 std::string script_result; 1421 std::string script_result;
1294 1422
1295 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */); 1423 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully());
1296 1424
1297 ASSERT_TRUE(RunScript("hasSubscription()", &script_result)); 1425 ASSERT_TRUE(RunScript("hasSubscription()", &script_result));
1298 EXPECT_EQ("true - subscribed", script_result); 1426 EXPECT_EQ("true - subscribed", script_result);
1299 1427
1300 ASSERT_TRUE(RunScript("permissionState()", &script_result)); 1428 ASSERT_TRUE(RunScript("permissionState()", &script_result));
1301 EXPECT_EQ("permission status - granted", script_result); 1429 EXPECT_EQ("permission status - granted", script_result);
1302 1430
1303 scoped_refptr<content::MessageLoopRunner> message_loop_runner = 1431 scoped_refptr<content::MessageLoopRunner> message_loop_runner =
1304 new content::MessageLoopRunner; 1432 new content::MessageLoopRunner;
1305 push_service()->SetContentSettingChangedCallbackForTesting( 1433 push_service()->SetContentSettingChangedCallbackForTesting(
(...skipping 15 matching lines...) Expand all
1321 } 1449 }
1322 1450
1323 // This test is testing some non-trivial content settings rules and make sure 1451 // This test is testing some non-trivial content settings rules and make sure
1324 // that they are respected with regards to automatic unsubscription. In other 1452 // that they are respected with regards to automatic unsubscription. In other
1325 // words, it checks that the push service does not end up unsubscribing origins 1453 // words, it checks that the push service does not end up unsubscribing origins
1326 // that have push permission with some non-common rules. 1454 // that have push permission with some non-common rules.
1327 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, 1455 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
1328 AutomaticUnsubscriptionFollowsContentSettingRules) { 1456 AutomaticUnsubscriptionFollowsContentSettingRules) {
1329 std::string script_result; 1457 std::string script_result;
1330 1458
1331 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */); 1459 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully());
1332 1460
1333 ASSERT_TRUE(RunScript("hasSubscription()", &script_result)); 1461 ASSERT_TRUE(RunScript("hasSubscription()", &script_result));
1334 EXPECT_EQ("true - subscribed", script_result); 1462 EXPECT_EQ("true - subscribed", script_result);
1335 1463
1336 ASSERT_TRUE(RunScript("permissionState()", &script_result)); 1464 ASSERT_TRUE(RunScript("permissionState()", &script_result));
1337 EXPECT_EQ("permission status - granted", script_result); 1465 EXPECT_EQ("permission status - granted", script_result);
1338 1466
1339 scoped_refptr<content::MessageLoopRunner> message_loop_runner = 1467 scoped_refptr<content::MessageLoopRunner> message_loop_runner =
1340 new content::MessageLoopRunner; 1468 new content::MessageLoopRunner;
1341 push_service()->SetContentSettingChangedCallbackForTesting( 1469 push_service()->SetContentSettingChangedCallbackForTesting(
(...skipping 15 matching lines...) Expand all
1357 // The Push service should not unsubcribe |origin| because at no point it was 1485 // The Push service should not unsubcribe |origin| because at no point it was
1358 // left without permission to use Push. 1486 // left without permission to use Push.
1359 1487
1360 ASSERT_TRUE(RunScript("permissionState()", &script_result)); 1488 ASSERT_TRUE(RunScript("permissionState()", &script_result));
1361 EXPECT_EQ("permission status - granted", script_result); 1489 EXPECT_EQ("permission status - granted", script_result);
1362 1490
1363 ASSERT_TRUE(RunScript("hasSubscription()", &script_result)); 1491 ASSERT_TRUE(RunScript("hasSubscription()", &script_result));
1364 EXPECT_EQ("true - subscribed", script_result); 1492 EXPECT_EQ("true - subscribed", script_result);
1365 } 1493 }
1366 1494
1367 // Checks that automatically unsubscribing due to a revoked permission is 1495 // Checks automatically unsubscribing due to a revoked permission after
1368 // handled well if the sender ID needed to unsubscribe was already deleted. 1496 // previously clearing site data.
1369 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, 1497 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
1370 ResetPushPermissionAfterClearingSiteData) { 1498 ResetPushPermissionAfterClearingSiteData) {
1371 std::string script_result; 1499 std::string script_result;
1372 1500
1373 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */); 1501 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully());
1374 1502
1375 PushMessagingAppIdentifier app_identifier = 1503 PushMessagingAppIdentifier app_identifier =
1376 GetAppIdentifierForServiceWorkerRegistration(0LL); 1504 GetAppIdentifierForServiceWorkerRegistration(0LL);
1377 EXPECT_EQ(app_identifier.app_id(), gcm_service()->last_registered_app_id());
1378 PushMessagingAppIdentifier stored_app_identifier = 1505 PushMessagingAppIdentifier stored_app_identifier =
1379 PushMessagingAppIdentifier::FindByAppId(GetBrowser()->profile(), 1506 PushMessagingAppIdentifier::FindByAppId(GetBrowser()->profile(),
1380 app_identifier.app_id()); 1507 app_identifier.app_id());
1381 EXPECT_FALSE(stored_app_identifier.is_null()); 1508 EXPECT_FALSE(stored_app_identifier.is_null());
1382 1509
1383 // Simulate a user clearing site data (including Service Workers, crucially). 1510 // Simulate a user clearing site data (including Service Workers, crucially).
1384 BrowsingDataRemover* remover = 1511 BrowsingDataRemover* remover =
1385 BrowsingDataRemoverFactory::GetForBrowserContext(GetBrowser()->profile()); 1512 BrowsingDataRemoverFactory::GetForBrowserContext(GetBrowser()->profile());
1386 BrowsingDataRemoverCompletionObserver observer(remover); 1513 BrowsingDataRemoverCompletionObserver observer(remover);
1387 remover->RemoveAndReply(BrowsingDataRemover::Unbounded(), 1514 remover->RemoveAndReply(BrowsingDataRemover::Unbounded(),
1388 BrowsingDataRemover::REMOVE_SITE_DATA, 1515 BrowsingDataRemover::REMOVE_SITE_DATA,
1389 BrowsingDataHelper::UNPROTECTED_WEB, &observer); 1516 BrowsingDataHelper::UNPROTECTED_WEB, &observer);
1390 observer.BlockUntilCompletion(); 1517 observer.BlockUntilCompletion();
1391 1518
1392 base::RunLoop run_loop; 1519 base::RunLoop run_loop;
1393 push_service()->SetContentSettingChangedCallbackForTesting( 1520 push_service()->SetContentSettingChangedCallbackForTesting(
1394 run_loop.QuitClosure()); 1521 run_loop.QuitClosure());
1395 1522
1396 // This shouldn't (asynchronously) cause a DCHECK. 1523 // This shouldn't (asynchronously) cause a DCHECK.
1397 // TODO(johnme): Get this test running on Android, which has a different 1524 // TODO(johnme): Get this test running on Android with legacy GCM
1398 // codepath due to sender_id being required for unsubscribing there. 1525 // registrations, which have a different codepath due to sender_id being
1526 // required for unsubscribing there.
1399 HostContentSettingsMapFactory::GetForProfile(GetBrowser()->profile()) 1527 HostContentSettingsMapFactory::GetForProfile(GetBrowser()->profile())
1400 ->ClearSettingsForOneType(CONTENT_SETTINGS_TYPE_NOTIFICATIONS); 1528 ->ClearSettingsForOneType(CONTENT_SETTINGS_TYPE_NOTIFICATIONS);
1401 1529
1402 run_loop.Run(); 1530 run_loop.Run();
1403 1531
1404 // |app_identifier| should no longer be stored in prefs. 1532 // |app_identifier| should no longer be stored in prefs.
1405 PushMessagingAppIdentifier stored_app_identifier2 = 1533 PushMessagingAppIdentifier stored_app_identifier2 =
1406 PushMessagingAppIdentifier::FindByAppId(GetBrowser()->profile(), 1534 PushMessagingAppIdentifier::FindByAppId(GetBrowser()->profile(),
1407 app_identifier.app_id()); 1535 app_identifier.app_id());
1408 EXPECT_TRUE(stored_app_identifier2.is_null()); 1536 EXPECT_TRUE(stored_app_identifier2.is_null());
1409 } 1537 }
1410 1538
1411 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, EncryptionKeyUniqueness) { 1539 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, EncryptionKeyUniqueness) {
1412 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */, false); 1540 std::string token1;
1541 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully(false /* use_key */, &token1));
1413 1542
1414 std::string first_public_key; 1543 std::string first_public_key;
1415 ASSERT_TRUE(RunScript("GetP256dh()", &first_public_key)); 1544 ASSERT_TRUE(RunScript("GetP256dh()", &first_public_key));
1416 EXPECT_GE(first_public_key.size(), 32u); 1545 EXPECT_GE(first_public_key.size(), 32u);
1417 1546
1418 std::string script_result; 1547 std::string script_result;
1419 gcm_service()->AddExpectedUnregisterResponse(gcm::GCMClient::SUCCESS);
1420 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result)); 1548 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
1421 EXPECT_EQ("unsubscribe result: true", script_result); 1549 EXPECT_EQ("unsubscribe result: true", script_result);
1422 1550
1423 TryToSubscribeSuccessfully("1-1" /* expected_push_subscription_id */); 1551 std::string token2;
1552 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully(true /* use_key */, &token2));
1553 EXPECT_NE(token1, token2);
1424 1554
1425 std::string second_public_key; 1555 std::string second_public_key;
1426 ASSERT_TRUE(RunScript("GetP256dh()", &second_public_key)); 1556 ASSERT_TRUE(RunScript("GetP256dh()", &second_public_key));
1427 EXPECT_GE(second_public_key.size(), 32u); 1557 EXPECT_GE(second_public_key.size(), 32u);
1428 1558
1429 EXPECT_NE(first_public_key, second_public_key); 1559 EXPECT_NE(first_public_key, second_public_key);
1430 } 1560 }
1431 1561
1432 class PushMessagingIncognitoBrowserTest : public PushMessagingBrowserTest { 1562 class PushMessagingIncognitoBrowserTest : public PushMessagingBrowserTest {
1433 public: 1563 public:
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
1465 #if BUILDFLAG(ENABLE_BACKGROUND) && !defined(OS_CHROMEOS) 1595 #if BUILDFLAG(ENABLE_BACKGROUND) && !defined(OS_CHROMEOS)
1466 // Push background mode is disabled by default. 1596 // Push background mode is disabled by default.
1467 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, 1597 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
1468 BackgroundModeDisabledByDefault) { 1598 BackgroundModeDisabledByDefault) {
1469 // Initially background mode is inactive. 1599 // Initially background mode is inactive.
1470 BackgroundModeManager* background_mode_manager = 1600 BackgroundModeManager* background_mode_manager =
1471 g_browser_process->background_mode_manager(); 1601 g_browser_process->background_mode_manager();
1472 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive()); 1602 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive());
1473 1603
1474 // Once there is a push subscription background mode is still inactive. 1604 // Once there is a push subscription background mode is still inactive.
1475 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */); 1605 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully());
1476 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive()); 1606 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive());
1477 1607
1478 // After dropping the last subscription it is still inactive. 1608 // After dropping the last subscription it is still inactive.
1479 std::string script_result; 1609 std::string script_result;
1480 gcm_service()->AddExpectedUnregisterResponse(gcm::GCMClient::SUCCESS);
1481 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result)); 1610 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
1482 EXPECT_EQ("unsubscribe result: true", script_result); 1611 EXPECT_EQ("unsubscribe result: true", script_result);
1483 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive()); 1612 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive());
1484 } 1613 }
1485 1614
1486 class PushMessagingBackgroundModeEnabledBrowserTest 1615 class PushMessagingBackgroundModeEnabledBrowserTest
1487 : public PushMessagingBrowserTest { 1616 : public PushMessagingBrowserTest {
1488 public: 1617 public:
1489 ~PushMessagingBackgroundModeEnabledBrowserTest() override {} 1618 ~PushMessagingBackgroundModeEnabledBrowserTest() override {}
1490 1619
1491 // PushMessagingBrowserTest: 1620 // PushMessagingBrowserTest:
1492 void SetUpCommandLine(base::CommandLine* command_line) override { 1621 void SetUpCommandLine(base::CommandLine* command_line) override {
1493 command_line->AppendSwitch(switches::kEnablePushApiBackgroundMode); 1622 command_line->AppendSwitch(switches::kEnablePushApiBackgroundMode);
1494 PushMessagingBrowserTest::SetUpCommandLine(command_line); 1623 PushMessagingBrowserTest::SetUpCommandLine(command_line);
1495 } 1624 }
1496 }; 1625 };
1497 1626
1498 // In this test the command line enables push background mode. 1627 // In this test the command line enables push background mode.
1499 IN_PROC_BROWSER_TEST_F(PushMessagingBackgroundModeEnabledBrowserTest, 1628 IN_PROC_BROWSER_TEST_F(PushMessagingBackgroundModeEnabledBrowserTest,
1500 BackgroundModeEnabledWithCommandLine) { 1629 BackgroundModeEnabledWithCommandLine) {
1501 // Initially background mode is inactive. 1630 // Initially background mode is inactive.
1502 BackgroundModeManager* background_mode_manager = 1631 BackgroundModeManager* background_mode_manager =
1503 g_browser_process->background_mode_manager(); 1632 g_browser_process->background_mode_manager();
1504 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive()); 1633 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive());
1505 1634
1506 // Once there is a push subscription background mode is active. 1635 // Once there is a push subscription background mode is active.
1507 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */); 1636 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully());
1508 ASSERT_TRUE(background_mode_manager->IsBackgroundModeActive()); 1637 ASSERT_TRUE(background_mode_manager->IsBackgroundModeActive());
1509 1638
1510 // Dropping the last subscription deactivates background mode. 1639 // Dropping the last subscription deactivates background mode.
1511 std::string script_result; 1640 std::string script_result;
1512 gcm_service()->AddExpectedUnregisterResponse(gcm::GCMClient::SUCCESS);
1513 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result)); 1641 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
1514 EXPECT_EQ("unsubscribe result: true", script_result); 1642 EXPECT_EQ("unsubscribe result: true", script_result);
1515 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive()); 1643 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive());
1516 } 1644 }
1517 1645
1518 class PushMessagingBackgroundModeDisabledBrowserTest 1646 class PushMessagingBackgroundModeDisabledBrowserTest
1519 : public PushMessagingBrowserTest { 1647 : public PushMessagingBrowserTest {
1520 public: 1648 public:
1521 ~PushMessagingBackgroundModeDisabledBrowserTest() override {} 1649 ~PushMessagingBackgroundModeDisabledBrowserTest() override {}
1522 1650
1523 // PushMessagingBrowserTest: 1651 // PushMessagingBrowserTest:
1524 void SetUpCommandLine(base::CommandLine* command_line) override { 1652 void SetUpCommandLine(base::CommandLine* command_line) override {
1525 command_line->AppendSwitch(switches::kDisablePushApiBackgroundMode); 1653 command_line->AppendSwitch(switches::kDisablePushApiBackgroundMode);
1526 PushMessagingBrowserTest::SetUpCommandLine(command_line); 1654 PushMessagingBrowserTest::SetUpCommandLine(command_line);
1527 } 1655 }
1528 }; 1656 };
1529 1657
1530 // In this test the command line disables push background mode. 1658 // In this test the command line disables push background mode.
1531 IN_PROC_BROWSER_TEST_F(PushMessagingBackgroundModeDisabledBrowserTest, 1659 IN_PROC_BROWSER_TEST_F(PushMessagingBackgroundModeDisabledBrowserTest,
1532 BackgroundModeDisabledWithCommandLine) { 1660 BackgroundModeDisabledWithCommandLine) {
1533 // Initially background mode is inactive. 1661 // Initially background mode is inactive.
1534 BackgroundModeManager* background_mode_manager = 1662 BackgroundModeManager* background_mode_manager =
1535 g_browser_process->background_mode_manager(); 1663 g_browser_process->background_mode_manager();
1536 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive()); 1664 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive());
1537 1665
1538 // Once there is a push subscription background mode is still inactive. 1666 // Once there is a push subscription background mode is still inactive.
1539 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */); 1667 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully());
1540 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive()); 1668 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive());
1541 1669
1542 // After dropping the last subscription background mode is still inactive. 1670 // After dropping the last subscription background mode is still inactive.
1543 std::string script_result; 1671 std::string script_result;
1544 gcm_service()->AddExpectedUnregisterResponse(gcm::GCMClient::SUCCESS);
1545 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result)); 1672 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
1546 EXPECT_EQ("unsubscribe result: true", script_result); 1673 EXPECT_EQ("unsubscribe result: true", script_result);
1547 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive()); 1674 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive());
1548 } 1675 }
1549 #endif // BUILDFLAG(ENABLE_BACKGROUND) && !defined(OS_CHROMEOS) 1676 #endif // BUILDFLAG(ENABLE_BACKGROUND) && !defined(OS_CHROMEOS)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698