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 "base/message_loop/message_loop.h"
6 #include "base/strings/utf_string_conversions.h"
7 #include "base/synchronization/waitable_event.h"
8 #include "components/storage_monitor/mock_removable_storage_observer.h"
9 #include "components/storage_monitor/storage_monitor.h"
10 #include "components/storage_monitor/test_storage_monitor.h"
11 #include "testing/gtest/include/gtest/gtest.h"
15 void SetLatch(bool* called
) {
21 namespace storage_monitor
{
23 TEST(StorageMonitorTest
, TestInitialize
) {
24 TestStorageMonitor::Destroy();
25 TestStorageMonitor monitor
;
26 EXPECT_FALSE(monitor
.init_called());
28 bool initialized
= false;
29 monitor
.EnsureInitialized(base::Bind(&SetLatch
, &initialized
));
30 EXPECT_TRUE(monitor
.init_called());
31 EXPECT_FALSE(initialized
);
32 monitor
.MarkInitialized();
33 EXPECT_TRUE(initialized
);
36 TEST(StorageMonitorTest
, DeviceAttachDetachNotifications
) {
37 TestStorageMonitor::Destroy();
38 base::MessageLoop message_loop
;
39 const base::string16 kDeviceName
= base::ASCIIToUTF16("media device");
40 const std::string kDeviceId1
= "dcim:UUID:FFF0-0001";
41 const std::string kDeviceId2
= "dcim:UUID:FFF0-0002";
42 MockRemovableStorageObserver observer1
;
43 MockRemovableStorageObserver observer2
;
44 TestStorageMonitor monitor
;
45 monitor
.AddObserver(&observer1
);
46 monitor
.AddObserver(&observer2
);
48 StorageInfo
info(kDeviceId1
, kDeviceName
, FILE_PATH_LITERAL("path"),
49 base::string16(), base::string16(), base::string16(), 0);
50 monitor
.receiver()->ProcessAttach(info
);
51 message_loop
.RunUntilIdle();
53 EXPECT_EQ(kDeviceId1
, observer1
.last_attached().device_id());
54 EXPECT_EQ(kDeviceName
, observer1
.last_attached().name());
55 EXPECT_EQ(FILE_PATH_LITERAL("path"), observer1
.last_attached().location());
56 EXPECT_EQ(kDeviceId1
, observer2
.last_attached().device_id());
57 EXPECT_EQ(kDeviceName
, observer2
.last_attached().name());
58 EXPECT_EQ(FILE_PATH_LITERAL("path"), observer2
.last_attached().location());
59 EXPECT_EQ(1, observer1
.attach_calls());
60 EXPECT_EQ(0, observer1
.detach_calls());
62 monitor
.receiver()->ProcessDetach(kDeviceId1
);
63 monitor
.receiver()->ProcessDetach(kDeviceId2
);
64 message_loop
.RunUntilIdle();
66 EXPECT_EQ(kDeviceId1
, observer1
.last_detached().device_id());
67 EXPECT_EQ(kDeviceName
, observer1
.last_detached().name());
68 EXPECT_EQ(FILE_PATH_LITERAL("path"), observer1
.last_detached().location());
69 EXPECT_EQ(kDeviceId1
, observer2
.last_detached().device_id());
70 EXPECT_EQ(kDeviceName
, observer2
.last_detached().name());
71 EXPECT_EQ(FILE_PATH_LITERAL("path"), observer2
.last_detached().location());
73 EXPECT_EQ(1, observer1
.attach_calls());
74 EXPECT_EQ(1, observer2
.attach_calls());
76 // The kDeviceId2 won't be notified since it was never attached.
77 EXPECT_EQ(1, observer1
.detach_calls());
78 EXPECT_EQ(1, observer2
.detach_calls());
80 monitor
.RemoveObserver(&observer1
);
81 monitor
.RemoveObserver(&observer2
);
84 TEST(StorageMonitorTest
, GetAllAvailableStoragesEmpty
) {
85 TestStorageMonitor::Destroy();
86 base::MessageLoop message_loop
;
87 TestStorageMonitor monitor
;
88 std::vector
<StorageInfo
> devices
= monitor
.GetAllAvailableStorages();
89 EXPECT_EQ(0U, devices
.size());
92 TEST(StorageMonitorTest
, GetAllAvailableStorageAttachDetach
) {
93 TestStorageMonitor::Destroy();
94 base::MessageLoop message_loop
;
95 TestStorageMonitor monitor
;
96 const std::string kDeviceId1
= "dcim:UUID:FFF0-0042";
97 const base::string16 kDeviceName1
= base::ASCIIToUTF16("test");
98 const base::FilePath
kDevicePath1(FILE_PATH_LITERAL("/testfoo"));
99 StorageInfo
info1(kDeviceId1
, kDeviceName1
, kDevicePath1
.value(),
100 base::string16(), base::string16(), base::string16(), 0);
101 monitor
.receiver()->ProcessAttach(info1
);
102 message_loop
.RunUntilIdle();
103 std::vector
<StorageInfo
> devices
= monitor
.GetAllAvailableStorages();
104 ASSERT_EQ(1U, devices
.size());
105 EXPECT_EQ(kDeviceId1
, devices
[0].device_id());
106 EXPECT_EQ(kDeviceName1
, devices
[0].name());
107 EXPECT_EQ(kDevicePath1
.value(), devices
[0].location());
109 const std::string kDeviceId2
= "dcim:UUID:FFF0-0044";
110 const base::string16 kDeviceName2
= base::ASCIIToUTF16("test2");
111 const base::FilePath
kDevicePath2(FILE_PATH_LITERAL("/testbar"));
112 StorageInfo
info2(kDeviceId2
, kDeviceName2
, kDevicePath2
.value(),
113 base::string16(), base::string16(), base::string16(), 0);
114 monitor
.receiver()->ProcessAttach(info2
);
115 message_loop
.RunUntilIdle();
116 devices
= monitor
.GetAllAvailableStorages();
117 ASSERT_EQ(2U, devices
.size());
118 EXPECT_EQ(kDeviceId1
, devices
[0].device_id());
119 EXPECT_EQ(kDeviceName1
, devices
[0].name());
120 EXPECT_EQ(kDevicePath1
.value(), devices
[0].location());
121 EXPECT_EQ(kDeviceId2
, devices
[1].device_id());
122 EXPECT_EQ(kDeviceName2
, devices
[1].name());
123 EXPECT_EQ(kDevicePath2
.value(), devices
[1].location());
125 monitor
.receiver()->ProcessDetach(kDeviceId1
);
126 message_loop
.RunUntilIdle();
127 devices
= monitor
.GetAllAvailableStorages();
128 ASSERT_EQ(1U, devices
.size());
129 EXPECT_EQ(kDeviceId2
, devices
[0].device_id());
130 EXPECT_EQ(kDeviceName2
, devices
[0].name());
131 EXPECT_EQ(kDevicePath2
.value(), devices
[0].location());
133 monitor
.receiver()->ProcessDetach(kDeviceId2
);
134 message_loop
.RunUntilIdle();
135 devices
= monitor
.GetAllAvailableStorages();
136 EXPECT_EQ(0U, devices
.size());
139 } // namespace storage_monitor