Home | History | Annotate | Download | only in disks
      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   ON_CALL(*this, EnsureMountInfoRefreshed(_))
     62       .WillByDefault(Invoke(
     63           this, &MockDiskMountManager::EnsureMountInfoRefreshedInternal));
     64 }
     65 
     66 MockDiskMountManager::~MockDiskMountManager() {
     67   STLDeleteContainerPairSecondPointers(disks_.begin(), disks_.end());
     68   disks_.clear();
     69 }
     70 
     71 void MockDiskMountManager::NotifyDeviceInsertEvents() {
     72   scoped_ptr<DiskMountManager::Disk> disk1(new DiskMountManager::Disk(
     73       std::string(kTestDevicePath),
     74       std::string(),
     75       std::string(kTestSystemPath),
     76       std::string(kTestFilePath),
     77       std::string(),
     78       std::string(kTestDriveLabel),
     79       std::string(kTestVendorId),
     80       std::string(kTestVendorName),
     81       std::string(kTestProductId),
     82       std::string(kTestProductName),
     83       std::string(kTestUuid),
     84       std::string(kTestSystemPathPrefix),
     85       DEVICE_TYPE_USB,
     86       4294967295U,
     87       false,  // is_parent
     88       false,  // is_read_only
     89       true,  // has_media
     90       false,  // on_boot_device
     91       true,  // on_removable_device
     92       false));  // is_hidden
     93 
     94   disks_.clear();
     95   disks_.insert(std::pair<std::string, DiskMountManager::Disk*>(
     96       std::string(kTestDevicePath), disk1.get()));
     97 
     98   // Device Added
     99   NotifyDeviceChanged(DEVICE_ADDED, kTestSystemPath);
    100 
    101   // Disk Added
    102   NotifyDiskChanged(DISK_ADDED, disk1.get());
    103 
    104   // Disk Changed
    105   scoped_ptr<DiskMountManager::Disk> disk2(new DiskMountManager::Disk(
    106       std::string(kTestDevicePath),
    107       std::string(kTestMountPath),
    108       std::string(kTestSystemPath),
    109       std::string(kTestFilePath),
    110       std::string(kTestDeviceLabel),
    111       std::string(kTestDriveLabel),
    112       std::string(kTestVendorId),
    113       std::string(kTestVendorName),
    114       std::string(kTestProductId),
    115       std::string(kTestProductName),
    116       std::string(kTestUuid),
    117       std::string(kTestSystemPathPrefix),
    118       DEVICE_TYPE_MOBILE,
    119       1073741824,
    120       false,  // is_parent
    121       false,  // is_read_only
    122       true,  // has_media
    123       false,  // on_boot_device
    124       true,  // on_removable_device
    125       false));  // is_hidden
    126   disks_.clear();
    127   disks_.insert(std::pair<std::string, DiskMountManager::Disk*>(
    128       std::string(kTestDevicePath), disk2.get()));
    129   NotifyDiskChanged(DISK_CHANGED, disk2.get());
    130 }
    131 
    132 void MockDiskMountManager::NotifyDeviceRemoveEvents() {
    133   scoped_ptr<DiskMountManager::Disk> disk(new DiskMountManager::Disk(
    134       std::string(kTestDevicePath),
    135       std::string(kTestMountPath),
    136       std::string(kTestSystemPath),
    137       std::string(kTestFilePath),
    138       std::string(kTestDeviceLabel),
    139       std::string(kTestDriveLabel),
    140       std::string(kTestVendorId),
    141       std::string(kTestVendorName),
    142       std::string(kTestProductId),
    143       std::string(kTestProductName),
    144       std::string(kTestUuid),
    145       std::string(kTestSystemPathPrefix),
    146       DEVICE_TYPE_SD,
    147       1073741824,
    148       false,  // is_parent
    149       false,  // is_read_only
    150       true,  // has_media
    151       false,  // on_boot_device
    152       true,  // on_removable_device
    153       false));  // is_hidden
    154   disks_.clear();
    155   disks_.insert(std::pair<std::string, DiskMountManager::Disk*>(
    156       std::string(kTestDevicePath), disk.get()));
    157   NotifyDiskChanged(DISK_REMOVED, disk.get());
    158 }
    159 
    160 void MockDiskMountManager::SetupDefaultReplies() {
    161   EXPECT_CALL(*this, AddObserver(_))
    162       .Times(AnyNumber());
    163   EXPECT_CALL(*this, RemoveObserver(_))
    164       .Times(AnyNumber());
    165   EXPECT_CALL(*this, disks())
    166       .WillRepeatedly(ReturnRef(disks_));
    167   EXPECT_CALL(*this, mount_points())
    168       .WillRepeatedly(ReturnRef(mount_points_));
    169   EXPECT_CALL(*this, FindDiskBySourcePath(_))
    170       .Times(AnyNumber());
    171   EXPECT_CALL(*this, EnsureMountInfoRefreshed(_))
    172       .Times(AnyNumber());
    173   EXPECT_CALL(*this, MountPath(_, _, _, _))
    174       .Times(AnyNumber());
    175   EXPECT_CALL(*this, UnmountPath(_, _, _))
    176       .Times(AnyNumber());
    177   EXPECT_CALL(*this, FormatMountedDevice(_))
    178       .Times(AnyNumber());
    179   EXPECT_CALL(*this, UnmountDeviceRecursively(_, _))
    180       .Times(AnyNumber());
    181 }
    182 
    183 void MockDiskMountManager::CreateDiskEntryForMountDevice(
    184     const DiskMountManager::MountPointInfo& mount_info,
    185     const std::string& device_id,
    186     const std::string& device_label,
    187     const std::string& vendor_name,
    188     const std::string& product_name,
    189     DeviceType device_type,
    190     uint64 total_size_in_bytes,
    191     bool is_parent,
    192     bool has_media,
    193     bool on_boot_device,
    194     bool on_removable_device) {
    195   Disk* disk = new DiskMountManager::Disk(mount_info.source_path,
    196                                           mount_info.mount_path,
    197                                           std::string(),  // system_path
    198                                           mount_info.source_path,
    199                                           device_label,
    200                                           std::string(),  // drive_label
    201                                           std::string(),  // vendor_id
    202                                           vendor_name,
    203                                           std::string(),  // product_id
    204                                           product_name,
    205                                           device_id,      // fs_uuid
    206                                           std::string(),  // system_path_prefix
    207                                           device_type,
    208                                           total_size_in_bytes,
    209                                           is_parent,
    210                                           false,  // is_read_only
    211                                           has_media,
    212                                           on_boot_device,
    213                                           on_removable_device,
    214                                           false);  // is_hidden
    215   DiskMountManager::DiskMap::iterator it = disks_.find(mount_info.source_path);
    216   if (it == disks_.end()) {
    217     disks_.insert(std::make_pair(std::string(mount_info.source_path), disk));
    218   } else {
    219     delete it->second;
    220     it->second = disk;
    221   }
    222 }
    223 
    224 void MockDiskMountManager::RemoveDiskEntryForMountDevice(
    225     const DiskMountManager::MountPointInfo& mount_info) {
    226   DiskMountManager::DiskMap::iterator it = disks_.find(mount_info.source_path);
    227   if (it != disks_.end()) {
    228     delete it->second;
    229     disks_.erase(it);
    230   }
    231 }
    232 
    233 const DiskMountManager::MountPointMap&
    234 MockDiskMountManager::mountPointsInternal() const {
    235   return mount_points_;
    236 }
    237 
    238 const DiskMountManager::Disk*
    239 MockDiskMountManager::FindDiskBySourcePathInternal(
    240     const std::string& source_path) const {
    241   DiskMap::const_iterator disk_it = disks_.find(source_path);
    242   return disk_it == disks_.end() ? NULL : disk_it->second;
    243 }
    244 
    245 void MockDiskMountManager::EnsureMountInfoRefreshedInternal(
    246     const EnsureMountInfoRefreshedCallback& callback) {
    247   callback.Run(true);
    248 }
    249 
    250 void MockDiskMountManager::NotifyDiskChanged(
    251     DiskEvent event,
    252     const DiskMountManager::Disk* disk) {
    253   FOR_EACH_OBSERVER(Observer, observers_, OnDiskEvent(event, disk));
    254 }
    255 
    256 void MockDiskMountManager::NotifyDeviceChanged(DeviceEvent event,
    257                                                const std::string& path) {
    258   FOR_EACH_OBSERVER(Observer, observers_, OnDeviceEvent(event, path));
    259 }
    260 
    261 }  // namespace disks
    262 }  // namespace chromeos
    263