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 std::string kDeviceId1
= "dcim:UUID:FFF0-0001";
40 const std::string kDeviceId2
= "dcim:UUID:FFF0-0002";
41 MockRemovableStorageObserver observer1
;
42 MockRemovableStorageObserver observer2
;
43 TestStorageMonitor monitor
;
44 monitor
.AddObserver(&observer1
);
45 monitor
.AddObserver(&observer2
);
47 StorageInfo
info(kDeviceId1
, FILE_PATH_LITERAL("path"), base::string16(),
48 base::string16(), base::string16(), 0);
49 monitor
.receiver()->ProcessAttach(info
);
50 message_loop
.RunUntilIdle();
52 EXPECT_EQ(kDeviceId1
, observer1
.last_attached().device_id());
53 EXPECT_EQ(FILE_PATH_LITERAL("path"), observer1
.last_attached().location());
54 EXPECT_EQ(kDeviceId1
, observer2
.last_attached().device_id());
55 EXPECT_EQ(FILE_PATH_LITERAL("path"), observer2
.last_attached().location());
56 EXPECT_EQ(1, observer1
.attach_calls());
57 EXPECT_EQ(0, observer1
.detach_calls());
59 monitor
.receiver()->ProcessDetach(kDeviceId1
);
60 monitor
.receiver()->ProcessDetach(kDeviceId2
);
61 message_loop
.RunUntilIdle();
63 EXPECT_EQ(kDeviceId1
, observer1
.last_detached().device_id());
64 EXPECT_EQ(FILE_PATH_LITERAL("path"), observer1
.last_detached().location());
65 EXPECT_EQ(kDeviceId1
, observer2
.last_detached().device_id());
66 EXPECT_EQ(FILE_PATH_LITERAL("path"), observer2
.last_detached().location());
68 EXPECT_EQ(1, observer1
.attach_calls());
69 EXPECT_EQ(1, observer2
.attach_calls());
71 // The kDeviceId2 won't be notified since it was never attached.
72 EXPECT_EQ(1, observer1
.detach_calls());
73 EXPECT_EQ(1, observer2
.detach_calls());
75 monitor
.RemoveObserver(&observer1
);
76 monitor
.RemoveObserver(&observer2
);
79 TEST(StorageMonitorTest
, GetAllAvailableStoragesEmpty
) {
80 TestStorageMonitor::Destroy();
81 base::MessageLoop message_loop
;
82 TestStorageMonitor monitor
;
83 std::vector
<StorageInfo
> devices
= monitor
.GetAllAvailableStorages();
84 EXPECT_EQ(0U, devices
.size());
87 TEST(StorageMonitorTest
, GetAllAvailableStorageAttachDetach
) {
88 TestStorageMonitor::Destroy();
89 base::MessageLoop message_loop
;
90 TestStorageMonitor monitor
;
91 const std::string kDeviceId1
= "dcim:UUID:FFF0-0042";
92 const base::FilePath
kDevicePath1(FILE_PATH_LITERAL("/testfoo"));
93 StorageInfo
info1(kDeviceId1
, kDevicePath1
.value(), base::string16(),
94 base::string16(), base::string16(), 0);
95 monitor
.receiver()->ProcessAttach(info1
);
96 message_loop
.RunUntilIdle();
97 std::vector
<StorageInfo
> devices
= monitor
.GetAllAvailableStorages();
98 ASSERT_EQ(1U, devices
.size());
99 EXPECT_EQ(kDeviceId1
, devices
[0].device_id());
100 EXPECT_EQ(kDevicePath1
.value(), devices
[0].location());
102 const std::string kDeviceId2
= "dcim:UUID:FFF0-0044";
103 const base::FilePath
kDevicePath2(FILE_PATH_LITERAL("/testbar"));
104 StorageInfo
info2(kDeviceId2
, kDevicePath2
.value(), base::string16(),
105 base::string16(), base::string16(), 0);
106 monitor
.receiver()->ProcessAttach(info2
);
107 message_loop
.RunUntilIdle();
108 devices
= monitor
.GetAllAvailableStorages();
109 ASSERT_EQ(2U, devices
.size());
110 EXPECT_EQ(kDeviceId1
, devices
[0].device_id());
111 EXPECT_EQ(kDevicePath1
.value(), devices
[0].location());
112 EXPECT_EQ(kDeviceId2
, devices
[1].device_id());
113 EXPECT_EQ(kDevicePath2
.value(), devices
[1].location());
115 monitor
.receiver()->ProcessDetach(kDeviceId1
);
116 message_loop
.RunUntilIdle();
117 devices
= monitor
.GetAllAvailableStorages();
118 ASSERT_EQ(1U, devices
.size());
119 EXPECT_EQ(kDeviceId2
, devices
[0].device_id());
120 EXPECT_EQ(kDevicePath2
.value(), devices
[0].location());
122 monitor
.receiver()->ProcessDetach(kDeviceId2
);
123 message_loop
.RunUntilIdle();
124 devices
= monitor
.GetAllAvailableStorages();
125 EXPECT_EQ(0U, devices
.size());
128 } // namespace storage_monitor