Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / chromeos / disks / mock_disk_mount_manager.cc
blobc915b002ef9298729972f87e97d9570963550d5f
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"
7 #include <utility>
9 #include "base/message_loop/message_loop.h"
10 #include "base/stl_util.h"
11 #include "base/strings/string_util.h"
13 using testing::_;
14 using testing::AnyNumber;
15 using testing::Invoke;
16 using testing::ReturnRef;
18 namespace chromeos {
19 namespace disks {
21 namespace {
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";
36 } // namespace
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());
68 disks_.clear();
71 void MockDiskMountManager::NotifyDeviceInsertEvents() {
72 scoped_ptr<DiskMountManager::Disk> disk1(new DiskMountManager::Disk(
73 std::string(kTestDevicePath),
74 std::string(),
75 std::string(kTestSystemPath),
76 std::string(kTestFilePath),
77 std::string(),
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),
85 DEVICE_TYPE_USB,
86 4294967295U,
87 false, // is_parent
88 false, // is_read_only
89 true, // has_media
90 false, // on_boot_device
91 true, // on_removable_device
92 false)); // is_hidden
94 disks_.clear();
95 disks_.insert(std::pair<std::string, DiskMountManager::Disk*>(
96 std::string(kTestDevicePath), disk1.get()));
98 // Device Added
99 NotifyDeviceChanged(DEVICE_ADDED, kTestSystemPath);
101 // Disk Added
102 NotifyDiskChanged(DISK_ADDED, disk1.get());
104 // Disk Changed
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),
118 DEVICE_TYPE_MOBILE,
119 1073741824,
120 false, // is_parent
121 false, // is_read_only
122 true, // has_media
123 false, // on_boot_device
124 true, // on_removable_device
125 false)); // is_hidden
126 disks_.clear();
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),
146 DEVICE_TYPE_SD,
147 1073741824,
148 false, // is_parent
149 false, // is_read_only
150 true, // has_media
151 false, // on_boot_device
152 true, // on_removable_device
153 false)); // is_hidden
154 disks_.clear();
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(_))
162 .Times(AnyNumber());
163 EXPECT_CALL(*this, RemoveObserver(_))
164 .Times(AnyNumber());
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(_))
170 .Times(AnyNumber());
171 EXPECT_CALL(*this, EnsureMountInfoRefreshed(_, _)).Times(AnyNumber());
172 EXPECT_CALL(*this, MountPath(_, _, _, _))
173 .Times(AnyNumber());
174 EXPECT_CALL(*this, UnmountPath(_, _, _))
175 .Times(AnyNumber());
176 EXPECT_CALL(*this, FormatMountedDevice(_))
177 .Times(AnyNumber());
178 EXPECT_CALL(*this, UnmountDeviceRecursively(_, _))
179 .Times(AnyNumber());
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,
190 bool is_parent,
191 bool has_media,
192 bool on_boot_device,
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,
198 device_label,
199 std::string(), // drive_label
200 std::string(), // vendor_id
201 vendor_name,
202 std::string(), // product_id
203 product_name,
204 device_id, // fs_uuid
205 std::string(), // system_path_prefix
206 device_type,
207 total_size_in_bytes,
208 is_parent,
209 false, // is_read_only
210 has_media,
211 on_boot_device,
212 on_removable_device,
213 false); // is_hidden
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));
217 } else {
218 delete it->second;
219 it->second = 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()) {
227 delete it->second;
228 disks_.erase(it);
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,
246 bool force) {
247 callback.Run(true);
250 void MockDiskMountManager::NotifyDiskChanged(
251 DiskEvent event,
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));
261 } // namespace disks
262 } // namespace chromeos