| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "content/public/browser/service_worker_context.h" | 5 #include "content/public/browser/service_worker_context.h" |
| 6 | 6 |
| 7 #include "base/files/scoped_temp_dir.h" | 7 #include "base/files/scoped_temp_dir.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "content/browser/browser_thread_impl.h" | 10 #include "content/browser/browser_thread_impl.h" |
| 11 #include "content/browser/service_worker/embedded_worker_registry.h" | 11 #include "content/browser/service_worker/embedded_worker_registry.h" |
| 12 #include "content/browser/service_worker/embedded_worker_test_helper.h" | 12 #include "content/browser/service_worker/embedded_worker_test_helper.h" |
| 13 #include "content/browser/service_worker/service_worker_context_core.h" | 13 #include "content/browser/service_worker/service_worker_context_core.h" |
| 14 #include "content/browser/service_worker/service_worker_context_observer.h" | 14 #include "content/browser/service_worker/service_worker_context_observer.h" |
| 15 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 15 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
| 16 #include "content/browser/service_worker/service_worker_provider_host.h" | 16 #include "content/browser/service_worker/service_worker_provider_host.h" |
| 17 #include "content/browser/service_worker/service_worker_registration.h" | 17 #include "content/browser/service_worker/service_worker_registration.h" |
| 18 #include "content/browser/service_worker/service_worker_storage.h" | 18 #include "content/browser/service_worker/service_worker_storage.h" |
| 19 #include "content/common/service_worker/embedded_worker_messages.h" | 19 #include "content/common/service_worker/embedded_worker_messages.h" |
| 20 #include "content/common/service_worker/service_worker_messages.h" | 20 #include "content/common/service_worker/service_worker_messages.h" |
| 21 #include "content/public/test/test_browser_thread_bundle.h" | 21 #include "content/public/test/test_browser_thread_bundle.h" |
| 22 #include "content/public/test/test_utils.h" | 22 #include "content/public/test/test_utils.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
| 24 | 24 |
| 25 namespace content { | 25 namespace content { |
| 26 | 26 |
| 27 namespace { | 27 namespace { |
| 28 | 28 |
| 29 void SaveResponseCallback(bool* called, | 29 void SaveResponseCallback(const scoped_refptr<MessageLoopRunner>& runner, |
| 30 int64* store_registration_id, | 30 int64* store_registration_id, |
| 31 ServiceWorkerStatusCode status, | 31 ServiceWorkerStatusCode status, |
| 32 const std::string& status_message, | 32 const std::string& status_message, |
| 33 int64 registration_id) { | 33 int64 registration_id) { |
| 34 EXPECT_EQ(SERVICE_WORKER_OK, status) << ServiceWorkerStatusToString(status); | 34 EXPECT_EQ(SERVICE_WORKER_OK, status) << ServiceWorkerStatusToString(status); |
| 35 *called = true; | |
| 36 *store_registration_id = registration_id; | 35 *store_registration_id = registration_id; |
| 36 runner->Quit(); |
| 37 } | 37 } |
| 38 | 38 |
| 39 ServiceWorkerContextCore::RegistrationCallback MakeRegisteredCallback( | 39 ServiceWorkerContextCore::RegistrationCallback MakeRegisteredCallback( |
| 40 bool* called, | 40 const scoped_refptr<MessageLoopRunner>& runner, |
| 41 int64* store_registration_id) { | 41 int64* store_registration_id) { |
| 42 return base::Bind(&SaveResponseCallback, called, store_registration_id); | 42 return base::Bind(&SaveResponseCallback, runner, store_registration_id); |
| 43 } | 43 } |
| 44 | 44 |
| 45 void CallCompletedCallback(bool* called, ServiceWorkerStatusCode) { | 45 void CallCompletedCallback(bool* called, ServiceWorkerStatusCode) { |
| 46 *called = true; | 46 *called = true; |
| 47 } | 47 } |
| 48 | 48 |
| 49 ServiceWorkerContextCore::UnregistrationCallback MakeUnregisteredCallback( | 49 ServiceWorkerContextCore::UnregistrationCallback MakeUnregisteredCallback( |
| 50 bool* called) { | 50 bool* called) { |
| 51 return base::Bind(&CallCompletedCallback, called); | 51 return base::Bind(&CallCompletedCallback, called); |
| 52 } | 52 } |
| (...skipping 18 matching lines...) Expand all Loading... |
| 71 | 71 |
| 72 if (expect_active) { | 72 if (expect_active) { |
| 73 EXPECT_TRUE(registration->active_version()); | 73 EXPECT_TRUE(registration->active_version()); |
| 74 } else { | 74 } else { |
| 75 EXPECT_FALSE(registration->active_version()); | 75 EXPECT_FALSE(registration->active_version()); |
| 76 } | 76 } |
| 77 } | 77 } |
| 78 | 78 |
| 79 class RejectInstallTestHelper : public EmbeddedWorkerTestHelper { | 79 class RejectInstallTestHelper : public EmbeddedWorkerTestHelper { |
| 80 public: | 80 public: |
| 81 explicit RejectInstallTestHelper(int mock_render_process_id) | 81 RejectInstallTestHelper() : EmbeddedWorkerTestHelper(base::FilePath()) {} |
| 82 : EmbeddedWorkerTestHelper(base::FilePath(), mock_render_process_id) {} | |
| 83 | 82 |
| 84 void OnInstallEvent(int embedded_worker_id, | 83 void OnInstallEvent(int embedded_worker_id, |
| 85 int request_id) override { | 84 int request_id) override { |
| 86 SimulateSend( | 85 SimulateSend( |
| 87 new ServiceWorkerHostMsg_InstallEventFinished( | 86 new ServiceWorkerHostMsg_InstallEventFinished( |
| 88 embedded_worker_id, request_id, | 87 embedded_worker_id, request_id, |
| 89 blink::WebServiceWorkerEventResultRejected)); | 88 blink::WebServiceWorkerEventResultRejected)); |
| 90 } | 89 } |
| 91 }; | 90 }; |
| 92 | 91 |
| 93 class RejectActivateTestHelper : public EmbeddedWorkerTestHelper { | 92 class RejectActivateTestHelper : public EmbeddedWorkerTestHelper { |
| 94 public: | 93 public: |
| 95 explicit RejectActivateTestHelper(int mock_render_process_id) | 94 explicit RejectActivateTestHelper() |
| 96 : EmbeddedWorkerTestHelper(base::FilePath(), mock_render_process_id) {} | 95 : EmbeddedWorkerTestHelper(base::FilePath()) {} |
| 97 | 96 |
| 98 void OnActivateEvent(int embedded_worker_id, int request_id) override { | 97 void OnActivateEvent(int embedded_worker_id, int request_id) override { |
| 99 SimulateSend( | 98 SimulateSend( |
| 100 new ServiceWorkerHostMsg_ActivateEventFinished( | 99 new ServiceWorkerHostMsg_ActivateEventFinished( |
| 101 embedded_worker_id, request_id, | 100 embedded_worker_id, request_id, |
| 102 blink::WebServiceWorkerEventResultRejected)); | 101 blink::WebServiceWorkerEventResultRejected)); |
| 103 } | 102 } |
| 104 }; | 103 }; |
| 105 | 104 |
| 106 enum NotificationType { | 105 enum NotificationType { |
| 107 REGISTRATION_STORED, | 106 REGISTRATION_STORED, |
| 108 REGISTRATION_DELETED, | 107 REGISTRATION_DELETED, |
| 109 STORAGE_RECOVERED, | 108 STORAGE_RECOVERED, |
| 110 }; | 109 }; |
| 111 | 110 |
| 112 struct NotificationLog { | 111 struct NotificationLog { |
| 113 NotificationType type; | 112 NotificationType type; |
| 114 GURL pattern; | 113 GURL pattern; |
| 115 int64 registration_id; | 114 int64 registration_id; |
| 116 }; | 115 }; |
| 117 | 116 |
| 118 } // namespace | 117 } // namespace |
| 119 | 118 |
| 120 class ServiceWorkerContextTest : public ServiceWorkerContextObserver, | 119 class ServiceWorkerContextTest : public ServiceWorkerContextObserver, |
| 121 public testing::Test { | 120 public testing::Test { |
| 122 public: | 121 public: |
| 123 ServiceWorkerContextTest() | 122 ServiceWorkerContextTest() |
| 124 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), | 123 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} |
| 125 render_process_id_(99) {} | |
| 126 | 124 |
| 127 void SetUp() override { | 125 void SetUp() override { |
| 128 helper_.reset( | 126 helper_.reset(new EmbeddedWorkerTestHelper(base::FilePath())); |
| 129 new EmbeddedWorkerTestHelper(base::FilePath(), render_process_id_)); | |
| 130 helper_->context_wrapper()->AddObserver(this); | 127 helper_->context_wrapper()->AddObserver(this); |
| 131 } | 128 } |
| 132 | 129 |
| 133 void TearDown() override { helper_.reset(); } | 130 void TearDown() override { helper_.reset(); } |
| 134 | 131 |
| 135 // ServiceWorkerContextObserver overrides. | 132 // ServiceWorkerContextObserver overrides. |
| 136 void OnRegistrationStored(int64 registration_id, | 133 void OnRegistrationStored(int64 registration_id, |
| 137 const GURL& pattern) override { | 134 const GURL& pattern) override { |
| 138 NotificationLog log; | 135 NotificationLog log; |
| 139 log.type = REGISTRATION_STORED; | 136 log.type = REGISTRATION_STORED; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 153 NotificationLog log; | 150 NotificationLog log; |
| 154 log.type = STORAGE_RECOVERED; | 151 log.type = STORAGE_RECOVERED; |
| 155 notifications_.push_back(log); | 152 notifications_.push_back(log); |
| 156 } | 153 } |
| 157 | 154 |
| 158 ServiceWorkerContextCore* context() { return helper_->context(); } | 155 ServiceWorkerContextCore* context() { return helper_->context(); } |
| 159 | 156 |
| 160 protected: | 157 protected: |
| 161 TestBrowserThreadBundle browser_thread_bundle_; | 158 TestBrowserThreadBundle browser_thread_bundle_; |
| 162 scoped_ptr<EmbeddedWorkerTestHelper> helper_; | 159 scoped_ptr<EmbeddedWorkerTestHelper> helper_; |
| 163 const int render_process_id_; | |
| 164 std::vector<NotificationLog> notifications_; | 160 std::vector<NotificationLog> notifications_; |
| 165 }; | 161 }; |
| 166 | 162 |
| 167 // Make sure basic registration is working. | 163 // Make sure basic registration is working. |
| 168 TEST_F(ServiceWorkerContextTest, Register) { | 164 TEST_F(ServiceWorkerContextTest, Register) { |
| 169 GURL pattern("http://www.example.com/"); | 165 GURL pattern("http://www.example.com/"); |
| 170 GURL script_url("http://www.example.com/service_worker.js"); | 166 GURL script_url("http://www.example.com/service_worker.js"); |
| 171 | 167 |
| 172 int64 registration_id = kInvalidServiceWorkerRegistrationId; | 168 int64 registration_id = kInvalidServiceWorkerRegistrationId; |
| 173 bool called = false; | 169 scoped_refptr<MessageLoopRunner> runner(new MessageLoopRunner); |
| 174 context()->RegisterServiceWorker( | 170 context()->RegisterServiceWorker( |
| 175 pattern, | 171 pattern, script_url, NULL, |
| 176 script_url, | 172 MakeRegisteredCallback(runner, ®istration_id)); |
| 177 NULL, | |
| 178 MakeRegisteredCallback(&called, ®istration_id)); | |
| 179 | 173 |
| 180 ASSERT_FALSE(called); | 174 runner->Run(); |
| 181 base::RunLoop().RunUntilIdle(); | |
| 182 EXPECT_TRUE(called); | |
| 183 | 175 |
| 184 EXPECT_EQ(4UL, helper_->ipc_sink()->message_count()); | 176 EXPECT_EQ(2UL, helper_->ipc_sink()->message_count()); |
| 185 EXPECT_TRUE(helper_->ipc_sink()->GetUniqueMessageMatching( | |
| 186 EmbeddedWorkerMsg_StartWorker::ID)); | |
| 187 EXPECT_TRUE(helper_->inner_ipc_sink()->GetUniqueMessageMatching( | 177 EXPECT_TRUE(helper_->inner_ipc_sink()->GetUniqueMessageMatching( |
| 188 ServiceWorkerMsg_InstallEvent::ID)); | 178 ServiceWorkerMsg_InstallEvent::ID)); |
| 189 EXPECT_TRUE(helper_->inner_ipc_sink()->GetUniqueMessageMatching( | 179 EXPECT_TRUE(helper_->inner_ipc_sink()->GetUniqueMessageMatching( |
| 190 ServiceWorkerMsg_ActivateEvent::ID)); | 180 ServiceWorkerMsg_ActivateEvent::ID)); |
| 191 EXPECT_TRUE(helper_->ipc_sink()->GetUniqueMessageMatching( | |
| 192 EmbeddedWorkerMsg_StopWorker::ID)); | |
| 193 EXPECT_NE(kInvalidServiceWorkerRegistrationId, registration_id); | 181 EXPECT_NE(kInvalidServiceWorkerRegistrationId, registration_id); |
| 194 | 182 |
| 195 ASSERT_EQ(1u, notifications_.size()); | 183 ASSERT_EQ(1u, notifications_.size()); |
| 196 EXPECT_EQ(REGISTRATION_STORED, notifications_[0].type); | 184 EXPECT_EQ(REGISTRATION_STORED, notifications_[0].type); |
| 197 EXPECT_EQ(pattern, notifications_[0].pattern); | 185 EXPECT_EQ(pattern, notifications_[0].pattern); |
| 198 EXPECT_EQ(registration_id, notifications_[0].registration_id); | 186 EXPECT_EQ(registration_id, notifications_[0].registration_id); |
| 199 | 187 |
| 200 context()->storage()->FindRegistrationForId( | 188 context()->storage()->FindRegistrationForId( |
| 201 registration_id, | 189 registration_id, |
| 202 pattern.GetOrigin(), | 190 pattern.GetOrigin(), |
| 203 base::Bind(&ExpectRegisteredWorkers, | 191 base::Bind(&ExpectRegisteredWorkers, |
| 204 SERVICE_WORKER_OK, | 192 SERVICE_WORKER_OK, |
| 205 false /* expect_waiting */, | 193 false /* expect_waiting */, |
| 206 true /* expect_active */)); | 194 true /* expect_active */)); |
| 207 base::RunLoop().RunUntilIdle(); | 195 base::RunLoop().RunUntilIdle(); |
| 208 } | 196 } |
| 209 | 197 |
| 210 // Test registration when the service worker rejects the install event. The | 198 // Test registration when the service worker rejects the install event. The |
| 211 // registration callback should indicate success, but there should be no waiting | 199 // registration callback should indicate success, but there should be no waiting |
| 212 // or active worker in the registration. | 200 // or active worker in the registration. |
| 213 TEST_F(ServiceWorkerContextTest, Register_RejectInstall) { | 201 TEST_F(ServiceWorkerContextTest, Register_RejectInstall) { |
| 214 GURL pattern("http://www.example.com/"); | 202 GURL pattern("http://www.example.com/"); |
| 215 GURL script_url("http://www.example.com/service_worker.js"); | 203 GURL script_url("http://www.example.com/service_worker.js"); |
| 216 | 204 |
| 217 helper_.reset(); // Make sure the process lookups stay overridden. | 205 helper_.reset(); // Make sure the process lookups stay overridden. |
| 218 helper_.reset(new RejectInstallTestHelper(render_process_id_)); | 206 helper_.reset(new RejectInstallTestHelper()); |
| 219 helper_->context_wrapper()->AddObserver(this); | 207 helper_->context_wrapper()->AddObserver(this); |
| 220 int64 registration_id = kInvalidServiceWorkerRegistrationId; | 208 int64 registration_id = kInvalidServiceWorkerRegistrationId; |
| 221 bool called = false; | 209 scoped_refptr<MessageLoopRunner> runner(new MessageLoopRunner); |
| 222 context()->RegisterServiceWorker( | 210 context()->RegisterServiceWorker( |
| 223 pattern, | 211 pattern, script_url, NULL, |
| 224 script_url, | 212 MakeRegisteredCallback(runner, ®istration_id)); |
| 225 NULL, | |
| 226 MakeRegisteredCallback(&called, ®istration_id)); | |
| 227 | 213 |
| 228 ASSERT_FALSE(called); | 214 runner->Run(); |
| 229 base::RunLoop().RunUntilIdle(); | |
| 230 EXPECT_TRUE(called); | |
| 231 | 215 |
| 232 EXPECT_EQ(3UL, helper_->ipc_sink()->message_count()); | 216 EXPECT_EQ(1UL, helper_->ipc_sink()->message_count()); |
| 233 EXPECT_TRUE(helper_->ipc_sink()->GetUniqueMessageMatching( | |
| 234 EmbeddedWorkerMsg_StartWorker::ID)); | |
| 235 EXPECT_TRUE(helper_->inner_ipc_sink()->GetUniqueMessageMatching( | 217 EXPECT_TRUE(helper_->inner_ipc_sink()->GetUniqueMessageMatching( |
| 236 ServiceWorkerMsg_InstallEvent::ID)); | 218 ServiceWorkerMsg_InstallEvent::ID)); |
| 237 EXPECT_FALSE(helper_->inner_ipc_sink()->GetUniqueMessageMatching( | 219 EXPECT_FALSE(helper_->inner_ipc_sink()->GetUniqueMessageMatching( |
| 238 ServiceWorkerMsg_ActivateEvent::ID)); | 220 ServiceWorkerMsg_ActivateEvent::ID)); |
| 239 EXPECT_TRUE(helper_->ipc_sink()->GetUniqueMessageMatching( | |
| 240 EmbeddedWorkerMsg_StopWorker::ID)); | |
| 241 EXPECT_NE(kInvalidServiceWorkerRegistrationId, registration_id); | 221 EXPECT_NE(kInvalidServiceWorkerRegistrationId, registration_id); |
| 242 | 222 |
| 243 ASSERT_EQ(1u, notifications_.size()); | 223 ASSERT_EQ(1u, notifications_.size()); |
| 244 EXPECT_EQ(REGISTRATION_STORED, notifications_[0].type); | 224 EXPECT_EQ(REGISTRATION_STORED, notifications_[0].type); |
| 245 EXPECT_EQ(pattern, notifications_[0].pattern); | 225 EXPECT_EQ(pattern, notifications_[0].pattern); |
| 246 EXPECT_EQ(registration_id, notifications_[0].registration_id); | 226 EXPECT_EQ(registration_id, notifications_[0].registration_id); |
| 247 | 227 |
| 248 context()->storage()->FindRegistrationForId( | 228 context()->storage()->FindRegistrationForId( |
| 249 registration_id, | 229 registration_id, |
| 250 pattern.GetOrigin(), | 230 pattern.GetOrigin(), |
| 251 base::Bind(&ExpectRegisteredWorkers, | 231 base::Bind(&ExpectRegisteredWorkers, |
| 252 SERVICE_WORKER_ERROR_NOT_FOUND, | 232 SERVICE_WORKER_ERROR_NOT_FOUND, |
| 253 false /* expect_waiting */, | 233 false /* expect_waiting */, |
| 254 false /* expect_active */)); | 234 false /* expect_active */)); |
| 255 base::RunLoop().RunUntilIdle(); | 235 base::RunLoop().RunUntilIdle(); |
| 256 } | 236 } |
| 257 | 237 |
| 258 // Test registration when the service worker rejects the activate event. The | 238 // Test registration when the service worker rejects the activate event. The |
| 259 // worker should be activated anyway. | 239 // worker should be activated anyway. |
| 260 TEST_F(ServiceWorkerContextTest, Register_RejectActivate) { | 240 TEST_F(ServiceWorkerContextTest, Register_RejectActivate) { |
| 261 GURL pattern("http://www.example.com/"); | 241 GURL pattern("http://www.example.com/"); |
| 262 GURL script_url("http://www.example.com/service_worker.js"); | 242 GURL script_url("http://www.example.com/service_worker.js"); |
| 263 | 243 |
| 264 helper_.reset(); | 244 helper_.reset(); |
| 265 helper_.reset(new RejectActivateTestHelper(render_process_id_)); | 245 helper_.reset(new RejectActivateTestHelper()); |
| 266 helper_->context_wrapper()->AddObserver(this); | 246 helper_->context_wrapper()->AddObserver(this); |
| 267 int64 registration_id = kInvalidServiceWorkerRegistrationId; | 247 int64 registration_id = kInvalidServiceWorkerRegistrationId; |
| 268 bool called = false; | 248 scoped_refptr<MessageLoopRunner> runner(new MessageLoopRunner); |
| 269 context()->RegisterServiceWorker( | 249 context()->RegisterServiceWorker( |
| 270 pattern, script_url, NULL, | 250 pattern, script_url, NULL, |
| 271 MakeRegisteredCallback(&called, ®istration_id)); | 251 MakeRegisteredCallback(runner, ®istration_id)); |
| 272 | 252 |
| 273 ASSERT_FALSE(called); | 253 runner->Run(); |
| 274 base::RunLoop().RunUntilIdle(); | |
| 275 EXPECT_TRUE(called); | |
| 276 | 254 |
| 277 EXPECT_EQ(4UL, helper_->ipc_sink()->message_count()); | 255 EXPECT_EQ(2UL, helper_->ipc_sink()->message_count()); |
| 278 EXPECT_TRUE(helper_->ipc_sink()->GetUniqueMessageMatching( | |
| 279 EmbeddedWorkerMsg_StartWorker::ID)); | |
| 280 EXPECT_TRUE(helper_->inner_ipc_sink()->GetUniqueMessageMatching( | 256 EXPECT_TRUE(helper_->inner_ipc_sink()->GetUniqueMessageMatching( |
| 281 ServiceWorkerMsg_InstallEvent::ID)); | 257 ServiceWorkerMsg_InstallEvent::ID)); |
| 282 EXPECT_TRUE(helper_->inner_ipc_sink()->GetUniqueMessageMatching( | 258 EXPECT_TRUE(helper_->inner_ipc_sink()->GetUniqueMessageMatching( |
| 283 ServiceWorkerMsg_ActivateEvent::ID)); | 259 ServiceWorkerMsg_ActivateEvent::ID)); |
| 284 EXPECT_TRUE(helper_->ipc_sink()->GetUniqueMessageMatching( | |
| 285 EmbeddedWorkerMsg_StopWorker::ID)); | |
| 286 EXPECT_NE(kInvalidServiceWorkerRegistrationId, registration_id); | 260 EXPECT_NE(kInvalidServiceWorkerRegistrationId, registration_id); |
| 287 | 261 |
| 288 ASSERT_EQ(1u, notifications_.size()); | 262 ASSERT_EQ(1u, notifications_.size()); |
| 289 EXPECT_EQ(REGISTRATION_STORED, notifications_[0].type); | 263 EXPECT_EQ(REGISTRATION_STORED, notifications_[0].type); |
| 290 EXPECT_EQ(pattern, notifications_[0].pattern); | 264 EXPECT_EQ(pattern, notifications_[0].pattern); |
| 291 EXPECT_EQ(registration_id, notifications_[0].registration_id); | 265 EXPECT_EQ(registration_id, notifications_[0].registration_id); |
| 292 | 266 |
| 293 context()->storage()->FindRegistrationForId( | 267 context()->storage()->FindRegistrationForId( |
| 294 registration_id, pattern.GetOrigin(), | 268 registration_id, pattern.GetOrigin(), |
| 295 base::Bind(&ExpectRegisteredWorkers, SERVICE_WORKER_OK, | 269 base::Bind(&ExpectRegisteredWorkers, SERVICE_WORKER_OK, |
| 296 false /* expect_waiting */, true /* expect_active */)); | 270 false /* expect_waiting */, true /* expect_active */)); |
| 297 base::RunLoop().RunUntilIdle(); | 271 base::RunLoop().RunUntilIdle(); |
| 298 } | 272 } |
| 299 | 273 |
| 300 // Make sure registrations are cleaned up when they are unregistered. | 274 // Make sure registrations are cleaned up when they are unregistered. |
| 301 TEST_F(ServiceWorkerContextTest, Unregister) { | 275 TEST_F(ServiceWorkerContextTest, Unregister) { |
| 302 GURL pattern("http://www.example.com/"); | 276 GURL pattern("http://www.example.com/"); |
| 303 | 277 |
| 304 bool called = false; | 278 scoped_refptr<MessageLoopRunner> runner(new MessageLoopRunner); |
| 305 int64 registration_id = kInvalidServiceWorkerRegistrationId; | 279 int64 registration_id = kInvalidServiceWorkerRegistrationId; |
| 306 context()->RegisterServiceWorker( | 280 context()->RegisterServiceWorker( |
| 307 pattern, | 281 pattern, GURL("http://www.example.com/service_worker.js"), NULL, |
| 308 GURL("http://www.example.com/service_worker.js"), | 282 MakeRegisteredCallback(runner, ®istration_id)); |
| 309 NULL, | |
| 310 MakeRegisteredCallback(&called, ®istration_id)); | |
| 311 | 283 |
| 312 ASSERT_FALSE(called); | 284 runner->Run(); |
| 313 base::RunLoop().RunUntilIdle(); | |
| 314 ASSERT_TRUE(called); | |
| 315 EXPECT_NE(kInvalidServiceWorkerRegistrationId, registration_id); | 285 EXPECT_NE(kInvalidServiceWorkerRegistrationId, registration_id); |
| 316 | 286 |
| 317 called = false; | 287 bool called = false; |
| 318 context()->UnregisterServiceWorker(pattern, | 288 context()->UnregisterServiceWorker(pattern, |
| 319 MakeUnregisteredCallback(&called)); | 289 MakeUnregisteredCallback(&called)); |
| 320 | 290 |
| 321 ASSERT_FALSE(called); | 291 ASSERT_FALSE(called); |
| 322 base::RunLoop().RunUntilIdle(); | 292 base::RunLoop().RunUntilIdle(); |
| 323 ASSERT_TRUE(called); | 293 ASSERT_TRUE(called); |
| 324 | 294 |
| 325 context()->storage()->FindRegistrationForId( | 295 context()->storage()->FindRegistrationForId( |
| 326 registration_id, | 296 registration_id, |
| 327 pattern.GetOrigin(), | 297 pattern.GetOrigin(), |
| (...skipping 12 matching lines...) Expand all Loading... |
| 340 EXPECT_EQ(registration_id, notifications_[1].registration_id); | 310 EXPECT_EQ(registration_id, notifications_[1].registration_id); |
| 341 } | 311 } |
| 342 | 312 |
| 343 // Make sure registrations are cleaned up when they are unregistered in bulk. | 313 // Make sure registrations are cleaned up when they are unregistered in bulk. |
| 344 TEST_F(ServiceWorkerContextTest, UnregisterMultiple) { | 314 TEST_F(ServiceWorkerContextTest, UnregisterMultiple) { |
| 345 GURL origin1_p1("http://www.example.com/test"); | 315 GURL origin1_p1("http://www.example.com/test"); |
| 346 GURL origin1_p2("http://www.example.com/hello"); | 316 GURL origin1_p2("http://www.example.com/hello"); |
| 347 GURL origin2_p1("http://www.example.com:8080/again"); | 317 GURL origin2_p1("http://www.example.com:8080/again"); |
| 348 GURL origin3_p1("http://www.other.com/"); | 318 GURL origin3_p1("http://www.other.com/"); |
| 349 | 319 |
| 350 bool called = false; | 320 scoped_refptr<MessageLoopRunner> runner1(new MessageLoopRunner); |
| 321 scoped_refptr<MessageLoopRunner> runner2(new MessageLoopRunner); |
| 322 scoped_refptr<MessageLoopRunner> runner3(new MessageLoopRunner); |
| 323 scoped_refptr<MessageLoopRunner> runner4(new MessageLoopRunner); |
| 351 int64 registration_id1 = kInvalidServiceWorkerRegistrationId; | 324 int64 registration_id1 = kInvalidServiceWorkerRegistrationId; |
| 352 int64 registration_id2 = kInvalidServiceWorkerRegistrationId; | 325 int64 registration_id2 = kInvalidServiceWorkerRegistrationId; |
| 353 int64 registration_id3 = kInvalidServiceWorkerRegistrationId; | 326 int64 registration_id3 = kInvalidServiceWorkerRegistrationId; |
| 354 int64 registration_id4 = kInvalidServiceWorkerRegistrationId; | 327 int64 registration_id4 = kInvalidServiceWorkerRegistrationId; |
| 355 context()->RegisterServiceWorker( | 328 context()->RegisterServiceWorker( |
| 356 origin1_p1, | 329 origin1_p1, GURL("http://www.example.com/service_worker.js"), NULL, |
| 357 GURL("http://www.example.com/service_worker.js"), | 330 MakeRegisteredCallback(runner1, ®istration_id1)); |
| 358 NULL, | |
| 359 MakeRegisteredCallback(&called, ®istration_id1)); | |
| 360 context()->RegisterServiceWorker( | 331 context()->RegisterServiceWorker( |
| 361 origin1_p2, | 332 origin1_p2, GURL("http://www.example.com/service_worker2.js"), NULL, |
| 362 GURL("http://www.example.com/service_worker2.js"), | 333 MakeRegisteredCallback(runner2, ®istration_id2)); |
| 363 NULL, | |
| 364 MakeRegisteredCallback(&called, ®istration_id2)); | |
| 365 context()->RegisterServiceWorker( | 334 context()->RegisterServiceWorker( |
| 366 origin2_p1, | 335 origin2_p1, GURL("http://www.example.com:8080/service_worker3.js"), NULL, |
| 367 GURL("http://www.example.com:8080/service_worker3.js"), | 336 MakeRegisteredCallback(runner3, ®istration_id3)); |
| 368 NULL, | |
| 369 MakeRegisteredCallback(&called, ®istration_id3)); | |
| 370 context()->RegisterServiceWorker( | 337 context()->RegisterServiceWorker( |
| 371 origin3_p1, | 338 origin3_p1, GURL("http://www.other.com/service_worker4.js"), NULL, |
| 372 GURL("http://www.other.com/service_worker4.js"), | 339 MakeRegisteredCallback(runner4, ®istration_id4)); |
| 373 NULL, | |
| 374 MakeRegisteredCallback(&called, ®istration_id4)); | |
| 375 | 340 |
| 376 ASSERT_FALSE(called); | 341 runner1->Run(); |
| 377 base::RunLoop().RunUntilIdle(); | 342 runner2->Run(); |
| 378 ASSERT_TRUE(called); | 343 runner3->Run(); |
| 344 runner4->Run(); |
| 379 | 345 |
| 380 EXPECT_NE(kInvalidServiceWorkerRegistrationId, registration_id1); | 346 EXPECT_NE(kInvalidServiceWorkerRegistrationId, registration_id1); |
| 381 EXPECT_NE(kInvalidServiceWorkerRegistrationId, registration_id2); | 347 EXPECT_NE(kInvalidServiceWorkerRegistrationId, registration_id2); |
| 382 EXPECT_NE(kInvalidServiceWorkerRegistrationId, registration_id3); | 348 EXPECT_NE(kInvalidServiceWorkerRegistrationId, registration_id3); |
| 383 EXPECT_NE(kInvalidServiceWorkerRegistrationId, registration_id4); | 349 EXPECT_NE(kInvalidServiceWorkerRegistrationId, registration_id4); |
| 384 | 350 |
| 385 called = false; | 351 bool called = false; |
| 386 context()->UnregisterServiceWorkers(origin1_p1.GetOrigin(), | 352 context()->UnregisterServiceWorkers(origin1_p1.GetOrigin(), |
| 387 MakeUnregisteredCallback(&called)); | 353 MakeUnregisteredCallback(&called)); |
| 388 | 354 |
| 389 ASSERT_FALSE(called); | 355 ASSERT_FALSE(called); |
| 390 base::RunLoop().RunUntilIdle(); | 356 base::RunLoop().RunUntilIdle(); |
| 391 ASSERT_TRUE(called); | 357 ASSERT_TRUE(called); |
| 392 | 358 |
| 393 context()->storage()->FindRegistrationForId( | 359 context()->storage()->FindRegistrationForId( |
| 394 registration_id1, | 360 registration_id1, |
| 395 origin1_p1.GetOrigin(), | 361 origin1_p1.GetOrigin(), |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 440 EXPECT_EQ(registration_id2, notifications_[4].registration_id); | 406 EXPECT_EQ(registration_id2, notifications_[4].registration_id); |
| 441 EXPECT_EQ(REGISTRATION_DELETED, notifications_[5].type); | 407 EXPECT_EQ(REGISTRATION_DELETED, notifications_[5].type); |
| 442 EXPECT_EQ(origin1_p1, notifications_[5].pattern); | 408 EXPECT_EQ(origin1_p1, notifications_[5].pattern); |
| 443 EXPECT_EQ(registration_id1, notifications_[5].registration_id); | 409 EXPECT_EQ(registration_id1, notifications_[5].registration_id); |
| 444 } | 410 } |
| 445 | 411 |
| 446 // Make sure registering a new script shares an existing registration. | 412 // Make sure registering a new script shares an existing registration. |
| 447 TEST_F(ServiceWorkerContextTest, RegisterNewScript) { | 413 TEST_F(ServiceWorkerContextTest, RegisterNewScript) { |
| 448 GURL pattern("http://www.example.com/"); | 414 GURL pattern("http://www.example.com/"); |
| 449 | 415 |
| 450 bool called = false; | 416 scoped_refptr<MessageLoopRunner> runner(new MessageLoopRunner); |
| 451 int64 old_registration_id = kInvalidServiceWorkerRegistrationId; | 417 int64 old_registration_id = kInvalidServiceWorkerRegistrationId; |
| 452 context()->RegisterServiceWorker( | 418 context()->RegisterServiceWorker( |
| 453 pattern, | 419 pattern, GURL("http://www.example.com/service_worker.js"), NULL, |
| 454 GURL("http://www.example.com/service_worker.js"), | 420 MakeRegisteredCallback(runner, &old_registration_id)); |
| 455 NULL, | |
| 456 MakeRegisteredCallback(&called, &old_registration_id)); | |
| 457 | 421 |
| 458 ASSERT_FALSE(called); | 422 runner->Run(); |
| 459 base::RunLoop().RunUntilIdle(); | |
| 460 ASSERT_TRUE(called); | |
| 461 EXPECT_NE(kInvalidServiceWorkerRegistrationId, old_registration_id); | 423 EXPECT_NE(kInvalidServiceWorkerRegistrationId, old_registration_id); |
| 462 | 424 |
| 463 called = false; | 425 runner = new MessageLoopRunner; |
| 464 int64 new_registration_id = kInvalidServiceWorkerRegistrationId; | 426 int64 new_registration_id = kInvalidServiceWorkerRegistrationId; |
| 465 context()->RegisterServiceWorker( | 427 context()->RegisterServiceWorker( |
| 466 pattern, | 428 pattern, GURL("http://www.example.com/service_worker_new.js"), NULL, |
| 467 GURL("http://www.example.com/service_worker_new.js"), | 429 MakeRegisteredCallback(runner, &new_registration_id)); |
| 468 NULL, | |
| 469 MakeRegisteredCallback(&called, &new_registration_id)); | |
| 470 | 430 |
| 471 ASSERT_FALSE(called); | 431 runner->Run(); |
| 472 base::RunLoop().RunUntilIdle(); | |
| 473 ASSERT_TRUE(called); | |
| 474 | 432 |
| 475 EXPECT_NE(kInvalidServiceWorkerRegistrationId, new_registration_id); | 433 EXPECT_NE(kInvalidServiceWorkerRegistrationId, new_registration_id); |
| 476 EXPECT_EQ(old_registration_id, new_registration_id); | 434 EXPECT_EQ(old_registration_id, new_registration_id); |
| 477 | 435 |
| 478 ASSERT_EQ(2u, notifications_.size()); | 436 ASSERT_EQ(2u, notifications_.size()); |
| 479 EXPECT_EQ(REGISTRATION_STORED, notifications_[0].type); | 437 EXPECT_EQ(REGISTRATION_STORED, notifications_[0].type); |
| 480 EXPECT_EQ(pattern, notifications_[0].pattern); | 438 EXPECT_EQ(pattern, notifications_[0].pattern); |
| 481 EXPECT_EQ(old_registration_id, notifications_[0].registration_id); | 439 EXPECT_EQ(old_registration_id, notifications_[0].registration_id); |
| 482 EXPECT_EQ(REGISTRATION_STORED, notifications_[1].type); | 440 EXPECT_EQ(REGISTRATION_STORED, notifications_[1].type); |
| 483 EXPECT_EQ(pattern, notifications_[1].pattern); | 441 EXPECT_EQ(pattern, notifications_[1].pattern); |
| 484 EXPECT_EQ(new_registration_id, notifications_[1].registration_id); | 442 EXPECT_EQ(new_registration_id, notifications_[1].registration_id); |
| 485 } | 443 } |
| 486 | 444 |
| 487 // Make sure that when registering a duplicate pattern+script_url | 445 // Make sure that when registering a duplicate pattern+script_url |
| 488 // combination, that the same registration is used. | 446 // combination, that the same registration is used. |
| 489 TEST_F(ServiceWorkerContextTest, RegisterDuplicateScript) { | 447 TEST_F(ServiceWorkerContextTest, RegisterDuplicateScript) { |
| 490 GURL pattern("http://www.example.com/"); | 448 GURL pattern("http://www.example.com/"); |
| 491 GURL script_url("http://www.example.com/service_worker.js"); | 449 GURL script_url("http://www.example.com/service_worker.js"); |
| 492 | 450 |
| 493 bool called = false; | 451 scoped_refptr<MessageLoopRunner> runner(new MessageLoopRunner); |
| 494 int64 old_registration_id = kInvalidServiceWorkerRegistrationId; | 452 int64 old_registration_id = kInvalidServiceWorkerRegistrationId; |
| 495 context()->RegisterServiceWorker( | 453 context()->RegisterServiceWorker( |
| 496 pattern, | 454 pattern, script_url, NULL, |
| 497 script_url, | 455 MakeRegisteredCallback(runner, &old_registration_id)); |
| 498 NULL, | |
| 499 MakeRegisteredCallback(&called, &old_registration_id)); | |
| 500 | 456 |
| 501 ASSERT_FALSE(called); | 457 runner->Run(); |
| 502 base::RunLoop().RunUntilIdle(); | |
| 503 ASSERT_TRUE(called); | |
| 504 EXPECT_NE(kInvalidServiceWorkerRegistrationId, old_registration_id); | 458 EXPECT_NE(kInvalidServiceWorkerRegistrationId, old_registration_id); |
| 505 | 459 |
| 506 called = false; | 460 runner = new MessageLoopRunner; |
| 507 int64 new_registration_id = kInvalidServiceWorkerRegistrationId; | 461 int64 new_registration_id = kInvalidServiceWorkerRegistrationId; |
| 508 context()->RegisterServiceWorker( | 462 context()->RegisterServiceWorker( |
| 509 pattern, | 463 pattern, script_url, NULL, |
| 510 script_url, | 464 MakeRegisteredCallback(runner, &new_registration_id)); |
| 511 NULL, | |
| 512 MakeRegisteredCallback(&called, &new_registration_id)); | |
| 513 | 465 |
| 514 ASSERT_FALSE(called); | 466 runner->Run(); |
| 515 base::RunLoop().RunUntilIdle(); | |
| 516 ASSERT_TRUE(called); | |
| 517 EXPECT_EQ(old_registration_id, new_registration_id); | 467 EXPECT_EQ(old_registration_id, new_registration_id); |
| 518 | 468 |
| 519 ASSERT_EQ(2u, notifications_.size()); | 469 ASSERT_EQ(2u, notifications_.size()); |
| 520 EXPECT_EQ(REGISTRATION_STORED, notifications_[0].type); | 470 EXPECT_EQ(REGISTRATION_STORED, notifications_[0].type); |
| 521 EXPECT_EQ(pattern, notifications_[0].pattern); | 471 EXPECT_EQ(pattern, notifications_[0].pattern); |
| 522 EXPECT_EQ(old_registration_id, notifications_[0].registration_id); | 472 EXPECT_EQ(old_registration_id, notifications_[0].registration_id); |
| 523 EXPECT_EQ(REGISTRATION_STORED, notifications_[1].type); | 473 EXPECT_EQ(REGISTRATION_STORED, notifications_[1].type); |
| 524 EXPECT_EQ(pattern, notifications_[1].pattern); | 474 EXPECT_EQ(pattern, notifications_[1].pattern); |
| 525 EXPECT_EQ(old_registration_id, notifications_[1].registration_id); | 475 EXPECT_EQ(old_registration_id, notifications_[1].registration_id); |
| 526 } | 476 } |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 613 | 563 |
| 614 TEST_P(ServiceWorkerContextRecoveryTest, DeleteAndStartOver) { | 564 TEST_P(ServiceWorkerContextRecoveryTest, DeleteAndStartOver) { |
| 615 GURL pattern("http://www.example.com/"); | 565 GURL pattern("http://www.example.com/"); |
| 616 GURL script_url("http://www.example.com/service_worker.js"); | 566 GURL script_url("http://www.example.com/service_worker.js"); |
| 617 | 567 |
| 618 bool is_storage_on_disk = GetParam(); | 568 bool is_storage_on_disk = GetParam(); |
| 619 if (is_storage_on_disk) { | 569 if (is_storage_on_disk) { |
| 620 // Reinitialize the helper to test on-disk storage. | 570 // Reinitialize the helper to test on-disk storage. |
| 621 base::ScopedTempDir user_data_directory; | 571 base::ScopedTempDir user_data_directory; |
| 622 ASSERT_TRUE(user_data_directory.CreateUniqueTempDir()); | 572 ASSERT_TRUE(user_data_directory.CreateUniqueTempDir()); |
| 623 helper_.reset(new EmbeddedWorkerTestHelper(user_data_directory.path(), | 573 helper_.reset(new EmbeddedWorkerTestHelper(user_data_directory.path())); |
| 624 render_process_id_)); | |
| 625 helper_->context_wrapper()->AddObserver(this); | 574 helper_->context_wrapper()->AddObserver(this); |
| 626 } | 575 } |
| 627 | 576 |
| 628 int64 registration_id = kInvalidServiceWorkerRegistrationId; | 577 int64 registration_id = kInvalidServiceWorkerRegistrationId; |
| 629 bool called = false; | 578 scoped_refptr<MessageLoopRunner> runner(new MessageLoopRunner); |
| 630 context()->RegisterServiceWorker( | 579 context()->RegisterServiceWorker( |
| 631 pattern, | 580 pattern, script_url, NULL, |
| 632 script_url, | 581 MakeRegisteredCallback(runner, ®istration_id)); |
| 633 NULL, | |
| 634 MakeRegisteredCallback(&called, ®istration_id)); | |
| 635 | 582 |
| 636 ASSERT_FALSE(called); | 583 runner->Run(); |
| 637 base::RunLoop().RunUntilIdle(); | |
| 638 EXPECT_TRUE(called); | |
| 639 | 584 |
| 640 context()->storage()->FindRegistrationForId( | 585 context()->storage()->FindRegistrationForId( |
| 641 registration_id, | 586 registration_id, |
| 642 pattern.GetOrigin(), | 587 pattern.GetOrigin(), |
| 643 base::Bind(&ExpectRegisteredWorkers, | 588 base::Bind(&ExpectRegisteredWorkers, |
| 644 SERVICE_WORKER_OK, | 589 SERVICE_WORKER_OK, |
| 645 false /* expect_waiting */, | 590 false /* expect_waiting */, |
| 646 true /* expect_active */)); | 591 true /* expect_active */)); |
| 647 base::RunLoop().RunUntilIdle(); | 592 base::RunLoop().RunUntilIdle(); |
| 648 | 593 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 667 // registration should not be found. | 612 // registration should not be found. |
| 668 context()->storage()->FindRegistrationForId( | 613 context()->storage()->FindRegistrationForId( |
| 669 registration_id, | 614 registration_id, |
| 670 pattern.GetOrigin(), | 615 pattern.GetOrigin(), |
| 671 base::Bind(&ExpectRegisteredWorkers, | 616 base::Bind(&ExpectRegisteredWorkers, |
| 672 SERVICE_WORKER_ERROR_NOT_FOUND, | 617 SERVICE_WORKER_ERROR_NOT_FOUND, |
| 673 false /* expect_waiting */, | 618 false /* expect_waiting */, |
| 674 true /* expect_active */)); | 619 true /* expect_active */)); |
| 675 base::RunLoop().RunUntilIdle(); | 620 base::RunLoop().RunUntilIdle(); |
| 676 | 621 |
| 677 called = false; | 622 runner = new MessageLoopRunner; |
| 678 context()->RegisterServiceWorker( | 623 context()->RegisterServiceWorker( |
| 679 pattern, | 624 pattern, script_url, NULL, |
| 680 script_url, | 625 MakeRegisteredCallback(runner, ®istration_id)); |
| 681 NULL, | |
| 682 MakeRegisteredCallback(&called, ®istration_id)); | |
| 683 | 626 |
| 684 ASSERT_FALSE(called); | 627 runner->Run(); |
| 685 base::RunLoop().RunUntilIdle(); | |
| 686 EXPECT_TRUE(called); | |
| 687 | 628 |
| 688 context()->storage()->FindRegistrationForId( | 629 context()->storage()->FindRegistrationForId( |
| 689 registration_id, | 630 registration_id, |
| 690 pattern.GetOrigin(), | 631 pattern.GetOrigin(), |
| 691 base::Bind(&ExpectRegisteredWorkers, | 632 base::Bind(&ExpectRegisteredWorkers, |
| 692 SERVICE_WORKER_OK, | 633 SERVICE_WORKER_OK, |
| 693 false /* expect_waiting */, | 634 false /* expect_waiting */, |
| 694 true /* expect_active */)); | 635 true /* expect_active */)); |
| 695 base::RunLoop().RunUntilIdle(); | 636 base::RunLoop().RunUntilIdle(); |
| 696 | 637 |
| 697 // The new context should take over next handle ids. | 638 // The new context should take over next handle ids. |
| 698 EXPECT_EQ(1, context()->GetNewServiceWorkerHandleId()); | 639 EXPECT_EQ(1, context()->GetNewServiceWorkerHandleId()); |
| 699 EXPECT_EQ(1, context()->GetNewRegistrationHandleId()); | 640 EXPECT_EQ(1, context()->GetNewRegistrationHandleId()); |
| 700 | 641 |
| 701 ASSERT_EQ(3u, notifications_.size()); | 642 ASSERT_EQ(3u, notifications_.size()); |
| 702 EXPECT_EQ(REGISTRATION_STORED, notifications_[0].type); | 643 EXPECT_EQ(REGISTRATION_STORED, notifications_[0].type); |
| 703 EXPECT_EQ(pattern, notifications_[0].pattern); | 644 EXPECT_EQ(pattern, notifications_[0].pattern); |
| 704 EXPECT_EQ(registration_id, notifications_[0].registration_id); | 645 EXPECT_EQ(registration_id, notifications_[0].registration_id); |
| 705 EXPECT_EQ(STORAGE_RECOVERED, notifications_[1].type); | 646 EXPECT_EQ(STORAGE_RECOVERED, notifications_[1].type); |
| 706 EXPECT_EQ(REGISTRATION_STORED, notifications_[2].type); | 647 EXPECT_EQ(REGISTRATION_STORED, notifications_[2].type); |
| 707 EXPECT_EQ(pattern, notifications_[2].pattern); | 648 EXPECT_EQ(pattern, notifications_[2].pattern); |
| 708 EXPECT_EQ(registration_id, notifications_[2].registration_id); | 649 EXPECT_EQ(registration_id, notifications_[2].registration_id); |
| 709 } | 650 } |
| 710 | 651 |
| 711 | 652 |
| 712 } // namespace content | 653 } // namespace content |
| OLD | NEW |