1 // Copyright (c) 2012 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 "chromeos/disks/mock_disk_mount_manager.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/stl_util.h"
11 #include "base/strings/string_util.h"
14 using testing::AnyNumber
;
15 using testing::Invoke
;
16 using testing::ReturnRef
;
23 const char* kTestSystemPath
= "/this/system/path";
24 const char* kTestSystemPathPrefix
= "/this/system";
25 const char* kTestDevicePath
= "/this/device/path";
26 const char* kTestMountPath
= "/media/foofoo";
27 const char* kTestFilePath
= "/this/file/path";
28 const char* kTestDeviceLabel
= "A label";
29 const char* kTestDriveLabel
= "Another label";
30 const char* kTestVendorId
= "0123";
31 const char* kTestVendorName
= "A vendor";
32 const char* kTestProductId
= "abcd";
33 const char* kTestProductName
= "A product";
34 const char* kTestUuid
= "FFFF-FFFF";
38 void MockDiskMountManager::AddObserverInternal(
39 DiskMountManager::Observer
* observer
) {
40 observers_
.AddObserver(observer
);
43 void MockDiskMountManager::RemoveObserverInternal(
44 DiskMountManager::Observer
* observer
) {
45 observers_
.RemoveObserver(observer
);
48 MockDiskMountManager::MockDiskMountManager() {
49 ON_CALL(*this, AddObserver(_
))
50 .WillByDefault(Invoke(this, &MockDiskMountManager::AddObserverInternal
));
51 ON_CALL(*this, RemoveObserver(_
))
52 .WillByDefault(Invoke(this,
53 &MockDiskMountManager::RemoveObserverInternal
));
54 ON_CALL(*this, disks())
55 .WillByDefault(Invoke(this, &MockDiskMountManager::disksInternal
));
56 ON_CALL(*this, mount_points())
57 .WillByDefault(Invoke(this, &MockDiskMountManager::mountPointsInternal
));
58 ON_CALL(*this, FindDiskBySourcePath(_
))
59 .WillByDefault(Invoke(
60 this, &MockDiskMountManager::FindDiskBySourcePathInternal
));
61 ON_CALL(*this, EnsureMountInfoRefreshed(_
))
62 .WillByDefault(Invoke(
63 this, &MockDiskMountManager::EnsureMountInfoRefreshedInternal
));
66 MockDiskMountManager::~MockDiskMountManager() {
67 STLDeleteContainerPairSecondPointers(disks_
.begin(), disks_
.end());
71 void MockDiskMountManager::NotifyDeviceInsertEvents() {
72 scoped_ptr
<DiskMountManager::Disk
> disk1(new DiskMountManager::Disk(
73 std::string(kTestDevicePath
),
75 std::string(kTestSystemPath
),
76 std::string(kTestFilePath
),
78 std::string(kTestDriveLabel
),
79 std::string(kTestVendorId
),
80 std::string(kTestVendorName
),
81 std::string(kTestProductId
),
82 std::string(kTestProductName
),
83 std::string(kTestUuid
),
84 std::string(kTestSystemPathPrefix
),
88 false, // is_read_only
90 false, // on_boot_device
91 true, // on_removable_device
95 disks_
.insert(std::pair
<std::string
, DiskMountManager::Disk
*>(
96 std::string(kTestDevicePath
), disk1
.get()));
99 NotifyDeviceChanged(DEVICE_ADDED
, kTestSystemPath
);
102 NotifyDiskChanged(DISK_ADDED
, disk1
.get());
105 scoped_ptr
<DiskMountManager::Disk
> disk2(new DiskMountManager::Disk(
106 std::string(kTestDevicePath
),
107 std::string(kTestMountPath
),
108 std::string(kTestSystemPath
),
109 std::string(kTestFilePath
),
110 std::string(kTestDeviceLabel
),
111 std::string(kTestDriveLabel
),
112 std::string(kTestVendorId
),
113 std::string(kTestVendorName
),
114 std::string(kTestProductId
),
115 std::string(kTestProductName
),
116 std::string(kTestUuid
),
117 std::string(kTestSystemPathPrefix
),
121 false, // is_read_only
123 false, // on_boot_device
124 true, // on_removable_device
125 false)); // is_hidden
127 disks_
.insert(std::pair
<std::string
, DiskMountManager::Disk
*>(
128 std::string(kTestDevicePath
), disk2
.get()));
129 NotifyDiskChanged(DISK_CHANGED
, disk2
.get());
132 void MockDiskMountManager::NotifyDeviceRemoveEvents() {
133 scoped_ptr
<DiskMountManager::Disk
> disk(new DiskMountManager::Disk(
134 std::string(kTestDevicePath
),
135 std::string(kTestMountPath
),
136 std::string(kTestSystemPath
),
137 std::string(kTestFilePath
),
138 std::string(kTestDeviceLabel
),
139 std::string(kTestDriveLabel
),
140 std::string(kTestVendorId
),
141 std::string(kTestVendorName
),
142 std::string(kTestProductId
),
143 std::string(kTestProductName
),
144 std::string(kTestUuid
),
145 std::string(kTestSystemPathPrefix
),
149 false, // is_read_only
151 false, // on_boot_device
152 true, // on_removable_device
153 false)); // is_hidden
155 disks_
.insert(std::pair
<std::string
, DiskMountManager::Disk
*>(
156 std::string(kTestDevicePath
), disk
.get()));
157 NotifyDiskChanged(DISK_REMOVED
, disk
.get());
160 void MockDiskMountManager::SetupDefaultReplies() {
161 EXPECT_CALL(*this, AddObserver(_
))
163 EXPECT_CALL(*this, RemoveObserver(_
))
165 EXPECT_CALL(*this, disks())
166 .WillRepeatedly(ReturnRef(disks_
));
167 EXPECT_CALL(*this, mount_points())
168 .WillRepeatedly(ReturnRef(mount_points_
));
169 EXPECT_CALL(*this, FindDiskBySourcePath(_
))
171 EXPECT_CALL(*this, EnsureMountInfoRefreshed(_
))
173 EXPECT_CALL(*this, MountPath(_
, _
, _
, _
))
175 EXPECT_CALL(*this, UnmountPath(_
, _
, _
))
177 EXPECT_CALL(*this, FormatMountedDevice(_
))
179 EXPECT_CALL(*this, UnmountDeviceRecursively(_
, _
))
183 void MockDiskMountManager::CreateDiskEntryForMountDevice(
184 const DiskMountManager::MountPointInfo
& mount_info
,
185 const std::string
& device_id
,
186 const std::string
& device_label
,
187 const std::string
& vendor_name
,
188 const std::string
& product_name
,
189 DeviceType device_type
,
190 uint64 total_size_in_bytes
,
194 bool on_removable_device
) {
195 Disk
* disk
= new DiskMountManager::Disk(mount_info
.source_path
,
196 mount_info
.mount_path
,
197 std::string(), // system_path
198 mount_info
.source_path
,
200 std::string(), // drive_label
201 std::string(), // vendor_id
203 std::string(), // product_id
205 device_id
, // fs_uuid
206 std::string(), // system_path_prefix
210 false, // is_read_only
215 DiskMountManager::DiskMap::iterator it
= disks_
.find(mount_info
.source_path
);
216 if (it
== disks_
.end()) {
217 disks_
.insert(std::make_pair(std::string(mount_info
.source_path
), disk
));
224 void MockDiskMountManager::RemoveDiskEntryForMountDevice(
225 const DiskMountManager::MountPointInfo
& mount_info
) {
226 DiskMountManager::DiskMap::iterator it
= disks_
.find(mount_info
.source_path
);
227 if (it
!= disks_
.end()) {
233 const DiskMountManager::MountPointMap
&
234 MockDiskMountManager::mountPointsInternal() const {
235 return mount_points_
;
238 const DiskMountManager::Disk
*
239 MockDiskMountManager::FindDiskBySourcePathInternal(
240 const std::string
& source_path
) const {
241 DiskMap::const_iterator disk_it
= disks_
.find(source_path
);
242 return disk_it
== disks_
.end() ? NULL
: disk_it
->second
;
245 void MockDiskMountManager::EnsureMountInfoRefreshedInternal(
246 const EnsureMountInfoRefreshedCallback
& callback
) {
250 void MockDiskMountManager::NotifyDiskChanged(
252 const DiskMountManager::Disk
* disk
) {
253 FOR_EACH_OBSERVER(Observer
, observers_
, OnDiskEvent(event
, disk
));
256 void MockDiskMountManager::NotifyDeviceChanged(DeviceEvent event
,
257 const std::string
& path
) {
258 FOR_EACH_OBSERVER(Observer
, observers_
, OnDeviceEvent(event
, path
));
262 } // namespace chromeos