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