Upstream version 7.35.139.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/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 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(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::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);
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         std::vector<fileapi::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& virtual_path) const {
360     return file_system_context_->CreateCrackedFileSystemURL(
361         GURL("http://www.example.com"), fileapi::kFileSystemTypePicasa,
362         base::FilePath::FromUTF8Unsafe(virtual_path));
363   }
364
365   fileapi::FileSystemOperationRunner* operation_runner() const {
366     return file_system_context_->operation_runner();
367   }
368
369   scoped_refptr<fileapi::FileSystemContext> file_system_context() const {
370     return file_system_context_;
371   }
372
373  private:
374   base::MessageLoop message_loop_;
375   content::TestBrowserThread io_thread_;
376
377   base::ScopedTempDir profile_dir_;
378
379   scoped_refptr<fileapi::FileSystemContext> file_system_context_;
380   scoped_ptr<PicasaDataProvider> picasa_data_provider_;
381   scoped_ptr<MediaPathFilter> media_path_filter_;
382
383   DISALLOW_COPY_AND_ASSIGN(PicasaFileUtilTest);
384 };
385
386 TEST_F(PicasaFileUtilTest, DateFormat) {
387   base::Time::Exploded exploded_shortmonth = { 2013, 4, 0, 16, 0, 0, 0, 0 };
388   base::Time shortmonth = base::Time::FromLocalExploded(exploded_shortmonth);
389
390   base::Time::Exploded exploded_shortday = { 2013, 11, 0, 3, 0, 0, 0, 0 };
391   base::Time shortday = base::Time::FromLocalExploded(exploded_shortday);
392
393   EXPECT_EQ("2013-04-16", DateToPathString(shortmonth));
394   EXPECT_EQ("2013-11-03", DateToPathString(shortday));
395 }
396
397 TEST_F(PicasaFileUtilTest, NameDeduplication) {
398   ScopedVector<TestFolder> test_folders;
399   std::vector<std::string> expected_names;
400
401   base::Time test_date = base::Time::FromLocalExploded(test_date_exploded);
402   base::Time test_date_2 = test_date - base::TimeDelta::FromDays(1);
403
404   std::string test_date_string = DateToPathString(test_date);
405   std::string test_date_2_string = DateToPathString(test_date_2);
406
407   test_folders.push_back(
408       new TestFolder("diff_date", test_date_2, "uuid3", 0, 0));
409   expected_names.push_back("diff_date " + test_date_2_string);
410
411   test_folders.push_back(
412       new TestFolder("diff_date", test_date, "uuid2", 0, 0));
413   expected_names.push_back("diff_date " + test_date_string);
414
415   test_folders.push_back(
416       new TestFolder("duplicate", test_date, "uuid4", 0, 0));
417   expected_names.push_back("duplicate " + test_date_string + " (1)");
418
419   test_folders.push_back(
420       new TestFolder("duplicate", test_date, "uuid5", 0, 0));
421   expected_names.push_back("duplicate " + test_date_string + " (2)");
422
423   test_folders.push_back(
424       new TestFolder("unique_name", test_date, "uuid1", 0, 0));
425   expected_names.push_back("unique_name " + test_date_string);
426
427   SetupFolders(&test_folders, std::vector<AlbumInfo>(), AlbumImagesMap());
428
429   FileSystemOperation::FileEntryList contents;
430   FileSystemURL url = CreateURL(kPicasaDirFolders);
431   bool completed = false;
432   ReadDirectoryTestHelper(operation_runner(), url, &contents, &completed);
433
434   ASSERT_TRUE(completed);
435   ASSERT_EQ(expected_names.size(), contents.size());
436   for (size_t i = 0; i < contents.size(); ++i) {
437     EXPECT_EQ(expected_names[i],
438               base::FilePath(contents[i].name).AsUTF8Unsafe());
439     EXPECT_EQ(test_folders[i]->timestamp(), contents[i].last_modified_time);
440     EXPECT_TRUE(contents[i].is_directory);
441   }
442 }
443
444 TEST_F(PicasaFileUtilTest, RootFolders) {
445   ScopedVector<TestFolder> empty_folders_list;
446   SetupFolders(&empty_folders_list, std::vector<AlbumInfo>(), AlbumImagesMap());
447
448   FileSystemOperation::FileEntryList contents;
449   FileSystemURL url = CreateURL("");
450   bool completed = false;
451   ReadDirectoryTestHelper(operation_runner(), url, &contents, &completed);
452
453   ASSERT_TRUE(completed);
454   ASSERT_EQ(2u, contents.size());
455
456   EXPECT_TRUE(contents.front().is_directory);
457   EXPECT_TRUE(contents.back().is_directory);
458
459   EXPECT_EQ(0, contents.front().size);
460   EXPECT_EQ(0, contents.back().size);
461
462   EXPECT_EQ(FILE_PATH_LITERAL("albums"), contents.front().name);
463   EXPECT_EQ(FILE_PATH_LITERAL("folders"), contents.back().name);
464 }
465
466 TEST_F(PicasaFileUtilTest, NonexistentFolder) {
467   ScopedVector<TestFolder> empty_folders_list;
468   SetupFolders(&empty_folders_list, std::vector<AlbumInfo>(), AlbumImagesMap());
469
470   TestNonexistentDirectory(std::string(kPicasaDirFolders) + "/foo");
471   TestNonexistentDirectory(std::string(kPicasaDirFolders) + "/foo/bar");
472   TestNonexistentDirectory(std::string(kPicasaDirFolders) + "/foo/bar/baz");
473 }
474
475 TEST_F(PicasaFileUtilTest, FolderContentsTrivial) {
476   ScopedVector<TestFolder> test_folders;
477   base::Time test_date = base::Time::FromLocalExploded(test_date_exploded);
478
479   test_folders.push_back(
480       new TestFolder("folder-1-empty", test_date, "uid-empty", 0, 0));
481   test_folders.push_back(
482       new TestFolder("folder-2-images", test_date, "uid-images", 5, 0));
483   test_folders.push_back(
484       new TestFolder("folder-3-nonimages", test_date, "uid-nonimages", 0, 5));
485   test_folders.push_back(
486       new TestFolder("folder-4-both", test_date, "uid-both", 5, 5));
487
488   SetupFolders(&test_folders, std::vector<AlbumInfo>(), AlbumImagesMap());
489   VerifyFolderDirectoryList(test_folders);
490 }
491
492 TEST_F(PicasaFileUtilTest, FolderWithManyFiles) {
493   ScopedVector<TestFolder> test_folders;
494   base::Time test_date = base::Time::FromLocalExploded(test_date_exploded);
495
496   test_folders.push_back(
497       new TestFolder("folder-many-files", test_date, "uid-both", 500, 500));
498
499   SetupFolders(&test_folders, std::vector<AlbumInfo>(), AlbumImagesMap());
500   VerifyFolderDirectoryList(test_folders);
501 }
502
503 TEST_F(PicasaFileUtilTest, ManyFolders) {
504   ScopedVector<TestFolder> test_folders;
505   base::Time test_date = base::Time::FromLocalExploded(test_date_exploded);
506
507   // TODO(tommycli): Turn number of test folders back up to 50 (or more) once
508   // https://codereview.chromium.org/15479003/ lands.
509   for (unsigned int i = 0; i < 25; ++i) {
510     base::Time date = test_date - base::TimeDelta::FromDays(i);
511
512     test_folders.push_back(
513         new TestFolder(base::StringPrintf("folder-%05d", i),
514                        date,
515                        base::StringPrintf("uid%05d", i), i % 5, i % 3));
516   }
517
518   SetupFolders(&test_folders, std::vector<AlbumInfo>(), AlbumImagesMap());
519   VerifyFolderDirectoryList(test_folders);
520 }
521
522 TEST_F(PicasaFileUtilTest, AlbumExistence) {
523   ScopedVector<TestFolder> test_folders;
524   base::Time test_date = base::Time::FromLocalExploded(test_date_exploded);
525
526   std::vector<AlbumInfo> albums;
527   AlbumInfo info;
528   info.name = "albumname";
529   info.uid = "albumuid";
530   info.timestamp = test_date;
531   albums.push_back(info);
532
533   AlbumImagesMap albums_images;
534   albums_images[info.uid] = AlbumImages();
535
536   SetupFolders(&test_folders, albums, albums_images);
537
538   TestEmptyDirectory(std::string(kPicasaDirAlbums) + "/albumname 2013-04-16");
539   TestNonexistentDirectory(std::string(kPicasaDirAlbums) +
540                            "/albumname 2013-04-16/toodeep");
541   TestNonexistentDirectory(std::string(kPicasaDirAlbums) + "/wrongname");
542 }
543
544 TEST_F(PicasaFileUtilTest, AlbumContents) {
545   ScopedVector<TestFolder> test_folders;
546   base::Time test_date = base::Time::FromLocalExploded(test_date_exploded);
547
548   std::vector<AlbumInfo> albums;
549   AlbumInfo info;
550   info.name = "albumname";
551   info.uid = "albumuid";
552   info.timestamp = test_date;
553   albums.push_back(info);
554
555   base::ScopedTempDir temp_dir;
556   ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
557
558   base::FilePath image_path = temp_dir.path().AppendASCII("img.jpg");
559   ASSERT_TRUE(WriteJPEGHeader(image_path));
560
561   AlbumImagesMap albums_images;
562   albums_images[info.uid] = AlbumImages();
563   albums_images[info.uid]["mapped_name.jpg"] = image_path;
564
565   SetupFolders(&test_folders, albums, albums_images);
566
567   FileSystemOperation::FileEntryList contents;
568   FileSystemURL url =
569       CreateURL(std::string(kPicasaDirAlbums) + "/albumname 2013-04-16");
570   bool completed = false;
571   ReadDirectoryTestHelper(operation_runner(), url, &contents, &completed);
572
573   ASSERT_TRUE(completed);
574   EXPECT_EQ(1u, contents.size());
575   EXPECT_EQ("mapped_name.jpg",
576             base::FilePath(contents.begin()->name).AsUTF8Unsafe());
577   EXPECT_FALSE(contents.begin()->is_directory);
578
579   // Create a snapshot file to verify the file path.
580   base::RunLoop loop;
581   base::File::Error error;
582   base::FilePath platform_path_result;
583   fileapi::FileSystemOperationRunner::SnapshotFileCallback snapshot_callback =
584       base::Bind(&CreateSnapshotFileTestHelperCallback,
585                  &loop,
586                  &error,
587                  &platform_path_result);
588   operation_runner()->CreateSnapshotFile(
589       CreateURL(std::string(kPicasaDirAlbums) +
590                 "/albumname 2013-04-16/mapped_name.jpg"),
591       snapshot_callback);
592   loop.Run();
593   EXPECT_EQ(base::File::FILE_OK, error);
594   EXPECT_EQ(image_path, platform_path_result);
595 }
596
597 }  // namespace picasa