Chromium Code Reviews| 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 17 matching lines...) Expand all Loading... | |
| 28 namespace fileapi { | 28 namespace fileapi { |
| 29 | 29 |
| 30 namespace { | 30 namespace { |
| 31 | 31 |
| 32 typedef FileSystemOperation::FileEntryList FileEntryList; | 32 typedef FileSystemOperation::FileEntryList FileEntryList; |
| 33 | 33 |
| 34 struct FilteringTestCase { | 34 struct FilteringTestCase { |
| 35 const base::FilePath::CharType* path; | 35 const base::FilePath::CharType* path; |
| 36 bool is_directory; | 36 bool is_directory; |
| 37 bool visible; | 37 bool visible; |
| 38 bool media_file; | |
| 39 const char* content; | |
| 38 }; | 40 }; |
| 39 | 41 |
| 40 const FilteringTestCase kFilteringTestCases[] = { | 42 const FilteringTestCase kFilteringTestCases[] = { |
| 41 // Directory should always be visible. | 43 // Directory should always be visible. |
| 42 { FPL("hoge"), true, true }, | 44 { FPL("hoge"), true, true }, |
| 43 { FPL("fuga.jpg"), true, true }, | 45 { FPL("fuga.jpg"), true, true }, |
| 44 { FPL("piyo.txt"), true, true }, | 46 { FPL("piyo.txt"), true, true }, |
| 45 { FPL("moga.cod"), true, true }, | 47 { FPL("moga.cod"), true, true }, |
| 46 | 48 |
| 47 // File should be visible if it's a supported media file. | 49 // File should be visible if it's a supported media file. |
| 48 { FPL("foo"), false, false }, // File without extension. | 50 { FPL("foo"), false, false }, // File without extension. |
| 49 { FPL("bar.jpg"), false, true }, // Supported media file. | 51 { FPL("bar.jpg"), false, true, true, "\xFF\xD8\xFF" }, // Supported media fil e. |
| 50 { FPL("baz.txt"), false, false }, // Non-media file. | 52 { FPL("sna.jpg"), false, true, false, "abc" }, // Unsupported masquerading fi le. |
| 51 { FPL("foobar.cod"), false, false }, // Unsupported media file. | 53 { FPL("baz.txt"), false, false, false, "abc" }, // Non-media file. |
| 54 { FPL("foobar.cod"), false, false, false, "abc" }, // Unsupported media file. | |
| 52 }; | 55 }; |
| 53 | 56 |
| 54 void ExpectEqHelper(const std::string& test_name, | 57 void ExpectEqHelper(const std::string& test_name, |
| 55 base::PlatformFileError expected, | 58 base::PlatformFileError expected, |
| 56 base::PlatformFileError actual) { | 59 base::PlatformFileError actual) { |
| 57 EXPECT_EQ(expected, actual) << test_name; | 60 EXPECT_EQ(expected, actual) << test_name; |
| 58 } | 61 } |
| 59 | 62 |
| 60 void ExpectMetadataEqHelper(const std::string& test_name, | 63 void ExpectMetadataEqHelper(const std::string& test_name, |
| 61 base::PlatformFileError expected, | 64 base::PlatformFileError expected, |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 81 } | 84 } |
| 82 | 85 |
| 83 void PopulateDirectoryWithTestCases(const base::FilePath& dir, | 86 void PopulateDirectoryWithTestCases(const base::FilePath& dir, |
| 84 const FilteringTestCase* test_cases, | 87 const FilteringTestCase* test_cases, |
| 85 size_t n) { | 88 size_t n) { |
| 86 for (size_t i = 0; i < n; ++i) { | 89 for (size_t i = 0; i < n; ++i) { |
| 87 base::FilePath path = dir.Append(test_cases[i].path); | 90 base::FilePath path = dir.Append(test_cases[i].path); |
| 88 if (test_cases[i].is_directory) { | 91 if (test_cases[i].is_directory) { |
| 89 ASSERT_TRUE(file_util::CreateDirectory(path)); | 92 ASSERT_TRUE(file_util::CreateDirectory(path)); |
| 90 } else { | 93 } else { |
| 94 base::PlatformFile file_handle; | |
| 91 bool created = false; | 95 bool created = false; |
| 92 ASSERT_EQ(base::PLATFORM_FILE_OK, | 96 ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 93 NativeFileUtil::EnsureFileExists(path, &created)); | 97 NativeFileUtil::CreateOrOpen( |
|
vandebo (ex-Chrome)
2013/04/03 18:54:14
It might be easier to use WriteFile() from base/fi
Kevin Bailey
2013/04/04 16:07:28
I agree, but that would remove a test ("created").
vandebo (ex-Chrome)
2013/04/04 20:24:20
I think it's ok to remove the assertion that we ju
Kevin Bailey
2013/04/05 15:24:35
Is this what you had in mind ?
| |
| 98 path, | |
| 99 base::PLATFORM_FILE_CREATE_ALWAYS | | |
| 100 base::PLATFORM_FILE_WRITE, | |
| 101 &file_handle, &created)); | |
| 102 if (test_cases[i].content) { | |
| 103 int len = strlen(test_cases[i].content); | |
| 104 ASSERT_EQ(len, | |
| 105 base::WritePlatformFile(file_handle, 0, test_cases[i].content, | |
| 106 len)); | |
| 107 } | |
| 108 base::ClosePlatformFile(file_handle); | |
| 109 if (test_cases[i].content) { | |
| 110 } | |
| 94 ASSERT_TRUE(created); | 111 ASSERT_TRUE(created); |
| 95 } | 112 } |
| 96 } | 113 } |
| 97 } | 114 } |
| 98 | 115 |
| 99 } // namespace | 116 } // namespace |
| 100 | 117 |
| 101 class NativeMediaFileUtilTest : public testing::Test { | 118 class NativeMediaFileUtilTest : public testing::Test { |
| 102 public: | 119 public: |
| 103 NativeMediaFileUtilTest() | 120 NativeMediaFileUtilTest() |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 219 kFilteringTestCases, | 236 kFilteringTestCases, |
| 220 arraysize(kFilteringTestCases)); | 237 arraysize(kFilteringTestCases)); |
| 221 | 238 |
| 222 std::set<base::FilePath::StringType> content; | 239 std::set<base::FilePath::StringType> content; |
| 223 FileSystemURL url = CreateURL(FPL("")); | 240 FileSystemURL url = CreateURL(FPL("")); |
| 224 bool completed = false; | 241 bool completed = false; |
| 225 NewOperation(url)->ReadDirectory( | 242 NewOperation(url)->ReadDirectory( |
| 226 url, base::Bind(&DidReadDirectory, &content, &completed)); | 243 url, base::Bind(&DidReadDirectory, &content, &completed)); |
| 227 MessageLoop::current()->RunUntilIdle(); | 244 MessageLoop::current()->RunUntilIdle(); |
| 228 EXPECT_TRUE(completed); | 245 EXPECT_TRUE(completed); |
| 229 EXPECT_EQ(5u, content.size()); | 246 EXPECT_EQ(6u, content.size()); |
| 230 | 247 |
| 231 for (size_t i = 0; i < arraysize(kFilteringTestCases); ++i) { | 248 for (size_t i = 0; i < arraysize(kFilteringTestCases); ++i) { |
| 232 base::FilePath::StringType name = | 249 base::FilePath::StringType name = |
| 233 base::FilePath(kFilteringTestCases[i].path).BaseName().value(); | 250 base::FilePath(kFilteringTestCases[i].path).BaseName().value(); |
| 234 std::set<base::FilePath::StringType>::const_iterator found = content.find(na me); | 251 std::set<base::FilePath::StringType>::const_iterator found = content.find(na me); |
| 235 EXPECT_EQ(kFilteringTestCases[i].visible, found != content.end()); | 252 EXPECT_EQ(kFilteringTestCases[i].visible, found != content.end()); |
| 236 } | 253 } |
| 237 } | 254 } |
| 238 | 255 |
| 239 TEST_F(NativeMediaFileUtilTest, CreateFileAndCreateDirectoryFiltering) { | 256 TEST_F(NativeMediaFileUtilTest, CreateFileAndCreateDirectoryFiltering) { |
| (...skipping 351 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 591 // Files do not exists. Touch fails. | 608 // Files do not exists. Touch fails. |
| 592 expectation = base::PLATFORM_FILE_ERROR_FAILED; | 609 expectation = base::PLATFORM_FILE_ERROR_FAILED; |
| 593 } | 610 } |
| 594 operation->TouchFile( | 611 operation->TouchFile( |
| 595 url, time, time, base::Bind(&ExpectEqHelper, test_name, expectation)); | 612 url, time, time, base::Bind(&ExpectEqHelper, test_name, expectation)); |
| 596 MessageLoop::current()->RunUntilIdle(); | 613 MessageLoop::current()->RunUntilIdle(); |
| 597 } | 614 } |
| 598 } | 615 } |
| 599 } | 616 } |
| 600 | 617 |
| 618 void CreateSnapshotCallback(base::PlatformFileError* error, | |
| 619 base::PlatformFileError result, const base::PlatformFileInfo&, | |
| 620 const base::FilePath&, | |
| 621 const scoped_refptr<webkit_blob::ShareableFileReference>&) { | |
| 622 *error = result; | |
| 623 } | |
| 624 | |
| 625 TEST_F(NativeMediaFileUtilTest, CreateSnapshot) { | |
| 626 PopulateDirectoryWithTestCases(root_path(), | |
| 627 kFilteringTestCases, | |
| 628 arraysize(kFilteringTestCases)); | |
| 629 for (size_t i = 0; i < arraysize(kFilteringTestCases); ++i) { | |
| 630 if (!kFilteringTestCases[i].is_directory && | |
|
vandebo (ex-Chrome)
2013/04/03 18:54:14
nit: reverse the case of the if and use continue.
Kevin Bailey
2013/04/04 16:07:28
Done.
Kevin Bailey
2013/04/04 16:07:28
Done.
| |
| 631 kFilteringTestCases[i].visible) { | |
| 632 FileSystemURL root_url = CreateURL(FPL("")); | |
| 633 FileSystemOperation* operation = NewOperation(root_url); | |
| 634 FileSystemURL url = CreateURL(kFilteringTestCases[i].path); | |
| 635 if (kFilteringTestCases[i].media_file) { | |
|
vandebo (ex-Chrome)
2013/04/03 18:54:14
nit: use an "expected" variable and assign it base
Kevin Bailey
2013/04/04 16:07:28
Done.
| |
| 636 base::PlatformFileError error; | |
| 637 operation->CreateSnapshotFile(url, | |
|
vandebo (ex-Chrome)
2013/04/03 18:54:14
After op->CrateSnapshotFile, you should call Messa
Kevin Bailey
2013/04/04 16:07:28
Done.
| |
| 638 base::Bind(CreateSnapshotCallback, &error)); | |
| 639 ASSERT_EQ(base::PLATFORM_FILE_OK, error); | |
| 640 } else { | |
| 641 base::PlatformFileError error; | |
| 642 operation->CreateSnapshotFile(url, | |
| 643 base::Bind(CreateSnapshotCallback, &error)); | |
| 644 ASSERT_EQ(base::PLATFORM_FILE_ERROR_SECURITY, error); | |
| 645 } | |
| 646 } | |
| 647 } | |
| 648 } | |
| 649 | |
| 601 } // namespace fileapi | 650 } // namespace fileapi |
| OLD | NEW |