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