Home | History | Annotate | Download | only in storage_monitor
      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.
      4 
      5 #include "components/storage_monitor/test_storage_monitor.h"
      6 
      7 #include "base/run_loop.h"
      8 #include "base/synchronization/waitable_event.h"
      9 #include "components/storage_monitor/storage_info.h"
     10 
     11 #if defined(OS_LINUX)
     12 #include "components/storage_monitor/test_media_transfer_protocol_manager_linux.h"
     13 #include "device/media_transfer_protocol/media_transfer_protocol_manager.h"
     14 #endif
     15 
     16 namespace storage_monitor {
     17 
     18 TestStorageMonitor::TestStorageMonitor()
     19     : StorageMonitor(),
     20       init_called_(false) {
     21 #if defined(OS_LINUX)
     22   media_transfer_protocol_manager_.reset(
     23       new TestMediaTransferProtocolManagerLinux());
     24 #endif
     25 }
     26 
     27 TestStorageMonitor::~TestStorageMonitor() {}
     28 
     29 // static
     30 TestStorageMonitor* TestStorageMonitor::CreateAndInstall() {
     31   TestStorageMonitor* monitor = new TestStorageMonitor();
     32   scoped_ptr<StorageMonitor> pass_monitor(monitor);
     33   monitor->Init();
     34   monitor->MarkInitialized();
     35 
     36   if (StorageMonitor::GetInstance() == NULL) {
     37     StorageMonitor::SetStorageMonitorForTesting(pass_monitor.Pass());
     38     return monitor;
     39   }
     40 
     41   return NULL;
     42 }
     43 
     44 // static
     45 TestStorageMonitor* TestStorageMonitor::CreateForBrowserTests() {
     46   TestStorageMonitor* monitor = new TestStorageMonitor();
     47   monitor->Init();
     48   monitor->MarkInitialized();
     49 
     50   scoped_ptr<StorageMonitor> pass_monitor(monitor);
     51   StorageMonitor::SetStorageMonitorForTesting(pass_monitor.Pass());
     52 
     53   return monitor;
     54 }
     55 
     56 // static
     57 void TestStorageMonitor::SyncInitialize() {
     58   StorageMonitor* monitor = StorageMonitor::GetInstance();
     59   if (monitor->IsInitialized())
     60     return;
     61 
     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();
     67   }
     68   DCHECK(monitor->IsInitialized());
     69 }
     70 
     71 void TestStorageMonitor::Init() {
     72   init_called_ = true;
     73 }
     74 
     75 void TestStorageMonitor::MarkInitialized() {
     76   StorageMonitor::MarkInitialized();
     77 }
     78 
     79 bool TestStorageMonitor::GetStorageInfoForPath(
     80     const base::FilePath& path,
     81     StorageInfo* device_info) const {
     82   DCHECK(device_info);
     83 
     84   if (!path.IsAbsolute())
     85     return false;
     86 
     87   std::string device_id = StorageInfo::MakeDeviceId(
     88       StorageInfo::FIXED_MASS_STORAGE, path.AsUTF8Unsafe());
     89   *device_info =
     90       StorageInfo(device_id, path.value(), base::string16(), base::string16(),
     91                   base::string16(), 0);
     92   return true;
     93 }
     94 
     95 #if defined(OS_WIN)
     96 bool TestStorageMonitor::GetMTPStorageInfoFromDeviceId(
     97     const std::string& storage_device_id,
     98     base::string16* device_location,
     99     base::string16* storage_object_id) const {
    100   return false;
    101 }
    102 #endif
    103 
    104 #if defined(OS_LINUX)
    105 device::MediaTransferProtocolManager*
    106 TestStorageMonitor::media_transfer_protocol_manager() {
    107   return media_transfer_protocol_manager_.get();
    108 }
    109 #endif
    110 
    111 StorageMonitor::Receiver* TestStorageMonitor::receiver() const {
    112   return StorageMonitor::receiver();
    113 }
    114 
    115 void TestStorageMonitor::EjectDevice(
    116     const std::string& device_id,
    117     base::Callback<void(EjectStatus)> callback) {
    118   ejected_device_ = device_id;
    119   callback.Run(EJECT_OK);
    120 }
    121 
    122 }  // namespace storage_monitor
    123