| 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 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 render_process_id_(99) {} | 124 render_process_id_(99) {} |
| 125 | 125 |
| 126 void SetUp() override { | 126 void SetUp() override { |
| 127 helper_.reset(new EmbeddedWorkerTestHelper(render_process_id_)); | 127 helper_.reset(new EmbeddedWorkerTestHelper(render_process_id_)); |
| 128 helper_->context_wrapper()->AddObserver(this); | 128 helper_->context_wrapper()->AddObserver(this); |
| 129 } | 129 } |
| 130 | 130 |
| 131 void TearDown() override { helper_.reset(); } | 131 void TearDown() override { helper_.reset(); } |
| 132 | 132 |
| 133 // ServiceWorkerContextObserver overrides. | 133 // ServiceWorkerContextObserver overrides. |
| 134 void OnRegistrationStored(const GURL& pattern) override { | 134 void OnRegistrationStored(int64 registration_id, |
| 135 const GURL& pattern) override { |
| 135 NotificationLog log; | 136 NotificationLog log; |
| 136 log.type = REGISTRATION_STORED; | 137 log.type = REGISTRATION_STORED; |
| 137 log.pattern = pattern; | 138 log.pattern = pattern; |
| 139 log.registration_id = registration_id; |
| 138 notifications_.push_back(log); | 140 notifications_.push_back(log); |
| 139 } | 141 } |
| 140 void OnRegistrationDeleted(int64 registration_id, | 142 void OnRegistrationDeleted(int64 registration_id, |
| 141 const GURL& pattern) override { | 143 const GURL& pattern) override { |
| 142 NotificationLog log; | 144 NotificationLog log; |
| 143 log.type = REGISTRATION_DELETED; | 145 log.type = REGISTRATION_DELETED; |
| 144 log.pattern = pattern; | 146 log.pattern = pattern; |
| 145 log.registration_id = registration_id; | 147 log.registration_id = registration_id; |
| 146 notifications_.push_back(log); | 148 notifications_.push_back(log); |
| 147 } | 149 } |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 193 pattern.GetOrigin(), | 195 pattern.GetOrigin(), |
| 194 base::Bind(&ExpectRegisteredWorkers, | 196 base::Bind(&ExpectRegisteredWorkers, |
| 195 SERVICE_WORKER_OK, | 197 SERVICE_WORKER_OK, |
| 196 false /* expect_waiting */, | 198 false /* expect_waiting */, |
| 197 true /* expect_active */)); | 199 true /* expect_active */)); |
| 198 base::RunLoop().RunUntilIdle(); | 200 base::RunLoop().RunUntilIdle(); |
| 199 | 201 |
| 200 ASSERT_EQ(1u, notifications_.size()); | 202 ASSERT_EQ(1u, notifications_.size()); |
| 201 EXPECT_EQ(REGISTRATION_STORED, notifications_[0].type); | 203 EXPECT_EQ(REGISTRATION_STORED, notifications_[0].type); |
| 202 EXPECT_EQ(pattern, notifications_[0].pattern); | 204 EXPECT_EQ(pattern, notifications_[0].pattern); |
| 205 EXPECT_EQ(registration_id, notifications_[0].registration_id); |
| 203 } | 206 } |
| 204 | 207 |
| 205 // Test registration when the service worker rejects the install event. The | 208 // Test registration when the service worker rejects the install event. The |
| 206 // registration callback should indicate success, but there should be no waiting | 209 // registration callback should indicate success, but there should be no waiting |
| 207 // or active worker in the registration. | 210 // or active worker in the registration. |
| 208 TEST_F(ServiceWorkerContextTest, Register_RejectInstall) { | 211 TEST_F(ServiceWorkerContextTest, Register_RejectInstall) { |
| 209 GURL pattern("http://www.example.com/"); | 212 GURL pattern("http://www.example.com/"); |
| 210 GURL script_url("http://www.example.com/service_worker.js"); | 213 GURL script_url("http://www.example.com/service_worker.js"); |
| 211 | 214 |
| 212 helper_.reset(); // Make sure the process lookups stay overridden. | 215 helper_.reset(); // Make sure the process lookups stay overridden. |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 317 pattern.GetOrigin(), | 320 pattern.GetOrigin(), |
| 318 base::Bind(&ExpectRegisteredWorkers, | 321 base::Bind(&ExpectRegisteredWorkers, |
| 319 SERVICE_WORKER_ERROR_NOT_FOUND, | 322 SERVICE_WORKER_ERROR_NOT_FOUND, |
| 320 false /* expect_waiting */, | 323 false /* expect_waiting */, |
| 321 false /* expect_active */)); | 324 false /* expect_active */)); |
| 322 base::RunLoop().RunUntilIdle(); | 325 base::RunLoop().RunUntilIdle(); |
| 323 | 326 |
| 324 ASSERT_EQ(2u, notifications_.size()); | 327 ASSERT_EQ(2u, notifications_.size()); |
| 325 EXPECT_EQ(REGISTRATION_STORED, notifications_[0].type); | 328 EXPECT_EQ(REGISTRATION_STORED, notifications_[0].type); |
| 326 EXPECT_EQ(pattern, notifications_[0].pattern); | 329 EXPECT_EQ(pattern, notifications_[0].pattern); |
| 330 EXPECT_EQ(registration_id, notifications_[0].registration_id); |
| 327 EXPECT_EQ(REGISTRATION_DELETED, notifications_[1].type); | 331 EXPECT_EQ(REGISTRATION_DELETED, notifications_[1].type); |
| 328 EXPECT_EQ(pattern, notifications_[1].pattern); | 332 EXPECT_EQ(pattern, notifications_[1].pattern); |
| 329 EXPECT_EQ(registration_id, notifications_[1].registration_id); | 333 EXPECT_EQ(registration_id, notifications_[1].registration_id); |
| 330 } | 334 } |
| 331 | 335 |
| 332 // Make sure registrations are cleaned up when they are unregistered in bulk. | 336 // Make sure registrations are cleaned up when they are unregistered in bulk. |
| 333 TEST_F(ServiceWorkerContextTest, UnregisterMultiple) { | 337 TEST_F(ServiceWorkerContextTest, UnregisterMultiple) { |
| 334 GURL origin1_p1("http://www.example.com/test"); | 338 GURL origin1_p1("http://www.example.com/test"); |
| 335 GURL origin1_p2("http://www.example.com/hello"); | 339 GURL origin1_p2("http://www.example.com/hello"); |
| 336 GURL origin2_p1("http://www.example.com:8080/again"); | 340 GURL origin2_p1("http://www.example.com:8080/again"); |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 406 origin3_p1.GetOrigin(), | 410 origin3_p1.GetOrigin(), |
| 407 base::Bind(&ExpectRegisteredWorkers, | 411 base::Bind(&ExpectRegisteredWorkers, |
| 408 SERVICE_WORKER_OK, | 412 SERVICE_WORKER_OK, |
| 409 false /* expect_waiting */, | 413 false /* expect_waiting */, |
| 410 true /* expect_active */)); | 414 true /* expect_active */)); |
| 411 | 415 |
| 412 base::RunLoop().RunUntilIdle(); | 416 base::RunLoop().RunUntilIdle(); |
| 413 | 417 |
| 414 ASSERT_EQ(6u, notifications_.size()); | 418 ASSERT_EQ(6u, notifications_.size()); |
| 415 EXPECT_EQ(REGISTRATION_STORED, notifications_[0].type); | 419 EXPECT_EQ(REGISTRATION_STORED, notifications_[0].type); |
| 420 EXPECT_EQ(registration_id1, notifications_[0].registration_id); |
| 416 EXPECT_EQ(origin1_p1, notifications_[0].pattern); | 421 EXPECT_EQ(origin1_p1, notifications_[0].pattern); |
| 417 EXPECT_EQ(REGISTRATION_STORED, notifications_[1].type); | 422 EXPECT_EQ(REGISTRATION_STORED, notifications_[1].type); |
| 418 EXPECT_EQ(origin1_p2, notifications_[1].pattern); | 423 EXPECT_EQ(origin1_p2, notifications_[1].pattern); |
| 424 EXPECT_EQ(registration_id2, notifications_[1].registration_id); |
| 419 EXPECT_EQ(REGISTRATION_STORED, notifications_[2].type); | 425 EXPECT_EQ(REGISTRATION_STORED, notifications_[2].type); |
| 420 EXPECT_EQ(origin2_p1, notifications_[2].pattern); | 426 EXPECT_EQ(origin2_p1, notifications_[2].pattern); |
| 427 EXPECT_EQ(registration_id3, notifications_[2].registration_id); |
| 421 EXPECT_EQ(REGISTRATION_STORED, notifications_[3].type); | 428 EXPECT_EQ(REGISTRATION_STORED, notifications_[3].type); |
| 422 EXPECT_EQ(origin3_p1, notifications_[3].pattern); | 429 EXPECT_EQ(origin3_p1, notifications_[3].pattern); |
| 430 EXPECT_EQ(registration_id4, notifications_[3].registration_id); |
| 423 EXPECT_EQ(REGISTRATION_DELETED, notifications_[4].type); | 431 EXPECT_EQ(REGISTRATION_DELETED, notifications_[4].type); |
| 424 EXPECT_EQ(origin1_p2, notifications_[4].pattern); | 432 EXPECT_EQ(origin1_p2, notifications_[4].pattern); |
| 425 EXPECT_EQ(registration_id2, notifications_[4].registration_id); | 433 EXPECT_EQ(registration_id2, notifications_[4].registration_id); |
| 426 EXPECT_EQ(REGISTRATION_DELETED, notifications_[5].type); | 434 EXPECT_EQ(REGISTRATION_DELETED, notifications_[5].type); |
| 427 EXPECT_EQ(origin1_p1, notifications_[5].pattern); | 435 EXPECT_EQ(origin1_p1, notifications_[5].pattern); |
| 428 EXPECT_EQ(registration_id1, notifications_[5].registration_id); | 436 EXPECT_EQ(registration_id1, notifications_[5].registration_id); |
| 429 } | 437 } |
| 430 | 438 |
| 431 // Make sure registering a new script shares an existing registration. | 439 // Make sure registering a new script shares an existing registration. |
| 432 TEST_F(ServiceWorkerContextTest, RegisterNewScript) { | 440 TEST_F(ServiceWorkerContextTest, RegisterNewScript) { |
| (...skipping 23 matching lines...) Expand all Loading... |
| 456 ASSERT_FALSE(called); | 464 ASSERT_FALSE(called); |
| 457 base::RunLoop().RunUntilIdle(); | 465 base::RunLoop().RunUntilIdle(); |
| 458 ASSERT_TRUE(called); | 466 ASSERT_TRUE(called); |
| 459 | 467 |
| 460 EXPECT_NE(kInvalidServiceWorkerRegistrationId, new_registration_id); | 468 EXPECT_NE(kInvalidServiceWorkerRegistrationId, new_registration_id); |
| 461 EXPECT_EQ(old_registration_id, new_registration_id); | 469 EXPECT_EQ(old_registration_id, new_registration_id); |
| 462 | 470 |
| 463 ASSERT_EQ(2u, notifications_.size()); | 471 ASSERT_EQ(2u, notifications_.size()); |
| 464 EXPECT_EQ(REGISTRATION_STORED, notifications_[0].type); | 472 EXPECT_EQ(REGISTRATION_STORED, notifications_[0].type); |
| 465 EXPECT_EQ(pattern, notifications_[0].pattern); | 473 EXPECT_EQ(pattern, notifications_[0].pattern); |
| 474 EXPECT_EQ(old_registration_id, notifications_[0].registration_id); |
| 466 EXPECT_EQ(REGISTRATION_STORED, notifications_[1].type); | 475 EXPECT_EQ(REGISTRATION_STORED, notifications_[1].type); |
| 467 EXPECT_EQ(pattern, notifications_[1].pattern); | 476 EXPECT_EQ(pattern, notifications_[1].pattern); |
| 477 EXPECT_EQ(new_registration_id, notifications_[1].registration_id); |
| 468 } | 478 } |
| 469 | 479 |
| 470 // Make sure that when registering a duplicate pattern+script_url | 480 // Make sure that when registering a duplicate pattern+script_url |
| 471 // combination, that the same registration is used. | 481 // combination, that the same registration is used. |
| 472 TEST_F(ServiceWorkerContextTest, RegisterDuplicateScript) { | 482 TEST_F(ServiceWorkerContextTest, RegisterDuplicateScript) { |
| 473 GURL pattern("http://www.example.com/"); | 483 GURL pattern("http://www.example.com/"); |
| 474 GURL script_url("http://www.example.com/service_worker.js"); | 484 GURL script_url("http://www.example.com/service_worker.js"); |
| 475 | 485 |
| 476 bool called = false; | 486 bool called = false; |
| 477 int64 old_registration_id = kInvalidServiceWorkerRegistrationId; | 487 int64 old_registration_id = kInvalidServiceWorkerRegistrationId; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 495 MakeRegisteredCallback(&called, &new_registration_id)); | 505 MakeRegisteredCallback(&called, &new_registration_id)); |
| 496 | 506 |
| 497 ASSERT_FALSE(called); | 507 ASSERT_FALSE(called); |
| 498 base::RunLoop().RunUntilIdle(); | 508 base::RunLoop().RunUntilIdle(); |
| 499 ASSERT_TRUE(called); | 509 ASSERT_TRUE(called); |
| 500 EXPECT_EQ(old_registration_id, new_registration_id); | 510 EXPECT_EQ(old_registration_id, new_registration_id); |
| 501 | 511 |
| 502 ASSERT_EQ(2u, notifications_.size()); | 512 ASSERT_EQ(2u, notifications_.size()); |
| 503 EXPECT_EQ(REGISTRATION_STORED, notifications_[0].type); | 513 EXPECT_EQ(REGISTRATION_STORED, notifications_[0].type); |
| 504 EXPECT_EQ(pattern, notifications_[0].pattern); | 514 EXPECT_EQ(pattern, notifications_[0].pattern); |
| 515 EXPECT_EQ(old_registration_id, notifications_[0].registration_id); |
| 505 EXPECT_EQ(REGISTRATION_STORED, notifications_[1].type); | 516 EXPECT_EQ(REGISTRATION_STORED, notifications_[1].type); |
| 506 EXPECT_EQ(pattern, notifications_[1].pattern); | 517 EXPECT_EQ(pattern, notifications_[1].pattern); |
| 518 EXPECT_EQ(old_registration_id, notifications_[1].registration_id); |
| 507 } | 519 } |
| 508 | 520 |
| 509 // TODO(nhiroki): Test this for on-disk storage. | 521 // TODO(nhiroki): Test this for on-disk storage. |
| 510 TEST_F(ServiceWorkerContextTest, DeleteAndStartOver) { | 522 TEST_F(ServiceWorkerContextTest, DeleteAndStartOver) { |
| 511 GURL pattern("http://www.example.com/"); | 523 GURL pattern("http://www.example.com/"); |
| 512 GURL script_url("http://www.example.com/service_worker.js"); | 524 GURL script_url("http://www.example.com/service_worker.js"); |
| 513 | 525 |
| 514 int64 registration_id = kInvalidServiceWorkerRegistrationId; | 526 int64 registration_id = kInvalidServiceWorkerRegistrationId; |
| 515 bool called = false; | 527 bool called = false; |
| 516 context()->RegisterServiceWorker( | 528 context()->RegisterServiceWorker( |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 580 true /* expect_active */)); | 592 true /* expect_active */)); |
| 581 base::RunLoop().RunUntilIdle(); | 593 base::RunLoop().RunUntilIdle(); |
| 582 | 594 |
| 583 // The new context should take over next handle ids. | 595 // The new context should take over next handle ids. |
| 584 EXPECT_EQ(1, context()->GetNewServiceWorkerHandleId()); | 596 EXPECT_EQ(1, context()->GetNewServiceWorkerHandleId()); |
| 585 EXPECT_EQ(1, context()->GetNewRegistrationHandleId()); | 597 EXPECT_EQ(1, context()->GetNewRegistrationHandleId()); |
| 586 | 598 |
| 587 ASSERT_EQ(3u, notifications_.size()); | 599 ASSERT_EQ(3u, notifications_.size()); |
| 588 EXPECT_EQ(REGISTRATION_STORED, notifications_[0].type); | 600 EXPECT_EQ(REGISTRATION_STORED, notifications_[0].type); |
| 589 EXPECT_EQ(pattern, notifications_[0].pattern); | 601 EXPECT_EQ(pattern, notifications_[0].pattern); |
| 602 EXPECT_EQ(registration_id, notifications_[0].registration_id); |
| 590 EXPECT_EQ(STORAGE_RECOVERED, notifications_[1].type); | 603 EXPECT_EQ(STORAGE_RECOVERED, notifications_[1].type); |
| 591 EXPECT_EQ(REGISTRATION_STORED, notifications_[2].type); | 604 EXPECT_EQ(REGISTRATION_STORED, notifications_[2].type); |
| 592 EXPECT_EQ(pattern, notifications_[2].pattern); | 605 EXPECT_EQ(pattern, notifications_[2].pattern); |
| 606 EXPECT_EQ(registration_id, notifications_[2].registration_id); |
| 593 } | 607 } |
| 594 | 608 |
| 595 } // namespace content | 609 } // namespace content |
| OLD | NEW |