Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(29)

Side by Side Diff: device/usb/mojo/device_manager_impl_unittest.cc

Issue 1946063002: Replace DeviceManager::GetDeviceChanges with a client interface. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@proper_stubs
Patch Set: Rebase.d Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 "device/usb/mojo/device_manager_impl.h" 5 #include "device/usb/mojo/device_manager_impl.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <memory> 9 #include <memory>
10 #include <set> 10 #include <set>
11 #include <string> 11 #include <string>
12 #include <utility> 12 #include <utility>
13 13
14 #include "base/barrier_closure.h"
14 #include "base/bind.h" 15 #include "base/bind.h"
15 #include "base/macros.h" 16 #include "base/macros.h"
16 #include "base/message_loop/message_loop.h" 17 #include "base/message_loop/message_loop.h"
17 #include "base/run_loop.h" 18 #include "base/run_loop.h"
18 #include "base/thread_task_runner_handle.h" 19 #include "base/thread_task_runner_handle.h"
19 #include "device/core/mock_device_client.h" 20 #include "device/core/mock_device_client.h"
20 #include "device/usb/mock_usb_device.h" 21 #include "device/usb/mock_usb_device.h"
21 #include "device/usb/mock_usb_device_handle.h" 22 #include "device/usb/mock_usb_device_handle.h"
22 #include "device/usb/mock_usb_service.h" 23 #include "device/usb/mock_usb_service.h"
23 #include "device/usb/mojo/device_impl.h" 24 #include "device/usb/mojo/device_impl.h"
24 #include "device/usb/mojo/mock_permission_provider.h" 25 #include "device/usb/mojo/mock_permission_provider.h"
25 #include "testing/gtest/include/gtest/gtest.h" 26 #include "testing/gtest/include/gtest/gtest.h"
26 27
27 using ::testing::Invoke; 28 using ::testing::Invoke;
28 using ::testing::_; 29 using ::testing::_;
29 30
30 namespace device { 31 namespace device {
31 namespace usb { 32 namespace usb {
32 33
33 namespace { 34 namespace {
34 35
36 ACTION_P2(ExpectGuidAndThen, expected_guid, callback) {
37 ASSERT_TRUE(arg0);
38 EXPECT_EQ(expected_guid, arg0->guid);
39 if (!callback.is_null())
40 callback.Run();
41 };
42
35 class USBDeviceManagerImplTest : public testing::Test { 43 class USBDeviceManagerImplTest : public testing::Test {
36 public: 44 public:
37 USBDeviceManagerImplTest() : message_loop_(new base::MessageLoop) {} 45 USBDeviceManagerImplTest() : message_loop_(new base::MessageLoop) {}
38 ~USBDeviceManagerImplTest() override {} 46 ~USBDeviceManagerImplTest() override {}
39 47
40 protected: 48 protected:
41 DeviceManagerPtr ConnectToDeviceManager() { 49 DeviceManagerPtr ConnectToDeviceManager() {
42 DeviceManagerPtr device_manager; 50 DeviceManagerPtr device_manager;
43 DeviceManagerImpl::Create(permission_provider_.GetWeakPtr(), 51 DeviceManagerImpl::Create(permission_provider_.GetWeakPtr(),
44 mojo::GetProxy(&device_manager)); 52 mojo::GetProxy(&device_manager));
45 return device_manager; 53 return device_manager;
46 } 54 }
47 55
48 MockDeviceClient device_client_; 56 MockDeviceClient device_client_;
49 57
50 private: 58 private:
51 MockPermissionProvider permission_provider_; 59 MockPermissionProvider permission_provider_;
52 std::unique_ptr<base::MessageLoop> message_loop_; 60 std::unique_ptr<base::MessageLoop> message_loop_;
53 }; 61 };
54 62
63 class MockDeviceManagerClient : public DeviceManagerClient {
64 public:
65 MockDeviceManagerClient() : m_binding(this) {}
66 ~MockDeviceManagerClient() {}
67
68 DeviceManagerClientPtr CreateInterfacePtrAndBind() {
69 return m_binding.CreateInterfacePtrAndBind();
70 }
71
72 MOCK_METHOD1(DoOnDeviceAdded, void(DeviceInfo*));
73 void OnDeviceAdded(DeviceInfoPtr device_info) {
74 DoOnDeviceAdded(device_info.get());
75 }
76
77 MOCK_METHOD1(DoOnDeviceRemoved, void(DeviceInfo*));
78 void OnDeviceRemoved(DeviceInfoPtr device_info) {
79 DoOnDeviceRemoved(device_info.get());
80 }
81
82 private:
83 mojo::Binding<DeviceManagerClient> m_binding;
84 };
85
55 void ExpectDevicesAndThen(const std::set<std::string>& expected_guids, 86 void ExpectDevicesAndThen(const std::set<std::string>& expected_guids,
56 const base::Closure& continuation, 87 const base::Closure& continuation,
57 mojo::Array<DeviceInfoPtr> results) { 88 mojo::Array<DeviceInfoPtr> results) {
58 EXPECT_EQ(expected_guids.size(), results.size()); 89 EXPECT_EQ(expected_guids.size(), results.size());
59 std::set<std::string> actual_guids; 90 std::set<std::string> actual_guids;
60 for (size_t i = 0; i < results.size(); ++i) 91 for (size_t i = 0; i < results.size(); ++i)
61 actual_guids.insert(results[i]->guid); 92 actual_guids.insert(results[i]->guid);
62 EXPECT_EQ(expected_guids, actual_guids); 93 EXPECT_EQ(expected_guids, actual_guids);
63 continuation.Run(); 94 continuation.Run();
64 } 95 }
65 96
66 void ExpectDeviceChangesAndThen(
67 const std::set<std::string>& expected_added_guids,
68 const std::set<std::string>& expected_removed_guids,
69 const base::Closure& continuation,
70 DeviceChangeNotificationPtr results) {
71 EXPECT_EQ(expected_added_guids.size(), results->devices_added.size());
72 std::set<std::string> actual_added_guids;
73 for (size_t i = 0; i < results->devices_added.size(); ++i)
74 actual_added_guids.insert(results->devices_added[i]->guid);
75 EXPECT_EQ(expected_added_guids, actual_added_guids);
76 EXPECT_EQ(expected_removed_guids.size(), results->devices_removed.size());
77 std::set<std::string> actual_removed_guids;
78 for (size_t i = 0; i < results->devices_removed.size(); ++i)
79 actual_removed_guids.insert(results->devices_removed[i]->guid);
80 EXPECT_EQ(expected_removed_guids, actual_removed_guids);
81 continuation.Run();
82 }
83
84 void ExpectDeviceInfoAndThen(const std::string& expected_guid, 97 void ExpectDeviceInfoAndThen(const std::string& expected_guid,
85 const base::Closure& continuation, 98 const base::Closure& continuation,
86 DeviceInfoPtr device_info) { 99 DeviceInfoPtr device_info) {
87 ASSERT_TRUE(device_info); 100 ASSERT_TRUE(device_info);
88 EXPECT_EQ(expected_guid, device_info->guid); 101 EXPECT_EQ(expected_guid, device_info->guid);
89 continuation.Run(); 102 continuation.Run();
90 } 103 }
91 104
92 } // namespace 105 } // namespace
93 106
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
147 device_manager->GetDevice("not a real guid", mojo::GetProxy(&bad_device)); 160 device_manager->GetDevice("not a real guid", mojo::GetProxy(&bad_device));
148 161
149 { 162 {
150 base::RunLoop loop; 163 base::RunLoop loop;
151 bad_device.set_connection_error_handler(loop.QuitClosure()); 164 bad_device.set_connection_error_handler(loop.QuitClosure());
152 loop.Run(); 165 loop.Run();
153 } 166 }
154 } 167 }
155 168
156 // Test requesting device enumeration updates with GetDeviceChanges. 169 // Test requesting device enumeration updates with GetDeviceChanges.
157 TEST_F(USBDeviceManagerImplTest, GetDeviceChanges) { 170 TEST_F(USBDeviceManagerImplTest, Client) {
158 scoped_refptr<MockUsbDevice> device0 = 171 scoped_refptr<MockUsbDevice> device0 =
159 new MockUsbDevice(0x1234, 0x5678, "ACME", "Frobinator", "ABCDEF"); 172 new MockUsbDevice(0x1234, 0x5678, "ACME", "Frobinator", "ABCDEF");
160 scoped_refptr<MockUsbDevice> device1 = 173 scoped_refptr<MockUsbDevice> device1 =
161 new MockUsbDevice(0x1234, 0x5679, "ACME", "Frobinator+", "GHIJKL"); 174 new MockUsbDevice(0x1234, 0x5679, "ACME", "Frobinator+", "GHIJKL");
162 scoped_refptr<MockUsbDevice> device2 = 175 scoped_refptr<MockUsbDevice> device2 =
163 new MockUsbDevice(0x1234, 0x567a, "ACME", "Frobinator Mk II", "MNOPQR"); 176 new MockUsbDevice(0x1234, 0x567a, "ACME", "Frobinator Mk II", "MNOPQR");
164 scoped_refptr<MockUsbDevice> device3 = 177 scoped_refptr<MockUsbDevice> device3 =
165 new MockUsbDevice(0x1234, 0x567b, "ACME", "Frobinator Xtreme", "STUVWX"); 178 new MockUsbDevice(0x1234, 0x567b, "ACME", "Frobinator Xtreme", "STUVWX");
166 179
167 device_client_.usb_service()->AddDevice(device0); 180 device_client_.usb_service()->AddDevice(device0);
168 181
169 DeviceManagerPtr device_manager = ConnectToDeviceManager(); 182 DeviceManagerPtr device_manager = ConnectToDeviceManager();
183 MockDeviceManagerClient mock_client;
184 device_manager->SetClient(mock_client.CreateInterfacePtrAndBind());
170 185
171 { 186 {
172 // Call GetDevices once to make sure the device manager is up and running 187 // Call GetDevices once to make sure the device manager is up and running
173 // or else we could end up waiting forever for device changes as the next 188 // and the client is set or else we could block forever waiting for calls.
174 // block races with the ServiceThreadHelper startup.
175 std::set<std::string> guids; 189 std::set<std::string> guids;
176 guids.insert(device0->guid()); 190 guids.insert(device0->guid());
177 base::RunLoop loop; 191 base::RunLoop loop;
178 device_manager->GetDevices( 192 device_manager->GetDevices(
179 nullptr, base::Bind(&ExpectDevicesAndThen, guids, loop.QuitClosure())); 193 nullptr, base::Bind(&ExpectDevicesAndThen, guids, loop.QuitClosure()));
180 loop.Run(); 194 loop.Run();
181 } 195 }
182 196
183 device_client_.usb_service()->AddDevice(device1); 197 device_client_.usb_service()->AddDevice(device1);
184 device_client_.usb_service()->AddDevice(device2); 198 device_client_.usb_service()->AddDevice(device2);
185 device_client_.usb_service()->RemoveDevice(device1); 199 device_client_.usb_service()->RemoveDevice(device1);
186
187 {
188 std::set<std::string> added_guids;
189 std::set<std::string> removed_guids;
190 added_guids.insert(device2->guid());
191 base::RunLoop loop;
192 device_manager->GetDeviceChanges(base::Bind(&ExpectDeviceChangesAndThen,
193 added_guids, removed_guids,
194 loop.QuitClosure()));
195 loop.Run();
196 }
197
198 device_client_.usb_service()->RemoveDevice(device0); 200 device_client_.usb_service()->RemoveDevice(device0);
199 device_client_.usb_service()->RemoveDevice(device2); 201 device_client_.usb_service()->RemoveDevice(device2);
200 device_client_.usb_service()->AddDevice(device3); 202 device_client_.usb_service()->AddDevice(device3);
201 203
202 { 204 {
203 std::set<std::string> added_guids;
204 std::set<std::string> removed_guids;
205 added_guids.insert(device3->guid());
206 removed_guids.insert(device0->guid());
207 removed_guids.insert(device2->guid());
208 base::RunLoop loop; 205 base::RunLoop loop;
209 device_manager->GetDeviceChanges(base::Bind(&ExpectDeviceChangesAndThen, 206 base::Closure barrier = base::BarrierClosure(6, loop.QuitClosure());
210 added_guids, removed_guids, 207 testing::InSequence s;
211 loop.QuitClosure())); 208 EXPECT_CALL(mock_client, DoOnDeviceAdded(_))
212 loop.Run(); 209 .WillOnce(ExpectGuidAndThen(device1->guid(), barrier))
213 } 210 .WillOnce(ExpectGuidAndThen(device2->guid(), barrier));
214 211 EXPECT_CALL(mock_client, DoOnDeviceRemoved(_))
215 { 212 .WillOnce(ExpectGuidAndThen(device1->guid(), barrier))
216 std::set<std::string> added_guids; 213 .WillOnce(ExpectGuidAndThen(device0->guid(), barrier))
217 std::set<std::string> removed_guids; 214 .WillOnce(ExpectGuidAndThen(device2->guid(), barrier));
218 added_guids.insert(device0->guid()); 215 EXPECT_CALL(mock_client, DoOnDeviceAdded(_))
219 base::RunLoop loop; 216 .WillOnce(ExpectGuidAndThen(device3->guid(), barrier));
220 device_manager->GetDeviceChanges(base::Bind(&ExpectDeviceChangesAndThen,
221 added_guids, removed_guids,
222 loop.QuitClosure()));
223 base::ThreadTaskRunnerHandle::Get()->PostTask(
224 FROM_HERE,
225 base::Bind(&MockUsbService::AddDevice,
226 base::Unretained(device_client_.usb_service()), device0));
227 loop.Run(); 217 loop.Run();
228 } 218 }
229 } 219 }
230 220
231 } // namespace usb 221 } // namespace usb
232 } // namespace device 222 } // namespace device
OLDNEW
« no previous file with comments | « device/usb/mojo/device_manager_impl.cc ('k') | device/usb/public/interfaces/device_manager.mojom » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698