OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <stddef.h> | 5 #include <stddef.h> |
6 #include <stdint.h> | 6 #include <stdint.h> |
7 | 7 |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/strings/stringprintf.h" | |
10 #include "chromeos/dbus/dbus_thread_manager.h" | 11 #include "chromeos/dbus/dbus_thread_manager.h" |
11 #include "chromeos/dbus/fake_cros_disks_client.h" | 12 #include "chromeos/dbus/fake_cros_disks_client.h" |
12 #include "chromeos/disks/disk_mount_manager.h" | 13 #include "chromeos/disks/disk_mount_manager.h" |
13 #include "testing/gmock/include/gmock/gmock.h" | |
14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
15 | 15 |
16 using base::StringPrintf; | |
16 using chromeos::disks::DiskMountManager; | 17 using chromeos::disks::DiskMountManager; |
17 using chromeos::CrosDisksClient; | 18 using chromeos::CrosDisksClient; |
18 using chromeos::DBusThreadManager; | 19 using chromeos::DBusThreadManager; |
19 using chromeos::FakeCrosDisksClient; | 20 using chromeos::FakeCrosDisksClient; |
20 using testing::_; | 21 using chromeos::MountType; |
21 using testing::Field; | 22 using chromeos::disks::MountCondition; |
22 using testing::InSequence; | |
23 using testing::InvokeWithoutArgs; | |
24 | 23 |
25 namespace { | 24 namespace { |
26 | 25 |
27 const char kReadOnlyMountpath[] = "/device/read_only_mount_path"; | 26 const char kReadOnlyMountpath[] = "/device/read_only_mount_path"; |
28 const char kReadOnlyDeviceSource[] = "/device/read_only_source_path"; | 27 const char kReadOnlyDeviceSource[] = "/device/read_only_source_path"; |
29 | 28 |
30 // Holds information needed to create a DiskMountManager::Disk instance. | 29 // Holds information needed to create a DiskMountManager::Disk instance. |
31 struct TestDiskInfo { | 30 struct TestDiskInfo { |
32 const char* source_path; | 31 const char* source_path; |
33 const char* mount_path; | 32 const char* mount_path; |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
144 chromeos::disks::MOUNT_CONDITION_NONE | 143 chromeos::disks::MOUNT_CONDITION_NONE |
145 }, | 144 }, |
146 { | 145 { |
147 kReadOnlyDeviceSource, | 146 kReadOnlyDeviceSource, |
148 kReadOnlyMountpath, | 147 kReadOnlyMountpath, |
149 chromeos::MOUNT_TYPE_DEVICE, | 148 chromeos::MOUNT_TYPE_DEVICE, |
150 chromeos::disks::MOUNT_CONDITION_NONE | 149 chromeos::disks::MOUNT_CONDITION_NONE |
151 }, | 150 }, |
152 }; | 151 }; |
153 | 152 |
154 // Mocks DiskMountManager observer. | 153 // Event classes which represent each invocation of functions in |Observer|. |
satorux1
2016/09/01 06:18:44
Instead of describing multiple classes at once her
yamaguchi
2016/09/02 13:25:42
Done.
| |
154 // OnDeviceEvent() | |
155 struct DeviceEvent { | |
156 DiskMountManager::DeviceEvent event; | |
157 std::string device_path; | |
158 | |
159 DeviceEvent() {} | |
160 | |
161 DeviceEvent(DiskMountManager::DeviceEvent event, | |
162 const std::string& device_path) | |
163 : event(event), device_path(device_path) {} | |
164 | |
165 bool operator==(const DeviceEvent& other) const { | |
166 return event == other.event && device_path == other.device_path; | |
167 } | |
168 | |
169 std::string DebugString() const { | |
170 return StringPrintf("OnDeviceEvent(%d, %s)", event, device_path.c_str()); | |
171 } | |
172 }; | |
173 | |
174 // OnDiskEvent() | |
175 struct DiskEvent { | |
176 DiskMountManager::DiskEvent event; | |
177 const DiskMountManager::Disk* disk; | |
178 | |
179 DiskEvent() {} | |
180 | |
181 DiskEvent(DiskMountManager::DiskEvent event, | |
182 const DiskMountManager::Disk* disk) | |
183 : event(event), disk(disk) {} | |
184 | |
185 DiskEvent(const DiskEvent& other) : event(other.event), disk(other.disk) {} | |
186 | |
187 bool operator==(const DiskEvent& other) const { | |
188 return event == other.event && disk == other.disk; | |
189 } | |
190 | |
191 std::string DebugString() const { | |
192 return StringPrintf("OnDiskEvent(event=%d, device_path=%s, mount_path=%s", | |
193 event, disk->device_path().c_str(), | |
194 disk->mount_path().c_str()); | |
195 } | |
196 }; | |
197 | |
198 // OnFormatEvent() | |
199 struct FormatEvent { | |
200 DiskMountManager::FormatEvent event; | |
201 chromeos::FormatError error_code; | |
202 std::string device_path; | |
203 | |
204 FormatEvent() {} | |
205 FormatEvent(DiskMountManager::FormatEvent event, | |
206 chromeos::FormatError error_code, | |
207 const std::string& device_path) | |
208 : event(event), error_code(error_code), device_path(device_path) {} | |
209 | |
210 bool operator==(const FormatEvent& other) const { | |
211 return event == other.event && error_code == other.error_code && | |
212 device_path == other.device_path; | |
213 } | |
214 | |
215 std::string DebugString() const { | |
216 return StringPrintf("OnFormatEvent(%d, %d, %s)", event, error_code, | |
217 device_path.c_str()); | |
218 } | |
219 }; | |
220 | |
221 // OnMountEvent() | |
222 struct MountEvent { | |
223 DiskMountManager::MountEvent event; | |
224 chromeos::MountError error_code; | |
225 DiskMountManager::MountPointInfo mount_point; | |
226 | |
227 // Not passed to callback, but read by handlers. So it's captured upon | |
228 // callback. | |
229 std::shared_ptr<DiskMountManager::Disk> disk; | |
230 | |
231 MountEvent() | |
232 : mount_point("", | |
233 "", | |
234 MountType::MOUNT_TYPE_INVALID, | |
235 MountCondition::MOUNT_CONDITION_NONE) {} | |
236 ~MountEvent() {} | |
237 | |
238 MountEvent(const MountEvent& other) | |
239 : event(other.event), | |
240 error_code(other.error_code), | |
241 mount_point(other.mount_point), | |
242 disk(other.disk) {} | |
243 MountEvent(DiskMountManager::MountEvent event, | |
244 chromeos::MountError error_code, | |
245 const DiskMountManager::MountPointInfo& mount_point, | |
246 std::shared_ptr<DiskMountManager::Disk> disk) | |
247 : event(event), | |
248 error_code(error_code), | |
249 mount_point(mount_point), | |
250 disk(disk) {} | |
251 | |
252 bool operator==(const MountEvent& other) const; | |
253 | |
254 std::string DebugString() const { | |
255 return StringPrintf("OnMountEvent(%d, %d, %s, %s, %d, %d)", event, | |
256 error_code, mount_point.source_path.c_str(), | |
257 mount_point.mount_path.c_str(), mount_point.mount_type, | |
258 mount_point.mount_condition); | |
259 } | |
260 }; | |
261 | |
262 // Represents every event notified to |Observer|. | |
263 class ObserverEvent { | |
satorux1
2016/09/01 06:18:44
Instead of this class, maybe we can use inheritanc
yamaguchi
2016/09/02 13:25:42
Done.
| |
264 private: | |
265 struct Event { | |
266 DeviceEvent device; | |
267 DiskEvent disk; | |
268 FormatEvent format; | |
269 MountEvent mount; | |
270 Event(const Event& other) | |
271 : device(other.device), | |
272 disk(other.disk), | |
273 format(other.format), | |
274 mount(other.mount) {} | |
275 ~Event() {} | |
276 Event(DeviceEvent device) : device(device) {} | |
yamaguchi
2016/08/31 10:10:21
Added overloaded constructors so as to eliminate t
| |
277 Event(DiskEvent disk) : disk(disk) {} | |
278 Event(FormatEvent format) : format(format) {} | |
279 Event(MountEvent mount) : mount(mount) {} | |
280 } event_; | |
281 | |
282 ObserverEvent(DeviceEvent event) : event_(event), type_(DEVICE_EVENT) {} | |
283 ObserverEvent(DiskEvent event) : event_(event), type_(DISK_EVENT) {} | |
284 ObserverEvent(FormatEvent event) : event_(event), type_(FORMAT_EVENT) {} | |
285 ObserverEvent(MountEvent event) : event_(event), type_(MOUNT_EVENT) {} | |
286 | |
287 public: | |
288 enum ObserverEventType { | |
289 DEVICE_EVENT, | |
290 DISK_EVENT, | |
291 FORMAT_EVENT, | |
292 MOUNT_EVENT | |
293 } type_; | |
294 | |
295 static ObserverEvent FromDeviceEvent(DeviceEvent event) { | |
296 return ObserverEvent(event); | |
297 } | |
298 | |
299 static ObserverEvent FromDiskEvent(DiskEvent event) { | |
300 return ObserverEvent(event); | |
301 } | |
302 | |
303 static ObserverEvent FromFormatEvent(FormatEvent event) { | |
304 return ObserverEvent(event); | |
305 } | |
306 | |
307 static ObserverEvent FromMountEvent(MountEvent event) { | |
308 return ObserverEvent(event); | |
309 } | |
310 | |
311 ObserverEvent(const ObserverEvent& other) | |
312 : event_(other.event_), type_(other.type_) {} | |
313 | |
314 const Event& GetEvent() const { return event_; } | |
315 | |
316 ObserverEventType GetType() const { return type_; } | |
317 | |
318 std::string DebugString() const { | |
319 switch (type_) { | |
320 case DEVICE_EVENT: | |
321 return event_.device.DebugString(); | |
322 case DISK_EVENT: | |
323 return event_.disk.DebugString(); | |
324 case FORMAT_EVENT: | |
325 return event_.format.DebugString(); | |
326 case MOUNT_EVENT: | |
327 return event_.mount.DebugString(); | |
328 } | |
329 return "(Unknown event type)"; | |
330 } | |
331 }; | |
332 | |
333 // A mock |Observer| class which records all invocation of the methods invoked | |
334 // from DiskMountManager and all the arguments passed to them. | |
155 class MockDiskMountManagerObserver : public DiskMountManager::Observer { | 335 class MockDiskMountManagerObserver : public DiskMountManager::Observer { |
156 public: | 336 public: |
157 virtual ~MockDiskMountManagerObserver() {} | 337 MockDiskMountManagerObserver(const DiskMountManager* manager) |
158 | 338 : manager_(manager) {} |
159 MOCK_METHOD2(OnDiskEvent, void(DiskMountManager::DiskEvent event, | 339 ~MockDiskMountManagerObserver() override {} |
160 const DiskMountManager::Disk* disk)); | 340 |
161 MOCK_METHOD2(OnDeviceEvent, void(DiskMountManager::DeviceEvent event, | 341 // Mock notify methods. |
162 const std::string& device_path)); | 342 void OnDeviceEvent(DiskMountManager::DeviceEvent event, |
163 MOCK_METHOD3(OnMountEvent, | 343 const std::string& device_path) override { |
164 void(DiskMountManager::MountEvent event, | 344 events_.push_back( |
165 chromeos::MountError error_code, | 345 ObserverEvent::FromDeviceEvent(DeviceEvent(event, device_path))); |
166 const DiskMountManager::MountPointInfo& mount_point)); | 346 } |
167 MOCK_METHOD3(OnFormatEvent, | 347 |
168 void(DiskMountManager::FormatEvent event, | 348 void OnDiskEvent(DiskMountManager::DiskEvent event, |
169 chromeos::FormatError error_code, | 349 const DiskMountManager::Disk* disk) override { |
170 const std::string& device_path)); | 350 // Take a snapshot (copy) of the Disk object at the time of invocation for |
171 }; | 351 // later verification. |
172 | 352 events_.push_back(ObserverEvent::FromDiskEvent( |
173 // Expect |is_read_only| value of a disk object keyed by |source_path|. | 353 DiskEvent(event, new DiskMountManager::Disk(*disk)))); |
174 void ExpectDiskReadOnly(const DiskMountManager* manager, | 354 } |
175 const std::string& source_path, | 355 |
176 bool expected) { | 356 void OnFormatEvent(DiskMountManager::FormatEvent event, |
177 EXPECT_EQ(expected, | 357 chromeos::FormatError error_code, |
178 manager->disks().find(source_path)->second->is_read_only()); | 358 const std::string& device_path) override { |
359 events_.push_back(ObserverEvent::FromFormatEvent( | |
360 FormatEvent(event, error_code, device_path))); | |
361 } | |
362 | |
363 void OnMountEvent( | |
364 DiskMountManager::MountEvent event, | |
365 chromeos::MountError error_code, | |
366 const DiskMountManager::MountPointInfo& mount_point) override { | |
367 // Take a snapshot (copy) of a Disk object at the time of invocation. | |
368 // It can be verified later besides the arguments. | |
369 events_.push_back(ObserverEvent::FromMountEvent(MountEvent( | |
370 event, error_code, mount_point, | |
371 std::shared_ptr<DiskMountManager::Disk>(new DiskMountManager::Disk( | |
372 *manager_->disks().find(mount_point.source_path)->second))))); | |
373 } | |
374 | |
375 // Get invocation history to be verified by testcases. | |
376 DeviceEvent GetDeviceEvent(size_t index) { | |
377 DCHECK_GE(events_.size(), index + 1); | |
satorux1
2016/09/01 06:18:44
DCHECK_GT(events_.size(), index) may be easier to
yamaguchi
2016/09/02 13:25:42
Done.
| |
378 DCHECK(ObserverEvent::DEVICE_EVENT == events_.at(index).GetType()); | |
379 return events_[index].GetEvent().device; | |
380 } | |
381 | |
382 DiskEvent GetDiskEvent(size_t index) { | |
383 DCHECK_GE(events_.size(), index + 1); | |
384 DCHECK(ObserverEvent::DISK_EVENT == events_.at(index).GetType()); | |
385 return events_[index].GetEvent().disk; | |
386 } | |
387 | |
388 FormatEvent GetFormatEvent(size_t index) { | |
389 DCHECK_GE(events_.size(), index + 1); | |
390 DCHECK(ObserverEvent::FORMAT_EVENT == events_.at(index).GetType()); | |
391 return events_[index].GetEvent().format; | |
392 } | |
393 | |
394 MountEvent GetMountEvent(size_t index) { | |
395 DCHECK_GE(events_.size(), index + 1); | |
396 DCHECK(ObserverEvent::MOUNT_EVENT == events_.at(index).GetType()); | |
397 return events_[index].GetEvent().mount; | |
398 } | |
399 | |
400 size_t GetEventCount() { return events_.size(); } | |
401 | |
402 const ObserverEvent& GetEvent(size_t index) const { | |
403 return events_.at(index); | |
404 } | |
405 | |
406 const std::vector<ObserverEvent>& GetEvents() const { return events_; } | |
407 | |
408 private: | |
409 // Pointer to the manager object to which this |Observer| is registered. | |
410 const DiskMountManager* manager_; | |
411 | |
412 // Records all invocations. | |
413 std::vector<ObserverEvent> events_; | |
414 }; | |
415 | |
416 // Shift operators of ostream. | |
417 // Needed to print values in case of EXPECT_* failure in gtest. | |
418 std::ostream& operator<<(std::ostream& stream, | |
419 const DeviceEvent& device_event) { | |
420 return stream << device_event.DebugString(); | |
421 } | |
422 | |
423 std::ostream& operator<<(std::ostream& stream, const DiskEvent& disk_event) { | |
424 return stream << disk_event.DebugString(); | |
425 } | |
426 | |
427 std::ostream& operator<<(std::ostream& stream, | |
428 const FormatEvent& format_event) { | |
429 return stream << format_event.DebugString(); | |
430 } | |
431 | |
432 std::ostream& operator<<(std::ostream& stream, const MountEvent& mount_event) { | |
433 return stream << mount_event.DebugString(); | |
434 } | |
435 | |
436 std::ostream& operator<<(std::ostream& stream, const ObserverEvent& event) { | |
437 return stream << event.DebugString(); | |
179 } | 438 } |
180 | 439 |
181 class DiskMountManagerTest : public testing::Test { | 440 class DiskMountManagerTest : public testing::Test { |
182 public: | 441 public: |
183 DiskMountManagerTest() {} | 442 DiskMountManagerTest() : observer_(NULL) {} |
184 ~DiskMountManagerTest() override {} | 443 ~DiskMountManagerTest() override {} |
185 | 444 |
186 // Sets up test dbus tread manager and disks mount manager. | 445 // Sets up test dbus tread manager and disks mount manager. |
187 // Initializes disk mount manager disks and mount points. | 446 // Initializes disk mount manager disks and mount points. |
188 // Adds a test observer to the disk mount manager. | 447 // Adds a test observer to the disk mount manager. |
189 void SetUp() override { | 448 void SetUp() override { |
190 fake_cros_disks_client_ = new FakeCrosDisksClient; | 449 fake_cros_disks_client_ = new FakeCrosDisksClient; |
191 DBusThreadManager::GetSetterForTesting()->SetCrosDisksClient( | 450 DBusThreadManager::GetSetterForTesting()->SetCrosDisksClient( |
192 std::unique_ptr<CrosDisksClient>(fake_cros_disks_client_)); | 451 std::unique_ptr<CrosDisksClient>(fake_cros_disks_client_)); |
193 | 452 |
194 DiskMountManager::Initialize(); | 453 DiskMountManager::Initialize(); |
195 | 454 |
196 InitDisksAndMountPoints(); | 455 InitDisksAndMountPoints(); |
197 | 456 |
457 observer_ = MockDiskMountManagerObserver(DiskMountManager::GetInstance()); | |
198 DiskMountManager::GetInstance()->AddObserver(&observer_); | 458 DiskMountManager::GetInstance()->AddObserver(&observer_); |
199 } | 459 } |
200 | 460 |
201 // Shuts down dbus thread manager and disk moutn manager used in the test. | 461 // Shuts down dbus thread manager and disk moutn manager used in the test. |
202 void TearDown() override { | 462 void TearDown() override { |
203 DiskMountManager::GetInstance()->RemoveObserver(&observer_); | 463 DiskMountManager::GetInstance()->RemoveObserver(&observer_); |
204 DiskMountManager::Shutdown(); | 464 DiskMountManager::Shutdown(); |
205 DBusThreadManager::Shutdown(); | 465 DBusThreadManager::Shutdown(); |
206 } | 466 } |
207 | 467 |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
264 | 524 |
265 protected: | 525 protected: |
266 chromeos::FakeCrosDisksClient* fake_cros_disks_client_; | 526 chromeos::FakeCrosDisksClient* fake_cros_disks_client_; |
267 MockDiskMountManagerObserver observer_; | 527 MockDiskMountManagerObserver observer_; |
268 base::MessageLoopForUI message_loop_; | 528 base::MessageLoopForUI message_loop_; |
269 }; | 529 }; |
270 | 530 |
271 // Tests that the observer gets notified on attempt to format non existent mount | 531 // Tests that the observer gets notified on attempt to format non existent mount |
272 // point. | 532 // point. |
273 TEST_F(DiskMountManagerTest, Format_NotMounted) { | 533 TEST_F(DiskMountManagerTest, Format_NotMounted) { |
274 EXPECT_CALL(observer_, OnFormatEvent(DiskMountManager::FORMAT_COMPLETED, | |
275 chromeos::FORMAT_ERROR_UNKNOWN, | |
276 "/mount/non_existent")) | |
277 .Times(1); | |
278 DiskMountManager::GetInstance()->FormatMountedDevice("/mount/non_existent"); | 534 DiskMountManager::GetInstance()->FormatMountedDevice("/mount/non_existent"); |
satorux1
2016/09/01 06:18:44
Add ASSERT_GT(1, observer_.GetEventCount()) here a
satorux1
2016/09/06 04:30:43
did you miss this comment?
yamaguchi
2016/09/06 06:21:19
Sorry, I overlooked it.
Done.
| |
535 EXPECT_EQ(FormatEvent(DiskMountManager::FORMAT_COMPLETED, | |
536 chromeos::FORMAT_ERROR_UNKNOWN, "/mount/non_existent"), | |
537 observer_.GetFormatEvent(0)); | |
279 } | 538 } |
280 | 539 |
281 // Tests that the observer gets notified on attempt to format read-only mount | 540 // Tests that the observer gets notified on attempt to format read-only mount |
282 // point. | 541 // point. |
283 TEST_F(DiskMountManagerTest, Format_ReadOnly) { | 542 TEST_F(DiskMountManagerTest, Format_ReadOnly) { |
284 EXPECT_CALL(observer_, | |
285 OnFormatEvent(DiskMountManager::FORMAT_COMPLETED, | |
286 chromeos::FORMAT_ERROR_DEVICE_NOT_ALLOWED, | |
287 kReadOnlyMountpath)) | |
288 .Times(1); | |
289 DiskMountManager::GetInstance()->FormatMountedDevice(kReadOnlyMountpath); | 543 DiskMountManager::GetInstance()->FormatMountedDevice(kReadOnlyMountpath); |
544 EXPECT_EQ(FormatEvent(DiskMountManager::FORMAT_COMPLETED, | |
545 chromeos::FORMAT_ERROR_DEVICE_NOT_ALLOWED, | |
546 kReadOnlyMountpath), | |
547 observer_.GetFormatEvent(0)); | |
290 } | 548 } |
291 | 549 |
292 // Tests that it is not possible to format archive mount point. | 550 // Tests that it is not possible to format archive mount point. |
293 TEST_F(DiskMountManagerTest, Format_Archive) { | 551 TEST_F(DiskMountManagerTest, Format_Archive) { |
294 EXPECT_CALL(observer_, OnFormatEvent(DiskMountManager::FORMAT_COMPLETED, | |
295 chromeos::FORMAT_ERROR_UNKNOWN, | |
296 "/archive/source_path")) | |
297 .Times(1); | |
298 | |
299 DiskMountManager::GetInstance()->FormatMountedDevice("/archive/mount_path"); | 552 DiskMountManager::GetInstance()->FormatMountedDevice("/archive/mount_path"); |
553 EXPECT_EQ(FormatEvent(DiskMountManager::FORMAT_COMPLETED, | |
554 chromeos::FORMAT_ERROR_UNKNOWN, "/archive/source_path"), | |
555 observer_.GetFormatEvent(0)); | |
300 } | 556 } |
301 | 557 |
302 // Tests that format fails if the device cannot be unmounted. | 558 // Tests that format fails if the device cannot be unmounted. |
303 TEST_F(DiskMountManagerTest, Format_FailToUnmount) { | 559 TEST_F(DiskMountManagerTest, Format_FailToUnmount) { |
304 // Before formatting mounted device, the device should be unmounted. | 560 // Before formatting mounted device, the device should be unmounted. |
305 // In this test unmount will fail, and there should be no attempt to | 561 // In this test unmount will fail, and there should be no attempt to |
306 // format the device. | 562 // format the device. |
307 | 563 |
308 // Set up expectations for observer mock. | |
309 // Observer should be notified that unmount attempt fails and format task | |
310 // failed to start. | |
311 { | |
312 InSequence s; | |
313 | |
314 EXPECT_CALL(observer_, | |
315 OnMountEvent(DiskMountManager::UNMOUNTING, | |
316 chromeos::MOUNT_ERROR_INTERNAL, | |
317 Field(&DiskMountManager::MountPointInfo::mount_path, | |
318 "/device/mount_path"))) | |
319 .Times(1); | |
320 | |
321 EXPECT_CALL(observer_, OnFormatEvent(DiskMountManager::FORMAT_COMPLETED, | |
322 chromeos::FORMAT_ERROR_UNKNOWN, | |
323 "/device/source_path")) | |
324 .Times(1); | |
325 } | |
326 | |
327 fake_cros_disks_client_->MakeUnmountFail(); | 564 fake_cros_disks_client_->MakeUnmountFail(); |
328 // Start test. | 565 // Start test. |
329 DiskMountManager::GetInstance()->FormatMountedDevice("/device/mount_path"); | 566 DiskMountManager::GetInstance()->FormatMountedDevice("/device/mount_path"); |
330 | 567 |
331 // Cros disks will respond asynchronoulsy, so let's drain the message loop. | 568 // Cros disks will respond asynchronoulsy, so let's drain the message loop. |
332 message_loop_.RunUntilIdle(); | 569 message_loop_.RunUntilIdle(); |
333 | 570 |
571 // Observer should be notified that unmount attempt fails and format task | |
572 // failed to start. | |
573 MountEvent mount_event = observer_.GetMountEvent(0); | |
574 EXPECT_EQ(DiskMountManager::UNMOUNTING, mount_event.event); | |
575 EXPECT_EQ(chromeos::MOUNT_ERROR_INTERNAL, mount_event.error_code); | |
576 EXPECT_EQ("/device/mount_path", mount_event.mount_point.mount_path); | |
577 | |
578 EXPECT_EQ(FormatEvent(DiskMountManager::FORMAT_COMPLETED, | |
579 chromeos::FORMAT_ERROR_UNKNOWN, "/device/source_path"), | |
580 observer_.GetFormatEvent(1)); | |
334 EXPECT_EQ(1, fake_cros_disks_client_->unmount_call_count()); | 581 EXPECT_EQ(1, fake_cros_disks_client_->unmount_call_count()); |
335 EXPECT_EQ("/device/mount_path", | 582 EXPECT_EQ("/device/mount_path", |
336 fake_cros_disks_client_->last_unmount_device_path()); | 583 fake_cros_disks_client_->last_unmount_device_path()); |
337 EXPECT_EQ(chromeos::UNMOUNT_OPTIONS_NONE, | 584 EXPECT_EQ(chromeos::UNMOUNT_OPTIONS_NONE, |
338 fake_cros_disks_client_->last_unmount_options()); | 585 fake_cros_disks_client_->last_unmount_options()); |
339 EXPECT_EQ(0, fake_cros_disks_client_->format_call_count()); | 586 EXPECT_EQ(0, fake_cros_disks_client_->format_call_count()); |
340 | 587 |
341 // The device mount should still be here. | 588 // The device mount should still be here. |
342 EXPECT_TRUE(HasMountPoint("/device/mount_path")); | 589 EXPECT_TRUE(HasMountPoint("/device/mount_path")); |
343 } | 590 } |
344 | 591 |
345 // Tests that observer is notified when cros disks fails to start format | 592 // Tests that observer is notified when cros disks fails to start format |
346 // process. | 593 // process. |
347 TEST_F(DiskMountManagerTest, Format_FormatFailsToStart) { | 594 TEST_F(DiskMountManagerTest, Format_FormatFailsToStart) { |
348 // Before formatting mounted device, the device should be unmounted. | 595 // Before formatting mounted device, the device should be unmounted. |
349 // In this test, unmount will succeed, but call to Format method will | 596 // In this test, unmount will succeed, but call to Format method will |
350 // fail. | 597 // fail. |
351 | 598 |
352 // Set up expectations for observer mock. | |
353 // Observer should be notified that the device was unmounted and format task | |
354 // failed to start. | |
355 { | |
356 InSequence s; | |
357 | |
358 EXPECT_CALL(observer_, | |
359 OnMountEvent(DiskMountManager::UNMOUNTING, | |
360 chromeos::MOUNT_ERROR_NONE, | |
361 Field(&DiskMountManager::MountPointInfo::mount_path, | |
362 "/device/mount_path"))) | |
363 .Times(1); | |
364 | |
365 EXPECT_CALL(observer_, OnFormatEvent(DiskMountManager::FORMAT_COMPLETED, | |
366 chromeos::FORMAT_ERROR_UNKNOWN, | |
367 "/device/source_path")) | |
368 .Times(1); | |
369 } | |
370 | |
371 fake_cros_disks_client_->MakeFormatFail(); | 599 fake_cros_disks_client_->MakeFormatFail(); |
372 // Start the test. | 600 // Start the test. |
373 DiskMountManager::GetInstance()->FormatMountedDevice("/device/mount_path"); | 601 DiskMountManager::GetInstance()->FormatMountedDevice("/device/mount_path"); |
374 | 602 |
375 // Cros disks will respond asynchronoulsy, so let's drain the message loop. | 603 // Cros disks will respond asynchronoulsy, so let's drain the message loop. |
376 message_loop_.RunUntilIdle(); | 604 message_loop_.RunUntilIdle(); |
377 | 605 |
606 // Observer should be notified that the device was unmounted and format task | |
607 // failed to start. | |
608 MountEvent mount_event = observer_.GetMountEvent(0); | |
609 EXPECT_EQ(DiskMountManager::UNMOUNTING, mount_event.event); | |
610 EXPECT_EQ(chromeos::MOUNT_ERROR_NONE, mount_event.error_code); | |
611 EXPECT_EQ("/device/mount_path", mount_event.mount_point.mount_path); | |
612 | |
613 EXPECT_EQ(FormatEvent(DiskMountManager::FORMAT_COMPLETED, | |
614 chromeos::FORMAT_ERROR_UNKNOWN, "/device/source_path"), | |
615 observer_.GetFormatEvent(1)); | |
616 | |
378 EXPECT_EQ(1, fake_cros_disks_client_->unmount_call_count()); | 617 EXPECT_EQ(1, fake_cros_disks_client_->unmount_call_count()); |
379 EXPECT_EQ("/device/mount_path", | 618 EXPECT_EQ("/device/mount_path", |
380 fake_cros_disks_client_->last_unmount_device_path()); | 619 fake_cros_disks_client_->last_unmount_device_path()); |
381 EXPECT_EQ(chromeos::UNMOUNT_OPTIONS_NONE, | 620 EXPECT_EQ(chromeos::UNMOUNT_OPTIONS_NONE, |
382 fake_cros_disks_client_->last_unmount_options()); | 621 fake_cros_disks_client_->last_unmount_options()); |
383 EXPECT_EQ(1, fake_cros_disks_client_->format_call_count()); | 622 EXPECT_EQ(1, fake_cros_disks_client_->format_call_count()); |
384 EXPECT_EQ("/device/source_path", | 623 EXPECT_EQ("/device/source_path", |
385 fake_cros_disks_client_->last_format_device_path()); | 624 fake_cros_disks_client_->last_format_device_path()); |
386 EXPECT_EQ("vfat", fake_cros_disks_client_->last_format_filesystem()); | 625 EXPECT_EQ("vfat", fake_cros_disks_client_->last_format_filesystem()); |
387 | 626 |
388 // The device mount should be gone. | 627 // The device mount should be gone. |
389 EXPECT_FALSE(HasMountPoint("/device/mount_path")); | 628 EXPECT_FALSE(HasMountPoint("/device/mount_path")); |
390 } | 629 } |
391 | 630 |
392 // Tests the case where there are two format requests for the same device. | 631 // Tests the case where there are two format requests for the same device. |
393 TEST_F(DiskMountManagerTest, Format_ConcurrentFormatCalls) { | 632 TEST_F(DiskMountManagerTest, Format_ConcurrentFormatCalls) { |
394 // Only the first format request should be processed (the second unmount | 633 // Only the first format request should be processed (the second unmount |
395 // request fails because the device is already unmounted at that point). | 634 // request fails because the device is already unmounted at that point). |
396 // CrosDisksClient will report that the format process for the first request | 635 // CrosDisksClient will report that the format process for the first request |
397 // is successfully started. | 636 // is successfully started. |
398 | 637 |
399 // Set up expectations for observer mock. | 638 fake_cros_disks_client_->set_unmount_listener( |
639 base::Bind(&FakeCrosDisksClient::MakeUnmountFail, | |
640 base::Unretained(fake_cros_disks_client_))); | |
641 // Start the test. | |
642 DiskMountManager::GetInstance()->FormatMountedDevice("/device/mount_path"); | |
643 DiskMountManager::GetInstance()->FormatMountedDevice("/device/mount_path"); | |
644 | |
645 // Cros disks will respond asynchronoulsy, so let's drain the message loop. | |
646 message_loop_.RunUntilIdle(); | |
647 | |
400 // The observer should get a FORMAT_STARTED event for one format request and a | 648 // The observer should get a FORMAT_STARTED event for one format request and a |
401 // FORMAT_COMPLETED with an error code for the other format request. The | 649 // FORMAT_COMPLETED with an error code for the other format request. The |
402 // formatting will be started only for the first request. | 650 // formatting will be started only for the first request. |
403 // There should be only one UNMOUNTING event. The result of the second one | 651 // There should be only one UNMOUNTING event. The result of the second one |
404 // should not be reported as the mount point will go away after the first | 652 // should not be reported as the mount point will go away after the first |
405 // request. | 653 // request. |
406 // | 654 // |
407 // Note that in this test the format completion signal will not be simulated, | 655 // Note that in this test the format completion signal will not be simulated, |
408 // so the observer should not get FORMAT_COMPLETED signal. | 656 // so the observer should not get FORMAT_COMPLETED signal. |
409 { | |
410 InSequence s; | |
411 | 657 |
412 EXPECT_CALL(observer_, | 658 MountEvent mount_event = observer_.GetMountEvent(0); |
413 OnMountEvent(DiskMountManager::UNMOUNTING, | 659 EXPECT_EQ(DiskMountManager::UNMOUNTING, mount_event.event); |
414 chromeos::MOUNT_ERROR_NONE, | 660 EXPECT_EQ(chromeos::MOUNT_ERROR_NONE, mount_event.error_code); |
415 Field(&DiskMountManager::MountPointInfo::mount_path, | 661 EXPECT_EQ("/device/mount_path", mount_event.mount_point.mount_path); |
416 "/device/mount_path"))) | 662 EXPECT_EQ(FormatEvent(DiskMountManager::FORMAT_COMPLETED, |
417 .Times(1); | 663 chromeos::FORMAT_ERROR_UNKNOWN, "/device/source_path"), |
418 | 664 observer_.GetFormatEvent(1)); |
419 EXPECT_CALL(observer_, OnFormatEvent(DiskMountManager::FORMAT_COMPLETED, | 665 EXPECT_EQ(FormatEvent(DiskMountManager::FORMAT_STARTED, |
420 chromeos::FORMAT_ERROR_UNKNOWN, | 666 chromeos::FORMAT_ERROR_NONE, "/device/source_path"), |
421 "/device/source_path")) | 667 observer_.GetFormatEvent(2)); |
422 .Times(1); | |
423 | |
424 EXPECT_CALL(observer_, OnFormatEvent(DiskMountManager::FORMAT_STARTED, | |
425 chromeos::FORMAT_ERROR_NONE, | |
426 "/device/source_path")) | |
427 .Times(1); | |
428 } | |
429 | |
430 fake_cros_disks_client_->set_unmount_listener( | |
431 base::Bind(&FakeCrosDisksClient::MakeUnmountFail, | |
432 base::Unretained(fake_cros_disks_client_))); | |
433 // Start the test. | |
434 DiskMountManager::GetInstance()->FormatMountedDevice("/device/mount_path"); | |
435 DiskMountManager::GetInstance()->FormatMountedDevice("/device/mount_path"); | |
436 | |
437 // Cros disks will respond asynchronoulsy, so let's drain the message loop. | |
438 message_loop_.RunUntilIdle(); | |
439 | 668 |
440 EXPECT_EQ(2, fake_cros_disks_client_->unmount_call_count()); | 669 EXPECT_EQ(2, fake_cros_disks_client_->unmount_call_count()); |
441 EXPECT_EQ("/device/mount_path", | 670 EXPECT_EQ("/device/mount_path", |
442 fake_cros_disks_client_->last_unmount_device_path()); | 671 fake_cros_disks_client_->last_unmount_device_path()); |
443 EXPECT_EQ(chromeos::UNMOUNT_OPTIONS_NONE, | 672 EXPECT_EQ(chromeos::UNMOUNT_OPTIONS_NONE, |
444 fake_cros_disks_client_->last_unmount_options()); | 673 fake_cros_disks_client_->last_unmount_options()); |
445 EXPECT_EQ(1, fake_cros_disks_client_->format_call_count()); | 674 EXPECT_EQ(1, fake_cros_disks_client_->format_call_count()); |
446 EXPECT_EQ("/device/source_path", | 675 EXPECT_EQ("/device/source_path", |
447 fake_cros_disks_client_->last_format_device_path()); | 676 fake_cros_disks_client_->last_format_device_path()); |
448 EXPECT_EQ("vfat", | 677 EXPECT_EQ("vfat", |
449 fake_cros_disks_client_->last_format_filesystem()); | 678 fake_cros_disks_client_->last_format_filesystem()); |
450 | 679 |
451 // The device mount should be gone. | 680 // The device mount should be gone. |
452 EXPECT_FALSE(HasMountPoint("/device/mount_path")); | 681 EXPECT_FALSE(HasMountPoint("/device/mount_path")); |
453 } | 682 } |
454 | 683 |
684 // Verifies a |MountEvent| with the given condition. This function only checks | |
685 // the |mount_path| in |MountPointInfo| to make sure to match the event with | |
686 // preceding mount invocations. | |
687 void VerifyMountEvent(MountEvent mount_event, | |
688 DiskMountManager::MountEvent mount_event_type, | |
689 chromeos::MountError error_code, | |
690 const std::string& mount_path) { | |
691 EXPECT_EQ(mount_event_type, mount_event.event); | |
692 EXPECT_EQ(error_code, mount_event.error_code); | |
693 EXPECT_EQ(mount_path, mount_event.mount_point.mount_path); | |
694 } | |
695 | |
455 // Tests the case when the format process actually starts and fails. | 696 // Tests the case when the format process actually starts and fails. |
456 TEST_F(DiskMountManagerTest, Format_FormatFails) { | 697 TEST_F(DiskMountManagerTest, Format_FormatFails) { |
457 // Both unmount and format device cals are successful in this test. | 698 // Both unmount and format device cals are successful in this test. |
458 | 699 |
459 // Set up expectations for observer mock. | |
460 // The observer should get notified that the device was unmounted and that | |
461 // formatting has started. | |
462 // After the formatting starts, the test will simulate failing | |
463 // FORMAT_COMPLETED signal, so the observer should also be notified the | |
464 // formatting has failed (FORMAT_COMPLETED event). | |
465 { | |
466 InSequence s; | |
467 | |
468 EXPECT_CALL(observer_, | |
469 OnMountEvent(DiskMountManager::UNMOUNTING, | |
470 chromeos::MOUNT_ERROR_NONE, | |
471 Field(&DiskMountManager::MountPointInfo::mount_path, | |
472 "/device/mount_path"))) | |
473 .Times(1); | |
474 | |
475 EXPECT_CALL(observer_, OnFormatEvent(DiskMountManager::FORMAT_STARTED, | |
476 chromeos::FORMAT_ERROR_NONE, | |
477 "/device/source_path")) | |
478 .Times(1); | |
479 | |
480 EXPECT_CALL(observer_, OnFormatEvent(DiskMountManager::FORMAT_COMPLETED, | |
481 chromeos::FORMAT_ERROR_UNKNOWN, | |
482 "/device/source_path")) | |
483 .Times(1); | |
484 } | |
485 | |
486 // Start the test. | 700 // Start the test. |
487 DiskMountManager::GetInstance()->FormatMountedDevice("/device/mount_path"); | 701 DiskMountManager::GetInstance()->FormatMountedDevice("/device/mount_path"); |
488 | 702 |
489 // Wait for Unmount and Format calls to end. | 703 // Wait for Unmount and Format calls to end. |
490 message_loop_.RunUntilIdle(); | 704 message_loop_.RunUntilIdle(); |
491 | 705 |
492 EXPECT_EQ(1, fake_cros_disks_client_->unmount_call_count()); | 706 EXPECT_EQ(1, fake_cros_disks_client_->unmount_call_count()); |
493 EXPECT_EQ("/device/mount_path", | 707 EXPECT_EQ("/device/mount_path", |
494 fake_cros_disks_client_->last_unmount_device_path()); | 708 fake_cros_disks_client_->last_unmount_device_path()); |
495 EXPECT_EQ(chromeos::UNMOUNT_OPTIONS_NONE, | 709 EXPECT_EQ(chromeos::UNMOUNT_OPTIONS_NONE, |
496 fake_cros_disks_client_->last_unmount_options()); | 710 fake_cros_disks_client_->last_unmount_options()); |
497 EXPECT_EQ(1, fake_cros_disks_client_->format_call_count()); | 711 EXPECT_EQ(1, fake_cros_disks_client_->format_call_count()); |
498 EXPECT_EQ("/device/source_path", | 712 EXPECT_EQ("/device/source_path", |
499 fake_cros_disks_client_->last_format_device_path()); | 713 fake_cros_disks_client_->last_format_device_path()); |
500 EXPECT_EQ("vfat", fake_cros_disks_client_->last_format_filesystem()); | 714 EXPECT_EQ("vfat", fake_cros_disks_client_->last_format_filesystem()); |
501 | 715 |
502 // The device should be unmounted by now. | 716 // The device should be unmounted by now. |
503 EXPECT_FALSE(HasMountPoint("/device/mount_path")); | 717 EXPECT_FALSE(HasMountPoint("/device/mount_path")); |
504 | 718 |
505 // Send failing FORMAT_COMPLETED signal. | 719 // Send failing FORMAT_COMPLETED signal. |
506 // The failure is marked by ! in fromt of the path (but this should change | 720 // The failure is marked by ! in fromt of the path (but this should change |
507 // soon). | 721 // soon). |
508 fake_cros_disks_client_->SendFormatCompletedEvent( | 722 fake_cros_disks_client_->SendFormatCompletedEvent( |
509 chromeos::FORMAT_ERROR_UNKNOWN, "/device/source_path"); | 723 chromeos::FORMAT_ERROR_UNKNOWN, "/device/source_path"); |
724 | |
725 // The observer should get notified that the device was unmounted and that | |
726 // formatting has started. | |
727 // After the formatting starts, the test will simulate failing | |
728 // FORMAT_COMPLETED signal, so the observer should also be notified the | |
729 // formatting has failed (FORMAT_COMPLETED event). | |
730 ASSERT_EQ(3U, observer_.GetEventCount()); | |
731 VerifyMountEvent(observer_.GetMountEvent(0), DiskMountManager::UNMOUNTING, | |
732 chromeos::MOUNT_ERROR_NONE, "/device/mount_path"); | |
733 EXPECT_EQ(FormatEvent(DiskMountManager::FORMAT_STARTED, | |
734 chromeos::FORMAT_ERROR_NONE, "/device/source_path"), | |
735 observer_.GetFormatEvent(1)); | |
736 EXPECT_EQ(FormatEvent(DiskMountManager::FORMAT_COMPLETED, | |
737 chromeos::FORMAT_ERROR_UNKNOWN, "/device/source_path"), | |
738 observer_.GetFormatEvent(2)); | |
510 } | 739 } |
511 | 740 |
512 // Tests the case when formatting completes successfully. | 741 // Tests the case when formatting completes successfully. |
513 TEST_F(DiskMountManagerTest, Format_FormatSuccess) { | 742 TEST_F(DiskMountManagerTest, Format_FormatSuccess) { |
514 // Set up cros disks client mocks. | 743 // Set up cros disks client mocks. |
515 // Both unmount and format device cals are successful in this test. | 744 // Both unmount and format device cals are successful in this test. |
516 | 745 |
517 // Set up expectations for observer mock. | |
518 // The observer should receive UNMOUNTING, FORMAT_STARTED and FORMAT_COMPLETED | |
519 // events (all of them without an error set). | |
520 { | |
521 InSequence s; | |
522 | |
523 EXPECT_CALL(observer_, | |
524 OnMountEvent(DiskMountManager::UNMOUNTING, | |
525 chromeos::MOUNT_ERROR_NONE, | |
526 Field(&DiskMountManager::MountPointInfo::mount_path, | |
527 "/device/mount_path"))) | |
528 .Times(1); | |
529 | |
530 EXPECT_CALL(observer_, OnFormatEvent(DiskMountManager::FORMAT_STARTED, | |
531 chromeos::FORMAT_ERROR_NONE, | |
532 "/device/source_path")) | |
533 .Times(1); | |
534 | |
535 EXPECT_CALL(observer_, OnFormatEvent(DiskMountManager::FORMAT_COMPLETED, | |
536 chromeos::FORMAT_ERROR_NONE, | |
537 "/device/source_path")) | |
538 .Times(1); | |
539 } | |
540 | |
541 // Start the test. | 746 // Start the test. |
542 DiskMountManager::GetInstance()->FormatMountedDevice("/device/mount_path"); | 747 DiskMountManager::GetInstance()->FormatMountedDevice("/device/mount_path"); |
543 | 748 |
544 // Wait for Unmount and Format calls to end. | 749 // Wait for Unmount and Format calls to end. |
545 message_loop_.RunUntilIdle(); | 750 message_loop_.RunUntilIdle(); |
546 | 751 |
547 EXPECT_EQ(1, fake_cros_disks_client_->unmount_call_count()); | 752 EXPECT_EQ(1, fake_cros_disks_client_->unmount_call_count()); |
548 EXPECT_EQ("/device/mount_path", | 753 EXPECT_EQ("/device/mount_path", |
549 fake_cros_disks_client_->last_unmount_device_path()); | 754 fake_cros_disks_client_->last_unmount_device_path()); |
550 EXPECT_EQ(chromeos::UNMOUNT_OPTIONS_NONE, | 755 EXPECT_EQ(chromeos::UNMOUNT_OPTIONS_NONE, |
551 fake_cros_disks_client_->last_unmount_options()); | 756 fake_cros_disks_client_->last_unmount_options()); |
552 EXPECT_EQ(1, fake_cros_disks_client_->format_call_count()); | 757 EXPECT_EQ(1, fake_cros_disks_client_->format_call_count()); |
553 EXPECT_EQ("/device/source_path", | 758 EXPECT_EQ("/device/source_path", |
554 fake_cros_disks_client_->last_format_device_path()); | 759 fake_cros_disks_client_->last_format_device_path()); |
555 EXPECT_EQ("vfat", fake_cros_disks_client_->last_format_filesystem()); | 760 EXPECT_EQ("vfat", fake_cros_disks_client_->last_format_filesystem()); |
556 | 761 |
557 // The device should be unmounted by now. | 762 // The device should be unmounted by now. |
558 EXPECT_FALSE(HasMountPoint("/device/mount_path")); | 763 EXPECT_FALSE(HasMountPoint("/device/mount_path")); |
559 | 764 |
560 // Simulate cros_disks reporting success. | 765 // Simulate cros_disks reporting success. |
561 fake_cros_disks_client_->SendFormatCompletedEvent( | 766 fake_cros_disks_client_->SendFormatCompletedEvent( |
562 chromeos::FORMAT_ERROR_NONE, "/device/source_path"); | 767 chromeos::FORMAT_ERROR_NONE, "/device/source_path"); |
768 | |
769 // The observer should receive UNMOUNTING, FORMAT_STARTED and FORMAT_COMPLETED | |
770 // events (all of them without an error set). | |
771 VerifyMountEvent(observer_.GetMountEvent(0), DiskMountManager::UNMOUNTING, | |
772 chromeos::MOUNT_ERROR_NONE, "/device/mount_path"); | |
773 EXPECT_EQ(FormatEvent(DiskMountManager::FORMAT_STARTED, | |
774 chromeos::FORMAT_ERROR_NONE, "/device/source_path"), | |
775 observer_.GetFormatEvent(1)); | |
776 EXPECT_EQ(FormatEvent(DiskMountManager::FORMAT_COMPLETED, | |
777 chromeos::FORMAT_ERROR_NONE, "/device/source_path"), | |
778 observer_.GetFormatEvent(2)); | |
779 } | |
780 | |
781 // Counts the number of |MountEvent| in |events| that matches the given | |
782 // condition. | |
783 size_t CountMountEvents(const std::vector<ObserverEvent>& events, | |
784 DiskMountManager::MountEvent mount_event_type, | |
785 chromeos::MountError error_code, | |
786 const std::string& mount_path) { | |
787 size_t matched = 0; | |
788 for (auto it : events) { | |
789 if (it.GetType() != ObserverEvent::MOUNT_EVENT) { | |
790 continue; | |
791 } | |
792 const MountEvent& mount_event = it.GetEvent().mount; | |
793 if (mount_event.event == mount_event_type && | |
794 mount_event.error_code == error_code && | |
795 mount_event.mount_point.mount_path == mount_path) { | |
796 matched++; | |
797 } | |
798 } | |
799 return matched; | |
800 } | |
801 | |
802 // Counts the number of |FormatEvent| in |events| that matches with | |
803 // |format_event|. | |
804 size_t CountFormatEvents(const std::vector<ObserverEvent>& events, | |
805 const FormatEvent& format_event) { | |
806 size_t matched = 0; | |
807 for (auto it : events) { | |
808 if (it.GetType() != ObserverEvent::FORMAT_EVENT) { | |
809 continue; | |
810 } | |
811 if (it.GetEvent().format == format_event) { | |
812 matched++; | |
813 } | |
814 } | |
815 return matched; | |
563 } | 816 } |
564 | 817 |
565 // Tests that it's possible to format the device twice in a row (this may not be | 818 // Tests that it's possible to format the device twice in a row (this may not be |
566 // true if the list of pending formats is not properly cleared). | 819 // true if the list of pending formats is not properly cleared). |
567 TEST_F(DiskMountManagerTest, Format_ConsecutiveFormatCalls) { | 820 TEST_F(DiskMountManagerTest, Format_ConsecutiveFormatCalls) { |
568 // All unmount and format device cals are successful in this test. | 821 // All unmount and format device cals are successful in this test. |
569 // Each of the should be made twice (once for each formatting task). | 822 // Each of the should be made twice (once for each formatting task). |
570 | 823 |
571 // Set up expectations for observer mock. | |
572 // The observer should receive UNMOUNTING, FORMAT_STARTED and FORMAT_COMPLETED | |
573 // events (all of them without an error set) twice (once for each formatting | |
574 // task). | |
575 // Also, there should be a MOUNTING event when the device remounting is | |
576 // simulated. | |
577 EXPECT_CALL(observer_, OnFormatEvent(DiskMountManager::FORMAT_COMPLETED, | |
578 chromeos::FORMAT_ERROR_NONE, | |
579 "/device/source_path")) | |
580 .Times(2); | |
581 | |
582 EXPECT_CALL(observer_, OnFormatEvent(DiskMountManager::FORMAT_STARTED, | |
583 chromeos::FORMAT_ERROR_NONE, | |
584 "/device/source_path")) | |
585 .Times(2); | |
586 | |
587 EXPECT_CALL(observer_, | |
588 OnMountEvent(DiskMountManager::UNMOUNTING, | |
589 chromeos::MOUNT_ERROR_NONE, | |
590 Field(&DiskMountManager::MountPointInfo::mount_path, | |
591 "/device/mount_path"))) | |
592 .Times(2); | |
593 | |
594 EXPECT_CALL(observer_, | |
595 OnMountEvent(DiskMountManager::MOUNTING, | |
596 chromeos::MOUNT_ERROR_NONE, | |
597 Field(&DiskMountManager::MountPointInfo::mount_path, | |
598 "/device/mount_path"))) | |
599 .Times(1); | |
600 | |
601 // Start the test. | 824 // Start the test. |
602 DiskMountManager::GetInstance()->FormatMountedDevice("/device/mount_path"); | 825 DiskMountManager::GetInstance()->FormatMountedDevice("/device/mount_path"); |
603 | 826 |
604 // Wait for Unmount and Format calls to end. | 827 // Wait for Unmount and Format calls to end. |
605 message_loop_.RunUntilIdle(); | 828 message_loop_.RunUntilIdle(); |
606 | 829 |
607 EXPECT_EQ(1, fake_cros_disks_client_->unmount_call_count()); | 830 EXPECT_EQ(1, fake_cros_disks_client_->unmount_call_count()); |
608 EXPECT_EQ("/device/mount_path", | 831 EXPECT_EQ("/device/mount_path", |
609 fake_cros_disks_client_->last_unmount_device_path()); | 832 fake_cros_disks_client_->last_unmount_device_path()); |
610 EXPECT_EQ(chromeos::UNMOUNT_OPTIONS_NONE, | 833 EXPECT_EQ(chromeos::UNMOUNT_OPTIONS_NONE, |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
642 EXPECT_EQ(chromeos::UNMOUNT_OPTIONS_NONE, | 865 EXPECT_EQ(chromeos::UNMOUNT_OPTIONS_NONE, |
643 fake_cros_disks_client_->last_unmount_options()); | 866 fake_cros_disks_client_->last_unmount_options()); |
644 EXPECT_EQ(2, fake_cros_disks_client_->format_call_count()); | 867 EXPECT_EQ(2, fake_cros_disks_client_->format_call_count()); |
645 EXPECT_EQ("/device/source_path", | 868 EXPECT_EQ("/device/source_path", |
646 fake_cros_disks_client_->last_format_device_path()); | 869 fake_cros_disks_client_->last_format_device_path()); |
647 EXPECT_EQ("vfat", fake_cros_disks_client_->last_format_filesystem()); | 870 EXPECT_EQ("vfat", fake_cros_disks_client_->last_format_filesystem()); |
648 | 871 |
649 // Simulate cros_disks reporting success. | 872 // Simulate cros_disks reporting success. |
650 fake_cros_disks_client_->SendFormatCompletedEvent( | 873 fake_cros_disks_client_->SendFormatCompletedEvent( |
651 chromeos::FORMAT_ERROR_NONE, "/device/source_path"); | 874 chromeos::FORMAT_ERROR_NONE, "/device/source_path"); |
875 | |
876 // The observer should receive UNMOUNTING, FORMAT_STARTED and FORMAT_COMPLETED | |
877 // events (all of them without an error set) twice (once for each formatting | |
878 // task). | |
879 // Also, there should be a MOUNTING event when the device remounting is | |
880 // simulated. | |
881 EXPECT_EQ(7U, observer_.GetEventCount()); | |
882 | |
883 EXPECT_EQ( | |
884 2U, CountFormatEvents( | |
885 observer_.GetEvents(), | |
886 FormatEvent(DiskMountManager::FORMAT_COMPLETED, | |
887 chromeos::FORMAT_ERROR_NONE, "/device/source_path"))); | |
888 | |
889 EXPECT_EQ(2U, CountFormatEvents(observer_.GetEvents(), | |
890 FormatEvent(DiskMountManager::FORMAT_STARTED, | |
891 chromeos::FORMAT_ERROR_NONE, | |
892 "/device/source_path"))); | |
893 | |
894 EXPECT_EQ( | |
895 2U, CountMountEvents(observer_.GetEvents(), DiskMountManager::UNMOUNTING, | |
896 chromeos::MOUNT_ERROR_NONE, "/device/mount_path")); | |
897 | |
898 EXPECT_EQ(1U, | |
899 CountMountEvents(observer_.GetEvents(), DiskMountManager::MOUNTING, | |
900 chromeos::MOUNT_ERROR_NONE, "/device/mount_path")); | |
652 } | 901 } |
653 | 902 |
654 TEST_F(DiskMountManagerTest, MountPath_RecordAccessMode) { | 903 TEST_F(DiskMountManagerTest, MountPath_RecordAccessMode) { |
655 DiskMountManager* manager = DiskMountManager::GetInstance(); | 904 DiskMountManager* manager = DiskMountManager::GetInstance(); |
656 const std::string kSourcePath1 = "/device/source_path"; | 905 const std::string kSourcePath1 = "/device/source_path"; |
657 const std::string kSourcePath2 = "/device/source_path2"; | 906 const std::string kSourcePath2 = "/device/source_path2"; |
658 const std::string kSourceFormat = std::string(); | 907 const std::string kSourceFormat = std::string(); |
659 const std::string kMountLabel = std::string(); // N/A for MOUNT_TYPE_DEVICE | 908 const std::string kMountLabel = std::string(); // N/A for MOUNT_TYPE_DEVICE |
660 // For MountCompleted. Must be non-empty strings. | 909 // For MountCompleted. Must be non-empty strings. |
661 const std::string kMountPath1 = "/media/foo"; | 910 const std::string kMountPath1 = "/media/foo"; |
662 const std::string kMountPath2 = "/media/bar"; | 911 const std::string kMountPath2 = "/media/bar"; |
663 | 912 |
664 // Event handlers of observers should be called. | |
665 EXPECT_CALL( | |
666 observer_, | |
667 OnMountEvent( | |
668 DiskMountManager::MOUNTING, chromeos::MOUNT_ERROR_NONE, | |
669 Field(&DiskMountManager::MountPointInfo::mount_path, kMountPath1))); | |
670 // For the 2nd source, the disk (block device) is not read-only but the | |
671 // test will mount it in read-only mode. | |
672 // Observers query |disks_| from |DiskMountManager| in its event handler for | |
673 // a mount completion event. Therefore |disks_| must be updated with correct | |
674 // |read_only| value before notifying to observers. | |
675 EXPECT_CALL( | |
676 observer_, | |
677 OnMountEvent( | |
678 DiskMountManager::MOUNTING, chromeos::MOUNT_ERROR_NONE, | |
679 Field(&DiskMountManager::MountPointInfo::mount_path, kMountPath2))) | |
680 .WillOnce(InvokeWithoutArgs( | |
681 // Verify if the disk appears read-only at the time of notification | |
682 // to observers. | |
683 [&]() { ExpectDiskReadOnly(manager, kSourcePath2, true); })); | |
684 | |
685 manager->MountPath(kSourcePath1, kSourceFormat, std::string(), | 913 manager->MountPath(kSourcePath1, kSourceFormat, std::string(), |
686 chromeos::MOUNT_TYPE_DEVICE, | 914 chromeos::MOUNT_TYPE_DEVICE, |
687 chromeos::MOUNT_ACCESS_MODE_READ_WRITE); | 915 chromeos::MOUNT_ACCESS_MODE_READ_WRITE); |
688 manager->MountPath(kSourcePath2, kSourceFormat, std::string(), | 916 manager->MountPath(kSourcePath2, kSourceFormat, std::string(), |
689 chromeos::MOUNT_TYPE_DEVICE, | 917 chromeos::MOUNT_TYPE_DEVICE, |
690 chromeos::MOUNT_ACCESS_MODE_READ_ONLY); | 918 chromeos::MOUNT_ACCESS_MODE_READ_ONLY); |
691 // Simulate cros_disks reporting mount completed. | 919 // Simulate cros_disks reporting mount completed. |
692 fake_cros_disks_client_->SendMountCompletedEvent( | 920 fake_cros_disks_client_->SendMountCompletedEvent( |
693 chromeos::MOUNT_ERROR_NONE, kSourcePath1, chromeos::MOUNT_TYPE_DEVICE, | 921 chromeos::MOUNT_ERROR_NONE, kSourcePath1, chromeos::MOUNT_TYPE_DEVICE, |
694 kMountPath1); | 922 kMountPath1); |
695 fake_cros_disks_client_->SendMountCompletedEvent( | 923 fake_cros_disks_client_->SendMountCompletedEvent( |
696 chromeos::MOUNT_ERROR_NONE, kSourcePath2, chromeos::MOUNT_TYPE_DEVICE, | 924 chromeos::MOUNT_ERROR_NONE, kSourcePath2, chromeos::MOUNT_TYPE_DEVICE, |
697 kMountPath2); | 925 kMountPath2); |
698 | 926 |
927 // Event handlers of observers should be called. | |
928 VerifyMountEvent(observer_.GetMountEvent(0), DiskMountManager::MOUNTING, | |
929 chromeos::MOUNT_ERROR_NONE, kMountPath1); | |
930 // For the 2nd source, the disk (block device) is not read-only but the | |
931 // test will mount it in read-only mode. | |
932 // Observers query |disks_| from |DiskMountManager| in its event handler for | |
933 // a mount completion event. Therefore |disks_| must be updated with correct | |
934 // |read_only| value before notifying to observers. | |
935 MountEvent secondMountEvent = observer_.GetMountEvent(1); | |
936 EXPECT_EQ(DiskMountManager::MOUNTING, secondMountEvent.event); | |
937 EXPECT_EQ(chromeos::MOUNT_ERROR_NONE, secondMountEvent.error_code); | |
938 EXPECT_EQ(kMountPath2, secondMountEvent.mount_point.mount_path); | |
939 // Verify if the disk appears read-only at the time of notification to | |
940 // observers. | |
941 EXPECT_TRUE(secondMountEvent.disk->is_read_only()); | |
942 | |
943 // Verify the final state of manager->disks. | |
699 const DiskMountManager::DiskMap& disks = manager->disks(); | 944 const DiskMountManager::DiskMap& disks = manager->disks(); |
700 ASSERT_GT(disks.count(kSourcePath1), 0U); | 945 ASSERT_GT(disks.count(kSourcePath1), 0U); |
701 EXPECT_FALSE(disks.find(kSourcePath1)->second->is_read_only()); | 946 EXPECT_FALSE(disks.find(kSourcePath1)->second->is_read_only()); |
702 ASSERT_GT(disks.count(kSourcePath2), 0U); | 947 ASSERT_GT(disks.count(kSourcePath2), 0U); |
703 EXPECT_TRUE(disks.find(kSourcePath2)->second->is_read_only()); | 948 EXPECT_TRUE(disks.find(kSourcePath2)->second->is_read_only()); |
704 } | 949 } |
705 | 950 |
706 } // namespace | 951 } // namespace |
OLD | NEW |