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

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

Powered by Google App Engine
This is Rietveld 408576698