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/bind.h" 6 #include "base/bind_helpers.h" 7 #include "base/command_line.h" 8 #include "base/files/scoped_temp_dir.h" 9 #include "base/strings/stringprintf.h" 10 #include "base/strings/utf_string_conversions.h" 11 #include "chrome/browser/browser_process.h" 12 #include "chrome/browser/extensions/extension_apitest.h" 13 #include "chrome/browser/extensions/extension_process_manager.h" 14 #include "chrome/browser/extensions/extension_system.h" 15 #include "chrome/browser/extensions/platform_app_browsertest_util.h" 16 #include "chrome/browser/media_galleries/media_file_system_registry.h" 17 #include "chrome/browser/media_galleries/media_galleries_preferences.h" 18 #include "chrome/browser/media_galleries/media_galleries_test_util.h" 19 #include "chrome/browser/storage_monitor/storage_info.h" 20 #include "chrome/browser/storage_monitor/storage_monitor.h" 21 #include "chrome/browser/ui/browser.h" 22 #include "content/public/browser/render_view_host.h" 23 #include "content/public/test/test_utils.h" 24 #include "extensions/common/switches.h" 25 26 using extensions::PlatformAppBrowserTest; 27 28 namespace { 29 30 // Dummy device properties. 31 const char kDeviceId[] = "testDeviceId"; 32 const char kDeviceName[] = "foobar"; 33 #if defined(FILE_PATH_USES_DRIVE_LETTERS) 34 base::FilePath::CharType kDevicePath[] = FILE_PATH_LITERAL("C:\\qux"); 35 #else 36 base::FilePath::CharType kDevicePath[] = FILE_PATH_LITERAL("/qux"); 37 #endif 38 39 const char kTestGalleries[] = "testGalleries(%d)"; 40 41 } // namespace 42 43 // This function is to ensure at least one (fake) media gallery exists for 44 // testing platforms with no default media galleries, such as CHROMEOS. 45 void MakeFakeMediaGalleryForTest(Profile* profile, const base::FilePath& path) { 46 base::RunLoop runloop; 47 chrome::StorageMonitor::GetInstance()->EnsureInitialized( 48 runloop.QuitClosure()); 49 runloop.Run(); 50 51 chrome::MediaGalleriesPreferences* preferences = 52 g_browser_process->media_file_system_registry()->GetPreferences(profile); 53 54 chrome::MediaGalleryPrefInfo gallery_info; 55 ASSERT_FALSE(preferences->LookUpGalleryByPath(path, &gallery_info)); 56 preferences->AddGallery(gallery_info.device_id, 57 gallery_info.path, 58 false /* user_added */, 59 gallery_info.volume_label, 60 gallery_info.vendor_name, 61 gallery_info.model_name, 62 gallery_info.total_size_in_bytes, 63 gallery_info.last_attach_time); 64 65 content::RunAllPendingInMessageLoop(); 66 } 67 68 class MediaGalleriesPlatformAppBrowserTest : public PlatformAppBrowserTest { 69 protected: 70 // Since ExtensionTestMessageListener does not work with RunPlatformAppTest(), 71 // This helper method can be used to run additional media gallery tests. 72 void RunSecondTestPhase(const string16& command) { 73 const extensions::Extension* extension = GetSingleLoadedExtension(); 74 extensions::ExtensionHost* host = 75 extensions::ExtensionSystem::Get(browser()->profile())-> 76 process_manager()->GetBackgroundHostForExtension(extension->id()); 77 ASSERT_TRUE(host); 78 79 ResultCatcher catcher; 80 host->render_view_host()->ExecuteJavascriptInWebFrame(string16(), command); 81 EXPECT_TRUE(catcher.GetNextResult()) << message_; 82 } 83 84 void AttachFakeDevice() { 85 device_id_ = chrome::StorageInfo::MakeDeviceId( 86 chrome::StorageInfo::REMOVABLE_MASS_STORAGE_WITH_DCIM, kDeviceId); 87 88 chrome::StorageMonitor::GetInstance()->receiver()->ProcessAttach( 89 chrome::StorageInfo(device_id_, string16(), kDevicePath, 90 ASCIIToUTF16(kDeviceName), 91 string16(), string16(), 0)); 92 content::RunAllPendingInMessageLoop(); 93 } 94 95 void DetachFakeDevice() { 96 chrome::StorageMonitor::GetInstance()->receiver()->ProcessDetach( 97 device_id_); 98 content::RunAllPendingInMessageLoop(); 99 } 100 101 private: 102 std::string device_id_; 103 }; 104 105 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest, 106 MediaGalleriesNoAccess) { 107 chrome::EnsureMediaDirectoriesExists media_directories; 108 ASSERT_TRUE(RunPlatformAppTest("api_test/media_galleries/no_access")) 109 << message_; 110 RunSecondTestPhase(base::UTF8ToUTF16(base::StringPrintf( 111 kTestGalleries, media_directories.num_galleries()))); 112 } 113 114 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest, NoGalleriesRead) { 115 chrome::EnsureMediaDirectoriesExists media_directories; 116 ASSERT_TRUE(RunPlatformAppTest("api_test/media_galleries/no_galleries")) 117 << message_; 118 } 119 120 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest, 121 NoGalleriesCopyTo) { 122 chrome::EnsureMediaDirectoriesExists media_directories; 123 ASSERT_TRUE(RunPlatformAppTest( 124 "api_test/media_galleries/no_galleries_copy_to")) << message_; 125 } 126 127 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest, 128 MediaGalleriesRead) { 129 chrome::EnsureMediaDirectoriesExists media_directories; 130 ASSERT_TRUE(RunPlatformAppTest("api_test/media_galleries/read_access")) 131 << message_; 132 RunSecondTestPhase(base::UTF8ToUTF16(base::StringPrintf( 133 kTestGalleries, media_directories.num_galleries()))); 134 } 135 136 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest, 137 MediaGalleriesCopyTo) { 138 chrome::EnsureMediaDirectoriesExists media_directories; 139 base::ScopedTempDir temp_dir; 140 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 141 MakeFakeMediaGalleryForTest(browser()->profile(), temp_dir.path()); 142 ASSERT_TRUE(RunPlatformAppTest("api_test/media_galleries/copy_to_access")) 143 << message_; 144 } 145 146 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest, 147 MediaGalleriesCopyToNoAccess) { 148 chrome::EnsureMediaDirectoriesExists media_directories; 149 base::ScopedTempDir temp_dir; 150 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 151 MakeFakeMediaGalleryForTest(browser()->profile(), temp_dir.path()); 152 ASSERT_TRUE(RunPlatformAppTest( 153 "api_test/media_galleries/copy_to_access/no_access")) 154 << message_; 155 } 156 157 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest, 158 MediaGalleriesAccessAttached) { 159 chrome::EnsureMediaDirectoriesExists media_directories; 160 161 AttachFakeDevice(); 162 163 ASSERT_TRUE(RunPlatformAppTest("api_test/media_galleries/access_attached")) 164 << message_; 165 166 RunSecondTestPhase(ASCIIToUTF16(base::StringPrintf( 167 "testGalleries(%d, \"%s\")", 168 media_directories.num_galleries() + 1, kDeviceName))); 169 170 DetachFakeDevice(); 171 } 172 173 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest, 174 GetFilesystemMetadata) { 175 chrome::EnsureMediaDirectoriesExists media_directories; 176 ASSERT_TRUE(RunPlatformAppTest("api_test/media_galleries/metadata")) 177 << message_; 178 } 179