| OLD | NEW |
| (Empty) |
| 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 | |
| 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/bind.h" | |
| 11 #include "base/files/scoped_temp_dir.h" | |
| 12 #include "base/logging.h" | |
| 13 #include "base/memory/scoped_ptr.h" | |
| 14 #include "base/memory/weak_ptr.h" | |
| 15 #include "base/message_loop.h" | |
| 16 #include "base/platform_file.h" | |
| 17 #include "base/strings/string_number_conversions.h" | |
| 18 #include "testing/gtest/include/gtest/gtest.h" | |
| 19 #include "webkit/fileapi/async_file_test_helper.h" | |
| 20 #include "webkit/fileapi/file_system_file_util.h" | |
| 21 #include "webkit/fileapi/file_system_operation_context.h" | |
| 22 #include "webkit/fileapi/file_system_usage_cache.h" | |
| 23 #include "webkit/fileapi/file_system_util.h" | |
| 24 #include "webkit/fileapi/local_file_system_operation.h" | |
| 25 #include "webkit/fileapi/local_file_system_test_helper.h" | |
| 26 #include "webkit/quota/quota_manager.h" | |
| 27 | |
| 28 namespace fileapi { | |
| 29 | |
| 30 const int kFileOperationStatusNotSet = 1; | |
| 31 | |
| 32 namespace { | |
| 33 | |
| 34 void AssertFileErrorEq(base::PlatformFileError expected, | |
| 35 base::PlatformFileError actual) { | |
| 36 ASSERT_EQ(expected, actual); | |
| 37 } | |
| 38 | |
| 39 } // namespace | |
| 40 | |
| 41 class LocalFileSystemQuotaTest | |
| 42 : public testing::Test, | |
| 43 public base::SupportsWeakPtr<LocalFileSystemQuotaTest> { | |
| 44 public: | |
| 45 LocalFileSystemQuotaTest() | |
| 46 : weak_factory_(this), | |
| 47 next_unique_path_suffix_(0), | |
| 48 status_(kFileOperationStatusNotSet), | |
| 49 quota_status_(quota::kQuotaStatusUnknown), | |
| 50 usage_(-1), | |
| 51 quota_(-1) {} | |
| 52 | |
| 53 virtual void SetUp() OVERRIDE { | |
| 54 ASSERT_TRUE(work_dir_.CreateUniqueTempDir()); | |
| 55 base::FilePath filesystem_dir_path = | |
| 56 work_dir_.path().AppendASCII("filesystem"); | |
| 57 ASSERT_TRUE(file_util::CreateDirectory(filesystem_dir_path)); | |
| 58 | |
| 59 quota_manager_ = new quota::QuotaManager( | |
| 60 false /* is_incognito */, | |
| 61 filesystem_dir_path, | |
| 62 base::MessageLoopProxy::current(), | |
| 63 base::MessageLoopProxy::current(), | |
| 64 NULL); | |
| 65 | |
| 66 test_helper_.SetUp(filesystem_dir_path, quota_manager_->proxy()); | |
| 67 } | |
| 68 | |
| 69 virtual void TearDown() OVERRIDE { | |
| 70 quota_manager_ = NULL; | |
| 71 test_helper_.TearDown(); | |
| 72 } | |
| 73 | |
| 74 LocalFileSystemOperation* NewOperation() { | |
| 75 return test_helper_.NewOperation(); | |
| 76 } | |
| 77 | |
| 78 int status() const { return status_; } | |
| 79 quota::QuotaStatusCode quota_status() const { return quota_status_; } | |
| 80 int64 usage() { return usage_; } | |
| 81 int64 quota() { return quota_; } | |
| 82 | |
| 83 protected: | |
| 84 FileSystemFileUtil* file_util() { | |
| 85 return test_helper_.file_util(); | |
| 86 } | |
| 87 | |
| 88 FileSystemOperationContext* NewContext() { | |
| 89 FileSystemOperationContext* context = test_helper_.NewOperationContext(); | |
| 90 context->set_allowed_bytes_growth(10000000); | |
| 91 return context; | |
| 92 } | |
| 93 | |
| 94 void PrepareFileSet(const base::FilePath& virtual_path); | |
| 95 | |
| 96 FileSystemURL URLForPath(const base::FilePath& path) const { | |
| 97 return test_helper_.CreateURL(path); | |
| 98 } | |
| 99 | |
| 100 base::FilePath PlatformPath(const base::FilePath& virtual_path) { | |
| 101 return test_helper_.GetLocalPath(virtual_path); | |
| 102 } | |
| 103 | |
| 104 int64 ActualFileSize() { | |
| 105 return test_helper_.ComputeCurrentOriginUsage() - | |
| 106 test_helper_.ComputeCurrentDirectoryDatabaseUsage(); | |
| 107 } | |
| 108 | |
| 109 int64 SizeByQuotaUtil() { | |
| 110 base::MessageLoop::current()->RunUntilIdle(); | |
| 111 return test_helper_.GetCachedOriginUsage(); | |
| 112 } | |
| 113 | |
| 114 void GetUsageAndQuotaFromQuotaManager() { | |
| 115 quota_status_ = AsyncFileTestHelper::GetUsageAndQuota( | |
| 116 quota_manager_, test_helper_.origin(), test_helper_.type(), | |
| 117 &usage_, "a_); | |
| 118 base::MessageLoop::current()->RunUntilIdle(); | |
| 119 } | |
| 120 | |
| 121 bool FileExists(const base::FilePath& virtual_path) { | |
| 122 FileSystemURL url = test_helper_.CreateURL(virtual_path); | |
| 123 return AsyncFileTestHelper::FileExists( | |
| 124 test_helper_.file_system_context(), url, | |
| 125 AsyncFileTestHelper::kDontCheckSize); | |
| 126 } | |
| 127 | |
| 128 bool DirectoryExists(const base::FilePath& virtual_path) { | |
| 129 FileSystemURL url = test_helper_.CreateURL(virtual_path); | |
| 130 return AsyncFileTestHelper::DirectoryExists( | |
| 131 test_helper_.file_system_context(), url); | |
| 132 } | |
| 133 | |
| 134 base::FilePath CreateUniqueFileInDir(const base::FilePath& virtual_dir_path) { | |
| 135 base::FilePath file_name = base::FilePath::FromUTF8Unsafe( | |
| 136 "tmpfile-" + base::IntToString(next_unique_path_suffix_++)); | |
| 137 FileSystemURL url = test_helper_.CreateURL( | |
| 138 virtual_dir_path.Append(file_name)); | |
| 139 | |
| 140 scoped_ptr<FileSystemOperationContext> context(NewContext()); | |
| 141 bool created; | |
| 142 EXPECT_EQ(base::PLATFORM_FILE_OK, | |
| 143 file_util()->EnsureFileExists(context.get(), url, &created)); | |
| 144 EXPECT_TRUE(created); | |
| 145 return url.path(); | |
| 146 } | |
| 147 | |
| 148 base::FilePath CreateUniqueDirInDir(const base::FilePath& virtual_dir_path) { | |
| 149 base::FilePath dir_name = base::FilePath::FromUTF8Unsafe( | |
| 150 "tmpdir-" + base::IntToString(next_unique_path_suffix_++)); | |
| 151 FileSystemURL url = test_helper_.CreateURL( | |
| 152 virtual_dir_path.Append(dir_name)); | |
| 153 | |
| 154 scoped_ptr<FileSystemOperationContext> context(NewContext()); | |
| 155 EXPECT_EQ(base::PLATFORM_FILE_OK, | |
| 156 file_util()->CreateDirectory(context.get(), url, false, true)); | |
| 157 return url.path(); | |
| 158 } | |
| 159 | |
| 160 base::FilePath CreateUniqueDir() { | |
| 161 return CreateUniqueDirInDir(base::FilePath()); | |
| 162 } | |
| 163 | |
| 164 base::FilePath child_dir_path_; | |
| 165 base::FilePath child_file1_path_; | |
| 166 base::FilePath child_file2_path_; | |
| 167 base::FilePath grandchild_file1_path_; | |
| 168 base::FilePath grandchild_file2_path_; | |
| 169 | |
| 170 int64 child_path_cost_; | |
| 171 int64 grandchild_path_cost_; | |
| 172 | |
| 173 protected: | |
| 174 // Callback for recording test results. | |
| 175 FileSystemOperation::StatusCallback RecordStatusCallback() { | |
| 176 return base::Bind(&LocalFileSystemQuotaTest::DidFinish, AsWeakPtr()); | |
| 177 } | |
| 178 | |
| 179 void DidFinish(base::PlatformFileError status) { | |
| 180 status_ = status; | |
| 181 } | |
| 182 | |
| 183 LocalFileSystemTestOriginHelper test_helper_; | |
| 184 | |
| 185 base::ScopedTempDir work_dir_; | |
| 186 base::MessageLoop message_loop_; | |
| 187 scoped_refptr<quota::QuotaManager> quota_manager_; | |
| 188 | |
| 189 base::WeakPtrFactory<LocalFileSystemQuotaTest> weak_factory_; | |
| 190 | |
| 191 int next_unique_path_suffix_; | |
| 192 | |
| 193 // For post-operation status. | |
| 194 int status_; | |
| 195 quota::QuotaStatusCode quota_status_; | |
| 196 int64 usage_; | |
| 197 int64 quota_; | |
| 198 | |
| 199 DISALLOW_COPY_AND_ASSIGN(LocalFileSystemQuotaTest); | |
| 200 }; | |
| 201 | |
| 202 void LocalFileSystemQuotaTest::PrepareFileSet( | |
| 203 const base::FilePath& virtual_path) { | |
| 204 int64 usage = SizeByQuotaUtil(); | |
| 205 child_dir_path_ = CreateUniqueDirInDir(virtual_path); | |
| 206 child_file1_path_ = CreateUniqueFileInDir(virtual_path); | |
| 207 child_file2_path_ = CreateUniqueFileInDir(virtual_path); | |
| 208 child_path_cost_ = SizeByQuotaUtil() - usage; | |
| 209 usage += child_path_cost_; | |
| 210 | |
| 211 grandchild_file1_path_ = CreateUniqueFileInDir(child_dir_path_); | |
| 212 grandchild_file2_path_ = CreateUniqueFileInDir(child_dir_path_); | |
| 213 grandchild_path_cost_ = SizeByQuotaUtil() - usage; | |
| 214 } | |
| 215 | |
| 216 TEST_F(LocalFileSystemQuotaTest, TestMoveSuccessSrcDirRecursive) { | |
| 217 base::FilePath src_dir_path(CreateUniqueDir()); | |
| 218 int src_path_cost = SizeByQuotaUtil(); | |
| 219 PrepareFileSet(src_dir_path); | |
| 220 base::FilePath dest_dir_path(CreateUniqueDir()); | |
| 221 | |
| 222 EXPECT_EQ(0, ActualFileSize()); | |
| 223 int total_path_cost = SizeByQuotaUtil(); | |
| 224 | |
| 225 NewOperation()->Truncate( | |
| 226 URLForPath(child_file1_path_), 5000, | |
| 227 base::Bind(&AssertFileErrorEq, base::PLATFORM_FILE_OK)); | |
| 228 NewOperation()->Truncate( | |
| 229 URLForPath(child_file2_path_), 400, | |
| 230 base::Bind(&AssertFileErrorEq, base::PLATFORM_FILE_OK)); | |
| 231 NewOperation()->Truncate( | |
| 232 URLForPath(grandchild_file1_path_), 30, | |
| 233 base::Bind(&AssertFileErrorEq, base::PLATFORM_FILE_OK)); | |
| 234 NewOperation()->Truncate( | |
| 235 URLForPath(grandchild_file2_path_), 2, | |
| 236 base::Bind(&AssertFileErrorEq, base::PLATFORM_FILE_OK)); | |
| 237 base::MessageLoop::current()->RunUntilIdle(); | |
| 238 | |
| 239 const int64 all_file_size = 5000 + 400 + 30 + 2; | |
| 240 | |
| 241 EXPECT_EQ(all_file_size, ActualFileSize()); | |
| 242 EXPECT_EQ(all_file_size + total_path_cost, SizeByQuotaUtil()); | |
| 243 GetUsageAndQuotaFromQuotaManager(); | |
| 244 EXPECT_EQ(quota::kQuotaStatusOk, quota_status()); | |
| 245 EXPECT_EQ(all_file_size + total_path_cost, usage()); | |
| 246 ASSERT_LT(all_file_size + total_path_cost, quota()); | |
| 247 | |
| 248 NewOperation()->Move( | |
| 249 URLForPath(src_dir_path), URLForPath(dest_dir_path), | |
| 250 RecordStatusCallback()); | |
| 251 base::MessageLoop::current()->RunUntilIdle(); | |
| 252 | |
| 253 EXPECT_EQ(base::PLATFORM_FILE_OK, status()); | |
| 254 EXPECT_TRUE(DirectoryExists(dest_dir_path.Append( | |
| 255 VirtualPath::BaseName(child_dir_path_)))); | |
| 256 EXPECT_TRUE(FileExists(dest_dir_path.Append( | |
| 257 VirtualPath::BaseName(child_dir_path_)).Append( | |
| 258 VirtualPath::BaseName(grandchild_file1_path_)))); | |
| 259 | |
| 260 EXPECT_EQ(all_file_size, ActualFileSize()); | |
| 261 EXPECT_EQ(all_file_size + total_path_cost - src_path_cost, | |
| 262 SizeByQuotaUtil()); | |
| 263 GetUsageAndQuotaFromQuotaManager(); | |
| 264 EXPECT_EQ(quota::kQuotaStatusOk, quota_status()); | |
| 265 EXPECT_EQ(all_file_size + total_path_cost - src_path_cost, usage()); | |
| 266 ASSERT_LT(all_file_size + total_path_cost - src_path_cost, quota()); | |
| 267 } | |
| 268 | |
| 269 TEST_F(LocalFileSystemQuotaTest, TestCopySuccessSrcDirRecursive) { | |
| 270 base::FilePath src_dir_path(CreateUniqueDir()); | |
| 271 PrepareFileSet(src_dir_path); | |
| 272 base::FilePath dest_dir1_path(CreateUniqueDir()); | |
| 273 base::FilePath dest_dir2_path(CreateUniqueDir()); | |
| 274 | |
| 275 EXPECT_EQ(0, ActualFileSize()); | |
| 276 int total_path_cost = SizeByQuotaUtil(); | |
| 277 | |
| 278 NewOperation()->Truncate( | |
| 279 URLForPath(child_file1_path_), 8000, | |
| 280 base::Bind(&AssertFileErrorEq, base::PLATFORM_FILE_OK)); | |
| 281 NewOperation()->Truncate( | |
| 282 URLForPath(child_file2_path_), 700, | |
| 283 base::Bind(&AssertFileErrorEq, base::PLATFORM_FILE_OK)); | |
| 284 NewOperation()->Truncate( | |
| 285 URLForPath(grandchild_file1_path_), 60, | |
| 286 base::Bind(&AssertFileErrorEq, base::PLATFORM_FILE_OK)); | |
| 287 NewOperation()->Truncate( | |
| 288 URLForPath(grandchild_file2_path_), 5, | |
| 289 base::Bind(&AssertFileErrorEq, base::PLATFORM_FILE_OK)); | |
| 290 base::MessageLoop::current()->RunUntilIdle(); | |
| 291 | |
| 292 const int64 child_file_size = 8000 + 700; | |
| 293 const int64 grandchild_file_size = 60 + 5; | |
| 294 const int64 all_file_size = child_file_size + grandchild_file_size; | |
| 295 int64 expected_usage = all_file_size + total_path_cost; | |
| 296 | |
| 297 EXPECT_EQ(all_file_size, ActualFileSize()); | |
| 298 EXPECT_EQ(expected_usage, SizeByQuotaUtil()); | |
| 299 GetUsageAndQuotaFromQuotaManager(); | |
| 300 EXPECT_EQ(quota::kQuotaStatusOk, quota_status()); | |
| 301 EXPECT_EQ(expected_usage, usage()); | |
| 302 ASSERT_LT(expected_usage, quota()); | |
| 303 | |
| 304 NewOperation()->Copy( | |
| 305 URLForPath(src_dir_path), URLForPath(dest_dir1_path), | |
| 306 RecordStatusCallback()); | |
| 307 base::MessageLoop::current()->RunUntilIdle(); | |
| 308 expected_usage += all_file_size + child_path_cost_ + grandchild_path_cost_; | |
| 309 | |
| 310 EXPECT_EQ(base::PLATFORM_FILE_OK, status()); | |
| 311 EXPECT_TRUE(DirectoryExists(src_dir_path.Append( | |
| 312 VirtualPath::BaseName(child_dir_path_)))); | |
| 313 EXPECT_TRUE(FileExists(src_dir_path.Append( | |
| 314 VirtualPath::BaseName(child_dir_path_)).Append( | |
| 315 VirtualPath::BaseName(grandchild_file1_path_)))); | |
| 316 EXPECT_EQ(base::PLATFORM_FILE_OK, status()); | |
| 317 EXPECT_TRUE(DirectoryExists(dest_dir1_path.Append( | |
| 318 VirtualPath::BaseName(child_dir_path_)))); | |
| 319 EXPECT_TRUE(FileExists(dest_dir1_path.Append( | |
| 320 VirtualPath::BaseName(child_dir_path_)).Append( | |
| 321 VirtualPath::BaseName(grandchild_file1_path_)))); | |
| 322 | |
| 323 EXPECT_EQ(2 * all_file_size, ActualFileSize()); | |
| 324 EXPECT_EQ(expected_usage, SizeByQuotaUtil()); | |
| 325 GetUsageAndQuotaFromQuotaManager(); | |
| 326 EXPECT_EQ(quota::kQuotaStatusOk, quota_status()); | |
| 327 EXPECT_EQ(expected_usage, usage()); | |
| 328 ASSERT_LT(expected_usage, quota()); | |
| 329 | |
| 330 NewOperation()->Copy( | |
| 331 URLForPath(child_dir_path_), URLForPath(dest_dir2_path), | |
| 332 RecordStatusCallback()); | |
| 333 base::MessageLoop::current()->RunUntilIdle(); | |
| 334 | |
| 335 EXPECT_EQ(base::PLATFORM_FILE_OK, status()); | |
| 336 expected_usage += grandchild_file_size + grandchild_path_cost_; | |
| 337 | |
| 338 EXPECT_EQ(2 * child_file_size + 3 * grandchild_file_size, ActualFileSize()); | |
| 339 EXPECT_EQ(expected_usage, SizeByQuotaUtil()); | |
| 340 GetUsageAndQuotaFromQuotaManager(); | |
| 341 EXPECT_EQ(quota::kQuotaStatusOk, quota_status()); | |
| 342 EXPECT_EQ(expected_usage, usage()); | |
| 343 ASSERT_LT(expected_usage, quota()); | |
| 344 } | |
| 345 | |
| 346 } // namespace fileapi | |
| OLD | NEW |