| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 <set> | 5 #include <set> |
| 6 #include <string> | 6 #include <string> |
| 7 | 7 |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/files/scoped_temp_dir.h" | 10 #include "base/files/scoped_temp_dir.h" |
| 11 #include "base/format_macros.h" | 11 #include "base/format_macros.h" |
| 12 #include "base/message_loop.h" | 12 #include "base/message_loop.h" |
| 13 #include "base/stringprintf.h" | 13 #include "base/stringprintf.h" |
| 14 #include "base/time.h" | 14 #include "base/time.h" |
| 15 #include "chrome/browser/media_galleries/fileapi/media_file_system_mount_point_p
rovider.h" | 15 #include "chrome/browser/media_galleries/fileapi/media_file_system_mount_point_p
rovider.h" |
| 16 #include "chrome/browser/media_galleries/fileapi/native_media_file_util.h" | 16 #include "chrome/browser/media_galleries/fileapi/native_media_file_util.h" |
| 17 #include "content/public/test/test_browser_thread.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 18 #include "webkit/browser/fileapi/external_mount_points.h" | 19 #include "webkit/browser/fileapi/external_mount_points.h" |
| 19 #include "webkit/browser/fileapi/file_system_context.h" | 20 #include "webkit/browser/fileapi/file_system_context.h" |
| 20 #include "webkit/browser/fileapi/file_system_mount_point_provider.h" | 21 #include "webkit/browser/fileapi/file_system_mount_point_provider.h" |
| 21 #include "webkit/browser/fileapi/file_system_operation.h" | 22 #include "webkit/browser/fileapi/file_system_operation.h" |
| 22 #include "webkit/browser/fileapi/file_system_task_runners.h" | 23 #include "webkit/browser/fileapi/file_system_task_runners.h" |
| 23 #include "webkit/browser/fileapi/file_system_url.h" | 24 #include "webkit/browser/fileapi/file_system_url.h" |
| 24 #include "webkit/browser/fileapi/isolated_context.h" | 25 #include "webkit/browser/fileapi/isolated_context.h" |
| 25 #include "webkit/browser/fileapi/mock_file_system_options.h" | 26 #include "webkit/browser/fileapi/mock_file_system_options.h" |
| 26 #include "webkit/browser/fileapi/native_file_util.h" | 27 #include "webkit/browser/fileapi/native_file_util.h" |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 107 ASSERT_EQ(len, file_util::WriteFile(path, test_cases[i].content, len)); | 108 ASSERT_EQ(len, file_util::WriteFile(path, test_cases[i].content, len)); |
| 108 } | 109 } |
| 109 } | 110 } |
| 110 } | 111 } |
| 111 | 112 |
| 112 } // namespace | 113 } // namespace |
| 113 | 114 |
| 114 class NativeMediaFileUtilTest : public testing::Test { | 115 class NativeMediaFileUtilTest : public testing::Test { |
| 115 public: | 116 public: |
| 116 NativeMediaFileUtilTest() | 117 NativeMediaFileUtilTest() |
| 117 : file_util_(NULL) { | 118 : io_thread_(content::BrowserThread::IO, &message_loop_) { |
| 118 } | 119 } |
| 119 | 120 |
| 120 virtual void SetUp() { | 121 virtual void SetUp() { |
| 121 ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); | 122 ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); |
| 122 ASSERT_TRUE(file_util::CreateDirectory(root_path())); | 123 ASSERT_TRUE(file_util::CreateDirectory(root_path())); |
| 123 | 124 |
| 124 scoped_refptr<quota::SpecialStoragePolicy> storage_policy = | 125 scoped_refptr<quota::SpecialStoragePolicy> storage_policy = |
| 125 new quota::MockSpecialStoragePolicy(); | 126 new quota::MockSpecialStoragePolicy(); |
| 126 | 127 |
| 127 ScopedVector<fileapi::FileSystemMountPointProvider> additional_providers; | 128 ScopedVector<fileapi::FileSystemMountPointProvider> additional_providers; |
| 128 additional_providers.push_back(new MediaFileSystemMountPointProvider( | 129 additional_providers.push_back(new MediaFileSystemMountPointProvider( |
| 129 data_dir_.path())); | 130 data_dir_.path())); |
| 130 | 131 |
| 131 file_system_context_ = new fileapi::FileSystemContext( | 132 file_system_context_ = new fileapi::FileSystemContext( |
| 132 fileapi::FileSystemTaskRunners::CreateMockTaskRunners(), | 133 fileapi::FileSystemTaskRunners::CreateMockTaskRunners(), |
| 133 fileapi::ExternalMountPoints::CreateRefCounted().get(), | 134 fileapi::ExternalMountPoints::CreateRefCounted().get(), |
| 134 storage_policy.get(), | 135 storage_policy.get(), |
| 135 NULL, | 136 NULL, |
| 136 additional_providers.Pass(), | 137 additional_providers.Pass(), |
| 137 data_dir_.path(), | 138 data_dir_.path(), |
| 138 fileapi::CreateAllowFileAccessOptions()); | 139 fileapi::CreateAllowFileAccessOptions()); |
| 139 | 140 |
| 140 file_util_ = file_system_context_->GetFileUtil( | |
| 141 fileapi::kFileSystemTypeNativeMedia); | |
| 142 | |
| 143 filesystem_id_ = isolated_context()->RegisterFileSystemForPath( | 141 filesystem_id_ = isolated_context()->RegisterFileSystemForPath( |
| 144 fileapi::kFileSystemTypeNativeMedia, root_path(), NULL); | 142 fileapi::kFileSystemTypeNativeMedia, root_path(), NULL); |
| 145 | 143 |
| 146 isolated_context()->AddReference(filesystem_id_); | 144 isolated_context()->AddReference(filesystem_id_); |
| 147 } | 145 } |
| 148 | 146 |
| 149 virtual void TearDown() { | 147 virtual void TearDown() { |
| 150 isolated_context()->RemoveReference(filesystem_id_); | 148 isolated_context()->RemoveReference(filesystem_id_); |
| 151 file_system_context_ = NULL; | 149 file_system_context_ = NULL; |
| 152 } | 150 } |
| (...skipping 18 matching lines...) Expand all Loading... |
| 171 return data_dir_.path().Append(FPL("Media Directory")); | 169 return data_dir_.path().Append(FPL("Media Directory")); |
| 172 } | 170 } |
| 173 | 171 |
| 174 base::FilePath GetVirtualPath( | 172 base::FilePath GetVirtualPath( |
| 175 const base::FilePath::CharType* test_case_path) { | 173 const base::FilePath::CharType* test_case_path) { |
| 176 return base::FilePath::FromUTF8Unsafe(filesystem_id_). | 174 return base::FilePath::FromUTF8Unsafe(filesystem_id_). |
| 177 Append(FPL("Media Directory")). | 175 Append(FPL("Media Directory")). |
| 178 Append(base::FilePath(test_case_path)); | 176 Append(base::FilePath(test_case_path)); |
| 179 } | 177 } |
| 180 | 178 |
| 181 fileapi::FileSystemFileUtil* file_util() { | |
| 182 return file_util_; | |
| 183 } | |
| 184 | |
| 185 GURL origin() { | 179 GURL origin() { |
| 186 return GURL("http://example.com"); | 180 return GURL("http://example.com"); |
| 187 } | 181 } |
| 188 | 182 |
| 189 fileapi::FileSystemType type() { | 183 fileapi::FileSystemType type() { |
| 190 return fileapi::kFileSystemTypeNativeMedia; | 184 return fileapi::kFileSystemTypeNativeMedia; |
| 191 } | 185 } |
| 192 | 186 |
| 193 FileSystemOperation* NewOperation(const FileSystemURL& url) { | 187 FileSystemOperation* NewOperation(const FileSystemURL& url) { |
| 194 return file_system_context_->CreateFileSystemOperation(url, NULL); | 188 return file_system_context_->CreateFileSystemOperation(url, NULL); |
| 195 } | 189 } |
| 196 | 190 |
| 197 private: | 191 private: |
| 198 base::MessageLoop message_loop_; | 192 base::MessageLoop message_loop_; |
| 193 content::TestBrowserThread io_thread_; |
| 199 | 194 |
| 200 base::ScopedTempDir data_dir_; | 195 base::ScopedTempDir data_dir_; |
| 201 scoped_refptr<fileapi::FileSystemContext> file_system_context_; | 196 scoped_refptr<fileapi::FileSystemContext> file_system_context_; |
| 202 | 197 |
| 203 fileapi::FileSystemFileUtil* file_util_; | |
| 204 std::string filesystem_id_; | 198 std::string filesystem_id_; |
| 205 | 199 |
| 206 DISALLOW_COPY_AND_ASSIGN(NativeMediaFileUtilTest); | 200 DISALLOW_COPY_AND_ASSIGN(NativeMediaFileUtilTest); |
| 207 }; | 201 }; |
| 208 | 202 |
| 209 TEST_F(NativeMediaFileUtilTest, DirectoryExistsAndFileExistsFiltering) { | 203 TEST_F(NativeMediaFileUtilTest, DirectoryExistsAndFileExistsFiltering) { |
| 210 PopulateDirectoryWithTestCases(root_path(), | 204 PopulateDirectoryWithTestCases(root_path(), |
| 211 kFilteringTestCases, | 205 kFilteringTestCases, |
| 212 arraysize(kFilteringTestCases)); | 206 arraysize(kFilteringTestCases)); |
| 213 | 207 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 249 | 243 |
| 250 for (size_t i = 0; i < arraysize(kFilteringTestCases); ++i) { | 244 for (size_t i = 0; i < arraysize(kFilteringTestCases); ++i) { |
| 251 base::FilePath::StringType name = | 245 base::FilePath::StringType name = |
| 252 base::FilePath(kFilteringTestCases[i].path).BaseName().value(); | 246 base::FilePath(kFilteringTestCases[i].path).BaseName().value(); |
| 253 std::set<base::FilePath::StringType>::const_iterator found = | 247 std::set<base::FilePath::StringType>::const_iterator found = |
| 254 content.find(name); | 248 content.find(name); |
| 255 EXPECT_EQ(kFilteringTestCases[i].visible, found != content.end()); | 249 EXPECT_EQ(kFilteringTestCases[i].visible, found != content.end()); |
| 256 } | 250 } |
| 257 } | 251 } |
| 258 | 252 |
| 259 TEST_F(NativeMediaFileUtilTest, CreateFileAndCreateDirectoryFiltering) { | 253 TEST_F(NativeMediaFileUtilTest, CreateDirectoryFiltering) { |
| 260 // Run the loop twice. The second loop attempts to create files that are | 254 // Run the loop twice. The second loop attempts to create directories that are |
| 261 // pre-existing. Though the result should be the same. | 255 // pre-existing. Though the result should be the same. |
| 262 for (int loop_count = 0; loop_count < 2; ++loop_count) { | 256 for (int loop_count = 0; loop_count < 2; ++loop_count) { |
| 263 for (size_t i = 0; i < arraysize(kFilteringTestCases); ++i) { | 257 for (size_t i = 0; i < arraysize(kFilteringTestCases); ++i) { |
| 264 FileSystemURL root_url = CreateURL(FPL("")); | 258 FileSystemURL root_url = CreateURL(FPL("")); |
| 265 FileSystemOperation* operation = NewOperation(root_url); | 259 FileSystemOperation* operation = NewOperation(root_url); |
| 266 | 260 |
| 267 FileSystemURL url = CreateURL(kFilteringTestCases[i].path); | 261 FileSystemURL url = CreateURL(kFilteringTestCases[i].path); |
| 268 | 262 |
| 269 std::string test_name = base::StringPrintf( | 263 std::string test_name = base::StringPrintf( |
| 270 "CreateFileAndCreateDirectoryFiltering run %d, test %" PRIuS, | 264 "CreateFileAndCreateDirectoryFiltering run %d, test %" PRIuS, |
| 271 loop_count, i); | 265 loop_count, i); |
| 272 base::PlatformFileError expectation = | 266 base::PlatformFileError expectation = |
| 273 kFilteringTestCases[i].visible ? | 267 kFilteringTestCases[i].visible ? |
| 274 base::PLATFORM_FILE_OK : | 268 base::PLATFORM_FILE_OK : |
| 275 base::PLATFORM_FILE_ERROR_SECURITY; | 269 base::PLATFORM_FILE_ERROR_SECURITY; |
| 276 if (kFilteringTestCases[i].is_directory) { | 270 if (kFilteringTestCases[i].is_directory) { |
| 277 operation->CreateDirectory( | 271 operation->CreateDirectory( |
| 278 url, false, false, | 272 url, false, false, |
| 279 base::Bind(&ExpectEqHelper, test_name, expectation)); | 273 base::Bind(&ExpectEqHelper, test_name, expectation)); |
| 280 } else { | |
| 281 operation->CreateFile( | |
| 282 url, false, base::Bind(&ExpectEqHelper, test_name, expectation)); | |
| 283 } | 274 } |
| 284 base::MessageLoop::current()->RunUntilIdle(); | 275 base::MessageLoop::current()->RunUntilIdle(); |
| 285 } | 276 } |
| 286 } | 277 } |
| 287 } | 278 } |
| 288 | 279 |
| 289 TEST_F(NativeMediaFileUtilTest, CopySourceFiltering) { | 280 TEST_F(NativeMediaFileUtilTest, CopySourceFiltering) { |
| 290 base::FilePath dest_path = root_path().AppendASCII("dest"); | 281 base::FilePath dest_path = root_path().AppendASCII("dest"); |
| 291 FileSystemURL dest_url = CreateURL(FPL("dest")); | 282 FileSystemURL dest_url = CreateURL(FPL("dest")); |
| 292 | 283 |
| (...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 522 operation->GetMetadata(url, | 513 operation->GetMetadata(url, |
| 523 base::Bind(&ExpectMetadataEqHelper, | 514 base::Bind(&ExpectMetadataEqHelper, |
| 524 test_name, | 515 test_name, |
| 525 expectation, | 516 expectation, |
| 526 kFilteringTestCases[i].is_directory)); | 517 kFilteringTestCases[i].is_directory)); |
| 527 base::MessageLoop::current()->RunUntilIdle(); | 518 base::MessageLoop::current()->RunUntilIdle(); |
| 528 } | 519 } |
| 529 } | 520 } |
| 530 } | 521 } |
| 531 | 522 |
| 532 TEST_F(NativeMediaFileUtilTest, RemoveFiltering) { | |
| 533 // Run the loop twice. The first run has no files. The second run does. | |
| 534 for (int loop_count = 0; loop_count < 2; ++loop_count) { | |
| 535 if (loop_count == 1) { | |
| 536 PopulateDirectoryWithTestCases(root_path(), | |
| 537 kFilteringTestCases, | |
| 538 arraysize(kFilteringTestCases)); | |
| 539 } | |
| 540 for (size_t i = 0; i < arraysize(kFilteringTestCases); ++i) { | |
| 541 FileSystemURL root_url = CreateURL(FPL("")); | |
| 542 FileSystemOperation* operation = NewOperation(root_url); | |
| 543 | |
| 544 FileSystemURL url = CreateURL(kFilteringTestCases[i].path); | |
| 545 | |
| 546 std::string test_name = base::StringPrintf( | |
| 547 "RemoveFiltering run %d test %" PRIuS, loop_count, i); | |
| 548 base::PlatformFileError expectation = base::PLATFORM_FILE_OK; | |
| 549 if (loop_count == 0 || !kFilteringTestCases[i].visible) { | |
| 550 // Cannot remove files that do not exist or are not visible. | |
| 551 expectation = base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
| 552 } | |
| 553 operation->Remove( | |
| 554 url, false, base::Bind(&ExpectEqHelper, test_name, expectation)); | |
| 555 base::MessageLoop::current()->RunUntilIdle(); | |
| 556 } | |
| 557 } | |
| 558 } | |
| 559 | |
| 560 TEST_F(NativeMediaFileUtilTest, TruncateFiltering) { | |
| 561 // Run the loop twice. The first run has no files. The second run does. | |
| 562 for (int loop_count = 0; loop_count < 2; ++loop_count) { | |
| 563 if (loop_count == 1) { | |
| 564 PopulateDirectoryWithTestCases(root_path(), | |
| 565 kFilteringTestCases, | |
| 566 arraysize(kFilteringTestCases)); | |
| 567 } | |
| 568 for (size_t i = 0; i < arraysize(kFilteringTestCases); ++i) { | |
| 569 FileSystemURL root_url = CreateURL(FPL("")); | |
| 570 FileSystemOperation* operation = NewOperation(root_url); | |
| 571 | |
| 572 FileSystemURL url = CreateURL(kFilteringTestCases[i].path); | |
| 573 | |
| 574 std::string test_name = base::StringPrintf( | |
| 575 "TruncateFiltering run %d test %" PRIuS, loop_count, i); | |
| 576 base::PlatformFileError expectation = base::PLATFORM_FILE_OK; | |
| 577 if (loop_count == 0 || !kFilteringTestCases[i].visible) { | |
| 578 // Cannot truncate files that do not exist or are not visible. | |
| 579 expectation = base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
| 580 } else if (kFilteringTestCases[i].is_directory) { | |
| 581 // Cannot truncate directories. | |
| 582 expectation = base::PLATFORM_FILE_ERROR_ACCESS_DENIED; | |
| 583 } | |
| 584 operation->Truncate( | |
| 585 url, 0, base::Bind(&ExpectEqHelper, test_name, expectation)); | |
| 586 base::MessageLoop::current()->RunUntilIdle(); | |
| 587 } | |
| 588 } | |
| 589 } | |
| 590 | |
| 591 TEST_F(NativeMediaFileUtilTest, TouchFileFiltering) { | |
| 592 base::Time time = base::Time::Now(); | |
| 593 | |
| 594 // Run the loop twice. The first run has no files. The second run does. | |
| 595 for (int loop_count = 0; loop_count < 2; ++loop_count) { | |
| 596 if (loop_count == 1) { | |
| 597 PopulateDirectoryWithTestCases(root_path(), | |
| 598 kFilteringTestCases, | |
| 599 arraysize(kFilteringTestCases)); | |
| 600 } | |
| 601 for (size_t i = 0; i < arraysize(kFilteringTestCases); ++i) { | |
| 602 FileSystemURL root_url = CreateURL(FPL("")); | |
| 603 FileSystemOperation* operation = NewOperation(root_url); | |
| 604 | |
| 605 FileSystemURL url = CreateURL(kFilteringTestCases[i].path); | |
| 606 | |
| 607 std::string test_name = base::StringPrintf( | |
| 608 "TouchFileFiltering run %d test %" PRIuS, loop_count, i); | |
| 609 base::PlatformFileError expectation = base::PLATFORM_FILE_OK; | |
| 610 if (loop_count == 0 || !kFilteringTestCases[i].visible) { | |
| 611 // Files do not exists. Touch fails. | |
| 612 expectation = base::PLATFORM_FILE_ERROR_FAILED; | |
| 613 } | |
| 614 operation->TouchFile( | |
| 615 url, time, time, base::Bind(&ExpectEqHelper, test_name, expectation)); | |
| 616 base::MessageLoop::current()->RunUntilIdle(); | |
| 617 } | |
| 618 } | |
| 619 } | |
| 620 | |
| 621 void CreateSnapshotCallback(base::PlatformFileError* error, | 523 void CreateSnapshotCallback(base::PlatformFileError* error, |
| 622 base::PlatformFileError result, const base::PlatformFileInfo&, | 524 base::PlatformFileError result, const base::PlatformFileInfo&, |
| 623 const base::FilePath&, | 525 const base::FilePath&, |
| 624 const scoped_refptr<webkit_blob::ShareableFileReference>&) { | 526 const scoped_refptr<webkit_blob::ShareableFileReference>&) { |
| 625 *error = result; | 527 *error = result; |
| 626 } | 528 } |
| 627 | 529 |
| 628 TEST_F(NativeMediaFileUtilTest, CreateSnapshot) { | 530 TEST_F(NativeMediaFileUtilTest, CreateSnapshot) { |
| 629 PopulateDirectoryWithTestCases(root_path(), | 531 PopulateDirectoryWithTestCases(root_path(), |
| 630 kFilteringTestCases, | 532 kFilteringTestCases, |
| (...skipping 13 matching lines...) Expand all Loading... |
| 644 expected_error = base::PLATFORM_FILE_ERROR_SECURITY; | 546 expected_error = base::PLATFORM_FILE_ERROR_SECURITY; |
| 645 error = base::PLATFORM_FILE_ERROR_FAILED; | 547 error = base::PLATFORM_FILE_ERROR_FAILED; |
| 646 operation->CreateSnapshotFile(url, | 548 operation->CreateSnapshotFile(url, |
| 647 base::Bind(CreateSnapshotCallback, &error)); | 549 base::Bind(CreateSnapshotCallback, &error)); |
| 648 base::MessageLoop::current()->RunUntilIdle(); | 550 base::MessageLoop::current()->RunUntilIdle(); |
| 649 ASSERT_EQ(expected_error, error); | 551 ASSERT_EQ(expected_error, error); |
| 650 } | 552 } |
| 651 } | 553 } |
| 652 | 554 |
| 653 } // namespace chrome | 555 } // namespace chrome |
| OLD | NEW |