Chromium Code Reviews| 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_status(int status) { status_ = status; } | |
| 44 int status() const { return status_; } | |
| 45 int64 quota() { return quota_; } | |
| 46 | |
| 47 virtual void SetUp(); | |
| 48 virtual void TearDown(); | |
| 49 | |
| 50 void OnGetUsageAndQuota( | |
| 51 quota::QuotaStatusCode status, int64 usage, int64 quota); | |
| 52 | |
| 53 protected: | |
| 54 base::PlatformFile OpenFile(const FilePath& virtual_path); | |
| 55 void PrepareFileSet(const FilePath& virtual_path); | |
| 56 | |
| 57 GURL URLForPath(const FilePath& path) const { | |
| 58 return test_helper_.GetURLForPath(path); | |
| 59 } | |
| 60 | |
| 61 FilePath PlatformPath(FilePath virtual_path) { | |
|
kinuko
2011/06/13 12:01:19
const FilePath& ? (here and elsewhere)
Dai Mikurube (NOT FULLTIME)
2011/06/14 06:29:41
Done.
| |
| 62 return test_helper_.GetLocalPath(virtual_path); | |
| 63 } | |
| 64 | |
| 65 int64 ActualSize() { | |
| 66 return test_helper_.ComputeCurrentOriginUsage(); | |
| 67 } | |
| 68 | |
| 69 int64 SizeInUsageFile() { | |
| 70 return test_helper_.GetCachedOriginUsage(); | |
| 71 } | |
| 72 | |
| 73 int64 SizeFromQuotaManager() { | |
|
kinuko
2011/06/13 12:01:19
I might prefer declaring this as GetUsageAndQuotaF
Dai Mikurube (NOT FULLTIME)
2011/06/14 06:29:41
Done.
| |
| 74 quota_manager_->GetUsageAndQuota( | |
| 75 test_helper_.origin(), test_helper_.storage_type(), | |
| 76 callback_factory_.NewCallback( | |
| 77 &FileSystemQuotaTest::OnGetUsageAndQuota)); | |
| 78 MessageLoop::current()->RunAllPending(); | |
| 79 return usage_; | |
| 80 } | |
| 81 | |
| 82 bool VirtualFileExists(FilePath virtual_path) { | |
| 83 return file_util::PathExists(PlatformPath(virtual_path)) && | |
| 84 !file_util::DirectoryExists(PlatformPath(virtual_path)); | |
| 85 } | |
| 86 | |
| 87 bool VirtualDirectoryExists(FilePath virtual_path) { | |
| 88 return file_util::DirectoryExists(PlatformPath(virtual_path)); | |
| 89 } | |
| 90 | |
| 91 FilePath CreateVirtualDirectory(const char* virtual_path_string) { | |
|
kinuko
2011/06/13 12:01:19
this isn't used?
| |
| 92 FilePath virtual_path(virtual_path_string); | |
|
kinuko
2011/06/13 12:01:19
This wouldn't work on Windows would it?
You can u
| |
| 93 file_util::CreateDirectory(PlatformPath(virtual_path)); | |
| 94 return virtual_path; | |
| 95 } | |
| 96 | |
| 97 FilePath CreateVirtualDirectoryInDir(const char* virtual_path_string, | |
| 98 const FilePath& virtual_dir_path) { | |
|
kinuko
2011/06/13 12:01:19
this isn't used?
Dai Mikurube (NOT FULLTIME)
2011/06/14 06:29:41
Done.
| |
| 99 FilePath virtual_path(virtual_dir_path.AppendASCII(virtual_path_string)); | |
| 100 file_util::CreateDirectory(PlatformPath(virtual_path)); | |
| 101 return virtual_path; | |
| 102 } | |
| 103 | |
| 104 FilePath CreateVirtualTemporaryFileInDir(const FilePath& virtual_dir_path) { | |
| 105 FilePath absolute_dir_path(PlatformPath(virtual_dir_path)); | |
| 106 FilePath absolute_file_path; | |
| 107 EXPECT_TRUE(file_util::CreateTemporaryFileInDir(absolute_dir_path, | |
| 108 &absolute_file_path)); | |
| 109 return virtual_dir_path.Append(absolute_file_path.BaseName()); | |
| 110 } | |
| 111 | |
| 112 FilePath CreateVirtualTemporaryDirInDir(const FilePath& virtual_dir_path, | |
| 113 const FilePath::StringType& prefix) { | |
| 114 FilePath absolute_parent_dir_path(PlatformPath(virtual_dir_path)); | |
| 115 FilePath absolute_child_dir_path; | |
| 116 EXPECT_TRUE(file_util::CreateTemporaryDirInDir(absolute_parent_dir_path, | |
| 117 prefix, | |
| 118 &absolute_child_dir_path)); | |
| 119 return virtual_dir_path.Append(absolute_child_dir_path.BaseName()); | |
| 120 } | |
| 121 | |
| 122 FilePath CreateVirtualTemporaryDir(const FilePath::StringType& prefix) { | |
| 123 return CreateVirtualTemporaryDirInDir(FilePath(), prefix); | |
| 124 } | |
| 125 | |
| 126 FilePath child_dir_path_; | |
| 127 FilePath child_file1_path_; | |
| 128 FilePath child_file2_path_; | |
| 129 FilePath grandchild_file1_path_; | |
| 130 FilePath grandchild_file2_path_; | |
| 131 | |
| 132 private: | |
| 133 FileSystemTestOriginHelper test_helper_; | |
| 134 | |
| 135 ScopedTempDir work_dir_; | |
| 136 scoped_refptr<quota::QuotaManager> quota_manager_; | |
| 137 | |
| 138 base::PlatformFile child_file1_; | |
| 139 base::PlatformFile child_file2_; | |
| 140 base::PlatformFile grandchild_file1_; | |
| 141 base::PlatformFile grandchild_file2_; | |
|
kinuko
2011/06/13 12:01:19
If they're defined as member vars just to get dele
Dai Mikurube (NOT FULLTIME)
2011/06/14 06:29:41
Ah, I found that they're not required at all. The
| |
| 142 | |
| 143 base::ScopedCallbackFactory<FileSystemQuotaTest> callback_factory_; | |
| 144 | |
| 145 // For post-operation status. | |
| 146 int status_; | |
| 147 quota::QuotaStatusCode quota_status_; | |
| 148 int64 usage_; | |
| 149 int64 quota_; | |
| 150 base::PlatformFileInfo info_; | |
| 151 FilePath path_; | |
| 152 FilePath local_path_; | |
| 153 std::vector<base::FileUtilProxy::Entry> entries_; | |
|
kinuko
2011/06/13 12:01:19
many of these variables appear unused now.
Dai Mikurube (NOT FULLTIME)
2011/06/14 06:29:41
Done.
| |
| 154 | |
| 155 DISALLOW_COPY_AND_ASSIGN(FileSystemQuotaTest); | |
| 156 }; | |
| 157 | |
| 158 namespace { | |
| 159 | |
| 160 class MockDispatcher : public FileSystemCallbackDispatcher { | |
| 161 public: | |
| 162 MockDispatcher(FileSystemQuotaTest* test) : test_(test) { } | |
| 163 | |
| 164 virtual void DidFail(base::PlatformFileError status) { | |
| 165 test_->set_status(status); | |
| 166 } | |
| 167 | |
| 168 virtual void DidSucceed() { | |
| 169 test_->set_status(base::PLATFORM_FILE_OK); | |
| 170 } | |
| 171 | |
| 172 virtual void DidGetLocalPath(const FilePath& local_path) { | |
| 173 ADD_FAILURE(); | |
| 174 } | |
| 175 | |
| 176 virtual void DidReadMetadata( | |
| 177 const base::PlatformFileInfo& info, | |
| 178 const FilePath& platform_path) { | |
| 179 ADD_FAILURE(); | |
| 180 } | |
| 181 | |
| 182 virtual void DidReadDirectory( | |
| 183 const std::vector<base::FileUtilProxy::Entry>& entries, | |
| 184 bool /* has_more */) { | |
| 185 ADD_FAILURE(); | |
| 186 } | |
| 187 | |
| 188 virtual void DidOpenFileSystem(const std::string&, const GURL&) { | |
| 189 ADD_FAILURE(); | |
| 190 } | |
| 191 | |
| 192 virtual void DidWrite(int64 bytes, bool complete) { | |
| 193 ADD_FAILURE(); | |
| 194 } | |
| 195 | |
| 196 private: | |
| 197 FileSystemQuotaTest* test_; | |
| 198 }; | |
| 199 | |
| 200 } // namespace (anonymous) | |
| 201 | |
| 202 void FileSystemQuotaTest::SetUp() { | |
| 203 ASSERT_TRUE(work_dir_.CreateUniqueTempDir()); | |
| 204 FilePath filesystem_dir_path = work_dir_.path().AppendASCII("filesystem"); | |
| 205 file_util::CreateDirectory(filesystem_dir_path); | |
| 206 | |
| 207 quota_manager_ = new quota::QuotaManager( | |
| 208 false /* is_incognito */, | |
| 209 filesystem_dir_path, | |
| 210 base::MessageLoopProxy::CreateForCurrentThread(), | |
| 211 base::MessageLoopProxy::CreateForCurrentThread(), | |
| 212 NULL); | |
| 213 | |
| 214 test_helper_.SetUp(filesystem_dir_path, | |
| 215 false /* incognito */, | |
| 216 false /* unlimited quota */, | |
| 217 quota_manager_->proxy(), | |
| 218 LocalFileSystemFileUtil::GetInstance()); | |
| 219 | |
| 220 child_file1_ = base::kInvalidPlatformFileValue; | |
| 221 child_file2_ = base::kInvalidPlatformFileValue; | |
| 222 grandchild_file1_ = base::kInvalidPlatformFileValue; | |
| 223 grandchild_file2_ = base::kInvalidPlatformFileValue; | |
|
kinuko
2011/06/13 12:01:19
I think they should be initialized at the test cla
Dai Mikurube (NOT FULLTIME)
2011/06/14 06:29:41
They're removed as described above.
| |
| 224 } | |
| 225 | |
| 226 void FileSystemQuotaTest::TearDown() { | |
| 227 quota_manager_ = NULL; | |
| 228 | |
| 229 EXPECT_NE(base::kInvalidPlatformFileValue, child_file1_); | |
| 230 EXPECT_NE(base::kInvalidPlatformFileValue, child_file2_); | |
| 231 EXPECT_NE(base::kInvalidPlatformFileValue, grandchild_file1_); | |
| 232 EXPECT_NE(base::kInvalidPlatformFileValue, grandchild_file2_); | |
| 233 base::ClosePlatformFile(child_file1_); | |
| 234 base::ClosePlatformFile(child_file2_); | |
| 235 base::ClosePlatformFile(grandchild_file1_); | |
| 236 base::ClosePlatformFile(grandchild_file2_); | |
| 237 | |
| 238 // Runs cleanup tasks posted by QuataManager and FileSystemContext. | |
| 239 MessageLoop::current()->RunAllPending(); | |
|
kinuko
2011/06/13 12:01:19
We won't need this line as helper's TearDown() cal
Dai Mikurube (NOT FULLTIME)
2011/06/14 06:29:41
Done.
| |
| 240 test_helper_.TearDown(); | |
| 241 } | |
| 242 | |
| 243 FileSystemOperation* FileSystemQuotaTest::operation() { | |
| 244 return test_helper_.NewOperation(new MockDispatcher(this)); | |
| 245 } | |
| 246 | |
| 247 base::PlatformFile FileSystemQuotaTest::OpenFile(const FilePath& virtual_path) { | |
| 248 base::PlatformFile file; | |
|
kinuko
2011/06/13 12:01:19
I think this line should be merged with line 251 (
Dai Mikurube (NOT FULLTIME)
2011/06/14 06:29:41
OpenFile() is now removed as described above.
| |
| 249 bool created; | |
| 250 base::PlatformFileError error_code; | |
|
kinuko
2011/06/13 12:01:19
maybe they should be initialized in prior? (create
Dai Mikurube (NOT FULLTIME)
2011/06/14 06:29:41
ditto.
| |
| 251 file = base::CreatePlatformFile( | |
| 252 PlatformPath(virtual_path), | |
| 253 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE | | |
| 254 base::PLATFORM_FILE_ASYNC, &created, &error_code); | |
| 255 EXPECT_EQ(base::PLATFORM_FILE_OK, error_code); | |
| 256 return file; | |
| 257 } | |
| 258 | |
| 259 void FileSystemQuotaTest::OnGetUsageAndQuota( | |
| 260 quota::QuotaStatusCode status, int64 usage, int64 quota) { | |
| 261 quota_status_ = status; | |
| 262 usage_ = usage; | |
| 263 quota_ = quota; | |
| 264 } | |
| 265 | |
| 266 void FileSystemQuotaTest::PrepareFileSet(const FilePath& virtual_path) { | |
| 267 child_dir_path_ = CreateVirtualTemporaryDirInDir(virtual_path, "prefix"); | |
| 268 child_file1_path_ = CreateVirtualTemporaryFileInDir(virtual_path); | |
| 269 child_file1_ = OpenFile(child_file1_path_); | |
| 270 child_file2_path_ = CreateVirtualTemporaryFileInDir(virtual_path); | |
| 271 child_file2_ = OpenFile(child_file2_path_); | |
| 272 grandchild_file1_path_ = CreateVirtualTemporaryFileInDir(child_dir_path_); | |
| 273 grandchild_file1_ = OpenFile(grandchild_file1_path_); | |
| 274 grandchild_file2_path_ = CreateVirtualTemporaryFileInDir(child_dir_path_); | |
| 275 grandchild_file2_ = OpenFile(grandchild_file2_path_); | |
| 276 } | |
| 277 | |
| 278 TEST_F(FileSystemQuotaTest, TestMoveSuccessSrcDirRecursive) { | |
| 279 FilePath src_dir_path(CreateVirtualTemporaryDir("")); | |
|
kinuko
2011/06/13 12:01:19
I think this will need to be FILE_PATH_LITERAL("")
Dai Mikurube (NOT FULLTIME)
2011/06/14 06:29:41
Removed all prefixes.
| |
| 280 PrepareFileSet(src_dir_path); | |
| 281 FilePath dest_dir_path(CreateVirtualTemporaryDir("")); | |
| 282 | |
| 283 EXPECT_EQ(0, ActualSize()); | |
| 284 | |
| 285 operation()->Truncate(URLForPath(child_file1_path_), 5000); | |
| 286 operation()->Truncate(URLForPath(child_file2_path_), 400); | |
| 287 operation()->Truncate(URLForPath(grandchild_file1_path_), 30); | |
| 288 operation()->Truncate(URLForPath(grandchild_file2_path_), 2); | |
| 289 MessageLoop::current()->RunAllPending(); | |
| 290 | |
| 291 const int64 all_file_size = 5000+400+30+2; | |
|
kinuko
2011/06/13 12:01:19
nit: space between digits and '+'
Dai Mikurube (NOT FULLTIME)
2011/06/14 06:29:41
Done.
| |
| 292 const int64 usage_file_size = FileSystemUsageCache::kUsageFileSize; | |
| 293 | |
| 294 EXPECT_EQ(all_file_size, ActualSize()); | |
| 295 EXPECT_EQ(all_file_size, SizeInUsageFile()); | |
| 296 EXPECT_EQ(all_file_size + usage_file_size, SizeFromQuotaManager()); | |
| 297 ASSERT_LT(all_file_size, quota()); | |
| 298 | |
| 299 operation()->Move(URLForPath(src_dir_path), URLForPath(dest_dir_path)); | |
| 300 MessageLoop::current()->RunAllPending(); | |
| 301 | |
| 302 EXPECT_EQ(base::PLATFORM_FILE_OK, status()); | |
| 303 EXPECT_TRUE(VirtualDirectoryExists(dest_dir_path.Append( | |
| 304 child_dir_path_.BaseName()))); | |
| 305 EXPECT_TRUE(VirtualFileExists(dest_dir_path.Append( | |
| 306 child_dir_path_.BaseName()).Append( | |
| 307 grandchild_file1_path_.BaseName()))); | |
| 308 | |
| 309 EXPECT_EQ(all_file_size, ActualSize()); | |
| 310 EXPECT_EQ(all_file_size, SizeInUsageFile()); | |
| 311 EXPECT_EQ(all_file_size + usage_file_size, SizeFromQuotaManager()); | |
| 312 ASSERT_LT(all_file_size, quota()); | |
| 313 } | |
| 314 | |
| 315 TEST_F(FileSystemQuotaTest, TestCopySuccessSrcDirRecursive) { | |
| 316 FilePath src_dir_path(CreateVirtualTemporaryDir("")); | |
| 317 PrepareFileSet(src_dir_path); | |
| 318 FilePath dest_dir1_path(CreateVirtualTemporaryDir("")); | |
| 319 FilePath dest_dir2_path(CreateVirtualTemporaryDir("")); | |
| 320 | |
| 321 EXPECT_EQ(0, ActualSize()); | |
| 322 | |
| 323 operation()->Truncate(URLForPath(child_file1_path_), 8000); | |
| 324 operation()->Truncate(URLForPath(child_file2_path_), 700); | |
| 325 operation()->Truncate(URLForPath(grandchild_file1_path_), 60); | |
| 326 operation()->Truncate(URLForPath(grandchild_file2_path_), 5); | |
| 327 MessageLoop::current()->RunAllPending(); | |
| 328 | |
| 329 const int64 child_file_size = 8000+700; | |
| 330 const int64 grandchild_file_size = 60+5; | |
| 331 const int64 all_file_size = child_file_size + grandchild_file_size; | |
| 332 const int64 usage_file_size = FileSystemUsageCache::kUsageFileSize; | |
| 333 | |
| 334 EXPECT_EQ(all_file_size, ActualSize()); | |
| 335 EXPECT_EQ(all_file_size, SizeInUsageFile()); | |
| 336 EXPECT_EQ(all_file_size + usage_file_size, SizeFromQuotaManager()); | |
| 337 ASSERT_LT(all_file_size, quota()); | |
| 338 | |
| 339 operation()->Copy(URLForPath(src_dir_path), URLForPath(dest_dir1_path)); | |
| 340 MessageLoop::current()->RunAllPending(); | |
| 341 | |
| 342 EXPECT_EQ(base::PLATFORM_FILE_OK, status()); | |
| 343 EXPECT_TRUE(VirtualDirectoryExists(src_dir_path.Append( | |
| 344 child_dir_path_.BaseName()))); | |
| 345 EXPECT_TRUE(VirtualFileExists(src_dir_path.Append( | |
| 346 child_dir_path_.BaseName()).Append( | |
| 347 grandchild_file1_path_.BaseName()))); | |
| 348 EXPECT_EQ(base::PLATFORM_FILE_OK, status()); | |
| 349 EXPECT_TRUE(VirtualDirectoryExists(dest_dir1_path.Append( | |
| 350 child_dir_path_.BaseName()))); | |
| 351 EXPECT_TRUE(VirtualFileExists(dest_dir1_path.Append( | |
| 352 child_dir_path_.BaseName()).Append( | |
| 353 grandchild_file1_path_.BaseName()))); | |
| 354 | |
| 355 EXPECT_EQ(2 * all_file_size, ActualSize()); | |
| 356 EXPECT_EQ(2 * all_file_size, SizeInUsageFile()); | |
| 357 EXPECT_EQ(2 * all_file_size + usage_file_size, SizeFromQuotaManager()); | |
| 358 ASSERT_LT(2 * all_file_size, quota()); | |
| 359 | |
| 360 operation()->Copy(URLForPath(child_dir_path_), URLForPath(dest_dir2_path)); | |
| 361 MessageLoop::current()->RunAllPending(); | |
| 362 | |
| 363 EXPECT_EQ(base::PLATFORM_FILE_OK, status()); | |
| 364 | |
| 365 EXPECT_EQ(2 * child_file_size + 3 * grandchild_file_size, | |
| 366 ActualSize()); | |
| 367 EXPECT_EQ(2 * child_file_size + 3 * grandchild_file_size, | |
| 368 SizeInUsageFile()); | |
| 369 EXPECT_EQ(2 * child_file_size + 3 * grandchild_file_size + usage_file_size, | |
| 370 SizeFromQuotaManager()); | |
| 371 ASSERT_LT(2 * child_file_size + 3 * grandchild_file_size, quota()); | |
| 372 } | |
| 373 | |
| 374 } // namespace fileapi | |
| OLD | NEW |