Update To 11.40.268.0
[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/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/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 "storage/browser/fileapi/async_file_util.h"
32 #include "storage/browser/fileapi/external_mount_points.h"
33 #include "storage/browser/fileapi/file_system_context.h"
34 #include "storage/browser/fileapi/file_system_operation_context.h"
35 #include "storage/browser/fileapi/file_system_operation_runner.h"
36 #include "storage/browser/fileapi/isolated_context.h"
37 #include "storage/common/blob/shareable_file_reference.h"
38 #include "testing/gtest/include/gtest/gtest.h"
39
40 using storage::FileSystemOperationContext;
41 using storage::FileSystemOperation;
42 using storage::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 base::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 (base::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::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();
143 }
144
145 void ReadDirectoryTestHelper(storage::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::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<storage::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   ~TestPicasaFileUtil() override {}
194
195  private:
196   PicasaDataProvider* GetDataProvider() override { return data_provider_; }
197
198   PicasaDataProvider* data_provider_;
199 };
200
201 class TestMediaFileSystemBackend : public MediaFileSystemBackend {
202  public:
203   TestMediaFileSystemBackend(const base::FilePath& profile_path,
204                              PicasaFileUtil* picasa_file_util)
205       : MediaFileSystemBackend(profile_path,
206                                MediaFileSystemBackend::MediaTaskRunner().get()),
207         test_file_util_(picasa_file_util) {}
208
209   storage::AsyncFileUtil* GetAsyncFileUtil(
210       storage::FileSystemType type) override {
211     if (type != storage::kFileSystemTypePicasa)
212       return NULL;
213
214     return test_file_util_.get();
215   }
216
217  private:
218   scoped_ptr<storage::AsyncFileUtil> test_file_util_;
219 };
220
221 class PicasaFileUtilTest : public testing::Test {
222  public:
223   PicasaFileUtilTest()
224       : io_thread_(content::BrowserThread::IO, &message_loop_) {
225   }
226   virtual ~PicasaFileUtilTest() {}
227
228   virtual void SetUp() override {
229     ASSERT_TRUE(profile_dir_.CreateUniqueTempDir());
230     ImportedMediaGalleryRegistry::GetInstance()->Initialize();
231
232     scoped_refptr<storage::SpecialStoragePolicy> storage_policy =
233         new content::MockSpecialStoragePolicy();
234
235     SynchronouslyRunOnMediaTaskRunner(base::Bind(
236         &PicasaFileUtilTest::SetUpOnMediaTaskRunner, base::Unretained(this)));
237
238     media_path_filter_.reset(new MediaPathFilter());
239
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())));
245
246     file_system_context_ = new storage::FileSystemContext(
247         base::MessageLoopProxy::current().get(),
248         base::MessageLoopProxy::current().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());
256   }
257
258   virtual void TearDown() override {
259     SynchronouslyRunOnMediaTaskRunner(
260         base::Bind(&PicasaFileUtilTest::TearDownOnMediaTaskRunner,
261                    base::Unretained(this)));
262   }
263
264  protected:
265   void SetUpOnMediaTaskRunner() {
266     picasa_data_provider_.reset(new PicasaDataProvider(base::FilePath()));
267   }
268
269   void TearDownOnMediaTaskRunner() {
270     picasa_data_provider_.reset();
271   }
272
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());
283     }
284
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;
292   }
293
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);
299
300     ASSERT_TRUE(completed);
301     ASSERT_EQ(test_folders.size(), contents.size());
302
303     for (size_t i = 0; i < contents.size(); ++i) {
304       EXPECT_TRUE(contents[i].is_directory);
305
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));
311
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);
319
320       EXPECT_TRUE(folder_read_completed);
321
322       const std::set<std::string>& image_filenames =
323           test_folders[i]->image_filenames();
324
325       EXPECT_EQ(image_filenames.size(), folder_contents.size());
326
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()));
332       }
333     }
334   }
335
336   std::string DateToPathString(const base::Time& time) {
337     return PicasaDataProvider::DateToPathString(time);
338   }
339
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);
345
346     ASSERT_FALSE(completed);
347   }
348
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);
354
355     ASSERT_TRUE(completed);
356     EXPECT_EQ(0u, contents.size());
357   }
358
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);
368   }
369
370   storage::FileSystemOperationRunner* operation_runner() const {
371     return file_system_context_->operation_runner();
372   }
373
374   scoped_refptr<storage::FileSystemContext> file_system_context() const {
375     return file_system_context_;
376   }
377
378  private:
379   base::MessageLoop message_loop_;
380   content::TestBrowserThread io_thread_;
381
382   base::ScopedTempDir profile_dir_;
383
384   scoped_refptr<storage::FileSystemContext> file_system_context_;
385   scoped_ptr<PicasaDataProvider> picasa_data_provider_;
386   scoped_ptr<MediaPathFilter> media_path_filter_;
387
388   DISALLOW_COPY_AND_ASSIGN(PicasaFileUtilTest);
389 };
390
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);
394
395   base::Time::Exploded exploded_shortday = { 2013, 11, 0, 3, 0, 0, 0, 0 };
396   base::Time shortday = base::Time::FromLocalExploded(exploded_shortday);
397
398   EXPECT_EQ("2013-04-16", DateToPathString(shortmonth));
399   EXPECT_EQ("2013-11-03", DateToPathString(shortday));
400 }
401
402 TEST_F(PicasaFileUtilTest, NameDeduplication) {
403   ScopedVector<TestFolder> test_folders;
404   std::vector<std::string> expected_names;
405
406   base::Time test_date = base::Time::FromLocalExploded(test_date_exploded);
407   base::Time test_date_2 = test_date - base::TimeDelta::FromDays(1);
408
409   std::string test_date_string = DateToPathString(test_date);
410   std::string test_date_2_string = DateToPathString(test_date_2);
411
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);
415
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);
419
420   test_folders.push_back(
421       new TestFolder("duplicate", test_date, "uuid4", 0, 0));
422   expected_names.push_back("duplicate " + test_date_string + " (1)");
423
424   test_folders.push_back(
425       new TestFolder("duplicate", test_date, "uuid5", 0, 0));
426   expected_names.push_back("duplicate " + test_date_string + " (2)");
427
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);
431
432   SetupFolders(&test_folders, std::vector<AlbumInfo>(), AlbumImagesMap());
433
434   FileSystemOperation::FileEntryList contents;
435   FileSystemURL url = CreateURL(kPicasaDirFolders);
436   bool completed = false;
437   ReadDirectoryTestHelper(operation_runner(), url, &contents, &completed);
438
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);
446   }
447 }
448
449 TEST_F(PicasaFileUtilTest, RootFolders) {
450   ScopedVector<TestFolder> empty_folders_list;
451   SetupFolders(&empty_folders_list, std::vector<AlbumInfo>(), AlbumImagesMap());
452
453   FileSystemOperation::FileEntryList contents;
454   FileSystemURL url = CreateURL("");
455   bool completed = false;
456   ReadDirectoryTestHelper(operation_runner(), url, &contents, &completed);
457
458   ASSERT_TRUE(completed);
459   ASSERT_EQ(2u, contents.size());
460
461   EXPECT_TRUE(contents.front().is_directory);
462   EXPECT_TRUE(contents.back().is_directory);
463
464   EXPECT_EQ(0, contents.front().size);
465   EXPECT_EQ(0, contents.back().size);
466
467   EXPECT_EQ(FILE_PATH_LITERAL("albums"), contents.front().name);
468   EXPECT_EQ(FILE_PATH_LITERAL("folders"), contents.back().name);
469 }
470
471 TEST_F(PicasaFileUtilTest, NonexistentFolder) {
472   ScopedVector<TestFolder> empty_folders_list;
473   SetupFolders(&empty_folders_list, std::vector<AlbumInfo>(), AlbumImagesMap());
474
475   TestNonexistentDirectory(std::string(kPicasaDirFolders) + "/foo");
476   TestNonexistentDirectory(std::string(kPicasaDirFolders) + "/foo/bar");
477   TestNonexistentDirectory(std::string(kPicasaDirFolders) + "/foo/bar/baz");
478 }
479
480 TEST_F(PicasaFileUtilTest, FolderContentsTrivial) {
481   ScopedVector<TestFolder> test_folders;
482   base::Time test_date = base::Time::FromLocalExploded(test_date_exploded);
483
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));
492
493   SetupFolders(&test_folders, std::vector<AlbumInfo>(), AlbumImagesMap());
494   VerifyFolderDirectoryList(test_folders);
495 }
496
497 TEST_F(PicasaFileUtilTest, FolderWithManyFiles) {
498   ScopedVector<TestFolder> test_folders;
499   base::Time test_date = base::Time::FromLocalExploded(test_date_exploded);
500
501   test_folders.push_back(
502       new TestFolder("folder-many-files", test_date, "uid-both", 50, 50));
503
504   SetupFolders(&test_folders, std::vector<AlbumInfo>(), AlbumImagesMap());
505   VerifyFolderDirectoryList(test_folders);
506 }
507
508 TEST_F(PicasaFileUtilTest, ManyFolders) {
509   ScopedVector<TestFolder> test_folders;
510   base::Time test_date = base::Time::FromLocalExploded(test_date_exploded);
511
512   for (unsigned int i = 0; i < 50; ++i) {
513     base::Time date = test_date - base::TimeDelta::FromDays(i);
514
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));
519   }
520
521   SetupFolders(&test_folders, std::vector<AlbumInfo>(), AlbumImagesMap());
522   VerifyFolderDirectoryList(test_folders);
523 }
524
525 TEST_F(PicasaFileUtilTest, AlbumExistence) {
526   ScopedVector<TestFolder> test_folders;
527   base::Time test_date = base::Time::FromLocalExploded(test_date_exploded);
528
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);
535
536   AlbumImagesMap albums_images;
537   albums_images[info.uid] = AlbumImages();
538
539   SetupFolders(&test_folders, albums, albums_images);
540
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");
545 }
546
547 TEST_F(PicasaFileUtilTest, AlbumContents) {
548   ScopedVector<TestFolder> test_folders;
549   base::Time test_date = base::Time::FromLocalExploded(test_date_exploded);
550
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);
557
558   base::ScopedTempDir temp_dir;
559   ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
560
561   base::FilePath image_path = temp_dir.path().AppendASCII("img.jpg");
562   ASSERT_TRUE(WriteJPEGHeader(image_path));
563
564   AlbumImagesMap albums_images;
565   albums_images[info.uid] = AlbumImages();
566   albums_images[info.uid]["mapped_name.jpg"] = image_path;
567
568   SetupFolders(&test_folders, albums, albums_images);
569
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);
575
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);
581
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);
598 }
599
600 }  // namespace picasa