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_context.h"
7 #include "base/files/scoped_temp_dir.h"
8 #include "base/logging.h"
9 #include "base/message_loop/message_loop.h"
10 #include "content/browser/browser_thread_impl.h"
11 #include "content/browser/service_worker/embedded_worker_registry.h"
12 #include "content/browser/service_worker/embedded_worker_test_helper.h"
13 #include "content/browser/service_worker/service_worker_context_core.h"
14 #include "content/browser/service_worker/service_worker_registration.h"
15 #include "content/public/test/test_browser_thread_bundle.h"
16 #include "content/public/test/test_utils.h"
17 #include "testing/gtest/include/gtest/gtest.h"
23 void SaveResponseCallback(bool* called
,
25 ServiceWorkerStatusCode status
,
28 *store_result
= result
;
31 ServiceWorkerContextCore::RegistrationCallback
MakeRegisteredCallback(
33 int64
* store_result
) {
34 return base::Bind(&SaveResponseCallback
, called
, store_result
);
37 void CallCompletedCallback(bool* called
, ServiceWorkerStatusCode
) {
41 ServiceWorkerContextCore::UnregistrationCallback
MakeUnregisteredCallback(
43 return base::Bind(&CallCompletedCallback
, called
);
48 class ServiceWorkerContextTest
: public testing::Test
{
50 ServiceWorkerContextTest()
51 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP
),
52 render_process_id_(99) {}
54 virtual void SetUp() OVERRIDE
{
55 context_
.reset(new ServiceWorkerContextCore(base::FilePath(), NULL
));
56 helper_
.reset(new EmbeddedWorkerTestHelper(
57 context_
.get(), render_process_id_
));
60 virtual void TearDown() OVERRIDE
{
66 TestBrowserThreadBundle browser_thread_bundle_
;
67 scoped_ptr
<ServiceWorkerContextCore
> context_
;
68 scoped_ptr
<EmbeddedWorkerTestHelper
> helper_
;
69 const int render_process_id_
;
72 void RegistrationCallback(
73 scoped_refptr
<ServiceWorkerRegistration
>* registration
,
74 const scoped_refptr
<ServiceWorkerRegistration
>& result
) {
75 *registration
= result
;
78 // Make sure basic registration is working.
79 TEST_F(ServiceWorkerContextTest
, Register
) {
80 int64 registration_id
= -1L;
82 context_
->RegisterServiceWorker(
83 GURL("http://www.example.com/*"),
84 GURL("http://www.example.com/service_worker.js"),
86 MakeRegisteredCallback(&called
, ®istration_id
));
89 base::RunLoop().RunUntilIdle();
92 EXPECT_EQ(1UL, helper_
->ipc_sink()->message_count());
93 EXPECT_NE(-1L, registration_id
);
96 // Make sure registrations are cleaned up when they are unregistered.
97 TEST_F(ServiceWorkerContextTest
, Unregister
) {
98 GURL
pattern("http://www.example.com/*");
101 int64 registration_id
= -1L;
102 context_
->RegisterServiceWorker(
104 GURL("http://www.example.com/service_worker.js"),
106 MakeRegisteredCallback(&called
, ®istration_id
));
108 ASSERT_FALSE(called
);
109 base::RunLoop().RunUntilIdle();
113 context_
->UnregisterServiceWorker(
114 pattern
, render_process_id_
, MakeUnregisteredCallback(&called
));
116 ASSERT_FALSE(called
);
117 base::RunLoop().RunUntilIdle();
121 // Make sure that when a new registration replaces an existing
122 // registration, that the old one is cleaned up.
123 TEST_F(ServiceWorkerContextTest
, RegisterNewScript
) {
124 GURL
pattern("http://www.example.com/*");
127 int64 old_registration_id
= -1L;
128 context_
->RegisterServiceWorker(
130 GURL("http://www.example.com/service_worker.js"),
132 MakeRegisteredCallback(&called
, &old_registration_id
));
134 ASSERT_FALSE(called
);
135 base::RunLoop().RunUntilIdle();
139 int64 new_registration_id
= -1L;
140 context_
->RegisterServiceWorker(
142 GURL("http://www.example.com/service_worker_new.js"),
144 MakeRegisteredCallback(&called
, &new_registration_id
));
146 ASSERT_FALSE(called
);
147 base::RunLoop().RunUntilIdle();
150 ASSERT_NE(old_registration_id
, new_registration_id
);
153 // Make sure that when registering a duplicate pattern+script_url
154 // combination, that the same registration is used.
155 TEST_F(ServiceWorkerContextTest
, RegisterDuplicateScript
) {
156 GURL
pattern("http://www.example.com/*");
157 GURL
script_url("http://www.example.com/service_worker.js");
160 int64 old_registration_id
= -1L;
161 context_
->RegisterServiceWorker(
165 MakeRegisteredCallback(&called
, &old_registration_id
));
167 ASSERT_FALSE(called
);
168 base::RunLoop().RunUntilIdle();
172 int64 new_registration_id
= -1L;
173 context_
->RegisterServiceWorker(
177 MakeRegisteredCallback(&called
, &new_registration_id
));
179 ASSERT_FALSE(called
);
180 base::RunLoop().RunUntilIdle();
183 ASSERT_EQ(old_registration_id
, new_registration_id
);
186 } // namespace content