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