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 "base/stl_util.h"
11 #include "chrome/browser/chromeos/file_manager/fake_disk_mount_manager.h"
12 #include "chromeos/dbus/fake_power_manager_client.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 namespace file_manager
{
18 // Creates a fake disk with |device_path| and |fs_uuid|.
19 scoped_ptr
<chromeos::disks::DiskMountManager::Disk
> CreateDisk(
20 const std::string
& device_path
,
21 const std::string
& fs_uuid
) {
22 return make_scoped_ptr(
23 new chromeos::disks::DiskMountManager::Disk(
24 device_path
, "", "", "", "", "", "", "", "", "", fs_uuid
, "",
25 chromeos::DEVICE_TYPE_USB
, 0, false, false, false, false, false));
30 class MountedDiskMonitorTest
: public testing::Test
{
32 MountedDiskMonitorTest() {
35 virtual ~MountedDiskMonitorTest() {
38 virtual void SetUp() OVERRIDE
{
39 power_manager_client_
.reset(new chromeos::FakePowerManagerClient
);
40 disk_mount_manager_
.reset(new FakeDiskMountManager
);
41 mounted_disk_monitor_
.reset(new MountedDiskMonitor(
42 power_manager_client_
.get(),
43 disk_mount_manager_
.get()));
44 mounted_disk_monitor_
->set_resuming_time_span_for_testing(
45 base::TimeDelta::FromSeconds(0));
48 base::MessageLoop message_loop_
;
49 scoped_ptr
<chromeos::FakePowerManagerClient
> power_manager_client_
;
50 scoped_ptr
<FakeDiskMountManager
> disk_mount_manager_
;
51 scoped_ptr
<MountedDiskMonitor
> mounted_disk_monitor_
;
54 // Makes sure that just mounting and unmounting repeatedly doesn't affect to
55 // "remounting" state.
56 TEST_F(MountedDiskMonitorTest
, WithoutSuspend
) {
57 scoped_ptr
<chromeos::disks::DiskMountManager::Disk
> disk(
58 CreateDisk("removable_device1", "uuid1"));
60 chromeos::disks::DiskMountManager::Disk
* disk_ptr
= disk
.get();
62 const chromeos::disks::DiskMountManager::MountPointInfo
kMountPoint(
63 "removable_device1", "/tmp/removable_device1",
64 chromeos::MOUNT_TYPE_DEVICE
, chromeos::disks::MOUNT_CONDITION_NONE
);
66 ASSERT_TRUE(disk_mount_manager_
->AddDiskForTest(disk
.release()));
68 // First, the disk is not remounting.
69 EXPECT_FALSE(mounted_disk_monitor_
->DiskIsRemounting(*disk_ptr
));
71 // Simple mounting and unmounting doesn't affect remounting state.
72 mounted_disk_monitor_
->OnMountEvent(
73 chromeos::disks::DiskMountManager::MOUNTING
,
74 chromeos::MOUNT_ERROR_NONE
,
76 EXPECT_FALSE(mounted_disk_monitor_
->DiskIsRemounting(*disk_ptr
));
78 mounted_disk_monitor_
->OnMountEvent(
79 chromeos::disks::DiskMountManager::UNMOUNTING
,
80 chromeos::MOUNT_ERROR_NONE
,
82 EXPECT_FALSE(mounted_disk_monitor_
->DiskIsRemounting(*disk_ptr
));
84 // Mounting again also should not affect remounting state.
85 mounted_disk_monitor_
->OnMountEvent(
86 chromeos::disks::DiskMountManager::MOUNTING
,
87 chromeos::MOUNT_ERROR_NONE
,
89 EXPECT_FALSE(mounted_disk_monitor_
->DiskIsRemounting(*disk_ptr
));
92 // Makes sure that the unmounting after system resuming triggers the
93 // "remounting" state, then after some period, the state is reset.
94 TEST_F(MountedDiskMonitorTest
, SuspendAndResume
) {
95 scoped_ptr
<chromeos::disks::DiskMountManager::Disk
> disk1(
96 CreateDisk("removable_device1", "uuid1"));
97 scoped_ptr
<chromeos::disks::DiskMountManager::Disk
> disk2(
98 CreateDisk("removable_device2", "uuid2"));
100 chromeos::disks::DiskMountManager::Disk
* disk1_ptr
= disk1
.get();
101 chromeos::disks::DiskMountManager::Disk
* disk2_ptr
= disk2
.get();
103 const chromeos::disks::DiskMountManager::MountPointInfo
kMountPoint1(
104 "removable_device1", "/tmp/removable_device1",
105 chromeos::MOUNT_TYPE_DEVICE
, chromeos::disks::MOUNT_CONDITION_NONE
);
106 const chromeos::disks::DiskMountManager::MountPointInfo
kMountPoint2(
107 "removable_device2", "/tmp/removable_device2",
108 chromeos::MOUNT_TYPE_DEVICE
, chromeos::disks::MOUNT_CONDITION_NONE
);
110 ASSERT_TRUE(disk_mount_manager_
->AddDiskForTest(disk1
.release()));
111 ASSERT_TRUE(disk_mount_manager_
->AddDiskForTest(disk2
.release()));
114 mounted_disk_monitor_
->OnMountEvent(
115 chromeos::disks::DiskMountManager::MOUNTING
,
116 chromeos::MOUNT_ERROR_NONE
,
118 EXPECT_FALSE(mounted_disk_monitor_
->DiskIsRemounting(*disk1_ptr
));
120 // Pseudo system suspend and resume.
121 mounted_disk_monitor_
->SuspendImminent();
122 mounted_disk_monitor_
->SuspendDone(base::TimeDelta::FromSeconds(0));
124 // On system resume, we expect unmount and then mount immediately.
125 // During the phase, we expect the disk is remounting.
126 mounted_disk_monitor_
->OnMountEvent(
127 chromeos::disks::DiskMountManager::UNMOUNTING
,
128 chromeos::MOUNT_ERROR_NONE
,
130 EXPECT_TRUE(mounted_disk_monitor_
->DiskIsRemounting(*disk1_ptr
));
132 mounted_disk_monitor_
->OnMountEvent(
133 chromeos::disks::DiskMountManager::MOUNTING
,
134 chromeos::MOUNT_ERROR_NONE
,
136 EXPECT_TRUE(mounted_disk_monitor_
->DiskIsRemounting(*disk1_ptr
));
138 // New disk should not be "remounting."
139 EXPECT_FALSE(mounted_disk_monitor_
->DiskIsRemounting(*disk2_ptr
));
140 mounted_disk_monitor_
->OnMountEvent(
141 chromeos::disks::DiskMountManager::MOUNTING
,
142 chromeos::MOUNT_ERROR_NONE
,
144 EXPECT_FALSE(mounted_disk_monitor_
->DiskIsRemounting(*disk2_ptr
));
146 // After certain period, remounting state should be cleared.
147 base::RunLoop().RunUntilIdle(); // Emulate time passage.
148 EXPECT_FALSE(mounted_disk_monitor_
->DiskIsRemounting(*disk1_ptr
));
149 EXPECT_FALSE(mounted_disk_monitor_
->DiskIsRemounting(*disk2_ptr
));
152 } // namespace file_manager