Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(824)

Side by Side Diff: content/browser/service_worker/service_worker_context_unittest.cc

Issue 605163002: Service Worker: Remove legacy code for resolving register() to a version (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: only resolve on active_version Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/public/browser/service_worker_context.h" 5 #include "content/public/browser/service_worker_context.h"
6 6
7 #include "base/files/scoped_temp_dir.h" 7 #include "base/files/scoped_temp_dir.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/message_loop/message_loop.h" 9 #include "base/message_loop/message_loop.h"
10 #include "content/browser/browser_thread_impl.h" 10 #include "content/browser/browser_thread_impl.h"
11 #include "content/browser/service_worker/embedded_worker_registry.h" 11 #include "content/browser/service_worker/embedded_worker_registry.h"
12 #include "content/browser/service_worker/embedded_worker_test_helper.h" 12 #include "content/browser/service_worker/embedded_worker_test_helper.h"
13 #include "content/browser/service_worker/service_worker_context_core.h" 13 #include "content/browser/service_worker/service_worker_context_core.h"
14 #include "content/browser/service_worker/service_worker_registration.h" 14 #include "content/browser/service_worker/service_worker_registration.h"
15 #include "content/browser/service_worker/service_worker_storage.h" 15 #include "content/browser/service_worker/service_worker_storage.h"
16 #include "content/common/service_worker/embedded_worker_messages.h" 16 #include "content/common/service_worker/embedded_worker_messages.h"
17 #include "content/common/service_worker/service_worker_messages.h" 17 #include "content/common/service_worker/service_worker_messages.h"
18 #include "content/public/test/test_browser_thread_bundle.h" 18 #include "content/public/test/test_browser_thread_bundle.h"
19 #include "content/public/test/test_utils.h" 19 #include "content/public/test/test_utils.h"
20 #include "testing/gtest/include/gtest/gtest.h" 20 #include "testing/gtest/include/gtest/gtest.h"
21 21
22 namespace content { 22 namespace content {
23 23
24 namespace { 24 namespace {
25 25
26 void SaveResponseCallback(bool* called, 26 void SaveResponseCallback(bool* called,
27 int64* store_registration_id, 27 int64* store_registration_id,
28 int64* store_version_id,
29 ServiceWorkerStatusCode status, 28 ServiceWorkerStatusCode status,
30 int64 registration_id, 29 int64 registration_id) {
31 int64 version_id) {
32 EXPECT_EQ(SERVICE_WORKER_OK, status) << ServiceWorkerStatusToString(status); 30 EXPECT_EQ(SERVICE_WORKER_OK, status) << ServiceWorkerStatusToString(status);
33 *called = true; 31 *called = true;
34 *store_registration_id = registration_id; 32 *store_registration_id = registration_id;
35 *store_version_id = version_id;
36 } 33 }
37 34
38 ServiceWorkerContextCore::RegistrationCallback MakeRegisteredCallback( 35 ServiceWorkerContextCore::RegistrationCallback MakeRegisteredCallback(
39 bool* called, 36 bool* called,
40 int64* store_registration_id, 37 int64* store_registration_id) {
41 int64* store_version_id) { 38 return base::Bind(&SaveResponseCallback, called, store_registration_id);
42 return base::Bind(&SaveResponseCallback, called,
43 store_registration_id,
44 store_version_id);
45 } 39 }
46 40
47 void CallCompletedCallback(bool* called, ServiceWorkerStatusCode) { 41 void CallCompletedCallback(bool* called, ServiceWorkerStatusCode) {
48 *called = true; 42 *called = true;
49 } 43 }
50 44
51 ServiceWorkerContextCore::UnregistrationCallback MakeUnregisteredCallback( 45 ServiceWorkerContextCore::UnregistrationCallback MakeUnregisteredCallback(
52 bool* called) { 46 bool* called) {
53 return base::Bind(&CallCompletedCallback, called); 47 return base::Bind(&CallCompletedCallback, called);
54 } 48 }
55 49
56 void ExpectRegisteredWorkers( 50 void ExpectRegisteredWorkers(
57 ServiceWorkerStatusCode expect_status, 51 ServiceWorkerStatusCode expect_status,
58 int64 expect_version_id,
59 bool expect_waiting, 52 bool expect_waiting,
60 bool expect_active, 53 bool expect_active,
61 ServiceWorkerStatusCode status, 54 ServiceWorkerStatusCode status,
62 const scoped_refptr<ServiceWorkerRegistration>& registration) { 55 const scoped_refptr<ServiceWorkerRegistration>& registration) {
63 ASSERT_EQ(expect_status, status); 56 ASSERT_EQ(expect_status, status);
64 if (status != SERVICE_WORKER_OK) { 57 if (status != SERVICE_WORKER_OK) {
65 EXPECT_FALSE(registration.get()); 58 EXPECT_FALSE(registration.get());
66 return; 59 return;
67 } 60 }
68 61
69 if (expect_waiting) { 62 if (expect_waiting) {
70 EXPECT_TRUE(registration->waiting_version()); 63 EXPECT_TRUE(registration->waiting_version());
71 EXPECT_EQ(expect_version_id,
72 registration->waiting_version()->version_id());
73 } else { 64 } else {
74 EXPECT_FALSE(registration->waiting_version()); 65 EXPECT_FALSE(registration->waiting_version());
75 } 66 }
76 67
77 if (expect_active) { 68 if (expect_active) {
78 EXPECT_TRUE(registration->active_version()); 69 EXPECT_TRUE(registration->active_version());
79 EXPECT_EQ(expect_version_id,
80 registration->active_version()->version_id());
81 } else { 70 } else {
82 EXPECT_FALSE(registration->active_version()); 71 EXPECT_FALSE(registration->active_version());
83 } 72 }
84 } 73 }
85 74
86 class RejectInstallTestHelper : public EmbeddedWorkerTestHelper { 75 class RejectInstallTestHelper : public EmbeddedWorkerTestHelper {
87 public: 76 public:
88 explicit RejectInstallTestHelper(int mock_render_process_id) 77 explicit RejectInstallTestHelper(int mock_render_process_id)
89 : EmbeddedWorkerTestHelper(mock_render_process_id) {} 78 : EmbeddedWorkerTestHelper(mock_render_process_id) {}
90 79
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
132 121
133 protected: 122 protected:
134 TestBrowserThreadBundle browser_thread_bundle_; 123 TestBrowserThreadBundle browser_thread_bundle_;
135 scoped_ptr<EmbeddedWorkerTestHelper> helper_; 124 scoped_ptr<EmbeddedWorkerTestHelper> helper_;
136 const int render_process_id_; 125 const int render_process_id_;
137 }; 126 };
138 127
139 // Make sure basic registration is working. 128 // Make sure basic registration is working.
140 TEST_F(ServiceWorkerContextTest, Register) { 129 TEST_F(ServiceWorkerContextTest, Register) {
141 int64 registration_id = kInvalidServiceWorkerRegistrationId; 130 int64 registration_id = kInvalidServiceWorkerRegistrationId;
142 int64 version_id = kInvalidServiceWorkerVersionId;
143 bool called = false; 131 bool called = false;
144 context()->RegisterServiceWorker( 132 context()->RegisterServiceWorker(
145 GURL("http://www.example.com/"), 133 GURL("http://www.example.com/"),
146 GURL("http://www.example.com/service_worker.js"), 134 GURL("http://www.example.com/service_worker.js"),
147 NULL, 135 NULL,
148 MakeRegisteredCallback(&called, &registration_id, &version_id)); 136 MakeRegisteredCallback(&called, &registration_id));
149 137
150 ASSERT_FALSE(called); 138 ASSERT_FALSE(called);
151 base::RunLoop().RunUntilIdle(); 139 base::RunLoop().RunUntilIdle();
152 EXPECT_TRUE(called); 140 EXPECT_TRUE(called);
153 141
154 EXPECT_EQ(4UL, helper_->ipc_sink()->message_count()); 142 EXPECT_EQ(4UL, helper_->ipc_sink()->message_count());
155 EXPECT_TRUE(helper_->ipc_sink()->GetUniqueMessageMatching( 143 EXPECT_TRUE(helper_->ipc_sink()->GetUniqueMessageMatching(
156 EmbeddedWorkerMsg_StartWorker::ID)); 144 EmbeddedWorkerMsg_StartWorker::ID));
157 EXPECT_TRUE(helper_->inner_ipc_sink()->GetUniqueMessageMatching( 145 EXPECT_TRUE(helper_->inner_ipc_sink()->GetUniqueMessageMatching(
158 ServiceWorkerMsg_InstallEvent::ID)); 146 ServiceWorkerMsg_InstallEvent::ID));
159 EXPECT_TRUE(helper_->inner_ipc_sink()->GetUniqueMessageMatching( 147 EXPECT_TRUE(helper_->inner_ipc_sink()->GetUniqueMessageMatching(
160 ServiceWorkerMsg_ActivateEvent::ID)); 148 ServiceWorkerMsg_ActivateEvent::ID));
161 EXPECT_TRUE(helper_->ipc_sink()->GetUniqueMessageMatching( 149 EXPECT_TRUE(helper_->ipc_sink()->GetUniqueMessageMatching(
162 EmbeddedWorkerMsg_StopWorker::ID)); 150 EmbeddedWorkerMsg_StopWorker::ID));
163 EXPECT_NE(kInvalidServiceWorkerRegistrationId, registration_id); 151 EXPECT_NE(kInvalidServiceWorkerRegistrationId, registration_id);
164 EXPECT_NE(kInvalidServiceWorkerVersionId, version_id);
165 152
166 context()->storage()->FindRegistrationForId( 153 context()->storage()->FindRegistrationForId(
167 registration_id, 154 registration_id,
168 GURL("http://www.example.com"), 155 GURL("http://www.example.com"),
169 base::Bind(&ExpectRegisteredWorkers, 156 base::Bind(&ExpectRegisteredWorkers,
170 SERVICE_WORKER_OK, 157 SERVICE_WORKER_OK,
171 version_id,
172 false /* expect_waiting */, 158 false /* expect_waiting */,
173 true /* expect_active */)); 159 true /* expect_active */));
174 base::RunLoop().RunUntilIdle(); 160 base::RunLoop().RunUntilIdle();
175 } 161 }
176 162
177 // Test registration when the service worker rejects the install event. The 163 // Test registration when the service worker rejects the install event. The
178 // registration callback should indicate success, but there should be no waiting 164 // registration callback should indicate success, but there should be no waiting
179 // or active worker in the registration. 165 // or active worker in the registration.
180 TEST_F(ServiceWorkerContextTest, Register_RejectInstall) { 166 TEST_F(ServiceWorkerContextTest, Register_RejectInstall) {
181 helper_.reset(); // Make sure the process lookups stay overridden. 167 helper_.reset(); // Make sure the process lookups stay overridden.
182 helper_.reset(new RejectInstallTestHelper(render_process_id_)); 168 helper_.reset(new RejectInstallTestHelper(render_process_id_));
183 int64 registration_id = kInvalidServiceWorkerRegistrationId; 169 int64 registration_id = kInvalidServiceWorkerRegistrationId;
184 int64 version_id = kInvalidServiceWorkerVersionId;
185 bool called = false; 170 bool called = false;
186 context()->RegisterServiceWorker( 171 context()->RegisterServiceWorker(
187 GURL("http://www.example.com/"), 172 GURL("http://www.example.com/"),
188 GURL("http://www.example.com/service_worker.js"), 173 GURL("http://www.example.com/service_worker.js"),
189 NULL, 174 NULL,
190 MakeRegisteredCallback(&called, &registration_id, &version_id)); 175 MakeRegisteredCallback(&called, &registration_id));
191 176
192 ASSERT_FALSE(called); 177 ASSERT_FALSE(called);
193 base::RunLoop().RunUntilIdle(); 178 base::RunLoop().RunUntilIdle();
194 EXPECT_TRUE(called); 179 EXPECT_TRUE(called);
195 180
196 EXPECT_EQ(3UL, helper_->ipc_sink()->message_count()); 181 EXPECT_EQ(3UL, helper_->ipc_sink()->message_count());
197 EXPECT_TRUE(helper_->ipc_sink()->GetUniqueMessageMatching( 182 EXPECT_TRUE(helper_->ipc_sink()->GetUniqueMessageMatching(
198 EmbeddedWorkerMsg_StartWorker::ID)); 183 EmbeddedWorkerMsg_StartWorker::ID));
199 EXPECT_TRUE(helper_->inner_ipc_sink()->GetUniqueMessageMatching( 184 EXPECT_TRUE(helper_->inner_ipc_sink()->GetUniqueMessageMatching(
200 ServiceWorkerMsg_InstallEvent::ID)); 185 ServiceWorkerMsg_InstallEvent::ID));
201 EXPECT_FALSE(helper_->inner_ipc_sink()->GetUniqueMessageMatching( 186 EXPECT_FALSE(helper_->inner_ipc_sink()->GetUniqueMessageMatching(
202 ServiceWorkerMsg_ActivateEvent::ID)); 187 ServiceWorkerMsg_ActivateEvent::ID));
203 EXPECT_TRUE(helper_->ipc_sink()->GetUniqueMessageMatching( 188 EXPECT_TRUE(helper_->ipc_sink()->GetUniqueMessageMatching(
204 EmbeddedWorkerMsg_StopWorker::ID)); 189 EmbeddedWorkerMsg_StopWorker::ID));
205 EXPECT_NE(kInvalidServiceWorkerRegistrationId, registration_id); 190 EXPECT_NE(kInvalidServiceWorkerRegistrationId, registration_id);
206 EXPECT_NE(kInvalidServiceWorkerVersionId, version_id);
207 191
208 context()->storage()->FindRegistrationForId( 192 context()->storage()->FindRegistrationForId(
209 registration_id, 193 registration_id,
210 GURL("http://www.example.com"), 194 GURL("http://www.example.com"),
211 base::Bind(&ExpectRegisteredWorkers, 195 base::Bind(&ExpectRegisteredWorkers,
212 SERVICE_WORKER_ERROR_NOT_FOUND, 196 SERVICE_WORKER_ERROR_NOT_FOUND,
213 kInvalidServiceWorkerVersionId,
214 false /* expect_waiting */, 197 false /* expect_waiting */,
215 false /* expect_active */)); 198 false /* expect_active */));
216 base::RunLoop().RunUntilIdle(); 199 base::RunLoop().RunUntilIdle();
217 } 200 }
218 201
219 // Test registration when the service worker rejects the activate event. The 202 // Test registration when the service worker rejects the activate event. The
220 // registration callback should indicate success, but there should be no waiting 203 // registration callback should indicate success, but there should be no waiting
221 // or active worker in the registration. 204 // or active worker in the registration.
222 TEST_F(ServiceWorkerContextTest, Register_RejectActivate) { 205 TEST_F(ServiceWorkerContextTest, Register_RejectActivate) {
223 helper_.reset(); // Make sure the process lookups stay overridden. 206 helper_.reset(); // Make sure the process lookups stay overridden.
224 helper_.reset(new RejectActivateTestHelper(render_process_id_)); 207 helper_.reset(new RejectActivateTestHelper(render_process_id_));
225 int64 registration_id = kInvalidServiceWorkerRegistrationId; 208 int64 registration_id = kInvalidServiceWorkerRegistrationId;
226 int64 version_id = kInvalidServiceWorkerVersionId;
227 bool called = false; 209 bool called = false;
228 context()->RegisterServiceWorker( 210 context()->RegisterServiceWorker(
229 GURL("http://www.example.com/"), 211 GURL("http://www.example.com/"),
230 GURL("http://www.example.com/service_worker.js"), 212 GURL("http://www.example.com/service_worker.js"),
231 NULL, 213 NULL,
232 MakeRegisteredCallback(&called, &registration_id, &version_id)); 214 MakeRegisteredCallback(&called, &registration_id));
233 215
234 ASSERT_FALSE(called); 216 ASSERT_FALSE(called);
235 base::RunLoop().RunUntilIdle(); 217 base::RunLoop().RunUntilIdle();
236 EXPECT_TRUE(called); 218 EXPECT_TRUE(called);
237 219
238 EXPECT_EQ(4UL, helper_->ipc_sink()->message_count()); 220 EXPECT_EQ(4UL, helper_->ipc_sink()->message_count());
239 EXPECT_TRUE(helper_->ipc_sink()->GetUniqueMessageMatching( 221 EXPECT_TRUE(helper_->ipc_sink()->GetUniqueMessageMatching(
240 EmbeddedWorkerMsg_StartWorker::ID)); 222 EmbeddedWorkerMsg_StartWorker::ID));
241 EXPECT_TRUE(helper_->inner_ipc_sink()->GetUniqueMessageMatching( 223 EXPECT_TRUE(helper_->inner_ipc_sink()->GetUniqueMessageMatching(
242 ServiceWorkerMsg_InstallEvent::ID)); 224 ServiceWorkerMsg_InstallEvent::ID));
243 EXPECT_TRUE(helper_->inner_ipc_sink()->GetUniqueMessageMatching( 225 EXPECT_TRUE(helper_->inner_ipc_sink()->GetUniqueMessageMatching(
244 ServiceWorkerMsg_ActivateEvent::ID)); 226 ServiceWorkerMsg_ActivateEvent::ID));
245 EXPECT_TRUE(helper_->ipc_sink()->GetUniqueMessageMatching( 227 EXPECT_TRUE(helper_->ipc_sink()->GetUniqueMessageMatching(
246 EmbeddedWorkerMsg_StopWorker::ID)); 228 EmbeddedWorkerMsg_StopWorker::ID));
247 EXPECT_NE(kInvalidServiceWorkerRegistrationId, registration_id); 229 EXPECT_NE(kInvalidServiceWorkerRegistrationId, registration_id);
248 EXPECT_NE(kInvalidServiceWorkerVersionId, version_id);
249 230
250 context()->storage()->FindRegistrationForId( 231 context()->storage()->FindRegistrationForId(
251 registration_id, 232 registration_id,
252 GURL("http://www.example.com"), 233 GURL("http://www.example.com"),
253 base::Bind(&ExpectRegisteredWorkers, 234 base::Bind(&ExpectRegisteredWorkers,
254 SERVICE_WORKER_ERROR_NOT_FOUND, 235 SERVICE_WORKER_ERROR_NOT_FOUND,
255 kInvalidServiceWorkerVersionId,
256 false /* expect_waiting */, 236 false /* expect_waiting */,
257 false /* expect_active */)); 237 false /* expect_active */));
258 base::RunLoop().RunUntilIdle(); 238 base::RunLoop().RunUntilIdle();
259 } 239 }
260 240
261 // Make sure registrations are cleaned up when they are unregistered. 241 // Make sure registrations are cleaned up when they are unregistered.
262 TEST_F(ServiceWorkerContextTest, Unregister) { 242 TEST_F(ServiceWorkerContextTest, Unregister) {
263 GURL pattern("http://www.example.com/"); 243 GURL pattern("http://www.example.com/");
264 244
265 bool called = false; 245 bool called = false;
266 int64 registration_id = kInvalidServiceWorkerRegistrationId; 246 int64 registration_id = kInvalidServiceWorkerRegistrationId;
267 int64 version_id = kInvalidServiceWorkerVersionId;
268 context()->RegisterServiceWorker( 247 context()->RegisterServiceWorker(
269 pattern, 248 pattern,
270 GURL("http://www.example.com/service_worker.js"), 249 GURL("http://www.example.com/service_worker.js"),
271 NULL, 250 NULL,
272 MakeRegisteredCallback(&called, &registration_id, &version_id)); 251 MakeRegisteredCallback(&called, &registration_id));
273 252
274 ASSERT_FALSE(called); 253 ASSERT_FALSE(called);
275 base::RunLoop().RunUntilIdle(); 254 base::RunLoop().RunUntilIdle();
276 ASSERT_TRUE(called); 255 ASSERT_TRUE(called);
277 EXPECT_NE(kInvalidServiceWorkerRegistrationId, registration_id); 256 EXPECT_NE(kInvalidServiceWorkerRegistrationId, registration_id);
278 EXPECT_NE(kInvalidServiceWorkerVersionId, version_id);
279 257
280 called = false; 258 called = false;
281 context()->UnregisterServiceWorker(pattern, 259 context()->UnregisterServiceWorker(pattern,
282 MakeUnregisteredCallback(&called)); 260 MakeUnregisteredCallback(&called));
283 261
284 ASSERT_FALSE(called); 262 ASSERT_FALSE(called);
285 base::RunLoop().RunUntilIdle(); 263 base::RunLoop().RunUntilIdle();
286 ASSERT_TRUE(called); 264 ASSERT_TRUE(called);
287 265
288 context()->storage()->FindRegistrationForId( 266 context()->storage()->FindRegistrationForId(
289 registration_id, 267 registration_id,
290 pattern.GetOrigin(), 268 pattern.GetOrigin(),
291 base::Bind(&ExpectRegisteredWorkers, 269 base::Bind(&ExpectRegisteredWorkers,
292 SERVICE_WORKER_ERROR_NOT_FOUND, 270 SERVICE_WORKER_ERROR_NOT_FOUND,
293 kInvalidServiceWorkerVersionId,
294 false /* expect_waiting */, 271 false /* expect_waiting */,
295 false /* expect_active */)); 272 false /* expect_active */));
296 base::RunLoop().RunUntilIdle(); 273 base::RunLoop().RunUntilIdle();
297 } 274 }
298 275
299 // Make sure registering a new script creates a new version and shares an 276 // Make sure registering a new script shares an existing registration.
300 // existing registration.
301 TEST_F(ServiceWorkerContextTest, RegisterNewScript) { 277 TEST_F(ServiceWorkerContextTest, RegisterNewScript) {
302 GURL pattern("http://www.example.com/"); 278 GURL pattern("http://www.example.com/");
303 279
304 bool called = false; 280 bool called = false;
305 int64 old_registration_id = kInvalidServiceWorkerRegistrationId; 281 int64 old_registration_id = kInvalidServiceWorkerRegistrationId;
306 int64 old_version_id = kInvalidServiceWorkerVersionId;
307 context()->RegisterServiceWorker( 282 context()->RegisterServiceWorker(
308 pattern, 283 pattern,
309 GURL("http://www.example.com/service_worker.js"), 284 GURL("http://www.example.com/service_worker.js"),
310 NULL, 285 NULL,
311 MakeRegisteredCallback(&called, &old_registration_id, &old_version_id)); 286 MakeRegisteredCallback(&called, &old_registration_id));
312 287
313 ASSERT_FALSE(called); 288 ASSERT_FALSE(called);
314 base::RunLoop().RunUntilIdle(); 289 base::RunLoop().RunUntilIdle();
315 ASSERT_TRUE(called); 290 ASSERT_TRUE(called);
316 EXPECT_NE(kInvalidServiceWorkerRegistrationId, old_registration_id); 291 EXPECT_NE(kInvalidServiceWorkerRegistrationId, old_registration_id);
317 EXPECT_NE(kInvalidServiceWorkerVersionId, old_version_id);
318 292
319 called = false; 293 called = false;
320 int64 new_registration_id = kInvalidServiceWorkerRegistrationId; 294 int64 new_registration_id = kInvalidServiceWorkerRegistrationId;
321 int64 new_version_id = kInvalidServiceWorkerVersionId;
322 context()->RegisterServiceWorker( 295 context()->RegisterServiceWorker(
323 pattern, 296 pattern,
324 GURL("http://www.example.com/service_worker_new.js"), 297 GURL("http://www.example.com/service_worker_new.js"),
325 NULL, 298 NULL,
326 MakeRegisteredCallback(&called, &new_registration_id, &new_version_id)); 299 MakeRegisteredCallback(&called, &new_registration_id));
327 300
328 ASSERT_FALSE(called); 301 ASSERT_FALSE(called);
329 base::RunLoop().RunUntilIdle(); 302 base::RunLoop().RunUntilIdle();
330 ASSERT_TRUE(called); 303 ASSERT_TRUE(called);
331 304
332 EXPECT_NE(kInvalidServiceWorkerRegistrationId, new_registration_id); 305 EXPECT_NE(kInvalidServiceWorkerRegistrationId, new_registration_id);
333 EXPECT_NE(kInvalidServiceWorkerVersionId, new_version_id);
334 EXPECT_EQ(old_registration_id, new_registration_id); 306 EXPECT_EQ(old_registration_id, new_registration_id);
335 EXPECT_NE(old_version_id, new_version_id);
336 } 307 }
337 308
338 // Make sure that when registering a duplicate pattern+script_url 309 // Make sure that when registering a duplicate pattern+script_url
339 // combination, that the same registration is used. 310 // combination, that the same registration is used.
340 TEST_F(ServiceWorkerContextTest, RegisterDuplicateScript) { 311 TEST_F(ServiceWorkerContextTest, RegisterDuplicateScript) {
341 GURL pattern("http://www.example.com/"); 312 GURL pattern("http://www.example.com/");
342 GURL script_url("http://www.example.com/service_worker.js"); 313 GURL script_url("http://www.example.com/service_worker.js");
343 314
344 bool called = false; 315 bool called = false;
345 int64 old_registration_id = kInvalidServiceWorkerRegistrationId; 316 int64 old_registration_id = kInvalidServiceWorkerRegistrationId;
346 int64 old_version_id = kInvalidServiceWorkerVersionId;
347 context()->RegisterServiceWorker( 317 context()->RegisterServiceWorker(
348 pattern, 318 pattern,
349 script_url, 319 script_url,
350 NULL, 320 NULL,
351 MakeRegisteredCallback(&called, &old_registration_id, &old_version_id)); 321 MakeRegisteredCallback(&called, &old_registration_id));
352 322
353 ASSERT_FALSE(called); 323 ASSERT_FALSE(called);
354 base::RunLoop().RunUntilIdle(); 324 base::RunLoop().RunUntilIdle();
355 ASSERT_TRUE(called); 325 ASSERT_TRUE(called);
356 EXPECT_NE(kInvalidServiceWorkerRegistrationId, old_registration_id); 326 EXPECT_NE(kInvalidServiceWorkerRegistrationId, old_registration_id);
357 EXPECT_NE(kInvalidServiceWorkerVersionId, old_version_id);
358 327
359 called = false; 328 called = false;
360 int64 new_registration_id = kInvalidServiceWorkerRegistrationId; 329 int64 new_registration_id = kInvalidServiceWorkerRegistrationId;
361 int64 new_version_id = kInvalidServiceWorkerVersionId;
362 context()->RegisterServiceWorker( 330 context()->RegisterServiceWorker(
363 pattern, 331 pattern,
364 script_url, 332 script_url,
365 NULL, 333 NULL,
366 MakeRegisteredCallback(&called, &new_registration_id, &new_version_id)); 334 MakeRegisteredCallback(&called, &new_registration_id));
367 335
368 ASSERT_FALSE(called); 336 ASSERT_FALSE(called);
369 base::RunLoop().RunUntilIdle(); 337 base::RunLoop().RunUntilIdle();
370 ASSERT_TRUE(called); 338 ASSERT_TRUE(called);
371 EXPECT_EQ(old_registration_id, new_registration_id); 339 EXPECT_EQ(old_registration_id, new_registration_id);
372 EXPECT_EQ(old_version_id, new_version_id);
373 } 340 }
374 341
375 // TODO(nhiroki): Test this for on-disk storage. 342 // TODO(nhiroki): Test this for on-disk storage.
376 TEST_F(ServiceWorkerContextTest, DeleteAndStartOver) { 343 TEST_F(ServiceWorkerContextTest, DeleteAndStartOver) {
377 int64 registration_id = kInvalidServiceWorkerRegistrationId; 344 int64 registration_id = kInvalidServiceWorkerRegistrationId;
378 int64 version_id = kInvalidServiceWorkerVersionId;
379 bool called = false; 345 bool called = false;
380 context()->RegisterServiceWorker( 346 context()->RegisterServiceWorker(
381 GURL("http://www.example.com/"), 347 GURL("http://www.example.com/"),
382 GURL("http://www.example.com/service_worker.js"), 348 GURL("http://www.example.com/service_worker.js"),
383 NULL, 349 NULL,
384 MakeRegisteredCallback(&called, &registration_id, &version_id)); 350 MakeRegisteredCallback(&called, &registration_id));
385 351
386 ASSERT_FALSE(called); 352 ASSERT_FALSE(called);
387 base::RunLoop().RunUntilIdle(); 353 base::RunLoop().RunUntilIdle();
388 EXPECT_TRUE(called); 354 EXPECT_TRUE(called);
389 355
390 context()->storage()->FindRegistrationForId( 356 context()->storage()->FindRegistrationForId(
391 registration_id, 357 registration_id,
392 GURL("http://www.example.com"), 358 GURL("http://www.example.com"),
393 base::Bind(&ExpectRegisteredWorkers, 359 base::Bind(&ExpectRegisteredWorkers,
394 SERVICE_WORKER_OK, 360 SERVICE_WORKER_OK,
395 version_id,
396 false /* expect_waiting */, 361 false /* expect_waiting */,
397 true /* expect_active */)); 362 true /* expect_active */));
398 base::RunLoop().RunUntilIdle(); 363 base::RunLoop().RunUntilIdle();
399 364
400 context()->ScheduleDeleteAndStartOver(); 365 context()->ScheduleDeleteAndStartOver();
401 366
402 // The storage is disabled while the recovery process is running, so the 367 // The storage is disabled while the recovery process is running, so the
403 // operation should be failed. 368 // operation should be failed.
404 context()->storage()->FindRegistrationForId( 369 context()->storage()->FindRegistrationForId(
405 registration_id, 370 registration_id,
406 GURL("http://www.example.com"), 371 GURL("http://www.example.com"),
407 base::Bind(&ExpectRegisteredWorkers, 372 base::Bind(&ExpectRegisteredWorkers,
408 SERVICE_WORKER_ERROR_FAILED, 373 SERVICE_WORKER_ERROR_FAILED,
409 version_id,
410 false /* expect_waiting */, 374 false /* expect_waiting */,
411 true /* expect_active */)); 375 true /* expect_active */));
412 base::RunLoop().RunUntilIdle(); 376 base::RunLoop().RunUntilIdle();
413 377
414 // The context started over and the storage was re-initialized, so the 378 // The context started over and the storage was re-initialized, so the
415 // registration should not be found. 379 // registration should not be found.
416 context()->storage()->FindRegistrationForId( 380 context()->storage()->FindRegistrationForId(
417 registration_id, 381 registration_id,
418 GURL("http://www.example.com"), 382 GURL("http://www.example.com"),
419 base::Bind(&ExpectRegisteredWorkers, 383 base::Bind(&ExpectRegisteredWorkers,
420 SERVICE_WORKER_ERROR_NOT_FOUND, 384 SERVICE_WORKER_ERROR_NOT_FOUND,
421 version_id,
422 false /* expect_waiting */, 385 false /* expect_waiting */,
423 true /* expect_active */)); 386 true /* expect_active */));
424 base::RunLoop().RunUntilIdle(); 387 base::RunLoop().RunUntilIdle();
425 388
426 called = false; 389 called = false;
427 context()->RegisterServiceWorker( 390 context()->RegisterServiceWorker(
428 GURL("http://www.example.com/"), 391 GURL("http://www.example.com/"),
429 GURL("http://www.example.com/service_worker.js"), 392 GURL("http://www.example.com/service_worker.js"),
430 NULL, 393 NULL,
431 MakeRegisteredCallback(&called, &registration_id, &version_id)); 394 MakeRegisteredCallback(&called, &registration_id));
432 395
433 ASSERT_FALSE(called); 396 ASSERT_FALSE(called);
434 base::RunLoop().RunUntilIdle(); 397 base::RunLoop().RunUntilIdle();
435 EXPECT_TRUE(called); 398 EXPECT_TRUE(called);
436 399
437 context()->storage()->FindRegistrationForId( 400 context()->storage()->FindRegistrationForId(
438 registration_id, 401 registration_id,
439 GURL("http://www.example.com"), 402 GURL("http://www.example.com"),
440 base::Bind(&ExpectRegisteredWorkers, 403 base::Bind(&ExpectRegisteredWorkers,
441 SERVICE_WORKER_OK, 404 SERVICE_WORKER_OK,
442 version_id,
443 false /* expect_waiting */, 405 false /* expect_waiting */,
444 true /* expect_active */)); 406 true /* expect_active */));
445 base::RunLoop().RunUntilIdle(); 407 base::RunLoop().RunUntilIdle();
446 } 408 }
447 409
448 } // namespace content 410 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698