931cace061a643f5876c6dadd256a801702b9f2a
[platform/framework/web/crosswalk.git] / src / content / browser / service_worker / service_worker_url_request_job_unittest.cc
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.
4
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"
25
26 namespace content {
27
28 namespace {
29
30 const int kBufferSize = 1024;
31 const int kProcessID = 1;
32 const int kProviderID = 100;
33
34 class MockURLRequestDelegate : public net::URLRequest::Delegate {
35  public:
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());
41       Read(request);
42     }
43   }
44   virtual void OnReadCompleted(net::URLRequest* request,
45                                int bytes_read) OVERRIDE {
46     EXPECT_EQ(0, bytes_read);
47   }
48
49  private:
50   void Read(net::URLRequest* request) {
51     if (request->is_pending()) {
52       int bytes_read = 0;
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);
57     }
58   }
59
60   scoped_refptr<net::IOBuffer> received_data_;
61   base::Closure on_complete_;
62 };
63
64 class MockProtocolHandler : public net::URLRequestJobFactory::ProtocolHandler {
65  public:
66   MockProtocolHandler(base::WeakPtr<ServiceWorkerProviderHost> provider_host)
67       : provider_host_(provider_host) {}
68   virtual ~MockProtocolHandler() {}
69
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();
76     return job;
77   }
78
79  private:
80   base::WeakPtr<ServiceWorkerProviderHost> provider_host_;
81 };
82
83 }  // namespace
84
85 class ServiceWorkerURLRequestJobTest : public testing::Test {
86  protected:
87   ServiceWorkerURLRequestJobTest()
88       : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {}
89   virtual ~ServiceWorkerURLRequestJobTest() {}
90
91   virtual void SetUp() OVERRIDE {
92     helper_.reset(new EmbeddedWorkerTestHelper(kProcessID));
93
94     registration_ = new ServiceWorkerRegistration(
95         GURL("http://example.com/*"),
96         GURL("http://example.com/service_worker.js"),
97         1L,
98         helper_->context()->AsWeakPtr());
99     version_ = new ServiceWorkerVersion(
100         registration_, 1L, helper_->context()->AsWeakPtr());
101
102     scoped_ptr<ServiceWorkerProviderHost> provider_host(
103         new ServiceWorkerProviderHost(
104             kProcessID, kProviderID, helper_->context()->AsWeakPtr(), NULL));
105     provider_host->SetActiveVersion(version_.get());
106
107     url_request_job_factory_.SetProtocolHandler(
108         "http", new MockProtocolHandler(provider_host->AsWeakPtr()));
109     url_request_context_.set_job_factory(&url_request_job_factory_);
110
111     helper_->context()->AddProviderHost(provider_host.Pass());
112   }
113
114   virtual void TearDown() OVERRIDE {
115     version_ = NULL;
116     registration_ = NULL;
117     helper_.reset();
118   }
119
120   void TestRequest() {
121     request_ = url_request_context_.CreateRequest(
122         GURL("http://example.com/foo.html"),
123         net::DEFAULT_PRIORITY,
124         &url_request_delegate_,
125         NULL);
126
127     request_->set_method("GET");
128     request_->Start();
129     base::RunLoop().RunUntilIdle();
130
131     // Verify response.
132     EXPECT_TRUE(request_->status().is_success());
133     EXPECT_EQ(200, request_->response_headers()->response_code());
134   }
135
136   TestBrowserThreadBundle thread_bundle_;
137
138   scoped_ptr<EmbeddedWorkerTestHelper> helper_;
139   scoped_refptr<ServiceWorkerRegistration> registration_;
140   scoped_refptr<ServiceWorkerVersion> version_;
141
142   net::URLRequestJobFactoryImpl url_request_job_factory_;
143   net::URLRequestContext url_request_context_;
144   MockURLRequestDelegate url_request_delegate_;
145   scoped_ptr<net::URLRequest> request_;
146
147   DISALLOW_COPY_AND_ASSIGN(ServiceWorkerURLRequestJobTest);
148 };
149
150 TEST_F(ServiceWorkerURLRequestJobTest, Simple) {
151   version_->SetStatus(ServiceWorkerVersion::ACTIVE);
152   TestRequest();
153 }
154
155 TEST_F(ServiceWorkerURLRequestJobTest, WaitForActivation) {
156   ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED;
157   version_->SetStatus(ServiceWorkerVersion::INSTALLED);
158   version_->DispatchActivateEvent(CreateReceiverOnCurrentThread(&status));
159
160   TestRequest();
161
162   EXPECT_EQ(SERVICE_WORKER_OK, status);
163 }
164
165 // TODO(kinuko): Add more tests with different response data and also for
166 // FallbackToNetwork case.
167
168 }  // namespace content