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

Side by Side Diff: webkit/fileapi/file_system_quota_unittest.cc

Issue 9380040: Revert 121620 - Refactor FileSystemOperation to take callback for each method. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 8 years, 10 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // This test checks the entire behavior of FileSystem usage and quota, such as: 5 // This test checks the entire behavior of FileSystem usage and quota, such as:
6 // 1) the actual size of files on disk, 6 // 1) the actual size of files on disk,
7 // 2) the described size in .usage, and 7 // 2) the described size in .usage, and
8 // 3) the result of QuotaManager::GetUsageAndQuota. 8 // 3) the result of QuotaManager::GetUsageAndQuota.
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/file_util.h" 11 #include "base/file_util.h"
12 #include "base/logging.h" 12 #include "base/logging.h"
13 #include "base/memory/scoped_ptr.h" 13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/message_loop.h" 14 #include "base/message_loop.h"
16 #include "base/platform_file.h" 15 #include "base/platform_file.h"
17 #include "base/scoped_temp_dir.h" 16 #include "base/scoped_temp_dir.h"
18 #include "testing/gtest/include/gtest/gtest.h" 17 #include "testing/gtest/include/gtest/gtest.h"
18 #include "webkit/fileapi/file_system_callback_dispatcher.h"
19 #include "webkit/fileapi/file_system_operation.h" 19 #include "webkit/fileapi/file_system_operation.h"
20 #include "webkit/fileapi/file_system_test_helper.h" 20 #include "webkit/fileapi/file_system_test_helper.h"
21 #include "webkit/fileapi/file_system_usage_cache.h" 21 #include "webkit/fileapi/file_system_usage_cache.h"
22 #include "webkit/fileapi/file_system_util.h" 22 #include "webkit/fileapi/file_system_util.h"
23 #include "webkit/fileapi/local_file_util.h" 23 #include "webkit/fileapi/local_file_util.h"
24 #include "webkit/fileapi/quota_file_util.h" 24 #include "webkit/fileapi/quota_file_util.h"
25 #include "webkit/quota/quota_manager.h" 25 #include "webkit/quota/quota_manager.h"
26 26
27 namespace fileapi { 27 namespace fileapi {
28 28
29 const int kFileOperationStatusNotSet = 1; 29 const int kFileOperationStatusNotSet = 1;
30 30
31 class FileSystemQuotaTest 31 class FileSystemQuotaTest : public testing::Test {
32 : public testing::Test,
33 public base::SupportsWeakPtr<FileSystemQuotaTest> {
34 public: 32 public:
35 FileSystemQuotaTest() 33 FileSystemQuotaTest()
36 : local_file_util_(new LocalFileUtil(QuotaFileUtil::CreateDefault())), 34 : local_file_util_(new LocalFileUtil(QuotaFileUtil::CreateDefault())),
37 weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), 35 weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
38 status_(kFileOperationStatusNotSet), 36 status_(kFileOperationStatusNotSet),
39 quota_status_(quota::kQuotaStatusUnknown), 37 quota_status_(quota::kQuotaStatusUnknown),
40 usage_(-1), 38 usage_(-1),
41 quota_(-1) {} 39 quota_(-1) {}
42 40
43 FileSystemOperation* operation(); 41 FileSystemOperation* operation();
44 42
43 void set_status(int status) { status_ = status; }
45 int status() const { return status_; } 44 int status() const { return status_; }
46 quota::QuotaStatusCode quota_status() const { return quota_status_; } 45 quota::QuotaStatusCode quota_status() const { return quota_status_; }
47 int64 usage() { return usage_; } 46 int64 usage() { return usage_; }
48 int64 quota() { return quota_; } 47 int64 quota() { return quota_; }
49 48
50 virtual void SetUp(); 49 virtual void SetUp();
51 virtual void TearDown(); 50 virtual void TearDown();
52 51
53 void OnGetUsageAndQuota( 52 void OnGetUsageAndQuota(
54 quota::QuotaStatusCode status, int64 usage, int64 quota); 53 quota::QuotaStatusCode status, int64 usage, int64 quota);
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
110 return CreateVirtualTemporaryDirInDir(FilePath()); 109 return CreateVirtualTemporaryDirInDir(FilePath());
111 } 110 }
112 111
113 FilePath child_dir_path_; 112 FilePath child_dir_path_;
114 FilePath child_file1_path_; 113 FilePath child_file1_path_;
115 FilePath child_file2_path_; 114 FilePath child_file2_path_;
116 FilePath grandchild_file1_path_; 115 FilePath grandchild_file1_path_;
117 FilePath grandchild_file2_path_; 116 FilePath grandchild_file2_path_;
118 117
119 protected: 118 protected:
120 // Callback for recording test results.
121 FileSystemOperationInterface::StatusCallback RecordStatusCallback() {
122 return base::Bind(&FileSystemQuotaTest::DidFinish, AsWeakPtr());
123 }
124
125 void DidFinish(base::PlatformFileError status) {
126 status_ = status;
127 }
128
129 FileSystemTestOriginHelper test_helper_; 119 FileSystemTestOriginHelper test_helper_;
130 120
131 ScopedTempDir work_dir_; 121 ScopedTempDir work_dir_;
132 scoped_refptr<quota::QuotaManager> quota_manager_; 122 scoped_refptr<quota::QuotaManager> quota_manager_;
133 scoped_ptr<LocalFileUtil> local_file_util_; 123 scoped_ptr<LocalFileUtil> local_file_util_;
134 124
135 base::WeakPtrFactory<FileSystemQuotaTest> weak_factory_; 125 base::WeakPtrFactory<FileSystemQuotaTest> weak_factory_;
136 126
137 // For post-operation status. 127 // For post-operation status.
138 int status_; 128 int status_;
(...skipping 10 matching lines...) Expand all
149 139
150 int64 RevokeUsageCache() { 140 int64 RevokeUsageCache() {
151 quota_manager_->ResetUsageTracker(test_helper_.storage_type()); 141 quota_manager_->ResetUsageTracker(test_helper_.storage_type());
152 return test_helper_.RevokeUsageCache(); 142 return test_helper_.RevokeUsageCache();
153 } 143 }
154 144
155 private: 145 private:
156 scoped_refptr<FileSystemContext> file_system_context_; 146 scoped_refptr<FileSystemContext> file_system_context_;
157 }; 147 };
158 148
149 namespace {
150
151 class MockDispatcher : public FileSystemCallbackDispatcher {
152 public:
153 explicit MockDispatcher(FileSystemQuotaTest* test) : test_(test) { }
154
155 virtual void DidFail(base::PlatformFileError status) {
156 test_->set_status(status);
157 }
158
159 virtual void DidSucceed() {
160 test_->set_status(base::PLATFORM_FILE_OK);
161 }
162
163 virtual void DidGetLocalPath(const FilePath& local_path) {
164 ADD_FAILURE();
165 }
166
167 virtual void DidReadMetadata(
168 const base::PlatformFileInfo& info,
169 const FilePath& platform_path) {
170 ADD_FAILURE();
171 }
172
173 virtual void DidReadDirectory(
174 const std::vector<base::FileUtilProxy::Entry>& entries,
175 bool /* has_more */) {
176 ADD_FAILURE();
177 }
178
179 virtual void DidOpenFileSystem(const std::string&, const GURL&) {
180 ADD_FAILURE();
181 }
182
183 virtual void DidWrite(int64 bytes, bool complete) {
184 ADD_FAILURE();
185 }
186
187 private:
188 FileSystemQuotaTest* test_;
189 };
190
191 } // namespace (anonymous)
192
159 void FileSystemQuotaTest::SetUp() { 193 void FileSystemQuotaTest::SetUp() {
160 ASSERT_TRUE(work_dir_.CreateUniqueTempDir()); 194 ASSERT_TRUE(work_dir_.CreateUniqueTempDir());
161 FilePath filesystem_dir_path = work_dir_.path().AppendASCII("filesystem"); 195 FilePath filesystem_dir_path = work_dir_.path().AppendASCII("filesystem");
162 file_util::CreateDirectory(filesystem_dir_path); 196 file_util::CreateDirectory(filesystem_dir_path);
163 197
164 quota_manager_ = new quota::QuotaManager( 198 quota_manager_ = new quota::QuotaManager(
165 false /* is_incognito */, 199 false /* is_incognito */,
166 filesystem_dir_path, 200 filesystem_dir_path,
167 base::MessageLoopProxy::current(), 201 base::MessageLoopProxy::current(),
168 base::MessageLoopProxy::current(), 202 base::MessageLoopProxy::current(),
169 NULL); 203 NULL);
170 204
171 test_helper_.SetUp(filesystem_dir_path, 205 test_helper_.SetUp(filesystem_dir_path,
172 false /* unlimited quota */, 206 false /* unlimited quota */,
173 quota_manager_->proxy(), 207 quota_manager_->proxy(),
174 local_file_util_.get()); 208 local_file_util_.get());
175 } 209 }
176 210
177 void FileSystemQuotaTest::TearDown() { 211 void FileSystemQuotaTest::TearDown() {
178 quota_manager_ = NULL; 212 quota_manager_ = NULL;
179 test_helper_.TearDown(); 213 test_helper_.TearDown();
180 } 214 }
181 215
182 FileSystemOperation* FileSystemQuotaTest::operation() { 216 FileSystemOperation* FileSystemQuotaTest::operation() {
183 return test_helper_.NewOperation(); 217 return test_helper_.NewOperation(new MockDispatcher(this));
184 } 218 }
185 219
186 void FileSystemQuotaTest::OnGetUsageAndQuota( 220 void FileSystemQuotaTest::OnGetUsageAndQuota(
187 quota::QuotaStatusCode status, int64 usage, int64 quota) { 221 quota::QuotaStatusCode status, int64 usage, int64 quota) {
188 quota_status_ = status; 222 quota_status_ = status;
189 usage_ = usage; 223 usage_ = usage;
190 quota_ = quota; 224 quota_ = quota;
191 } 225 }
192 226
193 void FileSystemQuotaTest::PrepareFileSet(const FilePath& virtual_path) { 227 void FileSystemQuotaTest::PrepareFileSet(const FilePath& virtual_path) {
194 child_dir_path_ = CreateVirtualTemporaryDirInDir(virtual_path); 228 child_dir_path_ = CreateVirtualTemporaryDirInDir(virtual_path);
195 child_file1_path_ = CreateVirtualTemporaryFileInDir(virtual_path); 229 child_file1_path_ = CreateVirtualTemporaryFileInDir(virtual_path);
196 child_file2_path_ = CreateVirtualTemporaryFileInDir(virtual_path); 230 child_file2_path_ = CreateVirtualTemporaryFileInDir(virtual_path);
197 grandchild_file1_path_ = CreateVirtualTemporaryFileInDir(child_dir_path_); 231 grandchild_file1_path_ = CreateVirtualTemporaryFileInDir(child_dir_path_);
198 grandchild_file2_path_ = CreateVirtualTemporaryFileInDir(child_dir_path_); 232 grandchild_file2_path_ = CreateVirtualTemporaryFileInDir(child_dir_path_);
199 } 233 }
200 234
201 TEST_F(FileSystemQuotaTest, TestMoveSuccessSrcDirRecursive) { 235 TEST_F(FileSystemQuotaTest, TestMoveSuccessSrcDirRecursive) {
202 FilePath src_dir_path(CreateVirtualTemporaryDir()); 236 FilePath src_dir_path(CreateVirtualTemporaryDir());
203 PrepareFileSet(src_dir_path); 237 PrepareFileSet(src_dir_path);
204 FilePath dest_dir_path(CreateVirtualTemporaryDir()); 238 FilePath dest_dir_path(CreateVirtualTemporaryDir());
205 239
206 EXPECT_EQ(0, ActualSize()); 240 EXPECT_EQ(0, ActualSize());
207 241
208 operation()->Truncate(URLForPath(child_file1_path_), 5000, 242 operation()->Truncate(URLForPath(child_file1_path_), 5000);
209 RecordStatusCallback()); 243 operation()->Truncate(URLForPath(child_file2_path_), 400);
210 operation()->Truncate(URLForPath(child_file2_path_), 400, 244 operation()->Truncate(URLForPath(grandchild_file1_path_), 30);
211 RecordStatusCallback()); 245 operation()->Truncate(URLForPath(grandchild_file2_path_), 2);
212 operation()->Truncate(URLForPath(grandchild_file1_path_), 30,
213 RecordStatusCallback());
214 operation()->Truncate(URLForPath(grandchild_file2_path_), 2,
215 RecordStatusCallback());
216 MessageLoop::current()->RunAllPending(); 246 MessageLoop::current()->RunAllPending();
217 247
218 const int64 all_file_size = 5000 + 400 + 30 + 2; 248 const int64 all_file_size = 5000 + 400 + 30 + 2;
219 249
220 EXPECT_EQ(all_file_size, ActualSize()); 250 EXPECT_EQ(all_file_size, ActualSize());
221 EXPECT_EQ(all_file_size, SizeInUsageFile()); 251 EXPECT_EQ(all_file_size, SizeInUsageFile());
222 GetUsageAndQuotaFromQuotaManager(); 252 GetUsageAndQuotaFromQuotaManager();
223 EXPECT_EQ(quota::kQuotaStatusOk, quota_status()); 253 EXPECT_EQ(quota::kQuotaStatusOk, quota_status());
224 EXPECT_EQ(all_file_size, usage()); 254 EXPECT_EQ(all_file_size, usage());
225 ASSERT_LT(all_file_size, quota()); 255 ASSERT_LT(all_file_size, quota());
226 256
227 operation()->Move(URLForPath(src_dir_path), URLForPath(dest_dir_path), 257 operation()->Move(URLForPath(src_dir_path), URLForPath(dest_dir_path));
228 RecordStatusCallback());
229 MessageLoop::current()->RunAllPending(); 258 MessageLoop::current()->RunAllPending();
230 259
231 EXPECT_EQ(base::PLATFORM_FILE_OK, status()); 260 EXPECT_EQ(base::PLATFORM_FILE_OK, status());
232 EXPECT_TRUE(VirtualDirectoryExists(dest_dir_path.Append( 261 EXPECT_TRUE(VirtualDirectoryExists(dest_dir_path.Append(
233 child_dir_path_.BaseName()))); 262 child_dir_path_.BaseName())));
234 EXPECT_TRUE(VirtualFileExists(dest_dir_path.Append( 263 EXPECT_TRUE(VirtualFileExists(dest_dir_path.Append(
235 child_dir_path_.BaseName()).Append( 264 child_dir_path_.BaseName()).Append(
236 grandchild_file1_path_.BaseName()))); 265 grandchild_file1_path_.BaseName())));
237 266
238 EXPECT_EQ(all_file_size, ActualSize()); 267 EXPECT_EQ(all_file_size, ActualSize());
239 EXPECT_EQ(all_file_size, SizeInUsageFile()); 268 EXPECT_EQ(all_file_size, SizeInUsageFile());
240 GetUsageAndQuotaFromQuotaManager(); 269 GetUsageAndQuotaFromQuotaManager();
241 EXPECT_EQ(quota::kQuotaStatusOk, quota_status()); 270 EXPECT_EQ(quota::kQuotaStatusOk, quota_status());
242 EXPECT_EQ(all_file_size, usage()); 271 EXPECT_EQ(all_file_size, usage());
243 ASSERT_LT(all_file_size, quota()); 272 ASSERT_LT(all_file_size, quota());
244 } 273 }
245 274
246 TEST_F(FileSystemQuotaTest, TestCopySuccessSrcDirRecursive) { 275 TEST_F(FileSystemQuotaTest, TestCopySuccessSrcDirRecursive) {
247 FilePath src_dir_path(CreateVirtualTemporaryDir()); 276 FilePath src_dir_path(CreateVirtualTemporaryDir());
248 PrepareFileSet(src_dir_path); 277 PrepareFileSet(src_dir_path);
249 FilePath dest_dir1_path(CreateVirtualTemporaryDir()); 278 FilePath dest_dir1_path(CreateVirtualTemporaryDir());
250 FilePath dest_dir2_path(CreateVirtualTemporaryDir()); 279 FilePath dest_dir2_path(CreateVirtualTemporaryDir());
251 280
252 EXPECT_EQ(0, ActualSize()); 281 EXPECT_EQ(0, ActualSize());
253 282
254 operation()->Truncate(URLForPath(child_file1_path_), 8000, 283 operation()->Truncate(URLForPath(child_file1_path_), 8000);
255 RecordStatusCallback()); 284 operation()->Truncate(URLForPath(child_file2_path_), 700);
256 operation()->Truncate(URLForPath(child_file2_path_), 700, 285 operation()->Truncate(URLForPath(grandchild_file1_path_), 60);
257 RecordStatusCallback()); 286 operation()->Truncate(URLForPath(grandchild_file2_path_), 5);
258 operation()->Truncate(URLForPath(grandchild_file1_path_), 60,
259 RecordStatusCallback());
260 operation()->Truncate(URLForPath(grandchild_file2_path_), 5,
261 RecordStatusCallback());
262 MessageLoop::current()->RunAllPending(); 287 MessageLoop::current()->RunAllPending();
263 288
264 const int64 child_file_size = 8000 + 700; 289 const int64 child_file_size = 8000 + 700;
265 const int64 grandchild_file_size = 60 + 5; 290 const int64 grandchild_file_size = 60 + 5;
266 const int64 all_file_size = child_file_size + grandchild_file_size; 291 const int64 all_file_size = child_file_size + grandchild_file_size;
267 292
268 EXPECT_EQ(all_file_size, ActualSize()); 293 EXPECT_EQ(all_file_size, ActualSize());
269 EXPECT_EQ(all_file_size, SizeInUsageFile()); 294 EXPECT_EQ(all_file_size, SizeInUsageFile());
270 GetUsageAndQuotaFromQuotaManager(); 295 GetUsageAndQuotaFromQuotaManager();
271 EXPECT_EQ(quota::kQuotaStatusOk, quota_status()); 296 EXPECT_EQ(quota::kQuotaStatusOk, quota_status());
272 EXPECT_EQ(all_file_size, usage()); 297 EXPECT_EQ(all_file_size, usage());
273 ASSERT_LT(all_file_size, quota()); 298 ASSERT_LT(all_file_size, quota());
274 299
275 operation()->Copy(URLForPath(src_dir_path), URLForPath(dest_dir1_path), 300 operation()->Copy(URLForPath(src_dir_path), URLForPath(dest_dir1_path));
276 RecordStatusCallback());
277 MessageLoop::current()->RunAllPending(); 301 MessageLoop::current()->RunAllPending();
278 302
279 EXPECT_EQ(base::PLATFORM_FILE_OK, status()); 303 EXPECT_EQ(base::PLATFORM_FILE_OK, status());
280 EXPECT_TRUE(VirtualDirectoryExists(src_dir_path.Append( 304 EXPECT_TRUE(VirtualDirectoryExists(src_dir_path.Append(
281 child_dir_path_.BaseName()))); 305 child_dir_path_.BaseName())));
282 EXPECT_TRUE(VirtualFileExists(src_dir_path.Append( 306 EXPECT_TRUE(VirtualFileExists(src_dir_path.Append(
283 child_dir_path_.BaseName()).Append( 307 child_dir_path_.BaseName()).Append(
284 grandchild_file1_path_.BaseName()))); 308 grandchild_file1_path_.BaseName())));
285 EXPECT_EQ(base::PLATFORM_FILE_OK, status()); 309 EXPECT_EQ(base::PLATFORM_FILE_OK, status());
286 EXPECT_TRUE(VirtualDirectoryExists(dest_dir1_path.Append( 310 EXPECT_TRUE(VirtualDirectoryExists(dest_dir1_path.Append(
287 child_dir_path_.BaseName()))); 311 child_dir_path_.BaseName())));
288 EXPECT_TRUE(VirtualFileExists(dest_dir1_path.Append( 312 EXPECT_TRUE(VirtualFileExists(dest_dir1_path.Append(
289 child_dir_path_.BaseName()).Append( 313 child_dir_path_.BaseName()).Append(
290 grandchild_file1_path_.BaseName()))); 314 grandchild_file1_path_.BaseName())));
291 315
292 EXPECT_EQ(2 * all_file_size, ActualSize()); 316 EXPECT_EQ(2 * all_file_size, ActualSize());
293 EXPECT_EQ(2 * all_file_size, SizeInUsageFile()); 317 EXPECT_EQ(2 * all_file_size, SizeInUsageFile());
294 GetUsageAndQuotaFromQuotaManager(); 318 GetUsageAndQuotaFromQuotaManager();
295 EXPECT_EQ(quota::kQuotaStatusOk, quota_status()); 319 EXPECT_EQ(quota::kQuotaStatusOk, quota_status());
296 EXPECT_EQ(2 * all_file_size, usage()); 320 EXPECT_EQ(2 * all_file_size, usage());
297 ASSERT_LT(2 * all_file_size, quota()); 321 ASSERT_LT(2 * all_file_size, quota());
298 322
299 operation()->Copy(URLForPath(child_dir_path_), URLForPath(dest_dir2_path), 323 operation()->Copy(URLForPath(child_dir_path_), URLForPath(dest_dir2_path));
300 RecordStatusCallback());
301 MessageLoop::current()->RunAllPending(); 324 MessageLoop::current()->RunAllPending();
302 325
303 EXPECT_EQ(base::PLATFORM_FILE_OK, status()); 326 EXPECT_EQ(base::PLATFORM_FILE_OK, status());
304 327
305 EXPECT_EQ(2 * child_file_size + 3 * grandchild_file_size, ActualSize()); 328 EXPECT_EQ(2 * child_file_size + 3 * grandchild_file_size, ActualSize());
306 EXPECT_EQ(2 * child_file_size + 3 * grandchild_file_size, SizeInUsageFile()); 329 EXPECT_EQ(2 * child_file_size + 3 * grandchild_file_size, SizeInUsageFile());
307 GetUsageAndQuotaFromQuotaManager(); 330 GetUsageAndQuotaFromQuotaManager();
308 EXPECT_EQ(quota::kQuotaStatusOk, quota_status()); 331 EXPECT_EQ(quota::kQuotaStatusOk, quota_status());
309 EXPECT_EQ(2 * child_file_size + 3 * grandchild_file_size, usage()); 332 EXPECT_EQ(2 * child_file_size + 3 * grandchild_file_size, usage());
310 ASSERT_LT(2 * child_file_size + 3 * grandchild_file_size, quota()); 333 ASSERT_LT(2 * child_file_size + 3 * grandchild_file_size, quota());
311 } 334 }
312 335
313 } // namespace fileapi 336 } // namespace fileapi
OLDNEW
« no previous file with comments | « webkit/fileapi/file_system_operation_write_unittest.cc ('k') | webkit/fileapi/file_system_test_helper.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698