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 "chromeos/disks/mock_disk_mount_manager.h" 6 7 #include <utility> 8 9 #include "base/message_loop/message_loop.h" 10 #include "base/stl_util.h" 11 #include "base/strings/string_util.h" 12 13 using testing::_; 14 using testing::AnyNumber; 15 using testing::Invoke; 16 using testing::ReturnRef; 17 18 namespace chromeos { 19 namespace disks { 20 21 namespace { 22 23 const char* kTestSystemPath = "/this/system/path"; 24 const char* kTestSystemPathPrefix = "/this/system"; 25 const char* kTestDevicePath = "/this/device/path"; 26 const char* kTestMountPath = "/media/foofoo"; 27 const char* kTestFilePath = "/this/file/path"; 28 const char* kTestDeviceLabel = "A label"; 29 const char* kTestDriveLabel = "Another label"; 30 const char* kTestVendorId = "0123"; 31 const char* kTestVendorName = "A vendor"; 32 const char* kTestProductId = "abcd"; 33 const char* kTestProductName = "A product"; 34 const char* kTestUuid = "FFFF-FFFF"; 35 36 } // namespace 37 38 void MockDiskMountManager::AddObserverInternal( 39 DiskMountManager::Observer* observer) { 40 observers_.AddObserver(observer); 41 } 42 43 void MockDiskMountManager::RemoveObserverInternal( 44 DiskMountManager::Observer* observer) { 45 observers_.RemoveObserver(observer); 46 } 47 48 MockDiskMountManager::MockDiskMountManager() { 49 ON_CALL(*this, AddObserver(_)) 50 .WillByDefault(Invoke(this, &MockDiskMountManager::AddObserverInternal)); 51 ON_CALL(*this, RemoveObserver(_)) 52 .WillByDefault(Invoke(this, 53 &MockDiskMountManager::RemoveObserverInternal)); 54 ON_CALL(*this, disks()) 55 .WillByDefault(Invoke(this, &MockDiskMountManager::disksInternal)); 56 ON_CALL(*this, mount_points()) 57 .WillByDefault(Invoke(this, &MockDiskMountManager::mountPointsInternal)); 58 ON_CALL(*this, FindDiskBySourcePath(_)) 59 .WillByDefault(Invoke( 60 this, &MockDiskMountManager::FindDiskBySourcePathInternal)); 61 } 62 63 MockDiskMountManager::~MockDiskMountManager() { 64 STLDeleteContainerPairSecondPointers(disks_.begin(), disks_.end()); 65 disks_.clear(); 66 } 67 68 void MockDiskMountManager::NotifyDeviceInsertEvents() { 69 scoped_ptr<DiskMountManager::Disk> disk1(new DiskMountManager::Disk( 70 std::string(kTestDevicePath), 71 std::string(), 72 std::string(kTestSystemPath), 73 std::string(kTestFilePath), 74 std::string(), 75 std::string(kTestDriveLabel), 76 std::string(kTestVendorId), 77 std::string(kTestVendorName), 78 std::string(kTestProductId), 79 std::string(kTestProductName), 80 std::string(kTestUuid), 81 std::string(kTestSystemPathPrefix), 82 DEVICE_TYPE_USB, 83 4294967295U, 84 false, // is_parent 85 false, // is_read_only 86 true, // has_media 87 false, // on_boot_device 88 false)); // is_hidden 89 90 disks_.clear(); 91 disks_.insert(std::pair<std::string, DiskMountManager::Disk*>( 92 std::string(kTestDevicePath), disk1.get())); 93 94 // Device Added 95 NotifyDeviceChanged(DEVICE_ADDED, kTestSystemPath); 96 97 // Disk Added 98 NotifyDiskChanged(DISK_ADDED, disk1.get()); 99 100 // Disk Changed 101 scoped_ptr<DiskMountManager::Disk> disk2(new DiskMountManager::Disk( 102 std::string(kTestDevicePath), 103 std::string(kTestMountPath), 104 std::string(kTestSystemPath), 105 std::string(kTestFilePath), 106 std::string(kTestDeviceLabel), 107 std::string(kTestDriveLabel), 108 std::string(kTestVendorId), 109 std::string(kTestVendorName), 110 std::string(kTestProductId), 111 std::string(kTestProductName), 112 std::string(kTestUuid), 113 std::string(kTestSystemPathPrefix), 114 DEVICE_TYPE_MOBILE, 115 1073741824, 116 false, // is_parent 117 false, // is_read_only 118 true, // has_media 119 false, // on_boot_device 120 false)); // is_hidden 121 disks_.clear(); 122 disks_.insert(std::pair<std::string, DiskMountManager::Disk*>( 123 std::string(kTestDevicePath), disk2.get())); 124 NotifyDiskChanged(DISK_CHANGED, disk2.get()); 125 } 126 127 void MockDiskMountManager::NotifyDeviceRemoveEvents() { 128 scoped_ptr<DiskMountManager::Disk> disk(new DiskMountManager::Disk( 129 std::string(kTestDevicePath), 130 std::string(kTestMountPath), 131 std::string(kTestSystemPath), 132 std::string(kTestFilePath), 133 std::string(kTestDeviceLabel), 134 std::string(kTestDriveLabel), 135 std::string(kTestVendorId), 136 std::string(kTestVendorName), 137 std::string(kTestProductId), 138 std::string(kTestProductName), 139 std::string(kTestUuid), 140 std::string(kTestSystemPathPrefix), 141 DEVICE_TYPE_SD, 142 1073741824, 143 false, // is_parent 144 false, // is_read_only 145 true, // has_media 146 false, // on_boot_device 147 false)); // is_hidden 148 disks_.clear(); 149 disks_.insert(std::pair<std::string, DiskMountManager::Disk*>( 150 std::string(kTestDevicePath), disk.get())); 151 NotifyDiskChanged(DISK_REMOVED, disk.get()); 152 } 153 154 void MockDiskMountManager::SetupDefaultReplies() { 155 EXPECT_CALL(*this, AddObserver(_)) 156 .Times(AnyNumber()); 157 EXPECT_CALL(*this, RemoveObserver(_)) 158 .Times(AnyNumber()); 159 EXPECT_CALL(*this, disks()) 160 .WillRepeatedly(ReturnRef(disks_)); 161 EXPECT_CALL(*this, mount_points()) 162 .WillRepeatedly(ReturnRef(mount_points_)); 163 EXPECT_CALL(*this, FindDiskBySourcePath(_)) 164 .Times(AnyNumber()); 165 EXPECT_CALL(*this, RequestMountInfoRefresh()) 166 .Times(AnyNumber()); 167 EXPECT_CALL(*this, MountPath(_, _, _, _)) 168 .Times(AnyNumber()); 169 EXPECT_CALL(*this, UnmountPath(_, _, _)) 170 .Times(AnyNumber()); 171 EXPECT_CALL(*this, FormatMountedDevice(_)) 172 .Times(AnyNumber()); 173 EXPECT_CALL(*this, UnmountDeviceRecursively(_, _)) 174 .Times(AnyNumber()); 175 } 176 177 void MockDiskMountManager::CreateDiskEntryForMountDevice( 178 const DiskMountManager::MountPointInfo& mount_info, 179 const std::string& device_id, 180 const std::string& device_label, 181 const std::string& vendor_name, 182 const std::string& product_name, 183 DeviceType device_type, 184 uint64 total_size_in_bytes, 185 bool is_parent, 186 bool has_media, 187 bool on_boot_device) { 188 Disk* disk = new DiskMountManager::Disk(mount_info.source_path, 189 mount_info.mount_path, 190 std::string(), // system_path 191 mount_info.source_path, 192 device_label, 193 std::string(), // drive_label 194 std::string(), // vendor_id 195 vendor_name, 196 std::string(), // product_id 197 product_name, 198 device_id, // fs_uuid 199 std::string(), // system_path_prefix 200 device_type, 201 total_size_in_bytes, 202 is_parent, 203 false, // is_read_only 204 has_media, 205 on_boot_device, 206 false); // is_hidden 207 DiskMountManager::DiskMap::iterator it = disks_.find(mount_info.source_path); 208 if (it == disks_.end()) { 209 disks_.insert(std::make_pair(std::string(mount_info.source_path), disk)); 210 } else { 211 delete it->second; 212 it->second = disk; 213 } 214 } 215 216 void MockDiskMountManager::RemoveDiskEntryForMountDevice( 217 const DiskMountManager::MountPointInfo& mount_info) { 218 DiskMountManager::DiskMap::iterator it = disks_.find(mount_info.source_path); 219 if (it != disks_.end()) { 220 delete it->second; 221 disks_.erase(it); 222 } 223 } 224 225 const DiskMountManager::MountPointMap& 226 MockDiskMountManager::mountPointsInternal() const { 227 return mount_points_; 228 } 229 230 const DiskMountManager::Disk* 231 MockDiskMountManager::FindDiskBySourcePathInternal( 232 const std::string& source_path) const { 233 DiskMap::const_iterator disk_it = disks_.find(source_path); 234 return disk_it == disks_.end() ? NULL : disk_it->second; 235 } 236 237 void MockDiskMountManager::NotifyDiskChanged( 238 DiskEvent event, 239 const DiskMountManager::Disk* disk) { 240 FOR_EACH_OBSERVER(Observer, observers_, OnDiskEvent(event, disk)); 241 } 242 243 void MockDiskMountManager::NotifyDeviceChanged(DeviceEvent event, 244 const std::string& path) { 245 FOR_EACH_OBSERVER(Observer, observers_, OnDeviceEvent(event, path)); 246 } 247 248 } // namespace disks 249 } // namespace chromeos 250