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
{
26 context_
.reset(new ServiceWorkerContextCore(
28 base::MessageLoopProxy::current(),
31 scoped_ptr
<ServiceWorkerProcessManager
>()));
33 scope_
= GURL("http://www.example.com/*");
34 script_url_
= GURL("http://www.example.com/service_worker.js");
35 registration_
= new ServiceWorkerRegistration(
36 scope_
, script_url_
, 1L, context_
->AsWeakPtr());
37 version_
= new ServiceWorkerVersion(
39 1L, context_
->AsWeakPtr());
41 // Prepare provider hosts (for the same process).
42 scoped_ptr
<ServiceWorkerProviderHost
> host1(new ServiceWorkerProviderHost(
43 kRenderProcessId
, 1 /* provider_id */,
44 context_
->AsWeakPtr(), NULL
));
45 scoped_ptr
<ServiceWorkerProviderHost
> host2(new ServiceWorkerProviderHost(
46 kRenderProcessId
, 2 /* provider_id */,
47 context_
->AsWeakPtr(), NULL
));
48 scoped_ptr
<ServiceWorkerProviderHost
> host3(new ServiceWorkerProviderHost(
49 kRenderProcessId
, 3 /* provider_id */,
50 context_
->AsWeakPtr(), NULL
));
51 provider_host1_
= host1
->AsWeakPtr();
52 provider_host2_
= host2
->AsWeakPtr();
53 provider_host3_
= host3
->AsWeakPtr();
54 context_
->AddProviderHost(make_scoped_ptr(host1
.release()));
55 context_
->AddProviderHost(make_scoped_ptr(host2
.release()));
56 context_
->AddProviderHost(make_scoped_ptr(host3
.release()));
59 virtual void TearDown() OVERRIDE
{
65 content::TestBrowserThreadBundle thread_bundle_
;
66 scoped_ptr
<ServiceWorkerContextCore
> context_
;
67 scoped_refptr
<ServiceWorkerRegistration
> registration_
;
68 scoped_refptr
<ServiceWorkerVersion
> version_
;
69 base::WeakPtr
<ServiceWorkerProviderHost
> provider_host1_
;
70 base::WeakPtr
<ServiceWorkerProviderHost
> provider_host2_
;
71 base::WeakPtr
<ServiceWorkerProviderHost
> provider_host3_
;
76 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProviderHostTest
);
79 TEST_F(ServiceWorkerProviderHostTest
, SetActiveVersion_ProcessStatus
) {
80 ASSERT_FALSE(version_
->HasProcessToRun());
82 // Associating version_ to a provider_host's active version will internally
83 // add the provider_host's process ref to the version.
84 provider_host1_
->SetActiveVersion(version_
);
85 ASSERT_TRUE(version_
->HasProcessToRun());
87 // Re-associating the same version and provider_host should just work too.
88 provider_host1_
->SetActiveVersion(version_
);
89 ASSERT_TRUE(version_
->HasProcessToRun());
91 // Resetting the provider_host's active version should remove process refs
93 provider_host1_
->SetActiveVersion(NULL
);
94 ASSERT_FALSE(version_
->HasProcessToRun());
97 TEST_F(ServiceWorkerProviderHostTest
,
98 SetActiveVersion_MultipleHostsForSameProcess
) {
99 ASSERT_FALSE(version_
->HasProcessToRun());
101 // Associating version_ to two providers as active version.
102 provider_host1_
->SetActiveVersion(version_
);
103 provider_host2_
->SetActiveVersion(version_
);
104 ASSERT_TRUE(version_
->HasProcessToRun());
106 // Disassociating one provider_host shouldn't remove all process refs
107 // from the version yet.
108 provider_host1_
->SetActiveVersion(NULL
);
109 ASSERT_TRUE(version_
->HasProcessToRun());
111 // Disassociating the other provider_host will remove all process refs.
112 provider_host2_
->SetActiveVersion(NULL
);
113 ASSERT_FALSE(version_
->HasProcessToRun());
116 TEST_F(ServiceWorkerProviderHostTest
, SetPendingVersion_ProcessStatus
) {
117 ASSERT_FALSE(version_
->HasProcessToRun());
119 // Associating version_ to a provider_host's pending version will internally
120 // add the provider_host's process ref to the version.
121 provider_host1_
->SetPendingVersion(version_
);
122 ASSERT_TRUE(version_
->HasProcessToRun());
124 // Re-associating the same version and provider_host should just work too.
125 provider_host1_
->SetPendingVersion(version_
);
126 ASSERT_TRUE(version_
->HasProcessToRun());
128 // Resetting the provider_host's pending version should remove process refs
130 provider_host1_
->SetPendingVersion(NULL
);
131 ASSERT_FALSE(version_
->HasProcessToRun());
134 TEST_F(ServiceWorkerProviderHostTest
,
135 SetPendingVersion_MultipleHostsForSameProcess
) {
136 ASSERT_FALSE(version_
->HasProcessToRun());
138 // Associating version_ to two providers as active version.
139 provider_host1_
->SetPendingVersion(version_
);
140 provider_host2_
->SetPendingVersion(version_
);
141 ASSERT_TRUE(version_
->HasProcessToRun());
143 // Disassociating one provider_host shouldn't remove all process refs
144 // from the version yet.
145 provider_host1_
->SetPendingVersion(NULL
);
146 ASSERT_TRUE(version_
->HasProcessToRun());
148 // Disassociating the other provider_host will remove all process refs.
149 provider_host2_
->SetPendingVersion(NULL
);
150 ASSERT_FALSE(version_
->HasProcessToRun());
153 class ServiceWorkerRegisterJobAndProviderHostTest
154 : public ServiceWorkerProviderHostTest
{
156 ServiceWorkerRegisterJobAndProviderHostTest() {}
157 virtual ~ServiceWorkerRegisterJobAndProviderHostTest() {}
159 virtual void SetUp() OVERRIDE
{
160 ServiceWorkerProviderHostTest::SetUp();
161 register_job_
.reset(new ServiceWorkerRegisterJob(
162 context_
->AsWeakPtr(), scope_
, script_url_
));
163 provider_host1_
->set_document_url(GURL("http://www.example.com/foo"));
164 provider_host2_
->set_document_url(GURL("http://www.example.com/bar"));
165 provider_host3_
->set_document_url(GURL("http://www.example.ca/foo"));
168 virtual void TearDown() OVERRIDE
{
169 ServiceWorkerProviderHostTest::TearDown();
170 register_job_
.reset();
173 scoped_ptr
<ServiceWorkerRegisterJob
> register_job_
;
176 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerRegisterJobAndProviderHostTest
);
179 // Test for ServiceWorkerRegisterJob::AssociatePendingVersionToDocuments.
180 TEST_F(ServiceWorkerRegisterJobAndProviderHostTest
,
181 AssociatePendingVersionToDocuments
) {
182 register_job_
->AssociatePendingVersionToDocuments(version_
.get());
183 EXPECT_EQ(version_
.get(), provider_host1_
->pending_version());
184 EXPECT_EQ(version_
.get(), provider_host2_
->pending_version());
185 EXPECT_EQ(NULL
, provider_host3_
->pending_version());
187 register_job_
->AssociatePendingVersionToDocuments(NULL
);
188 EXPECT_EQ(NULL
, provider_host1_
->pending_version());
189 EXPECT_EQ(NULL
, provider_host2_
->pending_version());
190 EXPECT_EQ(NULL
, provider_host3_
->pending_version());
193 } // namespace content