| 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 = file_system_context_->CreateCrackedFileSystemURL( | 130 FileSystemURL url = file_system_context_->CreateCrackedFileSystemURL( |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 242 EXPECT_TRUE(FileExists(url, test_case_map[relative]->data_file_size)); | 175 EXPECT_TRUE(FileExists(url, test_case_map[relative]->data_file_size)); |
| 243 } | 176 } |
| 244 test_case_map.erase(relative); | 177 test_case_map.erase(relative); |
| 245 } | 178 } |
| 246 } | 179 } |
| 247 EXPECT_TRUE(test_case_map.empty()); | 180 EXPECT_TRUE(test_case_map.empty()); |
| 248 } | 181 } |
| 249 | 182 |
| 250 base::PlatformFileError ReadDirectory(const FileSystemURL& url, | 183 base::PlatformFileError ReadDirectory(const FileSystemURL& url, |
| 251 FileEntryList* entries) { | 184 FileEntryList* entries) { |
| 252 entries->clear(); | 185 return AsyncFileTestHelper::ReadDirectory( |
| 253 base::PlatformFileError result = base::PLATFORM_FILE_ERROR_FAILED; | 186 file_system_context_, url, entries); |
| 254 FileSystemOperation* operation = | |
| 255 file_system_context_->CreateFileSystemOperation(url, NULL); | |
| 256 EXPECT_TRUE(operation != NULL); | |
| 257 base::RunLoop run_loop; | |
| 258 operation->ReadDirectory( | |
| 259 url, base::Bind(&ReadDirectoryCallback, &run_loop, &result, entries)); | |
| 260 run_loop.Run(); | |
| 261 return result; | |
| 262 } | 187 } |
| 263 | 188 |
| 264 base::PlatformFileError CreateDirectory(const FileSystemURL& url) { | 189 base::PlatformFileError CreateDirectory(const FileSystemURL& url) { |
| 265 FileSystemOperation* operation = | 190 return AsyncFileTestHelper::CreateDirectory( |
| 266 file_system_context_->CreateFileSystemOperation(url, NULL); | 191 file_system_context_, url); |
| 267 EXPECT_TRUE(operation != NULL); | |
| 268 base::PlatformFileError result = base::PLATFORM_FILE_ERROR_FAILED; | |
| 269 base::RunLoop run_loop; | |
| 270 operation->CreateDirectory(url, | |
| 271 false /* exclusive */, | |
| 272 false /* recursive */, | |
| 273 AssignAndQuitCallback(&run_loop, &result)); | |
| 274 run_loop.Run(); | |
| 275 return result; | |
| 276 } | 192 } |
| 277 | 193 |
| 278 base::PlatformFileError CreateFile(const FileSystemURL& url, size_t size) { | 194 base::PlatformFileError CreateFile(const FileSystemURL& url, size_t size) { |
| 279 base::PlatformFileError result = base::PLATFORM_FILE_ERROR_FAILED; | 195 base::PlatformFileError result = |
| 280 { | 196 AsyncFileTestHelper::CreateFile(file_system_context_, url); |
| 281 FileSystemOperation* operation = | |
| 282 file_system_context_->CreateFileSystemOperation(url, NULL); | |
| 283 EXPECT_TRUE(operation != NULL); | |
| 284 base::RunLoop run_loop; | |
| 285 operation->CreateFile(url, false /* exclusive */, | |
| 286 AssignAndQuitCallback(&run_loop, &result)); | |
| 287 run_loop.Run(); | |
| 288 } | |
| 289 if (result != base::PLATFORM_FILE_OK) | 197 if (result != base::PLATFORM_FILE_OK) |
| 290 return result; | 198 return result; |
| 291 | 199 return AsyncFileTestHelper::TruncateFile(file_system_context_, url, size); |
| 292 { | |
| 293 FileSystemOperation* operation = | |
| 294 file_system_context_->CreateFileSystemOperation(url, NULL); | |
| 295 EXPECT_TRUE(operation != NULL); | |
| 296 base::RunLoop run_loop; | |
| 297 operation->Truncate(url, size, | |
| 298 AssignAndQuitCallback(&run_loop, &result)); | |
| 299 run_loop.Run(); | |
| 300 } | |
| 301 return result; | |
| 302 } | 200 } |
| 303 | 201 |
| 304 bool FileExists(const FileSystemURL& url, int64 expected_size) { | 202 bool FileExists(const FileSystemURL& url, int64 expected_size) { |
| 305 base::PlatformFileError result = base::PLATFORM_FILE_ERROR_FAILED; | 203 return AsyncFileTestHelper::FileExists( |
| 306 base::PlatformFileInfo file_info; | 204 file_system_context_, url, expected_size); |
| 307 base::RunLoop run_loop; | |
| 308 FileSystemOperation* operation = | |
| 309 file_system_context_->CreateFileSystemOperation(url, NULL); | |
| 310 EXPECT_TRUE(operation != NULL); | |
| 311 operation->GetMetadata(url, base::Bind(&GetMetadataCallback, | |
| 312 &run_loop, &result, &file_info)); | |
| 313 run_loop.Run(); | |
| 314 if (result != base::PLATFORM_FILE_OK || file_info.is_directory) | |
| 315 return false; | |
| 316 return expected_size == kDontCheckSize || file_info.size == expected_size; | |
| 317 } | 205 } |
| 318 | 206 |
| 319 bool DirectoryExists(const FileSystemURL& url) { | 207 bool DirectoryExists(const FileSystemURL& url) { |
| 320 base::PlatformFileError result = base::PLATFORM_FILE_ERROR_FAILED; | 208 return AsyncFileTestHelper::DirectoryExists(file_system_context_, url); |
| 321 base::PlatformFileInfo file_info; | |
| 322 base::RunLoop run_loop; | |
| 323 FileSystemOperation* operation = | |
| 324 file_system_context_->CreateFileSystemOperation(url, NULL); | |
| 325 EXPECT_TRUE(operation != NULL); | |
| 326 operation->GetMetadata(url, base::Bind(&GetMetadataCallback, | |
| 327 &run_loop, &result, &file_info)); | |
| 328 run_loop.Run(); | |
| 329 return (result == base::PLATFORM_FILE_OK) && file_info.is_directory; | |
| 330 } | 209 } |
| 331 | 210 |
| 332 GURL origin() const { return origin_; } | |
| 333 FileSystemType src_type() const { return src_type_; } | |
| 334 FileSystemType dest_type() const { return dest_type_; } | |
| 335 | |
| 336 private: | 211 private: |
| 337 void GetUsageAndQuota(FileSystemType type, int64* usage, int64* quota) { | 212 void GetUsageAndQuota(FileSystemType type, int64* usage, int64* quota) { |
| 338 quota::QuotaStatusCode status = quota::kQuotaStatusUnknown; | 213 quota::QuotaStatusCode status = |
| 339 quota_manager_->GetUsageAndQuota( | 214 AsyncFileTestHelper::GetUsageAndQuota( |
| 340 origin_, | 215 quota_manager_, origin_, type, usage, quota); |
| 341 FileSystemTypeToQuotaStorageType(type), | |
| 342 base::Bind(&DidGetUsageAndQuota, &status, usage, quota)); | |
| 343 MessageLoop::current()->RunUntilIdle(); | |
| 344 ASSERT_EQ(quota::kQuotaStatusOk, status); | 216 ASSERT_EQ(quota::kQuotaStatusOk, status); |
| 345 } | 217 } |
| 346 | 218 |
| 219 private: |
| 347 base::ScopedTempDir base_; | 220 base::ScopedTempDir base_; |
| 348 | 221 |
| 349 const GURL origin_; | 222 const GURL origin_; |
| 350 const FileSystemType src_type_; | 223 const FileSystemType src_type_; |
| 351 const FileSystemType dest_type_; | 224 const FileSystemType dest_type_; |
| 352 | 225 |
| 353 MessageLoop message_loop_; | 226 MessageLoop message_loop_; |
| 354 scoped_refptr<FileSystemContext> file_system_context_; | 227 scoped_refptr<FileSystemContext> file_system_context_; |
| 355 scoped_refptr<quota::MockQuotaManagerProxy> quota_manager_proxy_; | 228 scoped_refptr<quota::MockQuotaManagerProxy> quota_manager_proxy_; |
| 356 scoped_refptr<quota::MockQuotaManager> quota_manager_; | 229 scoped_refptr<quota::MockQuotaManager> quota_manager_; |
| 357 | 230 |
| 358 DISALLOW_COPY_AND_ASSIGN(CrossOperationTestHelper); | 231 DISALLOW_COPY_AND_ASSIGN(CrossOperationTestHelper); |
| 359 }; | 232 }; |
| 360 | 233 |
| 361 } // namespace | |
| 362 | |
| 363 TEST(LocalFileSystemCrossOperationTest, CopySingleFile) { | 234 TEST(LocalFileSystemCrossOperationTest, CopySingleFile) { |
| 364 CrossOperationTestHelper helper(GURL("http://foo"), | 235 CrossOperationTestHelper helper(GURL("http://foo"), |
| 365 kFileSystemTypeTemporary, | 236 kFileSystemTypeTemporary, |
| 366 kFileSystemTypePersistent); | 237 kFileSystemTypePersistent); |
| 367 helper.SetUp(); | 238 helper.SetUp(); |
| 368 | 239 |
| 369 FileSystemURL src = helper.SourceURL("a"); | 240 FileSystemURL src = helper.SourceURL("a"); |
| 370 FileSystemURL dest = helper.DestURL("b"); | 241 FileSystemURL dest = helper.DestURL("b"); |
| 371 int64 src_initial_usage = helper.GetSourceUsage(); | 242 int64 src_initial_usage = helper.GetSourceUsage(); |
| 372 int64 dest_initial_usage = helper.GetDestUsage(); | 243 int64 dest_initial_usage = helper.GetDestUsage(); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 401 int64 dest_initial_usage = helper.GetDestUsage(); | 272 int64 dest_initial_usage = helper.GetDestUsage(); |
| 402 | 273 |
| 403 // Set up a source file. | 274 // Set up a source file. |
| 404 ASSERT_EQ(base::PLATFORM_FILE_OK, helper.CreateFile(src, 10)); | 275 ASSERT_EQ(base::PLATFORM_FILE_OK, helper.CreateFile(src, 10)); |
| 405 int64 src_increase = helper.GetSourceUsage() - src_initial_usage; | 276 int64 src_increase = helper.GetSourceUsage() - src_initial_usage; |
| 406 | 277 |
| 407 // Move it. | 278 // Move it. |
| 408 ASSERT_EQ(base::PLATFORM_FILE_OK, helper.Move(src, dest)); | 279 ASSERT_EQ(base::PLATFORM_FILE_OK, helper.Move(src, dest)); |
| 409 | 280 |
| 410 // Verify. | 281 // Verify. |
| 411 ASSERT_FALSE(helper.FileExists(src, kDontCheckSize)); | 282 ASSERT_FALSE(helper.FileExists(src, AsyncFileTestHelper::kDontCheckSize)); |
| 412 ASSERT_TRUE(helper.FileExists(dest, 10)); | 283 ASSERT_TRUE(helper.FileExists(dest, 10)); |
| 413 | 284 |
| 414 int64 src_new_usage = helper.GetSourceUsage(); | 285 int64 src_new_usage = helper.GetSourceUsage(); |
| 415 ASSERT_EQ(src_initial_usage, src_new_usage); | 286 ASSERT_EQ(src_initial_usage, src_new_usage); |
| 416 | 287 |
| 417 int64 dest_increase = helper.GetDestUsage() - dest_initial_usage; | 288 int64 dest_increase = helper.GetDestUsage() - dest_initial_usage; |
| 418 ASSERT_EQ(src_increase, dest_increase); | 289 ASSERT_EQ(src_increase, dest_increase); |
| 419 } | 290 } |
| 420 | 291 |
| 421 TEST(LocalFileSystemCrossOperationTest, CopySingleDirectory) { | 292 TEST(LocalFileSystemCrossOperationTest, CopySingleDirectory) { |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 544 test::kRegularTestCaseSize); | 415 test::kRegularTestCaseSize); |
| 545 | 416 |
| 546 int64 src_new_usage = helper.GetSourceUsage(); | 417 int64 src_new_usage = helper.GetSourceUsage(); |
| 547 ASSERT_EQ(src_initial_usage, src_new_usage); | 418 ASSERT_EQ(src_initial_usage, src_new_usage); |
| 548 | 419 |
| 549 int64 dest_increase = helper.GetDestUsage() - dest_initial_usage; | 420 int64 dest_increase = helper.GetDestUsage() - dest_initial_usage; |
| 550 ASSERT_EQ(src_increase, dest_increase); | 421 ASSERT_EQ(src_increase, dest_increase); |
| 551 } | 422 } |
| 552 | 423 |
| 553 } // namespace fileapi | 424 } // namespace fileapi |
| OLD | NEW |