diff options
author | michaeln@google.com <michaeln@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-10-24 21:42:25 +0000 |
---|---|---|
committer | michaeln@google.com <michaeln@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-10-24 21:42:25 +0000 |
commit | 550890ec7f1cf968abc4544b170e97a67f919902 (patch) | |
tree | 632dec32bed5266837a2668a9225d303303e3160 /webkit/appcache/mock_appcache_storage_unittest.cc | |
parent | ed249ab7916d438b0bdb650a796aaf709f6ae1ae (diff) | |
download | chromium_src-550890ec7f1cf968abc4544b170e97a67f919902.zip chromium_src-550890ec7f1cf968abc4544b170e97a67f919902.tar.gz chromium_src-550890ec7f1cf968abc4544b170e97a67f919902.tar.bz2 |
MockAppCacheStorage implemention
This is a quick and easy 'mock' implementation of the storage interface
that doesn't put anything to disk.
We simply add an extra reference to objects when they're put in storage,
and remove the extra reference when they are removed from storage.
Responses are never really removed from the in-memory disk cache.
Delegate callbacks are made asyncly to appropiately mimic what will
happen with a real disk-backed storage impl that involves IO on a
background thread.
This is for use in unit tests and to initially bring up the appcache
related layout tests.
TEST=mock_appcache_storage_unittest.cc
BUG=none
Review URL: http://codereview.chromium.org/300043
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@30017 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'webkit/appcache/mock_appcache_storage_unittest.cc')
-rw-r--r-- | webkit/appcache/mock_appcache_storage_unittest.cc | 358 |
1 files changed, 358 insertions, 0 deletions
diff --git a/webkit/appcache/mock_appcache_storage_unittest.cc b/webkit/appcache/mock_appcache_storage_unittest.cc new file mode 100644 index 0000000..60cd801 --- /dev/null +++ b/webkit/appcache/mock_appcache_storage_unittest.cc @@ -0,0 +1,358 @@ +// Copyright (c) 2009 The Chromium Authos. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "base/message_loop.h" +#include "testing/gtest/include/gtest/gtest.h" +#include "webkit/appcache/appcache.h" +#include "webkit/appcache/appcache_group.h" +#include "webkit/appcache/appcache_response.h" +#include "webkit/appcache/appcache_storage.h" +#include "webkit/appcache/mock_appcache_service.h" + +namespace appcache { + +class MockAppCacheStorageTest : public testing::Test { + public: + class MockStorageDelegate : public AppCacheStorage::Delegate { + public: + explicit MockStorageDelegate() + : loaded_cache_id_(0), stored_group_success_(false), + obsoleted_success_(false), found_cache_id_(kNoCacheId) { + } + + void OnCacheLoaded(AppCache* cache, int64 cache_id) { + loaded_cache_ = cache; + loaded_cache_id_ = cache_id; + } + + void OnGroupLoaded(AppCacheGroup* group, const GURL& manifest_url) { + loaded_group_ = group; + loaded_manifest_url_ = manifest_url; + } + + void OnGroupAndNewestCacheStored(AppCacheGroup* group, bool success) { + stored_group_ = group; + stored_group_success_ = success; + } + + void OnGroupMadeObsolete(AppCacheGroup* group, bool success) { + obsoleted_group_ = group; + obsoleted_success_ = success; + } + + void OnMainResponseFound(const GURL& url, const AppCacheEntry& entry, + int64 cache_id, const GURL& manifest_url) { + found_url_ = url; + found_entry_ = entry; + found_cache_id_ = cache_id; + found_manifest_url_ = manifest_url; + } + + scoped_refptr<AppCache> loaded_cache_; + int64 loaded_cache_id_; + scoped_refptr<AppCacheGroup> loaded_group_; + GURL loaded_manifest_url_; + scoped_refptr<AppCacheGroup> stored_group_; + bool stored_group_success_; + scoped_refptr<AppCacheGroup> obsoleted_group_; + bool obsoleted_success_; + GURL found_url_; + AppCacheEntry found_entry_; + int64 found_cache_id_; + GURL found_manifest_url_; + }; +}; + + +TEST_F(MockAppCacheStorageTest, LoadCache_Miss) { + // Attempt to load a cache that doesn't exist. Should + // complete asyncly. + MockAppCacheService service; + MockStorageDelegate delegate; + service.storage()->LoadCache(111, &delegate); + EXPECT_NE(111, delegate.loaded_cache_id_); + MessageLoop::current()->RunAllPending(); // Do async task execution. + EXPECT_EQ(111, delegate.loaded_cache_id_); + EXPECT_FALSE(delegate.loaded_cache_); +} + +TEST_F(MockAppCacheStorageTest, LoadCache_NearHit) { + // Attempt to load a cache that is currently in use + // and does not require loading from disk. This + // load should complete syncly. + MockAppCacheService service; + + // Setup some preconditions. Make an 'unstored' cache for + // us to load. The ctor should put it in the working set. + int64 cache_id = service.storage()->NewCacheId(); + scoped_refptr<AppCache> cache = new AppCache(&service, cache_id); + + // Conduct the test. + MockStorageDelegate delegate; + service.storage()->LoadCache(cache_id, &delegate); + EXPECT_EQ(cache_id, delegate.loaded_cache_id_); + EXPECT_EQ(cache.get(), delegate.loaded_cache_.get()); +} + +TEST_F(MockAppCacheStorageTest, CreateGroup) { + // Attempt to load/create a group that doesn't exist. + // Should complete asyncly. + MockAppCacheService service; + MockAppCacheStorage* storage = + reinterpret_cast<MockAppCacheStorage*>(service.storage()); + MockStorageDelegate delegate; + GURL manifest_url("http://blah/"); + service.storage()->LoadOrCreateGroup(manifest_url, &delegate); + EXPECT_NE(manifest_url, delegate.loaded_manifest_url_); + EXPECT_FALSE(delegate.loaded_group_.get()); + MessageLoop::current()->RunAllPending(); // Do async task execution. + EXPECT_EQ(manifest_url, delegate.loaded_manifest_url_); + EXPECT_TRUE(delegate.loaded_group_.get()); + EXPECT_TRUE(delegate.loaded_group_->HasOneRef()); + EXPECT_FALSE(delegate.loaded_group_->newest_complete_cache()); + EXPECT_TRUE(storage->stored_groups_.empty()); +} + +TEST_F(MockAppCacheStorageTest, LoadGroup_NearHit) { + // Attempt to load a group that is currently in use + // and does not require loading from disk. This + // load should complete syncly. + MockAppCacheService service; + MockStorageDelegate delegate; + + // Setup some preconditions. Create a group that appears + // to be "unstored" and "currently in use". + GURL manifest_url("http://blah/"); + service.storage()->LoadOrCreateGroup(manifest_url, &delegate); + MessageLoop::current()->RunAllPending(); // Do async task execution. + EXPECT_EQ(manifest_url, delegate.loaded_manifest_url_); + EXPECT_TRUE(delegate.loaded_group_.get()); + + // Reset our delegate, and take a reference to the new group. + scoped_refptr<AppCacheGroup> group; + group.swap(delegate.loaded_group_); + delegate.loaded_manifest_url_ = GURL(); + + // Conduct the test. + service.storage()->LoadOrCreateGroup(manifest_url, &delegate); + EXPECT_EQ(manifest_url, delegate.loaded_manifest_url_); + EXPECT_EQ(group.get(), delegate.loaded_group_.get()); +} + +TEST_F(MockAppCacheStorageTest, LoadGroupAndCache_FarHit) { + // Attempt to load a cache that is not currently in use + // and does require loading from disk. This + // load should complete asyncly. + MockAppCacheService service; + MockAppCacheStorage* storage = + reinterpret_cast<MockAppCacheStorage*>(service.storage()); + + // Setup some preconditions. Create a group and newest cache that + // appears to be "stored" and "not currently in use". + GURL manifest_url("http://blah/"); + scoped_refptr<AppCacheGroup> group = + new AppCacheGroup(&service, manifest_url); + int64 cache_id = storage->NewCacheId(); + scoped_refptr<AppCache> cache = new AppCache(&service, cache_id); + cache->set_complete(true); + group->AddCache(cache); + storage->AddStoredGroup(group); + storage->AddStoredCache(cache); + + // Drop the references from above so the only refs to these + // objects are from within the storage class. This is to make + // these objects appear as "not currently in use". + AppCache* cache_ptr = cache.get(); + AppCacheGroup* group_ptr = group.get(); + cache = NULL; + group = NULL; + + // Setup a delegate to receive completion callbacks. + MockStorageDelegate delegate; + + // Conduct the cache load test. + EXPECT_NE(cache_id, delegate.loaded_cache_id_); + EXPECT_NE(cache_ptr, delegate.loaded_cache_.get()); + storage->LoadCache(cache_id, &delegate); + EXPECT_NE(cache_id, delegate.loaded_cache_id_); + EXPECT_NE(cache_ptr, delegate.loaded_cache_.get()); + MessageLoop::current()->RunAllPending(); // Do async task execution. + EXPECT_EQ(cache_id, delegate.loaded_cache_id_); + EXPECT_EQ(cache_ptr, delegate.loaded_cache_.get()); + delegate.loaded_cache_ = NULL; + + // Conduct the group load test. + EXPECT_NE(manifest_url, delegate.loaded_manifest_url_); + EXPECT_FALSE(delegate.loaded_group_.get()); + storage->LoadOrCreateGroup(manifest_url, &delegate); + EXPECT_NE(manifest_url, delegate.loaded_manifest_url_); + EXPECT_FALSE(delegate.loaded_group_.get()); + MessageLoop::current()->RunAllPending(); // Do async task execution. + EXPECT_EQ(manifest_url, delegate.loaded_manifest_url_); + EXPECT_EQ(group_ptr, delegate.loaded_group_.get()); +} + +TEST_F(MockAppCacheStorageTest, StoreNewGroup) { + // Store a group and its newest cache. Should complete asyncly. + MockAppCacheService service; + MockAppCacheStorage* storage = + reinterpret_cast<MockAppCacheStorage*>(service.storage()); + + // Setup some preconditions. Create a group and newest cache that + // appears to be "unstored". + GURL manifest_url("http://blah/"); + scoped_refptr<AppCacheGroup> group = + new AppCacheGroup(&service, manifest_url); + int64 cache_id = storage->NewCacheId(); + scoped_refptr<AppCache> cache = new AppCache(&service, cache_id); + cache->set_complete(true); + group->AddCache(cache); + // Hold a ref to the cache simulate the UpdateJob holding that ref, + // and hold a ref to the group to simulate the CacheHost holding that ref. + + // Conduct the store test. + MockStorageDelegate delegate; + EXPECT_TRUE(storage->stored_caches_.empty()); + EXPECT_TRUE(storage->stored_groups_.empty()); + storage->StoreGroupAndNewestCache(group, &delegate); + EXPECT_FALSE(delegate.stored_group_success_); + EXPECT_TRUE(storage->stored_caches_.empty()); + EXPECT_TRUE(storage->stored_groups_.empty()); + MessageLoop::current()->RunAllPending(); // Do async task execution. + EXPECT_TRUE(delegate.stored_group_success_); + EXPECT_FALSE(storage->stored_caches_.empty()); + EXPECT_FALSE(storage->stored_groups_.empty()); +} + +TEST_F(MockAppCacheStorageTest, StoreExistingGroup) { + // Store a group and its newest cache. Should complete asyncly. + MockAppCacheService service; + MockAppCacheStorage* storage = + reinterpret_cast<MockAppCacheStorage*>(service.storage()); + + // Setup some preconditions. Create a group and old complete cache + // that appear to be "stored", and a newest unstored complete cache. + GURL manifest_url("http://blah/"); + scoped_refptr<AppCacheGroup> group = + new AppCacheGroup(&service, manifest_url); + int64 old_cache_id = storage->NewCacheId(); + scoped_refptr<AppCache> old_cache = new AppCache(&service, old_cache_id); + old_cache->set_complete(true); + group->AddCache(old_cache); + storage->AddStoredGroup(group); + storage->AddStoredCache(old_cache); + int64 new_cache_id = storage->NewCacheId(); + scoped_refptr<AppCache> new_cache = new AppCache(&service, new_cache_id); + new_cache->set_complete(true); + group->AddCache(new_cache); + EXPECT_EQ(new_cache.get(), group->newest_complete_cache()); + // Hold our refs to simulate the UpdateJob holding these refs. + + // Conduct the test. + MockStorageDelegate delegate; + EXPECT_EQ(size_t(1), storage->stored_caches_.size()); + EXPECT_EQ(size_t(1), storage->stored_groups_.size()); + EXPECT_TRUE(storage->IsCacheStored(old_cache)); + EXPECT_FALSE(storage->IsCacheStored(new_cache)); + storage->StoreGroupAndNewestCache(group, &delegate); + EXPECT_FALSE(delegate.stored_group_success_); + EXPECT_EQ(size_t(1), storage->stored_caches_.size()); + EXPECT_EQ(size_t(1), storage->stored_groups_.size()); + EXPECT_TRUE(storage->IsCacheStored(old_cache)); + EXPECT_FALSE(storage->IsCacheStored(new_cache)); + MessageLoop::current()->RunAllPending(); // Do async task execution. + EXPECT_TRUE(delegate.stored_group_success_); + EXPECT_EQ(size_t(1), storage->stored_caches_.size()); + EXPECT_EQ(size_t(1), storage->stored_groups_.size()); + EXPECT_FALSE(storage->IsCacheStored(old_cache)); + EXPECT_TRUE(storage->IsCacheStored(new_cache)); +} + +TEST_F(MockAppCacheStorageTest, MakeGroupObsolete) { + // Make a group obsolete, should complete asyncly. + MockAppCacheService service; + MockAppCacheStorage* storage = + reinterpret_cast<MockAppCacheStorage*>(service.storage()); + + // Setup some preconditions. Create a group and newest cache that + // appears to be "stored" and "currently in use". + GURL manifest_url("http://blah/"); + scoped_refptr<AppCacheGroup> group = + new AppCacheGroup(&service, manifest_url); + int64 cache_id = storage->NewCacheId(); + scoped_refptr<AppCache> cache = new AppCache(&service, cache_id); + cache->set_complete(true); + group->AddCache(cache); + storage->AddStoredGroup(group); + storage->AddStoredCache(cache); + // Hold our refs to simulate the UpdateJob holding these refs. + + // Conduct the test. + MockStorageDelegate delegate; + EXPECT_FALSE(group->is_obsolete()); + EXPECT_EQ(size_t(1), storage->stored_caches_.size()); + EXPECT_EQ(size_t(1), storage->stored_groups_.size()); + EXPECT_FALSE(cache->HasOneRef()); + EXPECT_FALSE(group->HasOneRef()); + storage->MakeGroupObsolete(group, &delegate); + EXPECT_FALSE(group->is_obsolete()); + EXPECT_EQ(size_t(1), storage->stored_caches_.size()); + EXPECT_EQ(size_t(1), storage->stored_groups_.size()); + EXPECT_FALSE(cache->HasOneRef()); + EXPECT_FALSE(group->HasOneRef()); + MessageLoop::current()->RunAllPending(); // Do async task execution. + EXPECT_TRUE(delegate.obsoleted_success_); + EXPECT_EQ(group.get(), delegate.obsoleted_group_.get()); + EXPECT_TRUE(group->is_obsolete()); + EXPECT_TRUE(storage->stored_caches_.empty()); + EXPECT_TRUE(storage->stored_groups_.empty()); + EXPECT_TRUE(cache->HasOneRef()); + EXPECT_FALSE(group->HasOneRef()); + delegate.obsoleted_group_ = NULL; + cache = NULL; + EXPECT_TRUE(group->HasOneRef()); +} + +TEST_F(MockAppCacheStorageTest, MarkEntryAsForeign) { + // Should complete syncly. + MockAppCacheService service; + MockAppCacheStorage* storage = + reinterpret_cast<MockAppCacheStorage*>(service.storage()); + + // Setup some preconditions. Create a cache with an entry. + GURL entry_url("http://blan/entry"); + int64 cache_id = storage->NewCacheId(); + scoped_refptr<AppCache> cache = new AppCache(&service, cache_id); + cache->AddEntry(entry_url, AppCacheEntry(AppCacheEntry::EXPLICIT)); + + // Conduct the test. + MockStorageDelegate delegate; + EXPECT_FALSE(cache->GetEntry(entry_url)->IsForeign()); + storage->MarkEntryAsForeign(entry_url, cache_id); + EXPECT_TRUE(cache->GetEntry(entry_url)->IsForeign()); + EXPECT_TRUE(cache->GetEntry(entry_url)->IsExplicit()); +} + +TEST_F(MockAppCacheStorageTest, FindNoMainResponse) { + // Should complete asyncly. + MockAppCacheService service; + MockAppCacheStorage* storage = + reinterpret_cast<MockAppCacheStorage*>(service.storage()); + + // Conduct the test. + MockStorageDelegate delegate; + GURL url("http://blah/some_url"); + EXPECT_NE(url, delegate.found_url_); + storage->FindResponseForMainRequest(url, &delegate); + EXPECT_NE(url, delegate.found_url_); + MessageLoop::current()->RunAllPending(); // Do async task execution. + EXPECT_EQ(url, delegate.found_url_); + EXPECT_TRUE(delegate.found_manifest_url_.is_empty()); + EXPECT_EQ(kNoCacheId, delegate.found_cache_id_); + EXPECT_EQ(kNoResponseId, delegate.found_entry_.response_id()); + EXPECT_EQ(0, delegate.found_entry_.types()); +} + +} // namespace appcache + |