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/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"
17 static const int kRenderProcessId = 33; // Dummy process ID for testing.
19 class ServiceWorkerProviderHostTest : public testing::Test {
21 ServiceWorkerProviderHostTest()
22 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {}
23 virtual ~ServiceWorkerProviderHostTest() {}
25 virtual void SetUp() OVERRIDE {
27 new ServiceWorkerContextCore(base::FilePath(),
28 base::MessageLoopProxy::current(),
29 base::MessageLoopProxy::current(),
30 base::MessageLoopProxy::current(),
35 scope_ = GURL("http://www.example.com/");
36 script_url_ = GURL("http://www.example.com/service_worker.js");
37 registration_ = new ServiceWorkerRegistration(
38 scope_, script_url_, 1L, context_->AsWeakPtr());
39 version_ = new ServiceWorkerVersion(
41 1L, context_->AsWeakPtr());
43 // Prepare provider hosts (for the same process).
44 scoped_ptr<ServiceWorkerProviderHost> host1(new ServiceWorkerProviderHost(
45 kRenderProcessId, 1 /* provider_id */,
46 context_->AsWeakPtr(), NULL));
47 scoped_ptr<ServiceWorkerProviderHost> host2(new ServiceWorkerProviderHost(
48 kRenderProcessId, 2 /* provider_id */,
49 context_->AsWeakPtr(), NULL));
50 provider_host1_ = host1->AsWeakPtr();
51 provider_host2_ = host2->AsWeakPtr();
52 context_->AddProviderHost(make_scoped_ptr(host1.release()));
53 context_->AddProviderHost(make_scoped_ptr(host2.release()));
56 virtual void TearDown() OVERRIDE {
62 void VerifyVersionAttributes(
63 base::WeakPtr<ServiceWorkerProviderHost> provider_host,
64 ServiceWorkerVersion* installing,
65 ServiceWorkerVersion* waiting,
66 ServiceWorkerVersion* active) {
67 EXPECT_EQ(installing, provider_host->installing_version_);
68 EXPECT_EQ(waiting, provider_host->waiting_version_);
69 EXPECT_EQ(active, provider_host->active_version_);
70 EXPECT_FALSE(provider_host->controlling_version_);
73 content::TestBrowserThreadBundle thread_bundle_;
74 scoped_ptr<ServiceWorkerContextCore> context_;
75 scoped_refptr<ServiceWorkerRegistration> registration_;
76 scoped_refptr<ServiceWorkerVersion> version_;
77 base::WeakPtr<ServiceWorkerProviderHost> provider_host1_;
78 base::WeakPtr<ServiceWorkerProviderHost> provider_host2_;
83 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProviderHostTest);
86 TEST_F(ServiceWorkerProviderHostTest, SetActiveVersion_ProcessStatus) {
87 provider_host1_->AssociateRegistration(registration_);
88 ASSERT_FALSE(version_->HasProcessToRun());
90 // Associating version_ to a provider_host's active version will internally
91 // add the provider_host's process ref to the version.
92 registration_->SetActiveVersion(version_);
93 ASSERT_TRUE(version_->HasProcessToRun());
95 // Re-associating the same version and provider_host should just work too.
96 registration_->SetActiveVersion(version_);
97 ASSERT_TRUE(version_->HasProcessToRun());
99 // Resetting the provider_host's active version should remove process refs
101 provider_host1_->UnassociateRegistration();
102 ASSERT_FALSE(version_->HasProcessToRun());
105 TEST_F(ServiceWorkerProviderHostTest,
106 SetActiveVersion_MultipleHostsForSameProcess) {
107 provider_host1_->AssociateRegistration(registration_);
108 provider_host2_->AssociateRegistration(registration_);
109 ASSERT_FALSE(version_->HasProcessToRun());
111 // Associating version_ to two providers as active version.
112 registration_->SetActiveVersion(version_);
113 ASSERT_TRUE(version_->HasProcessToRun());
115 // Disassociating one provider_host shouldn't remove all process refs
116 // from the version yet.
117 provider_host1_->UnassociateRegistration();
118 ASSERT_TRUE(version_->HasProcessToRun());
120 // Disassociating the other provider_host will remove all process refs.
121 provider_host2_->UnassociateRegistration();
122 ASSERT_FALSE(version_->HasProcessToRun());
125 TEST_F(ServiceWorkerProviderHostTest, SetWaitingVersion_ProcessStatus) {
126 provider_host1_->AssociateRegistration(registration_);
127 ASSERT_FALSE(version_->HasProcessToRun());
129 // Associating version_ to a provider_host's waiting version will internally
130 // add the provider_host's process ref to the version.
131 registration_->SetWaitingVersion(version_);
132 ASSERT_TRUE(version_->HasProcessToRun());
134 // Re-associating the same version and provider_host should just work too.
135 registration_->SetWaitingVersion(version_);
136 ASSERT_TRUE(version_->HasProcessToRun());
138 // Resetting the provider_host's waiting version should remove process refs
140 provider_host1_->UnassociateRegistration();
141 ASSERT_FALSE(version_->HasProcessToRun());
144 TEST_F(ServiceWorkerProviderHostTest,
145 SetWaitingVersion_MultipleHostsForSameProcess) {
146 provider_host1_->AssociateRegistration(registration_);
147 provider_host2_->AssociateRegistration(registration_);
148 ASSERT_FALSE(version_->HasProcessToRun());
150 // Associating version_ to two providers as waiting version.
151 registration_->SetWaitingVersion(version_);
152 ASSERT_TRUE(version_->HasProcessToRun());
154 // Disassociating one provider_host shouldn't remove all process refs
155 // from the version yet.
156 provider_host1_->UnassociateRegistration();
157 ASSERT_TRUE(version_->HasProcessToRun());
159 // Disassociating the other provider_host will remove all process refs.
160 provider_host2_->UnassociateRegistration();
161 ASSERT_FALSE(version_->HasProcessToRun());
164 TEST_F(ServiceWorkerProviderHostTest,
165 ObserveVersionAttributesChanged_Basic) {
166 provider_host1_->AssociateRegistration(registration_);
167 provider_host2_->AssociateRegistration(registration_);
168 VerifyVersionAttributes(provider_host1_, NULL, NULL, NULL);
169 VerifyVersionAttributes(provider_host2_, NULL, NULL, NULL);
171 registration_->SetInstallingVersion(version_);
172 VerifyVersionAttributes(provider_host1_, version_, NULL, NULL);
173 VerifyVersionAttributes(provider_host2_, version_, NULL, NULL);
175 registration_->SetWaitingVersion(version_);
176 VerifyVersionAttributes(provider_host1_, NULL, version_, NULL);
177 VerifyVersionAttributes(provider_host2_, NULL, version_, NULL);
179 // Disassociating the registration should clear all version attributes.
180 provider_host2_->UnassociateRegistration();
181 VerifyVersionAttributes(provider_host1_, NULL, version_, NULL);
182 VerifyVersionAttributes(provider_host2_, NULL, NULL, NULL);
184 // Shouldn't notify the disassociated provider of the change.
185 registration_->SetActiveVersion(version_);
186 VerifyVersionAttributes(provider_host1_, NULL, NULL, version_);
187 VerifyVersionAttributes(provider_host2_, NULL, NULL, NULL);
190 TEST_F(ServiceWorkerProviderHostTest,
191 ObserveVersionAttributesChanged_MultipleVersions) {
192 provider_host1_->AssociateRegistration(registration_);
193 provider_host2_->AssociateRegistration(registration_);
194 VerifyVersionAttributes(provider_host1_, NULL, NULL, NULL);
195 VerifyVersionAttributes(provider_host2_, NULL, NULL, NULL);
197 scoped_refptr<ServiceWorkerVersion> version1 =
198 new ServiceWorkerVersion(registration_, 10L, context_->AsWeakPtr());
199 scoped_refptr<ServiceWorkerVersion> version2 =
200 new ServiceWorkerVersion(registration_, 20L, context_->AsWeakPtr());
202 registration_->SetInstallingVersion(version1);
203 VerifyVersionAttributes(provider_host1_, version1, NULL, NULL);
204 VerifyVersionAttributes(provider_host2_, version1, NULL, NULL);
206 registration_->SetWaitingVersion(version1);
207 VerifyVersionAttributes(provider_host1_, NULL, version1, NULL);
208 VerifyVersionAttributes(provider_host2_, NULL, version1, NULL);
210 registration_->SetInstallingVersion(version2);
211 VerifyVersionAttributes(provider_host1_, version2, version1, NULL);
212 VerifyVersionAttributes(provider_host2_, version2, version1, NULL);
214 // Disassociating the registration should clear all version attributes.
215 provider_host2_->UnassociateRegistration();
216 VerifyVersionAttributes(provider_host1_, version2, version1, NULL);
217 VerifyVersionAttributes(provider_host2_, NULL, NULL, NULL);
219 // Shouldn't notify the disassociated provider of the change.
220 registration_->SetActiveVersion(version1);
221 VerifyVersionAttributes(provider_host1_, version2, NULL, version1);
222 VerifyVersionAttributes(provider_host2_, NULL, NULL, NULL);
224 registration_->SetActiveVersion(version2);
225 VerifyVersionAttributes(provider_host1_, NULL, NULL, version2);
226 VerifyVersionAttributes(provider_host2_, NULL, NULL, NULL);
229 } // namespace content