OLD | NEW |
---|---|
(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/power_manager_client.h" | |
12 #include "testing/gtest/include/gtest/gtest.h" | |
13 | |
14 namespace file_manager { | |
15 namespace { | |
16 | |
17 // Fake implementation of PowerManagerClient. Does nothing. | |
18 class FakePowerManagerClient : public chromeos::PowerManagerClient { | |
hashimoto
2013/09/09 12:15:17
I think you can use the one declared in chromeos/d
hidehiko
2013/09/09 12:44:11
Great to know. I checked once, but I seemed to ove
| |
19 public: | |
20 FakePowerManagerClient() {} | |
21 virtual ~FakePowerManagerClient() {} | |
22 | |
23 // DBusClient overrides. | |
24 virtual void Init(dbus::Bus* bus) OVERRIDE {} | |
25 | |
26 // PowerManagerClient overrides. | |
27 virtual void AddObserver(Observer* observer) OVERRIDE {} | |
28 virtual void RemoveObserver(Observer* observer) OVERRIDE {} | |
29 virtual bool HasObserver(Observer* observer) OVERRIDE { return false; } | |
30 virtual void DecreaseScreenBrightness(bool allow_off) OVERRIDE {} | |
31 virtual void IncreaseScreenBrightness() OVERRIDE {} | |
32 virtual void SetScreenBrightnessPercent( | |
33 double percent, bool gradual) OVERRIDE {} | |
34 virtual void GetScreenBrightnessPercent( | |
35 const chromeos::GetScreenBrightnessPercentCallback& callback) OVERRIDE {} | |
36 virtual void DecreaseKeyboardBrightness() OVERRIDE {} | |
37 virtual void IncreaseKeyboardBrightness() OVERRIDE {} | |
38 virtual void RequestStatusUpdate() OVERRIDE {} | |
39 virtual void RequestRestart() OVERRIDE {} | |
40 virtual void RequestShutdown() OVERRIDE {} | |
41 virtual void RequestIdleNotification(int64 threshold_secs) OVERRIDE {} | |
42 virtual void NotifyUserActivity( | |
43 power_manager::UserActivityType type) OVERRIDE {} | |
44 virtual void NotifyVideoActivity(bool is_fullscreen) OVERRIDE {} | |
45 virtual void SetPolicy( | |
46 const power_manager::PowerManagementPolicy& policy) OVERRIDE {} | |
47 virtual void SetIsProjecting(bool is_projecting) OVERRIDE {} | |
48 virtual base::Closure GetSuspendReadinessCallback() OVERRIDE { | |
49 return base::Closure(); | |
50 } | |
51 | |
52 private: | |
53 DISALLOW_COPY_AND_ASSIGN(FakePowerManagerClient); | |
54 }; | |
55 | |
56 // Fake implementation of DiskMountManager. Does nothing but returns some | |
57 // disk information. | |
58 class FakeDiskMountManager : public chromeos::disks::DiskMountManager { | |
59 public: | |
60 FakeDiskMountManager() {} | |
61 virtual ~FakeDiskMountManager() { | |
62 STLDeleteValues(&disks_); | |
63 } | |
64 | |
65 // DiskMountManager overrides. | |
66 virtual void AddObserver(Observer* observer) OVERRIDE {} | |
67 virtual void RemoveObserver(Observer* observer) OVERRIDE {} | |
68 virtual const DiskMap& disks() const OVERRIDE { return disks_; } | |
69 | |
70 virtual const Disk* FindDiskBySourcePath( | |
71 const std::string& source_path) const OVERRIDE { | |
72 DiskMap::const_iterator iter = disks_.find(source_path); | |
73 if (iter == disks_.end()) | |
74 return NULL; | |
75 return iter->second; | |
76 }; | |
77 | |
78 virtual const MountPointMap& mount_points() const OVERRIDE { | |
79 return mount_points_; | |
80 } | |
81 virtual void RequestMountInfoRefresh() OVERRIDE {} | |
82 virtual void MountPath(const std::string& source_path, | |
83 const std::string& source_format, | |
84 const std::string& mount_label, | |
85 chromeos::MountType type) OVERRIDE {} | |
86 virtual void UnmountPath(const std::string& mount_path, | |
87 chromeos::UnmountOptions options, | |
88 const UnmountPathCallback& callback) OVERRIDE {} | |
89 virtual void FormatMountedDevice(const std::string& mount_path) OVERRIDE {} | |
90 virtual void UnmountDeviceRecursively( | |
91 const std::string& device_path, | |
92 const UnmountDeviceRecursivelyCallbackType& callback) OVERRIDE {} | |
93 | |
94 virtual bool AddDiskForTest(Disk* disk) OVERRIDE { | |
95 DCHECK(disk); | |
96 DCHECK(disks_.find(disk->device_path()) == disks_.end()); | |
97 disks_[disk->device_path()] = disk; | |
98 return true; | |
99 } | |
100 virtual bool AddMountPointForTest( | |
101 const MountPointInfo& mount_point) OVERRIDE { return false; } | |
102 | |
103 private: | |
104 DiskMap disks_; | |
105 MountPointMap mount_points_; | |
106 | |
107 DISALLOW_COPY_AND_ASSIGN(FakeDiskMountManager); | |
108 }; | |
109 | |
110 } // namespace | |
111 | |
112 class MountedDiskMonitorTest : public testing::Test { | |
113 protected: | |
114 MountedDiskMonitorTest() { | |
115 } | |
116 | |
117 virtual ~MountedDiskMonitorTest() { | |
118 } | |
119 | |
120 virtual void SetUp() OVERRIDE { | |
121 power_manager_client_.reset(new FakePowerManagerClient); | |
122 disk_mount_manager_.reset(new FakeDiskMountManager); | |
123 mounted_disk_monitor_.reset(new MountedDiskMonitor( | |
124 power_manager_client_.get(), | |
125 disk_mount_manager_.get())); | |
126 mounted_disk_monitor_->SetResumingTimeSpanForTesting( | |
127 base::TimeDelta::FromSeconds(0)); | |
128 } | |
129 | |
130 base::MessageLoop message_loop_; | |
131 scoped_ptr<FakePowerManagerClient> power_manager_client_; | |
132 scoped_ptr<FakeDiskMountManager> disk_mount_manager_; | |
133 scoped_ptr<MountedDiskMonitor> mounted_disk_monitor_; | |
134 }; | |
135 | |
hashimoto
2013/09/09 12:15:17
nit: Could you briefly describe what kind of scena
hidehiko
2013/09/09 12:44:11
Done.
| |
136 TEST_F(MountedDiskMonitorTest, ScenarioTest) { | |
hashimoto
2013/09/09 12:15:17
nit: ScenarioTest sounds too general. What if we w
hidehiko
2013/09/09 12:44:11
SuspendAndResume sounds better.
| |
137 chromeos::disks::DiskMountManager::Disk* disk1 = | |
138 new chromeos::disks::DiskMountManager::Disk( | |
hashimoto
2013/09/09 12:15:17
nit: Could you make it easier to understand what d
hidehiko
2013/09/09 12:44:11
Extracted into a utility function.
| |
139 "removable_device1", "", "", "", "", "", "", "", "", "", "uuid1", "", | |
140 chromeos::DEVICE_TYPE_USB, 0, false, false, false, false, false); | |
141 chromeos::disks::DiskMountManager::Disk* disk2 = | |
142 new chromeos::disks::DiskMountManager::Disk( | |
hashimoto
2013/09/09 12:15:17
ditto.
hidehiko
2013/09/09 12:44:11
Done.
| |
143 "removable_device2", "", "", "", "", "", "", "", "", "", "uuid2", "", | |
144 chromeos::DEVICE_TYPE_USB, 0, false, false, false, false, false); | |
145 | |
146 const chromeos::disks::DiskMountManager::MountPointInfo kMount1( | |
147 "removable_device1", "/tmp/removable_device1", | |
148 chromeos::MOUNT_TYPE_DEVICE, chromeos::disks::MOUNT_CONDITION_NONE); | |
149 const chromeos::disks::DiskMountManager::MountPointInfo kMount2( | |
150 "removable_device2", "/tmp/removable_device2", | |
151 chromeos::MOUNT_TYPE_DEVICE, chromeos::disks::MOUNT_CONDITION_NONE); | |
152 | |
153 // The ownership of Disks are taken by DiskMountManager. | |
154 disk_mount_manager_->AddDiskForTest(disk1); | |
155 disk_mount_manager_->AddDiskForTest(disk2); | |
156 | |
157 EXPECT_FALSE(mounted_disk_monitor_->DiskIsRemounting(*disk1)); | |
158 EXPECT_FALSE(mounted_disk_monitor_->DiskIsRemounting(*disk2)); | |
159 | |
160 // Mount |disk1|. | |
161 mounted_disk_monitor_->OnMountEvent( | |
162 chromeos::disks::DiskMountManager::MOUNTING, | |
163 chromeos::MOUNT_ERROR_NONE, | |
164 kMount1); | |
165 EXPECT_FALSE(mounted_disk_monitor_->DiskIsRemounting(*disk1)); | |
166 | |
167 // Unmount |disk1|. | |
168 mounted_disk_monitor_->OnMountEvent( | |
169 chromeos::disks::DiskMountManager::UNMOUNTING, | |
170 chromeos::MOUNT_ERROR_NONE, | |
171 kMount1); | |
172 EXPECT_FALSE(mounted_disk_monitor_->DiskIsRemounting(*disk1)); | |
hashimoto
2013/09/09 12:15:17
Could you briefly describe what do you want to tes
hidehiko
2013/09/09 12:44:11
Done.
| |
173 | |
174 // Again mount |disk1|. | |
175 mounted_disk_monitor_->OnMountEvent( | |
176 chromeos::disks::DiskMountManager::MOUNTING, | |
177 chromeos::MOUNT_ERROR_NONE, | |
178 kMount1); | |
179 EXPECT_FALSE(mounted_disk_monitor_->DiskIsRemounting(*disk1)); | |
180 | |
181 // Pseudo system suspend and resume. | |
182 mounted_disk_monitor_->SuspendImminent(); | |
183 mounted_disk_monitor_->SystemResumed(base::TimeDelta::FromSeconds(0)); | |
184 | |
185 // On system resume, we expect unmount and then mount immediately. | |
186 mounted_disk_monitor_->OnMountEvent( | |
187 chromeos::disks::DiskMountManager::UNMOUNTING, | |
188 chromeos::MOUNT_ERROR_NONE, | |
189 kMount1); | |
190 EXPECT_TRUE(mounted_disk_monitor_->DiskIsRemounting(*disk1)); | |
191 | |
192 mounted_disk_monitor_->OnMountEvent( | |
193 chromeos::disks::DiskMountManager::MOUNTING, | |
194 chromeos::MOUNT_ERROR_NONE, | |
195 kMount1); | |
196 EXPECT_TRUE(mounted_disk_monitor_->DiskIsRemounting(*disk1)); | |
197 | |
198 // New disk should not be "remounting." | |
199 EXPECT_FALSE(mounted_disk_monitor_->DiskIsRemounting(*disk2)); | |
200 mounted_disk_monitor_->OnMountEvent( | |
201 chromeos::disks::DiskMountManager::MOUNTING, | |
202 chromeos::MOUNT_ERROR_NONE, | |
203 kMount2); | |
204 EXPECT_FALSE(mounted_disk_monitor_->DiskIsRemounting(*disk2)); | |
205 | |
206 // After certain period, remounting state should be cleared. | |
207 base::RunLoop().RunUntilIdle(); // Emulate time passage. | |
208 EXPECT_FALSE(mounted_disk_monitor_->DiskIsRemounting(*disk1)); | |
209 EXPECT_FALSE(mounted_disk_monitor_->DiskIsRemounting(*disk2)); | |
210 } | |
211 | |
212 } // namespace file_manager | |
OLD | NEW |