Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(418)

Side by Side Diff: chrome/browser/media_galleries/fileapi/native_media_file_util_unittest.cc

Issue 12703012: Have media gallery (through native media file util) use MIME sniffer (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressed comments about MIME sniffer. Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 22 matching lines...) Expand all
33 namespace chrome { 33 namespace chrome {
34 34
35 namespace { 35 namespace {
36 36
37 typedef FileSystemOperation::FileEntryList FileEntryList; 37 typedef FileSystemOperation::FileEntryList FileEntryList;
38 38
39 struct FilteringTestCase { 39 struct FilteringTestCase {
40 const base::FilePath::CharType* path; 40 const base::FilePath::CharType* path;
41 bool is_directory; 41 bool is_directory;
42 bool visible; 42 bool visible;
43 bool media_file;
44 const char* content;
43 }; 45 };
44 46
45 const FilteringTestCase kFilteringTestCases[] = { 47 const FilteringTestCase kFilteringTestCases[] = {
46 // Directory should always be visible. 48 // Directory should always be visible.
47 { FPL("hoge"), true, true }, 49 { FPL("hoge"), true, true, false, NULL },
48 { FPL("fuga.jpg"), true, true }, 50 { FPL("fuga.jpg"), true, true, false, NULL },
49 { FPL("piyo.txt"), true, true }, 51 { FPL("piyo.txt"), true, true, false, NULL },
50 { FPL("moga.cod"), true, true }, 52 { FPL("moga.cod"), true, true, false, NULL },
51 53
52 // File should be visible if it's a supported media file. 54 // File should be visible if it's a supported media file.
53 { FPL("foo"), false, false }, // File without extension. 55 // File without extension.
54 { FPL("bar.jpg"), false, true }, // Supported media file. 56 { FPL("foo"), false, false, false, "abc" },
55 { FPL("baz.txt"), false, false }, // Non-media file. 57 // Supported media file.
56 { FPL("foobar.cod"), false, false }, // Unsupported media file. 58 { FPL("bar.jpg"), false, true, true, "\xFF\xD8\xFF" },
59 // Unsupported masquerading file.
60 { FPL("sna.jpg"), false, true, false, "abc" },
61 // Non-media file.
62 { FPL("baz.txt"), false, false, false, "abc" },
63 // Unsupported media file.
64 { FPL("foobar.cod"), false, false, false, "abc" },
57 }; 65 };
58 66
59 void ExpectEqHelper(const std::string& test_name, 67 void ExpectEqHelper(const std::string& test_name,
60 base::PlatformFileError expected, 68 base::PlatformFileError expected,
61 base::PlatformFileError actual) { 69 base::PlatformFileError actual) {
62 EXPECT_EQ(expected, actual) << test_name; 70 EXPECT_EQ(expected, actual) << test_name;
63 } 71 }
64 72
65 void ExpectMetadataEqHelper(const std::string& test_name, 73 void ExpectMetadataEqHelper(const std::string& test_name,
66 base::PlatformFileError expected, 74 base::PlatformFileError expected,
(...skipping 19 matching lines...) Expand all
86 } 94 }
87 95
88 void PopulateDirectoryWithTestCases(const base::FilePath& dir, 96 void PopulateDirectoryWithTestCases(const base::FilePath& dir,
89 const FilteringTestCase* test_cases, 97 const FilteringTestCase* test_cases,
90 size_t n) { 98 size_t n) {
91 for (size_t i = 0; i < n; ++i) { 99 for (size_t i = 0; i < n; ++i) {
92 base::FilePath path = dir.Append(test_cases[i].path); 100 base::FilePath path = dir.Append(test_cases[i].path);
93 if (test_cases[i].is_directory) { 101 if (test_cases[i].is_directory) {
94 ASSERT_TRUE(file_util::CreateDirectory(path)); 102 ASSERT_TRUE(file_util::CreateDirectory(path));
95 } else { 103 } else {
96 bool created = false; 104 ASSERT_TRUE(test_cases[i].content != NULL);
97 ASSERT_EQ(base::PLATFORM_FILE_OK, 105 int len = strlen(test_cases[i].content);
98 fileapi::NativeFileUtil::EnsureFileExists(path, &created)); 106 ASSERT_EQ(len, file_util::WriteFile(path, test_cases[i].content, len));
99 ASSERT_TRUE(created);
100 } 107 }
101 } 108 }
102 } 109 }
103 110
104 } // namespace 111 } // namespace
105 112
106 class NativeMediaFileUtilTest : public testing::Test { 113 class NativeMediaFileUtilTest : public testing::Test {
107 public: 114 public:
108 NativeMediaFileUtilTest() 115 NativeMediaFileUtilTest()
109 : file_util_(NULL) { 116 : file_util_(NULL) {
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
231 kFilteringTestCases, 238 kFilteringTestCases,
232 arraysize(kFilteringTestCases)); 239 arraysize(kFilteringTestCases));
233 240
234 std::set<base::FilePath::StringType> content; 241 std::set<base::FilePath::StringType> content;
235 FileSystemURL url = CreateURL(FPL("")); 242 FileSystemURL url = CreateURL(FPL(""));
236 bool completed = false; 243 bool completed = false;
237 NewOperation(url)->ReadDirectory( 244 NewOperation(url)->ReadDirectory(
238 url, base::Bind(&DidReadDirectory, &content, &completed)); 245 url, base::Bind(&DidReadDirectory, &content, &completed));
239 MessageLoop::current()->RunUntilIdle(); 246 MessageLoop::current()->RunUntilIdle();
240 EXPECT_TRUE(completed); 247 EXPECT_TRUE(completed);
241 EXPECT_EQ(5u, content.size()); 248 EXPECT_EQ(6u, content.size());
242 249
243 for (size_t i = 0; i < arraysize(kFilteringTestCases); ++i) { 250 for (size_t i = 0; i < arraysize(kFilteringTestCases); ++i) {
244 base::FilePath::StringType name = 251 base::FilePath::StringType name =
245 base::FilePath(kFilteringTestCases[i].path).BaseName().value(); 252 base::FilePath(kFilteringTestCases[i].path).BaseName().value();
246 std::set<base::FilePath::StringType>::const_iterator found = 253 std::set<base::FilePath::StringType>::const_iterator found =
247 content.find(name); 254 content.find(name);
248 EXPECT_EQ(kFilteringTestCases[i].visible, found != content.end()); 255 EXPECT_EQ(kFilteringTestCases[i].visible, found != content.end());
249 } 256 }
250 } 257 }
251 258
(...skipping 352 matching lines...) Expand 10 before | Expand all | Expand 10 after
604 // Files do not exists. Touch fails. 611 // Files do not exists. Touch fails.
605 expectation = base::PLATFORM_FILE_ERROR_FAILED; 612 expectation = base::PLATFORM_FILE_ERROR_FAILED;
606 } 613 }
607 operation->TouchFile( 614 operation->TouchFile(
608 url, time, time, base::Bind(&ExpectEqHelper, test_name, expectation)); 615 url, time, time, base::Bind(&ExpectEqHelper, test_name, expectation));
609 MessageLoop::current()->RunUntilIdle(); 616 MessageLoop::current()->RunUntilIdle();
610 } 617 }
611 } 618 }
612 } 619 }
613 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;
vandebo (ex-Chrome) 2013/04/30 21:20:36 nit: initialize error to something other than what
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
614 } // namespace chrome 652 } // namespace chrome
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698