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(_
, _
)).Times(AnyNumber());
172 EXPECT_CALL(*this, MountPath(_
, _
, _
, _
))
174 EXPECT_CALL(*this, UnmountPath(_
, _
, _
))
176 EXPECT_CALL(*this, FormatMountedDevice(_
))
178 EXPECT_CALL(*this, UnmountDeviceRecursively(_
, _
))
182 void MockDiskMountManager::CreateDiskEntryForMountDevice(
183 const DiskMountManager::MountPointInfo
& mount_info
,
184 const std::string
& device_id
,
185 const std::string
& device_label
,
186 const std::string
& vendor_name
,
187 const std::string
& product_name
,
188 DeviceType device_type
,
189 uint64 total_size_in_bytes
,
193 bool on_removable_device
) {
194 Disk
* disk
= new DiskMountManager::Disk(mount_info
.source_path
,
195 mount_info
.mount_path
,
196 std::string(), // system_path
197 mount_info
.source_path
,
199 std::string(), // drive_label
200 std::string(), // vendor_id
202 std::string(), // product_id
204 device_id
, // fs_uuid
205 std::string(), // system_path_prefix
209 false, // is_read_only
214 DiskMountManager::DiskMap::iterator it
= disks_
.find(mount_info
.source_path
);
215 if (it
== disks_
.end()) {
216 disks_
.insert(std::make_pair(std::string(mount_info
.source_path
), disk
));
223 void MockDiskMountManager::RemoveDiskEntryForMountDevice(
224 const DiskMountManager::MountPointInfo
& mount_info
) {
225 DiskMountManager::DiskMap::iterator it
= disks_
.find(mount_info
.source_path
);
226 if (it
!= disks_
.end()) {
232 const DiskMountManager::MountPointMap
&
233 MockDiskMountManager::mountPointsInternal() const {
234 return mount_points_
;
237 const DiskMountManager::Disk
*
238 MockDiskMountManager::FindDiskBySourcePathInternal(
239 const std::string
& source_path
) const {
240 DiskMap::const_iterator disk_it
= disks_
.find(source_path
);
241 return disk_it
== disks_
.end() ? NULL
: disk_it
->second
;
244 void MockDiskMountManager::EnsureMountInfoRefreshedInternal(
245 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