blob: bc9d1f6b6554ee5627f2ccad582e245ccafc257e [file] [log] [blame]
[email protected]e3c1fc92012-11-15 00:56:461// 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
avi6e1a22d2015-12-21 03:43:205#include <stddef.h>
6#include <stdint.h>
7
[email protected]e3c1fc92012-11-15 00:56:468#include "base/bind.h"
[email protected]f129d2502013-07-17 22:45:509#include "base/message_loop/message_loop.h"
zelidrag29fe3382014-08-27 01:44:4810#include "chromeos/dbus/dbus_thread_manager.h"
[email protected]cc70f5012013-05-14 04:58:5611#include "chromeos/dbus/fake_cros_disks_client.h"
[email protected]e3c1fc92012-11-15 00:56:4612#include "chromeos/disks/disk_mount_manager.h"
13#include "testing/gmock/include/gmock/gmock.h"
14#include "testing/gtest/include/gtest/gtest.h"
15
16using chromeos::disks::DiskMountManager;
17using chromeos::CrosDisksClient;
18using chromeos::DBusThreadManager;
[email protected]cc70f5012013-05-14 04:58:5619using chromeos::FakeCrosDisksClient;
[email protected]e3c1fc92012-11-15 00:56:4620using testing::_;
21using testing::Field;
22using testing::InSequence;
23
24namespace {
25
26// Holds information needed to create a DiskMountManager::Disk instance.
27struct TestDiskInfo {
28 const char* source_path;
29 const char* mount_path;
30 const char* system_path;
31 const char* file_path;
32 const char* device_label;
33 const char* drive_label;
34 const char* vendor_id;
35 const char* vendor_name;
36 const char* product_id;
37 const char* product_name;
38 const char* fs_uuid;
39 const char* system_path_prefix;
40 chromeos::DeviceType device_type;
avi6e1a22d2015-12-21 03:43:2041 uint64_t size_in_bytes;
[email protected]e3c1fc92012-11-15 00:56:4642 bool is_parent;
43 bool is_read_only;
44 bool has_media;
45 bool on_boot_device;
[email protected]79ed457b2014-07-22 04:07:2646 bool on_removable_device;
[email protected]e3c1fc92012-11-15 00:56:4647 bool is_hidden;
48};
49
50// Holds information to create a DiskMOuntManager::MountPointInfo instance.
51struct TestMountPointInfo {
52 const char* source_path;
53 const char* mount_path;
54 chromeos::MountType mount_type;
55 chromeos::disks::MountCondition mount_condition;
56};
57
58// List of disks held in DiskMountManager at the begining of the test.
59const TestDiskInfo kTestDisks[] = {
60 {
61 "/device/source_path",
62 "/device/mount_path",
63 "/device/prefix/system_path",
64 "/device/file_path",
65 "/device/device_label",
66 "/device/drive_label",
67 "/device/vendor_id",
68 "/device/vendor_name",
69 "/device/product_id",
70 "/device/product_name",
71 "/device/fs_uuid",
72 "/device/prefix",
73 chromeos::DEVICE_TYPE_USB,
74 1073741824, // size in bytes
75 false, // is parent
76 false, // is read only
77 true, // has media
78 false, // is on boot device
[email protected]79ed457b2014-07-22 04:07:2679 true, // is on removable device
[email protected]e3c1fc92012-11-15 00:56:4680 false // is hidden
81 },
82};
83
[email protected]cc70f5012013-05-14 04:58:5684// List of mount points held in DiskMountManager at the begining of the test.
[email protected]e3c1fc92012-11-15 00:56:4685const TestMountPointInfo kTestMountPoints[] = {
86 {
87 "/archive/source_path",
88 "/archive/mount_path",
89 chromeos::MOUNT_TYPE_ARCHIVE,
90 chromeos::disks::MOUNT_CONDITION_NONE
91 },
92 {
93 "/device/source_path",
94 "/device/mount_path",
95 chromeos::MOUNT_TYPE_DEVICE,
96 chromeos::disks::MOUNT_CONDITION_NONE
97 },
98};
99
[email protected]e3c1fc92012-11-15 00:56:46100// Mocks DiskMountManager observer.
101class MockDiskMountManagerObserver : public DiskMountManager::Observer {
102 public:
103 virtual ~MockDiskMountManagerObserver() {}
104
105 MOCK_METHOD2(OnDiskEvent, void(DiskMountManager::DiskEvent event,
106 const DiskMountManager::Disk* disk));
107 MOCK_METHOD2(OnDeviceEvent, void(DiskMountManager::DeviceEvent event,
108 const std::string& device_path));
109 MOCK_METHOD3(OnMountEvent,
110 void(DiskMountManager::MountEvent event,
111 chromeos::MountError error_code,
112 const DiskMountManager::MountPointInfo& mount_point));
113 MOCK_METHOD3(OnFormatEvent,
114 void(DiskMountManager::FormatEvent event,
115 chromeos::FormatError error_code,
116 const std::string& device_path));
117};
118
119class DiskMountManagerTest : public testing::Test {
120 public:
121 DiskMountManagerTest() {}
dchengae98daa2015-01-21 20:30:49122 ~DiskMountManagerTest() override {}
[email protected]e3c1fc92012-11-15 00:56:46123
124 // Sets up test dbus tread manager and disks mount manager.
125 // Initializes disk mount manager disks and mount points.
126 // Adds a test observer to the disk mount manager.
dchengae98daa2015-01-21 20:30:49127 void SetUp() override {
[email protected]54652d82013-11-10 16:02:49128 fake_cros_disks_client_ = new FakeCrosDisksClient;
zelidrag29fe3382014-08-27 01:44:48129 DBusThreadManager::GetSetterForTesting()->SetCrosDisksClient(
dcheng0a6e80c2016-04-08 18:37:38130 std::unique_ptr<CrosDisksClient>(fake_cros_disks_client_));
[email protected]e3c1fc92012-11-15 00:56:46131
[email protected]e3c1fc92012-11-15 00:56:46132 DiskMountManager::Initialize();
133
134 InitDisksAndMountPoints();
135
136 DiskMountManager::GetInstance()->AddObserver(&observer_);
137 }
138
139 // Shuts down dbus thread manager and disk moutn manager used in the test.
dchengae98daa2015-01-21 20:30:49140 void TearDown() override {
[email protected]e3c1fc92012-11-15 00:56:46141 DiskMountManager::GetInstance()->RemoveObserver(&observer_);
142 DiskMountManager::Shutdown();
143 DBusThreadManager::Shutdown();
144 }
145
146 protected:
147 // Checks if disk mount manager contains a mount point with specified moutn
148 // path.
149 bool HasMountPoint(const std::string& mount_path) {
150 const DiskMountManager::MountPointMap& mount_points =
151 DiskMountManager::GetInstance()->mount_points();
152 return mount_points.find(mount_path) != mount_points.end();
153 }
154
155 private:
156 // Adds a new disk to the disk mount manager.
157 void AddTestDisk(const TestDiskInfo& disk) {
158 EXPECT_TRUE(DiskMountManager::GetInstance()->AddDiskForTest(
159 new DiskMountManager::Disk(disk.source_path,
160 disk.mount_path,
161 disk.system_path,
162 disk.file_path,
163 disk.device_label,
164 disk.drive_label,
165 disk.vendor_id,
166 disk.vendor_name,
167 disk.product_id,
168 disk.product_name,
169 disk.fs_uuid,
170 disk.system_path_prefix,
171 disk.device_type,
172 disk.size_in_bytes,
173 disk.is_parent,
174 disk.is_read_only,
175 disk.has_media,
176 disk.on_boot_device,
[email protected]79ed457b2014-07-22 04:07:26177 disk.on_removable_device,
[email protected]e3c1fc92012-11-15 00:56:46178 disk.is_hidden)));
179 }
180
181 // Adds a new mount point to the disk mount manager.
182 // If the moutn point is a device mount point, disk with its source path
183 // should already be added to the disk mount manager.
184 void AddTestMountPoint(const TestMountPointInfo& mount_point) {
185 EXPECT_TRUE(DiskMountManager::GetInstance()->AddMountPointForTest(
186 DiskMountManager::MountPointInfo(mount_point.source_path,
187 mount_point.mount_path,
188 mount_point.mount_type,
189 mount_point.mount_condition)));
190 }
191
192 // Adds disks and mount points to disk mount manager.
193 void InitDisksAndMountPoints() {
194 // Disks should be added first (when adding device mount points it is
195 // expected that the corresponding disk is already added).
196 for (size_t i = 0; i < arraysize(kTestDisks); i++)
197 AddTestDisk(kTestDisks[i]);
198
199 for (size_t i = 0; i < arraysize(kTestMountPoints); i++)
200 AddTestMountPoint(kTestMountPoints[i]);
201 }
202
203 protected:
[email protected]cc70f5012013-05-14 04:58:56204 chromeos::FakeCrosDisksClient* fake_cros_disks_client_;
[email protected]e3c1fc92012-11-15 00:56:46205 MockDiskMountManagerObserver observer_;
[email protected]df905632013-05-29 23:04:36206 base::MessageLoopForUI message_loop_;
[email protected]e3c1fc92012-11-15 00:56:46207};
208
209// Tests that the observer gets notified on attempt to format non existent mount
210// point.
211TEST_F(DiskMountManagerTest, Format_NotMounted) {
[email protected]f026c0f2014-05-06 21:52:35212 EXPECT_CALL(observer_, OnFormatEvent(DiskMountManager::FORMAT_COMPLETED,
[email protected]e3c1fc92012-11-15 00:56:46213 chromeos::FORMAT_ERROR_UNKNOWN,
214 "/mount/non_existent"))
215 .Times(1);
216 DiskMountManager::GetInstance()->FormatMountedDevice("/mount/non_existent");
217}
218
219// Tests that it is not possible to format archive mount point.
220TEST_F(DiskMountManagerTest, Format_Archive) {
[email protected]f026c0f2014-05-06 21:52:35221 EXPECT_CALL(observer_, OnFormatEvent(DiskMountManager::FORMAT_COMPLETED,
[email protected]e3c1fc92012-11-15 00:56:46222 chromeos::FORMAT_ERROR_UNKNOWN,
223 "/archive/source_path"))
224 .Times(1);
225
226 DiskMountManager::GetInstance()->FormatMountedDevice("/archive/mount_path");
227}
228
229// Tests that format fails if the device cannot be unmounted.
230TEST_F(DiskMountManagerTest, Format_FailToUnmount) {
[email protected]e3c1fc92012-11-15 00:56:46231 // Before formatting mounted device, the device should be unmounted.
232 // In this test unmount will fail, and there should be no attempt to
233 // format the device.
[email protected]e3c1fc92012-11-15 00:56:46234
235 // Set up expectations for observer mock.
236 // Observer should be notified that unmount attempt fails and format task
237 // failed to start.
238 {
239 InSequence s;
240
241 EXPECT_CALL(observer_,
242 OnMountEvent(DiskMountManager::UNMOUNTING,
243 chromeos::MOUNT_ERROR_INTERNAL,
244 Field(&DiskMountManager::MountPointInfo::mount_path,
245 "/device/mount_path")))
246 .Times(1);
247
[email protected]f026c0f2014-05-06 21:52:35248 EXPECT_CALL(observer_, OnFormatEvent(DiskMountManager::FORMAT_COMPLETED,
[email protected]e3c1fc92012-11-15 00:56:46249 chromeos::FORMAT_ERROR_UNKNOWN,
250 "/device/source_path"))
251 .Times(1);
252 }
253
[email protected]cc70f5012013-05-14 04:58:56254 fake_cros_disks_client_->MakeUnmountFail();
[email protected]e3c1fc92012-11-15 00:56:46255 // Start test.
256 DiskMountManager::GetInstance()->FormatMountedDevice("/device/mount_path");
257
258 // Cros disks will respond asynchronoulsy, so let's drain the message loop.
259 message_loop_.RunUntilIdle();
260
[email protected]cc70f5012013-05-14 04:58:56261 EXPECT_EQ(1, fake_cros_disks_client_->unmount_call_count());
262 EXPECT_EQ("/device/mount_path",
263 fake_cros_disks_client_->last_unmount_device_path());
264 EXPECT_EQ(chromeos::UNMOUNT_OPTIONS_NONE,
265 fake_cros_disks_client_->last_unmount_options());
[email protected]f026c0f2014-05-06 21:52:35266 EXPECT_EQ(0, fake_cros_disks_client_->format_call_count());
[email protected]cc70f5012013-05-14 04:58:56267
[email protected]e3c1fc92012-11-15 00:56:46268 // The device mount should still be here.
269 EXPECT_TRUE(HasMountPoint("/device/mount_path"));
270}
271
272// Tests that observer is notified when cros disks fails to start format
273// process.
274TEST_F(DiskMountManagerTest, Format_FormatFailsToStart) {
[email protected]e3c1fc92012-11-15 00:56:46275 // Before formatting mounted device, the device should be unmounted.
[email protected]f026c0f2014-05-06 21:52:35276 // In this test, unmount will succeed, but call to Format method will
[email protected]e3c1fc92012-11-15 00:56:46277 // fail.
[email protected]e3c1fc92012-11-15 00:56:46278
279 // Set up expectations for observer mock.
280 // Observer should be notified that the device was unmounted and format task
281 // failed to start.
282 {
283 InSequence s;
284
285 EXPECT_CALL(observer_,
286 OnMountEvent(DiskMountManager::UNMOUNTING,
287 chromeos::MOUNT_ERROR_NONE,
288 Field(&DiskMountManager::MountPointInfo::mount_path,
289 "/device/mount_path")))
290 .Times(1);
291
[email protected]f026c0f2014-05-06 21:52:35292 EXPECT_CALL(observer_, OnFormatEvent(DiskMountManager::FORMAT_COMPLETED,
[email protected]e3c1fc92012-11-15 00:56:46293 chromeos::FORMAT_ERROR_UNKNOWN,
294 "/device/source_path"))
295 .Times(1);
296 }
297
[email protected]f026c0f2014-05-06 21:52:35298 fake_cros_disks_client_->MakeFormatFail();
[email protected]e3c1fc92012-11-15 00:56:46299 // Start the test.
300 DiskMountManager::GetInstance()->FormatMountedDevice("/device/mount_path");
301
302 // Cros disks will respond asynchronoulsy, so let's drain the message loop.
303 message_loop_.RunUntilIdle();
304
[email protected]cc70f5012013-05-14 04:58:56305 EXPECT_EQ(1, fake_cros_disks_client_->unmount_call_count());
306 EXPECT_EQ("/device/mount_path",
307 fake_cros_disks_client_->last_unmount_device_path());
308 EXPECT_EQ(chromeos::UNMOUNT_OPTIONS_NONE,
309 fake_cros_disks_client_->last_unmount_options());
[email protected]f026c0f2014-05-06 21:52:35310 EXPECT_EQ(1, fake_cros_disks_client_->format_call_count());
[email protected]cc70f5012013-05-14 04:58:56311 EXPECT_EQ("/device/source_path",
[email protected]f026c0f2014-05-06 21:52:35312 fake_cros_disks_client_->last_format_device_path());
313 EXPECT_EQ("vfat", fake_cros_disks_client_->last_format_filesystem());
[email protected]cc70f5012013-05-14 04:58:56314
[email protected]e3c1fc92012-11-15 00:56:46315 // The device mount should be gone.
316 EXPECT_FALSE(HasMountPoint("/device/mount_path"));
317}
318
319// Tests the case where there are two format requests for the same device.
320TEST_F(DiskMountManagerTest, Format_ConcurrentFormatCalls) {
[email protected]8f919ee2013-03-14 19:53:29321 // Only the first format request should be processed (the second unmount
322 // request fails because the device is already unmounted at that point).
[email protected]e3c1fc92012-11-15 00:56:46323 // CrosDisksClient will report that the format process for the first request
324 // is successfully started.
[email protected]e3c1fc92012-11-15 00:56:46325
326 // Set up expectations for observer mock.
[email protected]f026c0f2014-05-06 21:52:35327 // The observer should get a FORMAT_STARTED event for one format request and a
328 // FORMAT_COMPLETED with an error code for the other format request. The
329 // formatting will be started only for the first request.
[email protected]8f919ee2013-03-14 19:53:29330 // There should be only one UNMOUNTING event. The result of the second one
331 // should not be reported as the mount point will go away after the first
332 // request.
[email protected]e3c1fc92012-11-15 00:56:46333 //
334 // Note that in this test the format completion signal will not be simulated,
335 // so the observer should not get FORMAT_COMPLETED signal.
336 {
337 InSequence s;
338
[email protected]e3c1fc92012-11-15 00:56:46339 EXPECT_CALL(observer_,
340 OnMountEvent(DiskMountManager::UNMOUNTING,
341 chromeos::MOUNT_ERROR_NONE,
342 Field(&DiskMountManager::MountPointInfo::mount_path,
343 "/device/mount_path")))
344 .Times(1);
345
[email protected]f026c0f2014-05-06 21:52:35346 EXPECT_CALL(observer_, OnFormatEvent(DiskMountManager::FORMAT_COMPLETED,
[email protected]8f919ee2013-03-14 19:53:29347 chromeos::FORMAT_ERROR_UNKNOWN,
348 "/device/source_path"))
349 .Times(1);
350
351 EXPECT_CALL(observer_, OnFormatEvent(DiskMountManager::FORMAT_STARTED,
[email protected]e3c1fc92012-11-15 00:56:46352 chromeos::FORMAT_ERROR_NONE,
353 "/device/source_path"))
354 .Times(1);
355 }
356
[email protected]cc70f5012013-05-14 04:58:56357 fake_cros_disks_client_->set_unmount_listener(
358 base::Bind(&FakeCrosDisksClient::MakeUnmountFail,
359 base::Unretained(fake_cros_disks_client_)));
[email protected]e3c1fc92012-11-15 00:56:46360 // Start the test.
361 DiskMountManager::GetInstance()->FormatMountedDevice("/device/mount_path");
362 DiskMountManager::GetInstance()->FormatMountedDevice("/device/mount_path");
363
364 // Cros disks will respond asynchronoulsy, so let's drain the message loop.
365 message_loop_.RunUntilIdle();
366
[email protected]cc70f5012013-05-14 04:58:56367 EXPECT_EQ(2, fake_cros_disks_client_->unmount_call_count());
368 EXPECT_EQ("/device/mount_path",
369 fake_cros_disks_client_->last_unmount_device_path());
370 EXPECT_EQ(chromeos::UNMOUNT_OPTIONS_NONE,
371 fake_cros_disks_client_->last_unmount_options());
[email protected]f026c0f2014-05-06 21:52:35372 EXPECT_EQ(1, fake_cros_disks_client_->format_call_count());
[email protected]cc70f5012013-05-14 04:58:56373 EXPECT_EQ("/device/source_path",
[email protected]f026c0f2014-05-06 21:52:35374 fake_cros_disks_client_->last_format_device_path());
[email protected]cc70f5012013-05-14 04:58:56375 EXPECT_EQ("vfat",
[email protected]f026c0f2014-05-06 21:52:35376 fake_cros_disks_client_->last_format_filesystem());
[email protected]cc70f5012013-05-14 04:58:56377
[email protected]e3c1fc92012-11-15 00:56:46378 // The device mount should be gone.
379 EXPECT_FALSE(HasMountPoint("/device/mount_path"));
380}
381
382// Tests the case when the format process actually starts and fails.
383TEST_F(DiskMountManagerTest, Format_FormatFails) {
[email protected]f026c0f2014-05-06 21:52:35384 // Both unmount and format device cals are successful in this test.
[email protected]e3c1fc92012-11-15 00:56:46385
386 // Set up expectations for observer mock.
387 // The observer should get notified that the device was unmounted and that
388 // formatting has started.
389 // After the formatting starts, the test will simulate failing
[email protected]a0278d52014-05-06 03:36:15390 // FORMAT_COMPLETED signal, so the observer should also be notified the
[email protected]e3c1fc92012-11-15 00:56:46391 // formatting has failed (FORMAT_COMPLETED event).
392 {
393 InSequence s;
394
395 EXPECT_CALL(observer_,
396 OnMountEvent(DiskMountManager::UNMOUNTING,
397 chromeos::MOUNT_ERROR_NONE,
398 Field(&DiskMountManager::MountPointInfo::mount_path,
399 "/device/mount_path")))
400 .Times(1);
401
402 EXPECT_CALL(observer_, OnFormatEvent(DiskMountManager::FORMAT_STARTED,
403 chromeos::FORMAT_ERROR_NONE,
404 "/device/source_path"))
405 .Times(1);
406
407 EXPECT_CALL(observer_, OnFormatEvent(DiskMountManager::FORMAT_COMPLETED,
408 chromeos::FORMAT_ERROR_UNKNOWN,
409 "/device/source_path"))
410 .Times(1);
411 }
412
413 // Start the test.
414 DiskMountManager::GetInstance()->FormatMountedDevice("/device/mount_path");
415
[email protected]f026c0f2014-05-06 21:52:35416 // Wait for Unmount and Format calls to end.
[email protected]e3c1fc92012-11-15 00:56:46417 message_loop_.RunUntilIdle();
418
[email protected]cc70f5012013-05-14 04:58:56419 EXPECT_EQ(1, fake_cros_disks_client_->unmount_call_count());
420 EXPECT_EQ("/device/mount_path",
421 fake_cros_disks_client_->last_unmount_device_path());
422 EXPECT_EQ(chromeos::UNMOUNT_OPTIONS_NONE,
423 fake_cros_disks_client_->last_unmount_options());
[email protected]f026c0f2014-05-06 21:52:35424 EXPECT_EQ(1, fake_cros_disks_client_->format_call_count());
[email protected]cc70f5012013-05-14 04:58:56425 EXPECT_EQ("/device/source_path",
[email protected]f026c0f2014-05-06 21:52:35426 fake_cros_disks_client_->last_format_device_path());
427 EXPECT_EQ("vfat", fake_cros_disks_client_->last_format_filesystem());
[email protected]cc70f5012013-05-14 04:58:56428
[email protected]e3c1fc92012-11-15 00:56:46429 // The device should be unmounted by now.
430 EXPECT_FALSE(HasMountPoint("/device/mount_path"));
431
[email protected]a0278d52014-05-06 03:36:15432 // Send failing FORMAT_COMPLETED signal.
[email protected]e3c1fc92012-11-15 00:56:46433 // The failure is marked by ! in fromt of the path (but this should change
434 // soon).
[email protected]a0278d52014-05-06 03:36:15435 fake_cros_disks_client_->SendFormatCompletedEvent(
436 chromeos::FORMAT_ERROR_UNKNOWN, "/device/source_path");
[email protected]e3c1fc92012-11-15 00:56:46437}
438
[email protected]e3c1fc92012-11-15 00:56:46439// Tests the case when formatting completes successfully.
440TEST_F(DiskMountManagerTest, Format_FormatSuccess) {
441 // Set up cros disks client mocks.
[email protected]f026c0f2014-05-06 21:52:35442 // Both unmount and format device cals are successful in this test.
[email protected]e3c1fc92012-11-15 00:56:46443
444 // Set up expectations for observer mock.
445 // The observer should receive UNMOUNTING, FORMAT_STARTED and FORMAT_COMPLETED
446 // events (all of them without an error set).
447 {
448 InSequence s;
449
450 EXPECT_CALL(observer_,
451 OnMountEvent(DiskMountManager::UNMOUNTING,
452 chromeos::MOUNT_ERROR_NONE,
453 Field(&DiskMountManager::MountPointInfo::mount_path,
454 "/device/mount_path")))
455 .Times(1);
456
457 EXPECT_CALL(observer_, OnFormatEvent(DiskMountManager::FORMAT_STARTED,
458 chromeos::FORMAT_ERROR_NONE,
459 "/device/source_path"))
460 .Times(1);
461
462 EXPECT_CALL(observer_, OnFormatEvent(DiskMountManager::FORMAT_COMPLETED,
463 chromeos::FORMAT_ERROR_NONE,
464 "/device/source_path"))
465 .Times(1);
466 }
467
468 // Start the test.
469 DiskMountManager::GetInstance()->FormatMountedDevice("/device/mount_path");
470
[email protected]f026c0f2014-05-06 21:52:35471 // Wait for Unmount and Format calls to end.
[email protected]e3c1fc92012-11-15 00:56:46472 message_loop_.RunUntilIdle();
473
[email protected]cc70f5012013-05-14 04:58:56474 EXPECT_EQ(1, fake_cros_disks_client_->unmount_call_count());
475 EXPECT_EQ("/device/mount_path",
476 fake_cros_disks_client_->last_unmount_device_path());
477 EXPECT_EQ(chromeos::UNMOUNT_OPTIONS_NONE,
478 fake_cros_disks_client_->last_unmount_options());
[email protected]f026c0f2014-05-06 21:52:35479 EXPECT_EQ(1, fake_cros_disks_client_->format_call_count());
[email protected]cc70f5012013-05-14 04:58:56480 EXPECT_EQ("/device/source_path",
[email protected]f026c0f2014-05-06 21:52:35481 fake_cros_disks_client_->last_format_device_path());
482 EXPECT_EQ("vfat", fake_cros_disks_client_->last_format_filesystem());
[email protected]cc70f5012013-05-14 04:58:56483
[email protected]e3c1fc92012-11-15 00:56:46484 // The device should be unmounted by now.
485 EXPECT_FALSE(HasMountPoint("/device/mount_path"));
486
487 // Simulate cros_disks reporting success.
[email protected]a0278d52014-05-06 03:36:15488 fake_cros_disks_client_->SendFormatCompletedEvent(
489 chromeos::FORMAT_ERROR_NONE, "/device/source_path");
[email protected]e3c1fc92012-11-15 00:56:46490}
491
492// Tests that it's possible to format the device twice in a row (this may not be
493// true if the list of pending formats is not properly cleared).
494TEST_F(DiskMountManagerTest, Format_ConsecutiveFormatCalls) {
[email protected]f026c0f2014-05-06 21:52:35495 // All unmount and format device cals are successful in this test.
[email protected]e3c1fc92012-11-15 00:56:46496 // Each of the should be made twice (once for each formatting task).
[email protected]e3c1fc92012-11-15 00:56:46497
498 // Set up expectations for observer mock.
499 // The observer should receive UNMOUNTING, FORMAT_STARTED and FORMAT_COMPLETED
500 // events (all of them without an error set) twice (once for each formatting
501 // task).
502 // Also, there should be a MOUNTING event when the device remounting is
503 // simulated.
504 EXPECT_CALL(observer_, OnFormatEvent(DiskMountManager::FORMAT_COMPLETED,
505 chromeos::FORMAT_ERROR_NONE,
506 "/device/source_path"))
507 .Times(2);
508
509 EXPECT_CALL(observer_, OnFormatEvent(DiskMountManager::FORMAT_STARTED,
510 chromeos::FORMAT_ERROR_NONE,
511 "/device/source_path"))
512 .Times(2);
513
514 EXPECT_CALL(observer_,
515 OnMountEvent(DiskMountManager::UNMOUNTING,
516 chromeos::MOUNT_ERROR_NONE,
517 Field(&DiskMountManager::MountPointInfo::mount_path,
518 "/device/mount_path")))
519 .Times(2);
520
521 EXPECT_CALL(observer_,
522 OnMountEvent(DiskMountManager::MOUNTING,
523 chromeos::MOUNT_ERROR_NONE,
524 Field(&DiskMountManager::MountPointInfo::mount_path,
525 "/device/mount_path")))
526 .Times(1);
527
528 // Start the test.
529 DiskMountManager::GetInstance()->FormatMountedDevice("/device/mount_path");
530
[email protected]f026c0f2014-05-06 21:52:35531 // Wait for Unmount and Format calls to end.
[email protected]e3c1fc92012-11-15 00:56:46532 message_loop_.RunUntilIdle();
533
[email protected]cc70f5012013-05-14 04:58:56534 EXPECT_EQ(1, fake_cros_disks_client_->unmount_call_count());
535 EXPECT_EQ("/device/mount_path",
536 fake_cros_disks_client_->last_unmount_device_path());
537 EXPECT_EQ(chromeos::UNMOUNT_OPTIONS_NONE,
538 fake_cros_disks_client_->last_unmount_options());
[email protected]f026c0f2014-05-06 21:52:35539 EXPECT_EQ(1, fake_cros_disks_client_->format_call_count());
[email protected]cc70f5012013-05-14 04:58:56540 EXPECT_EQ("/device/source_path",
[email protected]f026c0f2014-05-06 21:52:35541 fake_cros_disks_client_->last_format_device_path());
542 EXPECT_EQ("vfat", fake_cros_disks_client_->last_format_filesystem());
[email protected]cc70f5012013-05-14 04:58:56543
[email protected]e3c1fc92012-11-15 00:56:46544 // The device should be unmounted by now.
545 EXPECT_FALSE(HasMountPoint("/device/mount_path"));
546
547 // Simulate cros_disks reporting success.
[email protected]a0278d52014-05-06 03:36:15548 fake_cros_disks_client_->SendFormatCompletedEvent(
549 chromeos::FORMAT_ERROR_NONE, "/device/source_path");
[email protected]e3c1fc92012-11-15 00:56:46550
551 // Simulate the device remounting.
[email protected]cc70f5012013-05-14 04:58:56552 fake_cros_disks_client_->SendMountCompletedEvent(
[email protected]e3c1fc92012-11-15 00:56:46553 chromeos::MOUNT_ERROR_NONE,
554 "/device/source_path",
555 chromeos::MOUNT_TYPE_DEVICE,
556 "/device/mount_path");
557
558 EXPECT_TRUE(HasMountPoint("/device/mount_path"));
559
560 // Try formatting again.
561 DiskMountManager::GetInstance()->FormatMountedDevice("/device/mount_path");
562
[email protected]f026c0f2014-05-06 21:52:35563 // Wait for Unmount and Format calls to end.
[email protected]e3c1fc92012-11-15 00:56:46564 message_loop_.RunUntilIdle();
565
[email protected]cc70f5012013-05-14 04:58:56566 EXPECT_EQ(2, fake_cros_disks_client_->unmount_call_count());
567 EXPECT_EQ("/device/mount_path",
568 fake_cros_disks_client_->last_unmount_device_path());
569 EXPECT_EQ(chromeos::UNMOUNT_OPTIONS_NONE,
570 fake_cros_disks_client_->last_unmount_options());
[email protected]f026c0f2014-05-06 21:52:35571 EXPECT_EQ(2, fake_cros_disks_client_->format_call_count());
[email protected]cc70f5012013-05-14 04:58:56572 EXPECT_EQ("/device/source_path",
[email protected]f026c0f2014-05-06 21:52:35573 fake_cros_disks_client_->last_format_device_path());
574 EXPECT_EQ("vfat", fake_cros_disks_client_->last_format_filesystem());
[email protected]cc70f5012013-05-14 04:58:56575
[email protected]e3c1fc92012-11-15 00:56:46576 // Simulate cros_disks reporting success.
[email protected]a0278d52014-05-06 03:36:15577 fake_cros_disks_client_->SendFormatCompletedEvent(
578 chromeos::FORMAT_ERROR_NONE, "/device/source_path");
[email protected]e3c1fc92012-11-15 00:56:46579}
580
581} // namespace