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
3 // found in the LICENSE file.
5 #include "base/basictypes.h"
6 #include "base/callback.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/run_loop.h"
9 #include "content/browser/service_worker/embedded_worker_registry.h"
10 #include "content/browser/service_worker/embedded_worker_test_helper.h"
11 #include "content/browser/service_worker/service_worker_context_core.h"
12 #include "content/browser/service_worker/service_worker_provider_host.h"
13 #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_url_request_job.h"
16 #include "content/browser/service_worker/service_worker_version.h"
17 #include "content/public/test/test_browser_thread_bundle.h"
18 #include "net/base/io_buffer.h"
19 #include "net/http/http_request_headers.h"
20 #include "net/http/http_response_headers.h"
21 #include "net/url_request/url_request.h"
22 #include "net/url_request/url_request_context.h"
23 #include "net/url_request/url_request_job_factory_impl.h"
24 #include "testing/gtest/include/gtest/gtest.h"
30 const int kBufferSize = 1024;
31 const int kProcessID = 1;
32 const int kProviderID = 100;
34 class MockURLRequestDelegate : public net::URLRequest::Delegate {
36 MockURLRequestDelegate() : received_data_(new net::IOBuffer(kBufferSize)) {}
37 virtual ~MockURLRequestDelegate() {}
38 virtual void OnResponseStarted(net::URLRequest* request) OVERRIDE {
39 if (request->status().is_success()) {
40 EXPECT_TRUE(request->response_headers());
44 virtual void OnReadCompleted(net::URLRequest* request,
45 int bytes_read) OVERRIDE {
46 EXPECT_EQ(0, bytes_read);
50 void Read(net::URLRequest* request) {
51 if (request->is_pending()) {
53 request->Read(received_data_.get(), kBufferSize, &bytes_read);
54 // For now ServiceWorkerURLRequestJob wouldn't return
55 // any content data yet.
56 EXPECT_EQ(0, bytes_read);
60 scoped_refptr<net::IOBuffer> received_data_;
61 base::Closure on_complete_;
64 class MockProtocolHandler : public net::URLRequestJobFactory::ProtocolHandler {
66 MockProtocolHandler(base::WeakPtr<ServiceWorkerProviderHost> provider_host)
67 : provider_host_(provider_host) {}
68 virtual ~MockProtocolHandler() {}
70 virtual net::URLRequestJob* MaybeCreateJob(
71 net::URLRequest* request,
72 net::NetworkDelegate* network_delegate) const OVERRIDE {
73 ServiceWorkerURLRequestJob* job = new ServiceWorkerURLRequestJob(
74 request, network_delegate, provider_host_);
75 job->ForwardToServiceWorker();
80 base::WeakPtr<ServiceWorkerProviderHost> provider_host_;
85 class ServiceWorkerURLRequestJobTest : public testing::Test {
87 ServiceWorkerURLRequestJobTest()
88 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {}
89 virtual ~ServiceWorkerURLRequestJobTest() {}
91 virtual void SetUp() OVERRIDE {
92 helper_.reset(new EmbeddedWorkerTestHelper(kProcessID));
94 registration_ = new ServiceWorkerRegistration(
95 GURL("http://example.com/*"),
96 GURL("http://example.com/service_worker.js"),
98 helper_->context()->AsWeakPtr());
99 version_ = new ServiceWorkerVersion(
100 registration_, 1L, helper_->context()->AsWeakPtr());
102 scoped_ptr<ServiceWorkerProviderHost> provider_host(
103 new ServiceWorkerProviderHost(
104 kProcessID, kProviderID, helper_->context()->AsWeakPtr(), NULL));
105 provider_host->SetActiveVersion(version_.get());
107 url_request_job_factory_.SetProtocolHandler(
108 "http", new MockProtocolHandler(provider_host->AsWeakPtr()));
109 url_request_context_.set_job_factory(&url_request_job_factory_);
111 helper_->context()->AddProviderHost(provider_host.Pass());
114 virtual void TearDown() OVERRIDE {
116 registration_ = NULL;
121 request_ = url_request_context_.CreateRequest(
122 GURL("http://example.com/foo.html"),
123 net::DEFAULT_PRIORITY,
124 &url_request_delegate_,
127 request_->set_method("GET");
129 base::RunLoop().RunUntilIdle();
132 EXPECT_TRUE(request_->status().is_success());
133 EXPECT_EQ(200, request_->response_headers()->response_code());
136 TestBrowserThreadBundle thread_bundle_;
138 scoped_ptr<EmbeddedWorkerTestHelper> helper_;
139 scoped_refptr<ServiceWorkerRegistration> registration_;
140 scoped_refptr<ServiceWorkerVersion> version_;
142 net::URLRequestJobFactoryImpl url_request_job_factory_;
143 net::URLRequestContext url_request_context_;
144 MockURLRequestDelegate url_request_delegate_;
145 scoped_ptr<net::URLRequest> request_;
147 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerURLRequestJobTest);
150 TEST_F(ServiceWorkerURLRequestJobTest, Simple) {
151 version_->SetStatus(ServiceWorkerVersion::ACTIVE);
155 TEST_F(ServiceWorkerURLRequestJobTest, WaitForActivation) {
156 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED;
157 version_->SetStatus(ServiceWorkerVersion::INSTALLED);
158 version_->DispatchActivateEvent(CreateReceiverOnCurrentThread(&status));
162 EXPECT_EQ(SERVICE_WORKER_OK, status);
165 // TODO(kinuko): Add more tests with different response data and also for
166 // FallbackToNetwork case.
168 } // namespace content