| OLD | NEW |
| 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 "base/files/scoped_temp_dir.h" | 5 #include "base/files/scoped_temp_dir.h" |
| 6 #include "base/logging.h" | 6 #include "base/logging.h" |
| 7 #include "base/run_loop.h" | 7 #include "base/run_loop.h" |
| 8 #include "content/browser/browser_thread_impl.h" | 8 #include "content/browser/browser_thread_impl.h" |
| 9 #include "content/browser/service_worker/embedded_worker_registry.h" |
| 10 #include "content/browser/service_worker/embedded_worker_test_helper.h" |
| 11 #include "content/browser/service_worker/service_worker_context_core.h" |
| 9 #include "content/browser/service_worker/service_worker_job_coordinator.h" | 12 #include "content/browser/service_worker/service_worker_job_coordinator.h" |
| 10 #include "content/browser/service_worker/service_worker_registration.h" | 13 #include "content/browser/service_worker/service_worker_registration.h" |
| 11 #include "content/browser/service_worker/service_worker_registration_status.h" | 14 #include "content/browser/service_worker/service_worker_registration_status.h" |
| 12 #include "content/public/test/test_browser_thread_bundle.h" | 15 #include "content/public/test/test_browser_thread_bundle.h" |
| 16 #include "ipc/ipc_test_sink.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 18 |
| 15 // Unit tests for testing all job registration tasks. | 19 // Unit tests for testing all job registration tasks. |
| 16 namespace content { | 20 namespace content { |
| 17 | 21 |
| 18 namespace { | 22 namespace { |
| 19 | 23 |
| 20 void SaveRegistrationCallback( | 24 void SaveRegistrationCallback( |
| 21 ServiceWorkerStatusCode expected_status, | 25 ServiceWorkerStatusCode expected_status, |
| 22 bool* called, | 26 bool* called, |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 bool* called) { | 85 bool* called) { |
| 82 *called = false; | 86 *called = false; |
| 83 return base::Bind(&SaveUnregistrationCallback, expected_status, called); | 87 return base::Bind(&SaveUnregistrationCallback, expected_status, called); |
| 84 } | 88 } |
| 85 | 89 |
| 86 } // namespace | 90 } // namespace |
| 87 | 91 |
| 88 class ServiceWorkerJobTest : public testing::Test { | 92 class ServiceWorkerJobTest : public testing::Test { |
| 89 public: | 93 public: |
| 90 ServiceWorkerJobTest() | 94 ServiceWorkerJobTest() |
| 91 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} | 95 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), |
| 96 render_process_id_(-1) {} |
| 92 | 97 |
| 93 virtual void SetUp() OVERRIDE { | 98 virtual void SetUp() OVERRIDE { |
| 94 storage_.reset(new ServiceWorkerStorage(base::FilePath(), NULL)); | 99 context_.reset(new ServiceWorkerContextCore(base::FilePath(), NULL)); |
| 95 job_coordinator_.reset(new ServiceWorkerJobCoordinator(storage_.get())); | 100 helper_.reset(new EmbeddedWorkerTestHelper(context_.get())); |
| 101 |
| 102 scoped_ptr<EmbeddedWorkerInstance> worker = |
| 103 context_->embedded_worker_registry()->CreateWorker(); |
| 104 |
| 105 render_process_id_ = 88; |
| 106 int embedded_worker_id = worker->embedded_worker_id(); |
| 107 helper_->SimulateAddProcess(embedded_worker_id, render_process_id_); |
| 96 } | 108 } |
| 97 | 109 |
| 98 virtual void TearDown() OVERRIDE { storage_.reset(); } | 110 virtual void TearDown() OVERRIDE { |
| 111 helper_.reset(); |
| 112 context_.reset(); |
| 113 } |
| 114 |
| 115 ServiceWorkerJobCoordinator* job_coordinator() const { |
| 116 return context_->job_coordinator(); |
| 117 } |
| 118 ServiceWorkerStorage* storage() const { return context_->storage(); } |
| 99 | 119 |
| 100 protected: | 120 protected: |
| 101 TestBrowserThreadBundle browser_thread_bundle_; | 121 TestBrowserThreadBundle browser_thread_bundle_; |
| 102 scoped_ptr<ServiceWorkerStorage> storage_; | 122 scoped_ptr<ServiceWorkerContextCore> context_; |
| 103 scoped_ptr<ServiceWorkerJobCoordinator> job_coordinator_; | 123 scoped_ptr<EmbeddedWorkerTestHelper> helper_; |
| 124 |
| 125 int render_process_id_; |
| 104 }; | 126 }; |
| 105 | 127 |
| 106 TEST_F(ServiceWorkerJobTest, SameDocumentSameRegistration) { | 128 TEST_F(ServiceWorkerJobTest, SameDocumentSameRegistration) { |
| 107 scoped_refptr<ServiceWorkerRegistration> original_registration; | 129 scoped_refptr<ServiceWorkerRegistration> original_registration; |
| 108 bool called; | 130 bool called; |
| 109 job_coordinator_->Register( | 131 job_coordinator()->Register( |
| 110 GURL("http://www.example.com/*"), | 132 GURL("http://www.example.com/*"), |
| 111 GURL("http://www.example.com/service_worker.js"), | 133 GURL("http://www.example.com/service_worker.js"), |
| 134 render_process_id_, |
| 112 SaveRegistration(SERVICE_WORKER_OK, &called, &original_registration)); | 135 SaveRegistration(SERVICE_WORKER_OK, &called, &original_registration)); |
| 113 EXPECT_FALSE(called); | 136 EXPECT_FALSE(called); |
| 114 base::RunLoop().RunUntilIdle(); | 137 base::RunLoop().RunUntilIdle(); |
| 115 EXPECT_TRUE(called); | 138 EXPECT_TRUE(called); |
| 116 | 139 |
| 117 scoped_refptr<ServiceWorkerRegistration> registration1; | 140 scoped_refptr<ServiceWorkerRegistration> registration1; |
| 118 storage_->FindRegistrationForDocument( | 141 storage()->FindRegistrationForDocument( |
| 119 GURL("http://www.example.com/"), | 142 GURL("http://www.example.com/"), |
| 120 SaveFoundRegistration(true, SERVICE_WORKER_OK, &called, ®istration1)); | 143 SaveFoundRegistration(true, SERVICE_WORKER_OK, &called, ®istration1)); |
| 121 scoped_refptr<ServiceWorkerRegistration> registration2; | 144 scoped_refptr<ServiceWorkerRegistration> registration2; |
| 122 storage_->FindRegistrationForDocument( | 145 storage()->FindRegistrationForDocument( |
| 123 GURL("http://www.example.com/"), | 146 GURL("http://www.example.com/"), |
| 124 SaveFoundRegistration(true, SERVICE_WORKER_OK, &called, ®istration2)); | 147 SaveFoundRegistration(true, SERVICE_WORKER_OK, &called, ®istration2)); |
| 125 | 148 |
| 126 ServiceWorkerRegistration* null_registration(NULL); | 149 ServiceWorkerRegistration* null_registration(NULL); |
| 127 ASSERT_EQ(null_registration, registration1); | 150 ASSERT_EQ(null_registration, registration1); |
| 128 ASSERT_EQ(null_registration, registration2); | 151 ASSERT_EQ(null_registration, registration2); |
| 129 EXPECT_FALSE(called); | 152 EXPECT_FALSE(called); |
| 130 base::RunLoop().RunUntilIdle(); | 153 base::RunLoop().RunUntilIdle(); |
| 131 EXPECT_TRUE(called); | 154 EXPECT_TRUE(called); |
| 132 ASSERT_NE(null_registration, registration1); | 155 ASSERT_NE(null_registration, registration1); |
| 133 ASSERT_NE(null_registration, registration2); | 156 ASSERT_NE(null_registration, registration2); |
| 134 | 157 |
| 135 ASSERT_EQ(registration1, registration2); | 158 ASSERT_EQ(registration1, registration2); |
| 136 } | 159 } |
| 137 | 160 |
| 138 TEST_F(ServiceWorkerJobTest, SameMatchSameRegistration) { | 161 TEST_F(ServiceWorkerJobTest, SameMatchSameRegistration) { |
| 139 bool called; | 162 bool called; |
| 140 scoped_refptr<ServiceWorkerRegistration> original_registration; | 163 scoped_refptr<ServiceWorkerRegistration> original_registration; |
| 141 job_coordinator_->Register( | 164 job_coordinator()->Register( |
| 142 GURL("http://www.example.com/*"), | 165 GURL("http://www.example.com/*"), |
| 143 GURL("http://www.example.com/service_worker.js"), | 166 GURL("http://www.example.com/service_worker.js"), |
| 167 render_process_id_, |
| 144 SaveRegistration(SERVICE_WORKER_OK, &called, &original_registration)); | 168 SaveRegistration(SERVICE_WORKER_OK, &called, &original_registration)); |
| 145 EXPECT_FALSE(called); | 169 EXPECT_FALSE(called); |
| 146 base::RunLoop().RunUntilIdle(); | 170 base::RunLoop().RunUntilIdle(); |
| 147 EXPECT_TRUE(called); | 171 EXPECT_TRUE(called); |
| 148 ASSERT_NE(static_cast<ServiceWorkerRegistration*>(NULL), | 172 ASSERT_NE(static_cast<ServiceWorkerRegistration*>(NULL), |
| 149 original_registration.get()); | 173 original_registration.get()); |
| 150 | 174 |
| 151 scoped_refptr<ServiceWorkerRegistration> registration1; | 175 scoped_refptr<ServiceWorkerRegistration> registration1; |
| 152 storage_->FindRegistrationForDocument( | 176 storage()->FindRegistrationForDocument( |
| 153 GURL("http://www.example.com/one"), | 177 GURL("http://www.example.com/one"), |
| 154 SaveFoundRegistration(true, SERVICE_WORKER_OK, &called, ®istration1)); | 178 SaveFoundRegistration(true, SERVICE_WORKER_OK, &called, ®istration1)); |
| 155 | 179 |
| 156 EXPECT_FALSE(called); | 180 EXPECT_FALSE(called); |
| 157 base::RunLoop().RunUntilIdle(); | 181 base::RunLoop().RunUntilIdle(); |
| 158 EXPECT_TRUE(called); | 182 EXPECT_TRUE(called); |
| 159 | 183 |
| 160 scoped_refptr<ServiceWorkerRegistration> registration2; | 184 scoped_refptr<ServiceWorkerRegistration> registration2; |
| 161 storage_->FindRegistrationForDocument( | 185 storage()->FindRegistrationForDocument( |
| 162 GURL("http://www.example.com/two"), | 186 GURL("http://www.example.com/two"), |
| 163 SaveFoundRegistration(true, SERVICE_WORKER_OK, &called, ®istration2)); | 187 SaveFoundRegistration(true, SERVICE_WORKER_OK, &called, ®istration2)); |
| 164 EXPECT_FALSE(called); | 188 EXPECT_FALSE(called); |
| 165 base::RunLoop().RunUntilIdle(); | 189 base::RunLoop().RunUntilIdle(); |
| 166 EXPECT_TRUE(called); | 190 EXPECT_TRUE(called); |
| 167 | 191 |
| 168 ASSERT_EQ(registration1, registration2); | 192 ASSERT_EQ(registration1, registration2); |
| 169 } | 193 } |
| 170 | 194 |
| 171 TEST_F(ServiceWorkerJobTest, DifferentMatchDifferentRegistration) { | 195 TEST_F(ServiceWorkerJobTest, DifferentMatchDifferentRegistration) { |
| 172 bool called1; | 196 bool called1; |
| 173 scoped_refptr<ServiceWorkerRegistration> original_registration1; | 197 scoped_refptr<ServiceWorkerRegistration> original_registration1; |
| 174 job_coordinator_->Register( | 198 job_coordinator()->Register( |
| 175 GURL("http://www.example.com/one/*"), | 199 GURL("http://www.example.com/one/*"), |
| 176 GURL("http://www.example.com/service_worker.js"), | 200 GURL("http://www.example.com/service_worker.js"), |
| 201 render_process_id_, |
| 177 SaveRegistration(SERVICE_WORKER_OK, &called1, &original_registration1)); | 202 SaveRegistration(SERVICE_WORKER_OK, &called1, &original_registration1)); |
| 178 | 203 |
| 179 bool called2; | 204 bool called2; |
| 180 scoped_refptr<ServiceWorkerRegistration> original_registration2; | 205 scoped_refptr<ServiceWorkerRegistration> original_registration2; |
| 181 job_coordinator_->Register( | 206 job_coordinator()->Register( |
| 182 GURL("http://www.example.com/two/*"), | 207 GURL("http://www.example.com/two/*"), |
| 183 GURL("http://www.example.com/service_worker.js"), | 208 GURL("http://www.example.com/service_worker.js"), |
| 209 render_process_id_, |
| 184 SaveRegistration(SERVICE_WORKER_OK, &called2, &original_registration2)); | 210 SaveRegistration(SERVICE_WORKER_OK, &called2, &original_registration2)); |
| 185 | 211 |
| 186 EXPECT_FALSE(called1); | 212 EXPECT_FALSE(called1); |
| 187 EXPECT_FALSE(called2); | 213 EXPECT_FALSE(called2); |
| 188 base::RunLoop().RunUntilIdle(); | 214 base::RunLoop().RunUntilIdle(); |
| 189 EXPECT_TRUE(called2); | 215 EXPECT_TRUE(called2); |
| 190 EXPECT_TRUE(called1); | 216 EXPECT_TRUE(called1); |
| 191 | 217 |
| 192 scoped_refptr<ServiceWorkerRegistration> registration1; | 218 scoped_refptr<ServiceWorkerRegistration> registration1; |
| 193 storage_->FindRegistrationForDocument( | 219 storage()->FindRegistrationForDocument( |
| 194 GURL("http://www.example.com/one/"), | 220 GURL("http://www.example.com/one/"), |
| 195 SaveFoundRegistration(true, SERVICE_WORKER_OK, &called1, ®istration1)); | 221 SaveFoundRegistration(true, SERVICE_WORKER_OK, &called1, ®istration1)); |
| 196 scoped_refptr<ServiceWorkerRegistration> registration2; | 222 scoped_refptr<ServiceWorkerRegistration> registration2; |
| 197 storage_->FindRegistrationForDocument( | 223 storage()->FindRegistrationForDocument( |
| 198 GURL("http://www.example.com/two/"), | 224 GURL("http://www.example.com/two/"), |
| 199 SaveFoundRegistration(true, SERVICE_WORKER_OK, &called2, ®istration2)); | 225 SaveFoundRegistration(true, SERVICE_WORKER_OK, &called2, ®istration2)); |
| 200 | 226 |
| 201 EXPECT_FALSE(called1); | 227 EXPECT_FALSE(called1); |
| 202 EXPECT_FALSE(called2); | 228 EXPECT_FALSE(called2); |
| 203 base::RunLoop().RunUntilIdle(); | 229 base::RunLoop().RunUntilIdle(); |
| 204 EXPECT_TRUE(called2); | 230 EXPECT_TRUE(called2); |
| 205 EXPECT_TRUE(called1); | 231 EXPECT_TRUE(called1); |
| 206 | 232 |
| 207 ASSERT_NE(registration1, registration2); | 233 ASSERT_NE(registration1, registration2); |
| 208 } | 234 } |
| 209 | 235 |
| 210 // Make sure basic registration is working. | 236 // Make sure basic registration is working. |
| 211 TEST_F(ServiceWorkerJobTest, Register) { | 237 TEST_F(ServiceWorkerJobTest, Register) { |
| 212 bool called = false; | 238 bool called = false; |
| 213 scoped_refptr<ServiceWorkerRegistration> registration; | 239 scoped_refptr<ServiceWorkerRegistration> registration; |
| 214 job_coordinator_->Register( | 240 job_coordinator()->Register( |
| 215 GURL("http://www.example.com/*"), | 241 GURL("http://www.example.com/*"), |
| 216 GURL("http://www.example.com/service_worker.js"), | 242 GURL("http://www.example.com/service_worker.js"), |
| 243 render_process_id_, |
| 217 SaveRegistration(SERVICE_WORKER_OK, &called, ®istration)); | 244 SaveRegistration(SERVICE_WORKER_OK, &called, ®istration)); |
| 218 | 245 |
| 219 ASSERT_FALSE(called); | 246 ASSERT_FALSE(called); |
| 220 base::RunLoop().RunUntilIdle(); | 247 base::RunLoop().RunUntilIdle(); |
| 221 ASSERT_TRUE(called); | 248 ASSERT_TRUE(called); |
| 222 | 249 |
| 223 ASSERT_NE(scoped_refptr<ServiceWorkerRegistration>(NULL), registration); | 250 ASSERT_NE(scoped_refptr<ServiceWorkerRegistration>(NULL), registration); |
| 224 } | 251 } |
| 225 | 252 |
| 226 // Make sure registrations are cleaned up when they are unregistered. | 253 // Make sure registrations are cleaned up when they are unregistered. |
| 227 TEST_F(ServiceWorkerJobTest, Unregister) { | 254 TEST_F(ServiceWorkerJobTest, Unregister) { |
| 228 GURL pattern("http://www.example.com/*"); | 255 GURL pattern("http://www.example.com/*"); |
| 229 | 256 |
| 230 bool called; | 257 bool called; |
| 231 scoped_refptr<ServiceWorkerRegistration> registration; | 258 scoped_refptr<ServiceWorkerRegistration> registration; |
| 232 job_coordinator_->Register( | 259 job_coordinator()->Register( |
| 233 pattern, | 260 pattern, |
| 234 GURL("http://www.example.com/service_worker.js"), | 261 GURL("http://www.example.com/service_worker.js"), |
| 262 render_process_id_, |
| 235 SaveRegistration(SERVICE_WORKER_OK, &called, ®istration)); | 263 SaveRegistration(SERVICE_WORKER_OK, &called, ®istration)); |
| 236 | 264 |
| 237 ASSERT_FALSE(called); | 265 ASSERT_FALSE(called); |
| 238 base::RunLoop().RunUntilIdle(); | 266 base::RunLoop().RunUntilIdle(); |
| 239 ASSERT_TRUE(called); | 267 ASSERT_TRUE(called); |
| 240 | 268 |
| 241 job_coordinator_->Unregister(pattern, | 269 job_coordinator()->Unregister(pattern, |
| 242 SaveUnregistration(SERVICE_WORKER_OK, &called)); | 270 render_process_id_, |
| 271 SaveUnregistration(SERVICE_WORKER_OK, &called)); |
| 243 | 272 |
| 244 ASSERT_FALSE(called); | 273 ASSERT_FALSE(called); |
| 245 base::RunLoop().RunUntilIdle(); | 274 base::RunLoop().RunUntilIdle(); |
| 246 ASSERT_TRUE(called); | 275 ASSERT_TRUE(called); |
| 247 | 276 |
| 248 ASSERT_TRUE(registration->HasOneRef()); | 277 ASSERT_TRUE(registration->HasOneRef()); |
| 249 | 278 |
| 250 storage_->FindRegistrationForPattern( | 279 storage()->FindRegistrationForPattern( |
| 251 pattern, | 280 pattern, |
| 252 SaveFoundRegistration(false, SERVICE_WORKER_OK, &called, ®istration)); | 281 SaveFoundRegistration(false, SERVICE_WORKER_OK, &called, ®istration)); |
| 253 | 282 |
| 254 ASSERT_FALSE(called); | 283 ASSERT_FALSE(called); |
| 255 base::RunLoop().RunUntilIdle(); | 284 base::RunLoop().RunUntilIdle(); |
| 256 ASSERT_TRUE(called); | 285 ASSERT_TRUE(called); |
| 257 | 286 |
| 258 ASSERT_EQ(scoped_refptr<ServiceWorkerRegistration>(NULL), registration); | 287 ASSERT_EQ(scoped_refptr<ServiceWorkerRegistration>(NULL), registration); |
| 259 } | 288 } |
| 260 | 289 |
| 261 // Make sure that when a new registration replaces an existing | 290 // Make sure that when a new registration replaces an existing |
| 262 // registration, that the old one is cleaned up. | 291 // registration, that the old one is cleaned up. |
| 263 TEST_F(ServiceWorkerJobTest, RegisterNewScript) { | 292 TEST_F(ServiceWorkerJobTest, RegisterNewScript) { |
| 264 GURL pattern("http://www.example.com/*"); | 293 GURL pattern("http://www.example.com/*"); |
| 265 | 294 |
| 266 bool called; | 295 bool called; |
| 267 scoped_refptr<ServiceWorkerRegistration> old_registration; | 296 scoped_refptr<ServiceWorkerRegistration> old_registration; |
| 268 job_coordinator_->Register( | 297 job_coordinator()->Register( |
| 269 pattern, | 298 pattern, |
| 270 GURL("http://www.example.com/service_worker.js"), | 299 GURL("http://www.example.com/service_worker.js"), |
| 300 render_process_id_, |
| 271 SaveRegistration(SERVICE_WORKER_OK, &called, &old_registration)); | 301 SaveRegistration(SERVICE_WORKER_OK, &called, &old_registration)); |
| 272 | 302 |
| 273 ASSERT_FALSE(called); | 303 ASSERT_FALSE(called); |
| 274 base::RunLoop().RunUntilIdle(); | 304 base::RunLoop().RunUntilIdle(); |
| 275 ASSERT_TRUE(called); | 305 ASSERT_TRUE(called); |
| 276 | 306 |
| 277 scoped_refptr<ServiceWorkerRegistration> old_registration_by_pattern; | 307 scoped_refptr<ServiceWorkerRegistration> old_registration_by_pattern; |
| 278 storage_->FindRegistrationForPattern( | 308 storage()->FindRegistrationForPattern( |
| 279 pattern, | 309 pattern, |
| 280 SaveFoundRegistration( | 310 SaveFoundRegistration( |
| 281 true, SERVICE_WORKER_OK, &called, &old_registration_by_pattern)); | 311 true, SERVICE_WORKER_OK, &called, &old_registration_by_pattern)); |
| 282 | 312 |
| 283 ASSERT_FALSE(called); | 313 ASSERT_FALSE(called); |
| 284 base::RunLoop().RunUntilIdle(); | 314 base::RunLoop().RunUntilIdle(); |
| 285 ASSERT_TRUE(called); | 315 ASSERT_TRUE(called); |
| 286 | 316 |
| 287 ASSERT_EQ(old_registration, old_registration_by_pattern); | 317 ASSERT_EQ(old_registration, old_registration_by_pattern); |
| 288 old_registration_by_pattern = NULL; | 318 old_registration_by_pattern = NULL; |
| 289 | 319 |
| 290 scoped_refptr<ServiceWorkerRegistration> new_registration; | 320 scoped_refptr<ServiceWorkerRegistration> new_registration; |
| 291 job_coordinator_->Register( | 321 job_coordinator()->Register( |
| 292 pattern, | 322 pattern, |
| 293 GURL("http://www.example.com/service_worker_new.js"), | 323 GURL("http://www.example.com/service_worker_new.js"), |
| 324 render_process_id_, |
| 294 SaveRegistration(SERVICE_WORKER_OK, &called, &new_registration)); | 325 SaveRegistration(SERVICE_WORKER_OK, &called, &new_registration)); |
| 295 | 326 |
| 296 ASSERT_FALSE(called); | 327 ASSERT_FALSE(called); |
| 297 base::RunLoop().RunUntilIdle(); | 328 base::RunLoop().RunUntilIdle(); |
| 298 ASSERT_TRUE(called); | 329 ASSERT_TRUE(called); |
| 299 | 330 |
| 300 ASSERT_TRUE(old_registration->HasOneRef()); | 331 ASSERT_TRUE(old_registration->HasOneRef()); |
| 301 | 332 |
| 302 ASSERT_NE(old_registration, new_registration); | 333 ASSERT_NE(old_registration, new_registration); |
| 303 | 334 |
| 304 scoped_refptr<ServiceWorkerRegistration> new_registration_by_pattern; | 335 scoped_refptr<ServiceWorkerRegistration> new_registration_by_pattern; |
| 305 storage_->FindRegistrationForPattern( | 336 storage()->FindRegistrationForPattern( |
| 306 pattern, | 337 pattern, |
| 307 SaveFoundRegistration(true, SERVICE_WORKER_OK, &called, | 338 SaveFoundRegistration( |
| 308 &new_registration)); | 339 true, SERVICE_WORKER_OK, &called, &new_registration)); |
| 309 | 340 |
| 310 ASSERT_FALSE(called); | 341 ASSERT_FALSE(called); |
| 311 base::RunLoop().RunUntilIdle(); | 342 base::RunLoop().RunUntilIdle(); |
| 312 ASSERT_TRUE(called); | 343 ASSERT_TRUE(called); |
| 313 | 344 |
| 314 ASSERT_NE(new_registration_by_pattern, old_registration); | 345 ASSERT_NE(new_registration_by_pattern, old_registration); |
| 315 } | 346 } |
| 316 | 347 |
| 317 // Make sure that when registering a duplicate pattern+script_url | 348 // Make sure that when registering a duplicate pattern+script_url |
| 318 // combination, that the same registration is used. | 349 // combination, that the same registration is used. |
| 319 TEST_F(ServiceWorkerJobTest, RegisterDuplicateScript) { | 350 TEST_F(ServiceWorkerJobTest, RegisterDuplicateScript) { |
| 320 GURL pattern("http://www.example.com/*"); | 351 GURL pattern("http://www.example.com/*"); |
| 321 GURL script_url("http://www.example.com/service_worker.js"); | 352 GURL script_url("http://www.example.com/service_worker.js"); |
| 322 | 353 |
| 323 bool called; | 354 bool called; |
| 324 scoped_refptr<ServiceWorkerRegistration> old_registration; | 355 scoped_refptr<ServiceWorkerRegistration> old_registration; |
| 325 job_coordinator_->Register( | 356 job_coordinator()->Register( |
| 326 pattern, | 357 pattern, |
| 327 script_url, | 358 script_url, |
| 359 render_process_id_, |
| 328 SaveRegistration(SERVICE_WORKER_OK, &called, &old_registration)); | 360 SaveRegistration(SERVICE_WORKER_OK, &called, &old_registration)); |
| 329 | 361 |
| 330 ASSERT_FALSE(called); | 362 ASSERT_FALSE(called); |
| 331 base::RunLoop().RunUntilIdle(); | 363 base::RunLoop().RunUntilIdle(); |
| 332 ASSERT_TRUE(called); | 364 ASSERT_TRUE(called); |
| 333 | 365 |
| 334 scoped_refptr<ServiceWorkerRegistration> old_registration_by_pattern; | 366 scoped_refptr<ServiceWorkerRegistration> old_registration_by_pattern; |
| 335 storage_->FindRegistrationForPattern( | 367 storage()->FindRegistrationForPattern( |
| 336 pattern, | 368 pattern, |
| 337 SaveFoundRegistration( | 369 SaveFoundRegistration( |
| 338 true, SERVICE_WORKER_OK, &called, &old_registration_by_pattern)); | 370 true, SERVICE_WORKER_OK, &called, &old_registration_by_pattern)); |
| 339 ASSERT_FALSE(called); | 371 ASSERT_FALSE(called); |
| 340 base::RunLoop().RunUntilIdle(); | 372 base::RunLoop().RunUntilIdle(); |
| 341 ASSERT_TRUE(called); | 373 ASSERT_TRUE(called); |
| 342 | 374 |
| 343 ASSERT_TRUE(old_registration_by_pattern); | 375 ASSERT_TRUE(old_registration_by_pattern); |
| 344 | 376 |
| 345 scoped_refptr<ServiceWorkerRegistration> new_registration; | 377 scoped_refptr<ServiceWorkerRegistration> new_registration; |
| 346 job_coordinator_->Register( | 378 job_coordinator()->Register( |
| 347 pattern, | 379 pattern, |
| 348 script_url, | 380 script_url, |
| 381 render_process_id_, |
| 349 SaveRegistration(SERVICE_WORKER_OK, &called, &new_registration)); | 382 SaveRegistration(SERVICE_WORKER_OK, &called, &new_registration)); |
| 350 | 383 |
| 351 ASSERT_FALSE(called); | 384 ASSERT_FALSE(called); |
| 352 base::RunLoop().RunUntilIdle(); | 385 base::RunLoop().RunUntilIdle(); |
| 353 ASSERT_TRUE(called); | 386 ASSERT_TRUE(called); |
| 354 | 387 |
| 355 ASSERT_EQ(old_registration, new_registration); | 388 ASSERT_EQ(old_registration, new_registration); |
| 356 | 389 |
| 357 ASSERT_FALSE(old_registration->HasOneRef()); | 390 ASSERT_FALSE(old_registration->HasOneRef()); |
| 358 | 391 |
| 359 scoped_refptr<ServiceWorkerRegistration> new_registration_by_pattern; | 392 scoped_refptr<ServiceWorkerRegistration> new_registration_by_pattern; |
| 360 storage_->FindRegistrationForPattern( | 393 storage()->FindRegistrationForPattern( |
| 361 pattern, | 394 pattern, |
| 362 SaveFoundRegistration( | 395 SaveFoundRegistration( |
| 363 true, SERVICE_WORKER_OK, &called, &new_registration_by_pattern)); | 396 true, SERVICE_WORKER_OK, &called, &new_registration_by_pattern)); |
| 364 | 397 |
| 365 ASSERT_FALSE(called); | 398 ASSERT_FALSE(called); |
| 366 base::RunLoop().RunUntilIdle(); | 399 base::RunLoop().RunUntilIdle(); |
| 367 ASSERT_TRUE(called); | 400 ASSERT_TRUE(called); |
| 368 | 401 |
| 369 ASSERT_EQ(new_registration, old_registration); | 402 ASSERT_EQ(new_registration, old_registration); |
| 370 } | 403 } |
| 371 | 404 |
| 372 // Register and then unregister the pattern, in parallel. Job coordinator should | 405 // Register and then unregister the pattern, in parallel. Job coordinator should |
| 373 // process jobs until the last job. | 406 // process jobs until the last job. |
| 374 TEST_F(ServiceWorkerJobTest, ParallelRegUnreg) { | 407 TEST_F(ServiceWorkerJobTest, ParallelRegUnreg) { |
| 375 GURL pattern("http://www.example.com/*"); | 408 GURL pattern("http://www.example.com/*"); |
| 376 GURL script_url("http://www.example.com/service_worker.js"); | 409 GURL script_url("http://www.example.com/service_worker.js"); |
| 377 | 410 |
| 378 bool registration_called = false; | 411 bool registration_called = false; |
| 379 scoped_refptr<ServiceWorkerRegistration> registration; | 412 scoped_refptr<ServiceWorkerRegistration> registration; |
| 380 job_coordinator_->Register( | 413 job_coordinator()->Register( |
| 381 pattern, | 414 pattern, |
| 382 script_url, | 415 script_url, |
| 416 render_process_id_, |
| 383 SaveRegistration(SERVICE_WORKER_OK, ®istration_called, ®istration)); | 417 SaveRegistration(SERVICE_WORKER_OK, ®istration_called, ®istration)); |
| 384 | 418 |
| 385 bool unregistration_called = false; | 419 bool unregistration_called = false; |
| 386 job_coordinator_->Unregister( | 420 job_coordinator()->Unregister( |
| 387 pattern, SaveUnregistration(SERVICE_WORKER_OK, &unregistration_called)); | 421 pattern, |
| 422 render_process_id_, |
| 423 SaveUnregistration(SERVICE_WORKER_OK, &unregistration_called)); |
| 388 | 424 |
| 389 ASSERT_FALSE(registration_called); | 425 ASSERT_FALSE(registration_called); |
| 390 ASSERT_FALSE(unregistration_called); | 426 ASSERT_FALSE(unregistration_called); |
| 391 base::RunLoop().RunUntilIdle(); | 427 base::RunLoop().RunUntilIdle(); |
| 392 ASSERT_TRUE(registration_called); | 428 ASSERT_TRUE(registration_called); |
| 393 ASSERT_TRUE(unregistration_called); | 429 ASSERT_TRUE(unregistration_called); |
| 394 | 430 |
| 395 ASSERT_TRUE(registration->is_shutdown()); | 431 ASSERT_TRUE(registration->is_shutdown()); |
| 396 | 432 |
| 397 bool find_called = false; | 433 bool find_called = false; |
| 398 storage_->FindRegistrationForPattern( | 434 storage()->FindRegistrationForPattern( |
| 399 pattern, | 435 pattern, |
| 400 SaveFoundRegistration( | 436 SaveFoundRegistration( |
| 401 false, SERVICE_WORKER_OK, &find_called, ®istration)); | 437 false, SERVICE_WORKER_OK, &find_called, ®istration)); |
| 402 | 438 |
| 403 base::RunLoop().RunUntilIdle(); | 439 base::RunLoop().RunUntilIdle(); |
| 404 | 440 |
| 405 ASSERT_EQ(scoped_refptr<ServiceWorkerRegistration>(), registration); | 441 ASSERT_EQ(scoped_refptr<ServiceWorkerRegistration>(), registration); |
| 406 } | 442 } |
| 407 | 443 |
| 408 // Register conflicting scripts for the same pattern. The most recent | 444 // Register conflicting scripts for the same pattern. The most recent |
| 409 // registration should win, and the old registration should have been | 445 // registration should win, and the old registration should have been |
| 410 // shutdown. | 446 // shutdown. |
| 411 TEST_F(ServiceWorkerJobTest, ParallelRegNewScript) { | 447 TEST_F(ServiceWorkerJobTest, ParallelRegNewScript) { |
| 412 GURL pattern("http://www.example.com/*"); | 448 GURL pattern("http://www.example.com/*"); |
| 413 | 449 |
| 414 GURL script_url1("http://www.example.com/service_worker1.js"); | 450 GURL script_url1("http://www.example.com/service_worker1.js"); |
| 415 bool registration1_called = false; | 451 bool registration1_called = false; |
| 416 scoped_refptr<ServiceWorkerRegistration> registration1; | 452 scoped_refptr<ServiceWorkerRegistration> registration1; |
| 417 job_coordinator_->Register( | 453 job_coordinator()->Register( |
| 418 pattern, | 454 pattern, |
| 419 script_url1, | 455 script_url1, |
| 420 SaveRegistration(SERVICE_WORKER_OK, ®istration1_called, | 456 render_process_id_, |
| 421 ®istration1)); | 457 SaveRegistration( |
| 458 SERVICE_WORKER_OK, ®istration1_called, ®istration1)); |
| 422 | 459 |
| 423 GURL script_url2("http://www.example.com/service_worker2.js"); | 460 GURL script_url2("http://www.example.com/service_worker2.js"); |
| 424 bool registration2_called = false; | 461 bool registration2_called = false; |
| 425 scoped_refptr<ServiceWorkerRegistration> registration2; | 462 scoped_refptr<ServiceWorkerRegistration> registration2; |
| 426 job_coordinator_->Register( | 463 job_coordinator()->Register( |
| 427 pattern, | 464 pattern, |
| 428 script_url2, | 465 script_url2, |
| 429 SaveRegistration(SERVICE_WORKER_OK, ®istration2_called, | 466 render_process_id_, |
| 430 ®istration2)); | 467 SaveRegistration( |
| 468 SERVICE_WORKER_OK, ®istration2_called, ®istration2)); |
| 431 | 469 |
| 432 ASSERT_FALSE(registration1_called); | 470 ASSERT_FALSE(registration1_called); |
| 433 ASSERT_FALSE(registration2_called); | 471 ASSERT_FALSE(registration2_called); |
| 434 base::RunLoop().RunUntilIdle(); | 472 base::RunLoop().RunUntilIdle(); |
| 435 ASSERT_TRUE(registration1_called); | 473 ASSERT_TRUE(registration1_called); |
| 436 ASSERT_TRUE(registration2_called); | 474 ASSERT_TRUE(registration2_called); |
| 437 | 475 |
| 438 scoped_refptr<ServiceWorkerRegistration> registration; | 476 scoped_refptr<ServiceWorkerRegistration> registration; |
| 439 bool find_called = false; | 477 bool find_called = false; |
| 440 storage_->FindRegistrationForPattern( | 478 storage()->FindRegistrationForPattern( |
| 441 pattern, | 479 pattern, |
| 442 SaveFoundRegistration( | 480 SaveFoundRegistration( |
| 443 true, SERVICE_WORKER_OK, &find_called, ®istration)); | 481 true, SERVICE_WORKER_OK, &find_called, ®istration)); |
| 444 | 482 |
| 445 base::RunLoop().RunUntilIdle(); | 483 base::RunLoop().RunUntilIdle(); |
| 446 | 484 |
| 447 EXPECT_TRUE(registration1->is_shutdown()); | 485 EXPECT_TRUE(registration1->is_shutdown()); |
| 448 EXPECT_FALSE(registration2->is_shutdown()); | 486 EXPECT_FALSE(registration2->is_shutdown()); |
| 449 ASSERT_EQ(registration2, registration); | 487 ASSERT_EQ(registration2, registration); |
| 450 } | 488 } |
| 451 | 489 |
| 452 // Register the exact same pattern + script. Requests should be | 490 // Register the exact same pattern + script. Requests should be |
| 453 // coalesced such that both callers get the exact same registration | 491 // coalesced such that both callers get the exact same registration |
| 454 // object. | 492 // object. |
| 455 TEST_F(ServiceWorkerJobTest, ParallelRegSameScript) { | 493 TEST_F(ServiceWorkerJobTest, ParallelRegSameScript) { |
| 456 GURL pattern("http://www.example.com/*"); | 494 GURL pattern("http://www.example.com/*"); |
| 457 | 495 |
| 458 GURL script_url("http://www.example.com/service_worker1.js"); | 496 GURL script_url("http://www.example.com/service_worker1.js"); |
| 459 bool registration1_called = false; | 497 bool registration1_called = false; |
| 460 scoped_refptr<ServiceWorkerRegistration> registration1; | 498 scoped_refptr<ServiceWorkerRegistration> registration1; |
| 461 job_coordinator_->Register( | 499 job_coordinator()->Register( |
| 462 pattern, | 500 pattern, |
| 463 script_url, | 501 script_url, |
| 464 SaveRegistration(SERVICE_WORKER_OK, ®istration1_called, | 502 render_process_id_, |
| 465 ®istration1)); | 503 SaveRegistration( |
| 504 SERVICE_WORKER_OK, ®istration1_called, ®istration1)); |
| 466 | 505 |
| 467 bool registration2_called = false; | 506 bool registration2_called = false; |
| 468 scoped_refptr<ServiceWorkerRegistration> registration2; | 507 scoped_refptr<ServiceWorkerRegistration> registration2; |
| 469 job_coordinator_->Register( | 508 job_coordinator()->Register( |
| 470 pattern, | 509 pattern, |
| 471 script_url, | 510 script_url, |
| 472 SaveRegistration(SERVICE_WORKER_OK, ®istration2_called, | 511 render_process_id_, |
| 473 ®istration2)); | 512 SaveRegistration( |
| 513 SERVICE_WORKER_OK, ®istration2_called, ®istration2)); |
| 474 | 514 |
| 475 ASSERT_FALSE(registration1_called); | 515 ASSERT_FALSE(registration1_called); |
| 476 ASSERT_FALSE(registration2_called); | 516 ASSERT_FALSE(registration2_called); |
| 477 base::RunLoop().RunUntilIdle(); | 517 base::RunLoop().RunUntilIdle(); |
| 478 ASSERT_TRUE(registration1_called); | 518 ASSERT_TRUE(registration1_called); |
| 479 ASSERT_TRUE(registration2_called); | 519 ASSERT_TRUE(registration2_called); |
| 480 | 520 |
| 481 ASSERT_EQ(registration1, registration2); | 521 ASSERT_EQ(registration1, registration2); |
| 482 | 522 |
| 483 scoped_refptr<ServiceWorkerRegistration> registration; | 523 scoped_refptr<ServiceWorkerRegistration> registration; |
| 484 bool find_called = false; | 524 bool find_called = false; |
| 485 storage_->FindRegistrationForPattern( | 525 storage()->FindRegistrationForPattern( |
| 486 pattern, | 526 pattern, |
| 487 SaveFoundRegistration( | 527 SaveFoundRegistration( |
| 488 true, SERVICE_WORKER_OK, &find_called, ®istration)); | 528 true, SERVICE_WORKER_OK, &find_called, ®istration)); |
| 489 | 529 |
| 490 base::RunLoop().RunUntilIdle(); | 530 base::RunLoop().RunUntilIdle(); |
| 491 ASSERT_EQ(registration, registration1); | 531 ASSERT_EQ(registration, registration1); |
| 492 } | 532 } |
| 493 | 533 |
| 494 // Call simulataneous unregister calls. | 534 // Call simulataneous unregister calls. |
| 495 TEST_F(ServiceWorkerJobTest, ParallelUnreg) { | 535 TEST_F(ServiceWorkerJobTest, ParallelUnreg) { |
| 496 GURL pattern("http://www.example.com/*"); | 536 GURL pattern("http://www.example.com/*"); |
| 497 | 537 |
| 498 GURL script_url("http://www.example.com/service_worker.js"); | 538 GURL script_url("http://www.example.com/service_worker.js"); |
| 499 bool unregistration1_called = false; | 539 bool unregistration1_called = false; |
| 500 job_coordinator_->Unregister( | 540 job_coordinator()->Unregister( |
| 501 pattern, SaveUnregistration(SERVICE_WORKER_OK, &unregistration1_called)); | 541 pattern, |
| 542 render_process_id_, |
| 543 SaveUnregistration(SERVICE_WORKER_OK, &unregistration1_called)); |
| 502 | 544 |
| 503 bool unregistration2_called = false; | 545 bool unregistration2_called = false; |
| 504 job_coordinator_->Unregister( | 546 job_coordinator()->Unregister( |
| 505 pattern, SaveUnregistration(SERVICE_WORKER_OK, &unregistration2_called)); | 547 pattern, |
| 548 render_process_id_, |
| 549 SaveUnregistration(SERVICE_WORKER_OK, &unregistration2_called)); |
| 506 | 550 |
| 507 ASSERT_FALSE(unregistration1_called); | 551 ASSERT_FALSE(unregistration1_called); |
| 508 ASSERT_FALSE(unregistration2_called); | 552 ASSERT_FALSE(unregistration2_called); |
| 509 base::RunLoop().RunUntilIdle(); | 553 base::RunLoop().RunUntilIdle(); |
| 510 ASSERT_TRUE(unregistration1_called); | 554 ASSERT_TRUE(unregistration1_called); |
| 511 ASSERT_TRUE(unregistration2_called); | 555 ASSERT_TRUE(unregistration2_called); |
| 512 | 556 |
| 513 // There isn't really a way to test that they are being coalesced, | 557 // There isn't really a way to test that they are being coalesced, |
| 514 // but we can make sure they can exist simultaneously without | 558 // but we can make sure they can exist simultaneously without |
| 515 // crashing. | 559 // crashing. |
| 516 scoped_refptr<ServiceWorkerRegistration> registration; | 560 scoped_refptr<ServiceWorkerRegistration> registration; |
| 517 bool find_called = false; | 561 bool find_called = false; |
| 518 storage_->FindRegistrationForPattern( | 562 storage()->FindRegistrationForPattern( |
| 519 pattern, | 563 pattern, |
| 520 SaveFoundRegistration( | 564 SaveFoundRegistration( |
| 521 false, SERVICE_WORKER_OK, &find_called, ®istration)); | 565 false, SERVICE_WORKER_OK, &find_called, ®istration)); |
| 522 | 566 |
| 523 base::RunLoop().RunUntilIdle(); | 567 base::RunLoop().RunUntilIdle(); |
| 524 ASSERT_EQ(scoped_refptr<ServiceWorkerRegistration>(), registration); | 568 ASSERT_EQ(scoped_refptr<ServiceWorkerRegistration>(), registration); |
| 525 } | 569 } |
| 526 | 570 |
| 527 } // namespace content | 571 } // namespace content |
| OLD | NEW |