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 "content/browser/service_worker/service_worker_request_handler.h" |
6 #include "base/logging.h" | 6 |
7 #include "base/run_loop.h" | 7 #include "base/run_loop.h" |
8 #include "content/browser/browser_thread_impl.h" | |
9 #include "content/browser/fileapi/mock_url_request_delegate.h" | 8 #include "content/browser/fileapi/mock_url_request_delegate.h" |
10 #include "content/browser/service_worker/embedded_worker_test_helper.h" | 9 #include "content/browser/service_worker/embedded_worker_test_helper.h" |
11 #include "content/browser/service_worker/service_worker_context_core.h" | 10 #include "content/browser/service_worker/service_worker_context_core.h" |
12 #include "content/browser/service_worker/service_worker_controllee_request_handl
er.h" | |
13 #include "content/browser/service_worker/service_worker_provider_host.h" | 11 #include "content/browser/service_worker/service_worker_provider_host.h" |
14 #include "content/browser/service_worker/service_worker_registration.h" | 12 #include "content/browser/service_worker/service_worker_registration.h" |
15 #include "content/browser/service_worker/service_worker_registration.h" | |
16 #include "content/browser/service_worker/service_worker_url_request_job.h" | |
17 #include "content/browser/service_worker/service_worker_utils.h" | 13 #include "content/browser/service_worker/service_worker_utils.h" |
18 #include "content/common/resource_request_body.h" | 14 #include "content/common/resource_request_body.h" |
19 #include "content/public/test/test_browser_thread_bundle.h" | 15 #include "content/public/test/test_browser_thread_bundle.h" |
20 #include "net/url_request/url_request_context.h" | 16 #include "net/url_request/url_request_context.h" |
21 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 #include "webkit/browser/blob/blob_storage_context.h" |
22 | 19 |
23 namespace content { | 20 namespace content { |
24 | 21 |
25 namespace { | 22 namespace { |
26 | 23 |
27 int kMockRenderProcessId = 1224; | 24 int kMockRenderProcessId = 1224; |
| 25 int kMockProviderId = 1; |
28 | 26 |
29 void EmptyCallback() {} | 27 void EmptyCallback() { |
| 28 } |
30 | 29 |
31 } | 30 } |
32 | 31 |
33 class ServiceWorkerControlleeRequestHandlerTest : public testing::Test { | 32 class ServiceWorkerRequestHandlerTest : public testing::Test { |
34 public: | 33 public: |
35 ServiceWorkerControlleeRequestHandlerTest() | 34 ServiceWorkerRequestHandlerTest() |
36 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} | 35 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} |
37 | 36 |
38 virtual void SetUp() OVERRIDE { | 37 virtual void SetUp() OVERRIDE { |
39 helper_.reset(new EmbeddedWorkerTestHelper(kMockRenderProcessId)); | 38 helper_.reset(new EmbeddedWorkerTestHelper(kMockRenderProcessId)); |
40 | 39 |
41 // A new unstored registration/version. | 40 // A new unstored registration/version. |
42 scope_ = GURL("http://host/scope/"); | |
43 script_url_ = GURL("http://host/script.js"); | |
44 registration_ = new ServiceWorkerRegistration( | 41 registration_ = new ServiceWorkerRegistration( |
45 scope_, 1L, context()->AsWeakPtr()); | 42 GURL("http://host/scope/"), 1L, context()->AsWeakPtr()); |
46 version_ = new ServiceWorkerVersion( | 43 version_ = new ServiceWorkerVersion(registration_.get(), |
47 registration_.get(), script_url_, 1L, context()->AsWeakPtr()); | 44 GURL("http://host/script.js"), |
| 45 1L, |
| 46 context()->AsWeakPtr()); |
48 | 47 |
49 // An empty host. | 48 // An empty host. |
50 scoped_ptr<ServiceWorkerProviderHost> host(new ServiceWorkerProviderHost( | 49 scoped_ptr<ServiceWorkerProviderHost> host(new ServiceWorkerProviderHost( |
51 kMockRenderProcessId, 1 /* provider_id */, | 50 kMockRenderProcessId, kMockProviderId, context()->AsWeakPtr(), NULL)); |
52 context()->AsWeakPtr(), NULL)); | |
53 provider_host_ = host->AsWeakPtr(); | 51 provider_host_ = host->AsWeakPtr(); |
54 context()->AddProviderHost(host.Pass()); | 52 context()->AddProviderHost(host.Pass()); |
55 | 53 |
56 context()->storage()->LazyInitialize(base::Bind(&EmptyCallback)); | 54 context()->storage()->LazyInitialize(base::Bind(&EmptyCallback)); |
57 base::RunLoop().RunUntilIdle(); | 55 base::RunLoop().RunUntilIdle(); |
| 56 |
| 57 version_->SetStatus(ServiceWorkerVersion::ACTIVATED); |
| 58 registration_->SetActiveVersion(version_.get()); |
| 59 context()->storage()->StoreRegistration( |
| 60 registration_.get(), |
| 61 version_.get(), |
| 62 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); |
| 63 provider_host_->AssociateRegistration(registration_.get()); |
| 64 base::RunLoop().RunUntilIdle(); |
58 } | 65 } |
59 | 66 |
60 virtual void TearDown() OVERRIDE { | 67 virtual void TearDown() OVERRIDE { |
61 version_ = NULL; | 68 version_ = NULL; |
62 registration_ = NULL; | 69 registration_ = NULL; |
63 helper_.reset(); | 70 helper_.reset(); |
64 } | 71 } |
65 | 72 |
66 ServiceWorkerContextCore* context() const { return helper_->context(); } | 73 ServiceWorkerContextCore* context() const { return helper_->context(); } |
| 74 ServiceWorkerContextWrapper* context_wrapper() const { |
| 75 return helper_->context_wrapper(); |
| 76 } |
| 77 |
| 78 bool InitializeHandlerCheck(const std::string& url, |
| 79 const std::string& method, |
| 80 bool skip_service_worker, |
| 81 ResourceType resource_type) { |
| 82 const GURL kDocUrl(url); |
| 83 scoped_ptr<net::URLRequest> request = url_request_context_.CreateRequest( |
| 84 kDocUrl, net::DEFAULT_PRIORITY, &url_request_delegate_, NULL); |
| 85 request->set_method(method); |
| 86 ServiceWorkerRequestHandler::InitializeHandler(request.get(), |
| 87 context_wrapper(), |
| 88 &blob_storage_context_, |
| 89 kMockRenderProcessId, |
| 90 kMockProviderId, |
| 91 skip_service_worker, |
| 92 resource_type, |
| 93 NULL); |
| 94 return ServiceWorkerRequestHandler::GetHandler(request.get()) != NULL; |
| 95 } |
67 | 96 |
68 protected: | 97 protected: |
69 TestBrowserThreadBundle browser_thread_bundle_; | 98 TestBrowserThreadBundle browser_thread_bundle_; |
70 scoped_ptr<EmbeddedWorkerTestHelper> helper_; | 99 scoped_ptr<EmbeddedWorkerTestHelper> helper_; |
71 scoped_refptr<ServiceWorkerRegistration> registration_; | 100 scoped_refptr<ServiceWorkerRegistration> registration_; |
72 scoped_refptr<ServiceWorkerVersion> version_; | 101 scoped_refptr<ServiceWorkerVersion> version_; |
73 base::WeakPtr<ServiceWorkerProviderHost> provider_host_; | 102 base::WeakPtr<ServiceWorkerProviderHost> provider_host_; |
74 net::URLRequestContext url_request_context_; | 103 net::URLRequestContext url_request_context_; |
75 MockURLRequestDelegate url_request_delegate_; | 104 MockURLRequestDelegate url_request_delegate_; |
76 GURL scope_; | 105 storage::BlobStorageContext blob_storage_context_; |
77 GURL script_url_; | |
78 }; | 106 }; |
79 | 107 |
80 TEST_F(ServiceWorkerControlleeRequestHandlerTest, ActivateWaitingVersion) { | 108 TEST_F(ServiceWorkerRequestHandlerTest, InitializeHandler) { |
81 // Store a registration that is installed but not activated yet. | 109 EXPECT_TRUE(InitializeHandlerCheck( |
82 version_->SetStatus(ServiceWorkerVersion::INSTALLED); | 110 "http://host/scope/doc", "GET", false, RESOURCE_TYPE_MAIN_FRAME)); |
83 registration_->SetWaitingVersion(version_.get()); | 111 EXPECT_TRUE(InitializeHandlerCheck( |
84 context()->storage()->StoreRegistration( | 112 "https://host/scope/doc", "GET", false, RESOURCE_TYPE_MAIN_FRAME)); |
85 registration_.get(), | 113 EXPECT_FALSE(InitializeHandlerCheck( |
86 version_.get(), | 114 "ftp://host/scope/doc", "GET", false, RESOURCE_TYPE_MAIN_FRAME)); |
87 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); | |
88 base::RunLoop().RunUntilIdle(); | |
89 | 115 |
90 // Conduct a main resource load. | 116 EXPECT_FALSE(InitializeHandlerCheck( |
91 const GURL kDocUrl("http://host/scope/doc"); | 117 "http://host/scope/doc", "OPTIONS", false, RESOURCE_TYPE_MAIN_FRAME)); |
92 scoped_ptr<net::URLRequest> request = url_request_context_.CreateRequest( | 118 EXPECT_FALSE(InitializeHandlerCheck( |
93 kDocUrl, | 119 "https://host/scope/doc", "OPTIONS", false, RESOURCE_TYPE_MAIN_FRAME)); |
94 net::DEFAULT_PRIORITY, | |
95 &url_request_delegate_, | |
96 NULL); | |
97 scoped_ptr<ServiceWorkerControlleeRequestHandler> handler( | |
98 new ServiceWorkerControlleeRequestHandler( | |
99 context()->AsWeakPtr(), | |
100 provider_host_, | |
101 base::WeakPtr<storage::BlobStorageContext>(), | |
102 RESOURCE_TYPE_MAIN_FRAME, | |
103 scoped_refptr<ResourceRequestBody>())); | |
104 scoped_refptr<net::URLRequestJob> job = | |
105 handler->MaybeCreateJob(request.get(), NULL); | |
106 ServiceWorkerURLRequestJob* sw_job = | |
107 static_cast<ServiceWorkerURLRequestJob*>(job.get()); | |
108 | 120 |
109 EXPECT_FALSE(sw_job->ShouldFallbackToNetwork()); | 121 provider_host_->SetDocumentUrl(GURL("")); |
110 EXPECT_FALSE(sw_job->ShouldForwardToServiceWorker()); | 122 EXPECT_FALSE(InitializeHandlerCheck( |
111 EXPECT_FALSE(version_->HasControllee()); | 123 "http://host/scope/doc", "GET", true, RESOURCE_TYPE_MAIN_FRAME)); |
| 124 EXPECT_STREQ("http://host/scope/doc", |
| 125 provider_host_->document_url().spec().c_str()); |
| 126 EXPECT_FALSE(InitializeHandlerCheck( |
| 127 "https://host/scope/doc", "GET", true, RESOURCE_TYPE_MAIN_FRAME)); |
| 128 EXPECT_STREQ("https://host/scope/doc", |
| 129 provider_host_->document_url().spec().c_str()); |
112 | 130 |
113 base::RunLoop().RunUntilIdle(); | 131 provider_host_->SetDocumentUrl(GURL("")); |
| 132 EXPECT_FALSE(InitializeHandlerCheck( |
| 133 "http://host/scope/doc", "GET", true, RESOURCE_TYPE_SUB_FRAME)); |
| 134 EXPECT_STREQ("http://host/scope/doc", |
| 135 provider_host_->document_url().spec().c_str()); |
| 136 EXPECT_FALSE(InitializeHandlerCheck( |
| 137 "https://host/scope/doc", "GET", true, RESOURCE_TYPE_SUB_FRAME)); |
| 138 EXPECT_STREQ("https://host/scope/doc", |
| 139 provider_host_->document_url().spec().c_str()); |
114 | 140 |
115 EXPECT_EQ(ServiceWorkerVersion::ACTIVATED, | 141 provider_host_->SetDocumentUrl(GURL("")); |
116 version_->status()); | 142 EXPECT_FALSE(InitializeHandlerCheck( |
117 EXPECT_FALSE(sw_job->ShouldFallbackToNetwork()); | 143 "http://host/scope/doc", "GET", true, RESOURCE_TYPE_IMAGE)); |
118 EXPECT_TRUE(sw_job->ShouldForwardToServiceWorker()); | 144 EXPECT_STREQ("", provider_host_->document_url().spec().c_str()); |
119 EXPECT_TRUE(version_->HasControllee()); | 145 EXPECT_FALSE(InitializeHandlerCheck( |
120 | 146 "https://host/scope/doc", "GET", true, RESOURCE_TYPE_IMAGE)); |
121 // Navigations should trigger an update too. | 147 EXPECT_STREQ("", provider_host_->document_url().spec().c_str()); |
122 handler.reset(NULL); | |
123 EXPECT_TRUE(version_->update_timer_.IsRunning()); | |
124 } | 148 } |
125 | 149 |
126 } // namespace content | 150 } // namespace content |
OLD | NEW |