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

Side by Side Diff: components/proximity_auth/bluetooth_connection_unittest.cc

Issue 2561203002: Migrate weave-related classes from proximity_auth/ble to cryptauth/ble. (Closed)
Patch Set: Rebase. Created 4 years 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 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> 7 #include <utility>
8 8
9 #include "base/macros.h" 9 #include "base/macros.h"
10 #include "base/message_loop/message_loop.h" 10 #include "base/message_loop/message_loop.h"
11 #include "base/numerics/safe_conversions.h" 11 #include "base/numerics/safe_conversions.h"
12 #include "base/run_loop.h" 12 #include "base/run_loop.h"
13 #include "components/cryptauth/cryptauth_test_util.h"
13 #include "components/cryptauth/remote_device.h" 14 #include "components/cryptauth/remote_device.h"
14 #include "components/proximity_auth/proximity_auth_test_util.h" 15 #include "components/cryptauth/wire_message.h"
15 #include "components/proximity_auth/wire_message.h"
16 #include "device/bluetooth/bluetooth_adapter_factory.h" 16 #include "device/bluetooth/bluetooth_adapter_factory.h"
17 #include "device/bluetooth/bluetooth_uuid.h" 17 #include "device/bluetooth/bluetooth_uuid.h"
18 #include "device/bluetooth/test/mock_bluetooth_adapter.h" 18 #include "device/bluetooth/test/mock_bluetooth_adapter.h"
19 #include "device/bluetooth/test/mock_bluetooth_device.h" 19 #include "device/bluetooth/test/mock_bluetooth_device.h"
20 #include "device/bluetooth/test/mock_bluetooth_socket.h" 20 #include "device/bluetooth/test/mock_bluetooth_socket.h"
21 #include "net/base/io_buffer.h" 21 #include "net/base/io_buffer.h"
22 #include "testing/gmock/include/gmock/gmock.h" 22 #include "testing/gmock/include/gmock/gmock.h"
23 #include "testing/gtest/include/gtest/gtest.h" 23 #include "testing/gtest/include/gtest/gtest.h"
24 24
25 using testing::_; 25 using testing::_;
(...skipping 21 matching lines...) Expand all
47 // Create a buffer for testing received data. 47 // Create a buffer for testing received data.
48 scoped_refptr<net::IOBuffer> CreateReceiveBuffer() { 48 scoped_refptr<net::IOBuffer> CreateReceiveBuffer() {
49 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(kReceiveBufferSize); 49 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(kReceiveBufferSize);
50 memcpy(buffer->data(), kReceiveBufferContents, kReceiveBufferSize); 50 memcpy(buffer->data(), kReceiveBufferContents, kReceiveBufferSize);
51 return buffer; 51 return buffer;
52 } 52 }
53 53
54 class MockBluetoothConnection : public BluetoothConnection { 54 class MockBluetoothConnection : public BluetoothConnection {
55 public: 55 public:
56 MockBluetoothConnection() 56 MockBluetoothConnection()
57 : BluetoothConnection(CreateClassicRemoteDeviceForTest(), 57 : BluetoothConnection(cryptauth::CreateClassicRemoteDeviceForTest(),
58 device::BluetoothUUID(kUuid)) {} 58 device::BluetoothUUID(kUuid)) {}
59 59
60 // Calls back into the parent Connection class. 60 // Calls back into the parent Connection class.
61 MOCK_METHOD1(SetStatusProxy, void(Status status)); 61 MOCK_METHOD1(SetStatusProxy, void(Status status));
62 MOCK_METHOD1(OnBytesReceived, void(const std::string& bytes)); 62 MOCK_METHOD1(OnBytesReceived, void(const std::string& bytes));
63 MOCK_METHOD2(OnDidSendMessage, 63 MOCK_METHOD2(OnDidSendMessage,
64 void(const WireMessage& message, bool success)); 64 void(const cryptauth::WireMessage& message, bool success));
65 65
66 void SetStatus(Status status) override { 66 void SetStatus(Status status) override {
67 SetStatusProxy(status); 67 SetStatusProxy(status);
68 BluetoothConnection::SetStatus(status); 68 BluetoothConnection::SetStatus(status);
69 } 69 }
70 70
71 using BluetoothConnection::status; 71 using BluetoothConnection::status;
72 using BluetoothConnection::Connect; 72 using BluetoothConnection::Connect;
73 using BluetoothConnection::DeviceChanged; 73 using BluetoothConnection::DeviceChanged;
74 using BluetoothConnection::DeviceRemoved; 74 using BluetoothConnection::DeviceRemoved;
75 using BluetoothConnection::Disconnect; 75 using BluetoothConnection::Disconnect;
76 76
77 private: 77 private:
78 DISALLOW_COPY_AND_ASSIGN(MockBluetoothConnection); 78 DISALLOW_COPY_AND_ASSIGN(MockBluetoothConnection);
79 }; 79 };
80 80
81 class TestWireMessage : public WireMessage { 81 class TestWireMessage : public cryptauth::WireMessage {
82 public: 82 public:
83 TestWireMessage() : WireMessage("permit id", "payload") {} 83 TestWireMessage() : cryptauth::WireMessage("permit id", "payload") {}
84 ~TestWireMessage() override {} 84 ~TestWireMessage() override {}
85 85
86 std::string Serialize() const override { return kSerializedMessage; } 86 std::string Serialize() const override { return kSerializedMessage; }
87 87
88 private: 88 private:
89 DISALLOW_COPY_AND_ASSIGN(TestWireMessage); 89 DISALLOW_COPY_AND_ASSIGN(TestWireMessage);
90 }; 90 };
91 91
92 } // namespace 92 } // namespace
93 93
94 class ProximityAuthBluetoothConnectionTest : public testing::Test { 94 class ProximityAuthBluetoothConnectionTest : public testing::Test {
95 public: 95 public:
96 ProximityAuthBluetoothConnectionTest() 96 ProximityAuthBluetoothConnectionTest()
97 : adapter_(new device::MockBluetoothAdapter), 97 : adapter_(new device::MockBluetoothAdapter),
98 device_(adapter_.get(), 98 device_(adapter_.get(),
99 0, 99 0,
100 kTestRemoteDeviceName, 100 cryptauth::kTestRemoteDeviceName,
101 kTestRemoteDeviceBluetoothAddress, 101 cryptauth::kTestRemoteDeviceBluetoothAddress,
102 true, 102 true,
103 true), 103 true),
104 socket_(new StrictMock<device::MockBluetoothSocket>), 104 socket_(new StrictMock<device::MockBluetoothSocket>),
105 uuid_(kUuid) { 105 uuid_(kUuid) {
106 device::BluetoothAdapterFactory::SetAdapterForTesting(adapter_); 106 device::BluetoothAdapterFactory::SetAdapterForTesting(adapter_);
107 107
108 // Suppress uninteresting Gmock call warnings. 108 // Suppress uninteresting Gmock call warnings.
109 EXPECT_CALL(*adapter_, GetDevice(_)).Times(AnyNumber()); 109 EXPECT_CALL(*adapter_, GetDevice(_)).Times(AnyNumber());
110 } 110 }
111 111
112 // Transition the connection into an in-progress state. 112 // Transition the connection into an in-progress state.
113 void BeginConnecting(MockBluetoothConnection* connection) { 113 void BeginConnecting(MockBluetoothConnection* connection) {
114 EXPECT_EQ(Connection::DISCONNECTED, connection->status()); 114 EXPECT_EQ(cryptauth::Connection::DISCONNECTED, connection->status());
115 ON_CALL(device_, IsConnected()).WillByDefault(Return(false)); 115 ON_CALL(device_, IsConnected()).WillByDefault(Return(false));
116 116
117 ON_CALL(*adapter_, GetDevice(_)).WillByDefault(Return(&device_)); 117 ON_CALL(*adapter_, GetDevice(_)).WillByDefault(Return(&device_));
118 EXPECT_CALL(*connection, SetStatusProxy(Connection::IN_PROGRESS)); 118 EXPECT_CALL(*connection,
119 SetStatusProxy(cryptauth::Connection::IN_PROGRESS));
119 EXPECT_CALL(*adapter_, AddObserver(connection)); 120 EXPECT_CALL(*adapter_, AddObserver(connection));
120 EXPECT_CALL(device_, ConnectToServiceInsecurely(uuid_, _, _)); 121 EXPECT_CALL(device_, ConnectToServiceInsecurely(uuid_, _, _));
121 connection->Connect(); 122 connection->Connect();
122 123
123 EXPECT_EQ(Connection::IN_PROGRESS, connection->status()); 124 EXPECT_EQ(cryptauth::Connection::IN_PROGRESS, connection->status());
124 } 125 }
125 126
126 // Transition the connection into a connected state. 127 // Transition the connection into a connected state.
127 // Saves the success and error callbacks passed into OnReceive(), which can be 128 // Saves the success and error callbacks passed into OnReceive(), which can be
128 // accessed via receive_callback() and receive_success_callback(). 129 // accessed via receive_callback() and receive_success_callback().
129 void Connect(MockBluetoothConnection* connection) { 130 void Connect(MockBluetoothConnection* connection) {
130 EXPECT_EQ(Connection::DISCONNECTED, connection->status()); 131 EXPECT_EQ(cryptauth::Connection::DISCONNECTED, connection->status());
131 132
132 device::BluetoothDevice::ConnectToServiceCallback callback; 133 device::BluetoothDevice::ConnectToServiceCallback callback;
133 ON_CALL(*adapter_, GetDevice(_)).WillByDefault(Return(&device_)); 134 ON_CALL(*adapter_, GetDevice(_)).WillByDefault(Return(&device_));
134 EXPECT_CALL(*connection, SetStatusProxy(Connection::IN_PROGRESS)); 135 EXPECT_CALL(*connection,
136 SetStatusProxy(cryptauth::Connection::IN_PROGRESS));
135 EXPECT_CALL(*adapter_, AddObserver(connection)); 137 EXPECT_CALL(*adapter_, AddObserver(connection));
136 EXPECT_CALL(device_, ConnectToServiceInsecurely(_, _, _)) 138 EXPECT_CALL(device_, ConnectToServiceInsecurely(_, _, _))
137 .WillOnce(SaveArg<1>(&callback)); 139 .WillOnce(SaveArg<1>(&callback));
138 connection->Connect(); 140 connection->Connect();
139 ASSERT_FALSE(callback.is_null()); 141 ASSERT_FALSE(callback.is_null());
140 142
141 EXPECT_CALL(*connection, SetStatusProxy(Connection::CONNECTED)); 143 EXPECT_CALL(*connection, SetStatusProxy(cryptauth::Connection::CONNECTED));
142 EXPECT_CALL(*socket_, Receive(_, _, _)) 144 EXPECT_CALL(*socket_, Receive(_, _, _))
143 .WillOnce(DoAll(SaveArg<1>(&receive_callback_), 145 .WillOnce(DoAll(SaveArg<1>(&receive_callback_),
144 SaveArg<2>(&receive_error_callback_))); 146 SaveArg<2>(&receive_error_callback_)));
145 callback.Run(socket_); 147 callback.Run(socket_);
146 148
147 EXPECT_EQ(Connection::CONNECTED, connection->status()); 149 EXPECT_EQ(cryptauth::Connection::CONNECTED, connection->status());
148 ON_CALL(device_, IsConnected()).WillByDefault(Return(true)); 150 ON_CALL(device_, IsConnected()).WillByDefault(Return(true));
149 } 151 }
150 152
151 device::BluetoothSocket::ReceiveCompletionCallback* receive_callback() { 153 device::BluetoothSocket::ReceiveCompletionCallback* receive_callback() {
152 return &receive_callback_; 154 return &receive_callback_;
153 } 155 }
154 device::BluetoothSocket::ReceiveErrorCompletionCallback* 156 device::BluetoothSocket::ReceiveErrorCompletionCallback*
155 receive_error_callback() { 157 receive_error_callback() {
156 return &receive_error_callback_; 158 return &receive_error_callback_;
157 } 159 }
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
208 210
209 StrictMock<MockBluetoothConnection> connection; 211 StrictMock<MockBluetoothConnection> connection;
210 EXPECT_CALL(connection, SetStatusProxy(_)).Times(0); 212 EXPECT_CALL(connection, SetStatusProxy(_)).Times(0);
211 connection.Connect(); 213 connection.Connect();
212 } 214 }
213 215
214 TEST_F(ProximityAuthBluetoothConnectionTest, Connect_DeviceMissing) { 216 TEST_F(ProximityAuthBluetoothConnectionTest, Connect_DeviceMissing) {
215 StrictMock<MockBluetoothConnection> connection; 217 StrictMock<MockBluetoothConnection> connection;
216 218
217 ON_CALL(*adapter_, GetDevice(_)).WillByDefault(Return(nullptr)); 219 ON_CALL(*adapter_, GetDevice(_)).WillByDefault(Return(nullptr));
218 EXPECT_CALL(connection, SetStatusProxy(Connection::IN_PROGRESS)); 220 EXPECT_CALL(connection, SetStatusProxy(cryptauth::Connection::IN_PROGRESS));
219 EXPECT_CALL(connection, SetStatusProxy(Connection::DISCONNECTED)); 221 EXPECT_CALL(connection, SetStatusProxy(cryptauth::Connection::DISCONNECTED));
220 connection.Connect(); 222 connection.Connect();
221 } 223 }
222 224
223 TEST_F(ProximityAuthBluetoothConnectionTest, 225 TEST_F(ProximityAuthBluetoothConnectionTest,
224 Connect_DeviceRemovedWhileConnecting) { 226 Connect_DeviceRemovedWhileConnecting) {
225 // Create an in-progress connection. 227 // Create an in-progress connection.
226 StrictMock<MockBluetoothConnection> connection; 228 StrictMock<MockBluetoothConnection> connection;
227 BeginConnecting(&connection); 229 BeginConnecting(&connection);
228 230
229 // Remove the device while the connection is in-progress. This should cause 231 // Remove the device while the connection is in-progress. This should cause
230 // the connection to disconnect. 232 // the connection to disconnect.
231 EXPECT_CALL(connection, SetStatusProxy(Connection::DISCONNECTED)); 233 EXPECT_CALL(connection, SetStatusProxy(cryptauth::Connection::DISCONNECTED));
232 EXPECT_CALL(*adapter_, RemoveObserver(&connection)); 234 EXPECT_CALL(*adapter_, RemoveObserver(&connection));
233 connection.DeviceRemoved(adapter_.get(), &device_); 235 connection.DeviceRemoved(adapter_.get(), &device_);
234 } 236 }
235 237
236 TEST_F(ProximityAuthBluetoothConnectionTest, 238 TEST_F(ProximityAuthBluetoothConnectionTest,
237 Connect_OtherDeviceRemovedWhileConnecting) { 239 Connect_OtherDeviceRemovedWhileConnecting) {
238 // Create an in-progress connection. 240 // Create an in-progress connection.
239 StrictMock<MockBluetoothConnection> connection; 241 StrictMock<MockBluetoothConnection> connection;
240 BeginConnecting(&connection); 242 BeginConnecting(&connection);
241 243
242 // Remove a device other than the one that is being connected to. This should 244 // Remove a device other than the one that is being connected to. This should
243 // not have any effect on the connection. 245 // not have any effect on the connection.
244 NiceMock<device::MockBluetoothDevice> other_device( 246 NiceMock<device::MockBluetoothDevice> other_device(
245 adapter_.get(), 0, kOtherDeviceName, kOtherBluetoothAddress, true, true); 247 adapter_.get(), 0, kOtherDeviceName, kOtherBluetoothAddress, true, true);
246 EXPECT_CALL(connection, SetStatusProxy(_)).Times(0); 248 EXPECT_CALL(connection, SetStatusProxy(_)).Times(0);
247 connection.DeviceRemoved(adapter_.get(), &other_device); 249 connection.DeviceRemoved(adapter_.get(), &other_device);
248 250
249 // The connection removes itself as an observer when it is destroyed. 251 // The connection removes itself as an observer when it is destroyed.
250 EXPECT_CALL(*adapter_, RemoveObserver(&connection)); 252 EXPECT_CALL(*adapter_, RemoveObserver(&connection));
251 } 253 }
252 254
253 TEST_F(ProximityAuthBluetoothConnectionTest, Connect_ConnectionFails) { 255 TEST_F(ProximityAuthBluetoothConnectionTest, Connect_ConnectionFails) {
254 StrictMock<MockBluetoothConnection> connection; 256 StrictMock<MockBluetoothConnection> connection;
255 257
256 device::BluetoothDevice::ConnectToServiceErrorCallback error_callback; 258 device::BluetoothDevice::ConnectToServiceErrorCallback error_callback;
257 ON_CALL(*adapter_, GetDevice(_)).WillByDefault(Return(&device_)); 259 ON_CALL(*adapter_, GetDevice(_)).WillByDefault(Return(&device_));
258 EXPECT_CALL(connection, SetStatusProxy(Connection::IN_PROGRESS)); 260 EXPECT_CALL(connection, SetStatusProxy(cryptauth::Connection::IN_PROGRESS));
259 EXPECT_CALL(*adapter_, AddObserver(&connection)); 261 EXPECT_CALL(*adapter_, AddObserver(&connection));
260 EXPECT_CALL(device_, ConnectToServiceInsecurely(uuid_, _, _)) 262 EXPECT_CALL(device_, ConnectToServiceInsecurely(uuid_, _, _))
261 .WillOnce(SaveArg<2>(&error_callback)); 263 .WillOnce(SaveArg<2>(&error_callback));
262 connection.Connect(); 264 connection.Connect();
263 ASSERT_FALSE(error_callback.is_null()); 265 ASSERT_FALSE(error_callback.is_null());
264 266
265 EXPECT_CALL(connection, SetStatusProxy(Connection::DISCONNECTED)); 267 EXPECT_CALL(connection, SetStatusProxy(cryptauth::Connection::DISCONNECTED));
266 EXPECT_CALL(*adapter_, RemoveObserver(&connection)); 268 EXPECT_CALL(*adapter_, RemoveObserver(&connection));
267 error_callback.Run("super descriptive error message"); 269 error_callback.Run("super descriptive error message");
268 } 270 }
269 271
270 TEST_F(ProximityAuthBluetoothConnectionTest, Connect_ConnectionSucceeds) { 272 TEST_F(ProximityAuthBluetoothConnectionTest, Connect_ConnectionSucceeds) {
271 StrictMock<MockBluetoothConnection> connection; 273 StrictMock<MockBluetoothConnection> connection;
272 Connect(&connection); 274 Connect(&connection);
273 275
274 // The connection disconnects and unregisters as an observer upon destruction. 276 // The connection disconnects and unregisters as an observer upon destruction.
275 EXPECT_CALL(*socket_, Disconnect(_)); 277 EXPECT_CALL(*socket_, Disconnect(_));
276 EXPECT_CALL(*adapter_, RemoveObserver(&connection)); 278 EXPECT_CALL(*adapter_, RemoveObserver(&connection));
277 } 279 }
278 280
279 TEST_F(ProximityAuthBluetoothConnectionTest, 281 TEST_F(ProximityAuthBluetoothConnectionTest,
280 Connect_ConnectionSucceeds_ThenDeviceRemoved) { 282 Connect_ConnectionSucceeds_ThenDeviceRemoved) {
281 StrictMock<MockBluetoothConnection> connection; 283 StrictMock<MockBluetoothConnection> connection;
282 Connect(&connection); 284 Connect(&connection);
283 285
284 EXPECT_CALL(connection, SetStatusProxy(Connection::DISCONNECTED)); 286 EXPECT_CALL(connection, SetStatusProxy(cryptauth::Connection::DISCONNECTED));
285 EXPECT_CALL(*socket_, Disconnect(_)); 287 EXPECT_CALL(*socket_, Disconnect(_));
286 EXPECT_CALL(*adapter_, RemoveObserver(&connection)); 288 EXPECT_CALL(*adapter_, RemoveObserver(&connection));
287 connection.DeviceRemoved(adapter_.get(), &device_); 289 connection.DeviceRemoved(adapter_.get(), &device_);
288 } 290 }
289 291
290 TEST_F(ProximityAuthBluetoothConnectionTest, 292 TEST_F(ProximityAuthBluetoothConnectionTest,
291 Connect_ConnectionSucceeds_ReceiveData) { 293 Connect_ConnectionSucceeds_ReceiveData) {
292 StrictMock<MockBluetoothConnection> connection; 294 StrictMock<MockBluetoothConnection> connection;
293 Connect(&connection); 295 Connect(&connection);
294 ASSERT_TRUE(receive_callback() && !receive_callback()->is_null()); 296 ASSERT_TRUE(receive_callback() && !receive_callback()->is_null());
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
347 EXPECT_CALL(connection, SetStatusProxy(_)).Times(0); 349 EXPECT_CALL(connection, SetStatusProxy(_)).Times(0);
348 connection.Disconnect(); 350 connection.Disconnect();
349 } 351 }
350 352
351 TEST_F(ProximityAuthBluetoothConnectionTest, 353 TEST_F(ProximityAuthBluetoothConnectionTest,
352 Disconnect_ConnectionWasInProgress) { 354 Disconnect_ConnectionWasInProgress) {
353 // Create an in-progress connection. 355 // Create an in-progress connection.
354 StrictMock<MockBluetoothConnection> connection; 356 StrictMock<MockBluetoothConnection> connection;
355 BeginConnecting(&connection); 357 BeginConnecting(&connection);
356 358
357 EXPECT_CALL(connection, SetStatusProxy(Connection::DISCONNECTED)); 359 EXPECT_CALL(connection, SetStatusProxy(cryptauth::Connection::DISCONNECTED));
358 EXPECT_CALL(*adapter_, RemoveObserver(&connection)); 360 EXPECT_CALL(*adapter_, RemoveObserver(&connection));
359 connection.Disconnect(); 361 connection.Disconnect();
360 } 362 }
361 363
362 TEST_F(ProximityAuthBluetoothConnectionTest, 364 TEST_F(ProximityAuthBluetoothConnectionTest,
363 Disconnect_ConnectionWasConnected) { 365 Disconnect_ConnectionWasConnected) {
364 // Create a connected connection. 366 // Create a connected connection.
365 StrictMock<MockBluetoothConnection> connection; 367 StrictMock<MockBluetoothConnection> connection;
366 Connect(&connection); 368 Connect(&connection);
367 369
368 EXPECT_CALL(connection, SetStatusProxy(Connection::DISCONNECTED)); 370 EXPECT_CALL(connection, SetStatusProxy(cryptauth::Connection::DISCONNECTED));
369 EXPECT_CALL(*socket_, Disconnect(_)); 371 EXPECT_CALL(*socket_, Disconnect(_));
370 EXPECT_CALL(*adapter_, RemoveObserver(&connection)); 372 EXPECT_CALL(*adapter_, RemoveObserver(&connection));
371 connection.Disconnect(); 373 connection.Disconnect();
372 } 374 }
373 375
374 TEST_F(ProximityAuthBluetoothConnectionTest, 376 TEST_F(ProximityAuthBluetoothConnectionTest,
375 Connect_ThenDisconnectWhileInProgress_ThenBackingConnectionSucceeds) { 377 Connect_ThenDisconnectWhileInProgress_ThenBackingConnectionSucceeds) {
376 StrictMock<MockBluetoothConnection> connection; 378 StrictMock<MockBluetoothConnection> connection;
377 device::BluetoothDevice::ConnectToServiceCallback callback; 379 device::BluetoothDevice::ConnectToServiceCallback callback;
378 ON_CALL(*adapter_, GetDevice(_)).WillByDefault(Return(&device_)); 380 ON_CALL(*adapter_, GetDevice(_)).WillByDefault(Return(&device_));
379 EXPECT_CALL(connection, SetStatusProxy(Connection::IN_PROGRESS)); 381 EXPECT_CALL(connection, SetStatusProxy(cryptauth::Connection::IN_PROGRESS));
380 EXPECT_CALL(*adapter_, AddObserver(&connection)); 382 EXPECT_CALL(*adapter_, AddObserver(&connection));
381 EXPECT_CALL(device_, ConnectToServiceInsecurely(uuid_, _, _)) 383 EXPECT_CALL(device_, ConnectToServiceInsecurely(uuid_, _, _))
382 .WillOnce(SaveArg<1>(&callback)); 384 .WillOnce(SaveArg<1>(&callback));
383 connection.Connect(); 385 connection.Connect();
384 ASSERT_FALSE(callback.is_null()); 386 ASSERT_FALSE(callback.is_null());
385 387
386 EXPECT_CALL(connection, SetStatusProxy(Connection::DISCONNECTED)); 388 EXPECT_CALL(connection, SetStatusProxy(cryptauth::Connection::DISCONNECTED));
387 EXPECT_CALL(*adapter_, RemoveObserver(&connection)); 389 EXPECT_CALL(*adapter_, RemoveObserver(&connection));
388 connection.Disconnect(); 390 connection.Disconnect();
389 391
390 EXPECT_CALL(connection, SetStatusProxy(_)).Times(0); 392 EXPECT_CALL(connection, SetStatusProxy(_)).Times(0);
391 EXPECT_CALL(*socket_, Receive(_, _, _)).Times(0); 393 EXPECT_CALL(*socket_, Receive(_, _, _)).Times(0);
392 callback.Run(socket_); 394 callback.Run(socket_);
393 } 395 }
394 396
395 TEST_F(ProximityAuthBluetoothConnectionTest, 397 TEST_F(ProximityAuthBluetoothConnectionTest,
396 SendMessage_SendsExpectedDataOverTheWire) { 398 SendMessage_SendsExpectedDataOverTheWire) {
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
442 std::unique_ptr<TestWireMessage> wire_message(new TestWireMessage); 444 std::unique_ptr<TestWireMessage> wire_message(new TestWireMessage);
443 // Ownership will be transfered below, so grab a reference here. 445 // Ownership will be transfered below, so grab a reference here.
444 TestWireMessage* expected_wire_message = wire_message.get(); 446 TestWireMessage* expected_wire_message = wire_message.get();
445 447
446 device::BluetoothSocket::ErrorCompletionCallback error_callback; 448 device::BluetoothSocket::ErrorCompletionCallback error_callback;
447 EXPECT_CALL(*socket_, Send(_, _, _, _)).WillOnce(SaveArg<3>(&error_callback)); 449 EXPECT_CALL(*socket_, Send(_, _, _, _)).WillOnce(SaveArg<3>(&error_callback));
448 connection.SendMessage(std::move(wire_message)); 450 connection.SendMessage(std::move(wire_message));
449 451
450 ASSERT_FALSE(error_callback.is_null()); 452 ASSERT_FALSE(error_callback.is_null());
451 EXPECT_CALL(connection, OnDidSendMessage(Ref(*expected_wire_message), false)); 453 EXPECT_CALL(connection, OnDidSendMessage(Ref(*expected_wire_message), false));
452 EXPECT_CALL(connection, SetStatusProxy(Connection::DISCONNECTED)); 454 EXPECT_CALL(connection, SetStatusProxy(cryptauth::Connection::DISCONNECTED));
453 EXPECT_CALL(*socket_, Disconnect(_)); 455 EXPECT_CALL(*socket_, Disconnect(_));
454 EXPECT_CALL(*adapter_, RemoveObserver(&connection)); 456 EXPECT_CALL(*adapter_, RemoveObserver(&connection));
455 error_callback.Run("The most helpful of error messages"); 457 error_callback.Run("The most helpful of error messages");
456 } 458 }
457 459
458 TEST_F(ProximityAuthBluetoothConnectionTest, DeviceChanged_Disconnected) { 460 TEST_F(ProximityAuthBluetoothConnectionTest, DeviceChanged_Disconnected) {
459 // Create a connected connection. 461 // Create a connected connection.
460 StrictMock<MockBluetoothConnection> connection; 462 StrictMock<MockBluetoothConnection> connection;
461 Connect(&connection); 463 Connect(&connection);
462 EXPECT_TRUE(connection.IsConnected()); 464 EXPECT_TRUE(connection.IsConnected());
463 465
464 // If the remote device disconnects, |connection| should also disconnect. 466 // If the remote device disconnects, |connection| should also disconnect.
465 ON_CALL(device_, IsConnected()).WillByDefault(Return(false)); 467 ON_CALL(device_, IsConnected()).WillByDefault(Return(false));
466 EXPECT_CALL(connection, SetStatusProxy(Connection::DISCONNECTED)); 468 EXPECT_CALL(connection, SetStatusProxy(cryptauth::Connection::DISCONNECTED));
467 EXPECT_CALL(*socket_, Disconnect(_)); 469 EXPECT_CALL(*socket_, Disconnect(_));
468 EXPECT_CALL(*adapter_, RemoveObserver(&connection)); 470 EXPECT_CALL(*adapter_, RemoveObserver(&connection));
469 connection.DeviceChanged(adapter_.get(), &device_); 471 connection.DeviceChanged(adapter_.get(), &device_);
470 EXPECT_FALSE(connection.IsConnected()); 472 EXPECT_FALSE(connection.IsConnected());
471 } 473 }
472 474
473 TEST_F(ProximityAuthBluetoothConnectionTest, DeviceChanged_NotDisconnected) { 475 TEST_F(ProximityAuthBluetoothConnectionTest, DeviceChanged_NotDisconnected) {
474 // Nothing should happen if DeviceChanged is called, but the remote device is 476 // Nothing should happen if DeviceChanged is called, but the remote device is
475 // not disconnected. 477 // not disconnected.
476 StrictMock<MockBluetoothConnection> connection; 478 StrictMock<MockBluetoothConnection> connection;
477 Connect(&connection); 479 Connect(&connection);
478 EXPECT_TRUE(connection.IsConnected()); 480 EXPECT_TRUE(connection.IsConnected());
479 connection.DeviceChanged(adapter_.get(), &device_); 481 connection.DeviceChanged(adapter_.get(), &device_);
480 EXPECT_TRUE(connection.IsConnected()); 482 EXPECT_TRUE(connection.IsConnected());
481 483
482 // The connection disconnects and unregisters as an observer upon destruction. 484 // The connection disconnects and unregisters as an observer upon destruction.
483 EXPECT_CALL(*socket_, Disconnect(_)); 485 EXPECT_CALL(*socket_, Disconnect(_));
484 EXPECT_CALL(*adapter_, RemoveObserver(&connection)); 486 EXPECT_CALL(*adapter_, RemoveObserver(&connection));
485 } 487 }
486 488
487 } // namespace proximity_auth 489 } // namespace proximity_auth
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698