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

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

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