Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / media_galleries / fileapi / picasa_file_util_unittest.cc
blob6b597e48ba1befeb951ca45dc5a397bc14cd6261
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
3 // found in the LICENSE file.
5 #include <set>
6 #include <string>
7 #include <vector>
9 #include "base/bind_helpers.h"
10 #include "base/files/file_path.h"
11 #include "base/files/file_util.h"
12 #include "base/files/scoped_temp_dir.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_vector.h"
15 #include "base/message_loop/message_loop.h"
16 #include "base/run_loop.h"
17 #include "base/single_thread_task_runner.h"
18 #include "base/strings/stringprintf.h"
19 #include "base/thread_task_runner_handle.h"
20 #include "base/time/time.h"
21 #include "chrome/browser/media_galleries/fileapi/media_file_system_backend.h"
22 #include "chrome/browser/media_galleries/fileapi/media_path_filter.h"
23 #include "chrome/browser/media_galleries/fileapi/picasa_data_provider.h"
24 #include "chrome/browser/media_galleries/fileapi/picasa_file_util.h"
25 #include "chrome/browser/media_galleries/imported_media_gallery_registry.h"
26 #include "chrome/common/media_galleries/picasa_types.h"
27 #include "chrome/common/media_galleries/pmp_constants.h"
28 #include "content/public/browser/browser_thread.h"
29 #include "content/public/test/mock_special_storage_policy.h"
30 #include "content/public/test/test_browser_thread.h"
31 #include "content/public/test/test_file_system_options.h"
32 #include "storage/browser/blob/shareable_file_reference.h"
33 #include "storage/browser/fileapi/async_file_util.h"
34 #include "storage/browser/fileapi/external_mount_points.h"
35 #include "storage/browser/fileapi/file_system_context.h"
36 #include "storage/browser/fileapi/file_system_operation_context.h"
37 #include "storage/browser/fileapi/file_system_operation_runner.h"
38 #include "storage/browser/fileapi/isolated_context.h"
39 #include "testing/gtest/include/gtest/gtest.h"
41 using storage::FileSystemOperationContext;
42 using storage::FileSystemOperation;
43 using storage::FileSystemURL;
45 namespace picasa {
47 namespace {
49 base::Time::Exploded test_date_exploded = { 2013, 4, 0, 16, 0, 0, 0, 0 };
51 bool WriteJPEGHeader(const base::FilePath& path) {
52 const char kJpegHeader[] = "\xFF\xD8\xFF"; // Per HTML5 specification.
53 return base::WriteFile(path, kJpegHeader, arraysize(kJpegHeader)) != -1;
56 class TestFolder {
57 public:
58 TestFolder(const std::string& name, const base::Time& timestamp,
59 const std::string& uid, unsigned int image_files,
60 unsigned int non_image_files)
61 : name_(name),
62 timestamp_(timestamp),
63 uid_(uid),
64 image_files_(image_files),
65 non_image_files_(non_image_files),
66 folder_info_("", base::Time(), "", base::FilePath()) {
69 bool Init() {
70 if (!folder_dir_.CreateUniqueTempDir())
71 return false;
73 folder_info_ = AlbumInfo(name_, timestamp_, uid_, folder_dir_.path());
75 for (unsigned int i = 0; i < image_files_; ++i) {
76 std::string image_filename = base::StringPrintf("img%05d.jpg", i);
77 image_filenames_.insert(image_filename);
79 base::FilePath path = folder_dir_.path().AppendASCII(image_filename);
81 if (!WriteJPEGHeader(path))
82 return false;
85 for (unsigned int i = 0; i < non_image_files_; ++i) {
86 base::FilePath path = folder_dir_.path().AppendASCII(
87 base::StringPrintf("hello%05d.txt", i));
88 if (base::WriteFile(path, NULL, 0) == -1)
89 return false;
92 return true;
95 double GetVariantTimestamp() const {
96 DCHECK(!folder_dir_.path().empty());
97 base::Time variant_epoch = base::Time::FromLocalExploded(
98 picasa::kPmpVariantTimeEpoch);
100 int64 microseconds_since_epoch =
101 (folder_info_.timestamp - variant_epoch).InMicroseconds();
103 return static_cast<double>(microseconds_since_epoch) /
104 base::Time::kMicrosecondsPerDay;
107 const std::set<std::string>& image_filenames() const {
108 DCHECK(!folder_dir_.path().empty());
109 return image_filenames_;
112 const AlbumInfo& folder_info() const {
113 DCHECK(!folder_dir_.path().empty());
114 return folder_info_;
117 const base::Time& timestamp() const {
118 return timestamp_;
121 private:
122 const std::string name_;
123 const base::Time timestamp_;
124 const std::string uid_;
125 unsigned int image_files_;
126 unsigned int non_image_files_;
128 std::set<std::string> image_filenames_;
130 base::ScopedTempDir folder_dir_;
131 AlbumInfo folder_info_;
134 void ReadDirectoryTestHelperCallback(
135 base::RunLoop* run_loop,
136 FileSystemOperation::FileEntryList* contents,
137 bool* completed, base::File::Error error,
138 const FileSystemOperation::FileEntryList& file_list,
139 bool has_more) {
140 DCHECK(!*completed);
141 *completed = !has_more && error == base::File::FILE_OK;
142 *contents = file_list;
143 run_loop->Quit();
146 void ReadDirectoryTestHelper(storage::FileSystemOperationRunner* runner,
147 const FileSystemURL& url,
148 FileSystemOperation::FileEntryList* contents,
149 bool* completed) {
150 DCHECK(contents);
151 DCHECK(completed);
152 base::RunLoop run_loop;
153 runner->ReadDirectory(
154 url, base::Bind(&ReadDirectoryTestHelperCallback, &run_loop, contents,
155 completed));
156 run_loop.Run();
159 void SynchronouslyRunOnMediaTaskRunner(const base::Closure& closure) {
160 base::RunLoop loop;
161 MediaFileSystemBackend::MediaTaskRunner()->PostTaskAndReply(
162 FROM_HERE,
163 closure,
164 loop.QuitClosure());
165 loop.Run();
168 void CreateSnapshotFileTestHelperCallback(
169 base::RunLoop* run_loop,
170 base::File::Error* error,
171 base::FilePath* platform_path_result,
172 base::File::Error result,
173 const base::File::Info& file_info,
174 const base::FilePath& platform_path,
175 const scoped_refptr<storage::ShareableFileReference>& file_ref) {
176 DCHECK(run_loop);
177 DCHECK(error);
178 DCHECK(platform_path_result);
180 *error = result;
181 *platform_path_result = platform_path;
182 run_loop->Quit();
185 } // namespace
187 class TestPicasaFileUtil : public PicasaFileUtil {
188 public:
189 TestPicasaFileUtil(MediaPathFilter* media_path_filter,
190 PicasaDataProvider* data_provider)
191 : PicasaFileUtil(media_path_filter),
192 data_provider_(data_provider) {
194 ~TestPicasaFileUtil() override {}
196 private:
197 PicasaDataProvider* GetDataProvider() override { return data_provider_; }
199 PicasaDataProvider* data_provider_;
202 class TestMediaFileSystemBackend : public MediaFileSystemBackend {
203 public:
204 TestMediaFileSystemBackend(const base::FilePath& profile_path,
205 PicasaFileUtil* picasa_file_util)
206 : MediaFileSystemBackend(profile_path,
207 MediaFileSystemBackend::MediaTaskRunner().get()),
208 test_file_util_(picasa_file_util) {}
210 storage::AsyncFileUtil* GetAsyncFileUtil(
211 storage::FileSystemType type) override {
212 if (type != storage::kFileSystemTypePicasa)
213 return NULL;
215 return test_file_util_.get();
218 private:
219 scoped_ptr<storage::AsyncFileUtil> test_file_util_;
222 class PicasaFileUtilTest : public testing::Test {
223 public:
224 PicasaFileUtilTest()
225 : io_thread_(content::BrowserThread::IO, &message_loop_) {
228 void SetUp() override {
229 ASSERT_TRUE(profile_dir_.CreateUniqueTempDir());
230 ImportedMediaGalleryRegistry::GetInstance()->Initialize();
232 scoped_refptr<storage::SpecialStoragePolicy> storage_policy =
233 new content::MockSpecialStoragePolicy();
235 SynchronouslyRunOnMediaTaskRunner(base::Bind(
236 &PicasaFileUtilTest::SetUpOnMediaTaskRunner, base::Unretained(this)));
238 media_path_filter_.reset(new MediaPathFilter());
240 ScopedVector<storage::FileSystemBackend> additional_providers;
241 additional_providers.push_back(new TestMediaFileSystemBackend(
242 profile_dir_.path(),
243 new TestPicasaFileUtil(media_path_filter_.get(),
244 picasa_data_provider_.get())));
246 file_system_context_ = new storage::FileSystemContext(
247 base::ThreadTaskRunnerHandle::Get().get(),
248 base::ThreadTaskRunnerHandle::Get().get(),
249 storage::ExternalMountPoints::CreateRefCounted().get(),
250 storage_policy.get(),
251 NULL,
252 additional_providers.Pass(),
253 std::vector<storage::URLRequestAutoMountHandler>(),
254 profile_dir_.path(),
255 content::CreateAllowFileAccessOptions());
258 void TearDown() override {
259 SynchronouslyRunOnMediaTaskRunner(
260 base::Bind(&PicasaFileUtilTest::TearDownOnMediaTaskRunner,
261 base::Unretained(this)));
264 protected:
265 void SetUpOnMediaTaskRunner() {
266 picasa_data_provider_.reset(new PicasaDataProvider(base::FilePath()));
269 void TearDownOnMediaTaskRunner() {
270 picasa_data_provider_.reset();
273 // |test_folders| must be in alphabetical order for easy verification
274 void SetupFolders(ScopedVector<TestFolder>* test_folders,
275 const std::vector<AlbumInfo>& albums,
276 const AlbumImagesMap& albums_images) {
277 std::vector<AlbumInfo> folders;
278 for (ScopedVector<TestFolder>::iterator it = test_folders->begin();
279 it != test_folders->end(); ++it) {
280 TestFolder* test_folder = *it;
281 ASSERT_TRUE(test_folder->Init());
282 folders.push_back(test_folder->folder_info());
285 PicasaDataProvider::UniquifyNames(albums,
286 &picasa_data_provider_->album_map_);
287 PicasaDataProvider::UniquifyNames(folders,
288 &picasa_data_provider_->folder_map_);
289 picasa_data_provider_->albums_images_ = albums_images;
290 picasa_data_provider_->state_ =
291 PicasaDataProvider::ALBUMS_IMAGES_FRESH_STATE;
294 void VerifyFolderDirectoryList(const ScopedVector<TestFolder>& test_folders) {
295 FileSystemOperation::FileEntryList contents;
296 FileSystemURL url = CreateURL(kPicasaDirFolders);
297 bool completed = false;
298 ReadDirectoryTestHelper(operation_runner(), url, &contents, &completed);
300 ASSERT_TRUE(completed);
301 ASSERT_EQ(test_folders.size(), contents.size());
303 for (size_t i = 0; i < contents.size(); ++i) {
304 EXPECT_TRUE(contents[i].is_directory);
306 // Because the timestamp is written out as a floating point Microsoft
307 // variant time, we only expect it to be accurate to within a second.
308 base::TimeDelta delta = test_folders[i]->folder_info().timestamp -
309 contents[i].last_modified_time;
310 EXPECT_LT(delta, base::TimeDelta::FromSeconds(1));
312 FileSystemOperation::FileEntryList folder_contents;
313 FileSystemURL folder_url = CreateURL(
314 std::string(kPicasaDirFolders) + "/" +
315 base::FilePath(contents[i].name).AsUTF8Unsafe());
316 bool folder_read_completed = false;
317 ReadDirectoryTestHelper(operation_runner(), folder_url, &folder_contents,
318 &folder_read_completed);
320 EXPECT_TRUE(folder_read_completed);
322 const std::set<std::string>& image_filenames =
323 test_folders[i]->image_filenames();
325 EXPECT_EQ(image_filenames.size(), folder_contents.size());
327 for (FileSystemOperation::FileEntryList::const_iterator file_it =
328 folder_contents.begin(); file_it != folder_contents.end();
329 ++file_it) {
330 EXPECT_EQ(1u, image_filenames.count(
331 base::FilePath(file_it->name).AsUTF8Unsafe()));
336 std::string DateToPathString(const base::Time& time) {
337 return PicasaDataProvider::DateToPathString(time);
340 void TestNonexistentDirectory(const std::string& path) {
341 FileSystemOperation::FileEntryList contents;
342 FileSystemURL url = CreateURL(path);
343 bool completed = false;
344 ReadDirectoryTestHelper(operation_runner(), url, &contents, &completed);
346 ASSERT_FALSE(completed);
349 void TestEmptyDirectory(const std::string& path) {
350 FileSystemOperation::FileEntryList contents;
351 FileSystemURL url = CreateURL(path);
352 bool completed = false;
353 ReadDirectoryTestHelper(operation_runner(), url, &contents, &completed);
355 ASSERT_TRUE(completed);
356 EXPECT_EQ(0u, contents.size());
359 FileSystemURL CreateURL(const std::string& path) const {
360 base::FilePath virtual_path =
361 ImportedMediaGalleryRegistry::GetInstance()->ImportedRoot();
362 virtual_path = virtual_path.AppendASCII("picasa");
363 virtual_path = virtual_path.AppendASCII(path);
364 return file_system_context_->CreateCrackedFileSystemURL(
365 GURL("http://www.example.com"),
366 storage::kFileSystemTypePicasa,
367 virtual_path);
370 storage::FileSystemOperationRunner* operation_runner() const {
371 return file_system_context_->operation_runner();
374 scoped_refptr<storage::FileSystemContext> file_system_context() const {
375 return file_system_context_;
378 private:
379 base::MessageLoop message_loop_;
380 content::TestBrowserThread io_thread_;
382 base::ScopedTempDir profile_dir_;
384 scoped_refptr<storage::FileSystemContext> file_system_context_;
385 scoped_ptr<PicasaDataProvider> picasa_data_provider_;
386 scoped_ptr<MediaPathFilter> media_path_filter_;
388 DISALLOW_COPY_AND_ASSIGN(PicasaFileUtilTest);
391 TEST_F(PicasaFileUtilTest, DateFormat) {
392 base::Time::Exploded exploded_shortmonth = { 2013, 4, 0, 16, 0, 0, 0, 0 };
393 base::Time shortmonth = base::Time::FromLocalExploded(exploded_shortmonth);
395 base::Time::Exploded exploded_shortday = { 2013, 11, 0, 3, 0, 0, 0, 0 };
396 base::Time shortday = base::Time::FromLocalExploded(exploded_shortday);
398 EXPECT_EQ("2013-04-16", DateToPathString(shortmonth));
399 EXPECT_EQ("2013-11-03", DateToPathString(shortday));
402 TEST_F(PicasaFileUtilTest, NameDeduplication) {
403 ScopedVector<TestFolder> test_folders;
404 std::vector<std::string> expected_names;
406 base::Time test_date = base::Time::FromLocalExploded(test_date_exploded);
407 base::Time test_date_2 = test_date - base::TimeDelta::FromDays(1);
409 std::string test_date_string = DateToPathString(test_date);
410 std::string test_date_2_string = DateToPathString(test_date_2);
412 test_folders.push_back(
413 new TestFolder("diff_date", test_date_2, "uuid3", 0, 0));
414 expected_names.push_back("diff_date " + test_date_2_string);
416 test_folders.push_back(
417 new TestFolder("diff_date", test_date, "uuid2", 0, 0));
418 expected_names.push_back("diff_date " + test_date_string);
420 test_folders.push_back(
421 new TestFolder("duplicate", test_date, "uuid4", 0, 0));
422 expected_names.push_back("duplicate " + test_date_string + " (1)");
424 test_folders.push_back(
425 new TestFolder("duplicate", test_date, "uuid5", 0, 0));
426 expected_names.push_back("duplicate " + test_date_string + " (2)");
428 test_folders.push_back(
429 new TestFolder("unique_name", test_date, "uuid1", 0, 0));
430 expected_names.push_back("unique_name " + test_date_string);
432 SetupFolders(&test_folders, std::vector<AlbumInfo>(), AlbumImagesMap());
434 FileSystemOperation::FileEntryList contents;
435 FileSystemURL url = CreateURL(kPicasaDirFolders);
436 bool completed = false;
437 ReadDirectoryTestHelper(operation_runner(), url, &contents, &completed);
439 ASSERT_TRUE(completed);
440 ASSERT_EQ(expected_names.size(), contents.size());
441 for (size_t i = 0; i < contents.size(); ++i) {
442 EXPECT_EQ(expected_names[i],
443 base::FilePath(contents[i].name).AsUTF8Unsafe());
444 EXPECT_EQ(test_folders[i]->timestamp(), contents[i].last_modified_time);
445 EXPECT_TRUE(contents[i].is_directory);
449 TEST_F(PicasaFileUtilTest, RootFolders) {
450 ScopedVector<TestFolder> empty_folders_list;
451 SetupFolders(&empty_folders_list, std::vector<AlbumInfo>(), AlbumImagesMap());
453 FileSystemOperation::FileEntryList contents;
454 FileSystemURL url = CreateURL("");
455 bool completed = false;
456 ReadDirectoryTestHelper(operation_runner(), url, &contents, &completed);
458 ASSERT_TRUE(completed);
459 ASSERT_EQ(2u, contents.size());
461 EXPECT_TRUE(contents.front().is_directory);
462 EXPECT_TRUE(contents.back().is_directory);
464 EXPECT_EQ(0, contents.front().size);
465 EXPECT_EQ(0, contents.back().size);
467 EXPECT_EQ(FILE_PATH_LITERAL("albums"), contents.front().name);
468 EXPECT_EQ(FILE_PATH_LITERAL("folders"), contents.back().name);
471 TEST_F(PicasaFileUtilTest, NonexistentFolder) {
472 ScopedVector<TestFolder> empty_folders_list;
473 SetupFolders(&empty_folders_list, std::vector<AlbumInfo>(), AlbumImagesMap());
475 TestNonexistentDirectory(std::string(kPicasaDirFolders) + "/foo");
476 TestNonexistentDirectory(std::string(kPicasaDirFolders) + "/foo/bar");
477 TestNonexistentDirectory(std::string(kPicasaDirFolders) + "/foo/bar/baz");
480 TEST_F(PicasaFileUtilTest, FolderContentsTrivial) {
481 ScopedVector<TestFolder> test_folders;
482 base::Time test_date = base::Time::FromLocalExploded(test_date_exploded);
484 test_folders.push_back(
485 new TestFolder("folder-1-empty", test_date, "uid-empty", 0, 0));
486 test_folders.push_back(
487 new TestFolder("folder-2-images", test_date, "uid-images", 5, 0));
488 test_folders.push_back(
489 new TestFolder("folder-3-nonimages", test_date, "uid-nonimages", 0, 5));
490 test_folders.push_back(
491 new TestFolder("folder-4-both", test_date, "uid-both", 5, 5));
493 SetupFolders(&test_folders, std::vector<AlbumInfo>(), AlbumImagesMap());
494 VerifyFolderDirectoryList(test_folders);
497 TEST_F(PicasaFileUtilTest, FolderWithManyFiles) {
498 ScopedVector<TestFolder> test_folders;
499 base::Time test_date = base::Time::FromLocalExploded(test_date_exploded);
501 test_folders.push_back(
502 new TestFolder("folder-many-files", test_date, "uid-both", 50, 50));
504 SetupFolders(&test_folders, std::vector<AlbumInfo>(), AlbumImagesMap());
505 VerifyFolderDirectoryList(test_folders);
508 TEST_F(PicasaFileUtilTest, ManyFolders) {
509 ScopedVector<TestFolder> test_folders;
510 base::Time test_date = base::Time::FromLocalExploded(test_date_exploded);
512 for (unsigned int i = 0; i < 50; ++i) {
513 base::Time date = test_date - base::TimeDelta::FromDays(i);
515 test_folders.push_back(
516 new TestFolder(base::StringPrintf("folder-%05d", i),
517 date,
518 base::StringPrintf("uid%05d", i), i % 5, i % 3));
521 SetupFolders(&test_folders, std::vector<AlbumInfo>(), AlbumImagesMap());
522 VerifyFolderDirectoryList(test_folders);
525 TEST_F(PicasaFileUtilTest, AlbumExistence) {
526 ScopedVector<TestFolder> test_folders;
527 base::Time test_date = base::Time::FromLocalExploded(test_date_exploded);
529 std::vector<AlbumInfo> albums;
530 AlbumInfo info;
531 info.name = "albumname";
532 info.uid = "albumuid";
533 info.timestamp = test_date;
534 albums.push_back(info);
536 AlbumImagesMap albums_images;
537 albums_images[info.uid] = AlbumImages();
539 SetupFolders(&test_folders, albums, albums_images);
541 TestEmptyDirectory(std::string(kPicasaDirAlbums) + "/albumname 2013-04-16");
542 TestNonexistentDirectory(std::string(kPicasaDirAlbums) +
543 "/albumname 2013-04-16/toodeep");
544 TestNonexistentDirectory(std::string(kPicasaDirAlbums) + "/wrongname");
547 TEST_F(PicasaFileUtilTest, AlbumContents) {
548 ScopedVector<TestFolder> test_folders;
549 base::Time test_date = base::Time::FromLocalExploded(test_date_exploded);
551 std::vector<AlbumInfo> albums;
552 AlbumInfo info;
553 info.name = "albumname";
554 info.uid = "albumuid";
555 info.timestamp = test_date;
556 albums.push_back(info);
558 base::ScopedTempDir temp_dir;
559 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
561 base::FilePath image_path = temp_dir.path().AppendASCII("img.jpg");
562 ASSERT_TRUE(WriteJPEGHeader(image_path));
564 AlbumImagesMap albums_images;
565 albums_images[info.uid] = AlbumImages();
566 albums_images[info.uid]["mapped_name.jpg"] = image_path;
568 SetupFolders(&test_folders, albums, albums_images);
570 FileSystemOperation::FileEntryList contents;
571 FileSystemURL url =
572 CreateURL(std::string(kPicasaDirAlbums) + "/albumname 2013-04-16");
573 bool completed = false;
574 ReadDirectoryTestHelper(operation_runner(), url, &contents, &completed);
576 ASSERT_TRUE(completed);
577 EXPECT_EQ(1u, contents.size());
578 EXPECT_EQ("mapped_name.jpg",
579 base::FilePath(contents.begin()->name).AsUTF8Unsafe());
580 EXPECT_FALSE(contents.begin()->is_directory);
582 // Create a snapshot file to verify the file path.
583 base::RunLoop loop;
584 base::File::Error error;
585 base::FilePath platform_path_result;
586 storage::FileSystemOperationRunner::SnapshotFileCallback snapshot_callback =
587 base::Bind(&CreateSnapshotFileTestHelperCallback,
588 &loop,
589 &error,
590 &platform_path_result);
591 operation_runner()->CreateSnapshotFile(
592 CreateURL(std::string(kPicasaDirAlbums) +
593 "/albumname 2013-04-16/mapped_name.jpg"),
594 snapshot_callback);
595 loop.Run();
596 EXPECT_EQ(base::File::FILE_OK, error);
597 EXPECT_EQ(image_path, platform_path_result);
600 } // namespace picasa