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" | |
|
kinuko
2011/06/14 06:51:59
stale include? (We include this file below so I t
| |
| 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 quota::QuotaStatusCode quota_status() const { return quota_status_; } | |
| 46 int64 usage() { return usage_; } | |
| 47 int64 quota() { return quota_; } | |
| 48 | |
| 49 virtual void SetUp(); | |
| 50 virtual void TearDown(); | |
| 51 | |
| 52 void OnGetUsageAndQuota( | |
| 53 quota::QuotaStatusCode status, int64 usage, int64 quota); | |
| 54 | |
| 55 protected: | |
| 56 void PrepareFileSet(const FilePath& virtual_path); | |
| 57 | |
| 58 GURL URLForPath(const FilePath& path) const { | |
| 59 return test_helper_.GetURLForPath(path); | |
| 60 } | |
| 61 | |
| 62 FilePath PlatformPath(const FilePath& virtual_path) { | |
| 63 return test_helper_.GetLocalPath(virtual_path); | |
| 64 } | |
| 65 | |
| 66 int64 ActualSize() { | |
| 67 return test_helper_.ComputeCurrentOriginUsage(); | |
| 68 } | |
| 69 | |
| 70 int64 SizeInUsageFile() { | |
| 71 return test_helper_.GetCachedOriginUsage(); | |
| 72 } | |
| 73 | |
| 74 void GetUsageAndQuotaFromQuotaManager() { | |
| 75 quota_manager_->GetUsageAndQuota( | |
| 76 test_helper_.origin(), test_helper_.storage_type(), | |
| 77 callback_factory_.NewCallback( | |
| 78 &FileSystemQuotaTest::OnGetUsageAndQuota)); | |
| 79 MessageLoop::current()->RunAllPending(); | |
| 80 } | |
| 81 | |
| 82 bool VirtualFileExists(const FilePath& virtual_path) { | |
| 83 return file_util::PathExists(PlatformPath(virtual_path)) && | |
| 84 !file_util::DirectoryExists(PlatformPath(virtual_path)); | |
| 85 } | |
| 86 | |
| 87 bool VirtualDirectoryExists(const FilePath& virtual_path) { | |
| 88 return file_util::DirectoryExists(PlatformPath(virtual_path)); | |
| 89 } | |
| 90 | |
| 91 FilePath CreateVirtualTemporaryFileInDir(const FilePath& virtual_dir_path) { | |
| 92 FilePath absolute_dir_path(PlatformPath(virtual_dir_path)); | |
| 93 FilePath absolute_file_path; | |
| 94 EXPECT_TRUE(file_util::CreateTemporaryFileInDir(absolute_dir_path, | |
| 95 &absolute_file_path)); | |
| 96 return virtual_dir_path.Append(absolute_file_path.BaseName()); | |
| 97 } | |
| 98 | |
| 99 FilePath CreateVirtualTemporaryDirInDir(const FilePath& virtual_dir_path) { | |
| 100 FilePath absolute_parent_dir_path(PlatformPath(virtual_dir_path)); | |
| 101 FilePath absolute_child_dir_path; | |
| 102 EXPECT_TRUE(file_util::CreateTemporaryDirInDir(absolute_parent_dir_path, | |
| 103 FILE_PATH_LITERAL(""), | |
| 104 &absolute_child_dir_path)); | |
| 105 return virtual_dir_path.Append(absolute_child_dir_path.BaseName()); | |
| 106 } | |
| 107 | |
| 108 FilePath CreateVirtualTemporaryDir() { | |
| 109 return CreateVirtualTemporaryDirInDir(FilePath()); | |
| 110 } | |
| 111 | |
| 112 FilePath child_dir_path_; | |
| 113 FilePath child_file1_path_; | |
| 114 FilePath child_file2_path_; | |
| 115 FilePath grandchild_file1_path_; | |
| 116 FilePath grandchild_file2_path_; | |
| 117 | |
| 118 private: | |
| 119 FileSystemTestOriginHelper test_helper_; | |
| 120 | |
| 121 ScopedTempDir work_dir_; | |
| 122 scoped_refptr<quota::QuotaManager> quota_manager_; | |
| 123 | |
| 124 base::ScopedCallbackFactory<FileSystemQuotaTest> callback_factory_; | |
| 125 | |
| 126 // For post-operation status. | |
| 127 int status_; | |
| 128 quota::QuotaStatusCode quota_status_; | |
| 129 int64 usage_; | |
| 130 int64 quota_; | |
| 131 | |
| 132 DISALLOW_COPY_AND_ASSIGN(FileSystemQuotaTest); | |
| 133 }; | |
| 134 | |
| 135 namespace { | |
| 136 | |
| 137 class MockDispatcher : public FileSystemCallbackDispatcher { | |
| 138 public: | |
| 139 MockDispatcher(FileSystemQuotaTest* test) : test_(test) { } | |
|
kinuko
2011/06/14 06:51:59
nit: explicit
| |
| 140 | |
| 141 virtual void DidFail(base::PlatformFileError status) { | |
| 142 test_->set_status(status); | |
| 143 } | |
| 144 | |
| 145 virtual void DidSucceed() { | |
| 146 test_->set_status(base::PLATFORM_FILE_OK); | |
| 147 } | |
| 148 | |
| 149 virtual void DidGetLocalPath(const FilePath& local_path) { | |
| 150 ADD_FAILURE(); | |
| 151 } | |
| 152 | |
| 153 virtual void DidReadMetadata( | |
| 154 const base::PlatformFileInfo& info, | |
| 155 const FilePath& platform_path) { | |
| 156 ADD_FAILURE(); | |
| 157 } | |
| 158 | |
| 159 virtual void DidReadDirectory( | |
| 160 const std::vector<base::FileUtilProxy::Entry>& entries, | |
| 161 bool /* has_more */) { | |
| 162 ADD_FAILURE(); | |
| 163 } | |
| 164 | |
| 165 virtual void DidOpenFileSystem(const std::string&, const GURL&) { | |
| 166 ADD_FAILURE(); | |
| 167 } | |
| 168 | |
| 169 virtual void DidWrite(int64 bytes, bool complete) { | |
| 170 ADD_FAILURE(); | |
| 171 } | |
| 172 | |
| 173 private: | |
| 174 FileSystemQuotaTest* test_; | |
| 175 }; | |
| 176 | |
| 177 } // namespace (anonymous) | |
| 178 | |
| 179 void FileSystemQuotaTest::SetUp() { | |
| 180 ASSERT_TRUE(work_dir_.CreateUniqueTempDir()); | |
| 181 FilePath filesystem_dir_path = work_dir_.path().AppendASCII("filesystem"); | |
| 182 file_util::CreateDirectory(filesystem_dir_path); | |
| 183 | |
| 184 quota_manager_ = new quota::QuotaManager( | |
| 185 false /* is_incognito */, | |
| 186 filesystem_dir_path, | |
| 187 base::MessageLoopProxy::CreateForCurrentThread(), | |
| 188 base::MessageLoopProxy::CreateForCurrentThread(), | |
| 189 NULL); | |
| 190 | |
| 191 test_helper_.SetUp(filesystem_dir_path, | |
| 192 false /* incognito */, | |
| 193 false /* unlimited quota */, | |
| 194 quota_manager_->proxy(), | |
| 195 LocalFileSystemFileUtil::GetInstance()); | |
| 196 } | |
| 197 | |
| 198 void FileSystemQuotaTest::TearDown() { | |
| 199 quota_manager_ = NULL; | |
| 200 test_helper_.TearDown(); | |
| 201 } | |
| 202 | |
| 203 FileSystemOperation* FileSystemQuotaTest::operation() { | |
| 204 return test_helper_.NewOperation(new MockDispatcher(this)); | |
| 205 } | |
| 206 | |
| 207 void FileSystemQuotaTest::OnGetUsageAndQuota( | |
| 208 quota::QuotaStatusCode status, int64 usage, int64 quota) { | |
| 209 quota_status_ = status; | |
| 210 usage_ = usage; | |
| 211 quota_ = quota; | |
| 212 } | |
| 213 | |
| 214 void FileSystemQuotaTest::PrepareFileSet(const FilePath& virtual_path) { | |
| 215 child_dir_path_ = CreateVirtualTemporaryDirInDir(virtual_path); | |
| 216 child_file1_path_ = CreateVirtualTemporaryFileInDir(virtual_path); | |
| 217 child_file2_path_ = CreateVirtualTemporaryFileInDir(virtual_path); | |
| 218 grandchild_file1_path_ = CreateVirtualTemporaryFileInDir(child_dir_path_); | |
| 219 grandchild_file2_path_ = CreateVirtualTemporaryFileInDir(child_dir_path_); | |
| 220 } | |
| 221 | |
| 222 TEST_F(FileSystemQuotaTest, TestMoveSuccessSrcDirRecursive) { | |
| 223 FilePath src_dir_path(CreateVirtualTemporaryDir()); | |
| 224 PrepareFileSet(src_dir_path); | |
| 225 FilePath dest_dir_path(CreateVirtualTemporaryDir()); | |
| 226 | |
| 227 EXPECT_EQ(0, ActualSize()); | |
| 228 | |
| 229 operation()->Truncate(URLForPath(child_file1_path_), 5000); | |
| 230 operation()->Truncate(URLForPath(child_file2_path_), 400); | |
| 231 operation()->Truncate(URLForPath(grandchild_file1_path_), 30); | |
| 232 operation()->Truncate(URLForPath(grandchild_file2_path_), 2); | |
| 233 MessageLoop::current()->RunAllPending(); | |
| 234 | |
| 235 const int64 all_file_size = 5000 + 400 + 30 + 2; | |
| 236 const int64 usage_file_size = FileSystemUsageCache::kUsageFileSize; | |
| 237 | |
| 238 EXPECT_EQ(all_file_size, ActualSize()); | |
| 239 EXPECT_EQ(all_file_size, SizeInUsageFile()); | |
| 240 GetUsageAndQuotaFromQuotaManager(); | |
| 241 EXPECT_EQ(quota::kQuotaStatusOk, quota_status()); | |
| 242 EXPECT_EQ(all_file_size + usage_file_size, usage()); | |
| 243 ASSERT_LT(all_file_size, quota()); | |
| 244 | |
| 245 operation()->Move(URLForPath(src_dir_path), URLForPath(dest_dir_path)); | |
| 246 MessageLoop::current()->RunAllPending(); | |
| 247 | |
| 248 EXPECT_EQ(base::PLATFORM_FILE_OK, status()); | |
| 249 EXPECT_TRUE(VirtualDirectoryExists(dest_dir_path.Append( | |
| 250 child_dir_path_.BaseName()))); | |
| 251 EXPECT_TRUE(VirtualFileExists(dest_dir_path.Append( | |
| 252 child_dir_path_.BaseName()).Append( | |
| 253 grandchild_file1_path_.BaseName()))); | |
| 254 | |
| 255 EXPECT_EQ(all_file_size, ActualSize()); | |
| 256 EXPECT_EQ(all_file_size, SizeInUsageFile()); | |
| 257 GetUsageAndQuotaFromQuotaManager(); | |
| 258 EXPECT_EQ(quota::kQuotaStatusOk, quota_status()); | |
| 259 EXPECT_EQ(all_file_size + usage_file_size, usage()); | |
| 260 ASSERT_LT(all_file_size, quota()); | |
| 261 } | |
| 262 | |
| 263 TEST_F(FileSystemQuotaTest, TestCopySuccessSrcDirRecursive) { | |
| 264 FilePath src_dir_path(CreateVirtualTemporaryDir()); | |
| 265 PrepareFileSet(src_dir_path); | |
| 266 FilePath dest_dir1_path(CreateVirtualTemporaryDir()); | |
| 267 FilePath dest_dir2_path(CreateVirtualTemporaryDir()); | |
| 268 | |
| 269 EXPECT_EQ(0, ActualSize()); | |
| 270 | |
| 271 operation()->Truncate(URLForPath(child_file1_path_), 8000); | |
| 272 operation()->Truncate(URLForPath(child_file2_path_), 700); | |
| 273 operation()->Truncate(URLForPath(grandchild_file1_path_), 60); | |
| 274 operation()->Truncate(URLForPath(grandchild_file2_path_), 5); | |
| 275 MessageLoop::current()->RunAllPending(); | |
| 276 | |
| 277 const int64 child_file_size = 8000 + 700; | |
| 278 const int64 grandchild_file_size = 60 + 5; | |
| 279 const int64 all_file_size = child_file_size + grandchild_file_size; | |
| 280 const int64 usage_file_size = FileSystemUsageCache::kUsageFileSize; | |
| 281 | |
| 282 EXPECT_EQ(all_file_size, ActualSize()); | |
| 283 EXPECT_EQ(all_file_size, SizeInUsageFile()); | |
| 284 GetUsageAndQuotaFromQuotaManager(); | |
| 285 EXPECT_EQ(quota::kQuotaStatusOk, quota_status()); | |
| 286 EXPECT_EQ(all_file_size + usage_file_size, usage()); | |
| 287 ASSERT_LT(all_file_size, quota()); | |
| 288 | |
| 289 operation()->Copy(URLForPath(src_dir_path), URLForPath(dest_dir1_path)); | |
| 290 MessageLoop::current()->RunAllPending(); | |
| 291 | |
| 292 EXPECT_EQ(base::PLATFORM_FILE_OK, status()); | |
| 293 EXPECT_TRUE(VirtualDirectoryExists(src_dir_path.Append( | |
| 294 child_dir_path_.BaseName()))); | |
| 295 EXPECT_TRUE(VirtualFileExists(src_dir_path.Append( | |
| 296 child_dir_path_.BaseName()).Append( | |
| 297 grandchild_file1_path_.BaseName()))); | |
| 298 EXPECT_EQ(base::PLATFORM_FILE_OK, status()); | |
| 299 EXPECT_TRUE(VirtualDirectoryExists(dest_dir1_path.Append( | |
| 300 child_dir_path_.BaseName()))); | |
| 301 EXPECT_TRUE(VirtualFileExists(dest_dir1_path.Append( | |
| 302 child_dir_path_.BaseName()).Append( | |
| 303 grandchild_file1_path_.BaseName()))); | |
| 304 | |
| 305 EXPECT_EQ(2 * all_file_size, ActualSize()); | |
| 306 EXPECT_EQ(2 * all_file_size, SizeInUsageFile()); | |
| 307 GetUsageAndQuotaFromQuotaManager(); | |
| 308 EXPECT_EQ(quota::kQuotaStatusOk, quota_status()); | |
| 309 EXPECT_EQ(2 * all_file_size + usage_file_size, usage()); | |
| 310 ASSERT_LT(2 * all_file_size, quota()); | |
| 311 | |
| 312 operation()->Copy(URLForPath(child_dir_path_), URLForPath(dest_dir2_path)); | |
| 313 MessageLoop::current()->RunAllPending(); | |
| 314 | |
| 315 EXPECT_EQ(base::PLATFORM_FILE_OK, status()); | |
| 316 | |
| 317 EXPECT_EQ(2 * child_file_size + 3 * grandchild_file_size, | |
| 318 ActualSize()); | |
| 319 EXPECT_EQ(2 * child_file_size + 3 * grandchild_file_size, | |
| 320 SizeInUsageFile()); | |
| 321 GetUsageAndQuotaFromQuotaManager(); | |
| 322 EXPECT_EQ(quota::kQuotaStatusOk, quota_status()); | |
| 323 EXPECT_EQ(2 * child_file_size + 3 * grandchild_file_size + usage_file_size, | |
| 324 usage()); | |
| 325 ASSERT_LT(2 * child_file_size + 3 * grandchild_file_size, quota()); | |
| 326 } | |
| 327 | |
| 328 } // namespace fileapi | |
| OLD | NEW |