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 "chrome/browser/chromeos/extensions/file_manager/device_event_router.h" 6 7 #include <string> 8 #include <vector> 9 10 #include "base/message_loop/message_loop.h" 11 #include "base/run_loop.h" 12 #include "chrome/browser/chromeos/file_manager/volume_manager.h" 13 #include "testing/gtest/include/gtest/gtest.h" 14 15 namespace file_manager { 16 namespace { 17 18 namespace file_manager_private = extensions::api::file_manager_private; 19 typedef chromeos::disks::DiskMountManager::Disk Disk; 20 21 const char kTestDevicePath[] = "/device/test"; 22 23 struct DeviceEvent { 24 extensions::api::file_manager_private::DeviceEventType type; 25 std::string device_path; 26 }; 27 28 // DeviceEventRouter implementation for testing. 29 class DeviceEventRouterImpl : public DeviceEventRouter { 30 public: 31 DeviceEventRouterImpl() 32 : DeviceEventRouter(base::TimeDelta::FromSeconds(0)), 33 external_storage_disabled(false) {} 34 virtual ~DeviceEventRouterImpl() {} 35 36 // DeviceEventRouter overrides. 37 virtual void OnDeviceEvent(file_manager_private::DeviceEventType type, 38 const std::string& device_path) OVERRIDE { 39 DeviceEvent event; 40 event.type = type; 41 event.device_path = device_path; 42 events.push_back(event); 43 } 44 45 // DeviceEventRouter overrides. 46 virtual bool IsExternalStorageDisabled() OVERRIDE { 47 return external_storage_disabled; 48 } 49 50 // List of dispatched events. 51 std::vector<DeviceEvent> events; 52 53 // Flag returned by |IsExternalStorageDisabled|. 54 bool external_storage_disabled; 55 56 private: 57 DISALLOW_COPY_AND_ASSIGN(DeviceEventRouterImpl); 58 }; 59 60 } // namespace 61 62 class DeviceEventRouterTest : public testing::Test { 63 protected: 64 virtual void SetUp() OVERRIDE { 65 device_event_router.reset(new DeviceEventRouterImpl()); 66 } 67 68 // Creates a disk instance with |device_path| and |mount_path| for testing. 69 Disk CreateTestDisk(const std::string& device_path, 70 const std::string& mount_path) { 71 return Disk(device_path, 72 mount_path, 73 "", 74 "", 75 "", 76 "", 77 "", 78 "", 79 "", 80 "", 81 "", 82 device_path, 83 chromeos::DEVICE_TYPE_UNKNOWN, 84 0, 85 false, 86 false, 87 false, 88 false, 89 false, 90 false); 91 } 92 93 // Creates a volume info instance with |device_path| and |mount_path| for 94 // testing. 95 VolumeInfo CreateTestVolumeInfo(const std::string& device_path, 96 const std::string& mount_path) { 97 VolumeInfo volume_info; 98 volume_info.system_path_prefix = base::FilePath(device_path); 99 volume_info.mount_path = base::FilePath(mount_path); 100 return volume_info; 101 } 102 103 scoped_ptr<DeviceEventRouterImpl> device_event_router; 104 105 private: 106 base::MessageLoop message_loop_; 107 }; 108 109 TEST_F(DeviceEventRouterTest, AddAndRemoveDevice) { 110 const Disk disk1 = CreateTestDisk("/device/test", "/mount/path1"); 111 const Disk disk1_unmounted = CreateTestDisk("/device/test", ""); 112 const VolumeInfo volumeInfo = 113 CreateTestVolumeInfo("/device/test", "/mount/path1"); 114 device_event_router->OnDeviceAdded("/device/test"); 115 device_event_router->OnDiskAdded(disk1, true); 116 device_event_router->OnVolumeMounted(chromeos::MOUNT_ERROR_NONE, volumeInfo); 117 device_event_router->OnVolumeUnmounted(chromeos::MOUNT_ERROR_NONE, 118 volumeInfo); 119 device_event_router->OnDiskRemoved(disk1_unmounted); 120 device_event_router->OnDeviceRemoved("/device/test"); 121 ASSERT_EQ(1u, device_event_router->events.size()); 122 EXPECT_EQ(file_manager_private::DEVICE_EVENT_TYPE_REMOVED, 123 device_event_router->events[0].type); 124 EXPECT_EQ("/device/test", device_event_router->events[0].device_path); 125 } 126 127 TEST_F(DeviceEventRouterTest, HardUnplugged) { 128 const Disk disk1 = CreateTestDisk("/device/test", "/mount/path1"); 129 const Disk disk2 = CreateTestDisk("/device/test", "/mount/path2"); 130 device_event_router->OnDeviceAdded("/device/test"); 131 device_event_router->OnDiskAdded(disk1, true); 132 device_event_router->OnDiskAdded(disk2, true); 133 device_event_router->OnDiskRemoved(disk1); 134 device_event_router->OnDiskRemoved(disk2); 135 device_event_router->OnDeviceRemoved(kTestDevicePath); 136 base::RunLoop().RunUntilIdle(); 137 ASSERT_EQ(2u, device_event_router->events.size()); 138 EXPECT_EQ(file_manager_private::DEVICE_EVENT_TYPE_HARD_UNPLUGGED, 139 device_event_router->events[0].type); 140 EXPECT_EQ("/device/test", device_event_router->events[0].device_path); 141 EXPECT_EQ(file_manager_private::DEVICE_EVENT_TYPE_REMOVED, 142 device_event_router->events[1].type); 143 EXPECT_EQ("/device/test", device_event_router->events[1].device_path); 144 } 145 146 } // namespace file_manager 147