service_worker_controllee_request_handler_unittest.cc revision 5f1c94371a64b3196d4be9466099bb892df9b88e
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_controllee_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_registration.h"
16#include "content/browser/service_worker/service_worker_url_request_job.h"
17#include "content/browser/service_worker/service_worker_utils.h"
18#include "content/public/test/test_browser_thread_bundle.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 ServiceWorkerControlleeRequestHandlerTest : public testing::Test {
33 public:
34  ServiceWorkerControlleeRequestHandlerTest()
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_, script_url_, 1L, context()->AsWeakPtr());
45    version_ = new ServiceWorkerVersion(
46        registration_,
47        1L, context()->AsWeakPtr());
48
49    // An empty host.
50    scoped_ptr<ServiceWorkerProviderHost> host(new ServiceWorkerProviderHost(
51        kMockRenderProcessId, 1 /* provider_id */,
52        context()->AsWeakPtr(), NULL));
53    provider_host_ = host->AsWeakPtr();
54    context()->AddProviderHost(make_scoped_ptr(host.release()));
55
56    context()->storage()->LazyInitialize(base::Bind(&EmptyCallback));
57    base::RunLoop().RunUntilIdle();
58  }
59
60  virtual void TearDown() OVERRIDE {
61    version_ = NULL;
62    registration_ = NULL;
63    helper_.reset();
64  }
65
66  ServiceWorkerContextCore* context() const { return helper_->context(); }
67
68 protected:
69  TestBrowserThreadBundle browser_thread_bundle_;
70  scoped_ptr<EmbeddedWorkerTestHelper> helper_;
71  scoped_refptr<ServiceWorkerRegistration> registration_;
72  scoped_refptr<ServiceWorkerVersion> version_;
73  base::WeakPtr<ServiceWorkerProviderHost> provider_host_;
74  net::URLRequestContext url_request_context_;
75  MockURLRequestDelegate url_request_delegate_;
76  GURL scope_;
77  GURL script_url_;
78};
79
80TEST_F(ServiceWorkerControlleeRequestHandlerTest, ActivateWaitingVersion) {
81  // Store a registration that is installed but not activated yet.
82  version_->SetStatus(ServiceWorkerVersion::INSTALLED);
83  registration_->SetWaitingVersion(version_);
84  context()->storage()->StoreRegistration(
85      registration_, version_,
86      base::Bind(&ServiceWorkerUtils::NoOpStatusCallback));
87  base::RunLoop().RunUntilIdle();
88
89  // Conduct a main resource load.
90  const GURL kDocUrl("http://host/scope/doc");
91  scoped_ptr<net::URLRequest> request = url_request_context_.CreateRequest(
92      kDocUrl,
93      net::DEFAULT_PRIORITY,
94      &url_request_delegate_,
95      NULL);
96  scoped_ptr<ServiceWorkerControlleeRequestHandler> handler(
97      new ServiceWorkerControlleeRequestHandler(
98          context()->AsWeakPtr(),
99          provider_host_,
100          base::WeakPtr<webkit_blob::BlobStorageContext>(),
101          RESOURCE_TYPE_MAIN_FRAME));
102  scoped_refptr<net::URLRequestJob> job =
103      handler->MaybeCreateJob(request.get(), NULL);
104  ServiceWorkerURLRequestJob* sw_job =
105      static_cast<ServiceWorkerURLRequestJob*>(job.get());
106
107  EXPECT_FALSE(sw_job->ShouldFallbackToNetwork());
108  EXPECT_FALSE(sw_job->ShouldForwardToServiceWorker());
109  EXPECT_FALSE(version_->HasControllee());
110
111  base::RunLoop().RunUntilIdle();
112
113  EXPECT_EQ(ServiceWorkerVersion::ACTIVATED,
114            version_->status());
115  EXPECT_FALSE(sw_job->ShouldFallbackToNetwork());
116  EXPECT_TRUE(sw_job->ShouldForwardToServiceWorker());
117  EXPECT_TRUE(version_->HasControllee());
118
119  // Navigations should trigger an update too.
120  handler.reset(NULL);
121  EXPECT_TRUE(version_->update_timer_.IsRunning());
122}
123
124}  // namespace content
125