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