[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 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 | |
avi | 6e1a22d | 2015-12-21 03:43:20 | [diff] [blame] | 5 | #include <stddef.h> |
| 6 | #include <stdint.h> |
| 7 | |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 8 | #include "base/bind.h" |
[email protected] | f129d250 | 2013-07-17 22:45:50 | [diff] [blame] | 9 | #include "base/message_loop/message_loop.h" |
zelidrag | 29fe338 | 2014-08-27 01:44:48 | [diff] [blame] | 10 | #include "chromeos/dbus/dbus_thread_manager.h" |
[email protected] | cc70f501 | 2013-05-14 04:58:56 | [diff] [blame] | 11 | #include "chromeos/dbus/fake_cros_disks_client.h" |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 12 | #include "chromeos/disks/disk_mount_manager.h" |
| 13 | #include "testing/gmock/include/gmock/gmock.h" |
| 14 | #include "testing/gtest/include/gtest/gtest.h" |
| 15 | |
| 16 | using chromeos::disks::DiskMountManager; |
| 17 | using chromeos::CrosDisksClient; |
| 18 | using chromeos::DBusThreadManager; |
[email protected] | cc70f501 | 2013-05-14 04:58:56 | [diff] [blame] | 19 | using chromeos::FakeCrosDisksClient; |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 20 | using testing::_; |
| 21 | using testing::Field; |
| 22 | using testing::InSequence; |
| 23 | |
| 24 | namespace { |
| 25 | |
| 26 | // Holds information needed to create a DiskMountManager::Disk instance. |
| 27 | struct TestDiskInfo { |
| 28 | const char* source_path; |
| 29 | const char* mount_path; |
| 30 | const char* system_path; |
| 31 | const char* file_path; |
| 32 | const char* device_label; |
| 33 | const char* drive_label; |
| 34 | const char* vendor_id; |
| 35 | const char* vendor_name; |
| 36 | const char* product_id; |
| 37 | const char* product_name; |
| 38 | const char* fs_uuid; |
| 39 | const char* system_path_prefix; |
| 40 | chromeos::DeviceType device_type; |
avi | 6e1a22d | 2015-12-21 03:43:20 | [diff] [blame] | 41 | uint64_t size_in_bytes; |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 42 | bool is_parent; |
| 43 | bool is_read_only; |
| 44 | bool has_media; |
| 45 | bool on_boot_device; |
[email protected] | 79ed457b | 2014-07-22 04:07:26 | [diff] [blame] | 46 | bool on_removable_device; |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 47 | bool is_hidden; |
| 48 | }; |
| 49 | |
| 50 | // Holds information to create a DiskMOuntManager::MountPointInfo instance. |
| 51 | struct TestMountPointInfo { |
| 52 | const char* source_path; |
| 53 | const char* mount_path; |
| 54 | chromeos::MountType mount_type; |
| 55 | chromeos::disks::MountCondition mount_condition; |
| 56 | }; |
| 57 | |
| 58 | // List of disks held in DiskMountManager at the begining of the test. |
| 59 | const TestDiskInfo kTestDisks[] = { |
| 60 | { |
| 61 | "/device/source_path", |
| 62 | "/device/mount_path", |
| 63 | "/device/prefix/system_path", |
| 64 | "/device/file_path", |
| 65 | "/device/device_label", |
| 66 | "/device/drive_label", |
| 67 | "/device/vendor_id", |
| 68 | "/device/vendor_name", |
| 69 | "/device/product_id", |
| 70 | "/device/product_name", |
| 71 | "/device/fs_uuid", |
| 72 | "/device/prefix", |
| 73 | chromeos::DEVICE_TYPE_USB, |
| 74 | 1073741824, // size in bytes |
| 75 | false, // is parent |
| 76 | false, // is read only |
| 77 | true, // has media |
| 78 | false, // is on boot device |
[email protected] | 79ed457b | 2014-07-22 04:07:26 | [diff] [blame] | 79 | true, // is on removable device |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 80 | false // is hidden |
| 81 | }, |
| 82 | }; |
| 83 | |
[email protected] | cc70f501 | 2013-05-14 04:58:56 | [diff] [blame] | 84 | // List of mount points held in DiskMountManager at the begining of the test. |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 85 | const TestMountPointInfo kTestMountPoints[] = { |
| 86 | { |
| 87 | "/archive/source_path", |
| 88 | "/archive/mount_path", |
| 89 | chromeos::MOUNT_TYPE_ARCHIVE, |
| 90 | chromeos::disks::MOUNT_CONDITION_NONE |
| 91 | }, |
| 92 | { |
| 93 | "/device/source_path", |
| 94 | "/device/mount_path", |
| 95 | chromeos::MOUNT_TYPE_DEVICE, |
| 96 | chromeos::disks::MOUNT_CONDITION_NONE |
| 97 | }, |
| 98 | }; |
| 99 | |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 100 | // Mocks DiskMountManager observer. |
| 101 | class MockDiskMountManagerObserver : public DiskMountManager::Observer { |
| 102 | public: |
| 103 | virtual ~MockDiskMountManagerObserver() {} |
| 104 | |
| 105 | MOCK_METHOD2(OnDiskEvent, void(DiskMountManager::DiskEvent event, |
| 106 | const DiskMountManager::Disk* disk)); |
| 107 | MOCK_METHOD2(OnDeviceEvent, void(DiskMountManager::DeviceEvent event, |
| 108 | const std::string& device_path)); |
| 109 | MOCK_METHOD3(OnMountEvent, |
| 110 | void(DiskMountManager::MountEvent event, |
| 111 | chromeos::MountError error_code, |
| 112 | const DiskMountManager::MountPointInfo& mount_point)); |
| 113 | MOCK_METHOD3(OnFormatEvent, |
| 114 | void(DiskMountManager::FormatEvent event, |
| 115 | chromeos::FormatError error_code, |
| 116 | const std::string& device_path)); |
| 117 | }; |
| 118 | |
| 119 | class DiskMountManagerTest : public testing::Test { |
| 120 | public: |
| 121 | DiskMountManagerTest() {} |
dcheng | ae98daa | 2015-01-21 20:30:49 | [diff] [blame] | 122 | ~DiskMountManagerTest() override {} |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 123 | |
| 124 | // Sets up test dbus tread manager and disks mount manager. |
| 125 | // Initializes disk mount manager disks and mount points. |
| 126 | // Adds a test observer to the disk mount manager. |
dcheng | ae98daa | 2015-01-21 20:30:49 | [diff] [blame] | 127 | void SetUp() override { |
[email protected] | 54652d8 | 2013-11-10 16:02:49 | [diff] [blame] | 128 | fake_cros_disks_client_ = new FakeCrosDisksClient; |
zelidrag | 29fe338 | 2014-08-27 01:44:48 | [diff] [blame] | 129 | DBusThreadManager::GetSetterForTesting()->SetCrosDisksClient( |
dcheng | 0a6e80c | 2016-04-08 18:37:38 | [diff] [blame^] | 130 | std::unique_ptr<CrosDisksClient>(fake_cros_disks_client_)); |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 131 | |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 132 | DiskMountManager::Initialize(); |
| 133 | |
| 134 | InitDisksAndMountPoints(); |
| 135 | |
| 136 | DiskMountManager::GetInstance()->AddObserver(&observer_); |
| 137 | } |
| 138 | |
| 139 | // Shuts down dbus thread manager and disk moutn manager used in the test. |
dcheng | ae98daa | 2015-01-21 20:30:49 | [diff] [blame] | 140 | void TearDown() override { |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 141 | DiskMountManager::GetInstance()->RemoveObserver(&observer_); |
| 142 | DiskMountManager::Shutdown(); |
| 143 | DBusThreadManager::Shutdown(); |
| 144 | } |
| 145 | |
| 146 | protected: |
| 147 | // Checks if disk mount manager contains a mount point with specified moutn |
| 148 | // path. |
| 149 | bool HasMountPoint(const std::string& mount_path) { |
| 150 | const DiskMountManager::MountPointMap& mount_points = |
| 151 | DiskMountManager::GetInstance()->mount_points(); |
| 152 | return mount_points.find(mount_path) != mount_points.end(); |
| 153 | } |
| 154 | |
| 155 | private: |
| 156 | // Adds a new disk to the disk mount manager. |
| 157 | void AddTestDisk(const TestDiskInfo& disk) { |
| 158 | EXPECT_TRUE(DiskMountManager::GetInstance()->AddDiskForTest( |
| 159 | new DiskMountManager::Disk(disk.source_path, |
| 160 | disk.mount_path, |
| 161 | disk.system_path, |
| 162 | disk.file_path, |
| 163 | disk.device_label, |
| 164 | disk.drive_label, |
| 165 | disk.vendor_id, |
| 166 | disk.vendor_name, |
| 167 | disk.product_id, |
| 168 | disk.product_name, |
| 169 | disk.fs_uuid, |
| 170 | disk.system_path_prefix, |
| 171 | disk.device_type, |
| 172 | disk.size_in_bytes, |
| 173 | disk.is_parent, |
| 174 | disk.is_read_only, |
| 175 | disk.has_media, |
| 176 | disk.on_boot_device, |
[email protected] | 79ed457b | 2014-07-22 04:07:26 | [diff] [blame] | 177 | disk.on_removable_device, |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 178 | disk.is_hidden))); |
| 179 | } |
| 180 | |
| 181 | // Adds a new mount point to the disk mount manager. |
| 182 | // If the moutn point is a device mount point, disk with its source path |
| 183 | // should already be added to the disk mount manager. |
| 184 | void AddTestMountPoint(const TestMountPointInfo& mount_point) { |
| 185 | EXPECT_TRUE(DiskMountManager::GetInstance()->AddMountPointForTest( |
| 186 | DiskMountManager::MountPointInfo(mount_point.source_path, |
| 187 | mount_point.mount_path, |
| 188 | mount_point.mount_type, |
| 189 | mount_point.mount_condition))); |
| 190 | } |
| 191 | |
| 192 | // Adds disks and mount points to disk mount manager. |
| 193 | void InitDisksAndMountPoints() { |
| 194 | // Disks should be added first (when adding device mount points it is |
| 195 | // expected that the corresponding disk is already added). |
| 196 | for (size_t i = 0; i < arraysize(kTestDisks); i++) |
| 197 | AddTestDisk(kTestDisks[i]); |
| 198 | |
| 199 | for (size_t i = 0; i < arraysize(kTestMountPoints); i++) |
| 200 | AddTestMountPoint(kTestMountPoints[i]); |
| 201 | } |
| 202 | |
| 203 | protected: |
[email protected] | cc70f501 | 2013-05-14 04:58:56 | [diff] [blame] | 204 | chromeos::FakeCrosDisksClient* fake_cros_disks_client_; |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 205 | MockDiskMountManagerObserver observer_; |
[email protected] | df90563 | 2013-05-29 23:04:36 | [diff] [blame] | 206 | base::MessageLoopForUI message_loop_; |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 207 | }; |
| 208 | |
| 209 | // Tests that the observer gets notified on attempt to format non existent mount |
| 210 | // point. |
| 211 | TEST_F(DiskMountManagerTest, Format_NotMounted) { |
[email protected] | f026c0f | 2014-05-06 21:52:35 | [diff] [blame] | 212 | EXPECT_CALL(observer_, OnFormatEvent(DiskMountManager::FORMAT_COMPLETED, |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 213 | chromeos::FORMAT_ERROR_UNKNOWN, |
| 214 | "/mount/non_existent")) |
| 215 | .Times(1); |
| 216 | DiskMountManager::GetInstance()->FormatMountedDevice("/mount/non_existent"); |
| 217 | } |
| 218 | |
| 219 | // Tests that it is not possible to format archive mount point. |
| 220 | TEST_F(DiskMountManagerTest, Format_Archive) { |
[email protected] | f026c0f | 2014-05-06 21:52:35 | [diff] [blame] | 221 | EXPECT_CALL(observer_, OnFormatEvent(DiskMountManager::FORMAT_COMPLETED, |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 222 | chromeos::FORMAT_ERROR_UNKNOWN, |
| 223 | "/archive/source_path")) |
| 224 | .Times(1); |
| 225 | |
| 226 | DiskMountManager::GetInstance()->FormatMountedDevice("/archive/mount_path"); |
| 227 | } |
| 228 | |
| 229 | // Tests that format fails if the device cannot be unmounted. |
| 230 | TEST_F(DiskMountManagerTest, Format_FailToUnmount) { |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 231 | // Before formatting mounted device, the device should be unmounted. |
| 232 | // In this test unmount will fail, and there should be no attempt to |
| 233 | // format the device. |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 234 | |
| 235 | // Set up expectations for observer mock. |
| 236 | // Observer should be notified that unmount attempt fails and format task |
| 237 | // failed to start. |
| 238 | { |
| 239 | InSequence s; |
| 240 | |
| 241 | EXPECT_CALL(observer_, |
| 242 | OnMountEvent(DiskMountManager::UNMOUNTING, |
| 243 | chromeos::MOUNT_ERROR_INTERNAL, |
| 244 | Field(&DiskMountManager::MountPointInfo::mount_path, |
| 245 | "/device/mount_path"))) |
| 246 | .Times(1); |
| 247 | |
[email protected] | f026c0f | 2014-05-06 21:52:35 | [diff] [blame] | 248 | EXPECT_CALL(observer_, OnFormatEvent(DiskMountManager::FORMAT_COMPLETED, |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 249 | chromeos::FORMAT_ERROR_UNKNOWN, |
| 250 | "/device/source_path")) |
| 251 | .Times(1); |
| 252 | } |
| 253 | |
[email protected] | cc70f501 | 2013-05-14 04:58:56 | [diff] [blame] | 254 | fake_cros_disks_client_->MakeUnmountFail(); |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 255 | // Start test. |
| 256 | DiskMountManager::GetInstance()->FormatMountedDevice("/device/mount_path"); |
| 257 | |
| 258 | // Cros disks will respond asynchronoulsy, so let's drain the message loop. |
| 259 | message_loop_.RunUntilIdle(); |
| 260 | |
[email protected] | cc70f501 | 2013-05-14 04:58:56 | [diff] [blame] | 261 | EXPECT_EQ(1, fake_cros_disks_client_->unmount_call_count()); |
| 262 | EXPECT_EQ("/device/mount_path", |
| 263 | fake_cros_disks_client_->last_unmount_device_path()); |
| 264 | EXPECT_EQ(chromeos::UNMOUNT_OPTIONS_NONE, |
| 265 | fake_cros_disks_client_->last_unmount_options()); |
[email protected] | f026c0f | 2014-05-06 21:52:35 | [diff] [blame] | 266 | EXPECT_EQ(0, fake_cros_disks_client_->format_call_count()); |
[email protected] | cc70f501 | 2013-05-14 04:58:56 | [diff] [blame] | 267 | |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 268 | // The device mount should still be here. |
| 269 | EXPECT_TRUE(HasMountPoint("/device/mount_path")); |
| 270 | } |
| 271 | |
| 272 | // Tests that observer is notified when cros disks fails to start format |
| 273 | // process. |
| 274 | TEST_F(DiskMountManagerTest, Format_FormatFailsToStart) { |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 275 | // Before formatting mounted device, the device should be unmounted. |
[email protected] | f026c0f | 2014-05-06 21:52:35 | [diff] [blame] | 276 | // In this test, unmount will succeed, but call to Format method will |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 277 | // fail. |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 278 | |
| 279 | // Set up expectations for observer mock. |
| 280 | // Observer should be notified that the device was unmounted and format task |
| 281 | // failed to start. |
| 282 | { |
| 283 | InSequence s; |
| 284 | |
| 285 | EXPECT_CALL(observer_, |
| 286 | OnMountEvent(DiskMountManager::UNMOUNTING, |
| 287 | chromeos::MOUNT_ERROR_NONE, |
| 288 | Field(&DiskMountManager::MountPointInfo::mount_path, |
| 289 | "/device/mount_path"))) |
| 290 | .Times(1); |
| 291 | |
[email protected] | f026c0f | 2014-05-06 21:52:35 | [diff] [blame] | 292 | EXPECT_CALL(observer_, OnFormatEvent(DiskMountManager::FORMAT_COMPLETED, |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 293 | chromeos::FORMAT_ERROR_UNKNOWN, |
| 294 | "/device/source_path")) |
| 295 | .Times(1); |
| 296 | } |
| 297 | |
[email protected] | f026c0f | 2014-05-06 21:52:35 | [diff] [blame] | 298 | fake_cros_disks_client_->MakeFormatFail(); |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 299 | // Start the test. |
| 300 | DiskMountManager::GetInstance()->FormatMountedDevice("/device/mount_path"); |
| 301 | |
| 302 | // Cros disks will respond asynchronoulsy, so let's drain the message loop. |
| 303 | message_loop_.RunUntilIdle(); |
| 304 | |
[email protected] | cc70f501 | 2013-05-14 04:58:56 | [diff] [blame] | 305 | EXPECT_EQ(1, fake_cros_disks_client_->unmount_call_count()); |
| 306 | EXPECT_EQ("/device/mount_path", |
| 307 | fake_cros_disks_client_->last_unmount_device_path()); |
| 308 | EXPECT_EQ(chromeos::UNMOUNT_OPTIONS_NONE, |
| 309 | fake_cros_disks_client_->last_unmount_options()); |
[email protected] | f026c0f | 2014-05-06 21:52:35 | [diff] [blame] | 310 | EXPECT_EQ(1, fake_cros_disks_client_->format_call_count()); |
[email protected] | cc70f501 | 2013-05-14 04:58:56 | [diff] [blame] | 311 | EXPECT_EQ("/device/source_path", |
[email protected] | f026c0f | 2014-05-06 21:52:35 | [diff] [blame] | 312 | fake_cros_disks_client_->last_format_device_path()); |
| 313 | EXPECT_EQ("vfat", fake_cros_disks_client_->last_format_filesystem()); |
[email protected] | cc70f501 | 2013-05-14 04:58:56 | [diff] [blame] | 314 | |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 315 | // The device mount should be gone. |
| 316 | EXPECT_FALSE(HasMountPoint("/device/mount_path")); |
| 317 | } |
| 318 | |
| 319 | // Tests the case where there are two format requests for the same device. |
| 320 | TEST_F(DiskMountManagerTest, Format_ConcurrentFormatCalls) { |
[email protected] | 8f919ee | 2013-03-14 19:53:29 | [diff] [blame] | 321 | // Only the first format request should be processed (the second unmount |
| 322 | // request fails because the device is already unmounted at that point). |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 323 | // CrosDisksClient will report that the format process for the first request |
| 324 | // is successfully started. |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 325 | |
| 326 | // Set up expectations for observer mock. |
[email protected] | f026c0f | 2014-05-06 21:52:35 | [diff] [blame] | 327 | // The observer should get a FORMAT_STARTED event for one format request and a |
| 328 | // FORMAT_COMPLETED with an error code for the other format request. The |
| 329 | // formatting will be started only for the first request. |
[email protected] | 8f919ee | 2013-03-14 19:53:29 | [diff] [blame] | 330 | // There should be only one UNMOUNTING event. The result of the second one |
| 331 | // should not be reported as the mount point will go away after the first |
| 332 | // request. |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 333 | // |
| 334 | // Note that in this test the format completion signal will not be simulated, |
| 335 | // so the observer should not get FORMAT_COMPLETED signal. |
| 336 | { |
| 337 | InSequence s; |
| 338 | |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 339 | EXPECT_CALL(observer_, |
| 340 | OnMountEvent(DiskMountManager::UNMOUNTING, |
| 341 | chromeos::MOUNT_ERROR_NONE, |
| 342 | Field(&DiskMountManager::MountPointInfo::mount_path, |
| 343 | "/device/mount_path"))) |
| 344 | .Times(1); |
| 345 | |
[email protected] | f026c0f | 2014-05-06 21:52:35 | [diff] [blame] | 346 | EXPECT_CALL(observer_, OnFormatEvent(DiskMountManager::FORMAT_COMPLETED, |
[email protected] | 8f919ee | 2013-03-14 19:53:29 | [diff] [blame] | 347 | chromeos::FORMAT_ERROR_UNKNOWN, |
| 348 | "/device/source_path")) |
| 349 | .Times(1); |
| 350 | |
| 351 | EXPECT_CALL(observer_, OnFormatEvent(DiskMountManager::FORMAT_STARTED, |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 352 | chromeos::FORMAT_ERROR_NONE, |
| 353 | "/device/source_path")) |
| 354 | .Times(1); |
| 355 | } |
| 356 | |
[email protected] | cc70f501 | 2013-05-14 04:58:56 | [diff] [blame] | 357 | fake_cros_disks_client_->set_unmount_listener( |
| 358 | base::Bind(&FakeCrosDisksClient::MakeUnmountFail, |
| 359 | base::Unretained(fake_cros_disks_client_))); |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 360 | // Start the test. |
| 361 | DiskMountManager::GetInstance()->FormatMountedDevice("/device/mount_path"); |
| 362 | DiskMountManager::GetInstance()->FormatMountedDevice("/device/mount_path"); |
| 363 | |
| 364 | // Cros disks will respond asynchronoulsy, so let's drain the message loop. |
| 365 | message_loop_.RunUntilIdle(); |
| 366 | |
[email protected] | cc70f501 | 2013-05-14 04:58:56 | [diff] [blame] | 367 | EXPECT_EQ(2, fake_cros_disks_client_->unmount_call_count()); |
| 368 | EXPECT_EQ("/device/mount_path", |
| 369 | fake_cros_disks_client_->last_unmount_device_path()); |
| 370 | EXPECT_EQ(chromeos::UNMOUNT_OPTIONS_NONE, |
| 371 | fake_cros_disks_client_->last_unmount_options()); |
[email protected] | f026c0f | 2014-05-06 21:52:35 | [diff] [blame] | 372 | EXPECT_EQ(1, fake_cros_disks_client_->format_call_count()); |
[email protected] | cc70f501 | 2013-05-14 04:58:56 | [diff] [blame] | 373 | EXPECT_EQ("/device/source_path", |
[email protected] | f026c0f | 2014-05-06 21:52:35 | [diff] [blame] | 374 | fake_cros_disks_client_->last_format_device_path()); |
[email protected] | cc70f501 | 2013-05-14 04:58:56 | [diff] [blame] | 375 | EXPECT_EQ("vfat", |
[email protected] | f026c0f | 2014-05-06 21:52:35 | [diff] [blame] | 376 | fake_cros_disks_client_->last_format_filesystem()); |
[email protected] | cc70f501 | 2013-05-14 04:58:56 | [diff] [blame] | 377 | |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 378 | // The device mount should be gone. |
| 379 | EXPECT_FALSE(HasMountPoint("/device/mount_path")); |
| 380 | } |
| 381 | |
| 382 | // Tests the case when the format process actually starts and fails. |
| 383 | TEST_F(DiskMountManagerTest, Format_FormatFails) { |
[email protected] | f026c0f | 2014-05-06 21:52:35 | [diff] [blame] | 384 | // Both unmount and format device cals are successful in this test. |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 385 | |
| 386 | // Set up expectations for observer mock. |
| 387 | // The observer should get notified that the device was unmounted and that |
| 388 | // formatting has started. |
| 389 | // After the formatting starts, the test will simulate failing |
[email protected] | a0278d5 | 2014-05-06 03:36:15 | [diff] [blame] | 390 | // FORMAT_COMPLETED signal, so the observer should also be notified the |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 391 | // formatting has failed (FORMAT_COMPLETED event). |
| 392 | { |
| 393 | InSequence s; |
| 394 | |
| 395 | EXPECT_CALL(observer_, |
| 396 | OnMountEvent(DiskMountManager::UNMOUNTING, |
| 397 | chromeos::MOUNT_ERROR_NONE, |
| 398 | Field(&DiskMountManager::MountPointInfo::mount_path, |
| 399 | "/device/mount_path"))) |
| 400 | .Times(1); |
| 401 | |
| 402 | EXPECT_CALL(observer_, OnFormatEvent(DiskMountManager::FORMAT_STARTED, |
| 403 | chromeos::FORMAT_ERROR_NONE, |
| 404 | "/device/source_path")) |
| 405 | .Times(1); |
| 406 | |
| 407 | EXPECT_CALL(observer_, OnFormatEvent(DiskMountManager::FORMAT_COMPLETED, |
| 408 | chromeos::FORMAT_ERROR_UNKNOWN, |
| 409 | "/device/source_path")) |
| 410 | .Times(1); |
| 411 | } |
| 412 | |
| 413 | // Start the test. |
| 414 | DiskMountManager::GetInstance()->FormatMountedDevice("/device/mount_path"); |
| 415 | |
[email protected] | f026c0f | 2014-05-06 21:52:35 | [diff] [blame] | 416 | // Wait for Unmount and Format calls to end. |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 417 | message_loop_.RunUntilIdle(); |
| 418 | |
[email protected] | cc70f501 | 2013-05-14 04:58:56 | [diff] [blame] | 419 | EXPECT_EQ(1, fake_cros_disks_client_->unmount_call_count()); |
| 420 | EXPECT_EQ("/device/mount_path", |
| 421 | fake_cros_disks_client_->last_unmount_device_path()); |
| 422 | EXPECT_EQ(chromeos::UNMOUNT_OPTIONS_NONE, |
| 423 | fake_cros_disks_client_->last_unmount_options()); |
[email protected] | f026c0f | 2014-05-06 21:52:35 | [diff] [blame] | 424 | EXPECT_EQ(1, fake_cros_disks_client_->format_call_count()); |
[email protected] | cc70f501 | 2013-05-14 04:58:56 | [diff] [blame] | 425 | EXPECT_EQ("/device/source_path", |
[email protected] | f026c0f | 2014-05-06 21:52:35 | [diff] [blame] | 426 | fake_cros_disks_client_->last_format_device_path()); |
| 427 | EXPECT_EQ("vfat", fake_cros_disks_client_->last_format_filesystem()); |
[email protected] | cc70f501 | 2013-05-14 04:58:56 | [diff] [blame] | 428 | |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 429 | // The device should be unmounted by now. |
| 430 | EXPECT_FALSE(HasMountPoint("/device/mount_path")); |
| 431 | |
[email protected] | a0278d5 | 2014-05-06 03:36:15 | [diff] [blame] | 432 | // Send failing FORMAT_COMPLETED signal. |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 433 | // The failure is marked by ! in fromt of the path (but this should change |
| 434 | // soon). |
[email protected] | a0278d5 | 2014-05-06 03:36:15 | [diff] [blame] | 435 | fake_cros_disks_client_->SendFormatCompletedEvent( |
| 436 | chromeos::FORMAT_ERROR_UNKNOWN, "/device/source_path"); |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 437 | } |
| 438 | |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 439 | // Tests the case when formatting completes successfully. |
| 440 | TEST_F(DiskMountManagerTest, Format_FormatSuccess) { |
| 441 | // Set up cros disks client mocks. |
[email protected] | f026c0f | 2014-05-06 21:52:35 | [diff] [blame] | 442 | // Both unmount and format device cals are successful in this test. |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 443 | |
| 444 | // Set up expectations for observer mock. |
| 445 | // The observer should receive UNMOUNTING, FORMAT_STARTED and FORMAT_COMPLETED |
| 446 | // events (all of them without an error set). |
| 447 | { |
| 448 | InSequence s; |
| 449 | |
| 450 | EXPECT_CALL(observer_, |
| 451 | OnMountEvent(DiskMountManager::UNMOUNTING, |
| 452 | chromeos::MOUNT_ERROR_NONE, |
| 453 | Field(&DiskMountManager::MountPointInfo::mount_path, |
| 454 | "/device/mount_path"))) |
| 455 | .Times(1); |
| 456 | |
| 457 | EXPECT_CALL(observer_, OnFormatEvent(DiskMountManager::FORMAT_STARTED, |
| 458 | chromeos::FORMAT_ERROR_NONE, |
| 459 | "/device/source_path")) |
| 460 | .Times(1); |
| 461 | |
| 462 | EXPECT_CALL(observer_, OnFormatEvent(DiskMountManager::FORMAT_COMPLETED, |
| 463 | chromeos::FORMAT_ERROR_NONE, |
| 464 | "/device/source_path")) |
| 465 | .Times(1); |
| 466 | } |
| 467 | |
| 468 | // Start the test. |
| 469 | DiskMountManager::GetInstance()->FormatMountedDevice("/device/mount_path"); |
| 470 | |
[email protected] | f026c0f | 2014-05-06 21:52:35 | [diff] [blame] | 471 | // Wait for Unmount and Format calls to end. |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 472 | message_loop_.RunUntilIdle(); |
| 473 | |
[email protected] | cc70f501 | 2013-05-14 04:58:56 | [diff] [blame] | 474 | EXPECT_EQ(1, fake_cros_disks_client_->unmount_call_count()); |
| 475 | EXPECT_EQ("/device/mount_path", |
| 476 | fake_cros_disks_client_->last_unmount_device_path()); |
| 477 | EXPECT_EQ(chromeos::UNMOUNT_OPTIONS_NONE, |
| 478 | fake_cros_disks_client_->last_unmount_options()); |
[email protected] | f026c0f | 2014-05-06 21:52:35 | [diff] [blame] | 479 | EXPECT_EQ(1, fake_cros_disks_client_->format_call_count()); |
[email protected] | cc70f501 | 2013-05-14 04:58:56 | [diff] [blame] | 480 | EXPECT_EQ("/device/source_path", |
[email protected] | f026c0f | 2014-05-06 21:52:35 | [diff] [blame] | 481 | fake_cros_disks_client_->last_format_device_path()); |
| 482 | EXPECT_EQ("vfat", fake_cros_disks_client_->last_format_filesystem()); |
[email protected] | cc70f501 | 2013-05-14 04:58:56 | [diff] [blame] | 483 | |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 484 | // The device should be unmounted by now. |
| 485 | EXPECT_FALSE(HasMountPoint("/device/mount_path")); |
| 486 | |
| 487 | // Simulate cros_disks reporting success. |
[email protected] | a0278d5 | 2014-05-06 03:36:15 | [diff] [blame] | 488 | fake_cros_disks_client_->SendFormatCompletedEvent( |
| 489 | chromeos::FORMAT_ERROR_NONE, "/device/source_path"); |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | // Tests that it's possible to format the device twice in a row (this may not be |
| 493 | // true if the list of pending formats is not properly cleared). |
| 494 | TEST_F(DiskMountManagerTest, Format_ConsecutiveFormatCalls) { |
[email protected] | f026c0f | 2014-05-06 21:52:35 | [diff] [blame] | 495 | // All unmount and format device cals are successful in this test. |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 496 | // Each of the should be made twice (once for each formatting task). |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 497 | |
| 498 | // Set up expectations for observer mock. |
| 499 | // The observer should receive UNMOUNTING, FORMAT_STARTED and FORMAT_COMPLETED |
| 500 | // events (all of them without an error set) twice (once for each formatting |
| 501 | // task). |
| 502 | // Also, there should be a MOUNTING event when the device remounting is |
| 503 | // simulated. |
| 504 | EXPECT_CALL(observer_, OnFormatEvent(DiskMountManager::FORMAT_COMPLETED, |
| 505 | chromeos::FORMAT_ERROR_NONE, |
| 506 | "/device/source_path")) |
| 507 | .Times(2); |
| 508 | |
| 509 | EXPECT_CALL(observer_, OnFormatEvent(DiskMountManager::FORMAT_STARTED, |
| 510 | chromeos::FORMAT_ERROR_NONE, |
| 511 | "/device/source_path")) |
| 512 | .Times(2); |
| 513 | |
| 514 | EXPECT_CALL(observer_, |
| 515 | OnMountEvent(DiskMountManager::UNMOUNTING, |
| 516 | chromeos::MOUNT_ERROR_NONE, |
| 517 | Field(&DiskMountManager::MountPointInfo::mount_path, |
| 518 | "/device/mount_path"))) |
| 519 | .Times(2); |
| 520 | |
| 521 | EXPECT_CALL(observer_, |
| 522 | OnMountEvent(DiskMountManager::MOUNTING, |
| 523 | chromeos::MOUNT_ERROR_NONE, |
| 524 | Field(&DiskMountManager::MountPointInfo::mount_path, |
| 525 | "/device/mount_path"))) |
| 526 | .Times(1); |
| 527 | |
| 528 | // Start the test. |
| 529 | DiskMountManager::GetInstance()->FormatMountedDevice("/device/mount_path"); |
| 530 | |
[email protected] | f026c0f | 2014-05-06 21:52:35 | [diff] [blame] | 531 | // Wait for Unmount and Format calls to end. |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 532 | message_loop_.RunUntilIdle(); |
| 533 | |
[email protected] | cc70f501 | 2013-05-14 04:58:56 | [diff] [blame] | 534 | EXPECT_EQ(1, fake_cros_disks_client_->unmount_call_count()); |
| 535 | EXPECT_EQ("/device/mount_path", |
| 536 | fake_cros_disks_client_->last_unmount_device_path()); |
| 537 | EXPECT_EQ(chromeos::UNMOUNT_OPTIONS_NONE, |
| 538 | fake_cros_disks_client_->last_unmount_options()); |
[email protected] | f026c0f | 2014-05-06 21:52:35 | [diff] [blame] | 539 | EXPECT_EQ(1, fake_cros_disks_client_->format_call_count()); |
[email protected] | cc70f501 | 2013-05-14 04:58:56 | [diff] [blame] | 540 | EXPECT_EQ("/device/source_path", |
[email protected] | f026c0f | 2014-05-06 21:52:35 | [diff] [blame] | 541 | fake_cros_disks_client_->last_format_device_path()); |
| 542 | EXPECT_EQ("vfat", fake_cros_disks_client_->last_format_filesystem()); |
[email protected] | cc70f501 | 2013-05-14 04:58:56 | [diff] [blame] | 543 | |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 544 | // The device should be unmounted by now. |
| 545 | EXPECT_FALSE(HasMountPoint("/device/mount_path")); |
| 546 | |
| 547 | // Simulate cros_disks reporting success. |
[email protected] | a0278d5 | 2014-05-06 03:36:15 | [diff] [blame] | 548 | fake_cros_disks_client_->SendFormatCompletedEvent( |
| 549 | chromeos::FORMAT_ERROR_NONE, "/device/source_path"); |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 550 | |
| 551 | // Simulate the device remounting. |
[email protected] | cc70f501 | 2013-05-14 04:58:56 | [diff] [blame] | 552 | fake_cros_disks_client_->SendMountCompletedEvent( |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 553 | chromeos::MOUNT_ERROR_NONE, |
| 554 | "/device/source_path", |
| 555 | chromeos::MOUNT_TYPE_DEVICE, |
| 556 | "/device/mount_path"); |
| 557 | |
| 558 | EXPECT_TRUE(HasMountPoint("/device/mount_path")); |
| 559 | |
| 560 | // Try formatting again. |
| 561 | DiskMountManager::GetInstance()->FormatMountedDevice("/device/mount_path"); |
| 562 | |
[email protected] | f026c0f | 2014-05-06 21:52:35 | [diff] [blame] | 563 | // Wait for Unmount and Format calls to end. |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 564 | message_loop_.RunUntilIdle(); |
| 565 | |
[email protected] | cc70f501 | 2013-05-14 04:58:56 | [diff] [blame] | 566 | EXPECT_EQ(2, fake_cros_disks_client_->unmount_call_count()); |
| 567 | EXPECT_EQ("/device/mount_path", |
| 568 | fake_cros_disks_client_->last_unmount_device_path()); |
| 569 | EXPECT_EQ(chromeos::UNMOUNT_OPTIONS_NONE, |
| 570 | fake_cros_disks_client_->last_unmount_options()); |
[email protected] | f026c0f | 2014-05-06 21:52:35 | [diff] [blame] | 571 | EXPECT_EQ(2, fake_cros_disks_client_->format_call_count()); |
[email protected] | cc70f501 | 2013-05-14 04:58:56 | [diff] [blame] | 572 | EXPECT_EQ("/device/source_path", |
[email protected] | f026c0f | 2014-05-06 21:52:35 | [diff] [blame] | 573 | fake_cros_disks_client_->last_format_device_path()); |
| 574 | EXPECT_EQ("vfat", fake_cros_disks_client_->last_format_filesystem()); |
[email protected] | cc70f501 | 2013-05-14 04:58:56 | [diff] [blame] | 575 | |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 576 | // Simulate cros_disks reporting success. |
[email protected] | a0278d5 | 2014-05-06 03:36:15 | [diff] [blame] | 577 | fake_cros_disks_client_->SendFormatCompletedEvent( |
| 578 | chromeos::FORMAT_ERROR_NONE, "/device/source_path"); |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | } // namespace |