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

Side by Side Diff: webkit/fileapi/media/native_media_file_util_unittest.cc

Issue 10825042: Implement media path filter (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 4 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
(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 void ExpectEqHelper(base::PlatformFileError expected,
35 base::PlatformFileError actual) {
36 EXPECT_EQ(expected, actual);
37 }
38
39 void DidReadDirectory(std::set<FilePath::StringType>* content,
40 bool* completed,
41 base::PlatformFileError error,
42 const FileEntryList& file_list,
43 bool has_more) {
44 EXPECT_TRUE(!*completed);
45 *completed = !has_more;
46 for (FileEntryList::const_iterator itr = file_list.begin();
47 itr != file_list.end(); ++itr)
48 EXPECT_TRUE(content->insert(itr->name).second);
49 }
50
51 } // namespace
52
53 class NativeMediaFileUtilTest : public testing::Test {
54 public:
55 NativeMediaFileUtilTest()
56 : file_util_(NULL) {
57 }
58
59 void SetUp() {
60 ASSERT_TRUE(data_dir_.CreateUniqueTempDir());
61
62 scoped_refptr<quota::SpecialStoragePolicy> storage_policy =
63 new quota::MockSpecialStoragePolicy();
64
65 file_system_context_ =
66 new FileSystemContext(
67 base::MessageLoopProxy::current(),
68 base::MessageLoopProxy::current(),
69 storage_policy,
70 NULL,
71 data_dir_.path(),
72 CreateAllowFileAccessOptions());
73
74 file_util_ = file_system_context_->GetFileUtil(kFileSystemTypeNativeMedia);
75
76 filesystem_id_ = isolated_context()->RegisterFileSystemForPath(
77 kFileSystemTypeNativeMedia, root_path(), NULL);
78
79 isolated_context()->AddReference(filesystem_id_);
80
81 virtual_root_path_ =
82 isolated_context()->CreateVirtualRootPath(filesystem_id_);
83 }
84
85 void TearDown() {
86 isolated_context()->RemoveReference(filesystem_id_);
87 file_system_context_ = NULL;
88 }
89
90 protected:
91 FileSystemContext* file_system_context() {
92 return file_system_context_.get();
93 }
94
95 IsolatedContext* isolated_context() {
96 return IsolatedContext::GetInstance();
97 }
98
99 FilePath root_path() {
100 return data_dir_.path().Append(FPL("Media Directory"));
101 }
102
103 FileSystemFileUtil* file_util() {
104 return file_util_;
105 }
106
107 GURL origin() {
108 return GURL("http://example.com");
109 }
110
111 FileSystemType type() {
112 return kFileSystemTypeNativeMedia;
113 }
114
115 FileSystemOperationInterface* NewOperation(const FileSystemURL& url) {
116 return file_system_context_->CreateFileSystemOperation(url);
117 }
118
119 private:
120 MessageLoop message_loop_;
121
122 ScopedTempDir data_dir_;
123 scoped_refptr<FileSystemContext> file_system_context_;
124
125 FileSystemFileUtil* file_util_;
126 std::string filesystem_id_;
127 FilePath virtual_root_path_;
128
129 DISALLOW_COPY_AND_ASSIGN(NativeMediaFileUtilTest);
130 };
131
132 TEST_F(NativeMediaFileUtilTest, FilteringTest) {
kinuko 2012/07/31 01:26:35 For easier diagnosis could we split this test into
tzik 2012/07/31 22:31:23 Done.
133 const FilteringTestCase kTestCases[] = {
134 { FPL("hoge"), true, true },
135 // Directory should always be visible.
136 { FPL("hoge/fuga"), true, true },
137 { FPL("hoge/piyo.jpg"), true, true },
138 { FPL("hoge/moga.txt"), true, true },
139
140 // File should be visible if it's a supported media file.
141 { FPL("hoge/foo"), false, false }, // File without extension.
142 { FPL("hoge/bar.jpg"), false, true }, // Supported media file.
143 { FPL("hoge/baz.txt"), false, false }, // Non-media file.
144 { FPL("hoge/foobar.cod"), false, false }, // Unsupported media file.
145 };
146
147 for (size_t i = 0; i < arraysize(kTestCases); ++i) {
148 FilePath path = root_path().Append(kTestCases[i].path);
149 if (kTestCases[i].is_directory) {
150 ASSERT_TRUE(file_util::CreateDirectory(path));
151 } else {
152 bool created = false;
153 ASSERT_EQ(base::PLATFORM_FILE_OK,
154 NativeFileUtil::EnsureFileExists(path, &created));
155 ASSERT_TRUE(created);
156 }
157 }
158
159 for (size_t i = 0; i < arraysize(kTestCases); ++i) {
160 FilePath path = root_path().Append(kTestCases[i].path);
161 FileSystemURL url(origin(), type(), path);
162 FileSystemOperationInterface* operation = NewOperation(url);
163
164 base::PlatformFileError expectation =
165 kTestCases[i].visible ?
166 base::PLATFORM_FILE_OK :
167 base::PLATFORM_FILE_ERROR_NOT_FOUND;
168
169 if (kTestCases[i].is_directory)
170 operation->DirectoryExists(url, base::Bind(&ExpectEqHelper, expectation));
171 else
172 operation->FileExists(url, base::Bind(&ExpectEqHelper, expectation));
173 MessageLoop::current()->RunAllPending();
174 }
175
176 std::set<FilePath::StringType> content;
177 FileSystemURL url(origin(), type(),
178 root_path().Append(kTestCases[0].path));
179 bool completed = false;
180 NewOperation(url)->ReadDirectory(
181 url, base::Bind(&DidReadDirectory, &content, &completed));
182 MessageLoop::current()->RunAllPending();
183 EXPECT_TRUE(completed);
184 EXPECT_EQ(4u, content.size());
185
186 for (size_t i = 1; i < arraysize(kTestCases); ++i) {
187 FilePath::StringType name = FilePath(kTestCases[i].path).BaseName().value();
188 std::set<FilePath::StringType>::const_iterator found = content.find(name);
189 EXPECT_EQ(kTestCases[i].visible, found != content.end());
190 }
191 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698