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 virtual void SetUp() OVERRIDE
{
27 new ServiceWorkerContextCore(base::FilePath(),
28 base::ThreadTaskRunnerHandle::Get(),
29 base::ThreadTaskRunnerHandle::Get(),
30 base::ThreadTaskRunnerHandle::Get(),
34 context_ptr_
= context_
->AsWeakPtr();
37 virtual void TearDown() OVERRIDE
{
39 base::RunLoop().RunUntilIdle();
42 class RegistrationListener
: public ServiceWorkerRegistration::Listener
{
44 RegistrationListener() {}
45 ~RegistrationListener() {
46 if (observed_registration_
.get())
47 observed_registration_
->RemoveListener(this);
50 virtual void OnVersionAttributesChanged(
51 ServiceWorkerRegistration
* registration
,
52 ChangedVersionAttributesMask changed_mask
,
53 const ServiceWorkerRegistrationInfo
& info
) OVERRIDE
{
54 observed_registration_
= registration
;
55 observed_changed_mask_
= changed_mask
;
56 observed_info_
= info
;
59 virtual void OnRegistrationFailed(
60 ServiceWorkerRegistration
* registration
) OVERRIDE
{
65 observed_registration_
= NULL
;
66 observed_changed_mask_
= ChangedVersionAttributesMask();
67 observed_info_
= ServiceWorkerRegistrationInfo();
70 scoped_refptr
<ServiceWorkerRegistration
> observed_registration_
;
71 ChangedVersionAttributesMask observed_changed_mask_
;
72 ServiceWorkerRegistrationInfo observed_info_
;
76 scoped_ptr
<ServiceWorkerContextCore
> context_
;
77 base::WeakPtr
<ServiceWorkerContextCore
> context_ptr_
;
78 base::MessageLoopForIO message_loop_
;
79 BrowserThreadImpl io_thread_
;
82 TEST_F(ServiceWorkerRegistrationTest
, SetAndUnsetVersions
) {
83 const GURL
kScope("http://www.example.not/");
84 const GURL
kScript("http://www.example.not/service_worker.js");
85 int64 kRegistrationId
= 1L;
86 scoped_refptr
<ServiceWorkerRegistration
> registration
=
87 new ServiceWorkerRegistration(
92 const int64 version_1_id
= 1L;
93 const int64 version_2_id
= 2L;
94 scoped_refptr
<ServiceWorkerVersion
> version_1
= new ServiceWorkerVersion(
95 registration
.get(), kScript
, version_1_id
, context_ptr_
);
96 scoped_refptr
<ServiceWorkerVersion
> version_2
= new ServiceWorkerVersion(
97 registration
.get(), kScript
, version_2_id
, context_ptr_
);
99 RegistrationListener listener
;
100 registration
->AddListener(&listener
);
101 registration
->SetActiveVersion(version_1
.get());
103 EXPECT_EQ(version_1
.get(), registration
->active_version());
104 EXPECT_EQ(registration
, listener
.observed_registration_
);
105 EXPECT_EQ(ChangedVersionAttributesMask::ACTIVE_VERSION
,
106 listener
.observed_changed_mask_
.changed());
107 EXPECT_EQ(kScope
, listener
.observed_info_
.pattern
);
108 EXPECT_EQ(version_1_id
, listener
.observed_info_
.active_version
.version_id
);
109 EXPECT_EQ(kScript
, listener
.observed_info_
.active_version
.script_url
);
110 EXPECT_TRUE(listener
.observed_info_
.installing_version
.is_null
);
111 EXPECT_TRUE(listener
.observed_info_
.waiting_version
.is_null
);
112 EXPECT_TRUE(listener
.observed_info_
.controlling_version
.is_null
);
115 registration
->SetInstallingVersion(version_2
.get());
117 EXPECT_EQ(version_2
.get(), registration
->installing_version());
118 EXPECT_EQ(ChangedVersionAttributesMask::INSTALLING_VERSION
,
119 listener
.observed_changed_mask_
.changed());
120 EXPECT_EQ(version_1_id
, listener
.observed_info_
.active_version
.version_id
);
121 EXPECT_EQ(version_2_id
,
122 listener
.observed_info_
.installing_version
.version_id
);
123 EXPECT_TRUE(listener
.observed_info_
.waiting_version
.is_null
);
124 EXPECT_TRUE(listener
.observed_info_
.controlling_version
.is_null
);
127 registration
->SetWaitingVersion(version_2
.get());
129 EXPECT_EQ(version_2
.get(), registration
->waiting_version());
130 EXPECT_FALSE(registration
->installing_version());
131 EXPECT_TRUE(listener
.observed_changed_mask_
.waiting_changed());
132 EXPECT_TRUE(listener
.observed_changed_mask_
.installing_changed());
133 EXPECT_EQ(version_1_id
, listener
.observed_info_
.active_version
.version_id
);
134 EXPECT_EQ(version_2_id
, listener
.observed_info_
.waiting_version
.version_id
);
135 EXPECT_TRUE(listener
.observed_info_
.installing_version
.is_null
);
136 EXPECT_TRUE(listener
.observed_info_
.controlling_version
.is_null
);
139 registration
->UnsetVersion(version_2
.get());
141 EXPECT_FALSE(registration
->waiting_version());
142 EXPECT_EQ(ChangedVersionAttributesMask::WAITING_VERSION
,
143 listener
.observed_changed_mask_
.changed());
144 EXPECT_EQ(version_1_id
, listener
.observed_info_
.active_version
.version_id
);
145 EXPECT_TRUE(listener
.observed_info_
.waiting_version
.is_null
);
146 EXPECT_TRUE(listener
.observed_info_
.installing_version
.is_null
);
147 EXPECT_TRUE(listener
.observed_info_
.controlling_version
.is_null
);
150 TEST_F(ServiceWorkerRegistrationTest
, FailedRegistrationNoCrash
) {
151 const GURL
kScope("http://www.example.not/");
152 int64 kRegistrationId
= 1L;
154 scoped_refptr
<ServiceWorkerRegistration
> registration
=
155 new ServiceWorkerRegistration(
159 scoped_ptr
<ServiceWorkerRegistrationHandle
> handle(
160 new ServiceWorkerRegistrationHandle(context_ptr_
,
163 registration
.get()));
164 registration
->NotifyRegistrationFailed();
165 // Don't crash when handle gets destructed.
168 } // namespace content