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/run_loop.h"
10 #include "base/thread_task_runner_handle.h"
11 #include "content/browser/service_worker/service_worker_context_core.h"
12 #include "content/browser/service_worker/service_worker_registration_handle.h"
13 #include "content/public/test/test_browser_thread_bundle.h"
14 #include "testing/gtest/include/gtest/gtest.h"
19 class ServiceWorkerRegistrationTest
: public testing::Test
{
21 ServiceWorkerRegistrationTest()
22 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP
) {}
24 void SetUp() override
{
25 scoped_ptr
<ServiceWorkerDatabaseTaskManager
> database_task_manager(
26 new MockServiceWorkerDatabaseTaskManager(
27 base::ThreadTaskRunnerHandle::Get()));
29 new ServiceWorkerContextCore(base::FilePath(),
30 database_task_manager
.Pass(),
31 base::ThreadTaskRunnerHandle::Get(),
36 context_ptr_
= context_
->AsWeakPtr();
39 void TearDown() override
{
41 base::RunLoop().RunUntilIdle();
44 class RegistrationListener
: public ServiceWorkerRegistration::Listener
{
46 RegistrationListener() {}
47 ~RegistrationListener() {
48 if (observed_registration_
.get())
49 observed_registration_
->RemoveListener(this);
52 void OnVersionAttributesChanged(
53 ServiceWorkerRegistration
* registration
,
54 ChangedVersionAttributesMask changed_mask
,
55 const ServiceWorkerRegistrationInfo
& info
) override
{
56 observed_registration_
= registration
;
57 observed_changed_mask_
= changed_mask
;
58 observed_info_
= info
;
61 void OnRegistrationFailed(
62 ServiceWorkerRegistration
* registration
) override
{
66 void OnUpdateFound(ServiceWorkerRegistration
* registration
) override
{
71 observed_registration_
= NULL
;
72 observed_changed_mask_
= ChangedVersionAttributesMask();
73 observed_info_
= ServiceWorkerRegistrationInfo();
76 scoped_refptr
<ServiceWorkerRegistration
> observed_registration_
;
77 ChangedVersionAttributesMask observed_changed_mask_
;
78 ServiceWorkerRegistrationInfo observed_info_
;
82 scoped_ptr
<ServiceWorkerContextCore
> context_
;
83 base::WeakPtr
<ServiceWorkerContextCore
> context_ptr_
;
84 TestBrowserThreadBundle thread_bundle_
;
87 TEST_F(ServiceWorkerRegistrationTest
, SetAndUnsetVersions
) {
88 const GURL
kScope("http://www.example.not/");
89 const GURL
kScript("http://www.example.not/service_worker.js");
90 int64 kRegistrationId
= 1L;
91 scoped_refptr
<ServiceWorkerRegistration
> registration
=
92 new ServiceWorkerRegistration(
97 const int64 version_1_id
= 1L;
98 const int64 version_2_id
= 2L;
99 scoped_refptr
<ServiceWorkerVersion
> version_1
= new ServiceWorkerVersion(
100 registration
.get(), kScript
, version_1_id
, context_ptr_
);
101 scoped_refptr
<ServiceWorkerVersion
> version_2
= new ServiceWorkerVersion(
102 registration
.get(), kScript
, version_2_id
, context_ptr_
);
104 RegistrationListener listener
;
105 registration
->AddListener(&listener
);
106 registration
->SetActiveVersion(version_1
);
108 EXPECT_EQ(version_1
.get(), registration
->active_version());
109 EXPECT_EQ(registration
, listener
.observed_registration_
);
110 EXPECT_EQ(ChangedVersionAttributesMask::ACTIVE_VERSION
,
111 listener
.observed_changed_mask_
.changed());
112 EXPECT_EQ(kScope
, listener
.observed_info_
.pattern
);
113 EXPECT_EQ(version_1_id
, listener
.observed_info_
.active_version
.version_id
);
114 EXPECT_EQ(kScript
, listener
.observed_info_
.active_version
.script_url
);
115 EXPECT_EQ(listener
.observed_info_
.installing_version
.version_id
,
116 kInvalidServiceWorkerVersionId
);
117 EXPECT_EQ(listener
.observed_info_
.waiting_version
.version_id
,
118 kInvalidServiceWorkerVersionId
);
121 registration
->SetInstallingVersion(version_2
);
123 EXPECT_EQ(version_2
.get(), registration
->installing_version());
124 EXPECT_EQ(ChangedVersionAttributesMask::INSTALLING_VERSION
,
125 listener
.observed_changed_mask_
.changed());
126 EXPECT_EQ(version_1_id
, listener
.observed_info_
.active_version
.version_id
);
127 EXPECT_EQ(version_2_id
,
128 listener
.observed_info_
.installing_version
.version_id
);
129 EXPECT_EQ(listener
.observed_info_
.waiting_version
.version_id
,
130 kInvalidServiceWorkerVersionId
);
133 registration
->SetWaitingVersion(version_2
);
135 EXPECT_EQ(version_2
.get(), registration
->waiting_version());
136 EXPECT_FALSE(registration
->installing_version());
137 EXPECT_TRUE(listener
.observed_changed_mask_
.waiting_changed());
138 EXPECT_TRUE(listener
.observed_changed_mask_
.installing_changed());
139 EXPECT_EQ(version_1_id
, listener
.observed_info_
.active_version
.version_id
);
140 EXPECT_EQ(version_2_id
, listener
.observed_info_
.waiting_version
.version_id
);
141 EXPECT_EQ(listener
.observed_info_
.installing_version
.version_id
,
142 kInvalidServiceWorkerVersionId
);
145 registration
->UnsetVersion(version_2
.get());
147 EXPECT_FALSE(registration
->waiting_version());
148 EXPECT_EQ(ChangedVersionAttributesMask::WAITING_VERSION
,
149 listener
.observed_changed_mask_
.changed());
150 EXPECT_EQ(version_1_id
, listener
.observed_info_
.active_version
.version_id
);
151 EXPECT_EQ(listener
.observed_info_
.waiting_version
.version_id
,
152 kInvalidServiceWorkerVersionId
);
153 EXPECT_EQ(listener
.observed_info_
.installing_version
.version_id
,
154 kInvalidServiceWorkerVersionId
);
157 TEST_F(ServiceWorkerRegistrationTest
, FailedRegistrationNoCrash
) {
158 const GURL
kScope("http://www.example.not/");
159 int64 kRegistrationId
= 1L;
160 scoped_refptr
<ServiceWorkerRegistration
> registration
=
161 new ServiceWorkerRegistration(
165 scoped_ptr
<ServiceWorkerRegistrationHandle
> handle(
166 new ServiceWorkerRegistrationHandle(
168 base::WeakPtr
<ServiceWorkerProviderHost
>(),
169 registration
.get()));
170 registration
->NotifyRegistrationFailed();
171 // Don't crash when handle gets destructed.
174 } // namespace content