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 }
     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   Disk* disk = new DiskMountManager::Disk(std::string(mount_info.source_path),
    186                                           std::string(mount_info.mount_path),
    187                                           std::string(),  // system_path
    188                                           std::string(),  // file_path
    189                                           device_label,  // device_label
    190                                           std::string(),  // drive_label
    191                                           std::string(),  // vendor_id
    192                                           vendor_name,
    193                                           std::string(),  // product_id
    194                                           product_name,
    195                                           device_id,  // fs_uuid
    196                                           std::string(),  // system_path_prefix
    197                                           device_type,
    198                                           total_size_in_bytes,
    199                                           false,  // is_parent
    200                                           false,  // is_read_only
    201                                           true,  // has_media
    202                                           false,  // on_boot_device
    203                                           false);  // is_hidden
    204   DiskMountManager::DiskMap::iterator it = disks_.find(mount_info.source_path);
    205   if (it == disks_.end()) {
    206     disks_.insert(std::make_pair(std::string(mount_info.source_path), disk));
    207   } else {
    208     delete it->second;
    209     it->second = disk;
    210   }
    211 }
    212 
    213 void MockDiskMountManager::RemoveDiskEntryForMountDevice(
    214     const DiskMountManager::MountPointInfo& mount_info) {
    215   DiskMountManager::DiskMap::iterator it = disks_.find(mount_info.source_path);
    216   if (it != disks_.end()) {
    217     delete it->second;
    218     disks_.erase(it);
    219   }
    220 }
    221 
    222 const DiskMountManager::MountPointMap&
    223 MockDiskMountManager::mountPointsInternal() const {
    224   return mount_points_;
    225 }
    226 
    227 const DiskMountManager::Disk*
    228 MockDiskMountManager::FindDiskBySourcePathInternal(
    229     const std::string& source_path) const {
    230   DiskMap::const_iterator disk_it = disks_.find(source_path);
    231   return disk_it == disks_.end() ? NULL : disk_it->second;
    232 }
    233 
    234 void MockDiskMountManager::NotifyDiskChanged(
    235     DiskEvent event,
    236     const DiskMountManager::Disk* disk) {
    237   FOR_EACH_OBSERVER(Observer, observers_, OnDiskEvent(event, disk));
    238 }
    239 
    240 void MockDiskMountManager::NotifyDeviceChanged(DeviceEvent event,
    241                                                const std::string& path) {
    242   FOR_EACH_OBSERVER(Observer, observers_, OnDeviceEvent(event, path));
    243 }
    244 
    245 }  // namespace disks
    246 }  // namespace chromeos
    247