OLD | NEW |
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/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/files/scoped_temp_dir.h" | 10 #include "base/files/scoped_temp_dir.h" |
11 #include "base/format_macros.h" | 11 #include "base/format_macros.h" |
12 #include "base/message_loop.h" | 12 #include "base/message_loop.h" |
13 #include "base/stringprintf.h" | 13 #include "base/stringprintf.h" |
14 #include "base/time.h" | 14 #include "base/time.h" |
| 15 #include "chrome/browser/media_galleries/fileapi/async_file_util_test_helper.h" |
15 #include "chrome/browser/media_galleries/fileapi/media_file_system_mount_point_p
rovider.h" | 16 #include "chrome/browser/media_galleries/fileapi/media_file_system_mount_point_p
rovider.h" |
16 #include "chrome/browser/media_galleries/fileapi/native_media_file_util.h" | 17 #include "chrome/browser/media_galleries/fileapi/native_media_file_util.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
18 #include "webkit/browser/fileapi/external_mount_points.h" | 19 #include "webkit/browser/fileapi/external_mount_points.h" |
19 #include "webkit/browser/fileapi/file_system_context.h" | 20 #include "webkit/browser/fileapi/file_system_context.h" |
20 #include "webkit/browser/fileapi/file_system_mount_point_provider.h" | 21 #include "webkit/browser/fileapi/file_system_mount_point_provider.h" |
21 #include "webkit/browser/fileapi/file_system_operation.h" | 22 #include "webkit/browser/fileapi/file_system_operation.h" |
22 #include "webkit/browser/fileapi/file_system_task_runners.h" | 23 #include "webkit/browser/fileapi/file_system_task_runners.h" |
23 #include "webkit/browser/fileapi/file_system_url.h" | 24 #include "webkit/browser/fileapi/file_system_url.h" |
24 #include "webkit/browser/fileapi/isolated_context.h" | 25 #include "webkit/browser/fileapi/isolated_context.h" |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 file_system_context_ = | 132 file_system_context_ = |
132 new fileapi::FileSystemContext( | 133 new fileapi::FileSystemContext( |
133 fileapi::FileSystemTaskRunners::CreateMockTaskRunners(), | 134 fileapi::FileSystemTaskRunners::CreateMockTaskRunners(), |
134 fileapi::ExternalMountPoints::CreateRefCounted().get(), | 135 fileapi::ExternalMountPoints::CreateRefCounted().get(), |
135 storage_policy, | 136 storage_policy, |
136 NULL, | 137 NULL, |
137 additional_providers.Pass(), | 138 additional_providers.Pass(), |
138 data_dir_.path(), | 139 data_dir_.path(), |
139 fileapi::CreateAllowFileAccessOptions()); | 140 fileapi::CreateAllowFileAccessOptions()); |
140 | 141 |
141 file_util_ = file_system_context_->GetFileUtil( | 142 file_util_.reset( |
142 fileapi::kFileSystemTypeNativeMedia); | 143 new AsyncFileUtilTestHelper(new NativeMediaFileUtil())); |
143 | 144 |
144 filesystem_id_ = isolated_context()->RegisterFileSystemForPath( | 145 filesystem_id_ = isolated_context()->RegisterFileSystemForPath( |
145 fileapi::kFileSystemTypeNativeMedia, root_path(), NULL); | 146 fileapi::kFileSystemTypeNativeMedia, root_path(), NULL); |
146 | 147 |
147 isolated_context()->AddReference(filesystem_id_); | 148 isolated_context()->AddReference(filesystem_id_); |
148 } | 149 } |
149 | 150 |
150 virtual void TearDown() { | 151 virtual void TearDown() { |
151 isolated_context()->RemoveReference(filesystem_id_); | 152 isolated_context()->RemoveReference(filesystem_id_); |
152 file_system_context_ = NULL; | 153 file_system_context_ = NULL; |
(...skipping 19 matching lines...) Expand all Loading... |
172 return data_dir_.path().Append(FPL("Media Directory")); | 173 return data_dir_.path().Append(FPL("Media Directory")); |
173 } | 174 } |
174 | 175 |
175 base::FilePath GetVirtualPath( | 176 base::FilePath GetVirtualPath( |
176 const base::FilePath::CharType* test_case_path) { | 177 const base::FilePath::CharType* test_case_path) { |
177 return base::FilePath::FromUTF8Unsafe(filesystem_id_). | 178 return base::FilePath::FromUTF8Unsafe(filesystem_id_). |
178 Append(FPL("Media Directory")). | 179 Append(FPL("Media Directory")). |
179 Append(base::FilePath(test_case_path)); | 180 Append(base::FilePath(test_case_path)); |
180 } | 181 } |
181 | 182 |
182 fileapi::FileSystemFileUtil* file_util() { | 183 AsyncFileUtilTestHelper* file_util() { |
183 return file_util_; | 184 return file_util_.get(); |
184 } | 185 } |
185 | 186 |
186 GURL origin() { | 187 GURL origin() { |
187 return GURL("http://example.com"); | 188 return GURL("http://example.com"); |
188 } | 189 } |
189 | 190 |
190 fileapi::FileSystemType type() { | 191 fileapi::FileSystemType type() { |
191 return fileapi::kFileSystemTypeNativeMedia; | 192 return fileapi::kFileSystemTypeNativeMedia; |
192 } | 193 } |
193 | 194 |
194 FileSystemOperation* NewOperation(const FileSystemURL& url) { | 195 FileSystemOperation* NewOperation(const FileSystemURL& url) { |
195 return file_system_context_->CreateFileSystemOperation(url, NULL); | 196 return file_system_context_->CreateFileSystemOperation(url, NULL); |
196 } | 197 } |
197 | 198 |
198 private: | 199 private: |
199 base::MessageLoop message_loop_; | 200 base::MessageLoop message_loop_; |
200 | 201 |
201 base::ScopedTempDir data_dir_; | 202 base::ScopedTempDir data_dir_; |
202 scoped_refptr<fileapi::FileSystemContext> file_system_context_; | 203 scoped_refptr<fileapi::FileSystemContext> file_system_context_; |
203 | 204 |
204 fileapi::FileSystemFileUtil* file_util_; | 205 scoped_ptr<AsyncFileUtilTestHelper> file_util_; |
205 std::string filesystem_id_; | 206 std::string filesystem_id_; |
206 | 207 |
207 DISALLOW_COPY_AND_ASSIGN(NativeMediaFileUtilTest); | 208 DISALLOW_COPY_AND_ASSIGN(NativeMediaFileUtilTest); |
208 }; | 209 }; |
209 | 210 |
210 TEST_F(NativeMediaFileUtilTest, DirectoryExistsAndFileExistsFiltering) { | 211 TEST_F(NativeMediaFileUtilTest, DirectoryExistsAndFileExistsFiltering) { |
211 PopulateDirectoryWithTestCases(root_path(), | 212 PopulateDirectoryWithTestCases(root_path(), |
212 kFilteringTestCases, | 213 kFilteringTestCases, |
213 arraysize(kFilteringTestCases)); | 214 arraysize(kFilteringTestCases)); |
214 | 215 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 | 251 |
251 for (size_t i = 0; i < arraysize(kFilteringTestCases); ++i) { | 252 for (size_t i = 0; i < arraysize(kFilteringTestCases); ++i) { |
252 base::FilePath::StringType name = | 253 base::FilePath::StringType name = |
253 base::FilePath(kFilteringTestCases[i].path).BaseName().value(); | 254 base::FilePath(kFilteringTestCases[i].path).BaseName().value(); |
254 std::set<base::FilePath::StringType>::const_iterator found = | 255 std::set<base::FilePath::StringType>::const_iterator found = |
255 content.find(name); | 256 content.find(name); |
256 EXPECT_EQ(kFilteringTestCases[i].visible, found != content.end()); | 257 EXPECT_EQ(kFilteringTestCases[i].visible, found != content.end()); |
257 } | 258 } |
258 } | 259 } |
259 | 260 |
260 TEST_F(NativeMediaFileUtilTest, CreateFileAndCreateDirectoryFiltering) { | 261 TEST_F(NativeMediaFileUtilTest, CreateDirectoryFiltering) { |
261 // Run the loop twice. The second loop attempts to create files that are | 262 // Run the loop twice. The second loop attempts to create directories that are |
262 // pre-existing. Though the result should be the same. | 263 // pre-existing. Though the result should be the same. |
263 for (int loop_count = 0; loop_count < 2; ++loop_count) { | 264 for (int loop_count = 0; loop_count < 2; ++loop_count) { |
264 for (size_t i = 0; i < arraysize(kFilteringTestCases); ++i) { | 265 for (size_t i = 0; i < arraysize(kFilteringTestCases); ++i) { |
265 FileSystemURL root_url = CreateURL(FPL("")); | 266 FileSystemURL root_url = CreateURL(FPL("")); |
266 FileSystemOperation* operation = NewOperation(root_url); | 267 FileSystemOperation* operation = NewOperation(root_url); |
267 | 268 |
268 FileSystemURL url = CreateURL(kFilteringTestCases[i].path); | 269 FileSystemURL url = CreateURL(kFilteringTestCases[i].path); |
269 | 270 |
270 std::string test_name = base::StringPrintf( | 271 std::string test_name = base::StringPrintf( |
271 "CreateFileAndCreateDirectoryFiltering run %d, test %" PRIuS, | 272 "CreateFileAndCreateDirectoryFiltering run %d, test %" PRIuS, |
272 loop_count, i); | 273 loop_count, i); |
273 base::PlatformFileError expectation = | 274 base::PlatformFileError expectation = |
274 kFilteringTestCases[i].visible ? | 275 kFilteringTestCases[i].visible ? |
275 base::PLATFORM_FILE_OK : | 276 base::PLATFORM_FILE_OK : |
276 base::PLATFORM_FILE_ERROR_SECURITY; | 277 base::PLATFORM_FILE_ERROR_SECURITY; |
277 if (kFilteringTestCases[i].is_directory) { | 278 if (kFilteringTestCases[i].is_directory) { |
278 operation->CreateDirectory( | 279 operation->CreateDirectory( |
279 url, false, false, | 280 url, false, false, |
280 base::Bind(&ExpectEqHelper, test_name, expectation)); | 281 base::Bind(&ExpectEqHelper, test_name, expectation)); |
281 } else { | |
282 operation->CreateFile( | |
283 url, false, base::Bind(&ExpectEqHelper, test_name, expectation)); | |
284 } | 282 } |
285 base::MessageLoop::current()->RunUntilIdle(); | 283 base::MessageLoop::current()->RunUntilIdle(); |
286 } | 284 } |
287 } | 285 } |
288 } | 286 } |
289 | 287 |
290 TEST_F(NativeMediaFileUtilTest, CopySourceFiltering) { | 288 TEST_F(NativeMediaFileUtilTest, CopySourceFiltering) { |
291 base::FilePath dest_path = root_path().AppendASCII("dest"); | 289 base::FilePath dest_path = root_path().AppendASCII("dest"); |
292 FileSystemURL dest_url = CreateURL(FPL("dest")); | 290 FileSystemURL dest_url = CreateURL(FPL("dest")); |
293 | 291 |
(...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
523 operation->GetMetadata(url, | 521 operation->GetMetadata(url, |
524 base::Bind(&ExpectMetadataEqHelper, | 522 base::Bind(&ExpectMetadataEqHelper, |
525 test_name, | 523 test_name, |
526 expectation, | 524 expectation, |
527 kFilteringTestCases[i].is_directory)); | 525 kFilteringTestCases[i].is_directory)); |
528 base::MessageLoop::current()->RunUntilIdle(); | 526 base::MessageLoop::current()->RunUntilIdle(); |
529 } | 527 } |
530 } | 528 } |
531 } | 529 } |
532 | 530 |
533 TEST_F(NativeMediaFileUtilTest, RemoveFiltering) { | |
534 // Run the loop twice. The first run has no files. The second run does. | |
535 for (int loop_count = 0; loop_count < 2; ++loop_count) { | |
536 if (loop_count == 1) { | |
537 PopulateDirectoryWithTestCases(root_path(), | |
538 kFilteringTestCases, | |
539 arraysize(kFilteringTestCases)); | |
540 } | |
541 for (size_t i = 0; i < arraysize(kFilteringTestCases); ++i) { | |
542 FileSystemURL root_url = CreateURL(FPL("")); | |
543 FileSystemOperation* operation = NewOperation(root_url); | |
544 | |
545 FileSystemURL url = CreateURL(kFilteringTestCases[i].path); | |
546 | |
547 std::string test_name = base::StringPrintf( | |
548 "RemoveFiltering run %d test %" PRIuS, loop_count, i); | |
549 base::PlatformFileError expectation = base::PLATFORM_FILE_OK; | |
550 if (loop_count == 0 || !kFilteringTestCases[i].visible) { | |
551 // Cannot remove files that do not exist or are not visible. | |
552 expectation = base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
553 } | |
554 operation->Remove( | |
555 url, false, base::Bind(&ExpectEqHelper, test_name, expectation)); | |
556 base::MessageLoop::current()->RunUntilIdle(); | |
557 } | |
558 } | |
559 } | |
560 | |
561 TEST_F(NativeMediaFileUtilTest, TruncateFiltering) { | |
562 // Run the loop twice. The first run has no files. The second run does. | |
563 for (int loop_count = 0; loop_count < 2; ++loop_count) { | |
564 if (loop_count == 1) { | |
565 PopulateDirectoryWithTestCases(root_path(), | |
566 kFilteringTestCases, | |
567 arraysize(kFilteringTestCases)); | |
568 } | |
569 for (size_t i = 0; i < arraysize(kFilteringTestCases); ++i) { | |
570 FileSystemURL root_url = CreateURL(FPL("")); | |
571 FileSystemOperation* operation = NewOperation(root_url); | |
572 | |
573 FileSystemURL url = CreateURL(kFilteringTestCases[i].path); | |
574 | |
575 std::string test_name = base::StringPrintf( | |
576 "TruncateFiltering run %d test %" PRIuS, loop_count, i); | |
577 base::PlatformFileError expectation = base::PLATFORM_FILE_OK; | |
578 if (loop_count == 0 || !kFilteringTestCases[i].visible) { | |
579 // Cannot truncate files that do not exist or are not visible. | |
580 expectation = base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
581 } else if (kFilteringTestCases[i].is_directory) { | |
582 // Cannot truncate directories. | |
583 expectation = base::PLATFORM_FILE_ERROR_ACCESS_DENIED; | |
584 } | |
585 operation->Truncate( | |
586 url, 0, base::Bind(&ExpectEqHelper, test_name, expectation)); | |
587 base::MessageLoop::current()->RunUntilIdle(); | |
588 } | |
589 } | |
590 } | |
591 | |
592 TEST_F(NativeMediaFileUtilTest, TouchFileFiltering) { | |
593 base::Time time = base::Time::Now(); | |
594 | |
595 // Run the loop twice. The first run has no files. The second run does. | |
596 for (int loop_count = 0; loop_count < 2; ++loop_count) { | |
597 if (loop_count == 1) { | |
598 PopulateDirectoryWithTestCases(root_path(), | |
599 kFilteringTestCases, | |
600 arraysize(kFilteringTestCases)); | |
601 } | |
602 for (size_t i = 0; i < arraysize(kFilteringTestCases); ++i) { | |
603 FileSystemURL root_url = CreateURL(FPL("")); | |
604 FileSystemOperation* operation = NewOperation(root_url); | |
605 | |
606 FileSystemURL url = CreateURL(kFilteringTestCases[i].path); | |
607 | |
608 std::string test_name = base::StringPrintf( | |
609 "TouchFileFiltering run %d test %" PRIuS, loop_count, i); | |
610 base::PlatformFileError expectation = base::PLATFORM_FILE_OK; | |
611 if (loop_count == 0 || !kFilteringTestCases[i].visible) { | |
612 // Files do not exists. Touch fails. | |
613 expectation = base::PLATFORM_FILE_ERROR_FAILED; | |
614 } | |
615 operation->TouchFile( | |
616 url, time, time, base::Bind(&ExpectEqHelper, test_name, expectation)); | |
617 base::MessageLoop::current()->RunUntilIdle(); | |
618 } | |
619 } | |
620 } | |
621 | |
622 void CreateSnapshotCallback(base::PlatformFileError* error, | 531 void CreateSnapshotCallback(base::PlatformFileError* error, |
623 base::PlatformFileError result, const base::PlatformFileInfo&, | 532 base::PlatformFileError result, const base::PlatformFileInfo&, |
624 const base::FilePath&, | 533 const base::FilePath&, |
625 const scoped_refptr<webkit_blob::ShareableFileReference>&) { | 534 const scoped_refptr<webkit_blob::ShareableFileReference>&) { |
626 *error = result; | 535 *error = result; |
627 } | 536 } |
628 | 537 |
629 TEST_F(NativeMediaFileUtilTest, CreateSnapshot) { | 538 TEST_F(NativeMediaFileUtilTest, CreateSnapshot) { |
630 PopulateDirectoryWithTestCases(root_path(), | 539 PopulateDirectoryWithTestCases(root_path(), |
631 kFilteringTestCases, | 540 kFilteringTestCases, |
(...skipping 13 matching lines...) Expand all Loading... |
645 expected_error = base::PLATFORM_FILE_ERROR_SECURITY; | 554 expected_error = base::PLATFORM_FILE_ERROR_SECURITY; |
646 error = base::PLATFORM_FILE_ERROR_FAILED; | 555 error = base::PLATFORM_FILE_ERROR_FAILED; |
647 operation->CreateSnapshotFile(url, | 556 operation->CreateSnapshotFile(url, |
648 base::Bind(CreateSnapshotCallback, &error)); | 557 base::Bind(CreateSnapshotCallback, &error)); |
649 base::MessageLoop::current()->RunUntilIdle(); | 558 base::MessageLoop::current()->RunUntilIdle(); |
650 ASSERT_EQ(expected_error, error); | 559 ASSERT_EQ(expected_error, error); |
651 } | 560 } |
652 } | 561 } |
653 | 562 |
654 } // namespace chrome | 563 } // namespace chrome |
OLD | NEW |