Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(918)

Side by Side Diff: chrome/browser/chromeos/file_manager/mounted_disk_monitor_unittest.cc

Issue 23676008: Refactor MountedDiskMonitior and adds its test. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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.
4
5 #include "chrome/browser/chromeos/file_manager/mounted_disk_monitor.h"
6
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 "chromeos/dbus/fake_power_manager_client.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13
14 namespace file_manager {
15 namespace {
16
17 // Fake implementation of DiskMountManager. Does nothing but returns some
18 // disk information.
19 class FakeDiskMountManager : public chromeos::disks::DiskMountManager {
20 public:
21 FakeDiskMountManager() {}
22 virtual ~FakeDiskMountManager() {
23 STLDeleteValues(&disks_);
24 }
25
26 // DiskMountManager overrides.
27 virtual void AddObserver(Observer* observer) OVERRIDE {}
28 virtual void RemoveObserver(Observer* observer) OVERRIDE {}
29 virtual const DiskMap& disks() const OVERRIDE { return disks_; }
30
31 virtual const Disk* FindDiskBySourcePath(
32 const std::string& source_path) const OVERRIDE {
33 DiskMap::const_iterator iter = disks_.find(source_path);
34 if (iter == disks_.end())
35 return NULL;
36 return iter->second;
37 };
38
39 virtual const MountPointMap& mount_points() const OVERRIDE {
40 return mount_points_;
41 }
42 virtual void RequestMountInfoRefresh() OVERRIDE {}
43 virtual void MountPath(const std::string& source_path,
44 const std::string& source_format,
45 const std::string& mount_label,
46 chromeos::MountType type) OVERRIDE {}
47 virtual void UnmountPath(const std::string& mount_path,
48 chromeos::UnmountOptions options,
49 const UnmountPathCallback& callback) OVERRIDE {}
50 virtual void FormatMountedDevice(const std::string& mount_path) OVERRIDE {}
51 virtual void UnmountDeviceRecursively(
52 const std::string& device_path,
53 const UnmountDeviceRecursivelyCallbackType& callback) OVERRIDE {}
54
55 virtual bool AddDiskForTest(Disk* disk) OVERRIDE {
56 DCHECK(disk);
57 DCHECK(disks_.find(disk->device_path()) == disks_.end());
58 disks_[disk->device_path()] = disk;
59 return true;
60 }
61 virtual bool AddMountPointForTest(
62 const MountPointInfo& mount_point) OVERRIDE { return false; }
63
64 private:
65 DiskMap disks_;
66 MountPointMap mount_points_;
67
68 DISALLOW_COPY_AND_ASSIGN(FakeDiskMountManager);
69 };
70
71 // Creates a fake disk with |device_path| and |fs_uuid|.
72 scoped_ptr<chromeos::disks::DiskMountManager::Disk> CreateDisk(
73 const std::string& device_path,
74 const std::string& fs_uuid) {
75 return make_scoped_ptr(
76 new chromeos::disks::DiskMountManager::Disk(
77 device_path, "", "", "", "", "", "", "", "", "", fs_uuid, "",
78 chromeos::DEVICE_TYPE_USB, 0, false, false, false, false, false));
79 }
80
81 } // namespace
82
83 class MountedDiskMonitorTest : public testing::Test {
84 protected:
85 MountedDiskMonitorTest() {
86 }
87
88 virtual ~MountedDiskMonitorTest() {
89 }
90
91 virtual void SetUp() OVERRIDE {
92 power_manager_client_.reset(new chromeos::FakePowerManagerClient);
93 disk_mount_manager_.reset(new FakeDiskMountManager);
94 mounted_disk_monitor_.reset(new MountedDiskMonitor(
95 power_manager_client_.get(),
96 disk_mount_manager_.get()));
97 mounted_disk_monitor_->set_resuming_time_span_for_testing(
98 base::TimeDelta::FromSeconds(0));
99 }
100
101 base::MessageLoop message_loop_;
102 scoped_ptr<chromeos::FakePowerManagerClient> power_manager_client_;
103 scoped_ptr<FakeDiskMountManager> disk_mount_manager_;
104 scoped_ptr<MountedDiskMonitor> mounted_disk_monitor_;
105 };
106
107 // Makes sure that just mounting and unmounting repeatedly doesn't affect to
108 // "remounting" state.
109 TEST_F(MountedDiskMonitorTest, WithoutSuspend) {
110 scoped_ptr<chromeos::disks::DiskMountManager::Disk> disk(
111 CreateDisk("removable_device1", "uuid1"));
112
113 chromeos::disks::DiskMountManager::Disk* disk_ptr = disk.get();
114
115 const chromeos::disks::DiskMountManager::MountPointInfo kMountPoint(
116 "removable_device1", "/tmp/removable_device1",
117 chromeos::MOUNT_TYPE_DEVICE, chromeos::disks::MOUNT_CONDITION_NONE);
118
119 // The ownership of Disks are taken by DiskMountManager.
hashimoto 2013/09/09 14:34:24 nit: Now this comment is not needed because scoped
hidehiko 2013/09/09 15:18:54 Done.
120 disk_mount_manager_->AddDiskForTest(disk.release());
121
122 // First, the disk is not remounting.
123 EXPECT_FALSE(mounted_disk_monitor_->DiskIsRemounting(*disk_ptr));
124
125 // Simple mounting and unmounting doesn't affect remounting state.
126 mounted_disk_monitor_->OnMountEvent(
127 chromeos::disks::DiskMountManager::MOUNTING,
128 chromeos::MOUNT_ERROR_NONE,
129 kMountPoint);
130 EXPECT_FALSE(mounted_disk_monitor_->DiskIsRemounting(*disk_ptr));
131
132 mounted_disk_monitor_->OnMountEvent(
133 chromeos::disks::DiskMountManager::UNMOUNTING,
134 chromeos::MOUNT_ERROR_NONE,
135 kMountPoint);
136 EXPECT_FALSE(mounted_disk_monitor_->DiskIsRemounting(*disk_ptr));
137
138 // Mounting again also should not affect remounting state.
139 mounted_disk_monitor_->OnMountEvent(
140 chromeos::disks::DiskMountManager::MOUNTING,
141 chromeos::MOUNT_ERROR_NONE,
142 kMountPoint);
143 EXPECT_FALSE(mounted_disk_monitor_->DiskIsRemounting(*disk_ptr));
144 }
145
146 // Makes sure that the unmounting after system resuming triggers the
147 // "remounting" state, then after some period, the state is reset.
148 TEST_F(MountedDiskMonitorTest, SuspendAndResume) {
149 scoped_ptr<chromeos::disks::DiskMountManager::Disk> disk1(
150 CreateDisk("removable_device1", "uuid1"));
151 scoped_ptr<chromeos::disks::DiskMountManager::Disk> disk2(
152 CreateDisk("removable_device2", "uuid2"));
153
154 chromeos::disks::DiskMountManager::Disk* disk1_ptr = disk1.get();
155 chromeos::disks::DiskMountManager::Disk* disk2_ptr = disk2.get();
156
157 const chromeos::disks::DiskMountManager::MountPointInfo kMountPoint1(
158 "removable_device1", "/tmp/removable_device1",
159 chromeos::MOUNT_TYPE_DEVICE, chromeos::disks::MOUNT_CONDITION_NONE);
160 const chromeos::disks::DiskMountManager::MountPointInfo kMountPoint2(
161 "removable_device2", "/tmp/removable_device2",
162 chromeos::MOUNT_TYPE_DEVICE, chromeos::disks::MOUNT_CONDITION_NONE);
163
164 // The ownership of Disks are taken by DiskMountManager.
165 disk_mount_manager_->AddDiskForTest(disk1.release());
166 disk_mount_manager_->AddDiskForTest(disk2.release());
167
168 // Mount |disk1|.
169 mounted_disk_monitor_->OnMountEvent(
170 chromeos::disks::DiskMountManager::MOUNTING,
171 chromeos::MOUNT_ERROR_NONE,
172 kMountPoint1);
173 EXPECT_FALSE(mounted_disk_monitor_->DiskIsRemounting(*disk1_ptr));
174
175 // Pseudo system suspend and resume.
176 mounted_disk_monitor_->SuspendImminent();
177 mounted_disk_monitor_->SystemResumed(base::TimeDelta::FromSeconds(0));
178
179 // On system resume, we expect unmount and then mount immediately.
180 // During the phase, we expect the disk is remounting.
181 mounted_disk_monitor_->OnMountEvent(
182 chromeos::disks::DiskMountManager::UNMOUNTING,
183 chromeos::MOUNT_ERROR_NONE,
184 kMountPoint1);
185 EXPECT_TRUE(mounted_disk_monitor_->DiskIsRemounting(*disk1_ptr));
186
187 mounted_disk_monitor_->OnMountEvent(
188 chromeos::disks::DiskMountManager::MOUNTING,
189 chromeos::MOUNT_ERROR_NONE,
190 kMountPoint1);
191 EXPECT_TRUE(mounted_disk_monitor_->DiskIsRemounting(*disk1_ptr));
192
193 // New disk should not be "remounting."
194 EXPECT_FALSE(mounted_disk_monitor_->DiskIsRemounting(*disk2_ptr));
195 mounted_disk_monitor_->OnMountEvent(
196 chromeos::disks::DiskMountManager::MOUNTING,
197 chromeos::MOUNT_ERROR_NONE,
198 kMountPoint2);
199 EXPECT_FALSE(mounted_disk_monitor_->DiskIsRemounting(*disk2_ptr));
200
201 // After certain period, remounting state should be cleared.
202 base::RunLoop().RunUntilIdle(); // Emulate time passage.
203 EXPECT_FALSE(mounted_disk_monitor_->DiskIsRemounting(*disk1_ptr));
204 EXPECT_FALSE(mounted_disk_monitor_->DiskIsRemounting(*disk2_ptr));
205 }
206
207 } // namespace file_manager
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/file_manager/mounted_disk_monitor.cc ('k') | chrome/chrome_tests_unit.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698