- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / media_galleries / fileapi / picasa_file_util_unittest.cc
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.
4
5 #include <set>
6 #include <string>
7 #include <vector>
8
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/platform_file.h"
18 #include "base/run_loop.h"
19 #include "base/strings/stringprintf.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/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/test_browser_thread.h"
29 #include "content/public/test/test_file_system_options.h"
30 #include "testing/gtest/include/gtest/gtest.h"
31 #include "webkit/browser/fileapi/async_file_util.h"
32 #include "webkit/browser/fileapi/external_mount_points.h"
33 #include "webkit/browser/fileapi/file_system_context.h"
34 #include "webkit/browser/fileapi/file_system_operation_context.h"
35 #include "webkit/browser/fileapi/file_system_operation_runner.h"
36 #include "webkit/browser/fileapi/isolated_context.h"
37 #include "webkit/browser/quota/mock_special_storage_policy.h"
38 #include "webkit/common/blob/shareable_file_reference.h"
39
40 using fileapi::FileSystemOperationContext;
41 using fileapi::FileSystemOperation;
42 using fileapi::FileSystemURL;
43
44 namespace picasa {
45
46 namespace {
47
48 base::Time::Exploded test_date_exploded = { 2013, 4, 0, 16, 0, 0, 0, 0 };
49
50 bool WriteJPEGHeader(const base::FilePath& path) {
51   const char kJpegHeader[] = "\xFF\xD8\xFF";  // Per HTML5 specification.
52   return file_util::WriteFile(path, kJpegHeader, arraysize(kJpegHeader)) != -1;
53 }
54
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()) {
66   }
67
68   bool Init() {
69     if (!folder_dir_.CreateUniqueTempDir())
70       return false;
71
72     folder_info_ = AlbumInfo(name_, timestamp_, uid_, folder_dir_.path());
73
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);
77
78       base::FilePath path = folder_dir_.path().AppendASCII(image_filename);
79
80       if (!WriteJPEGHeader(path))
81         return false;
82     }
83
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 (file_util::WriteFile(path, NULL, 0) == -1)
88         return false;
89     }
90
91     return true;
92   }
93
94   double GetVariantTimestamp() const {
95     DCHECK(!folder_dir_.path().empty());
96     base::Time variant_epoch = base::Time::FromLocalExploded(
97         picasa::kPmpVariantTimeEpoch);
98
99     int64 microseconds_since_epoch =
100         (folder_info_.timestamp - variant_epoch).InMicroseconds();
101
102     return static_cast<double>(microseconds_since_epoch) /
103                                base::Time::kMicrosecondsPerDay;
104   }
105
106   const std::set<std::string>& image_filenames() const {
107     DCHECK(!folder_dir_.path().empty());
108     return image_filenames_;
109   }
110
111   const AlbumInfo& folder_info() const {
112     DCHECK(!folder_dir_.path().empty());
113     return folder_info_;
114   }
115
116   const base::Time& timestamp() const {
117     return timestamp_;
118   }
119
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_;
126
127   std::set<std::string> image_filenames_;
128
129   base::ScopedTempDir folder_dir_;
130   AlbumInfo folder_info_;
131 };
132
133 void ReadDirectoryTestHelperCallback(
134     base::RunLoop* run_loop,
135     FileSystemOperation::FileEntryList* contents,
136     bool* completed, base::PlatformFileError error,
137     const FileSystemOperation::FileEntryList& file_list,
138     bool has_more) {
139   DCHECK(!*completed);
140   *completed = !has_more && error == base::PLATFORM_FILE_OK;
141   *contents = file_list;
142   run_loop->Quit();
143 }
144
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();
156 }
157
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();
165 }
166
167 void CreateSnapshotFileTestHelperCallback(
168     base::RunLoop* run_loop,
169     base::PlatformFileError* error,
170     base::FilePath* platform_path_result,
171     base::PlatformFileError result,
172     const base::PlatformFileInfo& 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);
178
179   *error = result;
180   *platform_path_result = platform_path;
181   run_loop->Quit();
182 }
183
184 }  // namespace
185
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) {
192   }
193   virtual ~TestPicasaFileUtil() {}
194  private:
195   virtual PicasaDataProvider* GetDataProvider() OVERRIDE {
196     return data_provider_;
197   }
198
199   PicasaDataProvider* data_provider_;
200 };
201
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) {}
209
210   virtual fileapi::AsyncFileUtil*
211   GetAsyncFileUtil(fileapi::FileSystemType type) OVERRIDE {
212     if (type != fileapi::kFileSystemTypePicasa)
213       return NULL;
214
215     return test_file_util_.get();
216   }
217
218  private:
219   scoped_ptr<fileapi::AsyncFileUtil> test_file_util_;
220 };
221
222 class PicasaFileUtilTest : public testing::Test {
223  public:
224   PicasaFileUtilTest()
225       : io_thread_(content::BrowserThread::IO, &message_loop_) {
226   }
227   virtual ~PicasaFileUtilTest() {}
228
229   virtual void SetUp() OVERRIDE {
230     ASSERT_TRUE(profile_dir_.CreateUniqueTempDir());
231
232     scoped_refptr<quota::SpecialStoragePolicy> storage_policy =
233         new quota::MockSpecialStoragePolicy();
234
235     SynchronouslyRunOnMediaTaskRunner(base::Bind(
236         &PicasaFileUtilTest::SetUpOnMediaTaskRunner, base::Unretained(this)));
237
238     media_path_filter_.reset(new MediaPathFilter());
239
240     ScopedVector<fileapi::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())));
245
246     file_system_context_ = new fileapi::FileSystemContext(
247         base::MessageLoopProxy::current().get(),
248         base::MessageLoopProxy::current().get(),
249         fileapi::ExternalMountPoints::CreateRefCounted().get(),
250         storage_policy.get(),
251         NULL,
252         additional_providers.Pass(),
253         profile_dir_.path(),
254         fileapi::CreateAllowFileAccessOptions());
255   }
256
257   virtual void TearDown() OVERRIDE {
258     SynchronouslyRunOnMediaTaskRunner(
259         base::Bind(&PicasaFileUtilTest::TearDownOnMediaTaskRunner,
260                    base::Unretained(this)));
261   }
262
263  protected:
264   void SetUpOnMediaTaskRunner() {
265     picasa_data_provider_.reset(new PicasaDataProvider(base::FilePath()));
266   }
267
268   void TearDownOnMediaTaskRunner() {
269     picasa_data_provider_.reset();
270   }
271
272   // |test_folders| must be in alphabetical order for easy verification
273   void SetupFolders(ScopedVector<TestFolder>* test_folders,
274                     const std::vector<AlbumInfo>& albums,
275                     const AlbumImagesMap& albums_images) {
276     std::vector<AlbumInfo> folders;
277     for (ScopedVector<TestFolder>::iterator it = test_folders->begin();
278         it != test_folders->end(); ++it) {
279       TestFolder* test_folder = *it;
280       ASSERT_TRUE(test_folder->Init());
281       folders.push_back(test_folder->folder_info());
282     }
283
284     PicasaDataProvider::UniquifyNames(albums,
285                                       &picasa_data_provider_->album_map_);
286     PicasaDataProvider::UniquifyNames(folders,
287                                       &picasa_data_provider_->folder_map_);
288     picasa_data_provider_->albums_images_ = albums_images;
289     picasa_data_provider_->state_ =
290         PicasaDataProvider::ALBUMS_IMAGES_FRESH_STATE;
291   }
292
293   void VerifyFolderDirectoryList(const ScopedVector<TestFolder>& test_folders) {
294     FileSystemOperation::FileEntryList contents;
295     FileSystemURL url = CreateURL(kPicasaDirFolders);
296     bool completed = false;
297     ReadDirectoryTestHelper(operation_runner(), url, &contents, &completed);
298
299     ASSERT_TRUE(completed);
300     ASSERT_EQ(test_folders.size(), contents.size());
301
302     for (size_t i = 0; i < contents.size(); ++i) {
303       EXPECT_TRUE(contents[i].is_directory);
304
305       // Because the timestamp is written out as a floating point Microsoft
306       // variant time, we only expect it to be accurate to within a second.
307       base::TimeDelta delta = test_folders[i]->folder_info().timestamp -
308                               contents[i].last_modified_time;
309       EXPECT_LT(delta, base::TimeDelta::FromSeconds(1));
310
311       FileSystemOperation::FileEntryList folder_contents;
312       FileSystemURL folder_url = CreateURL(
313           std::string(kPicasaDirFolders) + "/" +
314           base::FilePath(contents[i].name).AsUTF8Unsafe());
315       bool folder_read_completed = false;
316       ReadDirectoryTestHelper(operation_runner(), folder_url, &folder_contents,
317                               &folder_read_completed);
318
319       EXPECT_TRUE(folder_read_completed);
320
321       const std::set<std::string>& image_filenames =
322           test_folders[i]->image_filenames();
323
324       EXPECT_EQ(image_filenames.size(), folder_contents.size());
325
326       for (FileSystemOperation::FileEntryList::const_iterator file_it =
327                folder_contents.begin(); file_it != folder_contents.end();
328            ++file_it) {
329         EXPECT_EQ(1u, image_filenames.count(
330             base::FilePath(file_it->name).AsUTF8Unsafe()));
331       }
332     }
333   }
334
335   std::string DateToPathString(const base::Time& time) {
336     return PicasaDataProvider::DateToPathString(time);
337   }
338
339   void TestNonexistentDirectory(const std::string& path) {
340     FileSystemOperation::FileEntryList contents;
341     FileSystemURL url = CreateURL(path);
342     bool completed = false;
343     ReadDirectoryTestHelper(operation_runner(), url, &contents, &completed);
344
345     ASSERT_FALSE(completed);
346   }
347
348   void TestEmptyDirectory(const std::string& path) {
349     FileSystemOperation::FileEntryList contents;
350     FileSystemURL url = CreateURL(path);
351     bool completed = false;
352     ReadDirectoryTestHelper(operation_runner(), url, &contents, &completed);
353
354     ASSERT_TRUE(completed);
355     EXPECT_EQ(0u, contents.size());
356   }
357
358   FileSystemURL CreateURL(const std::string& virtual_path) const {
359     return file_system_context_->CreateCrackedFileSystemURL(
360         GURL("http://www.example.com"), fileapi::kFileSystemTypePicasa,
361         base::FilePath::FromUTF8Unsafe(virtual_path));
362   }
363
364   fileapi::FileSystemOperationRunner* operation_runner() const {
365     return file_system_context_->operation_runner();
366   }
367
368   scoped_refptr<fileapi::FileSystemContext> file_system_context() const {
369     return file_system_context_;
370   }
371
372  private:
373   base::MessageLoop message_loop_;
374   content::TestBrowserThread io_thread_;
375
376   base::ScopedTempDir profile_dir_;
377
378   scoped_refptr<fileapi::FileSystemContext> file_system_context_;
379   scoped_ptr<PicasaDataProvider> picasa_data_provider_;
380   scoped_ptr<MediaPathFilter> media_path_filter_;
381
382   DISALLOW_COPY_AND_ASSIGN(PicasaFileUtilTest);
383 };
384
385 TEST_F(PicasaFileUtilTest, DateFormat) {
386   base::Time::Exploded exploded_shortmonth = { 2013, 4, 0, 16, 0, 0, 0, 0 };
387   base::Time shortmonth = base::Time::FromLocalExploded(exploded_shortmonth);
388
389   base::Time::Exploded exploded_shortday = { 2013, 11, 0, 3, 0, 0, 0, 0 };
390   base::Time shortday = base::Time::FromLocalExploded(exploded_shortday);
391
392   EXPECT_EQ("2013-04-16", DateToPathString(shortmonth));
393   EXPECT_EQ("2013-11-03", DateToPathString(shortday));
394 }
395
396 TEST_F(PicasaFileUtilTest, NameDeduplication) {
397   ScopedVector<TestFolder> test_folders;
398   std::vector<std::string> expected_names;
399
400   base::Time test_date = base::Time::FromLocalExploded(test_date_exploded);
401   base::Time test_date_2 = test_date - base::TimeDelta::FromDays(1);
402
403   std::string test_date_string = DateToPathString(test_date);
404   std::string test_date_2_string = DateToPathString(test_date_2);
405
406   test_folders.push_back(
407       new TestFolder("diff_date", test_date_2, "uuid3", 0, 0));
408   expected_names.push_back("diff_date " + test_date_2_string);
409
410   test_folders.push_back(
411       new TestFolder("diff_date", test_date, "uuid2", 0, 0));
412   expected_names.push_back("diff_date " + test_date_string);
413
414   test_folders.push_back(
415       new TestFolder("duplicate", test_date, "uuid4", 0, 0));
416   expected_names.push_back("duplicate " + test_date_string + " (1)");
417
418   test_folders.push_back(
419       new TestFolder("duplicate", test_date, "uuid5", 0, 0));
420   expected_names.push_back("duplicate " + test_date_string + " (2)");
421
422   test_folders.push_back(
423       new TestFolder("unique_name", test_date, "uuid1", 0, 0));
424   expected_names.push_back("unique_name " + test_date_string);
425
426   SetupFolders(&test_folders, std::vector<AlbumInfo>(), AlbumImagesMap());
427
428   FileSystemOperation::FileEntryList contents;
429   FileSystemURL url = CreateURL(kPicasaDirFolders);
430   bool completed = false;
431   ReadDirectoryTestHelper(operation_runner(), url, &contents, &completed);
432
433   ASSERT_TRUE(completed);
434   ASSERT_EQ(expected_names.size(), contents.size());
435   for (size_t i = 0; i < contents.size(); ++i) {
436     EXPECT_EQ(expected_names[i],
437               base::FilePath(contents[i].name).AsUTF8Unsafe());
438     EXPECT_EQ(test_folders[i]->timestamp(), contents[i].last_modified_time);
439     EXPECT_TRUE(contents[i].is_directory);
440   }
441 }
442
443 TEST_F(PicasaFileUtilTest, RootFolders) {
444   ScopedVector<TestFolder> empty_folders_list;
445   SetupFolders(&empty_folders_list, std::vector<AlbumInfo>(), AlbumImagesMap());
446
447   FileSystemOperation::FileEntryList contents;
448   FileSystemURL url = CreateURL("");
449   bool completed = false;
450   ReadDirectoryTestHelper(operation_runner(), url, &contents, &completed);
451
452   ASSERT_TRUE(completed);
453   ASSERT_EQ(2u, contents.size());
454
455   EXPECT_TRUE(contents.front().is_directory);
456   EXPECT_TRUE(contents.back().is_directory);
457
458   EXPECT_EQ(0, contents.front().size);
459   EXPECT_EQ(0, contents.back().size);
460
461   EXPECT_EQ(FILE_PATH_LITERAL("albums"), contents.front().name);
462   EXPECT_EQ(FILE_PATH_LITERAL("folders"), contents.back().name);
463 }
464
465 TEST_F(PicasaFileUtilTest, NonexistentFolder) {
466   ScopedVector<TestFolder> empty_folders_list;
467   SetupFolders(&empty_folders_list, std::vector<AlbumInfo>(), AlbumImagesMap());
468
469   TestNonexistentDirectory(std::string(kPicasaDirFolders) + "/foo");
470   TestNonexistentDirectory(std::string(kPicasaDirFolders) + "/foo/bar");
471   TestNonexistentDirectory(std::string(kPicasaDirFolders) + "/foo/bar/baz");
472 }
473
474 TEST_F(PicasaFileUtilTest, FolderContentsTrivial) {
475   ScopedVector<TestFolder> test_folders;
476   base::Time test_date = base::Time::FromLocalExploded(test_date_exploded);
477
478   test_folders.push_back(
479       new TestFolder("folder-1-empty", test_date, "uid-empty", 0, 0));
480   test_folders.push_back(
481       new TestFolder("folder-2-images", test_date, "uid-images", 5, 0));
482   test_folders.push_back(
483       new TestFolder("folder-3-nonimages", test_date, "uid-nonimages", 0, 5));
484   test_folders.push_back(
485       new TestFolder("folder-4-both", test_date, "uid-both", 5, 5));
486
487   SetupFolders(&test_folders, std::vector<AlbumInfo>(), AlbumImagesMap());
488   VerifyFolderDirectoryList(test_folders);
489 }
490
491 TEST_F(PicasaFileUtilTest, FolderWithManyFiles) {
492   ScopedVector<TestFolder> test_folders;
493   base::Time test_date = base::Time::FromLocalExploded(test_date_exploded);
494
495   test_folders.push_back(
496       new TestFolder("folder-many-files", test_date, "uid-both", 500, 500));
497
498   SetupFolders(&test_folders, std::vector<AlbumInfo>(), AlbumImagesMap());
499   VerifyFolderDirectoryList(test_folders);
500 }
501
502 TEST_F(PicasaFileUtilTest, ManyFolders) {
503   ScopedVector<TestFolder> test_folders;
504   base::Time test_date = base::Time::FromLocalExploded(test_date_exploded);
505
506   // TODO(tommycli): Turn number of test folders back up to 50 (or more) once
507   // https://codereview.chromium.org/15479003/ lands.
508   for (unsigned int i = 0; i < 25; ++i) {
509     base::Time date = test_date - base::TimeDelta::FromDays(i);
510
511     test_folders.push_back(
512         new TestFolder(base::StringPrintf("folder-%05d", i),
513                        date,
514                        base::StringPrintf("uid%05d", i), i % 5, i % 3));
515   }
516
517   SetupFolders(&test_folders, std::vector<AlbumInfo>(), AlbumImagesMap());
518   VerifyFolderDirectoryList(test_folders);
519 }
520
521 TEST_F(PicasaFileUtilTest, AlbumExistence) {
522   ScopedVector<TestFolder> test_folders;
523   base::Time test_date = base::Time::FromLocalExploded(test_date_exploded);
524
525   std::vector<AlbumInfo> albums;
526   AlbumInfo info;
527   info.name = "albumname";
528   info.uid = "albumuid";
529   info.timestamp = test_date;
530   albums.push_back(info);
531
532   AlbumImagesMap albums_images;
533   albums_images[info.uid] = AlbumImages();
534
535   SetupFolders(&test_folders, albums, albums_images);
536
537   TestEmptyDirectory(std::string(kPicasaDirAlbums) + "/albumname 2013-04-16");
538   TestNonexistentDirectory(std::string(kPicasaDirAlbums) +
539                            "/albumname 2013-04-16/toodeep");
540   TestNonexistentDirectory(std::string(kPicasaDirAlbums) + "/wrongname");
541 }
542
543 TEST_F(PicasaFileUtilTest, AlbumContents) {
544   ScopedVector<TestFolder> test_folders;
545   base::Time test_date = base::Time::FromLocalExploded(test_date_exploded);
546
547   std::vector<AlbumInfo> albums;
548   AlbumInfo info;
549   info.name = "albumname";
550   info.uid = "albumuid";
551   info.timestamp = test_date;
552   albums.push_back(info);
553
554   base::ScopedTempDir temp_dir;
555   ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
556
557   base::FilePath image_path = temp_dir.path().AppendASCII("img.jpg");
558   ASSERT_TRUE(WriteJPEGHeader(image_path));
559
560   AlbumImagesMap albums_images;
561   albums_images[info.uid] = AlbumImages();
562   albums_images[info.uid]["mapped_name.jpg"] = image_path;
563
564   SetupFolders(&test_folders, albums, albums_images);
565
566   FileSystemOperation::FileEntryList contents;
567   FileSystemURL url =
568       CreateURL(std::string(kPicasaDirAlbums) + "/albumname 2013-04-16");
569   bool completed = false;
570   ReadDirectoryTestHelper(operation_runner(), url, &contents, &completed);
571
572   ASSERT_TRUE(completed);
573   EXPECT_EQ(1u, contents.size());
574   EXPECT_EQ("mapped_name.jpg",
575             base::FilePath(contents.begin()->name).AsUTF8Unsafe());
576   EXPECT_FALSE(contents.begin()->is_directory);
577
578   // Create a snapshot file to verify the file path.
579   base::RunLoop loop;
580   base::PlatformFileError error;
581   base::FilePath platform_path_result;
582   fileapi::FileSystemOperationRunner::SnapshotFileCallback snapshot_callback =
583       base::Bind(&CreateSnapshotFileTestHelperCallback,
584                  &loop,
585                  &error,
586                  &platform_path_result);
587   operation_runner()->CreateSnapshotFile(
588       CreateURL(std::string(kPicasaDirAlbums) +
589                 "/albumname 2013-04-16/mapped_name.jpg"),
590       snapshot_callback);
591   loop.Run();
592   EXPECT_EQ(base::PLATFORM_FILE_OK, error);
593   EXPECT_EQ(image_path, platform_path_result);
594 }
595
596 }  // namespace picasa