diff options
author | kinuko@chromium.org <kinuko@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-09-21 12:09:59 +0000 |
---|---|---|
committer | kinuko@chromium.org <kinuko@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-09-21 12:09:59 +0000 |
commit | 849d2167057a0bf54c95ca4f743e136555a13150 (patch) | |
tree | 70ad56e7af885ad312403d4f1642e57873d46c5b | |
parent | ead34cae2197353705a47f12572081446816c963 (diff) | |
download | chromium_src-849d2167057a0bf54c95ca4f743e136555a13150.zip chromium_src-849d2167057a0bf54c95ca4f743e136555a13150.tar.gz chromium_src-849d2167057a0bf54c95ca4f743e136555a13150.tar.bz2 |
Merge MockQuotaManager in multiple places
- to share the single implementation in existing tests
- to make it easier for writing new tests
BUG=none
TEST=existing tests
Review URL: https://chromiumcodereview.appspot.com/10915202
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@157964 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | webkit/appcache/appcache_storage_unittest.cc | 88 | ||||
-rw-r--r-- | webkit/fileapi/local_file_system_operation_unittest.cc | 176 | ||||
-rw-r--r-- | webkit/quota/mock_quota_manager.cc | 71 | ||||
-rw-r--r-- | webkit/quota/mock_quota_manager.h | 168 |
4 files changed, 267 insertions, 236 deletions
diff --git a/webkit/appcache/appcache_storage_unittest.cc b/webkit/appcache/appcache_storage_unittest.cc index 8021e30..a5ad0b7 100644 --- a/webkit/appcache/appcache_storage_unittest.cc +++ b/webkit/appcache/appcache_storage_unittest.cc @@ -9,57 +9,19 @@ #include "webkit/appcache/appcache_response.h" #include "webkit/appcache/appcache_storage.h" #include "webkit/appcache/mock_appcache_service.h" +#include "webkit/quota/mock_quota_manager.h" namespace appcache { +namespace { +const quota::StorageType kTemp = quota::kStorageTypeTemporary; +} + class AppCacheStorageTest : public testing::Test { public: class MockStorageDelegate : public AppCacheStorage::Delegate { public: }; - - class MockQuotaManagerProxy : public quota::QuotaManagerProxy { - public: - MockQuotaManagerProxy() - : QuotaManagerProxy(NULL, NULL), - notify_storage_accessed_count_(0), - notify_storage_modified_count_(0), - last_delta_(0) { - } - - virtual void NotifyStorageAccessed(quota::QuotaClient::ID client_id, - const GURL& origin, - quota::StorageType type) OVERRIDE { - EXPECT_EQ(quota::QuotaClient::kAppcache, client_id); - EXPECT_EQ(quota::kStorageTypeTemporary, type); - ++notify_storage_accessed_count_; - last_origin_ = origin; - } - - virtual void NotifyStorageModified(quota::QuotaClient::ID client_id, - const GURL& origin, - quota::StorageType type, - int64 delta) OVERRIDE { - EXPECT_EQ(quota::QuotaClient::kAppcache, client_id); - EXPECT_EQ(quota::kStorageTypeTemporary, type); - ++notify_storage_modified_count_; - last_origin_ = origin; - last_delta_ = delta; - } - - // Not needed for our tests. - virtual void RegisterClient(quota::QuotaClient* client) OVERRIDE {} - virtual void NotifyOriginInUse(const GURL& origin) OVERRIDE {} - virtual void NotifyOriginNoLongerInUse(const GURL& origin) OVERRIDE {} - - int notify_storage_accessed_count_; - int notify_storage_modified_count_; - GURL last_origin_; - int last_delta_; - - protected: - virtual ~MockQuotaManagerProxy() {} - }; }; TEST_F(AppCacheStorageTest, AddRemoveCache) { @@ -155,41 +117,47 @@ TEST_F(AppCacheStorageTest, UsageMap) { const GURL kOrigin2("http://origin2/"); MockAppCacheService service; - scoped_refptr<MockQuotaManagerProxy> mock_proxy(new MockQuotaManagerProxy); + scoped_refptr<quota::MockQuotaManagerProxy> mock_proxy( + new quota::MockQuotaManagerProxy(NULL, NULL)); service.set_quota_manager_proxy(mock_proxy); service.storage()->UpdateUsageMapAndNotify(kOrigin, 0); - EXPECT_EQ(0, mock_proxy->notify_storage_modified_count_); + EXPECT_EQ(0, mock_proxy->notify_storage_modified_count()); service.storage()->UpdateUsageMapAndNotify(kOrigin, 10); - EXPECT_EQ(1, mock_proxy->notify_storage_modified_count_); - EXPECT_EQ(10, mock_proxy->last_delta_); - EXPECT_EQ(kOrigin, mock_proxy->last_origin_); + EXPECT_EQ(1, mock_proxy->notify_storage_modified_count()); + EXPECT_EQ(10, mock_proxy->last_notified_delta()); + EXPECT_EQ(kOrigin, mock_proxy->last_notified_origin()); + EXPECT_EQ(kTemp, mock_proxy->last_notified_type()); service.storage()->UpdateUsageMapAndNotify(kOrigin, 100); - EXPECT_EQ(2, mock_proxy->notify_storage_modified_count_); - EXPECT_EQ(90, mock_proxy->last_delta_); - EXPECT_EQ(kOrigin, mock_proxy->last_origin_); + EXPECT_EQ(2, mock_proxy->notify_storage_modified_count()); + EXPECT_EQ(90, mock_proxy->last_notified_delta()); + EXPECT_EQ(kOrigin, mock_proxy->last_notified_origin()); + EXPECT_EQ(kTemp, mock_proxy->last_notified_type()); service.storage()->UpdateUsageMapAndNotify(kOrigin, 0); - EXPECT_EQ(3, mock_proxy->notify_storage_modified_count_); - EXPECT_EQ(-100, mock_proxy->last_delta_); - EXPECT_EQ(kOrigin, mock_proxy->last_origin_); + EXPECT_EQ(3, mock_proxy->notify_storage_modified_count()); + EXPECT_EQ(-100, mock_proxy->last_notified_delta()); + EXPECT_EQ(kOrigin, mock_proxy->last_notified_origin()); + EXPECT_EQ(kTemp, mock_proxy->last_notified_type()); service.storage()->NotifyStorageAccessed(kOrigin2); - EXPECT_EQ(0, mock_proxy->notify_storage_accessed_count_); + EXPECT_EQ(0, mock_proxy->notify_storage_accessed_count()); service.storage()->usage_map_[kOrigin2] = 1; service.storage()->NotifyStorageAccessed(kOrigin2); - EXPECT_EQ(1, mock_proxy->notify_storage_accessed_count_); - EXPECT_EQ(kOrigin2, mock_proxy->last_origin_); + EXPECT_EQ(1, mock_proxy->notify_storage_accessed_count()); + EXPECT_EQ(kOrigin2, mock_proxy->last_notified_origin()); + EXPECT_EQ(kTemp, mock_proxy->last_notified_type()); service.storage()->usage_map_.clear(); service.storage()->usage_map_[kOrigin] = 5000; service.storage()->ClearUsageMapAndNotify(); - EXPECT_EQ(4, mock_proxy->notify_storage_modified_count_); - EXPECT_EQ(-5000, mock_proxy->last_delta_); - EXPECT_EQ(kOrigin, mock_proxy->last_origin_); + EXPECT_EQ(4, mock_proxy->notify_storage_modified_count()); + EXPECT_EQ(-5000, mock_proxy->last_notified_delta()); + EXPECT_EQ(kOrigin, mock_proxy->last_notified_origin()); + EXPECT_EQ(kTemp, mock_proxy->last_notified_type()); EXPECT_TRUE(service.storage()->usage_map_.empty()); } diff --git a/webkit/fileapi/local_file_system_operation_unittest.cc b/webkit/fileapi/local_file_system_operation_unittest.cc index 44a1bf9..980e61b 100644 --- a/webkit/fileapi/local_file_system_operation_unittest.cc +++ b/webkit/fileapi/local_file_system_operation_unittest.cc @@ -23,11 +23,10 @@ #include "webkit/fileapi/local_file_system_test_helper.h" #include "webkit/fileapi/mock_file_change_observer.h" #include "webkit/quota/quota_manager.h" +#include "webkit/quota/mock_quota_manager.h" -using quota::QuotaClient; using quota::QuotaManager; using quota::QuotaManagerProxy; -using quota::StorageType; using webkit_blob::ShareableFileReference; namespace fileapi { @@ -41,120 +40,6 @@ void AssertFileErrorEq(base::PlatformFileError expected, ASSERT_EQ(expected, actual); } -class MockQuotaManager : public QuotaManager { - public: - MockQuotaManager(const FilePath& base_dir, - const GURL& origin, - StorageType type) - : QuotaManager(false /* is_incognito */, base_dir, - base::MessageLoopProxy::current(), - base::MessageLoopProxy::current(), - NULL), - origin_(origin), - type_(type), - usage_(0), - quota_(kint64max), - accessed_(0) {} - - virtual void GetUsageAndQuota( - const GURL& origin, quota::StorageType type, - const GetUsageAndQuotaCallback& callback) OVERRIDE { - EXPECT_EQ(origin_, origin); - EXPECT_EQ(type_, type); - callback.Run(quota::kQuotaStatusOk, usage_, quota_); - } - - protected: - virtual ~MockQuotaManager() {} - - private: - friend class MockQuotaManagerProxy; - - void SetQuota(const GURL& origin, StorageType type, int64 quota) { - EXPECT_EQ(origin_, origin); - EXPECT_EQ(type_, type); - quota_ = quota; - } - - void RecordStorageAccessed(const GURL& origin, StorageType type) { - EXPECT_EQ(origin_, origin); - EXPECT_EQ(type_, type); - ++accessed_; - } - - void UpdateUsage(const GURL& origin, StorageType type, int64 delta) { - EXPECT_EQ(origin_, origin); - EXPECT_EQ(type_, type); - usage_ += delta; - } - - const GURL& origin_; - const StorageType type_; - int64 usage_; - int64 quota_; - int accessed_; -}; - -class MockQuotaManagerProxy : public QuotaManagerProxy { - public: - explicit MockQuotaManagerProxy(QuotaManager* quota_manager) - : QuotaManagerProxy(quota_manager, - base::MessageLoopProxy::current()), - registered_client_(NULL) { - } - - virtual void RegisterClient(QuotaClient* client) OVERRIDE { - EXPECT_FALSE(registered_client_); - registered_client_ = client; - } - - void SimulateQuotaManagerDestroyed() { - if (registered_client_) { - // We cannot call this in the destructor as the client (indirectly) - // holds a refptr of the proxy. - registered_client_->OnQuotaManagerDestroyed(); - registered_client_ = NULL; - } - } - - // We don't mock them. - virtual void NotifyOriginInUse(const GURL& origin) OVERRIDE {} - virtual void NotifyOriginNoLongerInUse(const GURL& origin) OVERRIDE {} - - virtual void NotifyStorageAccessed(QuotaClient::ID client_id, - const GURL& origin, - StorageType type) OVERRIDE { - mock_manager()->RecordStorageAccessed(origin, type); - } - - virtual void NotifyStorageModified(QuotaClient::ID client_id, - const GURL& origin, - StorageType type, - int64 delta) OVERRIDE { - mock_manager()->UpdateUsage(origin, type, delta); - } - - int storage_accessed_count() const { - return mock_manager()->accessed_; - } - - void SetQuota(const GURL& origin, StorageType type, int64 quota) { - mock_manager()->SetQuota(origin, type, quota); - } - - protected: - virtual ~MockQuotaManagerProxy() { - EXPECT_FALSE(registered_client_); - } - - private: - MockQuotaManager* mock_manager() const { - return static_cast<MockQuotaManager*>(quota_manager()); - } - - QuotaClient* registered_client_; -}; - FilePath ASCIIToFilePath(const std::string& str) { return FilePath().AppendASCII(str); } @@ -192,8 +77,13 @@ class LocalFileSystemOperationTest // Common temp base for nondestructive uses. ScopedTempDir base_; - MockQuotaManagerProxy* quota_manager_proxy() { - return static_cast<MockQuotaManagerProxy*>(quota_manager_proxy_.get()); + quota::MockQuotaManager* quota_manager() { + return static_cast<quota::MockQuotaManager*>(quota_manager_.get()); + } + + quota::MockQuotaManagerProxy* quota_manager_proxy() { + return static_cast<quota::MockQuotaManagerProxy*>( + quota_manager_proxy_.get()); } FileSystemFileUtil* file_util() { @@ -370,17 +260,17 @@ class LocalFileSystemOperationTest void GrantQuotaForCurrentUsage() { int64 usage; GetUsageAndQuota(&usage, NULL); - quota_manager_proxy()->SetQuota(test_helper_.origin(), - test_helper_.storage_type(), - usage); + quota_manager()->SetQuota(test_helper_.origin(), + test_helper_.storage_type(), + usage); } void AddQuota(int64 quota_delta) { int64 quota; GetUsageAndQuota(NULL, "a); - quota_manager_proxy()->SetQuota(test_helper_.origin(), - test_helper_.storage_type(), - quota + quota_delta); + quota_manager()->SetQuota(test_helper_.origin(), + test_helper_.storage_type(), + quota + quota_delta); } // For post-operation status. @@ -405,9 +295,14 @@ class LocalFileSystemOperationTest void LocalFileSystemOperationTest::SetUp() { FilePath base_dir = base_.path().AppendASCII("filesystem"); - quota_manager_ = new MockQuotaManager( - base_dir, test_helper_.origin(), test_helper_.storage_type()); - quota_manager_proxy_ = new MockQuotaManagerProxy(quota_manager_.get()); + quota_manager_ = new quota::MockQuotaManager( + false /* is_incognito */, base_dir, + base::MessageLoopProxy::current(), + base::MessageLoopProxy::current(), + NULL /* special storage policy */); + quota_manager_proxy_ = new quota::MockQuotaManagerProxy( + quota_manager(), + base::MessageLoopProxy::current()); test_helper_.SetUp(base_dir, false /* unlimited quota */, quota_manager_proxy_.get(), @@ -519,7 +414,7 @@ TEST_F(LocalFileSystemOperationTest, TestMoveSuccessSrcFileAndOverwrite) { // Move is considered 'write' access (for both side), and won't be counted // as read access. - EXPECT_EQ(0, quota_manager_proxy()->storage_accessed_count()); + EXPECT_EQ(0, quota_manager_proxy()->notify_storage_accessed_count()); } TEST_F(LocalFileSystemOperationTest, TestMoveSuccessSrcFileAndNew) { @@ -719,7 +614,7 @@ TEST_F(LocalFileSystemOperationTest, TestCopySuccessSrcFileAndOverwrite) { MessageLoop::current()->RunAllPending(); EXPECT_EQ(base::PLATFORM_FILE_OK, status()); EXPECT_TRUE(FileExists(dest_file_path)); - EXPECT_EQ(1, quota_manager_proxy()->storage_accessed_count()); + EXPECT_EQ(1, quota_manager_proxy()->notify_storage_accessed_count()); EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count()); EXPECT_TRUE(change_observer()->HasNoChange()); @@ -736,7 +631,7 @@ TEST_F(LocalFileSystemOperationTest, TestCopySuccessSrcFileAndNew) { MessageLoop::current()->RunAllPending(); EXPECT_EQ(base::PLATFORM_FILE_OK, status()); EXPECT_TRUE(FileExists(dest_file_path)); - EXPECT_EQ(1, quota_manager_proxy()->storage_accessed_count()); + EXPECT_EQ(1, quota_manager_proxy()->notify_storage_accessed_count()); EXPECT_EQ(1, change_observer()->get_and_reset_create_file_from_count()); EXPECT_TRUE(change_observer()->HasNoChange()); @@ -755,7 +650,7 @@ TEST_F(LocalFileSystemOperationTest, TestCopySuccessSrcDirAndOverwrite) { EXPECT_TRUE(DirectoryExists(dest_dir_path)); EXPECT_FALSE(DirectoryExists( dest_dir_path.Append(VirtualPath::BaseName(src_dir_path)))); - EXPECT_EQ(1, quota_manager_proxy()->storage_accessed_count()); + EXPECT_EQ(1, quota_manager_proxy()->notify_storage_accessed_count()); EXPECT_EQ(1, change_observer()->get_and_reset_remove_directory_count()); EXPECT_EQ(1, change_observer()->get_and_reset_create_directory_count()); @@ -773,7 +668,7 @@ TEST_F(LocalFileSystemOperationTest, TestCopySuccessSrcDirAndNew) { MessageLoop::current()->RunAllPending(); EXPECT_EQ(base::PLATFORM_FILE_OK, status()); EXPECT_TRUE(DirectoryExists(dest_child_dir_path)); - EXPECT_EQ(1, quota_manager_proxy()->storage_accessed_count()); + EXPECT_EQ(1, quota_manager_proxy()->notify_storage_accessed_count()); EXPECT_EQ(1, change_observer()->get_and_reset_create_directory_count()); EXPECT_TRUE(change_observer()->HasNoChange()); @@ -795,7 +690,7 @@ TEST_F(LocalFileSystemOperationTest, TestCopySuccessSrcDirRecursive) { EXPECT_TRUE(FileExists(dest_dir_path.Append( VirtualPath::BaseName(child_dir_path)).Append( VirtualPath::BaseName(grandchild_file_path)))); - EXPECT_EQ(1, quota_manager_proxy()->storage_accessed_count()); + EXPECT_EQ(1, quota_manager_proxy()->notify_storage_accessed_count()); EXPECT_EQ(2, change_observer()->get_and_reset_create_directory_count()); EXPECT_EQ(1, change_observer()->get_and_reset_remove_directory_count()); @@ -849,9 +744,9 @@ TEST_F(LocalFileSystemOperationTest, TestCopyInForeignFileFailureByQuota) { FileSystemURL dest_file_url = URLForPath(dest_file_path); // Set quota of 0 which should force copy to fail by quota. - quota_manager_proxy()->SetQuota(dest_file_url.origin(), - test_helper_.storage_type(), - static_cast<int64>(0)); + quota_manager()->SetQuota(dest_file_url.origin(), + test_helper_.storage_type(), + static_cast<int64>(0)); operation()->CopyInForeignFile(src_local_disk_file_path, dest_file_url, RecordStatusCallback()); @@ -1030,7 +925,8 @@ TEST_F(LocalFileSystemOperationTest, TestExistsAndMetadataSuccess) { EXPECT_EQ(PlatformPath(file_path), path()); ++read_access; - EXPECT_EQ(read_access, quota_manager_proxy()->storage_accessed_count()); + EXPECT_EQ(read_access, + quota_manager_proxy()->notify_storage_accessed_count()); EXPECT_TRUE(change_observer()->HasNoChange()); } @@ -1093,7 +989,7 @@ TEST_F(LocalFileSystemOperationTest, TestReadDirSuccess) { entries()[i].name); } } - EXPECT_EQ(1, quota_manager_proxy()->storage_accessed_count()); + EXPECT_EQ(1, quota_manager_proxy()->notify_storage_accessed_count()); EXPECT_TRUE(change_observer()->HasNoChange()); } @@ -1157,7 +1053,7 @@ TEST_F(LocalFileSystemOperationTest, TestRemoveSuccess) { EXPECT_FALSE(DirectoryExists(parent_dir_path)); // Remove is not a 'read' access. - EXPECT_EQ(0, quota_manager_proxy()->storage_accessed_count()); + EXPECT_EQ(0, quota_manager_proxy()->notify_storage_accessed_count()); EXPECT_EQ(2, change_observer()->get_and_reset_remove_directory_count()); EXPECT_EQ(1, change_observer()->get_and_reset_remove_file_count()); @@ -1223,7 +1119,7 @@ TEST_F(LocalFileSystemOperationTest, TestTruncate) { // Truncate is not a 'read' access. (Here expected access count is 1 // since we made 1 read access for GetMetadata.) - EXPECT_EQ(1, quota_manager_proxy()->storage_accessed_count()); + EXPECT_EQ(1, quota_manager_proxy()->notify_storage_accessed_count()); } TEST_F(LocalFileSystemOperationTest, TestTruncateFailureByQuota) { diff --git a/webkit/quota/mock_quota_manager.cc b/webkit/quota/mock_quota_manager.cc index 535fb65..34bd3b0 100644 --- a/webkit/quota/mock_quota_manager.cc +++ b/webkit/quota/mock_quota_manager.cc @@ -90,6 +90,11 @@ MockQuotaManager::OriginInfo::OriginInfo( MockQuotaManager::OriginInfo::~OriginInfo() {} +MockQuotaManager::StorageInfo::StorageInfo() : usage(0), quota(kint64max) {} +MockQuotaManager::StorageInfo::~StorageInfo() {} + +// MockQuotaManager ---------------------------------------------------------- + MockQuotaManager::MockQuotaManager( bool is_incognito, const FilePath& profile_path, @@ -100,6 +105,19 @@ MockQuotaManager::MockQuotaManager( special_storage_policy) { } +void MockQuotaManager::GetUsageAndQuota( + const GURL& origin, + quota::StorageType type, + const GetUsageAndQuotaCallback& callback) { + StorageInfo& info = usage_and_quota_map_[std::make_pair(origin, type)]; + callback.Run(quota::kQuotaStatusOk, info.usage, info.quota); +} + +void MockQuotaManager::SetQuota(const GURL& origin, StorageType type, + int64 quota) { + usage_and_quota_map_[std::make_pair(origin, type)].quota = quota; +} + bool MockQuotaManager::AddOrigin( const GURL& origin, StorageType type, @@ -160,4 +178,57 @@ void MockQuotaManager::DeleteOriginData( MockQuotaManager::~MockQuotaManager() {} +void MockQuotaManager::UpdateUsage( + const GURL& origin, StorageType type, int64 delta) { + usage_and_quota_map_[std::make_pair(origin, type)].usage += delta; +} + +// MockQuotaManagerProxy ----------------------------------------------------- + +MockQuotaManagerProxy::MockQuotaManagerProxy( + MockQuotaManager* quota_manager, + base::SingleThreadTaskRunner* task_runner) + : QuotaManagerProxy(quota_manager, task_runner), + storage_accessed_count_(0), + storage_modified_count_(0), + last_notified_type_(kStorageTypeUnknown), + last_notified_delta_(0), + registered_client_(NULL) {} + +void MockQuotaManagerProxy::RegisterClient(QuotaClient* client) { + DCHECK(!registered_client_); + registered_client_ = client; +} + +void MockQuotaManagerProxy::SimulateQuotaManagerDestroyed() { + if (registered_client_) { + // We cannot call this in the destructor as the client (indirectly) + // holds a refptr of the proxy. + registered_client_->OnQuotaManagerDestroyed(); + registered_client_ = NULL; + } +} + +void MockQuotaManagerProxy::NotifyStorageAccessed( + QuotaClient::ID client_id, const GURL& origin, StorageType type) { + ++storage_accessed_count_; + last_notified_origin_ = origin; + last_notified_type_ = type; +} + +void MockQuotaManagerProxy::NotifyStorageModified( + QuotaClient::ID client_id, const GURL& origin, + StorageType type, int64 delta) { + ++storage_modified_count_; + last_notified_origin_ = origin; + last_notified_type_ = type; + last_notified_delta_ = delta; + if (mock_manager()) + mock_manager()->UpdateUsage(origin, type, delta); +} + +MockQuotaManagerProxy::~MockQuotaManagerProxy() { + DCHECK(!registered_client_); +} + } // namespace quota diff --git a/webkit/quota/mock_quota_manager.h b/webkit/quota/mock_quota_manager.h index ead5c35..b52ac2f 100644 --- a/webkit/quota/mock_quota_manager.h +++ b/webkit/quota/mock_quota_manager.h @@ -17,50 +17,33 @@ namespace quota { -// Mocks the pieces of QuotaManager's interface that are used for time-based -// deletion of a profile's browsing data. Origins can be added to the mock by -// calling AddOrigin, and that list of origins is then searched through in -// GetOriginsModifiedSince. Neither GetOriginsModifiedSince nor DeleteOriginData -// touches the actual origin data stored in the profile. +// Mocks the pieces of QuotaManager's interface. +// +// For usage/quota tracking test: +// Usage and quota information can be updated by following private helper +// methods: SetQuota() and UpdateUsage(). +// +// For time-based deletion test: +// Origins can be added to the mock by calling AddOrigin, and that list of +// origins is then searched through in GetOriginsModifiedSince. +// Neither GetOriginsModifiedSince nor DeleteOriginData touches the actual +// origin data stored in the profile. class MockQuotaManager : public QuotaManager { public: - // Contains the essential bits of information about an origin that the - // MockQuotaManager needs to understand: the origin itself, the StorageType, - // and its modification time. - struct OriginInfo { - OriginInfo(const GURL& origin, - StorageType type, - int quota_client_mask, - base::Time modified); - ~OriginInfo(); - - GURL origin; - StorageType type; - int quota_client_mask; - base::Time modified; - }; - MockQuotaManager(bool is_incognito, const FilePath& profile_path, base::SingleThreadTaskRunner* io_thread, base::SequencedTaskRunner* db_thread, SpecialStoragePolicy* special_storage_policy); - // Adds an origin to the canned list that will be searched through via - // GetOriginsModifiedSince. The caller must provide |quota_client_mask| - // which specifies the types of QuotaClients this canned origin contains - // as a bitmask built from QuotaClient::IDs. - bool AddOrigin(const GURL& origin, - StorageType type, - int quota_client_mask, - base::Time modified); - - // Checks an origin and type against the origins that have been added via - // AddOrigin and removed via DeleteOriginData. If the origin exists in the - // canned list with the proper StorageType and client, returns true. - bool OriginHasData(const GURL& origin, - StorageType type, - QuotaClient::ID quota_client) const; + // Overrides QuotaManager's implementation. The internal usage data is + // updated when MockQuotaManagerProxy::NotifyStorageModified() is + // called. The internal quota value can be updated by calling + // a helper method MockQuotaManagerProxy::SetQuota(). + virtual void GetUsageAndQuota( + const GURL& origin, + quota::StorageType type, + const GetUsageAndQuotaCallback& callback) OVERRIDE; // Overrides QuotaManager's implementation with a canned implementation that // allows clients to set up the origin database that should be queried. This @@ -81,19 +64,132 @@ class MockQuotaManager : public QuotaManager { int quota_client_mask, const StatusCallback& callback) OVERRIDE; + // Helper method for updating internal quota info. + void SetQuota(const GURL& origin, StorageType type, int64 quota); + + // Helper methods for timed-deletion testing: + // Adds an origin to the canned list that will be searched through via + // GetOriginsModifiedSince. The caller must provide |quota_client_mask| + // which specifies the types of QuotaClients this canned origin contains + // as a bitmask built from QuotaClient::IDs. + bool AddOrigin(const GURL& origin, + StorageType type, + int quota_client_mask, + base::Time modified); + + // Helper methods for timed-deletion testing: + // Checks an origin and type against the origins that have been added via + // AddOrigin and removed via DeleteOriginData. If the origin exists in the + // canned list with the proper StorageType and client, returns true. + bool OriginHasData(const GURL& origin, + StorageType type, + QuotaClient::ID quota_client) const; + protected: virtual ~MockQuotaManager(); private: + friend class MockQuotaManagerProxy; + + // Contains the essential bits of information about an origin that the + // MockQuotaManager needs to understand for time-based deletion: + // the origin itself, the StorageType and its modification time. + struct OriginInfo { + OriginInfo(const GURL& origin, + StorageType type, + int quota_client_mask, + base::Time modified); + ~OriginInfo(); + + GURL origin; + StorageType type; + int quota_client_mask; + base::Time modified; + }; + + // Contains the essential information for each origin for usage/quota testing. + // (Ideally this should probably merged into the above struct, but for + // regular usage/quota testing we hardly need modified time but only + // want to keep usage and quota information, so this struct exists. + struct StorageInfo { + StorageInfo(); + ~StorageInfo(); + int64 usage; + int64 quota; + }; + + typedef std::pair<GURL, StorageType> OriginAndType; + typedef std::map<OriginAndType, StorageInfo> UsageAndQuotaMap; + class GetModifiedSinceTask; class DeleteOriginDataTask; + // This must be called via MockQuotaManagerProxy. + void UpdateUsage(const GURL& origin, StorageType type, int64 delta); + // The list of stored origins that have been added via AddOrigin. std::vector<OriginInfo> origins_; + UsageAndQuotaMap usage_and_quota_map_; + DISALLOW_COPY_AND_ASSIGN(MockQuotaManager); }; +// MockQuotaManagerProxy. +class MockQuotaManagerProxy : public QuotaManagerProxy { + public: + // It is ok to give NULL to |quota_manager|. + MockQuotaManagerProxy(MockQuotaManager* quota_manager, + base::SingleThreadTaskRunner* task_runner); + + virtual void RegisterClient(QuotaClient* client) OVERRIDE; + + void SimulateQuotaManagerDestroyed(); + + // We don't mock them. + virtual void NotifyOriginInUse(const GURL& origin) OVERRIDE {} + virtual void NotifyOriginNoLongerInUse(const GURL& origin) OVERRIDE {} + + // Validates the |client_id| and updates the internal access count + // which can be accessed via notify_storage_accessed_count(). + // The also records the |origin| and |type| in last_notified_origin_ and + // last_notified_type_. + virtual void NotifyStorageAccessed(QuotaClient::ID client_id, + const GURL& origin, + StorageType type) OVERRIDE; + + // Records the |origin|, |type| and |delta| as last_notified_origin_, + // last_notified_type_ and last_notified_delta_ respecitvely. + // If non-null MockQuotaManager is given to the constructor this also + // updates the manager's internal usage information. + virtual void NotifyStorageModified(QuotaClient::ID client_id, + const GURL& origin, + StorageType type, + int64 delta) OVERRIDE; + + int notify_storage_accessed_count() const { return storage_accessed_count_; } + int notify_storage_modified_count() const { return storage_modified_count_; } + GURL last_notified_origin() const { return last_notified_origin_; } + StorageType last_notified_type() const { return last_notified_type_; } + int64 last_notified_delta() const { return last_notified_delta_; } + + protected: + virtual ~MockQuotaManagerProxy(); + + private: + MockQuotaManager* mock_manager() const { + return static_cast<MockQuotaManager*>(quota_manager()); + } + + int storage_accessed_count_; + int storage_modified_count_; + GURL last_notified_origin_; + StorageType last_notified_type_; + int64 last_notified_delta_; + + QuotaClient* registered_client_; +}; + } // namespace quota #endif // WEBKIT_QUOTA_MOCK_QUOTA_MANAGER_H_ |