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 |
| 112 void TearDown() { |
| 113 isolated_context()->RemoveReference(filesystem_id_); |
| 114 file_system_context_ = NULL; |
| 115 } |
| 116 |
| 117 protected: |
| 118 FileSystemContext* file_system_context() { |
| 119 return file_system_context_.get(); |
| 120 } |
| 121 |
| 122 IsolatedContext* isolated_context() { |
| 123 return IsolatedContext::GetInstance(); |
| 124 } |
| 125 |
| 126 FilePath root_path() { |
| 127 return data_dir_.path().Append(FPL("Media Directory")); |
| 128 } |
| 129 |
| 130 FileSystemFileUtil* file_util() { |
| 131 return file_util_; |
| 132 } |
| 133 |
| 134 GURL origin() { |
| 135 return GURL("http://example.com"); |
| 136 } |
| 137 |
| 138 fileapi::FileSystemType type() { |
| 139 return kFileSystemTypeNativeMedia; |
| 140 } |
| 141 |
| 142 FileSystemOperationInterface* NewOperation(const FileSystemURL& url) { |
| 143 return file_system_context_->CreateFileSystemOperation(url); |
| 144 } |
| 145 |
| 146 private: |
| 147 MessageLoop message_loop_; |
| 148 |
| 149 ScopedTempDir data_dir_; |
| 150 scoped_refptr<FileSystemContext> file_system_context_; |
| 151 |
| 152 FileSystemFileUtil* file_util_; |
| 153 std::string filesystem_id_; |
| 154 |
| 155 DISALLOW_COPY_AND_ASSIGN(NativeMediaFileUtilTest); |
| 156 }; |
| 157 |
| 158 TEST_F(NativeMediaFileUtilTest, DirectoryExistsAndFileExistsFiltering) { |
| 159 PopulateDirectoryWithTestCases(root_path(), |
| 160 kFilteringTestCases, |
| 161 arraysize(kFilteringTestCases)); |
| 162 |
| 163 for (size_t i = 0; i < arraysize(kFilteringTestCases); ++i) { |
| 164 FilePath path = root_path().Append(kFilteringTestCases[i].path); |
| 165 FileSystemURL url(origin(), type(), path); |
| 166 FileSystemOperationInterface* operation = NewOperation(url); |
| 167 |
| 168 base::PlatformFileError expectation = |
| 169 kFilteringTestCases[i].visible ? |
| 170 base::PLATFORM_FILE_OK : |
| 171 base::PLATFORM_FILE_ERROR_NOT_FOUND; |
| 172 |
| 173 if (kFilteringTestCases[i].is_directory) |
| 174 operation->DirectoryExists(url, base::Bind(&ExpectEqHelper, expectation)); |
| 175 else |
| 176 operation->FileExists(url, base::Bind(&ExpectEqHelper, expectation)); |
| 177 MessageLoop::current()->RunAllPending(); |
| 178 } |
| 179 } |
| 180 |
| 181 TEST_F(NativeMediaFileUtilTest, ReadDirectoryFiltering) { |
| 182 PopulateDirectoryWithTestCases(root_path(), |
| 183 kFilteringTestCases, |
| 184 arraysize(kFilteringTestCases)); |
| 185 |
| 186 std::set<FilePath::StringType> content; |
| 187 FileSystemURL url(origin(), type(), root_path()); |
| 188 bool completed = false; |
| 189 NewOperation(url)->ReadDirectory( |
| 190 url, base::Bind(&DidReadDirectory, &content, &completed)); |
| 191 MessageLoop::current()->RunAllPending(); |
| 192 EXPECT_TRUE(completed); |
| 193 EXPECT_EQ(5u, content.size()); |
| 194 |
| 195 for (size_t i = 0; i < arraysize(kFilteringTestCases); ++i) { |
| 196 FilePath::StringType name = |
| 197 FilePath(kFilteringTestCases[i].path).BaseName().value(); |
| 198 std::set<FilePath::StringType>::const_iterator found = content.find(name); |
| 199 EXPECT_EQ(kFilteringTestCases[i].visible, found != content.end()); |
| 200 } |
| 201 } |
OLD | NEW |