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

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

Issue 15653004: Picasa import: Make NativeMediaFileUtil an AsyncFileUtil (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address vandebo comments. Created 7 years, 6 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/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/media_file_system_mount_point_p rovider.h" 15 #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" 16 #include "chrome/browser/media_galleries/fileapi/native_media_file_util.h"
17 #include "content/public/test/test_browser_thread.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"
25 #include "webkit/browser/fileapi/mock_file_system_options.h" 26 #include "webkit/browser/fileapi/mock_file_system_options.h"
26 #include "webkit/browser/fileapi/native_file_util.h" 27 #include "webkit/browser/fileapi/native_file_util.h"
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
107 ASSERT_EQ(len, file_util::WriteFile(path, test_cases[i].content, len)); 108 ASSERT_EQ(len, file_util::WriteFile(path, test_cases[i].content, len));
108 } 109 }
109 } 110 }
110 } 111 }
111 112
112 } // namespace 113 } // namespace
113 114
114 class NativeMediaFileUtilTest : public testing::Test { 115 class NativeMediaFileUtilTest : public testing::Test {
115 public: 116 public:
116 NativeMediaFileUtilTest() 117 NativeMediaFileUtilTest()
117 : file_util_(NULL) { 118 : io_thread_(content::BrowserThread::IO, &message_loop_) {
118 } 119 }
119 120
120 virtual void SetUp() { 121 virtual void SetUp() {
121 ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); 122 ASSERT_TRUE(data_dir_.CreateUniqueTempDir());
122 ASSERT_TRUE(file_util::CreateDirectory(root_path())); 123 ASSERT_TRUE(file_util::CreateDirectory(root_path()));
123 124
124 scoped_refptr<quota::SpecialStoragePolicy> storage_policy = 125 scoped_refptr<quota::SpecialStoragePolicy> storage_policy =
125 new quota::MockSpecialStoragePolicy(); 126 new quota::MockSpecialStoragePolicy();
126 127
127 ScopedVector<fileapi::FileSystemMountPointProvider> additional_providers; 128 ScopedVector<fileapi::FileSystemMountPointProvider> additional_providers;
128 additional_providers.push_back(new MediaFileSystemMountPointProvider( 129 additional_providers.push_back(new MediaFileSystemMountPointProvider(
129 data_dir_.path())); 130 data_dir_.path()));
130 131
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 fileapi::kFileSystemTypeNativeMedia);
143
144 filesystem_id_ = isolated_context()->RegisterFileSystemForPath( 142 filesystem_id_ = isolated_context()->RegisterFileSystemForPath(
145 fileapi::kFileSystemTypeNativeMedia, root_path(), NULL); 143 fileapi::kFileSystemTypeNativeMedia, root_path(), NULL);
146 144
147 isolated_context()->AddReference(filesystem_id_); 145 isolated_context()->AddReference(filesystem_id_);
148 } 146 }
149 147
150 virtual void TearDown() { 148 virtual void TearDown() {
151 isolated_context()->RemoveReference(filesystem_id_); 149 isolated_context()->RemoveReference(filesystem_id_);
152 file_system_context_ = NULL; 150 file_system_context_ = NULL;
153 } 151 }
(...skipping 18 matching lines...) Expand all
172 return data_dir_.path().Append(FPL("Media Directory")); 170 return data_dir_.path().Append(FPL("Media Directory"));
173 } 171 }
174 172
175 base::FilePath GetVirtualPath( 173 base::FilePath GetVirtualPath(
176 const base::FilePath::CharType* test_case_path) { 174 const base::FilePath::CharType* test_case_path) {
177 return base::FilePath::FromUTF8Unsafe(filesystem_id_). 175 return base::FilePath::FromUTF8Unsafe(filesystem_id_).
178 Append(FPL("Media Directory")). 176 Append(FPL("Media Directory")).
179 Append(base::FilePath(test_case_path)); 177 Append(base::FilePath(test_case_path));
180 } 178 }
181 179
182 fileapi::FileSystemFileUtil* file_util() {
183 return file_util_;
184 }
185
186 GURL origin() { 180 GURL origin() {
187 return GURL("http://example.com"); 181 return GURL("http://example.com");
188 } 182 }
189 183
190 fileapi::FileSystemType type() { 184 fileapi::FileSystemType type() {
191 return fileapi::kFileSystemTypeNativeMedia; 185 return fileapi::kFileSystemTypeNativeMedia;
192 } 186 }
193 187
194 FileSystemOperation* NewOperation(const FileSystemURL& url) { 188 FileSystemOperation* NewOperation(const FileSystemURL& url) {
195 return file_system_context_->CreateFileSystemOperation(url, NULL); 189 return file_system_context_->CreateFileSystemOperation(url, NULL);
196 } 190 }
197 191
198 private: 192 private:
199 base::MessageLoop message_loop_; 193 base::MessageLoop message_loop_;
194 content::TestBrowserThread io_thread_;
200 195
201 base::ScopedTempDir data_dir_; 196 base::ScopedTempDir data_dir_;
202 scoped_refptr<fileapi::FileSystemContext> file_system_context_; 197 scoped_refptr<fileapi::FileSystemContext> file_system_context_;
203 198
204 fileapi::FileSystemFileUtil* file_util_;
205 std::string filesystem_id_; 199 std::string filesystem_id_;
206 200
207 DISALLOW_COPY_AND_ASSIGN(NativeMediaFileUtilTest); 201 DISALLOW_COPY_AND_ASSIGN(NativeMediaFileUtilTest);
208 }; 202 };
209 203
210 TEST_F(NativeMediaFileUtilTest, DirectoryExistsAndFileExistsFiltering) { 204 TEST_F(NativeMediaFileUtilTest, DirectoryExistsAndFileExistsFiltering) {
211 PopulateDirectoryWithTestCases(root_path(), 205 PopulateDirectoryWithTestCases(root_path(),
212 kFilteringTestCases, 206 kFilteringTestCases,
213 arraysize(kFilteringTestCases)); 207 arraysize(kFilteringTestCases));
214 208
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
250 244
251 for (size_t i = 0; i < arraysize(kFilteringTestCases); ++i) { 245 for (size_t i = 0; i < arraysize(kFilteringTestCases); ++i) {
252 base::FilePath::StringType name = 246 base::FilePath::StringType name =
253 base::FilePath(kFilteringTestCases[i].path).BaseName().value(); 247 base::FilePath(kFilteringTestCases[i].path).BaseName().value();
254 std::set<base::FilePath::StringType>::const_iterator found = 248 std::set<base::FilePath::StringType>::const_iterator found =
255 content.find(name); 249 content.find(name);
256 EXPECT_EQ(kFilteringTestCases[i].visible, found != content.end()); 250 EXPECT_EQ(kFilteringTestCases[i].visible, found != content.end());
257 } 251 }
258 } 252 }
259 253
260 TEST_F(NativeMediaFileUtilTest, CreateFileAndCreateDirectoryFiltering) { 254 TEST_F(NativeMediaFileUtilTest, CreateDirectoryFiltering) {
261 // Run the loop twice. The second loop attempts to create files that are 255 // Run the loop twice. The second loop attempts to create directories that are
262 // pre-existing. Though the result should be the same. 256 // pre-existing. Though the result should be the same.
263 for (int loop_count = 0; loop_count < 2; ++loop_count) { 257 for (int loop_count = 0; loop_count < 2; ++loop_count) {
264 for (size_t i = 0; i < arraysize(kFilteringTestCases); ++i) { 258 for (size_t i = 0; i < arraysize(kFilteringTestCases); ++i) {
265 FileSystemURL root_url = CreateURL(FPL("")); 259 FileSystemURL root_url = CreateURL(FPL(""));
266 FileSystemOperation* operation = NewOperation(root_url); 260 FileSystemOperation* operation = NewOperation(root_url);
267 261
268 FileSystemURL url = CreateURL(kFilteringTestCases[i].path); 262 FileSystemURL url = CreateURL(kFilteringTestCases[i].path);
269 263
270 std::string test_name = base::StringPrintf( 264 std::string test_name = base::StringPrintf(
271 "CreateFileAndCreateDirectoryFiltering run %d, test %" PRIuS, 265 "CreateFileAndCreateDirectoryFiltering run %d, test %" PRIuS,
272 loop_count, i); 266 loop_count, i);
273 base::PlatformFileError expectation = 267 base::PlatformFileError expectation =
274 kFilteringTestCases[i].visible ? 268 kFilteringTestCases[i].visible ?
275 base::PLATFORM_FILE_OK : 269 base::PLATFORM_FILE_OK :
276 base::PLATFORM_FILE_ERROR_SECURITY; 270 base::PLATFORM_FILE_ERROR_SECURITY;
277 if (kFilteringTestCases[i].is_directory) { 271 if (kFilteringTestCases[i].is_directory) {
278 operation->CreateDirectory( 272 operation->CreateDirectory(
279 url, false, false, 273 url, false, false,
280 base::Bind(&ExpectEqHelper, test_name, expectation)); 274 base::Bind(&ExpectEqHelper, test_name, expectation));
281 } else {
282 operation->CreateFile(
283 url, false, base::Bind(&ExpectEqHelper, test_name, expectation));
284 } 275 }
285 base::MessageLoop::current()->RunUntilIdle(); 276 base::MessageLoop::current()->RunUntilIdle();
286 } 277 }
287 } 278 }
288 } 279 }
289 280
290 TEST_F(NativeMediaFileUtilTest, CopySourceFiltering) { 281 TEST_F(NativeMediaFileUtilTest, CopySourceFiltering) {
291 base::FilePath dest_path = root_path().AppendASCII("dest"); 282 base::FilePath dest_path = root_path().AppendASCII("dest");
292 FileSystemURL dest_url = CreateURL(FPL("dest")); 283 FileSystemURL dest_url = CreateURL(FPL("dest"));
293 284
(...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after
523 operation->GetMetadata(url, 514 operation->GetMetadata(url,
524 base::Bind(&ExpectMetadataEqHelper, 515 base::Bind(&ExpectMetadataEqHelper,
525 test_name, 516 test_name,
526 expectation, 517 expectation,
527 kFilteringTestCases[i].is_directory)); 518 kFilteringTestCases[i].is_directory));
528 base::MessageLoop::current()->RunUntilIdle(); 519 base::MessageLoop::current()->RunUntilIdle();
529 } 520 }
530 } 521 }
531 } 522 }
532 523
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, 524 void CreateSnapshotCallback(base::PlatformFileError* error,
623 base::PlatformFileError result, const base::PlatformFileInfo&, 525 base::PlatformFileError result, const base::PlatformFileInfo&,
624 const base::FilePath&, 526 const base::FilePath&,
625 const scoped_refptr<webkit_blob::ShareableFileReference>&) { 527 const scoped_refptr<webkit_blob::ShareableFileReference>&) {
626 *error = result; 528 *error = result;
627 } 529 }
628 530
629 TEST_F(NativeMediaFileUtilTest, CreateSnapshot) { 531 TEST_F(NativeMediaFileUtilTest, CreateSnapshot) {
630 PopulateDirectoryWithTestCases(root_path(), 532 PopulateDirectoryWithTestCases(root_path(),
631 kFilteringTestCases, 533 kFilteringTestCases,
(...skipping 13 matching lines...) Expand all
645 expected_error = base::PLATFORM_FILE_ERROR_SECURITY; 547 expected_error = base::PLATFORM_FILE_ERROR_SECURITY;
646 error = base::PLATFORM_FILE_ERROR_FAILED; 548 error = base::PLATFORM_FILE_ERROR_FAILED;
647 operation->CreateSnapshotFile(url, 549 operation->CreateSnapshotFile(url,
648 base::Bind(CreateSnapshotCallback, &error)); 550 base::Bind(CreateSnapshotCallback, &error));
649 base::MessageLoop::current()->RunUntilIdle(); 551 base::MessageLoop::current()->RunUntilIdle();
650 ASSERT_EQ(expected_error, error); 552 ASSERT_EQ(expected_error, error);
651 } 553 }
652 } 554 }
653 555
654 } // namespace chrome 556 } // namespace chrome
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698