Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / content / browser / appcache / appcache_storage_impl_unittest.cc
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <stack>
6
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/callback.h"
10 #include "base/file_util.h"
11 #include "base/files/scoped_temp_dir.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/message_loop/message_loop.h"
14 #include "base/synchronization/waitable_event.h"
15 #include "base/threading/thread.h"
16 #include "content/browser/appcache/appcache_interceptor.h"
17 #include "net/base/net_errors.h"
18 #include "net/base/request_priority.h"
19 #include "net/http/http_response_headers.h"
20 #include "net/url_request/url_request_error_job.h"
21 #include "net/url_request/url_request_job_factory_impl.h"
22 #include "net/url_request/url_request_test_job.h"
23 #include "net/url_request/url_request_test_util.h"
24 #include "sql/test/test_helpers.h"
25 #include "testing/gtest/include/gtest/gtest.h"
26 #include "webkit/browser/appcache/appcache.h"
27 #include "webkit/browser/appcache/appcache_backend_impl.h"
28 #include "webkit/browser/appcache/appcache_database.h"
29 #include "webkit/browser/appcache/appcache_entry.h"
30 #include "webkit/browser/appcache/appcache_group.h"
31 #include "webkit/browser/appcache/appcache_host.h"
32 #include "webkit/browser/appcache/appcache_request_handler.h"
33 #include "webkit/browser/appcache/appcache_service.h"
34 #include "webkit/browser/appcache/appcache_storage_impl.h"
35 #include "webkit/browser/quota/quota_manager.h"
36
37 using appcache::FALLBACK_NAMESPACE;
38 using appcache::NETWORK_NAMESPACE;
39 using appcache::AppCacheBackendImpl;
40 using appcache::AppCacheDatabase;
41 using appcache::AppCacheEntry;
42 using appcache::AppCacheFrontend;
43 using appcache::AppCacheHost;
44 using appcache::AppCacheInfo;
45 using appcache::AppCacheGroup;
46 using appcache::AppCacheService;
47 using appcache::AppCacheStorage;
48 using appcache::AppCacheStorageImpl;
49 using appcache::AppCacheStorageReference;
50 using appcache::AppCache;
51 using appcache::ErrorDetails;
52 using appcache::EventID;
53 using appcache::kNoCacheId;
54 using appcache::kNoResponseId;
55 using appcache::INTERCEPT_NAMESPACE;
56 using appcache::LogLevel;
57 using appcache::Namespace;
58 using appcache::Status;
59
60 namespace content {
61
62 namespace {
63
64 const base::Time kZeroTime;
65 const GURL kManifestUrl("http://blah/manifest");
66 const GURL kManifestUrl2("http://blah/manifest2");
67 const GURL kManifestUrl3("http://blah/manifest3");
68 const GURL kEntryUrl("http://blah/entry");
69 const GURL kEntryUrl2("http://blah/entry2");
70 const GURL kFallbackNamespace("http://blah/fallback_namespace/");
71 const GURL kFallbackNamespace2("http://blah/fallback_namespace/longer");
72 const GURL kFallbackTestUrl("http://blah/fallback_namespace/longer/test");
73 const GURL kOnlineNamespace("http://blah/online_namespace");
74 const GURL kOnlineNamespaceWithinFallback(
75     "http://blah/fallback_namespace/online/");
76 const GURL kInterceptNamespace("http://blah/intercept_namespace/");
77 const GURL kInterceptNamespace2("http://blah/intercept_namespace/longer/");
78 const GURL kInterceptTestUrl("http://blah/intercept_namespace/longer/test");
79 const GURL kInterceptPatternNamespace("http://blah/intercept_pattern/*/bar");
80 const GURL kInterceptPatternTestPositiveUrl(
81     "http://blah/intercept_pattern/foo/bar");
82 const GURL kInterceptPatternTestNegativeUrl(
83     "http://blah/intercept_pattern/foo/not_bar");
84 const GURL kFallbackPatternNamespace("http://blah/fallback_pattern/*/bar");
85 const GURL kFallbackPatternTestPositiveUrl(
86     "http://blah/fallback_pattern/foo/bar");
87 const GURL kFallbackPatternTestNegativeUrl(
88     "http://blah/fallback_pattern/foo/not_bar");
89 const GURL kOrigin(kManifestUrl.GetOrigin());
90
91 const int kManifestEntryIdOffset = 100;
92 const int kFallbackEntryIdOffset = 1000;
93
94 const GURL kDefaultEntryUrl("http://blah/makecacheandgroup_default_entry");
95 const int kDefaultEntrySize = 10;
96 const int kDefaultEntryIdOffset = 12345;
97
98 const int kMockQuota = 5000;
99
100 // The Reinitialize test needs some http accessible resources to run,
101 // we mock stuff inprocess for that.
102 class MockHttpServer {
103  public:
104   static GURL GetMockUrl(const std::string& path) {
105     return GURL("http://mockhost/" + path);
106   }
107
108   static net::URLRequestJob* CreateJob(
109       net::URLRequest* request, net::NetworkDelegate* network_delegate) {
110     if (request->url().host() != "mockhost")
111       return new net::URLRequestErrorJob(request, network_delegate, -100);
112
113     std::string headers, body;
114     GetMockResponse(request->url().path(), &headers, &body);
115     return new net::URLRequestTestJob(
116         request, network_delegate, headers, body, true);
117   }
118
119  private:
120   static void GetMockResponse(const std::string& path,
121                               std::string* headers,
122                               std::string* body) {
123     const char manifest_headers[] =
124         "HTTP/1.1 200 OK\0"
125         "Content-type: text/cache-manifest\0"
126         "\0";
127     const char page_headers[] =
128         "HTTP/1.1 200 OK\0"
129         "Content-type: text/html\0"
130         "\0";
131     const char not_found_headers[] =
132         "HTTP/1.1 404 NOT FOUND\0"
133         "\0";
134
135     if (path == "/manifest") {
136       (*headers) = std::string(manifest_headers, arraysize(manifest_headers));
137       (*body) = "CACHE MANIFEST\n";
138     } else if (path == "/empty.html") {
139       (*headers) = std::string(page_headers, arraysize(page_headers));
140       (*body) = "";
141     } else {
142       (*headers) = std::string(not_found_headers,
143                                arraysize(not_found_headers));
144       (*body) = "";
145     }
146   }
147 };
148
149 class MockHttpServerJobFactory
150     : public net::URLRequestJobFactory::ProtocolHandler {
151  public:
152   virtual net::URLRequestJob* MaybeCreateJob(
153       net::URLRequest* request,
154       net::NetworkDelegate* network_delegate) const OVERRIDE {
155     return MockHttpServer::CreateJob(request, network_delegate);
156   }
157 };
158
159 class IOThread : public base::Thread {
160  public:
161   explicit IOThread(const char* name)
162       : base::Thread(name) {
163   }
164
165   virtual ~IOThread() {
166     Stop();
167   }
168
169   net::URLRequestContext* request_context() {
170     return request_context_.get();
171   }
172
173   virtual void Init() OVERRIDE {
174     scoped_ptr<net::URLRequestJobFactoryImpl> factory(
175         new net::URLRequestJobFactoryImpl());
176     factory->SetProtocolHandler("http", new MockHttpServerJobFactory);
177     job_factory_ = factory.Pass();
178     request_context_.reset(new net::TestURLRequestContext());
179     request_context_->set_job_factory(job_factory_.get());
180     AppCacheInterceptor::EnsureRegistered();
181   }
182
183   virtual void CleanUp() OVERRIDE {
184     request_context_.reset();
185     job_factory_.reset();
186   }
187
188  private:
189   scoped_ptr<net::URLRequestJobFactory> job_factory_;
190   scoped_ptr<net::URLRequestContext> request_context_;
191 };
192
193 scoped_ptr<IOThread> io_thread;
194 scoped_ptr<base::Thread> db_thread;
195
196 }  // namespace
197
198 class AppCacheStorageImplTest : public testing::Test {
199  public:
200   class MockStorageDelegate : public AppCacheStorage::Delegate {
201    public:
202     explicit MockStorageDelegate(AppCacheStorageImplTest* test)
203         : loaded_cache_id_(0), stored_group_success_(false),
204           would_exceed_quota_(false), obsoleted_success_(false),
205           found_cache_id_(kNoCacheId), test_(test) {
206     }
207
208     virtual void OnCacheLoaded(AppCache* cache, int64 cache_id) OVERRIDE {
209       loaded_cache_ = cache;
210       loaded_cache_id_ = cache_id;
211       test_->ScheduleNextTask();
212     }
213
214     virtual void OnGroupLoaded(AppCacheGroup* group,
215                                const GURL& manifest_url) OVERRIDE {
216       loaded_group_ = group;
217       loaded_manifest_url_ = manifest_url;
218       loaded_groups_newest_cache_ = group ? group->newest_complete_cache()
219                                           : NULL;
220       test_->ScheduleNextTask();
221     }
222
223     virtual void OnGroupAndNewestCacheStored(
224         AppCacheGroup* group, AppCache* newest_cache, bool success,
225         bool would_exceed_quota) OVERRIDE {
226       stored_group_ = group;
227       stored_group_success_ = success;
228       would_exceed_quota_ = would_exceed_quota;
229       test_->ScheduleNextTask();
230     }
231
232     virtual void OnGroupMadeObsolete(AppCacheGroup* group,
233                                      bool success,
234                                      int response_code) OVERRIDE {
235       obsoleted_group_ = group;
236       obsoleted_success_ = success;
237       test_->ScheduleNextTask();
238     }
239
240     virtual void OnMainResponseFound(const GURL& url,
241                                      const AppCacheEntry& entry,
242                                      const GURL& namespace_entry_url,
243                                      const AppCacheEntry& fallback_entry,
244                                      int64 cache_id,
245                                      int64 group_id,
246                                      const GURL& manifest_url) OVERRIDE {
247       found_url_ = url;
248       found_entry_ = entry;
249       found_namespace_entry_url_ = namespace_entry_url;
250       found_fallback_entry_ = fallback_entry;
251       found_cache_id_ = cache_id;
252       found_group_id_ = group_id;
253       found_manifest_url_ = manifest_url;
254       test_->ScheduleNextTask();
255     }
256
257     scoped_refptr<AppCache> loaded_cache_;
258     int64 loaded_cache_id_;
259     scoped_refptr<AppCacheGroup> loaded_group_;
260     GURL loaded_manifest_url_;
261     scoped_refptr<AppCache> loaded_groups_newest_cache_;
262     scoped_refptr<AppCacheGroup> stored_group_;
263     bool stored_group_success_;
264     bool would_exceed_quota_;
265     scoped_refptr<AppCacheGroup> obsoleted_group_;
266     bool obsoleted_success_;
267     GURL found_url_;
268     AppCacheEntry found_entry_;
269     GURL found_namespace_entry_url_;
270     AppCacheEntry found_fallback_entry_;
271     int64 found_cache_id_;
272     int64 found_group_id_;
273     GURL found_manifest_url_;
274     AppCacheStorageImplTest* test_;
275   };
276
277   class MockQuotaManager : public quota::QuotaManager {
278    public:
279     MockQuotaManager()
280         : QuotaManager(true /* is_incognito */,
281                        base::FilePath(),
282                        io_thread->message_loop_proxy().get(),
283                        db_thread->message_loop_proxy().get(),
284                        NULL),
285           async_(false) {}
286
287     virtual void GetUsageAndQuota(
288         const GURL& origin,
289         quota::StorageType type,
290         const GetUsageAndQuotaCallback& callback) OVERRIDE {
291       EXPECT_EQ(quota::kStorageTypeTemporary, type);
292       if (async_) {
293         base::MessageLoop::current()->PostTask(
294             FROM_HERE,
295             base::Bind(&MockQuotaManager::CallCallback,
296                        base::Unretained(this),
297                        callback));
298         return;
299       }
300       CallCallback(callback);
301     }
302
303     void CallCallback(const GetUsageAndQuotaCallback& callback) {
304       callback.Run(quota::kQuotaStatusOk, 0, kMockQuota);
305     }
306
307     bool async_;
308
309    protected:
310     virtual ~MockQuotaManager() {}
311   };
312
313   class MockQuotaManagerProxy : public quota::QuotaManagerProxy {
314    public:
315     MockQuotaManagerProxy()
316         : QuotaManagerProxy(NULL, NULL),
317           notify_storage_accessed_count_(0),
318           notify_storage_modified_count_(0),
319           last_delta_(0),
320           mock_manager_(new MockQuotaManager) {
321       manager_ = mock_manager_.get();
322     }
323
324     virtual void NotifyStorageAccessed(quota::QuotaClient::ID client_id,
325                                        const GURL& origin,
326                                        quota::StorageType type) OVERRIDE {
327       EXPECT_EQ(quota::QuotaClient::kAppcache, client_id);
328       EXPECT_EQ(quota::kStorageTypeTemporary, type);
329       ++notify_storage_accessed_count_;
330       last_origin_ = origin;
331     }
332
333     virtual void NotifyStorageModified(quota::QuotaClient::ID client_id,
334                                        const GURL& origin,
335                                        quota::StorageType type,
336                                        int64 delta) OVERRIDE {
337       EXPECT_EQ(quota::QuotaClient::kAppcache, client_id);
338       EXPECT_EQ(quota::kStorageTypeTemporary, type);
339       ++notify_storage_modified_count_;
340       last_origin_ = origin;
341       last_delta_ = delta;
342     }
343
344     // Not needed for our tests.
345     virtual void RegisterClient(quota::QuotaClient* client) OVERRIDE {}
346     virtual void NotifyOriginInUse(const GURL& origin) OVERRIDE {}
347     virtual void NotifyOriginNoLongerInUse(const GURL& origin) OVERRIDE {}
348     virtual void SetUsageCacheEnabled(quota::QuotaClient::ID client_id,
349                                       const GURL& origin,
350                                       quota::StorageType type,
351                                       bool enabled) OVERRIDE {}
352     virtual void GetUsageAndQuota(
353         base::SequencedTaskRunner* original_task_runner,
354         const GURL& origin,
355         quota::StorageType type,
356         const GetUsageAndQuotaCallback& callback) OVERRIDE {}
357
358     int notify_storage_accessed_count_;
359     int notify_storage_modified_count_;
360     GURL last_origin_;
361     int last_delta_;
362     scoped_refptr<MockQuotaManager> mock_manager_;
363
364    protected:
365     virtual ~MockQuotaManagerProxy() {}
366   };
367
368   template <class Method>
369   void RunMethod(Method method) {
370     (this->*method)();
371   }
372
373   // Helper callback to run a test on our io_thread. The io_thread is spun up
374   // once and reused for all tests.
375   template <class Method>
376   void MethodWrapper(Method method) {
377     SetUpTest();
378
379     // Ensure InitTask execution prior to conducting a test.
380     FlushDbThreadTasks();
381
382     // We also have to wait for InitTask completion call to be performed
383     // on the IO thread prior to running the test. Its guaranteed to be
384     // queued by this time.
385     base::MessageLoop::current()->PostTask(
386         FROM_HERE,
387         base::Bind(&AppCacheStorageImplTest::RunMethod<Method>,
388                    base::Unretained(this),
389                    method));
390   }
391
392   static void SetUpTestCase() {
393     // We start both threads as TYPE_IO because we also use the db_thead
394     // for the disk_cache which needs to be of TYPE_IO.
395     base::Thread::Options options(base::MessageLoop::TYPE_IO, 0);
396     io_thread.reset(new IOThread("AppCacheTest.IOThread"));
397     ASSERT_TRUE(io_thread->StartWithOptions(options));
398     db_thread.reset(new base::Thread("AppCacheTest::DBThread"));
399     ASSERT_TRUE(db_thread->StartWithOptions(options));
400   }
401
402   static void TearDownTestCase() {
403     io_thread.reset(NULL);
404     db_thread.reset(NULL);
405   }
406
407   // Test harness --------------------------------------------------
408
409   AppCacheStorageImplTest() {
410   }
411
412   template <class Method>
413   void RunTestOnIOThread(Method method) {
414     test_finished_event_ .reset(new base::WaitableEvent(false, false));
415     io_thread->message_loop()->PostTask(
416         FROM_HERE, base::Bind(&AppCacheStorageImplTest::MethodWrapper<Method>,
417                               base::Unretained(this), method));
418     test_finished_event_->Wait();
419   }
420
421   void SetUpTest() {
422     DCHECK(base::MessageLoop::current() == io_thread->message_loop());
423     service_.reset(new AppCacheService(NULL));
424     service_->Initialize(
425         base::FilePath(), db_thread->message_loop_proxy().get(), NULL);
426     mock_quota_manager_proxy_ = new MockQuotaManagerProxy();
427     service_->quota_manager_proxy_ = mock_quota_manager_proxy_;
428     delegate_.reset(new MockStorageDelegate(this));
429   }
430
431   void TearDownTest() {
432     DCHECK(base::MessageLoop::current() == io_thread->message_loop());
433     storage()->CancelDelegateCallbacks(delegate());
434     group_ = NULL;
435     cache_ = NULL;
436     cache2_ = NULL;
437     mock_quota_manager_proxy_ = NULL;
438     delegate_.reset();
439     service_.reset();
440     FlushDbThreadTasks();
441   }
442
443   void TestFinished() {
444     // We unwind the stack prior to finishing up to let stack
445     // based objects get deleted.
446     DCHECK(base::MessageLoop::current() == io_thread->message_loop());
447     base::MessageLoop::current()->PostTask(
448         FROM_HERE,
449         base::Bind(&AppCacheStorageImplTest::TestFinishedUnwound,
450                    base::Unretained(this)));
451   }
452
453   void TestFinishedUnwound() {
454     TearDownTest();
455     test_finished_event_->Signal();
456   }
457
458   void PushNextTask(const base::Closure& task) {
459     task_stack_.push(task);
460   }
461
462   void ScheduleNextTask() {
463     DCHECK(base::MessageLoop::current() == io_thread->message_loop());
464     if (task_stack_.empty()) {
465       return;
466     }
467     base::MessageLoop::current()->PostTask(FROM_HERE, task_stack_.top());
468     task_stack_.pop();
469   }
470
471   static void SignalEvent(base::WaitableEvent* event) {
472     event->Signal();
473   }
474
475   void FlushDbThreadTasks() {
476     // We pump a task thru the db thread to ensure any tasks previously
477     // scheduled on that thread have been performed prior to return.
478     base::WaitableEvent event(false, false);
479     db_thread->message_loop()->PostTask(
480         FROM_HERE, base::Bind(&AppCacheStorageImplTest::SignalEvent, &event));
481     event.Wait();
482   }
483
484   // LoadCache_Miss ----------------------------------------------------
485
486   void LoadCache_Miss() {
487     // Attempt to load a cache that doesn't exist. Should
488     // complete asynchronously.
489     PushNextTask(base::Bind(&AppCacheStorageImplTest::Verify_LoadCache_Miss,
490                             base::Unretained(this)));
491
492     storage()->LoadCache(111, delegate());
493     EXPECT_NE(111, delegate()->loaded_cache_id_);
494   }
495
496   void Verify_LoadCache_Miss() {
497     EXPECT_EQ(111, delegate()->loaded_cache_id_);
498     EXPECT_FALSE(delegate()->loaded_cache_.get());
499     EXPECT_EQ(0, mock_quota_manager_proxy_->notify_storage_accessed_count_);
500     EXPECT_EQ(0, mock_quota_manager_proxy_->notify_storage_modified_count_);
501     TestFinished();
502   }
503
504   // LoadCache_NearHit -------------------------------------------------
505
506   void LoadCache_NearHit() {
507     // Attempt to load a cache that is currently in use
508     // and does not require loading from storage. This
509     // load should complete syncly.
510
511     // Setup some preconditions. Make an 'unstored' cache for
512     // us to load. The ctor should put it in the working set.
513     int64 cache_id = storage()->NewCacheId();
514     scoped_refptr<AppCache> cache(new AppCache(storage(), cache_id));
515
516     // Conduct the test.
517     storage()->LoadCache(cache_id, delegate());
518     EXPECT_EQ(cache_id, delegate()->loaded_cache_id_);
519     EXPECT_EQ(cache.get(), delegate()->loaded_cache_.get());
520     EXPECT_EQ(0, mock_quota_manager_proxy_->notify_storage_accessed_count_);
521     EXPECT_EQ(0, mock_quota_manager_proxy_->notify_storage_modified_count_);
522     TestFinished();
523   }
524
525   // CreateGroup  --------------------------------------------
526
527   void CreateGroupInEmptyOrigin() {
528     // Attempt to load a group that doesn't exist, one should
529     // be created for us, but not stored.
530
531     // Since the origin has no groups, the storage class will respond
532     // syncly.
533     storage()->LoadOrCreateGroup(kManifestUrl, delegate());
534     Verify_CreateGroup();
535   }
536
537   void CreateGroupInPopulatedOrigin() {
538     // Attempt to load a group that doesn't exist, one should
539     // be created for us, but not stored.
540     PushNextTask(base::Bind(&AppCacheStorageImplTest::Verify_CreateGroup,
541                             base::Unretained(this)));
542
543     // Since the origin has groups, storage class will have to
544     // consult the database and completion will be async.
545     storage()->usage_map_[kOrigin] = kDefaultEntrySize;
546
547     storage()->LoadOrCreateGroup(kManifestUrl, delegate());
548     EXPECT_FALSE(delegate()->loaded_group_.get());
549   }
550
551   void Verify_CreateGroup() {
552     EXPECT_EQ(kManifestUrl, delegate()->loaded_manifest_url_);
553     EXPECT_TRUE(delegate()->loaded_group_.get());
554     EXPECT_TRUE(delegate()->loaded_group_->HasOneRef());
555     EXPECT_FALSE(delegate()->loaded_group_->newest_complete_cache());
556
557     // Should not have been stored in the database.
558     AppCacheDatabase::GroupRecord record;
559     EXPECT_FALSE(database()->FindGroup(
560         delegate()->loaded_group_->group_id(), &record));
561
562     EXPECT_EQ(0, mock_quota_manager_proxy_->notify_storage_accessed_count_);
563     EXPECT_EQ(0, mock_quota_manager_proxy_->notify_storage_modified_count_);
564
565     TestFinished();
566   }
567
568   // LoadGroupAndCache_FarHit  --------------------------------------
569
570   void LoadGroupAndCache_FarHit() {
571     // Attempt to load a cache that is not currently in use
572     // and does require loading from disk. This
573     // load should complete asynchronously.
574     PushNextTask(base::Bind(&AppCacheStorageImplTest::Verify_LoadCache_Far_Hit,
575                             base::Unretained(this)));
576
577     // Setup some preconditions. Create a group and newest cache that
578     // appear to be "stored" and "not currently in use".
579     MakeCacheAndGroup(kManifestUrl, 1, 1, true);
580     group_ = NULL;
581     cache_ = NULL;
582
583     // Conduct the cache load test, completes async
584     storage()->LoadCache(1, delegate());
585   }
586
587   void Verify_LoadCache_Far_Hit() {
588     EXPECT_TRUE(delegate()->loaded_cache_.get());
589     EXPECT_TRUE(delegate()->loaded_cache_->HasOneRef());
590     EXPECT_EQ(1, delegate()->loaded_cache_id_);
591
592     // The group should also have been loaded.
593     EXPECT_TRUE(delegate()->loaded_cache_->owning_group());
594     EXPECT_TRUE(delegate()->loaded_cache_->owning_group()->HasOneRef());
595     EXPECT_EQ(1, delegate()->loaded_cache_->owning_group()->group_id());
596
597     EXPECT_EQ(1, mock_quota_manager_proxy_->notify_storage_accessed_count_);
598     EXPECT_EQ(0, mock_quota_manager_proxy_->notify_storage_modified_count_);
599
600     // Drop things from the working set.
601     delegate()->loaded_cache_ = NULL;
602     EXPECT_FALSE(delegate()->loaded_group_.get());
603
604     // Conduct the group load test, also complete asynchronously.
605     PushNextTask(base::Bind(&AppCacheStorageImplTest::Verify_LoadGroup_Far_Hit,
606                             base::Unretained(this)));
607
608     storage()->LoadOrCreateGroup(kManifestUrl, delegate());
609   }
610
611   void Verify_LoadGroup_Far_Hit() {
612     EXPECT_TRUE(delegate()->loaded_group_.get());
613     EXPECT_EQ(kManifestUrl, delegate()->loaded_manifest_url_);
614     EXPECT_TRUE(delegate()->loaded_group_->newest_complete_cache());
615     delegate()->loaded_groups_newest_cache_ = NULL;
616     EXPECT_TRUE(delegate()->loaded_group_->HasOneRef());
617     EXPECT_EQ(2, mock_quota_manager_proxy_->notify_storage_accessed_count_);
618     EXPECT_EQ(0, mock_quota_manager_proxy_->notify_storage_modified_count_);
619     TestFinished();
620   }
621
622   // StoreNewGroup  --------------------------------------
623
624   void StoreNewGroup() {
625     // Store a group and its newest cache. Should complete asynchronously.
626     PushNextTask(base::Bind(&AppCacheStorageImplTest::Verify_StoreNewGroup,
627                             base::Unretained(this)));
628
629     // Setup some preconditions. Create a group and newest cache that
630     // appear to be "unstored".
631     group_ = new AppCacheGroup(
632         storage(), kManifestUrl, storage()->NewGroupId());
633     cache_ = new AppCache(storage(), storage()->NewCacheId());
634     cache_->AddEntry(kEntryUrl, AppCacheEntry(AppCacheEntry::EXPLICIT, 1,
635                                               kDefaultEntrySize));
636     // Hold a ref to the cache simulate the UpdateJob holding that ref,
637     // and hold a ref to the group to simulate the CacheHost holding that ref.
638
639     // Have the quota manager retrun asynchronously for this test.
640     mock_quota_manager_proxy_->mock_manager_->async_ = true;
641
642     // Conduct the store test.
643     storage()->StoreGroupAndNewestCache(group_.get(), cache_.get(), delegate());
644     EXPECT_FALSE(delegate()->stored_group_success_);
645   }
646
647   void Verify_StoreNewGroup() {
648     EXPECT_TRUE(delegate()->stored_group_success_);
649     EXPECT_EQ(group_.get(), delegate()->stored_group_.get());
650     EXPECT_EQ(cache_.get(), group_->newest_complete_cache());
651     EXPECT_TRUE(cache_->is_complete());
652
653     // Should have been stored in the database.
654     AppCacheDatabase::GroupRecord group_record;
655     AppCacheDatabase::CacheRecord cache_record;
656     EXPECT_TRUE(database()->FindGroup(group_->group_id(), &group_record));
657     EXPECT_TRUE(database()->FindCache(cache_->cache_id(), &cache_record));
658
659     // Verify quota bookkeeping
660     EXPECT_EQ(kDefaultEntrySize, storage()->usage_map_[kOrigin]);
661     EXPECT_EQ(1, mock_quota_manager_proxy_->notify_storage_modified_count_);
662     EXPECT_EQ(kOrigin, mock_quota_manager_proxy_->last_origin_);
663     EXPECT_EQ(kDefaultEntrySize, mock_quota_manager_proxy_->last_delta_);
664
665     TestFinished();
666   }
667
668   // StoreExistingGroup  --------------------------------------
669
670   void StoreExistingGroup() {
671     // Store a group and its newest cache. Should complete asynchronously.
672     PushNextTask(base::Bind(&AppCacheStorageImplTest::Verify_StoreExistingGroup,
673                             base::Unretained(this)));
674
675     // Setup some preconditions. Create a group and old complete cache
676     // that appear to be "stored"
677     MakeCacheAndGroup(kManifestUrl, 1, 1, true);
678     EXPECT_EQ(kDefaultEntrySize, storage()->usage_map_[kOrigin]);
679
680     // And a newest unstored complete cache.
681     cache2_ = new AppCache(storage(), 2);
682     cache2_->AddEntry(kEntryUrl, AppCacheEntry(AppCacheEntry::MASTER, 1,
683                                                kDefaultEntrySize + 100));
684
685     // Conduct the test.
686     storage()->StoreGroupAndNewestCache(
687         group_.get(), cache2_.get(), delegate());
688     EXPECT_FALSE(delegate()->stored_group_success_);
689   }
690
691   void Verify_StoreExistingGroup() {
692     EXPECT_TRUE(delegate()->stored_group_success_);
693     EXPECT_EQ(group_.get(), delegate()->stored_group_.get());
694     EXPECT_EQ(cache2_.get(), group_->newest_complete_cache());
695     EXPECT_TRUE(cache2_->is_complete());
696
697     // The new cache should have been stored in the database.
698     AppCacheDatabase::GroupRecord group_record;
699     AppCacheDatabase::CacheRecord cache_record;
700     EXPECT_TRUE(database()->FindGroup(1, &group_record));
701     EXPECT_TRUE(database()->FindCache(2, &cache_record));
702
703     // The old cache should have been deleted
704     EXPECT_FALSE(database()->FindCache(1, &cache_record));
705
706     // Verify quota bookkeeping
707     EXPECT_EQ(kDefaultEntrySize + 100, storage()->usage_map_[kOrigin]);
708     EXPECT_EQ(1, mock_quota_manager_proxy_->notify_storage_modified_count_);
709     EXPECT_EQ(kOrigin, mock_quota_manager_proxy_->last_origin_);
710     EXPECT_EQ(100, mock_quota_manager_proxy_->last_delta_);
711
712     TestFinished();
713   }
714
715   // StoreExistingGroupExistingCache  -------------------------------
716
717   void StoreExistingGroupExistingCache() {
718     // Store a group with updates to its existing newest complete cache.
719     // Setup some preconditions. Create a group and a complete cache that
720     // appear to be "stored".
721
722     // Setup some preconditions. Create a group and old complete cache
723     // that appear to be "stored"
724     MakeCacheAndGroup(kManifestUrl, 1, 1, true);
725     EXPECT_EQ(kDefaultEntrySize, storage()->usage_map_[kOrigin]);
726
727     // Change the cache.
728     base::Time now = base::Time::Now();
729     cache_->AddEntry(kEntryUrl, AppCacheEntry(AppCacheEntry::MASTER, 1, 100));
730     cache_->set_update_time(now);
731
732     PushNextTask(base::Bind(
733         &AppCacheStorageImplTest::Verify_StoreExistingGroupExistingCache,
734         base::Unretained(this), now));
735
736     // Conduct the test.
737     EXPECT_EQ(cache_, group_->newest_complete_cache());
738     storage()->StoreGroupAndNewestCache(group_.get(), cache_.get(), delegate());
739     EXPECT_FALSE(delegate()->stored_group_success_);
740   }
741
742   void Verify_StoreExistingGroupExistingCache(
743       base::Time expected_update_time) {
744     EXPECT_TRUE(delegate()->stored_group_success_);
745     EXPECT_EQ(cache_, group_->newest_complete_cache());
746
747     AppCacheDatabase::CacheRecord cache_record;
748     EXPECT_TRUE(database()->FindCache(1, &cache_record));
749     EXPECT_EQ(1, cache_record.cache_id);
750     EXPECT_EQ(1, cache_record.group_id);
751     EXPECT_FALSE(cache_record.online_wildcard);
752     EXPECT_TRUE(expected_update_time == cache_record.update_time);
753     EXPECT_EQ(100 + kDefaultEntrySize, cache_record.cache_size);
754
755     std::vector<AppCacheDatabase::EntryRecord> entry_records;
756     EXPECT_TRUE(database()->FindEntriesForCache(1, &entry_records));
757     EXPECT_EQ(2U, entry_records.size());
758     if (entry_records[0].url == kDefaultEntryUrl)
759       entry_records.erase(entry_records.begin());
760     EXPECT_EQ(1 , entry_records[0].cache_id);
761     EXPECT_EQ(kEntryUrl, entry_records[0].url);
762     EXPECT_EQ(AppCacheEntry::MASTER, entry_records[0].flags);
763     EXPECT_EQ(1, entry_records[0].response_id);
764     EXPECT_EQ(100, entry_records[0].response_size);
765
766     // Verify quota bookkeeping
767     EXPECT_EQ(100 + kDefaultEntrySize, storage()->usage_map_[kOrigin]);
768     EXPECT_EQ(1, mock_quota_manager_proxy_->notify_storage_modified_count_);
769     EXPECT_EQ(kOrigin, mock_quota_manager_proxy_->last_origin_);
770     EXPECT_EQ(100, mock_quota_manager_proxy_->last_delta_);
771
772     TestFinished();
773   }
774
775   // FailStoreGroup  --------------------------------------
776
777   void FailStoreGroup() {
778     // Store a group and its newest cache. Should complete asynchronously.
779     PushNextTask(base::Bind(&AppCacheStorageImplTest::Verify_FailStoreGroup,
780                             base::Unretained(this)));
781
782     // Setup some preconditions. Create a group and newest cache that
783     // appear to be "unstored" and big enough to exceed the 5M limit.
784     const int64 kTooBig = 10 * 1024 * 1024;  // 10M
785     group_ = new AppCacheGroup(
786         storage(), kManifestUrl, storage()->NewGroupId());
787     cache_ = new AppCache(storage(), storage()->NewCacheId());
788     cache_->AddEntry(kManifestUrl,
789                      AppCacheEntry(AppCacheEntry::MANIFEST, 1, kTooBig));
790     // Hold a ref to the cache simulate the UpdateJob holding that ref,
791     // and hold a ref to the group to simulate the CacheHost holding that ref.
792
793     // Conduct the store test.
794     storage()->StoreGroupAndNewestCache(group_.get(), cache_.get(), delegate());
795     EXPECT_FALSE(delegate()->stored_group_success_);  // Expected to be async.
796   }
797
798   void Verify_FailStoreGroup() {
799     EXPECT_FALSE(delegate()->stored_group_success_);
800     EXPECT_TRUE(delegate()->would_exceed_quota_);
801
802     // Should not have been stored in the database.
803     AppCacheDatabase::GroupRecord group_record;
804     AppCacheDatabase::CacheRecord cache_record;
805     EXPECT_FALSE(database()->FindGroup(group_->group_id(), &group_record));
806     EXPECT_FALSE(database()->FindCache(cache_->cache_id(), &cache_record));
807
808     EXPECT_EQ(0, mock_quota_manager_proxy_->notify_storage_accessed_count_);
809     EXPECT_EQ(0, mock_quota_manager_proxy_->notify_storage_modified_count_);
810
811     TestFinished();
812   }
813
814   // MakeGroupObsolete  -------------------------------
815
816   void MakeGroupObsolete() {
817     // Make a group obsolete, should complete asynchronously.
818     PushNextTask(base::Bind(&AppCacheStorageImplTest::Verify_MakeGroupObsolete,
819                             base::Unretained(this)));
820
821     // Setup some preconditions. Create a group and newest cache that
822     // appears to be "stored" and "currently in use".
823     MakeCacheAndGroup(kManifestUrl, 1, 1, true);
824     EXPECT_EQ(kDefaultEntrySize, storage()->usage_map_[kOrigin]);
825
826     // Also insert some related records.
827     AppCacheDatabase::EntryRecord entry_record;
828     entry_record.cache_id = 1;
829     entry_record.flags = AppCacheEntry::FALLBACK;
830     entry_record.response_id = 1;
831     entry_record.url = kEntryUrl;
832     EXPECT_TRUE(database()->InsertEntry(&entry_record));
833
834     AppCacheDatabase::NamespaceRecord fallback_namespace_record;
835     fallback_namespace_record.cache_id = 1;
836     fallback_namespace_record.namespace_.target_url = kEntryUrl;
837     fallback_namespace_record.namespace_.namespace_url = kFallbackNamespace;
838     fallback_namespace_record.origin = kManifestUrl.GetOrigin();
839     EXPECT_TRUE(database()->InsertNamespace(&fallback_namespace_record));
840
841     AppCacheDatabase::OnlineWhiteListRecord online_whitelist_record;
842     online_whitelist_record.cache_id = 1;
843     online_whitelist_record.namespace_url = kOnlineNamespace;
844     EXPECT_TRUE(database()->InsertOnlineWhiteList(&online_whitelist_record));
845
846     // Conduct the test.
847     storage()->MakeGroupObsolete(group_.get(), delegate(), 0);
848     EXPECT_FALSE(group_->is_obsolete());
849   }
850
851   void Verify_MakeGroupObsolete() {
852     EXPECT_TRUE(delegate()->obsoleted_success_);
853     EXPECT_EQ(group_.get(), delegate()->obsoleted_group_.get());
854     EXPECT_TRUE(group_->is_obsolete());
855     EXPECT_TRUE(storage()->usage_map_.empty());
856
857     // The cache and group have been deleted from the database.
858     AppCacheDatabase::GroupRecord group_record;
859     AppCacheDatabase::CacheRecord cache_record;
860     EXPECT_FALSE(database()->FindGroup(1, &group_record));
861     EXPECT_FALSE(database()->FindCache(1, &cache_record));
862
863     // The related records should have been deleted too.
864     std::vector<AppCacheDatabase::EntryRecord> entry_records;
865     database()->FindEntriesForCache(1, &entry_records);
866     EXPECT_TRUE(entry_records.empty());
867     std::vector<AppCacheDatabase::NamespaceRecord> intercept_records;
868     std::vector<AppCacheDatabase::NamespaceRecord> fallback_records;
869     database()->FindNamespacesForCache(
870         1, &intercept_records, &fallback_records);
871     EXPECT_TRUE(fallback_records.empty());
872     std::vector<AppCacheDatabase::OnlineWhiteListRecord> whitelist_records;
873     database()->FindOnlineWhiteListForCache(1, &whitelist_records);
874     EXPECT_TRUE(whitelist_records.empty());
875
876     // Verify quota bookkeeping
877     EXPECT_TRUE(storage()->usage_map_.empty());
878     EXPECT_EQ(1, mock_quota_manager_proxy_->notify_storage_modified_count_);
879     EXPECT_EQ(kOrigin, mock_quota_manager_proxy_->last_origin_);
880     EXPECT_EQ(-kDefaultEntrySize, mock_quota_manager_proxy_->last_delta_);
881
882     TestFinished();
883   }
884
885   // MarkEntryAsForeign  -------------------------------
886
887   void MarkEntryAsForeign() {
888     // Setup some preconditions. Create a cache with an entry
889     // in storage and in the working set.
890     MakeCacheAndGroup(kManifestUrl, 1, 1, true);
891     cache_->AddEntry(kEntryUrl, AppCacheEntry(AppCacheEntry::EXPLICIT));
892     AppCacheDatabase::EntryRecord entry_record;
893     entry_record.cache_id = 1;
894     entry_record.url = kEntryUrl;
895     entry_record.flags = AppCacheEntry::EXPLICIT;
896     entry_record.response_id = 0;
897     EXPECT_TRUE(database()->InsertEntry(&entry_record));
898     EXPECT_FALSE(cache_->GetEntry(kEntryUrl)->IsForeign());
899
900     // Conduct the test.
901     storage()->MarkEntryAsForeign(kEntryUrl, 1);
902
903     // The entry in the working set should have been updated syncly.
904     EXPECT_TRUE(cache_->GetEntry(kEntryUrl)->IsForeign());
905     EXPECT_TRUE(cache_->GetEntry(kEntryUrl)->IsExplicit());
906
907     // And the entry in storage should also be updated, but that
908     // happens asynchronously on the db thread.
909     FlushDbThreadTasks();
910     AppCacheDatabase::EntryRecord entry_record2;
911     EXPECT_TRUE(database()->FindEntry(1, kEntryUrl, &entry_record2));
912     EXPECT_EQ(AppCacheEntry::EXPLICIT | AppCacheEntry::FOREIGN,
913               entry_record2.flags);
914     TestFinished();
915   }
916
917   // MarkEntryAsForeignWithLoadInProgress  -------------------------------
918
919   void MarkEntryAsForeignWithLoadInProgress() {
920     PushNextTask(base::Bind(
921        &AppCacheStorageImplTest::Verify_MarkEntryAsForeignWithLoadInProgress,
922        base::Unretained(this)));
923
924     // Setup some preconditions. Create a cache with an entry
925     // in storage, but not in the working set.
926     MakeCacheAndGroup(kManifestUrl, 1, 1, true);
927     cache_->AddEntry(kEntryUrl, AppCacheEntry(AppCacheEntry::EXPLICIT));
928     AppCacheDatabase::EntryRecord entry_record;
929     entry_record.cache_id = 1;
930     entry_record.url = kEntryUrl;
931     entry_record.flags = AppCacheEntry::EXPLICIT;
932     entry_record.response_id = 0;
933     EXPECT_TRUE(database()->InsertEntry(&entry_record));
934     EXPECT_FALSE(cache_->GetEntry(kEntryUrl)->IsForeign());
935     EXPECT_TRUE(cache_->HasOneRef());
936     cache_ = NULL;
937     group_ = NULL;
938
939     // Conduct the test, start a cache load, and prior to completion
940     // of that load, mark the entry as foreign.
941     storage()->LoadCache(1, delegate());
942     storage()->MarkEntryAsForeign(kEntryUrl, 1);
943   }
944
945   void Verify_MarkEntryAsForeignWithLoadInProgress() {
946     EXPECT_EQ(1, delegate()->loaded_cache_id_);
947     EXPECT_TRUE(delegate()->loaded_cache_.get());
948
949     // The entry in the working set should have been updated upon load.
950     EXPECT_TRUE(delegate()->loaded_cache_->GetEntry(kEntryUrl)->IsForeign());
951     EXPECT_TRUE(delegate()->loaded_cache_->GetEntry(kEntryUrl)->IsExplicit());
952
953     // And the entry in storage should also be updated.
954     FlushDbThreadTasks();
955     AppCacheDatabase::EntryRecord entry_record;
956     EXPECT_TRUE(database()->FindEntry(1, kEntryUrl, &entry_record));
957     EXPECT_EQ(AppCacheEntry::EXPLICIT | AppCacheEntry::FOREIGN,
958               entry_record.flags);
959     TestFinished();
960   }
961
962   // FindNoMainResponse  -------------------------------
963
964   void FindNoMainResponse() {
965     PushNextTask(base::Bind(&AppCacheStorageImplTest::Verify_FindNoMainResponse,
966                             base::Unretained(this)));
967
968     // Conduct the test.
969     storage()->FindResponseForMainRequest(kEntryUrl, GURL(), delegate());
970     EXPECT_NE(kEntryUrl, delegate()->found_url_);
971   }
972
973   void Verify_FindNoMainResponse() {
974     EXPECT_EQ(kEntryUrl, delegate()->found_url_);
975     EXPECT_TRUE(delegate()->found_manifest_url_.is_empty());
976     EXPECT_EQ(kNoCacheId, delegate()->found_cache_id_);
977     EXPECT_EQ(kNoResponseId, delegate()->found_entry_.response_id());
978     EXPECT_EQ(kNoResponseId, delegate()->found_fallback_entry_.response_id());
979     EXPECT_TRUE(delegate()->found_namespace_entry_url_.is_empty());
980     EXPECT_EQ(0, delegate()->found_entry_.types());
981     EXPECT_EQ(0, delegate()->found_fallback_entry_.types());
982     TestFinished();
983   }
984
985   // BasicFindMainResponse  -------------------------------
986
987   void BasicFindMainResponseInDatabase() {
988     BasicFindMainResponse(true);
989   }
990
991   void BasicFindMainResponseInWorkingSet() {
992     BasicFindMainResponse(false);
993   }
994
995   void BasicFindMainResponse(bool drop_from_working_set) {
996     PushNextTask(base::Bind(
997         &AppCacheStorageImplTest::Verify_BasicFindMainResponse,
998         base::Unretained(this)));
999
1000     // Setup some preconditions. Create a complete cache with an entry
1001     // in storage.
1002     MakeCacheAndGroup(kManifestUrl, 2, 1, true);
1003     cache_->AddEntry(kEntryUrl, AppCacheEntry(AppCacheEntry::EXPLICIT, 1));
1004     AppCacheDatabase::EntryRecord entry_record;
1005     entry_record.cache_id = 1;
1006     entry_record.url = kEntryUrl;
1007     entry_record.flags = AppCacheEntry::EXPLICIT;
1008     entry_record.response_id = 1;
1009     EXPECT_TRUE(database()->InsertEntry(&entry_record));
1010
1011     // Optionally drop the cache/group pair from the working set.
1012     if (drop_from_working_set) {
1013       EXPECT_TRUE(cache_->HasOneRef());
1014       cache_ = NULL;
1015       EXPECT_TRUE(group_->HasOneRef());
1016       group_ = NULL;
1017     }
1018
1019     // Conduct the test.
1020     storage()->FindResponseForMainRequest(kEntryUrl, GURL(), delegate());
1021     EXPECT_NE(kEntryUrl,  delegate()->found_url_);
1022   }
1023
1024   void Verify_BasicFindMainResponse() {
1025     EXPECT_EQ(kEntryUrl, delegate()->found_url_);
1026     EXPECT_EQ(kManifestUrl, delegate()->found_manifest_url_);
1027     EXPECT_EQ(1, delegate()->found_cache_id_);
1028     EXPECT_EQ(2, delegate()->found_group_id_);
1029     EXPECT_EQ(1, delegate()->found_entry_.response_id());
1030     EXPECT_TRUE(delegate()->found_entry_.IsExplicit());
1031     EXPECT_FALSE(delegate()->found_fallback_entry_.has_response_id());
1032     TestFinished();
1033   }
1034
1035   // BasicFindMainFallbackResponse  -------------------------------
1036
1037   void BasicFindMainFallbackResponseInDatabase() {
1038     BasicFindMainFallbackResponse(true);
1039   }
1040
1041   void BasicFindMainFallbackResponseInWorkingSet() {
1042     BasicFindMainFallbackResponse(false);
1043   }
1044
1045   void BasicFindMainFallbackResponse(bool drop_from_working_set) {
1046     PushNextTask(base::Bind(
1047         &AppCacheStorageImplTest::Verify_BasicFindMainFallbackResponse,
1048         base::Unretained(this)));
1049
1050     // Setup some preconditions. Create a complete cache with a
1051     // fallback namespace and entry.
1052     MakeCacheAndGroup(kManifestUrl, 2, 1, true);
1053     cache_->AddEntry(kEntryUrl, AppCacheEntry(AppCacheEntry::FALLBACK, 1));
1054     cache_->AddEntry(kEntryUrl2, AppCacheEntry(AppCacheEntry::FALLBACK, 2));
1055     cache_->fallback_namespaces_.push_back(
1056         Namespace(FALLBACK_NAMESPACE, kFallbackNamespace2, kEntryUrl2, false));
1057     cache_->fallback_namespaces_.push_back(
1058         Namespace(FALLBACK_NAMESPACE, kFallbackNamespace, kEntryUrl, false));
1059     AppCacheDatabase::CacheRecord cache_record;
1060     std::vector<AppCacheDatabase::EntryRecord> entries;
1061     std::vector<AppCacheDatabase::NamespaceRecord> intercepts;
1062     std::vector<AppCacheDatabase::NamespaceRecord> fallbacks;
1063     std::vector<AppCacheDatabase::OnlineWhiteListRecord> whitelists;
1064     cache_->ToDatabaseRecords(group_.get(),
1065                               &cache_record,
1066                               &entries,
1067                               &intercepts,
1068                               &fallbacks,
1069                               &whitelists);
1070
1071     std::vector<AppCacheDatabase::EntryRecord>::const_iterator iter =
1072         entries.begin();
1073     while (iter != entries.end()) {
1074       // MakeCacheAndGroup has inserted the default entry record already.
1075       if (iter->url != kDefaultEntryUrl)
1076         EXPECT_TRUE(database()->InsertEntry(&(*iter)));
1077       ++iter;
1078     }
1079
1080     EXPECT_TRUE(database()->InsertNamespaceRecords(fallbacks));
1081     EXPECT_TRUE(database()->InsertOnlineWhiteListRecords(whitelists));
1082     if (drop_from_working_set) {
1083       EXPECT_TRUE(cache_->HasOneRef());
1084       cache_ = NULL;
1085       EXPECT_TRUE(group_->HasOneRef());
1086       group_ = NULL;
1087     }
1088
1089     // Conduct the test. The test url is in both fallback namespace urls,
1090     // but should match the longer of the two.
1091     storage()->FindResponseForMainRequest(kFallbackTestUrl, GURL(), delegate());
1092     EXPECT_NE(kFallbackTestUrl, delegate()->found_url_);
1093   }
1094
1095   void Verify_BasicFindMainFallbackResponse() {
1096     EXPECT_EQ(kFallbackTestUrl, delegate()->found_url_);
1097     EXPECT_EQ(kManifestUrl, delegate()->found_manifest_url_);
1098     EXPECT_EQ(1, delegate()->found_cache_id_);
1099     EXPECT_EQ(2, delegate()->found_group_id_);
1100     EXPECT_FALSE(delegate()->found_entry_.has_response_id());
1101     EXPECT_EQ(2, delegate()->found_fallback_entry_.response_id());
1102     EXPECT_EQ(kEntryUrl2, delegate()->found_namespace_entry_url_);
1103     EXPECT_TRUE(delegate()->found_fallback_entry_.IsFallback());
1104     TestFinished();
1105   }
1106
1107   // BasicFindMainInterceptResponse  -------------------------------
1108
1109   void BasicFindMainInterceptResponseInDatabase() {
1110     BasicFindMainInterceptResponse(true);
1111   }
1112
1113   void BasicFindMainInterceptResponseInWorkingSet() {
1114     BasicFindMainInterceptResponse(false);
1115   }
1116
1117   void BasicFindMainInterceptResponse(bool drop_from_working_set) {
1118     PushNextTask(base::Bind(
1119         &AppCacheStorageImplTest::Verify_BasicFindMainInterceptResponse,
1120         base::Unretained(this)));
1121
1122     // Setup some preconditions. Create a complete cache with an
1123     // intercept namespace and entry.
1124     MakeCacheAndGroup(kManifestUrl, 2, 1, true);
1125     cache_->AddEntry(kEntryUrl, AppCacheEntry(AppCacheEntry::INTERCEPT, 1));
1126     cache_->AddEntry(kEntryUrl2, AppCacheEntry(AppCacheEntry::INTERCEPT, 2));
1127     cache_->intercept_namespaces_.push_back(
1128         Namespace(INTERCEPT_NAMESPACE, kInterceptNamespace2,
1129                   kEntryUrl2, false));
1130     cache_->intercept_namespaces_.push_back(
1131         Namespace(INTERCEPT_NAMESPACE, kInterceptNamespace,
1132                   kEntryUrl, false));
1133     AppCacheDatabase::CacheRecord cache_record;
1134     std::vector<AppCacheDatabase::EntryRecord> entries;
1135     std::vector<AppCacheDatabase::NamespaceRecord> intercepts;
1136     std::vector<AppCacheDatabase::NamespaceRecord> fallbacks;
1137     std::vector<AppCacheDatabase::OnlineWhiteListRecord> whitelists;
1138     cache_->ToDatabaseRecords(group_.get(),
1139                               &cache_record,
1140                               &entries,
1141                               &intercepts,
1142                               &fallbacks,
1143                               &whitelists);
1144
1145     std::vector<AppCacheDatabase::EntryRecord>::const_iterator iter =
1146         entries.begin();
1147     while (iter != entries.end()) {
1148       // MakeCacheAndGroup has inserted  the default entry record already
1149       if (iter->url != kDefaultEntryUrl)
1150         EXPECT_TRUE(database()->InsertEntry(&(*iter)));
1151       ++iter;
1152     }
1153
1154     EXPECT_TRUE(database()->InsertNamespaceRecords(intercepts));
1155     EXPECT_TRUE(database()->InsertOnlineWhiteListRecords(whitelists));
1156     if (drop_from_working_set) {
1157       EXPECT_TRUE(cache_->HasOneRef());
1158       cache_ = NULL;
1159       EXPECT_TRUE(group_->HasOneRef());
1160       group_ = NULL;
1161     }
1162
1163     // Conduct the test. The test url is in both intercept namespaces,
1164     // but should match the longer of the two.
1165     storage()->FindResponseForMainRequest(
1166         kInterceptTestUrl, GURL(), delegate());
1167     EXPECT_NE(kInterceptTestUrl, delegate()->found_url_);
1168   }
1169
1170   void Verify_BasicFindMainInterceptResponse() {
1171     EXPECT_EQ(kInterceptTestUrl, delegate()->found_url_);
1172     EXPECT_EQ(kManifestUrl, delegate()->found_manifest_url_);
1173     EXPECT_EQ(1, delegate()->found_cache_id_);
1174     EXPECT_EQ(2, delegate()->found_group_id_);
1175     EXPECT_EQ(2, delegate()->found_entry_.response_id());
1176     EXPECT_TRUE(delegate()->found_entry_.IsIntercept());
1177     EXPECT_EQ(kEntryUrl2, delegate()->found_namespace_entry_url_);
1178     EXPECT_FALSE(delegate()->found_fallback_entry_.has_response_id());
1179     TestFinished();
1180   }
1181
1182   // FindInterceptPatternMatch ----------------------------------------
1183
1184   void FindInterceptPatternMatchInDatabase() {
1185     FindInterceptPatternMatch(true);
1186   }
1187
1188   void FindInterceptPatternMatchInWorkingSet() {
1189     FindInterceptPatternMatch(false);
1190   }
1191
1192   void FindInterceptPatternMatch(bool drop_from_working_set) {
1193     // Setup some preconditions. Create a complete cache with an
1194     // pattern matching intercept namespace and entry.
1195     MakeCacheAndGroup(kManifestUrl, 2, 1, true);
1196     cache_->AddEntry(kEntryUrl, AppCacheEntry(AppCacheEntry::INTERCEPT, 1));
1197     cache_->intercept_namespaces_.push_back(
1198         Namespace(INTERCEPT_NAMESPACE, kInterceptPatternNamespace,
1199                   kEntryUrl, true));
1200     AppCacheDatabase::CacheRecord cache_record;
1201     std::vector<AppCacheDatabase::EntryRecord> entries;
1202     std::vector<AppCacheDatabase::NamespaceRecord> intercepts;
1203     std::vector<AppCacheDatabase::NamespaceRecord> fallbacks;
1204     std::vector<AppCacheDatabase::OnlineWhiteListRecord> whitelists;
1205     cache_->ToDatabaseRecords(group_.get(),
1206                               &cache_record,
1207                               &entries,
1208                               &intercepts,
1209                               &fallbacks,
1210                               &whitelists);
1211
1212     std::vector<AppCacheDatabase::EntryRecord>::const_iterator iter =
1213         entries.begin();
1214     while (iter != entries.end()) {
1215       // MakeCacheAndGroup has inserted  the default entry record already
1216       if (iter->url != kDefaultEntryUrl)
1217         EXPECT_TRUE(database()->InsertEntry(&(*iter)));
1218       ++iter;
1219     }
1220
1221     EXPECT_TRUE(database()->InsertNamespaceRecords(intercepts));
1222     if (drop_from_working_set) {
1223       EXPECT_TRUE(cache_->HasOneRef());
1224       cache_ = NULL;
1225       EXPECT_TRUE(group_->HasOneRef());
1226       group_ = NULL;
1227     }
1228
1229     // First test something that does not match the pattern.
1230     PushNextTask(base::Bind(
1231         &AppCacheStorageImplTest::Verify_FindInterceptPatternMatchNegative,
1232         base::Unretained(this)));
1233     storage()->FindResponseForMainRequest(
1234         kInterceptPatternTestNegativeUrl, GURL(), delegate());
1235     EXPECT_EQ(GURL(), delegate()->found_url_);  // Is always async.
1236   }
1237
1238   void Verify_FindInterceptPatternMatchNegative() {
1239     EXPECT_EQ(kInterceptPatternTestNegativeUrl, delegate()->found_url_);
1240     EXPECT_TRUE(delegate()->found_manifest_url_.is_empty());
1241     EXPECT_EQ(kNoCacheId, delegate()->found_cache_id_);
1242     EXPECT_EQ(kNoResponseId, delegate()->found_entry_.response_id());
1243     EXPECT_EQ(kNoResponseId, delegate()->found_fallback_entry_.response_id());
1244     EXPECT_TRUE(delegate()->found_namespace_entry_url_.is_empty());
1245     EXPECT_EQ(0, delegate()->found_entry_.types());
1246     EXPECT_EQ(0, delegate()->found_fallback_entry_.types());
1247
1248     // Then test something that matches.
1249     PushNextTask(base::Bind(
1250         &AppCacheStorageImplTest::Verify_FindInterceptPatternMatchPositive,
1251         base::Unretained(this)));
1252     storage()->FindResponseForMainRequest(
1253         kInterceptPatternTestPositiveUrl, GURL(), delegate());
1254   }
1255
1256   void Verify_FindInterceptPatternMatchPositive() {
1257     EXPECT_EQ(kInterceptPatternTestPositiveUrl, delegate()->found_url_);
1258     EXPECT_EQ(kManifestUrl, delegate()->found_manifest_url_);
1259     EXPECT_EQ(1, delegate()->found_cache_id_);
1260     EXPECT_EQ(2, delegate()->found_group_id_);
1261     EXPECT_EQ(1, delegate()->found_entry_.response_id());
1262     EXPECT_TRUE(delegate()->found_entry_.IsIntercept());
1263     EXPECT_EQ(kEntryUrl, delegate()->found_namespace_entry_url_);
1264     EXPECT_FALSE(delegate()->found_fallback_entry_.has_response_id());
1265     TestFinished();
1266   }
1267
1268   // FindFallbackPatternMatch  -------------------------------
1269
1270   void FindFallbackPatternMatchInDatabase() {
1271     FindFallbackPatternMatch(true);
1272   }
1273
1274   void FindFallbackPatternMatchInWorkingSet() {
1275     FindFallbackPatternMatch(false);
1276   }
1277
1278   void FindFallbackPatternMatch(bool drop_from_working_set) {
1279     // Setup some preconditions. Create a complete cache with a
1280     // pattern matching fallback namespace and entry.
1281     MakeCacheAndGroup(kManifestUrl, 2, 1, true);
1282     cache_->AddEntry(kEntryUrl, AppCacheEntry(AppCacheEntry::FALLBACK, 1));
1283     cache_->fallback_namespaces_.push_back(
1284         Namespace(FALLBACK_NAMESPACE, kFallbackPatternNamespace,
1285                   kEntryUrl, true));
1286     AppCacheDatabase::CacheRecord cache_record;
1287     std::vector<AppCacheDatabase::EntryRecord> entries;
1288     std::vector<AppCacheDatabase::NamespaceRecord> intercepts;
1289     std::vector<AppCacheDatabase::NamespaceRecord> fallbacks;
1290     std::vector<AppCacheDatabase::OnlineWhiteListRecord> whitelists;
1291     cache_->ToDatabaseRecords(group_.get(),
1292                               &cache_record,
1293                               &entries,
1294                               &intercepts,
1295                               &fallbacks,
1296                               &whitelists);
1297
1298     std::vector<AppCacheDatabase::EntryRecord>::const_iterator iter =
1299         entries.begin();
1300     while (iter != entries.end()) {
1301       // MakeCacheAndGroup has inserted the default entry record already.
1302       if (iter->url != kDefaultEntryUrl)
1303         EXPECT_TRUE(database()->InsertEntry(&(*iter)));
1304       ++iter;
1305     }
1306
1307     EXPECT_TRUE(database()->InsertNamespaceRecords(fallbacks));
1308     if (drop_from_working_set) {
1309       EXPECT_TRUE(cache_->HasOneRef());
1310       cache_ = NULL;
1311       EXPECT_TRUE(group_->HasOneRef());
1312       group_ = NULL;
1313     }
1314
1315     // First test something that does not match the pattern.
1316     PushNextTask(base::Bind(
1317         &AppCacheStorageImplTest::Verify_FindFallbackPatternMatchNegative,
1318         base::Unretained(this)));
1319     storage()->FindResponseForMainRequest(
1320         kFallbackPatternTestNegativeUrl, GURL(), delegate());
1321     EXPECT_EQ(GURL(), delegate()->found_url_);  // Is always async.
1322   }
1323
1324   void Verify_FindFallbackPatternMatchNegative() {
1325     EXPECT_EQ(kFallbackPatternTestNegativeUrl, delegate()->found_url_);
1326       EXPECT_TRUE(delegate()->found_manifest_url_.is_empty());
1327       EXPECT_EQ(kNoCacheId, delegate()->found_cache_id_);
1328       EXPECT_EQ(kNoResponseId, delegate()->found_entry_.response_id());
1329       EXPECT_EQ(kNoResponseId, delegate()->found_fallback_entry_.response_id());
1330       EXPECT_TRUE(delegate()->found_namespace_entry_url_.is_empty());
1331       EXPECT_EQ(0, delegate()->found_entry_.types());
1332       EXPECT_EQ(0, delegate()->found_fallback_entry_.types());
1333
1334       // Then test something that matches.
1335       PushNextTask(base::Bind(
1336           &AppCacheStorageImplTest::Verify_FindFallbackPatternMatchPositive,
1337           base::Unretained(this)));
1338       storage()->FindResponseForMainRequest(
1339           kFallbackPatternTestPositiveUrl, GURL(), delegate());
1340   }
1341
1342   void Verify_FindFallbackPatternMatchPositive() {
1343     EXPECT_EQ(kFallbackPatternTestPositiveUrl, delegate()->found_url_);
1344     EXPECT_EQ(kManifestUrl, delegate()->found_manifest_url_);
1345     EXPECT_EQ(1, delegate()->found_cache_id_);
1346     EXPECT_EQ(2, delegate()->found_group_id_);
1347     EXPECT_EQ(1, delegate()->found_fallback_entry_.response_id());
1348     EXPECT_TRUE(delegate()->found_fallback_entry_.IsFallback());
1349     EXPECT_EQ(kEntryUrl, delegate()->found_namespace_entry_url_);
1350     EXPECT_FALSE(delegate()->found_entry_.has_response_id());
1351     TestFinished();
1352   }
1353
1354   // FindMainResponseWithMultipleHits  -------------------------------
1355
1356   void FindMainResponseWithMultipleHits() {
1357     PushNextTask(base::Bind(
1358         &AppCacheStorageImplTest::Verify_FindMainResponseWithMultipleHits,
1359         base::Unretained(this)));
1360
1361     // Setup some preconditions, create a few caches with an identical set
1362     // of entries and fallback namespaces. Only the last one remains in
1363     // the working set to simulate appearing as "in use".
1364     MakeMultipleHitCacheAndGroup(kManifestUrl, 1);
1365     MakeMultipleHitCacheAndGroup(kManifestUrl2, 2);
1366     MakeMultipleHitCacheAndGroup(kManifestUrl3, 3);
1367
1368     // Conduct the test, we should find the response from the last cache
1369     // since it's "in use".
1370     storage()->FindResponseForMainRequest(kEntryUrl, GURL(), delegate());
1371     EXPECT_NE(kEntryUrl, delegate()->found_url_);
1372   }
1373
1374   void MakeMultipleHitCacheAndGroup(const GURL& manifest_url, int id) {
1375     MakeCacheAndGroup(manifest_url, id, id, true);
1376     AppCacheDatabase::EntryRecord entry_record;
1377
1378     // Add an entry for kEntryUrl
1379     entry_record.cache_id = id;
1380     entry_record.url = kEntryUrl;
1381     entry_record.flags = AppCacheEntry::EXPLICIT;
1382     entry_record.response_id = id;
1383     EXPECT_TRUE(database()->InsertEntry(&entry_record));
1384     cache_->AddEntry(
1385         entry_record.url,
1386         AppCacheEntry(entry_record.flags, entry_record.response_id));
1387
1388     // Add an entry for the manifestUrl
1389     entry_record.cache_id = id;
1390     entry_record.url = manifest_url;
1391     entry_record.flags = AppCacheEntry::MANIFEST;
1392     entry_record.response_id = id + kManifestEntryIdOffset;
1393     EXPECT_TRUE(database()->InsertEntry(&entry_record));
1394     cache_->AddEntry(
1395         entry_record.url,
1396         AppCacheEntry(entry_record.flags, entry_record.response_id));
1397
1398     // Add a fallback entry and namespace
1399     entry_record.cache_id = id;
1400     entry_record.url = kEntryUrl2;
1401     entry_record.flags = AppCacheEntry::FALLBACK;
1402     entry_record.response_id = id + kFallbackEntryIdOffset;
1403     EXPECT_TRUE(database()->InsertEntry(&entry_record));
1404     cache_->AddEntry(
1405         entry_record.url,
1406         AppCacheEntry(entry_record.flags, entry_record.response_id));
1407     AppCacheDatabase::NamespaceRecord fallback_namespace_record;
1408     fallback_namespace_record.cache_id = id;
1409     fallback_namespace_record.namespace_.target_url = entry_record.url;
1410     fallback_namespace_record.namespace_.namespace_url = kFallbackNamespace;
1411     fallback_namespace_record.origin = manifest_url.GetOrigin();
1412     EXPECT_TRUE(database()->InsertNamespace(&fallback_namespace_record));
1413     cache_->fallback_namespaces_.push_back(
1414         Namespace(FALLBACK_NAMESPACE, kFallbackNamespace, kEntryUrl2, false));
1415   }
1416
1417   void Verify_FindMainResponseWithMultipleHits() {
1418     EXPECT_EQ(kEntryUrl, delegate()->found_url_);
1419     EXPECT_EQ(kManifestUrl3, delegate()->found_manifest_url_);
1420     EXPECT_EQ(3, delegate()->found_cache_id_);
1421     EXPECT_EQ(3, delegate()->found_group_id_);
1422     EXPECT_EQ(3, delegate()->found_entry_.response_id());
1423     EXPECT_TRUE(delegate()->found_entry_.IsExplicit());
1424     EXPECT_FALSE(delegate()->found_fallback_entry_.has_response_id());
1425
1426     // Conduct another test perferring kManifestUrl
1427     delegate_.reset(new MockStorageDelegate(this));
1428     PushNextTask(base::Bind(
1429         &AppCacheStorageImplTest::Verify_FindMainResponseWithMultipleHits2,
1430         base::Unretained(this)));
1431     storage()->FindResponseForMainRequest(kEntryUrl, kManifestUrl, delegate());
1432     EXPECT_NE(kEntryUrl, delegate()->found_url_);
1433   }
1434
1435   void Verify_FindMainResponseWithMultipleHits2() {
1436     EXPECT_EQ(kEntryUrl, delegate()->found_url_);
1437     EXPECT_EQ(kManifestUrl, delegate()->found_manifest_url_);
1438     EXPECT_EQ(1, delegate()->found_cache_id_);
1439     EXPECT_EQ(1, delegate()->found_group_id_);
1440     EXPECT_EQ(1, delegate()->found_entry_.response_id());
1441     EXPECT_TRUE(delegate()->found_entry_.IsExplicit());
1442     EXPECT_FALSE(delegate()->found_fallback_entry_.has_response_id());
1443
1444     // Conduct the another test perferring kManifestUrl2
1445     delegate_.reset(new MockStorageDelegate(this));
1446     PushNextTask(base::Bind(
1447         &AppCacheStorageImplTest::Verify_FindMainResponseWithMultipleHits3,
1448         base::Unretained(this)));
1449     storage()->FindResponseForMainRequest(kEntryUrl, kManifestUrl2, delegate());
1450     EXPECT_NE(kEntryUrl, delegate()->found_url_);
1451   }
1452
1453   void Verify_FindMainResponseWithMultipleHits3() {
1454     EXPECT_EQ(kEntryUrl, delegate()->found_url_);
1455     EXPECT_EQ(kManifestUrl2, delegate()->found_manifest_url_);
1456     EXPECT_EQ(2, delegate()->found_cache_id_);
1457     EXPECT_EQ(2, delegate()->found_group_id_);
1458     EXPECT_EQ(2, delegate()->found_entry_.response_id());
1459     EXPECT_TRUE(delegate()->found_entry_.IsExplicit());
1460     EXPECT_FALSE(delegate()->found_fallback_entry_.has_response_id());
1461
1462     // Conduct another test with no preferred manifest that hits the fallback.
1463     delegate_.reset(new MockStorageDelegate(this));
1464     PushNextTask(base::Bind(
1465         &AppCacheStorageImplTest::Verify_FindMainResponseWithMultipleHits4,
1466         base::Unretained(this)));
1467     storage()->FindResponseForMainRequest(
1468         kFallbackTestUrl, GURL(), delegate());
1469     EXPECT_NE(kFallbackTestUrl, delegate()->found_url_);
1470   }
1471
1472   void Verify_FindMainResponseWithMultipleHits4() {
1473     EXPECT_EQ(kFallbackTestUrl, delegate()->found_url_);
1474     EXPECT_EQ(kManifestUrl3, delegate()->found_manifest_url_);
1475     EXPECT_EQ(3, delegate()->found_cache_id_);
1476     EXPECT_EQ(3, delegate()->found_group_id_);
1477     EXPECT_FALSE(delegate()->found_entry_.has_response_id());
1478     EXPECT_EQ(3 + kFallbackEntryIdOffset,
1479               delegate()->found_fallback_entry_.response_id());
1480     EXPECT_TRUE(delegate()->found_fallback_entry_.IsFallback());
1481     EXPECT_EQ(kEntryUrl2, delegate()->found_namespace_entry_url_);
1482
1483     // Conduct another test preferring kManifestUrl2 that hits the fallback.
1484     delegate_.reset(new MockStorageDelegate(this));
1485     PushNextTask(base::Bind(
1486         &AppCacheStorageImplTest::Verify_FindMainResponseWithMultipleHits5,
1487         base::Unretained(this)));
1488     storage()->FindResponseForMainRequest(
1489         kFallbackTestUrl, kManifestUrl2, delegate());
1490     EXPECT_NE(kFallbackTestUrl, delegate()->found_url_);
1491   }
1492
1493   void Verify_FindMainResponseWithMultipleHits5() {
1494     EXPECT_EQ(kFallbackTestUrl, delegate()->found_url_);
1495     EXPECT_EQ(kManifestUrl2, delegate()->found_manifest_url_);
1496     EXPECT_EQ(2, delegate()->found_cache_id_);
1497     EXPECT_EQ(2, delegate()->found_group_id_);
1498     EXPECT_FALSE(delegate()->found_entry_.has_response_id());
1499     EXPECT_EQ(2 + kFallbackEntryIdOffset,
1500               delegate()->found_fallback_entry_.response_id());
1501     EXPECT_TRUE(delegate()->found_fallback_entry_.IsFallback());
1502     EXPECT_EQ(kEntryUrl2, delegate()->found_namespace_entry_url_);
1503
1504     TestFinished();
1505   }
1506
1507   // FindMainResponseExclusions  -------------------------------
1508
1509   void FindMainResponseExclusionsInDatabase() {
1510     FindMainResponseExclusions(true);
1511   }
1512
1513   void FindMainResponseExclusionsInWorkingSet() {
1514     FindMainResponseExclusions(false);
1515   }
1516
1517   void FindMainResponseExclusions(bool drop_from_working_set) {
1518     // Setup some preconditions. Create a complete cache with a
1519     // foreign entry, an online namespace, and a second online
1520     // namespace nested within a fallback namespace.
1521     MakeCacheAndGroup(kManifestUrl, 1, 1, true);
1522     cache_->AddEntry(kEntryUrl,
1523         AppCacheEntry(AppCacheEntry::EXPLICIT | AppCacheEntry::FOREIGN, 1));
1524     cache_->AddEntry(kEntryUrl2, AppCacheEntry(AppCacheEntry::FALLBACK, 2));
1525     cache_->fallback_namespaces_.push_back(
1526         Namespace(FALLBACK_NAMESPACE, kFallbackNamespace, kEntryUrl2, false));
1527     cache_->online_whitelist_namespaces_.push_back(
1528         Namespace(NETWORK_NAMESPACE, kOnlineNamespace,
1529                   GURL(), false));
1530     cache_->online_whitelist_namespaces_.push_back(
1531         Namespace(NETWORK_NAMESPACE, kOnlineNamespaceWithinFallback,
1532                   GURL(), false));
1533
1534     AppCacheDatabase::EntryRecord entry_record;
1535     entry_record.cache_id = 1;
1536     entry_record.url = kEntryUrl;
1537     entry_record.flags = AppCacheEntry::EXPLICIT | AppCacheEntry::FOREIGN;
1538     entry_record.response_id = 1;
1539     EXPECT_TRUE(database()->InsertEntry(&entry_record));
1540     AppCacheDatabase::OnlineWhiteListRecord whitelist_record;
1541     whitelist_record.cache_id = 1;
1542     whitelist_record.namespace_url = kOnlineNamespace;
1543     EXPECT_TRUE(database()->InsertOnlineWhiteList(&whitelist_record));
1544     AppCacheDatabase::NamespaceRecord fallback_namespace_record;
1545     fallback_namespace_record.cache_id = 1;
1546     fallback_namespace_record.namespace_.target_url = kEntryUrl2;
1547     fallback_namespace_record.namespace_.namespace_url = kFallbackNamespace;
1548     fallback_namespace_record.origin = kManifestUrl.GetOrigin();
1549     EXPECT_TRUE(database()->InsertNamespace(&fallback_namespace_record));
1550     whitelist_record.cache_id = 1;
1551     whitelist_record.namespace_url = kOnlineNamespaceWithinFallback;
1552     EXPECT_TRUE(database()->InsertOnlineWhiteList(&whitelist_record));
1553     if (drop_from_working_set) {
1554       cache_ = NULL;
1555       group_ = NULL;
1556     }
1557
1558     // We should not find anything for the foreign entry.
1559     PushNextTask(base::Bind(&AppCacheStorageImplTest::Verify_ExclusionNotFound,
1560                             base::Unretained(this), kEntryUrl, 1));
1561     storage()->FindResponseForMainRequest(kEntryUrl, GURL(), delegate());
1562   }
1563
1564   void Verify_ExclusionNotFound(GURL expected_url, int phase) {
1565     EXPECT_EQ(expected_url, delegate()->found_url_);
1566     EXPECT_TRUE(delegate()->found_manifest_url_.is_empty());
1567     EXPECT_EQ(kNoCacheId, delegate()->found_cache_id_);
1568     EXPECT_EQ(0, delegate()->found_group_id_);
1569     EXPECT_EQ(kNoResponseId, delegate()->found_entry_.response_id());
1570     EXPECT_EQ(kNoResponseId, delegate()->found_fallback_entry_.response_id());
1571     EXPECT_TRUE(delegate()->found_namespace_entry_url_.is_empty());
1572     EXPECT_EQ(0, delegate()->found_entry_.types());
1573     EXPECT_EQ(0, delegate()->found_fallback_entry_.types());
1574
1575     if (phase == 1) {
1576       // We should not find anything for the online namespace.
1577       PushNextTask(
1578           base::Bind(&AppCacheStorageImplTest::Verify_ExclusionNotFound,
1579                      base::Unretained(this), kOnlineNamespace, 2));
1580       storage()->FindResponseForMainRequest(
1581           kOnlineNamespace, GURL(), delegate());
1582       return;
1583     }
1584     if (phase == 2) {
1585       // We should not find anything for the online namespace nested within
1586       // the fallback namespace.
1587       PushNextTask(base::Bind(
1588           &AppCacheStorageImplTest::Verify_ExclusionNotFound,
1589           base::Unretained(this), kOnlineNamespaceWithinFallback, 3));
1590       storage()->FindResponseForMainRequest(
1591           kOnlineNamespaceWithinFallback, GURL(), delegate());
1592       return;
1593     }
1594
1595     TestFinished();
1596   }
1597
1598   // Reinitialize -------------------------------
1599   // These tests are somewhat of a system integration test.
1600   // They rely on running a mock http server on our IO thread,
1601   // and involves other appcache classes to get some code
1602   // coverage thruout when Reinitialize happens.
1603
1604   class MockServiceObserver : public AppCacheService::Observer {
1605    public:
1606     explicit MockServiceObserver(AppCacheStorageImplTest* test)
1607         : test_(test) {}
1608
1609     virtual void OnServiceReinitialized(
1610         AppCacheStorageReference* old_storage_ref) OVERRIDE {
1611       observed_old_storage_ = old_storage_ref;
1612       test_->ScheduleNextTask();
1613     }
1614
1615     scoped_refptr<AppCacheStorageReference> observed_old_storage_;
1616     AppCacheStorageImplTest* test_;
1617   };
1618
1619   class MockAppCacheFrontend : public AppCacheFrontend {
1620    public:
1621     MockAppCacheFrontend() : error_event_was_raised_(false) {}
1622
1623     virtual void OnCacheSelected(
1624         int host_id, const AppCacheInfo& info) OVERRIDE {}
1625     virtual void OnStatusChanged(const std::vector<int>& host_ids,
1626                                  Status status) OVERRIDE {}
1627     virtual void OnEventRaised(const std::vector<int>& host_ids,
1628                                EventID event_id) OVERRIDE {}
1629     virtual void OnProgressEventRaised(
1630         const std::vector<int>& host_ids,
1631         const GURL& url,
1632         int num_total, int num_complete) OVERRIDE {}
1633     virtual void OnErrorEventRaised(const std::vector<int>& host_ids,
1634                                     const ErrorDetails& details)
1635         OVERRIDE {
1636       error_event_was_raised_ = true;
1637     }
1638     virtual void OnLogMessage(int host_id, LogLevel log_level,
1639                               const std::string& message) OVERRIDE {}
1640     virtual void OnContentBlocked(
1641         int host_id, const GURL& manifest_url) OVERRIDE {}
1642
1643     bool error_event_was_raised_;
1644   };
1645
1646   enum ReinitTestCase {
1647      CORRUPT_CACHE_ON_INSTALL,
1648      CORRUPT_CACHE_ON_LOAD_EXISTING,
1649      CORRUPT_SQL_ON_INSTALL
1650   };
1651
1652   void Reinitialize1() {
1653     // Recover from a corrupt disk cache discovered while
1654     // installing a new appcache.
1655     Reinitialize(CORRUPT_CACHE_ON_INSTALL);
1656   }
1657
1658   void Reinitialize2() {
1659     // Recover from a corrupt disk cache discovered while
1660     // trying to load a resource from an existing appcache.
1661     Reinitialize(CORRUPT_CACHE_ON_LOAD_EXISTING);
1662   }
1663
1664   void Reinitialize3() {
1665     // Recover from a corrupt sql database discovered while
1666     // installing a new appcache.
1667     Reinitialize(CORRUPT_SQL_ON_INSTALL);
1668   }
1669
1670   void Reinitialize(ReinitTestCase test_case) {
1671     // Unlike all of the other tests, this one actually read/write files.
1672     ASSERT_TRUE(temp_directory_.CreateUniqueTempDir());
1673
1674     AppCacheDatabase db(temp_directory_.path().AppendASCII("Index"));
1675     EXPECT_TRUE(db.LazyOpen(true));
1676
1677     if (test_case == CORRUPT_CACHE_ON_INSTALL ||
1678         test_case == CORRUPT_CACHE_ON_LOAD_EXISTING) {
1679       // Create a corrupt/unopenable disk_cache index file.
1680       const std::string kCorruptData("deadbeef");
1681       base::FilePath disk_cache_directory =
1682           temp_directory_.path().AppendASCII("Cache");
1683       ASSERT_TRUE(base::CreateDirectory(disk_cache_directory));
1684       base::FilePath index_file = disk_cache_directory.AppendASCII("index");
1685       EXPECT_EQ(static_cast<int>(kCorruptData.length()),
1686                 base::WriteFile(
1687                     index_file, kCorruptData.data(), kCorruptData.length()));
1688     }
1689
1690     // Create records for a degenerate cached manifest that only contains
1691     // one entry for the manifest file resource.
1692     if (test_case == CORRUPT_CACHE_ON_LOAD_EXISTING) {
1693       AppCacheDatabase db(temp_directory_.path().AppendASCII("Index"));
1694       GURL manifest_url = MockHttpServer::GetMockUrl("manifest");
1695
1696       AppCacheDatabase::GroupRecord group_record;
1697       group_record.group_id = 1;
1698       group_record.manifest_url = manifest_url;
1699       group_record.origin = manifest_url.GetOrigin();
1700       EXPECT_TRUE(db.InsertGroup(&group_record));
1701       AppCacheDatabase::CacheRecord cache_record;
1702       cache_record.cache_id = 1;
1703       cache_record.group_id = 1;
1704       cache_record.online_wildcard = false;
1705       cache_record.update_time = kZeroTime;
1706       cache_record.cache_size = kDefaultEntrySize;
1707       EXPECT_TRUE(db.InsertCache(&cache_record));
1708       AppCacheDatabase::EntryRecord entry_record;
1709       entry_record.cache_id = 1;
1710       entry_record.url = manifest_url;
1711       entry_record.flags = AppCacheEntry::MANIFEST;
1712       entry_record.response_id = 1;
1713       entry_record.response_size = kDefaultEntrySize;
1714       EXPECT_TRUE(db.InsertEntry(&entry_record));
1715     }
1716
1717     // Recreate the service to point at the db and corruption on disk.
1718     service_.reset(new AppCacheService(NULL));
1719     service_->set_request_context(io_thread->request_context());
1720     service_->Initialize(
1721         temp_directory_.path(),
1722         db_thread->message_loop_proxy().get(),
1723         db_thread->message_loop_proxy().get());
1724     mock_quota_manager_proxy_ = new MockQuotaManagerProxy();
1725     service_->quota_manager_proxy_ = mock_quota_manager_proxy_;
1726     delegate_.reset(new MockStorageDelegate(this));
1727
1728     // Additional setup to observe reinitailize happens.
1729     observer_.reset(new MockServiceObserver(this));
1730     service_->AddObserver(observer_.get());
1731
1732     // We continue after the init task is complete including the callback
1733     // on the current thread.
1734     FlushDbThreadTasks();
1735     base::MessageLoop::current()->PostTask(
1736         FROM_HERE,
1737         base::Bind(&AppCacheStorageImplTest::Continue_Reinitialize,
1738                    base::Unretained(this),
1739                    test_case));
1740   }
1741
1742   void Continue_Reinitialize(ReinitTestCase test_case) {
1743     const int kMockProcessId = 1;
1744     backend_.reset(new AppCacheBackendImpl);
1745     backend_->Initialize(service_.get(), &frontend_, kMockProcessId);
1746
1747     if (test_case == CORRUPT_SQL_ON_INSTALL) {
1748       // Break the db file
1749       EXPECT_FALSE(database()->was_corruption_detected());
1750       ASSERT_TRUE(sql::test::CorruptSizeInHeader(
1751           temp_directory_.path().AppendASCII("Index")));
1752     }
1753
1754     if (test_case == CORRUPT_CACHE_ON_INSTALL  ||
1755         test_case == CORRUPT_SQL_ON_INSTALL) {
1756       // Try to create a new appcache, the resulting update job will
1757       // eventually fail when it gets to disk cache initialization.
1758       backend_->RegisterHost(1);
1759       AppCacheHost* host1 = backend_->GetHost(1);
1760       const GURL kEmptyPageUrl(MockHttpServer::GetMockUrl("empty.html"));
1761       host1->first_party_url_ = kEmptyPageUrl;
1762       host1->SelectCache(kEmptyPageUrl,
1763                          kNoCacheId,
1764                          MockHttpServer::GetMockUrl("manifest"));
1765     } else {
1766       ASSERT_EQ(CORRUPT_CACHE_ON_LOAD_EXISTING, test_case);
1767       // Try to access the existing cache manifest.
1768       // The URLRequestJob  will eventually fail when it gets to disk
1769       // cache initialization.
1770       backend_->RegisterHost(2);
1771       AppCacheHost* host2 = backend_->GetHost(2);
1772       GURL manifest_url = MockHttpServer::GetMockUrl("manifest");
1773       request_ = service()->request_context()->CreateRequest(
1774           manifest_url, net::DEFAULT_PRIORITY, NULL, NULL);
1775       AppCacheInterceptor::SetExtraRequestInfo(
1776           request_.get(), service_.get(),
1777           backend_->process_id(), host2->host_id(),
1778           ResourceType::MAIN_FRAME);
1779       request_->Start();
1780     }
1781
1782     PushNextTask(base::Bind(
1783         &AppCacheStorageImplTest::Verify_Reinitialized,
1784         base::Unretained(this),
1785         test_case));
1786   }
1787
1788   void Verify_Reinitialized(ReinitTestCase test_case) {
1789     // Verify we got notified of reinit and a new storage instance is created,
1790     // and that the old data has been deleted.
1791     EXPECT_TRUE(observer_->observed_old_storage_.get());
1792     EXPECT_TRUE(observer_->observed_old_storage_->storage() != storage());
1793     EXPECT_FALSE(PathExists(
1794         temp_directory_.path().AppendASCII("Cache").AppendASCII("index")));
1795     EXPECT_FALSE(PathExists(
1796         temp_directory_.path().AppendASCII("Index")));
1797
1798     if (test_case == CORRUPT_SQL_ON_INSTALL) {
1799       AppCacheStorageImpl* storage = static_cast<AppCacheStorageImpl*>(
1800           observer_->observed_old_storage_->storage());
1801       EXPECT_TRUE(storage->database_->was_corruption_detected());
1802     }
1803
1804     // Verify that the hosts saw appropriate events.
1805     if (test_case == CORRUPT_CACHE_ON_INSTALL ||
1806         test_case == CORRUPT_SQL_ON_INSTALL) {
1807       EXPECT_TRUE(frontend_.error_event_was_raised_);
1808       AppCacheHost* host1 = backend_->GetHost(1);
1809       EXPECT_FALSE(host1->associated_cache());
1810       EXPECT_FALSE(host1->group_being_updated_);
1811       EXPECT_TRUE(host1->disabled_storage_reference_.get());
1812     } else {
1813       ASSERT_EQ(CORRUPT_CACHE_ON_LOAD_EXISTING, test_case);
1814       AppCacheHost* host2 = backend_->GetHost(2);
1815       EXPECT_EQ(1, host2->main_resource_cache_->cache_id());
1816       EXPECT_TRUE(host2->disabled_storage_reference_.get());
1817     }
1818
1819     // Cleanup and claim victory.
1820     service_->RemoveObserver(observer_.get());
1821     request_.reset();
1822     backend_.reset();
1823     observer_.reset();
1824     TestFinished();
1825   }
1826
1827   // Test case helpers --------------------------------------------------
1828
1829   AppCacheService* service() {
1830     return service_.get();
1831   }
1832
1833   AppCacheStorageImpl* storage() {
1834     return static_cast<AppCacheStorageImpl*>(service()->storage());
1835   }
1836
1837   AppCacheDatabase* database() {
1838     return storage()->database_;
1839   }
1840
1841   MockStorageDelegate* delegate() {
1842     return delegate_.get();
1843   }
1844
1845   void MakeCacheAndGroup(
1846       const GURL& manifest_url, int64 group_id, int64 cache_id,
1847       bool add_to_database) {
1848     AppCacheEntry default_entry(
1849         AppCacheEntry::EXPLICIT, cache_id + kDefaultEntryIdOffset,
1850         kDefaultEntrySize);
1851     group_ = new AppCacheGroup(storage(), manifest_url, group_id);
1852     cache_ = new AppCache(storage(), cache_id);
1853     cache_->AddEntry(kDefaultEntryUrl, default_entry);
1854     cache_->set_complete(true);
1855     group_->AddCache(cache_.get());
1856     if (add_to_database) {
1857       AppCacheDatabase::GroupRecord group_record;
1858       group_record.group_id = group_id;
1859       group_record.manifest_url = manifest_url;
1860       group_record.origin = manifest_url.GetOrigin();
1861       EXPECT_TRUE(database()->InsertGroup(&group_record));
1862       AppCacheDatabase::CacheRecord cache_record;
1863       cache_record.cache_id = cache_id;
1864       cache_record.group_id = group_id;
1865       cache_record.online_wildcard = false;
1866       cache_record.update_time = kZeroTime;
1867       cache_record.cache_size = kDefaultEntrySize;
1868       EXPECT_TRUE(database()->InsertCache(&cache_record));
1869       AppCacheDatabase::EntryRecord entry_record;
1870       entry_record.cache_id = cache_id;
1871       entry_record.url = kDefaultEntryUrl;
1872       entry_record.flags = default_entry.types();
1873       entry_record.response_id = default_entry.response_id();
1874       entry_record.response_size = default_entry.response_size();
1875       EXPECT_TRUE(database()->InsertEntry(&entry_record));
1876
1877       storage()->usage_map_[manifest_url.GetOrigin()] =
1878           default_entry.response_size();
1879     }
1880   }
1881
1882   // Data members --------------------------------------------------
1883
1884   scoped_ptr<base::WaitableEvent> test_finished_event_;
1885   std::stack<base::Closure> task_stack_;
1886   scoped_ptr<AppCacheService> service_;
1887   scoped_ptr<MockStorageDelegate> delegate_;
1888   scoped_refptr<MockQuotaManagerProxy> mock_quota_manager_proxy_;
1889   scoped_refptr<AppCacheGroup> group_;
1890   scoped_refptr<AppCache> cache_;
1891   scoped_refptr<AppCache> cache2_;
1892
1893   // Specifically for the Reinitalize test.
1894   base::ScopedTempDir temp_directory_;
1895   scoped_ptr<MockServiceObserver> observer_;
1896   MockAppCacheFrontend frontend_;
1897   scoped_ptr<AppCacheBackendImpl> backend_;
1898   scoped_ptr<net::URLRequest> request_;
1899 };
1900
1901
1902 TEST_F(AppCacheStorageImplTest, LoadCache_Miss) {
1903   RunTestOnIOThread(&AppCacheStorageImplTest::LoadCache_Miss);
1904 }
1905
1906 TEST_F(AppCacheStorageImplTest, LoadCache_NearHit) {
1907   RunTestOnIOThread(&AppCacheStorageImplTest::LoadCache_NearHit);
1908 }
1909
1910 TEST_F(AppCacheStorageImplTest, CreateGroupInEmptyOrigin) {
1911   RunTestOnIOThread(&AppCacheStorageImplTest::CreateGroupInEmptyOrigin);
1912 }
1913
1914 TEST_F(AppCacheStorageImplTest, CreateGroupInPopulatedOrigin) {
1915   RunTestOnIOThread(&AppCacheStorageImplTest::CreateGroupInPopulatedOrigin);
1916 }
1917
1918 TEST_F(AppCacheStorageImplTest, LoadGroupAndCache_FarHit) {
1919   RunTestOnIOThread(&AppCacheStorageImplTest::LoadGroupAndCache_FarHit);
1920 }
1921
1922 TEST_F(AppCacheStorageImplTest, StoreNewGroup) {
1923   RunTestOnIOThread(&AppCacheStorageImplTest::StoreNewGroup);
1924 }
1925
1926 TEST_F(AppCacheStorageImplTest, StoreExistingGroup) {
1927   RunTestOnIOThread(&AppCacheStorageImplTest::StoreExistingGroup);
1928 }
1929
1930 TEST_F(AppCacheStorageImplTest, StoreExistingGroupExistingCache) {
1931   RunTestOnIOThread(&AppCacheStorageImplTest::StoreExistingGroupExistingCache);
1932 }
1933
1934 TEST_F(AppCacheStorageImplTest, FailStoreGroup) {
1935   RunTestOnIOThread(&AppCacheStorageImplTest::FailStoreGroup);
1936 }
1937
1938 TEST_F(AppCacheStorageImplTest, MakeGroupObsolete) {
1939   RunTestOnIOThread(&AppCacheStorageImplTest::MakeGroupObsolete);
1940 }
1941
1942 TEST_F(AppCacheStorageImplTest, MarkEntryAsForeign) {
1943   RunTestOnIOThread(&AppCacheStorageImplTest::MarkEntryAsForeign);
1944 }
1945
1946 TEST_F(AppCacheStorageImplTest, MarkEntryAsForeignWithLoadInProgress) {
1947   RunTestOnIOThread(
1948       &AppCacheStorageImplTest::MarkEntryAsForeignWithLoadInProgress);
1949 }
1950
1951 TEST_F(AppCacheStorageImplTest, FindNoMainResponse) {
1952   RunTestOnIOThread(&AppCacheStorageImplTest::FindNoMainResponse);
1953 }
1954
1955 TEST_F(AppCacheStorageImplTest, BasicFindMainResponseInDatabase) {
1956   RunTestOnIOThread(
1957       &AppCacheStorageImplTest::BasicFindMainResponseInDatabase);
1958 }
1959
1960 TEST_F(AppCacheStorageImplTest, BasicFindMainResponseInWorkingSet) {
1961   RunTestOnIOThread(
1962       &AppCacheStorageImplTest::BasicFindMainResponseInWorkingSet);
1963 }
1964
1965 TEST_F(AppCacheStorageImplTest, BasicFindMainFallbackResponseInDatabase) {
1966   RunTestOnIOThread(
1967       &AppCacheStorageImplTest::BasicFindMainFallbackResponseInDatabase);
1968 }
1969
1970 TEST_F(AppCacheStorageImplTest, BasicFindMainFallbackResponseInWorkingSet) {
1971   RunTestOnIOThread(
1972       &AppCacheStorageImplTest::BasicFindMainFallbackResponseInWorkingSet);
1973 }
1974
1975 TEST_F(AppCacheStorageImplTest, BasicFindMainInterceptResponseInDatabase) {
1976   RunTestOnIOThread(
1977       &AppCacheStorageImplTest::BasicFindMainInterceptResponseInDatabase);
1978 }
1979
1980 TEST_F(AppCacheStorageImplTest, BasicFindMainInterceptResponseInWorkingSet) {
1981   RunTestOnIOThread(
1982       &AppCacheStorageImplTest::BasicFindMainInterceptResponseInWorkingSet);
1983 }
1984
1985 TEST_F(AppCacheStorageImplTest, FindMainResponseWithMultipleHits) {
1986   RunTestOnIOThread(
1987       &AppCacheStorageImplTest::FindMainResponseWithMultipleHits);
1988 }
1989
1990 TEST_F(AppCacheStorageImplTest, FindMainResponseExclusionsInDatabase) {
1991   RunTestOnIOThread(
1992       &AppCacheStorageImplTest::FindMainResponseExclusionsInDatabase);
1993 }
1994
1995 TEST_F(AppCacheStorageImplTest, FindMainResponseExclusionsInWorkingSet) {
1996   RunTestOnIOThread(
1997       &AppCacheStorageImplTest::FindMainResponseExclusionsInWorkingSet);
1998 }
1999
2000 TEST_F(AppCacheStorageImplTest, FindInterceptPatternMatchInWorkingSet) {
2001   RunTestOnIOThread(
2002       &AppCacheStorageImplTest::FindInterceptPatternMatchInWorkingSet);
2003 }
2004
2005 TEST_F(AppCacheStorageImplTest, FindInterceptPatternMatchInDatabase) {
2006   RunTestOnIOThread(
2007       &AppCacheStorageImplTest::FindInterceptPatternMatchInDatabase);
2008 }
2009
2010 TEST_F(AppCacheStorageImplTest, FindFallbackPatternMatchInWorkingSet) {
2011   RunTestOnIOThread(
2012       &AppCacheStorageImplTest::FindFallbackPatternMatchInWorkingSet);
2013 }
2014
2015 TEST_F(AppCacheStorageImplTest, FindFallbackPatternMatchInDatabase) {
2016   RunTestOnIOThread(
2017       &AppCacheStorageImplTest::FindFallbackPatternMatchInDatabase);
2018 }
2019
2020 TEST_F(AppCacheStorageImplTest, Reinitialize1) {
2021   RunTestOnIOThread(&AppCacheStorageImplTest::Reinitialize1);
2022 }
2023
2024 TEST_F(AppCacheStorageImplTest, Reinitialize2) {
2025   RunTestOnIOThread(&AppCacheStorageImplTest::Reinitialize2);
2026 }
2027
2028 TEST_F(AppCacheStorageImplTest, Reinitialize3) {
2029   RunTestOnIOThread(&AppCacheStorageImplTest::Reinitialize3);
2030 }
2031
2032 // That's all folks!
2033
2034 }  // namespace content