| 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 "components/proximity_auth/bluetooth_connection.h" | 5 #include "components/proximity_auth/bluetooth_connection.h" |
| 6 | 6 |
| 7 #include <utility> |
| 8 |
| 7 #include "base/macros.h" | 9 #include "base/macros.h" |
| 8 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| 9 #include "base/numerics/safe_conversions.h" | 11 #include "base/numerics/safe_conversions.h" |
| 10 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
| 11 #include "components/proximity_auth/proximity_auth_test_util.h" | 13 #include "components/proximity_auth/proximity_auth_test_util.h" |
| 12 #include "components/proximity_auth/remote_device.h" | 14 #include "components/proximity_auth/remote_device.h" |
| 13 #include "components/proximity_auth/wire_message.h" | 15 #include "components/proximity_auth/wire_message.h" |
| 14 #include "device/bluetooth/bluetooth_adapter_factory.h" | 16 #include "device/bluetooth/bluetooth_adapter_factory.h" |
| 15 #include "device/bluetooth/bluetooth_uuid.h" | 17 #include "device/bluetooth/bluetooth_uuid.h" |
| 16 #include "device/bluetooth/test/mock_bluetooth_adapter.h" | 18 #include "device/bluetooth/test/mock_bluetooth_adapter.h" |
| (...skipping 377 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 394 TEST_F(ProximityAuthBluetoothConnectionTest, | 396 TEST_F(ProximityAuthBluetoothConnectionTest, |
| 395 SendMessage_SendsExpectedDataOverTheWire) { | 397 SendMessage_SendsExpectedDataOverTheWire) { |
| 396 // Create a connected connection. | 398 // Create a connected connection. |
| 397 StrictMock<MockBluetoothConnection> connection; | 399 StrictMock<MockBluetoothConnection> connection; |
| 398 Connect(&connection); | 400 Connect(&connection); |
| 399 | 401 |
| 400 scoped_refptr<net::IOBuffer> buffer; | 402 scoped_refptr<net::IOBuffer> buffer; |
| 401 scoped_ptr<TestWireMessage> wire_message(new TestWireMessage); | 403 scoped_ptr<TestWireMessage> wire_message(new TestWireMessage); |
| 402 EXPECT_CALL(*socket_, Send(_, kSerializedMessageLength, _, _)) | 404 EXPECT_CALL(*socket_, Send(_, kSerializedMessageLength, _, _)) |
| 403 .WillOnce(SaveArg<0>(&buffer)); | 405 .WillOnce(SaveArg<0>(&buffer)); |
| 404 connection.SendMessage(wire_message.Pass()); | 406 connection.SendMessage(std::move(wire_message)); |
| 405 ASSERT_TRUE(buffer.get()); | 407 ASSERT_TRUE(buffer.get()); |
| 406 EXPECT_EQ(kSerializedMessage, | 408 EXPECT_EQ(kSerializedMessage, |
| 407 std::string(buffer->data(), kSerializedMessageLength)); | 409 std::string(buffer->data(), kSerializedMessageLength)); |
| 408 | 410 |
| 409 // The connection disconnects and unregisters as an observer upon destruction. | 411 // The connection disconnects and unregisters as an observer upon destruction. |
| 410 EXPECT_CALL(*socket_, Disconnect(_)); | 412 EXPECT_CALL(*socket_, Disconnect(_)); |
| 411 EXPECT_CALL(*adapter_, RemoveObserver(&connection)); | 413 EXPECT_CALL(*adapter_, RemoveObserver(&connection)); |
| 412 } | 414 } |
| 413 | 415 |
| 414 TEST_F(ProximityAuthBluetoothConnectionTest, SendMessage_Success) { | 416 TEST_F(ProximityAuthBluetoothConnectionTest, SendMessage_Success) { |
| 415 // Create a connected connection. | 417 // Create a connected connection. |
| 416 StrictMock<MockBluetoothConnection> connection; | 418 StrictMock<MockBluetoothConnection> connection; |
| 417 Connect(&connection); | 419 Connect(&connection); |
| 418 | 420 |
| 419 scoped_ptr<TestWireMessage> wire_message(new TestWireMessage); | 421 scoped_ptr<TestWireMessage> wire_message(new TestWireMessage); |
| 420 // Ownership will be transfered below, so grab a reference here. | 422 // Ownership will be transfered below, so grab a reference here. |
| 421 TestWireMessage* expected_wire_message = wire_message.get(); | 423 TestWireMessage* expected_wire_message = wire_message.get(); |
| 422 | 424 |
| 423 device::BluetoothSocket::SendCompletionCallback callback; | 425 device::BluetoothSocket::SendCompletionCallback callback; |
| 424 EXPECT_CALL(*socket_, Send(_, _, _, _)).WillOnce(SaveArg<2>(&callback)); | 426 EXPECT_CALL(*socket_, Send(_, _, _, _)).WillOnce(SaveArg<2>(&callback)); |
| 425 connection.SendMessage(wire_message.Pass()); | 427 connection.SendMessage(std::move(wire_message)); |
| 426 ASSERT_FALSE(callback.is_null()); | 428 ASSERT_FALSE(callback.is_null()); |
| 427 | 429 |
| 428 EXPECT_CALL(connection, OnDidSendMessage(Ref(*expected_wire_message), true)); | 430 EXPECT_CALL(connection, OnDidSendMessage(Ref(*expected_wire_message), true)); |
| 429 callback.Run(kSerializedMessageLength); | 431 callback.Run(kSerializedMessageLength); |
| 430 | 432 |
| 431 // The connection disconnects and unregisters as an observer upon destruction. | 433 // The connection disconnects and unregisters as an observer upon destruction. |
| 432 EXPECT_CALL(*socket_, Disconnect(_)); | 434 EXPECT_CALL(*socket_, Disconnect(_)); |
| 433 EXPECT_CALL(*adapter_, RemoveObserver(&connection)); | 435 EXPECT_CALL(*adapter_, RemoveObserver(&connection)); |
| 434 } | 436 } |
| 435 | 437 |
| 436 TEST_F(ProximityAuthBluetoothConnectionTest, SendMessage_Failure) { | 438 TEST_F(ProximityAuthBluetoothConnectionTest, SendMessage_Failure) { |
| 437 // Create a connected connection. | 439 // Create a connected connection. |
| 438 StrictMock<MockBluetoothConnection> connection; | 440 StrictMock<MockBluetoothConnection> connection; |
| 439 Connect(&connection); | 441 Connect(&connection); |
| 440 | 442 |
| 441 scoped_ptr<TestWireMessage> wire_message(new TestWireMessage); | 443 scoped_ptr<TestWireMessage> wire_message(new TestWireMessage); |
| 442 // Ownership will be transfered below, so grab a reference here. | 444 // Ownership will be transfered below, so grab a reference here. |
| 443 TestWireMessage* expected_wire_message = wire_message.get(); | 445 TestWireMessage* expected_wire_message = wire_message.get(); |
| 444 | 446 |
| 445 device::BluetoothSocket::ErrorCompletionCallback error_callback; | 447 device::BluetoothSocket::ErrorCompletionCallback error_callback; |
| 446 EXPECT_CALL(*socket_, Send(_, _, _, _)).WillOnce(SaveArg<3>(&error_callback)); | 448 EXPECT_CALL(*socket_, Send(_, _, _, _)).WillOnce(SaveArg<3>(&error_callback)); |
| 447 connection.SendMessage(wire_message.Pass()); | 449 connection.SendMessage(std::move(wire_message)); |
| 448 | 450 |
| 449 ASSERT_FALSE(error_callback.is_null()); | 451 ASSERT_FALSE(error_callback.is_null()); |
| 450 EXPECT_CALL(connection, OnDidSendMessage(Ref(*expected_wire_message), false)); | 452 EXPECT_CALL(connection, OnDidSendMessage(Ref(*expected_wire_message), false)); |
| 451 EXPECT_CALL(connection, SetStatusProxy(Connection::DISCONNECTED)); | 453 EXPECT_CALL(connection, SetStatusProxy(Connection::DISCONNECTED)); |
| 452 EXPECT_CALL(*socket_, Disconnect(_)); | 454 EXPECT_CALL(*socket_, Disconnect(_)); |
| 453 EXPECT_CALL(*adapter_, RemoveObserver(&connection)); | 455 EXPECT_CALL(*adapter_, RemoveObserver(&connection)); |
| 454 error_callback.Run("The most helpful of error messages"); | 456 error_callback.Run("The most helpful of error messages"); |
| 455 } | 457 } |
| 456 | 458 |
| 457 TEST_F(ProximityAuthBluetoothConnectionTest, DeviceChanged_Disconnected) { | 459 TEST_F(ProximityAuthBluetoothConnectionTest, DeviceChanged_Disconnected) { |
| (...skipping 19 matching lines...) Expand all Loading... |
| 477 EXPECT_TRUE(connection.IsConnected()); | 479 EXPECT_TRUE(connection.IsConnected()); |
| 478 connection.DeviceChanged(adapter_.get(), &device_); | 480 connection.DeviceChanged(adapter_.get(), &device_); |
| 479 EXPECT_TRUE(connection.IsConnected()); | 481 EXPECT_TRUE(connection.IsConnected()); |
| 480 | 482 |
| 481 // The connection disconnects and unregisters as an observer upon destruction. | 483 // The connection disconnects and unregisters as an observer upon destruction. |
| 482 EXPECT_CALL(*socket_, Disconnect(_)); | 484 EXPECT_CALL(*socket_, Disconnect(_)); |
| 483 EXPECT_CALL(*adapter_, RemoveObserver(&connection)); | 485 EXPECT_CALL(*adapter_, RemoveObserver(&connection)); |
| 484 } | 486 } |
| 485 | 487 |
| 486 } // namespace proximity_auth | 488 } // namespace proximity_auth |
| OLD | NEW |