| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <map> | 5 #include <map> |
| 6 #include <queue> | 6 #include <queue> |
| 7 | 7 |
| 8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/files/scoped_temp_dir.h" | 10 #include "base/files/scoped_temp_dir.h" |
| 11 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
| 12 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
| 13 #include "base/stl_util.h" | 13 #include "base/stl_util.h" |
| 14 #include "base/stringprintf.h" | |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 #include "webkit/fileapi/async_file_test_helper.h" |
| 16 #include "webkit/fileapi/external_mount_points.h" | 16 #include "webkit/fileapi/external_mount_points.h" |
| 17 #include "webkit/fileapi/file_system_context.h" | 17 #include "webkit/fileapi/file_system_context.h" |
| 18 #include "webkit/fileapi/file_system_mount_point_provider.h" | 18 #include "webkit/fileapi/file_system_mount_point_provider.h" |
| 19 #include "webkit/fileapi/file_system_operation.h" |
| 19 #include "webkit/fileapi/file_system_task_runners.h" | 20 #include "webkit/fileapi/file_system_task_runners.h" |
| 20 #include "webkit/fileapi/file_system_url.h" | 21 #include "webkit/fileapi/file_system_url.h" |
| 21 #include "webkit/fileapi/file_system_util.h" | 22 #include "webkit/fileapi/file_system_util.h" |
| 22 #include "webkit/fileapi/local_file_system_operation.h" | |
| 23 #include "webkit/fileapi/mock_file_system_options.h" | 23 #include "webkit/fileapi/mock_file_system_options.h" |
| 24 #include "webkit/fileapi/test_file_set.h" | 24 #include "webkit/fileapi/test_file_set.h" |
| 25 #include "webkit/quota/mock_quota_manager.h" | 25 #include "webkit/quota/mock_quota_manager.h" |
| 26 #include "webkit/quota/mock_special_storage_policy.h" | 26 #include "webkit/quota/mock_special_storage_policy.h" |
| 27 #include "webkit/quota/quota_manager.h" | 27 #include "webkit/quota/quota_manager.h" |
| 28 | 28 |
| 29 namespace fileapi { | 29 namespace fileapi { |
| 30 | 30 |
| 31 namespace { | |
| 32 | |
| 33 const int64 kDontCheckSize = -1; | |
| 34 typedef FileSystemOperation::FileEntryList FileEntryList; | 31 typedef FileSystemOperation::FileEntryList FileEntryList; |
| 35 | 32 |
| 36 void AssignAndQuit(base::RunLoop* run_loop, | |
| 37 base::PlatformFileError* result_out, | |
| 38 base::PlatformFileError result) { | |
| 39 *result_out = result; | |
| 40 run_loop->Quit(); | |
| 41 } | |
| 42 | |
| 43 base::Callback<void(base::PlatformFileError)> | |
| 44 AssignAndQuitCallback(base::RunLoop* run_loop, | |
| 45 base::PlatformFileError* result) { | |
| 46 return base::Bind(&AssignAndQuit, run_loop, base::Unretained(result)); | |
| 47 } | |
| 48 | |
| 49 void GetMetadataCallback(base::RunLoop* run_loop, | |
| 50 base::PlatformFileError* result_out, | |
| 51 base::PlatformFileInfo* file_info_out, | |
| 52 base::PlatformFileError result, | |
| 53 const base::PlatformFileInfo& file_info, | |
| 54 const base::FilePath& /* platform_path */) { | |
| 55 *result_out = result; | |
| 56 *file_info_out = file_info; | |
| 57 run_loop->Quit(); | |
| 58 } | |
| 59 | |
| 60 void ReadDirectoryCallback(base::RunLoop* run_loop, | |
| 61 base::PlatformFileError* result_out, | |
| 62 FileEntryList* entries_out, | |
| 63 base::PlatformFileError result, | |
| 64 const FileEntryList& entries, | |
| 65 bool has_more) { | |
| 66 *result_out = result; | |
| 67 *entries_out = entries; | |
| 68 if (result != base::PLATFORM_FILE_OK || !has_more) | |
| 69 run_loop->Quit(); | |
| 70 } | |
| 71 | |
| 72 void DidGetUsageAndQuota(quota::QuotaStatusCode* status_out, | |
| 73 int64* usage_out, | |
| 74 int64* quota_out, | |
| 75 quota::QuotaStatusCode status, | |
| 76 int64 usage, | |
| 77 int64 quota) { | |
| 78 if (status_out) | |
| 79 *status_out = status; | |
| 80 if (usage_out) | |
| 81 *usage_out = usage; | |
| 82 if (quota_out) | |
| 83 *quota_out = quota; | |
| 84 } | |
| 85 | |
| 86 class CrossOperationTestHelper { | 33 class CrossOperationTestHelper { |
| 87 public: | 34 public: |
| 88 CrossOperationTestHelper( | 35 CrossOperationTestHelper( |
| 89 const GURL& origin, | 36 const GURL& origin, |
| 90 FileSystemType src_type, | 37 FileSystemType src_type, |
| 91 FileSystemType dest_type) | 38 FileSystemType dest_type) |
| 92 : origin_(origin), | 39 : origin_(origin), |
| 93 src_type_(src_type), | 40 src_type_(src_type), |
| 94 dest_type_(dest_type) {} | 41 dest_type_(dest_type) {} |
| 95 | 42 |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 origin_, src_type_, FilePath::FromUTF8Unsafe(path)); | 105 origin_, src_type_, FilePath::FromUTF8Unsafe(path)); |
| 159 } | 106 } |
| 160 | 107 |
| 161 FileSystemURL DestURL(const std::string& path) { | 108 FileSystemURL DestURL(const std::string& path) { |
| 162 return file_system_context_->CreateCrackedFileSystemURL( | 109 return file_system_context_->CreateCrackedFileSystemURL( |
| 163 origin_, dest_type_, FilePath::FromUTF8Unsafe(path)); | 110 origin_, dest_type_, FilePath::FromUTF8Unsafe(path)); |
| 164 } | 111 } |
| 165 | 112 |
| 166 base::PlatformFileError Copy(const FileSystemURL& src, | 113 base::PlatformFileError Copy(const FileSystemURL& src, |
| 167 const FileSystemURL& dest) { | 114 const FileSystemURL& dest) { |
| 168 FileSystemOperation* operation = | 115 return AsyncFileTestHelper::Copy(file_system_context_, src, dest); |
| 169 file_system_context_->CreateFileSystemOperation(dest, NULL); | |
| 170 EXPECT_TRUE(operation != NULL); | |
| 171 base::PlatformFileError result = base::PLATFORM_FILE_ERROR_FAILED; | |
| 172 base::RunLoop run_loop; | |
| 173 operation->Copy(src, dest, AssignAndQuitCallback(&run_loop, &result)); | |
| 174 run_loop.Run(); | |
| 175 return result; | |
| 176 } | 116 } |
| 177 | 117 |
| 178 base::PlatformFileError Move(const FileSystemURL& src, | 118 base::PlatformFileError Move(const FileSystemURL& src, |
| 179 const FileSystemURL& dest) { | 119 const FileSystemURL& dest) { |
| 180 FileSystemOperation* operation = | 120 return AsyncFileTestHelper::Move(file_system_context_, src, dest); |
| 181 file_system_context_->CreateFileSystemOperation(dest, NULL); | |
| 182 EXPECT_TRUE(operation != NULL); | |
| 183 base::PlatformFileError result = base::PLATFORM_FILE_ERROR_FAILED; | |
| 184 base::RunLoop run_loop; | |
| 185 operation->Move(src, dest, AssignAndQuitCallback(&run_loop, &result)); | |
| 186 run_loop.Run(); | |
| 187 return result; | |
| 188 } | 121 } |
| 189 | 122 |
| 190 base::PlatformFileError SetUpTestCaseFiles( | 123 base::PlatformFileError SetUpTestCaseFiles( |
| 191 const FileSystemURL& root, | 124 const FileSystemURL& root, |
| 192 const test::TestCaseRecord* const test_cases, | 125 const test::TestCaseRecord* const test_cases, |
| 193 size_t test_case_size) { | 126 size_t test_case_size) { |
| 194 base::PlatformFileError result = base::PLATFORM_FILE_ERROR_FAILED; | 127 base::PlatformFileError result = base::PLATFORM_FILE_ERROR_FAILED; |
| 195 for (size_t i = 0; i < test_case_size; ++i) { | 128 for (size_t i = 0; i < test_case_size; ++i) { |
| 196 const test::TestCaseRecord& test_case = test_cases[i]; | 129 const test::TestCaseRecord& test_case = test_cases[i]; |
| 197 FileSystemURL url = root.WithPath(root.path().Append(test_case.path)); | 130 FileSystemURL url = root.WithPath(root.path().Append(test_case.path)); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 236 EXPECT_TRUE(FileExists(url, test_case_map[relative]->data_file_size)); | 169 EXPECT_TRUE(FileExists(url, test_case_map[relative]->data_file_size)); |
| 237 } | 170 } |
| 238 test_case_map.erase(relative); | 171 test_case_map.erase(relative); |
| 239 } | 172 } |
| 240 } | 173 } |
| 241 EXPECT_TRUE(test_case_map.empty()); | 174 EXPECT_TRUE(test_case_map.empty()); |
| 242 } | 175 } |
| 243 | 176 |
| 244 base::PlatformFileError ReadDirectory(const FileSystemURL& url, | 177 base::PlatformFileError ReadDirectory(const FileSystemURL& url, |
| 245 FileEntryList* entries) { | 178 FileEntryList* entries) { |
| 246 entries->clear(); | 179 return AsyncFileTestHelper::ReadDirectory( |
| 247 base::PlatformFileError result = base::PLATFORM_FILE_ERROR_FAILED; | 180 file_system_context_, url, entries); |
| 248 FileSystemOperation* operation = | |
| 249 file_system_context_->CreateFileSystemOperation(url, NULL); | |
| 250 EXPECT_TRUE(operation != NULL); | |
| 251 base::RunLoop run_loop; | |
| 252 operation->ReadDirectory( | |
| 253 url, base::Bind(&ReadDirectoryCallback, &run_loop, &result, entries)); | |
| 254 run_loop.Run(); | |
| 255 return result; | |
| 256 } | 181 } |
| 257 | 182 |
| 258 base::PlatformFileError CreateDirectory(const FileSystemURL& url) { | 183 base::PlatformFileError CreateDirectory(const FileSystemURL& url) { |
| 259 FileSystemOperation* operation = | 184 return AsyncFileTestHelper::CreateDirectory( |
| 260 file_system_context_->CreateFileSystemOperation(url, NULL); | 185 file_system_context_, url); |
| 261 EXPECT_TRUE(operation != NULL); | |
| 262 base::PlatformFileError result = base::PLATFORM_FILE_ERROR_FAILED; | |
| 263 base::RunLoop run_loop; | |
| 264 operation->CreateDirectory(url, | |
| 265 false /* exclusive */, | |
| 266 false /* recursive */, | |
| 267 AssignAndQuitCallback(&run_loop, &result)); | |
| 268 run_loop.Run(); | |
| 269 return result; | |
| 270 } | 186 } |
| 271 | 187 |
| 272 base::PlatformFileError CreateFile(const FileSystemURL& url, size_t size) { | 188 base::PlatformFileError CreateFile(const FileSystemURL& url, size_t size) { |
| 273 base::PlatformFileError result = base::PLATFORM_FILE_ERROR_FAILED; | 189 base::PlatformFileError result = |
| 274 { | 190 AsyncFileTestHelper::CreateFile(file_system_context_, url); |
| 275 FileSystemOperation* operation = | |
| 276 file_system_context_->CreateFileSystemOperation(url, NULL); | |
| 277 EXPECT_TRUE(operation != NULL); | |
| 278 base::RunLoop run_loop; | |
| 279 operation->CreateFile(url, false /* exclusive */, | |
| 280 AssignAndQuitCallback(&run_loop, &result)); | |
| 281 run_loop.Run(); | |
| 282 } | |
| 283 if (result != base::PLATFORM_FILE_OK) | 191 if (result != base::PLATFORM_FILE_OK) |
| 284 return result; | 192 return result; |
| 285 | 193 return AsyncFileTestHelper::TruncateFile(file_system_context_, url, size); |
| 286 { | |
| 287 FileSystemOperation* operation = | |
| 288 file_system_context_->CreateFileSystemOperation(url, NULL); | |
| 289 EXPECT_TRUE(operation != NULL); | |
| 290 base::RunLoop run_loop; | |
| 291 operation->Truncate(url, size, | |
| 292 AssignAndQuitCallback(&run_loop, &result)); | |
| 293 run_loop.Run(); | |
| 294 } | |
| 295 return result; | |
| 296 } | 194 } |
| 297 | 195 |
| 298 bool FileExists(const FileSystemURL& url, int64 expected_size) { | 196 bool FileExists(const FileSystemURL& url, int64 expected_size) { |
| 299 base::PlatformFileError result = base::PLATFORM_FILE_ERROR_FAILED; | 197 return AsyncFileTestHelper::FileExists( |
| 300 base::PlatformFileInfo file_info; | 198 file_system_context_, url, expected_size); |
| 301 base::RunLoop run_loop; | |
| 302 FileSystemOperation* operation = | |
| 303 file_system_context_->CreateFileSystemOperation(url, NULL); | |
| 304 EXPECT_TRUE(operation != NULL); | |
| 305 operation->GetMetadata(url, base::Bind(&GetMetadataCallback, | |
| 306 &run_loop, &result, &file_info)); | |
| 307 run_loop.Run(); | |
| 308 if (result != base::PLATFORM_FILE_OK || file_info.is_directory) | |
| 309 return false; | |
| 310 return expected_size == kDontCheckSize || file_info.size == expected_size; | |
| 311 } | 199 } |
| 312 | 200 |
| 313 bool DirectoryExists(const FileSystemURL& url) { | 201 bool DirectoryExists(const FileSystemURL& url) { |
| 314 base::PlatformFileError result = base::PLATFORM_FILE_ERROR_FAILED; | 202 return AsyncFileTestHelper::DirectoryExists(file_system_context_, url); |
| 315 base::PlatformFileInfo file_info; | |
| 316 base::RunLoop run_loop; | |
| 317 FileSystemOperation* operation = | |
| 318 file_system_context_->CreateFileSystemOperation(url, NULL); | |
| 319 EXPECT_TRUE(operation != NULL); | |
| 320 operation->GetMetadata(url, base::Bind(&GetMetadataCallback, | |
| 321 &run_loop, &result, &file_info)); | |
| 322 run_loop.Run(); | |
| 323 return (result == base::PLATFORM_FILE_OK) && file_info.is_directory; | |
| 324 } | 203 } |
| 325 | 204 |
| 326 GURL origin() const { return origin_; } | |
| 327 FileSystemType src_type() const { return src_type_; } | |
| 328 FileSystemType dest_type() const { return dest_type_; } | |
| 329 | |
| 330 private: | 205 private: |
| 331 void GetUsageAndQuota(FileSystemType type, int64* usage, int64* quota) { | 206 void GetUsageAndQuota(FileSystemType type, int64* usage, int64* quota) { |
| 332 quota::QuotaStatusCode status = quota::kQuotaStatusUnknown; | 207 quota::QuotaStatusCode status = |
| 333 quota_manager_->GetUsageAndQuota( | 208 AsyncFileTestHelper::GetUsageAndQuota( |
| 334 origin_, | 209 quota_manager_, origin_, type, usage, quota); |
| 335 FileSystemTypeToQuotaStorageType(type), | |
| 336 base::Bind(&DidGetUsageAndQuota, &status, usage, quota)); | |
| 337 MessageLoop::current()->RunUntilIdle(); | |
| 338 ASSERT_EQ(quota::kQuotaStatusOk, status); | 210 ASSERT_EQ(quota::kQuotaStatusOk, status); |
| 339 } | 211 } |
| 340 | 212 |
| 213 private: |
| 341 base::ScopedTempDir base_; | 214 base::ScopedTempDir base_; |
| 342 | 215 |
| 343 const GURL origin_; | 216 const GURL origin_; |
| 344 const FileSystemType src_type_; | 217 const FileSystemType src_type_; |
| 345 const FileSystemType dest_type_; | 218 const FileSystemType dest_type_; |
| 346 | 219 |
| 347 MessageLoop message_loop_; | 220 MessageLoop message_loop_; |
| 348 scoped_refptr<FileSystemContext> file_system_context_; | 221 scoped_refptr<FileSystemContext> file_system_context_; |
| 349 scoped_refptr<quota::MockQuotaManagerProxy> quota_manager_proxy_; | 222 scoped_refptr<quota::MockQuotaManagerProxy> quota_manager_proxy_; |
| 350 scoped_refptr<quota::MockQuotaManager> quota_manager_; | 223 scoped_refptr<quota::MockQuotaManager> quota_manager_; |
| 351 | 224 |
| 352 DISALLOW_COPY_AND_ASSIGN(CrossOperationTestHelper); | 225 DISALLOW_COPY_AND_ASSIGN(CrossOperationTestHelper); |
| 353 }; | 226 }; |
| 354 | 227 |
| 355 } // namespace | |
| 356 | |
| 357 TEST(LocalFileSystemCrossOperationTest, CopySingleFile) { | 228 TEST(LocalFileSystemCrossOperationTest, CopySingleFile) { |
| 358 CrossOperationTestHelper helper(GURL("http://foo"), | 229 CrossOperationTestHelper helper(GURL("http://foo"), |
| 359 kFileSystemTypeTemporary, | 230 kFileSystemTypeTemporary, |
| 360 kFileSystemTypePersistent); | 231 kFileSystemTypePersistent); |
| 361 helper.SetUp(); | 232 helper.SetUp(); |
| 362 | 233 |
| 363 FileSystemURL src = helper.SourceURL("a"); | 234 FileSystemURL src = helper.SourceURL("a"); |
| 364 FileSystemURL dest = helper.DestURL("b"); | 235 FileSystemURL dest = helper.DestURL("b"); |
| 365 int64 src_initial_usage = helper.GetSourceUsage(); | 236 int64 src_initial_usage = helper.GetSourceUsage(); |
| 366 int64 dest_initial_usage = helper.GetDestUsage(); | 237 int64 dest_initial_usage = helper.GetDestUsage(); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 395 int64 dest_initial_usage = helper.GetDestUsage(); | 266 int64 dest_initial_usage = helper.GetDestUsage(); |
| 396 | 267 |
| 397 // Set up a source file. | 268 // Set up a source file. |
| 398 ASSERT_EQ(base::PLATFORM_FILE_OK, helper.CreateFile(src, 10)); | 269 ASSERT_EQ(base::PLATFORM_FILE_OK, helper.CreateFile(src, 10)); |
| 399 int64 src_increase = helper.GetSourceUsage() - src_initial_usage; | 270 int64 src_increase = helper.GetSourceUsage() - src_initial_usage; |
| 400 | 271 |
| 401 // Move it. | 272 // Move it. |
| 402 ASSERT_EQ(base::PLATFORM_FILE_OK, helper.Move(src, dest)); | 273 ASSERT_EQ(base::PLATFORM_FILE_OK, helper.Move(src, dest)); |
| 403 | 274 |
| 404 // Verify. | 275 // Verify. |
| 405 ASSERT_FALSE(helper.FileExists(src, kDontCheckSize)); | 276 ASSERT_FALSE(helper.FileExists(src, AsyncFileTestHelper::kDontCheckSize)); |
| 406 ASSERT_TRUE(helper.FileExists(dest, 10)); | 277 ASSERT_TRUE(helper.FileExists(dest, 10)); |
| 407 | 278 |
| 408 int64 src_new_usage = helper.GetSourceUsage(); | 279 int64 src_new_usage = helper.GetSourceUsage(); |
| 409 ASSERT_EQ(src_initial_usage, src_new_usage); | 280 ASSERT_EQ(src_initial_usage, src_new_usage); |
| 410 | 281 |
| 411 int64 dest_increase = helper.GetDestUsage() - dest_initial_usage; | 282 int64 dest_increase = helper.GetDestUsage() - dest_initial_usage; |
| 412 ASSERT_EQ(src_increase, dest_increase); | 283 ASSERT_EQ(src_increase, dest_increase); |
| 413 } | 284 } |
| 414 | 285 |
| 415 TEST(LocalFileSystemCrossOperationTest, CopySingleDirectory) { | 286 TEST(LocalFileSystemCrossOperationTest, CopySingleDirectory) { |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 538 test::kRegularTestCaseSize); | 409 test::kRegularTestCaseSize); |
| 539 | 410 |
| 540 int64 src_new_usage = helper.GetSourceUsage(); | 411 int64 src_new_usage = helper.GetSourceUsage(); |
| 541 ASSERT_EQ(src_initial_usage, src_new_usage); | 412 ASSERT_EQ(src_initial_usage, src_new_usage); |
| 542 | 413 |
| 543 int64 dest_increase = helper.GetDestUsage() - dest_initial_usage; | 414 int64 dest_increase = helper.GetDestUsage() - dest_initial_usage; |
| 544 ASSERT_EQ(src_increase, dest_increase); | 415 ASSERT_EQ(src_increase, dest_increase); |
| 545 } | 416 } |
| 546 | 417 |
| 547 } // namespace fileapi | 418 } // namespace fileapi |
| OLD | NEW |