OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "components/proximity_auth/bluetooth_connection.h" | |
6 | |
7 #include "base/message_loop/message_loop.h" | |
8 #include "base/numerics/safe_conversions.h" | |
9 #include "base/run_loop.h" | |
10 #include "components/proximity_auth/remote_device.h" | |
11 #include "components/proximity_auth/wire_message.h" | |
12 #include "device/bluetooth/bluetooth_adapter_factory.h" | |
13 #include "device/bluetooth/bluetooth_uuid.h" | |
14 #include "device/bluetooth/test/mock_bluetooth_adapter.h" | |
15 #include "device/bluetooth/test/mock_bluetooth_device.h" | |
16 #include "device/bluetooth/test/mock_bluetooth_socket.h" | |
17 #include "net/base/io_buffer.h" | |
18 #include "testing/gmock/include/gmock/gmock.h" | |
19 #include "testing/gtest/include/gtest/gtest.h" | |
20 | |
21 using testing::_; | |
22 using testing::NiceMock; | |
23 using testing::Ref; | |
24 using testing::Return; | |
25 using testing::SaveArg; | |
26 using testing::StrictMock; | |
27 | |
28 namespace proximity_auth { | |
29 namespace { | |
30 | |
31 const char kDeviceName[] = "Device name"; | |
32 const char kOtherDeviceName[] = "Other device name"; | |
33 | |
34 const char kBluetoothAddress[] = "11:22:33:44:55:66"; | |
35 const char kOtherBluetoothAddress[] = "AA:BB:CC:DD:EE:FF"; | |
36 | |
37 const char kSerializedMessage[] = "Yarrr, this be a serialized message. Yarr!"; | |
38 const int kSerializedMessageLength = strlen(kSerializedMessage); | |
39 | |
40 const char kUuid[] = "DEADBEEF-CAFE-FEED-FOOD-D15EA5EBEEF"; | |
41 | |
42 const RemoteDevice kRemoteDevice = {kDeviceName, kBluetoothAddress}; | |
43 | |
44 const int kReceiveBufferSize = 6; | |
45 const char kReceiveBufferContents[] = "bytes"; | |
46 | |
47 // Create a buffer for testing received data. | |
48 scoped_refptr<net::IOBuffer> CreateReceiveBuffer() { | |
49 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(kReceiveBufferSize); | |
50 memcpy(buffer->data(), kReceiveBufferContents, kReceiveBufferSize); | |
51 return buffer; | |
52 } | |
53 | |
54 class MockBluetoothConnection : public BluetoothConnection { | |
55 public: | |
56 MockBluetoothConnection() | |
57 : BluetoothConnection(kRemoteDevice, device::BluetoothUUID(kUuid)) {} | |
58 | |
59 // Bluetooth dependencies. | |
60 typedef device::BluetoothDevice::ConnectToServiceCallback | |
61 ConnectToServiceCallback; | |
62 typedef device::BluetoothDevice::ConnectToServiceErrorCallback | |
63 ConnectToServiceErrorCallback; | |
64 MOCK_METHOD4(ConnectToService, | |
65 void(device::BluetoothDevice* device, | |
66 const device::BluetoothUUID& uuid, | |
67 const ConnectToServiceCallback& callback, | |
68 const ConnectToServiceErrorCallback& error_callback)); | |
69 | |
70 // Calls back into the parent Connection class. | |
71 MOCK_METHOD1(SetStatusProxy, void(Status status)); | |
72 MOCK_METHOD1(OnBytesReceived, void(const std::string& bytes)); | |
73 MOCK_METHOD2(OnDidSendMessage, | |
74 void(const WireMessage& message, bool success)); | |
75 | |
76 virtual void SetStatus(Status status) OVERRIDE { | |
77 SetStatusProxy(status); | |
78 BluetoothConnection::SetStatus(status); | |
79 } | |
80 | |
81 using BluetoothConnection::Connect; | |
82 using BluetoothConnection::DeviceRemoved; | |
83 using BluetoothConnection::Disconnect; | |
84 | |
85 private: | |
86 DISALLOW_COPY_AND_ASSIGN(MockBluetoothConnection); | |
87 }; | |
88 | |
89 class TestWireMessage : public WireMessage { | |
90 public: | |
91 TestWireMessage() : WireMessage("permit id", "payload") {} | |
92 virtual ~TestWireMessage() {} | |
93 | |
94 virtual std::string Serialize() const OVERRIDE { return kSerializedMessage; } | |
95 | |
96 private: | |
97 DISALLOW_COPY_AND_ASSIGN(TestWireMessage); | |
98 }; | |
99 | |
100 } // namespace | |
101 | |
102 class ProximityAuthBluetoothConnectionTest : public testing::Test { | |
103 public: | |
104 ProximityAuthBluetoothConnectionTest() | |
105 : adapter_(new StrictMock<device::MockBluetoothAdapter>), | |
106 device_(adapter_.get(), 0, kDeviceName, kBluetoothAddress, true, true), | |
107 socket_(new StrictMock<device::MockBluetoothSocket>), | |
108 uuid_(kUuid) { | |
109 device::BluetoothAdapterFactory::SetAdapterForTesting(adapter_); | |
110 } | |
111 | |
112 // Transition the connection into an in-progress state. | |
113 void BeginConnecting(MockBluetoothConnection* connection) { | |
Tim Song
2014/09/22 23:30:38
check STATUS == DISCONNECTED
Ilya Sherman
2014/09/23 05:24:22
Done.
| |
114 EXPECT_CALL(*connection, SetStatusProxy(Connection::IN_PROGRESS)); | |
115 EXPECT_CALL(*adapter_, GetDevice(_)).WillOnce(Return(&device_)); | |
Tim Song
2014/09/22 23:30:39
use ON_CALL instead.
Ilya Sherman
2014/09/23 05:24:22
Done.
| |
116 EXPECT_CALL(*adapter_, AddObserver(connection)); | |
117 EXPECT_CALL(*connection, ConnectToService(&device_, uuid_, _, _)); | |
118 connection->Connect(); | |
Tim Song
2014/09/22 23:30:38
check status == CONNECTING
Ilya Sherman
2014/09/23 05:24:22
Done.
| |
119 } | |
120 | |
121 // Transition the connection into a connected state. | |
122 void Connect(MockBluetoothConnection* connection) { | |
123 device::BluetoothDevice::ConnectToServiceCallback callback; | |
124 EXPECT_CALL(*connection, SetStatusProxy(Connection::IN_PROGRESS)); | |
125 EXPECT_CALL(*adapter_, GetDevice(_)).WillOnce(Return(&device_)); | |
Tim Song
2014/09/22 23:30:39
use ON_CALL
Ilya Sherman
2014/09/23 05:24:22
Done.
| |
126 EXPECT_CALL(*adapter_, AddObserver(connection)); | |
127 EXPECT_CALL(*connection, ConnectToService(_, _, _, _)) | |
128 .WillOnce(SaveArg<2>(&callback)); | |
129 connection->Connect(); | |
130 ASSERT_FALSE(callback.is_null()); | |
131 | |
132 EXPECT_CALL(*connection, SetStatusProxy(Connection::CONNECTED)); | |
133 EXPECT_CALL(*socket_, Receive(_, _, _)); | |
134 callback.Run(socket_); | |
Tim Song
2014/09/22 23:30:39
Check status == CONNECTED
Ilya Sherman
2014/09/23 05:24:22
Done.
| |
135 } | |
136 | |
137 protected: | |
138 // Mocks used for verifying interactions with the Bluetooth subsystem. | |
139 scoped_refptr<StrictMock<device::MockBluetoothAdapter> > adapter_; | |
140 NiceMock<device::MockBluetoothDevice> device_; | |
141 scoped_refptr<StrictMock<device::MockBluetoothSocket> > socket_; | |
142 | |
143 device::BluetoothUUID uuid_; | |
144 | |
145 private: | |
146 base::MessageLoop message_loop_; | |
147 }; | |
148 | |
149 TEST_F(ProximityAuthBluetoothConnectionTest, Connect_ConnectionWasInProgress) { | |
150 // Create an in-progress connection. | |
151 StrictMock<MockBluetoothConnection> connection; | |
152 BeginConnecting(&connection); | |
153 | |
154 // A second call to Connect() should be ignored. | |
155 EXPECT_CALL(connection, SetStatusProxy(_)).Times(0); | |
156 connection.Connect(); | |
157 | |
158 // The connection cleans up after itself upon destruction. | |
159 EXPECT_CALL(*adapter_, RemoveObserver(&connection)); | |
160 } | |
161 | |
162 TEST_F(ProximityAuthBluetoothConnectionTest, Connect_ConnectionWasConnected) { | |
163 // Create a connected connection. | |
164 StrictMock<MockBluetoothConnection> connection; | |
165 Connect(&connection); | |
166 | |
167 // A second call to Connect() should be ignored. | |
168 EXPECT_CALL(connection, SetStatusProxy(_)).Times(0); | |
169 connection.Connect(); | |
170 | |
171 // The connection disconnects and unregisters as an observer upon destruction. | |
172 EXPECT_CALL(*socket_, Disconnect(_)); | |
173 EXPECT_CALL(*adapter_, RemoveObserver(&connection)); | |
174 } | |
175 | |
176 TEST_F(ProximityAuthBluetoothConnectionTest, Connect_NoBluetoothAdapter) { | |
177 // Some platforms do not support Bluetooth. This test is only meaningful on | |
178 // those platforms. | |
179 adapter_ = NULL; | |
180 if (device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) | |
181 return; | |
182 | |
183 StrictMock<MockBluetoothConnection> connection; | |
184 EXPECT_CALL(connection, SetStatusProxy(_)).Times(0); | |
185 connection.Connect(); | |
186 } | |
187 | |
188 TEST_F(ProximityAuthBluetoothConnectionTest, Connect_DeviceMissing) { | |
189 StrictMock<MockBluetoothConnection> connection; | |
190 | |
191 EXPECT_CALL(connection, SetStatusProxy(Connection::IN_PROGRESS)); | |
192 EXPECT_CALL(*adapter_, GetDevice(_)) | |
193 .WillOnce(Return(static_cast<device::BluetoothDevice*>(NULL))); | |
194 EXPECT_CALL(connection, SetStatusProxy(Connection::DISCONNECTED)); | |
195 connection.Connect(); | |
196 } | |
197 | |
198 TEST_F(ProximityAuthBluetoothConnectionTest, | |
199 Connect_DeviceRemovedWhileConnecting) { | |
200 // Create an in-progress connection. | |
201 StrictMock<MockBluetoothConnection> connection; | |
202 BeginConnecting(&connection); | |
203 | |
204 // Remove the device while the connection is in-progress. This should cause | |
205 // the connection to disconnect. | |
206 EXPECT_CALL(connection, SetStatusProxy(Connection::DISCONNECTED)); | |
207 EXPECT_CALL(*adapter_, RemoveObserver(&connection)); | |
208 connection.DeviceRemoved(adapter_.get(), &device_); | |
209 } | |
210 | |
211 TEST_F(ProximityAuthBluetoothConnectionTest, | |
212 Connect_OtherDeviceRemovedWhileConnecting) { | |
213 // Create an in-progress connection. | |
214 StrictMock<MockBluetoothConnection> connection; | |
215 BeginConnecting(&connection); | |
216 | |
217 // Remove a device other than the one that is being connected to. This should | |
218 // not have any effect on the connection. | |
219 NiceMock<device::MockBluetoothDevice> other_device( | |
220 adapter_.get(), 0, kOtherDeviceName, kOtherBluetoothAddress, true, true); | |
221 EXPECT_CALL(connection, SetStatusProxy(_)).Times(0); | |
222 connection.DeviceRemoved(adapter_.get(), &other_device); | |
223 | |
224 // The connection removes itself as an observer when it is destroyed. | |
225 EXPECT_CALL(*adapter_, RemoveObserver(&connection)); | |
226 } | |
227 | |
228 TEST_F(ProximityAuthBluetoothConnectionTest, Connect_ConnectionFails) { | |
229 StrictMock<MockBluetoothConnection> connection; | |
230 | |
231 device::BluetoothDevice::ConnectToServiceErrorCallback error_callback; | |
232 EXPECT_CALL(connection, SetStatusProxy(Connection::IN_PROGRESS)); | |
233 EXPECT_CALL(*adapter_, GetDevice(_)).WillOnce(Return(&device_)); | |
234 EXPECT_CALL(*adapter_, AddObserver(&connection)); | |
235 EXPECT_CALL(connection, ConnectToService(&device_, uuid_, _, _)) | |
236 .WillOnce(SaveArg<3>(&error_callback)); | |
237 connection.Connect(); | |
238 ASSERT_FALSE(error_callback.is_null()); | |
239 | |
240 EXPECT_CALL(connection, SetStatusProxy(Connection::DISCONNECTED)); | |
241 EXPECT_CALL(*adapter_, RemoveObserver(&connection)); | |
242 error_callback.Run("super descriptive error message"); | |
243 } | |
244 | |
245 TEST_F(ProximityAuthBluetoothConnectionTest, Connect_ConnectionSucceeds) { | |
246 StrictMock<MockBluetoothConnection> connection; | |
247 | |
248 device::BluetoothDevice::ConnectToServiceCallback callback; | |
249 EXPECT_CALL(connection, SetStatusProxy(Connection::IN_PROGRESS)); | |
250 EXPECT_CALL(*adapter_, GetDevice(_)).WillOnce(Return(&device_)); | |
251 EXPECT_CALL(*adapter_, AddObserver(&connection)); | |
252 EXPECT_CALL(connection, ConnectToService(&device_, uuid_, _, _)) | |
253 .WillOnce(SaveArg<2>(&callback)); | |
254 connection.Connect(); | |
Tim Song
2014/09/22 23:30:38
Why not just use the ProximityAuthBluetoothConnect
Ilya Sherman
2014/09/23 05:24:22
Because we need to save a reference to the callbac
Tim Song
2014/09/23 21:22:12
I don't understand. This function is almost line-b
Ilya Sherman
2014/09/23 22:07:07
Hmm, I suppose you're right. Okay, I've updated t
| |
255 ASSERT_FALSE(callback.is_null()); | |
256 | |
257 EXPECT_CALL(connection, SetStatusProxy(Connection::CONNECTED)); | |
258 EXPECT_CALL(*socket_, Receive(_, _, _)); | |
259 callback.Run(socket_); | |
260 | |
261 // The connection disconnects and unregisters as an observer upon destruction. | |
262 EXPECT_CALL(*socket_, Disconnect(_)); | |
263 EXPECT_CALL(*adapter_, RemoveObserver(&connection)); | |
264 } | |
265 | |
266 TEST_F(ProximityAuthBluetoothConnectionTest, | |
267 Connect_ConnectionSucceeds_ThenDeviceRemoved) { | |
268 StrictMock<MockBluetoothConnection> connection; | |
269 | |
270 device::BluetoothDevice::ConnectToServiceCallback callback; | |
271 EXPECT_CALL(connection, SetStatusProxy(Connection::IN_PROGRESS)); | |
272 EXPECT_CALL(*adapter_, GetDevice(_)).WillOnce(Return(&device_)); | |
273 EXPECT_CALL(*adapter_, AddObserver(&connection)); | |
274 EXPECT_CALL(connection, ConnectToService(&device_, uuid_, _, _)) | |
275 .WillOnce(SaveArg<2>(&callback)); | |
276 connection.Connect(); | |
277 ASSERT_FALSE(callback.is_null()); | |
278 | |
279 EXPECT_CALL(connection, SetStatusProxy(Connection::CONNECTED)); | |
280 EXPECT_CALL(*socket_, Receive(_, _, _)); | |
281 callback.Run(socket_); | |
282 | |
283 EXPECT_CALL(connection, SetStatusProxy(Connection::DISCONNECTED)); | |
284 EXPECT_CALL(*socket_, Disconnect(_)); | |
285 EXPECT_CALL(*adapter_, RemoveObserver(&connection)); | |
286 connection.DeviceRemoved(adapter_.get(), &device_); | |
287 } | |
288 | |
289 TEST_F(ProximityAuthBluetoothConnectionTest, | |
290 Connect_ConnectionSucceeds_ReceiveData) { | |
291 StrictMock<MockBluetoothConnection> connection; | |
292 | |
293 // Connect. | |
294 device::BluetoothDevice::ConnectToServiceCallback callback; | |
295 EXPECT_CALL(connection, SetStatusProxy(Connection::IN_PROGRESS)); | |
296 EXPECT_CALL(*adapter_, GetDevice(_)).WillOnce(Return(&device_)); | |
297 EXPECT_CALL(*adapter_, AddObserver(&connection)); | |
298 EXPECT_CALL(connection, ConnectToService(&device_, uuid_, _, _)) | |
299 .WillOnce(SaveArg<2>(&callback)); | |
300 connection.Connect(); | |
301 ASSERT_FALSE(callback.is_null()); | |
302 | |
303 device::BluetoothSocket::ReceiveCompletionCallback receive_callback; | |
304 EXPECT_CALL(connection, SetStatusProxy(Connection::CONNECTED)); | |
305 EXPECT_CALL(*socket_, Receive(_, _, _)) | |
306 .WillOnce(SaveArg<1>(&receive_callback)); | |
307 callback.Run(socket_); | |
308 ASSERT_FALSE(receive_callback.is_null()); | |
309 | |
310 // Receive some data. Once complete, the connection should re-register to be | |
311 // ready receive more data. | |
312 scoped_refptr<net::IOBuffer> buffer = CreateReceiveBuffer(); | |
313 EXPECT_CALL( | |
314 connection, | |
315 OnBytesReceived(std::string(kReceiveBufferContents, kReceiveBufferSize))); | |
316 EXPECT_CALL(*socket_, Receive(_, _, _)); | |
317 receive_callback.Run(kReceiveBufferSize, buffer); | |
318 base::RunLoop run_loop; | |
319 run_loop.RunUntilIdle(); | |
320 | |
321 // The connection disconnects and unregisters as an observer upon destruction. | |
322 EXPECT_CALL(*socket_, Disconnect(_)); | |
323 EXPECT_CALL(*adapter_, RemoveObserver(&connection)); | |
324 } | |
325 | |
326 TEST_F(ProximityAuthBluetoothConnectionTest, | |
327 Connect_ConnectionSucceeds_ReceiveDataAfterReceiveError) { | |
328 StrictMock<MockBluetoothConnection> connection; | |
329 | |
330 // Connect. | |
331 device::BluetoothDevice::ConnectToServiceCallback callback; | |
332 EXPECT_CALL(connection, SetStatusProxy(Connection::IN_PROGRESS)); | |
333 EXPECT_CALL(*adapter_, GetDevice(_)).WillOnce(Return(&device_)); | |
334 EXPECT_CALL(*adapter_, AddObserver(&connection)); | |
335 EXPECT_CALL(connection, ConnectToService(&device_, uuid_, _, _)) | |
336 .WillOnce(SaveArg<2>(&callback)); | |
337 connection.Connect(); | |
338 ASSERT_FALSE(callback.is_null()); | |
339 | |
340 device::BluetoothSocket::ReceiveErrorCompletionCallback | |
341 receive_error_callback; | |
342 EXPECT_CALL(connection, SetStatusProxy(Connection::CONNECTED)); | |
343 EXPECT_CALL(*socket_, Receive(_, _, _)) | |
344 .WillOnce(SaveArg<2>(&receive_error_callback)); | |
345 callback.Run(socket_); | |
346 ASSERT_FALSE(receive_error_callback.is_null()); | |
347 | |
348 // Simulate an error while receiving data. The connection should re-register | |
349 // to be ready receive more data despite the error. | |
350 device::BluetoothSocket::ReceiveCompletionCallback receive_callback; | |
351 EXPECT_CALL(*socket_, Receive(_, _, _)) | |
352 .WillOnce(SaveArg<1>(&receive_callback)); | |
353 receive_error_callback.Run(device::BluetoothSocket::kSystemError, | |
354 "The system is down. They're taking over!"); | |
355 base::RunLoop run_loop; | |
356 run_loop.RunUntilIdle(); | |
357 | |
358 // Receive some data. | |
359 scoped_refptr<net::IOBuffer> buffer = CreateReceiveBuffer(); | |
360 EXPECT_CALL( | |
361 connection, | |
362 OnBytesReceived(std::string(kReceiveBufferContents, kReceiveBufferSize))); | |
363 EXPECT_CALL(*socket_, Receive(_, _, _)); | |
364 receive_callback.Run(kReceiveBufferSize, buffer); | |
365 base::RunLoop run_loop2; | |
366 run_loop2.RunUntilIdle(); | |
367 | |
368 // The connection disconnects and unregisters as an observer upon destruction. | |
369 EXPECT_CALL(*socket_, Disconnect(_)); | |
370 EXPECT_CALL(*adapter_, RemoveObserver(&connection)); | |
371 } | |
372 | |
373 TEST_F(ProximityAuthBluetoothConnectionTest, | |
374 Disconnect_ConnectionWasAlreadyDisconnected) { | |
375 StrictMock<MockBluetoothConnection> connection; | |
376 EXPECT_CALL(connection, SetStatusProxy(_)).Times(0); | |
377 connection.Disconnect(); | |
378 } | |
379 | |
380 TEST_F(ProximityAuthBluetoothConnectionTest, | |
381 Disconnect_ConnectionWasInProgress) { | |
382 // Create an in-progress connection. | |
383 StrictMock<MockBluetoothConnection> connection; | |
384 BeginConnecting(&connection); | |
385 | |
386 EXPECT_CALL(connection, SetStatusProxy(Connection::DISCONNECTED)); | |
387 EXPECT_CALL(*adapter_, RemoveObserver(&connection)); | |
388 connection.Disconnect(); | |
389 } | |
390 | |
391 TEST_F(ProximityAuthBluetoothConnectionTest, | |
392 Disconnect_ConnectionWasConnected) { | |
393 // Create a connected connection. | |
394 StrictMock<MockBluetoothConnection> connection; | |
395 Connect(&connection); | |
396 | |
397 EXPECT_CALL(connection, SetStatusProxy(Connection::DISCONNECTED)); | |
398 EXPECT_CALL(*socket_, Disconnect(_)); | |
399 EXPECT_CALL(*adapter_, RemoveObserver(&connection)); | |
400 connection.Disconnect(); | |
401 } | |
402 | |
403 TEST_F(ProximityAuthBluetoothConnectionTest, | |
404 Connect_ThenDisconnectWhileInProgress_ThenBackingConnectionSucceeds) { | |
405 StrictMock<MockBluetoothConnection> connection; | |
406 device::BluetoothDevice::ConnectToServiceCallback callback; | |
407 EXPECT_CALL(connection, SetStatusProxy(Connection::IN_PROGRESS)); | |
408 EXPECT_CALL(*adapter_, GetDevice(_)).WillOnce(Return(&device_)); | |
409 EXPECT_CALL(*adapter_, AddObserver(&connection)); | |
410 EXPECT_CALL(connection, ConnectToService(&device_, uuid_, _, _)) | |
411 .WillOnce(SaveArg<2>(&callback)); | |
412 connection.Connect(); | |
Tim Song
2014/09/22 23:30:38
Why not use BeginConnection?
Ilya Sherman
2014/09/23 05:24:22
Same as before: We need to save a reference to the
| |
413 ASSERT_FALSE(callback.is_null()); | |
414 | |
415 EXPECT_CALL(connection, SetStatusProxy(Connection::DISCONNECTED)); | |
416 EXPECT_CALL(*adapter_, RemoveObserver(&connection)); | |
417 connection.Disconnect(); | |
418 | |
419 EXPECT_CALL(connection, SetStatusProxy(_)).Times(0); | |
420 EXPECT_CALL(*socket_, Receive(_, _, _)).Times(0); | |
421 callback.Run(socket_); | |
422 } | |
423 | |
424 TEST_F(ProximityAuthBluetoothConnectionTest, | |
425 SendMessage_SendsExpectedDataOverTheWire) { | |
426 // Create a connected connection. | |
427 StrictMock<MockBluetoothConnection> connection; | |
428 Connect(&connection); | |
429 | |
430 scoped_refptr<net::IOBuffer> buffer; | |
431 scoped_ptr<TestWireMessage> wire_message(new TestWireMessage); | |
432 EXPECT_CALL(*socket_, Send(_, kSerializedMessageLength, _, _)) | |
433 .WillOnce(SaveArg<0>(&buffer)); | |
434 connection.SendMessage(wire_message.PassAs<WireMessage>()); | |
435 ASSERT_TRUE(buffer.get()); | |
436 EXPECT_EQ(kSerializedMessage, | |
437 std::string(buffer->data(), kSerializedMessageLength)); | |
438 | |
439 // The connection disconnects and unregisters as an observer upon destruction. | |
440 EXPECT_CALL(*socket_, Disconnect(_)); | |
441 EXPECT_CALL(*adapter_, RemoveObserver(&connection)); | |
442 } | |
443 | |
444 TEST_F(ProximityAuthBluetoothConnectionTest, SendMessage_Success) { | |
445 // Create a connected connection. | |
446 StrictMock<MockBluetoothConnection> connection; | |
447 Connect(&connection); | |
448 | |
449 scoped_ptr<TestWireMessage> wire_message(new TestWireMessage); | |
450 // Ownership will be transfered below, so grab a reference here. | |
451 TestWireMessage* expected_wire_message = wire_message.get(); | |
452 | |
453 device::BluetoothSocket::SendCompletionCallback callback; | |
454 EXPECT_CALL(*socket_, Send(_, _, _, _)).WillOnce(SaveArg<2>(&callback)); | |
455 connection.SendMessage(wire_message.PassAs<WireMessage>()); | |
456 ASSERT_FALSE(callback.is_null()); | |
457 | |
458 EXPECT_CALL(connection, OnDidSendMessage(Ref(*expected_wire_message), true)); | |
459 callback.Run(kSerializedMessageLength); | |
460 | |
461 // The connection disconnects and unregisters as an observer upon destruction. | |
462 EXPECT_CALL(*socket_, Disconnect(_)); | |
463 EXPECT_CALL(*adapter_, RemoveObserver(&connection)); | |
464 } | |
465 | |
466 TEST_F(ProximityAuthBluetoothConnectionTest, SendMessage_Failure) { | |
467 // Create a connected connection. | |
468 StrictMock<MockBluetoothConnection> connection; | |
469 Connect(&connection); | |
470 | |
471 scoped_ptr<TestWireMessage> wire_message(new TestWireMessage); | |
472 // Ownership will be transfered below, so grab a reference here. | |
473 TestWireMessage* expected_wire_message = wire_message.get(); | |
474 | |
475 device::BluetoothSocket::ErrorCompletionCallback error_callback; | |
476 EXPECT_CALL(*socket_, Send(_, _, _, _)).WillOnce(SaveArg<3>(&error_callback)); | |
477 connection.SendMessage(wire_message.PassAs<WireMessage>()); | |
478 | |
479 ASSERT_FALSE(error_callback.is_null()); | |
480 EXPECT_CALL(connection, OnDidSendMessage(Ref(*expected_wire_message), false)); | |
481 EXPECT_CALL(connection, SetStatusProxy(Connection::DISCONNECTED)); | |
482 EXPECT_CALL(*socket_, Disconnect(_)); | |
483 EXPECT_CALL(*adapter_, RemoveObserver(&connection)); | |
484 error_callback.Run("The most helpful of error messages"); | |
485 } | |
486 | |
487 } // namespace proximity_auth | |
OLD | NEW |