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 "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/service_worker_job_coordinator.h" | 9 #include "content/browser/service_worker/service_worker_job_coordinator.h" |
10 #include "content/browser/service_worker/service_worker_registration.h" | 10 #include "content/browser/service_worker/service_worker_registration.h" |
(...skipping 351 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
362 SaveFoundRegistration( | 362 SaveFoundRegistration( |
363 true, REGISTRATION_OK, &called, &new_registration_by_pattern)); | 363 true, REGISTRATION_OK, &called, &new_registration_by_pattern)); |
364 | 364 |
365 ASSERT_FALSE(called); | 365 ASSERT_FALSE(called); |
366 base::RunLoop().RunUntilIdle(); | 366 base::RunLoop().RunUntilIdle(); |
367 ASSERT_TRUE(called); | 367 ASSERT_TRUE(called); |
368 | 368 |
369 ASSERT_EQ(new_registration, old_registration); | 369 ASSERT_EQ(new_registration, old_registration); |
370 } | 370 } |
371 | 371 |
| 372 // Register and then unregister the pattern, in parallel. Job coordinator should |
| 373 // process jobs until the last job. |
| 374 TEST_F(ServiceWorkerJobTest, ParallelRegUnreg) { |
| 375 GURL pattern("http://www.example.com/*"); |
| 376 GURL script_url("http://www.example.com/service_worker.js"); |
| 377 |
| 378 bool registration_called = false; |
| 379 scoped_refptr<ServiceWorkerRegistration> registration; |
| 380 job_coordinator_->Register( |
| 381 pattern, |
| 382 script_url, |
| 383 SaveRegistration(REGISTRATION_OK, ®istration_called, ®istration)); |
| 384 |
| 385 bool unregistration_called = false; |
| 386 job_coordinator_->Unregister( |
| 387 pattern, SaveUnregistration(REGISTRATION_OK, &unregistration_called)); |
| 388 |
| 389 ASSERT_FALSE(registration_called); |
| 390 ASSERT_FALSE(unregistration_called); |
| 391 base::RunLoop().RunUntilIdle(); |
| 392 ASSERT_TRUE(registration_called); |
| 393 ASSERT_TRUE(unregistration_called); |
| 394 |
| 395 ASSERT_TRUE(registration->is_shutdown()); |
| 396 |
| 397 bool find_called = false; |
| 398 storage_->FindRegistrationForPattern( |
| 399 pattern, |
| 400 SaveFoundRegistration( |
| 401 false, REGISTRATION_OK, &find_called, ®istration)); |
| 402 |
| 403 base::RunLoop().RunUntilIdle(); |
| 404 |
| 405 ASSERT_EQ(scoped_refptr<ServiceWorkerRegistration>(NULL), registration); |
| 406 } |
| 407 |
| 408 // Register conflicting scripts for the same pattern. The most recent |
| 409 // registration should win, and the old registration should have been |
| 410 // shutdown. |
| 411 TEST_F(ServiceWorkerJobTest, ParallelRegNewScript) { |
| 412 GURL pattern("http://www.example.com/*"); |
| 413 |
| 414 GURL script_url1("http://www.example.com/service_worker1.js"); |
| 415 bool registration1_called = false; |
| 416 scoped_refptr<ServiceWorkerRegistration> registration1; |
| 417 job_coordinator_->Register( |
| 418 pattern, |
| 419 script_url1, |
| 420 SaveRegistration(REGISTRATION_OK, ®istration1_called, ®istration1)); |
| 421 |
| 422 GURL script_url2("http://www.example.com/service_worker2.js"); |
| 423 bool registration2_called = false; |
| 424 scoped_refptr<ServiceWorkerRegistration> registration2; |
| 425 job_coordinator_->Register( |
| 426 pattern, |
| 427 script_url2, |
| 428 SaveRegistration(REGISTRATION_OK, ®istration2_called, ®istration2)); |
| 429 |
| 430 ASSERT_FALSE(registration1_called); |
| 431 ASSERT_FALSE(registration2_called); |
| 432 base::RunLoop().RunUntilIdle(); |
| 433 ASSERT_TRUE(registration1_called); |
| 434 ASSERT_TRUE(registration2_called); |
| 435 |
| 436 scoped_refptr<ServiceWorkerRegistration> registration; |
| 437 bool find_called = false; |
| 438 storage_->FindRegistrationForPattern( |
| 439 pattern, |
| 440 SaveFoundRegistration( |
| 441 true, REGISTRATION_OK, &find_called, ®istration)); |
| 442 |
| 443 base::RunLoop().RunUntilIdle(); |
| 444 |
| 445 EXPECT_TRUE(registration1->is_shutdown()); |
| 446 EXPECT_FALSE(registration2->is_shutdown()); |
| 447 ASSERT_EQ(registration2, registration); |
| 448 } |
| 449 |
| 450 // Register the exact same pattern + script. Requests should be |
| 451 // coalesced such that both callers get the exact same registration |
| 452 // object. |
| 453 TEST_F(ServiceWorkerJobTest, ParallelRegSameScript) { |
| 454 GURL pattern("http://www.example.com/*"); |
| 455 |
| 456 GURL script_url("http://www.example.com/service_worker1.js"); |
| 457 bool registration1_called = false; |
| 458 scoped_refptr<ServiceWorkerRegistration> registration1; |
| 459 job_coordinator_->Register( |
| 460 pattern, |
| 461 script_url, |
| 462 SaveRegistration(REGISTRATION_OK, ®istration1_called, ®istration1)); |
| 463 |
| 464 bool registration2_called = false; |
| 465 scoped_refptr<ServiceWorkerRegistration> registration2; |
| 466 job_coordinator_->Register( |
| 467 pattern, |
| 468 script_url, |
| 469 SaveRegistration(REGISTRATION_OK, ®istration2_called, ®istration2)); |
| 470 |
| 471 ASSERT_FALSE(registration1_called); |
| 472 ASSERT_FALSE(registration2_called); |
| 473 base::RunLoop().RunUntilIdle(); |
| 474 ASSERT_TRUE(registration1_called); |
| 475 ASSERT_TRUE(registration2_called); |
| 476 |
| 477 ASSERT_EQ(registration1, registration2); |
| 478 |
| 479 scoped_refptr<ServiceWorkerRegistration> registration; |
| 480 bool find_called = false; |
| 481 storage_->FindRegistrationForPattern( |
| 482 pattern, |
| 483 SaveFoundRegistration( |
| 484 true, REGISTRATION_OK, &find_called, ®istration)); |
| 485 |
| 486 base::RunLoop().RunUntilIdle(); |
| 487 ASSERT_EQ(registration, registration1); |
| 488 } |
| 489 |
372 } // namespace content | 490 } // namespace content |
OLD | NEW |