OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/browser/storage_monitor/mock_removable_storage_observer.h" | 8 #include "chrome/browser/storage_monitor/mock_removable_storage_observer.h" |
9 #include "chrome/browser/storage_monitor/storage_monitor.h" | 9 #include "chrome/browser/storage_monitor/storage_monitor.h" |
10 #include "chrome/browser/storage_monitor/test_storage_monitor.h" | 10 #include "chrome/browser/storage_monitor/test_storage_monitor.h" |
(...skipping 27 matching lines...) Expand all Loading... |
38 base::MessageLoop message_loop; | 38 base::MessageLoop message_loop; |
39 const string16 kDeviceName = ASCIIToUTF16("media device"); | 39 const string16 kDeviceName = ASCIIToUTF16("media device"); |
40 const std::string kDeviceId1 = "dcim:UUID:FFF0-0001"; | 40 const std::string kDeviceId1 = "dcim:UUID:FFF0-0001"; |
41 const std::string kDeviceId2 = "dcim:UUID:FFF0-0002"; | 41 const std::string kDeviceId2 = "dcim:UUID:FFF0-0002"; |
42 MockRemovableStorageObserver observer1; | 42 MockRemovableStorageObserver observer1; |
43 MockRemovableStorageObserver observer2; | 43 MockRemovableStorageObserver observer2; |
44 test::TestStorageMonitor monitor; | 44 test::TestStorageMonitor monitor; |
45 monitor.AddObserver(&observer1); | 45 monitor.AddObserver(&observer1); |
46 monitor.AddObserver(&observer2); | 46 monitor.AddObserver(&observer2); |
47 | 47 |
48 StorageInfo info(kDeviceId1, kDeviceName, FILE_PATH_LITERAL("path"), | 48 StorageInfo info(kDeviceId1, FILE_PATH_LITERAL("path"), base::string16(), |
49 string16(), string16(), string16(), 0); | 49 base::string16(), base::string16(), 0); |
50 monitor.receiver()->ProcessAttach(info); | 50 monitor.receiver()->ProcessAttach(info); |
51 message_loop.RunUntilIdle(); | 51 message_loop.RunUntilIdle(); |
52 | 52 |
53 EXPECT_EQ(kDeviceId1, observer1.last_attached().device_id()); | 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()); | 54 EXPECT_EQ(FILE_PATH_LITERAL("path"), observer1.last_attached().location()); |
56 EXPECT_EQ(kDeviceId1, observer2.last_attached().device_id()); | 55 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()); | 56 EXPECT_EQ(FILE_PATH_LITERAL("path"), observer2.last_attached().location()); |
59 EXPECT_EQ(1, observer1.attach_calls()); | 57 EXPECT_EQ(1, observer1.attach_calls()); |
60 EXPECT_EQ(0, observer1.detach_calls()); | 58 EXPECT_EQ(0, observer1.detach_calls()); |
61 | 59 |
62 monitor.receiver()->ProcessDetach(kDeviceId1); | 60 monitor.receiver()->ProcessDetach(kDeviceId1); |
63 monitor.receiver()->ProcessDetach(kDeviceId2); | 61 monitor.receiver()->ProcessDetach(kDeviceId2); |
64 message_loop.RunUntilIdle(); | 62 message_loop.RunUntilIdle(); |
65 | 63 |
66 EXPECT_EQ(kDeviceId1, observer1.last_detached().device_id()); | 64 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()); | 65 EXPECT_EQ(FILE_PATH_LITERAL("path"), observer1.last_detached().location()); |
69 EXPECT_EQ(kDeviceId1, observer2.last_detached().device_id()); | 66 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()); | 67 EXPECT_EQ(FILE_PATH_LITERAL("path"), observer2.last_detached().location()); |
72 | 68 |
73 EXPECT_EQ(1, observer1.attach_calls()); | 69 EXPECT_EQ(1, observer1.attach_calls()); |
74 EXPECT_EQ(1, observer2.attach_calls()); | 70 EXPECT_EQ(1, observer2.attach_calls()); |
75 | 71 |
76 // The kDeviceId2 won't be notified since it was never attached. | 72 // The kDeviceId2 won't be notified since it was never attached. |
77 EXPECT_EQ(1, observer1.detach_calls()); | 73 EXPECT_EQ(1, observer1.detach_calls()); |
78 EXPECT_EQ(1, observer2.detach_calls()); | 74 EXPECT_EQ(1, observer2.detach_calls()); |
79 | 75 |
80 monitor.RemoveObserver(&observer1); | 76 monitor.RemoveObserver(&observer1); |
81 monitor.RemoveObserver(&observer2); | 77 monitor.RemoveObserver(&observer2); |
82 } | 78 } |
83 | 79 |
84 TEST(StorageMonitorTest, GetAllAvailableStoragesEmpty) { | 80 TEST(StorageMonitorTest, GetAllAvailableStoragesEmpty) { |
85 test::TestStorageMonitor::RemoveSingleton(); | 81 test::TestStorageMonitor::RemoveSingleton(); |
86 base::MessageLoop message_loop; | 82 base::MessageLoop message_loop; |
87 test::TestStorageMonitor monitor; | 83 test::TestStorageMonitor monitor; |
88 std::vector<StorageInfo> devices = monitor.GetAllAvailableStorages(); | 84 std::vector<StorageInfo> devices = monitor.GetAllAvailableStorages(); |
89 EXPECT_EQ(0U, devices.size()); | 85 EXPECT_EQ(0U, devices.size()); |
90 } | 86 } |
91 | 87 |
92 TEST(StorageMonitorTest, GetAllAvailableStorageAttachDetach) { | 88 TEST(StorageMonitorTest, GetAllAvailableStorageAttachDetach) { |
93 test::TestStorageMonitor::RemoveSingleton(); | 89 test::TestStorageMonitor::RemoveSingleton(); |
94 base::MessageLoop message_loop; | 90 base::MessageLoop message_loop; |
95 test::TestStorageMonitor monitor; | 91 test::TestStorageMonitor monitor; |
96 const std::string kDeviceId1 = "dcim:UUID:FFF0-0042"; | 92 const std::string kDeviceId1 = "dcim:UUID:FFF0-0042"; |
97 const string16 kDeviceName1 = ASCIIToUTF16("test"); | 93 const string16 kDeviceName1 = ASCIIToUTF16("test"); |
98 const base::FilePath kDevicePath1(FILE_PATH_LITERAL("/testfoo")); | 94 const base::FilePath kDevicePath1(FILE_PATH_LITERAL("/testfoo")); |
99 StorageInfo info1(kDeviceId1, kDeviceName1, kDevicePath1.value(), | 95 StorageInfo info1(kDeviceId1, kDevicePath1.value(), base::string16(), |
100 string16(), string16(), string16(), 0); | 96 base::string16(), base::string16(), 0); |
101 monitor.receiver()->ProcessAttach(info1); | 97 monitor.receiver()->ProcessAttach(info1); |
102 message_loop.RunUntilIdle(); | 98 message_loop.RunUntilIdle(); |
103 std::vector<StorageInfo> devices = monitor.GetAllAvailableStorages(); | 99 std::vector<StorageInfo> devices = monitor.GetAllAvailableStorages(); |
104 ASSERT_EQ(1U, devices.size()); | 100 ASSERT_EQ(1U, devices.size()); |
105 EXPECT_EQ(kDeviceId1, devices[0].device_id()); | 101 EXPECT_EQ(kDeviceId1, devices[0].device_id()); |
106 EXPECT_EQ(kDeviceName1, devices[0].name()); | |
107 EXPECT_EQ(kDevicePath1.value(), devices[0].location()); | 102 EXPECT_EQ(kDevicePath1.value(), devices[0].location()); |
108 | 103 |
109 const std::string kDeviceId2 = "dcim:UUID:FFF0-0044"; | 104 const std::string kDeviceId2 = "dcim:UUID:FFF0-0044"; |
110 const string16 kDeviceName2 = ASCIIToUTF16("test2"); | 105 const string16 kDeviceName2 = ASCIIToUTF16("test2"); |
111 const base::FilePath kDevicePath2(FILE_PATH_LITERAL("/testbar")); | 106 const base::FilePath kDevicePath2(FILE_PATH_LITERAL("/testbar")); |
112 StorageInfo info2(kDeviceId2, kDeviceName2, kDevicePath2.value(), | 107 StorageInfo info2(kDeviceId2, kDevicePath2.value(), base::string16(), |
113 string16(), string16(), string16(), 0); | 108 base::string16(), base::string16(), 0); |
114 monitor.receiver()->ProcessAttach(info2); | 109 monitor.receiver()->ProcessAttach(info2); |
115 message_loop.RunUntilIdle(); | 110 message_loop.RunUntilIdle(); |
116 devices = monitor.GetAllAvailableStorages(); | 111 devices = monitor.GetAllAvailableStorages(); |
117 ASSERT_EQ(2U, devices.size()); | 112 ASSERT_EQ(2U, devices.size()); |
118 EXPECT_EQ(kDeviceId1, devices[0].device_id()); | 113 EXPECT_EQ(kDeviceId1, devices[0].device_id()); |
119 EXPECT_EQ(kDeviceName1, devices[0].name()); | |
120 EXPECT_EQ(kDevicePath1.value(), devices[0].location()); | 114 EXPECT_EQ(kDevicePath1.value(), devices[0].location()); |
121 EXPECT_EQ(kDeviceId2, devices[1].device_id()); | 115 EXPECT_EQ(kDeviceId2, devices[1].device_id()); |
122 EXPECT_EQ(kDeviceName2, devices[1].name()); | |
123 EXPECT_EQ(kDevicePath2.value(), devices[1].location()); | 116 EXPECT_EQ(kDevicePath2.value(), devices[1].location()); |
124 | 117 |
125 monitor.receiver()->ProcessDetach(kDeviceId1); | 118 monitor.receiver()->ProcessDetach(kDeviceId1); |
126 message_loop.RunUntilIdle(); | 119 message_loop.RunUntilIdle(); |
127 devices = monitor.GetAllAvailableStorages(); | 120 devices = monitor.GetAllAvailableStorages(); |
128 ASSERT_EQ(1U, devices.size()); | 121 ASSERT_EQ(1U, devices.size()); |
129 EXPECT_EQ(kDeviceId2, devices[0].device_id()); | 122 EXPECT_EQ(kDeviceId2, devices[0].device_id()); |
130 EXPECT_EQ(kDeviceName2, devices[0].name()); | |
131 EXPECT_EQ(kDevicePath2.value(), devices[0].location()); | 123 EXPECT_EQ(kDevicePath2.value(), devices[0].location()); |
132 | 124 |
133 monitor.receiver()->ProcessDetach(kDeviceId2); | 125 monitor.receiver()->ProcessDetach(kDeviceId2); |
134 message_loop.RunUntilIdle(); | 126 message_loop.RunUntilIdle(); |
135 devices = monitor.GetAllAvailableStorages(); | 127 devices = monitor.GetAllAvailableStorages(); |
136 EXPECT_EQ(0U, devices.size()); | 128 EXPECT_EQ(0U, devices.size()); |
137 } | 129 } |
138 | 130 |
139 } // namespace chrome | 131 } // namespace chrome |
OLD | NEW |