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

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

Issue 14247034: Move Media Galleries FileAPI code out of webkit. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@cr-14352004
Patch Set: Formatting fixes. 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
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"
11 #include "base/message_loop.h" 11 #include "base/message_loop.h"
12 #include "base/stringprintf.h" 12 #include "base/stringprintf.h"
13 #include "base/time.h" 13 #include "base/time.h"
14 #include "chrome/browser/media_galleries/fileapi/native_media_file_util.h"
14 #include "testing/gtest/include/gtest/gtest.h" 15 #include "testing/gtest/include/gtest/gtest.h"
15 #include "webkit/fileapi/external_mount_points.h" 16 #include "webkit/fileapi/external_mount_points.h"
16 #include "webkit/fileapi/file_system_context.h" 17 #include "webkit/fileapi/file_system_context.h"
17 #include "webkit/fileapi/file_system_mount_point_provider.h" 18 #include "webkit/fileapi/file_system_mount_point_provider.h"
18 #include "webkit/fileapi/file_system_operation.h" 19 #include "webkit/fileapi/file_system_operation.h"
19 #include "webkit/fileapi/file_system_task_runners.h" 20 #include "webkit/fileapi/file_system_task_runners.h"
20 #include "webkit/fileapi/file_system_url.h" 21 #include "webkit/fileapi/file_system_url.h"
21 #include "webkit/fileapi/isolated_context.h" 22 #include "webkit/fileapi/isolated_context.h"
22 #include "webkit/fileapi/media/native_media_file_util.h"
23 #include "webkit/fileapi/mock_file_system_options.h" 23 #include "webkit/fileapi/mock_file_system_options.h"
24 #include "webkit/fileapi/native_file_util.h" 24 #include "webkit/fileapi/native_file_util.h"
25 #include "webkit/quota/mock_special_storage_policy.h" 25 #include "webkit/quota/mock_special_storage_policy.h"
26 26
27 #define FPL(x) FILE_PATH_LITERAL(x) 27 #define FPL(x) FILE_PATH_LITERAL(x)
28 28
29 namespace fileapi { 29 using fileapi::FileSystemOperation;
30 using fileapi::FileSystemURL;
31
32 namespace chrome {
30 33
31 namespace { 34 namespace {
32 35
33 typedef FileSystemOperation::FileEntryList FileEntryList; 36 typedef FileSystemOperation::FileEntryList FileEntryList;
34 37
35 struct FilteringTestCase { 38 struct FilteringTestCase {
36 const base::FilePath::CharType* path; 39 const base::FilePath::CharType* path;
37 bool is_directory; 40 bool is_directory;
38 bool visible; 41 bool visible;
39 }; 42 };
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
84 void PopulateDirectoryWithTestCases(const base::FilePath& dir, 87 void PopulateDirectoryWithTestCases(const base::FilePath& dir,
85 const FilteringTestCase* test_cases, 88 const FilteringTestCase* test_cases,
86 size_t n) { 89 size_t n) {
87 for (size_t i = 0; i < n; ++i) { 90 for (size_t i = 0; i < n; ++i) {
88 base::FilePath path = dir.Append(test_cases[i].path); 91 base::FilePath path = dir.Append(test_cases[i].path);
89 if (test_cases[i].is_directory) { 92 if (test_cases[i].is_directory) {
90 ASSERT_TRUE(file_util::CreateDirectory(path)); 93 ASSERT_TRUE(file_util::CreateDirectory(path));
91 } else { 94 } else {
92 bool created = false; 95 bool created = false;
93 ASSERT_EQ(base::PLATFORM_FILE_OK, 96 ASSERT_EQ(base::PLATFORM_FILE_OK,
94 NativeFileUtil::EnsureFileExists(path, &created)); 97 fileapi::NativeFileUtil::EnsureFileExists(path, &created));
95 ASSERT_TRUE(created); 98 ASSERT_TRUE(created);
96 } 99 }
97 } 100 }
98 } 101 }
99 102
100 } // namespace 103 } // namespace
101 104
102 class NativeMediaFileUtilTest : public testing::Test { 105 class NativeMediaFileUtilTest : public testing::Test {
103 public: 106 public:
104 NativeMediaFileUtilTest() 107 NativeMediaFileUtilTest()
105 : file_util_(NULL) { 108 : file_util_(NULL) {
106 } 109 }
107 110
108 virtual void SetUp() { 111 virtual void SetUp() {
109 ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); 112 ASSERT_TRUE(data_dir_.CreateUniqueTempDir());
110 ASSERT_TRUE(file_util::CreateDirectory(root_path())); 113 ASSERT_TRUE(file_util::CreateDirectory(root_path()));
111 114
112 scoped_refptr<quota::SpecialStoragePolicy> storage_policy = 115 scoped_refptr<quota::SpecialStoragePolicy> storage_policy =
113 new quota::MockSpecialStoragePolicy(); 116 new quota::MockSpecialStoragePolicy();
114 117
115 file_system_context_ = 118 file_system_context_ =
116 new FileSystemContext( 119 new fileapi::FileSystemContext(
117 FileSystemTaskRunners::CreateMockTaskRunners(), 120 fileapi::FileSystemTaskRunners::CreateMockTaskRunners(),
118 ExternalMountPoints::CreateRefCounted().get(), 121 fileapi::ExternalMountPoints::CreateRefCounted().get(),
119 storage_policy, 122 storage_policy,
120 NULL, 123 NULL,
121 ScopedVector<FileSystemMountPointProvider>(), 124 ScopedVector<fileapi::FileSystemMountPointProvider>(),
122 data_dir_.path(), 125 data_dir_.path(),
123 CreateAllowFileAccessOptions()); 126 fileapi::CreateAllowFileAccessOptions());
124 127
125 file_util_ = file_system_context_->GetFileUtil(kFileSystemTypeNativeMedia); 128 file_util_ = file_system_context_->GetFileUtil(
129 fileapi::kFileSystemTypeNativeMedia);
126 130
127 filesystem_id_ = isolated_context()->RegisterFileSystemForPath( 131 filesystem_id_ = isolated_context()->RegisterFileSystemForPath(
128 kFileSystemTypeNativeMedia, root_path(), NULL); 132 fileapi::kFileSystemTypeNativeMedia, root_path(), NULL);
129 133
130 isolated_context()->AddReference(filesystem_id_); 134 isolated_context()->AddReference(filesystem_id_);
131 } 135 }
132 136
133 virtual void TearDown() { 137 virtual void TearDown() {
134 isolated_context()->RemoveReference(filesystem_id_); 138 isolated_context()->RemoveReference(filesystem_id_);
135 file_system_context_ = NULL; 139 file_system_context_ = NULL;
136 } 140 }
137 141
138 protected: 142 protected:
139 FileSystemContext* file_system_context() { 143 fileapi::FileSystemContext* file_system_context() {
140 return file_system_context_.get(); 144 return file_system_context_.get();
141 } 145 }
142 146
143 FileSystemURL CreateURL(const base::FilePath::CharType* test_case_path) { 147 FileSystemURL CreateURL(const base::FilePath::CharType* test_case_path) {
144 return file_system_context_->CreateCrackedFileSystemURL( 148 return file_system_context_->CreateCrackedFileSystemURL(
145 origin(), 149 origin(),
146 fileapi::kFileSystemTypeIsolated, 150 fileapi::kFileSystemTypeIsolated,
147 GetVirtualPath(test_case_path)); 151 GetVirtualPath(test_case_path));
148 } 152 }
149 153
150 IsolatedContext* isolated_context() { 154 fileapi::IsolatedContext* isolated_context() {
151 return IsolatedContext::GetInstance(); 155 return fileapi::IsolatedContext::GetInstance();
152 } 156 }
153 157
154 base::FilePath root_path() { 158 base::FilePath root_path() {
155 return data_dir_.path().Append(FPL("Media Directory")); 159 return data_dir_.path().Append(FPL("Media Directory"));
156 } 160 }
157 161
158 base::FilePath GetVirtualPath(const base::FilePath::CharType* test_case_path) { 162 base::FilePath GetVirtualPath(
163 const base::FilePath::CharType* test_case_path) {
159 return base::FilePath::FromUTF8Unsafe(filesystem_id_). 164 return base::FilePath::FromUTF8Unsafe(filesystem_id_).
160 Append(FPL("Media Directory")). 165 Append(FPL("Media Directory")).
161 Append(base::FilePath(test_case_path)); 166 Append(base::FilePath(test_case_path));
162 } 167 }
163 168
164 FileSystemFileUtil* file_util() { 169 fileapi::FileSystemFileUtil* file_util() {
165 return file_util_; 170 return file_util_;
166 } 171 }
167 172
168 GURL origin() { 173 GURL origin() {
169 return GURL("http://example.com"); 174 return GURL("http://example.com");
170 } 175 }
171 176
172 fileapi::FileSystemType type() { 177 fileapi::FileSystemType type() {
173 return kFileSystemTypeNativeMedia; 178 return fileapi::kFileSystemTypeNativeMedia;
174 } 179 }
175 180
176 FileSystemOperation* NewOperation(const FileSystemURL& url) { 181 FileSystemOperation* NewOperation(const FileSystemURL& url) {
177 return file_system_context_->CreateFileSystemOperation(url, NULL); 182 return file_system_context_->CreateFileSystemOperation(url, NULL);
178 } 183 }
179 184
180 private: 185 private:
181 MessageLoop message_loop_; 186 MessageLoop message_loop_;
182 187
183 base::ScopedTempDir data_dir_; 188 base::ScopedTempDir data_dir_;
184 scoped_refptr<FileSystemContext> file_system_context_; 189 scoped_refptr<fileapi::FileSystemContext> file_system_context_;
185 190
186 FileSystemFileUtil* file_util_; 191 fileapi::FileSystemFileUtil* file_util_;
187 std::string filesystem_id_; 192 std::string filesystem_id_;
188 193
189 DISALLOW_COPY_AND_ASSIGN(NativeMediaFileUtilTest); 194 DISALLOW_COPY_AND_ASSIGN(NativeMediaFileUtilTest);
190 }; 195 };
191 196
192 TEST_F(NativeMediaFileUtilTest, DirectoryExistsAndFileExistsFiltering) { 197 TEST_F(NativeMediaFileUtilTest, DirectoryExistsAndFileExistsFiltering) {
193 PopulateDirectoryWithTestCases(root_path(), 198 PopulateDirectoryWithTestCases(root_path(),
194 kFilteringTestCases, 199 kFilteringTestCases,
195 arraysize(kFilteringTestCases)); 200 arraysize(kFilteringTestCases));
196 201
(...skipping 29 matching lines...) Expand all
226 bool completed = false; 231 bool completed = false;
227 NewOperation(url)->ReadDirectory( 232 NewOperation(url)->ReadDirectory(
228 url, base::Bind(&DidReadDirectory, &content, &completed)); 233 url, base::Bind(&DidReadDirectory, &content, &completed));
229 MessageLoop::current()->RunUntilIdle(); 234 MessageLoop::current()->RunUntilIdle();
230 EXPECT_TRUE(completed); 235 EXPECT_TRUE(completed);
231 EXPECT_EQ(5u, content.size()); 236 EXPECT_EQ(5u, content.size());
232 237
233 for (size_t i = 0; i < arraysize(kFilteringTestCases); ++i) { 238 for (size_t i = 0; i < arraysize(kFilteringTestCases); ++i) {
234 base::FilePath::StringType name = 239 base::FilePath::StringType name =
235 base::FilePath(kFilteringTestCases[i].path).BaseName().value(); 240 base::FilePath(kFilteringTestCases[i].path).BaseName().value();
236 std::set<base::FilePath::StringType>::const_iterator found = content.find(na me); 241 std::set<base::FilePath::StringType>::const_iterator found =
242 content.find(name);
237 EXPECT_EQ(kFilteringTestCases[i].visible, found != content.end()); 243 EXPECT_EQ(kFilteringTestCases[i].visible, found != content.end());
238 } 244 }
239 } 245 }
240 246
241 TEST_F(NativeMediaFileUtilTest, CreateFileAndCreateDirectoryFiltering) { 247 TEST_F(NativeMediaFileUtilTest, CreateFileAndCreateDirectoryFiltering) {
242 // Run the loop twice. The second loop attempts to create files that are 248 // Run the loop twice. The second loop attempts to create files that are
243 // pre-existing. Though the result should be the same. 249 // pre-existing. Though the result should be the same.
244 for (int loop_count = 0; loop_count < 2; ++loop_count) { 250 for (int loop_count = 0; loop_count < 2; ++loop_count) {
245 for (size_t i = 0; i < arraysize(kFilteringTestCases); ++i) { 251 for (size_t i = 0; i < arraysize(kFilteringTestCases); ++i) {
246 FileSystemURL root_url = CreateURL(FPL("")); 252 FileSystemURL root_url = CreateURL(FPL(""));
(...skipping 346 matching lines...) Expand 10 before | Expand all | Expand 10 after
593 // Files do not exists. Touch fails. 599 // Files do not exists. Touch fails.
594 expectation = base::PLATFORM_FILE_ERROR_FAILED; 600 expectation = base::PLATFORM_FILE_ERROR_FAILED;
595 } 601 }
596 operation->TouchFile( 602 operation->TouchFile(
597 url, time, time, base::Bind(&ExpectEqHelper, test_name, expectation)); 603 url, time, time, base::Bind(&ExpectEqHelper, test_name, expectation));
598 MessageLoop::current()->RunUntilIdle(); 604 MessageLoop::current()->RunUntilIdle();
599 } 605 }
600 } 606 }
601 } 607 }
602 608
603 } // namespace fileapi 609 } // namespace chrome
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698