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