OLD | NEW |
(Empty) | |
| 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 "chrome/browser/chromeos/file_manager/mounted_disk_monitor.h" |
| 6 |
| 7 #include "base/basictypes.h" |
| 8 #include "base/message_loop/message_loop.h" |
| 9 #include "base/run_loop.h" |
| 10 #include "base/stl_util.h" |
| 11 #include "chromeos/dbus/fake_power_manager_client.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 |
| 14 namespace file_manager { |
| 15 namespace { |
| 16 |
| 17 // Fake implementation of DiskMountManager. Does nothing but returns some |
| 18 // disk information. |
| 19 class FakeDiskMountManager : public chromeos::disks::DiskMountManager { |
| 20 public: |
| 21 FakeDiskMountManager() {} |
| 22 virtual ~FakeDiskMountManager() { |
| 23 STLDeleteValues(&disks_); |
| 24 } |
| 25 |
| 26 // DiskMountManager overrides. |
| 27 virtual void AddObserver(Observer* observer) OVERRIDE {} |
| 28 virtual void RemoveObserver(Observer* observer) OVERRIDE {} |
| 29 virtual const DiskMap& disks() const OVERRIDE { return disks_; } |
| 30 |
| 31 virtual const Disk* FindDiskBySourcePath( |
| 32 const std::string& source_path) const OVERRIDE { |
| 33 DiskMap::const_iterator iter = disks_.find(source_path); |
| 34 if (iter == disks_.end()) |
| 35 return NULL; |
| 36 return iter->second; |
| 37 }; |
| 38 |
| 39 virtual const MountPointMap& mount_points() const OVERRIDE { |
| 40 return mount_points_; |
| 41 } |
| 42 virtual void RequestMountInfoRefresh() OVERRIDE {} |
| 43 virtual void MountPath(const std::string& source_path, |
| 44 const std::string& source_format, |
| 45 const std::string& mount_label, |
| 46 chromeos::MountType type) OVERRIDE {} |
| 47 virtual void UnmountPath(const std::string& mount_path, |
| 48 chromeos::UnmountOptions options, |
| 49 const UnmountPathCallback& callback) OVERRIDE {} |
| 50 virtual void FormatMountedDevice(const std::string& mount_path) OVERRIDE {} |
| 51 virtual void UnmountDeviceRecursively( |
| 52 const std::string& device_path, |
| 53 const UnmountDeviceRecursivelyCallbackType& callback) OVERRIDE {} |
| 54 |
| 55 virtual bool AddDiskForTest(Disk* disk) OVERRIDE { |
| 56 DCHECK(disk); |
| 57 DCHECK(disks_.find(disk->device_path()) == disks_.end()); |
| 58 disks_[disk->device_path()] = disk; |
| 59 return true; |
| 60 } |
| 61 virtual bool AddMountPointForTest( |
| 62 const MountPointInfo& mount_point) OVERRIDE { return false; } |
| 63 |
| 64 private: |
| 65 DiskMap disks_; |
| 66 MountPointMap mount_points_; |
| 67 |
| 68 DISALLOW_COPY_AND_ASSIGN(FakeDiskMountManager); |
| 69 }; |
| 70 |
| 71 // Creates a fake disk with |device_path| and |fs_uuid|. |
| 72 scoped_ptr<chromeos::disks::DiskMountManager::Disk> CreateDisk( |
| 73 const std::string& device_path, |
| 74 const std::string& fs_uuid) { |
| 75 return make_scoped_ptr( |
| 76 new chromeos::disks::DiskMountManager::Disk( |
| 77 device_path, "", "", "", "", "", "", "", "", "", fs_uuid, "", |
| 78 chromeos::DEVICE_TYPE_USB, 0, false, false, false, false, false)); |
| 79 } |
| 80 |
| 81 } // namespace |
| 82 |
| 83 class MountedDiskMonitorTest : public testing::Test { |
| 84 protected: |
| 85 MountedDiskMonitorTest() { |
| 86 } |
| 87 |
| 88 virtual ~MountedDiskMonitorTest() { |
| 89 } |
| 90 |
| 91 virtual void SetUp() OVERRIDE { |
| 92 power_manager_client_.reset(new chromeos::FakePowerManagerClient); |
| 93 disk_mount_manager_.reset(new FakeDiskMountManager); |
| 94 mounted_disk_monitor_.reset(new MountedDiskMonitor( |
| 95 power_manager_client_.get(), |
| 96 disk_mount_manager_.get())); |
| 97 mounted_disk_monitor_->set_resuming_time_span_for_testing( |
| 98 base::TimeDelta::FromSeconds(0)); |
| 99 } |
| 100 |
| 101 base::MessageLoop message_loop_; |
| 102 scoped_ptr<chromeos::FakePowerManagerClient> power_manager_client_; |
| 103 scoped_ptr<FakeDiskMountManager> disk_mount_manager_; |
| 104 scoped_ptr<MountedDiskMonitor> mounted_disk_monitor_; |
| 105 }; |
| 106 |
| 107 // Makes sure that just mounting and unmounting repeatedly doesn't affect to |
| 108 // "remounting" state. |
| 109 TEST_F(MountedDiskMonitorTest, WithoutSuspend) { |
| 110 scoped_ptr<chromeos::disks::DiskMountManager::Disk> disk( |
| 111 CreateDisk("removable_device1", "uuid1")); |
| 112 |
| 113 chromeos::disks::DiskMountManager::Disk* disk_ptr = disk.get(); |
| 114 |
| 115 const chromeos::disks::DiskMountManager::MountPointInfo kMountPoint( |
| 116 "removable_device1", "/tmp/removable_device1", |
| 117 chromeos::MOUNT_TYPE_DEVICE, chromeos::disks::MOUNT_CONDITION_NONE); |
| 118 |
| 119 disk_mount_manager_->AddDiskForTest(disk.release()); |
| 120 |
| 121 // First, the disk is not remounting. |
| 122 EXPECT_FALSE(mounted_disk_monitor_->DiskIsRemounting(*disk_ptr)); |
| 123 |
| 124 // Simple mounting and unmounting doesn't affect remounting state. |
| 125 mounted_disk_monitor_->OnMountEvent( |
| 126 chromeos::disks::DiskMountManager::MOUNTING, |
| 127 chromeos::MOUNT_ERROR_NONE, |
| 128 kMountPoint); |
| 129 EXPECT_FALSE(mounted_disk_monitor_->DiskIsRemounting(*disk_ptr)); |
| 130 |
| 131 mounted_disk_monitor_->OnMountEvent( |
| 132 chromeos::disks::DiskMountManager::UNMOUNTING, |
| 133 chromeos::MOUNT_ERROR_NONE, |
| 134 kMountPoint); |
| 135 EXPECT_FALSE(mounted_disk_monitor_->DiskIsRemounting(*disk_ptr)); |
| 136 |
| 137 // Mounting again also should not affect remounting state. |
| 138 mounted_disk_monitor_->OnMountEvent( |
| 139 chromeos::disks::DiskMountManager::MOUNTING, |
| 140 chromeos::MOUNT_ERROR_NONE, |
| 141 kMountPoint); |
| 142 EXPECT_FALSE(mounted_disk_monitor_->DiskIsRemounting(*disk_ptr)); |
| 143 } |
| 144 |
| 145 // Makes sure that the unmounting after system resuming triggers the |
| 146 // "remounting" state, then after some period, the state is reset. |
| 147 TEST_F(MountedDiskMonitorTest, SuspendAndResume) { |
| 148 scoped_ptr<chromeos::disks::DiskMountManager::Disk> disk1( |
| 149 CreateDisk("removable_device1", "uuid1")); |
| 150 scoped_ptr<chromeos::disks::DiskMountManager::Disk> disk2( |
| 151 CreateDisk("removable_device2", "uuid2")); |
| 152 |
| 153 chromeos::disks::DiskMountManager::Disk* disk1_ptr = disk1.get(); |
| 154 chromeos::disks::DiskMountManager::Disk* disk2_ptr = disk2.get(); |
| 155 |
| 156 const chromeos::disks::DiskMountManager::MountPointInfo kMountPoint1( |
| 157 "removable_device1", "/tmp/removable_device1", |
| 158 chromeos::MOUNT_TYPE_DEVICE, chromeos::disks::MOUNT_CONDITION_NONE); |
| 159 const chromeos::disks::DiskMountManager::MountPointInfo kMountPoint2( |
| 160 "removable_device2", "/tmp/removable_device2", |
| 161 chromeos::MOUNT_TYPE_DEVICE, chromeos::disks::MOUNT_CONDITION_NONE); |
| 162 |
| 163 disk_mount_manager_->AddDiskForTest(disk1.release()); |
| 164 disk_mount_manager_->AddDiskForTest(disk2.release()); |
| 165 |
| 166 // Mount |disk1|. |
| 167 mounted_disk_monitor_->OnMountEvent( |
| 168 chromeos::disks::DiskMountManager::MOUNTING, |
| 169 chromeos::MOUNT_ERROR_NONE, |
| 170 kMountPoint1); |
| 171 EXPECT_FALSE(mounted_disk_monitor_->DiskIsRemounting(*disk1_ptr)); |
| 172 |
| 173 // Pseudo system suspend and resume. |
| 174 mounted_disk_monitor_->SuspendImminent(); |
| 175 mounted_disk_monitor_->SystemResumed(base::TimeDelta::FromSeconds(0)); |
| 176 |
| 177 // On system resume, we expect unmount and then mount immediately. |
| 178 // During the phase, we expect the disk is remounting. |
| 179 mounted_disk_monitor_->OnMountEvent( |
| 180 chromeos::disks::DiskMountManager::UNMOUNTING, |
| 181 chromeos::MOUNT_ERROR_NONE, |
| 182 kMountPoint1); |
| 183 EXPECT_TRUE(mounted_disk_monitor_->DiskIsRemounting(*disk1_ptr)); |
| 184 |
| 185 mounted_disk_monitor_->OnMountEvent( |
| 186 chromeos::disks::DiskMountManager::MOUNTING, |
| 187 chromeos::MOUNT_ERROR_NONE, |
| 188 kMountPoint1); |
| 189 EXPECT_TRUE(mounted_disk_monitor_->DiskIsRemounting(*disk1_ptr)); |
| 190 |
| 191 // New disk should not be "remounting." |
| 192 EXPECT_FALSE(mounted_disk_monitor_->DiskIsRemounting(*disk2_ptr)); |
| 193 mounted_disk_monitor_->OnMountEvent( |
| 194 chromeos::disks::DiskMountManager::MOUNTING, |
| 195 chromeos::MOUNT_ERROR_NONE, |
| 196 kMountPoint2); |
| 197 EXPECT_FALSE(mounted_disk_monitor_->DiskIsRemounting(*disk2_ptr)); |
| 198 |
| 199 // After certain period, remounting state should be cleared. |
| 200 base::RunLoop().RunUntilIdle(); // Emulate time passage. |
| 201 EXPECT_FALSE(mounted_disk_monitor_->DiskIsRemounting(*disk1_ptr)); |
| 202 EXPECT_FALSE(mounted_disk_monitor_->DiskIsRemounting(*disk2_ptr)); |
| 203 } |
| 204 |
| 205 } // namespace file_manager |
OLD | NEW |