OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/message_loop/message_loop.h" | 5 #include "base/message_loop/message_loop.h" |
6 #include "base/strings/utf_string_conversions.h" | 6 #include "base/strings/utf_string_conversions.h" |
7 #include "base/synchronization/waitable_event.h" | 7 #include "base/synchronization/waitable_event.h" |
8 #include "components/storage_monitor/mock_removable_storage_observer.h" | 8 #include "components/storage_monitor/mock_removable_storage_observer.h" |
9 #include "components/storage_monitor/storage_monitor.h" | 9 #include "components/storage_monitor/storage_monitor.h" |
10 #include "components/storage_monitor/test_storage_monitor.h" | 10 #include "components/storage_monitor/test_storage_monitor.h" |
(...skipping 18 matching lines...) Expand all Loading... |
29 monitor.EnsureInitialized(base::Bind(&SetLatch, &initialized)); | 29 monitor.EnsureInitialized(base::Bind(&SetLatch, &initialized)); |
30 EXPECT_TRUE(monitor.init_called()); | 30 EXPECT_TRUE(monitor.init_called()); |
31 EXPECT_FALSE(initialized); | 31 EXPECT_FALSE(initialized); |
32 monitor.MarkInitialized(); | 32 monitor.MarkInitialized(); |
33 EXPECT_TRUE(initialized); | 33 EXPECT_TRUE(initialized); |
34 } | 34 } |
35 | 35 |
36 TEST(StorageMonitorTest, DeviceAttachDetachNotifications) { | 36 TEST(StorageMonitorTest, DeviceAttachDetachNotifications) { |
37 TestStorageMonitor::Destroy(); | 37 TestStorageMonitor::Destroy(); |
38 base::MessageLoop message_loop; | 38 base::MessageLoop message_loop; |
39 const base::string16 kDeviceName = base::ASCIIToUTF16("media device"); | |
40 const std::string kDeviceId1 = "dcim:UUID:FFF0-0001"; | 39 const std::string kDeviceId1 = "dcim:UUID:FFF0-0001"; |
41 const std::string kDeviceId2 = "dcim:UUID:FFF0-0002"; | 40 const std::string kDeviceId2 = "dcim:UUID:FFF0-0002"; |
42 MockRemovableStorageObserver observer1; | 41 MockRemovableStorageObserver observer1; |
43 MockRemovableStorageObserver observer2; | 42 MockRemovableStorageObserver observer2; |
44 TestStorageMonitor monitor; | 43 TestStorageMonitor monitor; |
45 monitor.AddObserver(&observer1); | 44 monitor.AddObserver(&observer1); |
46 monitor.AddObserver(&observer2); | 45 monitor.AddObserver(&observer2); |
47 | 46 |
48 StorageInfo info(kDeviceId1, kDeviceName, FILE_PATH_LITERAL("path"), | 47 StorageInfo info(kDeviceId1, FILE_PATH_LITERAL("path"), base::string16(), |
49 base::string16(), base::string16(), base::string16(), 0); | 48 base::string16(), base::string16(), 0); |
50 monitor.receiver()->ProcessAttach(info); | 49 monitor.receiver()->ProcessAttach(info); |
51 message_loop.RunUntilIdle(); | 50 message_loop.RunUntilIdle(); |
52 | 51 |
53 EXPECT_EQ(kDeviceId1, observer1.last_attached().device_id()); | 52 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()); | 53 EXPECT_EQ(FILE_PATH_LITERAL("path"), observer1.last_attached().location()); |
56 EXPECT_EQ(kDeviceId1, observer2.last_attached().device_id()); | 54 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()); | 55 EXPECT_EQ(FILE_PATH_LITERAL("path"), observer2.last_attached().location()); |
59 EXPECT_EQ(1, observer1.attach_calls()); | 56 EXPECT_EQ(1, observer1.attach_calls()); |
60 EXPECT_EQ(0, observer1.detach_calls()); | 57 EXPECT_EQ(0, observer1.detach_calls()); |
61 | 58 |
62 monitor.receiver()->ProcessDetach(kDeviceId1); | 59 monitor.receiver()->ProcessDetach(kDeviceId1); |
63 monitor.receiver()->ProcessDetach(kDeviceId2); | 60 monitor.receiver()->ProcessDetach(kDeviceId2); |
64 message_loop.RunUntilIdle(); | 61 message_loop.RunUntilIdle(); |
65 | 62 |
66 EXPECT_EQ(kDeviceId1, observer1.last_detached().device_id()); | 63 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()); | 64 EXPECT_EQ(FILE_PATH_LITERAL("path"), observer1.last_detached().location()); |
69 EXPECT_EQ(kDeviceId1, observer2.last_detached().device_id()); | 65 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()); | 66 EXPECT_EQ(FILE_PATH_LITERAL("path"), observer2.last_detached().location()); |
72 | 67 |
73 EXPECT_EQ(1, observer1.attach_calls()); | 68 EXPECT_EQ(1, observer1.attach_calls()); |
74 EXPECT_EQ(1, observer2.attach_calls()); | 69 EXPECT_EQ(1, observer2.attach_calls()); |
75 | 70 |
76 // The kDeviceId2 won't be notified since it was never attached. | 71 // The kDeviceId2 won't be notified since it was never attached. |
77 EXPECT_EQ(1, observer1.detach_calls()); | 72 EXPECT_EQ(1, observer1.detach_calls()); |
78 EXPECT_EQ(1, observer2.detach_calls()); | 73 EXPECT_EQ(1, observer2.detach_calls()); |
79 | 74 |
80 monitor.RemoveObserver(&observer1); | 75 monitor.RemoveObserver(&observer1); |
81 monitor.RemoveObserver(&observer2); | 76 monitor.RemoveObserver(&observer2); |
82 } | 77 } |
83 | 78 |
84 TEST(StorageMonitorTest, GetAllAvailableStoragesEmpty) { | 79 TEST(StorageMonitorTest, GetAllAvailableStoragesEmpty) { |
85 TestStorageMonitor::Destroy(); | 80 TestStorageMonitor::Destroy(); |
86 base::MessageLoop message_loop; | 81 base::MessageLoop message_loop; |
87 TestStorageMonitor monitor; | 82 TestStorageMonitor monitor; |
88 std::vector<StorageInfo> devices = monitor.GetAllAvailableStorages(); | 83 std::vector<StorageInfo> devices = monitor.GetAllAvailableStorages(); |
89 EXPECT_EQ(0U, devices.size()); | 84 EXPECT_EQ(0U, devices.size()); |
90 } | 85 } |
91 | 86 |
92 TEST(StorageMonitorTest, GetAllAvailableStorageAttachDetach) { | 87 TEST(StorageMonitorTest, GetAllAvailableStorageAttachDetach) { |
93 TestStorageMonitor::Destroy(); | 88 TestStorageMonitor::Destroy(); |
94 base::MessageLoop message_loop; | 89 base::MessageLoop message_loop; |
95 TestStorageMonitor monitor; | 90 TestStorageMonitor monitor; |
96 const std::string kDeviceId1 = "dcim:UUID:FFF0-0042"; | 91 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")); | 92 const base::FilePath kDevicePath1(FILE_PATH_LITERAL("/testfoo")); |
99 StorageInfo info1(kDeviceId1, kDeviceName1, kDevicePath1.value(), | 93 StorageInfo info1(kDeviceId1, kDevicePath1.value(), base::string16(), |
100 base::string16(), base::string16(), base::string16(), 0); | 94 base::string16(), base::string16(), 0); |
101 monitor.receiver()->ProcessAttach(info1); | 95 monitor.receiver()->ProcessAttach(info1); |
102 message_loop.RunUntilIdle(); | 96 message_loop.RunUntilIdle(); |
103 std::vector<StorageInfo> devices = monitor.GetAllAvailableStorages(); | 97 std::vector<StorageInfo> devices = monitor.GetAllAvailableStorages(); |
104 ASSERT_EQ(1U, devices.size()); | 98 ASSERT_EQ(1U, devices.size()); |
105 EXPECT_EQ(kDeviceId1, devices[0].device_id()); | 99 EXPECT_EQ(kDeviceId1, devices[0].device_id()); |
106 EXPECT_EQ(kDeviceName1, devices[0].name()); | |
107 EXPECT_EQ(kDevicePath1.value(), devices[0].location()); | 100 EXPECT_EQ(kDevicePath1.value(), devices[0].location()); |
108 | 101 |
109 const std::string kDeviceId2 = "dcim:UUID:FFF0-0044"; | 102 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")); | 103 const base::FilePath kDevicePath2(FILE_PATH_LITERAL("/testbar")); |
112 StorageInfo info2(kDeviceId2, kDeviceName2, kDevicePath2.value(), | 104 StorageInfo info2(kDeviceId2, kDevicePath2.value(), base::string16(), |
113 base::string16(), base::string16(), base::string16(), 0); | 105 base::string16(), base::string16(), 0); |
114 monitor.receiver()->ProcessAttach(info2); | 106 monitor.receiver()->ProcessAttach(info2); |
115 message_loop.RunUntilIdle(); | 107 message_loop.RunUntilIdle(); |
116 devices = monitor.GetAllAvailableStorages(); | 108 devices = monitor.GetAllAvailableStorages(); |
117 ASSERT_EQ(2U, devices.size()); | 109 ASSERT_EQ(2U, devices.size()); |
118 EXPECT_EQ(kDeviceId1, devices[0].device_id()); | 110 EXPECT_EQ(kDeviceId1, devices[0].device_id()); |
119 EXPECT_EQ(kDeviceName1, devices[0].name()); | |
120 EXPECT_EQ(kDevicePath1.value(), devices[0].location()); | 111 EXPECT_EQ(kDevicePath1.value(), devices[0].location()); |
121 EXPECT_EQ(kDeviceId2, devices[1].device_id()); | 112 EXPECT_EQ(kDeviceId2, devices[1].device_id()); |
122 EXPECT_EQ(kDeviceName2, devices[1].name()); | |
123 EXPECT_EQ(kDevicePath2.value(), devices[1].location()); | 113 EXPECT_EQ(kDevicePath2.value(), devices[1].location()); |
124 | 114 |
125 monitor.receiver()->ProcessDetach(kDeviceId1); | 115 monitor.receiver()->ProcessDetach(kDeviceId1); |
126 message_loop.RunUntilIdle(); | 116 message_loop.RunUntilIdle(); |
127 devices = monitor.GetAllAvailableStorages(); | 117 devices = monitor.GetAllAvailableStorages(); |
128 ASSERT_EQ(1U, devices.size()); | 118 ASSERT_EQ(1U, devices.size()); |
129 EXPECT_EQ(kDeviceId2, devices[0].device_id()); | 119 EXPECT_EQ(kDeviceId2, devices[0].device_id()); |
130 EXPECT_EQ(kDeviceName2, devices[0].name()); | |
131 EXPECT_EQ(kDevicePath2.value(), devices[0].location()); | 120 EXPECT_EQ(kDevicePath2.value(), devices[0].location()); |
132 | 121 |
133 monitor.receiver()->ProcessDetach(kDeviceId2); | 122 monitor.receiver()->ProcessDetach(kDeviceId2); |
134 message_loop.RunUntilIdle(); | 123 message_loop.RunUntilIdle(); |
135 devices = monitor.GetAllAvailableStorages(); | 124 devices = monitor.GetAllAvailableStorages(); |
136 EXPECT_EQ(0U, devices.size()); | 125 EXPECT_EQ(0U, devices.size()); |
137 } | 126 } |
138 | 127 |
139 } // namespace storage_monitor | 128 } // namespace storage_monitor |
OLD | NEW |