1 // Copyright 2014 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 // TestVolumeMountWatcherWin implementation.
7 #include "components/storage_monitor/test_volume_mount_watcher_win.h"
10 #include "base/files/file_path.h"
11 #include "base/files/scoped_temp_dir.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "components/storage_monitor/storage_info.h"
15 namespace storage_monitor
{
19 base::FilePath
GetTempRoot() {
20 base::ScopedTempDir temp_dir
;
21 temp_dir
.CreateUniqueTempDir();
22 base::FilePath temp_root
= temp_dir
.path();
23 while (temp_root
.DirName() != temp_root
)
24 temp_root
= temp_root
.DirName();
28 std::vector
<base::FilePath
> FakeGetSingleAttachedDevice() {
29 std::vector
<base::FilePath
> result
;
30 result
.push_back(VolumeMountWatcherWin::DriveNumberToFilePath(2)); // C
32 // Make sure we are adding the drive on which ScopedTempDir will make
34 base::FilePath temp_root
= GetTempRoot();
35 if (temp_root
!= VolumeMountWatcherWin::DriveNumberToFilePath(2))
36 result
.push_back(temp_root
);
41 std::vector
<base::FilePath
> FakeGetAttachedDevices() {
42 std::vector
<base::FilePath
> result
;
43 result
.push_back(VolumeMountWatcherWin::DriveNumberToFilePath(0)); // A
44 result
.push_back(VolumeMountWatcherWin::DriveNumberToFilePath(1)); // B
45 result
.push_back(VolumeMountWatcherWin::DriveNumberToFilePath(2)); // C
46 result
.push_back(VolumeMountWatcherWin::DriveNumberToFilePath(3)); // D
47 result
.push_back(VolumeMountWatcherWin::DriveNumberToFilePath(5)); // F
48 result
.push_back(VolumeMountWatcherWin::DriveNumberToFilePath(7)); // H
49 result
.push_back(VolumeMountWatcherWin::DriveNumberToFilePath(13)); // N
50 result
.push_back(VolumeMountWatcherWin::DriveNumberToFilePath(25)); // Z
54 // Gets the details of the mass storage device specified by the |device_path|.
55 // |device_path| inputs of 'A:\' - 'Z:\' are valid. 'N:\' is not removable.
56 // 'C:\' is not removable (so that auto-added paths are correctly handled).
57 bool GetMassStorageDeviceDetails(const base::FilePath
& device_path
,
61 // Truncate to root path.
62 base::FilePath
path(device_path
);
63 if (device_path
.value().length() > 3)
64 path
= base::FilePath(device_path
.value().substr(0, 3));
65 base::FilePath::CharType drive_letter
= path
.value()[0];
66 if (drive_letter
< L
'A' || drive_letter
> L
'Z')
69 StorageInfo::Type type
= StorageInfo::FIXED_MASS_STORAGE
;
70 if (path
.value() != base::ASCIIToUTF16("N:\\") &&
71 path
.value() != base::ASCIIToUTF16("C:\\") &&
72 path
.value() != GetTempRoot().value()) {
73 type
= StorageInfo::REMOVABLE_MASS_STORAGE_WITH_DCIM
;
75 std::string unique_id
=
76 "\\\\?\\Volume{00000000-0000-0000-0000-000000000000}\\";
77 unique_id
[11] = static_cast<char>(drive_letter
);
78 std::string device_id
= StorageInfo::MakeDeviceId(type
, unique_id
);
79 base::string16 storage_label
= path
.Append(L
" Drive").LossyDisplayName();
80 *info
= StorageInfo(device_id
, path
.value(), storage_label
, base::string16(),
81 base::string16(), 1000000);
88 // TestVolumeMountWatcherWin ---------------------------------------------------
90 TestVolumeMountWatcherWin::TestVolumeMountWatcherWin()
91 : attached_devices_fake_(false) {}
93 TestVolumeMountWatcherWin::~TestVolumeMountWatcherWin() {
96 void TestVolumeMountWatcherWin::AddDeviceForTesting(
97 const base::FilePath
& device_path
,
98 const std::string
& device_id
,
99 const base::string16
& storage_label
,
100 uint64 total_size_in_bytes
) {
101 StorageInfo
info(device_id
, device_path
.value(), storage_label
,
102 base::string16(), base::string16(), total_size_in_bytes
);
103 HandleDeviceAttachEventOnUIThread(device_path
, info
);
106 void TestVolumeMountWatcherWin::SetAttachedDevicesFake() {
107 attached_devices_fake_
= true;
110 void TestVolumeMountWatcherWin::FlushWorkerPoolForTesting() {
111 device_info_worker_pool_
->FlushForTesting();
114 void TestVolumeMountWatcherWin::DeviceCheckComplete(
115 const base::FilePath
& device_path
) {
116 devices_checked_
.push_back(device_path
);
117 if (device_check_complete_event_
.get())
118 device_check_complete_event_
->Wait();
119 VolumeMountWatcherWin::DeviceCheckComplete(device_path
);
122 void TestVolumeMountWatcherWin::BlockDeviceCheckForTesting() {
123 device_check_complete_event_
.reset(new base::WaitableEvent(false, false));
124 devices_checked_
.clear();
127 void TestVolumeMountWatcherWin::ReleaseDeviceCheck() {
128 device_check_complete_event_
->Signal();
132 bool TestVolumeMountWatcherWin::GetDeviceRemovable(
133 const base::FilePath
& device_path
,
136 bool success
= GetMassStorageDeviceDetails(device_path
, &info
);
137 *removable
= StorageInfo::IsRemovableDevice(info
.device_id());
141 VolumeMountWatcherWin::GetDeviceDetailsCallbackType
142 TestVolumeMountWatcherWin::GetDeviceDetailsCallback() const {
143 return base::Bind(&GetMassStorageDeviceDetails
);
146 VolumeMountWatcherWin::GetAttachedDevicesCallbackType
147 TestVolumeMountWatcherWin::GetAttachedDevicesCallback() const {
148 if (attached_devices_fake_
)
149 return base::Bind(&FakeGetAttachedDevices
);
150 return base::Bind(&FakeGetSingleAttachedDevice
);
153 void TestVolumeMountWatcherWin::ShutdownWorkerPool() {
154 device_info_worker_pool_
->Shutdown();
157 } // namespace storage_monitor