1 // Copyright 2013 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.
5 #include "chrome/browser/chromeos/file_manager/mounted_disk_monitor.h"
7 #include "base/basictypes.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/run_loop.h"
10 #include "chromeos/dbus/fake_power_manager_client.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 namespace file_manager {
16 // Creates a fake disk with |device_path| and |fs_uuid|.
17 scoped_ptr<chromeos::disks::DiskMountManager::Disk> CreateDisk(
18 const std::string& device_path,
19 const std::string& fs_uuid) {
20 return make_scoped_ptr(
21 new chromeos::disks::DiskMountManager::Disk(
22 device_path, "", "", "", "", "", "", "", "", "", fs_uuid, "",
23 chromeos::DEVICE_TYPE_USB, 0, false, false, false, false, false,
29 class MountedDiskMonitorTest : public testing::Test {
31 virtual void SetUp() OVERRIDE {
32 power_manager_client_.reset(new chromeos::FakePowerManagerClient);
33 mounted_disk_monitor_.reset(new MountedDiskMonitor(
34 power_manager_client_.get()));
35 mounted_disk_monitor_->set_resuming_time_span_for_testing(
36 base::TimeDelta::FromSeconds(0));
39 base::MessageLoop message_loop_;
40 scoped_ptr<chromeos::FakePowerManagerClient> power_manager_client_;
41 scoped_ptr<MountedDiskMonitor> mounted_disk_monitor_;
44 // Makes sure that just mounting and unmounting repeatedly doesn't affect to
45 // "remounting" state.
46 TEST_F(MountedDiskMonitorTest, WithoutSuspend) {
47 scoped_ptr<chromeos::disks::DiskMountManager::Disk> disk(
48 CreateDisk("removable_device1", "uuid1"));
50 const chromeos::disks::DiskMountManager::MountPointInfo kMountPoint(
51 "removable_device1", "/tmp/removable_device1",
52 chromeos::MOUNT_TYPE_DEVICE, chromeos::disks::MOUNT_CONDITION_NONE);
54 // First, the disk is not remounting.
55 EXPECT_FALSE(mounted_disk_monitor_->DiskIsRemounting(*disk));
57 // Simple mounting and unmounting doesn't affect remounting state.
58 mounted_disk_monitor_->OnMountEvent(
59 chromeos::disks::DiskMountManager::MOUNTING,
60 chromeos::MOUNT_ERROR_NONE,
63 EXPECT_FALSE(mounted_disk_monitor_->DiskIsRemounting(*disk));
65 mounted_disk_monitor_->OnMountEvent(
66 chromeos::disks::DiskMountManager::UNMOUNTING,
67 chromeos::MOUNT_ERROR_NONE,
70 EXPECT_FALSE(mounted_disk_monitor_->DiskIsRemounting(*disk));
72 // Mounting again also should not affect remounting state.
73 mounted_disk_monitor_->OnMountEvent(
74 chromeos::disks::DiskMountManager::MOUNTING,
75 chromeos::MOUNT_ERROR_NONE,
78 EXPECT_FALSE(mounted_disk_monitor_->DiskIsRemounting(*disk));
81 // Makes sure that the unmounting after system resuming triggers the
82 // "remounting" state, then after some period, the state is reset.
83 TEST_F(MountedDiskMonitorTest, SuspendAndResume) {
84 scoped_ptr<chromeos::disks::DiskMountManager::Disk> disk1(
85 CreateDisk("removable_device1", "uuid1"));
86 scoped_ptr<chromeos::disks::DiskMountManager::Disk> disk2(
87 CreateDisk("removable_device2", "uuid2"));
89 const chromeos::disks::DiskMountManager::MountPointInfo kMountPoint1(
90 "removable_device1", "/tmp/removable_device1",
91 chromeos::MOUNT_TYPE_DEVICE, chromeos::disks::MOUNT_CONDITION_NONE);
92 const chromeos::disks::DiskMountManager::MountPointInfo kMountPoint2(
93 "removable_device2", "/tmp/removable_device2",
94 chromeos::MOUNT_TYPE_DEVICE, chromeos::disks::MOUNT_CONDITION_NONE);
97 mounted_disk_monitor_->OnMountEvent(
98 chromeos::disks::DiskMountManager::MOUNTING,
99 chromeos::MOUNT_ERROR_NONE,
102 EXPECT_FALSE(mounted_disk_monitor_->DiskIsRemounting(*disk1));
104 // Pseudo system suspend and resume.
105 mounted_disk_monitor_->SuspendImminent();
106 mounted_disk_monitor_->SuspendDone(base::TimeDelta::FromSeconds(0));
108 // On system resume, we expect unmount and then mount immediately.
109 // During the phase, we expect the disk is remounting.
110 mounted_disk_monitor_->OnMountEvent(
111 chromeos::disks::DiskMountManager::UNMOUNTING,
112 chromeos::MOUNT_ERROR_NONE,
115 EXPECT_TRUE(mounted_disk_monitor_->DiskIsRemounting(*disk1));
117 mounted_disk_monitor_->OnMountEvent(
118 chromeos::disks::DiskMountManager::MOUNTING,
119 chromeos::MOUNT_ERROR_NONE,
122 EXPECT_TRUE(mounted_disk_monitor_->DiskIsRemounting(*disk1));
124 // New disk should not be "remounting."
125 EXPECT_FALSE(mounted_disk_monitor_->DiskIsRemounting(*disk2));
126 mounted_disk_monitor_->OnMountEvent(
127 chromeos::disks::DiskMountManager::MOUNTING,
128 chromeos::MOUNT_ERROR_NONE,
131 EXPECT_FALSE(mounted_disk_monitor_->DiskIsRemounting(*disk2));
133 // After certain period, remounting state should be cleared.
134 base::RunLoop().RunUntilIdle(); // Emulate time passage.
135 EXPECT_FALSE(mounted_disk_monitor_->DiskIsRemounting(*disk1));
136 EXPECT_FALSE(mounted_disk_monitor_->DiskIsRemounting(*disk2));
139 } // namespace file_manager