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

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: Address review comments, and rewrite following rebase (e.g. no longer depends on NestedMessagePumpA… Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <stddef.h> 5 #include <stddef.h>
6 #include <stdint.h> 6 #include <stdint.h>
7 7
8 #include <map> 8 #include <map>
9 #include <string> 9 #include <string>
10 10
(...skipping 25 matching lines...) Expand all
36 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h" 36 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h"
37 #include "chrome/common/chrome_switches.h" 37 #include "chrome/common/chrome_switches.h"
38 #include "chrome/common/features.h" 38 #include "chrome/common/features.h"
39 #include "chrome/test/base/in_process_browser_test.h" 39 #include "chrome/test/base/in_process_browser_test.h"
40 #include "chrome/test/base/ui_test_utils.h" 40 #include "chrome/test/base/ui_test_utils.h"
41 #include "components/content_settings/core/browser/host_content_settings_map.h" 41 #include "components/content_settings/core/browser/host_content_settings_map.h"
42 #include "components/content_settings/core/common/content_settings.h" 42 #include "components/content_settings/core/common/content_settings.h"
43 #include "components/content_settings/core/common/content_settings_types.h" 43 #include "components/content_settings/core/common/content_settings_types.h"
44 #include "components/gcm_driver/common/gcm_messages.h" 44 #include "components/gcm_driver/common/gcm_messages.h"
45 #include "components/gcm_driver/gcm_client.h" 45 #include "components/gcm_driver/gcm_client.h"
46 #include "components/gcm_driver/instance_id/fake_gcm_driver_for_instance_id.h"
46 #include "content/public/browser/web_contents.h" 47 #include "content/public/browser/web_contents.h"
47 #include "content/public/common/content_switches.h" 48 #include "content/public/common/content_switches.h"
48 #include "content/public/test/browser_test_utils.h" 49 #include "content/public/test/browser_test_utils.h"
49 #include "content/public/test/test_utils.h" 50 #include "content/public/test/test_utils.h"
50 #include "net/test/embedded_test_server/embedded_test_server.h" 51 #include "net/test/embedded_test_server/embedded_test_server.h"
52 #include "testing/gmock/include/gmock/gmock.h"
51 #include "ui/base/window_open_disposition.h" 53 #include "ui/base/window_open_disposition.h"
52 54
53 #if BUILDFLAG(ENABLE_BACKGROUND) 55 #if BUILDFLAG(ENABLE_BACKGROUND)
54 #include "chrome/browser/background/background_mode_manager.h" 56 #include "chrome/browser/background/background_mode_manager.h"
55 #endif 57 #endif
56 58
57 namespace { 59 namespace {
58 60
59 // NIST P-256 public key made available to tests. Must be an uncompressed 61 // NIST P-256 public key made available to tests. Must be an uncompressed
60 // point in accordance with SEC1 2.3.3. 62 // point in accordance with SEC1 2.3.3.
61 const uint8_t kApplicationServerKey[65] = { 63 const uint8_t kApplicationServerKey[65] = {
62 0x04, 0x55, 0x52, 0x6A, 0xA5, 0x6E, 0x8E, 0xAA, 0x47, 0x97, 0x36, 64 0x04, 0x55, 0x52, 0x6A, 0xA5, 0x6E, 0x8E, 0xAA, 0x47, 0x97, 0x36,
63 0x10, 0xC1, 0x66, 0x3C, 0x1E, 0x65, 0xBF, 0xA1, 0x7B, 0xEE, 0x48, 65 0x10, 0xC1, 0x66, 0x3C, 0x1E, 0x65, 0xBF, 0xA1, 0x7B, 0xEE, 0x48,
64 0xC9, 0xC6, 0xBB, 0xBF, 0x02, 0x18, 0x53, 0x72, 0x1D, 0x0C, 0x7B, 66 0xC9, 0xC6, 0xBB, 0xBF, 0x02, 0x18, 0x53, 0x72, 0x1D, 0x0C, 0x7B,
65 0xA9, 0xE3, 0x11, 0xB7, 0x03, 0x52, 0x21, 0xD3, 0x71, 0x90, 0x13, 67 0xA9, 0xE3, 0x11, 0xB7, 0x03, 0x52, 0x21, 0xD3, 0x71, 0x90, 0x13,
66 0xA8, 0xC1, 0xCF, 0xED, 0x20, 0xF7, 0x1F, 0xD1, 0x7F, 0xF2, 0x76, 68 0xA8, 0xC1, 0xCF, 0xED, 0x20, 0xF7, 0x1F, 0xD1, 0x7F, 0xF2, 0x76,
67 0xB6, 0x01, 0x20, 0xD8, 0x35, 0xA5, 0xD9, 0x3C, 0x43, 0xFD}; 69 0xB6, 0x01, 0x20, 0xD8, 0x35, 0xA5, 0xD9, 0x3C, 0x43, 0xFD};
68 70
69 // URL-safe base64 encoded version of the |kApplicationServerKey|. 71 // URL-safe base64 encoded version of the |kApplicationServerKey|.
70 const char kEncodedApplicationServerKey[] = 72 const char kEncodedApplicationServerKey[] =
71 "BFVSaqVujqpHlzYQwWY8HmW_oXvuSMnGu78CGFNyHQx7qeMRtwNSIdNxkBOowc_tIPcf0X_ydr" 73 "BFVSaqVujqpHlzYQwWY8HmW_oXvuSMnGu78CGFNyHQx7qeMRtwNSIdNxkBOowc_tIPcf0X_ydr"
72 "YBINg1pdk8Q_0"; 74 "YBINg1pdk8Q_0";
73 75
74 std::string GetTestApplicationServerKey() { 76 std::string GetTestApplicationServerKey() {
75 return std::string(kApplicationServerKey, 77 return std::string(kApplicationServerKey,
76 kApplicationServerKey + arraysize(kApplicationServerKey)); 78 kApplicationServerKey + arraysize(kApplicationServerKey));
77 } 79 }
78 80
79 // Class to instantiate on the stack that is meant to be used with 81 const char kManifestSenderId[] = "1234567890";
Peter Beverloo 2016/04/21 13:26:18 micro nit: Please group the constants together (i.
johnme 2016/05/25 14:11:50 Done (moved to line 61)
80 // FakeGCMProfileService. The ::Run() method follows the signature of
81 // FakeGCMProfileService::UnregisterCallback.
82 class UnregistrationCallback {
83 public:
84 UnregistrationCallback()
85 : message_loop_runner_(new content::MessageLoopRunner) {}
86
87 void Run(const std::string& app_id) {
88 app_id_ = app_id;
89 message_loop_runner_->Quit();
90 }
91
92 void WaitUntilSatisfied() { message_loop_runner_->Run(); }
93
94 const std::string& app_id() { return app_id_; }
95
96 private:
97 scoped_refptr<content::MessageLoopRunner> message_loop_runner_;
98 std::string app_id_;
99 };
100 82
101 } // namespace 83 } // namespace
102 84
103 class PushMessagingBrowserTest : public InProcessBrowserTest { 85 class PushMessagingBrowserTest : public InProcessBrowserTest {
104 public: 86 public:
105 PushMessagingBrowserTest() : gcm_service_(nullptr) {} 87 PushMessagingBrowserTest() : gcm_driver_(nullptr) {}
106 ~PushMessagingBrowserTest() override {} 88 ~PushMessagingBrowserTest() override {}
107 89
108 // InProcessBrowserTest: 90 // InProcessBrowserTest:
109 void SetUp() override { 91 void SetUp() override {
110 https_server_.reset( 92 https_server_.reset(
111 new net::EmbeddedTestServer(net::EmbeddedTestServer::TYPE_HTTPS)); 93 new net::EmbeddedTestServer(net::EmbeddedTestServer::TYPE_HTTPS));
112 https_server_->ServeFilesFromSourceDirectory("chrome/test/data"); 94 https_server_->ServeFilesFromSourceDirectory("chrome/test/data");
113 ASSERT_TRUE(https_server_->Start()); 95 ASSERT_TRUE(https_server_->Start());
114 96
115 #if defined(ENABLE_NOTIFICATIONS) 97 #if defined(ENABLE_NOTIFICATIONS)
116 notification_manager_.reset(new StubNotificationUIManager); 98 notification_manager_.reset(new StubNotificationUIManager);
117 notification_service()->SetNotificationUIManagerForTesting( 99 notification_service()->SetNotificationUIManagerForTesting(
118 notification_manager()); 100 notification_manager());
119 #endif 101 #endif
120 102
121 InProcessBrowserTest::SetUp(); 103 InProcessBrowserTest::SetUp();
122 } 104 }
123 105
124 void SetUpCommandLine(base::CommandLine* command_line) override { 106 void SetUpCommandLine(base::CommandLine* command_line) override {
125 // Enable experimental features for subscription restrictions. 107 // Enable experimental features for subscription restrictions.
126 command_line->AppendSwitch( 108 command_line->AppendSwitch(
127 switches::kEnableExperimentalWebPlatformFeatures); 109 switches::kEnableExperimentalWebPlatformFeatures);
128 InProcessBrowserTest::SetUpCommandLine(command_line); 110 InProcessBrowserTest::SetUpCommandLine(command_line);
129 } 111 }
130 112
131 // InProcessBrowserTest: 113 // InProcessBrowserTest:
132 void SetUpOnMainThread() override { 114 void SetUpOnMainThread() override {
133 gcm_service_ = static_cast<gcm::FakeGCMProfileService*>( 115 gcm::GCMProfileService* gcm_service = static_cast<gcm::GCMProfileService*>(
134 gcm::GCMProfileServiceFactory::GetInstance()->SetTestingFactoryAndUse( 116 gcm::GCMProfileServiceFactory::GetInstance()->SetTestingFactoryAndUse(
135 GetBrowser()->profile(), &gcm::FakeGCMProfileService::Build)); 117 GetBrowser()->profile(), &gcm::FakeGCMProfileService::Build));
136 gcm_service_->set_collect(true); 118 gcm_driver_ = static_cast<instance_id::FakeGCMDriverForInstanceID*>(
119 gcm_service->driver());
120
137 push_service_ = 121 push_service_ =
138 PushMessagingServiceFactory::GetForProfile(GetBrowser()->profile()); 122 PushMessagingServiceFactory::GetForProfile(GetBrowser()->profile());
139 123
140 LoadTestPage(); 124 LoadTestPage();
141 125
142 InProcessBrowserTest::SetUpOnMainThread(); 126 InProcessBrowserTest::SetUpOnMainThread();
143 } 127 }
144 128
145 void RestartPushService() { 129 void RestartPushService() {
146 Profile* profile = GetBrowser()->profile(); 130 Profile* profile = GetBrowser()->profile();
(...skipping 26 matching lines...) Expand all
173 157
174 bool RunScript(const std::string& script, std::string* result, 158 bool RunScript(const std::string& script, std::string* result,
175 content::WebContents* web_contents) { 159 content::WebContents* web_contents) {
176 if (!web_contents) 160 if (!web_contents)
177 web_contents = GetBrowser()->tab_strip_model()->GetActiveWebContents(); 161 web_contents = GetBrowser()->tab_strip_model()->GetActiveWebContents();
178 return content::ExecuteScriptAndExtractString(web_contents->GetMainFrame(), 162 return content::ExecuteScriptAndExtractString(web_contents->GetMainFrame(),
179 script, result); 163 script, result);
180 } 164 }
181 165
182 gcm::GCMAppHandler* GetAppHandler() { 166 gcm::GCMAppHandler* GetAppHandler() {
183 return gcm_service()->driver()->GetAppHandler( 167 return gcm_driver_->GetAppHandler(kPushMessagingAppIdentifierPrefix);
184 kPushMessagingAppIdentifierPrefix);
185 } 168 }
186 169
187 PermissionBubbleManager* GetPermissionBubbleManager() { 170 PermissionBubbleManager* GetPermissionBubbleManager() {
188 return PermissionBubbleManager::FromWebContents( 171 return PermissionBubbleManager::FromWebContents(
189 GetBrowser()->tab_strip_model()->GetActiveWebContents()); 172 GetBrowser()->tab_strip_model()->GetActiveWebContents());
190 } 173 }
191 174
192 void RequestAndAcceptPermission(); 175 void RequestAndAcceptPermission();
193 void RequestAndDenyPermission(); 176 void RequestAndDenyPermission();
194 177
195 void TryToSubscribeSuccessfully( 178 // Sets out_token to the subscription token (not including server URL).
Peter Beverloo 2016/04/21 13:26:18 // Calls should be wrapped in the ASSERT_NO_FATAL_
johnme 2016/05/25 14:11:50 Done.
196 const std::string& expected_push_subscription_info, 179 void SubscribeSuccessfully(bool use_key = true,
197 bool use_key = true); 180 std::string* out_token = nullptr);
198 181
199 std::string GetEndpointForSubscriptionId(const std::string& subscription_id) { 182 // Strips server URL from a registration endpoint to get subscription token.
200 return std::string(kPushMessagingEndpoint) + "/" + subscription_id; 183 void EndpointToToken(const std::string& endpoint,
201 } 184 std::string* out_token = nullptr);
202 185
203 PushMessagingAppIdentifier GetAppIdentifierForServiceWorkerRegistration( 186 PushMessagingAppIdentifier GetAppIdentifierForServiceWorkerRegistration(
204 int64_t service_worker_registration_id); 187 int64_t service_worker_registration_id);
205 188
206 void SendMessageAndWaitUntilHandled( 189 void SendMessageAndWaitUntilHandled(
207 const PushMessagingAppIdentifier& app_identifier, 190 const PushMessagingAppIdentifier& app_identifier,
208 const gcm::IncomingMessage& message); 191 const gcm::IncomingMessage& message);
209 192
210 net::EmbeddedTestServer* https_server() const { return https_server_.get(); } 193 net::EmbeddedTestServer* https_server() const { return https_server_.get(); }
211 194
212 gcm::FakeGCMProfileService* gcm_service() const { return gcm_service_; }
213
214 #if defined(ENABLE_NOTIFICATIONS) 195 #if defined(ENABLE_NOTIFICATIONS)
215 // To be called when delivery of a push message has finished. The |run_loop| 196 // To be called when delivery of a push message has finished. The |run_loop|
216 // will be told to quit after |messages_required| messages were received. 197 // will be told to quit after |messages_required| messages were received.
217 void OnDeliveryFinished(std::vector<size_t>* number_of_notifications_shown, 198 void OnDeliveryFinished(std::vector<size_t>* number_of_notifications_shown,
218 const base::Closure& done_closure) { 199 const base::Closure& done_closure) {
219 DCHECK(number_of_notifications_shown); 200 DCHECK(number_of_notifications_shown);
220 201
221 number_of_notifications_shown->push_back( 202 number_of_notifications_shown->push_back(
222 notification_manager_->GetNotificationCount()); 203 notification_manager_->GetNotificationCount());
223 204
224 done_closure.Run(); 205 done_closure.Run();
225 } 206 }
226 207
227 StubNotificationUIManager* notification_manager() const { 208 StubNotificationUIManager* notification_manager() const {
228 return notification_manager_.get(); 209 return notification_manager_.get();
229 } 210 }
230 211
231 PlatformNotificationServiceImpl* notification_service() const { 212 PlatformNotificationServiceImpl* notification_service() const {
232 return PlatformNotificationServiceImpl::GetInstance(); 213 return PlatformNotificationServiceImpl::GetInstance();
233 } 214 }
234 #endif 215 #endif
235 216
236 PushMessagingServiceImpl* push_service() const { return push_service_; } 217 PushMessagingServiceImpl* push_service() const { return push_service_; }
237 218
238 protected: 219 protected:
239 virtual std::string GetTestURL() { return "/push_messaging/test.html"; } 220 virtual std::string GetTestURL() { return "/push_messaging/test.html"; }
240 221
241 virtual Browser* GetBrowser() const { return browser(); } 222 virtual Browser* GetBrowser() const { return browser(); }
242 223
243 private: 224 protected:
244 scoped_ptr<net::EmbeddedTestServer> https_server_; 225 scoped_ptr<net::EmbeddedTestServer> https_server_;
245 gcm::FakeGCMProfileService* gcm_service_; 226 instance_id::FakeGCMDriverForInstanceID* gcm_driver_;
246 PushMessagingServiceImpl* push_service_; 227 PushMessagingServiceImpl* push_service_;
247 228
248 #if defined(ENABLE_NOTIFICATIONS) 229 #if defined(ENABLE_NOTIFICATIONS)
249 scoped_ptr<StubNotificationUIManager> notification_manager_; 230 scoped_ptr<StubNotificationUIManager> notification_manager_;
250 #endif 231 #endif
251 232
252 DISALLOW_COPY_AND_ASSIGN(PushMessagingBrowserTest); 233 DISALLOW_COPY_AND_ASSIGN(PushMessagingBrowserTest);
253 }; 234 };
254 235
255 class PushMessagingBrowserTestEmptySubscriptionOptions 236 class PushMessagingBrowserTestEmptySubscriptionOptions
256 : public PushMessagingBrowserTest { 237 : public PushMessagingBrowserTest {
257 std::string GetTestURL() override { 238 std::string GetTestURL() override {
258 return "/push_messaging/test_no_subscription_options.html"; 239 return "/push_messaging/test_no_subscription_options.html";
259 } 240 }
260 }; 241 };
261 242
262 void PushMessagingBrowserTest::RequestAndAcceptPermission() { 243 void PushMessagingBrowserTest::RequestAndAcceptPermission() {
263 std::string script_result; 244 std::string script_result;
264 GetPermissionBubbleManager()->set_auto_response_for_test( 245 GetPermissionBubbleManager()->set_auto_response_for_test(
265 PermissionBubbleManager::ACCEPT_ALL); 246 PermissionBubbleManager::ACCEPT_ALL);
266 EXPECT_TRUE(RunScript("requestNotificationPermission();", &script_result)); 247 ASSERT_TRUE(RunScript("requestNotificationPermission();", &script_result));
267 EXPECT_EQ("permission status - granted", script_result); 248 ASSERT_EQ("permission status - granted", script_result);
268 } 249 }
269 250
270 void PushMessagingBrowserTest::RequestAndDenyPermission() { 251 void PushMessagingBrowserTest::RequestAndDenyPermission() {
271 std::string script_result; 252 std::string script_result;
272 GetPermissionBubbleManager()->set_auto_response_for_test( 253 GetPermissionBubbleManager()->set_auto_response_for_test(
273 PermissionBubbleManager::DENY_ALL); 254 PermissionBubbleManager::DENY_ALL);
274 EXPECT_TRUE(RunScript("requestNotificationPermission();", &script_result)); 255 ASSERT_TRUE(RunScript("requestNotificationPermission();", &script_result));
275 EXPECT_EQ("permission status - denied", script_result); 256 ASSERT_EQ("permission status - denied", script_result);
276 } 257 }
277 258
278 void PushMessagingBrowserTest::TryToSubscribeSuccessfully( 259 void PushMessagingBrowserTest::SubscribeSuccessfully(bool use_key,
279 const std::string& expected_push_subscription_info, 260 std::string* out_token) {
280 bool use_key) {
281 std::string script_result; 261 std::string script_result;
282 262
283 EXPECT_TRUE(RunScript("registerServiceWorker()", &script_result)); 263 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
284 EXPECT_EQ("ok - service worker registered", script_result); 264 ASSERT_EQ("ok - service worker registered", script_result);
285 265
286 RequestAndAcceptPermission(); 266 ASSERT_NO_FATAL_FAILURE(RequestAndAcceptPermission());
287 267
288 if (use_key) { 268 if (use_key) {
289 ASSERT_TRUE(RunScript("removeManifest()", &script_result)); 269 ASSERT_TRUE(RunScript("removeManifest()", &script_result));
290 ASSERT_EQ("manifest removed", script_result); 270 ASSERT_EQ("manifest removed", script_result);
291 271
292 EXPECT_TRUE(RunScript("documentSubscribePush()", &script_result)); 272 ASSERT_TRUE(RunScript("documentSubscribePush()", &script_result));
293 } else { 273 } else {
294 // Test backwards compatibility with old ID based subscriptions. 274 // Test backwards compatibility with old ID based subscriptions.
295 EXPECT_TRUE(RunScript("documentSubscribePushWithoutKey()", &script_result)); 275 ASSERT_TRUE(RunScript("documentSubscribePushWithoutKey()", &script_result));
296 } 276 }
297 277
298 EXPECT_EQ(GetEndpointForSubscriptionId(expected_push_subscription_info), 278 ASSERT_NO_FATAL_FAILURE(EndpointToToken(script_result, out_token));
299 script_result); 279 }
280
281 void PushMessagingBrowserTest::EndpointToToken(const std::string& endpoint,
282 std::string* out_token) {
283 std::string https_scheme = "https://";
284 EXPECT_THAT(endpoint, ::testing::StartsWith(https_scheme));
285
286 size_t last_slash = endpoint.rfind('/');
287 // Generous sanity checks.
288 size_t min_host_length = 3;
289 size_t min_token_length = 5;
290 EXPECT_LT(https_scheme.length() - 1 + min_host_length, last_slash);
291 EXPECT_LT(last_slash, endpoint.length() - min_token_length);
292
293 if (out_token)
294 *out_token = endpoint.substr(last_slash);
300 } 295 }
301 296
302 PushMessagingAppIdentifier 297 PushMessagingAppIdentifier
303 PushMessagingBrowserTest::GetAppIdentifierForServiceWorkerRegistration( 298 PushMessagingBrowserTest::GetAppIdentifierForServiceWorkerRegistration(
304 int64_t service_worker_registration_id) { 299 int64_t service_worker_registration_id) {
305 GURL origin = https_server()->GetURL("/").GetOrigin(); 300 GURL origin = https_server()->GetURL("/").GetOrigin();
306 PushMessagingAppIdentifier app_identifier = 301 PushMessagingAppIdentifier app_identifier =
307 PushMessagingAppIdentifier::FindByServiceWorker( 302 PushMessagingAppIdentifier::FindByServiceWorker(
308 GetBrowser()->profile(), origin, service_worker_registration_id); 303 GetBrowser()->profile(), origin, service_worker_registration_id);
309 EXPECT_FALSE(app_identifier.is_null()); 304 EXPECT_FALSE(app_identifier.is_null());
310 return app_identifier; 305 return app_identifier;
311 } 306 }
312 307
313 void PushMessagingBrowserTest::SendMessageAndWaitUntilHandled( 308 void PushMessagingBrowserTest::SendMessageAndWaitUntilHandled(
314 const PushMessagingAppIdentifier& app_identifier, 309 const PushMessagingAppIdentifier& app_identifier,
315 const gcm::IncomingMessage& message) { 310 const gcm::IncomingMessage& message) {
316 base::RunLoop run_loop; 311 base::RunLoop run_loop;
317 push_service()->SetMessageCallbackForTesting(run_loop.QuitClosure()); 312 push_service()->SetMessageCallbackForTesting(run_loop.QuitClosure());
318 push_service()->OnMessage(app_identifier.app_id(), message); 313 push_service()->OnMessage(app_identifier.app_id(), message);
319 run_loop.Run(); 314 run_loop.Run();
320 } 315 }
321 316
322 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, 317 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
323 SubscribeWithoutKeySuccessNotificationsGranted) { 318 SubscribeWithoutKeySuccessNotificationsGranted) {
324 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */, false); 319 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully(false /* use_key */));
325 320 EXPECT_EQ(kManifestSenderId,
326 PushMessagingAppIdentifier app_identifier = 321 gcm_driver_->last_registered_authorized_entity());
327 GetAppIdentifierForServiceWorkerRegistration(0LL); 322 EXPECT_EQ(GetAppIdentifierForServiceWorkerRegistration(0LL).app_id(),
328 EXPECT_EQ(app_identifier.app_id(), gcm_service()->last_registered_app_id()); 323 gcm_driver_->last_registered_app_id());
329 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]);
330 } 324 }
331 325
332 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, 326 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
333 SubscribeSuccessNotificationsGranted) { 327 SubscribeSuccessNotificationsGranted) {
334 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */); 328 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully());
335
336 PushMessagingAppIdentifier app_identifier =
337 GetAppIdentifierForServiceWorkerRegistration(0LL);
338 EXPECT_EQ(app_identifier.app_id(), gcm_service()->last_registered_app_id());
339 EXPECT_EQ(kEncodedApplicationServerKey, 329 EXPECT_EQ(kEncodedApplicationServerKey,
340 gcm_service()->last_registered_sender_ids()[0]); 330 gcm_driver_->last_registered_authorized_entity());
331 EXPECT_EQ(GetAppIdentifierForServiceWorkerRegistration(0LL).app_id(),
332 gcm_driver_->last_registered_app_id());
341 } 333 }
342 334
343 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, 335 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
344 SubscribeSuccessNotificationsPrompt) { 336 SubscribeSuccessNotificationsPrompt) {
345 std::string script_result; 337 std::string script_result;
346 338
347 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); 339 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
348 ASSERT_EQ("ok - service worker registered", script_result); 340 ASSERT_EQ("ok - service worker registered", script_result);
349 341
350 GetPermissionBubbleManager()->set_auto_response_for_test( 342 GetPermissionBubbleManager()->set_auto_response_for_test(
351 PermissionBubbleManager::ACCEPT_ALL); 343 PermissionBubbleManager::ACCEPT_ALL);
352 ASSERT_TRUE(RunScript("documentSubscribePush()", &script_result)); 344 ASSERT_TRUE(RunScript("documentSubscribePush()", &script_result));
353 EXPECT_EQ(GetEndpointForSubscriptionId("1-0"), script_result); 345 // Both of these methods EXPECT that they succeed.
354 346 ASSERT_NO_FATAL_FAILURE(EndpointToToken(script_result));
355 PushMessagingAppIdentifier app_identifier = 347 GetAppIdentifierForServiceWorkerRegistration(0LL);
356 GetAppIdentifierForServiceWorkerRegistration(0LL);
357 EXPECT_EQ(app_identifier.app_id(), gcm_service()->last_registered_app_id());
358 EXPECT_EQ(kEncodedApplicationServerKey,
359 gcm_service()->last_registered_sender_ids()[0]);
360 } 348 }
361 349
362 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, SubscribeFailureBadKey) { 350 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, SubscribeFailureBadKey) {
363 std::string script_result; 351 std::string script_result;
364 352
365 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); 353 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
366 ASSERT_EQ("ok - service worker registered", script_result); 354 ASSERT_EQ("ok - service worker registered", script_result);
367 355
368 RequestAndAcceptPermission(); 356 ASSERT_NO_FATAL_FAILURE(RequestAndAcceptPermission());
369 357
370 ASSERT_TRUE(RunScript("documentSubscribePushBadKey()", &script_result)); 358 ASSERT_TRUE(RunScript("documentSubscribePushBadKey()", &script_result));
371 EXPECT_EQ( 359 EXPECT_EQ(
372 "InvalidAccessError - Failed to execute 'subscribe' on 'PushManager': " 360 "InvalidAccessError - Failed to execute 'subscribe' on 'PushManager': "
373 "The provided applicationServerKey is not valid.", 361 "The provided applicationServerKey is not valid.",
374 script_result); 362 script_result);
375 } 363 }
376 364
377 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, 365 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
378 SubscribeFailureNotificationsBlocked) { 366 SubscribeFailureNotificationsBlocked) {
379 std::string script_result; 367 std::string script_result;
380 368
381 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); 369 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
382 ASSERT_EQ("ok - service worker registered", script_result); 370 ASSERT_EQ("ok - service worker registered", script_result);
383 371
384 RequestAndDenyPermission(); 372 ASSERT_NO_FATAL_FAILURE(RequestAndDenyPermission());
385 373
386 ASSERT_TRUE(RunScript("documentSubscribePush()", &script_result)); 374 ASSERT_TRUE(RunScript("documentSubscribePush()", &script_result));
387 EXPECT_EQ("PermissionDeniedError - Registration failed - permission denied", 375 EXPECT_EQ("PermissionDeniedError - Registration failed - permission denied",
388 script_result); 376 script_result);
389 } 377 }
390 378
391 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, SubscribeFailureNoManifest) { 379 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, SubscribeFailureNoManifest) {
392 std::string script_result; 380 std::string script_result;
393 381
394 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); 382 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
395 ASSERT_EQ("ok - service worker registered", script_result); 383 ASSERT_EQ("ok - service worker registered", script_result);
396 384
397 RequestAndAcceptPermission(); 385 ASSERT_NO_FATAL_FAILURE(RequestAndAcceptPermission());
398 386
399 ASSERT_TRUE(RunScript("removeManifest()", &script_result)); 387 ASSERT_TRUE(RunScript("removeManifest()", &script_result));
400 ASSERT_EQ("manifest removed", script_result); 388 ASSERT_EQ("manifest removed", script_result);
401 389
402 ASSERT_TRUE(RunScript("documentSubscribePushWithoutKey()", &script_result)); 390 ASSERT_TRUE(RunScript("documentSubscribePushWithoutKey()", &script_result));
403 EXPECT_EQ("AbortError - Registration failed - manifest empty or missing", 391 EXPECT_EQ("AbortError - Registration failed - manifest empty or missing",
404 script_result); 392 script_result);
405 } 393 }
406 394
407 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, SubscribeFailureNoSenderId) { 395 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, SubscribeFailureNoSenderId) {
408 std::string script_result; 396 std::string script_result;
409 397
410 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); 398 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
411 ASSERT_EQ("ok - service worker registered", script_result); 399 ASSERT_EQ("ok - service worker registered", script_result);
412 400
413 RequestAndAcceptPermission(); 401 ASSERT_NO_FATAL_FAILURE(RequestAndAcceptPermission());
414 402
415 ASSERT_TRUE(RunScript("swapManifestNoSenderId()", &script_result)); 403 ASSERT_TRUE(RunScript("swapManifestNoSenderId()", &script_result));
416 ASSERT_EQ("sender id removed from manifest", script_result); 404 ASSERT_EQ("sender id removed from manifest", script_result);
417 405
418 ASSERT_TRUE(RunScript("documentSubscribePushWithoutKey()", &script_result)); 406 ASSERT_TRUE(RunScript("documentSubscribePushWithoutKey()", &script_result));
419 EXPECT_EQ( 407 EXPECT_EQ(
420 "AbortError - Registration failed - gcm_sender_id not found in manifest", 408 "AbortError - Registration failed - gcm_sender_id not found in manifest",
421 script_result); 409 script_result);
422 } 410 }
423 411
424 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTestEmptySubscriptionOptions, 412 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTestEmptySubscriptionOptions,
425 RegisterFailureEmptyPushSubscriptionOptions) { 413 RegisterFailureEmptyPushSubscriptionOptions) {
426 std::string script_result; 414 std::string script_result;
427 415
428 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); 416 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
429 ASSERT_EQ("ok - service worker registered", script_result); 417 ASSERT_EQ("ok - service worker registered", script_result);
430 418
431 RequestAndAcceptPermission(); 419 ASSERT_NO_FATAL_FAILURE(RequestAndAcceptPermission());
432 420
433 ASSERT_TRUE(RunScript("documentSubscribePush()", &script_result)); 421 ASSERT_TRUE(RunScript("documentSubscribePush()", &script_result));
434 EXPECT_EQ("PermissionDeniedError - Registration failed - permission denied", 422 EXPECT_EQ("PermissionDeniedError - Registration failed - permission denied",
435 script_result); 423 script_result);
436 } 424 }
437 425
438 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, SubscribeWorker) { 426 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, SubscribeWorker) {
439 std::string script_result; 427 std::string script_result;
440 428
441 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); 429 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
442 ASSERT_EQ("ok - service worker registered", script_result); 430 ASSERT_EQ("ok - service worker registered", script_result);
443 431
444 RequestAndAcceptPermission(); 432 ASSERT_NO_FATAL_FAILURE(RequestAndAcceptPermission());
445 433
446 LoadTestPage(); // Reload to become controlled. 434 LoadTestPage(); // Reload to become controlled.
447 435
448 ASSERT_TRUE(RunScript("isControlled()", &script_result)); 436 ASSERT_TRUE(RunScript("isControlled()", &script_result));
449 ASSERT_EQ("true - is controlled", script_result); 437 ASSERT_EQ("true - is controlled", script_result);
450 438
451 // Try to subscribe from a worker without a key. This should fail. 439 // Try to subscribe from a worker without a key. This should fail.
452 ASSERT_TRUE(RunScript("workerSubscribePushNoKey()", &script_result)); 440 ASSERT_TRUE(RunScript("workerSubscribePushNoKey()", &script_result));
453 EXPECT_EQ( 441 EXPECT_EQ(
454 "AbortError - Registration failed - gcm_sender_id not found in manifest", 442 "AbortError - Registration failed - gcm_sender_id not found in manifest",
455 script_result); 443 script_result);
456 // Now run the subscribe from the service worker with a key. This 444 // Now run the subscribe from the service worker with a key. This
457 // should succeed, and write the key to the datastore. 445 // should succeed, and write the key to the datastore.
458 ASSERT_TRUE(RunScript("workerSubscribePush()", &script_result)); 446 ASSERT_TRUE(RunScript("workerSubscribePush()", &script_result));
459 EXPECT_EQ(GetEndpointForSubscriptionId("1-0"), script_result); 447 std::string token1;
448 ASSERT_NO_FATAL_FAILURE(EndpointToToken(script_result, &token1));
460 449
461 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result)); 450 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
462 EXPECT_EQ("unsubscribe result: true", script_result); 451 EXPECT_EQ("unsubscribe result: true", script_result);
463 EXPECT_NE(push_service(), GetAppHandler()); 452 EXPECT_NE(push_service(), GetAppHandler());
464 453
465 // Now run the subscribe from the service worker without a key. 454 // Now run the subscribe from the service worker without a key.
466 // In this case, the key will be read from the datastore. 455 // In this case, the key will be read from the datastore.
467 ASSERT_TRUE(RunScript("workerSubscribePushNoKey()", &script_result)); 456 ASSERT_TRUE(RunScript("workerSubscribePushNoKey()", &script_result));
468 EXPECT_EQ(GetEndpointForSubscriptionId("1-1"), script_result); 457 std::string token2;
458 ASSERT_NO_FATAL_FAILURE(EndpointToToken(script_result, &token2));
459 EXPECT_NE(token1, token2);
469 460
470 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result)); 461 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
471 EXPECT_EQ("unsubscribe result: true", script_result); 462 EXPECT_EQ("unsubscribe result: true", script_result);
472 EXPECT_NE(push_service(), GetAppHandler()); 463 EXPECT_NE(push_service(), GetAppHandler());
473 } 464 }
474 465
475 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, SubscribeWorkerUsingManifest) { 466 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, SubscribeWorkerUsingManifest) {
476 std::string script_result; 467 std::string script_result;
477 468
478 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); 469 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
479 ASSERT_EQ("ok - service worker registered", script_result); 470 ASSERT_EQ("ok - service worker registered", script_result);
480 471
481 RequestAndAcceptPermission(); 472 ASSERT_NO_FATAL_FAILURE(RequestAndAcceptPermission());
482 473
483 LoadTestPage(); // Reload to become controlled. 474 LoadTestPage(); // Reload to become controlled.
484 475
485 ASSERT_TRUE(RunScript("isControlled()", &script_result)); 476 ASSERT_TRUE(RunScript("isControlled()", &script_result));
486 ASSERT_EQ("true - is controlled", script_result); 477 ASSERT_EQ("true - is controlled", script_result);
487 478
488 // Try to subscribe from a worker without a key. This should fail. 479 // Try to subscribe from a worker without a key. This should fail.
489 ASSERT_TRUE(RunScript("workerSubscribePushNoKey()", &script_result)); 480 ASSERT_TRUE(RunScript("workerSubscribePushNoKey()", &script_result));
490 EXPECT_EQ( 481 EXPECT_EQ(
491 "AbortError - Registration failed - gcm_sender_id not found in manifest", 482 "AbortError - Registration failed - gcm_sender_id not found in manifest",
492 script_result); 483 script_result);
493 EXPECT_NE(push_service(), GetAppHandler()); 484 EXPECT_NE(push_service(), GetAppHandler());
494 485
495 // Run the subscription from the document without a key, this will trigger 486 // Run the subscription from the document without a key, this will trigger
496 // the code to read sender id from the manifest and will write it to the 487 // the code to read sender id from the manifest and will write it to the
497 // datastore. 488 // datastore.
498 ASSERT_TRUE(RunScript("documentSubscribePushWithoutKey()", &script_result)); 489 ASSERT_TRUE(RunScript("documentSubscribePushWithoutKey()", &script_result));
499 EXPECT_EQ(GetEndpointForSubscriptionId("1-0"), script_result); 490 std::string token1;
491 ASSERT_NO_FATAL_FAILURE(EndpointToToken(script_result, &token1));
500 492
501 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result)); 493 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
502 EXPECT_EQ("unsubscribe result: true", script_result); 494 EXPECT_EQ("unsubscribe result: true", script_result);
503 EXPECT_NE(push_service(), GetAppHandler()); 495 EXPECT_NE(push_service(), GetAppHandler());
504 496
505 // Now run the subscribe from the service worker without a key. 497 // Now run the subscribe from the service worker without a key.
506 // In this case, the sender id will be read from the datastore. 498 // In this case, the sender id will be read from the datastore.
507 ASSERT_TRUE(RunScript("workerSubscribePushNoKey()", &script_result)); 499 ASSERT_TRUE(RunScript("workerSubscribePushNoKey()", &script_result));
508 EXPECT_EQ(GetEndpointForSubscriptionId("1-1"), script_result); 500 std::string token2;
501 ASSERT_NO_FATAL_FAILURE(EndpointToToken(script_result, &token2));
502 EXPECT_NE(token1, token2);
509 503
510 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result)); 504 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
511 EXPECT_EQ("unsubscribe result: true", script_result); 505 EXPECT_EQ("unsubscribe result: true", script_result);
512 EXPECT_NE(push_service(), GetAppHandler()); 506 EXPECT_NE(push_service(), GetAppHandler());
513 } 507 }
514 508
515 // Disabled on Windows and Linux due to flakiness (http://crbug.com/554003). 509 // Disabled on Windows and Linux due to flakiness (http://crbug.com/554003).
516 #if defined(OS_WIN) || defined(OS_LINUX) 510 #if defined(OS_WIN) || defined(OS_LINUX)
517 #define MAYBE_SubscribePersisted DISABLED_SubscribePersisted 511 #define MAYBE_SubscribePersisted DISABLED_SubscribePersisted
518 #else 512 #else
519 #define MAYBE_SubscribePersisted SubscribePersisted 513 #define MAYBE_SubscribePersisted SubscribePersisted
520 #endif 514 #endif
521 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, MAYBE_SubscribePersisted) { 515 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, MAYBE_SubscribePersisted) {
522 std::string script_result; 516 std::string script_result;
523 517
524 // First, test that Service Worker registration IDs are assigned in order of 518 // First, test that Service Worker registration IDs are assigned in order of
525 // registering the Service Workers, and the (fake) push subscription ids are 519 // registering the Service Workers, and the (fake) push subscription ids are
526 // assigned in order of push subscription (even when these orders are 520 // assigned in order of push subscription (even when these orders are
527 // different). 521 // different).
528 522
529 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */); 523 std::string token1;
524 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully(true /* use_key */, &token1));
530 PushMessagingAppIdentifier sw0_identifier = 525 PushMessagingAppIdentifier sw0_identifier =
531 GetAppIdentifierForServiceWorkerRegistration(0LL); 526 GetAppIdentifierForServiceWorkerRegistration(0LL);
532 EXPECT_EQ(sw0_identifier.app_id(), gcm_service()->last_registered_app_id()); 527 EXPECT_EQ(sw0_identifier.app_id(), gcm_driver_->last_registered_app_id());
533 528
534 LoadTestPage("/push_messaging/subscope1/test.html"); 529 LoadTestPage("/push_messaging/subscope1/test.html");
535 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); 530 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
536 ASSERT_EQ("ok - service worker registered", script_result); 531 ASSERT_EQ("ok - service worker registered", script_result);
537 532
538 LoadTestPage("/push_messaging/subscope2/test.html"); 533 LoadTestPage("/push_messaging/subscope2/test.html");
539 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); 534 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
540 ASSERT_EQ("ok - service worker registered", script_result); 535 ASSERT_EQ("ok - service worker registered", script_result);
541 536
542 // Note that we need to reload the page after registering, otherwise 537 // Note that we need to reload the page after registering, otherwise
543 // navigator.serviceWorker.ready is going to be resolved with the parent 538 // navigator.serviceWorker.ready is going to be resolved with the parent
544 // Service Worker which still controls the page. 539 // Service Worker which still controls the page.
545 LoadTestPage("/push_messaging/subscope2/test.html"); 540 LoadTestPage("/push_messaging/subscope2/test.html");
546 TryToSubscribeSuccessfully("1-1" /* expected_push_subscription_id */); 541 std::string token2;
542 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully(true /* use_key */, &token2));
543 EXPECT_NE(token1, token2);
547 PushMessagingAppIdentifier sw2_identifier = 544 PushMessagingAppIdentifier sw2_identifier =
548 GetAppIdentifierForServiceWorkerRegistration(2LL); 545 GetAppIdentifierForServiceWorkerRegistration(2LL);
549 EXPECT_EQ(sw2_identifier.app_id(), gcm_service()->last_registered_app_id()); 546 EXPECT_EQ(sw2_identifier.app_id(), gcm_driver_->last_registered_app_id());
550 547
551 LoadTestPage("/push_messaging/subscope1/test.html"); 548 LoadTestPage("/push_messaging/subscope1/test.html");
552 TryToSubscribeSuccessfully("1-2" /* expected_push_subscription_id */); 549 std::string token3;
550 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully(true /* use_key */, &token3));
551 EXPECT_NE(token1, token3);
552 EXPECT_NE(token2, token3);
553 PushMessagingAppIdentifier sw1_identifier = 553 PushMessagingAppIdentifier sw1_identifier =
554 GetAppIdentifierForServiceWorkerRegistration(1LL); 554 GetAppIdentifierForServiceWorkerRegistration(1LL);
555 EXPECT_EQ(sw1_identifier.app_id(), gcm_service()->last_registered_app_id()); 555 EXPECT_EQ(sw1_identifier.app_id(), gcm_driver_->last_registered_app_id());
556 556
557 // Now test that the Service Worker registration IDs and push subscription IDs 557 // Now test that the Service Worker registration IDs and push subscription IDs
558 // generated above were persisted to SW storage, by checking that they are 558 // generated above were persisted to SW storage, by checking that they are
559 // unchanged despite requesting them in a different order. 559 // unchanged despite requesting them in a different order.
560 // TODO(johnme): Ideally we would restart the browser at this point to check 560 // TODO(johnme): Ideally we would restart the browser at this point to check
561 // they were persisted to disk, but that's not currently possible since the 561 // they were persisted to disk, but that's not currently possible since the
562 // test server uses random port numbers for each test (even PRE_Foo and Foo), 562 // test server uses random port numbers for each test (even PRE_Foo and Foo),
563 // so we wouldn't be able to load the test pages with the same origin. 563 // so we wouldn't be able to load the test pages with the same origin.
564 564
565 LoadTestPage("/push_messaging/subscope1/test.html"); 565 LoadTestPage("/push_messaging/subscope1/test.html");
566 TryToSubscribeSuccessfully("1-2" /* expected_push_subscription_id */); 566 std::string token4;
567 EXPECT_EQ(sw1_identifier.app_id(), gcm_service()->last_registered_app_id()); 567 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully(true /* use_key */, &token4));
568 EXPECT_EQ(token3, token4);
569 EXPECT_EQ(sw1_identifier.app_id(), gcm_driver_->last_registered_app_id());
568 570
569 LoadTestPage("/push_messaging/subscope2/test.html"); 571 LoadTestPage("/push_messaging/subscope2/test.html");
570 TryToSubscribeSuccessfully("1-1" /* expected_push_subscription_id */); 572 std::string token5;
571 EXPECT_EQ(sw1_identifier.app_id(), gcm_service()->last_registered_app_id()); 573 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully(true /* use_key */, &token5));
574 EXPECT_EQ(token2, token5);
575 EXPECT_EQ(sw1_identifier.app_id(), gcm_driver_->last_registered_app_id());
572 576
573 LoadTestPage(); 577 LoadTestPage();
574 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */); 578 std::string token6;
575 EXPECT_EQ(sw1_identifier.app_id(), gcm_service()->last_registered_app_id()); 579 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully(true /* use_key */, &token6));
580 EXPECT_EQ(token1, token6);
581 EXPECT_EQ(sw1_identifier.app_id(), gcm_driver_->last_registered_app_id());
576 } 582 }
577 583
578 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, AppHandlerOnlyIfSubscribed) { 584 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, AppHandlerOnlyIfSubscribed) {
579 // This test restarts the push service to simulate restarting the browser. 585 // This test restarts the push service to simulate restarting the browser.
580 586
581 EXPECT_NE(push_service(), GetAppHandler()); 587 EXPECT_NE(push_service(), GetAppHandler());
582 ASSERT_NO_FATAL_FAILURE(RestartPushService()); 588 ASSERT_NO_FATAL_FAILURE(RestartPushService());
583 EXPECT_NE(push_service(), GetAppHandler()); 589 EXPECT_NE(push_service(), GetAppHandler());
584 590
585 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */); 591 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully());
586 592
587 EXPECT_EQ(push_service(), GetAppHandler()); 593 EXPECT_EQ(push_service(), GetAppHandler());
588 ASSERT_NO_FATAL_FAILURE(RestartPushService()); 594 ASSERT_NO_FATAL_FAILURE(RestartPushService());
589 EXPECT_EQ(push_service(), GetAppHandler()); 595 EXPECT_EQ(push_service(), GetAppHandler());
590 596
591 // Unsubscribe. 597 // Unsubscribe.
592 std::string script_result; 598 std::string script_result;
593 gcm_service()->AddExpectedUnregisterResponse(gcm::GCMClient::SUCCESS);
594 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result)); 599 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
595 EXPECT_EQ("unsubscribe result: true", script_result); 600 EXPECT_EQ("unsubscribe result: true", script_result);
596 601
597 EXPECT_NE(push_service(), GetAppHandler()); 602 EXPECT_NE(push_service(), GetAppHandler());
598 ASSERT_NO_FATAL_FAILURE(RestartPushService()); 603 ASSERT_NO_FATAL_FAILURE(RestartPushService());
599 EXPECT_NE(push_service(), GetAppHandler()); 604 EXPECT_NE(push_service(), GetAppHandler());
600 } 605 }
601 606
602 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, PushEventSuccess) { 607 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, PushEventSuccess) {
603 std::string script_result; 608 std::string script_result;
604 609
605 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */); 610 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully());
606
607 PushMessagingAppIdentifier app_identifier = 611 PushMessagingAppIdentifier app_identifier =
608 GetAppIdentifierForServiceWorkerRegistration(0LL); 612 GetAppIdentifierForServiceWorkerRegistration(0LL);
609 EXPECT_EQ(app_identifier.app_id(), gcm_service()->last_registered_app_id());
610 EXPECT_EQ(kEncodedApplicationServerKey,
611 gcm_service()->last_registered_sender_ids()[0]);
612 613
613 ASSERT_TRUE(RunScript("isControlled()", &script_result)); 614 ASSERT_TRUE(RunScript("isControlled()", &script_result));
614 ASSERT_EQ("false - is not controlled", script_result); 615 ASSERT_EQ("false - is not controlled", script_result);
615 616
616 LoadTestPage(); // Reload to become controlled. 617 LoadTestPage(); // Reload to become controlled.
617 618
618 ASSERT_TRUE(RunScript("isControlled()", &script_result)); 619 ASSERT_TRUE(RunScript("isControlled()", &script_result));
619 ASSERT_EQ("true - is controlled", script_result); 620 ASSERT_EQ("true - is controlled", script_result);
620 621
621 gcm::IncomingMessage message; 622 gcm::IncomingMessage message;
622 message.sender_id = GetTestApplicationServerKey(); 623 message.sender_id = GetTestApplicationServerKey();
623 message.raw_data = "testdata"; 624 message.raw_data = "testdata";
624 message.decrypted = true; 625 message.decrypted = true;
625 push_service()->OnMessage(app_identifier.app_id(), message); 626 push_service()->OnMessage(app_identifier.app_id(), message);
626 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result)); 627 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result));
627 EXPECT_EQ("testdata", script_result); 628 EXPECT_EQ("testdata", script_result);
628 } 629 }
629 630
630 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, PushEventWithoutPayload) { 631 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, PushEventWithoutPayload) {
631 std::string script_result; 632 std::string script_result;
632 633
633 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */); 634 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully());
634
635 PushMessagingAppIdentifier app_identifier = 635 PushMessagingAppIdentifier app_identifier =
636 GetAppIdentifierForServiceWorkerRegistration(0LL); 636 GetAppIdentifierForServiceWorkerRegistration(0LL);
637 EXPECT_EQ(app_identifier.app_id(), gcm_service()->last_registered_app_id());
638 EXPECT_EQ(kEncodedApplicationServerKey,
639 gcm_service()->last_registered_sender_ids()[0]);
640 637
641 ASSERT_TRUE(RunScript("isControlled()", &script_result)); 638 ASSERT_TRUE(RunScript("isControlled()", &script_result));
642 ASSERT_EQ("false - is not controlled", script_result); 639 ASSERT_EQ("false - is not controlled", script_result);
643 640
644 LoadTestPage(); // Reload to become controlled. 641 LoadTestPage(); // Reload to become controlled.
645 642
646 ASSERT_TRUE(RunScript("isControlled()", &script_result)); 643 ASSERT_TRUE(RunScript("isControlled()", &script_result));
647 ASSERT_EQ("true - is controlled", script_result); 644 ASSERT_EQ("true - is controlled", script_result);
648 645
649 gcm::IncomingMessage message; 646 gcm::IncomingMessage message;
650 message.sender_id = GetTestApplicationServerKey(); 647 message.sender_id = GetTestApplicationServerKey();
651 message.decrypted = false; 648 message.decrypted = false;
652 649
653 push_service()->OnMessage(app_identifier.app_id(), message); 650 push_service()->OnMessage(app_identifier.app_id(), message);
654 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result)); 651 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result));
655 EXPECT_EQ("[NULL]", script_result); 652 EXPECT_EQ("[NULL]", script_result);
656 } 653 }
657 654
658 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, PushEventNoServiceWorker) { 655 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, PushEventNoServiceWorker) {
659 std::string script_result; 656 std::string script_result;
660 657
661 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */); 658 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully());
662
663 PushMessagingAppIdentifier app_identifier = 659 PushMessagingAppIdentifier app_identifier =
664 GetAppIdentifierForServiceWorkerRegistration(0LL); 660 GetAppIdentifierForServiceWorkerRegistration(0LL);
665 EXPECT_EQ(app_identifier.app_id(), gcm_service()->last_registered_app_id());
666 EXPECT_EQ(kEncodedApplicationServerKey,
667 gcm_service()->last_registered_sender_ids()[0]);
668 661
669 ASSERT_TRUE(RunScript("isControlled()", &script_result)); 662 ASSERT_TRUE(RunScript("isControlled()", &script_result));
670 ASSERT_EQ("false - is not controlled", script_result); 663 ASSERT_EQ("false - is not controlled", script_result);
671 664
672 LoadTestPage(); // Reload to become controlled. 665 LoadTestPage(); // Reload to become controlled.
673 666
674 ASSERT_TRUE(RunScript("isControlled()", &script_result)); 667 ASSERT_TRUE(RunScript("isControlled()", &script_result));
675 ASSERT_EQ("true - is controlled", script_result); 668 ASSERT_EQ("true - is controlled", script_result);
676 669
677 // Unregister service worker. Sending a message should now fail. 670 // Unregister service worker. Sending a message should now fail.
678 ASSERT_TRUE(RunScript("unregisterServiceWorker()", &script_result)); 671 ASSERT_TRUE(RunScript("unregisterServiceWorker()", &script_result));
679 ASSERT_EQ("service worker unregistration status: true", script_result); 672 ASSERT_EQ("service worker unregistration status: true", script_result);
680 673
681 // When the push service will receive it next message, given that there is no 674 // Unregistering the service worker doesn't yet unsubscribe from push (though
682 // SW available, it should unregister |app_identifier.app_id()|. 675 // it should), and FindByServiceWorker doesn't require a live SW.
683 UnregistrationCallback callback; 676 GURL origin = https_server()->GetURL("/").GetOrigin();
684 gcm_service()->SetUnregisterCallback( 677 PushMessagingAppIdentifier app_identifier2 =
685 base::Bind(&UnregistrationCallback::Run, base::Unretained(&callback))); 678 PushMessagingAppIdentifier::FindByServiceWorker(
679 GetBrowser()->profile(), origin,
680 0LL /* service_worker_registration_id */);
681 EXPECT_FALSE(app_identifier2.is_null());
682 EXPECT_EQ(app_identifier.app_id(), app_identifier2.app_id());
686 683
687 gcm::IncomingMessage message; 684 gcm::IncomingMessage message;
688 message.sender_id = GetTestApplicationServerKey(); 685 message.sender_id = GetTestApplicationServerKey();
689 message.raw_data = "testdata"; 686 message.raw_data = "testdata";
690 message.decrypted = true; 687 message.decrypted = true;
691 push_service()->OnMessage(app_identifier.app_id(), message); 688 SendMessageAndWaitUntilHandled(app_identifier, message);
692 689
693 callback.WaitUntilSatisfied(); 690 // Now the push service has received a message and failed to find its service
694 EXPECT_EQ(app_identifier.app_id(), callback.app_id()); 691 // worker, it should have automatically unsubscribed app_identifier.app_id().
692 PushMessagingAppIdentifier app_identifier3 =
693 PushMessagingAppIdentifier::FindByServiceWorker(
694 GetBrowser()->profile(), origin,
695 0LL /* service_worker_registration_id */);
696 EXPECT_TRUE(app_identifier3.is_null());
695 697
696 // No push data should have been received. 698 // No push data should have been received.
697 ASSERT_TRUE(RunScript("resultQueue.popImmediately()", &script_result)); 699 ASSERT_TRUE(RunScript("resultQueue.popImmediately()", &script_result));
698 EXPECT_EQ("null", script_result); 700 EXPECT_EQ("null", script_result);
699 } 701 }
700 702
701 #if defined(ENABLE_NOTIFICATIONS) 703 #if defined(ENABLE_NOTIFICATIONS)
702 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, 704 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
703 PushEventEnforcesUserVisibleNotification) { 705 PushEventEnforcesUserVisibleNotification) {
704 std::string script_result; 706 std::string script_result;
705 707
706 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */); 708 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully());
707
708 PushMessagingAppIdentifier app_identifier = 709 PushMessagingAppIdentifier app_identifier =
709 GetAppIdentifierForServiceWorkerRegistration(0LL); 710 GetAppIdentifierForServiceWorkerRegistration(0LL);
710 EXPECT_EQ(app_identifier.app_id(), gcm_service()->last_registered_app_id());
711 EXPECT_EQ(kEncodedApplicationServerKey,
712 gcm_service()->last_registered_sender_ids()[0]);
713 711
714 ASSERT_TRUE(RunScript("isControlled()", &script_result)); 712 ASSERT_TRUE(RunScript("isControlled()", &script_result));
715 ASSERT_EQ("false - is not controlled", script_result); 713 ASSERT_EQ("false - is not controlled", script_result);
716 714
717 LoadTestPage(); // Reload to become controlled. 715 LoadTestPage(); // Reload to become controlled.
718 716
719 ASSERT_TRUE(RunScript("isControlled()", &script_result)); 717 ASSERT_TRUE(RunScript("isControlled()", &script_result));
720 ASSERT_EQ("true - is controlled", script_result); 718 ASSERT_EQ("true - is controlled", script_result);
721 719
722 notification_manager()->CancelAll(); 720 notification_manager()->CancelAll();
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
804 SendMessageAndWaitUntilHandled(app_identifier, message); 802 SendMessageAndWaitUntilHandled(app_identifier, message);
805 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result, web_contents)); 803 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result, web_contents));
806 EXPECT_EQ("testdata", script_result); 804 EXPECT_EQ("testdata", script_result);
807 EXPECT_EQ(0u, notification_manager()->GetNotificationCount()); 805 EXPECT_EQ(0u, notification_manager()->GetNotificationCount());
808 } 806 }
809 807
810 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, 808 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
811 PushEventEnforcesUserVisibleNotificationAfterQueue) { 809 PushEventEnforcesUserVisibleNotificationAfterQueue) {
812 std::string script_result; 810 std::string script_result;
813 811
814 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */); 812 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully());
815
816 PushMessagingAppIdentifier app_identifier = 813 PushMessagingAppIdentifier app_identifier =
817 GetAppIdentifierForServiceWorkerRegistration(0LL); 814 GetAppIdentifierForServiceWorkerRegistration(0LL);
818 EXPECT_EQ(app_identifier.app_id(), gcm_service()->last_registered_app_id());
819 EXPECT_EQ(kEncodedApplicationServerKey,
820 gcm_service()->last_registered_sender_ids()[0]);
821 815
822 ASSERT_TRUE(RunScript("isControlled()", &script_result)); 816 ASSERT_TRUE(RunScript("isControlled()", &script_result));
823 ASSERT_EQ("false - is not controlled", script_result); 817 ASSERT_EQ("false - is not controlled", script_result);
824 818
825 LoadTestPage(); // Reload to become controlled. 819 LoadTestPage(); // Reload to become controlled.
826 820
827 ASSERT_TRUE(RunScript("isControlled()", &script_result)); 821 ASSERT_TRUE(RunScript("isControlled()", &script_result));
828 ASSERT_EQ("true - is controlled", script_result); 822 ASSERT_EQ("true - is controlled", script_result);
829 823
830 // Fire off two push messages in sequence, only the second one of which will 824 // Fire off two push messages in sequence, only the second one of which will
(...skipping 27 matching lines...) Expand all
858 EXPECT_EQ(0u, number_of_notifications_shown[0]); 852 EXPECT_EQ(0u, number_of_notifications_shown[0]);
859 EXPECT_EQ(1u, number_of_notifications_shown[1]); 853 EXPECT_EQ(1u, number_of_notifications_shown[1]);
860 } 854 }
861 855
862 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, 856 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
863 PushEventNotificationWithoutEventWaitUntil) { 857 PushEventNotificationWithoutEventWaitUntil) {
864 std::string script_result; 858 std::string script_result;
865 content::WebContents* web_contents = 859 content::WebContents* web_contents =
866 GetBrowser()->tab_strip_model()->GetActiveWebContents(); 860 GetBrowser()->tab_strip_model()->GetActiveWebContents();
867 861
868 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */); 862 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully());
869
870 PushMessagingAppIdentifier app_identifier = 863 PushMessagingAppIdentifier app_identifier =
871 GetAppIdentifierForServiceWorkerRegistration(0LL); 864 GetAppIdentifierForServiceWorkerRegistration(0LL);
872 EXPECT_EQ(app_identifier.app_id(), gcm_service()->last_registered_app_id());
873 EXPECT_EQ(kEncodedApplicationServerKey,
874 gcm_service()->last_registered_sender_ids()[0]);
875 865
876 ASSERT_TRUE(RunScript("isControlled()", &script_result)); 866 ASSERT_TRUE(RunScript("isControlled()", &script_result));
877 ASSERT_EQ("false - is not controlled", script_result); 867 ASSERT_EQ("false - is not controlled", script_result);
878 868
879 LoadTestPage(); // Reload to become controlled. 869 LoadTestPage(); // Reload to become controlled.
880 870
881 ASSERT_TRUE(RunScript("isControlled()", &script_result)); 871 ASSERT_TRUE(RunScript("isControlled()", &script_result));
882 ASSERT_EQ("true - is controlled", script_result); 872 ASSERT_EQ("true - is controlled", script_result);
883 873
884 scoped_refptr<content::MessageLoopRunner> message_loop_runner = 874 scoped_refptr<content::MessageLoopRunner> message_loop_runner =
(...skipping 30 matching lines...) Expand all
915 ASSERT_TRUE(RunScript("permissionState()", &script_result)); 905 ASSERT_TRUE(RunScript("permissionState()", &script_result));
916 ASSERT_EQ("permission status - prompt", script_result); 906 ASSERT_EQ("permission status - prompt", script_result);
917 } 907 }
918 908
919 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, PermissionStateSaysGranted) { 909 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, PermissionStateSaysGranted) {
920 std::string script_result; 910 std::string script_result;
921 911
922 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); 912 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
923 ASSERT_EQ("ok - service worker registered", script_result); 913 ASSERT_EQ("ok - service worker registered", script_result);
924 914
925 RequestAndAcceptPermission(); 915 ASSERT_NO_FATAL_FAILURE(RequestAndAcceptPermission());
926 916
927 ASSERT_TRUE(RunScript("documentSubscribePush()", &script_result)); 917 ASSERT_TRUE(RunScript("documentSubscribePush()", &script_result));
928 EXPECT_EQ(GetEndpointForSubscriptionId("1-0"), script_result); 918 ASSERT_NO_FATAL_FAILURE(EndpointToToken(script_result));
929 919
930 ASSERT_TRUE(RunScript("permissionState()", &script_result)); 920 ASSERT_TRUE(RunScript("permissionState()", &script_result));
931 EXPECT_EQ("permission status - granted", script_result); 921 EXPECT_EQ("permission status - granted", script_result);
932 } 922 }
933 923
934 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, PermissionStateSaysDenied) { 924 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, PermissionStateSaysDenied) {
935 std::string script_result; 925 std::string script_result;
936 926
937 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); 927 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
938 ASSERT_EQ("ok - service worker registered", script_result); 928 ASSERT_EQ("ok - service worker registered", script_result);
939 929
940 RequestAndDenyPermission(); 930 ASSERT_NO_FATAL_FAILURE(RequestAndDenyPermission());
941 931
942 ASSERT_TRUE(RunScript("documentSubscribePush()", &script_result)); 932 ASSERT_TRUE(RunScript("documentSubscribePush()", &script_result));
943 EXPECT_EQ("PermissionDeniedError - Registration failed - permission denied", 933 EXPECT_EQ("PermissionDeniedError - Registration failed - permission denied",
944 script_result); 934 script_result);
945 935
946 ASSERT_TRUE(RunScript("permissionState()", &script_result)); 936 ASSERT_TRUE(RunScript("permissionState()", &script_result));
947 EXPECT_EQ("permission status - denied", script_result); 937 EXPECT_EQ("permission status - denied", script_result);
948 } 938 }
949 939
950 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, UnsubscribeSuccess) { 940 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, UnsubscribeSuccess) {
951 std::string script_result; 941 std::string script_result;
952 942
953 EXPECT_TRUE(RunScript("registerServiceWorker()", &script_result)); 943 EXPECT_TRUE(RunScript("registerServiceWorker()", &script_result));
954 EXPECT_EQ("ok - service worker registered", script_result); 944 EXPECT_EQ("ok - service worker registered", script_result);
955 945
956 // Resolves true if there was a subscription. 946 // Resolves true if there was a subscription.
957 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */, false); 947 std::string token1;
958 gcm_service()->AddExpectedUnregisterResponse(gcm::GCMClient::SUCCESS); 948 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully(false /* use_key */, &token1));
959 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result)); 949 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
960 EXPECT_EQ("unsubscribe result: true", script_result); 950 EXPECT_EQ("unsubscribe result: true", script_result);
961 951
962 // Resolves false if there was no longer a subscription. 952 // Resolves false if there was no longer a subscription.
963 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result)); 953 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
964 EXPECT_EQ("unsubscribe result: false", script_result); 954 EXPECT_EQ("unsubscribe result: false", script_result);
965 955
966 // Doesn't reject if there was a network error (deactivates subscription 956 // Doesn't reject if there was a network error (deactivates subscription
967 // locally anyway). 957 // locally anyway).
968 TryToSubscribeSuccessfully("1-1" /* expected_push_subscription_id */, false); 958 std::string token2;
969 gcm_service()->AddExpectedUnregisterResponse(gcm::GCMClient::NETWORK_ERROR); 959 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully(false /* use_key */, &token2));
960 EXPECT_NE(token1, token2);
961 // TODO(johnme): AddExpectedUnregisterResponse(gcm::GCMClient::NETWORK_ERROR);
970 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result)); 962 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
971 EXPECT_EQ("unsubscribe result: true", script_result); 963 EXPECT_EQ("unsubscribe result: true", script_result);
972 ASSERT_TRUE(RunScript("hasSubscription()", &script_result)); 964 ASSERT_TRUE(RunScript("hasSubscription()", &script_result));
973 EXPECT_EQ("false - not subscribed", script_result); 965 EXPECT_EQ("false - not subscribed", script_result);
974 966
975 // Doesn't reject if there were other push service errors (deactivates 967 // Doesn't reject if there were other push service errors (deactivates
976 // subscription locally anyway). 968 // subscription locally anyway).
977 TryToSubscribeSuccessfully("1-2" /* expected_push_subscription_id */, false); 969 std::string token3;
978 gcm_service()->AddExpectedUnregisterResponse( 970 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully(false /* use_key */, &token3));
979 gcm::GCMClient::INVALID_PARAMETER); 971 EXPECT_NE(token1, token3);
972 EXPECT_NE(token2, token3);
973 // TODO(johnme): AddExpectedUnregisterResponse(
974 // gcm::GCMClient::INVALID_PARAMETER);
980 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result)); 975 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
981 EXPECT_EQ("unsubscribe result: true", script_result); 976 EXPECT_EQ("unsubscribe result: true", script_result);
982 977
983 // Unsubscribing (with an existing reference to a PushSubscription), after 978 // Unsubscribing (with an existing reference to a PushSubscription), after
984 // unregistering the Service Worker, just means push subscription isn't found. 979 // unregistering the Service Worker, just means push subscription isn't found.
985 TryToSubscribeSuccessfully("1-3" /* expected_push_subscription_id */, false); 980 std::string token4;
981 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully(false /* use_key */, &token4));
982 EXPECT_NE(token1, token4);
983 EXPECT_NE(token2, token4);
984 EXPECT_NE(token3, token4);
986 ASSERT_TRUE(RunScript("unregisterServiceWorker()", &script_result)); 985 ASSERT_TRUE(RunScript("unregisterServiceWorker()", &script_result));
987 ASSERT_EQ("service worker unregistration status: true", script_result); 986 ASSERT_EQ("service worker unregistration status: true", script_result);
988 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result)); 987 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
989 EXPECT_EQ("unsubscribe result: false", script_result); 988 EXPECT_EQ("unsubscribe result: false", script_result);
990 } 989 }
991 990
992 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, 991 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
993 GlobalResetPushPermissionUnsubscribes) { 992 GlobalResetPushPermissionUnsubscribes) {
994 std::string script_result; 993 std::string script_result;
995 994
996 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */); 995 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully());
997 996
998 ASSERT_TRUE(RunScript("hasSubscription()", &script_result)); 997 ASSERT_TRUE(RunScript("hasSubscription()", &script_result));
999 EXPECT_EQ("true - subscribed", script_result); 998 EXPECT_EQ("true - subscribed", script_result);
1000 999
1001 ASSERT_TRUE(RunScript("permissionState()", &script_result)); 1000 ASSERT_TRUE(RunScript("permissionState()", &script_result));
1002 EXPECT_EQ("permission status - granted", script_result); 1001 EXPECT_EQ("permission status - granted", script_result);
1003 1002
1004 scoped_refptr<content::MessageLoopRunner> message_loop_runner = 1003 scoped_refptr<content::MessageLoopRunner> message_loop_runner =
1005 new content::MessageLoopRunner; 1004 new content::MessageLoopRunner;
1006 push_service()->SetContentSettingChangedCallbackForTesting( 1005 push_service()->SetContentSettingChangedCallbackForTesting(
(...skipping 11 matching lines...) Expand all
1018 EXPECT_EQ("permission status - prompt", script_result); 1017 EXPECT_EQ("permission status - prompt", script_result);
1019 1018
1020 ASSERT_TRUE(RunScript("hasSubscription()", &script_result)); 1019 ASSERT_TRUE(RunScript("hasSubscription()", &script_result));
1021 EXPECT_EQ("false - not subscribed", script_result); 1020 EXPECT_EQ("false - not subscribed", script_result);
1022 } 1021 }
1023 1022
1024 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, 1023 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
1025 LocalResetPushPermissionUnsubscribes) { 1024 LocalResetPushPermissionUnsubscribes) {
1026 std::string script_result; 1025 std::string script_result;
1027 1026
1028 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */); 1027 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully());
1029 1028
1030 ASSERT_TRUE(RunScript("hasSubscription()", &script_result)); 1029 ASSERT_TRUE(RunScript("hasSubscription()", &script_result));
1031 EXPECT_EQ("true - subscribed", script_result); 1030 EXPECT_EQ("true - subscribed", script_result);
1032 1031
1033 ASSERT_TRUE(RunScript("permissionState()", &script_result)); 1032 ASSERT_TRUE(RunScript("permissionState()", &script_result));
1034 EXPECT_EQ("permission status - granted", script_result); 1033 EXPECT_EQ("permission status - granted", script_result);
1035 1034
1036 scoped_refptr<content::MessageLoopRunner> message_loop_runner = 1035 scoped_refptr<content::MessageLoopRunner> message_loop_runner =
1037 new content::MessageLoopRunner; 1036 new content::MessageLoopRunner;
1038 push_service()->SetContentSettingChangedCallbackForTesting( 1037 push_service()->SetContentSettingChangedCallbackForTesting(
(...skipping 15 matching lines...) Expand all
1054 EXPECT_EQ("permission status - prompt", script_result); 1053 EXPECT_EQ("permission status - prompt", script_result);
1055 1054
1056 ASSERT_TRUE(RunScript("hasSubscription()", &script_result)); 1055 ASSERT_TRUE(RunScript("hasSubscription()", &script_result));
1057 EXPECT_EQ("false - not subscribed", script_result); 1056 EXPECT_EQ("false - not subscribed", script_result);
1058 } 1057 }
1059 1058
1060 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, 1059 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
1061 DenyPushPermissionUnsubscribes) { 1060 DenyPushPermissionUnsubscribes) {
1062 std::string script_result; 1061 std::string script_result;
1063 1062
1064 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */); 1063 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully());
1065 1064
1066 ASSERT_TRUE(RunScript("hasSubscription()", &script_result)); 1065 ASSERT_TRUE(RunScript("hasSubscription()", &script_result));
1067 EXPECT_EQ("true - subscribed", script_result); 1066 EXPECT_EQ("true - subscribed", script_result);
1068 1067
1069 ASSERT_TRUE(RunScript("permissionState()", &script_result)); 1068 ASSERT_TRUE(RunScript("permissionState()", &script_result));
1070 EXPECT_EQ("permission status - granted", script_result); 1069 EXPECT_EQ("permission status - granted", script_result);
1071 1070
1072 scoped_refptr<content::MessageLoopRunner> message_loop_runner = 1071 scoped_refptr<content::MessageLoopRunner> message_loop_runner =
1073 new content::MessageLoopRunner; 1072 new content::MessageLoopRunner;
1074 push_service()->SetContentSettingChangedCallbackForTesting( 1073 push_service()->SetContentSettingChangedCallbackForTesting(
(...skipping 11 matching lines...) Expand all
1086 EXPECT_EQ("permission status - denied", script_result); 1085 EXPECT_EQ("permission status - denied", script_result);
1087 1086
1088 ASSERT_TRUE(RunScript("hasSubscription()", &script_result)); 1087 ASSERT_TRUE(RunScript("hasSubscription()", &script_result));
1089 EXPECT_EQ("false - not subscribed", script_result); 1088 EXPECT_EQ("false - not subscribed", script_result);
1090 } 1089 }
1091 1090
1092 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, 1091 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
1093 GlobalResetNotificationsPermissionUnsubscribes) { 1092 GlobalResetNotificationsPermissionUnsubscribes) {
1094 std::string script_result; 1093 std::string script_result;
1095 1094
1096 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */); 1095 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully());
1097 1096
1098 ASSERT_TRUE(RunScript("hasSubscription()", &script_result)); 1097 ASSERT_TRUE(RunScript("hasSubscription()", &script_result));
1099 EXPECT_EQ("true - subscribed", script_result); 1098 EXPECT_EQ("true - subscribed", script_result);
1100 1099
1101 ASSERT_TRUE(RunScript("permissionState()", &script_result)); 1100 ASSERT_TRUE(RunScript("permissionState()", &script_result));
1102 EXPECT_EQ("permission status - granted", script_result); 1101 EXPECT_EQ("permission status - granted", script_result);
1103 1102
1104 scoped_refptr<content::MessageLoopRunner> message_loop_runner = 1103 scoped_refptr<content::MessageLoopRunner> message_loop_runner =
1105 new content::MessageLoopRunner; 1104 new content::MessageLoopRunner;
1106 push_service()->SetContentSettingChangedCallbackForTesting( 1105 push_service()->SetContentSettingChangedCallbackForTesting(
1107 message_loop_runner->QuitClosure()); 1106 message_loop_runner->QuitClosure());
1108 1107
1109 HostContentSettingsMapFactory::GetForProfile(GetBrowser()->profile()) 1108 HostContentSettingsMapFactory::GetForProfile(GetBrowser()->profile())
1110 ->ClearSettingsForOneType(CONTENT_SETTINGS_TYPE_NOTIFICATIONS); 1109 ->ClearSettingsForOneType(CONTENT_SETTINGS_TYPE_NOTIFICATIONS);
1111 1110
1112 message_loop_runner->Run(); 1111 message_loop_runner->Run();
1113 1112
1114 ASSERT_TRUE(RunScript("permissionState()", &script_result)); 1113 ASSERT_TRUE(RunScript("permissionState()", &script_result));
1115 EXPECT_EQ("permission status - prompt", script_result); 1114 EXPECT_EQ("permission status - prompt", script_result);
1116 1115
1117 ASSERT_TRUE(RunScript("hasSubscription()", &script_result)); 1116 ASSERT_TRUE(RunScript("hasSubscription()", &script_result));
1118 EXPECT_EQ("false - not subscribed", script_result); 1117 EXPECT_EQ("false - not subscribed", script_result);
1119 } 1118 }
1120 1119
1121 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, 1120 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
1122 LocalResetNotificationsPermissionUnsubscribes) { 1121 LocalResetNotificationsPermissionUnsubscribes) {
1123 std::string script_result; 1122 std::string script_result;
1124 1123
1125 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */); 1124 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully());
1126 1125
1127 ASSERT_TRUE(RunScript("hasSubscription()", &script_result)); 1126 ASSERT_TRUE(RunScript("hasSubscription()", &script_result));
1128 EXPECT_EQ("true - subscribed", script_result); 1127 EXPECT_EQ("true - subscribed", script_result);
1129 1128
1130 ASSERT_TRUE(RunScript("permissionState()", &script_result)); 1129 ASSERT_TRUE(RunScript("permissionState()", &script_result));
1131 EXPECT_EQ("permission status - granted", script_result); 1130 EXPECT_EQ("permission status - granted", script_result);
1132 1131
1133 scoped_refptr<content::MessageLoopRunner> message_loop_runner = 1132 scoped_refptr<content::MessageLoopRunner> message_loop_runner =
1134 new content::MessageLoopRunner; 1133 new content::MessageLoopRunner;
1135 push_service()->SetContentSettingChangedCallbackForTesting( 1134 push_service()->SetContentSettingChangedCallbackForTesting(
(...skipping 11 matching lines...) Expand all
1147 EXPECT_EQ("permission status - prompt", script_result); 1146 EXPECT_EQ("permission status - prompt", script_result);
1148 1147
1149 ASSERT_TRUE(RunScript("hasSubscription()", &script_result)); 1148 ASSERT_TRUE(RunScript("hasSubscription()", &script_result));
1150 EXPECT_EQ("false - not subscribed", script_result); 1149 EXPECT_EQ("false - not subscribed", script_result);
1151 } 1150 }
1152 1151
1153 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, 1152 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
1154 DenyNotificationsPermissionUnsubscribes) { 1153 DenyNotificationsPermissionUnsubscribes) {
1155 std::string script_result; 1154 std::string script_result;
1156 1155
1157 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */); 1156 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully());
1158 1157
1159 ASSERT_TRUE(RunScript("hasSubscription()", &script_result)); 1158 ASSERT_TRUE(RunScript("hasSubscription()", &script_result));
1160 EXPECT_EQ("true - subscribed", script_result); 1159 EXPECT_EQ("true - subscribed", script_result);
1161 1160
1162 ASSERT_TRUE(RunScript("permissionState()", &script_result)); 1161 ASSERT_TRUE(RunScript("permissionState()", &script_result));
1163 EXPECT_EQ("permission status - granted", script_result); 1162 EXPECT_EQ("permission status - granted", script_result);
1164 1163
1165 scoped_refptr<content::MessageLoopRunner> message_loop_runner = 1164 scoped_refptr<content::MessageLoopRunner> message_loop_runner =
1166 new content::MessageLoopRunner; 1165 new content::MessageLoopRunner;
1167 push_service()->SetContentSettingChangedCallbackForTesting( 1166 push_service()->SetContentSettingChangedCallbackForTesting(
(...skipping 11 matching lines...) Expand all
1179 EXPECT_EQ("permission status - denied", script_result); 1178 EXPECT_EQ("permission status - denied", script_result);
1180 1179
1181 ASSERT_TRUE(RunScript("hasSubscription()", &script_result)); 1180 ASSERT_TRUE(RunScript("hasSubscription()", &script_result));
1182 EXPECT_EQ("false - not subscribed", script_result); 1181 EXPECT_EQ("false - not subscribed", script_result);
1183 } 1182 }
1184 1183
1185 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, 1184 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
1186 GrantAlreadyGrantedPermissionDoesNotUnsubscribe) { 1185 GrantAlreadyGrantedPermissionDoesNotUnsubscribe) {
1187 std::string script_result; 1186 std::string script_result;
1188 1187
1189 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */); 1188 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully());
1190 1189
1191 ASSERT_TRUE(RunScript("hasSubscription()", &script_result)); 1190 ASSERT_TRUE(RunScript("hasSubscription()", &script_result));
1192 EXPECT_EQ("true - subscribed", script_result); 1191 EXPECT_EQ("true - subscribed", script_result);
1193 1192
1194 ASSERT_TRUE(RunScript("permissionState()", &script_result)); 1193 ASSERT_TRUE(RunScript("permissionState()", &script_result));
1195 EXPECT_EQ("permission status - granted", script_result); 1194 EXPECT_EQ("permission status - granted", script_result);
1196 1195
1197 scoped_refptr<content::MessageLoopRunner> message_loop_runner = 1196 scoped_refptr<content::MessageLoopRunner> message_loop_runner =
1198 new content::MessageLoopRunner; 1197 new content::MessageLoopRunner;
1199 push_service()->SetContentSettingChangedCallbackForTesting( 1198 push_service()->SetContentSettingChangedCallbackForTesting(
(...skipping 19 matching lines...) Expand all
1219 } 1218 }
1220 1219
1221 // This test is testing some non-trivial content settings rules and make sure 1220 // This test is testing some non-trivial content settings rules and make sure
1222 // that they are respected with regards to automatic unsubscription. In other 1221 // that they are respected with regards to automatic unsubscription. In other
1223 // words, it checks that the push service does not end up unsubscribing origins 1222 // words, it checks that the push service does not end up unsubscribing origins
1224 // that have push permission with some non-common rules. 1223 // that have push permission with some non-common rules.
1225 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, 1224 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
1226 AutomaticUnsubscriptionFollowsContentSettingRules) { 1225 AutomaticUnsubscriptionFollowsContentSettingRules) {
1227 std::string script_result; 1226 std::string script_result;
1228 1227
1229 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */); 1228 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully());
1230 1229
1231 ASSERT_TRUE(RunScript("hasSubscription()", &script_result)); 1230 ASSERT_TRUE(RunScript("hasSubscription()", &script_result));
1232 EXPECT_EQ("true - subscribed", script_result); 1231 EXPECT_EQ("true - subscribed", script_result);
1233 1232
1234 ASSERT_TRUE(RunScript("permissionState()", &script_result)); 1233 ASSERT_TRUE(RunScript("permissionState()", &script_result));
1235 EXPECT_EQ("permission status - granted", script_result); 1234 EXPECT_EQ("permission status - granted", script_result);
1236 1235
1237 scoped_refptr<content::MessageLoopRunner> message_loop_runner = 1236 scoped_refptr<content::MessageLoopRunner> message_loop_runner =
1238 new content::MessageLoopRunner; 1237 new content::MessageLoopRunner;
1239 push_service()->SetContentSettingChangedCallbackForTesting( 1238 push_service()->SetContentSettingChangedCallbackForTesting(
(...skipping 25 matching lines...) Expand all
1265 // The Push service should not unsubcribe |origin| because at no point it was 1264 // The Push service should not unsubcribe |origin| because at no point it was
1266 // left without permission to use Push. 1265 // left without permission to use Push.
1267 1266
1268 ASSERT_TRUE(RunScript("permissionState()", &script_result)); 1267 ASSERT_TRUE(RunScript("permissionState()", &script_result));
1269 EXPECT_EQ("permission status - granted", script_result); 1268 EXPECT_EQ("permission status - granted", script_result);
1270 1269
1271 ASSERT_TRUE(RunScript("hasSubscription()", &script_result)); 1270 ASSERT_TRUE(RunScript("hasSubscription()", &script_result));
1272 EXPECT_EQ("true - subscribed", script_result); 1271 EXPECT_EQ("true - subscribed", script_result);
1273 } 1272 }
1274 1273
1275 // Checks that automatically unsubscribing due to a revoked permission is 1274 // Checks automatically unsubscribing due to a revoked permission after
1276 // handled well if the sender ID needed to unsubscribe was already deleted. 1275 // previously clearing site data.
1277 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, 1276 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
1278 ResetPushPermissionAfterClearingSiteData) { 1277 ResetPushPermissionAfterClearingSiteData) {
1279 std::string script_result; 1278 std::string script_result;
1280 1279
1281 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */); 1280 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully());
1282 1281
1283 PushMessagingAppIdentifier app_identifier = 1282 PushMessagingAppIdentifier app_identifier =
1284 GetAppIdentifierForServiceWorkerRegistration(0LL); 1283 GetAppIdentifierForServiceWorkerRegistration(0LL);
1285 EXPECT_EQ(app_identifier.app_id(), gcm_service()->last_registered_app_id());
1286 PushMessagingAppIdentifier stored_app_identifier = 1284 PushMessagingAppIdentifier stored_app_identifier =
1287 PushMessagingAppIdentifier::FindByAppId(GetBrowser()->profile(), 1285 PushMessagingAppIdentifier::FindByAppId(GetBrowser()->profile(),
1288 app_identifier.app_id()); 1286 app_identifier.app_id());
1289 EXPECT_FALSE(stored_app_identifier.is_null()); 1287 EXPECT_FALSE(stored_app_identifier.is_null());
1290 1288
1291 // Simulate a user clearing site data (including Service Workers, crucially). 1289 // Simulate a user clearing site data (including Service Workers, crucially).
1292 BrowsingDataRemover* remover = 1290 BrowsingDataRemover* remover =
1293 BrowsingDataRemoverFactory::GetForBrowserContext(GetBrowser()->profile()); 1291 BrowsingDataRemoverFactory::GetForBrowserContext(GetBrowser()->profile());
1294 BrowsingDataRemoverCompletionObserver observer(remover); 1292 BrowsingDataRemoverCompletionObserver observer(remover);
1295 remover->Remove(BrowsingDataRemover::Unbounded(), 1293 remover->Remove(BrowsingDataRemover::Unbounded(),
(...skipping 16 matching lines...) Expand all
1312 run_loop.Run(); 1310 run_loop.Run();
1313 1311
1314 // |app_identifier| should no longer be stored in prefs. 1312 // |app_identifier| should no longer be stored in prefs.
1315 PushMessagingAppIdentifier stored_app_identifier2 = 1313 PushMessagingAppIdentifier stored_app_identifier2 =
1316 PushMessagingAppIdentifier::FindByAppId(GetBrowser()->profile(), 1314 PushMessagingAppIdentifier::FindByAppId(GetBrowser()->profile(),
1317 app_identifier.app_id()); 1315 app_identifier.app_id());
1318 EXPECT_TRUE(stored_app_identifier2.is_null()); 1316 EXPECT_TRUE(stored_app_identifier2.is_null());
1319 } 1317 }
1320 1318
1321 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, EncryptionKeyUniqueness) { 1319 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, EncryptionKeyUniqueness) {
1322 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */, false); 1320 std::string token1;
1321 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully(false /* use_key */, &token1));
1323 1322
1324 std::string first_public_key; 1323 std::string first_public_key;
1325 ASSERT_TRUE(RunScript("GetP256dh()", &first_public_key)); 1324 ASSERT_TRUE(RunScript("GetP256dh()", &first_public_key));
1326 EXPECT_GE(first_public_key.size(), 32u); 1325 EXPECT_GE(first_public_key.size(), 32u);
1327 1326
1328 std::string script_result; 1327 std::string script_result;
1329 gcm_service()->AddExpectedUnregisterResponse(gcm::GCMClient::SUCCESS);
1330 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result)); 1328 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
1331 EXPECT_EQ("unsubscribe result: true", script_result); 1329 EXPECT_EQ("unsubscribe result: true", script_result);
1332 1330
1333 TryToSubscribeSuccessfully("1-1" /* expected_push_subscription_id */); 1331 std::string token2;
1332 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully(true /* use_key */, &token2));
1333 EXPECT_NE(token1, token2);
1334 1334
1335 std::string second_public_key; 1335 std::string second_public_key;
1336 ASSERT_TRUE(RunScript("GetP256dh()", &second_public_key)); 1336 ASSERT_TRUE(RunScript("GetP256dh()", &second_public_key));
1337 EXPECT_GE(second_public_key.size(), 32u); 1337 EXPECT_GE(second_public_key.size(), 32u);
1338 1338
1339 EXPECT_NE(first_public_key, second_public_key); 1339 EXPECT_NE(first_public_key, second_public_key);
1340 } 1340 }
1341 1341
1342 class PushMessagingIncognitoBrowserTest : public PushMessagingBrowserTest { 1342 class PushMessagingIncognitoBrowserTest : public PushMessagingBrowserTest {
1343 public: 1343 public:
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
1375 #if BUILDFLAG(ENABLE_BACKGROUND) && !defined(OS_CHROMEOS) 1375 #if BUILDFLAG(ENABLE_BACKGROUND) && !defined(OS_CHROMEOS)
1376 // Push background mode is disabled by default. 1376 // Push background mode is disabled by default.
1377 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, 1377 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
1378 BackgroundModeDisabledByDefault) { 1378 BackgroundModeDisabledByDefault) {
1379 // Initially background mode is inactive. 1379 // Initially background mode is inactive.
1380 BackgroundModeManager* background_mode_manager = 1380 BackgroundModeManager* background_mode_manager =
1381 g_browser_process->background_mode_manager(); 1381 g_browser_process->background_mode_manager();
1382 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive()); 1382 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive());
1383 1383
1384 // Once there is a push subscription background mode is still inactive. 1384 // Once there is a push subscription background mode is still inactive.
1385 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */); 1385 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully());
1386 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive()); 1386 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive());
1387 1387
1388 // After dropping the last subscription it is still inactive. 1388 // After dropping the last subscription it is still inactive.
1389 std::string script_result; 1389 std::string script_result;
1390 gcm_service()->AddExpectedUnregisterResponse(gcm::GCMClient::SUCCESS);
1391 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result)); 1390 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
1392 EXPECT_EQ("unsubscribe result: true", script_result); 1391 EXPECT_EQ("unsubscribe result: true", script_result);
1393 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive()); 1392 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive());
1394 } 1393 }
1395 1394
1396 class PushMessagingBackgroundModeEnabledBrowserTest 1395 class PushMessagingBackgroundModeEnabledBrowserTest
1397 : public PushMessagingBrowserTest { 1396 : public PushMessagingBrowserTest {
1398 public: 1397 public:
1399 ~PushMessagingBackgroundModeEnabledBrowserTest() override {} 1398 ~PushMessagingBackgroundModeEnabledBrowserTest() override {}
1400 1399
1401 // PushMessagingBrowserTest: 1400 // PushMessagingBrowserTest:
1402 void SetUpCommandLine(base::CommandLine* command_line) override { 1401 void SetUpCommandLine(base::CommandLine* command_line) override {
1403 command_line->AppendSwitch(switches::kEnablePushApiBackgroundMode); 1402 command_line->AppendSwitch(switches::kEnablePushApiBackgroundMode);
1404 PushMessagingBrowserTest::SetUpCommandLine(command_line); 1403 PushMessagingBrowserTest::SetUpCommandLine(command_line);
1405 } 1404 }
1406 }; 1405 };
1407 1406
1408 // In this test the command line enables push background mode. 1407 // In this test the command line enables push background mode.
1409 IN_PROC_BROWSER_TEST_F(PushMessagingBackgroundModeEnabledBrowserTest, 1408 IN_PROC_BROWSER_TEST_F(PushMessagingBackgroundModeEnabledBrowserTest,
1410 BackgroundModeEnabledWithCommandLine) { 1409 BackgroundModeEnabledWithCommandLine) {
1411 // Initially background mode is inactive. 1410 // Initially background mode is inactive.
1412 BackgroundModeManager* background_mode_manager = 1411 BackgroundModeManager* background_mode_manager =
1413 g_browser_process->background_mode_manager(); 1412 g_browser_process->background_mode_manager();
1414 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive()); 1413 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive());
1415 1414
1416 // Once there is a push subscription background mode is active. 1415 // Once there is a push subscription background mode is active.
1417 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */); 1416 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully());
1418 ASSERT_TRUE(background_mode_manager->IsBackgroundModeActive()); 1417 ASSERT_TRUE(background_mode_manager->IsBackgroundModeActive());
1419 1418
1420 // Dropping the last subscription deactivates background mode. 1419 // Dropping the last subscription deactivates background mode.
1421 std::string script_result; 1420 std::string script_result;
1422 gcm_service()->AddExpectedUnregisterResponse(gcm::GCMClient::SUCCESS);
1423 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result)); 1421 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
1424 EXPECT_EQ("unsubscribe result: true", script_result); 1422 EXPECT_EQ("unsubscribe result: true", script_result);
1425 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive()); 1423 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive());
1426 } 1424 }
1427 1425
1428 class PushMessagingBackgroundModeDisabledBrowserTest 1426 class PushMessagingBackgroundModeDisabledBrowserTest
1429 : public PushMessagingBrowserTest { 1427 : public PushMessagingBrowserTest {
1430 public: 1428 public:
1431 ~PushMessagingBackgroundModeDisabledBrowserTest() override {} 1429 ~PushMessagingBackgroundModeDisabledBrowserTest() override {}
1432 1430
1433 // PushMessagingBrowserTest: 1431 // PushMessagingBrowserTest:
1434 void SetUpCommandLine(base::CommandLine* command_line) override { 1432 void SetUpCommandLine(base::CommandLine* command_line) override {
1435 command_line->AppendSwitch(switches::kDisablePushApiBackgroundMode); 1433 command_line->AppendSwitch(switches::kDisablePushApiBackgroundMode);
1436 PushMessagingBrowserTest::SetUpCommandLine(command_line); 1434 PushMessagingBrowserTest::SetUpCommandLine(command_line);
1437 } 1435 }
1438 }; 1436 };
1439 1437
1440 // In this test the command line disables push background mode. 1438 // In this test the command line disables push background mode.
1441 IN_PROC_BROWSER_TEST_F(PushMessagingBackgroundModeDisabledBrowserTest, 1439 IN_PROC_BROWSER_TEST_F(PushMessagingBackgroundModeDisabledBrowserTest,
1442 BackgroundModeDisabledWithCommandLine) { 1440 BackgroundModeDisabledWithCommandLine) {
1443 // Initially background mode is inactive. 1441 // Initially background mode is inactive.
1444 BackgroundModeManager* background_mode_manager = 1442 BackgroundModeManager* background_mode_manager =
1445 g_browser_process->background_mode_manager(); 1443 g_browser_process->background_mode_manager();
1446 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive()); 1444 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive());
1447 1445
1448 // Once there is a push subscription background mode is still inactive. 1446 // Once there is a push subscription background mode is still inactive.
1449 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */); 1447 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully());
1450 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive()); 1448 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive());
1451 1449
1452 // After dropping the last subscription background mode is still inactive. 1450 // After dropping the last subscription background mode is still inactive.
1453 std::string script_result; 1451 std::string script_result;
1454 gcm_service()->AddExpectedUnregisterResponse(gcm::GCMClient::SUCCESS);
1455 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result)); 1452 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
1456 EXPECT_EQ("unsubscribe result: true", script_result); 1453 EXPECT_EQ("unsubscribe result: true", script_result);
1457 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive()); 1454 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive());
1458 } 1455 }
1459 #endif // BUILDFLAG(ENABLE_BACKGROUND) && !defined(OS_CHROMEOS) 1456 #endif // BUILDFLAG(ENABLE_BACKGROUND) && !defined(OS_CHROMEOS)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698