Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / media_galleries / media_galleries_apitest.cc
1 // Copyright (c) 2012 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 "base/auto_reset.h"
6 #include "base/callback.h"
7 #include "base/file_util.h"
8 #include "base/files/scoped_temp_dir.h"
9 #include "base/json/json_writer.h"
10 #include "base/numerics/safe_conversions.h"
11 #include "base/path_service.h"
12 #include "base/strings/string_util.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "base/values.h"
15 #include "chrome/browser/apps/app_browsertest_util.h"
16 #include "chrome/browser/browser_process.h"
17 #include "chrome/browser/extensions/api/media_galleries/media_galleries_api.h"
18 #include "chrome/browser/media_galleries/media_file_system_registry.h"
19 #include "chrome/browser/media_galleries/media_folder_finder.h"
20 #include "chrome/browser/media_galleries/media_galleries_preferences.h"
21 #include "chrome/browser/media_galleries/media_galleries_scan_result_dialog_controller.h"
22 #include "chrome/browser/media_galleries/media_galleries_test_util.h"
23 #include "chrome/browser/media_galleries/media_scan_manager.h"
24 #include "chrome/common/chrome_paths.h"
25 #include "components/storage_monitor/storage_info.h"
26 #include "components/storage_monitor/storage_monitor.h"
27 #include "content/public/browser/web_contents.h"
28 #include "content/public/test/test_utils.h"
29 #include "extensions/browser/extension_system.h"
30 #include "extensions/common/extension.h"
31 #include "media/base/test_data_util.h"
32
33 #if defined(OS_WIN) || defined(OS_MACOSX)
34 #include "chrome/browser/media_galleries/fileapi/picasa_finder.h"
35 #include "chrome/common/media_galleries/picasa_test_util.h"
36 #include "chrome/common/media_galleries/picasa_types.h"
37 #include "chrome/common/media_galleries/pmp_test_util.h"
38 #endif
39
40 #if defined(OS_MACOSX)
41 #include "base/mac/foundation_util.h"
42 #include "base/strings/sys_string_conversions.h"
43 #include "chrome/browser/media_galleries/fileapi/iapps_finder_impl.h"
44 #endif  // OS_MACOSX
45
46 #if !defined(DISABLE_NACL)
47 #include "base/command_line.h"
48 #include "chrome/browser/ui/extensions/application_launch.h"
49 #include "ppapi/shared_impl/ppapi_switches.h"
50 #endif
51
52 using extensions::PlatformAppBrowserTest;
53 using storage_monitor::StorageInfo;
54 using storage_monitor::StorageMonitor;
55
56 namespace {
57
58 // Dummy device properties.
59 const char kDeviceId[] = "testDeviceId";
60 const char kDeviceName[] = "foobar";
61 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
62 base::FilePath::CharType kDevicePath[] = FILE_PATH_LITERAL("C:\\qux");
63 #else
64 base::FilePath::CharType kDevicePath[] = FILE_PATH_LITERAL("/qux");
65 #endif
66
67 class DoNothingMediaFolderFinder : public MediaFolderFinder {
68  public:
69   explicit DoNothingMediaFolderFinder(
70       const MediaFolderFinderResultsCallback& callback)
71       : MediaFolderFinder(callback) {
72   }
73   virtual ~DoNothingMediaFolderFinder() {}
74
75   static MediaFolderFinder* CreateDoNothingMediaFolderFinder(
76       const MediaFolderFinderResultsCallback& callback) {
77     return new DoNothingMediaFolderFinder(callback);
78   }
79
80   virtual void StartScan() OVERRIDE {}
81
82  private:
83 };
84
85 }  // namespace
86
87 class TestMediaGalleriesAddScanResultsFunction
88     : public extensions::MediaGalleriesAddScanResultsFunction {
89  public:
90   static ExtensionFunction* Factory() {
91     return new TestMediaGalleriesAddScanResultsFunction;
92   }
93
94  protected:
95   virtual ~TestMediaGalleriesAddScanResultsFunction() {}
96
97   // Accepts the dialog as soon as it is created.
98   virtual MediaGalleriesScanResultDialogController* MakeDialog(
99       content::WebContents* web_contents,
100       const extensions::Extension& extension,
101       const base::Closure& on_finish) OVERRIDE {
102     MediaGalleriesScanResultDialogController* controller =
103         extensions::MediaGalleriesAddScanResultsFunction::MakeDialog(
104             web_contents, extension, on_finish);
105     controller->dialog_->AcceptDialogForTesting();
106     // The dialog is closing or closed so don't return it.
107     return NULL;
108   }
109 };
110
111 class MediaGalleriesPlatformAppBrowserTest : public PlatformAppBrowserTest {
112  protected:
113   MediaGalleriesPlatformAppBrowserTest() : test_jpg_size_(0) {}
114   virtual ~MediaGalleriesPlatformAppBrowserTest() {}
115
116   virtual void SetUpOnMainThread() OVERRIDE {
117     PlatformAppBrowserTest::SetUpOnMainThread();
118     ensure_media_directories_exists_.reset(new EnsureMediaDirectoriesExists);
119
120     int64 file_size;
121     ASSERT_TRUE(base::GetFileSize(GetCommonDataDir().AppendASCII("test.jpg"),
122                                   &file_size));
123     test_jpg_size_ = base::checked_cast<int>(file_size);
124   }
125
126   virtual void TearDownOnMainThread() OVERRIDE {
127     ensure_media_directories_exists_.reset();
128     PlatformAppBrowserTest::TearDownOnMainThread();
129   }
130
131   bool RunMediaGalleriesTest(const std::string& extension_name) {
132     base::ListValue empty_list_value;
133     return RunMediaGalleriesTestWithArg(extension_name, empty_list_value);
134   }
135
136   bool RunMediaGalleriesTestWithArg(const std::string& extension_name,
137                                     const base::ListValue& custom_arg_value) {
138     // Copy the test data for this test into a temporary directory. Then add
139     // a common_injected.js to the temporary copy and run it.
140     const char kTestDir[] = "api_test/media_galleries/";
141     base::FilePath from_dir =
142         test_data_dir_.AppendASCII(kTestDir + extension_name);
143     from_dir = from_dir.NormalizePathSeparators();
144
145     base::ScopedTempDir temp_dir;
146     if (!temp_dir.CreateUniqueTempDir())
147       return false;
148
149     if (!base::CopyDirectory(from_dir, temp_dir.path(), true))
150       return false;
151
152     base::FilePath common_js_path(
153         GetCommonDataDir().AppendASCII("common_injected.js"));
154     base::FilePath inject_js_path(
155         temp_dir.path().AppendASCII(extension_name)
156                        .AppendASCII("common_injected.js"));
157     if (!base::CopyFile(common_js_path, inject_js_path))
158       return false;
159
160     const char* custom_arg = NULL;
161     std::string json_string;
162     if (!custom_arg_value.empty()) {
163       base::JSONWriter::Write(&custom_arg_value, &json_string);
164       custom_arg = json_string.c_str();
165     }
166
167     base::AutoReset<base::FilePath> reset(&test_data_dir_, temp_dir.path());
168     bool result = RunPlatformAppTestWithArg(extension_name, custom_arg);
169     content::RunAllPendingInMessageLoop();  // avoid race on exit in registry.
170     return result;
171   }
172
173   void AttachFakeDevice() {
174     device_id_ = StorageInfo::MakeDeviceId(
175         StorageInfo::REMOVABLE_MASS_STORAGE_WITH_DCIM, kDeviceId);
176
177     StorageMonitor::GetInstance()->receiver()->ProcessAttach(
178         StorageInfo(device_id_, kDevicePath, base::ASCIIToUTF16(kDeviceName),
179                     base::string16(), base::string16(), 0));
180     content::RunAllPendingInMessageLoop();
181   }
182
183   void DetachFakeDevice() {
184     StorageMonitor::GetInstance()->receiver()->ProcessDetach(device_id_);
185     content::RunAllPendingInMessageLoop();
186   }
187
188   // Called if test only wants a single gallery it creates.
189   void RemoveAllGalleries() {
190     MediaGalleriesPreferences* preferences = GetAndInitializePreferences();
191
192     // Make a copy, as the iterator would be invalidated otherwise.
193     const MediaGalleriesPrefInfoMap galleries =
194         preferences->known_galleries();
195     for (MediaGalleriesPrefInfoMap::const_iterator it = galleries.begin();
196          it != galleries.end(); ++it) {
197       preferences->ForgetGalleryById(it->first);
198     }
199   }
200
201   // This function makes a single fake gallery. This is needed to test platforms
202   // with no default media galleries, such as CHROMEOS. This fake gallery is
203   // pre-populated with a test.jpg and test.txt.
204   void MakeSingleFakeGallery(MediaGalleryPrefId* pref_id) {
205     ASSERT_FALSE(fake_gallery_temp_dir_.IsValid());
206     ASSERT_TRUE(fake_gallery_temp_dir_.CreateUniqueTempDir());
207
208     MediaGalleriesPreferences* preferences = GetAndInitializePreferences();
209
210     MediaGalleryPrefInfo gallery_info;
211     ASSERT_FALSE(preferences->LookUpGalleryByPath(fake_gallery_temp_dir_.path(),
212                                                   &gallery_info));
213     MediaGalleryPrefId id = preferences->AddGallery(
214         gallery_info.device_id,
215         gallery_info.path,
216         MediaGalleryPrefInfo::kAutoDetected,
217         gallery_info.volume_label,
218         gallery_info.vendor_name,
219         gallery_info.model_name,
220         gallery_info.total_size_in_bytes,
221         gallery_info.last_attach_time,
222         0, 0, 0);
223     if (pref_id)
224       *pref_id = id;
225
226     content::RunAllPendingInMessageLoop();
227
228     // Valid file, should show up in JS as a FileEntry.
229     AddFileToSingleFakeGallery(GetCommonDataDir().AppendASCII("test.jpg"));
230
231     // Invalid file, should not show up as a FileEntry in JS at all.
232     AddFileToSingleFakeGallery(GetCommonDataDir().AppendASCII("test.txt"));
233   }
234
235   void AddFileToSingleFakeGallery(const base::FilePath& source_path) {
236     ASSERT_TRUE(fake_gallery_temp_dir_.IsValid());
237
238     ASSERT_TRUE(base::CopyFile(
239         source_path,
240         fake_gallery_temp_dir_.path().Append(source_path.BaseName())));
241   }
242
243 #if defined(OS_WIN) || defined(OS_MACOSX)
244   void PopulatePicasaTestData(const base::FilePath& picasa_app_data_root) {
245     base::FilePath picasa_database_path =
246         picasa::MakePicasaDatabasePath(picasa_app_data_root);
247     base::FilePath picasa_temp_dir_path =
248         picasa_database_path.DirName().AppendASCII(picasa::kPicasaTempDirName);
249     ASSERT_TRUE(base::CreateDirectory(picasa_database_path));
250     ASSERT_TRUE(base::CreateDirectory(picasa_temp_dir_path));
251
252     // Create fake folder directories.
253     base::FilePath folders_root =
254         ensure_media_directories_exists_->GetFakePicasaFoldersRootPath();
255     base::FilePath fake_folder_1 = folders_root.AppendASCII("folder1");
256     base::FilePath fake_folder_2 = folders_root.AppendASCII("folder2");
257     ASSERT_TRUE(base::CreateDirectory(fake_folder_1));
258     ASSERT_TRUE(base::CreateDirectory(fake_folder_2));
259
260     // Write folder and album contents.
261     picasa::WriteTestAlbumTable(
262         picasa_database_path, fake_folder_1, fake_folder_2);
263     picasa::WriteTestAlbumsImagesIndex(fake_folder_1, fake_folder_2);
264
265     base::FilePath test_jpg_path = GetCommonDataDir().AppendASCII("test.jpg");
266     ASSERT_TRUE(base::CopyFile(
267         test_jpg_path, fake_folder_1.AppendASCII("InBoth.jpg")));
268     ASSERT_TRUE(base::CopyFile(
269         test_jpg_path, fake_folder_1.AppendASCII("InSecondAlbumOnly.jpg")));
270     ASSERT_TRUE(base::CopyFile(
271         test_jpg_path, fake_folder_2.AppendASCII("InFirstAlbumOnly.jpg")));
272   }
273 #endif  // defined(OS_WIN) || defined(OS_MACOSX)
274
275 #if defined(OS_MACOSX)
276   void PopulateIPhotoTestData(const base::FilePath& iphoto_data_root) {
277     std::string xml_contents = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
278     "<plist version=\"1.0\">"
279     "<dict>\n"
280
281     "    <key>List of Albums</key>"
282     "    <array>\n"
283
284     "    <dict>\n"
285     "      <key>AlbumId</key>"
286     "      <integer>1</integer>"
287     "      <key>AlbumName</key>"
288     "      <string>Album1</string>"
289     "      <key>KeyList</key>\n"
290     "      <array>"
291     "      <string>1</string>"
292     "      <string>2</string>"
293     "      </array>\n"
294     "    </dict>\n"
295
296     "    <dict>\n"
297     "      <key>AlbumId</key>"
298     "      <integer>2</integer>"
299     "      <key>AlbumName</key>"
300     "      <string>Album2</string>"
301     "      <key>KeyList</key>\n"
302     "      <array>"
303     "      <string>2</string>"
304     "      </array>\n"
305     "    </dict>\n"
306
307     "    </array>\n"
308
309     "   <key>Master Image List</key>\n"
310     "   <dict>\n"
311
312     "  <key>1</key>"
313     "  <dict>\n"
314     "    <key>MediaType</key>"
315     "    <string>Image</string>"
316     "    <key>Caption</key>"
317     "    <string>caption 1</string>"
318     "    <key>GUID</key>"
319     "    <string>1</string>"
320     "    <key>ModDateAsTimerInterval</key>"
321     "    <string>386221543.0000</string>"
322     "    <key>DateAsTimerInterval</key>"
323     "    <string>386221543.0000</string>"
324     "    <key>DateAsTimerIntervalGMT</key>"
325     "    <string>385123456.00</string>"
326     "    <key>ImagePath</key>"
327     "    <string>$path1</string>"
328     "    <key>ThumbPath</key>"
329     "    <string>/thumb/path</string>\n"
330     "  </dict>\n"
331
332     "  <key>2</key>\n"
333     "  <dict>\n"
334     "    <key>MediaType</key>"
335     "    <string>Image</string>"
336     "    <key>Caption</key>"
337     "    <string>caption 2</string>"
338     "    <key>GUID</key>"
339     "    <string>2</string>"
340     "    <key>ModDateAsTimerInterval</key>"
341     "    <string>386221543.0000</string>"
342     "    <key>DateAsTimerInterval</key>"
343     "    <string>386221543.0000</string>"
344     "    <key>DateAsTimerIntervalGMT</key>"
345     "    <string>385123456.00</string>"
346     "    <key>ImagePath</key>"
347     "    <string>$path2</string>"
348     "    <key>ThumbPath</key>"
349     "    <string>/thumb/path2</string>\n"
350     "  </dict>\n"
351
352     "   </dict>\n"  // Master Image List
353
354     "</dict>\n"
355     "</plist>";
356
357     base::FilePath test_jpg_path = GetCommonDataDir().AppendASCII("test.jpg");
358     ASSERT_TRUE(base::CreateDirectory(iphoto_data_root));
359     base::FilePath first_only_jpg =
360         iphoto_data_root.AppendASCII("InFirstAlbumOnly.jpg");
361     base::FilePath in_both_jpg = iphoto_data_root.AppendASCII("InBoth.jpg");
362     ASSERT_TRUE(base::CopyFile(test_jpg_path, first_only_jpg));
363     ASSERT_TRUE(base::CopyFile(test_jpg_path, in_both_jpg));
364     ReplaceFirstSubstringAfterOffset(
365         &xml_contents, 0, std::string("$path1"), first_only_jpg.value());
366     ReplaceFirstSubstringAfterOffset(
367         &xml_contents, 0, std::string("$path2"), in_both_jpg.value());
368
369     base::FilePath album_xml = iphoto_data_root.AppendASCII("AlbumData.xml");
370     ASSERT_NE(-1, base::WriteFile(album_xml,
371                                   xml_contents.c_str(), xml_contents.size()));
372   }
373 #endif  // defined(OS_MACOSX)
374
375   base::FilePath GetCommonDataDir() const {
376     return test_data_dir_.AppendASCII("api_test")
377                          .AppendASCII("media_galleries")
378                          .AppendASCII("common");
379   }
380
381   base::FilePath GetWallpaperTestDataDir() const {
382     return test_data_dir_.AppendASCII("api_test")
383                          .AppendASCII("wallpaper");
384   }
385
386   int num_galleries() const {
387     return ensure_media_directories_exists_->num_galleries();
388   }
389
390   int test_jpg_size() const { return test_jpg_size_; }
391
392   EnsureMediaDirectoriesExists* ensure_media_directories_exists() const {
393     return ensure_media_directories_exists_.get();
394   }
395
396   void InstallDoNothingFolderFinder() {
397     MediaScanManager * scan_manager =
398         g_browser_process->media_file_system_registry()->media_scan_manager();
399     scan_manager->SetMediaFolderFinderFactory(base::Bind(
400         &DoNothingMediaFolderFinder::CreateDoNothingMediaFolderFinder));
401   }
402
403   void SetRootsForFolderFinder(const std::vector<base::FilePath>& roots) {
404     MediaScanManager* scan_manager =
405         g_browser_process->media_file_system_registry()->media_scan_manager();
406     scan_manager->SetMediaFolderFinderFactory(base::Bind(
407         &MediaGalleriesPlatformAppBrowserTest::CreateMediaFolderFinderWithRoots,
408         roots));
409   }
410
411  private:
412   static MediaFolderFinder* CreateMediaFolderFinderWithRoots(
413       const std::vector<base::FilePath>& roots,
414       const MediaFolderFinder::MediaFolderFinderResultsCallback& callback) {
415     MediaFolderFinder* finder = new MediaFolderFinder(callback);
416     finder->SetRootsForTesting(roots);
417     return finder;
418   }
419
420   MediaGalleriesPreferences* GetAndInitializePreferences() {
421     MediaGalleriesPreferences* preferences =
422         g_browser_process->media_file_system_registry()->GetPreferences(
423             browser()->profile());
424     base::RunLoop runloop;
425     preferences->EnsureInitialized(runloop.QuitClosure());
426     runloop.Run();
427     return preferences;
428   }
429
430   std::string device_id_;
431   base::ScopedTempDir fake_gallery_temp_dir_;
432   int test_jpg_size_;
433   scoped_ptr<EnsureMediaDirectoriesExists> ensure_media_directories_exists_;
434 };
435
436 #if !defined(DISABLE_NACL)
437 class MediaGalleriesPlatformAppPpapiTest
438     : public MediaGalleriesPlatformAppBrowserTest {
439  protected:
440   virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
441     MediaGalleriesPlatformAppBrowserTest::SetUpCommandLine(command_line);
442     command_line->AppendSwitch(switches::kEnablePepperTesting);
443   }
444
445   virtual void SetUpOnMainThread() OVERRIDE {
446     MediaGalleriesPlatformAppBrowserTest::SetUpOnMainThread();
447
448     ASSERT_TRUE(PathService::Get(chrome::DIR_GEN_TEST_DATA, &app_dir_));
449     app_dir_ = app_dir_.AppendASCII("ppapi")
450                        .AppendASCII("tests")
451                        .AppendASCII("extensions")
452                        .AppendASCII("media_galleries")
453                        .AppendASCII("newlib");
454   }
455
456   const base::FilePath& app_dir() const {
457     return app_dir_;
458   }
459
460  private:
461   base::FilePath app_dir_;
462 };
463
464 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppPpapiTest, SendFilesystem) {
465   RemoveAllGalleries();
466   MakeSingleFakeGallery(NULL);
467
468   const extensions::Extension* extension = LoadExtension(app_dir());
469   ASSERT_TRUE(extension);
470
471   ResultCatcher catcher;
472   AppLaunchParams params(browser()->profile(),
473                          extension,
474                          extensions::LAUNCH_CONTAINER_NONE,
475                          NEW_WINDOW);
476   params.command_line = *CommandLine::ForCurrentProcess();
477   OpenApplication(params);
478
479   bool result = true;
480   if (!catcher.GetNextResult()) {
481     message_ = catcher.message();
482     result = false;
483   }
484   content::RunAllPendingInMessageLoop();  // avoid race on exit in registry.
485   ASSERT_TRUE(result) << message_;
486 }
487
488 #endif  // !defined(DISABLE_NACL)
489
490 // Test is flaky, it fails on certain bots, namely WinXP Tests(1) and Linux
491 // (dbg)(1)(32).  See crbug.com/354425.
492 #if (defined(ARCH_CPU_X86)) && (defined(OS_WIN) || defined(OS_LINUX))
493 #define MAYBE_MediaGalleriesNoAccess DISABLED_MediaGalleriesNoAccess
494 #else
495 #define MAYBE_MediaGalleriesNoAccess MediaGalleriesNoAccess
496 #endif
497 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest,
498                        MAYBE_MediaGalleriesNoAccess) {
499   MakeSingleFakeGallery(NULL);
500
501   base::ListValue custom_args;
502   custom_args.AppendInteger(num_galleries() + 1);
503
504   ASSERT_TRUE(RunMediaGalleriesTestWithArg("no_access", custom_args))
505       << message_;
506 }
507
508 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest, NoGalleriesRead) {
509   ASSERT_TRUE(RunMediaGalleriesTest("no_galleries")) << message_;
510 }
511
512 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest,
513                        NoGalleriesCopyTo) {
514   ASSERT_TRUE(RunMediaGalleriesTest("no_galleries_copy_to")) << message_;
515 }
516
517 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest,
518                        MediaGalleriesRead) {
519   RemoveAllGalleries();
520   MakeSingleFakeGallery(NULL);
521   base::ListValue custom_args;
522   custom_args.AppendInteger(test_jpg_size());
523
524   ASSERT_TRUE(RunMediaGalleriesTestWithArg("read_access", custom_args))
525       << message_;
526 }
527
528 // Test is flaky, it fails on certain bots, namely WinXP Tests(1) and Linux
529 // (dbg)(1)(32).  See crbug.com/354425.
530 #if (defined(ARCH_CPU_X86)) && (defined(OS_WIN) || defined(OS_LINUX))
531 #define MAYBE_MediaGalleriesCopyTo DISABLED_MediaGalleriesCopyTo
532 #else
533 #define MAYBE_MediaGalleriesCopyTo MediaGalleriesCopyTo
534 #endif
535 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest,
536                        MAYBE_MediaGalleriesCopyTo) {
537   RemoveAllGalleries();
538   MakeSingleFakeGallery(NULL);
539   ASSERT_TRUE(RunMediaGalleriesTest("copy_to_access")) << message_;
540 }
541
542 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest,
543                        MediaGalleriesDelete) {
544   MakeSingleFakeGallery(NULL);
545   base::ListValue custom_args;
546   custom_args.AppendInteger(num_galleries() + 1);
547   ASSERT_TRUE(RunMediaGalleriesTestWithArg("delete_access", custom_args))
548       << message_;
549 }
550
551 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest,
552                        MediaGalleriesAccessAttached) {
553   AttachFakeDevice();
554
555   base::ListValue custom_args;
556   custom_args.AppendInteger(num_galleries() + 1);
557   custom_args.AppendString(kDeviceName);
558
559   ASSERT_TRUE(RunMediaGalleriesTestWithArg("access_attached", custom_args))
560       << message_;
561
562   DetachFakeDevice();
563 }
564
565 #if defined(OS_WIN)|| defined(OS_MACOSX)
566 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest,
567                        PicasaDefaultLocation) {
568 #if defined(OS_WIN)
569   PopulatePicasaTestData(
570       ensure_media_directories_exists()->GetFakeLocalAppDataPath());
571 #elif defined(OS_MACOSX)
572   PopulatePicasaTestData(
573       ensure_media_directories_exists()->GetFakeAppDataPath());
574 #endif
575
576   base::ListValue custom_args;
577   custom_args.AppendInteger(test_jpg_size());
578   ASSERT_TRUE(RunMediaGalleriesTestWithArg("picasa", custom_args)) << message_;
579 }
580
581 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest,
582                        PicasaCustomLocation) {
583   base::ScopedTempDir custom_picasa_app_data_root;
584   ASSERT_TRUE(custom_picasa_app_data_root.CreateUniqueTempDir());
585   ensure_media_directories_exists()->SetCustomPicasaAppDataPath(
586       custom_picasa_app_data_root.path());
587   PopulatePicasaTestData(custom_picasa_app_data_root.path());
588
589   base::ListValue custom_args;
590   custom_args.AppendInteger(test_jpg_size());
591   ASSERT_TRUE(RunMediaGalleriesTestWithArg("picasa", custom_args)) << message_;
592 }
593 #endif  // defined(OS_WIN) || defined(OS_MACOSX)
594
595 #if defined(OS_MACOSX)
596 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest,
597                        IPhotoTest) {
598   PopulateIPhotoTestData(
599       ensure_media_directories_exists()->GetFakeIPhotoRootPath());
600
601   base::ListValue custom_args;
602   custom_args.AppendInteger(test_jpg_size());
603   ASSERT_TRUE(RunMediaGalleriesTestWithArg("iphoto", custom_args)) << message_;
604
605   iapps::SetMacPreferencesForTesting(NULL);
606 }
607 #endif  // defined(OS_MACOSX)
608
609 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest, CancelScan) {
610   InstallDoNothingFolderFinder();
611   ASSERT_TRUE(RunMediaGalleriesTest("cancel_scan")) << message_;
612 }
613
614 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest, Scan) {
615   base::ScopedTempDir scan_root;
616   ASSERT_TRUE(scan_root.CreateUniqueTempDir());
617   std::vector<base::FilePath> roots;
618   roots.push_back(scan_root.path());
619   SetRootsForFolderFinder(roots);
620
621   // Override addScanResults so that the dialog is accepted as soon as it is
622   // created.
623   ASSERT_TRUE(extensions::ExtensionFunctionDispatcher::OverrideFunction(
624       "mediaGalleries.addScanResults",
625       &TestMediaGalleriesAddScanResultsFunction::Factory));
626
627   // Add some files and directories to the scan root for testing. Only the
628   // "f" directory should be found.
629   std::string dummy_data;
630   dummy_data.resize(1);
631   ASSERT_TRUE(base::CreateDirectory(scan_root.path().AppendASCII("a/b")));
632   ASSERT_EQ(static_cast<int>(dummy_data.size()),
633             base::WriteFile(scan_root.path().AppendASCII("a/b/c.jpg"),
634                             dummy_data.c_str(), dummy_data.size()));
635   ASSERT_TRUE(base::CreateDirectory(scan_root.path().AppendASCII("a/d")));
636   dummy_data.resize(201 * 1024);  // 200k is the min size for the folder finder.
637   ASSERT_EQ(static_cast<int>(dummy_data.size()),
638             base::WriteFile(scan_root.path().AppendASCII("a/d/e.txt"),
639                             dummy_data.c_str(), dummy_data.size()));
640   ASSERT_TRUE(base::CreateDirectory(scan_root.path().AppendASCII("f")));
641   ASSERT_EQ(static_cast<int>(dummy_data.size()),
642             base::WriteFile(scan_root.path().AppendASCII("f/g.jpg"),
643                             dummy_data.c_str(), dummy_data.size()));
644
645   ASSERT_TRUE(RunMediaGalleriesTest("scan")) << message_;
646 }
647
648 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest, ToURL) {
649   RemoveAllGalleries();
650   MediaGalleryPrefId pref_id;
651   MakeSingleFakeGallery(&pref_id);
652
653   base::ListValue custom_args;
654   custom_args.AppendInteger(base::checked_cast<int>(pref_id));
655   custom_args.AppendString(
656       browser()->profile()->GetPath().BaseName().MaybeAsASCII());
657
658   ASSERT_TRUE(RunMediaGalleriesTestWithArg("tourl", custom_args)) << message_;
659 }
660
661 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest, GetMetadata) {
662   RemoveAllGalleries();
663   MakeSingleFakeGallery(NULL);
664
665   AddFileToSingleFakeGallery(media::GetTestDataFilePath("90rotation.mp4"));
666   AddFileToSingleFakeGallery(media::GetTestDataFilePath("id3_png_test.mp3"));
667   AddFileToSingleFakeGallery(GetWallpaperTestDataDir().AppendASCII("test.jpg"));
668
669   base::ListValue custom_args;
670 #if defined(USE_PROPRIETARY_CODECS)
671   custom_args.AppendBoolean(true);
672 #else
673   custom_args.AppendBoolean(false);
674 #endif
675   ASSERT_TRUE(RunMediaGalleriesTestWithArg("media_metadata", custom_args))
676       << message_;
677 }