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
));
63 MockDiskMountManager::~MockDiskMountManager() {
64 STLDeleteContainerPairSecondPointers(disks_
.begin(), disks_
.end());
68 void MockDiskMountManager::NotifyDeviceInsertEvents() {
69 scoped_ptr
<DiskMountManager::Disk
> disk1(new DiskMountManager::Disk(
70 std::string(kTestDevicePath
),
72 std::string(kTestSystemPath
),
73 std::string(kTestFilePath
),
75 std::string(kTestDriveLabel
),
76 std::string(kTestVendorId
),
77 std::string(kTestVendorName
),
78 std::string(kTestProductId
),
79 std::string(kTestProductName
),
80 std::string(kTestUuid
),
81 std::string(kTestSystemPathPrefix
),
85 false, // is_read_only
87 false, // on_boot_device
91 disks_
.insert(std::pair
<std::string
, DiskMountManager::Disk
*>(
92 std::string(kTestDevicePath
), disk1
.get()));
95 NotifyDeviceChanged(DEVICE_ADDED
, kTestSystemPath
);
98 NotifyDiskChanged(DISK_ADDED
, disk1
.get());
101 scoped_ptr
<DiskMountManager::Disk
> disk2(new DiskMountManager::Disk(
102 std::string(kTestDevicePath
),
103 std::string(kTestMountPath
),
104 std::string(kTestSystemPath
),
105 std::string(kTestFilePath
),
106 std::string(kTestDeviceLabel
),
107 std::string(kTestDriveLabel
),
108 std::string(kTestVendorId
),
109 std::string(kTestVendorName
),
110 std::string(kTestProductId
),
111 std::string(kTestProductName
),
112 std::string(kTestUuid
),
113 std::string(kTestSystemPathPrefix
),
117 false, // is_read_only
119 false, // on_boot_device
120 false)); // is_hidden
122 disks_
.insert(std::pair
<std::string
, DiskMountManager::Disk
*>(
123 std::string(kTestDevicePath
), disk2
.get()));
124 NotifyDiskChanged(DISK_CHANGED
, disk2
.get());
127 void MockDiskMountManager::NotifyDeviceRemoveEvents() {
128 scoped_ptr
<DiskMountManager::Disk
> disk(new DiskMountManager::Disk(
129 std::string(kTestDevicePath
),
130 std::string(kTestMountPath
),
131 std::string(kTestSystemPath
),
132 std::string(kTestFilePath
),
133 std::string(kTestDeviceLabel
),
134 std::string(kTestDriveLabel
),
135 std::string(kTestVendorId
),
136 std::string(kTestVendorName
),
137 std::string(kTestProductId
),
138 std::string(kTestProductName
),
139 std::string(kTestUuid
),
140 std::string(kTestSystemPathPrefix
),
144 false, // is_read_only
146 false, // on_boot_device
147 false)); // is_hidden
149 disks_
.insert(std::pair
<std::string
, DiskMountManager::Disk
*>(
150 std::string(kTestDevicePath
), disk
.get()));
151 NotifyDiskChanged(DISK_REMOVED
, disk
.get());
154 void MockDiskMountManager::SetupDefaultReplies() {
155 EXPECT_CALL(*this, AddObserver(_
))
157 EXPECT_CALL(*this, RemoveObserver(_
))
159 EXPECT_CALL(*this, disks())
160 .WillRepeatedly(ReturnRef(disks_
));
161 EXPECT_CALL(*this, mount_points())
162 .WillRepeatedly(ReturnRef(mount_points_
));
163 EXPECT_CALL(*this, FindDiskBySourcePath(_
))
165 EXPECT_CALL(*this, RequestMountInfoRefresh())
167 EXPECT_CALL(*this, MountPath(_
, _
, _
, _
))
169 EXPECT_CALL(*this, UnmountPath(_
, _
, _
))
171 EXPECT_CALL(*this, FormatMountedDevice(_
))
173 EXPECT_CALL(*this, UnmountDeviceRecursively(_
, _
))
177 void MockDiskMountManager::CreateDiskEntryForMountDevice(
178 const DiskMountManager::MountPointInfo
& mount_info
,
179 const std::string
& device_id
,
180 const std::string
& device_label
,
181 const std::string
& vendor_name
,
182 const std::string
& product_name
,
183 DeviceType device_type
,
184 uint64 total_size_in_bytes
,
187 bool on_boot_device
) {
188 Disk
* disk
= new DiskMountManager::Disk(mount_info
.source_path
,
189 mount_info
.mount_path
,
190 std::string(), // system_path
191 mount_info
.source_path
,
193 std::string(), // drive_label
194 std::string(), // vendor_id
196 std::string(), // product_id
198 device_id
, // fs_uuid
199 std::string(), // system_path_prefix
203 false, // is_read_only
207 DiskMountManager::DiskMap::iterator it
= disks_
.find(mount_info
.source_path
);
208 if (it
== disks_
.end()) {
209 disks_
.insert(std::make_pair(std::string(mount_info
.source_path
), disk
));
216 void MockDiskMountManager::RemoveDiskEntryForMountDevice(
217 const DiskMountManager::MountPointInfo
& mount_info
) {
218 DiskMountManager::DiskMap::iterator it
= disks_
.find(mount_info
.source_path
);
219 if (it
!= disks_
.end()) {
225 const DiskMountManager::MountPointMap
&
226 MockDiskMountManager::mountPointsInternal() const {
227 return mount_points_
;
230 const DiskMountManager::Disk
*
231 MockDiskMountManager::FindDiskBySourcePathInternal(
232 const std::string
& source_path
) const {
233 DiskMap::const_iterator disk_it
= disks_
.find(source_path
);
234 return disk_it
== disks_
.end() ? NULL
: disk_it
->second
;
237 void MockDiskMountManager::NotifyDiskChanged(
239 const DiskMountManager::Disk
* disk
) {
240 FOR_EACH_OBSERVER(Observer
, observers_
, OnDiskEvent(event
, disk
));
243 void MockDiskMountManager::NotifyDeviceChanged(DeviceEvent event
,
244 const std::string
& path
) {
245 FOR_EACH_OBSERVER(Observer
, observers_
, OnDeviceEvent(event
, path
));
249 } // namespace chromeos