OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include <set> | |
6 #include <string> | |
7 | |
8 #include "base/bind.h" | |
9 #include "base/message_loop.h" | |
10 #include "base/scoped_temp_dir.h" | |
11 #include "testing/gtest/include/gtest/gtest.h" | |
12 #include "webkit/fileapi/file_system_context.h" | |
13 #include "webkit/fileapi/file_system_operation_interface.h" | |
14 #include "webkit/fileapi/isolated_context.h" | |
15 #include "webkit/fileapi/media/native_media_file_util.h" | |
16 #include "webkit/fileapi/mock_file_system_options.h" | |
17 #include "webkit/fileapi/native_file_util.h" | |
18 #include "webkit/quota/mock_special_storage_policy.h" | |
19 | |
20 #define FPL(x) FILE_PATH_LITERAL(x) | |
21 | |
22 using namespace fileapi; | |
23 | |
24 namespace { | |
25 | |
26 typedef FileSystemOperationInterface::FileEntryList FileEntryList; | |
27 | |
28 struct FilteringTestCase { | |
29 const FilePath::CharType* path; | |
30 bool is_directory; | |
31 bool visible; | |
32 }; | |
33 | |
34 const FilteringTestCase kFilteringTestCases[] = { | |
35 // Directory should always be visible. | |
36 { FPL("hoge"), true, true }, | |
37 { FPL("fuga.jpg"), true, true }, | |
38 { FPL("piyo.txt"), true, true }, | |
39 { FPL("moga.cod"), true, true }, | |
40 | |
41 // File should be visible if it's a supported media file. | |
42 { FPL("foo"), false, false }, // File without extension. | |
43 { FPL("bar.jpg"), false, true }, // Supported media file. | |
44 { FPL("baz.txt"), false, false }, // Non-media file. | |
45 { FPL("foobar.cod"), false, false }, // Unsupported media file. | |
46 }; | |
47 | |
48 void ExpectEqHelper(base::PlatformFileError expected, | |
49 base::PlatformFileError actual) { | |
50 EXPECT_EQ(expected, actual); | |
51 } | |
52 | |
53 void DidReadDirectory(std::set<FilePath::StringType>* content, | |
54 bool* completed, | |
55 base::PlatformFileError error, | |
56 const FileEntryList& file_list, | |
57 bool has_more) { | |
58 EXPECT_TRUE(!*completed); | |
59 *completed = !has_more; | |
60 for (FileEntryList::const_iterator itr = file_list.begin(); | |
61 itr != file_list.end(); ++itr) | |
62 EXPECT_TRUE(content->insert(itr->name).second); | |
63 } | |
64 | |
65 void PopulateDirectoryWithTestCases(const FilePath& dir, | |
66 const FilteringTestCase* test_cases, | |
67 size_t n) { | |
68 for (size_t i = 0; i < n; ++i) { | |
69 FilePath path = dir.Append(test_cases[i].path); | |
70 if (test_cases[i].is_directory) { | |
71 ASSERT_TRUE(file_util::CreateDirectory(path)); | |
72 } else { | |
73 bool created = false; | |
74 ASSERT_EQ(base::PLATFORM_FILE_OK, | |
75 NativeFileUtil::EnsureFileExists(path, &created)); | |
76 ASSERT_TRUE(created); | |
77 } | |
78 } | |
79 } | |
80 | |
81 } // namespace | |
82 | |
83 class NativeMediaFileUtilTest : public testing::Test { | |
84 public: | |
85 NativeMediaFileUtilTest() | |
86 : file_util_(NULL) { | |
87 } | |
88 | |
89 void SetUp() { | |
90 ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); | |
91 | |
92 scoped_refptr<quota::SpecialStoragePolicy> storage_policy = | |
93 new quota::MockSpecialStoragePolicy(); | |
94 | |
95 file_system_context_ = | |
96 new FileSystemContext( | |
97 base::MessageLoopProxy::current(), | |
98 base::MessageLoopProxy::current(), | |
99 storage_policy, | |
100 NULL, | |
101 data_dir_.path(), | |
102 CreateAllowFileAccessOptions()); | |
103 | |
104 file_util_ = file_system_context_->GetFileUtil(kFileSystemTypeNativeMedia); | |
105 | |
106 filesystem_id_ = isolated_context()->RegisterFileSystemForPath( | |
107 kFileSystemTypeNativeMedia, root_path(), NULL); | |
108 | |
109 isolated_context()->AddReference(filesystem_id_); | |
110 | |
111 virtual_root_path_ = | |
112 isolated_context()->CreateVirtualRootPath(filesystem_id_); | |
kinuko
2012/07/31 23:36:50
this is not used?
tzik
2012/08/01 00:54:05
Done.
| |
113 } | |
114 | |
115 void TearDown() { | |
116 isolated_context()->RemoveReference(filesystem_id_); | |
117 file_system_context_ = NULL; | |
118 } | |
119 | |
120 protected: | |
121 FileSystemContext* file_system_context() { | |
122 return file_system_context_.get(); | |
123 } | |
124 | |
125 IsolatedContext* isolated_context() { | |
126 return IsolatedContext::GetInstance(); | |
127 } | |
128 | |
129 FilePath root_path() { | |
130 return data_dir_.path().Append(FPL("Media Directory")); | |
131 } | |
132 | |
133 FileSystemFileUtil* file_util() { | |
134 return file_util_; | |
135 } | |
136 | |
137 GURL origin() { | |
138 return GURL("http://example.com"); | |
139 } | |
140 | |
141 FileSystemType type() { | |
142 return kFileSystemTypeNativeMedia; | |
143 } | |
144 | |
145 FileSystemOperationInterface* NewOperation(const FileSystemURL& url) { | |
146 return file_system_context_->CreateFileSystemOperation(url); | |
147 } | |
148 | |
149 private: | |
150 MessageLoop message_loop_; | |
151 | |
152 ScopedTempDir data_dir_; | |
153 scoped_refptr<FileSystemContext> file_system_context_; | |
154 | |
155 FileSystemFileUtil* file_util_; | |
156 std::string filesystem_id_; | |
157 FilePath virtual_root_path_; | |
158 | |
159 DISALLOW_COPY_AND_ASSIGN(NativeMediaFileUtilTest); | |
160 }; | |
161 | |
162 TEST_F(NativeMediaFileUtilTest, DirectoryExistsAndFileExistsFiltering) { | |
163 PopulateDirectoryWithTestCases(root_path(), | |
164 kFilteringTestCases, | |
165 arraysize(kFilteringTestCases)); | |
166 | |
167 for (size_t i = 0; i < arraysize(kFilteringTestCases); ++i) { | |
168 FilePath path = root_path().Append(kFilteringTestCases[i].path); | |
169 FileSystemURL url(origin(), type(), path); | |
170 FileSystemOperationInterface* operation = NewOperation(url); | |
171 | |
172 base::PlatformFileError expectation = | |
173 kFilteringTestCases[i].visible ? | |
174 base::PLATFORM_FILE_OK : | |
175 base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
176 | |
177 if (kFilteringTestCases[i].is_directory) | |
178 operation->DirectoryExists(url, base::Bind(&ExpectEqHelper, expectation)); | |
179 else | |
180 operation->FileExists(url, base::Bind(&ExpectEqHelper, expectation)); | |
181 MessageLoop::current()->RunAllPending(); | |
182 } | |
183 } | |
184 | |
185 TEST_F(NativeMediaFileUtilTest, ReadDirectoryFiltering) { | |
186 PopulateDirectoryWithTestCases(root_path(), | |
187 kFilteringTestCases, | |
188 arraysize(kFilteringTestCases)); | |
189 | |
190 std::set<FilePath::StringType> content; | |
191 FileSystemURL url(origin(), type(), root_path()); | |
192 bool completed = false; | |
193 NewOperation(url)->ReadDirectory( | |
194 url, base::Bind(&DidReadDirectory, &content, &completed)); | |
195 MessageLoop::current()->RunAllPending(); | |
196 EXPECT_TRUE(completed); | |
197 EXPECT_EQ(5u, content.size()); | |
198 | |
199 for (size_t i = 0; i < arraysize(kFilteringTestCases); ++i) { | |
200 FilePath::StringType name = | |
201 FilePath(kFilteringTestCases[i].path).BaseName().value(); | |
202 std::set<FilePath::StringType>::const_iterator found = content.find(name); | |
203 EXPECT_EQ(kFilteringTestCases[i].visible, found != content.end()); | |
204 } | |
205 } | |
OLD | NEW |