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" |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
46 | 46 |
47 void CallCompletedCallback(bool* called, ServiceWorkerStatusCode) { | 47 void CallCompletedCallback(bool* called, ServiceWorkerStatusCode) { |
48 *called = true; | 48 *called = true; |
49 } | 49 } |
50 | 50 |
51 ServiceWorkerContextCore::UnregistrationCallback MakeUnregisteredCallback( | 51 ServiceWorkerContextCore::UnregistrationCallback MakeUnregisteredCallback( |
52 bool* called) { | 52 bool* called) { |
53 return base::Bind(&CallCompletedCallback, called); | 53 return base::Bind(&CallCompletedCallback, called); |
54 } | 54 } |
55 | 55 |
| 56 void SaveRegistrationIdCallback(bool* called, |
| 57 int64* store_registration_id, |
| 58 ServiceWorkerStatusCode status, |
| 59 int64 registration_id) { |
| 60 EXPECT_EQ(SERVICE_WORKER_OK, status) << ServiceWorkerStatusToString(status); |
| 61 *called = true; |
| 62 *store_registration_id = registration_id; |
| 63 } |
| 64 |
56 void ExpectRegisteredWorkers( | 65 void ExpectRegisteredWorkers( |
57 ServiceWorkerStatusCode expect_status, | 66 ServiceWorkerStatusCode expect_status, |
58 int64 expect_version_id, | 67 int64 expect_version_id, |
59 bool expect_waiting, | 68 bool expect_waiting, |
60 bool expect_active, | 69 bool expect_active, |
61 ServiceWorkerStatusCode status, | 70 ServiceWorkerStatusCode status, |
62 const scoped_refptr<ServiceWorkerRegistration>& registration) { | 71 const scoped_refptr<ServiceWorkerRegistration>& registration) { |
63 ASSERT_EQ(expect_status, status); | 72 ASSERT_EQ(expect_status, status); |
64 if (status != SERVICE_WORKER_OK) { | 73 if (status != SERVICE_WORKER_OK) { |
65 EXPECT_FALSE(registration.get()); | 74 EXPECT_FALSE(registration.get()); |
(...skipping 382 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
448 registration_id, | 457 registration_id, |
449 GURL("http://www.example.com"), | 458 GURL("http://www.example.com"), |
450 base::Bind(&ExpectRegisteredWorkers, | 459 base::Bind(&ExpectRegisteredWorkers, |
451 SERVICE_WORKER_OK, | 460 SERVICE_WORKER_OK, |
452 version_id, | 461 version_id, |
453 false /* expect_waiting */, | 462 false /* expect_waiting */, |
454 true /* expect_active */)); | 463 true /* expect_active */)); |
455 base::RunLoop().RunUntilIdle(); | 464 base::RunLoop().RunUntilIdle(); |
456 } | 465 } |
457 | 466 |
| 467 TEST_F(ServiceWorkerContextTest, GetRegistration) { |
| 468 GURL pattern("http://www.example.com/"); |
| 469 GURL document_url("http://www.example.com/foo"); |
| 470 |
| 471 bool called = false; |
| 472 int64 registration_id = kInvalidServiceWorkerRegistrationId; |
| 473 int64 version_id = kInvalidServiceWorkerVersionId; |
| 474 context()->RegisterServiceWorker( |
| 475 pattern, |
| 476 GURL("http://www.example.com/service_worker.js"), |
| 477 render_process_id_, |
| 478 NULL, |
| 479 MakeRegisteredCallback(&called, ®istration_id, &version_id)); |
| 480 |
| 481 ASSERT_FALSE(called); |
| 482 base::RunLoop().RunUntilIdle(); |
| 483 ASSERT_TRUE(called); |
| 484 EXPECT_NE(kInvalidServiceWorkerRegistrationId, registration_id); |
| 485 EXPECT_NE(kInvalidServiceWorkerVersionId, version_id); |
| 486 |
| 487 called = false; |
| 488 int64 result_registration_id; |
| 489 context()->GetRegistration( |
| 490 document_url, |
| 491 base::Bind(&SaveRegistrationIdCallback, &called, |
| 492 &result_registration_id)); |
| 493 |
| 494 ASSERT_FALSE(called); |
| 495 base::RunLoop().RunUntilIdle(); |
| 496 ASSERT_TRUE(called); |
| 497 EXPECT_EQ(registration_id, result_registration_id); |
| 498 } |
| 499 |
| 500 TEST_F(ServiceWorkerContextTest, GetRegistration_NotFound) { |
| 501 GURL pattern("http://www.example.com/scope"); |
| 502 GURL document_url("http://www.example.com/out_of_scope"); |
| 503 |
| 504 bool called = false; |
| 505 int64 registration_id = kInvalidServiceWorkerRegistrationId; |
| 506 int64 version_id = kInvalidServiceWorkerVersionId; |
| 507 context()->RegisterServiceWorker( |
| 508 pattern, |
| 509 GURL("http://www.example.com/service_worker.js"), |
| 510 render_process_id_, |
| 511 NULL, |
| 512 MakeRegisteredCallback(&called, ®istration_id, &version_id)); |
| 513 |
| 514 ASSERT_FALSE(called); |
| 515 base::RunLoop().RunUntilIdle(); |
| 516 ASSERT_TRUE(called); |
| 517 EXPECT_NE(kInvalidServiceWorkerRegistrationId, registration_id); |
| 518 EXPECT_NE(kInvalidServiceWorkerVersionId, version_id); |
| 519 |
| 520 called = false; |
| 521 int64 result_registration_id; |
| 522 context()->GetRegistration( |
| 523 document_url, |
| 524 base::Bind(&SaveRegistrationIdCallback, &called, |
| 525 &result_registration_id)); |
| 526 |
| 527 ASSERT_FALSE(called); |
| 528 base::RunLoop().RunUntilIdle(); |
| 529 ASSERT_TRUE(called); |
| 530 EXPECT_EQ(kInvalidServiceWorkerRegistrationId, result_registration_id); |
| 531 } |
| 532 |
458 } // namespace content | 533 } // namespace content |
OLD | NEW |