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