OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <stdint.h> | 5 #include <stdint.h> |
6 | 6 |
7 #include "base/auto_reset.h" | 7 #include "base/auto_reset.h" |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
10 #include "base/files/scoped_temp_dir.h" | 10 #include "base/files/scoped_temp_dir.h" |
11 #include "base/json/json_writer.h" | 11 #include "base/json/json_writer.h" |
12 #include "base/numerics/safe_conversions.h" | 12 #include "base/numerics/safe_conversions.h" |
13 #include "base/path_service.h" | 13 #include "base/path_service.h" |
14 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
15 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
16 #include "base/values.h" | 16 #include "base/values.h" |
17 #include "build/build_config.h" | 17 #include "build/build_config.h" |
18 #include "chrome/browser/apps/app_browsertest_util.h" | 18 #include "chrome/browser/apps/app_browsertest_util.h" |
19 #include "chrome/browser/browser_process.h" | 19 #include "chrome/browser/browser_process.h" |
20 #include "chrome/browser/extensions/api/media_galleries/media_galleries_api.h" | 20 #include "chrome/browser/extensions/api/media_galleries/media_galleries_api.h" |
21 #include "chrome/browser/media_galleries/media_file_system_registry.h" | 21 #include "chrome/browser/media_galleries/media_file_system_registry.h" |
22 #include "chrome/browser/media_galleries/media_folder_finder.h" | |
23 #include "chrome/browser/media_galleries/media_galleries_preferences.h" | 22 #include "chrome/browser/media_galleries/media_galleries_preferences.h" |
24 #include "chrome/browser/media_galleries/media_galleries_scan_result_controller.
h" | |
25 #include "chrome/browser/media_galleries/media_galleries_test_util.h" | 23 #include "chrome/browser/media_galleries/media_galleries_test_util.h" |
26 #include "chrome/browser/media_galleries/media_scan_manager.h" | |
27 #include "chrome/browser/ui/extensions/app_launch_params.h" | 24 #include "chrome/browser/ui/extensions/app_launch_params.h" |
28 #include "chrome/browser/ui/extensions/application_launch.h" | 25 #include "chrome/browser/ui/extensions/application_launch.h" |
29 #include "chrome/common/chrome_paths.h" | 26 #include "chrome/common/chrome_paths.h" |
30 #include "components/storage_monitor/storage_info.h" | 27 #include "components/storage_monitor/storage_info.h" |
31 #include "components/storage_monitor/storage_monitor.h" | 28 #include "components/storage_monitor/storage_monitor.h" |
32 #include "content/public/browser/web_contents.h" | 29 #include "content/public/browser/web_contents.h" |
33 #include "content/public/test/test_utils.h" | 30 #include "content/public/test/test_utils.h" |
34 #include "extensions/browser/extension_system.h" | 31 #include "extensions/browser/extension_system.h" |
35 #include "extensions/common/constants.h" | 32 #include "extensions/common/constants.h" |
36 #include "extensions/common/extension.h" | 33 #include "extensions/common/extension.h" |
(...skipping 27 matching lines...) Expand all Loading... |
64 | 61 |
65 // Dummy device properties. | 62 // Dummy device properties. |
66 const char kDeviceId[] = "testDeviceId"; | 63 const char kDeviceId[] = "testDeviceId"; |
67 const char kDeviceName[] = "foobar"; | 64 const char kDeviceName[] = "foobar"; |
68 #if defined(FILE_PATH_USES_DRIVE_LETTERS) | 65 #if defined(FILE_PATH_USES_DRIVE_LETTERS) |
69 base::FilePath::CharType kDevicePath[] = FILE_PATH_LITERAL("C:\\qux"); | 66 base::FilePath::CharType kDevicePath[] = FILE_PATH_LITERAL("C:\\qux"); |
70 #else | 67 #else |
71 base::FilePath::CharType kDevicePath[] = FILE_PATH_LITERAL("/qux"); | 68 base::FilePath::CharType kDevicePath[] = FILE_PATH_LITERAL("/qux"); |
72 #endif | 69 #endif |
73 | 70 |
74 class DoNothingMediaFolderFinder : public MediaFolderFinder { | |
75 public: | |
76 explicit DoNothingMediaFolderFinder( | |
77 const MediaFolderFinderResultsCallback& callback) | |
78 : MediaFolderFinder(callback) { | |
79 } | |
80 ~DoNothingMediaFolderFinder() override {} | |
81 | |
82 static MediaFolderFinder* CreateDoNothingMediaFolderFinder( | |
83 const MediaFolderFinderResultsCallback& callback) { | |
84 return new DoNothingMediaFolderFinder(callback); | |
85 } | |
86 | |
87 void StartScan() override {} | |
88 | |
89 private: | |
90 }; | |
91 | |
92 } // namespace | 71 } // namespace |
93 | 72 |
94 class TestMediaGalleriesAddScanResultsFunction | |
95 : public extensions::MediaGalleriesAddScanResultsFunction { | |
96 public: | |
97 static ExtensionFunction* Factory() { | |
98 return new TestMediaGalleriesAddScanResultsFunction; | |
99 } | |
100 | |
101 protected: | |
102 ~TestMediaGalleriesAddScanResultsFunction() override {} | |
103 | |
104 // Accepts the dialog as soon as it is created. | |
105 MediaGalleriesScanResultController* MakeDialog( | |
106 content::WebContents* web_contents, | |
107 const extensions::Extension& extension, | |
108 const base::Closure& on_finish) override { | |
109 MediaGalleriesScanResultController* controller = | |
110 extensions::MediaGalleriesAddScanResultsFunction::MakeDialog( | |
111 web_contents, extension, on_finish); | |
112 controller->dialog_->AcceptDialogForTesting(); | |
113 // The dialog is closing or closed so don't return it. | |
114 return NULL; | |
115 } | |
116 }; | |
117 | |
118 class MediaGalleriesPlatformAppBrowserTest : public PlatformAppBrowserTest { | 73 class MediaGalleriesPlatformAppBrowserTest : public PlatformAppBrowserTest { |
119 protected: | 74 protected: |
120 MediaGalleriesPlatformAppBrowserTest() : test_jpg_size_(0) {} | 75 MediaGalleriesPlatformAppBrowserTest() : test_jpg_size_(0) {} |
121 ~MediaGalleriesPlatformAppBrowserTest() override {} | 76 ~MediaGalleriesPlatformAppBrowserTest() override {} |
122 | 77 |
123 void SetUpOnMainThread() override { | 78 void SetUpOnMainThread() override { |
124 PlatformAppBrowserTest::SetUpOnMainThread(); | 79 PlatformAppBrowserTest::SetUpOnMainThread(); |
125 ensure_media_directories_exists_.reset(new EnsureMediaDirectoriesExists); | 80 ensure_media_directories_exists_.reset(new EnsureMediaDirectoriesExists); |
126 | 81 |
127 int64_t file_size; | 82 int64_t file_size; |
(...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
393 int num_galleries() const { | 348 int num_galleries() const { |
394 return ensure_media_directories_exists_->num_galleries(); | 349 return ensure_media_directories_exists_->num_galleries(); |
395 } | 350 } |
396 | 351 |
397 int test_jpg_size() const { return test_jpg_size_; } | 352 int test_jpg_size() const { return test_jpg_size_; } |
398 | 353 |
399 EnsureMediaDirectoriesExists* ensure_media_directories_exists() const { | 354 EnsureMediaDirectoriesExists* ensure_media_directories_exists() const { |
400 return ensure_media_directories_exists_.get(); | 355 return ensure_media_directories_exists_.get(); |
401 } | 356 } |
402 | 357 |
403 void InstallDoNothingFolderFinder() { | |
404 MediaScanManager * scan_manager = | |
405 g_browser_process->media_file_system_registry()->media_scan_manager(); | |
406 scan_manager->SetMediaFolderFinderFactory(base::Bind( | |
407 &DoNothingMediaFolderFinder::CreateDoNothingMediaFolderFinder)); | |
408 } | |
409 | |
410 void SetRootsForFolderFinder(const std::vector<base::FilePath>& roots) { | |
411 MediaScanManager* scan_manager = | |
412 g_browser_process->media_file_system_registry()->media_scan_manager(); | |
413 scan_manager->SetMediaFolderFinderFactory(base::Bind( | |
414 &MediaGalleriesPlatformAppBrowserTest::CreateMediaFolderFinderWithRoots, | |
415 roots)); | |
416 } | |
417 | |
418 private: | 358 private: |
419 static MediaFolderFinder* CreateMediaFolderFinderWithRoots( | |
420 const std::vector<base::FilePath>& roots, | |
421 const MediaFolderFinder::MediaFolderFinderResultsCallback& callback) { | |
422 MediaFolderFinder* finder = new MediaFolderFinder(callback); | |
423 finder->SetRootsForTesting(roots); | |
424 return finder; | |
425 } | |
426 | |
427 MediaGalleriesPreferences* GetAndInitializePreferences() { | 359 MediaGalleriesPreferences* GetAndInitializePreferences() { |
428 MediaGalleriesPreferences* preferences = | 360 MediaGalleriesPreferences* preferences = |
429 g_browser_process->media_file_system_registry()->GetPreferences( | 361 g_browser_process->media_file_system_registry()->GetPreferences( |
430 browser()->profile()); | 362 browser()->profile()); |
431 base::RunLoop runloop; | 363 base::RunLoop runloop; |
432 preferences->EnsureInitialized(runloop.QuitClosure()); | 364 preferences->EnsureInitialized(runloop.QuitClosure()); |
433 runloop.Run(); | 365 runloop.Run(); |
434 return preferences; | 366 return preferences; |
435 } | 367 } |
436 | 368 |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
614 ensure_media_directories_exists()->GetFakeIPhotoRootPath()); | 546 ensure_media_directories_exists()->GetFakeIPhotoRootPath()); |
615 | 547 |
616 base::ListValue custom_args; | 548 base::ListValue custom_args; |
617 custom_args.AppendInteger(test_jpg_size()); | 549 custom_args.AppendInteger(test_jpg_size()); |
618 ASSERT_TRUE(RunMediaGalleriesTestWithArg("iphoto", custom_args)) << message_; | 550 ASSERT_TRUE(RunMediaGalleriesTestWithArg("iphoto", custom_args)) << message_; |
619 | 551 |
620 iapps::SetMacPreferencesForTesting(NULL); | 552 iapps::SetMacPreferencesForTesting(NULL); |
621 } | 553 } |
622 #endif // defined(OS_MACOSX) | 554 #endif // defined(OS_MACOSX) |
623 | 555 |
624 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest, CancelScan) { | |
625 InstallDoNothingFolderFinder(); | |
626 ASSERT_TRUE(RunMediaGalleriesTest("cancel_scan")) << message_; | |
627 } | |
628 | |
629 // Flaky time outs on MSAN. https://crbug.com/503329 | |
630 #if defined(MEMORY_SANITIZER) | |
631 #define MAYBE_Scan DISABLED_Scan | |
632 #else | |
633 #define MAYBE_Scan Scan | |
634 #endif | |
635 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest, MAYBE_Scan) { | |
636 base::ScopedTempDir scan_root; | |
637 ASSERT_TRUE(scan_root.CreateUniqueTempDir()); | |
638 std::vector<base::FilePath> roots; | |
639 roots.push_back(scan_root.path()); | |
640 SetRootsForFolderFinder(roots); | |
641 | |
642 // Override addScanResults so that the dialog is accepted as soon as it is | |
643 // created. | |
644 ASSERT_TRUE(extensions::ExtensionFunctionDispatcher::OverrideFunction( | |
645 "mediaGalleries.addScanResults", | |
646 &TestMediaGalleriesAddScanResultsFunction::Factory)); | |
647 | |
648 // Add some files and directories to the scan root for testing. Only the | |
649 // "f" directory should be found. | |
650 std::string dummy_data; | |
651 dummy_data.resize(1); | |
652 ASSERT_TRUE(base::CreateDirectory(scan_root.path().AppendASCII("a/b"))); | |
653 ASSERT_EQ(static_cast<int>(dummy_data.size()), | |
654 base::WriteFile(scan_root.path().AppendASCII("a/b/c.jpg"), | |
655 dummy_data.c_str(), dummy_data.size())); | |
656 ASSERT_TRUE(base::CreateDirectory(scan_root.path().AppendASCII("a/d"))); | |
657 dummy_data.resize(201 * 1024); // 200k is the min size for the folder finder. | |
658 ASSERT_EQ(static_cast<int>(dummy_data.size()), | |
659 base::WriteFile(scan_root.path().AppendASCII("a/d/e.txt"), | |
660 dummy_data.c_str(), dummy_data.size())); | |
661 ASSERT_TRUE(base::CreateDirectory(scan_root.path().AppendASCII("f"))); | |
662 ASSERT_EQ(static_cast<int>(dummy_data.size()), | |
663 base::WriteFile(scan_root.path().AppendASCII("f/g.jpg"), | |
664 dummy_data.c_str(), dummy_data.size())); | |
665 | |
666 ASSERT_TRUE(RunMediaGalleriesTest("scan")) << message_; | |
667 } | |
668 | |
669 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest, ToURL) { | 556 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest, ToURL) { |
670 RemoveAllGalleries(); | 557 RemoveAllGalleries(); |
671 MediaGalleryPrefId pref_id; | 558 MediaGalleryPrefId pref_id; |
672 MakeSingleFakeGallery(&pref_id); | 559 MakeSingleFakeGallery(&pref_id); |
673 | 560 |
674 base::ListValue custom_args; | 561 base::ListValue custom_args; |
675 custom_args.AppendInteger(base::checked_cast<int>(pref_id)); | 562 custom_args.AppendInteger(base::checked_cast<int>(pref_id)); |
676 custom_args.AppendString( | 563 custom_args.AppendString( |
677 browser()->profile()->GetPath().BaseName().MaybeAsASCII()); | 564 browser()->profile()->GetPath().BaseName().MaybeAsASCII()); |
678 | 565 |
(...skipping 10 matching lines...) Expand all Loading... |
689 | 576 |
690 base::ListValue custom_args; | 577 base::ListValue custom_args; |
691 #if defined(USE_PROPRIETARY_CODECS) | 578 #if defined(USE_PROPRIETARY_CODECS) |
692 custom_args.AppendBoolean(true); | 579 custom_args.AppendBoolean(true); |
693 #else | 580 #else |
694 custom_args.AppendBoolean(false); | 581 custom_args.AppendBoolean(false); |
695 #endif | 582 #endif |
696 ASSERT_TRUE(RunMediaGalleriesTestWithArg("media_metadata", custom_args)) | 583 ASSERT_TRUE(RunMediaGalleriesTestWithArg("media_metadata", custom_args)) |
697 << message_; | 584 << message_; |
698 } | 585 } |
OLD | NEW |