service_worker_provider_host_unittest.cc revision 0529e5d033099cbfc42635f6f6183833b09dff6e
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/memory/weak_ptr.h"
7#include "content/browser/service_worker/service_worker_context_core.h"
8#include "content/browser/service_worker/service_worker_provider_host.h"
9#include "content/browser/service_worker/service_worker_register_job.h"
10#include "content/browser/service_worker/service_worker_registration.h"
11#include "content/browser/service_worker/service_worker_version.h"
12#include "content/public/test/test_browser_thread_bundle.h"
13#include "testing/gtest/include/gtest/gtest.h"
14
15namespace content {
16
17static const int kRenderProcessId = 33;  // Dummy process ID for testing.
18
19class ServiceWorkerProviderHostTest : public testing::Test {
20 protected:
21  ServiceWorkerProviderHostTest()
22      : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {}
23  virtual ~ServiceWorkerProviderHostTest() {}
24
25  virtual void SetUp() OVERRIDE {
26    context_.reset(new ServiceWorkerContextCore(base::FilePath(), NULL, NULL));
27
28    scope_ = GURL("http://www.example.com/*");
29    script_url_ = GURL("http://www.example.com/service_worker.js");
30    registration_ = new ServiceWorkerRegistration(
31        scope_, script_url_, 1L, context_->AsWeakPtr());
32    version_ = new ServiceWorkerVersion(
33        registration_,
34        1L, context_->AsWeakPtr());
35
36    // Prepare provider hosts (for the same process).
37    scoped_ptr<ServiceWorkerProviderHost> host1(new ServiceWorkerProviderHost(
38        kRenderProcessId, 1 /* provider_id */,
39        context_->AsWeakPtr(), NULL));
40    scoped_ptr<ServiceWorkerProviderHost> host2(new ServiceWorkerProviderHost(
41        kRenderProcessId, 2 /* provider_id */,
42        context_->AsWeakPtr(), NULL));
43    scoped_ptr<ServiceWorkerProviderHost> host3(new ServiceWorkerProviderHost(
44        kRenderProcessId, 3 /* provider_id */,
45        context_->AsWeakPtr(), NULL));
46    provider_host1_ = host1->AsWeakPtr();
47    provider_host2_ = host2->AsWeakPtr();
48    provider_host3_ = host3->AsWeakPtr();
49    context_->AddProviderHost(make_scoped_ptr(host1.release()));
50    context_->AddProviderHost(make_scoped_ptr(host2.release()));
51    context_->AddProviderHost(make_scoped_ptr(host3.release()));
52  }
53
54  virtual void TearDown() OVERRIDE {
55    version_ = 0;
56    registration_ = 0;
57    context_.reset();
58  }
59
60  content::TestBrowserThreadBundle thread_bundle_;
61  scoped_ptr<ServiceWorkerContextCore> context_;
62  scoped_refptr<ServiceWorkerRegistration> registration_;
63  scoped_refptr<ServiceWorkerVersion> version_;
64  base::WeakPtr<ServiceWorkerProviderHost> provider_host1_;
65  base::WeakPtr<ServiceWorkerProviderHost> provider_host2_;
66  base::WeakPtr<ServiceWorkerProviderHost> provider_host3_;
67  GURL scope_;
68  GURL script_url_;
69
70 private:
71  DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProviderHostTest);
72};
73
74TEST_F(ServiceWorkerProviderHostTest, SetActiveVersion_ProcessStatus) {
75  ASSERT_FALSE(version_->HasProcessToRun());
76
77  // Associating version_ to a provider_host's active version will internally
78  // add the provider_host's process ref to the version.
79  provider_host1_->SetActiveVersion(version_);
80  ASSERT_TRUE(version_->HasProcessToRun());
81
82  // Re-associating the same version and provider_host should just work too.
83  provider_host1_->SetActiveVersion(version_);
84  ASSERT_TRUE(version_->HasProcessToRun());
85
86  // Resetting the provider_host's active version should remove process refs
87  // from the version.
88  provider_host1_->SetActiveVersion(NULL);
89  ASSERT_FALSE(version_->HasProcessToRun());
90}
91
92TEST_F(ServiceWorkerProviderHostTest,
93       SetActiveVersion_MultipleHostsForSameProcess) {
94  ASSERT_FALSE(version_->HasProcessToRun());
95
96  // Associating version_ to two providers as active version.
97  provider_host1_->SetActiveVersion(version_);
98  provider_host2_->SetActiveVersion(version_);
99  ASSERT_TRUE(version_->HasProcessToRun());
100
101  // Disassociating one provider_host shouldn't remove all process refs
102  // from the version yet.
103  provider_host1_->SetActiveVersion(NULL);
104  ASSERT_TRUE(version_->HasProcessToRun());
105
106  // Disassociating the other provider_host will remove all process refs.
107  provider_host2_->SetActiveVersion(NULL);
108  ASSERT_FALSE(version_->HasProcessToRun());
109}
110
111TEST_F(ServiceWorkerProviderHostTest, SetPendingVersion_ProcessStatus) {
112  ASSERT_FALSE(version_->HasProcessToRun());
113
114  // Associating version_ to a provider_host's pending version will internally
115  // add the provider_host's process ref to the version.
116  provider_host1_->SetPendingVersion(version_);
117  ASSERT_TRUE(version_->HasProcessToRun());
118
119  // Re-associating the same version and provider_host should just work too.
120  provider_host1_->SetPendingVersion(version_);
121  ASSERT_TRUE(version_->HasProcessToRun());
122
123  // Resetting the provider_host's pending version should remove process refs
124  // from the version.
125  provider_host1_->SetPendingVersion(NULL);
126  ASSERT_FALSE(version_->HasProcessToRun());
127}
128
129TEST_F(ServiceWorkerProviderHostTest,
130       SetPendingVersion_MultipleHostsForSameProcess) {
131  ASSERT_FALSE(version_->HasProcessToRun());
132
133  // Associating version_ to two providers as active version.
134  provider_host1_->SetPendingVersion(version_);
135  provider_host2_->SetPendingVersion(version_);
136  ASSERT_TRUE(version_->HasProcessToRun());
137
138  // Disassociating one provider_host shouldn't remove all process refs
139  // from the version yet.
140  provider_host1_->SetPendingVersion(NULL);
141  ASSERT_TRUE(version_->HasProcessToRun());
142
143  // Disassociating the other provider_host will remove all process refs.
144  provider_host2_->SetPendingVersion(NULL);
145  ASSERT_FALSE(version_->HasProcessToRun());
146}
147
148class ServiceWorkerRegisterJobAndProviderHostTest
149    : public ServiceWorkerProviderHostTest {
150 protected:
151  ServiceWorkerRegisterJobAndProviderHostTest() {}
152  virtual ~ServiceWorkerRegisterJobAndProviderHostTest() {}
153
154  virtual void SetUp() OVERRIDE {
155    ServiceWorkerProviderHostTest::SetUp();
156    register_job_.reset(new ServiceWorkerRegisterJob(
157        context_->AsWeakPtr(), scope_, script_url_));
158    provider_host1_->set_document_url(GURL("http://www.example.com/foo"));
159    provider_host2_->set_document_url(GURL("http://www.example.com/bar"));
160    provider_host3_->set_document_url(GURL("http://www.example.ca/foo"));
161  }
162
163  virtual void TearDown() OVERRIDE {
164    ServiceWorkerProviderHostTest::TearDown();
165    register_job_.reset();
166  }
167
168  scoped_ptr<ServiceWorkerRegisterJob> register_job_;
169
170 private:
171  DISALLOW_COPY_AND_ASSIGN(ServiceWorkerRegisterJobAndProviderHostTest);
172};
173
174// Test for ServiceWorkerRegisterJob::AssociatePendingVersionToDocuments.
175TEST_F(ServiceWorkerRegisterJobAndProviderHostTest,
176       AssociatePendingVersionToDocuments) {
177  register_job_->AssociatePendingVersionToDocuments(version_.get());
178  EXPECT_EQ(version_.get(), provider_host1_->pending_version());
179  EXPECT_EQ(version_.get(), provider_host2_->pending_version());
180  EXPECT_EQ(NULL, provider_host3_->pending_version());
181
182  register_job_->AssociatePendingVersionToDocuments(NULL);
183  EXPECT_EQ(NULL, provider_host1_->pending_version());
184  EXPECT_EQ(NULL, provider_host2_->pending_version());
185  EXPECT_EQ(NULL, provider_host3_->pending_version());
186}
187
188}  // namespace content
189