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

Side by Side Diff: webkit/fileapi/media/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: Unit test code review tweaks Created 7 years, 8 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
« no previous file with comments | « webkit/fileapi/media/native_media_file_util.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 17 matching lines...) Expand all
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, false, NULL },
43 { FPL("fuga.jpg"), true, true }, 45 { FPL("fuga.jpg"), true, true, false, NULL },
44 { FPL("piyo.txt"), true, true }, 46 { FPL("piyo.txt"), true, true, false, NULL },
45 { FPL("moga.cod"), true, true }, 47 { FPL("moga.cod"), true, true, false, NULL },
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, false, "abc" },
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
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 {
91 bool created = false; 99 ASSERT_NE((const char*)NULL, test_cases[i].content);
vandebo (ex-Chrome) 2013/04/05 17:54:52 nit: we don't use C style casts and iirc, the asse
Kevin Bailey 2013/04/05 20:39:51 Right, the cast was to make it happy. Done.
92 ASSERT_EQ(base::PLATFORM_FILE_OK, 100 int len = strlen(test_cases[i].content);
93 NativeFileUtil::EnsureFileExists(path, &created)); 101 ASSERT_EQ(len, file_util::WriteFile(path, test_cases[i].content, len));
94 ASSERT_TRUE(created);
95 } 102 }
96 } 103 }
97 } 104 }
98 105
99 } // namespace 106 } // namespace
100 107
101 class NativeMediaFileUtilTest : public testing::Test { 108 class NativeMediaFileUtilTest : public testing::Test {
102 public: 109 public:
103 NativeMediaFileUtilTest() 110 NativeMediaFileUtilTest()
104 : file_util_(NULL) { 111 : file_util_(NULL) {
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
219 kFilteringTestCases, 226 kFilteringTestCases,
220 arraysize(kFilteringTestCases)); 227 arraysize(kFilteringTestCases));
221 228
222 std::set<base::FilePath::StringType> content; 229 std::set<base::FilePath::StringType> content;
223 FileSystemURL url = CreateURL(FPL("")); 230 FileSystemURL url = CreateURL(FPL(""));
224 bool completed = false; 231 bool completed = false;
225 NewOperation(url)->ReadDirectory( 232 NewOperation(url)->ReadDirectory(
226 url, base::Bind(&DidReadDirectory, &content, &completed)); 233 url, base::Bind(&DidReadDirectory, &content, &completed));
227 MessageLoop::current()->RunUntilIdle(); 234 MessageLoop::current()->RunUntilIdle();
228 EXPECT_TRUE(completed); 235 EXPECT_TRUE(completed);
229 EXPECT_EQ(5u, content.size()); 236 EXPECT_EQ(6u, content.size());
230 237
231 for (size_t i = 0; i < arraysize(kFilteringTestCases); ++i) { 238 for (size_t i = 0; i < arraysize(kFilteringTestCases); ++i) {
232 base::FilePath::StringType name = 239 base::FilePath::StringType name =
233 base::FilePath(kFilteringTestCases[i].path).BaseName().value(); 240 base::FilePath(kFilteringTestCases[i].path).BaseName().value();
234 std::set<base::FilePath::StringType>::const_iterator found = content.find(na me); 241 std::set<base::FilePath::StringType>::const_iterator found = content.find(na me);
235 EXPECT_EQ(kFilteringTestCases[i].visible, found != content.end()); 242 EXPECT_EQ(kFilteringTestCases[i].visible, found != content.end());
236 } 243 }
237 } 244 }
238 245
239 TEST_F(NativeMediaFileUtilTest, CreateFileAndCreateDirectoryFiltering) { 246 TEST_F(NativeMediaFileUtilTest, CreateFileAndCreateDirectoryFiltering) {
(...skipping 351 matching lines...) Expand 10 before | Expand all | Expand 10 after
591 // Files do not exists. Touch fails. 598 // Files do not exists. Touch fails.
592 expectation = base::PLATFORM_FILE_ERROR_FAILED; 599 expectation = base::PLATFORM_FILE_ERROR_FAILED;
593 } 600 }
594 operation->TouchFile( 601 operation->TouchFile(
595 url, time, time, base::Bind(&ExpectEqHelper, test_name, expectation)); 602 url, time, time, base::Bind(&ExpectEqHelper, test_name, expectation));
596 MessageLoop::current()->RunUntilIdle(); 603 MessageLoop::current()->RunUntilIdle();
597 } 604 }
598 } 605 }
599 } 606 }
600 607
608 void CreateSnapshotCallback(base::PlatformFileError* error,
609 base::PlatformFileError result, const base::PlatformFileInfo&,
610 const base::FilePath&,
611 const scoped_refptr<webkit_blob::ShareableFileReference>&) {
612 *error = result;
613 }
614
615 TEST_F(NativeMediaFileUtilTest, CreateSnapshot) {
616 PopulateDirectoryWithTestCases(root_path(),
617 kFilteringTestCases,
618 arraysize(kFilteringTestCases));
619 for (size_t i = 0; i < arraysize(kFilteringTestCases); ++i) {
620 if (kFilteringTestCases[i].is_directory ||
621 !kFilteringTestCases[i].visible) {
622 continue;
623 }
624 FileSystemURL root_url = CreateURL(FPL(""));
625 FileSystemOperation* operation = NewOperation(root_url);
626 FileSystemURL url = CreateURL(kFilteringTestCases[i].path);
627 base::PlatformFileError expected_error, error;
628 if (kFilteringTestCases[i].media_file)
629 expected_error = base::PLATFORM_FILE_OK;
630 else
631 expected_error = base::PLATFORM_FILE_ERROR_SECURITY;
632 operation->CreateSnapshotFile(url,
633 base::Bind(CreateSnapshotCallback, &error));
634 MessageLoop::current()->RunUntilIdle();
635 ASSERT_EQ(expected_error, error);
636 }
637 }
638
601 } // namespace fileapi 639 } // namespace fileapi
OLDNEW
« no previous file with comments | « webkit/fileapi/media/native_media_file_util.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698