Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(96)

Side by Side Diff: chromeos/disks/disk_mount_manager_unittest.cc

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

Powered by Google App Engine
This is Rietveld 408576698