mtp_device_delegate_impl_win_unittest.cc revision eb525c5499e34cc9c4b825d6d9e75bb07cc06ace
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.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_win.h" 22#include "chrome/browser/storage_monitor/test_volume_mount_watcher_win.h" 23#include "chrome/test/base/chrome_render_view_host_test_harness.h" 24#include "chrome/test/base/testing_profile.h" 25#include "content/public/browser/render_view_host.h" 26#include "content/public/browser/web_contents.h" 27#include "testing/gtest/include/gtest/gtest.h" 28 29namespace chrome { 30 31namespace { 32 33typedef std::map<MediaGalleryPrefId, MediaFileSystemInfo> FSInfoMap; 34 35void 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 46class MTPDeviceDelegateImplWinTest : public ChromeRenderViewHostTestHarness { 47 protected: 48 void SetUp() OVERRIDE; 49 void TearDown() OVERRIDE; 50 51 void ProcessAttach(const std::string& id, 52 const 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 string16& name, 59 const base::FilePath& path, 60 bool removable, 61 bool media_device); 62 63 // Note: need to make this weak ptr once ownership moves to g_browser_process 64 scoped_ptr<test::TestStorageMonitorWin> monitor_; 65 scoped_refptr<extensions::Extension> extension_; 66 67 EnsureMediaDirectoriesExists media_directories_; 68}; 69 70void MTPDeviceDelegateImplWinTest::SetUp() { 71 ChromeRenderViewHostTestHarness::SetUp(); 72 test::TestPortableDeviceWatcherWin* portable_device_watcher = 73 new test::TestPortableDeviceWatcherWin; 74 test::TestVolumeMountWatcherWin* mount_watcher = 75 new test::TestVolumeMountWatcherWin; 76 portable_device_watcher->set_use_dummy_mtp_storage_info(true); 77 monitor_.reset(new test::TestStorageMonitorWin( 78 mount_watcher, portable_device_watcher)); 79 base::RunLoop runloop; 80 monitor_->EnsureInitialized(runloop.QuitClosure()); 81 runloop.Run(); 82 83 extensions::TestExtensionSystem* extension_system( 84 static_cast<extensions::TestExtensionSystem*>( 85 extensions::ExtensionSystem::Get(profile()))); 86 extension_system->CreateExtensionService( 87 CommandLine::ForCurrentProcess(), base::FilePath(), false); 88 89 std::vector<std::string> all_permissions; 90 all_permissions.push_back("allAutoDetected"); 91 all_permissions.push_back("read"); 92 extension_ = AddMediaGalleriesApp("all", all_permissions, profile()); 93} 94 95void MTPDeviceDelegateImplWinTest::TearDown() { 96 monitor_.reset(); 97 98 ChromeRenderViewHostTestHarness::TearDown(); 99} 100 101void MTPDeviceDelegateImplWinTest::ProcessAttach( 102 const std::string& id, 103 const string16& label, 104 const base::FilePath::StringType& location) { 105 StorageInfo info(id, string16(), location, label, string16(), string16(), 0); 106 monitor_->receiver()->ProcessAttach(info); 107} 108 109std::string MTPDeviceDelegateImplWinTest::AttachDevice( 110 StorageInfo::Type type, 111 const std::string& unique_id, 112 const base::FilePath& location) { 113 std::string device_id = StorageInfo::MakeDeviceId(type, unique_id); 114 DCHECK(StorageInfo::IsRemovableDevice(device_id)); 115 string16 label = location.LossyDisplayName(); 116 ProcessAttach(device_id, label, location.value()); 117 base::RunLoop().RunUntilIdle(); 118 return device_id; 119} 120 121void MTPDeviceDelegateImplWinTest::CheckGalleryInfo( 122 const MediaFileSystemInfo& info, 123 const string16& name, 124 const base::FilePath& path, 125 bool removable, 126 bool media_device) { 127 EXPECT_EQ(name, info.name); 128 EXPECT_EQ(path, info.path); 129 EXPECT_EQ(removable, info.removable); 130 EXPECT_EQ(media_device, info.media_device); 131 EXPECT_NE(0UL, info.pref_id); 132 133 if (removable) 134 EXPECT_NE(0UL, info.transient_device_id.size()); 135 else 136 EXPECT_EQ(0UL, info.transient_device_id.size()); 137} 138 139TEST_F(MTPDeviceDelegateImplWinTest, GalleryNameMTP) { 140 base::FilePath location( 141 PortableDeviceWatcherWin::GetStoragePathFromStorageId( 142 test::TestPortableDeviceWatcherWin::kStorageUniqueIdA)); 143 AttachDevice(StorageInfo::MTP_OR_PTP, "mtp_fake_id", location); 144 145 content::RenderViewHost* rvh = web_contents()->GetRenderViewHost(); 146 FSInfoMap results; 147 MediaFileSystemRegistry* registry = 148 g_browser_process->media_file_system_registry(); 149 registry->GetMediaFileSystemsForExtension( 150 rvh, extension_.get(), 151 base::Bind(&GetGalleryInfoCallback, base::Unretained(&results))); 152 base::RunLoop().RunUntilIdle(); 153 154 ASSERT_EQ(media_directories_.num_galleries() + 1, results.size()); 155 bool checked = false; 156 for (FSInfoMap::iterator i = results.begin(); i != results.end(); ++i) { 157 MediaFileSystemInfo info = i->second; 158 if (info.path == location) { 159 CheckGalleryInfo(info, location.LossyDisplayName(), location, true, true); 160 checked = true; 161 break; 162 } 163 } 164 EXPECT_TRUE(checked); 165} 166 167} // namespace chrome 168