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. |
vandebo (ex-Chrome)
2013/04/04 20:24:20
Should init media_file and content for all the ent
Kevin Bailey
2013/04/05 15:24:35
I didn't want to clutter it with unnecessary init.
| |
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 // File without extension. |
49 { FPL("bar.jpg"), false, true }, // Supported media file. | 51 { FPL("foo"), false, false }, |
50 { FPL("baz.txt"), false, false }, // Non-media file. | 52 // Supported media file. |
51 { FPL("foobar.cod"), false, false }, // Unsupported media file. | 53 { FPL("bar.jpg"), false, true, true, "\xFF\xD8\xFF" }, |
54 // Unsupported masquerading file. | |
55 { FPL("sna.jpg"), false, true, false, "abc" }, | |
56 // Non-media file. | |
57 { FPL("baz.txt"), false, false, false, "abc" }, | |
58 // Unsupported media file. | |
59 { FPL("foobar.cod"), false, false, false, "abc" }, | |
52 }; | 60 }; |
53 | 61 |
54 void ExpectEqHelper(const std::string& test_name, | 62 void ExpectEqHelper(const std::string& test_name, |
55 base::PlatformFileError expected, | 63 base::PlatformFileError expected, |
56 base::PlatformFileError actual) { | 64 base::PlatformFileError actual) { |
57 EXPECT_EQ(expected, actual) << test_name; | 65 EXPECT_EQ(expected, actual) << test_name; |
58 } | 66 } |
59 | 67 |
60 void ExpectMetadataEqHelper(const std::string& test_name, | 68 void ExpectMetadataEqHelper(const std::string& test_name, |
61 base::PlatformFileError expected, | 69 base::PlatformFileError expected, |
(...skipping 19 matching lines...) Expand all Loading... | |
81 } | 89 } |
82 | 90 |
83 void PopulateDirectoryWithTestCases(const base::FilePath& dir, | 91 void PopulateDirectoryWithTestCases(const base::FilePath& dir, |
84 const FilteringTestCase* test_cases, | 92 const FilteringTestCase* test_cases, |
85 size_t n) { | 93 size_t n) { |
86 for (size_t i = 0; i < n; ++i) { | 94 for (size_t i = 0; i < n; ++i) { |
87 base::FilePath path = dir.Append(test_cases[i].path); | 95 base::FilePath path = dir.Append(test_cases[i].path); |
88 if (test_cases[i].is_directory) { | 96 if (test_cases[i].is_directory) { |
89 ASSERT_TRUE(file_util::CreateDirectory(path)); | 97 ASSERT_TRUE(file_util::CreateDirectory(path)); |
90 } else { | 98 } else { |
99 base::PlatformFile file_handle; | |
91 bool created = false; | 100 bool created = false; |
92 ASSERT_EQ(base::PLATFORM_FILE_OK, | 101 ASSERT_EQ(base::PLATFORM_FILE_OK, |
93 NativeFileUtil::EnsureFileExists(path, &created)); | 102 NativeFileUtil::CreateOrOpen( |
103 path, | |
104 base::PLATFORM_FILE_CREATE_ALWAYS | | |
105 base::PLATFORM_FILE_WRITE, | |
106 &file_handle, &created)); | |
107 if (test_cases[i].content) { | |
108 int len = strlen(test_cases[i].content); | |
109 ASSERT_EQ(len, | |
110 base::WritePlatformFile(file_handle, 0, test_cases[i].content, | |
111 len)); | |
112 } | |
113 base::ClosePlatformFile(file_handle); | |
94 ASSERT_TRUE(created); | 114 ASSERT_TRUE(created); |
95 } | 115 } |
96 } | 116 } |
97 } | 117 } |
98 | 118 |
99 } // namespace | 119 } // namespace |
100 | 120 |
101 class NativeMediaFileUtilTest : public testing::Test { | 121 class NativeMediaFileUtilTest : public testing::Test { |
102 public: | 122 public: |
103 NativeMediaFileUtilTest() | 123 NativeMediaFileUtilTest() |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
219 kFilteringTestCases, | 239 kFilteringTestCases, |
220 arraysize(kFilteringTestCases)); | 240 arraysize(kFilteringTestCases)); |
221 | 241 |
222 std::set<base::FilePath::StringType> content; | 242 std::set<base::FilePath::StringType> content; |
223 FileSystemURL url = CreateURL(FPL("")); | 243 FileSystemURL url = CreateURL(FPL("")); |
224 bool completed = false; | 244 bool completed = false; |
225 NewOperation(url)->ReadDirectory( | 245 NewOperation(url)->ReadDirectory( |
226 url, base::Bind(&DidReadDirectory, &content, &completed)); | 246 url, base::Bind(&DidReadDirectory, &content, &completed)); |
227 MessageLoop::current()->RunUntilIdle(); | 247 MessageLoop::current()->RunUntilIdle(); |
228 EXPECT_TRUE(completed); | 248 EXPECT_TRUE(completed); |
229 EXPECT_EQ(5u, content.size()); | 249 EXPECT_EQ(6u, content.size()); |
230 | 250 |
231 for (size_t i = 0; i < arraysize(kFilteringTestCases); ++i) { | 251 for (size_t i = 0; i < arraysize(kFilteringTestCases); ++i) { |
232 base::FilePath::StringType name = | 252 base::FilePath::StringType name = |
233 base::FilePath(kFilteringTestCases[i].path).BaseName().value(); | 253 base::FilePath(kFilteringTestCases[i].path).BaseName().value(); |
234 std::set<base::FilePath::StringType>::const_iterator found = content.find(na me); | 254 std::set<base::FilePath::StringType>::const_iterator found = content.find(na me); |
235 EXPECT_EQ(kFilteringTestCases[i].visible, found != content.end()); | 255 EXPECT_EQ(kFilteringTestCases[i].visible, found != content.end()); |
236 } | 256 } |
237 } | 257 } |
238 | 258 |
239 TEST_F(NativeMediaFileUtilTest, CreateFileAndCreateDirectoryFiltering) { | 259 TEST_F(NativeMediaFileUtilTest, CreateFileAndCreateDirectoryFiltering) { |
(...skipping 351 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
591 // Files do not exists. Touch fails. | 611 // Files do not exists. Touch fails. |
592 expectation = base::PLATFORM_FILE_ERROR_FAILED; | 612 expectation = base::PLATFORM_FILE_ERROR_FAILED; |
593 } | 613 } |
594 operation->TouchFile( | 614 operation->TouchFile( |
595 url, time, time, base::Bind(&ExpectEqHelper, test_name, expectation)); | 615 url, time, time, base::Bind(&ExpectEqHelper, test_name, expectation)); |
596 MessageLoop::current()->RunUntilIdle(); | 616 MessageLoop::current()->RunUntilIdle(); |
597 } | 617 } |
598 } | 618 } |
599 } | 619 } |
600 | 620 |
621 void CreateSnapshotCallback(base::PlatformFileError* error, | |
622 base::PlatformFileError result, const base::PlatformFileInfo&, | |
623 const base::FilePath&, | |
624 const scoped_refptr<webkit_blob::ShareableFileReference>&) { | |
625 *error = result; | |
626 } | |
627 | |
628 TEST_F(NativeMediaFileUtilTest, CreateSnapshot) { | |
629 PopulateDirectoryWithTestCases(root_path(), | |
630 kFilteringTestCases, | |
631 arraysize(kFilteringTestCases)); | |
632 for (size_t i = 0; i < arraysize(kFilteringTestCases); ++i) { | |
633 if (kFilteringTestCases[i].is_directory || | |
634 !kFilteringTestCases[i].visible) { | |
635 continue; | |
636 } | |
637 FileSystemURL root_url = CreateURL(FPL("")); | |
638 FileSystemOperation* operation = NewOperation(root_url); | |
639 FileSystemURL url = CreateURL(kFilteringTestCases[i].path); | |
640 base::PlatformFileError expected_error, error; | |
641 if (kFilteringTestCases[i].media_file) | |
642 expected_error = base::PLATFORM_FILE_OK; | |
643 else | |
644 expected_error = base::PLATFORM_FILE_ERROR_SECURITY; | |
645 operation->CreateSnapshotFile(url, | |
646 base::Bind(CreateSnapshotCallback, &error)); | |
647 MessageLoop::current()->RunUntilIdle(); | |
648 ASSERT_EQ(expected_error, error); | |
649 } | |
650 } | |
651 | |
601 } // namespace fileapi | 652 } // namespace fileapi |
OLD | NEW |