| 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/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
| 10 #include "base/format_macros.h" | 10 #include "base/format_macros.h" |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 namespace fileapi { | 29 namespace fileapi { |
| 30 | 30 |
| 31 namespace { | 31 namespace { |
| 32 | 32 |
| 33 typedef FileSystemOperation::FileEntryList FileEntryList; | 33 typedef FileSystemOperation::FileEntryList FileEntryList; |
| 34 | 34 |
| 35 struct FilteringTestCase { | 35 struct FilteringTestCase { |
| 36 const base::FilePath::CharType* path; | 36 const base::FilePath::CharType* path; |
| 37 bool is_directory; | 37 bool is_directory; |
| 38 bool visible; | 38 bool visible; |
| 39 bool media_file; |
| 40 const char* content; |
| 39 }; | 41 }; |
| 40 | 42 |
| 41 const FilteringTestCase kFilteringTestCases[] = { | 43 const FilteringTestCase kFilteringTestCases[] = { |
| 42 // Directory should always be visible. | 44 // Directory should always be visible. |
| 43 { FPL("hoge"), true, true }, | 45 { FPL("hoge"), true, true, false, NULL }, |
| 44 { FPL("fuga.jpg"), true, true }, | 46 { FPL("fuga.jpg"), true, true, false, NULL }, |
| 45 { FPL("piyo.txt"), true, true }, | 47 { FPL("piyo.txt"), true, true, false, NULL }, |
| 46 { FPL("moga.cod"), true, true }, | 48 { FPL("moga.cod"), true, true, false, NULL }, |
| 47 | 49 |
| 48 // File should be visible if it's a supported media file. | 50 // File should be visible if it's a supported media file. |
| 49 { FPL("foo"), false, false }, // File without extension. | 51 // File without extension. |
| 50 { FPL("bar.jpg"), false, true }, // Supported media file. | 52 { FPL("foo"), false, false, false, "abc" }, |
| 51 { FPL("baz.txt"), false, false }, // Non-media file. | 53 // Supported media file. |
| 52 { FPL("foobar.cod"), false, false }, // Unsupported media file. | 54 { FPL("bar.jpg"), false, true, true, "\xFF\xD8\xFF" }, |
| 55 // Unsupported masquerading file. |
| 56 { FPL("sna.jpg"), false, true, false, "abc" }, |
| 57 // Non-media file. |
| 58 { FPL("baz.txt"), false, false, false, "abc" }, |
| 59 // Unsupported media file. |
| 60 { FPL("foobar.cod"), false, false, false, "abc" }, |
| 53 }; | 61 }; |
| 54 | 62 |
| 55 void ExpectEqHelper(const std::string& test_name, | 63 void ExpectEqHelper(const std::string& test_name, |
| 56 base::PlatformFileError expected, | 64 base::PlatformFileError expected, |
| 57 base::PlatformFileError actual) { | 65 base::PlatformFileError actual) { |
| 58 EXPECT_EQ(expected, actual) << test_name; | 66 EXPECT_EQ(expected, actual) << test_name; |
| 59 } | 67 } |
| 60 | 68 |
| 61 void ExpectMetadataEqHelper(const std::string& test_name, | 69 void ExpectMetadataEqHelper(const std::string& test_name, |
| 62 base::PlatformFileError expected, | 70 base::PlatformFileError expected, |
| (...skipping 19 matching lines...) Expand all Loading... |
| 82 } | 90 } |
| 83 | 91 |
| 84 void PopulateDirectoryWithTestCases(const base::FilePath& dir, | 92 void PopulateDirectoryWithTestCases(const base::FilePath& dir, |
| 85 const FilteringTestCase* test_cases, | 93 const FilteringTestCase* test_cases, |
| 86 size_t n) { | 94 size_t n) { |
| 87 for (size_t i = 0; i < n; ++i) { | 95 for (size_t i = 0; i < n; ++i) { |
| 88 base::FilePath path = dir.Append(test_cases[i].path); | 96 base::FilePath path = dir.Append(test_cases[i].path); |
| 89 if (test_cases[i].is_directory) { | 97 if (test_cases[i].is_directory) { |
| 90 ASSERT_TRUE(file_util::CreateDirectory(path)); | 98 ASSERT_TRUE(file_util::CreateDirectory(path)); |
| 91 } else { | 99 } else { |
| 92 bool created = false; | 100 ASSERT_TRUE(test_cases[i].content != NULL); |
| 93 ASSERT_EQ(base::PLATFORM_FILE_OK, | 101 int len = strlen(test_cases[i].content); |
| 94 NativeFileUtil::EnsureFileExists(path, &created)); | 102 ASSERT_EQ(len, file_util::WriteFile(path, test_cases[i].content, len)); |
| 95 ASSERT_TRUE(created); | |
| 96 } | 103 } |
| 97 } | 104 } |
| 98 } | 105 } |
| 99 | 106 |
| 100 } // namespace | 107 } // namespace |
| 101 | 108 |
| 102 class NativeMediaFileUtilTest : public testing::Test { | 109 class NativeMediaFileUtilTest : public testing::Test { |
| 103 public: | 110 public: |
| 104 NativeMediaFileUtilTest() | 111 NativeMediaFileUtilTest() |
| 105 : file_util_(NULL) { | 112 : file_util_(NULL) { |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 221 kFilteringTestCases, | 228 kFilteringTestCases, |
| 222 arraysize(kFilteringTestCases)); | 229 arraysize(kFilteringTestCases)); |
| 223 | 230 |
| 224 std::set<base::FilePath::StringType> content; | 231 std::set<base::FilePath::StringType> content; |
| 225 FileSystemURL url = CreateURL(FPL("")); | 232 FileSystemURL url = CreateURL(FPL("")); |
| 226 bool completed = false; | 233 bool completed = false; |
| 227 NewOperation(url)->ReadDirectory( | 234 NewOperation(url)->ReadDirectory( |
| 228 url, base::Bind(&DidReadDirectory, &content, &completed)); | 235 url, base::Bind(&DidReadDirectory, &content, &completed)); |
| 229 MessageLoop::current()->RunUntilIdle(); | 236 MessageLoop::current()->RunUntilIdle(); |
| 230 EXPECT_TRUE(completed); | 237 EXPECT_TRUE(completed); |
| 231 EXPECT_EQ(5u, content.size()); | 238 EXPECT_EQ(6u, content.size()); |
| 232 | 239 |
| 233 for (size_t i = 0; i < arraysize(kFilteringTestCases); ++i) { | 240 for (size_t i = 0; i < arraysize(kFilteringTestCases); ++i) { |
| 234 base::FilePath::StringType name = | 241 base::FilePath::StringType name = |
| 235 base::FilePath(kFilteringTestCases[i].path).BaseName().value(); | 242 base::FilePath(kFilteringTestCases[i].path).BaseName().value(); |
| 236 std::set<base::FilePath::StringType>::const_iterator found = content.find(na
me); | 243 std::set<base::FilePath::StringType>::const_iterator found = content.find(na
me); |
| 237 EXPECT_EQ(kFilteringTestCases[i].visible, found != content.end()); | 244 EXPECT_EQ(kFilteringTestCases[i].visible, found != content.end()); |
| 238 } | 245 } |
| 239 } | 246 } |
| 240 | 247 |
| 241 TEST_F(NativeMediaFileUtilTest, CreateFileAndCreateDirectoryFiltering) { | 248 TEST_F(NativeMediaFileUtilTest, CreateFileAndCreateDirectoryFiltering) { |
| (...skipping 351 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 593 // Files do not exists. Touch fails. | 600 // Files do not exists. Touch fails. |
| 594 expectation = base::PLATFORM_FILE_ERROR_FAILED; | 601 expectation = base::PLATFORM_FILE_ERROR_FAILED; |
| 595 } | 602 } |
| 596 operation->TouchFile( | 603 operation->TouchFile( |
| 597 url, time, time, base::Bind(&ExpectEqHelper, test_name, expectation)); | 604 url, time, time, base::Bind(&ExpectEqHelper, test_name, expectation)); |
| 598 MessageLoop::current()->RunUntilIdle(); | 605 MessageLoop::current()->RunUntilIdle(); |
| 599 } | 606 } |
| 600 } | 607 } |
| 601 } | 608 } |
| 602 | 609 |
| 610 void CreateSnapshotCallback(base::PlatformFileError* error, |
| 611 base::PlatformFileError result, const base::PlatformFileInfo&, |
| 612 const base::FilePath&, |
| 613 const scoped_refptr<webkit_blob::ShareableFileReference>&) { |
| 614 *error = result; |
| 615 } |
| 616 |
| 617 TEST_F(NativeMediaFileUtilTest, CreateSnapshot) { |
| 618 PopulateDirectoryWithTestCases(root_path(), |
| 619 kFilteringTestCases, |
| 620 arraysize(kFilteringTestCases)); |
| 621 for (size_t i = 0; i < arraysize(kFilteringTestCases); ++i) { |
| 622 if (kFilteringTestCases[i].is_directory || |
| 623 !kFilteringTestCases[i].visible) { |
| 624 continue; |
| 625 } |
| 626 FileSystemURL root_url = CreateURL(FPL("")); |
| 627 FileSystemOperation* operation = NewOperation(root_url); |
| 628 FileSystemURL url = CreateURL(kFilteringTestCases[i].path); |
| 629 base::PlatformFileError expected_error, error; |
| 630 if (kFilteringTestCases[i].media_file) |
| 631 expected_error = base::PLATFORM_FILE_OK; |
| 632 else |
| 633 expected_error = base::PLATFORM_FILE_ERROR_SECURITY; |
| 634 operation->CreateSnapshotFile(url, |
| 635 base::Bind(CreateSnapshotCallback, &error)); |
| 636 MessageLoop::current()->RunUntilIdle(); |
| 637 ASSERT_EQ(expected_error, error); |
| 638 } |
| 639 } |
| 640 |
| 603 } // namespace fileapi | 641 } // namespace fileapi |
| OLD | NEW |