Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "webkit/browser/fileapi/quota/quota_backend_impl.h" | |
| 6 | |
| 7 #include "base/files/scoped_temp_dir.h" | |
| 8 #include "base/message_loop/message_loop.h" | |
| 9 #include "testing/gtest/include/gtest/gtest.h" | |
| 10 #include "webkit/browser/fileapi/file_system_usage_cache.h" | |
| 11 #include "webkit/browser/fileapi/obfuscated_file_util.h" | |
| 12 #include "webkit/browser/quota/quota_manager.h" | |
| 13 | |
| 14 namespace fileapi { | |
| 15 | |
| 16 namespace { | |
| 17 | |
| 18 const char kOrigin[] = "example.com"; | |
|
tzik
2013/11/07 07:34:13
this should be "http://example.com".
nhiroki
2013/11/08 00:12:05
Oh... good catch! Done.
| |
| 19 | |
| 20 bool DidReserveQuota(bool accepted, | |
| 21 base::PlatformFileError* error_out, | |
| 22 base::PlatformFileError error) { | |
| 23 DCHECK(error_out); | |
| 24 *error_out = error; | |
| 25 return accepted; | |
| 26 } | |
| 27 | |
| 28 void DidCommitQuotaUsage(base::PlatformFileError* error_out, | |
| 29 base::PlatformFileError error) { | |
| 30 DCHECK(error_out); | |
| 31 *error_out = error; | |
| 32 } | |
| 33 | |
| 34 class MockQuotaManagerProxy : public quota::QuotaManagerProxy { | |
| 35 public: | |
| 36 MockQuotaManagerProxy() | |
| 37 : QuotaManagerProxy(NULL, NULL), | |
| 38 storage_modified_count_(0), | |
| 39 usage_(0), quota_(0) {} | |
| 40 | |
| 41 // We don't mock them. | |
| 42 virtual void NotifyOriginInUse(const GURL& origin) OVERRIDE {} | |
| 43 virtual void NotifyOriginNoLongerInUse(const GURL& origin) OVERRIDE {} | |
| 44 virtual void SetUsageCacheEnabled(quota::QuotaClient::ID client_id, | |
| 45 const GURL& origin, | |
| 46 quota::StorageType type, | |
| 47 bool enabled) OVERRIDE {} | |
| 48 | |
| 49 virtual void NotifyStorageModified( | |
| 50 quota::QuotaClient::ID client_id, | |
| 51 const GURL& origin, | |
| 52 quota::StorageType type, | |
| 53 int64 delta) OVERRIDE { | |
| 54 ++storage_modified_count_; | |
| 55 usage_ += delta; | |
| 56 ASSERT_LT(usage_, quota_); | |
| 57 } | |
| 58 | |
| 59 virtual void GetUsageAndQuota( | |
| 60 base::SequencedTaskRunner* original_task_runner, | |
| 61 const GURL& origin, | |
| 62 quota::StorageType type, | |
| 63 const GetUsageAndQuotaCallback& callback) OVERRIDE { | |
| 64 callback.Run(quota::kQuotaStatusOk, usage_, quota_); | |
| 65 } | |
| 66 | |
| 67 int storage_modified_count() { return storage_modified_count_; } | |
| 68 int64 usage() { return usage_; } | |
| 69 void set_usage(int64 usage) { usage_ = usage; } | |
| 70 void set_quota(int64 quota) { quota_ = quota; } | |
| 71 | |
| 72 protected: | |
| 73 virtual ~MockQuotaManagerProxy() {} | |
| 74 | |
| 75 private: | |
| 76 int storage_modified_count_; | |
| 77 int64 usage_; | |
| 78 int64 quota_; | |
| 79 | |
| 80 DISALLOW_COPY_AND_ASSIGN(MockQuotaManagerProxy); | |
| 81 }; | |
| 82 | |
| 83 } // namespace | |
| 84 | |
| 85 class QuotaBackendImplTest : public testing::Test { | |
| 86 public: | |
| 87 QuotaBackendImplTest() | |
| 88 : file_system_usage_cache_(file_task_runner()), | |
| 89 quota_manager_proxy_(new MockQuotaManagerProxy) {} | |
| 90 | |
| 91 virtual void SetUp() { | |
| 92 ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); | |
| 93 file_util_.reset(ObfuscatedFileUtil::CreateForTesting( | |
| 94 NULL, data_dir_.path(), file_task_runner())); | |
| 95 backend_.reset(new QuotaBackendImpl(file_task_runner(), | |
| 96 file_util_.get(), | |
| 97 &file_system_usage_cache_, | |
| 98 quota_manager_proxy_.get())); | |
| 99 } | |
| 100 | |
|
tzik
2013/11/07 07:34:13
Maybe, we need TearDown to reset scoped_*ptr and f
nhiroki
2013/11/08 00:12:05
Done.
| |
| 101 protected: | |
| 102 void InitializeForOriginAndType(const GURL& origin, FileSystemType type) { | |
| 103 file_util_->InitOriginDatabase(origin, true /* create */); | |
| 104 ASSERT_TRUE(file_util_->origin_database_ != NULL); | |
| 105 | |
| 106 std::string type_string = | |
| 107 SandboxFileSystemBackendDelegate::GetTypeString(type); | |
| 108 base::PlatformFileError error = base::PLATFORM_FILE_ERROR_FAILED; | |
| 109 base::FilePath path = file_util_->GetDirectoryForOriginAndType( | |
| 110 origin, type_string, true /* create */, &error); | |
| 111 ASSERT_EQ(base::PLATFORM_FILE_OK, error); | |
| 112 file_system_usage_cache_.UpdateUsage(GetUsageCachePath(origin, type), 0); | |
| 113 } | |
| 114 | |
| 115 base::SequencedTaskRunner* file_task_runner() { | |
| 116 return base::MessageLoopProxy::current().get(); | |
| 117 } | |
| 118 | |
| 119 base::FilePath GetUsageCachePath(const GURL& origin, FileSystemType type) { | |
| 120 base::FilePath path; | |
| 121 DCHECK_EQ(base::PLATFORM_FILE_OK, | |
| 122 backend_->GetUsageCachePath(origin, type, &path)); | |
| 123 DCHECK(!path.empty()); | |
| 124 return path; | |
| 125 } | |
| 126 | |
| 127 base::MessageLoop message_loop_; | |
| 128 base::ScopedTempDir data_dir_; | |
| 129 scoped_ptr<ObfuscatedFileUtil> file_util_; | |
| 130 FileSystemUsageCache file_system_usage_cache_; | |
| 131 scoped_refptr<MockQuotaManagerProxy> quota_manager_proxy_; | |
| 132 scoped_ptr<QuotaBackendImpl> backend_; | |
| 133 | |
| 134 private: | |
| 135 DISALLOW_COPY_AND_ASSIGN(QuotaBackendImplTest); | |
| 136 }; | |
| 137 | |
| 138 TEST_F(QuotaBackendImplTest, ReserveQuota_Basic) { | |
| 139 FileSystemType type = fileapi::kFileSystemTypeTemporary; | |
| 140 InitializeForOriginAndType(GURL(kOrigin), type); | |
| 141 quota_manager_proxy_->set_quota(10000); | |
| 142 | |
| 143 const int64 kDelta1 = 1000; | |
| 144 base::PlatformFileError error = base::PLATFORM_FILE_ERROR_FAILED; | |
| 145 backend_->ReserveQuota(GURL(kOrigin), type, kDelta1, | |
| 146 base::Bind(&DidReserveQuota, true, &error)); | |
| 147 EXPECT_EQ(base::PLATFORM_FILE_OK, error); | |
| 148 EXPECT_EQ(kDelta1, quota_manager_proxy_->usage()); | |
| 149 | |
| 150 const int64 kDelta2 = -300; | |
| 151 error = base::PLATFORM_FILE_ERROR_FAILED; | |
| 152 backend_->ReserveQuota(GURL(kOrigin), type, kDelta2, | |
| 153 base::Bind(&DidReserveQuota, true, &error)); | |
| 154 EXPECT_EQ(base::PLATFORM_FILE_OK, error); | |
| 155 EXPECT_EQ(kDelta1 + kDelta2, quota_manager_proxy_->usage()); | |
| 156 | |
| 157 EXPECT_EQ(2, quota_manager_proxy_->storage_modified_count()); | |
| 158 } | |
| 159 | |
| 160 TEST_F(QuotaBackendImplTest, ReserveQuota_NoSpace) { | |
| 161 FileSystemType type = fileapi::kFileSystemTypeTemporary; | |
| 162 InitializeForOriginAndType(GURL(kOrigin), type); | |
| 163 quota_manager_proxy_->set_quota(100); | |
| 164 | |
| 165 const int64 kDelta = 1000; | |
| 166 base::PlatformFileError error = base::PLATFORM_FILE_ERROR_FAILED; | |
| 167 backend_->ReserveQuota(GURL(kOrigin), type, kDelta, | |
| 168 base::Bind(&DidReserveQuota, true, &error)); | |
| 169 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, error); | |
| 170 EXPECT_EQ(0, quota_manager_proxy_->usage()); | |
| 171 | |
| 172 EXPECT_EQ(0, quota_manager_proxy_->storage_modified_count()); | |
| 173 } | |
| 174 | |
| 175 TEST_F(QuotaBackendImplTest, ReserveQuota_Revert) { | |
| 176 FileSystemType type = fileapi::kFileSystemTypeTemporary; | |
| 177 InitializeForOriginAndType(GURL(kOrigin), type); | |
| 178 quota_manager_proxy_->set_quota(10000); | |
| 179 | |
| 180 const int64 kDelta = 1000; | |
| 181 base::PlatformFileError error = base::PLATFORM_FILE_ERROR_FAILED; | |
| 182 backend_->ReserveQuota(GURL(kOrigin), type, kDelta, | |
| 183 base::Bind(&DidReserveQuota, false, &error)); | |
| 184 EXPECT_EQ(base::PLATFORM_FILE_OK, error); | |
| 185 EXPECT_EQ(0, quota_manager_proxy_->usage()); | |
| 186 | |
| 187 EXPECT_EQ(2, quota_manager_proxy_->storage_modified_count()); | |
| 188 } | |
| 189 | |
| 190 TEST_F(QuotaBackendImplTest, ReleaseReservedQuota) { | |
| 191 FileSystemType type = fileapi::kFileSystemTypeTemporary; | |
| 192 InitializeForOriginAndType(GURL(kOrigin), type); | |
| 193 const int64 kInitialUsage = 2000; | |
| 194 quota_manager_proxy_->set_usage(kInitialUsage); | |
| 195 quota_manager_proxy_->set_quota(10000); | |
| 196 | |
| 197 const int64 kSize = 1000; | |
| 198 backend_->ReleaseReservedQuota(GURL(kOrigin), type, kSize); | |
| 199 EXPECT_EQ(kInitialUsage - kSize, quota_manager_proxy_->usage()); | |
| 200 | |
| 201 EXPECT_EQ(1, quota_manager_proxy_->storage_modified_count()); | |
| 202 } | |
| 203 | |
| 204 TEST_F(QuotaBackendImplTest, CommitQuotaUsage_Basic) { | |
| 205 FileSystemType type = fileapi::kFileSystemTypeTemporary; | |
| 206 InitializeForOriginAndType(GURL(kOrigin), type); | |
| 207 quota_manager_proxy_->set_quota(10000); | |
| 208 base::FilePath path = GetUsageCachePath(GURL(kOrigin), type); | |
| 209 | |
| 210 const int64 kDelta1 = 1000; | |
| 211 base::PlatformFileError error = base::PLATFORM_FILE_ERROR_FAILED; | |
| 212 backend_->CommitQuotaUsage(GURL(kOrigin), type, kDelta1, | |
| 213 base::Bind(&DidCommitQuotaUsage, &error)); | |
| 214 EXPECT_EQ(base::PLATFORM_FILE_OK, error); | |
| 215 EXPECT_EQ(kDelta1, quota_manager_proxy_->usage()); | |
| 216 int64 usage = 0; | |
| 217 EXPECT_TRUE(file_system_usage_cache_.GetUsage(path, &usage)); | |
| 218 EXPECT_EQ(kDelta1, usage); | |
| 219 | |
| 220 const int64 kDelta2 = -300; | |
| 221 error = base::PLATFORM_FILE_ERROR_FAILED; | |
| 222 backend_->CommitQuotaUsage(GURL(kOrigin), type, kDelta2, | |
| 223 base::Bind(&DidCommitQuotaUsage, &error)); | |
| 224 EXPECT_EQ(base::PLATFORM_FILE_OK, error); | |
| 225 EXPECT_EQ(kDelta1 + kDelta2, quota_manager_proxy_->usage()); | |
| 226 usage = 0; | |
| 227 EXPECT_TRUE(file_system_usage_cache_.GetUsage(path, &usage)); | |
| 228 EXPECT_EQ(kDelta1 + kDelta2, usage); | |
| 229 | |
| 230 EXPECT_EQ(2, quota_manager_proxy_->storage_modified_count()); | |
| 231 } | |
| 232 | |
| 233 TEST_F(QuotaBackendImplTest, CommitQuotaUsage_NoSpace) { | |
| 234 FileSystemType type = fileapi::kFileSystemTypeTemporary; | |
| 235 InitializeForOriginAndType(GURL(kOrigin), type); | |
| 236 quota_manager_proxy_->set_quota(100); | |
| 237 base::FilePath path = GetUsageCachePath(GURL(kOrigin), type); | |
| 238 | |
| 239 const int64 kDelta = 1000; | |
| 240 base::PlatformFileError error = base::PLATFORM_FILE_ERROR_FAILED; | |
| 241 backend_->CommitQuotaUsage(GURL(kOrigin), type, kDelta, | |
| 242 base::Bind(&DidCommitQuotaUsage, &error)); | |
| 243 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, error); | |
| 244 EXPECT_EQ(0, quota_manager_proxy_->usage()); | |
| 245 int64 usage = 0; | |
| 246 EXPECT_TRUE(file_system_usage_cache_.GetUsage(path, &usage)); | |
| 247 EXPECT_EQ(0, usage); | |
| 248 | |
| 249 EXPECT_EQ(0, quota_manager_proxy_->storage_modified_count()); | |
| 250 } | |
| 251 | |
| 252 TEST_F(QuotaBackendImplTest, DirtyCount) { | |
| 253 FileSystemType type = fileapi::kFileSystemTypeTemporary; | |
| 254 InitializeForOriginAndType(GURL(kOrigin), type); | |
| 255 base::FilePath path = GetUsageCachePath(GURL(kOrigin), type); | |
| 256 | |
| 257 backend_->IncrementDirtyCount(GURL(kOrigin), type); | |
| 258 uint32_t dirty = 0; | |
| 259 ASSERT_TRUE(file_system_usage_cache_.GetDirty(path, &dirty)); | |
| 260 EXPECT_EQ(1u, dirty); | |
| 261 | |
| 262 backend_->DecrementDirtyCount(GURL(kOrigin), type); | |
| 263 ASSERT_TRUE(file_system_usage_cache_.GetDirty(path, &dirty)); | |
| 264 EXPECT_EQ(0u, dirty); | |
| 265 } | |
| 266 | |
| 267 } // namespace fileapi | |
| OLD | NEW |