1a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved. 2a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 3a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// found in the LICENSE file. 4a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) 546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include <string> 646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) 7116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "base/files/scoped_temp_dir.h" 8a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "base/logging.h" 90529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "base/run_loop.h" 1003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)#include "base/thread_task_runner_handle.h" 110529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "content/browser/browser_thread_impl.h" 120529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "content/browser/service_worker/service_worker_context_core.h" 13cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "content/browser/service_worker/service_worker_disk_cache.h" 14a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "content/browser/service_worker/service_worker_registration.h" 15cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "content/browser/service_worker/service_worker_storage.h" 16116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "content/browser/service_worker/service_worker_utils.h" 170529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "content/browser/service_worker/service_worker_version.h" 18cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "content/common/service_worker/service_worker_status_code.h" 190529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "content/public/test/test_browser_thread_bundle.h" 20cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "net/base/io_buffer.h" 21cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "net/base/net_errors.h" 22116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "net/base/test_completion_callback.h" 23cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "net/http/http_response_headers.h" 24a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h" 25a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) 26cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)using net::IOBuffer; 27116680a4aac90f2aa7413d9095a592090648e557Ben Murdochusing net::TestCompletionCallback; 28cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)using net::WrappedIOBuffer; 29cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 30a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)namespace content { 31a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) 320529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochnamespace { 330529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch 34cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)typedef ServiceWorkerDatabase::RegistrationData RegistrationData; 35cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)typedef ServiceWorkerDatabase::ResourceRecord ResourceRecord; 36cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 370529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochvoid StatusCallback(bool* was_called, 380529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch ServiceWorkerStatusCode* result, 390529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch ServiceWorkerStatusCode status) { 400529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch *was_called = true; 410529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch *result = status; 420529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch} 430529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch 440529e5d033099cbfc42635f6f6183833b09dff6eBen MurdochServiceWorkerStorage::StatusCallback MakeStatusCallback( 450529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch bool* was_called, 460529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch ServiceWorkerStatusCode* result) { 470529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch return base::Bind(&StatusCallback, was_called, result); 480529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch} 490529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch 500529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochvoid FindCallback( 510529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch bool* was_called, 520529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch ServiceWorkerStatusCode* result, 530529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch scoped_refptr<ServiceWorkerRegistration>* found, 540529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch ServiceWorkerStatusCode status, 550529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch const scoped_refptr<ServiceWorkerRegistration>& registration) { 560529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch *was_called = true; 570529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch *result = status; 580529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch *found = registration; 590529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch} 600529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch 610529e5d033099cbfc42635f6f6183833b09dff6eBen MurdochServiceWorkerStorage::FindRegistrationCallback MakeFindCallback( 620529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch bool* was_called, 630529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch ServiceWorkerStatusCode* result, 640529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch scoped_refptr<ServiceWorkerRegistration>* found) { 650529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch return base::Bind(&FindCallback, was_called, result, found); 660529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch} 670529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch 680529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochvoid GetAllCallback( 690529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch bool* was_called, 700529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch std::vector<ServiceWorkerRegistrationInfo>* all_out, 710529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch const std::vector<ServiceWorkerRegistrationInfo>& all) { 720529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch *was_called = true; 730529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch *all_out = all; 740529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch} 750529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch 760529e5d033099cbfc42635f6f6183833b09dff6eBen MurdochServiceWorkerStorage::GetAllRegistrationInfosCallback MakeGetAllCallback( 770529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch bool* was_called, 780529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch std::vector<ServiceWorkerRegistrationInfo>* all) { 790529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch return base::Bind(&GetAllCallback, was_called, all); 800529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch} 810529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch 82cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void OnIOComplete(int* rv_out, int rv) { 83cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) *rv_out = rv; 84cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)} 85cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 865f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)void OnCompareComplete( 875f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) ServiceWorkerStatusCode* status_out, bool* are_equal_out, 885f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) ServiceWorkerStatusCode status, bool are_equal) { 895f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) *status_out = status; 905f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) *are_equal_out = are_equal; 915f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)} 925f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 935f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)void WriteResponse( 945f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) ServiceWorkerStorage* storage, int64 id, 955f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) const std::string& headers, 965f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) IOBuffer* body, int length) { 97cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) scoped_ptr<ServiceWorkerResponseWriter> writer = 98cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) storage->CreateResponseWriter(id); 99cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 100cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) scoped_ptr<net::HttpResponseInfo> info(new net::HttpResponseInfo); 101cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) info->request_time = base::Time::Now(); 102cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) info->response_time = base::Time::Now(); 103cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) info->was_cached = false; 1045f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) info->headers = new net::HttpResponseHeaders(headers); 105cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) scoped_refptr<HttpResponseInfoIOBuffer> info_buffer = 106cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) new HttpResponseInfoIOBuffer(info.release()); 107cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 108cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) int rv = -1234; 1091320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci writer->WriteInfo(info_buffer.get(), base::Bind(&OnIOComplete, &rv)); 110cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) base::RunLoop().RunUntilIdle(); 111cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) EXPECT_LT(0, rv); 112cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 113cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) rv = -1234; 1145f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) writer->WriteData(body, length, base::Bind(&OnIOComplete, &rv)); 115cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) base::RunLoop().RunUntilIdle(); 1165f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) EXPECT_EQ(length, rv); 1175f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)} 1185f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 1195f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)void WriteStringResponse( 1205f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) ServiceWorkerStorage* storage, int64 id, 1215f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) const std::string& headers, 1225f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) const std::string& body) { 1235f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) scoped_refptr<IOBuffer> body_buffer(new WrappedIOBuffer(body.data())); 1241320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci WriteResponse(storage, id, headers, body_buffer.get(), body.length()); 1255f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)} 1265f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 1275f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)void WriteBasicResponse(ServiceWorkerStorage* storage, int64 id) { 1285f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) scoped_ptr<ServiceWorkerResponseWriter> writer = 1295f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) storage->CreateResponseWriter(id); 1305f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 1315f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) const char kHttpHeaders[] = "HTTP/1.0 200 HONKYDORY\0Content-Length: 5\0\0"; 1325f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) const char kHttpBody[] = "Hello"; 1335f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) std::string headers(kHttpHeaders, arraysize(kHttpHeaders)); 1345f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) WriteStringResponse(storage, id, headers, std::string(kHttpBody)); 135cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)} 136cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 137cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)bool VerifyBasicResponse(ServiceWorkerStorage* storage, int64 id, 138cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) bool expected_positive_result) { 1395f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) const std::string kExpectedHttpBody("Hello"); 140cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) scoped_ptr<ServiceWorkerResponseReader> reader = 141cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) storage->CreateResponseReader(id); 142cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) scoped_refptr<HttpResponseInfoIOBuffer> info_buffer = 143cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) new HttpResponseInfoIOBuffer(); 144116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch { 145116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch TestCompletionCallback cb; 1461320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci reader->ReadInfo(info_buffer.get(), cb.callback()); 147116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch int rv = cb.WaitForResult(); 148116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch if (expected_positive_result) 149116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch EXPECT_LT(0, rv); 150116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch if (rv <= 0) 151116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch return false; 152116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch } 153cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 1545f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) std::string received_body; 155116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch { 1565f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) const int kBigEnough = 512; 1575f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) scoped_refptr<net::IOBuffer> buffer = new IOBuffer(kBigEnough); 158116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch TestCompletionCallback cb; 1591320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci reader->ReadData(buffer.get(), kBigEnough, cb.callback()); 160116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch int rv = cb.WaitForResult(); 1615f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) EXPECT_EQ(static_cast<int>(kExpectedHttpBody.size()), rv); 162116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch if (rv <= 0) 163116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch return false; 1645f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) received_body.assign(buffer->data(), rv); 165116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch } 166cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 167cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) bool status_match = 168cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) std::string("HONKYDORY") == 169cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) info_buffer->http_info->headers->GetStatusText(); 1705f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) bool data_match = kExpectedHttpBody == received_body; 171cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 172cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) EXPECT_TRUE(status_match); 173cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) EXPECT_TRUE(data_match); 174cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) return status_match && data_match; 175cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)} 176cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 1775f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)void WriteResponseOfSize(ServiceWorkerStorage* storage, int64 id, 1785f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) char val, int size) { 1795f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) const char kHttpHeaders[] = "HTTP/1.0 200 HONKYDORY\00"; 1805f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) std::string headers(kHttpHeaders, arraysize(kHttpHeaders)); 1815f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(size); 1825f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) memset(buffer->data(), val, size); 1831320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci WriteResponse(storage, id, headers, buffer.get(), size); 1845f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)} 1855f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 1860529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch} // namespace 1870529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch 188a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)class ServiceWorkerStorageTest : public testing::Test { 189a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) public: 1900529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch ServiceWorkerStorageTest() 1910529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) { 1920529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch } 193a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) 194a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) virtual void SetUp() OVERRIDE { 195cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) context_.reset( 196116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch new ServiceWorkerContextCore(GetUserDataDirectory(), 19703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) base::ThreadTaskRunnerHandle::Get(), 19803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) base::ThreadTaskRunnerHandle::Get(), 19903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) base::ThreadTaskRunnerHandle::Get(), 200cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) NULL, 201cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) NULL, 202cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) NULL)); 2030529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch context_ptr_ = context_->AsWeakPtr(); 204a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) } 205a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) 2060529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch virtual void TearDown() OVERRIDE { 2070529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch context_.reset(); 2080529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch } 2090529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch 210116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch virtual base::FilePath GetUserDataDirectory() { return base::FilePath(); } 211116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 2120529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch ServiceWorkerStorage* storage() { return context_->storage(); } 213a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) 214cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) // A static class method for friendliness. 215cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) static void VerifyPurgeableListStatusCallback( 216cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) ServiceWorkerDatabase* database, 217cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) std::set<int64> *purgeable_ids, 218cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) bool* was_called, 219cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) ServiceWorkerStatusCode* result, 220cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) ServiceWorkerStatusCode status) { 221cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) *was_called = true; 222cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) *result = status; 223cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, 224cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) database->GetPurgeableResourceIds(purgeable_ids)); 225cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) } 226cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 227a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) protected: 228f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) ServiceWorkerStatusCode StoreRegistration( 229f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) scoped_refptr<ServiceWorkerRegistration> registration, 230f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) scoped_refptr<ServiceWorkerVersion> version) { 231f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) bool was_called = false; 232f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) ServiceWorkerStatusCode result = SERVICE_WORKER_ERROR_FAILED; 2331320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci storage()->StoreRegistration(registration.get(), 2341320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci version.get(), 2351320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci MakeStatusCallback(&was_called, &result)); 236f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) EXPECT_FALSE(was_called); // always async 237f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) base::RunLoop().RunUntilIdle(); 238f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) EXPECT_TRUE(was_called); 239f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) return result; 240f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) } 241f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) 242f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) ServiceWorkerStatusCode DeleteRegistration( 243f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) int64 registration_id, 244f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) const GURL& origin) { 245f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) bool was_called = false; 246f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) ServiceWorkerStatusCode result = SERVICE_WORKER_ERROR_FAILED; 247f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) storage()->DeleteRegistration( 248f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) registration_id, origin, MakeStatusCallback(&was_called, &result)); 249f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) EXPECT_FALSE(was_called); // always async 250f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) base::RunLoop().RunUntilIdle(); 251f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) EXPECT_TRUE(was_called); 252f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) return result; 253f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) } 254f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) 255f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) void GetAllRegistrations( 256f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) std::vector<ServiceWorkerRegistrationInfo>* registrations) { 257f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) bool was_called = false; 258f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) storage()->GetAllRegistrations( 259f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) MakeGetAllCallback(&was_called, registrations)); 260f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) EXPECT_FALSE(was_called); // always async 261f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) base::RunLoop().RunUntilIdle(); 262f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) EXPECT_TRUE(was_called); 263f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) } 264f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) 265f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) ServiceWorkerStatusCode UpdateToActiveState( 266f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) scoped_refptr<ServiceWorkerRegistration> registration) { 267f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) bool was_called = false; 268f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) ServiceWorkerStatusCode result = SERVICE_WORKER_ERROR_FAILED; 2691320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci storage()->UpdateToActiveState(registration.get(), 2701320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci MakeStatusCallback(&was_called, &result)); 271f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) EXPECT_FALSE(was_called); // always async 272f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) base::RunLoop().RunUntilIdle(); 273f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) EXPECT_TRUE(was_called); 274f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) return result; 275f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) } 276f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) 2776e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) void UpdateLastUpdateCheckTime(ServiceWorkerRegistration* registration) { 2786e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) storage()->UpdateLastUpdateCheckTime(registration); 2796e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) base::RunLoop().RunUntilIdle(); 2806e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) } 2816e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) 282f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) ServiceWorkerStatusCode FindRegistrationForDocument( 283f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) const GURL& document_url, 284f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) scoped_refptr<ServiceWorkerRegistration>* registration) { 285f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) bool was_called = false; 286f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) ServiceWorkerStatusCode result = SERVICE_WORKER_ERROR_FAILED; 287f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) storage()->FindRegistrationForDocument( 288f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) document_url, MakeFindCallback(&was_called, &result, registration)); 289f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) base::RunLoop().RunUntilIdle(); 290f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) EXPECT_TRUE(was_called); 291f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) return result; 292f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) } 293f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) 294f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) ServiceWorkerStatusCode FindRegistrationForPattern( 295f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) const GURL& scope, 296f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) scoped_refptr<ServiceWorkerRegistration>* registration) { 297f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) bool was_called = false; 298f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) ServiceWorkerStatusCode result = SERVICE_WORKER_ERROR_FAILED; 299f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) storage()->FindRegistrationForPattern( 300f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) scope, MakeFindCallback(&was_called, &result, registration)); 301f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) EXPECT_FALSE(was_called); // always async 302f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) base::RunLoop().RunUntilIdle(); 303f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) EXPECT_TRUE(was_called); 304f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) return result; 305f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) } 306f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) 307f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) ServiceWorkerStatusCode FindRegistrationForId( 308f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) int64 registration_id, 309f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) const GURL& origin, 310f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) scoped_refptr<ServiceWorkerRegistration>* registration) { 311f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) bool was_called = false; 312f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) ServiceWorkerStatusCode result = SERVICE_WORKER_ERROR_FAILED; 313f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) storage()->FindRegistrationForId( 314f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) registration_id, origin, 315f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) MakeFindCallback(&was_called, &result, registration)); 316f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) base::RunLoop().RunUntilIdle(); 317f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) EXPECT_TRUE(was_called); 318f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) return result; 319f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) } 320f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) 3210529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch scoped_ptr<ServiceWorkerContextCore> context_; 3220529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch base::WeakPtr<ServiceWorkerContextCore> context_ptr_; 3230529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch TestBrowserThreadBundle browser_thread_bundle_; 324a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)}; 325a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) 3260529e5d033099cbfc42635f6f6183833b09dff6eBen MurdochTEST_F(ServiceWorkerStorageTest, StoreFindUpdateDeleteRegistration) { 3275f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) const GURL kScope("http://www.test.not/scope/"); 328cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) const GURL kScript("http://www.test.not/script.js"); 329cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) const GURL kDocumentUrl("http://www.test.not/scope/document.html"); 330010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) const int64 kRegistrationId = 0; 331010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) const int64 kVersionId = 0; 3326e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) const base::Time kToday = base::Time::Now(); 3336e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) const base::Time kYesterday = kToday - base::TimeDelta::FromDays(1); 3340529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch 3350529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch scoped_refptr<ServiceWorkerRegistration> found_registration; 3360529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch 3370529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch // We shouldn't find anything without having stored anything. 338f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, 339f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) FindRegistrationForDocument(kDocumentUrl, &found_registration)); 3401320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci EXPECT_FALSE(found_registration.get()); 341f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) 342f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, 343f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) FindRegistrationForPattern(kScope, &found_registration)); 3441320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci EXPECT_FALSE(found_registration.get()); 345f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) 346f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, 347f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) FindRegistrationForId( 348f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) kRegistrationId, kScope.GetOrigin(), &found_registration)); 3491320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci EXPECT_FALSE(found_registration.get()); 3500529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch 3510529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch // Store something. 3520529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch scoped_refptr<ServiceWorkerRegistration> live_registration = 3530529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch new ServiceWorkerRegistration( 3541320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci kScope, kRegistrationId, context_ptr_); 3550529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch scoped_refptr<ServiceWorkerVersion> live_version = 3560529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch new ServiceWorkerVersion( 3571320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci live_registration.get(), kScript, kVersionId, context_ptr_); 3580529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch live_version->SetStatus(ServiceWorkerVersion::INSTALLED); 3591320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci live_registration->SetWaitingVersion(live_version.get()); 3606e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) live_registration->set_last_update_check(kYesterday); 361f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) EXPECT_EQ(SERVICE_WORKER_OK, 362f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) StoreRegistration(live_registration, live_version)); 3630529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch 3640529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch // Now we should find it and get the live ptr back immediately. 365f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) EXPECT_EQ(SERVICE_WORKER_OK, 366f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) FindRegistrationForDocument(kDocumentUrl, &found_registration)); 3670529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch EXPECT_EQ(live_registration, found_registration); 3680529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch found_registration = NULL; 3690529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch 3700529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch // But FindRegistrationForPattern is always async. 371f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) EXPECT_EQ(SERVICE_WORKER_OK, 372f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) FindRegistrationForPattern(kScope, &found_registration)); 3730529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch EXPECT_EQ(live_registration, found_registration); 3740529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch found_registration = NULL; 3750529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch 3760529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch // Can be found by id too. 377f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) EXPECT_EQ(SERVICE_WORKER_OK, 378f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) FindRegistrationForId( 379f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) kRegistrationId, kScope.GetOrigin(), &found_registration)); 3801320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci ASSERT_TRUE(found_registration.get()); 3810529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch EXPECT_EQ(kRegistrationId, found_registration->id()); 3820529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch EXPECT_EQ(live_registration, found_registration); 3830529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch found_registration = NULL; 3840529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch 3850529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch // Drop the live registration, but keep the version live. 3860529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch live_registration = NULL; 3870529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch 3880529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch // Now FindRegistrationForDocument should be async. 389f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) EXPECT_EQ(SERVICE_WORKER_OK, 390f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) FindRegistrationForDocument(kDocumentUrl, &found_registration)); 3911320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci ASSERT_TRUE(found_registration.get()); 3920529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch EXPECT_EQ(kRegistrationId, found_registration->id()); 3930529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch EXPECT_TRUE(found_registration->HasOneRef()); 3941320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci EXPECT_EQ(live_version.get(), found_registration->waiting_version()); 3950529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch found_registration = NULL; 3960529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch 3970529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch // Drop the live version too. 3980529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch live_version = NULL; 3990529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch 4000529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch // And FindRegistrationForPattern is always async. 401f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) EXPECT_EQ(SERVICE_WORKER_OK, 402f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) FindRegistrationForPattern(kScope, &found_registration)); 4031320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci ASSERT_TRUE(found_registration.get()); 4040529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch EXPECT_EQ(kRegistrationId, found_registration->id()); 4050529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch EXPECT_TRUE(found_registration->HasOneRef()); 4060529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch EXPECT_FALSE(found_registration->active_version()); 407cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) ASSERT_TRUE(found_registration->waiting_version()); 4086e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) EXPECT_EQ(kYesterday, found_registration->last_update_check()); 4090529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch EXPECT_EQ(ServiceWorkerVersion::INSTALLED, 410cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) found_registration->waiting_version()->status()); 4110529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch 4126e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) // Update to active and update the last check time. 4130529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch scoped_refptr<ServiceWorkerVersion> temp_version = 414cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) found_registration->waiting_version(); 415116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch temp_version->SetStatus(ServiceWorkerVersion::ACTIVATED); 4161320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci found_registration->SetActiveVersion(temp_version.get()); 4170529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch temp_version = NULL; 418f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) EXPECT_EQ(SERVICE_WORKER_OK, UpdateToActiveState(found_registration)); 4196e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) found_registration->set_last_update_check(kToday); 4201320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci UpdateLastUpdateCheckTime(found_registration.get()); 4216e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) 4220529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch found_registration = NULL; 4230529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch 4240529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch // Trying to update a unstored registration to active should fail. 4250529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch scoped_refptr<ServiceWorkerRegistration> unstored_registration = 4260529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch new ServiceWorkerRegistration( 4271320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci kScope, kRegistrationId + 1, context_ptr_); 428f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, 429f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) UpdateToActiveState(unstored_registration)); 4300529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch unstored_registration = NULL; 4310529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch 4326e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) // The Find methods should return a registration with an active version 4336e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) // and the expected update time. 434f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) EXPECT_EQ(SERVICE_WORKER_OK, 435f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) FindRegistrationForDocument(kDocumentUrl, &found_registration)); 4361320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci ASSERT_TRUE(found_registration.get()); 4370529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch EXPECT_EQ(kRegistrationId, found_registration->id()); 4380529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch EXPECT_TRUE(found_registration->HasOneRef()); 439cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) EXPECT_FALSE(found_registration->waiting_version()); 4400529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch ASSERT_TRUE(found_registration->active_version()); 441116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch EXPECT_EQ(ServiceWorkerVersion::ACTIVATED, 4420529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch found_registration->active_version()->status()); 4436e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) EXPECT_EQ(kToday, found_registration->last_update_check()); 4440529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch 4450529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch // Delete from storage but with a instance still live. 4460529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch EXPECT_TRUE(context_->GetLiveVersion(kRegistrationId)); 447f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) EXPECT_EQ(SERVICE_WORKER_OK, 448f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) DeleteRegistration(kRegistrationId, kScope.GetOrigin())); 4490529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch EXPECT_TRUE(context_->GetLiveVersion(kRegistrationId)); 4500529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch 4510529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch // Should no longer be found. 452f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, 453f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) FindRegistrationForId( 454f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) kRegistrationId, kScope.GetOrigin(), &found_registration)); 4551320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci EXPECT_FALSE(found_registration.get()); 4560529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch 457010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) // Deleting an unstored registration should succeed. 458f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) EXPECT_EQ(SERVICE_WORKER_OK, 459f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) DeleteRegistration(kRegistrationId + 1, kScope.GetOrigin())); 4600529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch} 4610529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch 4620529e5d033099cbfc42635f6f6183833b09dff6eBen MurdochTEST_F(ServiceWorkerStorageTest, InstallingRegistrationsAreFindable) { 4635f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) const GURL kScope("http://www.test.not/scope/"); 464cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) const GURL kScript("http://www.test.not/script.js"); 465cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) const GURL kDocumentUrl("http://www.test.not/scope/document.html"); 466010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) const int64 kRegistrationId = 0; 467010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) const int64 kVersionId = 0; 4680529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch 4690529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch scoped_refptr<ServiceWorkerRegistration> found_registration; 4700529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch 4710529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch // Create an unstored registration. 4720529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch scoped_refptr<ServiceWorkerRegistration> live_registration = 4730529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch new ServiceWorkerRegistration( 4741320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci kScope, kRegistrationId, context_ptr_); 4750529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch scoped_refptr<ServiceWorkerVersion> live_version = 4760529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch new ServiceWorkerVersion( 4771320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci live_registration.get(), kScript, kVersionId, context_ptr_); 4780529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch live_version->SetStatus(ServiceWorkerVersion::INSTALLING); 4791320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci live_registration->SetWaitingVersion(live_version.get()); 4800529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch 4810529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch // Should not be findable, including by GetAllRegistrations. 482f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, 483f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) FindRegistrationForId( 484f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) kRegistrationId, kScope.GetOrigin(), &found_registration)); 4851320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci EXPECT_FALSE(found_registration.get()); 486f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) 487f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, 488f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) FindRegistrationForDocument(kDocumentUrl, &found_registration)); 4891320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci EXPECT_FALSE(found_registration.get()); 490f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) 491f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, 492f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) FindRegistrationForPattern(kScope, &found_registration)); 4931320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci EXPECT_FALSE(found_registration.get()); 494f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) 4950529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch std::vector<ServiceWorkerRegistrationInfo> all_registrations; 496f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) GetAllRegistrations(&all_registrations); 4970529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch EXPECT_TRUE(all_registrations.empty()); 4980529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch 4990529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch // Notify storage of it being installed. 5001320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci storage()->NotifyInstallingRegistration(live_registration.get()); 5010529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch 5020529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch // Now should be findable. 503f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) EXPECT_EQ(SERVICE_WORKER_OK, 504f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) FindRegistrationForId( 505f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) kRegistrationId, kScope.GetOrigin(), &found_registration)); 5060529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch EXPECT_EQ(live_registration, found_registration); 5070529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch found_registration = NULL; 508f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) 509f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) EXPECT_EQ(SERVICE_WORKER_OK, 510f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) FindRegistrationForDocument(kDocumentUrl, &found_registration)); 5110529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch EXPECT_EQ(live_registration, found_registration); 5120529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch found_registration = NULL; 513f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) 514f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) EXPECT_EQ(SERVICE_WORKER_OK, 515f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) FindRegistrationForPattern(kScope, &found_registration)); 5160529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch EXPECT_EQ(live_registration, found_registration); 5170529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch found_registration = NULL; 518f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) 519f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) GetAllRegistrations(&all_registrations); 5200529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch EXPECT_EQ(1u, all_registrations.size()); 5210529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch all_registrations.clear(); 5220529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch 5230529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch // Notify storage of installation no longer happening. 524cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) storage()->NotifyDoneInstallingRegistration( 5251320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci live_registration.get(), NULL, SERVICE_WORKER_OK); 5260529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch 5270529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch // Once again, should not be findable. 528f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, 529f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) FindRegistrationForId( 530f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) kRegistrationId, kScope.GetOrigin(), &found_registration)); 5311320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci EXPECT_FALSE(found_registration.get()); 532f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) 533f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, 534f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) FindRegistrationForDocument(kDocumentUrl, &found_registration)); 5351320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci EXPECT_FALSE(found_registration.get()); 536f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) 537f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, 538f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) FindRegistrationForPattern(kScope, &found_registration)); 5391320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci EXPECT_FALSE(found_registration.get()); 5400529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch 541f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) GetAllRegistrations(&all_registrations); 5420529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch EXPECT_TRUE(all_registrations.empty()); 5430529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch} 5440529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch 545116680a4aac90f2aa7413d9095a592090648e557Ben Murdochclass ServiceWorkerResourceStorageTest : public ServiceWorkerStorageTest { 546116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch public: 547116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch virtual void SetUp() OVERRIDE { 548116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch ServiceWorkerStorageTest::SetUp(); 549116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 550116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch storage()->LazyInitialize(base::Bind(&base::DoNothing)); 551116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch base::RunLoop().RunUntilIdle(); 5525f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) scope_ = GURL("http://www.test.not/scope/"); 553116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch script_ = GURL("http://www.test.not/script.js"); 554116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch import_ = GURL("http://www.test.not/import.js"); 555116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch document_url_ = GURL("http://www.test.not/scope/document.html"); 556116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch registration_id_ = storage()->NewRegistrationId(); 557116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch version_id_ = storage()->NewVersionId(); 558116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch resource_id1_ = storage()->NewResourceId(); 559116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch resource_id2_ = storage()->NewResourceId(); 560116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 561116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Cons up a new registration+version with two script resources. 562116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch RegistrationData data; 563116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch data.registration_id = registration_id_; 564116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch data.scope = scope_; 565116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch data.script = script_; 566116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch data.version_id = version_id_; 567116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch data.is_active = false; 568116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch std::vector<ResourceRecord> resources; 569116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch resources.push_back(ResourceRecord(resource_id1_, script_)); 570116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch resources.push_back(ResourceRecord(resource_id2_, import_)); 571116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch registration_ = storage()->GetOrCreateRegistration(data, resources); 572116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch registration_->waiting_version()->SetStatus(ServiceWorkerVersion::NEW); 573116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 574116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Add the resources ids to the uncommitted list. 575116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch storage()->StoreUncommittedResponseId(resource_id1_); 576116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch storage()->StoreUncommittedResponseId(resource_id2_); 577116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch base::RunLoop().RunUntilIdle(); 578116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch std::set<int64> verify_ids; 579116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, 580116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch storage()->database_->GetUncommittedResourceIds(&verify_ids)); 581116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch EXPECT_EQ(2u, verify_ids.size()); 582116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 583116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // And dump something in the disk cache for them. 584116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch WriteBasicResponse(storage(), resource_id1_); 585116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch WriteBasicResponse(storage(), resource_id2_); 586116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch EXPECT_TRUE(VerifyBasicResponse(storage(), resource_id1_, true)); 587116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch EXPECT_TRUE(VerifyBasicResponse(storage(), resource_id2_, true)); 588116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 589116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Storing the registration/version should take the resources ids out 590116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // of the uncommitted list. 591116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch EXPECT_EQ( 592116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch SERVICE_WORKER_OK, 593116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch StoreRegistration(registration_, registration_->waiting_version())); 594116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch verify_ids.clear(); 595116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, 596116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch storage()->database_->GetUncommittedResourceIds(&verify_ids)); 597116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch EXPECT_TRUE(verify_ids.empty()); 598116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch } 599116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 600116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch protected: 601116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch GURL scope_; 602116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch GURL script_; 603116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch GURL import_; 604116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch GURL document_url_; 605116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch int64 registration_id_; 606116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch int64 version_id_; 607116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch int64 resource_id1_; 608116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch int64 resource_id2_; 609116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch scoped_refptr<ServiceWorkerRegistration> registration_; 610116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}; 611116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 612116680a4aac90f2aa7413d9095a592090648e557Ben Murdochclass ServiceWorkerResourceStorageDiskTest 613116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch : public ServiceWorkerResourceStorageTest { 614116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch public: 615116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch virtual void SetUp() OVERRIDE { 616116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch ASSERT_TRUE(user_data_directory_.CreateUniqueTempDir()); 617116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch ServiceWorkerResourceStorageTest::SetUp(); 618116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch } 619116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 620116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch virtual base::FilePath GetUserDataDirectory() OVERRIDE { 621116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch return user_data_directory_.path(); 622116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch } 623116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 624116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch protected: 625116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch base::ScopedTempDir user_data_directory_; 626116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}; 627116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 628116680a4aac90f2aa7413d9095a592090648e557Ben MurdochTEST_F(ServiceWorkerResourceStorageTest, DeleteRegistration_NoLiveVersion) { 629116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch bool was_called = false; 630116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch ServiceWorkerStatusCode result = SERVICE_WORKER_ERROR_FAILED; 631116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch std::set<int64> verify_ids; 632116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 633116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch registration_->SetWaitingVersion(NULL); 634116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch registration_ = NULL; 635116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 636116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Deleting the registration should result in the resources being added to the 637116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // purgeable list and then doomed in the disk cache and removed from that 638116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // list. 639116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch storage()->DeleteRegistration( 640116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch registration_id_, 641116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch scope_.GetOrigin(), 642116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch base::Bind(&VerifyPurgeableListStatusCallback, 643116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch base::Unretained(storage()->database_.get()), 644116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch &verify_ids, 645116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch &was_called, 646116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch &result)); 647cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) base::RunLoop().RunUntilIdle(); 648116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch ASSERT_TRUE(was_called); 649116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch EXPECT_EQ(SERVICE_WORKER_OK, result); 650116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch EXPECT_EQ(2u, verify_ids.size()); 651116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch verify_ids.clear(); 652cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, 653116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch storage()->database_->GetPurgeableResourceIds(&verify_ids)); 654116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch EXPECT_TRUE(verify_ids.empty()); 655cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 656116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch EXPECT_FALSE(VerifyBasicResponse(storage(), resource_id1_, false)); 657116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch EXPECT_FALSE(VerifyBasicResponse(storage(), resource_id2_, false)); 658116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch} 659cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 660116680a4aac90f2aa7413d9095a592090648e557Ben MurdochTEST_F(ServiceWorkerResourceStorageTest, DeleteRegistration_WaitingVersion) { 661116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch bool was_called = false; 662116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch ServiceWorkerStatusCode result = SERVICE_WORKER_ERROR_FAILED; 663cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) std::set<int64> verify_ids; 664116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 665116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Deleting the registration should result in the resources being added to the 666116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // purgeable list and then doomed in the disk cache and removed from that 667116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // list. 668116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch storage()->DeleteRegistration( 669116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch registration_->id(), 670116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch scope_.GetOrigin(), 671116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch base::Bind(&VerifyPurgeableListStatusCallback, 672116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch base::Unretained(storage()->database_.get()), 673116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch &verify_ids, 674116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch &was_called, 675116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch &result)); 676116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch base::RunLoop().RunUntilIdle(); 677116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch ASSERT_TRUE(was_called); 678116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch EXPECT_EQ(SERVICE_WORKER_OK, result); 679116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch EXPECT_EQ(2u, verify_ids.size()); 680116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch verify_ids.clear(); 681cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, 682116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch storage()->database_->GetPurgeableResourceIds(&verify_ids)); 683116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch EXPECT_EQ(2u, verify_ids.size()); 684116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 685116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch EXPECT_TRUE(VerifyBasicResponse(storage(), resource_id1_, false)); 686116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch EXPECT_TRUE(VerifyBasicResponse(storage(), resource_id2_, false)); 687116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 688116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Doom the version, now it happens. 689116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch registration_->waiting_version()->Doom(); 690116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch base::RunLoop().RunUntilIdle(); 691116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch EXPECT_EQ(SERVICE_WORKER_OK, result); 692116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch EXPECT_EQ(2u, verify_ids.size()); 693116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch verify_ids.clear(); 694116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, 695116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch storage()->database_->GetPurgeableResourceIds(&verify_ids)); 696cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) EXPECT_TRUE(verify_ids.empty()); 697cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 698116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch EXPECT_FALSE(VerifyBasicResponse(storage(), resource_id1_, false)); 699116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch EXPECT_FALSE(VerifyBasicResponse(storage(), resource_id2_, false)); 700116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch} 701116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 702116680a4aac90f2aa7413d9095a592090648e557Ben MurdochTEST_F(ServiceWorkerResourceStorageTest, DeleteRegistration_ActiveVersion) { 703116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Promote the worker to active and add a controllee. 704116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch registration_->SetActiveVersion(registration_->waiting_version()); 705116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch storage()->UpdateToActiveState( 7061320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci registration_.get(), base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); 707116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch scoped_ptr<ServiceWorkerProviderHost> host( 708116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch new ServiceWorkerProviderHost(33 /* dummy render process id */, 709116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 1 /* dummy provider_id */, 710116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch context_->AsWeakPtr(), 711116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch NULL)); 712116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch registration_->active_version()->AddControllee(host.get()); 713116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 714f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) bool was_called = false; 715f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) ServiceWorkerStatusCode result = SERVICE_WORKER_ERROR_FAILED; 716116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch std::set<int64> verify_ids; 717116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 718116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Deleting the registration should move the resources to the purgeable list 719116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // but keep them available. 720116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch storage()->DeleteRegistration( 721116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch registration_->id(), 722116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch scope_.GetOrigin(), 723116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch base::Bind(&VerifyPurgeableListStatusCallback, 724116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch base::Unretained(storage()->database_.get()), 725116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch &verify_ids, 726116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch &was_called, 727116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch &result)); 728116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch registration_->active_version()->Doom(); 729116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch base::RunLoop().RunUntilIdle(); 730116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch ASSERT_TRUE(was_called); 731116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch EXPECT_EQ(SERVICE_WORKER_OK, result); 732116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch EXPECT_EQ(2u, verify_ids.size()); 733cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) verify_ids.clear(); 734116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, 735116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch storage()->database_->GetPurgeableResourceIds(&verify_ids)); 736116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch EXPECT_EQ(2u, verify_ids.size()); 737116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 738116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch EXPECT_TRUE(VerifyBasicResponse(storage(), resource_id1_, true)); 739116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch EXPECT_TRUE(VerifyBasicResponse(storage(), resource_id2_, true)); 740116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 741116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Removing the controllee should cause the resources to be deleted. 742116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch registration_->active_version()->RemoveControllee(host.get()); 743116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch base::RunLoop().RunUntilIdle(); 744116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch verify_ids.clear(); 745116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, 746116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch storage()->database_->GetPurgeableResourceIds(&verify_ids)); 747116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch EXPECT_TRUE(verify_ids.empty()); 748116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 749116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch EXPECT_FALSE(VerifyBasicResponse(storage(), resource_id1_, false)); 750116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch EXPECT_FALSE(VerifyBasicResponse(storage(), resource_id2_, false)); 751116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch} 752116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 753116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// Android has flaky IO error: http://crbug.com/387045 754116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#if defined(OS_ANDROID) 755116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#define MAYBE_CleanupOnRestart DISABLED_CleanupOnRestart 756116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#else 757116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#define MAYBE_CleanupOnRestart CleanupOnRestart 758116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#endif 759116680a4aac90f2aa7413d9095a592090648e557Ben MurdochTEST_F(ServiceWorkerResourceStorageDiskTest, MAYBE_CleanupOnRestart) { 760116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Promote the worker to active and add a controllee. 761116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch registration_->SetActiveVersion(registration_->waiting_version()); 762116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch registration_->SetWaitingVersion(NULL); 763116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch storage()->UpdateToActiveState( 7641320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci registration_.get(), base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); 765116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch scoped_ptr<ServiceWorkerProviderHost> host( 766116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch new ServiceWorkerProviderHost(33 /* dummy render process id */, 767116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 1 /* dummy provider_id */, 768116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch context_->AsWeakPtr(), 769116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch NULL)); 770116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch registration_->active_version()->AddControllee(host.get()); 771116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 772116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch bool was_called = false; 773116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch ServiceWorkerStatusCode result = SERVICE_WORKER_ERROR_FAILED; 774116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch std::set<int64> verify_ids; 775116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 776116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Deleting the registration should move the resources to the purgeable list 777116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // but keep them available. 778cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) storage()->DeleteRegistration( 779116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch registration_->id(), 780116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch scope_.GetOrigin(), 781cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) base::Bind(&VerifyPurgeableListStatusCallback, 782cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) base::Unretained(storage()->database_.get()), 783116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch &verify_ids, 784116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch &was_called, 785116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch &result)); 786cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) base::RunLoop().RunUntilIdle(); 787cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) ASSERT_TRUE(was_called); 788cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) EXPECT_EQ(SERVICE_WORKER_OK, result); 789cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) EXPECT_EQ(2u, verify_ids.size()); 790cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) verify_ids.clear(); 791cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, 792cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) storage()->database_->GetPurgeableResourceIds(&verify_ids)); 793116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch EXPECT_EQ(2u, verify_ids.size()); 794116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 795116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch EXPECT_TRUE(VerifyBasicResponse(storage(), resource_id1_, true)); 796116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch EXPECT_TRUE(VerifyBasicResponse(storage(), resource_id2_, true)); 797116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 798116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Also add an uncommitted resource. 799116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch int64 kStaleUncommittedResourceId = storage()->NewResourceId(); 800116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch storage()->StoreUncommittedResponseId(kStaleUncommittedResourceId); 801116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch base::RunLoop().RunUntilIdle(); 802116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch verify_ids.clear(); 803116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, 804116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch storage()->database_->GetUncommittedResourceIds(&verify_ids)); 805116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch EXPECT_EQ(1u, verify_ids.size()); 806116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch WriteBasicResponse(storage(), kStaleUncommittedResourceId); 807116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch EXPECT_TRUE( 808116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch VerifyBasicResponse(storage(), kStaleUncommittedResourceId, true)); 809116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 810116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Simulate browser shutdown. The purgeable and uncommitted resources are now 811116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // stale. 812116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch context_.reset(); 81303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) context_.reset( 81403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) new ServiceWorkerContextCore(GetUserDataDirectory(), 81503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) base::ThreadTaskRunnerHandle::Get(), 81603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) base::ThreadTaskRunnerHandle::Get(), 81703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) base::ThreadTaskRunnerHandle::Get(), 81803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) NULL, 81903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) NULL, 82003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) NULL)); 821116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch storage()->LazyInitialize(base::Bind(&base::DoNothing)); 822116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch base::RunLoop().RunUntilIdle(); 823116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 824116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Store a new uncommitted resource. This triggers stale resource cleanup. 825116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch int64 kNewResourceId = storage()->NewResourceId(); 826116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch WriteBasicResponse(storage(), kNewResourceId); 827116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch storage()->StoreUncommittedResponseId(kNewResourceId); 828116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch base::RunLoop().RunUntilIdle(); 829116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 830116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // The stale resources should be purged, but the new resource should persist. 831116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch verify_ids.clear(); 832116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, 833116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch storage()->database_->GetUncommittedResourceIds(&verify_ids)); 834116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch ASSERT_EQ(1u, verify_ids.size()); 835116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch EXPECT_EQ(kNewResourceId, *verify_ids.begin()); 836116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 837116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch verify_ids.clear(); 838116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, 839116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch storage()->database_->GetPurgeableResourceIds(&verify_ids)); 840116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch EXPECT_TRUE(verify_ids.empty()); 841116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch EXPECT_FALSE(VerifyBasicResponse(storage(), resource_id1_, false)); 842116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch EXPECT_FALSE(VerifyBasicResponse(storage(), resource_id2_, false)); 843116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch EXPECT_FALSE( 844116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch VerifyBasicResponse(storage(), kStaleUncommittedResourceId, false)); 845116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch EXPECT_TRUE(VerifyBasicResponse(storage(), kNewResourceId, true)); 846116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch} 847116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 848116680a4aac90f2aa7413d9095a592090648e557Ben MurdochTEST_F(ServiceWorkerResourceStorageTest, UpdateRegistration) { 849116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Promote the worker to active worker and add a controllee. 850116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch registration_->SetActiveVersion(registration_->waiting_version()); 851116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch storage()->UpdateToActiveState( 8521320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci registration_.get(), base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); 853116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch scoped_ptr<ServiceWorkerProviderHost> host( 854116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch new ServiceWorkerProviderHost(33 /* dummy render process id */, 855116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 1 /* dummy provider_id */, 856116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch context_->AsWeakPtr(), 857116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch NULL)); 858116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch registration_->active_version()->AddControllee(host.get()); 859116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 860116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch bool was_called = false; 861116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch ServiceWorkerStatusCode result = SERVICE_WORKER_ERROR_FAILED; 862116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch std::set<int64> verify_ids; 863116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 864116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Make an updated registration. 865116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch scoped_refptr<ServiceWorkerVersion> live_version = new ServiceWorkerVersion( 8661320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci registration_.get(), script_, storage()->NewVersionId(), context_ptr_); 867116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch live_version->SetStatus(ServiceWorkerVersion::NEW); 8681320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci registration_->SetWaitingVersion(live_version.get()); 869116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 870116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Writing the registration should move the old version's resources to the 871116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // purgeable list but keep them available. 872116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch storage()->StoreRegistration( 8731320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci registration_.get(), 874116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch registration_->waiting_version(), 875116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch base::Bind(&VerifyPurgeableListStatusCallback, 876116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch base::Unretained(storage()->database_.get()), 877116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch &verify_ids, 878116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch &was_called, 879116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch &result)); 880116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch registration_->active_version()->Doom(); 881116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch base::RunLoop().RunUntilIdle(); 882116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch ASSERT_TRUE(was_called); 883116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch EXPECT_EQ(SERVICE_WORKER_OK, result); 884116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch EXPECT_EQ(2u, verify_ids.size()); 885116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch verify_ids.clear(); 886116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, 887116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch storage()->database_->GetPurgeableResourceIds(&verify_ids)); 888116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch EXPECT_EQ(2u, verify_ids.size()); 889116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 890116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch EXPECT_TRUE(VerifyBasicResponse(storage(), resource_id1_, false)); 891116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch EXPECT_TRUE(VerifyBasicResponse(storage(), resource_id2_, false)); 892116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 893116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Removing the controllee should cause the old version's resources to be 894116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // deleted. 895116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch registration_->active_version()->RemoveControllee(host.get()); 896116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch base::RunLoop().RunUntilIdle(); 897116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch verify_ids.clear(); 898116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, 899116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch storage()->database_->GetPurgeableResourceIds(&verify_ids)); 900cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) EXPECT_TRUE(verify_ids.empty()); 901cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 902116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch EXPECT_FALSE(VerifyBasicResponse(storage(), resource_id1_, false)); 903116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch EXPECT_FALSE(VerifyBasicResponse(storage(), resource_id2_, false)); 904cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)} 905cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 90646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)TEST_F(ServiceWorkerStorageTest, FindRegistration_LongestScopeMatch) { 90746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) const GURL kDocumentUrl("http://www.example.com/scope/foo"); 90846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) scoped_refptr<ServiceWorkerRegistration> found_registration; 90946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) 9105f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) // Registration for "/scope/". 9115f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) const GURL kScope1("http://www.example.com/scope/"); 91246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) const GURL kScript1("http://www.example.com/script1.js"); 91346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) const int64 kRegistrationId1 = 1; 91446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) const int64 kVersionId1 = 1; 91546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) scoped_refptr<ServiceWorkerRegistration> live_registration1 = 91646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) new ServiceWorkerRegistration( 9171320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci kScope1, kRegistrationId1, context_ptr_); 91846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) scoped_refptr<ServiceWorkerVersion> live_version1 = 91946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) new ServiceWorkerVersion( 9201320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci live_registration1.get(), kScript1, kVersionId1, context_ptr_); 92146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) live_version1->SetStatus(ServiceWorkerVersion::INSTALLED); 9221320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci live_registration1->SetWaitingVersion(live_version1.get()); 92346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) 9245f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) // Registration for "/scope/foo". 9255f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) const GURL kScope2("http://www.example.com/scope/foo"); 92646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) const GURL kScript2("http://www.example.com/script2.js"); 92746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) const int64 kRegistrationId2 = 2; 92846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) const int64 kVersionId2 = 2; 92946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) scoped_refptr<ServiceWorkerRegistration> live_registration2 = 93046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) new ServiceWorkerRegistration( 9311320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci kScope2, kRegistrationId2, context_ptr_); 93246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) scoped_refptr<ServiceWorkerVersion> live_version2 = 93346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) new ServiceWorkerVersion( 9341320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci live_registration2.get(), kScript2, kVersionId2, context_ptr_); 93546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) live_version2->SetStatus(ServiceWorkerVersion::INSTALLED); 9361320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci live_registration2->SetWaitingVersion(live_version2.get()); 93746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) 9385f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) // Registration for "/scope/foobar". 9395f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) const GURL kScope3("http://www.example.com/scope/foobar"); 94046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) const GURL kScript3("http://www.example.com/script3.js"); 94146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) const int64 kRegistrationId3 = 3; 94246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) const int64 kVersionId3 = 3; 94346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) scoped_refptr<ServiceWorkerRegistration> live_registration3 = 94446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) new ServiceWorkerRegistration( 9451320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci kScope3, kRegistrationId3, context_ptr_); 94646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) scoped_refptr<ServiceWorkerVersion> live_version3 = 94746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) new ServiceWorkerVersion( 9481320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci live_registration3.get(), kScript3, kVersionId3, context_ptr_); 94946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) live_version3->SetStatus(ServiceWorkerVersion::INSTALLED); 9501320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci live_registration3->SetWaitingVersion(live_version3.get()); 95146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) 95246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) // Notify storage of they being installed. 9531320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci storage()->NotifyInstallingRegistration(live_registration1.get()); 9541320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci storage()->NotifyInstallingRegistration(live_registration2.get()); 9551320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci storage()->NotifyInstallingRegistration(live_registration3.get()); 95646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) 95746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) // Find a registration among installing ones. 958f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) EXPECT_EQ(SERVICE_WORKER_OK, 959f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) FindRegistrationForDocument(kDocumentUrl, &found_registration)); 96046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) EXPECT_EQ(live_registration2, found_registration); 96146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) found_registration = NULL; 96246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) 96346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) // Store registrations. 964f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) EXPECT_EQ(SERVICE_WORKER_OK, 965f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) StoreRegistration(live_registration1, live_version1)); 966f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) EXPECT_EQ(SERVICE_WORKER_OK, 967f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) StoreRegistration(live_registration2, live_version2)); 968f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) EXPECT_EQ(SERVICE_WORKER_OK, 969f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) StoreRegistration(live_registration3, live_version3)); 97046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) 97146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) // Notify storage of installations no longer happening. 97246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) storage()->NotifyDoneInstallingRegistration( 9731320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci live_registration1.get(), NULL, SERVICE_WORKER_OK); 97446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) storage()->NotifyDoneInstallingRegistration( 9751320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci live_registration2.get(), NULL, SERVICE_WORKER_OK); 97646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) storage()->NotifyDoneInstallingRegistration( 9771320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci live_registration3.get(), NULL, SERVICE_WORKER_OK); 97846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) 97946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) // Find a registration among installed ones. 980f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) EXPECT_EQ(SERVICE_WORKER_OK, 981f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) FindRegistrationForDocument(kDocumentUrl, &found_registration)); 98246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) EXPECT_EQ(live_registration2, found_registration); 98346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)} 98446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) 9855f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)TEST_F(ServiceWorkerStorageTest, CompareResources) { 9865f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) // Compare two small responses containing the same data. 9875f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) WriteBasicResponse(storage(), 1); 9885f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) WriteBasicResponse(storage(), 2); 9895f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) ServiceWorkerStatusCode status = static_cast<ServiceWorkerStatusCode>(-1); 9905f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) bool are_equal = false; 9915f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) storage()->CompareScriptResources( 9925f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 1, 2, 9935f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) base::Bind(&OnCompareComplete, &status, &are_equal)); 9945f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) base::RunLoop().RunUntilIdle(); 9955f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) EXPECT_EQ(SERVICE_WORKER_OK, status); 9965f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) EXPECT_TRUE(are_equal); 9975f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 9985f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) // Compare two small responses with different data. 9995f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) const char kHttpHeaders[] = "HTTP/1.0 200 HONKYDORY\0\0"; 10005f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) const char kHttpBody[] = "Goodbye"; 10015f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) std::string headers(kHttpHeaders, arraysize(kHttpHeaders)); 10025f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) WriteStringResponse(storage(), 3, headers, std::string(kHttpBody)); 10035f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) status = static_cast<ServiceWorkerStatusCode>(-1); 10045f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) are_equal = true; 10055f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) storage()->CompareScriptResources( 10065f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 1, 3, 10075f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) base::Bind(&OnCompareComplete, &status, &are_equal)); 10085f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) base::RunLoop().RunUntilIdle(); 10095f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) EXPECT_EQ(SERVICE_WORKER_OK, status); 10105f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) EXPECT_FALSE(are_equal); 10115f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 10125f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) // Compare two large responses with the same data. 10135f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) const int k32K = 32 * 1024; 10145f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) WriteResponseOfSize(storage(), 4, 'a', k32K); 10155f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) WriteResponseOfSize(storage(), 5, 'a', k32K); 10165f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) status = static_cast<ServiceWorkerStatusCode>(-1); 10175f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) are_equal = false; 10185f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) storage()->CompareScriptResources( 10195f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 4, 5, 10205f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) base::Bind(&OnCompareComplete, &status, &are_equal)); 10215f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) base::RunLoop().RunUntilIdle(); 10225f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) EXPECT_EQ(SERVICE_WORKER_OK, status); 10235f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) EXPECT_TRUE(are_equal); 10245f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 10255f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) // Compare a large and small response. 10265f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) status = static_cast<ServiceWorkerStatusCode>(-1); 10275f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) are_equal = true; 10285f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) storage()->CompareScriptResources( 10295f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 1, 5, 10305f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) base::Bind(&OnCompareComplete, &status, &are_equal)); 10315f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) base::RunLoop().RunUntilIdle(); 10325f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) EXPECT_EQ(SERVICE_WORKER_OK, status); 10335f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) EXPECT_FALSE(are_equal); 10345f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 10355f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) // Compare two large responses with different data. 10365f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) WriteResponseOfSize(storage(), 6, 'b', k32K); 10375f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) status = static_cast<ServiceWorkerStatusCode>(-1); 10385f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) are_equal = true; 10395f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) storage()->CompareScriptResources( 10405f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 5, 6, 10415f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) base::Bind(&OnCompareComplete, &status, &are_equal)); 10425f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) base::RunLoop().RunUntilIdle(); 10435f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) EXPECT_EQ(SERVICE_WORKER_OK, status); 10445f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) EXPECT_FALSE(are_equal); 10455f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)} 10465f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 1047a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)} // namespace content 1048