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/files/scoped_temp_dir.h"
6#include "base/logging.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"
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_context_request_handler.h"
13#include "content/browser/service_worker/service_worker_provider_host.h"
14#include "content/browser/service_worker/service_worker_registration.h"
15#include "content/browser/service_worker/service_worker_utils.h"
16#include "content/browser/service_worker/service_worker_write_to_cache_job.h"
17#include "content/public/test/test_browser_thread_bundle.h"
18#include "net/base/load_flags.h"
19#include "net/url_request/url_request_context.h"
20#include "testing/gtest/include/gtest/gtest.h"
21
22namespace content {
23
24namespace {
25
26int kMockRenderProcessId = 1224;
27
28void EmptyCallback() {}
29
30}
31
32class ServiceWorkerContextRequestHandlerTest : public testing::Test {
33 public:
34  ServiceWorkerContextRequestHandlerTest()
35      : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {}
36
37  virtual void SetUp() OVERRIDE {
38    helper_.reset(new EmbeddedWorkerTestHelper(kMockRenderProcessId));
39
40    // A new unstored registration/version.
41    scope_ = GURL("http://host/scope/");
42    script_url_ = GURL("http://host/script.js");
43    registration_ = new ServiceWorkerRegistration(
44        scope_, 1L, context()->AsWeakPtr());
45    version_ = new ServiceWorkerVersion(
46        registration_.get(), script_url_, 1L, context()->AsWeakPtr());
47
48    // An empty host.
49    scoped_ptr<ServiceWorkerProviderHost> host(new ServiceWorkerProviderHost(
50        kMockRenderProcessId, 1 /* provider_id */,
51        context()->AsWeakPtr(), NULL));
52    provider_host_ = host->AsWeakPtr();
53    context()->AddProviderHost(host.Pass());
54
55    context()->storage()->LazyInitialize(base::Bind(&EmptyCallback));
56    base::RunLoop().RunUntilIdle();
57  }
58
59  virtual void TearDown() OVERRIDE {
60    version_ = NULL;
61    registration_ = NULL;
62    helper_.reset();
63  }
64
65  ServiceWorkerContextCore* context() const { return helper_->context(); }
66
67 protected:
68  TestBrowserThreadBundle browser_thread_bundle_;
69  scoped_ptr<EmbeddedWorkerTestHelper> helper_;
70  scoped_refptr<ServiceWorkerRegistration> registration_;
71  scoped_refptr<ServiceWorkerVersion> version_;
72  base::WeakPtr<ServiceWorkerProviderHost> provider_host_;
73  net::URLRequestContext url_request_context_;
74  MockURLRequestDelegate url_request_delegate_;
75  GURL scope_;
76  GURL script_url_;
77};
78
79TEST_F(ServiceWorkerContextRequestHandlerTest, UpdateBefore24Hours) {
80  // Give the registration a very recent last update time and pretend
81  // we're installing a new version.
82  registration_->set_last_update_check(base::Time::Now());
83  version_->SetStatus(ServiceWorkerVersion::NEW);
84  provider_host_->running_hosted_version_ = version_;
85
86  // Conduct a resource fetch for the main script.
87  const GURL kScriptUrl("http://host/script.js");
88  scoped_ptr<net::URLRequest> request = url_request_context_.CreateRequest(
89      kScriptUrl,
90      net::DEFAULT_PRIORITY,
91      &url_request_delegate_,
92      NULL);
93  scoped_ptr<ServiceWorkerContextRequestHandler> handler(
94      new ServiceWorkerContextRequestHandler(
95          context()->AsWeakPtr(),
96          provider_host_,
97          base::WeakPtr<storage::BlobStorageContext>(),
98          RESOURCE_TYPE_SERVICE_WORKER));
99  scoped_refptr<net::URLRequestJob> job =
100      handler->MaybeCreateJob(request.get(), NULL);
101  ASSERT_TRUE(job.get());
102  ServiceWorkerWriteToCacheJob* sw_job =
103      static_cast<ServiceWorkerWriteToCacheJob*>(job.get());
104
105  // Verify the net request is not initialized to bypass the browser cache.
106  EXPECT_FALSE(sw_job->net_request_->load_flags() & net::LOAD_BYPASS_CACHE);
107}
108
109TEST_F(ServiceWorkerContextRequestHandlerTest, UpdateAfter24Hours) {
110  // Give the registration a old update time and pretend
111  // we're installing a new version.
112  registration_->set_last_update_check(
113      base::Time::Now() - base::TimeDelta::FromDays(7));
114  version_->SetStatus(ServiceWorkerVersion::NEW);
115  provider_host_->running_hosted_version_ = version_;
116
117  // Conduct a resource fetch for the main script.
118  const GURL kScriptUrl("http://host/script.js");
119  scoped_ptr<net::URLRequest> request = url_request_context_.CreateRequest(
120      kScriptUrl,
121      net::DEFAULT_PRIORITY,
122      &url_request_delegate_,
123      NULL);
124  scoped_ptr<ServiceWorkerContextRequestHandler> handler(
125      new ServiceWorkerContextRequestHandler(
126          context()->AsWeakPtr(),
127          provider_host_,
128          base::WeakPtr<storage::BlobStorageContext>(),
129          RESOURCE_TYPE_SERVICE_WORKER));
130  scoped_refptr<net::URLRequestJob> job =
131      handler->MaybeCreateJob(request.get(), NULL);
132  ASSERT_TRUE(job.get());
133  ServiceWorkerWriteToCacheJob* sw_job =
134      static_cast<ServiceWorkerWriteToCacheJob*>(job.get());
135
136  // Verify the net request is initialized to bypass the browser cache.
137  EXPECT_TRUE(sw_job->net_request_->load_flags() & net::LOAD_BYPASS_CACHE);
138}
139
140}  // namespace content
141