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(/* zero_time_delta */ true), |
| 33 external_storage_disabled(false) {} |
| 34 virtual ~DeviceEventRouterImpl() {} |
| 35 |
| 36 // Override |
| 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 // Override |
| 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 } // namespace |
| 60 |
| 61 class DeviceEventRouterTest : public testing::Test { |
| 62 protected: |
| 63 virtual void SetUp() OVERRIDE { |
| 64 device_event_router.reset(new DeviceEventRouterImpl()); |
| 65 } |
| 66 |
| 67 // Creates a disk instance with |device_path| and |mount_path| for testing. |
| 68 Disk CreateTestDisk(const std::string& device_path, |
| 69 const std::string& mount_path) { |
| 70 return Disk(device_path, |
| 71 mount_path, |
| 72 "", |
| 73 "", |
| 74 "", |
| 75 "", |
| 76 "", |
| 77 "", |
| 78 "", |
| 79 "", |
| 80 "", |
| 81 device_path, |
| 82 chromeos::DEVICE_TYPE_UNKNOWN, |
| 83 0, |
| 84 false, |
| 85 false, |
| 86 false, |
| 87 false, |
| 88 false, |
| 89 false); |
| 90 } |
| 91 |
| 92 // Creates a volume info instance with |device_path| and |mount_path| for |
| 93 // testing. |
| 94 VolumeInfo CreateTestVolumeInfo(const std::string& device_path, |
| 95 const std::string& mount_path) { |
| 96 VolumeInfo volume_info; |
| 97 volume_info.system_path_prefix = base::FilePath(device_path); |
| 98 volume_info.mount_path = base::FilePath(mount_path); |
| 99 return volume_info; |
| 100 } |
| 101 |
| 102 scoped_ptr<DeviceEventRouterImpl> device_event_router; |
| 103 |
| 104 private: |
| 105 base::MessageLoop message_loop_; |
| 106 }; |
| 107 |
| 108 TEST_F(DeviceEventRouterTest, AddAndRemoveDevice) { |
| 109 const Disk disk1 = CreateTestDisk("/device/test", "/mount/path1"); |
| 110 const Disk disk1_unmounted = CreateTestDisk("/device/test", ""); |
| 111 const VolumeInfo volumeInfo = |
| 112 CreateTestVolumeInfo("/device/test", "/mount/path1"); |
| 113 device_event_router->OnDeviceAdded("/device/test"); |
| 114 device_event_router->OnDiskAdded(disk1, true); |
| 115 device_event_router->OnVolumeMounted( |
| 116 chromeos::MOUNT_ERROR_NONE, volumeInfo, false); |
| 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 EXPECT_EQ(1u, device_event_router->events.size()); |
| 122 EXPECT_EQ(file_browser_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, DeviceScan) { |
| 128 const Disk disk = CreateTestDisk("/device/test", "/mount/path1"); |
| 129 const Disk disk_unmounted = CreateTestDisk("/device/test", ""); |
| 130 const VolumeInfo volumeInfo = |
| 131 CreateTestVolumeInfo("/device/test", "/mount/path1"); |
| 132 device_event_router->OnDeviceAdded("/device/test"); |
| 133 base::RunLoop().RunUntilIdle(); |
| 134 device_event_router->OnDiskAdded(disk, true); |
| 135 device_event_router->OnVolumeMounted( |
| 136 chromeos::MOUNT_ERROR_NONE, volumeInfo, false); |
| 137 device_event_router->OnVolumeUnmounted(chromeos::MOUNT_ERROR_NONE, |
| 138 volumeInfo); |
| 139 device_event_router->OnDiskRemoved(disk_unmounted); |
| 140 device_event_router->OnDeviceRemoved("/device/test"); |
| 141 EXPECT_EQ(2u, device_event_router->events.size()); |
| 142 EXPECT_EQ(file_browser_private::DEVICE_EVENT_TYPE_ADDED, |
| 143 device_event_router->events[0].type); |
| 144 EXPECT_EQ("/device/test", device_event_router->events[0].device_path); |
| 145 EXPECT_EQ(file_browser_private::DEVICE_EVENT_TYPE_REMOVED, |
| 146 device_event_router->events[1].type); |
| 147 EXPECT_EQ("/device/test", device_event_router->events[1].device_path); |
| 148 } |
| 149 |
| 150 TEST_F(DeviceEventRouterTest, DeviceScanCancelled) { |
| 151 const Disk disk = CreateTestDisk("/device/test", "/mount/path1"); |
| 152 const Disk disk_unmounted = CreateTestDisk("/device/test", ""); |
| 153 const VolumeInfo volumeInfo = |
| 154 CreateTestVolumeInfo("/device/test", "/mount/path1"); |
| 155 device_event_router->OnDeviceAdded("/device/test"); |
| 156 base::RunLoop().RunUntilIdle(); |
| 157 device_event_router->OnDiskAdded(disk, false); |
| 158 device_event_router->OnDiskRemoved(disk_unmounted); |
| 159 device_event_router->OnDeviceRemoved("/device/test"); |
| 160 EXPECT_EQ(3u, device_event_router->events.size()); |
| 161 EXPECT_EQ(file_browser_private::DEVICE_EVENT_TYPE_ADDED, |
| 162 device_event_router->events[0].type); |
| 163 EXPECT_EQ("/device/test", device_event_router->events[0].device_path); |
| 164 EXPECT_EQ(file_browser_private::DEVICE_EVENT_TYPE_SCAN_CANCELED, |
| 165 device_event_router->events[1].type); |
| 166 EXPECT_EQ("/device/test", device_event_router->events[1].device_path); |
| 167 EXPECT_EQ(file_browser_private::DEVICE_EVENT_TYPE_REMOVED, |
| 168 device_event_router->events[2].type); |
| 169 EXPECT_EQ("/device/test", device_event_router->events[2].device_path); |
| 170 } |
| 171 |
| 172 TEST_F(DeviceEventRouterTest, HardUnplugged) { |
| 173 const Disk disk1 = CreateTestDisk("/device/test", "/mount/path1"); |
| 174 const Disk disk2 = CreateTestDisk("/device/test", "/mount/path2"); |
| 175 device_event_router->OnDeviceAdded("/device/test"); |
| 176 device_event_router->OnDiskAdded(disk1, true); |
| 177 device_event_router->OnDiskAdded(disk2, true); |
| 178 device_event_router->OnDiskRemoved(disk1); |
| 179 device_event_router->OnDiskRemoved(disk2); |
| 180 device_event_router->OnDeviceRemoved(kTestDevicePath); |
| 181 base::RunLoop().RunUntilIdle(); |
| 182 EXPECT_EQ(2u, device_event_router->events.size()); |
| 183 EXPECT_EQ(file_browser_private::DEVICE_EVENT_TYPE_HARD_UNPLUGGED, |
| 184 device_event_router->events[0].type); |
| 185 EXPECT_EQ("/device/test", device_event_router->events[0].device_path); |
| 186 EXPECT_EQ(file_browser_private::DEVICE_EVENT_TYPE_REMOVED, |
| 187 device_event_router->events[1].type); |
| 188 EXPECT_EQ("/device/test", device_event_router->events[1].device_path); |
| 189 } |
| 190 |
| 191 } // namespace file_manager |
OLD | NEW |