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"
14 #include "content/public/browser/browser_thread.h"
16 namespace storage_monitor
{
20 base::FilePath
GetTempRoot() {
21 base::ScopedTempDir temp_dir
;
22 temp_dir
.CreateUniqueTempDir();
23 base::FilePath temp_root
= temp_dir
.path();
24 while (temp_root
.DirName() != temp_root
)
25 temp_root
= temp_root
.DirName();
29 std::vector
<base::FilePath
> FakeGetSingleAttachedDevice() {
30 std::vector
<base::FilePath
> result
;
31 result
.push_back(VolumeMountWatcherWin::DriveNumberToFilePath(2)); // C
33 // Make sure we are adding the drive on which ScopedTempDir will make
35 base::FilePath temp_root
= GetTempRoot();
36 if (temp_root
!= VolumeMountWatcherWin::DriveNumberToFilePath(2))
37 result
.push_back(temp_root
);
42 std::vector
<base::FilePath
> FakeGetAttachedDevices() {
43 std::vector
<base::FilePath
> result
;
44 result
.push_back(VolumeMountWatcherWin::DriveNumberToFilePath(0)); // A
45 result
.push_back(VolumeMountWatcherWin::DriveNumberToFilePath(1)); // B
46 result
.push_back(VolumeMountWatcherWin::DriveNumberToFilePath(2)); // C
47 result
.push_back(VolumeMountWatcherWin::DriveNumberToFilePath(3)); // D
48 result
.push_back(VolumeMountWatcherWin::DriveNumberToFilePath(5)); // F
49 result
.push_back(VolumeMountWatcherWin::DriveNumberToFilePath(7)); // H
50 result
.push_back(VolumeMountWatcherWin::DriveNumberToFilePath(13)); // N
51 result
.push_back(VolumeMountWatcherWin::DriveNumberToFilePath(25)); // Z
55 // Gets the details of the mass storage device specified by the |device_path|.
56 // |device_path| inputs of 'A:\' - 'Z:\' are valid. 'N:\' is not removable.
57 // 'C:\' is not removable (so that auto-added paths are correctly handled).
58 bool GetMassStorageDeviceDetails(const base::FilePath
& device_path
,
62 // Truncate to root path.
63 base::FilePath
path(device_path
);
64 if (device_path
.value().length() > 3)
65 path
= base::FilePath(device_path
.value().substr(0, 3));
66 base::FilePath::CharType drive_letter
= path
.value()[0];
67 if (drive_letter
< L
'A' || drive_letter
> L
'Z')
70 StorageInfo::Type type
= StorageInfo::FIXED_MASS_STORAGE
;
71 if (path
.value() != base::ASCIIToUTF16("N:\\") &&
72 path
.value() != base::ASCIIToUTF16("C:\\") &&
73 path
.value() != GetTempRoot().value()) {
74 type
= StorageInfo::REMOVABLE_MASS_STORAGE_WITH_DCIM
;
76 std::string unique_id
=
77 "\\\\?\\Volume{00000000-0000-0000-0000-000000000000}\\";
78 unique_id
[11] = static_cast<char>(drive_letter
);
79 std::string device_id
= StorageInfo::MakeDeviceId(type
, unique_id
);
80 base::string16 storage_label
= path
.Append(L
" Drive").LossyDisplayName();
81 *info
= StorageInfo(device_id
, path
.value(), storage_label
, base::string16(),
82 base::string16(), 1000000);
89 // TestVolumeMountWatcherWin ---------------------------------------------------
91 TestVolumeMountWatcherWin::TestVolumeMountWatcherWin()
92 : attached_devices_fake_(false) {}
94 TestVolumeMountWatcherWin::~TestVolumeMountWatcherWin() {
97 void TestVolumeMountWatcherWin::AddDeviceForTesting(
98 const base::FilePath
& device_path
,
99 const std::string
& device_id
,
100 const base::string16
& storage_label
,
101 uint64 total_size_in_bytes
) {
102 StorageInfo
info(device_id
, device_path
.value(), storage_label
,
103 base::string16(), base::string16(), total_size_in_bytes
);
104 HandleDeviceAttachEventOnUIThread(device_path
, info
);
107 void TestVolumeMountWatcherWin::SetAttachedDevicesFake() {
108 attached_devices_fake_
= true;
111 void TestVolumeMountWatcherWin::FlushWorkerPoolForTesting() {
112 content::BrowserThread::GetBlockingPool()->FlushForTesting();
115 void TestVolumeMountWatcherWin::DeviceCheckComplete(
116 const base::FilePath
& device_path
) {
117 devices_checked_
.push_back(device_path
);
118 if (device_check_complete_event_
.get())
119 device_check_complete_event_
->Wait();
120 VolumeMountWatcherWin::DeviceCheckComplete(device_path
);
123 void TestVolumeMountWatcherWin::BlockDeviceCheckForTesting() {
124 device_check_complete_event_
.reset(new base::WaitableEvent(false, false));
125 devices_checked_
.clear();
128 void TestVolumeMountWatcherWin::ReleaseDeviceCheck() {
129 device_check_complete_event_
->Signal();
133 bool TestVolumeMountWatcherWin::GetDeviceRemovable(
134 const base::FilePath
& device_path
,
137 bool success
= GetMassStorageDeviceDetails(device_path
, &info
);
138 *removable
= StorageInfo::IsRemovableDevice(info
.device_id());
142 VolumeMountWatcherWin::GetDeviceDetailsCallbackType
143 TestVolumeMountWatcherWin::GetDeviceDetailsCallback() const {
144 return base::Bind(&GetMassStorageDeviceDetails
);
147 VolumeMountWatcherWin::GetAttachedDevicesCallbackType
148 TestVolumeMountWatcherWin::GetAttachedDevicesCallback() const {
149 if (attached_devices_fake_
)
150 return base::Bind(&FakeGetAttachedDevices
);
151 return base::Bind(&FakeGetSingleAttachedDevice
);
154 } // namespace storage_monitor