OLD | NEW |
(Empty) | |
| 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_browser_private = extensions::api::file_browser_private; |
| 19 typedef chromeos::disks::DiskMountManager::Disk Disk; |
| 20 |
| 21 const char kTestDevicePath[] = "/device/test"; |
| 22 |
| 23 struct DeviceEvent { |
| 24 extensions::api::file_browser_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_browser_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( |
| 117 chromeos::MOUNT_ERROR_NONE, volumeInfo, false); |
| 118 device_event_router->OnVolumeUnmounted(chromeos::MOUNT_ERROR_NONE, |
| 119 volumeInfo); |
| 120 device_event_router->OnDiskRemoved(disk1_unmounted); |
| 121 device_event_router->OnDeviceRemoved("/device/test"); |
| 122 ASSERT_EQ(1u, device_event_router->events.size()); |
| 123 EXPECT_EQ(file_browser_private::DEVICE_EVENT_TYPE_REMOVED, |
| 124 device_event_router->events[0].type); |
| 125 EXPECT_EQ("/device/test", device_event_router->events[0].device_path); |
| 126 } |
| 127 |
| 128 TEST_F(DeviceEventRouterTest, DeviceScan) { |
| 129 const Disk disk = CreateTestDisk("/device/test", "/mount/path1"); |
| 130 const Disk disk_unmounted = CreateTestDisk("/device/test", ""); |
| 131 const VolumeInfo volumeInfo = |
| 132 CreateTestVolumeInfo("/device/test", "/mount/path1"); |
| 133 device_event_router->OnDeviceAdded("/device/test"); |
| 134 base::RunLoop().RunUntilIdle(); |
| 135 device_event_router->OnDiskAdded(disk, true); |
| 136 device_event_router->OnVolumeMounted( |
| 137 chromeos::MOUNT_ERROR_NONE, volumeInfo, false); |
| 138 device_event_router->OnVolumeUnmounted(chromeos::MOUNT_ERROR_NONE, |
| 139 volumeInfo); |
| 140 device_event_router->OnDiskRemoved(disk_unmounted); |
| 141 device_event_router->OnDeviceRemoved("/device/test"); |
| 142 ASSERT_EQ(2u, device_event_router->events.size()); |
| 143 EXPECT_EQ(file_browser_private::DEVICE_EVENT_TYPE_ADDED, |
| 144 device_event_router->events[0].type); |
| 145 EXPECT_EQ("/device/test", device_event_router->events[0].device_path); |
| 146 EXPECT_EQ(file_browser_private::DEVICE_EVENT_TYPE_REMOVED, |
| 147 device_event_router->events[1].type); |
| 148 EXPECT_EQ("/device/test", device_event_router->events[1].device_path); |
| 149 } |
| 150 |
| 151 TEST_F(DeviceEventRouterTest, DeviceScanCancelled) { |
| 152 const Disk disk = CreateTestDisk("/device/test", "/mount/path1"); |
| 153 const Disk disk_unmounted = CreateTestDisk("/device/test", ""); |
| 154 const VolumeInfo volumeInfo = |
| 155 CreateTestVolumeInfo("/device/test", "/mount/path1"); |
| 156 device_event_router->OnDeviceAdded("/device/test"); |
| 157 base::RunLoop().RunUntilIdle(); |
| 158 device_event_router->OnDiskAdded(disk, false); |
| 159 device_event_router->OnDiskRemoved(disk_unmounted); |
| 160 device_event_router->OnDeviceRemoved("/device/test"); |
| 161 ASSERT_EQ(3u, device_event_router->events.size()); |
| 162 EXPECT_EQ(file_browser_private::DEVICE_EVENT_TYPE_ADDED, |
| 163 device_event_router->events[0].type); |
| 164 EXPECT_EQ("/device/test", device_event_router->events[0].device_path); |
| 165 EXPECT_EQ(file_browser_private::DEVICE_EVENT_TYPE_SCAN_CANCELED, |
| 166 device_event_router->events[1].type); |
| 167 EXPECT_EQ("/device/test", device_event_router->events[1].device_path); |
| 168 EXPECT_EQ(file_browser_private::DEVICE_EVENT_TYPE_REMOVED, |
| 169 device_event_router->events[2].type); |
| 170 EXPECT_EQ("/device/test", device_event_router->events[2].device_path); |
| 171 } |
| 172 |
| 173 TEST_F(DeviceEventRouterTest, HardUnplugged) { |
| 174 const Disk disk1 = CreateTestDisk("/device/test", "/mount/path1"); |
| 175 const Disk disk2 = CreateTestDisk("/device/test", "/mount/path2"); |
| 176 device_event_router->OnDeviceAdded("/device/test"); |
| 177 device_event_router->OnDiskAdded(disk1, true); |
| 178 device_event_router->OnDiskAdded(disk2, true); |
| 179 device_event_router->OnDiskRemoved(disk1); |
| 180 device_event_router->OnDiskRemoved(disk2); |
| 181 device_event_router->OnDeviceRemoved(kTestDevicePath); |
| 182 base::RunLoop().RunUntilIdle(); |
| 183 ASSERT_EQ(2u, device_event_router->events.size()); |
| 184 EXPECT_EQ(file_browser_private::DEVICE_EVENT_TYPE_HARD_UNPLUGGED, |
| 185 device_event_router->events[0].type); |
| 186 EXPECT_EQ("/device/test", device_event_router->events[0].device_path); |
| 187 EXPECT_EQ(file_browser_private::DEVICE_EVENT_TYPE_REMOVED, |
| 188 device_event_router->events[1].type); |
| 189 EXPECT_EQ("/device/test", device_event_router->events[1].device_path); |
| 190 } |
| 191 |
| 192 } // namespace file_manager |
OLD | NEW |