Skip to content

Commit

Permalink
Reland: Unmount SD card when the device is suspended.
Browse files Browse the repository at this point in the history
Previous CL (crrev.com/1208823002) was reverted because it compares the reuslt
value of std::count and unsigned expected values in unitteest, though std::count
returns signed value.

BUG=434372
TEST=SuspendUnmountManagerTest

Review URL: https://codereview.chromium.org/1214083006

Cr-Commit-Position: refs/heads/master@{#337161}
  • Loading branch information
hirono-chromium authored and Commit bot committed Jul 2, 2015
1 parent e0ee5d3 commit 34699b1
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 16 deletions.
3 changes: 2 additions & 1 deletion chromeos/disks/suspend_unmount_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ void SuspendUnmountManager::SuspendImminent() {
return;
std::set<std::string> mount_paths;
for (const auto& pair : disk_mount_manager_->disks()) {
if (pair.second->device_type() == DEVICE_TYPE_USB &&
if ((pair.second->device_type() == DEVICE_TYPE_USB ||
pair.second->device_type() == DEVICE_TYPE_SD) &&
!pair.second->mount_path().empty()) {
mount_paths.insert(pair.second->mount_path());
}
Expand Down
69 changes: 54 additions & 15 deletions chromeos/disks/suspend_unmount_manager_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <algorithm>
#include <string>
#include <vector>

#include "chromeos/dbus/fake_power_manager_client.h"
#include "chromeos/disks/disk_mount_manager.h"
#include "chromeos/disks/mock_disk_mount_manager.h"
Expand All @@ -12,10 +16,17 @@ namespace chromeos {
namespace disks {
namespace {

const char kDeviceId[] = "device_id";
const char kDeviceLabel[] = "device_label";
const char kVendor[] = "vendor";
const char kProduct[] = "product";

class FakeDiskMountManager : public MockDiskMountManager {
public:
void NotifyUnmountDeviceComplete(MountError error) const {
callback_.Run(error);
for (const UnmountPathCallback& callback : callbacks_) {
callback.Run(error);
}
}

const std::vector<std::string>& unmounting_mount_paths() const {
Expand All @@ -27,10 +38,10 @@ class FakeDiskMountManager : public MockDiskMountManager {
UnmountOptions options,
const UnmountPathCallback& callback) override {
unmounting_mount_paths_.push_back(mount_path);
callback_ = callback;
callbacks_.push_back(callback);
}
std::vector<std::string> unmounting_mount_paths_;
UnmountPathCallback callback_;
std::vector<UnmountPathCallback> callbacks_;
};

class SuspendUnmountManagerTest : public testing::Test {
Expand All @@ -46,44 +57,72 @@ class SuspendUnmountManagerTest : public testing::Test {
};

TEST_F(SuspendUnmountManagerTest, Basic) {
const std::string dummy_mount_path = "/dummy/mount";
const std::string kDummyMountPathUsb = "/dummy/mount/usb";
const std::string kDummyMountPathSd = "/dummy/mount/sd";
const std::string kDummyMountPathUnknown = "/dummy/mount/unknown";
disk_mount_manager_.CreateDiskEntryForMountDevice(
chromeos::disks::DiskMountManager::MountPointInfo(
"/dummy/device/usb", kDummyMountPathUsb, chromeos::MOUNT_TYPE_DEVICE,
chromeos::disks::MOUNT_CONDITION_NONE),
kDeviceId, kDeviceLabel, kVendor, kProduct, chromeos::DEVICE_TYPE_USB,
1024 * 1024, false /* is_parent */, false /* has_media */,
false /* on_boot_device */, true /* on_removable_device */);
disk_mount_manager_.CreateDiskEntryForMountDevice(
chromeos::disks::DiskMountManager::MountPointInfo(
"/dummy/device", dummy_mount_path, chromeos::MOUNT_TYPE_DEVICE,
"/dummy/device/sd", kDummyMountPathSd, chromeos::MOUNT_TYPE_DEVICE,
chromeos::disks::MOUNT_CONDITION_NONE),
"device_id", "device_label", "Vendor", "Product",
chromeos::DEVICE_TYPE_USB, 1024 * 1024, true, true, true, false);
kDeviceId, kDeviceLabel, kVendor, kProduct, chromeos::DEVICE_TYPE_SD,
1024 * 1024, false /* is_parent */, false /* has_media */,
false /* on_boot_device */, true /* on_removable_device */);
disk_mount_manager_.CreateDiskEntryForMountDevice(
chromeos::disks::DiskMountManager::MountPointInfo(
"/dummy/device/unknown", kDummyMountPathUnknown,
chromeos::MOUNT_TYPE_DEVICE, chromeos::disks::MOUNT_CONDITION_NONE),
kDeviceId, kDeviceLabel, kVendor, kProduct, chromeos::DEVICE_TYPE_UNKNOWN,
1024 * 1024, false /* is_parent */, false /* has_media */,
false /* on_boot_device */, true /* on_removable_device */);
disk_mount_manager_.SetupDefaultReplies();
fake_power_client_.SendSuspendImminent();

EXPECT_EQ(1, fake_power_client_.GetNumPendingSuspendReadinessCallbacks());
ASSERT_EQ(1u, disk_mount_manager_.unmounting_mount_paths().size());
EXPECT_EQ(dummy_mount_path, disk_mount_manager_.unmounting_mount_paths()[0]);
EXPECT_EQ(2u, disk_mount_manager_.unmounting_mount_paths().size());
EXPECT_EQ(1, std::count(disk_mount_manager_.unmounting_mount_paths().begin(),
disk_mount_manager_.unmounting_mount_paths().end(),
kDummyMountPathUsb));
EXPECT_EQ(1, std::count(disk_mount_manager_.unmounting_mount_paths().begin(),
disk_mount_manager_.unmounting_mount_paths().end(),
kDummyMountPathSd));
EXPECT_EQ(0, std::count(disk_mount_manager_.unmounting_mount_paths().begin(),
disk_mount_manager_.unmounting_mount_paths().end(),
kDummyMountPathUnknown));
disk_mount_manager_.NotifyUnmountDeviceComplete(MOUNT_ERROR_NONE);
EXPECT_EQ(0, fake_power_client_.GetNumPendingSuspendReadinessCallbacks());
}

TEST_F(SuspendUnmountManagerTest, CancelAndSuspendAgain) {
const std::string dummy_mount_path = "/dummy/mount";
const std::string kDummyMountPath = "/dummy/mount";
disk_mount_manager_.CreateDiskEntryForMountDevice(
chromeos::disks::DiskMountManager::MountPointInfo(
"/dummy/device", dummy_mount_path, chromeos::MOUNT_TYPE_DEVICE,
"/dummy/device", kDummyMountPath, chromeos::MOUNT_TYPE_DEVICE,
chromeos::disks::MOUNT_CONDITION_NONE),
"device_id", "device_label", "Vendor", "Product",
chromeos::DEVICE_TYPE_USB, 1024 * 1024, true, true, true, false);
kDeviceId, kDeviceLabel, kVendor, kProduct, chromeos::DEVICE_TYPE_USB,
1024 * 1024, false /* is_parent */, false /* has_media */,
false /* on_boot_device */, true /* on_removable_device */);
disk_mount_manager_.SetupDefaultReplies();
fake_power_client_.SendSuspendImminent();
EXPECT_EQ(1, fake_power_client_.GetNumPendingSuspendReadinessCallbacks());
ASSERT_EQ(1u, disk_mount_manager_.unmounting_mount_paths().size());
EXPECT_EQ(dummy_mount_path, disk_mount_manager_.unmounting_mount_paths()[0]);
EXPECT_EQ(kDummyMountPath,
disk_mount_manager_.unmounting_mount_paths().front());

// Suspend cancelled.
fake_power_client_.SendSuspendDone();

// Suspend again.
fake_power_client_.SendSuspendImminent();
ASSERT_EQ(2u, disk_mount_manager_.unmounting_mount_paths().size());
EXPECT_EQ(dummy_mount_path, disk_mount_manager_.unmounting_mount_paths()[1]);
EXPECT_EQ(kDummyMountPath,
disk_mount_manager_.unmounting_mount_paths().front());
}

} // namespace
Expand Down

0 comments on commit 34699b1

Please sign in to comment.