OLD | NEW |
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 <set> | 5 #include <set> |
6 #include <string> | 6 #include <string> |
7 | 7 |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
10 #include "base/memory/scoped_vector.h" | 10 #include "base/memory/scoped_vector.h" |
11 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
| 12 #include "base/run_loop.h" |
12 #include "base/stringprintf.h" | 13 #include "base/stringprintf.h" |
13 #include "base/time.h" | 14 #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/media_file_system_mount_point_p
rovider.h" |
15 #include "chrome/browser/media_galleries/fileapi/media_path_filter.h" | 16 #include "chrome/browser/media_galleries/fileapi/media_path_filter.h" |
16 #include "chrome/browser/media_galleries/fileapi/picasa/picasa_album_table_reade
r.h" | 17 #include "chrome/browser/media_galleries/fileapi/picasa/picasa_album_table_reade
r.h" |
17 #include "chrome/browser/media_galleries/fileapi/picasa/picasa_data_provider.h" | 18 #include "chrome/browser/media_galleries/fileapi/picasa/picasa_data_provider.h" |
18 #include "chrome/browser/media_galleries/fileapi/picasa/picasa_file_util.h" | 19 #include "chrome/browser/media_galleries/fileapi/picasa/picasa_file_util.h" |
19 #include "chrome/browser/media_galleries/fileapi/picasa/pmp_constants.h" | 20 #include "chrome/browser/media_galleries/fileapi/picasa/pmp_constants.h" |
20 #include "chrome/browser/media_galleries/fileapi/picasa/pmp_test_helper.h" | 21 #include "chrome/browser/media_galleries/fileapi/picasa/pmp_test_helper.h" |
| 22 #include "content/public/test/test_browser_thread.h" |
21 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
22 #include "webkit/browser/fileapi/async_file_util_adapter.h" | 24 #include "webkit/browser/fileapi/async_file_util_adapter.h" |
23 #include "webkit/browser/fileapi/external_mount_points.h" | 25 #include "webkit/browser/fileapi/external_mount_points.h" |
24 #include "webkit/browser/fileapi/file_system_context.h" | 26 #include "webkit/browser/fileapi/file_system_context.h" |
25 #include "webkit/browser/fileapi/file_system_file_util.h" | 27 #include "webkit/browser/fileapi/file_system_file_util.h" |
26 #include "webkit/browser/fileapi/file_system_operation.h" | 28 #include "webkit/browser/fileapi/file_system_operation.h" |
27 #include "webkit/browser/fileapi/file_system_operation_context.h" | 29 #include "webkit/browser/fileapi/file_system_operation_context.h" |
28 #include "webkit/browser/fileapi/file_system_task_runners.h" | 30 #include "webkit/browser/fileapi/file_system_task_runners.h" |
29 #include "webkit/browser/fileapi/isolated_context.h" | 31 #include "webkit/browser/fileapi/isolated_context.h" |
30 #include "webkit/browser/fileapi/local_file_system_operation.h" | 32 #include "webkit/browser/fileapi/local_file_system_operation.h" |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 } | 141 } |
140 | 142 |
141 scoped_ptr<PicasaDataProvider> data_provider_; | 143 scoped_ptr<PicasaDataProvider> data_provider_; |
142 }; | 144 }; |
143 | 145 |
144 class TestMediaFileSystemMountPointProvider | 146 class TestMediaFileSystemMountPointProvider |
145 : public chrome::MediaFileSystemMountPointProvider { | 147 : public chrome::MediaFileSystemMountPointProvider { |
146 public: | 148 public: |
147 TestMediaFileSystemMountPointProvider( | 149 TestMediaFileSystemMountPointProvider( |
148 const base::FilePath& profile_path, | 150 const base::FilePath& profile_path, |
149 scoped_ptr<PicasaFileUtil> picasa_file_util) | 151 PicasaFileUtil* picasa_file_util) |
150 : chrome::MediaFileSystemMountPointProvider(profile_path), | 152 : chrome::MediaFileSystemMountPointProvider(profile_path), |
151 test_file_util_(new fileapi::AsyncFileUtilAdapter( | 153 test_file_util_(picasa_file_util) { |
152 picasa_file_util.release())) { | |
153 } | 154 } |
154 | 155 |
155 virtual fileapi::AsyncFileUtil* | 156 virtual fileapi::AsyncFileUtil* |
156 GetAsyncFileUtil(fileapi::FileSystemType type) OVERRIDE { | 157 GetAsyncFileUtil(fileapi::FileSystemType type) OVERRIDE { |
157 if (type != fileapi::kFileSystemTypePicasa) | 158 if (type != fileapi::kFileSystemTypePicasa) |
158 return NULL; | 159 return NULL; |
159 | 160 |
160 return test_file_util_.get(); | 161 return test_file_util_.get(); |
161 } | 162 } |
162 | 163 |
163 private: | 164 private: |
164 scoped_ptr<fileapi::AsyncFileUtil> test_file_util_; | 165 scoped_ptr<fileapi::AsyncFileUtil> test_file_util_; |
165 }; | 166 }; |
166 | 167 |
167 void DidReadDirectory(FileSystemOperation::FileEntryList* contents, | 168 void DidReadDirectory(FileSystemOperation::FileEntryList* contents, |
168 bool* completed, base::PlatformFileError error, | 169 bool* completed, base::PlatformFileError error, |
169 const FileSystemOperation::FileEntryList& file_list, | 170 const FileSystemOperation::FileEntryList& file_list, |
170 bool has_more) { | 171 bool has_more) { |
171 DCHECK(!*completed); | 172 DCHECK(!*completed); |
172 *completed = !has_more && error == base::PLATFORM_FILE_OK; | 173 *completed = !has_more && error == base::PLATFORM_FILE_OK; |
173 *contents = file_list; | 174 *contents = file_list; |
174 } | 175 } |
175 | 176 |
| 177 void ReadDirectoryTestCallback( |
| 178 base::RunLoop* run_loop, |
| 179 base::PlatformFileError* error_result, |
| 180 fileapi::AsyncFileUtil::EntryList* file_list_result, |
| 181 base::PlatformFileError error, |
| 182 const fileapi::AsyncFileUtil::EntryList& file_list, |
| 183 bool /*has_more*/) { |
| 184 DCHECK(error_result); |
| 185 DCHECK(file_list_result); |
| 186 *error_result = error; |
| 187 *file_list_result = file_list; |
| 188 run_loop->Quit(); |
| 189 } |
| 190 |
| 191 base::PlatformFileError ReadDirectoryTestHelper( |
| 192 fileapi::AsyncFileUtil* file_util, |
| 193 FileSystemOperationContext* operation_context, |
| 194 FileSystemURL url, |
| 195 fileapi::AsyncFileUtil::EntryList* file_list) { |
| 196 base::RunLoop run_loop; |
| 197 base::PlatformFileError result; |
| 198 file_util->ReadDirectory( |
| 199 operation_context, |
| 200 url, |
| 201 base::Bind(&ReadDirectoryTestCallback, &run_loop, &result, file_list)); |
| 202 run_loop.Run(); |
| 203 return result; |
| 204 } |
| 205 |
176 } // namespace | 206 } // namespace |
177 | 207 |
178 class PicasaFileUtilTest : public testing::Test { | 208 class PicasaFileUtilTest : public testing::Test { |
179 public: | 209 public: |
180 PicasaFileUtilTest() {} | 210 PicasaFileUtilTest() |
| 211 : io_thread_(content::BrowserThread::IO, &message_loop_) { |
| 212 } |
181 virtual ~PicasaFileUtilTest() {} | 213 virtual ~PicasaFileUtilTest() {} |
182 | 214 |
183 virtual void SetUp() OVERRIDE { | 215 virtual void SetUp() OVERRIDE { |
184 test_helper_.reset(new PmpTestHelper(kPicasaAlbumTableName)); | 216 test_helper_.reset(new PmpTestHelper(kPicasaAlbumTableName)); |
185 ASSERT_TRUE(test_helper_->Init()); | 217 ASSERT_TRUE(test_helper_->Init()); |
186 | 218 |
187 scoped_ptr<PicasaDataProvider> data_provider( | 219 scoped_ptr<PicasaDataProvider> data_provider( |
188 new PicasaDataProvider(test_helper_->GetTempDirPath())); | 220 new PicasaDataProvider(test_helper_->GetTempDirPath())); |
189 scoped_ptr<PicasaFileUtil> file_util(new TestPicasaFileUtil( | |
190 data_provider.Pass())); | |
191 | 221 |
192 ASSERT_TRUE(profile_dir_.CreateUniqueTempDir()); | 222 ASSERT_TRUE(profile_dir_.CreateUniqueTempDir()); |
193 | 223 |
194 scoped_refptr<quota::SpecialStoragePolicy> storage_policy = | 224 scoped_refptr<quota::SpecialStoragePolicy> storage_policy = |
195 new quota::MockSpecialStoragePolicy(); | 225 new quota::MockSpecialStoragePolicy(); |
196 | 226 |
197 ScopedVector<fileapi::FileSystemMountPointProvider> additional_providers; | 227 ScopedVector<fileapi::FileSystemMountPointProvider> additional_providers; |
198 additional_providers.push_back( | 228 additional_providers.push_back(new TestMediaFileSystemMountPointProvider( |
199 new TestMediaFileSystemMountPointProvider(profile_dir_.path(), | 229 profile_dir_.path(), |
200 file_util.Pass())); | 230 new TestPicasaFileUtil(data_provider.Pass()))); |
201 | 231 |
202 file_system_context_ = new fileapi::FileSystemContext( | 232 file_system_context_ = new fileapi::FileSystemContext( |
203 fileapi::FileSystemTaskRunners::CreateMockTaskRunners(), | 233 fileapi::FileSystemTaskRunners::CreateMockTaskRunners(), |
204 fileapi::ExternalMountPoints::CreateRefCounted().get(), | 234 fileapi::ExternalMountPoints::CreateRefCounted().get(), |
205 storage_policy.get(), | 235 storage_policy.get(), |
206 NULL, | 236 NULL, |
207 additional_providers.Pass(), | 237 additional_providers.Pass(), |
208 profile_dir_.path(), | 238 profile_dir_.path(), |
209 fileapi::CreateAllowFileAccessOptions()); | 239 fileapi::CreateAllowFileAccessOptions()); |
210 } | 240 } |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
318 FileSystemOperation* NewOperation(const FileSystemURL& url) { | 348 FileSystemOperation* NewOperation(const FileSystemURL& url) { |
319 return file_system_context_->CreateFileSystemOperation(url, NULL); | 349 return file_system_context_->CreateFileSystemOperation(url, NULL); |
320 } | 350 } |
321 | 351 |
322 scoped_refptr<fileapi::FileSystemContext> file_system_context() { | 352 scoped_refptr<fileapi::FileSystemContext> file_system_context() { |
323 return file_system_context_; | 353 return file_system_context_; |
324 } | 354 } |
325 | 355 |
326 private: | 356 private: |
327 base::MessageLoop message_loop_; | 357 base::MessageLoop message_loop_; |
| 358 content::TestBrowserThread io_thread_; |
328 | 359 |
329 base::ScopedTempDir profile_dir_; | 360 base::ScopedTempDir profile_dir_; |
330 | 361 |
331 scoped_refptr<fileapi::FileSystemContext> file_system_context_; | 362 scoped_refptr<fileapi::FileSystemContext> file_system_context_; |
332 scoped_ptr<PmpTestHelper> test_helper_; | 363 scoped_ptr<PmpTestHelper> test_helper_; |
333 | 364 |
334 DISALLOW_COPY_AND_ASSIGN(PicasaFileUtilTest); | 365 DISALLOW_COPY_AND_ASSIGN(PicasaFileUtilTest); |
335 }; | 366 }; |
336 | 367 |
337 TEST_F(PicasaFileUtilTest, DateFormat) { | 368 TEST_F(PicasaFileUtilTest, DateFormat) { |
(...skipping 30 matching lines...) Expand all Loading... |
368 expected_names.push_back("duplicate " + test_date_string + " (1)"); | 399 expected_names.push_back("duplicate " + test_date_string + " (1)"); |
369 | 400 |
370 folders.push_back( | 401 folders.push_back( |
371 AlbumInfo("duplicate", test_date, "uuid5", base::FilePath())); | 402 AlbumInfo("duplicate", test_date, "uuid5", base::FilePath())); |
372 expected_names.push_back("duplicate " + test_date_string + " (2)"); | 403 expected_names.push_back("duplicate " + test_date_string + " (2)"); |
373 | 404 |
374 folders.push_back( | 405 folders.push_back( |
375 AlbumInfo("unique_name", test_date, "uuid1", base::FilePath())); | 406 AlbumInfo("unique_name", test_date, "uuid1", base::FilePath())); |
376 expected_names.push_back("unique_name " + test_date_string); | 407 expected_names.push_back("unique_name " + test_date_string); |
377 | 408 |
378 scoped_ptr<FileSystemOperationContext> operation_context( | 409 FileSystemOperationContext operation_context(file_system_context().get()); |
379 new FileSystemOperationContext(file_system_context().get())); | |
380 | 410 |
381 scoped_ptr<chrome::MediaPathFilter> media_path_filter( | 411 scoped_ptr<chrome::MediaPathFilter> media_path_filter( |
382 new chrome::MediaPathFilter()); | 412 new chrome::MediaPathFilter()); |
383 | 413 |
384 operation_context->SetUserValue( | 414 operation_context.SetUserValue( |
385 chrome::MediaFileSystemMountPointProvider::kMediaPathFilterKey, | 415 chrome::MediaFileSystemMountPointProvider::kMediaPathFilterKey, |
386 media_path_filter.get()); | 416 media_path_filter.get()); |
387 | 417 |
388 scoped_ptr<PicasaDataProvider> data_provider( | 418 scoped_ptr<PicasaDataProvider> data_provider( |
389 new TestPicasaDataProvider(std::vector<AlbumInfo>(), folders)); | 419 new TestPicasaDataProvider(std::vector<AlbumInfo>(), folders)); |
390 TestPicasaFileUtil file_util(data_provider.Pass()); | 420 TestPicasaFileUtil test_file_util(data_provider.Pass()); |
391 | 421 |
392 scoped_ptr<FileSystemFileUtil::AbstractFileEnumerator> file_enumerator = | 422 fileapi::AsyncFileUtil::EntryList file_list; |
393 file_util.CreateFileEnumerator(operation_context.get(), | 423 ASSERT_EQ(base::PLATFORM_FILE_OK, |
394 CreateURL("folders")); | 424 ReadDirectoryTestHelper(&test_file_util, &operation_context, |
| 425 CreateURL(kPicasaDirFolders), |
| 426 &file_list)); |
395 | 427 |
396 for (unsigned int i = 0; i < expected_names.size(); ++i) { | 428 ASSERT_EQ(expected_names.size(), file_list.size()); |
397 base::FilePath path = file_enumerator->Next(); | 429 for (size_t i = 0; i < file_list.size(); ++i) { |
398 | 430 EXPECT_EQ(expected_names[i], |
399 ASSERT_TRUE(!path.empty()); | 431 base::FilePath(file_list[i].name).AsUTF8Unsafe()); |
400 EXPECT_EQ(expected_names[i], path.BaseName().AsUTF8Unsafe()); | 432 EXPECT_EQ(folders[i].timestamp, file_list[i].last_modified_time); |
401 EXPECT_EQ(folders[i].timestamp, file_enumerator->LastModifiedTime()); | 433 EXPECT_TRUE(file_list[i].is_directory); |
402 EXPECT_TRUE(file_enumerator->IsDirectory()); | |
403 } | 434 } |
404 } | 435 } |
405 | 436 |
406 TEST_F(PicasaFileUtilTest, RootFolders) { | 437 TEST_F(PicasaFileUtilTest, RootFolders) { |
407 FileSystemOperation::FileEntryList contents; | 438 FileSystemOperation::FileEntryList contents; |
408 FileSystemURL url = CreateURL(""); | 439 FileSystemURL url = CreateURL(""); |
409 bool completed = false; | 440 bool completed = false; |
410 NewOperation(url)->ReadDirectory( | 441 NewOperation(url)->ReadDirectory( |
411 url, base::Bind(&DidReadDirectory, &contents, &completed)); | 442 url, base::Bind(&DidReadDirectory, &contents, &completed)); |
412 base::MessageLoop::current()->RunUntilIdle(); | 443 base::MessageLoop::current()->RunUntilIdle(); |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
474 new TestFolder(base::StringPrintf("folder-%05d", i), | 505 new TestFolder(base::StringPrintf("folder-%05d", i), |
475 date, | 506 date, |
476 base::StringPrintf("uid%05d", i), i % 5, i % 3)); | 507 base::StringPrintf("uid%05d", i), i % 5, i % 3)); |
477 } | 508 } |
478 | 509 |
479 SetupFolders(&test_folders); | 510 SetupFolders(&test_folders); |
480 VerifyFolderDirectoryList(test_folders); | 511 VerifyFolderDirectoryList(test_folders); |
481 } | 512 } |
482 | 513 |
483 } // namespace chrome | 514 } // namespace chrome |
OLD | NEW |