Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/browser/service_worker/foreign_fetch_request_handler.h" | 5 #include "content/browser/service_worker/foreign_fetch_request_handler.h" |
| 6 | 6 |
| 7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
| 8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
| 9 #include "base/test/simple_test_clock.h" | |
| 9 #include "base/test/simple_test_tick_clock.h" | 10 #include "base/test/simple_test_tick_clock.h" |
| 10 #include "content/browser/browser_thread_impl.h" | 11 #include "content/browser/browser_thread_impl.h" |
| 11 #include "content/browser/service_worker/embedded_worker_test_helper.h" | 12 #include "content/browser/service_worker/embedded_worker_test_helper.h" |
| 12 #include "content/browser/service_worker/service_worker_context_core.h" | 13 #include "content/browser/service_worker/service_worker_context_core.h" |
| 13 #include "content/browser/service_worker/service_worker_registration.h" | 14 #include "content/browser/service_worker/service_worker_registration.h" |
| 14 #include "content/browser/service_worker/service_worker_test_utils.h" | 15 #include "content/browser/service_worker/service_worker_test_utils.h" |
| 15 #include "content/browser/service_worker/service_worker_version.h" | 16 #include "content/browser/service_worker/service_worker_version.h" |
| 16 #include "content/common/service_worker/service_worker_utils.h" | 17 #include "content/common/service_worker/service_worker_utils.h" |
| 17 #include "content/public/common/content_client.h" | 18 #include "content/public/common/content_client.h" |
| 18 #include "content/public/common/origin_trial_policy.h" | 19 #include "content/public/common/origin_trial_policy.h" |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 43 const uint8_t kTestPublicKey[] = { | 44 const uint8_t kTestPublicKey[] = { |
| 44 0x75, 0x10, 0xac, 0xf9, 0x3a, 0x1c, 0xb8, 0xa9, 0x28, 0x70, 0xd2, | 45 0x75, 0x10, 0xac, 0xf9, 0x3a, 0x1c, 0xb8, 0xa9, 0x28, 0x70, 0xd2, |
| 45 0x9a, 0xd0, 0x0b, 0x59, 0xe1, 0xac, 0x2b, 0xb7, 0xd5, 0xca, 0x1f, | 46 0x9a, 0xd0, 0x0b, 0x59, 0xe1, 0xac, 0x2b, 0xb7, 0xd5, 0xca, 0x1f, |
| 46 0x64, 0x90, 0x08, 0x8e, 0xa8, 0xe0, 0x56, 0x3a, 0x04, 0xd0, | 47 0x64, 0x90, 0x08, 0x8e, 0xa8, 0xe0, 0x56, 0x3a, 0x04, 0xd0, |
| 47 }; | 48 }; |
| 48 | 49 |
| 49 int kMockProviderId = 1; | 50 int kMockProviderId = 1; |
| 50 | 51 |
| 51 const char* kValidUrl = "https://valid.example.com/foo/bar"; | 52 const char* kValidUrl = "https://valid.example.com/foo/bar"; |
| 52 | 53 |
| 54 // This timestamp is set to a time after the expiry timestamp of the expired | |
| 55 // tokens in this test, but before the expiry timestamp of the valid ones. | |
| 56 double kNowTimestamp = 1500000000; | |
| 57 | |
| 53 void EmptyCallback() {} | 58 void EmptyCallback() {} |
| 54 | 59 |
| 55 } // namespace | 60 } // namespace |
| 56 | 61 |
| 57 class ForeignFetchRequestHandlerTest : public testing::Test { | 62 class ForeignFetchRequestHandlerTest : public testing::Test { |
| 58 public: | 63 public: |
| 59 ForeignFetchRequestHandlerTest() | 64 ForeignFetchRequestHandlerTest() |
| 60 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) { | 65 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) { |
| 61 SetContentClient(&test_content_client_); | 66 SetContentClient(&test_content_client_); |
| 62 SetBrowserClientForTesting(&test_content_browser_client_); | 67 SetBrowserClientForTesting(&test_content_browser_client_); |
| 63 } | 68 } |
| 64 ~ForeignFetchRequestHandlerTest() override {} | 69 ~ForeignFetchRequestHandlerTest() override {} |
| 65 | 70 |
| 66 void SetUp() override { | 71 void SetUp() override { |
| 67 const GURL kScope("https://valid.example.com/scope/"); | 72 const GURL kScope("https://valid.example.com/scope/"); |
| 68 const GURL kResource1("https://valid.example.com/scope/sw.js"); | 73 const GURL kResource1("https://valid.example.com/scope/sw.js"); |
| 69 const int64_t kRegistrationId = 0; | 74 const int64_t kRegistrationId = 0; |
| 70 const int64_t kVersionId = 0; | 75 const int64_t kVersionId = 0; |
| 71 helper_.reset(new EmbeddedWorkerTestHelper(base::FilePath())); | 76 helper_.reset(new EmbeddedWorkerTestHelper(base::FilePath())); |
| 72 | 77 |
| 73 // Create a registration for the worker which has foreign fetch event | 78 // Create a registration for the worker which has foreign fetch event |
| 74 // handler. | 79 // handler. |
| 75 registration_ = | 80 registration_ = |
| 76 new ServiceWorkerRegistration(ServiceWorkerRegistrationOptions(kScope), | 81 new ServiceWorkerRegistration(ServiceWorkerRegistrationOptions(kScope), |
| 77 kRegistrationId, context()->AsWeakPtr()); | 82 kRegistrationId, context()->AsWeakPtr()); |
| 78 version_ = new ServiceWorkerVersion(registration_.get(), kResource1, | 83 version_ = new ServiceWorkerVersion(registration_.get(), kResource1, |
| 79 kVersionId, context()->AsWeakPtr()); | 84 kVersionId, context()->AsWeakPtr()); |
| 80 version_->set_foreign_fetch_scopes({kScope}); | 85 version_->set_foreign_fetch_scopes({kScope}); |
| 81 | 86 |
| 87 // Fix the time for testing to kNowTimestamp | |
| 88 base::SimpleTestClock* clock = new base::SimpleTestClock(); | |
| 89 clock->SetNow(base::Time::FromDoubleT(kNowTimestamp)); | |
| 90 version_->SetClockForTesting(base::WrapUnique(clock)); | |
|
Marijn Kruisselbrink
2017/08/02 19:57:25
why WrapUnique here, and not simply base::MakeUniq
iclelland
2017/08/02 20:22:05
Because I need to call SetNow() on it -- if I make
Marijn Kruisselbrink
2017/08/02 20:34:20
Yeah, that's what I meant. std::unique_ptr<Foo> is
| |
| 91 | |
| 82 context()->storage()->LazyInitialize(base::Bind(&EmptyCallback)); | 92 context()->storage()->LazyInitialize(base::Bind(&EmptyCallback)); |
| 83 base::RunLoop().RunUntilIdle(); | 93 base::RunLoop().RunUntilIdle(); |
| 84 | 94 |
| 85 // Persist the registration data. | 95 // Persist the registration data. |
| 86 std::vector<ServiceWorkerDatabase::ResourceRecord> records; | 96 std::vector<ServiceWorkerDatabase::ResourceRecord> records; |
| 87 records.push_back( | 97 records.push_back( |
| 88 ServiceWorkerDatabase::ResourceRecord(10, version_->script_url(), 100)); | 98 ServiceWorkerDatabase::ResourceRecord(10, version_->script_url(), 100)); |
| 89 version_->script_cache_map()->SetResources(records); | 99 version_->script_cache_map()->SetResources(records); |
| 90 version_->set_fetch_handler_existence( | 100 version_->set_fetch_handler_existence( |
| 91 ServiceWorkerVersion::FetchHandlerExistence::EXISTS); | 101 ServiceWorkerVersion::FetchHandlerExistence::EXISTS); |
| (...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 384 // Make sure new request only gets remaining timeout. | 394 // Make sure new request only gets remaining timeout. |
| 385 ForeignFetchRequestHandler* handler = | 395 ForeignFetchRequestHandler* handler = |
| 386 InitializeHandler("https://valid.example.com/foo", RESOURCE_TYPE_IMAGE, | 396 InitializeHandler("https://valid.example.com/foo", RESOURCE_TYPE_IMAGE, |
| 387 nullptr /* initiator */); | 397 nullptr /* initiator */); |
| 388 ASSERT_TRUE(handler); | 398 ASSERT_TRUE(handler); |
| 389 ASSERT_TRUE(timeout_for_request(handler).has_value()); | 399 ASSERT_TRUE(timeout_for_request(handler).has_value()); |
| 390 EXPECT_EQ(remaining_time, timeout_for_request(handler).value()); | 400 EXPECT_EQ(remaining_time, timeout_for_request(handler).value()); |
| 391 } | 401 } |
| 392 | 402 |
| 393 } // namespace content | 403 } // namespace content |
| OLD | NEW |