OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 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 // This test checks the entire behavior of FileSystem usage and quota, such as: | |
6 // 1) the actual size of files on disk, | |
7 // 2) the described size in .usage, and | |
8 // 3) the result of QuotaManager::GetUsageAndQuota. | |
9 | |
10 #include "webkit/fileapi/file_system_operation.h" | |
11 | |
12 #include "base/file_util.h" | |
13 #include "base/logging.h" | |
14 #include "base/memory/scoped_callback_factory.h" | |
15 #include "base/memory/scoped_ptr.h" | |
16 #include "base/message_loop.h" | |
17 #include "base/platform_file.h" | |
18 #include "base/scoped_temp_dir.h" | |
19 #include "testing/gtest/include/gtest/gtest.h" | |
20 #include "webkit/fileapi/file_system_callback_dispatcher.h" | |
21 #include "webkit/fileapi/file_system_operation.h" | |
22 #include "webkit/fileapi/file_system_test_helper.h" | |
23 #include "webkit/fileapi/file_system_usage_cache.h" | |
24 #include "webkit/fileapi/file_system_util.h" | |
25 #include "webkit/fileapi/local_file_system_file_util.h" | |
26 #include "webkit/quota/quota_manager.h" | |
27 | |
28 namespace fileapi { | |
29 | |
30 const int kFileOperationStatusNotSet = 1; | |
31 | |
32 class FileSystemQuotaTest : public testing::Test { | |
33 public: | |
34 FileSystemQuotaTest() | |
35 : callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), | |
36 status_(kFileOperationStatusNotSet), | |
37 quota_status_(quota::kQuotaStatusUnknown), | |
38 usage_(-1), | |
39 quota_(-1) {} | |
40 | |
41 FileSystemOperation* operation(); | |
42 | |
43 void set_local_path(const FilePath& path) { local_path_ = path; } | |
44 const FilePath& local_path() const { return local_path_; } | |
45 void set_status(int status) { status_ = status; } | |
46 int status() const { return status_; } | |
47 void set_info(const base::PlatformFileInfo& info) { info_ = info; } | |
48 const base::PlatformFileInfo& info() const { return info_; } | |
49 void set_path(const FilePath& path) { path_ = path; } | |
50 const FilePath& path() const { return path_; } | |
51 void set_entries(const std::vector<base::FileUtilProxy::Entry>& entries) { | |
52 entries_ = entries; | |
53 } | |
54 const std::vector<base::FileUtilProxy::Entry>& entries() const { | |
55 return entries_; | |
56 } | |
kinuko
2011/06/02 08:04:08
Many of them seem to be unnecessary (see the other
Dai Mikurube (NOT FULLTIME)
2011/06/13 08:12:09
Done.
| |
57 | |
58 virtual void SetUp(); | |
59 virtual void TearDown(); | |
60 | |
61 void OnGetUsageAndQuota( | |
62 quota::QuotaStatusCode status, int64 usage, int64 quota); | |
63 | |
64 protected: | |
65 base::PlatformFile OpenFile(const FilePath& virtual_path); | |
66 void PrepareFileSet(const FilePath& virtual_path); | |
67 | |
68 GURL URLForPath(const FilePath& path) const { | |
69 return test_helper_.GetURLForPath(path); | |
70 } | |
71 | |
72 FilePath PlatformPath(FilePath virtual_path) { | |
73 return test_helper_.GetLocalPath(virtual_path); | |
74 } | |
75 | |
76 int64 ActualSize() { | |
77 return file_util::ComputeDirectorySize(base_dir_path_); | |
78 } | |
79 | |
80 int64 SizeInUsageFile() { | |
81 return FileSystemUsageCache::GetUsage(usage_file_path_); | |
82 } | |
kinuko
2011/06/02 08:04:08
For these two methods (ActualSize and SizeInUsageF
Dai Mikurube (NOT FULLTIME)
2011/06/13 08:12:09
Done.
| |
83 | |
84 int64 SizeFromQuotaManager() { | |
85 quota_manager_->GetUsageAndQuota( | |
86 test_helper_.origin(), test_helper_.storage_type(), | |
87 callback_factory_.NewCallback( | |
88 &FileSystemQuotaTest::OnGetUsageAndQuota)); | |
89 MessageLoop::current()->RunAllPending(); | |
90 return usage_; | |
91 } | |
92 | |
93 bool VirtualFileExists(FilePath virtual_path) { | |
94 return file_util::PathExists(PlatformPath(virtual_path)) && | |
95 !file_util::DirectoryExists(PlatformPath(virtual_path)); | |
96 } | |
97 | |
98 bool VirtualDirectoryExists(FilePath virtual_path) { | |
99 return file_util::DirectoryExists(PlatformPath(virtual_path)); | |
100 } | |
101 | |
102 FilePath CreateVirtualDirectory(const char* virtual_path_string) { | |
103 FilePath virtual_path(virtual_path_string); | |
104 file_util::CreateDirectory(PlatformPath(virtual_path)); | |
105 return virtual_path; | |
106 } | |
107 | |
108 FilePath CreateVirtualDirectoryInDir(const char* virtual_path_string, | |
109 const FilePath& virtual_dir_path) { | |
110 FilePath virtual_path(virtual_dir_path.AppendASCII(virtual_path_string)); | |
111 file_util::CreateDirectory(PlatformPath(virtual_path)); | |
112 return virtual_path; | |
113 } | |
114 | |
115 FilePath CreateVirtualTemporaryFileInDir(const FilePath& virtual_dir_path) { | |
116 FilePath absolute_dir_path(PlatformPath(virtual_dir_path)); | |
117 FilePath absolute_file_path; | |
118 if (file_util::CreateTemporaryFileInDir(absolute_dir_path, | |
119 &absolute_file_path)) | |
120 return virtual_dir_path.Append(absolute_file_path.BaseName()); | |
121 else | |
122 return FilePath(); | |
kinuko
2011/06/02 08:04:08
For these checks (checking the return value of fil
Dai Mikurube (NOT FULLTIME)
2011/06/13 08:12:09
Done.
| |
123 } | |
124 | |
125 FilePath CreateVirtualTemporaryDirInDir(const FilePath& virtual_dir_path, | |
126 const FilePath::StringType& prefix) { | |
127 FilePath absolute_parent_dir_path(PlatformPath(virtual_dir_path)); | |
128 FilePath absolute_child_dir_path; | |
129 if (file_util::CreateTemporaryDirInDir(absolute_parent_dir_path, prefix, | |
130 &absolute_child_dir_path)) | |
131 return virtual_dir_path.Append(absolute_child_dir_path.BaseName()); | |
132 else | |
133 return FilePath(); | |
134 } | |
135 | |
136 FilePath CreateVirtualTemporaryDir(const FilePath::StringType& prefix) { | |
137 return CreateVirtualTemporaryDirInDir(FilePath(), prefix); | |
138 } | |
139 | |
140 FileSystemTestOriginHelper test_helper_; | |
141 | |
142 ScopedTempDir work_dir_; | |
143 scoped_refptr<quota::QuotaManager> quota_manager_; | |
144 | |
145 FilePath filesystem_dir_path_; | |
kinuko
2011/06/02 08:04:08
Looks like this (and probably two other paths belo
Dai Mikurube (NOT FULLTIME)
2011/06/13 08:12:09
Done.
| |
146 FilePath base_dir_path_; | |
147 FilePath usage_file_path_; | |
148 | |
149 FilePath child_dir_path_; | |
150 FilePath child_file1_path_; | |
151 FilePath child_file2_path_; | |
152 FilePath grandchild_file1_path_; | |
153 FilePath grandchild_file2_path_; | |
154 base::PlatformFile child_file1_; | |
155 base::PlatformFile child_file2_; | |
156 base::PlatformFile grandchild_file1_; | |
157 base::PlatformFile grandchild_file2_; | |
158 | |
159 base::ScopedCallbackFactory<FileSystemQuotaTest> callback_factory_; | |
160 | |
161 // For post-operation status. | |
162 int status_; | |
163 quota::QuotaStatusCode quota_status_; | |
164 int64 usage_; | |
165 int64 quota_; | |
166 base::PlatformFileInfo info_; | |
167 FilePath path_; | |
168 FilePath local_path_; | |
169 std::vector<base::FileUtilProxy::Entry> entries_; | |
170 | |
171 DISALLOW_COPY_AND_ASSIGN(FileSystemQuotaTest); | |
172 }; | |
173 | |
174 namespace { | |
175 | |
176 class MockDispatcher : public FileSystemCallbackDispatcher { | |
177 public: | |
178 MockDispatcher(FileSystemQuotaTest* test) : test_(test) { } | |
179 | |
180 virtual void DidFail(base::PlatformFileError status) { | |
181 test_->set_status(status); | |
182 } | |
183 | |
184 virtual void DidSucceed() { | |
185 test_->set_status(base::PLATFORM_FILE_OK); | |
186 } | |
187 | |
188 virtual void DidGetLocalPath(const FilePath& local_path) { | |
kinuko
2011/06/02 08:04:08
We no longer have this method
Dai Mikurube (NOT FULLTIME)
2011/06/13 08:12:09
Done.
| |
189 test_->set_local_path(local_path); | |
190 test_->set_status(base::PLATFORM_FILE_OK); | |
191 } | |
192 | |
193 virtual void DidReadMetadata( | |
194 const base::PlatformFileInfo& info, | |
195 const FilePath& platform_path) { | |
196 test_->set_info(info); | |
197 test_->set_path(platform_path); | |
198 test_->set_status(base::PLATFORM_FILE_OK); | |
kinuko
2011/06/02 08:04:08
this doesn't seem to be used
ADD_FAILURE()
Dai Mikurube (NOT FULLTIME)
2011/06/13 08:12:09
Done.
| |
199 } | |
200 | |
201 virtual void DidReadDirectory( | |
202 const std::vector<base::FileUtilProxy::Entry>& entries, | |
203 bool /* has_more */) { | |
204 test_->set_entries(entries); | |
kinuko
2011/06/02 08:04:08
this doesn't seem to be used
ADD_FAILURE()
Dai Mikurube (NOT FULLTIME)
2011/06/13 08:12:09
Done.
| |
205 } | |
206 | |
207 virtual void DidOpenFileSystem(const std::string&, const GURL&) { | |
208 ADD_FAILURE(); | |
209 } | |
210 | |
211 virtual void DidWrite(int64 bytes, bool complete) { | |
212 ADD_FAILURE(); | |
213 } | |
214 | |
215 private: | |
216 FileSystemQuotaTest* test_; | |
217 }; | |
218 | |
219 } // namespace (anonymous) | |
220 | |
221 void FileSystemQuotaTest::SetUp() { | |
222 ASSERT_TRUE(work_dir_.CreateUniqueTempDir()); | |
223 filesystem_dir_path_ = work_dir_.path().AppendASCII("filesystem"); | |
224 file_util::CreateDirectory(filesystem_dir_path_); | |
225 | |
226 quota_manager_ = new quota::QuotaManager( | |
227 false /* is_incognito */, | |
228 filesystem_dir_path_, | |
229 base::MessageLoopProxy::CreateForCurrentThread(), | |
230 base::MessageLoopProxy::CreateForCurrentThread(), | |
231 NULL); | |
232 | |
233 test_helper_.SetUp(filesystem_dir_path_, | |
234 false /* incognito */, | |
235 false /* unlimited quota */, | |
236 quota_manager_->proxy(), | |
237 LocalFileSystemFileUtil::GetInstance()); | |
238 base_dir_path_ = test_helper_.GetOriginRootPath(); | |
239 usage_file_path_ = test_helper_.GetUsageCachePath(); | |
240 file_util::Delete(usage_file_path_, false); | |
kinuko
2011/06/02 08:04:08
This doesn't look right to me...?
If it's for mak
Dai Mikurube (NOT FULLTIME)
2011/06/13 08:12:09
Removing .usage was required so that .usage catche
| |
241 } | |
242 | |
243 void FileSystemQuotaTest::TearDown() { | |
244 quota_manager_ = NULL; | |
245 | |
246 // Runs cleanup tasks posted by QuataManager and FileSystemContext. | |
247 MessageLoop::current()->RunAllPending(); | |
248 test_helper_.TearDown(); | |
249 } | |
250 | |
251 FileSystemOperation* FileSystemQuotaTest::operation() { | |
252 return test_helper_.NewOperation(new MockDispatcher(this)); | |
253 } | |
254 | |
255 base::PlatformFile FileSystemQuotaTest::OpenFile(const FilePath& virtual_path) { | |
256 base::PlatformFile file; | |
257 bool created; | |
258 base::PlatformFileError error_code; | |
259 file = base::CreatePlatformFile( | |
260 PlatformPath(virtual_path), | |
261 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE | | |
262 base::PLATFORM_FILE_ASYNC, &created, &error_code); | |
kinuko
2011/06/02 08:04:08
maybe better to check error_code here?
Dai Mikurube (NOT FULLTIME)
2011/06/13 08:12:09
Done.
| |
263 return file; | |
264 } | |
265 | |
266 void FileSystemQuotaTest::OnGetUsageAndQuota( | |
267 quota::QuotaStatusCode status, int64 usage, int64 quota) { | |
268 quota_status_ = status; | |
269 usage_ = usage; | |
270 quota_ = quota; | |
271 } | |
272 | |
273 void FileSystemQuotaTest::PrepareFileSet(const FilePath& virtual_path) { | |
274 child_dir_path_ = CreateVirtualTemporaryDirInDir(virtual_path, "prefix"); | |
275 child_file1_path_ = CreateVirtualTemporaryFileInDir(virtual_path); | |
276 child_file1_ = OpenFile(child_file1_path_); | |
277 child_file2_path_ = CreateVirtualTemporaryFileInDir(virtual_path); | |
278 child_file2_ = OpenFile(child_file2_path_); | |
279 grandchild_file1_path_ = CreateVirtualTemporaryFileInDir(child_dir_path_); | |
280 grandchild_file1_ = OpenFile(grandchild_file1_path_); | |
281 grandchild_file2_path_ = CreateVirtualTemporaryFileInDir(child_dir_path_); | |
282 grandchild_file2_ = OpenFile(grandchild_file2_path_); | |
kinuko
2011/06/02 08:04:08
Don't we need to close the opened files in TearDow
Dai Mikurube (NOT FULLTIME)
2011/06/13 08:12:09
Done.
| |
283 } | |
284 | |
285 TEST_F(FileSystemQuotaTest, TestMoveSuccessSrcDirRecursive) { | |
286 FilePath src_dir_path(CreateVirtualTemporaryDir("")); | |
287 PrepareFileSet(src_dir_path); | |
288 FilePath dest_dir_path(CreateVirtualTemporaryDir("")); | |
289 | |
290 EXPECT_EQ(0, ActualSize()); | |
291 | |
292 ASSERT_TRUE(base::TruncatePlatformFile(child_file1_, 1013)); | |
293 ASSERT_TRUE(base::TruncatePlatformFile(child_file2_, 129)); | |
294 ASSERT_TRUE(base::TruncatePlatformFile(grandchild_file1_, 94)); | |
295 ASSERT_TRUE(base::TruncatePlatformFile(grandchild_file2_, 517)); | |
kinuko
2011/06/02 08:04:08
nit: these days I'm trying to use simpler values l
Dai Mikurube (NOT FULLTIME)
2011/06/13 08:12:09
Changed it to { 5000, 400, 30, 2 } and { 8000, 700
| |
296 | |
kinuko
2011/06/02 08:04:08
it might be better to check we have enough quota h
Dai Mikurube (NOT FULLTIME)
2011/06/13 08:12:09
Done.
| |
297 int64 child_file_size = 1013+129; | |
298 int64 grandchild_file_size = 94+517; | |
299 int64 all_file_size = child_file_size + grandchild_file_size; | |
kinuko
2011/06/02 08:04:08
nit: For this test seems like you can directly cal
kinuko
2011/06/02 08:04:08
nit: const ?
Dai Mikurube (NOT FULLTIME)
2011/06/13 08:12:09
Done.
Dai Mikurube (NOT FULLTIME)
2011/06/13 08:12:09
Done.
| |
300 int64 usage_file_size = FileSystemUsageCache::kUsageFileSize; | |
kinuko
2011/06/02 08:04:08
nit: const ?
Dai Mikurube (NOT FULLTIME)
2011/06/13 08:12:09
Done.
| |
301 | |
302 EXPECT_EQ(all_file_size, ActualSize()); | |
303 EXPECT_EQ(-1, SizeInUsageFile()); | |
304 EXPECT_EQ(all_file_size + usage_file_size, SizeFromQuotaManager()); | |
305 EXPECT_EQ(all_file_size + usage_file_size, ActualSize()); | |
306 EXPECT_EQ(all_file_size + usage_file_size, SizeInUsageFile()); | |
307 | |
308 operation()->Move(URLForPath(src_dir_path), URLForPath(dest_dir_path)); | |
309 MessageLoop::current()->RunAllPending(); | |
kinuko
2011/06/02 08:04:08
should check status_ here?
Dai Mikurube (NOT FULLTIME)
2011/06/13 08:12:09
Done in the following line.
| |
310 | |
311 EXPECT_EQ(base::PLATFORM_FILE_OK, status()); | |
312 EXPECT_TRUE(VirtualDirectoryExists(dest_dir_path.Append( | |
313 child_dir_path_.BaseName()))); | |
314 EXPECT_TRUE(VirtualFileExists(dest_dir_path.Append( | |
315 child_dir_path_.BaseName()).Append( | |
316 grandchild_file1_path_.BaseName()))); | |
317 | |
318 EXPECT_EQ(all_file_size + usage_file_size, ActualSize()); | |
319 EXPECT_EQ(all_file_size + usage_file_size, SizeInUsageFile()); | |
320 EXPECT_EQ(all_file_size + usage_file_size, SizeFromQuotaManager()); | |
321 } | |
322 | |
323 TEST_F(FileSystemQuotaTest, TestCopySuccessSrcDirRecursive) { | |
324 FilePath src_dir_path(CreateVirtualTemporaryDir("")); | |
325 PrepareFileSet(src_dir_path); | |
326 FilePath dest_dir1_path(CreateVirtualTemporaryDir("")); | |
327 FilePath dest_dir2_path(CreateVirtualTemporaryDir("")); | |
328 | |
329 EXPECT_EQ(0, ActualSize()); | |
330 | |
331 ASSERT_TRUE(base::TruncatePlatformFile(child_file1_, 12)); | |
332 ASSERT_TRUE(base::TruncatePlatformFile(child_file2_, 23)); | |
333 ASSERT_TRUE(base::TruncatePlatformFile(grandchild_file1_, 34)); | |
334 ASSERT_TRUE(base::TruncatePlatformFile(grandchild_file2_, 45)); | |
335 | |
336 int64 child_file_size = 12+23; | |
337 int64 grandchild_file_size = 34+45; | |
338 int64 all_file_size = child_file_size + grandchild_file_size; | |
339 int64 usage_file_size = FileSystemUsageCache::kUsageFileSize; | |
340 | |
341 EXPECT_EQ(all_file_size, ActualSize()); | |
342 EXPECT_EQ(-1, SizeInUsageFile()); | |
343 EXPECT_EQ(all_file_size + usage_file_size, SizeFromQuotaManager()); | |
344 EXPECT_EQ(all_file_size + usage_file_size, ActualSize()); | |
345 EXPECT_EQ(all_file_size + usage_file_size, SizeInUsageFile()); | |
346 | |
347 operation()->Copy(URLForPath(src_dir_path), URLForPath(dest_dir1_path)); | |
348 MessageLoop::current()->RunAllPending(); | |
349 | |
350 EXPECT_EQ(base::PLATFORM_FILE_OK, status()); | |
351 EXPECT_TRUE(VirtualDirectoryExists(src_dir_path.Append( | |
352 child_dir_path_.BaseName()))); | |
353 EXPECT_TRUE(VirtualFileExists(src_dir_path.Append( | |
354 child_dir_path_.BaseName()).Append( | |
355 grandchild_file1_path_.BaseName()))); | |
356 EXPECT_EQ(base::PLATFORM_FILE_OK, status()); | |
357 EXPECT_TRUE(VirtualDirectoryExists(dest_dir1_path.Append( | |
358 child_dir_path_.BaseName()))); | |
359 EXPECT_TRUE(VirtualFileExists(dest_dir1_path.Append( | |
360 child_dir_path_.BaseName()).Append( | |
361 grandchild_file1_path_.BaseName()))); | |
362 | |
363 EXPECT_EQ(2 * all_file_size + usage_file_size, ActualSize()); | |
364 EXPECT_EQ(2 * all_file_size + usage_file_size, SizeInUsageFile()); | |
365 EXPECT_EQ(2 * all_file_size + usage_file_size, SizeFromQuotaManager()); | |
366 | |
367 operation()->Copy(URLForPath(child_dir_path_), URLForPath(dest_dir2_path)); | |
368 MessageLoop::current()->RunAllPending(); | |
kinuko
2011/06/02 08:04:08
should check status_ here?
Dai Mikurube (NOT FULLTIME)
2011/06/13 08:12:09
Done.
| |
369 | |
370 EXPECT_EQ(2 * child_file_size + 3 * grandchild_file_size + usage_file_size, | |
371 ActualSize()); | |
372 EXPECT_EQ(2 * child_file_size + 3 * grandchild_file_size + usage_file_size, | |
373 SizeInUsageFile()); | |
374 EXPECT_EQ(2 * child_file_size + 3 * grandchild_file_size + usage_file_size, | |
375 SizeFromQuotaManager()); | |
376 } | |
377 | |
378 } // namespace fileapi | |
OLD | NEW |