Rename isSystemLocationEnabled to isLocationEnabled, as per internal review (185995).
[chromium-blink-merge.git] / content / browser / service_worker / service_worker_storage_unittest.cc
blob4a24444af1146c277812b5f48242a6be13271a58
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 <string>
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/browser_thread_impl.h"
12 #include "content/browser/service_worker/service_worker_context_core.h"
13 #include "content/browser/service_worker/service_worker_disk_cache.h"
14 #include "content/browser/service_worker/service_worker_registration.h"
15 #include "content/browser/service_worker/service_worker_storage.h"
16 #include "content/browser/service_worker/service_worker_utils.h"
17 #include "content/browser/service_worker/service_worker_version.h"
18 #include "content/common/service_worker/service_worker_status_code.h"
19 #include "content/public/test/test_browser_thread_bundle.h"
20 #include "ipc/ipc_message.h"
21 #include "net/base/io_buffer.h"
22 #include "net/base/net_errors.h"
23 #include "net/base/test_completion_callback.h"
24 #include "net/http/http_response_headers.h"
25 #include "testing/gtest/include/gtest/gtest.h"
27 using net::IOBuffer;
28 using net::TestCompletionCallback;
29 using net::WrappedIOBuffer;
31 namespace content {
33 namespace {
35 typedef ServiceWorkerDatabase::RegistrationData RegistrationData;
36 typedef ServiceWorkerDatabase::ResourceRecord ResourceRecord;
38 void StatusCallback(bool* was_called,
39 ServiceWorkerStatusCode* result,
40 ServiceWorkerStatusCode status) {
41 *was_called = true;
42 *result = status;
45 ServiceWorkerStorage::StatusCallback MakeStatusCallback(
46 bool* was_called,
47 ServiceWorkerStatusCode* result) {
48 return base::Bind(&StatusCallback, was_called, result);
51 void FindCallback(
52 bool* was_called,
53 ServiceWorkerStatusCode* result,
54 scoped_refptr<ServiceWorkerRegistration>* found,
55 ServiceWorkerStatusCode status,
56 const scoped_refptr<ServiceWorkerRegistration>& registration) {
57 *was_called = true;
58 *result = status;
59 *found = registration;
62 ServiceWorkerStorage::FindRegistrationCallback MakeFindCallback(
63 bool* was_called,
64 ServiceWorkerStatusCode* result,
65 scoped_refptr<ServiceWorkerRegistration>* found) {
66 return base::Bind(&FindCallback, was_called, result, found);
69 void GetAllCallback(
70 bool* was_called,
71 std::vector<ServiceWorkerRegistrationInfo>* all_out,
72 const std::vector<ServiceWorkerRegistrationInfo>& all) {
73 *was_called = true;
74 *all_out = all;
77 ServiceWorkerStorage::GetAllRegistrationInfosCallback MakeGetAllCallback(
78 bool* was_called,
79 std::vector<ServiceWorkerRegistrationInfo>* all) {
80 return base::Bind(&GetAllCallback, was_called, all);
83 void GetUserDataCallback(
84 bool* was_called,
85 std::string* data_out,
86 ServiceWorkerStatusCode* status_out,
87 const std::string& data,
88 ServiceWorkerStatusCode status) {
89 *was_called = true;
90 *data_out = data;
91 *status_out = status;
94 void OnCompareComplete(
95 ServiceWorkerStatusCode* status_out, bool* are_equal_out,
96 ServiceWorkerStatusCode status, bool are_equal) {
97 *status_out = status;
98 *are_equal_out = are_equal;
101 void WriteResponse(
102 ServiceWorkerStorage* storage, int64 id,
103 const std::string& headers,
104 IOBuffer* body, int length) {
105 scoped_ptr<ServiceWorkerResponseWriter> writer =
106 storage->CreateResponseWriter(id);
108 scoped_ptr<net::HttpResponseInfo> info(new net::HttpResponseInfo);
109 info->request_time = base::Time::Now();
110 info->response_time = base::Time::Now();
111 info->was_cached = false;
112 info->headers = new net::HttpResponseHeaders(headers);
113 scoped_refptr<HttpResponseInfoIOBuffer> info_buffer =
114 new HttpResponseInfoIOBuffer(info.release());
116 TestCompletionCallback cb;
117 writer->WriteInfo(info_buffer.get(), cb.callback());
118 int rv = cb.WaitForResult();
119 EXPECT_LT(0, rv);
122 TestCompletionCallback cb;
123 writer->WriteData(body, length, cb.callback());
124 int rv = cb.WaitForResult();
125 EXPECT_EQ(length, rv);
129 void WriteStringResponse(
130 ServiceWorkerStorage* storage, int64 id,
131 const std::string& headers,
132 const std::string& body) {
133 scoped_refptr<IOBuffer> body_buffer(new WrappedIOBuffer(body.data()));
134 WriteResponse(storage, id, headers, body_buffer.get(), body.length());
137 void WriteBasicResponse(ServiceWorkerStorage* storage, int64 id) {
138 scoped_ptr<ServiceWorkerResponseWriter> writer =
139 storage->CreateResponseWriter(id);
141 const char kHttpHeaders[] = "HTTP/1.0 200 HONKYDORY\0Content-Length: 5\0\0";
142 const char kHttpBody[] = "Hello";
143 std::string headers(kHttpHeaders, arraysize(kHttpHeaders));
144 WriteStringResponse(storage, id, headers, std::string(kHttpBody));
147 bool VerifyBasicResponse(ServiceWorkerStorage* storage, int64 id,
148 bool expected_positive_result) {
149 const std::string kExpectedHttpBody("Hello");
150 scoped_ptr<ServiceWorkerResponseReader> reader =
151 storage->CreateResponseReader(id);
152 scoped_refptr<HttpResponseInfoIOBuffer> info_buffer =
153 new HttpResponseInfoIOBuffer();
155 TestCompletionCallback cb;
156 reader->ReadInfo(info_buffer.get(), cb.callback());
157 int rv = cb.WaitForResult();
158 if (expected_positive_result)
159 EXPECT_LT(0, rv);
160 if (rv <= 0)
161 return false;
164 std::string received_body;
166 const int kBigEnough = 512;
167 scoped_refptr<net::IOBuffer> buffer = new IOBuffer(kBigEnough);
168 TestCompletionCallback cb;
169 reader->ReadData(buffer.get(), kBigEnough, cb.callback());
170 int rv = cb.WaitForResult();
171 EXPECT_EQ(static_cast<int>(kExpectedHttpBody.size()), rv);
172 if (rv <= 0)
173 return false;
174 received_body.assign(buffer->data(), rv);
177 bool status_match =
178 std::string("HONKYDORY") ==
179 info_buffer->http_info->headers->GetStatusText();
180 bool data_match = kExpectedHttpBody == received_body;
182 EXPECT_TRUE(status_match);
183 EXPECT_TRUE(data_match);
184 return status_match && data_match;
187 void WriteResponseOfSize(ServiceWorkerStorage* storage, int64 id,
188 char val, int size) {
189 const char kHttpHeaders[] = "HTTP/1.0 200 HONKYDORY\00";
190 std::string headers(kHttpHeaders, arraysize(kHttpHeaders));
191 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(size);
192 memset(buffer->data(), val, size);
193 WriteResponse(storage, id, headers, buffer.get(), size);
196 } // namespace
198 class ServiceWorkerStorageTest : public testing::Test {
199 public:
200 ServiceWorkerStorageTest()
201 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {
204 void SetUp() override {
205 scoped_ptr<ServiceWorkerDatabaseTaskManager> database_task_manager(
206 new MockServiceWorkerDatabaseTaskManager(
207 base::ThreadTaskRunnerHandle::Get()));
208 context_.reset(
209 new ServiceWorkerContextCore(GetUserDataDirectory(),
210 base::ThreadTaskRunnerHandle::Get(),
211 database_task_manager.Pass(),
212 base::ThreadTaskRunnerHandle::Get(),
213 NULL,
214 NULL,
215 NULL,
216 NULL));
217 context_ptr_ = context_->AsWeakPtr();
220 void TearDown() override { context_.reset(); }
222 virtual base::FilePath GetUserDataDirectory() { return base::FilePath(); }
224 ServiceWorkerStorage* storage() { return context_->storage(); }
226 // A static class method for friendliness.
227 static void VerifyPurgeableListStatusCallback(
228 ServiceWorkerDatabase* database,
229 std::set<int64> *purgeable_ids,
230 bool* was_called,
231 ServiceWorkerStatusCode* result,
232 ServiceWorkerStatusCode status) {
233 *was_called = true;
234 *result = status;
235 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK,
236 database->GetPurgeableResourceIds(purgeable_ids));
239 protected:
240 void LazyInitialize() {
241 storage()->LazyInitialize(base::Bind(&base::DoNothing));
242 base::RunLoop().RunUntilIdle();
245 ServiceWorkerStatusCode StoreRegistration(
246 scoped_refptr<ServiceWorkerRegistration> registration,
247 scoped_refptr<ServiceWorkerVersion> version) {
248 bool was_called = false;
249 ServiceWorkerStatusCode result = SERVICE_WORKER_ERROR_FAILED;
250 storage()->StoreRegistration(registration.get(),
251 version.get(),
252 MakeStatusCallback(&was_called, &result));
253 EXPECT_FALSE(was_called); // always async
254 base::RunLoop().RunUntilIdle();
255 EXPECT_TRUE(was_called);
256 return result;
259 ServiceWorkerStatusCode DeleteRegistration(
260 int64 registration_id,
261 const GURL& origin) {
262 bool was_called = false;
263 ServiceWorkerStatusCode result = SERVICE_WORKER_ERROR_FAILED;
264 storage()->DeleteRegistration(
265 registration_id, origin, MakeStatusCallback(&was_called, &result));
266 EXPECT_FALSE(was_called); // always async
267 base::RunLoop().RunUntilIdle();
268 EXPECT_TRUE(was_called);
269 return result;
272 void GetAllRegistrations(
273 std::vector<ServiceWorkerRegistrationInfo>* registrations) {
274 bool was_called = false;
275 storage()->GetAllRegistrations(
276 MakeGetAllCallback(&was_called, registrations));
277 EXPECT_FALSE(was_called); // always async
278 base::RunLoop().RunUntilIdle();
279 EXPECT_TRUE(was_called);
282 ServiceWorkerStatusCode GetUserData(
283 int64 registration_id,
284 const std::string& key,
285 std::string* data) {
286 bool was_called = false;
287 ServiceWorkerStatusCode result = SERVICE_WORKER_ERROR_FAILED;
288 storage()->GetUserData(
289 registration_id, key,
290 base::Bind(&GetUserDataCallback, &was_called, data, &result));
291 EXPECT_FALSE(was_called); // always async
292 base::RunLoop().RunUntilIdle();
293 EXPECT_TRUE(was_called);
294 return result;
297 ServiceWorkerStatusCode StoreUserData(
298 int64 registration_id,
299 const GURL& origin,
300 const std::string& key,
301 const std::string& data) {
302 bool was_called = false;
303 ServiceWorkerStatusCode result = SERVICE_WORKER_ERROR_FAILED;
304 storage()->StoreUserData(
305 registration_id, origin, key, data,
306 MakeStatusCallback(&was_called, &result));
307 EXPECT_FALSE(was_called); // always async
308 base::RunLoop().RunUntilIdle();
309 EXPECT_TRUE(was_called);
310 return result;
313 ServiceWorkerStatusCode ClearUserData(
314 int64 registration_id,
315 const std::string& key) {
316 bool was_called = false;
317 ServiceWorkerStatusCode result = SERVICE_WORKER_ERROR_FAILED;
318 storage()->ClearUserData(
319 registration_id, key, MakeStatusCallback(&was_called, &result));
320 EXPECT_FALSE(was_called); // always async
321 base::RunLoop().RunUntilIdle();
322 EXPECT_TRUE(was_called);
323 return result;
326 ServiceWorkerStatusCode UpdateToActiveState(
327 scoped_refptr<ServiceWorkerRegistration> registration) {
328 bool was_called = false;
329 ServiceWorkerStatusCode result = SERVICE_WORKER_ERROR_FAILED;
330 storage()->UpdateToActiveState(registration.get(),
331 MakeStatusCallback(&was_called, &result));
332 EXPECT_FALSE(was_called); // always async
333 base::RunLoop().RunUntilIdle();
334 EXPECT_TRUE(was_called);
335 return result;
338 void UpdateLastUpdateCheckTime(ServiceWorkerRegistration* registration) {
339 storage()->UpdateLastUpdateCheckTime(registration);
340 base::RunLoop().RunUntilIdle();
343 ServiceWorkerStatusCode FindRegistrationForDocument(
344 const GURL& document_url,
345 scoped_refptr<ServiceWorkerRegistration>* registration) {
346 bool was_called = false;
347 ServiceWorkerStatusCode result = SERVICE_WORKER_ERROR_FAILED;
348 storage()->FindRegistrationForDocument(
349 document_url, MakeFindCallback(&was_called, &result, registration));
350 base::RunLoop().RunUntilIdle();
351 EXPECT_TRUE(was_called);
352 return result;
355 ServiceWorkerStatusCode FindRegistrationForPattern(
356 const GURL& scope,
357 scoped_refptr<ServiceWorkerRegistration>* registration) {
358 bool was_called = false;
359 ServiceWorkerStatusCode result = SERVICE_WORKER_ERROR_FAILED;
360 storage()->FindRegistrationForPattern(
361 scope, MakeFindCallback(&was_called, &result, registration));
362 EXPECT_FALSE(was_called); // always async
363 base::RunLoop().RunUntilIdle();
364 EXPECT_TRUE(was_called);
365 return result;
368 ServiceWorkerStatusCode FindRegistrationForId(
369 int64 registration_id,
370 const GURL& origin,
371 scoped_refptr<ServiceWorkerRegistration>* registration) {
372 bool was_called = false;
373 ServiceWorkerStatusCode result = SERVICE_WORKER_ERROR_FAILED;
374 storage()->FindRegistrationForId(
375 registration_id, origin,
376 MakeFindCallback(&was_called, &result, registration));
377 base::RunLoop().RunUntilIdle();
378 EXPECT_TRUE(was_called);
379 return result;
382 scoped_ptr<ServiceWorkerContextCore> context_;
383 base::WeakPtr<ServiceWorkerContextCore> context_ptr_;
384 TestBrowserThreadBundle browser_thread_bundle_;
387 TEST_F(ServiceWorkerStorageTest, StoreFindUpdateDeleteRegistration) {
388 const GURL kScope("http://www.test.not/scope/");
389 const GURL kScript("http://www.test.not/script.js");
390 const GURL kDocumentUrl("http://www.test.not/scope/document.html");
391 const GURL kResource1("http://www.test.not/scope/resource1.js");
392 const int64 kResource1Size = 1591234;
393 const GURL kResource2("http://www.test.not/scope/resource2.js");
394 const int64 kResource2Size = 51;
395 const int64 kRegistrationId = 0;
396 const int64 kVersionId = 0;
397 const base::Time kToday = base::Time::Now();
398 const base::Time kYesterday = kToday - base::TimeDelta::FromDays(1);
400 scoped_refptr<ServiceWorkerRegistration> found_registration;
402 // We shouldn't find anything without having stored anything.
403 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND,
404 FindRegistrationForDocument(kDocumentUrl, &found_registration));
405 EXPECT_FALSE(found_registration.get());
407 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND,
408 FindRegistrationForPattern(kScope, &found_registration));
409 EXPECT_FALSE(found_registration.get());
411 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND,
412 FindRegistrationForId(
413 kRegistrationId, kScope.GetOrigin(), &found_registration));
414 EXPECT_FALSE(found_registration.get());
416 std::vector<ServiceWorkerDatabase::ResourceRecord> resources;
417 resources.push_back(
418 ServiceWorkerDatabase::ResourceRecord(1, kResource1, kResource1Size));
419 resources.push_back(
420 ServiceWorkerDatabase::ResourceRecord(2, kResource2, kResource2Size));
422 // Store something.
423 scoped_refptr<ServiceWorkerRegistration> live_registration =
424 new ServiceWorkerRegistration(
425 kScope, kRegistrationId, context_ptr_);
426 scoped_refptr<ServiceWorkerVersion> live_version =
427 new ServiceWorkerVersion(
428 live_registration.get(), kScript, kVersionId, context_ptr_);
429 live_version->SetStatus(ServiceWorkerVersion::INSTALLED);
430 live_version->script_cache_map()->SetResources(resources);
431 live_registration->SetWaitingVersion(live_version.get());
432 live_registration->set_last_update_check(kYesterday);
433 EXPECT_EQ(SERVICE_WORKER_OK,
434 StoreRegistration(live_registration, live_version));
436 // Now we should find it and get the live ptr back immediately.
437 EXPECT_EQ(SERVICE_WORKER_OK,
438 FindRegistrationForDocument(kDocumentUrl, &found_registration));
439 EXPECT_EQ(live_registration, found_registration);
440 EXPECT_EQ(kResource1Size + kResource2Size,
441 live_registration->resources_total_size_bytes());
442 EXPECT_EQ(kResource1Size + kResource2Size,
443 found_registration->resources_total_size_bytes());
444 found_registration = NULL;
446 // But FindRegistrationForPattern is always async.
447 EXPECT_EQ(SERVICE_WORKER_OK,
448 FindRegistrationForPattern(kScope, &found_registration));
449 EXPECT_EQ(live_registration, found_registration);
450 found_registration = NULL;
452 // Can be found by id too.
453 EXPECT_EQ(SERVICE_WORKER_OK,
454 FindRegistrationForId(
455 kRegistrationId, kScope.GetOrigin(), &found_registration));
456 ASSERT_TRUE(found_registration.get());
457 EXPECT_EQ(kRegistrationId, found_registration->id());
458 EXPECT_EQ(live_registration, found_registration);
459 found_registration = NULL;
461 // Drop the live registration, but keep the version live.
462 live_registration = NULL;
464 // Now FindRegistrationForDocument should be async.
465 EXPECT_EQ(SERVICE_WORKER_OK,
466 FindRegistrationForDocument(kDocumentUrl, &found_registration));
467 ASSERT_TRUE(found_registration.get());
468 EXPECT_EQ(kRegistrationId, found_registration->id());
469 EXPECT_TRUE(found_registration->HasOneRef());
471 // Check that sizes are populated correctly
472 EXPECT_EQ(live_version.get(), found_registration->waiting_version());
473 EXPECT_EQ(kResource1Size + kResource2Size,
474 found_registration->resources_total_size_bytes());
475 std::vector<ServiceWorkerRegistrationInfo> all_registrations;
476 GetAllRegistrations(&all_registrations);
477 EXPECT_EQ(1u, all_registrations.size());
478 ServiceWorkerRegistrationInfo info = all_registrations[0];
479 EXPECT_EQ(kResource1Size + kResource2Size, info.stored_version_size_bytes);
480 all_registrations.clear();
482 found_registration = NULL;
484 // Drop the live version too.
485 live_version = NULL;
487 // And FindRegistrationForPattern is always async.
488 EXPECT_EQ(SERVICE_WORKER_OK,
489 FindRegistrationForPattern(kScope, &found_registration));
490 ASSERT_TRUE(found_registration.get());
491 EXPECT_EQ(kRegistrationId, found_registration->id());
492 EXPECT_TRUE(found_registration->HasOneRef());
493 EXPECT_FALSE(found_registration->active_version());
494 ASSERT_TRUE(found_registration->waiting_version());
495 EXPECT_EQ(kYesterday, found_registration->last_update_check());
496 EXPECT_EQ(ServiceWorkerVersion::INSTALLED,
497 found_registration->waiting_version()->status());
499 // Update to active and update the last check time.
500 scoped_refptr<ServiceWorkerVersion> temp_version =
501 found_registration->waiting_version();
502 temp_version->SetStatus(ServiceWorkerVersion::ACTIVATED);
503 found_registration->SetActiveVersion(temp_version.get());
504 temp_version = NULL;
505 EXPECT_EQ(SERVICE_WORKER_OK, UpdateToActiveState(found_registration));
506 found_registration->set_last_update_check(kToday);
507 UpdateLastUpdateCheckTime(found_registration.get());
509 found_registration = NULL;
511 // Trying to update a unstored registration to active should fail.
512 scoped_refptr<ServiceWorkerRegistration> unstored_registration =
513 new ServiceWorkerRegistration(
514 kScope, kRegistrationId + 1, context_ptr_);
515 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND,
516 UpdateToActiveState(unstored_registration));
517 unstored_registration = NULL;
519 // The Find methods should return a registration with an active version
520 // and the expected update time.
521 EXPECT_EQ(SERVICE_WORKER_OK,
522 FindRegistrationForDocument(kDocumentUrl, &found_registration));
523 ASSERT_TRUE(found_registration.get());
524 EXPECT_EQ(kRegistrationId, found_registration->id());
525 EXPECT_TRUE(found_registration->HasOneRef());
526 EXPECT_FALSE(found_registration->waiting_version());
527 ASSERT_TRUE(found_registration->active_version());
528 EXPECT_EQ(ServiceWorkerVersion::ACTIVATED,
529 found_registration->active_version()->status());
530 EXPECT_EQ(kToday, found_registration->last_update_check());
532 // Delete from storage but with a instance still live.
533 EXPECT_TRUE(context_->GetLiveVersion(kRegistrationId));
534 EXPECT_EQ(SERVICE_WORKER_OK,
535 DeleteRegistration(kRegistrationId, kScope.GetOrigin()));
536 EXPECT_TRUE(context_->GetLiveVersion(kRegistrationId));
538 // Should no longer be found.
539 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND,
540 FindRegistrationForId(
541 kRegistrationId, kScope.GetOrigin(), &found_registration));
542 EXPECT_FALSE(found_registration.get());
544 // Deleting an unstored registration should succeed.
545 EXPECT_EQ(SERVICE_WORKER_OK,
546 DeleteRegistration(kRegistrationId + 1, kScope.GetOrigin()));
549 TEST_F(ServiceWorkerStorageTest, InstallingRegistrationsAreFindable) {
550 const GURL kScope("http://www.test.not/scope/");
551 const GURL kScript("http://www.test.not/script.js");
552 const GURL kDocumentUrl("http://www.test.not/scope/document.html");
553 const int64 kRegistrationId = 0;
554 const int64 kVersionId = 0;
556 scoped_refptr<ServiceWorkerRegistration> found_registration;
558 // Create an unstored registration.
559 scoped_refptr<ServiceWorkerRegistration> live_registration =
560 new ServiceWorkerRegistration(
561 kScope, kRegistrationId, context_ptr_);
562 scoped_refptr<ServiceWorkerVersion> live_version =
563 new ServiceWorkerVersion(
564 live_registration.get(), kScript, kVersionId, context_ptr_);
565 live_version->SetStatus(ServiceWorkerVersion::INSTALLING);
566 live_registration->SetWaitingVersion(live_version.get());
568 // Should not be findable, including by GetAllRegistrations.
569 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND,
570 FindRegistrationForId(
571 kRegistrationId, kScope.GetOrigin(), &found_registration));
572 EXPECT_FALSE(found_registration.get());
574 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND,
575 FindRegistrationForDocument(kDocumentUrl, &found_registration));
576 EXPECT_FALSE(found_registration.get());
578 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND,
579 FindRegistrationForPattern(kScope, &found_registration));
580 EXPECT_FALSE(found_registration.get());
582 std::vector<ServiceWorkerRegistrationInfo> all_registrations;
583 GetAllRegistrations(&all_registrations);
584 EXPECT_TRUE(all_registrations.empty());
586 // Notify storage of it being installed.
587 storage()->NotifyInstallingRegistration(live_registration.get());
589 // Now should be findable.
590 EXPECT_EQ(SERVICE_WORKER_OK,
591 FindRegistrationForId(
592 kRegistrationId, kScope.GetOrigin(), &found_registration));
593 EXPECT_EQ(live_registration, found_registration);
594 found_registration = NULL;
596 EXPECT_EQ(SERVICE_WORKER_OK,
597 FindRegistrationForDocument(kDocumentUrl, &found_registration));
598 EXPECT_EQ(live_registration, found_registration);
599 found_registration = NULL;
601 EXPECT_EQ(SERVICE_WORKER_OK,
602 FindRegistrationForPattern(kScope, &found_registration));
603 EXPECT_EQ(live_registration, found_registration);
604 found_registration = NULL;
606 GetAllRegistrations(&all_registrations);
607 EXPECT_EQ(1u, all_registrations.size());
608 all_registrations.clear();
610 // Notify storage of installation no longer happening.
611 storage()->NotifyDoneInstallingRegistration(
612 live_registration.get(), NULL, SERVICE_WORKER_OK);
614 // Once again, should not be findable.
615 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND,
616 FindRegistrationForId(
617 kRegistrationId, kScope.GetOrigin(), &found_registration));
618 EXPECT_FALSE(found_registration.get());
620 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND,
621 FindRegistrationForDocument(kDocumentUrl, &found_registration));
622 EXPECT_FALSE(found_registration.get());
624 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND,
625 FindRegistrationForPattern(kScope, &found_registration));
626 EXPECT_FALSE(found_registration.get());
628 GetAllRegistrations(&all_registrations);
629 EXPECT_TRUE(all_registrations.empty());
632 TEST_F(ServiceWorkerStorageTest, StoreUserData) {
633 const GURL kScope("http://www.test.not/scope/");
634 const GURL kScript("http://www.test.not/script.js");
635 const int64 kRegistrationId = 0;
636 const int64 kVersionId = 0;
638 LazyInitialize();
640 // Store a registration.
641 scoped_refptr<ServiceWorkerRegistration> live_registration =
642 new ServiceWorkerRegistration(
643 kScope, kRegistrationId, context_ptr_);
644 scoped_refptr<ServiceWorkerVersion> live_version =
645 new ServiceWorkerVersion(
646 live_registration.get(), kScript, kVersionId, context_ptr_);
647 live_version->SetStatus(ServiceWorkerVersion::INSTALLED);
648 live_registration->SetWaitingVersion(live_version.get());
649 EXPECT_EQ(SERVICE_WORKER_OK,
650 StoreRegistration(live_registration, live_version));
652 // Store user data associated with the registration.
653 std::string data_out;
654 EXPECT_EQ(SERVICE_WORKER_OK,
655 StoreUserData(kRegistrationId, kScope.GetOrigin(), "key", "data"));
656 EXPECT_EQ(SERVICE_WORKER_OK, GetUserData(kRegistrationId, "key", &data_out));
657 EXPECT_EQ("data", data_out);
658 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND,
659 GetUserData(kRegistrationId, "unknown_key", &data_out));
660 EXPECT_EQ(SERVICE_WORKER_OK, ClearUserData(kRegistrationId, "key"));
661 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND,
662 GetUserData(kRegistrationId, "key", &data_out));
664 // User data should be deleted when the associated registration is deleted.
665 ASSERT_EQ(SERVICE_WORKER_OK,
666 StoreUserData(kRegistrationId, kScope.GetOrigin(), "key", "data"));
667 ASSERT_EQ(SERVICE_WORKER_OK,
668 GetUserData(kRegistrationId, "key", &data_out));
669 ASSERT_EQ("data", data_out);
671 EXPECT_EQ(SERVICE_WORKER_OK,
672 DeleteRegistration(kRegistrationId, kScope.GetOrigin()));
673 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND,
674 GetUserData(kRegistrationId, "key", &data_out));
676 // Data access with an invalid registration id should be failed.
677 EXPECT_EQ(SERVICE_WORKER_ERROR_FAILED,
678 StoreUserData(kInvalidServiceWorkerRegistrationId,
679 kScope.GetOrigin(), "key", "data"));
680 EXPECT_EQ(SERVICE_WORKER_ERROR_FAILED,
681 GetUserData(kInvalidServiceWorkerRegistrationId, "key", &data_out));
682 EXPECT_EQ(SERVICE_WORKER_ERROR_FAILED,
683 ClearUserData(kInvalidServiceWorkerRegistrationId, "key"));
685 // Data access with an empty key should be failed.
686 EXPECT_EQ(SERVICE_WORKER_ERROR_FAILED,
687 StoreUserData(
688 kRegistrationId, kScope.GetOrigin(), std::string(), "data"));
689 EXPECT_EQ(SERVICE_WORKER_ERROR_FAILED,
690 GetUserData(kRegistrationId, std::string(), &data_out));
691 EXPECT_EQ(SERVICE_WORKER_ERROR_FAILED,
692 ClearUserData(kRegistrationId, std::string()));
695 class ServiceWorkerResourceStorageTest : public ServiceWorkerStorageTest {
696 public:
697 void SetUp() override {
698 ServiceWorkerStorageTest::SetUp();
700 storage()->LazyInitialize(base::Bind(&base::DoNothing));
701 base::RunLoop().RunUntilIdle();
702 scope_ = GURL("http://www.test.not/scope/");
703 script_ = GURL("http://www.test.not/script.js");
704 import_ = GURL("http://www.test.not/import.js");
705 document_url_ = GURL("http://www.test.not/scope/document.html");
706 registration_id_ = storage()->NewRegistrationId();
707 version_id_ = storage()->NewVersionId();
708 resource_id1_ = storage()->NewResourceId();
709 resource_id2_ = storage()->NewResourceId();
710 resource_id1_size_ = 239193;
711 resource_id2_size_ = 59923;
713 // Cons up a new registration+version with two script resources.
714 RegistrationData data;
715 data.registration_id = registration_id_;
716 data.scope = scope_;
717 data.script = script_;
718 data.version_id = version_id_;
719 data.is_active = false;
720 std::vector<ResourceRecord> resources;
721 resources.push_back(
722 ResourceRecord(resource_id1_, script_, resource_id1_size_));
723 resources.push_back(
724 ResourceRecord(resource_id2_, import_, resource_id2_size_));
725 registration_ = storage()->GetOrCreateRegistration(data, resources);
726 registration_->waiting_version()->SetStatus(ServiceWorkerVersion::NEW);
728 // Add the resources ids to the uncommitted list.
729 storage()->StoreUncommittedResponseId(resource_id1_);
730 storage()->StoreUncommittedResponseId(resource_id2_);
731 base::RunLoop().RunUntilIdle();
732 std::set<int64> verify_ids;
733 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK,
734 storage()->database_->GetUncommittedResourceIds(&verify_ids));
735 EXPECT_EQ(2u, verify_ids.size());
737 // And dump something in the disk cache for them.
738 WriteBasicResponse(storage(), resource_id1_);
739 WriteBasicResponse(storage(), resource_id2_);
740 EXPECT_TRUE(VerifyBasicResponse(storage(), resource_id1_, true));
741 EXPECT_TRUE(VerifyBasicResponse(storage(), resource_id2_, true));
743 // Storing the registration/version should take the resources ids out
744 // of the uncommitted list.
745 EXPECT_EQ(
746 SERVICE_WORKER_OK,
747 StoreRegistration(registration_, registration_->waiting_version()));
748 verify_ids.clear();
749 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK,
750 storage()->database_->GetUncommittedResourceIds(&verify_ids));
751 EXPECT_TRUE(verify_ids.empty());
754 protected:
755 GURL scope_;
756 GURL script_;
757 GURL import_;
758 GURL document_url_;
759 int64 registration_id_;
760 int64 version_id_;
761 int64 resource_id1_;
762 uint64 resource_id1_size_;
763 int64 resource_id2_;
764 uint64 resource_id2_size_;
765 scoped_refptr<ServiceWorkerRegistration> registration_;
768 class ServiceWorkerResourceStorageDiskTest
769 : public ServiceWorkerResourceStorageTest {
770 public:
771 void SetUp() override {
772 ASSERT_TRUE(user_data_directory_.CreateUniqueTempDir());
773 ServiceWorkerResourceStorageTest::SetUp();
776 base::FilePath GetUserDataDirectory() override {
777 return user_data_directory_.path();
780 protected:
781 base::ScopedTempDir user_data_directory_;
784 TEST_F(ServiceWorkerResourceStorageTest, DeleteRegistration_NoLiveVersion) {
785 bool was_called = false;
786 ServiceWorkerStatusCode result = SERVICE_WORKER_ERROR_FAILED;
787 std::set<int64> verify_ids;
789 registration_->SetWaitingVersion(NULL);
790 registration_ = NULL;
792 // Deleting the registration should result in the resources being added to the
793 // purgeable list and then doomed in the disk cache and removed from that
794 // list.
795 storage()->DeleteRegistration(
796 registration_id_,
797 scope_.GetOrigin(),
798 base::Bind(&VerifyPurgeableListStatusCallback,
799 base::Unretained(storage()->database_.get()),
800 &verify_ids,
801 &was_called,
802 &result));
803 base::RunLoop().RunUntilIdle();
804 ASSERT_TRUE(was_called);
805 EXPECT_EQ(SERVICE_WORKER_OK, result);
806 EXPECT_EQ(2u, verify_ids.size());
807 verify_ids.clear();
808 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK,
809 storage()->database_->GetPurgeableResourceIds(&verify_ids));
810 EXPECT_TRUE(verify_ids.empty());
812 EXPECT_FALSE(VerifyBasicResponse(storage(), resource_id1_, false));
813 EXPECT_FALSE(VerifyBasicResponse(storage(), resource_id2_, false));
816 TEST_F(ServiceWorkerResourceStorageTest, DeleteRegistration_WaitingVersion) {
817 bool was_called = false;
818 ServiceWorkerStatusCode result = SERVICE_WORKER_ERROR_FAILED;
819 std::set<int64> verify_ids;
821 // Deleting the registration should result in the resources being added to the
822 // purgeable list and then doomed in the disk cache and removed from that
823 // list.
824 storage()->DeleteRegistration(
825 registration_->id(),
826 scope_.GetOrigin(),
827 base::Bind(&VerifyPurgeableListStatusCallback,
828 base::Unretained(storage()->database_.get()),
829 &verify_ids,
830 &was_called,
831 &result));
832 base::RunLoop().RunUntilIdle();
833 ASSERT_TRUE(was_called);
834 EXPECT_EQ(SERVICE_WORKER_OK, result);
835 EXPECT_EQ(2u, verify_ids.size());
836 verify_ids.clear();
837 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK,
838 storage()->database_->GetPurgeableResourceIds(&verify_ids));
839 EXPECT_EQ(2u, verify_ids.size());
841 EXPECT_TRUE(VerifyBasicResponse(storage(), resource_id1_, false));
842 EXPECT_TRUE(VerifyBasicResponse(storage(), resource_id2_, false));
844 // Doom the version, now it happens.
845 registration_->waiting_version()->Doom();
846 base::RunLoop().RunUntilIdle();
847 EXPECT_EQ(SERVICE_WORKER_OK, result);
848 EXPECT_EQ(2u, verify_ids.size());
849 verify_ids.clear();
850 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK,
851 storage()->database_->GetPurgeableResourceIds(&verify_ids));
852 EXPECT_TRUE(verify_ids.empty());
854 EXPECT_FALSE(VerifyBasicResponse(storage(), resource_id1_, false));
855 EXPECT_FALSE(VerifyBasicResponse(storage(), resource_id2_, false));
858 TEST_F(ServiceWorkerResourceStorageTest, DeleteRegistration_ActiveVersion) {
859 // Promote the worker to active and add a controllee.
860 registration_->SetActiveVersion(registration_->waiting_version());
861 storage()->UpdateToActiveState(
862 registration_.get(), base::Bind(&ServiceWorkerUtils::NoOpStatusCallback));
863 scoped_ptr<ServiceWorkerProviderHost> host(
864 new ServiceWorkerProviderHost(33 /* dummy render process id */,
865 MSG_ROUTING_NONE,
866 1 /* dummy provider_id */,
867 context_->AsWeakPtr(),
868 NULL));
869 registration_->active_version()->AddControllee(host.get());
871 bool was_called = false;
872 ServiceWorkerStatusCode result = SERVICE_WORKER_ERROR_FAILED;
873 std::set<int64> verify_ids;
875 // Deleting the registration should move the resources to the purgeable list
876 // but keep them available.
877 storage()->DeleteRegistration(
878 registration_->id(),
879 scope_.GetOrigin(),
880 base::Bind(&VerifyPurgeableListStatusCallback,
881 base::Unretained(storage()->database_.get()),
882 &verify_ids,
883 &was_called,
884 &result));
885 registration_->active_version()->Doom();
886 base::RunLoop().RunUntilIdle();
887 ASSERT_TRUE(was_called);
888 EXPECT_EQ(SERVICE_WORKER_OK, result);
889 EXPECT_EQ(2u, verify_ids.size());
890 verify_ids.clear();
891 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK,
892 storage()->database_->GetPurgeableResourceIds(&verify_ids));
893 EXPECT_EQ(2u, verify_ids.size());
895 EXPECT_TRUE(VerifyBasicResponse(storage(), resource_id1_, true));
896 EXPECT_TRUE(VerifyBasicResponse(storage(), resource_id2_, true));
898 // Removing the controllee should cause the resources to be deleted.
899 registration_->active_version()->RemoveControllee(host.get());
900 base::RunLoop().RunUntilIdle();
901 verify_ids.clear();
902 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK,
903 storage()->database_->GetPurgeableResourceIds(&verify_ids));
904 EXPECT_TRUE(verify_ids.empty());
906 EXPECT_FALSE(VerifyBasicResponse(storage(), resource_id1_, false));
907 EXPECT_FALSE(VerifyBasicResponse(storage(), resource_id2_, false));
910 TEST_F(ServiceWorkerResourceStorageDiskTest, CleanupOnRestart) {
911 // Promote the worker to active and add a controllee.
912 registration_->SetActiveVersion(registration_->waiting_version());
913 registration_->SetWaitingVersion(NULL);
914 storage()->UpdateToActiveState(
915 registration_.get(), base::Bind(&ServiceWorkerUtils::NoOpStatusCallback));
916 scoped_ptr<ServiceWorkerProviderHost> host(
917 new ServiceWorkerProviderHost(33 /* dummy render process id */,
918 MSG_ROUTING_NONE,
919 1 /* dummy provider_id */,
920 context_->AsWeakPtr(),
921 NULL));
922 registration_->active_version()->AddControllee(host.get());
924 bool was_called = false;
925 ServiceWorkerStatusCode result = SERVICE_WORKER_ERROR_FAILED;
926 std::set<int64> verify_ids;
928 // Deleting the registration should move the resources to the purgeable list
929 // but keep them available.
930 storage()->DeleteRegistration(
931 registration_->id(),
932 scope_.GetOrigin(),
933 base::Bind(&VerifyPurgeableListStatusCallback,
934 base::Unretained(storage()->database_.get()),
935 &verify_ids,
936 &was_called,
937 &result));
938 base::RunLoop().RunUntilIdle();
939 ASSERT_TRUE(was_called);
940 EXPECT_EQ(SERVICE_WORKER_OK, result);
941 EXPECT_EQ(2u, verify_ids.size());
942 verify_ids.clear();
943 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK,
944 storage()->database_->GetPurgeableResourceIds(&verify_ids));
945 EXPECT_EQ(2u, verify_ids.size());
947 EXPECT_TRUE(VerifyBasicResponse(storage(), resource_id1_, true));
948 EXPECT_TRUE(VerifyBasicResponse(storage(), resource_id2_, true));
950 // Also add an uncommitted resource.
951 int64 kStaleUncommittedResourceId = storage()->NewResourceId();
952 storage()->StoreUncommittedResponseId(kStaleUncommittedResourceId);
953 base::RunLoop().RunUntilIdle();
954 verify_ids.clear();
955 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK,
956 storage()->database_->GetUncommittedResourceIds(&verify_ids));
957 EXPECT_EQ(1u, verify_ids.size());
958 WriteBasicResponse(storage(), kStaleUncommittedResourceId);
959 EXPECT_TRUE(
960 VerifyBasicResponse(storage(), kStaleUncommittedResourceId, true));
962 // Simulate browser shutdown. The purgeable and uncommitted resources are now
963 // stale.
964 context_.reset();
965 scoped_ptr<ServiceWorkerDatabaseTaskManager> database_task_manager(
966 new MockServiceWorkerDatabaseTaskManager(
967 base::ThreadTaskRunnerHandle::Get()));
968 context_.reset(
969 new ServiceWorkerContextCore(GetUserDataDirectory(),
970 base::ThreadTaskRunnerHandle::Get(),
971 database_task_manager.Pass(),
972 base::ThreadTaskRunnerHandle::Get(),
973 NULL,
974 NULL,
975 NULL,
976 NULL));
977 storage()->LazyInitialize(base::Bind(&base::DoNothing));
978 base::RunLoop().RunUntilIdle();
980 // Store a new uncommitted resource. This triggers stale resource cleanup.
981 int64 kNewResourceId = storage()->NewResourceId();
982 WriteBasicResponse(storage(), kNewResourceId);
983 storage()->StoreUncommittedResponseId(kNewResourceId);
984 base::RunLoop().RunUntilIdle();
986 // The stale resources should be purged, but the new resource should persist.
987 verify_ids.clear();
988 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK,
989 storage()->database_->GetUncommittedResourceIds(&verify_ids));
990 ASSERT_EQ(1u, verify_ids.size());
991 EXPECT_EQ(kNewResourceId, *verify_ids.begin());
993 // Purging resources needs interactions with SimpleCache's worker thread,
994 // so single RunUntilIdle() call may not be sufficient.
995 while (storage()->is_purge_pending_)
996 base::RunLoop().RunUntilIdle();
998 verify_ids.clear();
999 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK,
1000 storage()->database_->GetPurgeableResourceIds(&verify_ids));
1001 EXPECT_TRUE(verify_ids.empty());
1002 EXPECT_FALSE(VerifyBasicResponse(storage(), resource_id1_, false));
1003 EXPECT_FALSE(VerifyBasicResponse(storage(), resource_id2_, false));
1004 EXPECT_FALSE(
1005 VerifyBasicResponse(storage(), kStaleUncommittedResourceId, false));
1006 EXPECT_TRUE(VerifyBasicResponse(storage(), kNewResourceId, true));
1009 TEST_F(ServiceWorkerResourceStorageTest, UpdateRegistration) {
1010 // Promote the worker to active worker and add a controllee.
1011 registration_->SetActiveVersion(registration_->waiting_version());
1012 storage()->UpdateToActiveState(
1013 registration_.get(), base::Bind(&ServiceWorkerUtils::NoOpStatusCallback));
1014 scoped_ptr<ServiceWorkerProviderHost> host(
1015 new ServiceWorkerProviderHost(33 /* dummy render process id */,
1016 MSG_ROUTING_NONE,
1017 1 /* dummy provider_id */,
1018 context_->AsWeakPtr(),
1019 NULL));
1020 registration_->active_version()->AddControllee(host.get());
1022 bool was_called = false;
1023 ServiceWorkerStatusCode result = SERVICE_WORKER_ERROR_FAILED;
1024 std::set<int64> verify_ids;
1026 // Make an updated registration.
1027 scoped_refptr<ServiceWorkerVersion> live_version = new ServiceWorkerVersion(
1028 registration_.get(), script_, storage()->NewVersionId(), context_ptr_);
1029 live_version->SetStatus(ServiceWorkerVersion::NEW);
1030 registration_->SetWaitingVersion(live_version.get());
1032 // Writing the registration should move the old version's resources to the
1033 // purgeable list but keep them available.
1034 storage()->StoreRegistration(
1035 registration_.get(),
1036 registration_->waiting_version(),
1037 base::Bind(&VerifyPurgeableListStatusCallback,
1038 base::Unretained(storage()->database_.get()),
1039 &verify_ids,
1040 &was_called,
1041 &result));
1042 registration_->active_version()->Doom();
1043 base::RunLoop().RunUntilIdle();
1044 ASSERT_TRUE(was_called);
1045 EXPECT_EQ(SERVICE_WORKER_OK, result);
1046 EXPECT_EQ(2u, verify_ids.size());
1047 verify_ids.clear();
1048 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK,
1049 storage()->database_->GetPurgeableResourceIds(&verify_ids));
1050 EXPECT_EQ(2u, verify_ids.size());
1052 EXPECT_TRUE(VerifyBasicResponse(storage(), resource_id1_, false));
1053 EXPECT_TRUE(VerifyBasicResponse(storage(), resource_id2_, false));
1055 // Removing the controllee should cause the old version's resources to be
1056 // deleted.
1057 registration_->active_version()->RemoveControllee(host.get());
1058 base::RunLoop().RunUntilIdle();
1059 verify_ids.clear();
1060 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK,
1061 storage()->database_->GetPurgeableResourceIds(&verify_ids));
1062 EXPECT_TRUE(verify_ids.empty());
1064 EXPECT_FALSE(VerifyBasicResponse(storage(), resource_id1_, false));
1065 EXPECT_FALSE(VerifyBasicResponse(storage(), resource_id2_, false));
1068 TEST_F(ServiceWorkerStorageTest, FindRegistration_LongestScopeMatch) {
1069 const GURL kDocumentUrl("http://www.example.com/scope/foo");
1070 scoped_refptr<ServiceWorkerRegistration> found_registration;
1072 // Registration for "/scope/".
1073 const GURL kScope1("http://www.example.com/scope/");
1074 const GURL kScript1("http://www.example.com/script1.js");
1075 const int64 kRegistrationId1 = 1;
1076 const int64 kVersionId1 = 1;
1077 scoped_refptr<ServiceWorkerRegistration> live_registration1 =
1078 new ServiceWorkerRegistration(
1079 kScope1, kRegistrationId1, context_ptr_);
1080 scoped_refptr<ServiceWorkerVersion> live_version1 =
1081 new ServiceWorkerVersion(
1082 live_registration1.get(), kScript1, kVersionId1, context_ptr_);
1083 live_version1->SetStatus(ServiceWorkerVersion::INSTALLED);
1084 live_registration1->SetWaitingVersion(live_version1.get());
1086 // Registration for "/scope/foo".
1087 const GURL kScope2("http://www.example.com/scope/foo");
1088 const GURL kScript2("http://www.example.com/script2.js");
1089 const int64 kRegistrationId2 = 2;
1090 const int64 kVersionId2 = 2;
1091 scoped_refptr<ServiceWorkerRegistration> live_registration2 =
1092 new ServiceWorkerRegistration(
1093 kScope2, kRegistrationId2, context_ptr_);
1094 scoped_refptr<ServiceWorkerVersion> live_version2 =
1095 new ServiceWorkerVersion(
1096 live_registration2.get(), kScript2, kVersionId2, context_ptr_);
1097 live_version2->SetStatus(ServiceWorkerVersion::INSTALLED);
1098 live_registration2->SetWaitingVersion(live_version2.get());
1100 // Registration for "/scope/foobar".
1101 const GURL kScope3("http://www.example.com/scope/foobar");
1102 const GURL kScript3("http://www.example.com/script3.js");
1103 const int64 kRegistrationId3 = 3;
1104 const int64 kVersionId3 = 3;
1105 scoped_refptr<ServiceWorkerRegistration> live_registration3 =
1106 new ServiceWorkerRegistration(
1107 kScope3, kRegistrationId3, context_ptr_);
1108 scoped_refptr<ServiceWorkerVersion> live_version3 =
1109 new ServiceWorkerVersion(
1110 live_registration3.get(), kScript3, kVersionId3, context_ptr_);
1111 live_version3->SetStatus(ServiceWorkerVersion::INSTALLED);
1112 live_registration3->SetWaitingVersion(live_version3.get());
1114 // Notify storage of they being installed.
1115 storage()->NotifyInstallingRegistration(live_registration1.get());
1116 storage()->NotifyInstallingRegistration(live_registration2.get());
1117 storage()->NotifyInstallingRegistration(live_registration3.get());
1119 // Find a registration among installing ones.
1120 EXPECT_EQ(SERVICE_WORKER_OK,
1121 FindRegistrationForDocument(kDocumentUrl, &found_registration));
1122 EXPECT_EQ(live_registration2, found_registration);
1123 found_registration = NULL;
1125 // Store registrations.
1126 EXPECT_EQ(SERVICE_WORKER_OK,
1127 StoreRegistration(live_registration1, live_version1));
1128 EXPECT_EQ(SERVICE_WORKER_OK,
1129 StoreRegistration(live_registration2, live_version2));
1130 EXPECT_EQ(SERVICE_WORKER_OK,
1131 StoreRegistration(live_registration3, live_version3));
1133 // Notify storage of installations no longer happening.
1134 storage()->NotifyDoneInstallingRegistration(
1135 live_registration1.get(), NULL, SERVICE_WORKER_OK);
1136 storage()->NotifyDoneInstallingRegistration(
1137 live_registration2.get(), NULL, SERVICE_WORKER_OK);
1138 storage()->NotifyDoneInstallingRegistration(
1139 live_registration3.get(), NULL, SERVICE_WORKER_OK);
1141 // Find a registration among installed ones.
1142 EXPECT_EQ(SERVICE_WORKER_OK,
1143 FindRegistrationForDocument(kDocumentUrl, &found_registration));
1144 EXPECT_EQ(live_registration2, found_registration);
1147 TEST_F(ServiceWorkerStorageTest, CompareResources) {
1148 // Compare two small responses containing the same data.
1149 WriteBasicResponse(storage(), 1);
1150 WriteBasicResponse(storage(), 2);
1151 ServiceWorkerStatusCode status = static_cast<ServiceWorkerStatusCode>(-1);
1152 bool are_equal = false;
1153 storage()->CompareScriptResources(
1154 1, 2,
1155 base::Bind(&OnCompareComplete, &status, &are_equal));
1156 base::RunLoop().RunUntilIdle();
1157 EXPECT_EQ(SERVICE_WORKER_OK, status);
1158 EXPECT_TRUE(are_equal);
1160 // Compare two small responses with different data.
1161 const char kHttpHeaders[] = "HTTP/1.0 200 HONKYDORY\0\0";
1162 const char kHttpBody[] = "Goodbye";
1163 std::string headers(kHttpHeaders, arraysize(kHttpHeaders));
1164 WriteStringResponse(storage(), 3, headers, std::string(kHttpBody));
1165 status = static_cast<ServiceWorkerStatusCode>(-1);
1166 are_equal = true;
1167 storage()->CompareScriptResources(
1168 1, 3,
1169 base::Bind(&OnCompareComplete, &status, &are_equal));
1170 base::RunLoop().RunUntilIdle();
1171 EXPECT_EQ(SERVICE_WORKER_OK, status);
1172 EXPECT_FALSE(are_equal);
1174 // Compare two large responses with the same data.
1175 const int k32K = 32 * 1024;
1176 WriteResponseOfSize(storage(), 4, 'a', k32K);
1177 WriteResponseOfSize(storage(), 5, 'a', k32K);
1178 status = static_cast<ServiceWorkerStatusCode>(-1);
1179 are_equal = false;
1180 storage()->CompareScriptResources(
1181 4, 5,
1182 base::Bind(&OnCompareComplete, &status, &are_equal));
1183 base::RunLoop().RunUntilIdle();
1184 EXPECT_EQ(SERVICE_WORKER_OK, status);
1185 EXPECT_TRUE(are_equal);
1187 // Compare a large and small response.
1188 status = static_cast<ServiceWorkerStatusCode>(-1);
1189 are_equal = true;
1190 storage()->CompareScriptResources(
1191 1, 5,
1192 base::Bind(&OnCompareComplete, &status, &are_equal));
1193 base::RunLoop().RunUntilIdle();
1194 EXPECT_EQ(SERVICE_WORKER_OK, status);
1195 EXPECT_FALSE(are_equal);
1197 // Compare two large responses with different data.
1198 WriteResponseOfSize(storage(), 6, 'b', k32K);
1199 status = static_cast<ServiceWorkerStatusCode>(-1);
1200 are_equal = true;
1201 storage()->CompareScriptResources(
1202 5, 6,
1203 base::Bind(&OnCompareComplete, &status, &are_equal));
1204 base::RunLoop().RunUntilIdle();
1205 EXPECT_EQ(SERVICE_WORKER_OK, status);
1206 EXPECT_FALSE(are_equal);
1209 } // namespace content