1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
9 #include "chromeos/dbus/fake_power_manager_client.h"
10 #include "chromeos/disks/disk_mount_manager.h"
11 #include "chromeos/disks/mock_disk_mount_manager.h"
12 #include "chromeos/disks/suspend_unmount_manager.h"
13 #include "testing/gtest/include/gtest/gtest.h"
19 const char kDeviceId
[] = "device_id";
20 const char kDeviceLabel
[] = "device_label";
21 const char kVendor
[] = "vendor";
22 const char kProduct
[] = "product";
24 class FakeDiskMountManager
: public MockDiskMountManager
{
26 void NotifyUnmountDeviceComplete(MountError error
) const {
27 for (const UnmountPathCallback
& callback
: callbacks_
) {
32 const std::vector
<std::string
>& unmounting_mount_paths() const {
33 return unmounting_mount_paths_
;
37 void UnmountPath(const std::string
& mount_path
,
38 UnmountOptions options
,
39 const UnmountPathCallback
& callback
) override
{
40 unmounting_mount_paths_
.push_back(mount_path
);
41 callbacks_
.push_back(callback
);
43 std::vector
<std::string
> unmounting_mount_paths_
;
44 std::vector
<UnmountPathCallback
> callbacks_
;
47 class SuspendUnmountManagerTest
: public testing::Test
{
49 SuspendUnmountManagerTest()
50 : suspend_unmount_manager_(&disk_mount_manager_
, &fake_power_client_
) {}
51 ~SuspendUnmountManagerTest() override
{}
54 FakeDiskMountManager disk_mount_manager_
;
55 FakePowerManagerClient fake_power_client_
;
56 SuspendUnmountManager suspend_unmount_manager_
;
59 TEST_F(SuspendUnmountManagerTest
, Basic
) {
60 const std::string kDummyMountPathUsb
= "/dummy/mount/usb";
61 const std::string kDummyMountPathSd
= "/dummy/mount/sd";
62 const std::string kDummyMountPathUnknown
= "/dummy/mount/unknown";
63 disk_mount_manager_
.CreateDiskEntryForMountDevice(
64 chromeos::disks::DiskMountManager::MountPointInfo(
65 "/dummy/device/usb", kDummyMountPathUsb
, chromeos::MOUNT_TYPE_DEVICE
,
66 chromeos::disks::MOUNT_CONDITION_NONE
),
67 kDeviceId
, kDeviceLabel
, kVendor
, kProduct
, chromeos::DEVICE_TYPE_USB
,
68 1024 * 1024, false /* is_parent */, false /* has_media */,
69 false /* on_boot_device */, true /* on_removable_device */);
70 disk_mount_manager_
.CreateDiskEntryForMountDevice(
71 chromeos::disks::DiskMountManager::MountPointInfo(
72 "/dummy/device/sd", kDummyMountPathSd
, chromeos::MOUNT_TYPE_DEVICE
,
73 chromeos::disks::MOUNT_CONDITION_NONE
),
74 kDeviceId
, kDeviceLabel
, kVendor
, kProduct
, chromeos::DEVICE_TYPE_SD
,
75 1024 * 1024, false /* is_parent */, false /* has_media */,
76 false /* on_boot_device */, true /* on_removable_device */);
77 disk_mount_manager_
.CreateDiskEntryForMountDevice(
78 chromeos::disks::DiskMountManager::MountPointInfo(
79 "/dummy/device/unknown", kDummyMountPathUnknown
,
80 chromeos::MOUNT_TYPE_DEVICE
, chromeos::disks::MOUNT_CONDITION_NONE
),
81 kDeviceId
, kDeviceLabel
, kVendor
, kProduct
, chromeos::DEVICE_TYPE_UNKNOWN
,
82 1024 * 1024, false /* is_parent */, false /* has_media */,
83 false /* on_boot_device */, true /* on_removable_device */);
84 disk_mount_manager_
.SetupDefaultReplies();
85 fake_power_client_
.SendSuspendImminent();
87 EXPECT_EQ(1, fake_power_client_
.GetNumPendingSuspendReadinessCallbacks());
88 EXPECT_EQ(2u, disk_mount_manager_
.unmounting_mount_paths().size());
89 EXPECT_EQ(1, std::count(disk_mount_manager_
.unmounting_mount_paths().begin(),
90 disk_mount_manager_
.unmounting_mount_paths().end(),
92 EXPECT_EQ(1, std::count(disk_mount_manager_
.unmounting_mount_paths().begin(),
93 disk_mount_manager_
.unmounting_mount_paths().end(),
95 EXPECT_EQ(0, std::count(disk_mount_manager_
.unmounting_mount_paths().begin(),
96 disk_mount_manager_
.unmounting_mount_paths().end(),
97 kDummyMountPathUnknown
));
98 disk_mount_manager_
.NotifyUnmountDeviceComplete(MOUNT_ERROR_NONE
);
99 EXPECT_EQ(0, fake_power_client_
.GetNumPendingSuspendReadinessCallbacks());
102 TEST_F(SuspendUnmountManagerTest
, CancelAndSuspendAgain
) {
103 const std::string kDummyMountPath
= "/dummy/mount";
104 disk_mount_manager_
.CreateDiskEntryForMountDevice(
105 chromeos::disks::DiskMountManager::MountPointInfo(
106 "/dummy/device", kDummyMountPath
, chromeos::MOUNT_TYPE_DEVICE
,
107 chromeos::disks::MOUNT_CONDITION_NONE
),
108 kDeviceId
, kDeviceLabel
, kVendor
, kProduct
, chromeos::DEVICE_TYPE_USB
,
109 1024 * 1024, false /* is_parent */, false /* has_media */,
110 false /* on_boot_device */, true /* on_removable_device */);
111 disk_mount_manager_
.SetupDefaultReplies();
112 fake_power_client_
.SendSuspendImminent();
113 EXPECT_EQ(1, fake_power_client_
.GetNumPendingSuspendReadinessCallbacks());
114 ASSERT_EQ(1u, disk_mount_manager_
.unmounting_mount_paths().size());
115 EXPECT_EQ(kDummyMountPath
,
116 disk_mount_manager_
.unmounting_mount_paths().front());
118 // Suspend cancelled.
119 fake_power_client_
.SendSuspendDone();
122 fake_power_client_
.SendSuspendImminent();
123 ASSERT_EQ(2u, disk_mount_manager_
.unmounting_mount_paths().size());
124 EXPECT_EQ(kDummyMountPath
,
125 disk_mount_manager_
.unmounting_mount_paths().front());
129 } // namespace chromeos