Remove stray platform_file.h from chrome and components
[chromium-blink-merge.git] / chrome / browser / media_galleries / fileapi / picasa_file_util_unittest.cc
blob04bf3341c914dd85c6bec176fb8f98981c62a058
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/file_util.h"
11 #include "base/files/file_path.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/message_loop/message_loop_proxy.h"
17 #include "base/run_loop.h"
18 #include "base/strings/stringprintf.h"
19 #include "base/time/time.h"
20 #include "chrome/browser/media_galleries/fileapi/media_file_system_backend.h"
21 #include "chrome/browser/media_galleries/fileapi/media_path_filter.h"
22 #include "chrome/browser/media_galleries/fileapi/picasa_data_provider.h"
23 #include "chrome/browser/media_galleries/fileapi/picasa_file_util.h"
24 #include "chrome/browser/media_galleries/imported_media_gallery_registry.h"
25 #include "chrome/common/media_galleries/picasa_types.h"
26 #include "chrome/common/media_galleries/pmp_constants.h"
27 #include "content/public/browser/browser_thread.h"
28 #include "content/public/test/mock_special_storage_policy.h"
29 #include "content/public/test/test_browser_thread.h"
30 #include "content/public/test/test_file_system_options.h"
31 #include "testing/gtest/include/gtest/gtest.h"
32 #include "webkit/browser/fileapi/async_file_util.h"
33 #include "webkit/browser/fileapi/external_mount_points.h"
34 #include "webkit/browser/fileapi/file_system_context.h"
35 #include "webkit/browser/fileapi/file_system_operation_context.h"
36 #include "webkit/browser/fileapi/file_system_operation_runner.h"
37 #include "webkit/browser/fileapi/isolated_context.h"
38 #include "webkit/common/blob/shareable_file_reference.h"
40 using fileapi::FileSystemOperationContext;
41 using fileapi::FileSystemOperation;
42 using fileapi::FileSystemURL;
44 namespace picasa {
46 namespace {
48 base::Time::Exploded test_date_exploded = { 2013, 4, 0, 16, 0, 0, 0, 0 };
50 bool WriteJPEGHeader(const base::FilePath& path) {
51 const char kJpegHeader[] = "\xFF\xD8\xFF"; // Per HTML5 specification.
52 return base::WriteFile(path, kJpegHeader, arraysize(kJpegHeader)) != -1;
55 class TestFolder {
56 public:
57 TestFolder(const std::string& name, const base::Time& timestamp,
58 const std::string& uid, unsigned int image_files,
59 unsigned int non_image_files)
60 : name_(name),
61 timestamp_(timestamp),
62 uid_(uid),
63 image_files_(image_files),
64 non_image_files_(non_image_files),
65 folder_info_("", base::Time(), "", base::FilePath()) {
68 bool Init() {
69 if (!folder_dir_.CreateUniqueTempDir())
70 return false;
72 folder_info_ = AlbumInfo(name_, timestamp_, uid_, folder_dir_.path());
74 for (unsigned int i = 0; i < image_files_; ++i) {
75 std::string image_filename = base::StringPrintf("img%05d.jpg", i);
76 image_filenames_.insert(image_filename);
78 base::FilePath path = folder_dir_.path().AppendASCII(image_filename);
80 if (!WriteJPEGHeader(path))
81 return false;
84 for (unsigned int i = 0; i < non_image_files_; ++i) {
85 base::FilePath path = folder_dir_.path().AppendASCII(
86 base::StringPrintf("hello%05d.txt", i));
87 if (base::WriteFile(path, NULL, 0) == -1)
88 return false;
91 return true;
94 double GetVariantTimestamp() const {
95 DCHECK(!folder_dir_.path().empty());
96 base::Time variant_epoch = base::Time::FromLocalExploded(
97 picasa::kPmpVariantTimeEpoch);
99 int64 microseconds_since_epoch =
100 (folder_info_.timestamp - variant_epoch).InMicroseconds();
102 return static_cast<double>(microseconds_since_epoch) /
103 base::Time::kMicrosecondsPerDay;
106 const std::set<std::string>& image_filenames() const {
107 DCHECK(!folder_dir_.path().empty());
108 return image_filenames_;
111 const AlbumInfo& folder_info() const {
112 DCHECK(!folder_dir_.path().empty());
113 return folder_info_;
116 const base::Time& timestamp() const {
117 return timestamp_;
120 private:
121 const std::string name_;
122 const base::Time timestamp_;
123 const std::string uid_;
124 unsigned int image_files_;
125 unsigned int non_image_files_;
127 std::set<std::string> image_filenames_;
129 base::ScopedTempDir folder_dir_;
130 AlbumInfo folder_info_;
133 void ReadDirectoryTestHelperCallback(
134 base::RunLoop* run_loop,
135 FileSystemOperation::FileEntryList* contents,
136 bool* completed, base::File::Error error,
137 const FileSystemOperation::FileEntryList& file_list,
138 bool has_more) {
139 DCHECK(!*completed);
140 *completed = !has_more && error == base::File::FILE_OK;
141 *contents = file_list;
142 run_loop->Quit();
145 void ReadDirectoryTestHelper(fileapi::FileSystemOperationRunner* runner,
146 const FileSystemURL& url,
147 FileSystemOperation::FileEntryList* contents,
148 bool* completed) {
149 DCHECK(contents);
150 DCHECK(completed);
151 base::RunLoop run_loop;
152 runner->ReadDirectory(
153 url, base::Bind(&ReadDirectoryTestHelperCallback, &run_loop, contents,
154 completed));
155 run_loop.Run();
158 void SynchronouslyRunOnMediaTaskRunner(const base::Closure& closure) {
159 base::RunLoop loop;
160 MediaFileSystemBackend::MediaTaskRunner()->PostTaskAndReply(
161 FROM_HERE,
162 closure,
163 loop.QuitClosure());
164 loop.Run();
167 void CreateSnapshotFileTestHelperCallback(
168 base::RunLoop* run_loop,
169 base::File::Error* error,
170 base::FilePath* platform_path_result,
171 base::File::Error result,
172 const base::File::Info& file_info,
173 const base::FilePath& platform_path,
174 const scoped_refptr<webkit_blob::ShareableFileReference>& file_ref) {
175 DCHECK(run_loop);
176 DCHECK(error);
177 DCHECK(platform_path_result);
179 *error = result;
180 *platform_path_result = platform_path;
181 run_loop->Quit();
184 } // namespace
186 class TestPicasaFileUtil : public PicasaFileUtil {
187 public:
188 TestPicasaFileUtil(MediaPathFilter* media_path_filter,
189 PicasaDataProvider* data_provider)
190 : PicasaFileUtil(media_path_filter),
191 data_provider_(data_provider) {
193 virtual ~TestPicasaFileUtil() {}
194 private:
195 virtual PicasaDataProvider* GetDataProvider() OVERRIDE {
196 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 virtual fileapi::AsyncFileUtil*
211 GetAsyncFileUtil(fileapi::FileSystemType type) OVERRIDE {
212 if (type != fileapi::kFileSystemTypePicasa)
213 return NULL;
215 return test_file_util_.get();
218 private:
219 scoped_ptr<fileapi::AsyncFileUtil> test_file_util_;
222 class PicasaFileUtilTest : public testing::Test {
223 public:
224 PicasaFileUtilTest()
225 : io_thread_(content::BrowserThread::IO, &message_loop_) {
227 virtual ~PicasaFileUtilTest() {}
229 virtual void SetUp() OVERRIDE {
230 ASSERT_TRUE(profile_dir_.CreateUniqueTempDir());
231 ImportedMediaGalleryRegistry::GetInstance()->Initialize();
233 scoped_refptr<quota::SpecialStoragePolicy> storage_policy =
234 new content::MockSpecialStoragePolicy();
236 SynchronouslyRunOnMediaTaskRunner(base::Bind(
237 &PicasaFileUtilTest::SetUpOnMediaTaskRunner, base::Unretained(this)));
239 media_path_filter_.reset(new MediaPathFilter());
241 ScopedVector<fileapi::FileSystemBackend> additional_providers;
242 additional_providers.push_back(new TestMediaFileSystemBackend(
243 profile_dir_.path(),
244 new TestPicasaFileUtil(media_path_filter_.get(),
245 picasa_data_provider_.get())));
247 file_system_context_ = new fileapi::FileSystemContext(
248 base::MessageLoopProxy::current().get(),
249 base::MessageLoopProxy::current().get(),
250 fileapi::ExternalMountPoints::CreateRefCounted().get(),
251 storage_policy.get(),
252 NULL,
253 additional_providers.Pass(),
254 std::vector<fileapi::URLRequestAutoMountHandler>(),
255 profile_dir_.path(),
256 content::CreateAllowFileAccessOptions());
259 virtual void TearDown() OVERRIDE {
260 SynchronouslyRunOnMediaTaskRunner(
261 base::Bind(&PicasaFileUtilTest::TearDownOnMediaTaskRunner,
262 base::Unretained(this)));
265 protected:
266 void SetUpOnMediaTaskRunner() {
267 picasa_data_provider_.reset(new PicasaDataProvider(base::FilePath()));
270 void TearDownOnMediaTaskRunner() {
271 picasa_data_provider_.reset();
274 // |test_folders| must be in alphabetical order for easy verification
275 void SetupFolders(ScopedVector<TestFolder>* test_folders,
276 const std::vector<AlbumInfo>& albums,
277 const AlbumImagesMap& albums_images) {
278 std::vector<AlbumInfo> folders;
279 for (ScopedVector<TestFolder>::iterator it = test_folders->begin();
280 it != test_folders->end(); ++it) {
281 TestFolder* test_folder = *it;
282 ASSERT_TRUE(test_folder->Init());
283 folders.push_back(test_folder->folder_info());
286 PicasaDataProvider::UniquifyNames(albums,
287 &picasa_data_provider_->album_map_);
288 PicasaDataProvider::UniquifyNames(folders,
289 &picasa_data_provider_->folder_map_);
290 picasa_data_provider_->albums_images_ = albums_images;
291 picasa_data_provider_->state_ =
292 PicasaDataProvider::ALBUMS_IMAGES_FRESH_STATE;
295 void VerifyFolderDirectoryList(const ScopedVector<TestFolder>& test_folders) {
296 FileSystemOperation::FileEntryList contents;
297 FileSystemURL url = CreateURL(kPicasaDirFolders);
298 bool completed = false;
299 ReadDirectoryTestHelper(operation_runner(), url, &contents, &completed);
301 ASSERT_TRUE(completed);
302 ASSERT_EQ(test_folders.size(), contents.size());
304 for (size_t i = 0; i < contents.size(); ++i) {
305 EXPECT_TRUE(contents[i].is_directory);
307 // Because the timestamp is written out as a floating point Microsoft
308 // variant time, we only expect it to be accurate to within a second.
309 base::TimeDelta delta = test_folders[i]->folder_info().timestamp -
310 contents[i].last_modified_time;
311 EXPECT_LT(delta, base::TimeDelta::FromSeconds(1));
313 FileSystemOperation::FileEntryList folder_contents;
314 FileSystemURL folder_url = CreateURL(
315 std::string(kPicasaDirFolders) + "/" +
316 base::FilePath(contents[i].name).AsUTF8Unsafe());
317 bool folder_read_completed = false;
318 ReadDirectoryTestHelper(operation_runner(), folder_url, &folder_contents,
319 &folder_read_completed);
321 EXPECT_TRUE(folder_read_completed);
323 const std::set<std::string>& image_filenames =
324 test_folders[i]->image_filenames();
326 EXPECT_EQ(image_filenames.size(), folder_contents.size());
328 for (FileSystemOperation::FileEntryList::const_iterator file_it =
329 folder_contents.begin(); file_it != folder_contents.end();
330 ++file_it) {
331 EXPECT_EQ(1u, image_filenames.count(
332 base::FilePath(file_it->name).AsUTF8Unsafe()));
337 std::string DateToPathString(const base::Time& time) {
338 return PicasaDataProvider::DateToPathString(time);
341 void TestNonexistentDirectory(const std::string& path) {
342 FileSystemOperation::FileEntryList contents;
343 FileSystemURL url = CreateURL(path);
344 bool completed = false;
345 ReadDirectoryTestHelper(operation_runner(), url, &contents, &completed);
347 ASSERT_FALSE(completed);
350 void TestEmptyDirectory(const std::string& path) {
351 FileSystemOperation::FileEntryList contents;
352 FileSystemURL url = CreateURL(path);
353 bool completed = false;
354 ReadDirectoryTestHelper(operation_runner(), url, &contents, &completed);
356 ASSERT_TRUE(completed);
357 EXPECT_EQ(0u, contents.size());
360 FileSystemURL CreateURL(const std::string& path) const {
361 base::FilePath virtual_path =
362 ImportedMediaGalleryRegistry::GetInstance()->ImportedRoot();
363 virtual_path = virtual_path.AppendASCII("picasa");
364 virtual_path = virtual_path.AppendASCII(path);
365 return file_system_context_->CreateCrackedFileSystemURL(
366 GURL("http://www.example.com"), fileapi::kFileSystemTypePicasa,
367 virtual_path);
370 fileapi::FileSystemOperationRunner* operation_runner() const {
371 return file_system_context_->operation_runner();
374 scoped_refptr<fileapi::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<fileapi::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", 500, 500));
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 fileapi::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