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 "base/thread_task_runner_handle.h"
8 #include "content/browser/service_worker/service_worker_context_core.h"
9 #include "content/browser/service_worker/service_worker_provider_host.h"
10 #include "content/browser/service_worker/service_worker_register_job.h"
11 #include "content/browser/service_worker/service_worker_registration.h"
12 #include "content/browser/service_worker/service_worker_version.h"
13 #include "content/public/test/test_browser_thread_bundle.h"
14 #include "testing/gtest/include/gtest/gtest.h"
18 static const int kRenderProcessId
= 33; // Dummy process ID for testing.
20 class ServiceWorkerProviderHostTest
: public testing::Test
{
22 ServiceWorkerProviderHostTest()
23 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP
) {}
24 virtual ~ServiceWorkerProviderHostTest() {}
26 virtual void SetUp() OVERRIDE
{
28 new ServiceWorkerContextCore(base::FilePath(),
29 base::ThreadTaskRunnerHandle::Get(),
30 base::ThreadTaskRunnerHandle::Get(),
31 base::ThreadTaskRunnerHandle::Get(),
36 scope_
= GURL("http://www.example.com/");
37 script_url_
= GURL("http://www.example.com/service_worker.js");
38 registration_
= new ServiceWorkerRegistration(
39 scope_
, script_url_
, 1L, context_
->AsWeakPtr());
40 version_
= new ServiceWorkerVersion(
41 registration_
.get(), 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_
.get());
68 EXPECT_EQ(waiting
, provider_host
->waiting_version_
.get());
69 EXPECT_EQ(active
, provider_host
->active_version_
.get());
70 EXPECT_FALSE(provider_host
->controlling_version_
.get());
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_
.get());
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_
.get());
93 ASSERT_TRUE(version_
->HasProcessToRun());
95 // Re-associating the same version and provider_host should just work too.
96 registration_
->SetActiveVersion(version_
.get());
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_
.get());
108 provider_host2_
->AssociateRegistration(registration_
.get());
109 ASSERT_FALSE(version_
->HasProcessToRun());
111 // Associating version_ to two providers as active version.
112 registration_
->SetActiveVersion(version_
.get());
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_
.get());
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_
.get());
132 ASSERT_TRUE(version_
->HasProcessToRun());
134 // Re-associating the same version and provider_host should just work too.
135 registration_
->SetWaitingVersion(version_
.get());
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_
.get());
147 provider_host2_
->AssociateRegistration(registration_
.get());
148 ASSERT_FALSE(version_
->HasProcessToRun());
150 // Associating version_ to two providers as waiting version.
151 registration_
->SetWaitingVersion(version_
.get());
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_
.get());
167 provider_host2_
->AssociateRegistration(registration_
.get());
168 VerifyVersionAttributes(provider_host1_
, NULL
, NULL
, NULL
);
169 VerifyVersionAttributes(provider_host2_
, NULL
, NULL
, NULL
);
171 registration_
->SetInstallingVersion(version_
.get());
172 VerifyVersionAttributes(provider_host1_
, version_
.get(), NULL
, NULL
);
173 VerifyVersionAttributes(provider_host2_
, version_
.get(), NULL
, NULL
);
175 registration_
->SetWaitingVersion(version_
.get());
176 VerifyVersionAttributes(provider_host1_
, NULL
, version_
.get(), NULL
);
177 VerifyVersionAttributes(provider_host2_
, NULL
, version_
.get(), NULL
);
179 // Disassociating the registration should clear all version attributes.
180 provider_host2_
->UnassociateRegistration();
181 VerifyVersionAttributes(provider_host1_
, NULL
, version_
.get(), NULL
);
182 VerifyVersionAttributes(provider_host2_
, NULL
, NULL
, NULL
);
184 // Shouldn't notify the disassociated provider of the change.
185 registration_
->SetActiveVersion(version_
.get());
186 VerifyVersionAttributes(provider_host1_
, NULL
, NULL
, version_
.get());
187 VerifyVersionAttributes(provider_host2_
, NULL
, NULL
, NULL
);
190 TEST_F(ServiceWorkerProviderHostTest
,
191 ObserveVersionAttributesChanged_MultipleVersions
) {
192 provider_host1_
->AssociateRegistration(registration_
.get());
193 provider_host2_
->AssociateRegistration(registration_
.get());
194 VerifyVersionAttributes(provider_host1_
, NULL
, NULL
, NULL
);
195 VerifyVersionAttributes(provider_host2_
, NULL
, NULL
, NULL
);
197 scoped_refptr
<ServiceWorkerVersion
> version1
=
198 new ServiceWorkerVersion(registration_
.get(), 10L, context_
->AsWeakPtr());
199 scoped_refptr
<ServiceWorkerVersion
> version2
=
200 new ServiceWorkerVersion(registration_
.get(), 20L, context_
->AsWeakPtr());
202 registration_
->SetInstallingVersion(version1
.get());
203 VerifyVersionAttributes(provider_host1_
, version1
.get(), NULL
, NULL
);
204 VerifyVersionAttributes(provider_host2_
, version1
.get(), NULL
, NULL
);
206 registration_
->SetWaitingVersion(version1
.get());
207 VerifyVersionAttributes(provider_host1_
, NULL
, version1
.get(), NULL
);
208 VerifyVersionAttributes(provider_host2_
, NULL
, version1
.get(), NULL
);
210 registration_
->SetInstallingVersion(version2
.get());
211 VerifyVersionAttributes(
212 provider_host1_
, version2
.get(), version1
.get(), NULL
);
213 VerifyVersionAttributes(
214 provider_host2_
, version2
.get(), version1
.get(), NULL
);
216 // Disassociating the registration should clear all version attributes.
217 provider_host2_
->UnassociateRegistration();
218 VerifyVersionAttributes(
219 provider_host1_
, version2
.get(), version1
.get(), NULL
);
220 VerifyVersionAttributes(provider_host2_
, NULL
, NULL
, NULL
);
222 // Shouldn't notify the disassociated provider of the change.
223 registration_
->SetActiveVersion(version1
.get());
224 VerifyVersionAttributes(
225 provider_host1_
, version2
.get(), NULL
, version1
.get());
226 VerifyVersionAttributes(provider_host2_
, NULL
, NULL
, NULL
);
228 registration_
->SetActiveVersion(version2
.get());
229 VerifyVersionAttributes(provider_host1_
, NULL
, NULL
, version2
.get());
230 VerifyVersionAttributes(provider_host2_
, NULL
, NULL
, NULL
);
233 } // namespace content