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 "chrome/browser/chromeos/extensions/file_manager/device_event_router.h"
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"
15 namespace file_manager {
18 namespace file_manager_private = extensions::api::file_manager_private;
19 typedef chromeos::disks::DiskMountManager::Disk Disk;
21 const char kTestDevicePath[] = "/device/test";
24 extensions::api::file_manager_private::DeviceEventType type;
25 std::string device_path;
28 // DeviceEventRouter implementation for testing.
29 class DeviceEventRouterImpl : public DeviceEventRouter {
31 DeviceEventRouterImpl()
32 : DeviceEventRouter(base::TimeDelta::FromSeconds(0)),
33 external_storage_disabled(false) {}
34 virtual ~DeviceEventRouterImpl() {}
36 // DeviceEventRouter overrides.
37 virtual void OnDeviceEvent(file_manager_private::DeviceEventType type,
38 const std::string& device_path) OVERRIDE {
41 event.device_path = device_path;
42 events.push_back(event);
45 // DeviceEventRouter overrides.
46 virtual bool IsExternalStorageDisabled() OVERRIDE {
47 return external_storage_disabled;
50 // List of dispatched events.
51 std::vector<DeviceEvent> events;
53 // Flag returned by |IsExternalStorageDisabled|.
54 bool external_storage_disabled;
57 DISALLOW_COPY_AND_ASSIGN(DeviceEventRouterImpl);
62 class DeviceEventRouterTest : public testing::Test {
64 virtual void SetUp() OVERRIDE {
65 device_event_router.reset(new DeviceEventRouterImpl());
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,
83 chromeos::DEVICE_TYPE_UNKNOWN,
93 // Creates a volume info instance with |device_path| and |mount_path| for
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);
103 scoped_ptr<DeviceEventRouterImpl> device_event_router;
106 base::MessageLoop message_loop_;
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,
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);
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);
146 } // namespace file_manager