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 <vector> 6 7 #include "base/command_line.h" 8 #include "base/files/file_path.h" 9 #include "base/message_loop/message_loop.h" 10 #include "base/run_loop.h" 11 #include "base/stl_util.h" 12 #include "chrome/browser/browser_process.h" 13 #include "chrome/browser/extensions/extension_service.h" 14 #include "chrome/browser/extensions/extension_system.h" 15 #include "chrome/browser/extensions/test_extension_system.h" 16 #include "chrome/browser/media_galleries/media_file_system_registry.h" 17 #include "chrome/browser/media_galleries/media_galleries_test_util.h" 18 #include "chrome/browser/storage_monitor/storage_info.h" 19 #include "chrome/browser/storage_monitor/storage_monitor.h" 20 #include "chrome/browser/storage_monitor/test_portable_device_watcher_win.h" 21 #include "chrome/browser/storage_monitor/test_storage_monitor.h" 22 #include "chrome/browser/storage_monitor/test_storage_monitor_win.h" 23 #include "chrome/browser/storage_monitor/test_volume_mount_watcher_win.h" 24 #include "chrome/test/base/chrome_render_view_host_test_harness.h" 25 #include "chrome/test/base/testing_browser_process.h" 26 #include "chrome/test/base/testing_profile.h" 27 #include "content/public/browser/render_view_host.h" 28 #include "content/public/browser/web_contents.h" 29 #include "testing/gtest/include/gtest/gtest.h" 30 31 namespace { 32 33 typedef std::map<MediaGalleryPrefId, MediaFileSystemInfo> FSInfoMap; 34 35 void GetGalleryInfoCallback( 36 FSInfoMap* results, 37 const std::vector<MediaFileSystemInfo>& file_systems) { 38 for (size_t i = 0; i < file_systems.size(); ++i) { 39 ASSERT_FALSE(ContainsKey(*results, file_systems[i].pref_id)); 40 (*results)[file_systems[i].pref_id] = file_systems[i]; 41 } 42 } 43 44 } // namespace 45 46 class MTPDeviceDelegateImplWinTest : public ChromeRenderViewHostTestHarness { 47 protected: 48 void SetUp() OVERRIDE; 49 void TearDown() OVERRIDE; 50 51 void ProcessAttach(const std::string& id, 52 const base::string16& name, 53 const base::FilePath::StringType& location); 54 std::string AttachDevice(StorageInfo::Type type, 55 const std::string& unique_id, 56 const base::FilePath& location); 57 void CheckGalleryInfo(const MediaFileSystemInfo& info, 58 const base::string16& name, 59 const base::FilePath& path, 60 bool removable, 61 bool media_device); 62 63 // Pointer to the storage monitor. Owned by TestingBrowserProcess. 64 TestStorageMonitorWin* monitor_; 65 scoped_refptr<extensions::Extension> extension_; 66 67 EnsureMediaDirectoriesExists media_directories_; 68 }; 69 70 void MTPDeviceDelegateImplWinTest::SetUp() { 71 ChromeRenderViewHostTestHarness::SetUp(); 72 73 TestStorageMonitor::RemoveSingleton(); 74 TestPortableDeviceWatcherWin* portable_device_watcher = 75 new TestPortableDeviceWatcherWin; 76 TestVolumeMountWatcherWin* mount_watcher = new TestVolumeMountWatcherWin; 77 portable_device_watcher->set_use_dummy_mtp_storage_info(true); 78 scoped_ptr<TestStorageMonitorWin> monitor( 79 new TestStorageMonitorWin(mount_watcher, portable_device_watcher)); 80 TestingBrowserProcess* browser_process = TestingBrowserProcess::GetGlobal(); 81 DCHECK(browser_process); 82 monitor_ = monitor.get(); 83 browser_process->SetStorageMonitor(monitor.Pass()); 84 85 base::RunLoop runloop; 86 browser_process->media_file_system_registry()->GetPreferences(profile())-> 87 EnsureInitialized(runloop.QuitClosure()); 88 runloop.Run(); 89 90 extensions::TestExtensionSystem* extension_system( 91 static_cast<extensions::TestExtensionSystem*>( 92 extensions::ExtensionSystem::Get(profile()))); 93 extension_system->CreateExtensionService( 94 CommandLine::ForCurrentProcess(), base::FilePath(), false); 95 96 std::vector<std::string> all_permissions; 97 all_permissions.push_back("allAutoDetected"); 98 all_permissions.push_back("read"); 99 extension_ = AddMediaGalleriesApp("all", all_permissions, profile()); 100 } 101 102 void MTPDeviceDelegateImplWinTest::TearDown() { 103 // Windows storage monitor must be destroyed on the same thread 104 // as construction. 105 TestStorageMonitor::RemoveSingleton(); 106 107 ChromeRenderViewHostTestHarness::TearDown(); 108 } 109 110 void MTPDeviceDelegateImplWinTest::ProcessAttach( 111 const std::string& id, 112 const base::string16& label, 113 const base::FilePath::StringType& location) { 114 StorageInfo info(id, base::string16(), location, label, base::string16(), 115 base::string16(), 0); 116 monitor_->receiver()->ProcessAttach(info); 117 } 118 119 std::string MTPDeviceDelegateImplWinTest::AttachDevice( 120 StorageInfo::Type type, 121 const std::string& unique_id, 122 const base::FilePath& location) { 123 std::string device_id = StorageInfo::MakeDeviceId(type, unique_id); 124 DCHECK(StorageInfo::IsRemovableDevice(device_id)); 125 base::string16 label = location.LossyDisplayName(); 126 ProcessAttach(device_id, label, location.value()); 127 base::RunLoop().RunUntilIdle(); 128 return device_id; 129 } 130 131 void MTPDeviceDelegateImplWinTest::CheckGalleryInfo( 132 const MediaFileSystemInfo& info, 133 const base::string16& name, 134 const base::FilePath& path, 135 bool removable, 136 bool media_device) { 137 EXPECT_EQ(name, info.name); 138 EXPECT_EQ(path, info.path); 139 EXPECT_EQ(removable, info.removable); 140 EXPECT_EQ(media_device, info.media_device); 141 EXPECT_NE(0UL, info.pref_id); 142 143 if (removable) 144 EXPECT_NE(0UL, info.transient_device_id.size()); 145 else 146 EXPECT_EQ(0UL, info.transient_device_id.size()); 147 } 148 149 TEST_F(MTPDeviceDelegateImplWinTest, GalleryNameMTP) { 150 base::FilePath location( 151 PortableDeviceWatcherWin::GetStoragePathFromStorageId( 152 TestPortableDeviceWatcherWin::kStorageUniqueIdA)); 153 AttachDevice(StorageInfo::MTP_OR_PTP, "mtp_fake_id", location); 154 155 content::RenderViewHost* rvh = web_contents()->GetRenderViewHost(); 156 FSInfoMap results; 157 MediaFileSystemRegistry* registry = 158 g_browser_process->media_file_system_registry(); 159 registry->GetMediaFileSystemsForExtension( 160 rvh, extension_.get(), 161 base::Bind(&GetGalleryInfoCallback, base::Unretained(&results))); 162 base::RunLoop().RunUntilIdle(); 163 164 ASSERT_EQ(media_directories_.num_galleries() + 1, results.size()); 165 bool checked = false; 166 for (FSInfoMap::iterator i = results.begin(); i != results.end(); ++i) { 167 MediaFileSystemInfo info = i->second; 168 if (info.path == location) { 169 CheckGalleryInfo(info, location.LossyDisplayName(), location, true, true); 170 checked = true; 171 break; 172 } 173 } 174 EXPECT_TRUE(checked); 175 } 176