This source file includes following definitions.
- SetUp
- TearDown
- HasMountPoint
- AddTestDisk
- AddTestMountPoint
- InitDisksAndMountPoints
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "base/bind.h"
#include "base/message_loop/message_loop.h"
#include "chromeos/dbus/fake_cros_disks_client.h"
#include "chromeos/dbus/fake_dbus_thread_manager.h"
#include "chromeos/disks/disk_mount_manager.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using chromeos::disks::DiskMountManager;
using chromeos::CrosDisksClient;
using chromeos::DBusThreadManager;
using chromeos::FakeCrosDisksClient;
using chromeos::FakeDBusThreadManager;
using testing::_;
using testing::Field;
using testing::InSequence;
namespace {
struct TestDiskInfo {
const char* source_path;
const char* mount_path;
const char* system_path;
const char* file_path;
const char* device_label;
const char* drive_label;
const char* vendor_id;
const char* vendor_name;
const char* product_id;
const char* product_name;
const char* fs_uuid;
const char* system_path_prefix;
chromeos::DeviceType device_type;
uint64 size_in_bytes;
bool is_parent;
bool is_read_only;
bool has_media;
bool on_boot_device;
bool is_hidden;
};
struct TestMountPointInfo {
const char* source_path;
const char* mount_path;
chromeos::MountType mount_type;
chromeos::disks::MountCondition mount_condition;
};
const TestDiskInfo kTestDisks[] = {
{
"/device/source_path",
"/device/mount_path",
"/device/prefix/system_path",
"/device/file_path",
"/device/device_label",
"/device/drive_label",
"/device/vendor_id",
"/device/vendor_name",
"/device/product_id",
"/device/product_name",
"/device/fs_uuid",
"/device/prefix",
chromeos::DEVICE_TYPE_USB,
1073741824,
false,
false,
true,
false,
false
},
};
const TestMountPointInfo kTestMountPoints[] = {
{
"/archive/source_path",
"/archive/mount_path",
chromeos::MOUNT_TYPE_ARCHIVE,
chromeos::disks::MOUNT_CONDITION_NONE
},
{
"/device/source_path",
"/device/mount_path",
chromeos::MOUNT_TYPE_DEVICE,
chromeos::disks::MOUNT_CONDITION_NONE
},
};
class MockDiskMountManagerObserver : public DiskMountManager::Observer {
public:
virtual ~MockDiskMountManagerObserver() {}
MOCK_METHOD2(OnDiskEvent, void(DiskMountManager::DiskEvent event,
const DiskMountManager::Disk* disk));
MOCK_METHOD2(OnDeviceEvent, void(DiskMountManager::DeviceEvent event,
const std::string& device_path));
MOCK_METHOD3(OnMountEvent,
void(DiskMountManager::MountEvent event,
chromeos::MountError error_code,
const DiskMountManager::MountPointInfo& mount_point));
MOCK_METHOD3(OnFormatEvent,
void(DiskMountManager::FormatEvent event,
chromeos::FormatError error_code,
const std::string& device_path));
};
class DiskMountManagerTest : public testing::Test {
public:
DiskMountManagerTest() {}
virtual ~DiskMountManagerTest() {}
virtual void SetUp() {
FakeDBusThreadManager* fake_thread_manager = new FakeDBusThreadManager();
fake_cros_disks_client_ = new FakeCrosDisksClient;
fake_thread_manager->SetCrosDisksClient(
scoped_ptr<CrosDisksClient>(fake_cros_disks_client_));
DBusThreadManager::InitializeForTesting(fake_thread_manager);
DiskMountManager::Initialize();
InitDisksAndMountPoints();
DiskMountManager::GetInstance()->AddObserver(&observer_);
}
virtual void TearDown() {
DiskMountManager::GetInstance()->RemoveObserver(&observer_);
DiskMountManager::Shutdown();
DBusThreadManager::Shutdown();
}
protected:
bool HasMountPoint(const std::string& mount_path) {
const DiskMountManager::MountPointMap& mount_points =
DiskMountManager::GetInstance()->mount_points();
return mount_points.find(mount_path) != mount_points.end();
}
private:
void AddTestDisk(const TestDiskInfo& disk) {
EXPECT_TRUE(DiskMountManager::GetInstance()->AddDiskForTest(
new DiskMountManager::Disk(disk.source_path,
disk.mount_path,
disk.system_path,
disk.file_path,
disk.device_label,
disk.drive_label,
disk.vendor_id,
disk.vendor_name,
disk.product_id,
disk.product_name,
disk.fs_uuid,
disk.system_path_prefix,
disk.device_type,
disk.size_in_bytes,
disk.is_parent,
disk.is_read_only,
disk.has_media,
disk.on_boot_device,
disk.is_hidden)));
}
void AddTestMountPoint(const TestMountPointInfo& mount_point) {
EXPECT_TRUE(DiskMountManager::GetInstance()->AddMountPointForTest(
DiskMountManager::MountPointInfo(mount_point.source_path,
mount_point.mount_path,
mount_point.mount_type,
mount_point.mount_condition)));
}
void InitDisksAndMountPoints() {
for (size_t i = 0; i < arraysize(kTestDisks); i++)
AddTestDisk(kTestDisks[i]);
for (size_t i = 0; i < arraysize(kTestMountPoints); i++)
AddTestMountPoint(kTestMountPoints[i]);
}
protected:
chromeos::FakeCrosDisksClient* fake_cros_disks_client_;
MockDiskMountManagerObserver observer_;
base::MessageLoopForUI message_loop_;
};
TEST_F(DiskMountManagerTest, Format_NotMounted) {
EXPECT_CALL(observer_, OnFormatEvent(DiskMountManager::FORMAT_STARTED,
chromeos::FORMAT_ERROR_UNKNOWN,
"/mount/non_existent"))
.Times(1);
DiskMountManager::GetInstance()->FormatMountedDevice("/mount/non_existent");
}
TEST_F(DiskMountManagerTest, Format_Archive) {
EXPECT_CALL(observer_, OnFormatEvent(DiskMountManager::FORMAT_STARTED,
chromeos::FORMAT_ERROR_UNKNOWN,
"/archive/source_path"))
.Times(1);
DiskMountManager::GetInstance()->FormatMountedDevice("/archive/mount_path");
}
TEST_F(DiskMountManagerTest, Format_FailToUnmount) {
{
InSequence s;
EXPECT_CALL(observer_,
OnMountEvent(DiskMountManager::UNMOUNTING,
chromeos::MOUNT_ERROR_INTERNAL,
Field(&DiskMountManager::MountPointInfo::mount_path,
"/device/mount_path")))
.Times(1);
EXPECT_CALL(observer_, OnFormatEvent(DiskMountManager::FORMAT_STARTED,
chromeos::FORMAT_ERROR_UNKNOWN,
"/device/source_path"))
.Times(1);
}
fake_cros_disks_client_->MakeUnmountFail();
DiskMountManager::GetInstance()->FormatMountedDevice("/device/mount_path");
message_loop_.RunUntilIdle();
EXPECT_EQ(1, fake_cros_disks_client_->unmount_call_count());
EXPECT_EQ("/device/mount_path",
fake_cros_disks_client_->last_unmount_device_path());
EXPECT_EQ(chromeos::UNMOUNT_OPTIONS_NONE,
fake_cros_disks_client_->last_unmount_options());
EXPECT_EQ(0, fake_cros_disks_client_->format_device_call_count());
EXPECT_TRUE(HasMountPoint("/device/mount_path"));
}
TEST_F(DiskMountManagerTest, Format_FormatFailsToStart) {
{
InSequence s;
EXPECT_CALL(observer_,
OnMountEvent(DiskMountManager::UNMOUNTING,
chromeos::MOUNT_ERROR_NONE,
Field(&DiskMountManager::MountPointInfo::mount_path,
"/device/mount_path")))
.Times(1);
EXPECT_CALL(observer_, OnFormatEvent(DiskMountManager::FORMAT_STARTED,
chromeos::FORMAT_ERROR_UNKNOWN,
"/device/source_path"))
.Times(1);
}
fake_cros_disks_client_->MakeFormatDeviceFail();
DiskMountManager::GetInstance()->FormatMountedDevice("/device/mount_path");
message_loop_.RunUntilIdle();
EXPECT_EQ(1, fake_cros_disks_client_->unmount_call_count());
EXPECT_EQ("/device/mount_path",
fake_cros_disks_client_->last_unmount_device_path());
EXPECT_EQ(chromeos::UNMOUNT_OPTIONS_NONE,
fake_cros_disks_client_->last_unmount_options());
EXPECT_EQ(1, fake_cros_disks_client_->format_device_call_count());
EXPECT_EQ("/device/source_path",
fake_cros_disks_client_->last_format_device_device_path());
EXPECT_EQ("vfat",
fake_cros_disks_client_->last_format_device_filesystem());
EXPECT_FALSE(HasMountPoint("/device/mount_path"));
}
TEST_F(DiskMountManagerTest, Format_ConcurrentFormatCalls) {
{
InSequence s;
EXPECT_CALL(observer_,
OnMountEvent(DiskMountManager::UNMOUNTING,
chromeos::MOUNT_ERROR_NONE,
Field(&DiskMountManager::MountPointInfo::mount_path,
"/device/mount_path")))
.Times(1);
EXPECT_CALL(observer_, OnFormatEvent(DiskMountManager::FORMAT_STARTED,
chromeos::FORMAT_ERROR_UNKNOWN,
"/device/source_path"))
.Times(1);
EXPECT_CALL(observer_, OnFormatEvent(DiskMountManager::FORMAT_STARTED,
chromeos::FORMAT_ERROR_NONE,
"/device/source_path"))
.Times(1);
}
fake_cros_disks_client_->set_unmount_listener(
base::Bind(&FakeCrosDisksClient::MakeUnmountFail,
base::Unretained(fake_cros_disks_client_)));
DiskMountManager::GetInstance()->FormatMountedDevice("/device/mount_path");
DiskMountManager::GetInstance()->FormatMountedDevice("/device/mount_path");
message_loop_.RunUntilIdle();
EXPECT_EQ(2, fake_cros_disks_client_->unmount_call_count());
EXPECT_EQ("/device/mount_path",
fake_cros_disks_client_->last_unmount_device_path());
EXPECT_EQ(chromeos::UNMOUNT_OPTIONS_NONE,
fake_cros_disks_client_->last_unmount_options());
EXPECT_EQ(1, fake_cros_disks_client_->format_device_call_count());
EXPECT_EQ("/device/source_path",
fake_cros_disks_client_->last_format_device_device_path());
EXPECT_EQ("vfat",
fake_cros_disks_client_->last_format_device_filesystem());
EXPECT_FALSE(HasMountPoint("/device/mount_path"));
}
TEST_F(DiskMountManagerTest, Format_FormatFails) {
{
InSequence s;
EXPECT_CALL(observer_,
OnMountEvent(DiskMountManager::UNMOUNTING,
chromeos::MOUNT_ERROR_NONE,
Field(&DiskMountManager::MountPointInfo::mount_path,
"/device/mount_path")))
.Times(1);
EXPECT_CALL(observer_, OnFormatEvent(DiskMountManager::FORMAT_STARTED,
chromeos::FORMAT_ERROR_NONE,
"/device/source_path"))
.Times(1);
EXPECT_CALL(observer_, OnFormatEvent(DiskMountManager::FORMAT_COMPLETED,
chromeos::FORMAT_ERROR_UNKNOWN,
"/device/source_path"))
.Times(1);
}
DiskMountManager::GetInstance()->FormatMountedDevice("/device/mount_path");
message_loop_.RunUntilIdle();
EXPECT_EQ(1, fake_cros_disks_client_->unmount_call_count());
EXPECT_EQ("/device/mount_path",
fake_cros_disks_client_->last_unmount_device_path());
EXPECT_EQ(chromeos::UNMOUNT_OPTIONS_NONE,
fake_cros_disks_client_->last_unmount_options());
EXPECT_EQ(1, fake_cros_disks_client_->format_device_call_count());
EXPECT_EQ("/device/source_path",
fake_cros_disks_client_->last_format_device_device_path());
EXPECT_EQ("vfat",
fake_cros_disks_client_->last_format_device_filesystem());
EXPECT_FALSE(HasMountPoint("/device/mount_path"));
fake_cros_disks_client_->SendMountEvent(
chromeos::CROS_DISKS_FORMATTING_FINISHED, "!/device/source_path");
}
TEST_F(DiskMountManagerTest, Format_FormatFailsAndReturnFilePath) {
{
InSequence s;
EXPECT_CALL(observer_,
OnMountEvent(DiskMountManager::UNMOUNTING,
chromeos::MOUNT_ERROR_NONE,
Field(&DiskMountManager::MountPointInfo::mount_path,
"/device/mount_path")))
.Times(1);
EXPECT_CALL(observer_, OnFormatEvent(DiskMountManager::FORMAT_STARTED,
chromeos::FORMAT_ERROR_NONE,
"/device/source_path"))
.Times(1);
EXPECT_CALL(observer_, OnFormatEvent(DiskMountManager::FORMAT_COMPLETED,
chromeos::FORMAT_ERROR_UNKNOWN,
"/device/source_path"))
.Times(1);
}
DiskMountManager::GetInstance()->FormatMountedDevice("/device/mount_path");
message_loop_.RunUntilIdle();
EXPECT_EQ(1, fake_cros_disks_client_->unmount_call_count());
EXPECT_EQ("/device/mount_path",
fake_cros_disks_client_->last_unmount_device_path());
EXPECT_EQ(chromeos::UNMOUNT_OPTIONS_NONE,
fake_cros_disks_client_->last_unmount_options());
EXPECT_EQ(1, fake_cros_disks_client_->format_device_call_count());
EXPECT_EQ("/device/source_path",
fake_cros_disks_client_->last_format_device_device_path());
EXPECT_EQ("vfat",
fake_cros_disks_client_->last_format_device_filesystem());
EXPECT_FALSE(HasMountPoint("/device/mount_path"));
fake_cros_disks_client_->SendMountEvent(
chromeos::CROS_DISKS_FORMATTING_FINISHED, "!/device/file_path");
}
TEST_F(DiskMountManagerTest, Format_FormatSuccess) {
{
InSequence s;
EXPECT_CALL(observer_,
OnMountEvent(DiskMountManager::UNMOUNTING,
chromeos::MOUNT_ERROR_NONE,
Field(&DiskMountManager::MountPointInfo::mount_path,
"/device/mount_path")))
.Times(1);
EXPECT_CALL(observer_, OnFormatEvent(DiskMountManager::FORMAT_STARTED,
chromeos::FORMAT_ERROR_NONE,
"/device/source_path"))
.Times(1);
EXPECT_CALL(observer_, OnFormatEvent(DiskMountManager::FORMAT_COMPLETED,
chromeos::FORMAT_ERROR_NONE,
"/device/source_path"))
.Times(1);
}
DiskMountManager::GetInstance()->FormatMountedDevice("/device/mount_path");
message_loop_.RunUntilIdle();
EXPECT_EQ(1, fake_cros_disks_client_->unmount_call_count());
EXPECT_EQ("/device/mount_path",
fake_cros_disks_client_->last_unmount_device_path());
EXPECT_EQ(chromeos::UNMOUNT_OPTIONS_NONE,
fake_cros_disks_client_->last_unmount_options());
EXPECT_EQ(1, fake_cros_disks_client_->format_device_call_count());
EXPECT_EQ("/device/source_path",
fake_cros_disks_client_->last_format_device_device_path());
EXPECT_EQ("vfat",
fake_cros_disks_client_->last_format_device_filesystem());
EXPECT_FALSE(HasMountPoint("/device/mount_path"));
fake_cros_disks_client_->SendMountEvent(
chromeos::CROS_DISKS_FORMATTING_FINISHED, "/device/source_path");
}
TEST_F(DiskMountManagerTest, Format_ConsecutiveFormatCalls) {
EXPECT_CALL(observer_, OnFormatEvent(DiskMountManager::FORMAT_COMPLETED,
chromeos::FORMAT_ERROR_NONE,
"/device/source_path"))
.Times(2);
EXPECT_CALL(observer_, OnFormatEvent(DiskMountManager::FORMAT_STARTED,
chromeos::FORMAT_ERROR_NONE,
"/device/source_path"))
.Times(2);
EXPECT_CALL(observer_,
OnMountEvent(DiskMountManager::UNMOUNTING,
chromeos::MOUNT_ERROR_NONE,
Field(&DiskMountManager::MountPointInfo::mount_path,
"/device/mount_path")))
.Times(2);
EXPECT_CALL(observer_,
OnMountEvent(DiskMountManager::MOUNTING,
chromeos::MOUNT_ERROR_NONE,
Field(&DiskMountManager::MountPointInfo::mount_path,
"/device/mount_path")))
.Times(1);
DiskMountManager::GetInstance()->FormatMountedDevice("/device/mount_path");
message_loop_.RunUntilIdle();
EXPECT_EQ(1, fake_cros_disks_client_->unmount_call_count());
EXPECT_EQ("/device/mount_path",
fake_cros_disks_client_->last_unmount_device_path());
EXPECT_EQ(chromeos::UNMOUNT_OPTIONS_NONE,
fake_cros_disks_client_->last_unmount_options());
EXPECT_EQ(1, fake_cros_disks_client_->format_device_call_count());
EXPECT_EQ("/device/source_path",
fake_cros_disks_client_->last_format_device_device_path());
EXPECT_EQ("vfat",
fake_cros_disks_client_->last_format_device_filesystem());
EXPECT_FALSE(HasMountPoint("/device/mount_path"));
fake_cros_disks_client_->SendMountEvent(
chromeos::CROS_DISKS_FORMATTING_FINISHED, "/device/source_path");
fake_cros_disks_client_->SendMountCompletedEvent(
chromeos::MOUNT_ERROR_NONE,
"/device/source_path",
chromeos::MOUNT_TYPE_DEVICE,
"/device/mount_path");
EXPECT_TRUE(HasMountPoint("/device/mount_path"));
DiskMountManager::GetInstance()->FormatMountedDevice("/device/mount_path");
message_loop_.RunUntilIdle();
EXPECT_EQ(2, fake_cros_disks_client_->unmount_call_count());
EXPECT_EQ("/device/mount_path",
fake_cros_disks_client_->last_unmount_device_path());
EXPECT_EQ(chromeos::UNMOUNT_OPTIONS_NONE,
fake_cros_disks_client_->last_unmount_options());
EXPECT_EQ(2, fake_cros_disks_client_->format_device_call_count());
EXPECT_EQ("/device/source_path",
fake_cros_disks_client_->last_format_device_device_path());
EXPECT_EQ("vfat",
fake_cros_disks_client_->last_format_device_filesystem());
fake_cros_disks_client_->SendMountEvent(
chromeos::CROS_DISKS_FORMATTING_FINISHED, "/device/source_path");
}
}