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

Side by Side Diff: content/browser/fileapi/dragged_file_util_unittest.cc

Issue 442383002: Move storage-related files from webkit/ to new top-level directory storage/ (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 6 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
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 <map> 5 #include <map>
6 #include <queue> 6 #include <queue>
7 #include <set> 7 #include <set>
8 #include <string> 8 #include <string>
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/file_util.h" 11 #include "base/file_util.h"
12 #include "base/files/file_enumerator.h" 12 #include "base/files/file_enumerator.h"
13 #include "base/files/scoped_temp_dir.h" 13 #include "base/files/scoped_temp_dir.h"
14 #include "base/logging.h" 14 #include "base/logging.h"
15 #include "base/message_loop/message_loop.h" 15 #include "base/message_loop/message_loop.h"
16 #include "base/message_loop/message_loop_proxy.h" 16 #include "base/message_loop/message_loop_proxy.h"
17 #include "base/time/time.h" 17 #include "base/time/time.h"
18 #include "content/public/test/async_file_test_helper.h" 18 #include "content/public/test/async_file_test_helper.h"
19 #include "content/public/test/test_file_system_context.h" 19 #include "content/public/test/test_file_system_context.h"
20 #include "content/test/fileapi_test_file_set.h" 20 #include "content/test/fileapi_test_file_set.h"
21 #include "testing/gtest/include/gtest/gtest.h" 21 #include "testing/gtest/include/gtest/gtest.h"
22 #include "webkit/browser/fileapi/dragged_file_util.h" 22 #include "storage/browser/fileapi/dragged_file_util.h"
23 #include "webkit/browser/fileapi/file_system_context.h" 23 #include "storage/browser/fileapi/file_system_context.h"
24 #include "webkit/browser/fileapi/file_system_operation_context.h" 24 #include "storage/browser/fileapi/file_system_operation_context.h"
25 #include "webkit/browser/fileapi/isolated_context.h" 25 #include "storage/browser/fileapi/isolated_context.h"
26 #include "webkit/browser/fileapi/local_file_util.h" 26 #include "storage/browser/fileapi/local_file_util.h"
27 #include "webkit/browser/fileapi/native_file_util.h" 27 #include "storage/browser/fileapi/native_file_util.h"
28 28
29 using content::AsyncFileTestHelper; 29 using content::AsyncFileTestHelper;
30 using fileapi::FileSystemContext; 30 using storage::FileSystemContext;
31 using fileapi::FileSystemOperationContext; 31 using storage::FileSystemOperationContext;
32 using fileapi::FileSystemType; 32 using storage::FileSystemType;
33 using fileapi::FileSystemURL; 33 using storage::FileSystemURL;
34 34
35 namespace content { 35 namespace content {
36 36
37 namespace { 37 namespace {
38 38
39 typedef AsyncFileTestHelper::FileEntryList FileEntryList; 39 typedef AsyncFileTestHelper::FileEntryList FileEntryList;
40 40
41 // Used in DraggedFileUtilTest::SimulateDropFiles(). 41 // Used in DraggedFileUtilTest::SimulateDropFiles().
42 // Random root paths in which we create each file/directory of the 42 // Random root paths in which we create each file/directory of the
43 // RegularTestCases (so that we can simulate a drop with files/directories 43 // RegularTestCases (so that we can simulate a drop with files/directories
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
93 93
94 } // namespace 94 } // namespace
95 95
96 class DraggedFileUtilTest : public testing::Test { 96 class DraggedFileUtilTest : public testing::Test {
97 public: 97 public:
98 DraggedFileUtilTest() {} 98 DraggedFileUtilTest() {}
99 99
100 virtual void SetUp() { 100 virtual void SetUp() {
101 ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); 101 ASSERT_TRUE(data_dir_.CreateUniqueTempDir());
102 ASSERT_TRUE(partition_dir_.CreateUniqueTempDir()); 102 ASSERT_TRUE(partition_dir_.CreateUniqueTempDir());
103 file_util_.reset(new fileapi::DraggedFileUtil()); 103 file_util_.reset(new storage::DraggedFileUtil());
104 104
105 // Register the files/directories of RegularTestCases (with random 105 // Register the files/directories of RegularTestCases (with random
106 // root paths) as dropped files. 106 // root paths) as dropped files.
107 SimulateDropFiles(); 107 SimulateDropFiles();
108 108
109 file_system_context_ = CreateFileSystemContextForTesting( 109 file_system_context_ = CreateFileSystemContextForTesting(
110 NULL /* quota_manager */, 110 NULL /* quota_manager */,
111 partition_dir_.path()); 111 partition_dir_.path());
112 112
113 isolated_context()->AddReference(filesystem_id_); 113 isolated_context()->AddReference(filesystem_id_);
114 } 114 }
115 115
116 virtual void TearDown() { 116 virtual void TearDown() {
117 isolated_context()->RemoveReference(filesystem_id_); 117 isolated_context()->RemoveReference(filesystem_id_);
118 } 118 }
119 119
120 protected: 120 protected:
121 fileapi::IsolatedContext* isolated_context() const { 121 storage::IsolatedContext* isolated_context() const {
122 return fileapi::IsolatedContext::GetInstance(); 122 return storage::IsolatedContext::GetInstance();
123 } 123 }
124 const base::FilePath& root_path() const { 124 const base::FilePath& root_path() const {
125 return data_dir_.path(); 125 return data_dir_.path();
126 } 126 }
127 FileSystemContext* file_system_context() const { 127 FileSystemContext* file_system_context() const {
128 return file_system_context_.get(); 128 return file_system_context_.get();
129 } 129 }
130 fileapi::FileSystemFileUtil* file_util() const { return file_util_.get(); } 130 storage::FileSystemFileUtil* file_util() const { return file_util_.get(); }
131 std::string filesystem_id() const { return filesystem_id_; } 131 std::string filesystem_id() const { return filesystem_id_; }
132 132
133 base::FilePath GetTestCasePlatformPath( 133 base::FilePath GetTestCasePlatformPath(
134 const base::FilePath::StringType& path) { 134 const base::FilePath::StringType& path) {
135 return toplevel_root_map_[GetTopLevelPath(base::FilePath(path))] 135 return toplevel_root_map_[GetTopLevelPath(base::FilePath(path))]
136 .Append(path).NormalizePathSeparators(); 136 .Append(path).NormalizePathSeparators();
137 } 137 }
138 138
139 base::FilePath GetTestCaseLocalPath(const base::FilePath& path) { 139 base::FilePath GetTestCaseLocalPath(const base::FilePath& path) {
140 base::FilePath relative; 140 base::FilePath relative;
141 if (data_dir_.path().AppendRelativePath(path, &relative)) 141 if (data_dir_.path().AppendRelativePath(path, &relative))
142 return relative; 142 return relative;
143 return path; 143 return path;
144 } 144 }
145 145
146 FileSystemURL GetFileSystemURL(const base::FilePath& path) const { 146 FileSystemURL GetFileSystemURL(const base::FilePath& path) const {
147 base::FilePath virtual_path = isolated_context()->CreateVirtualRootPath( 147 base::FilePath virtual_path = isolated_context()->CreateVirtualRootPath(
148 filesystem_id()).Append(path); 148 filesystem_id()).Append(path);
149 return file_system_context_->CreateCrackedFileSystemURL( 149 return file_system_context_->CreateCrackedFileSystemURL(
150 GURL("http://example.com"), 150 GURL("http://example.com"),
151 fileapi::kFileSystemTypeIsolated, 151 storage::kFileSystemTypeIsolated,
152 virtual_path); 152 virtual_path);
153 } 153 }
154 154
155 FileSystemURL GetOtherFileSystemURL(const base::FilePath& path) const { 155 FileSystemURL GetOtherFileSystemURL(const base::FilePath& path) const {
156 return file_system_context()->CreateCrackedFileSystemURL( 156 return file_system_context()->CreateCrackedFileSystemURL(
157 GURL("http://example.com"), 157 GURL("http://example.com"),
158 fileapi::kFileSystemTypeTemporary, 158 storage::kFileSystemTypeTemporary,
159 base::FilePath().AppendASCII("dest").Append(path)); 159 base::FilePath().AppendASCII("dest").Append(path));
160 } 160 }
161 161
162 void VerifyFilesHaveSameContent(const FileSystemURL& url1, 162 void VerifyFilesHaveSameContent(const FileSystemURL& url1,
163 const FileSystemURL& url2) { 163 const FileSystemURL& url2) {
164 // Get the file info and the platform path for url1. 164 // Get the file info and the platform path for url1.
165 base::File::Info info1; 165 base::File::Info info1;
166 ASSERT_EQ(base::File::FILE_OK, 166 ASSERT_EQ(base::File::FILE_OK,
167 AsyncFileTestHelper::GetMetadata( 167 AsyncFileTestHelper::GetMetadata(
168 file_system_context(), url1, &info1)); 168 file_system_context(), url1, &info1));
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
240 IsDirectoryEmpty(file_system_context(), url2)); 240 IsDirectoryEmpty(file_system_context(), url2));
241 continue; 241 continue;
242 } 242 }
243 base::FilePath relative = GetRelativeVirtualPath(root2, url2); 243 base::FilePath relative = GetRelativeVirtualPath(root2, url2);
244 EXPECT_TRUE(file_set1.find(relative) != file_set1.end()); 244 EXPECT_TRUE(file_set1.find(relative) != file_set1.end());
245 VerifyFilesHaveSameContent(url1, url2); 245 VerifyFilesHaveSameContent(url1, url2);
246 } 246 }
247 } 247 }
248 } 248 }
249 249
250 scoped_ptr<fileapi::FileSystemOperationContext> GetOperationContext() { 250 scoped_ptr<storage::FileSystemOperationContext> GetOperationContext() {
251 return make_scoped_ptr( 251 return make_scoped_ptr(new storage::FileSystemOperationContext(
252 new fileapi::FileSystemOperationContext(file_system_context())).Pass(); 252 file_system_context())).Pass();
253 } 253 }
254 254
255 255
256 private: 256 private:
257 void SimulateDropFiles() { 257 void SimulateDropFiles() {
258 size_t root_path_index = 0; 258 size_t root_path_index = 0;
259 259
260 fileapi::IsolatedContext::FileInfoSet toplevels; 260 storage::IsolatedContext::FileInfoSet toplevels;
261 for (size_t i = 0; i < kRegularFileSystemTestCaseSize; ++i) { 261 for (size_t i = 0; i < kRegularFileSystemTestCaseSize; ++i) {
262 const FileSystemTestCaseRecord& test_case = 262 const FileSystemTestCaseRecord& test_case =
263 kRegularFileSystemTestCases[i]; 263 kRegularFileSystemTestCases[i];
264 base::FilePath path(test_case.path); 264 base::FilePath path(test_case.path);
265 base::FilePath toplevel = GetTopLevelPath(path); 265 base::FilePath toplevel = GetTopLevelPath(path);
266 266
267 // We create the test case files under one of the kRootPaths 267 // We create the test case files under one of the kRootPaths
268 // to simulate a drop with multiple directories. 268 // to simulate a drop with multiple directories.
269 if (toplevel_root_map_.find(toplevel) == toplevel_root_map_.end()) { 269 if (toplevel_root_map_.find(toplevel) == toplevel_root_map_.end()) {
270 base::FilePath root = root_path().Append( 270 base::FilePath root = root_path().Append(
271 kRootPaths[(root_path_index++) % arraysize(kRootPaths)]); 271 kRootPaths[(root_path_index++) % arraysize(kRootPaths)]);
272 toplevel_root_map_[toplevel] = root; 272 toplevel_root_map_[toplevel] = root;
273 toplevels.AddPath(root.Append(path), NULL); 273 toplevels.AddPath(root.Append(path), NULL);
274 } 274 }
275 275
276 SetUpOneFileSystemTestCase(toplevel_root_map_[toplevel], test_case); 276 SetUpOneFileSystemTestCase(toplevel_root_map_[toplevel], test_case);
277 } 277 }
278 278
279 // Register the toplevel entries. 279 // Register the toplevel entries.
280 filesystem_id_ = isolated_context()->RegisterDraggedFileSystem(toplevels); 280 filesystem_id_ = isolated_context()->RegisterDraggedFileSystem(toplevels);
281 } 281 }
282 282
283 base::ScopedTempDir data_dir_; 283 base::ScopedTempDir data_dir_;
284 base::ScopedTempDir partition_dir_; 284 base::ScopedTempDir partition_dir_;
285 base::MessageLoopForIO message_loop_; 285 base::MessageLoopForIO message_loop_;
286 std::string filesystem_id_; 286 std::string filesystem_id_;
287 scoped_refptr<FileSystemContext> file_system_context_; 287 scoped_refptr<FileSystemContext> file_system_context_;
288 std::map<base::FilePath, base::FilePath> toplevel_root_map_; 288 std::map<base::FilePath, base::FilePath> toplevel_root_map_;
289 scoped_ptr<fileapi::DraggedFileUtil> file_util_; 289 scoped_ptr<storage::DraggedFileUtil> file_util_;
290 DISALLOW_COPY_AND_ASSIGN(DraggedFileUtilTest); 290 DISALLOW_COPY_AND_ASSIGN(DraggedFileUtilTest);
291 }; 291 };
292 292
293 TEST_F(DraggedFileUtilTest, BasicTest) { 293 TEST_F(DraggedFileUtilTest, BasicTest) {
294 for (size_t i = 0; i < kRegularFileSystemTestCaseSize; ++i) { 294 for (size_t i = 0; i < kRegularFileSystemTestCaseSize; ++i) {
295 SCOPED_TRACE(testing::Message() << "Testing RegularTestCases " << i); 295 SCOPED_TRACE(testing::Message() << "Testing RegularTestCases " << i);
296 const FileSystemTestCaseRecord& test_case = 296 const FileSystemTestCaseRecord& test_case =
297 kRegularFileSystemTestCases[i]; 297 kRegularFileSystemTestCases[i];
298 298
299 FileSystemURL url = GetFileSystemURL(base::FilePath(test_case.path)); 299 FileSystemURL url = GetFileSystemURL(base::FilePath(test_case.path));
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
354 for (size_t i = 0; i < kRegularFileSystemTestCaseSize; ++i) { 354 for (size_t i = 0; i < kRegularFileSystemTestCaseSize; ++i) {
355 const FileSystemTestCaseRecord& test_case = 355 const FileSystemTestCaseRecord& test_case =
356 kRegularFileSystemTestCases[i]; 356 kRegularFileSystemTestCases[i];
357 if (!test_case.is_directory) 357 if (!test_case.is_directory)
358 continue; 358 continue;
359 359
360 SCOPED_TRACE(testing::Message() << "Testing RegularTestCases " << i 360 SCOPED_TRACE(testing::Message() << "Testing RegularTestCases " << i
361 << ": " << test_case.path); 361 << ": " << test_case.path);
362 362
363 // Read entries in the directory to construct the expected results map. 363 // Read entries in the directory to construct the expected results map.
364 typedef std::map<base::FilePath::StringType, fileapi::DirectoryEntry> 364 typedef std::map<base::FilePath::StringType, storage::DirectoryEntry>
365 EntryMap; 365 EntryMap;
366 EntryMap expected_entry_map; 366 EntryMap expected_entry_map;
367 367
368 base::FilePath dir_path = GetTestCasePlatformPath(test_case.path); 368 base::FilePath dir_path = GetTestCasePlatformPath(test_case.path);
369 base::FileEnumerator file_enum( 369 base::FileEnumerator file_enum(
370 dir_path, false /* not recursive */, 370 dir_path, false /* not recursive */,
371 base::FileEnumerator::FILES | base::FileEnumerator::DIRECTORIES); 371 base::FileEnumerator::FILES | base::FileEnumerator::DIRECTORIES);
372 base::FilePath current; 372 base::FilePath current;
373 while (!(current = file_enum.Next()).empty()) { 373 while (!(current = file_enum.Next()).empty()) {
374 base::FileEnumerator::FileInfo file_info = file_enum.GetInfo(); 374 base::FileEnumerator::FileInfo file_info = file_enum.GetInfo();
375 fileapi::DirectoryEntry entry; 375 storage::DirectoryEntry entry;
376 entry.is_directory = file_info.IsDirectory(); 376 entry.is_directory = file_info.IsDirectory();
377 entry.name = current.BaseName().value(); 377 entry.name = current.BaseName().value();
378 entry.size = file_info.GetSize(); 378 entry.size = file_info.GetSize();
379 entry.last_modified_time = file_info.GetLastModifiedTime(); 379 entry.last_modified_time = file_info.GetLastModifiedTime();
380 expected_entry_map[entry.name] = entry; 380 expected_entry_map[entry.name] = entry;
381 381
382 #if defined(OS_POSIX) 382 #if defined(OS_POSIX)
383 // Creates a symlink for each file/directory. 383 // Creates a symlink for each file/directory.
384 // They should be ignored by ReadDirectory, so we don't add them 384 // They should be ignored by ReadDirectory, so we don't add them
385 // to expected_entry_map. 385 // to expected_entry_map.
386 base::CreateSymbolicLink( 386 base::CreateSymbolicLink(
387 current, 387 current,
388 dir_path.Append(current.BaseName().AddExtension( 388 dir_path.Append(current.BaseName().AddExtension(
389 FILE_PATH_LITERAL("link")))); 389 FILE_PATH_LITERAL("link"))));
390 #endif 390 #endif
391 } 391 }
392 392
393 // Perform ReadDirectory in the isolated filesystem. 393 // Perform ReadDirectory in the isolated filesystem.
394 FileSystemURL url = GetFileSystemURL(base::FilePath(test_case.path)); 394 FileSystemURL url = GetFileSystemURL(base::FilePath(test_case.path));
395 FileEntryList entries; 395 FileEntryList entries;
396 ASSERT_EQ(base::File::FILE_OK, 396 ASSERT_EQ(base::File::FILE_OK,
397 AsyncFileTestHelper::ReadDirectory( 397 AsyncFileTestHelper::ReadDirectory(
398 file_system_context(), url, &entries)); 398 file_system_context(), url, &entries));
399 399
400 EXPECT_EQ(expected_entry_map.size(), entries.size()); 400 EXPECT_EQ(expected_entry_map.size(), entries.size());
401 for (size_t i = 0; i < entries.size(); ++i) { 401 for (size_t i = 0; i < entries.size(); ++i) {
402 const fileapi::DirectoryEntry& entry = entries[i]; 402 const storage::DirectoryEntry& entry = entries[i];
403 EntryMap::iterator found = expected_entry_map.find(entry.name); 403 EntryMap::iterator found = expected_entry_map.find(entry.name);
404 EXPECT_TRUE(found != expected_entry_map.end()); 404 EXPECT_TRUE(found != expected_entry_map.end());
405 EXPECT_EQ(found->second.name, entry.name); 405 EXPECT_EQ(found->second.name, entry.name);
406 EXPECT_EQ(found->second.is_directory, entry.is_directory); 406 EXPECT_EQ(found->second.is_directory, entry.is_directory);
407 EXPECT_EQ(found->second.size, entry.size); 407 EXPECT_EQ(found->second.size, entry.size);
408 EXPECT_EQ(found->second.last_modified_time.ToDoubleT(), 408 EXPECT_EQ(found->second.last_modified_time.ToDoubleT(),
409 entry.last_modified_time.ToDoubleT()); 409 entry.last_modified_time.ToDoubleT());
410 } 410 }
411 } 411 }
412 } 412 }
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after
548 EXPECT_EQ(base::File::FILE_OK, 548 EXPECT_EQ(base::File::FILE_OK,
549 file_util()->Truncate(GetOperationContext().get(), url, 999)); 549 file_util()->Truncate(GetOperationContext().get(), url, 999));
550 ASSERT_EQ(base::File::FILE_OK, 550 ASSERT_EQ(base::File::FILE_OK,
551 file_util()->GetFileInfo(GetOperationContext().get(), url, 551 file_util()->GetFileInfo(GetOperationContext().get(), url,
552 &info, &platform_path)); 552 &info, &platform_path));
553 EXPECT_EQ(999, info.size); 553 EXPECT_EQ(999, info.size);
554 } 554 }
555 } 555 }
556 556
557 } // namespace content 557 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698