1 // Copyright (c) 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.
5 #include "chrome/browser/chromeos/imageburner/burn_device_handler.h"
11 #include "base/bind.h"
12 #include "base/logging.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/observer_list.h"
15 #include "base/stl_util.h"
16 #include "chromeos/dbus/cros_disks_client.h"
17 #include "chromeos/disks/disk_mount_manager.h"
18 #include "testing/gtest/include/gtest/gtest.h"
21 namespace imageburner {
25 const bool kIsParent = true;
26 const bool kIsBootDevice = true;
27 const bool kHasMedia = true;
29 class FakeDiskMountManager : public disks::DiskMountManager {
31 FakeDiskMountManager() {}
33 virtual ~FakeDiskMountManager() {
34 STLDeleteValues(&disks_);
37 // Emulates to add new disk physically (e.g., connecting a
38 // new USB flash to a Chrome OS).
39 void EmulateAddDisk(scoped_ptr<Disk> in_disk) {
40 DCHECK(in_disk.get());
41 // Keep the reference for the callback, before passing the ownership to
42 // InsertDisk. It should be safe, because it won't be deleted in
44 Disk* disk = in_disk.get();
45 bool new_disk = InsertDisk(disk->device_path(), in_disk.Pass());
48 OnDiskEvent(new_disk ? DISK_ADDED : DISK_CHANGED, disk));
51 // Emulates to remove a disk phyically (e.g., removing a USB flash from
53 void EmulateRemoveDisk(const std::string& source_path) {
54 scoped_ptr<Disk> disk(RemoveDisk(source_path));
57 Observer, observers_, OnDiskEvent(DISK_REMOVED, disk.get()));
61 // DiskMountManager overrides.
62 virtual void AddObserver(Observer* observer) OVERRIDE {
63 observers_.AddObserver(observer);
66 virtual void RemoveObserver(Observer* observer) OVERRIDE {
67 observers_.RemoveObserver(observer);
70 virtual const DiskMap& disks() const OVERRIDE {
74 // Following methods are not implemented.
75 virtual const Disk* FindDiskBySourcePath(
76 const std::string& source_path) const OVERRIDE {
79 virtual const MountPointMap& mount_points() const OVERRIDE {
80 // Note: mount_points_ will always be empty, now.
83 virtual void EnsureMountInfoRefreshed(
84 const EnsureMountInfoRefreshedCallback& callback) OVERRIDE {}
85 virtual void MountPath(const std::string& source_path,
86 const std::string& source_format,
87 const std::string& mount_label,
88 MountType type) OVERRIDE {}
89 virtual void UnmountPath(const std::string& mount_path,
90 UnmountOptions options,
91 const UnmountPathCallback& callback) OVERRIDE {}
92 virtual void FormatMountedDevice(const std::string& mount_path) OVERRIDE {}
93 virtual void UnmountDeviceRecursively(
94 const std::string& device_path,
95 const UnmountDeviceRecursivelyCallbackType& callback) OVERRIDE {}
96 virtual bool AddDiskForTest(Disk* disk) OVERRIDE { return false; }
97 virtual bool AddMountPointForTest(
98 const MountPointInfo& mount_point) OVERRIDE {
103 bool InsertDisk(const std::string& path, scoped_ptr<Disk> disk) {
104 std::pair<DiskMap::iterator, bool> insert_result =
105 disks_.insert(std::pair<std::string, Disk*>(path, NULL));
106 if (!insert_result.second) {
107 // There is already an entry. Delete it before replacing.
108 delete insert_result.first->second;
110 insert_result.first->second = disk.release(); // Moves ownership.
111 return insert_result.second;
114 scoped_ptr<Disk> RemoveDisk(const std::string& path) {
115 DiskMap::iterator iter = disks_.find(path);
116 if (iter == disks_.end()) {
118 return scoped_ptr<Disk>();
120 scoped_ptr<Disk> result(iter->second);
122 return result.Pass();
125 ObserverList<Observer> observers_;
127 MountPointMap mount_points_;
129 DISALLOW_COPY_AND_ASSIGN(FakeDiskMountManager);
132 void CopyDevicePathCallback(
133 std::string* out_path, const disks::DiskMountManager::Disk& disk) {
134 *out_path = disk.device_path();
139 class BurnDeviceHandlerTest : public testing::Test {
141 virtual void SetUp() OVERRIDE {
142 disk_mount_manager_.reset(new FakeDiskMountManager);
145 virtual void TearDown() OVERRIDE {
146 disk_mount_manager_.reset();
149 static scoped_ptr<disks::DiskMountManager::Disk> CreateMockDisk(
150 const std::string& device_path,
154 DeviceType device_type) {
155 return scoped_ptr<disks::DiskMountManager::Disk>(
156 new disks::DiskMountManager::Disk(
168 "", // system path prefix
170 0, // total size in bytes
172 false, // is read only
175 true, // on_removable_device
176 false)); // is hidden
179 scoped_ptr<FakeDiskMountManager> disk_mount_manager_;
182 TEST_F(BurnDeviceHandlerTest, GetBurnableDevices) {
183 // The devices which should be retrieved as burnable.
184 disk_mount_manager_->EmulateAddDisk(
185 CreateMockDisk("/dev/burnable_usb",
186 kIsParent, !kIsBootDevice, kHasMedia, DEVICE_TYPE_USB));
187 disk_mount_manager_->EmulateAddDisk(
188 CreateMockDisk("/dev/burnable_sd",
189 kIsParent, !kIsBootDevice, kHasMedia, DEVICE_TYPE_SD));
191 // If the device type is neither USB nor SD, it shouldn't be burnable.
192 disk_mount_manager_->EmulateAddDisk(
194 "/dev/non_burnable_unknown",
195 kIsParent, !kIsBootDevice, kHasMedia, DEVICE_TYPE_UNKNOWN));
196 disk_mount_manager_->EmulateAddDisk(
197 CreateMockDisk("/dev/non_burnable_dvd",
198 kIsParent, !kIsBootDevice, kHasMedia, DEVICE_TYPE_DVD));
200 // If not parent, it shouldn't be burnable.
201 disk_mount_manager_->EmulateAddDisk(
202 CreateMockDisk("/dev/non_burnable_not_parent",
203 !kIsParent, !kIsBootDevice, kHasMedia, DEVICE_TYPE_USB));
205 // If on_boot_device, it shouldn't be burnable.
206 disk_mount_manager_->EmulateAddDisk(
207 CreateMockDisk("/dev/non_burnable_boot_device",
208 kIsParent, kIsBootDevice, kHasMedia, DEVICE_TYPE_USB));
210 // If no media, it shouldn't be burnable.
211 disk_mount_manager_->EmulateAddDisk(
212 CreateMockDisk("/dev/non_burnable_no_media",
213 kIsParent, !kIsBootDevice, !kHasMedia, DEVICE_TYPE_USB));
215 BurnDeviceHandler handler(disk_mount_manager_.get());
217 const std::vector<disks::DiskMountManager::Disk>& burnable_devices =
218 handler.GetBurnableDevices();
219 ASSERT_EQ(2u, burnable_devices.size());
220 bool burnable_usb_found = false;
221 bool burnable_sd_found = false;
222 for (size_t i = 0; i < burnable_devices.size(); ++i) {
223 const std::string& device_path = burnable_devices[i].device_path();
224 burnable_usb_found |= (device_path == "/dev/burnable_usb");
225 burnable_sd_found |= (device_path == "/dev/burnable_sd");
228 EXPECT_TRUE(burnable_usb_found);
229 EXPECT_TRUE(burnable_sd_found);
232 TEST_F(BurnDeviceHandlerTest, Callback) {
233 std::string added_device;
234 std::string removed_device;
236 BurnDeviceHandler handler(disk_mount_manager_.get());
237 handler.SetCallbacks(
238 base::Bind(CopyDevicePathCallback, &added_device),
239 base::Bind(CopyDevicePathCallback, &removed_device));
241 // Emulate to connect a burnable device.
242 // |add_callback| should be invoked.
243 disk_mount_manager_->EmulateAddDisk(
244 CreateMockDisk("/dev/burnable",
245 kIsParent, !kIsBootDevice, kHasMedia, DEVICE_TYPE_USB));
246 EXPECT_EQ("/dev/burnable", added_device);
247 EXPECT_TRUE(removed_device.empty());
249 // Emulate to change the currently connected burnable device.
250 // Neither |add_callback| nor |remove_callback| should be called.
251 added_device.clear();
252 removed_device.clear();
253 disk_mount_manager_->EmulateAddDisk(
254 CreateMockDisk("/dev/burnable",
255 kIsParent, !kIsBootDevice, kHasMedia, DEVICE_TYPE_USB));
256 EXPECT_TRUE(added_device.empty());
257 EXPECT_TRUE(removed_device.empty());
259 // Emulate to disconnect the burnable device.
260 // |remove_callback| should be called.
261 added_device.clear();
262 removed_device.clear();
263 disk_mount_manager_->EmulateRemoveDisk("/dev/burnable");
264 EXPECT_TRUE(added_device.empty());
265 EXPECT_EQ("/dev/burnable", removed_device);
267 // Emulate to connect and unconnect an unburnable device.
268 // For each case, neither |add_callback| nor |remove_callback| should be
270 added_device.clear();
271 removed_device.clear();
272 disk_mount_manager_->EmulateAddDisk(
273 CreateMockDisk("/dev/unburnable",
274 !kIsParent, !kIsBootDevice, kHasMedia, DEVICE_TYPE_USB));
275 EXPECT_TRUE(added_device.empty());
276 EXPECT_TRUE(removed_device.empty());
278 added_device.clear();
279 removed_device.clear();
280 disk_mount_manager_->EmulateRemoveDisk("/dev/unburnable");
281 EXPECT_TRUE(added_device.empty());
282 EXPECT_TRUE(removed_device.empty());
285 } // namespace imageburner
286 } // namespace chromeos