1 // Copyright (c) 2012 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 "chrome/browser/storage_monitor/mock_removable_storage_observer.h"
9 #include "chrome/browser/storage_monitor/storage_monitor.h"
10 #include "chrome/browser/storage_monitor/test_storage_monitor.h"
11 #include "testing/gtest/include/gtest/gtest.h"
15 void SetLatch(bool* called
) {
21 TEST(StorageMonitorTest
, TestInitialize
) {
22 TestStorageMonitor::RemoveSingleton();
23 TestStorageMonitor monitor
;
24 EXPECT_FALSE(monitor
.init_called());
26 bool initialized
= false;
27 monitor
.EnsureInitialized(base::Bind(&SetLatch
, &initialized
));
28 EXPECT_TRUE(monitor
.init_called());
29 EXPECT_FALSE(initialized
);
30 monitor
.MarkInitialized();
31 EXPECT_TRUE(initialized
);
34 TEST(StorageMonitorTest
, DeviceAttachDetachNotifications
) {
35 TestStorageMonitor::RemoveSingleton();
36 base::MessageLoop message_loop
;
37 const base::string16 kDeviceName
= base::ASCIIToUTF16("media device");
38 const std::string kDeviceId1
= "dcim:UUID:FFF0-0001";
39 const std::string kDeviceId2
= "dcim:UUID:FFF0-0002";
40 MockRemovableStorageObserver observer1
;
41 MockRemovableStorageObserver observer2
;
42 TestStorageMonitor monitor
;
43 monitor
.AddObserver(&observer1
);
44 monitor
.AddObserver(&observer2
);
46 StorageInfo
info(kDeviceId1
, kDeviceName
, FILE_PATH_LITERAL("path"),
47 base::string16(), base::string16(), base::string16(), 0);
48 monitor
.receiver()->ProcessAttach(info
);
49 message_loop
.RunUntilIdle();
51 EXPECT_EQ(kDeviceId1
, observer1
.last_attached().device_id());
52 EXPECT_EQ(kDeviceName
, observer1
.last_attached().name());
53 EXPECT_EQ(FILE_PATH_LITERAL("path"), observer1
.last_attached().location());
54 EXPECT_EQ(kDeviceId1
, observer2
.last_attached().device_id());
55 EXPECT_EQ(kDeviceName
, observer2
.last_attached().name());
56 EXPECT_EQ(FILE_PATH_LITERAL("path"), observer2
.last_attached().location());
57 EXPECT_EQ(1, observer1
.attach_calls());
58 EXPECT_EQ(0, observer1
.detach_calls());
60 monitor
.receiver()->ProcessDetach(kDeviceId1
);
61 monitor
.receiver()->ProcessDetach(kDeviceId2
);
62 message_loop
.RunUntilIdle();
64 EXPECT_EQ(kDeviceId1
, observer1
.last_detached().device_id());
65 EXPECT_EQ(kDeviceName
, observer1
.last_detached().name());
66 EXPECT_EQ(FILE_PATH_LITERAL("path"), observer1
.last_detached().location());
67 EXPECT_EQ(kDeviceId1
, observer2
.last_detached().device_id());
68 EXPECT_EQ(kDeviceName
, observer2
.last_detached().name());
69 EXPECT_EQ(FILE_PATH_LITERAL("path"), observer2
.last_detached().location());
71 EXPECT_EQ(1, observer1
.attach_calls());
72 EXPECT_EQ(1, observer2
.attach_calls());
74 // The kDeviceId2 won't be notified since it was never attached.
75 EXPECT_EQ(1, observer1
.detach_calls());
76 EXPECT_EQ(1, observer2
.detach_calls());
78 monitor
.RemoveObserver(&observer1
);
79 monitor
.RemoveObserver(&observer2
);
82 TEST(StorageMonitorTest
, GetAllAvailableStoragesEmpty
) {
83 TestStorageMonitor::RemoveSingleton();
84 base::MessageLoop message_loop
;
85 TestStorageMonitor monitor
;
86 std::vector
<StorageInfo
> devices
= monitor
.GetAllAvailableStorages();
87 EXPECT_EQ(0U, devices
.size());
90 TEST(StorageMonitorTest
, GetAllAvailableStorageAttachDetach
) {
91 TestStorageMonitor::RemoveSingleton();
92 base::MessageLoop message_loop
;
93 TestStorageMonitor monitor
;
94 const std::string kDeviceId1
= "dcim:UUID:FFF0-0042";
95 const base::string16 kDeviceName1
= base::ASCIIToUTF16("test");
96 const base::FilePath
kDevicePath1(FILE_PATH_LITERAL("/testfoo"));
97 StorageInfo
info1(kDeviceId1
, kDeviceName1
, kDevicePath1
.value(),
98 base::string16(), base::string16(), base::string16(), 0);
99 monitor
.receiver()->ProcessAttach(info1
);
100 message_loop
.RunUntilIdle();
101 std::vector
<StorageInfo
> devices
= monitor
.GetAllAvailableStorages();
102 ASSERT_EQ(1U, devices
.size());
103 EXPECT_EQ(kDeviceId1
, devices
[0].device_id());
104 EXPECT_EQ(kDeviceName1
, devices
[0].name());
105 EXPECT_EQ(kDevicePath1
.value(), devices
[0].location());
107 const std::string kDeviceId2
= "dcim:UUID:FFF0-0044";
108 const base::string16 kDeviceName2
= base::ASCIIToUTF16("test2");
109 const base::FilePath
kDevicePath2(FILE_PATH_LITERAL("/testbar"));
110 StorageInfo
info2(kDeviceId2
, kDeviceName2
, kDevicePath2
.value(),
111 base::string16(), base::string16(), base::string16(), 0);
112 monitor
.receiver()->ProcessAttach(info2
);
113 message_loop
.RunUntilIdle();
114 devices
= monitor
.GetAllAvailableStorages();
115 ASSERT_EQ(2U, devices
.size());
116 EXPECT_EQ(kDeviceId1
, devices
[0].device_id());
117 EXPECT_EQ(kDeviceName1
, devices
[0].name());
118 EXPECT_EQ(kDevicePath1
.value(), devices
[0].location());
119 EXPECT_EQ(kDeviceId2
, devices
[1].device_id());
120 EXPECT_EQ(kDeviceName2
, devices
[1].name());
121 EXPECT_EQ(kDevicePath2
.value(), devices
[1].location());
123 monitor
.receiver()->ProcessDetach(kDeviceId1
);
124 message_loop
.RunUntilIdle();
125 devices
= monitor
.GetAllAvailableStorages();
126 ASSERT_EQ(1U, devices
.size());
127 EXPECT_EQ(kDeviceId2
, devices
[0].device_id());
128 EXPECT_EQ(kDeviceName2
, devices
[0].name());
129 EXPECT_EQ(kDevicePath2
.value(), devices
[0].location());
131 monitor
.receiver()->ProcessDetach(kDeviceId2
);
132 message_loop
.RunUntilIdle();
133 devices
= monitor
.GetAllAvailableStorages();
134 EXPECT_EQ(0U, devices
.size());