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 ~DeviceEventRouterImpl() override
{}
36 // DeviceEventRouter overrides.
37 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 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 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 scoped_ptr
<DeviceEventRouterImpl
> device_event_router
;
96 base::MessageLoop message_loop_
;
99 TEST_F(DeviceEventRouterTest
, AddAndRemoveDevice
) {
100 const Disk disk1
= CreateTestDisk("/device/test", "/mount/path1");
101 const Disk disk1_unmounted
= CreateTestDisk("/device/test", "");
102 scoped_ptr
<Volume
> volume(Volume::CreateForTesting(
103 base::FilePath(FILE_PATH_LITERAL("/device/test")),
104 base::FilePath(FILE_PATH_LITERAL("/mount/path1"))));
105 device_event_router
->OnDeviceAdded("/device/test");
106 device_event_router
->OnDiskAdded(disk1
, true);
107 device_event_router
->OnVolumeMounted(chromeos::MOUNT_ERROR_NONE
,
109 device_event_router
->OnVolumeUnmounted(chromeos::MOUNT_ERROR_NONE
,
111 device_event_router
->OnDiskRemoved(disk1_unmounted
);
112 device_event_router
->OnDeviceRemoved("/device/test");
113 ASSERT_EQ(1u, device_event_router
->events
.size());
114 EXPECT_EQ(file_manager_private::DEVICE_EVENT_TYPE_REMOVED
,
115 device_event_router
->events
[0].type
);
116 EXPECT_EQ("/device/test", device_event_router
->events
[0].device_path
);
119 TEST_F(DeviceEventRouterTest
, HardUnplugged
) {
120 const Disk disk1
= CreateTestDisk("/device/test", "/mount/path1");
121 const Disk disk2
= CreateTestDisk("/device/test", "/mount/path2");
122 device_event_router
->OnDeviceAdded("/device/test");
123 device_event_router
->OnDiskAdded(disk1
, true);
124 device_event_router
->OnDiskAdded(disk2
, true);
125 device_event_router
->OnDiskRemoved(disk1
);
126 device_event_router
->OnDiskRemoved(disk2
);
127 device_event_router
->OnDeviceRemoved(kTestDevicePath
);
128 base::RunLoop().RunUntilIdle();
129 ASSERT_EQ(2u, device_event_router
->events
.size());
130 EXPECT_EQ(file_manager_private::DEVICE_EVENT_TYPE_HARD_UNPLUGGED
,
131 device_event_router
->events
[0].type
);
132 EXPECT_EQ("/device/test", device_event_router
->events
[0].device_path
);
133 EXPECT_EQ(file_manager_private::DEVICE_EVENT_TYPE_REMOVED
,
134 device_event_router
->events
[1].type
);
135 EXPECT_EQ("/device/test", device_event_router
->events
[1].device_path
);
138 } // namespace file_manager