1 // Copyright 2013 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 "content/browser/service_worker/service_worker_registration.h"
7 #include "base/files/scoped_temp_dir.h"
8 #include "base/logging.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/run_loop.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "content/browser/browser_thread_impl.h"
13 #include "content/browser/service_worker/service_worker_context_core.h"
14 #include "content/browser/service_worker/service_worker_registration_handle.h"
15 #include "testing/gtest/include/gtest/gtest.h"
20 class ServiceWorkerRegistrationTest
: public testing::Test
{
22 ServiceWorkerRegistrationTest()
23 : io_thread_(BrowserThread::IO
, &message_loop_
) {}
25 void SetUp() override
{
26 scoped_ptr
<ServiceWorkerDatabaseTaskManager
> database_task_manager(
27 new MockServiceWorkerDatabaseTaskManager(
28 base::ThreadTaskRunnerHandle::Get()));
30 new ServiceWorkerContextCore(base::FilePath(),
31 base::ThreadTaskRunnerHandle::Get(),
32 database_task_manager
.Pass(),
33 base::ThreadTaskRunnerHandle::Get(),
38 context_ptr_
= context_
->AsWeakPtr();
41 void TearDown() override
{
43 base::RunLoop().RunUntilIdle();
46 class RegistrationListener
: public ServiceWorkerRegistration::Listener
{
48 RegistrationListener() {}
49 ~RegistrationListener() {
50 if (observed_registration_
.get())
51 observed_registration_
->RemoveListener(this);
54 void OnVersionAttributesChanged(
55 ServiceWorkerRegistration
* registration
,
56 ChangedVersionAttributesMask changed_mask
,
57 const ServiceWorkerRegistrationInfo
& info
) override
{
58 observed_registration_
= registration
;
59 observed_changed_mask_
= changed_mask
;
60 observed_info_
= info
;
63 void OnRegistrationFailed(
64 ServiceWorkerRegistration
* registration
) override
{
68 void OnRegistrationFinishedUninstalling(
69 ServiceWorkerRegistration
* registration
) override
{
73 void OnUpdateFound(ServiceWorkerRegistration
* registration
) override
{
78 observed_registration_
= NULL
;
79 observed_changed_mask_
= ChangedVersionAttributesMask();
80 observed_info_
= ServiceWorkerRegistrationInfo();
83 scoped_refptr
<ServiceWorkerRegistration
> observed_registration_
;
84 ChangedVersionAttributesMask observed_changed_mask_
;
85 ServiceWorkerRegistrationInfo observed_info_
;
89 scoped_ptr
<ServiceWorkerContextCore
> context_
;
90 base::WeakPtr
<ServiceWorkerContextCore
> context_ptr_
;
91 base::MessageLoopForIO message_loop_
;
92 BrowserThreadImpl io_thread_
;
95 TEST_F(ServiceWorkerRegistrationTest
, SetAndUnsetVersions
) {
96 const GURL
kScope("http://www.example.not/");
97 const GURL
kScript("http://www.example.not/service_worker.js");
98 int64 kRegistrationId
= 1L;
99 scoped_refptr
<ServiceWorkerRegistration
> registration
=
100 new ServiceWorkerRegistration(
105 const int64 version_1_id
= 1L;
106 const int64 version_2_id
= 2L;
107 scoped_refptr
<ServiceWorkerVersion
> version_1
= new ServiceWorkerVersion(
108 registration
.get(), kScript
, version_1_id
, context_ptr_
);
109 scoped_refptr
<ServiceWorkerVersion
> version_2
= new ServiceWorkerVersion(
110 registration
.get(), kScript
, version_2_id
, context_ptr_
);
112 RegistrationListener listener
;
113 registration
->AddListener(&listener
);
114 registration
->SetActiveVersion(version_1
.get());
116 EXPECT_EQ(version_1
.get(), registration
->active_version());
117 EXPECT_EQ(registration
, listener
.observed_registration_
);
118 EXPECT_EQ(ChangedVersionAttributesMask::ACTIVE_VERSION
,
119 listener
.observed_changed_mask_
.changed());
120 EXPECT_EQ(kScope
, listener
.observed_info_
.pattern
);
121 EXPECT_EQ(version_1_id
, listener
.observed_info_
.active_version
.version_id
);
122 EXPECT_EQ(kScript
, listener
.observed_info_
.active_version
.script_url
);
123 EXPECT_EQ(listener
.observed_info_
.installing_version
.version_id
,
124 kInvalidServiceWorkerVersionId
);
125 EXPECT_EQ(listener
.observed_info_
.waiting_version
.version_id
,
126 kInvalidServiceWorkerVersionId
);
129 registration
->SetInstallingVersion(version_2
.get());
131 EXPECT_EQ(version_2
.get(), registration
->installing_version());
132 EXPECT_EQ(ChangedVersionAttributesMask::INSTALLING_VERSION
,
133 listener
.observed_changed_mask_
.changed());
134 EXPECT_EQ(version_1_id
, listener
.observed_info_
.active_version
.version_id
);
135 EXPECT_EQ(version_2_id
,
136 listener
.observed_info_
.installing_version
.version_id
);
137 EXPECT_EQ(listener
.observed_info_
.waiting_version
.version_id
,
138 kInvalidServiceWorkerVersionId
);
141 registration
->SetWaitingVersion(version_2
.get());
143 EXPECT_EQ(version_2
.get(), registration
->waiting_version());
144 EXPECT_FALSE(registration
->installing_version());
145 EXPECT_TRUE(listener
.observed_changed_mask_
.waiting_changed());
146 EXPECT_TRUE(listener
.observed_changed_mask_
.installing_changed());
147 EXPECT_EQ(version_1_id
, listener
.observed_info_
.active_version
.version_id
);
148 EXPECT_EQ(version_2_id
, listener
.observed_info_
.waiting_version
.version_id
);
149 EXPECT_EQ(listener
.observed_info_
.installing_version
.version_id
,
150 kInvalidServiceWorkerVersionId
);
153 registration
->UnsetVersion(version_2
.get());
155 EXPECT_FALSE(registration
->waiting_version());
156 EXPECT_EQ(ChangedVersionAttributesMask::WAITING_VERSION
,
157 listener
.observed_changed_mask_
.changed());
158 EXPECT_EQ(version_1_id
, listener
.observed_info_
.active_version
.version_id
);
159 EXPECT_EQ(listener
.observed_info_
.waiting_version
.version_id
,
160 kInvalidServiceWorkerVersionId
);
161 EXPECT_EQ(listener
.observed_info_
.installing_version
.version_id
,
162 kInvalidServiceWorkerVersionId
);
165 TEST_F(ServiceWorkerRegistrationTest
, FailedRegistrationNoCrash
) {
166 const GURL
kScope("http://www.example.not/");
167 int64 kRegistrationId
= 1L;
168 scoped_refptr
<ServiceWorkerRegistration
> registration
=
169 new ServiceWorkerRegistration(
173 scoped_ptr
<ServiceWorkerRegistrationHandle
> handle(
174 new ServiceWorkerRegistrationHandle(
176 base::WeakPtr
<ServiceWorkerProviderHost
>(),
177 registration
.get()));
178 registration
->NotifyRegistrationFailed();
179 // Don't crash when handle gets destructed.
182 } // namespace content