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 #include "components/storage_monitor/test_storage_monitor.h"
7 #include "base/run_loop.h"
8 #include "base/synchronization/waitable_event.h"
9 #include "components/storage_monitor/storage_info.h"
12 #include "components/storage_monitor/test_media_transfer_protocol_manager_linux.h"
13 #include "device/media_transfer_protocol/media_transfer_protocol_manager.h"
16 namespace storage_monitor
{
18 TestStorageMonitor::TestStorageMonitor()
22 media_transfer_protocol_manager_
.reset(
23 new TestMediaTransferProtocolManagerLinux());
27 TestStorageMonitor::~TestStorageMonitor() {}
30 TestStorageMonitor
* TestStorageMonitor::CreateAndInstall() {
31 TestStorageMonitor
* monitor
= new TestStorageMonitor();
32 scoped_ptr
<StorageMonitor
> pass_monitor(monitor
);
34 monitor
->MarkInitialized();
36 if (StorageMonitor::GetInstance() == NULL
) {
37 StorageMonitor::SetStorageMonitorForTesting(pass_monitor
.Pass());
45 TestStorageMonitor
* TestStorageMonitor::CreateForBrowserTests() {
46 TestStorageMonitor
* monitor
= new TestStorageMonitor();
48 monitor
->MarkInitialized();
50 scoped_ptr
<StorageMonitor
> pass_monitor(monitor
);
51 StorageMonitor::SetStorageMonitorForTesting(pass_monitor
.Pass());
57 void TestStorageMonitor::SyncInitialize() {
58 StorageMonitor
* monitor
= StorageMonitor::GetInstance();
59 if (monitor
->IsInitialized())
62 base::WaitableEvent
event(true, false);
63 monitor
->EnsureInitialized(base::Bind(&base::WaitableEvent::Signal
,
64 base::Unretained(&event
)));
65 while (!event
.IsSignaled()) {
66 base::RunLoop().RunUntilIdle();
68 DCHECK(monitor
->IsInitialized());
71 void TestStorageMonitor::Init() {
75 void TestStorageMonitor::MarkInitialized() {
76 StorageMonitor::MarkInitialized();
79 bool TestStorageMonitor::GetStorageInfoForPath(
80 const base::FilePath
& path
,
81 StorageInfo
* device_info
) const {
84 if (!path
.IsAbsolute())
87 bool is_removable
= false;
88 for (const base::FilePath
& removable
: removable_paths_
) {
89 if (path
== removable
|| removable
.IsParent(path
)) {
95 std::string device_id
= StorageInfo::MakeDeviceId(
96 is_removable
? StorageInfo::REMOVABLE_MASS_STORAGE_NO_DCIM
97 : StorageInfo::FIXED_MASS_STORAGE
,
100 StorageInfo(device_id
, path
.value(), base::string16(), base::string16(),
101 base::string16(), 0);
106 bool TestStorageMonitor::GetMTPStorageInfoFromDeviceId(
107 const std::string
& storage_device_id
,
108 base::string16
* device_location
,
109 base::string16
* storage_object_id
) const {
114 #if defined(OS_LINUX)
115 device::MediaTransferProtocolManager
*
116 TestStorageMonitor::media_transfer_protocol_manager() {
117 return media_transfer_protocol_manager_
.get();
121 StorageMonitor::Receiver
* TestStorageMonitor::receiver() const {
122 return StorageMonitor::receiver();
125 void TestStorageMonitor::EjectDevice(
126 const std::string
& device_id
,
127 base::Callback
<void(EjectStatus
)> callback
) {
128 ejected_device_
= device_id
;
129 callback
.Run(EJECT_OK
);
132 void TestStorageMonitor::AddRemovablePath(const base::FilePath
& path
) {
133 CHECK(path
.IsAbsolute());
134 removable_paths_
.push_back(path
);
137 } // namespace storage_monitor