Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(74)

Side by Side Diff: content/browser/quota/quota_backend_impl_unittest.cc

Issue 2776273002: Move some quota tests next to the files they cover. (Closed)
Patch Set: Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | content/browser/quota/quota_reservation_manager_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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 "storage/browser/fileapi/quota/quota_backend_impl.h"
6
7 #include <stdint.h>
8
9 #include <memory>
10 #include <string>
11
12 #include "base/files/scoped_temp_dir.h"
13 #include "base/macros.h"
14 #include "base/run_loop.h"
15 #include "base/threading/thread_task_runner_handle.h"
16 #include "storage/browser/fileapi/file_system_usage_cache.h"
17 #include "storage/browser/fileapi/obfuscated_file_util.h"
18 #include "storage/browser/quota/quota_manager_proxy.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20 #include "third_party/leveldatabase/src/helpers/memenv/memenv.h"
21 #include "third_party/leveldatabase/src/include/leveldb/env.h"
22
23 using storage::FileSystemUsageCache;
24 using storage::ObfuscatedFileUtil;
25 using storage::QuotaBackendImpl;
26 using storage::SandboxFileSystemBackendDelegate;
27
28 namespace content {
29
30 namespace {
31
32 const char kOrigin[] = "http://example.com";
33
34 bool DidReserveQuota(bool accepted,
35 base::File::Error* error_out,
36 int64_t* delta_out,
37 base::File::Error error,
38 int64_t delta) {
39 DCHECK(error_out);
40 DCHECK(delta_out);
41 *error_out = error;
42 *delta_out = delta;
43 return accepted;
44 }
45
46 class MockQuotaManagerProxy : public storage::QuotaManagerProxy {
47 public:
48 MockQuotaManagerProxy()
49 : QuotaManagerProxy(NULL, NULL),
50 storage_modified_count_(0),
51 usage_(0), quota_(0) {}
52
53 // We don't mock them.
54 void NotifyOriginInUse(const GURL& origin) override {}
55 void NotifyOriginNoLongerInUse(const GURL& origin) override {}
56 void SetUsageCacheEnabled(storage::QuotaClient::ID client_id,
57 const GURL& origin,
58 storage::StorageType type,
59 bool enabled) override {}
60
61 void NotifyStorageModified(storage::QuotaClient::ID client_id,
62 const GURL& origin,
63 storage::StorageType type,
64 int64_t delta) override {
65 ++storage_modified_count_;
66 usage_ += delta;
67 ASSERT_LE(usage_, quota_);
68 }
69
70 void GetUsageAndQuota(base::SequencedTaskRunner* original_task_runner,
71 const GURL& origin,
72 storage::StorageType type,
73 const UsageAndQuotaCallback& callback) override {
74 callback.Run(storage::kQuotaStatusOk, usage_, quota_);
75 }
76
77 int storage_modified_count() { return storage_modified_count_; }
78 int64_t usage() { return usage_; }
79 void set_usage(int64_t usage) { usage_ = usage; }
80 void set_quota(int64_t quota) { quota_ = quota; }
81
82 protected:
83 ~MockQuotaManagerProxy() override {}
84
85 private:
86 int storage_modified_count_;
87 int64_t usage_;
88 int64_t quota_;
89
90 DISALLOW_COPY_AND_ASSIGN(MockQuotaManagerProxy);
91 };
92
93 } // namespace
94
95 class QuotaBackendImplTest : public testing::Test {
96 public:
97 QuotaBackendImplTest()
98 : file_system_usage_cache_(file_task_runner()),
99 quota_manager_proxy_(new MockQuotaManagerProxy) {}
100
101 void SetUp() override {
102 ASSERT_TRUE(data_dir_.CreateUniqueTempDir());
103 in_memory_env_.reset(leveldb::NewMemEnv(leveldb::Env::Default()));
104 file_util_.reset(ObfuscatedFileUtil::CreateForTesting(
105 NULL, data_dir_.GetPath(), in_memory_env_.get(), file_task_runner()));
106 backend_.reset(new QuotaBackendImpl(file_task_runner(),
107 file_util_.get(),
108 &file_system_usage_cache_,
109 quota_manager_proxy_.get()));
110 }
111
112 void TearDown() override {
113 backend_.reset();
114 quota_manager_proxy_ = NULL;
115 file_util_.reset();
116 base::RunLoop().RunUntilIdle();
117 }
118
119 protected:
120 void InitializeForOriginAndType(const GURL& origin,
121 storage::FileSystemType type) {
122 ASSERT_TRUE(file_util_->InitOriginDatabase(origin, true /* create */));
123 ASSERT_TRUE(file_util_->origin_database_ != NULL);
124
125 std::string type_string =
126 SandboxFileSystemBackendDelegate::GetTypeString(type);
127 base::File::Error error = base::File::FILE_ERROR_FAILED;
128 base::FilePath path = file_util_->GetDirectoryForOriginAndType(
129 origin, type_string, true /* create */, &error);
130 ASSERT_EQ(base::File::FILE_OK, error);
131
132 ASSERT_TRUE(file_system_usage_cache_.UpdateUsage(
133 GetUsageCachePath(origin, type), 0));
134 }
135
136 base::SequencedTaskRunner* file_task_runner() {
137 return base::ThreadTaskRunnerHandle::Get().get();
138 }
139
140 base::FilePath GetUsageCachePath(const GURL& origin,
141 storage::FileSystemType type) {
142 base::FilePath path;
143 base::File::Error error =
144 backend_->GetUsageCachePath(origin, type, &path);
145 EXPECT_EQ(base::File::FILE_OK, error);
146 EXPECT_FALSE(path.empty());
147 return path;
148 }
149
150 base::MessageLoop message_loop_;
151 base::ScopedTempDir data_dir_;
152 std::unique_ptr<leveldb::Env> in_memory_env_;
153 std::unique_ptr<ObfuscatedFileUtil> file_util_;
154 FileSystemUsageCache file_system_usage_cache_;
155 scoped_refptr<MockQuotaManagerProxy> quota_manager_proxy_;
156 std::unique_ptr<QuotaBackendImpl> backend_;
157
158 private:
159 DISALLOW_COPY_AND_ASSIGN(QuotaBackendImplTest);
160 };
161
162 TEST_F(QuotaBackendImplTest, ReserveQuota_Basic) {
163 storage::FileSystemType type = storage::kFileSystemTypeTemporary;
164 InitializeForOriginAndType(GURL(kOrigin), type);
165 quota_manager_proxy_->set_quota(10000);
166
167 int64_t delta = 0;
168
169 const int64_t kDelta1 = 1000;
170 base::File::Error error = base::File::FILE_ERROR_FAILED;
171 backend_->ReserveQuota(GURL(kOrigin), type, kDelta1,
172 base::Bind(&DidReserveQuota, true, &error, &delta));
173 EXPECT_EQ(base::File::FILE_OK, error);
174 EXPECT_EQ(kDelta1, delta);
175 EXPECT_EQ(kDelta1, quota_manager_proxy_->usage());
176
177 const int64_t kDelta2 = -300;
178 error = base::File::FILE_ERROR_FAILED;
179 backend_->ReserveQuota(GURL(kOrigin), type, kDelta2,
180 base::Bind(&DidReserveQuota, true, &error, &delta));
181 EXPECT_EQ(base::File::FILE_OK, error);
182 EXPECT_EQ(kDelta2, delta);
183 EXPECT_EQ(kDelta1 + kDelta2, quota_manager_proxy_->usage());
184
185 EXPECT_EQ(2, quota_manager_proxy_->storage_modified_count());
186 }
187
188 TEST_F(QuotaBackendImplTest, ReserveQuota_NoSpace) {
189 storage::FileSystemType type = storage::kFileSystemTypeTemporary;
190 InitializeForOriginAndType(GURL(kOrigin), type);
191 quota_manager_proxy_->set_quota(100);
192
193 int64_t delta = 0;
194
195 const int64_t kDelta = 1000;
196 base::File::Error error = base::File::FILE_ERROR_FAILED;
197 backend_->ReserveQuota(GURL(kOrigin), type, kDelta,
198 base::Bind(&DidReserveQuota, true, &error, &delta));
199 EXPECT_EQ(base::File::FILE_OK, error);
200 EXPECT_EQ(100, delta);
201 EXPECT_EQ(100, quota_manager_proxy_->usage());
202
203 EXPECT_EQ(1, quota_manager_proxy_->storage_modified_count());
204 }
205
206 TEST_F(QuotaBackendImplTest, ReserveQuota_Revert) {
207 storage::FileSystemType type = storage::kFileSystemTypeTemporary;
208 InitializeForOriginAndType(GURL(kOrigin), type);
209 quota_manager_proxy_->set_quota(10000);
210
211 int64_t delta = 0;
212
213 const int64_t kDelta = 1000;
214 base::File::Error error = base::File::FILE_ERROR_FAILED;
215 backend_->ReserveQuota(GURL(kOrigin), type, kDelta,
216 base::Bind(&DidReserveQuota, false, &error, &delta));
217 EXPECT_EQ(base::File::FILE_OK, error);
218 EXPECT_EQ(kDelta, delta);
219 EXPECT_EQ(0, quota_manager_proxy_->usage());
220
221 EXPECT_EQ(2, quota_manager_proxy_->storage_modified_count());
222 }
223
224 TEST_F(QuotaBackendImplTest, ReleaseReservedQuota) {
225 storage::FileSystemType type = storage::kFileSystemTypeTemporary;
226 InitializeForOriginAndType(GURL(kOrigin), type);
227 const int64_t kInitialUsage = 2000;
228 quota_manager_proxy_->set_usage(kInitialUsage);
229 quota_manager_proxy_->set_quota(10000);
230
231 const int64_t kSize = 1000;
232 backend_->ReleaseReservedQuota(GURL(kOrigin), type, kSize);
233 EXPECT_EQ(kInitialUsage - kSize, quota_manager_proxy_->usage());
234
235 EXPECT_EQ(1, quota_manager_proxy_->storage_modified_count());
236 }
237
238 TEST_F(QuotaBackendImplTest, CommitQuotaUsage) {
239 storage::FileSystemType type = storage::kFileSystemTypeTemporary;
240 InitializeForOriginAndType(GURL(kOrigin), type);
241 quota_manager_proxy_->set_quota(10000);
242 base::FilePath path = GetUsageCachePath(GURL(kOrigin), type);
243
244 const int64_t kDelta1 = 1000;
245 backend_->CommitQuotaUsage(GURL(kOrigin), type, kDelta1);
246 EXPECT_EQ(kDelta1, quota_manager_proxy_->usage());
247 int64_t usage = 0;
248 EXPECT_TRUE(file_system_usage_cache_.GetUsage(path, &usage));
249 EXPECT_EQ(kDelta1, usage);
250
251 const int64_t kDelta2 = -300;
252 backend_->CommitQuotaUsage(GURL(kOrigin), type, kDelta2);
253 EXPECT_EQ(kDelta1 + kDelta2, quota_manager_proxy_->usage());
254 usage = 0;
255 EXPECT_TRUE(file_system_usage_cache_.GetUsage(path, &usage));
256 EXPECT_EQ(kDelta1 + kDelta2, usage);
257
258 EXPECT_EQ(2, quota_manager_proxy_->storage_modified_count());
259 }
260
261 TEST_F(QuotaBackendImplTest, DirtyCount) {
262 storage::FileSystemType type = storage::kFileSystemTypeTemporary;
263 InitializeForOriginAndType(GURL(kOrigin), type);
264 base::FilePath path = GetUsageCachePath(GURL(kOrigin), type);
265
266 backend_->IncrementDirtyCount(GURL(kOrigin), type);
267 uint32_t dirty = 0;
268 ASSERT_TRUE(file_system_usage_cache_.GetDirty(path, &dirty));
269 EXPECT_EQ(1u, dirty);
270
271 backend_->DecrementDirtyCount(GURL(kOrigin), type);
272 ASSERT_TRUE(file_system_usage_cache_.GetDirty(path, &dirty));
273 EXPECT_EQ(0u, dirty);
274 }
275
276 } // namespace content
OLDNEW
« no previous file with comments | « no previous file | content/browser/quota/quota_reservation_manager_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698