OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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/ble/bluetooth_low_energy_weave_client_connec tion.h" | |
6 | |
7 #include <stdint.h> | |
8 | |
9 #include <memory> | |
10 #include <utility> | |
11 | |
12 #include "base/bind.h" | |
13 #include "base/memory/ptr_util.h" | |
14 #include "base/message_loop/message_loop.h" | |
15 #include "base/run_loop.h" | |
16 #include "base/test/test_simple_task_runner.h" | |
17 #include "components/proximity_auth/bluetooth_throttler.h" | |
18 #include "components/proximity_auth/connection_finder.h" | |
19 #include "components/proximity_auth/proximity_auth_test_util.h" | |
20 #include "components/proximity_auth/remote_device.h" | |
21 #include "components/proximity_auth/wire_message.h" | |
22 #include "device/bluetooth/bluetooth_adapter_factory.h" | |
23 #include "device/bluetooth/test/mock_bluetooth_adapter.h" | |
24 #include "device/bluetooth/test/mock_bluetooth_device.h" | |
25 #include "device/bluetooth/test/mock_bluetooth_discovery_session.h" | |
26 #include "device/bluetooth/test/mock_bluetooth_gatt_characteristic.h" | |
27 #include "device/bluetooth/test/mock_bluetooth_gatt_connection.h" | |
28 #include "device/bluetooth/test/mock_bluetooth_gatt_notify_session.h" | |
29 #include "testing/gmock/include/gmock/gmock.h" | |
30 #include "testing/gtest/include/gtest/gtest.h" | |
31 | |
32 using testing::_; | |
33 using testing::AtLeast; | |
34 using testing::NiceMock; | |
35 using testing::Return; | |
36 using testing::StrictMock; | |
37 using testing::SaveArg; | |
38 | |
39 namespace proximity_auth { | |
40 namespace { | |
41 | |
42 class MockBluetoothThrottler : public BluetoothThrottler { | |
43 public: | |
44 MockBluetoothThrottler() {} | |
45 ~MockBluetoothThrottler() override {} | |
46 | |
47 MOCK_CONST_METHOD0(GetDelay, base::TimeDelta()); | |
48 MOCK_METHOD1(OnConnection, void(Connection* connection)); | |
49 | |
50 private: | |
51 DISALLOW_COPY_AND_ASSIGN(MockBluetoothThrottler); | |
52 }; | |
53 | |
54 class MockBluetoothLowEnergyCharacteristicsFinder | |
55 : public BluetoothLowEnergyCharacteristicsFinder { | |
56 public: | |
57 MockBluetoothLowEnergyCharacteristicsFinder() {} | |
58 ~MockBluetoothLowEnergyCharacteristicsFinder() override {} | |
59 | |
60 private: | |
61 DISALLOW_COPY_AND_ASSIGN(MockBluetoothLowEnergyCharacteristicsFinder); | |
62 }; | |
63 | |
64 } // namespace | |
65 | |
66 namespace weave { | |
67 namespace { | |
68 | |
69 typedef BluetoothLowEnergyWeaveClientConnection::SubStatus SubStatus; | |
70 typedef BluetoothLowEnergyWeavePacketReceiver::State ReceiverState; | |
71 typedef BluetoothLowEnergyWeavePacketReceiver::ReceiverError ReceiverError; | |
72 typedef BluetoothLowEnergyWeavePacketReceiver::ReceiverType ReceiverType; | |
73 | |
74 const char kServiceUUID[] = "DEADBEEF-CAFE-FEED-FOOD-D15EA5EBEEEF"; | |
75 const char kTXCharacteristicUUID[] = "977c6674-1239-4e72-993b-502369b8bb5a"; | |
76 const char kRXCharacteristicUUID[] = "f4b904a2-a030-43b3-98a8-221c536c03cb"; | |
77 | |
78 const char kServiceID[] = "service id"; | |
79 const char kTXCharacteristicID[] = "TX characteristic id"; | |
80 const char kRXCharacteristicID[] = "RX characteristic id"; | |
81 | |
82 const device::BluetoothRemoteGattCharacteristic::Properties | |
83 kCharacteristicProperties = | |
84 device::BluetoothRemoteGattCharacteristic::PROPERTY_BROADCAST | | |
85 device::BluetoothRemoteGattCharacteristic::PROPERTY_READ | | |
86 device::BluetoothRemoteGattCharacteristic:: | |
87 PROPERTY_WRITE_WITHOUT_RESPONSE | | |
88 device::BluetoothRemoteGattCharacteristic::PROPERTY_INDICATE; | |
89 | |
90 const int kMaxNumberOfTries = 3; | |
91 const uint16_t kLargeMaxPacketSize = 30; | |
92 | |
93 const uint8_t kDataHeader = 0; | |
94 const uint8_t kConnectionRequestHeader = 1; | |
95 const uint8_t kSmallConnectionResponseHeader = 2; | |
96 const uint8_t kLargeConnectionResponseHeader = 3; | |
97 const uint8_t kConnectionCloseHeader = 4; | |
98 const uint8_t kErroneousHeader = 5; | |
99 | |
100 const std::string kSmallMessage = "bb"; | |
101 const std::string kLargeMessage = "aaabbb"; | |
102 const std::string kLargeMessage0 = "aaa"; | |
103 const std::string kLargeMessage1 = "bbb"; | |
104 | |
105 const Packet kConnectionRequest{kConnectionRequestHeader}; | |
106 const Packet kSmallConnectionResponse{kSmallConnectionResponseHeader}; | |
107 const Packet kLargeConnectionResponse{kLargeConnectionResponseHeader}; | |
108 const Packet kConnectionCloseSuccess{kConnectionCloseHeader, | |
109 ReasonForClose::CLOSE_WITHOUT_ERROR}; | |
110 const Packet kConnectionCloseUnknownError{kConnectionCloseHeader, | |
111 ReasonForClose::UNKNOWN_ERROR}; | |
112 const Packet kConnectionCloseApplicationError{ | |
113 kConnectionCloseHeader, ReasonForClose::APPLICATION_ERROR}; | |
114 | |
115 const Packet kSmallPackets0 = Packet{kDataHeader, 'b', 'b'}; | |
116 const Packet kLargePackets0 = Packet{kDataHeader, 'a', 'a', 'a'}; | |
117 const Packet kLargePackets1 = Packet{kDataHeader, 'b', 'b', 'b'}; | |
118 const Packet kErroneousPacket = Packet{kErroneousHeader}; | |
119 | |
120 const std::vector<Packet> kSmallPackets{kSmallPackets0}; | |
121 const std::vector<Packet> kLargePackets{kLargePackets0, kLargePackets1}; | |
122 | |
123 class MockBluetoothLowEnergyWeavePacketGenerator | |
124 : public BluetoothLowEnergyWeavePacketGenerator { | |
125 public: | |
126 MockBluetoothLowEnergyWeavePacketGenerator() | |
127 : max_packet_size_(kDefaultMaxPacketSize) {} | |
128 | |
129 Packet CreateConnectionRequest() override { return kConnectionRequest; } | |
130 | |
131 Packet CreateConnectionResponse() override { | |
132 NOTIMPLEMENTED(); | |
133 return Packet(); | |
134 } | |
135 | |
136 Packet CreateConnectionClose(ReasonForClose reason_for_close) override { | |
137 return Packet{kConnectionCloseHeader, | |
138 static_cast<uint8_t>(reason_for_close)}; | |
139 } | |
140 | |
141 void SetMaxPacketSize(uint16_t size) override { max_packet_size_ = size; } | |
142 | |
143 std::vector<Packet> EncodeDataMessage(std::string message) override { | |
144 if (message == kSmallMessage && max_packet_size_ == kDefaultMaxPacketSize) { | |
145 return kSmallPackets; | |
146 } else if (message == kLargeMessage && | |
147 max_packet_size_ == kLargeMaxPacketSize) { | |
148 return kLargePackets; | |
149 } else { | |
150 NOTREACHED(); | |
151 return std::vector<Packet>(); | |
152 } | |
153 } | |
154 | |
155 uint16_t GetMaxPacketSize() { return max_packet_size_; } | |
156 | |
157 private: | |
158 uint16_t max_packet_size_; | |
159 }; | |
160 | |
161 class MockBluetoothLowEnergyWeavePacketReceiver | |
162 : public BluetoothLowEnergyWeavePacketReceiver { | |
163 public: | |
164 MockBluetoothLowEnergyWeavePacketReceiver() | |
165 : BluetoothLowEnergyWeavePacketReceiver(ReceiverType::CLIENT), | |
166 state_(State::CONNECTING), | |
167 max_packet_size_(kDefaultMaxPacketSize), | |
168 reason_for_close_(ReasonForClose::CLOSE_WITHOUT_ERROR), | |
169 reason_to_close_(ReasonForClose::CLOSE_WITHOUT_ERROR) {} | |
170 | |
171 ReceiverState GetState() override { return state_; } | |
172 | |
173 uint16_t GetMaxPacketSize() override { return max_packet_size_; } | |
174 | |
175 ReasonForClose GetReasonForClose() override { return reason_for_close_; } | |
176 | |
177 ReasonForClose GetReasonToClose() override { return reason_to_close_; } | |
178 | |
179 std::string GetDataMessage() override { | |
180 if (max_packet_size_ == kDefaultMaxPacketSize) { | |
181 return kSmallMessage; | |
182 } else { | |
183 return kLargeMessage; | |
184 } | |
185 } | |
186 | |
187 ReceiverError GetReceiverError() override { | |
188 return ReceiverError::NO_ERROR_DETECTED; | |
189 } | |
190 | |
191 ReceiverState ReceivePacket(const Packet& packet) override { | |
192 switch (packet[0]) { | |
193 case kSmallConnectionResponseHeader: | |
194 max_packet_size_ = kDefaultMaxPacketSize; | |
195 state_ = ReceiverState::WAITING; | |
196 break; | |
197 case kLargeConnectionResponseHeader: | |
198 max_packet_size_ = kLargeMaxPacketSize; | |
199 state_ = ReceiverState::WAITING; | |
200 break; | |
201 case kConnectionCloseHeader: | |
202 state_ = ReceiverState::CONNECTION_CLOSED; | |
203 reason_for_close_ = static_cast<ReasonForClose>(packet[1]); | |
204 break; | |
205 case kDataHeader: | |
206 if (packet == kSmallPackets0 || packet == kLargePackets1) { | |
207 state_ = ReceiverState::DATA_READY; | |
208 } else { | |
209 state_ = ReceiverState::RECEIVING_DATA; | |
210 } | |
211 break; | |
212 default: | |
213 reason_to_close_ = ReasonForClose::APPLICATION_ERROR; | |
214 state_ = ReceiverState::ERROR_DETECTED; | |
215 } | |
216 return state_; | |
217 } | |
218 | |
219 private: | |
220 ReceiverState state_; | |
221 uint16_t max_packet_size_; | |
222 ReasonForClose reason_for_close_; | |
223 ReasonForClose reason_to_close_; | |
224 }; | |
225 | |
226 class MockBluetoothLowEnergyWeavePacketGeneratorFactory | |
227 : public BluetoothLowEnergyWeavePacketGenerator::Factory { | |
228 public: | |
229 // most_recent_instance_ will be obsolete after the connection class | |
230 // destructs. Do not use if that's the case. | |
231 MockBluetoothLowEnergyWeavePacketGenerator* GetMostRecentInstance() { | |
232 return most_recent_instance_; | |
233 } | |
234 | |
235 private: | |
236 std::unique_ptr<BluetoothLowEnergyWeavePacketGenerator> BuildInstance() | |
237 override { | |
238 most_recent_instance_ = new MockBluetoothLowEnergyWeavePacketGenerator(); | |
239 return std::unique_ptr<BluetoothLowEnergyWeavePacketGenerator>( | |
240 most_recent_instance_); | |
241 } | |
242 | |
243 MockBluetoothLowEnergyWeavePacketGenerator* most_recent_instance_; | |
244 }; | |
245 | |
246 class MockBluetoothLowEnergyWeavePacketReceiverFactory | |
247 : public BluetoothLowEnergyWeavePacketReceiver::Factory { | |
248 public: | |
249 // most_recent_instance_ will be obsolete after the connection class | |
250 // destructs. Do not use if that's the case. | |
251 MockBluetoothLowEnergyWeavePacketReceiver* GetMostRecentInstance() { | |
252 return most_recent_instance_; | |
253 } | |
254 | |
255 private: | |
256 std::unique_ptr<BluetoothLowEnergyWeavePacketReceiver> BuildInstance( | |
257 ReceiverType receiver_type) override { | |
258 most_recent_instance_ = new MockBluetoothLowEnergyWeavePacketReceiver(); | |
259 return std::unique_ptr<BluetoothLowEnergyWeavePacketReceiver>( | |
260 most_recent_instance_); | |
261 } | |
262 | |
263 MockBluetoothLowEnergyWeavePacketReceiver* most_recent_instance_; | |
264 }; | |
265 | |
266 class TestBluetoothLowEnergyWeaveClientConnection | |
267 : public BluetoothLowEnergyWeaveClientConnection { | |
268 public: | |
269 TestBluetoothLowEnergyWeaveClientConnection( | |
270 const RemoteDevice& remote_device, | |
271 scoped_refptr<device::BluetoothAdapter> adapter, | |
272 const device::BluetoothUUID remote_service_uuid, | |
273 BluetoothThrottler* bluetooth_throttler, | |
274 int max_number_of_write_attempts) | |
275 : BluetoothLowEnergyWeaveClientConnection(remote_device, | |
276 adapter, | |
277 remote_service_uuid, | |
278 bluetooth_throttler, | |
279 max_number_of_write_attempts) {} | |
280 | |
281 ~TestBluetoothLowEnergyWeaveClientConnection() override {} | |
282 | |
283 MOCK_METHOD2( | |
284 CreateCharacteristicsFinder, | |
285 BluetoothLowEnergyCharacteristicsFinder*( | |
286 const BluetoothLowEnergyCharacteristicsFinder::SuccessCallback& | |
287 success, | |
288 const BluetoothLowEnergyCharacteristicsFinder::ErrorCallback& error)); | |
289 | |
290 MOCK_METHOD2(OnDidSendMessage, | |
291 void(const WireMessage& message, bool success)); | |
292 MOCK_METHOD1(OnBytesReceived, void(const std::string& bytes)); | |
293 | |
294 // Exposing inherited protected methods for testing. | |
295 using BluetoothLowEnergyWeaveClientConnection::GattCharacteristicValueChanged; | |
296 using BluetoothLowEnergyWeaveClientConnection::SetTaskRunnerForTesting; | |
297 using BluetoothLowEnergyWeaveClientConnection::DestroyConnection; | |
298 | |
299 // Exposing inherited protected fields for testing. | |
300 using BluetoothLowEnergyWeaveClientConnection::status; | |
301 using BluetoothLowEnergyWeaveClientConnection::sub_status; | |
302 | |
303 private: | |
304 DISALLOW_COPY_AND_ASSIGN(TestBluetoothLowEnergyWeaveClientConnection); | |
305 }; | |
306 | |
307 } // namespace | |
308 | |
309 class ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest | |
310 : public testing::Test { | |
311 public: | |
312 ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest() | |
313 : adapter_(new NiceMock<device::MockBluetoothAdapter>), | |
314 remote_device_(CreateLERemoteDeviceForTest()), | |
315 service_uuid_(device::BluetoothUUID(kServiceUUID)), | |
316 tx_characteristic_uuid_(device::BluetoothUUID(kTXCharacteristicUUID)), | |
317 rx_characteristic_uuid_(device::BluetoothUUID(kRXCharacteristicUUID)), | |
318 notify_session_alias_(NULL), | |
319 bluetooth_throttler_(new NiceMock<MockBluetoothThrottler>), | |
320 task_runner_(new base::TestSimpleTaskRunner) { | |
321 BluetoothLowEnergyWeavePacketGenerator::Factory::SetInstanceForTesting( | |
322 &generator_factory_); | |
323 BluetoothLowEnergyWeavePacketReceiver::Factory::SetInstanceForTesting( | |
324 &receiver_factory_); | |
325 } | |
326 | |
327 void SetUp() override { | |
328 device_ = base::WrapUnique(new NiceMock<device::MockBluetoothDevice>( | |
329 adapter_.get(), 0, kTestRemoteDeviceName, | |
330 kTestRemoteDeviceBluetoothAddress, false, false)); | |
331 | |
332 service_ = base::WrapUnique(new NiceMock<device::MockBluetoothGattService>( | |
333 device_.get(), kServiceID, service_uuid_, true, false)); | |
334 tx_characteristic_ = | |
335 base::WrapUnique(new NiceMock<device::MockBluetoothGattCharacteristic>( | |
336 service_.get(), kTXCharacteristicID, tx_characteristic_uuid_, false, | |
337 kCharacteristicProperties, | |
338 device::BluetoothRemoteGattCharacteristic::PERMISSION_NONE)); | |
339 | |
340 rx_characteristic_ = | |
341 base::WrapUnique(new NiceMock<device::MockBluetoothGattCharacteristic>( | |
342 service_.get(), kRXCharacteristicID, rx_characteristic_uuid_, false, | |
343 kCharacteristicProperties, | |
344 device::BluetoothRemoteGattCharacteristic::PERMISSION_NONE)); | |
345 | |
346 device::BluetoothAdapterFactory::SetAdapterForTesting(adapter_); | |
347 | |
348 std::vector<const device::BluetoothDevice*> devices; | |
349 devices.push_back(device_.get()); | |
350 ON_CALL(*adapter_, GetDevices()).WillByDefault(Return(devices)); | |
351 ON_CALL(*adapter_, GetDevice(kTestRemoteDeviceBluetoothAddress)) | |
352 .WillByDefault(Return(device_.get())); | |
353 ON_CALL(*device_, GetGattService(kServiceID)) | |
354 .WillByDefault(Return(service_.get())); | |
355 ON_CALL(*service_, GetCharacteristic(kRXCharacteristicID)) | |
356 .WillByDefault(Return(rx_characteristic_.get())); | |
357 ON_CALL(*service_, GetCharacteristic(kTXCharacteristicID)) | |
358 .WillByDefault(Return(tx_characteristic_.get())); | |
359 } | |
360 | |
361 // Creates a BluetoothLowEnergyWeaveClientConnection and verifies it's in | |
362 // DISCONNECTED state. | |
363 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> | |
364 CreateConnection() { | |
365 EXPECT_CALL(*adapter_, AddObserver(_)); | |
366 EXPECT_CALL(*adapter_, RemoveObserver(_)); | |
367 | |
368 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( | |
369 new TestBluetoothLowEnergyWeaveClientConnection( | |
370 remote_device_, adapter_, service_uuid_, bluetooth_throttler_.get(), | |
371 kMaxNumberOfTries)); | |
372 | |
373 EXPECT_EQ(connection->sub_status(), SubStatus::DISCONNECTED); | |
374 EXPECT_EQ(connection->status(), Connection::DISCONNECTED); | |
375 | |
376 connection->SetTaskRunnerForTesting(task_runner_); | |
377 | |
378 return connection; | |
379 } | |
380 | |
381 // Transitions |connection| from DISCONNECTED to WAITING_CHARACTERISTICS | |
382 // state, without an existing GATT connection. | |
383 void ConnectGatt(TestBluetoothLowEnergyWeaveClientConnection* connection) { | |
384 // Preparing |connection| for a CreateGattConnection call. | |
385 EXPECT_CALL(*device_, CreateGattConnection(_, _)) | |
386 .WillOnce(DoAll(SaveArg<0>(&create_gatt_connection_success_callback_), | |
387 SaveArg<1>(&create_gatt_connection_error_callback_))); | |
388 | |
389 // No throttling by default | |
390 EXPECT_CALL(*bluetooth_throttler_, GetDelay()) | |
391 .WillOnce(Return(base::TimeDelta())); | |
392 | |
393 connection->Connect(); | |
394 | |
395 EXPECT_EQ(connection->sub_status(), SubStatus::WAITING_GATT_CONNECTION); | |
396 EXPECT_EQ(connection->status(), Connection::IN_PROGRESS); | |
397 | |
398 // Preparing |connection| to run |create_gatt_connection_success_callback_|. | |
399 EXPECT_FALSE(create_gatt_connection_error_callback_.is_null()); | |
400 ASSERT_FALSE(create_gatt_connection_success_callback_.is_null()); | |
401 EXPECT_CALL(*connection, CreateCharacteristicsFinder(_, _)) | |
402 .WillOnce(DoAll( | |
403 SaveArg<0>(&characteristics_finder_success_callback_), | |
404 SaveArg<1>(&characteristics_finder_error_callback_), | |
405 Return(new NiceMock<MockBluetoothLowEnergyCharacteristicsFinder>))); | |
406 | |
407 create_gatt_connection_success_callback_.Run( | |
408 base::WrapUnique(new NiceMock<device::MockBluetoothGattConnection>( | |
409 adapter_, kTestRemoteDeviceBluetoothAddress))); | |
410 | |
411 EXPECT_EQ(connection->sub_status(), SubStatus::WAITING_CHARACTERISTICS); | |
412 EXPECT_EQ(connection->status(), Connection::IN_PROGRESS); | |
413 } | |
414 | |
415 // Transitions |connection| from WAITING_CHARACTERISTICS to | |
416 // WAITING_NOTIFY_SESSION state. | |
417 void CharacteristicsFound( | |
418 TestBluetoothLowEnergyWeaveClientConnection* connection) { | |
419 EXPECT_CALL(*rx_characteristic_, StartNotifySession(_, _)) | |
420 .WillOnce(DoAll(SaveArg<0>(¬ify_session_success_callback_), | |
421 SaveArg<1>(¬ify_session_error_callback_))); | |
422 EXPECT_FALSE(characteristics_finder_error_callback_.is_null()); | |
423 ASSERT_FALSE(characteristics_finder_success_callback_.is_null()); | |
424 | |
425 characteristics_finder_success_callback_.Run( | |
426 {service_uuid_, kServiceID}, | |
427 {tx_characteristic_uuid_, kTXCharacteristicID}, | |
428 {rx_characteristic_uuid_, kRXCharacteristicID}); | |
429 | |
430 EXPECT_EQ(connection->sub_status(), SubStatus::WAITING_NOTIFY_SESSION); | |
431 EXPECT_EQ(connection->status(), Connection::IN_PROGRESS); | |
432 } | |
433 | |
434 // Transitions |connection| from WAITING_NOTIFY_SESSION to | |
435 // WAITING_CONNECTION_RESPONSE state. | |
436 void NotifySessionStarted( | |
437 TestBluetoothLowEnergyWeaveClientConnection* connection) { | |
438 EXPECT_CALL(*tx_characteristic_, WriteRemoteCharacteristic(_, _, _)) | |
439 .WillOnce( | |
440 DoAll(SaveArg<0>(&last_value_written_on_tx_characteristic_), | |
441 SaveArg<1>(&write_remote_characteristic_success_callback_), | |
442 SaveArg<2>(&write_remote_characteristic_error_callback_))); | |
443 EXPECT_FALSE(notify_session_error_callback_.is_null()); | |
444 ASSERT_FALSE(notify_session_success_callback_.is_null()); | |
445 | |
446 // Store an alias for the notify session passed |connection|. | |
447 std::unique_ptr<device::MockBluetoothGattNotifySession> notify_session( | |
448 new NiceMock<device::MockBluetoothGattNotifySession>( | |
449 kTXCharacteristicID)); | |
450 notify_session_alias_ = notify_session.get(); | |
451 | |
452 notify_session_success_callback_.Run(std::move(notify_session)); | |
453 task_runner_->RunUntilIdle(); | |
454 | |
455 // Written value contains only the mock Connection Request. | |
456 EXPECT_EQ(last_value_written_on_tx_characteristic_, kConnectionRequest); | |
457 | |
458 EXPECT_EQ(connection->sub_status(), SubStatus::WAITING_CONNECTION_RESPONSE); | |
459 EXPECT_EQ(connection->status(), Connection::IN_PROGRESS); | |
460 } | |
461 | |
462 // Transitions |connection| from WAITING_CONNECTION_RESPONSE to CONNECTED. | |
463 void ConnectionResponseReceived( | |
464 TestBluetoothLowEnergyWeaveClientConnection* connection, | |
465 uint16_t selected_packet_size) { | |
466 // Written value contains only the mock Connection Request. | |
467 EXPECT_EQ(last_value_written_on_tx_characteristic_, kConnectionRequest); | |
468 | |
469 EXPECT_CALL(*connection, OnDidSendMessage(_, _)).Times(0); | |
470 RunWriteCharacteristicSuccessCallback(); | |
471 | |
472 // Received Connection Response. | |
473 if (selected_packet_size == kDefaultMaxPacketSize) { | |
474 connection->GattCharacteristicValueChanged( | |
475 adapter_.get(), rx_characteristic_.get(), kSmallConnectionResponse); | |
476 EXPECT_EQ(receiver_factory_.GetMostRecentInstance()->GetMaxPacketSize(), | |
477 kDefaultMaxPacketSize); | |
478 EXPECT_EQ(generator_factory_.GetMostRecentInstance()->GetMaxPacketSize(), | |
479 kDefaultMaxPacketSize); | |
480 } else if (selected_packet_size == kLargeMaxPacketSize) { | |
481 connection->GattCharacteristicValueChanged( | |
482 adapter_.get(), rx_characteristic_.get(), kLargeConnectionResponse); | |
483 EXPECT_EQ(receiver_factory_.GetMostRecentInstance()->GetMaxPacketSize(), | |
484 kLargeMaxPacketSize); | |
485 EXPECT_EQ(generator_factory_.GetMostRecentInstance()->GetMaxPacketSize(), | |
486 kLargeMaxPacketSize); | |
487 } else { | |
488 NOTREACHED(); | |
489 } | |
490 | |
491 EXPECT_EQ(connection->sub_status(), SubStatus::CONNECTED); | |
492 EXPECT_EQ(connection->status(), Connection::CONNECTED); | |
493 } | |
494 | |
495 // Transitions |connection| to a DISCONNECTED state regardless of its initial | |
496 // state. | |
497 void Disconnect(TestBluetoothLowEnergyWeaveClientConnection* connection) { | |
498 // A notify session was previously set. | |
499 if (notify_session_alias_) | |
500 EXPECT_CALL(*notify_session_alias_, Stop(_)); | |
501 | |
502 if (connection->sub_status() == SubStatus::CONNECTED) { | |
503 EXPECT_CALL(*tx_characteristic_, WriteRemoteCharacteristic(_, _, _)) | |
504 .WillOnce( | |
505 DoAll(SaveArg<0>(&last_value_written_on_tx_characteristic_), | |
506 SaveArg<1>(&write_remote_characteristic_success_callback_), | |
507 SaveArg<2>(&write_remote_characteristic_error_callback_))); | |
508 } | |
509 | |
510 connection->Disconnect(); | |
511 | |
512 if (connection->sub_status() == SubStatus::CONNECTED) { | |
513 connection->DestroyConnection(); | |
514 EXPECT_EQ(last_value_written_on_tx_characteristic_, | |
515 kConnectionCloseSuccess); | |
516 RunWriteCharacteristicSuccessCallback(); | |
517 } | |
518 | |
519 EXPECT_EQ(connection->sub_status(), SubStatus::DISCONNECTED); | |
520 EXPECT_EQ(connection->status(), Connection::DISCONNECTED); | |
521 } | |
522 | |
523 void InitializeConnection( | |
524 TestBluetoothLowEnergyWeaveClientConnection* connection, | |
525 uint32_t selected_packet_size) { | |
526 ConnectGatt(connection); | |
527 CharacteristicsFound(connection); | |
528 NotifySessionStarted(connection); | |
529 ConnectionResponseReceived(connection, selected_packet_size); | |
530 } | |
531 | |
532 void RunWriteCharacteristicSuccessCallback() { | |
533 EXPECT_FALSE(write_remote_characteristic_error_callback_.is_null()); | |
534 ASSERT_FALSE(write_remote_characteristic_success_callback_.is_null()); | |
535 write_remote_characteristic_success_callback_.Run(); | |
536 } | |
537 | |
538 protected: | |
539 scoped_refptr<device::MockBluetoothAdapter> adapter_; | |
540 RemoteDevice remote_device_; | |
541 device::BluetoothUUID service_uuid_; | |
542 device::BluetoothUUID tx_characteristic_uuid_; | |
543 device::BluetoothUUID rx_characteristic_uuid_; | |
544 std::unique_ptr<device::MockBluetoothDevice> device_; | |
545 std::unique_ptr<device::MockBluetoothGattService> service_; | |
546 std::unique_ptr<device::MockBluetoothGattCharacteristic> tx_characteristic_; | |
547 std::unique_ptr<device::MockBluetoothGattCharacteristic> rx_characteristic_; | |
548 std::vector<uint8_t> last_value_written_on_tx_characteristic_; | |
549 device::MockBluetoothGattNotifySession* notify_session_alias_; | |
550 std::unique_ptr<MockBluetoothThrottler> bluetooth_throttler_; | |
551 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | |
552 base::MessageLoop message_loop_; | |
553 | |
554 // Callbacks | |
555 device::BluetoothDevice::GattConnectionCallback | |
556 create_gatt_connection_success_callback_; | |
557 device::BluetoothDevice::ConnectErrorCallback | |
558 create_gatt_connection_error_callback_; | |
559 | |
560 BluetoothLowEnergyCharacteristicsFinder::SuccessCallback | |
561 characteristics_finder_success_callback_; | |
562 BluetoothLowEnergyCharacteristicsFinder::ErrorCallback | |
563 characteristics_finder_error_callback_; | |
564 | |
565 device::BluetoothRemoteGattCharacteristic::NotifySessionCallback | |
566 notify_session_success_callback_; | |
567 device::BluetoothRemoteGattCharacteristic::ErrorCallback | |
568 notify_session_error_callback_; | |
569 | |
570 base::Closure write_remote_characteristic_success_callback_; | |
571 device::BluetoothRemoteGattCharacteristic::ErrorCallback | |
572 write_remote_characteristic_error_callback_; | |
573 | |
574 MockBluetoothLowEnergyWeavePacketGeneratorFactory generator_factory_; | |
575 MockBluetoothLowEnergyWeavePacketReceiverFactory receiver_factory_; | |
576 }; | |
577 | |
578 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, | |
579 CreateAndDestroyWithoutConnectCallDoesntCrash) { | |
580 BluetoothLowEnergyWeaveClientConnection connection( | |
581 remote_device_, adapter_, service_uuid_, bluetooth_throttler_.get(), | |
582 kMaxNumberOfTries); | |
583 } | |
584 | |
585 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, | |
586 DisconectWithoutConnectDoesntCrash) { | |
587 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( | |
588 CreateConnection()); | |
589 Disconnect(connection.get()); | |
590 } | |
591 | |
592 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, | |
593 ConnectSuccess) { | |
594 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( | |
595 CreateConnection()); | |
596 ConnectGatt(connection.get()); | |
597 CharacteristicsFound(connection.get()); | |
598 NotifySessionStarted(connection.get()); | |
599 ConnectionResponseReceived(connection.get(), kDefaultMaxPacketSize); | |
600 } | |
601 | |
602 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, | |
603 ConnectSuccessDisconnect) { | |
604 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( | |
605 CreateConnection()); | |
606 InitializeConnection(connection.get(), kDefaultMaxPacketSize); | |
607 EXPECT_EQ(connection->sub_status(), SubStatus::CONNECTED); | |
608 Disconnect(connection.get()); | |
609 } | |
610 | |
611 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, | |
612 ConnectIncompleteDisconnectFromWaitingCharacteristicsState) { | |
613 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( | |
614 CreateConnection()); | |
615 ConnectGatt(connection.get()); | |
616 Disconnect(connection.get()); | |
617 } | |
618 | |
619 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, | |
620 ConnectIncompleteDisconnectFromWaitingNotifySessionState) { | |
621 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( | |
622 CreateConnection()); | |
623 ConnectGatt(connection.get()); | |
624 CharacteristicsFound(connection.get()); | |
625 Disconnect(connection.get()); | |
626 } | |
627 | |
628 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, | |
629 ConnectIncompleteDisconnectFromWaitingConnectionResponseState) { | |
630 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( | |
631 CreateConnection()); | |
632 ConnectGatt(connection.get()); | |
633 CharacteristicsFound(connection.get()); | |
634 NotifySessionStarted(connection.get()); | |
635 Disconnect(connection.get()); | |
636 } | |
637 | |
638 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, | |
639 ConnectFailsCharacteristicsNotFound) { | |
640 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( | |
641 CreateConnection()); | |
642 ConnectGatt(connection.get()); | |
643 | |
644 EXPECT_CALL(*rx_characteristic_, StartNotifySession(_, _)).Times(0); | |
645 EXPECT_FALSE(characteristics_finder_success_callback_.is_null()); | |
646 ASSERT_FALSE(characteristics_finder_error_callback_.is_null()); | |
647 | |
648 characteristics_finder_error_callback_.Run( | |
649 {tx_characteristic_uuid_, kTXCharacteristicID}, | |
650 {rx_characteristic_uuid_, kRXCharacteristicID}); | |
651 | |
652 EXPECT_EQ(connection->sub_status(), SubStatus::DISCONNECTED); | |
653 EXPECT_EQ(connection->status(), Connection::DISCONNECTED); | |
654 } | |
655 | |
656 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, | |
657 ConnectFailsNotifySessionError) { | |
658 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( | |
659 CreateConnection()); | |
660 ConnectGatt(connection.get()); | |
661 CharacteristicsFound(connection.get()); | |
662 | |
663 EXPECT_CALL(*tx_characteristic_, WriteRemoteCharacteristic(_, _, _)).Times(0); | |
664 EXPECT_FALSE(notify_session_success_callback_.is_null()); | |
665 ASSERT_FALSE(notify_session_error_callback_.is_null()); | |
666 | |
667 notify_session_error_callback_.Run( | |
668 device::BluetoothRemoteGattService::GATT_ERROR_UNKNOWN); | |
669 | |
670 EXPECT_EQ(connection->sub_status(), SubStatus::DISCONNECTED); | |
671 EXPECT_EQ(connection->status(), Connection::DISCONNECTED); | |
672 } | |
673 | |
674 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, | |
675 ConnectFailsErrorSendingConnectionRequest) { | |
676 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( | |
677 CreateConnection()); | |
678 ConnectGatt(connection.get()); | |
679 CharacteristicsFound(connection.get()); | |
680 NotifySessionStarted(connection.get()); | |
681 | |
682 // |connection| will call WriteRemoteCharacteristics(_,_) to try to send the | |
683 // message |kMaxNumberOfTries| times. There is alredy one EXPECTA_CALL for | |
684 // WriteRemoteCharacteristic(_,_,_) in NotifySessionStated, that's why we use | |
685 // |kMaxNumberOfTries-1| in the EXPECT_CALL statement. | |
686 EXPECT_CALL(*connection, OnDidSendMessage(_, _)).Times(0); | |
687 EXPECT_CALL(*tx_characteristic_, WriteRemoteCharacteristic(_, _, _)) | |
688 .Times(kMaxNumberOfTries - 1) | |
689 .WillRepeatedly( | |
690 DoAll(SaveArg<0>(&last_value_written_on_tx_characteristic_), | |
691 SaveArg<1>(&write_remote_characteristic_success_callback_), | |
692 SaveArg<2>(&write_remote_characteristic_error_callback_))); | |
693 | |
694 for (int i = 0; i < kMaxNumberOfTries; i++) { | |
695 EXPECT_EQ(last_value_written_on_tx_characteristic_, kConnectionRequest); | |
696 ASSERT_FALSE(write_remote_characteristic_error_callback_.is_null()); | |
697 EXPECT_FALSE(write_remote_characteristic_success_callback_.is_null()); | |
698 write_remote_characteristic_error_callback_.Run( | |
699 device::BluetoothRemoteGattService::GATT_ERROR_UNKNOWN); | |
700 } | |
701 | |
702 EXPECT_EQ(connection->sub_status(), SubStatus::DISCONNECTED); | |
703 EXPECT_EQ(connection->status(), Connection::DISCONNECTED); | |
704 } | |
705 | |
706 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, | |
707 ReceiveMessageSmallerThanCharacteristicSize) { | |
708 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( | |
709 CreateConnection()); | |
710 InitializeConnection(connection.get(), kDefaultMaxPacketSize); | |
711 | |
712 std::string received_bytes; | |
713 EXPECT_CALL(*connection, OnBytesReceived(_)) | |
714 .WillOnce(SaveArg<0>(&received_bytes)); | |
715 | |
716 connection->GattCharacteristicValueChanged( | |
717 adapter_.get(), rx_characteristic_.get(), kSmallPackets0); | |
718 | |
719 EXPECT_EQ(received_bytes, kSmallMessage); | |
720 } | |
721 | |
722 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, | |
723 ReceiveMessageLargerThanCharacteristicSize) { | |
724 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( | |
725 CreateConnection()); | |
726 | |
727 InitializeConnection(connection.get(), kLargeMaxPacketSize); | |
728 | |
729 std::string received_bytes; | |
730 EXPECT_CALL(*connection, OnBytesReceived(_)) | |
731 .WillOnce(SaveArg<0>(&received_bytes)); | |
732 | |
733 std::vector<Packet> packets = kLargePackets; | |
734 | |
735 for (auto packet : packets) { | |
736 connection->GattCharacteristicValueChanged( | |
737 adapter_.get(), rx_characteristic_.get(), packet); | |
738 } | |
739 EXPECT_EQ(received_bytes, kLargeMessage); | |
740 } | |
741 | |
742 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, | |
743 SendMessageSmallerThanCharacteristicSize) { | |
744 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( | |
745 CreateConnection()); | |
746 InitializeConnection(connection.get(), kDefaultMaxPacketSize); | |
747 | |
748 // Expecting a first call of WriteRemoteCharacteristic, after SendMessage is | |
749 // called. | |
750 EXPECT_CALL(*tx_characteristic_, WriteRemoteCharacteristic(_, _, _)) | |
751 .WillOnce( | |
752 DoAll(SaveArg<0>(&last_value_written_on_tx_characteristic_), | |
753 SaveArg<1>(&write_remote_characteristic_success_callback_), | |
754 SaveArg<2>(&write_remote_characteristic_error_callback_))); | |
755 | |
756 connection->SendMessage(base::WrapUnique(new FakeWireMessage(kSmallMessage))); | |
757 | |
758 EXPECT_EQ(last_value_written_on_tx_characteristic_, kSmallPackets0); | |
759 EXPECT_CALL(*connection, OnDidSendMessage(_, _)); | |
760 | |
761 RunWriteCharacteristicSuccessCallback(); | |
762 } | |
763 | |
764 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, | |
765 SendMessageLargerThanCharacteristicSize) { | |
766 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( | |
767 CreateConnection()); | |
768 | |
769 InitializeConnection(connection.get(), kLargeMaxPacketSize); | |
770 | |
771 // Expecting a first call of WriteRemoteCharacteristic, after SendMessage is | |
772 // called. | |
773 EXPECT_CALL(*tx_characteristic_, WriteRemoteCharacteristic(_, _, _)) | |
774 .WillOnce( | |
775 DoAll(SaveArg<0>(&last_value_written_on_tx_characteristic_), | |
776 SaveArg<1>(&write_remote_characteristic_success_callback_), | |
777 SaveArg<2>(&write_remote_characteristic_error_callback_))); | |
778 | |
779 connection->SendMessage(base::WrapUnique(new FakeWireMessage(kLargeMessage))); | |
780 | |
781 EXPECT_EQ(last_value_written_on_tx_characteristic_, kLargePackets0); | |
782 std::vector<uint8_t> bytes_received( | |
783 last_value_written_on_tx_characteristic_.begin() + 1, | |
784 last_value_written_on_tx_characteristic_.end()); | |
785 | |
786 EXPECT_CALL(*tx_characteristic_, WriteRemoteCharacteristic(_, _, _)) | |
787 .WillOnce( | |
788 DoAll(SaveArg<0>(&last_value_written_on_tx_characteristic_), | |
789 SaveArg<1>(&write_remote_characteristic_success_callback_), | |
790 SaveArg<2>(&write_remote_characteristic_error_callback_))); | |
791 | |
792 RunWriteCharacteristicSuccessCallback(); | |
793 bytes_received.insert(bytes_received.end(), | |
794 last_value_written_on_tx_characteristic_.begin() + 1, | |
795 last_value_written_on_tx_characteristic_.end()); | |
796 | |
797 std::vector<uint8_t> expected(kLargeMessage.begin(), kLargeMessage.end()); | |
798 EXPECT_EQ(expected, bytes_received); | |
799 | |
800 EXPECT_CALL(*connection, OnDidSendMessage(_, _)); | |
801 RunWriteCharacteristicSuccessCallback(); | |
802 } | |
803 | |
804 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, | |
805 ReceiveCloseConnectionTest) { | |
806 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( | |
807 CreateConnection()); | |
808 InitializeConnection(connection.get(), kDefaultMaxPacketSize); | |
809 | |
810 connection->GattCharacteristicValueChanged( | |
811 adapter_.get(), rx_characteristic_.get(), kConnectionCloseUnknownError); | |
812 | |
813 EXPECT_EQ(receiver_factory_.GetMostRecentInstance()->GetReasonForClose(), | |
814 ReasonForClose::UNKNOWN_ERROR); | |
815 EXPECT_EQ(connection->sub_status(), SubStatus::DISCONNECTED); | |
816 EXPECT_EQ(connection->status(), Connection::DISCONNECTED); | |
817 } | |
818 | |
819 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, | |
Kyle Horimoto
2016/07/11 21:34:59
Can you also write a test which does tests this si
jingxuy
2016/07/11 23:15:17
Done.
| |
820 ReceiverErrorTest) { | |
821 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( | |
822 CreateConnection()); | |
823 | |
824 InitializeConnection(connection.get(), kDefaultMaxPacketSize); | |
825 | |
826 EXPECT_CALL(*tx_characteristic_, WriteRemoteCharacteristic(_, _, _)) | |
827 .WillOnce( | |
828 DoAll(SaveArg<0>(&last_value_written_on_tx_characteristic_), | |
829 SaveArg<1>(&write_remote_characteristic_success_callback_), | |
830 SaveArg<2>(&write_remote_characteristic_error_callback_))); | |
831 | |
832 connection->GattCharacteristicValueChanged( | |
833 adapter_.get(), rx_characteristic_.get(), kErroneousPacket); | |
834 | |
835 EXPECT_EQ(last_value_written_on_tx_characteristic_, | |
836 kConnectionCloseApplicationError); | |
837 EXPECT_EQ(receiver_factory_.GetMostRecentInstance()->GetReasonToClose(), | |
838 ReasonForClose::APPLICATION_ERROR); | |
839 | |
840 RunWriteCharacteristicSuccessCallback(); | |
841 EXPECT_EQ(connection->sub_status(), SubStatus::DISCONNECTED); | |
842 EXPECT_EQ(connection->status(), Connection::DISCONNECTED); | |
843 } | |
844 | |
845 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, | |
Kyle Horimoto
2016/07/11 21:34:59
Can you register for callbacks and check that OnDi
jingxuy
2016/07/11 23:15:17
OnDidSendMessage is only called if a wire message
Kyle Horimoto
2016/07/12 01:03:12
That's incorrect - it is also called if a message
jingxuy
2016/07/15 18:24:59
What I meant is that there are 3 types of write re
| |
846 WriteConnectionCloseMaxNumberOfTimes) { | |
847 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( | |
848 CreateConnection()); | |
849 | |
850 InitializeConnection(connection.get(), kDefaultMaxPacketSize); | |
851 EXPECT_EQ(connection->sub_status(), SubStatus::CONNECTED); | |
852 | |
853 EXPECT_CALL(*tx_characteristic_, WriteRemoteCharacteristic(_, _, _)) | |
854 .WillOnce( | |
855 DoAll(SaveArg<0>(&last_value_written_on_tx_characteristic_), | |
856 SaveArg<1>(&write_remote_characteristic_success_callback_), | |
857 SaveArg<2>(&write_remote_characteristic_error_callback_))); | |
858 connection->Disconnect(); | |
859 EXPECT_EQ(connection->sub_status(), SubStatus::CONNECTED); | |
860 | |
861 for (int i = 0; i < kMaxNumberOfTries; i++) { | |
862 EXPECT_EQ(last_value_written_on_tx_characteristic_, | |
863 kConnectionCloseSuccess); | |
864 ASSERT_FALSE(write_remote_characteristic_error_callback_.is_null()); | |
865 EXPECT_FALSE(write_remote_characteristic_success_callback_.is_null()); | |
866 | |
867 if (i != kMaxNumberOfTries - 1) { | |
868 EXPECT_CALL(*tx_characteristic_, WriteRemoteCharacteristic(_, _, _)) | |
869 .WillOnce( | |
870 DoAll(SaveArg<0>(&last_value_written_on_tx_characteristic_), | |
871 SaveArg<1>(&write_remote_characteristic_success_callback_), | |
872 SaveArg<2>(&write_remote_characteristic_error_callback_))); | |
873 } | |
874 | |
875 write_remote_characteristic_error_callback_.Run( | |
876 device::BluetoothRemoteGattService::GATT_ERROR_UNKNOWN); | |
877 } | |
878 | |
879 EXPECT_EQ(connection->sub_status(), SubStatus::DISCONNECTED); | |
880 EXPECT_EQ(connection->status(), Connection::DISCONNECTED); | |
881 } | |
882 | |
883 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, | |
884 ConnectAfterADelayWhenThrottled) { | |
885 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( | |
886 CreateConnection()); | |
887 | |
888 EXPECT_CALL(*bluetooth_throttler_, GetDelay()) | |
889 .WillOnce(Return(base::TimeDelta(base::TimeDelta::FromSeconds(1)))); | |
890 EXPECT_CALL(*device_, CreateGattConnection(_, _)) | |
891 .WillOnce(DoAll(SaveArg<0>(&create_gatt_connection_success_callback_), | |
892 SaveArg<1>(&create_gatt_connection_error_callback_))); | |
893 | |
894 // No GATT connection should be created before the delay. | |
895 connection->Connect(); | |
896 EXPECT_EQ(connection->sub_status(), SubStatus::WAITING_GATT_CONNECTION); | |
897 EXPECT_EQ(connection->status(), Connection::IN_PROGRESS); | |
898 EXPECT_TRUE(create_gatt_connection_error_callback_.is_null()); | |
899 EXPECT_TRUE(create_gatt_connection_success_callback_.is_null()); | |
900 | |
901 // A GATT connection should be created after the delay. | |
902 task_runner_->RunUntilIdle(); | |
903 EXPECT_FALSE(create_gatt_connection_error_callback_.is_null()); | |
904 ASSERT_FALSE(create_gatt_connection_success_callback_.is_null()); | |
905 | |
906 // Preparing |connection| to run |create_gatt_connection_success_callback_|. | |
907 EXPECT_CALL(*connection, CreateCharacteristicsFinder(_, _)) | |
908 .WillOnce(DoAll( | |
909 SaveArg<0>(&characteristics_finder_success_callback_), | |
910 SaveArg<1>(&characteristics_finder_error_callback_), | |
911 Return(new NiceMock<MockBluetoothLowEnergyCharacteristicsFinder>))); | |
912 | |
913 create_gatt_connection_success_callback_.Run( | |
914 base::WrapUnique(new NiceMock<device::MockBluetoothGattConnection>( | |
915 adapter_, kTestRemoteDeviceBluetoothAddress))); | |
916 | |
917 CharacteristicsFound(connection.get()); | |
918 NotifySessionStarted(connection.get()); | |
919 ConnectionResponseReceived(connection.get(), kDefaultMaxPacketSize); | |
920 } | |
921 | |
922 } // namespace weave | |
923 | |
924 } // namespace proximity_auth | |
OLD | NEW |