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/embedded_worker_test_helper.h"
9 #include "content/browser/service_worker/service_worker_context_core.h"
10 #include "content/browser/service_worker/service_worker_provider_host.h"
11 #include "content/browser/service_worker/service_worker_register_job.h"
12 #include "content/browser/service_worker/service_worker_registration.h"
13 #include "content/browser/service_worker/service_worker_version.h"
14 #include "content/public/test/test_browser_thread_bundle.h"
15 #include "testing/gtest/include/gtest/gtest.h"
19 static const int kRenderProcessId
= 33; // Dummy process ID for testing.
21 class ServiceWorkerProviderHostTest
: public testing::Test
{
23 ServiceWorkerProviderHostTest()
24 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP
) {}
25 ~ServiceWorkerProviderHostTest() override
{}
27 void SetUp() override
{
28 helper_
.reset(new EmbeddedWorkerTestHelper(kRenderProcessId
));
29 context_
= helper_
->context();
30 pattern_
= GURL("http://www.example.com/");
31 script_url_
= GURL("http://www.example.com/service_worker.js");
32 registration_
= new ServiceWorkerRegistration(
33 pattern_
, 1L, context_
->AsWeakPtr());
34 version_
= new ServiceWorkerVersion(
35 registration_
.get(), script_url_
, 1L, context_
->AsWeakPtr());
37 // Prepare provider hosts (for the same process).
38 scoped_ptr
<ServiceWorkerProviderHost
> host1(new ServiceWorkerProviderHost(
39 kRenderProcessId
, MSG_ROUTING_NONE
, 1 /* provider_id */,
40 SERVICE_WORKER_PROVIDER_FOR_CONTROLLEE
,
41 context_
->AsWeakPtr(), NULL
));
42 scoped_ptr
<ServiceWorkerProviderHost
> host2(new ServiceWorkerProviderHost(
43 kRenderProcessId
, MSG_ROUTING_NONE
, 2 /* provider_id */,
44 SERVICE_WORKER_PROVIDER_FOR_CONTROLLEE
,
45 context_
->AsWeakPtr(), NULL
));
46 provider_host1_
= host1
->AsWeakPtr();
47 provider_host2_
= host2
->AsWeakPtr();
48 context_
->AddProviderHost(make_scoped_ptr(host1
.release()));
49 context_
->AddProviderHost(make_scoped_ptr(host2
.release()));
52 void TearDown() override
{
58 bool HasProcessToRun() const {
59 return context_
->process_manager()->PatternHasProcessToRun(pattern_
);
62 content::TestBrowserThreadBundle thread_bundle_
;
63 scoped_ptr
<EmbeddedWorkerTestHelper
> helper_
;
64 ServiceWorkerContextCore
* context_
;
65 scoped_refptr
<ServiceWorkerRegistration
> registration_
;
66 scoped_refptr
<ServiceWorkerVersion
> version_
;
67 base::WeakPtr
<ServiceWorkerProviderHost
> provider_host1_
;
68 base::WeakPtr
<ServiceWorkerProviderHost
> provider_host2_
;
73 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProviderHostTest
);
76 TEST_F(ServiceWorkerProviderHostTest
, SetActiveVersion_ProcessStatus
) {
77 provider_host1_
->AssociateRegistration(registration_
.get());
78 ASSERT_TRUE(HasProcessToRun());
80 // Associating version_ to a provider_host's active version will internally
81 // add the provider_host's process ref to the version.
82 registration_
->SetActiveVersion(version_
.get());
83 ASSERT_TRUE(HasProcessToRun());
85 // Re-associating the same version and provider_host should just work too.
86 registration_
->SetActiveVersion(version_
.get());
87 ASSERT_TRUE(HasProcessToRun());
89 // Resetting the provider_host's active version should remove process refs
91 provider_host1_
->DisassociateRegistration();
92 ASSERT_FALSE(HasProcessToRun());
95 TEST_F(ServiceWorkerProviderHostTest
,
96 SetActiveVersion_MultipleHostsForSameProcess
) {
97 provider_host1_
->AssociateRegistration(registration_
.get());
98 provider_host2_
->AssociateRegistration(registration_
.get());
99 ASSERT_TRUE(HasProcessToRun());
101 // Associating version_ to two providers as active version.
102 registration_
->SetActiveVersion(version_
.get());
103 ASSERT_TRUE(HasProcessToRun());
105 // Disassociating one provider_host shouldn't remove all process refs
106 // from the version yet.
107 provider_host1_
->DisassociateRegistration();
108 ASSERT_TRUE(HasProcessToRun());
110 // Disassociating the other provider_host will remove all process refs.
111 provider_host2_
->DisassociateRegistration();
112 ASSERT_FALSE(HasProcessToRun());
115 TEST_F(ServiceWorkerProviderHostTest
, SetWaitingVersion_ProcessStatus
) {
116 provider_host1_
->AssociateRegistration(registration_
.get());
117 ASSERT_TRUE(HasProcessToRun());
119 // Associating version_ to a provider_host's waiting version will internally
120 // add the provider_host's process ref to the version.
121 registration_
->SetWaitingVersion(version_
.get());
122 ASSERT_TRUE(HasProcessToRun());
124 // Re-associating the same version and provider_host should just work too.
125 registration_
->SetWaitingVersion(version_
.get());
126 ASSERT_TRUE(HasProcessToRun());
128 // Resetting the provider_host's waiting version should remove process refs
130 provider_host1_
->DisassociateRegistration();
131 ASSERT_FALSE(HasProcessToRun());
134 TEST_F(ServiceWorkerProviderHostTest
,
135 SetWaitingVersion_MultipleHostsForSameProcess
) {
136 provider_host1_
->AssociateRegistration(registration_
.get());
137 provider_host2_
->AssociateRegistration(registration_
.get());
138 ASSERT_TRUE(HasProcessToRun());
140 // Associating version_ to two providers as waiting version.
141 registration_
->SetWaitingVersion(version_
.get());
142 ASSERT_TRUE(HasProcessToRun());
144 // Disassociating one provider_host shouldn't remove all process refs
145 // from the version yet.
146 provider_host1_
->DisassociateRegistration();
147 ASSERT_TRUE(HasProcessToRun());
149 // Disassociating the other provider_host will remove all process refs.
150 provider_host2_
->DisassociateRegistration();
151 ASSERT_FALSE(HasProcessToRun());
154 } // namespace content