Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/proximity_auth/ble/bluetooth_low_energy_connection.h" | 5 #include "components/proximity_auth/ble/bluetooth_low_energy_weave_client_connec tion.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <utility> | 10 #include <utility> |
| 11 | 11 |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/macros.h" | |
| 14 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
| 15 #include "base/memory/ref_counted.h" | |
| 16 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
| 17 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
| 18 #include "base/test/test_simple_task_runner.h" | 16 #include "base/test/test_simple_task_runner.h" |
| 19 #include "components/proximity_auth/ble/bluetooth_low_energy_characteristics_fin der.h" | |
| 20 #include "components/proximity_auth/bluetooth_throttler.h" | 17 #include "components/proximity_auth/bluetooth_throttler.h" |
| 21 #include "components/proximity_auth/connection_finder.h" | 18 #include "components/proximity_auth/connection_finder.h" |
| 22 #include "components/proximity_auth/proximity_auth_test_util.h" | 19 #include "components/proximity_auth/proximity_auth_test_util.h" |
| 23 #include "components/proximity_auth/remote_device.h" | 20 #include "components/proximity_auth/remote_device.h" |
| 24 #include "components/proximity_auth/wire_message.h" | 21 #include "components/proximity_auth/wire_message.h" |
| 25 #include "device/bluetooth/bluetooth_adapter_factory.h" | 22 #include "device/bluetooth/bluetooth_adapter_factory.h" |
| 26 #include "device/bluetooth/bluetooth_remote_gatt_characteristic.h" | |
| 27 #include "device/bluetooth/bluetooth_uuid.h" | |
| 28 #include "device/bluetooth/test/mock_bluetooth_adapter.h" | 23 #include "device/bluetooth/test/mock_bluetooth_adapter.h" |
| 29 #include "device/bluetooth/test/mock_bluetooth_device.h" | 24 #include "device/bluetooth/test/mock_bluetooth_device.h" |
| 30 #include "device/bluetooth/test/mock_bluetooth_discovery_session.h" | 25 #include "device/bluetooth/test/mock_bluetooth_discovery_session.h" |
| 31 #include "device/bluetooth/test/mock_bluetooth_gatt_characteristic.h" | 26 #include "device/bluetooth/test/mock_bluetooth_gatt_characteristic.h" |
| 32 #include "device/bluetooth/test/mock_bluetooth_gatt_connection.h" | 27 #include "device/bluetooth/test/mock_bluetooth_gatt_connection.h" |
| 33 #include "device/bluetooth/test/mock_bluetooth_gatt_notify_session.h" | 28 #include "device/bluetooth/test/mock_bluetooth_gatt_notify_session.h" |
| 34 #include "testing/gmock/include/gmock/gmock.h" | 29 #include "testing/gmock/include/gmock/gmock.h" |
| 35 #include "testing/gtest/include/gtest/gtest.h" | 30 #include "testing/gtest/include/gtest/gtest.h" |
| 36 | 31 |
| 37 using testing::_; | 32 using testing::_; |
| 38 using testing::AtLeast; | 33 using testing::AtLeast; |
| 39 using testing::NiceMock; | 34 using testing::NiceMock; |
| 40 using testing::Return; | 35 using testing::Return; |
| 41 using testing::StrictMock; | 36 using testing::StrictMock; |
| 42 using testing::SaveArg; | 37 using testing::SaveArg; |
| 43 | 38 |
| 44 namespace proximity_auth { | 39 namespace proximity_auth { |
| 45 namespace { | 40 namespace { |
| 46 | 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 | |
| 47 const char kServiceUUID[] = "DEADBEEF-CAFE-FEED-FOOD-D15EA5EBEEEF"; | 74 const char kServiceUUID[] = "DEADBEEF-CAFE-FEED-FOOD-D15EA5EBEEEF"; |
| 48 const char kToPeripheralCharUUID[] = "977c6674-1239-4e72-993b-502369b8bb5a"; | 75 const char kTXCharacteristicUUID[] = "977c6674-1239-4e72-993b-502369b8bb5a"; |
| 49 const char kFromPeripheralCharUUID[] = "f4b904a2-a030-43b3-98a8-221c536c03cb"; | 76 const char kRXCharacteristicUUID[] = "f4b904a2-a030-43b3-98a8-221c536c03cb"; |
| 50 | 77 |
| 51 const char kServiceID[] = "service id"; | 78 const char kServiceID[] = "service id"; |
| 52 const char kToPeripheralCharID[] = "to peripheral char id"; | 79 const char kTXCharacteristicID[] = "TX characteristic id"; |
| 53 const char kFromPeripheralCharID[] = "from peripheral char id"; | 80 const char kRXCharacteristicID[] = "RX characteristic id"; |
| 54 | 81 |
| 55 const device::BluetoothRemoteGattCharacteristic::Properties | 82 const device::BluetoothRemoteGattCharacteristic::Properties |
| 56 kCharacteristicProperties = | 83 kCharacteristicProperties = |
| 57 device::BluetoothRemoteGattCharacteristic::PROPERTY_BROADCAST | | 84 device::BluetoothRemoteGattCharacteristic::PROPERTY_BROADCAST | |
| 58 device::BluetoothRemoteGattCharacteristic::PROPERTY_READ | | 85 device::BluetoothRemoteGattCharacteristic::PROPERTY_READ | |
| 59 device::BluetoothRemoteGattCharacteristic:: | 86 device::BluetoothRemoteGattCharacteristic:: |
| 60 PROPERTY_WRITE_WITHOUT_RESPONSE | | 87 PROPERTY_WRITE_WITHOUT_RESPONSE | |
| 61 device::BluetoothRemoteGattCharacteristic::PROPERTY_INDICATE; | 88 device::BluetoothRemoteGattCharacteristic::PROPERTY_INDICATE; |
| 62 | 89 |
| 63 const int kMaxNumberOfTries = 3; | 90 const int kMaxNumberOfTries = 3; |
| 91 const uint16_t kLargeMaxPacketSize = 30; | |
| 64 | 92 |
| 65 class MockBluetoothThrottler : public BluetoothThrottler { | 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 | |
| 98 const std::string kSmallMessage = "bb"; | |
| 99 const std::string kLargeMessage = "aaabbb"; | |
| 100 | |
| 101 const Packet kConnectionRequest{kConnectionRequestHeader}; | |
| 102 const Packet kSmallConnectionResponse{kSmallConnectionResponseHeader}; | |
| 103 const Packet kLargeConnectionResponse{kLargeConnectionResponseHeader}; | |
| 104 | |
| 105 const std::vector<Packet> kSmallPackets{Packet{kDataHeader, 'b', 'b'}}; | |
|
Kyle Horimoto
2016/06/30 01:29:48
Create a constant for each Packet so that they can
jingxuy
2016/06/30 21:59:25
Done.
| |
| 106 const std::vector<Packet> kLargePackets{Packet{kDataHeader, 'a', 'a', 'a'}, | |
| 107 Packet{kDataHeader, 'b', 'b', 'b'}}; | |
| 108 | |
| 109 class MockBluetoothLowEnergyWeavePacketGenerator | |
| 110 : public BluetoothLowEnergyWeavePacketGenerator { | |
| 66 public: | 111 public: |
| 67 MockBluetoothThrottler() {} | 112 MockBluetoothLowEnergyWeavePacketGenerator() |
| 68 ~MockBluetoothThrottler() override {} | 113 : max_packet_size_(kDefaultMaxPacketSize) {} |
| 69 | 114 |
| 70 MOCK_CONST_METHOD0(GetDelay, base::TimeDelta()); | 115 Packet CreateConnectionRequest() override { return kConnectionRequest; } |
| 71 MOCK_METHOD1(OnConnection, void(Connection* connection)); | 116 |
| 117 Packet CreateConnectionResponse() override { | |
|
Kyle Horimoto
2016/06/30 01:29:48
This function should never be called by the client
jingxuy
2016/06/30 21:59:25
Done.
| |
| 118 if (max_packet_size_ == kDefaultMaxPacketSize) { | |
| 119 DLOG(ERROR) << "getting small"; | |
|
Kyle Horimoto
2016/06/30 01:29:48
Please remove these logs. Alternatively, make the
jingxuy
2016/06/30 21:59:25
It's already done in the most recent version.
| |
| 120 return kSmallConnectionResponse; | |
| 121 } else { | |
| 122 DLOG(ERROR) << "getting large"; | |
| 123 return kLargeConnectionResponse; | |
| 124 } | |
| 125 } | |
| 126 | |
| 127 Packet CreateConnectionClose(ReasonForClose reason_for_close) override { | |
| 128 return Packet{static_cast<uint8_t>(reason_for_close)}; | |
|
Kyle Horimoto
2016/06/30 01:29:48
You should use a special header for this just as y
jingxuy
2016/06/30 21:59:26
Done.
| |
| 129 } | |
| 130 | |
| 131 void SetMaxPacketSize(uint16_t size) override { | |
| 132 DLOG(ERROR) << "\n" | |
| 133 << "Setting generator packet size" << size << "\n"; | |
| 134 max_packet_size_ = size; | |
| 135 } | |
| 136 | |
| 137 std::vector<Packet> EncodeDataMessage(std::string message) override { | |
|
Kyle Horimoto
2016/06/30 01:29:48
Please add some string constants which correspond
jingxuy
2016/06/30 21:59:25
Done.
| |
| 138 if (max_packet_size_ == kDefaultMaxPacketSize) { | |
| 139 return kSmallPackets; | |
| 140 } else { | |
| 141 return kLargePackets; | |
| 142 } | |
| 143 } | |
| 72 | 144 |
| 73 private: | 145 private: |
| 74 DISALLOW_COPY_AND_ASSIGN(MockBluetoothThrottler); | 146 uint16_t max_packet_size_; |
|
Kyle Horimoto
2016/06/30 01:29:48
Add a getter for this so that you can test that wh
jingxuy
2016/06/30 21:59:26
I replied in a comment below. A getter is not goin
Kyle Horimoto
2016/06/30 22:36:11
Please test this explicitly. You should create a n
jingxuy
2016/07/01 00:00:44
Done.
| |
| 75 }; | 147 }; |
| 76 | 148 |
| 77 class MockBluetoothLowEnergyCharacteristicsFinder | 149 class MockBluetoothLowEnergyWeavePacketReceiver |
| 78 : public BluetoothLowEnergyCharacteristicsFinder { | 150 : public BluetoothLowEnergyWeavePacketReceiver { |
| 79 public: | 151 public: |
| 80 MockBluetoothLowEnergyCharacteristicsFinder() {} | 152 MockBluetoothLowEnergyWeavePacketReceiver() |
| 81 ~MockBluetoothLowEnergyCharacteristicsFinder() override {} | 153 : BluetoothLowEnergyWeavePacketReceiver(ReceiverType::CLIENT), |
| 154 state_(State::CONNECTING), | |
| 155 max_packet_size_(kDefaultMaxPacketSize) {} | |
| 156 | |
| 157 ReceiverState GetState() override { return state_; } | |
| 158 | |
| 159 uint16_t GetMaxPacketSize() override { return max_packet_size_; } | |
| 160 | |
| 161 ReasonForClose GetReasonForClose() override { | |
| 162 return ReasonForClose::CLOSE_WITHOUT_ERROR; | |
|
Kyle Horimoto
2016/06/30 01:29:48
Don't just return a constant. Add a setter for thi
jingxuy
2016/07/01 00:00:44
Done.
| |
| 163 } | |
| 164 | |
| 165 ReasonForClose GetReasonToClose() override { | |
| 166 return ReasonForClose::UNKNOWN_ERROR; | |
| 167 } | |
| 168 | |
| 169 std::string GetDataMessage() override { | |
| 170 if (max_packet_size_ == kDefaultMaxPacketSize) { | |
| 171 return kSmallMessage; | |
| 172 } else { | |
| 173 return kLargeMessage; | |
| 174 } | |
| 175 } | |
| 176 | |
| 177 ReceiverError GetReceiverError() override { | |
| 178 return ReceiverError::NO_ERROR_DETECTED; | |
| 179 } | |
| 180 | |
| 181 ReceiverState ReceivePacket(const Packet& packet) override { | |
| 182 for (auto chr : packet) { | |
| 183 DLOG(ERROR) << chr; | |
| 184 } | |
| 185 switch (packet[0]) { | |
| 186 case kSmallConnectionResponseHeader: | |
| 187 max_packet_size_ = kDefaultMaxPacketSize; | |
| 188 state_ = ReceiverState::WAITING; | |
| 189 break; | |
| 190 case kLargeConnectionResponseHeader: | |
| 191 max_packet_size_ = kLargeMaxPacketSize; | |
| 192 state_ = ReceiverState::WAITING; | |
| 193 break; | |
| 194 case kDataHeader: | |
| 195 if (packet[1] == 'b') { | |
|
Kyle Horimoto
2016/06/30 01:29:48
Check the entire packet for equality instead of on
jingxuy
2016/06/30 21:59:26
Done.
| |
| 196 state_ = ReceiverState::DATA_READY; | |
| 197 } else { | |
| 198 state_ = ReceiverState::RECEIVING_DATA; | |
| 199 } | |
| 200 break; | |
| 201 default: | |
| 202 NOTREACHED(); | |
| 203 } | |
| 204 return state_; | |
| 205 } | |
| 82 | 206 |
| 83 private: | 207 private: |
| 84 DISALLOW_COPY_AND_ASSIGN(MockBluetoothLowEnergyCharacteristicsFinder); | 208 ReceiverState state_; |
| 209 uint16_t max_packet_size_; | |
| 85 }; | 210 }; |
| 86 | 211 |
| 87 class MockBluetoothLowEnergyConnection : public BluetoothLowEnergyConnection { | 212 class MockBluetoothLowEnergyWeavePacketGeneratorFactory |
| 213 : public BluetoothLowEnergyWeavePacketGenerator::Factory { | |
| 214 private: | |
|
Kyle Horimoto
2016/06/30 01:29:48
Why is this private? Same below.
jingxuy
2016/06/30 21:59:25
it only need to be called from the factory class.
| |
| 215 std::unique_ptr<BluetoothLowEnergyWeavePacketGenerator> BuildInstance() | |
| 216 override { | |
| 217 return std::unique_ptr<MockBluetoothLowEnergyWeavePacketGenerator>( | |
| 218 new MockBluetoothLowEnergyWeavePacketGenerator()); | |
| 219 } | |
| 220 }; | |
| 221 | |
| 222 class MockBluetoothLowEnergyWeavePacketReceiverFactory | |
| 223 : public BluetoothLowEnergyWeavePacketReceiver::Factory { | |
| 224 private: | |
| 225 std::unique_ptr<BluetoothLowEnergyWeavePacketReceiver> BuildInstance( | |
| 226 ReceiverType receiver_type) override { | |
| 227 return std::unique_ptr<MockBluetoothLowEnergyWeavePacketReceiver>( | |
| 228 new MockBluetoothLowEnergyWeavePacketReceiver()); | |
| 229 } | |
| 230 }; | |
| 231 | |
| 232 class TestBluetoothLowEnergyWeaveClientConnection | |
| 233 : public BluetoothLowEnergyWeaveClientConnection { | |
| 88 public: | 234 public: |
| 89 MockBluetoothLowEnergyConnection( | 235 TestBluetoothLowEnergyWeaveClientConnection( |
| 90 const RemoteDevice& remote_device, | 236 const RemoteDevice& remote_device, |
| 91 scoped_refptr<device::BluetoothAdapter> adapter, | 237 scoped_refptr<device::BluetoothAdapter> adapter, |
| 92 const device::BluetoothUUID remote_service_uuid, | 238 const device::BluetoothUUID remote_service_uuid, |
| 93 BluetoothThrottler* bluetooth_throttler, | 239 BluetoothThrottler* bluetooth_throttler, |
| 94 int max_number_of_write_attempts) | 240 int max_number_of_write_attempts) |
| 95 : BluetoothLowEnergyConnection(remote_device, | 241 : BluetoothLowEnergyWeaveClientConnection(remote_device, |
| 96 adapter, | 242 adapter, |
| 97 remote_service_uuid, | 243 remote_service_uuid, |
| 98 bluetooth_throttler, | 244 bluetooth_throttler, |
| 99 max_number_of_write_attempts) {} | 245 max_number_of_write_attempts) {} |
| 100 | 246 |
| 101 ~MockBluetoothLowEnergyConnection() override {} | 247 ~TestBluetoothLowEnergyWeaveClientConnection() override {} |
| 102 | 248 |
| 103 MOCK_METHOD2( | 249 MOCK_METHOD2( |
| 104 CreateCharacteristicsFinder, | 250 CreateCharacteristicsFinder, |
| 105 BluetoothLowEnergyCharacteristicsFinder*( | 251 BluetoothLowEnergyCharacteristicsFinder*( |
| 106 const BluetoothLowEnergyCharacteristicsFinder::SuccessCallback& | 252 const BluetoothLowEnergyCharacteristicsFinder::SuccessCallback& |
| 107 success, | 253 success, |
| 108 const BluetoothLowEnergyCharacteristicsFinder::ErrorCallback& error)); | 254 const BluetoothLowEnergyCharacteristicsFinder::ErrorCallback& error)); |
| 109 | 255 |
| 110 MOCK_METHOD2(OnDidSendMessage, | 256 MOCK_METHOD2(OnDidSendMessage, |
| 111 void(const WireMessage& message, bool success)); | 257 void(const WireMessage& message, bool success)); |
| 112 MOCK_METHOD1(OnBytesReceived, void(const std::string& bytes)); | 258 MOCK_METHOD1(OnBytesReceived, void(const std::string& bytes)); |
| 113 | 259 |
| 114 // Exposing inherited protected methods for testing. | 260 // Exposing inherited protected methods for testing. |
| 115 using BluetoothLowEnergyConnection::GattCharacteristicValueChanged; | 261 using BluetoothLowEnergyWeaveClientConnection::GattCharacteristicValueChanged; |
| 116 using BluetoothLowEnergyConnection::SetTaskRunnerForTesting; | 262 using BluetoothLowEnergyWeaveClientConnection::SetTaskRunnerForTesting; |
| 117 | 263 |
| 118 // Exposing inherited protected fields for testing. | 264 // Exposing inherited protected fields for testing. |
| 119 using BluetoothLowEnergyConnection::status; | 265 using BluetoothLowEnergyWeaveClientConnection::status; |
| 120 using BluetoothLowEnergyConnection::sub_status; | 266 using BluetoothLowEnergyWeaveClientConnection::sub_status; |
| 121 | 267 |
| 122 private: | 268 private: |
| 123 DISALLOW_COPY_AND_ASSIGN(MockBluetoothLowEnergyConnection); | 269 DISALLOW_COPY_AND_ASSIGN(TestBluetoothLowEnergyWeaveClientConnection); |
| 124 }; | 270 }; |
| 125 | 271 |
| 126 } // namespace | 272 } // namespace |
| 127 | 273 |
| 128 class ProximityAuthBluetoothLowEnergyConnectionTest : public testing::Test { | 274 class ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest |
| 275 : public testing::Test { | |
| 129 public: | 276 public: |
| 130 ProximityAuthBluetoothLowEnergyConnectionTest() | 277 ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest() |
| 131 : adapter_(new NiceMock<device::MockBluetoothAdapter>), | 278 : adapter_(new NiceMock<device::MockBluetoothAdapter>), |
| 132 remote_device_(CreateLERemoteDeviceForTest()), | 279 remote_device_(CreateLERemoteDeviceForTest()), |
| 133 service_uuid_(device::BluetoothUUID(kServiceUUID)), | 280 service_uuid_(device::BluetoothUUID(kServiceUUID)), |
| 134 to_peripheral_char_uuid_(device::BluetoothUUID(kToPeripheralCharUUID)), | 281 tx_characteristic_uuid_(device::BluetoothUUID(kTXCharacteristicUUID)), |
| 135 from_peripheral_char_uuid_( | 282 rx_characteristic_uuid_(device::BluetoothUUID(kRXCharacteristicUUID)), |
| 136 device::BluetoothUUID(kFromPeripheralCharUUID)), | |
| 137 notify_session_alias_(NULL), | 283 notify_session_alias_(NULL), |
| 138 bluetooth_throttler_(new NiceMock<MockBluetoothThrottler>), | 284 bluetooth_throttler_(new NiceMock<MockBluetoothThrottler>), |
| 139 task_runner_(new base::TestSimpleTaskRunner) {} | 285 task_runner_(new base::TestSimpleTaskRunner) {} |
| 140 | 286 |
| 141 void SetUp() override { | 287 void SetUp() override { |
| 142 device_ = base::WrapUnique(new NiceMock<device::MockBluetoothDevice>( | 288 device_ = base::WrapUnique(new NiceMock<device::MockBluetoothDevice>( |
| 143 adapter_.get(), 0, kTestRemoteDeviceName, | 289 adapter_.get(), 0, kTestRemoteDeviceName, |
| 144 kTestRemoteDeviceBluetoothAddress, false, false)); | 290 kTestRemoteDeviceBluetoothAddress, false, false)); |
| 145 | 291 |
| 146 service_ = base::WrapUnique(new NiceMock<device::MockBluetoothGattService>( | 292 service_ = base::WrapUnique(new NiceMock<device::MockBluetoothGattService>( |
| 147 device_.get(), kServiceID, service_uuid_, true, false)); | 293 device_.get(), kServiceID, service_uuid_, true, false)); |
| 148 to_peripheral_char_ = | 294 tx_characteristic_ = |
| 149 base::WrapUnique(new NiceMock<device::MockBluetoothGattCharacteristic>( | 295 base::WrapUnique(new NiceMock<device::MockBluetoothGattCharacteristic>( |
| 150 service_.get(), kToPeripheralCharID, to_peripheral_char_uuid_, | 296 service_.get(), kTXCharacteristicID, tx_characteristic_uuid_, false, |
| 151 false, kCharacteristicProperties, | 297 kCharacteristicProperties, |
| 152 device::BluetoothRemoteGattCharacteristic::PERMISSION_NONE)); | 298 device::BluetoothRemoteGattCharacteristic::PERMISSION_NONE)); |
| 153 | 299 |
| 154 from_peripheral_char_ = | 300 rx_characteristic_ = |
| 155 base::WrapUnique(new NiceMock<device::MockBluetoothGattCharacteristic>( | 301 base::WrapUnique(new NiceMock<device::MockBluetoothGattCharacteristic>( |
| 156 service_.get(), kFromPeripheralCharID, from_peripheral_char_uuid_, | 302 service_.get(), kRXCharacteristicID, rx_characteristic_uuid_, false, |
| 157 false, kCharacteristicProperties, | 303 kCharacteristicProperties, |
| 158 device::BluetoothRemoteGattCharacteristic::PERMISSION_NONE)); | 304 device::BluetoothRemoteGattCharacteristic::PERMISSION_NONE)); |
| 159 | 305 |
| 160 device::BluetoothAdapterFactory::SetAdapterForTesting(adapter_); | 306 device::BluetoothAdapterFactory::SetAdapterForTesting(adapter_); |
| 161 | 307 |
| 162 std::vector<const device::BluetoothDevice*> devices; | 308 std::vector<const device::BluetoothDevice*> devices; |
| 163 devices.push_back(device_.get()); | 309 devices.push_back(device_.get()); |
| 164 ON_CALL(*adapter_, GetDevices()).WillByDefault(Return(devices)); | 310 ON_CALL(*adapter_, GetDevices()).WillByDefault(Return(devices)); |
| 165 ON_CALL(*adapter_, GetDevice(kTestRemoteDeviceBluetoothAddress)) | 311 ON_CALL(*adapter_, GetDevice(kTestRemoteDeviceBluetoothAddress)) |
| 166 .WillByDefault(Return(device_.get())); | 312 .WillByDefault(Return(device_.get())); |
| 167 ON_CALL(*device_, GetGattService(kServiceID)) | 313 ON_CALL(*device_, GetGattService(kServiceID)) |
| 168 .WillByDefault(Return(service_.get())); | 314 .WillByDefault(Return(service_.get())); |
| 169 ON_CALL(*service_, GetCharacteristic(kFromPeripheralCharID)) | 315 ON_CALL(*service_, GetCharacteristic(kRXCharacteristicID)) |
| 170 .WillByDefault(Return(from_peripheral_char_.get())); | 316 .WillByDefault(Return(rx_characteristic_.get())); |
| 171 ON_CALL(*service_, GetCharacteristic(kToPeripheralCharID)) | 317 ON_CALL(*service_, GetCharacteristic(kTXCharacteristicID)) |
| 172 .WillByDefault(Return(to_peripheral_char_.get())); | 318 .WillByDefault(Return(tx_characteristic_.get())); |
| 173 } | 319 } |
| 174 | 320 |
| 175 // Creates a BluetoothLowEnergyConnection and verifies it's in DISCONNECTED | 321 // Creates a BluetoothLowEnergyWeaveClientConnection and verifies it's in |
| 176 // state. | 322 // DISCONNECTED state. |
| 177 std::unique_ptr<MockBluetoothLowEnergyConnection> CreateConnection() { | 323 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> |
| 324 CreateConnection() { | |
| 178 EXPECT_CALL(*adapter_, AddObserver(_)); | 325 EXPECT_CALL(*adapter_, AddObserver(_)); |
| 179 EXPECT_CALL(*adapter_, RemoveObserver(_)); | 326 EXPECT_CALL(*adapter_, RemoveObserver(_)); |
| 180 | 327 |
| 181 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( | 328 BluetoothLowEnergyWeavePacketGenerator::Factory::SetInstanceForTesting( |
|
Kyle Horimoto
2016/06/30 01:29:48
These should be moved to SetUp().
jingxuy
2016/06/30 21:59:26
I moved them to the constructor since they only ne
Kyle Horimoto
2016/06/30 22:36:11
You should create a new instance of the generator/
jingxuy
2016/07/01 00:00:44
instance of generator/receiver is still unique to
| |
| 182 new MockBluetoothLowEnergyConnection( | 329 &generator_factory_); |
| 330 BluetoothLowEnergyWeavePacketReceiver::Factory::SetInstanceForTesting( | |
| 331 &receiver_factory_); | |
| 332 | |
| 333 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( | |
| 334 new TestBluetoothLowEnergyWeaveClientConnection( | |
| 183 remote_device_, adapter_, service_uuid_, bluetooth_throttler_.get(), | 335 remote_device_, adapter_, service_uuid_, bluetooth_throttler_.get(), |
| 184 kMaxNumberOfTries)); | 336 kMaxNumberOfTries)); |
| 185 | 337 |
| 186 EXPECT_EQ(connection->sub_status(), | 338 EXPECT_EQ(connection->sub_status(), SubStatus::DISCONNECTED); |
| 187 BluetoothLowEnergyConnection::SubStatus::DISCONNECTED); | |
| 188 EXPECT_EQ(connection->status(), Connection::DISCONNECTED); | 339 EXPECT_EQ(connection->status(), Connection::DISCONNECTED); |
| 189 | 340 |
| 190 connection->SetTaskRunnerForTesting(task_runner_); | 341 connection->SetTaskRunnerForTesting(task_runner_); |
| 191 | 342 |
| 192 return connection; | 343 return connection; |
| 193 } | 344 } |
| 194 | 345 |
| 195 // Transitions |connection| from DISCONNECTED to WAITING_CHARACTERISTICS | 346 // Transitions |connection| from DISCONNECTED to WAITING_CHARACTERISTICS |
| 196 // state, without an existing GATT connection. | 347 // state, without an existing GATT connection. |
| 197 void ConnectGatt(MockBluetoothLowEnergyConnection* connection) { | 348 void ConnectGatt(TestBluetoothLowEnergyWeaveClientConnection* connection) { |
| 198 // Preparing |connection| for a CreateGattConnection call. | 349 // Preparing |connection| for a CreateGattConnection call. |
| 199 EXPECT_CALL(*device_, CreateGattConnection(_, _)) | 350 EXPECT_CALL(*device_, CreateGattConnection(_, _)) |
| 200 .WillOnce(DoAll(SaveArg<0>(&create_gatt_connection_success_callback_), | 351 .WillOnce(DoAll(SaveArg<0>(&create_gatt_connection_success_callback_), |
| 201 SaveArg<1>(&create_gatt_connection_error_callback_))); | 352 SaveArg<1>(&create_gatt_connection_error_callback_))); |
| 202 | 353 |
| 203 // No throttling by default | 354 // No throttling by default |
| 204 EXPECT_CALL(*bluetooth_throttler_, GetDelay()) | 355 EXPECT_CALL(*bluetooth_throttler_, GetDelay()) |
| 205 .WillOnce(Return(base::TimeDelta())); | 356 .WillOnce(Return(base::TimeDelta())); |
| 206 | 357 |
| 207 connection->Connect(); | 358 connection->Connect(); |
| 208 | 359 |
| 209 EXPECT_EQ(connection->sub_status(), | 360 EXPECT_EQ(connection->sub_status(), SubStatus::WAITING_GATT_CONNECTION); |
| 210 BluetoothLowEnergyConnection::SubStatus::WAITING_GATT_CONNECTION); | |
| 211 EXPECT_EQ(connection->status(), Connection::IN_PROGRESS); | 361 EXPECT_EQ(connection->status(), Connection::IN_PROGRESS); |
| 212 | 362 |
| 213 // Preparing |connection| to run |create_gatt_connection_success_callback_|. | 363 // Preparing |connection| to run |create_gatt_connection_success_callback_|. |
| 214 EXPECT_FALSE(create_gatt_connection_error_callback_.is_null()); | 364 EXPECT_FALSE(create_gatt_connection_error_callback_.is_null()); |
| 215 ASSERT_FALSE(create_gatt_connection_success_callback_.is_null()); | 365 ASSERT_FALSE(create_gatt_connection_success_callback_.is_null()); |
| 216 EXPECT_CALL(*connection, CreateCharacteristicsFinder(_, _)) | 366 EXPECT_CALL(*connection, CreateCharacteristicsFinder(_, _)) |
| 217 .WillOnce(DoAll( | 367 .WillOnce(DoAll( |
| 218 SaveArg<0>(&characteristics_finder_success_callback_), | 368 SaveArg<0>(&characteristics_finder_success_callback_), |
| 219 SaveArg<1>(&characteristics_finder_error_callback_), | 369 SaveArg<1>(&characteristics_finder_error_callback_), |
| 220 Return(new NiceMock<MockBluetoothLowEnergyCharacteristicsFinder>))); | 370 Return(new NiceMock<MockBluetoothLowEnergyCharacteristicsFinder>))); |
| 221 | 371 |
| 222 create_gatt_connection_success_callback_.Run( | 372 create_gatt_connection_success_callback_.Run( |
| 223 base::WrapUnique(new NiceMock<device::MockBluetoothGattConnection>( | 373 base::WrapUnique(new NiceMock<device::MockBluetoothGattConnection>( |
| 224 adapter_, kTestRemoteDeviceBluetoothAddress))); | 374 adapter_, kTestRemoteDeviceBluetoothAddress))); |
| 225 | 375 |
| 226 EXPECT_EQ(connection->sub_status(), | 376 EXPECT_EQ(connection->sub_status(), SubStatus::WAITING_CHARACTERISTICS); |
| 227 BluetoothLowEnergyConnection::SubStatus::WAITING_CHARACTERISTICS); | |
| 228 EXPECT_EQ(connection->status(), Connection::IN_PROGRESS); | 377 EXPECT_EQ(connection->status(), Connection::IN_PROGRESS); |
| 229 } | 378 } |
| 230 | 379 |
| 231 // Transitions |connection| from WAITING_CHARACTERISTICS to | 380 // Transitions |connection| from WAITING_CHARACTERISTICS to |
| 232 // WAITING_NOTIFY_SESSION state. | 381 // WAITING_NOTIFY_SESSION state. |
| 233 void CharacteristicsFound(MockBluetoothLowEnergyConnection* connection) { | 382 void CharacteristicsFound( |
| 234 EXPECT_CALL(*from_peripheral_char_, StartNotifySession(_, _)) | 383 TestBluetoothLowEnergyWeaveClientConnection* connection) { |
| 384 EXPECT_CALL(*rx_characteristic_, StartNotifySession(_, _)) | |
| 235 .WillOnce(DoAll(SaveArg<0>(¬ify_session_success_callback_), | 385 .WillOnce(DoAll(SaveArg<0>(¬ify_session_success_callback_), |
| 236 SaveArg<1>(¬ify_session_error_callback_))); | 386 SaveArg<1>(¬ify_session_error_callback_))); |
| 237 EXPECT_FALSE(characteristics_finder_error_callback_.is_null()); | 387 EXPECT_FALSE(characteristics_finder_error_callback_.is_null()); |
| 238 ASSERT_FALSE(characteristics_finder_success_callback_.is_null()); | 388 ASSERT_FALSE(characteristics_finder_success_callback_.is_null()); |
| 239 | 389 |
| 240 characteristics_finder_success_callback_.Run( | 390 characteristics_finder_success_callback_.Run( |
| 241 {service_uuid_, kServiceID}, | 391 {service_uuid_, kServiceID}, |
| 242 {to_peripheral_char_uuid_, kToPeripheralCharID}, | 392 {tx_characteristic_uuid_, kTXCharacteristicID}, |
| 243 {from_peripheral_char_uuid_, kFromPeripheralCharID}); | 393 {rx_characteristic_uuid_, kRXCharacteristicID}); |
| 244 | 394 |
| 245 EXPECT_EQ(connection->sub_status(), | 395 EXPECT_EQ(connection->sub_status(), SubStatus::WAITING_NOTIFY_SESSION); |
| 246 BluetoothLowEnergyConnection::SubStatus::WAITING_NOTIFY_SESSION); | |
| 247 EXPECT_EQ(connection->status(), Connection::IN_PROGRESS); | 396 EXPECT_EQ(connection->status(), Connection::IN_PROGRESS); |
| 248 } | 397 } |
| 249 | 398 |
| 250 // Transitions |connection| from WAITING_NOTIFY_SESSION to | 399 // Transitions |connection| from WAITING_NOTIFY_SESSION to |
| 251 // WAITING_RESPONSE_SIGNAL state. | 400 // WAITING_CONNECTION_RESPONSE state. |
| 252 void NotifySessionStarted(MockBluetoothLowEnergyConnection* connection) { | 401 void NotifySessionStarted( |
| 253 EXPECT_CALL(*to_peripheral_char_, WriteRemoteCharacteristic(_, _, _)) | 402 TestBluetoothLowEnergyWeaveClientConnection* connection) { |
| 403 EXPECT_CALL(*tx_characteristic_, WriteRemoteCharacteristic(_, _, _)) | |
| 254 .WillOnce( | 404 .WillOnce( |
| 255 DoAll(SaveArg<0>(&last_value_written_on_to_peripheral_char_), | 405 DoAll(SaveArg<0>(&last_value_written_on_tx_characteristic_), |
| 256 SaveArg<1>(&write_remote_characteristic_success_callback_), | 406 SaveArg<1>(&write_remote_characteristic_success_callback_), |
| 257 SaveArg<2>(&write_remote_characteristic_error_callback_))); | 407 SaveArg<2>(&write_remote_characteristic_error_callback_))); |
| 258 EXPECT_FALSE(notify_session_error_callback_.is_null()); | 408 EXPECT_FALSE(notify_session_error_callback_.is_null()); |
| 259 ASSERT_FALSE(notify_session_success_callback_.is_null()); | 409 ASSERT_FALSE(notify_session_success_callback_.is_null()); |
| 260 | 410 |
| 261 // Store an alias for the notify session passed |connection|. | 411 // Store an alias for the notify session passed |connection|. |
| 262 std::unique_ptr<device::MockBluetoothGattNotifySession> notify_session( | 412 std::unique_ptr<device::MockBluetoothGattNotifySession> notify_session( |
| 263 new NiceMock<device::MockBluetoothGattNotifySession>( | 413 new NiceMock<device::MockBluetoothGattNotifySession>( |
| 264 kToPeripheralCharID)); | 414 kTXCharacteristicID)); |
| 265 notify_session_alias_ = notify_session.get(); | 415 notify_session_alias_ = notify_session.get(); |
| 266 | 416 |
| 267 notify_session_success_callback_.Run(std::move(notify_session)); | 417 notify_session_success_callback_.Run(std::move(notify_session)); |
| 268 task_runner_->RunUntilIdle(); | 418 task_runner_->RunUntilIdle(); |
| 269 | 419 |
|
Kyle Horimoto
2016/06/30 01:29:48
Assert that a connection request was sent.
jingxuy
2016/06/30 21:59:26
what do you mean? the expect_call on tx_characteri
Kyle Horimoto
2016/06/30 22:36:11
Where? It looks like it only asserts that WriteRem
jingxuy
2016/07/01 00:00:44
It's kind of weird because this function merely st
Kyle Horimoto
2016/07/01 17:23:21
Please test this explicitly here. Some tests call
jingxuy
2016/07/01 19:28:08
Done.
| |
| 270 EXPECT_EQ(connection->sub_status(), | 420 EXPECT_EQ(connection->sub_status(), SubStatus::WAITING_CONNECTION_RESPONSE); |
| 271 BluetoothLowEnergyConnection::SubStatus::WAITING_RESPONSE_SIGNAL); | |
| 272 EXPECT_EQ(connection->status(), Connection::IN_PROGRESS); | 421 EXPECT_EQ(connection->status(), Connection::IN_PROGRESS); |
| 273 } | 422 } |
| 274 | 423 |
| 275 // Transitions |connection| from WAITING_RESPONSE_SIGNAL to CONNECTED state. | 424 // Transitions |connection| from WAITING_CONNECTION_RESPONSE to CONNECTED. |
| 276 void ResponseSignalReceived(MockBluetoothLowEnergyConnection* connection) { | 425 void ConnectionResponseReceived( |
| 277 // Written value contains only the | 426 TestBluetoothLowEnergyWeaveClientConnection* connection, |
| 278 // BluetoothLowEneryConnection::ControlSignal::kInviteToConnectSignal. | 427 uint16_t selected_packet_size) { |
| 279 const std::vector<uint8_t> kInviteToConnectSignal = ToByteVector( | 428 // Written value contains only the mock Connection Request. |
| 280 static_cast<uint32_t>(BluetoothLowEnergyConnection::ControlSignal:: | 429 EXPECT_EQ(last_value_written_on_tx_characteristic_, kConnectionRequest); |
| 281 kInviteToConnectSignal)); | |
| 282 EXPECT_EQ(last_value_written_on_to_peripheral_char_, | |
| 283 kInviteToConnectSignal); | |
| 284 | 430 |
| 285 EXPECT_CALL(*connection, OnDidSendMessage(_, _)).Times(0); | 431 EXPECT_CALL(*connection, OnDidSendMessage(_, _)).Times(0); |
| 286 RunWriteCharacteristicSuccessCallback(); | 432 RunWriteCharacteristicSuccessCallback(); |
| 287 | 433 |
| 288 // Received the | 434 // Received Connection Response. |
| 289 // BluetoothLowEneryConnection::ControlSignal::kInvitationResponseSignal. | 435 if (selected_packet_size == kDefaultMaxPacketSize) { |
| 290 const std::vector<uint8_t> kInvitationResponseSignal = ToByteVector( | 436 connection->GattCharacteristicValueChanged( |
| 291 static_cast<uint32_t>(BluetoothLowEnergyConnection::ControlSignal:: | 437 adapter_.get(), rx_characteristic_.get(), kSmallConnectionResponse); |
| 292 kInvitationResponseSignal)); | 438 } else { |
| 293 connection->GattCharacteristicValueChanged( | 439 connection->GattCharacteristicValueChanged( |
| 294 adapter_.get(), from_peripheral_char_.get(), kInvitationResponseSignal); | 440 adapter_.get(), rx_characteristic_.get(), kLargeConnectionResponse); |
| 441 } | |
| 295 | 442 |
|
Kyle Horimoto
2016/06/30 01:29:48
Assert that the correct data packet size was set o
jingxuy
2016/06/30 21:59:25
the test class has no access to generator referenc
Kyle Horimoto
2016/06/30 22:36:11
Please test this explicitly. You should create an
jingxuy
2016/07/01 00:00:44
Done.
| |
| 296 EXPECT_EQ(connection->sub_status(), | 443 EXPECT_EQ(connection->sub_status(), SubStatus::CONNECTED); |
| 297 BluetoothLowEnergyConnection::SubStatus::CONNECTED); | |
| 298 EXPECT_EQ(connection->status(), Connection::CONNECTED); | 444 EXPECT_EQ(connection->status(), Connection::CONNECTED); |
| 299 } | 445 } |
| 300 | 446 |
| 301 // Transitions |connection| to a DISCONNECTED state regardless of its initial | 447 // Transitions |connection| to a DISCONNECTED state regardless of its initial |
| 302 // state. | 448 // state. |
| 303 void Disconnect(MockBluetoothLowEnergyConnection* connection) { | 449 void Disconnect(TestBluetoothLowEnergyWeaveClientConnection* connection) { |
| 304 // A notify session was previously set. | 450 // A notify session was previously set. |
| 305 if (notify_session_alias_) | 451 if (notify_session_alias_) |
| 306 EXPECT_CALL(*notify_session_alias_, Stop(_)); | 452 EXPECT_CALL(*notify_session_alias_, Stop(_)); |
| 307 | 453 |
| 308 connection->Disconnect(); | 454 connection->Disconnect(); |
| 309 | 455 |
| 310 EXPECT_EQ(connection->sub_status(), | 456 EXPECT_EQ(connection->sub_status(), SubStatus::DISCONNECTED); |
| 311 BluetoothLowEnergyConnection::SubStatus::DISCONNECTED); | |
| 312 EXPECT_EQ(connection->status(), Connection::DISCONNECTED); | 457 EXPECT_EQ(connection->status(), Connection::DISCONNECTED); |
| 313 } | 458 } |
| 314 | 459 |
| 315 void InitializeConnection(MockBluetoothLowEnergyConnection* connection) { | 460 void InitializeConnection( |
| 461 TestBluetoothLowEnergyWeaveClientConnection* connection, | |
| 462 uint32_t selected_packet_size) { | |
| 316 ConnectGatt(connection); | 463 ConnectGatt(connection); |
| 317 CharacteristicsFound(connection); | 464 CharacteristicsFound(connection); |
| 318 NotifySessionStarted(connection); | 465 NotifySessionStarted(connection); |
| 319 ResponseSignalReceived(connection); | 466 ConnectionResponseReceived(connection, selected_packet_size); |
| 320 } | 467 } |
| 321 | 468 |
| 322 void RunWriteCharacteristicSuccessCallback() { | 469 void RunWriteCharacteristicSuccessCallback() { |
| 323 EXPECT_FALSE(write_remote_characteristic_error_callback_.is_null()); | 470 EXPECT_FALSE(write_remote_characteristic_error_callback_.is_null()); |
| 324 ASSERT_FALSE(write_remote_characteristic_success_callback_.is_null()); | 471 ASSERT_FALSE(write_remote_characteristic_success_callback_.is_null()); |
| 325 write_remote_characteristic_success_callback_.Run(); | 472 write_remote_characteristic_success_callback_.Run(); |
| 326 } | 473 } |
| 327 | 474 |
| 328 std::vector<uint8_t> CreateSendSignalWithSize(int message_size) { | |
| 329 std::vector<uint8_t> value = ToByteVector(static_cast<uint32_t>( | |
| 330 BluetoothLowEnergyConnection::ControlSignal::kSendSignal)); | |
| 331 std::vector<uint8_t> size = | |
| 332 ToByteVector(static_cast<uint32_t>(message_size)); | |
| 333 value.insert(value.end(), size.begin(), size.end()); | |
| 334 return value; | |
| 335 } | |
| 336 | |
| 337 std::vector<uint8_t> CreateFirstCharacteristicValue( | |
| 338 const std::string& message, | |
| 339 int size) { | |
| 340 std::vector<uint8_t> value(CreateSendSignalWithSize(size)); | |
| 341 std::vector<uint8_t> bytes(message.begin(), message.end()); | |
| 342 value.insert(value.end(), bytes.begin(), bytes.end()); | |
| 343 return value; | |
| 344 } | |
| 345 | |
| 346 std::vector<uint8_t> ToByteVector(uint32_t value) { | |
| 347 std::vector<uint8_t> bytes(4, 0); | |
| 348 bytes[0] = static_cast<uint8_t>(value); | |
| 349 bytes[1] = static_cast<uint8_t>(value >> 8); | |
| 350 bytes[2] = static_cast<uint8_t>(value >> 16); | |
| 351 bytes[3] = static_cast<uint8_t>(value >> 24); | |
| 352 return bytes; | |
| 353 } | |
| 354 | |
| 355 protected: | 475 protected: |
| 356 scoped_refptr<device::MockBluetoothAdapter> adapter_; | 476 scoped_refptr<device::MockBluetoothAdapter> adapter_; |
| 357 RemoteDevice remote_device_; | 477 RemoteDevice remote_device_; |
| 358 device::BluetoothUUID service_uuid_; | 478 device::BluetoothUUID service_uuid_; |
| 359 device::BluetoothUUID to_peripheral_char_uuid_; | 479 device::BluetoothUUID tx_characteristic_uuid_; |
| 360 device::BluetoothUUID from_peripheral_char_uuid_; | 480 device::BluetoothUUID rx_characteristic_uuid_; |
| 361 std::unique_ptr<device::MockBluetoothDevice> device_; | 481 std::unique_ptr<device::MockBluetoothDevice> device_; |
| 362 std::unique_ptr<device::MockBluetoothGattService> service_; | 482 std::unique_ptr<device::MockBluetoothGattService> service_; |
| 363 std::unique_ptr<device::MockBluetoothGattCharacteristic> to_peripheral_char_; | 483 std::unique_ptr<device::MockBluetoothGattCharacteristic> tx_characteristic_; |
| 364 std::unique_ptr<device::MockBluetoothGattCharacteristic> | 484 std::unique_ptr<device::MockBluetoothGattCharacteristic> rx_characteristic_; |
| 365 from_peripheral_char_; | 485 std::vector<uint8_t> last_value_written_on_tx_characteristic_; |
| 366 std::vector<uint8_t> last_value_written_on_to_peripheral_char_; | |
| 367 device::MockBluetoothGattNotifySession* notify_session_alias_; | 486 device::MockBluetoothGattNotifySession* notify_session_alias_; |
| 368 std::unique_ptr<MockBluetoothThrottler> bluetooth_throttler_; | 487 std::unique_ptr<MockBluetoothThrottler> bluetooth_throttler_; |
| 369 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | 488 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
| 370 base::MessageLoop message_loop_; | 489 base::MessageLoop message_loop_; |
| 371 | 490 |
| 372 // Callbacks | 491 // Callbacks |
| 373 device::BluetoothDevice::GattConnectionCallback | 492 device::BluetoothDevice::GattConnectionCallback |
| 374 create_gatt_connection_success_callback_; | 493 create_gatt_connection_success_callback_; |
| 375 device::BluetoothDevice::ConnectErrorCallback | 494 device::BluetoothDevice::ConnectErrorCallback |
| 376 create_gatt_connection_error_callback_; | 495 create_gatt_connection_error_callback_; |
| 377 | 496 |
| 378 BluetoothLowEnergyCharacteristicsFinder::SuccessCallback | 497 BluetoothLowEnergyCharacteristicsFinder::SuccessCallback |
| 379 characteristics_finder_success_callback_; | 498 characteristics_finder_success_callback_; |
| 380 BluetoothLowEnergyCharacteristicsFinder::ErrorCallback | 499 BluetoothLowEnergyCharacteristicsFinder::ErrorCallback |
| 381 characteristics_finder_error_callback_; | 500 characteristics_finder_error_callback_; |
| 382 | 501 |
| 383 device::BluetoothRemoteGattCharacteristic::NotifySessionCallback | 502 device::BluetoothRemoteGattCharacteristic::NotifySessionCallback |
| 384 notify_session_success_callback_; | 503 notify_session_success_callback_; |
| 385 device::BluetoothRemoteGattCharacteristic::ErrorCallback | 504 device::BluetoothRemoteGattCharacteristic::ErrorCallback |
| 386 notify_session_error_callback_; | 505 notify_session_error_callback_; |
| 387 | 506 |
| 388 base::Closure write_remote_characteristic_success_callback_; | 507 base::Closure write_remote_characteristic_success_callback_; |
| 389 device::BluetoothRemoteGattCharacteristic::ErrorCallback | 508 device::BluetoothRemoteGattCharacteristic::ErrorCallback |
| 390 write_remote_characteristic_error_callback_; | 509 write_remote_characteristic_error_callback_; |
| 510 | |
| 511 MockBluetoothLowEnergyWeavePacketGeneratorFactory generator_factory_; | |
| 512 MockBluetoothLowEnergyWeavePacketReceiverFactory receiver_factory_; | |
| 391 }; | 513 }; |
| 392 | 514 |
| 393 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, | 515 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, |
| 394 CreateAndDestroyWithouthConnectCallDoesntCrash) { | 516 CreateAndDestroyWithoutConnectCallDoesntCrash) { |
| 395 BluetoothLowEnergyConnection connection( | 517 BluetoothLowEnergyWeaveClientConnection connection( |
| 396 remote_device_, adapter_, service_uuid_, bluetooth_throttler_.get(), | 518 remote_device_, adapter_, service_uuid_, bluetooth_throttler_.get(), |
| 397 kMaxNumberOfTries); | 519 kMaxNumberOfTries); |
| 398 } | 520 } |
| 399 | 521 |
| 400 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, | 522 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, |
| 401 Disconect_WithoutConnectDoesntCrash) { | 523 DisconectWithoutConnectDoesntCrash) { |
| 402 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( | 524 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( |
| 403 CreateConnection()); | 525 CreateConnection()); |
| 404 Disconnect(connection.get()); | 526 Disconnect(connection.get()); |
| 405 } | 527 } |
| 406 | 528 |
| 407 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, Connect_Success) { | 529 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, |
| 408 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( | 530 ConnectSuccess) { |
| 531 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( | |
| 409 CreateConnection()); | 532 CreateConnection()); |
| 410 ConnectGatt(connection.get()); | 533 ConnectGatt(connection.get()); |
| 411 CharacteristicsFound(connection.get()); | 534 CharacteristicsFound(connection.get()); |
| 412 NotifySessionStarted(connection.get()); | 535 NotifySessionStarted(connection.get()); |
| 413 ResponseSignalReceived(connection.get()); | 536 ConnectionResponseReceived(connection.get(), kDefaultMaxPacketSize); |
| 414 } | 537 } |
| 415 | 538 |
| 416 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, | 539 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, |
| 417 Connect_Success_Disconnect) { | 540 ConnectSuccessDisconnect) { |
| 418 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( | 541 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( |
| 419 CreateConnection()); | 542 CreateConnection()); |
| 420 InitializeConnection(connection.get()); | 543 InitializeConnection(connection.get(), kDefaultMaxPacketSize); |
| 421 Disconnect(connection.get()); | 544 Disconnect(connection.get()); |
| 422 } | 545 } |
| 423 | 546 |
| 424 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, | 547 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, |
| 425 Connect_Incomplete_Disconnect_FromWaitingCharacteristicsState) { | 548 ConnectIncompleteDisconnectFromWaitingCharacteristicsState) { |
| 426 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( | 549 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( |
| 427 CreateConnection()); | 550 CreateConnection()); |
| 428 ConnectGatt(connection.get()); | 551 ConnectGatt(connection.get()); |
| 429 Disconnect(connection.get()); | 552 Disconnect(connection.get()); |
| 430 } | 553 } |
| 431 | 554 |
| 432 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, | 555 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, |
| 433 Connect_Incomplete_Disconnect_FromWaitingNotifySessionState) { | 556 ConnectIncompleteDisconnectFromWaitingNotifySessionState) { |
| 434 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( | 557 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( |
| 435 CreateConnection()); | 558 CreateConnection()); |
| 436 ConnectGatt(connection.get()); | 559 ConnectGatt(connection.get()); |
| 437 CharacteristicsFound(connection.get()); | 560 CharacteristicsFound(connection.get()); |
| 438 Disconnect(connection.get()); | 561 Disconnect(connection.get()); |
| 439 } | 562 } |
| 440 | 563 |
| 441 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, | 564 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, |
| 442 Connect_Incomplete_Disconnect_FromWaitingResponseSignalState) { | 565 ConnectIncompleteDisconnectFromWaitingConnectionResponseState) { |
| 443 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( | 566 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( |
| 444 CreateConnection()); | 567 CreateConnection()); |
| 445 ConnectGatt(connection.get()); | 568 ConnectGatt(connection.get()); |
| 446 CharacteristicsFound(connection.get()); | 569 CharacteristicsFound(connection.get()); |
| 447 NotifySessionStarted(connection.get()); | 570 NotifySessionStarted(connection.get()); |
| 448 Disconnect(connection.get()); | 571 Disconnect(connection.get()); |
| 449 } | 572 } |
| 450 | 573 |
| 451 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, | 574 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, |
| 452 Connect_Fails_CharacteristicsNotFound) { | 575 ConnectFailsCharacteristicsNotFound) { |
| 453 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( | 576 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( |
| 454 CreateConnection()); | 577 CreateConnection()); |
| 455 ConnectGatt(connection.get()); | 578 ConnectGatt(connection.get()); |
| 456 | 579 |
| 457 EXPECT_CALL(*from_peripheral_char_, StartNotifySession(_, _)).Times(0); | 580 EXPECT_CALL(*rx_characteristic_, StartNotifySession(_, _)).Times(0); |
| 458 EXPECT_FALSE(characteristics_finder_success_callback_.is_null()); | 581 EXPECT_FALSE(characteristics_finder_success_callback_.is_null()); |
| 459 ASSERT_FALSE(characteristics_finder_error_callback_.is_null()); | 582 ASSERT_FALSE(characteristics_finder_error_callback_.is_null()); |
| 460 | 583 |
| 461 characteristics_finder_error_callback_.Run( | 584 characteristics_finder_error_callback_.Run( |
| 462 {to_peripheral_char_uuid_, kToPeripheralCharID}, | 585 {tx_characteristic_uuid_, kTXCharacteristicID}, |
| 463 {from_peripheral_char_uuid_, kFromPeripheralCharID}); | 586 {rx_characteristic_uuid_, kRXCharacteristicID}); |
| 464 | 587 |
| 465 EXPECT_EQ(connection->sub_status(), | 588 EXPECT_EQ(connection->sub_status(), SubStatus::DISCONNECTED); |
| 466 BluetoothLowEnergyConnection::SubStatus::DISCONNECTED); | |
| 467 EXPECT_EQ(connection->status(), Connection::DISCONNECTED); | 589 EXPECT_EQ(connection->status(), Connection::DISCONNECTED); |
| 468 } | 590 } |
| 469 | 591 |
| 470 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, | 592 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, |
| 471 Connect_Fails_NotifySessionError) { | 593 ConnectFailsNotifySessionError) { |
| 472 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( | 594 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( |
| 473 CreateConnection()); | 595 CreateConnection()); |
| 474 ConnectGatt(connection.get()); | 596 ConnectGatt(connection.get()); |
| 475 CharacteristicsFound(connection.get()); | 597 CharacteristicsFound(connection.get()); |
| 476 | 598 |
| 477 EXPECT_CALL(*to_peripheral_char_, WriteRemoteCharacteristic(_, _, _)) | 599 EXPECT_CALL(*tx_characteristic_, WriteRemoteCharacteristic(_, _, _)).Times(0); |
| 478 .Times(0); | |
| 479 EXPECT_FALSE(notify_session_success_callback_.is_null()); | 600 EXPECT_FALSE(notify_session_success_callback_.is_null()); |
| 480 ASSERT_FALSE(notify_session_error_callback_.is_null()); | 601 ASSERT_FALSE(notify_session_error_callback_.is_null()); |
| 481 | 602 |
| 482 notify_session_error_callback_.Run( | 603 notify_session_error_callback_.Run( |
| 483 device::BluetoothRemoteGattService::GATT_ERROR_UNKNOWN); | 604 device::BluetoothRemoteGattService::GATT_ERROR_UNKNOWN); |
| 484 | 605 |
| 485 EXPECT_EQ(connection->sub_status(), | 606 EXPECT_EQ(connection->sub_status(), SubStatus::DISCONNECTED); |
| 486 BluetoothLowEnergyConnection::SubStatus::DISCONNECTED); | |
| 487 EXPECT_EQ(connection->status(), Connection::DISCONNECTED); | 607 EXPECT_EQ(connection->status(), Connection::DISCONNECTED); |
| 488 } | 608 } |
| 489 | 609 |
| 490 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, | 610 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, |
| 491 Connect_Fails_ErrorSendingInviteToConnectSignal) { | 611 ConnectFailsErrorSendingConnectionRequest) { |
| 492 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( | 612 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( |
| 493 CreateConnection()); | 613 CreateConnection()); |
| 494 ConnectGatt(connection.get()); | 614 ConnectGatt(connection.get()); |
| 495 CharacteristicsFound(connection.get()); | 615 CharacteristicsFound(connection.get()); |
| 496 NotifySessionStarted(connection.get()); | 616 NotifySessionStarted(connection.get()); |
| 497 | 617 |
| 498 // |connection| will call WriteRemoteCharacteristics(_,_) to try to send the | 618 // |connection| will call WriteRemoteCharacteristics(_,_) to try to send the |
| 499 // message |kMaxNumberOfTries| times. There is alredy one EXPECTA_CALL for | 619 // message |kMaxNumberOfTries| times. There is alredy one EXPECTA_CALL for |
| 500 // WriteRemoteCharacteristic(_,_,_) in NotifySessionStated, that's why we use | 620 // WriteRemoteCharacteristic(_,_,_) in NotifySessionStated, that's why we use |
| 501 // |kMaxNumberOfTries-1| in the EXPECT_CALL statement. | 621 // |kMaxNumberOfTries-1| in the EXPECT_CALL statement. |
| 502 EXPECT_CALL(*connection, OnDidSendMessage(_, _)).Times(0); | 622 EXPECT_CALL(*connection, OnDidSendMessage(_, _)).Times(0); |
| 503 EXPECT_CALL(*to_peripheral_char_, WriteRemoteCharacteristic(_, _, _)) | 623 EXPECT_CALL(*tx_characteristic_, WriteRemoteCharacteristic(_, _, _)) |
| 504 .Times(kMaxNumberOfTries - 1) | 624 .Times(kMaxNumberOfTries - 1) |
| 505 .WillRepeatedly( | 625 .WillRepeatedly( |
| 506 DoAll(SaveArg<0>(&last_value_written_on_to_peripheral_char_), | 626 DoAll(SaveArg<0>(&last_value_written_on_tx_characteristic_), |
| 507 SaveArg<1>(&write_remote_characteristic_success_callback_), | 627 SaveArg<1>(&write_remote_characteristic_success_callback_), |
| 508 SaveArg<2>(&write_remote_characteristic_error_callback_))); | 628 SaveArg<2>(&write_remote_characteristic_error_callback_))); |
| 509 | 629 |
| 510 for (int i = 0; i < kMaxNumberOfTries; i++) { | 630 for (int i = 0; i < kMaxNumberOfTries; i++) { |
| 511 const std::vector<uint8_t> kInviteToConnectSignal = ToByteVector( | 631 EXPECT_EQ(last_value_written_on_tx_characteristic_, kConnectionRequest); |
| 512 static_cast<uint32_t>(BluetoothLowEnergyConnection::ControlSignal:: | |
| 513 kInviteToConnectSignal)); | |
| 514 EXPECT_EQ(last_value_written_on_to_peripheral_char_, | |
| 515 kInviteToConnectSignal); | |
| 516 ASSERT_FALSE(write_remote_characteristic_error_callback_.is_null()); | 632 ASSERT_FALSE(write_remote_characteristic_error_callback_.is_null()); |
| 517 EXPECT_FALSE(write_remote_characteristic_success_callback_.is_null()); | 633 EXPECT_FALSE(write_remote_characteristic_success_callback_.is_null()); |
| 518 write_remote_characteristic_error_callback_.Run( | 634 write_remote_characteristic_error_callback_.Run( |
| 519 device::BluetoothRemoteGattService::GATT_ERROR_UNKNOWN); | 635 device::BluetoothRemoteGattService::GATT_ERROR_UNKNOWN); |
| 520 } | 636 } |
| 521 | 637 |
| 522 EXPECT_EQ(connection->sub_status(), | 638 EXPECT_EQ(connection->sub_status(), SubStatus::DISCONNECTED); |
| 523 BluetoothLowEnergyConnection::SubStatus::DISCONNECTED); | |
| 524 EXPECT_EQ(connection->status(), Connection::DISCONNECTED); | 639 EXPECT_EQ(connection->status(), Connection::DISCONNECTED); |
| 525 } | 640 } |
| 526 | 641 |
| 527 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, | 642 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, |
| 528 Receive_MessageSmallerThanCharacteristicSize) { | 643 ReceiveMessageSmallerThanCharacteristicSize) { |
| 529 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( | 644 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( |
| 530 CreateConnection()); | 645 CreateConnection()); |
| 531 InitializeConnection(connection.get()); | 646 InitializeConnection(connection.get(), kDefaultMaxPacketSize); |
| 532 | 647 |
| 533 std::string received_bytes; | 648 std::string received_bytes; |
| 534 EXPECT_CALL(*connection, OnBytesReceived(_)) | 649 EXPECT_CALL(*connection, OnBytesReceived(_)) |
| 535 .WillOnce(SaveArg<0>(&received_bytes)); | 650 .WillOnce(SaveArg<0>(&received_bytes)); |
| 536 | 651 |
| 537 // Message (bytes) that is going to be received. | 652 connection->GattCharacteristicValueChanged( |
| 538 std::string message(100, 'A'); | 653 adapter_.get(), rx_characteristic_.get(), kSmallPackets[0]); |
| 539 | 654 |
| 540 // Sending the |kSendSignal| + |message_size| + |message|. | 655 EXPECT_EQ(received_bytes, kSmallMessage); |
| 541 connection->GattCharacteristicValueChanged( | |
| 542 adapter_.get(), from_peripheral_char_.get(), | |
| 543 CreateFirstCharacteristicValue(message, message.size())); | |
| 544 | |
| 545 EXPECT_EQ(received_bytes, message); | |
| 546 } | 656 } |
| 547 | 657 |
| 548 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, | 658 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, |
| 549 Receive_MessageLargerThanCharacteristicSize) { | 659 ReceiveMessageLargerThanCharacteristicSize) { |
| 550 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( | 660 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( |
| 551 CreateConnection()); | 661 CreateConnection()); |
| 552 InitializeConnection(connection.get()); | 662 |
| 663 const uint32_t selected_packet_size = 500; | |
| 664 InitializeConnection(connection.get(), selected_packet_size); | |
| 553 | 665 |
| 554 std::string received_bytes; | 666 std::string received_bytes; |
| 555 int chunk_size = 500; | |
| 556 EXPECT_CALL(*connection, OnBytesReceived(_)) | 667 EXPECT_CALL(*connection, OnBytesReceived(_)) |
| 557 .WillOnce(SaveArg<0>(&received_bytes)); | 668 .WillOnce(SaveArg<0>(&received_bytes)); |
| 558 | 669 |
| 559 // Message (bytes) that is going to be received. | 670 std::vector<Packet> packets = kLargePackets; |
| 560 int message_size = 600; | |
| 561 std::string message(message_size, 'A'); | |
| 562 | 671 |
| 563 // Sending the |kSendSignal| + |message_size| + |message| (truncated at | 672 for (auto packet : packets) { |
| 564 // |chunk_size|). | 673 connection->GattCharacteristicValueChanged( |
| 565 int first_write_payload_size = | 674 adapter_.get(), rx_characteristic_.get(), packet); |
| 566 chunk_size - CreateSendSignalWithSize(message_size).size(); | 675 } |
| 567 connection->GattCharacteristicValueChanged( | 676 EXPECT_EQ(received_bytes, kLargeMessage); |
| 568 adapter_.get(), from_peripheral_char_.get(), | |
| 569 CreateFirstCharacteristicValue( | |
| 570 message.substr(0, first_write_payload_size), message.size())); | |
| 571 | |
| 572 // Sending the remaining bytes. | |
| 573 std::vector<uint8_t> value; | |
| 574 value.push_back(0); | |
| 575 value.insert(value.end(), message.begin() + first_write_payload_size, | |
| 576 message.end()); | |
| 577 connection->GattCharacteristicValueChanged( | |
| 578 adapter_.get(), from_peripheral_char_.get(), value); | |
| 579 | |
| 580 EXPECT_EQ(received_bytes, message); | |
| 581 } | 677 } |
| 582 | 678 |
| 583 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, | 679 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, |
| 584 SendMessage_SmallerThanCharacteristicSize) { | 680 SendMessageSmallerThanCharacteristicSize) { |
| 585 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( | 681 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( |
| 586 CreateConnection()); | 682 CreateConnection()); |
| 587 InitializeConnection(connection.get()); | 683 InitializeConnection(connection.get(), kDefaultMaxPacketSize); |
| 588 | 684 |
| 589 // Expecting a first call of WriteRemoteCharacteristic, after SendMessage is | 685 // Expecting a first call of WriteRemoteCharacteristic, after SendMessage is |
| 590 // called. | 686 // called. |
| 591 EXPECT_CALL(*to_peripheral_char_, WriteRemoteCharacteristic(_, _, _)) | 687 EXPECT_CALL(*tx_characteristic_, WriteRemoteCharacteristic(_, _, _)) |
| 592 .WillOnce( | 688 .WillOnce( |
| 593 DoAll(SaveArg<0>(&last_value_written_on_to_peripheral_char_), | 689 DoAll(SaveArg<0>(&last_value_written_on_tx_characteristic_), |
| 594 SaveArg<1>(&write_remote_characteristic_success_callback_), | 690 SaveArg<1>(&write_remote_characteristic_success_callback_), |
| 595 SaveArg<2>(&write_remote_characteristic_error_callback_))); | 691 SaveArg<2>(&write_remote_characteristic_error_callback_))); |
| 596 | 692 |
| 597 // Message (bytes) that is going to be sent. | 693 connection->SendMessage(base::WrapUnique(new FakeWireMessage(kSmallMessage))); |
| 598 int message_size = 100; | |
| 599 std::string message(message_size, 'A'); | |
| 600 message[0] = 'B'; | |
| 601 connection->SendMessage(base::WrapUnique(new FakeWireMessage(message))); | |
| 602 | 694 |
| 603 // Expecting that |kSendSignal| + |message_size| + |message| was written. | 695 EXPECT_EQ(last_value_written_on_tx_characteristic_, kSmallPackets[0]); |
| 604 EXPECT_EQ(last_value_written_on_to_peripheral_char_, | |
| 605 CreateFirstCharacteristicValue(message, message.size())); | |
| 606 EXPECT_CALL(*connection, OnDidSendMessage(_, _)); | 696 EXPECT_CALL(*connection, OnDidSendMessage(_, _)); |
| 607 | 697 |
| 608 RunWriteCharacteristicSuccessCallback(); | 698 RunWriteCharacteristicSuccessCallback(); |
| 609 } | 699 } |
| 610 | 700 |
| 611 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, | 701 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, |
| 612 SendMessage_LagerThanCharacteristicSize) { | 702 SendMessageLargerThanCharacteristicSize) { |
| 613 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( | 703 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( |
| 614 CreateConnection()); | 704 CreateConnection()); |
| 615 InitializeConnection(connection.get()); | 705 |
| 706 const uint32_t selected_packet_size = 500; | |
| 707 InitializeConnection(connection.get(), selected_packet_size); | |
| 616 | 708 |
| 617 // Expecting a first call of WriteRemoteCharacteristic, after SendMessage is | 709 // Expecting a first call of WriteRemoteCharacteristic, after SendMessage is |
| 618 // called. | 710 // called. |
| 619 EXPECT_CALL(*to_peripheral_char_, WriteRemoteCharacteristic(_, _, _)) | 711 EXPECT_CALL(*tx_characteristic_, WriteRemoteCharacteristic(_, _, _)) |
| 620 .WillOnce( | 712 .WillOnce( |
| 621 DoAll(SaveArg<0>(&last_value_written_on_to_peripheral_char_), | 713 DoAll(SaveArg<0>(&last_value_written_on_tx_characteristic_), |
| 622 SaveArg<1>(&write_remote_characteristic_success_callback_), | 714 SaveArg<1>(&write_remote_characteristic_success_callback_), |
| 623 SaveArg<2>(&write_remote_characteristic_error_callback_))); | 715 SaveArg<2>(&write_remote_characteristic_error_callback_))); |
| 624 | 716 |
| 625 // Message (bytes) that is going to be sent. | 717 connection->SendMessage(base::WrapUnique(new FakeWireMessage(kLargeMessage))); |
| 626 int message_size = 600; | |
| 627 std::string message(message_size, 'A'); | |
| 628 message[0] = 'B'; | |
| 629 connection->SendMessage(base::WrapUnique(new FakeWireMessage(message))); | |
| 630 | 718 |
| 631 // Expecting that |kSendSignal| + |message_size| was written in the first 8 | 719 EXPECT_EQ(last_value_written_on_tx_characteristic_, kLargePackets[0]); |
| 632 // bytes. | |
| 633 std::vector<uint8_t> prefix( | |
| 634 last_value_written_on_to_peripheral_char_.begin(), | |
| 635 last_value_written_on_to_peripheral_char_.begin() + 8); | |
| 636 EXPECT_EQ(prefix, CreateSendSignalWithSize(message_size)); | |
| 637 std::vector<uint8_t> bytes_received( | 720 std::vector<uint8_t> bytes_received( |
| 638 last_value_written_on_to_peripheral_char_.begin() + 8, | 721 last_value_written_on_tx_characteristic_.begin() + 1, |
| 639 last_value_written_on_to_peripheral_char_.end()); | 722 last_value_written_on_tx_characteristic_.end()); |
| 640 | 723 |
| 641 // Expecting a second call of WriteRemoteCharacteristic, after success | 724 EXPECT_CALL(*tx_characteristic_, WriteRemoteCharacteristic(_, _, _)) |
| 642 // callback is called. | |
| 643 EXPECT_CALL(*to_peripheral_char_, WriteRemoteCharacteristic(_, _, _)) | |
| 644 .WillOnce( | 725 .WillOnce( |
| 645 DoAll(SaveArg<0>(&last_value_written_on_to_peripheral_char_), | 726 DoAll(SaveArg<0>(&last_value_written_on_tx_characteristic_), |
| 646 SaveArg<1>(&write_remote_characteristic_success_callback_), | 727 SaveArg<1>(&write_remote_characteristic_success_callback_), |
| 647 SaveArg<2>(&write_remote_characteristic_error_callback_))); | 728 SaveArg<2>(&write_remote_characteristic_error_callback_))); |
| 648 | 729 |
| 649 RunWriteCharacteristicSuccessCallback(); | 730 RunWriteCharacteristicSuccessCallback(); |
| 650 bytes_received.insert(bytes_received.end(), | 731 bytes_received.insert(bytes_received.end(), |
| 651 last_value_written_on_to_peripheral_char_.begin() + 1, | 732 last_value_written_on_tx_characteristic_.begin() + 1, |
| 652 last_value_written_on_to_peripheral_char_.end()); | 733 last_value_written_on_tx_characteristic_.end()); |
| 653 | 734 |
| 654 // Expecting that the message was written. | 735 std::vector<uint8_t> expected(kLargeMessage.begin(), kLargeMessage.end()); |
| 655 std::vector<uint8_t> expected_value(message.begin(), message.end()); | 736 EXPECT_EQ(expected, bytes_received); |
| 656 EXPECT_EQ(expected_value.size(), bytes_received.size()); | |
| 657 EXPECT_EQ(expected_value, bytes_received); | |
| 658 | 737 |
| 659 EXPECT_CALL(*connection, OnDidSendMessage(_, _)); | 738 EXPECT_CALL(*connection, OnDidSendMessage(_, _)); |
| 660 RunWriteCharacteristicSuccessCallback(); | 739 RunWriteCharacteristicSuccessCallback(); |
| 661 } | 740 } |
| 662 | 741 |
| 663 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, | 742 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, |
| 664 Connect_AfterADelayWhenThrottled) { | 743 ConnectAfterDelayWhenThrottled) { |
| 665 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( | 744 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( |
| 666 CreateConnection()); | 745 CreateConnection()); |
| 667 | 746 |
| 668 EXPECT_CALL(*bluetooth_throttler_, GetDelay()) | 747 EXPECT_CALL(*bluetooth_throttler_, GetDelay()) |
| 669 .WillOnce(Return(base::TimeDelta(base::TimeDelta::FromSeconds(1)))); | 748 .WillOnce(Return(base::TimeDelta(base::TimeDelta::FromSeconds(1)))); |
| 670 EXPECT_CALL(*device_, CreateGattConnection(_, _)) | 749 EXPECT_CALL(*device_, CreateGattConnection(_, _)) |
| 671 .WillOnce(DoAll(SaveArg<0>(&create_gatt_connection_success_callback_), | 750 .WillOnce(DoAll(SaveArg<0>(&create_gatt_connection_success_callback_), |
| 672 SaveArg<1>(&create_gatt_connection_error_callback_))); | 751 SaveArg<1>(&create_gatt_connection_error_callback_))); |
| 673 | 752 |
| 674 // No GATT connection should be created before the delay. | 753 // No GATT connection should be created before the delay. |
| 675 connection->Connect(); | 754 connection->Connect(); |
| 676 EXPECT_EQ(connection->sub_status(), | 755 EXPECT_EQ(connection->sub_status(), SubStatus::WAITING_GATT_CONNECTION); |
| 677 BluetoothLowEnergyConnection::SubStatus::WAITING_GATT_CONNECTION); | |
| 678 EXPECT_EQ(connection->status(), Connection::IN_PROGRESS); | 756 EXPECT_EQ(connection->status(), Connection::IN_PROGRESS); |
| 679 EXPECT_TRUE(create_gatt_connection_error_callback_.is_null()); | 757 EXPECT_TRUE(create_gatt_connection_error_callback_.is_null()); |
| 680 EXPECT_TRUE(create_gatt_connection_success_callback_.is_null()); | 758 EXPECT_TRUE(create_gatt_connection_success_callback_.is_null()); |
| 681 | 759 |
| 682 // A GATT connection should be created after the delay. | 760 // A GATT connection should be created after the delay. |
| 683 task_runner_->RunUntilIdle(); | 761 task_runner_->RunUntilIdle(); |
| 684 EXPECT_FALSE(create_gatt_connection_error_callback_.is_null()); | 762 EXPECT_FALSE(create_gatt_connection_error_callback_.is_null()); |
| 685 ASSERT_FALSE(create_gatt_connection_success_callback_.is_null()); | 763 ASSERT_FALSE(create_gatt_connection_success_callback_.is_null()); |
| 686 | 764 |
| 687 // Preparing |connection| to run |create_gatt_connection_success_callback_|. | 765 // Preparing |connection| to run |create_gatt_connection_success_callback_|. |
| 688 EXPECT_CALL(*connection, CreateCharacteristicsFinder(_, _)) | 766 EXPECT_CALL(*connection, CreateCharacteristicsFinder(_, _)) |
| 689 .WillOnce(DoAll( | 767 .WillOnce(DoAll( |
| 690 SaveArg<0>(&characteristics_finder_success_callback_), | 768 SaveArg<0>(&characteristics_finder_success_callback_), |
| 691 SaveArg<1>(&characteristics_finder_error_callback_), | 769 SaveArg<1>(&characteristics_finder_error_callback_), |
| 692 Return(new NiceMock<MockBluetoothLowEnergyCharacteristicsFinder>))); | 770 Return(new NiceMock<MockBluetoothLowEnergyCharacteristicsFinder>))); |
| 693 | 771 |
| 694 create_gatt_connection_success_callback_.Run( | 772 create_gatt_connection_success_callback_.Run( |
| 695 base::WrapUnique(new NiceMock<device::MockBluetoothGattConnection>( | 773 base::WrapUnique(new NiceMock<device::MockBluetoothGattConnection>( |
| 696 adapter_, kTestRemoteDeviceBluetoothAddress))); | 774 adapter_, kTestRemoteDeviceBluetoothAddress))); |
| 697 | 775 |
| 698 CharacteristicsFound(connection.get()); | 776 CharacteristicsFound(connection.get()); |
| 699 NotifySessionStarted(connection.get()); | 777 NotifySessionStarted(connection.get()); |
| 700 ResponseSignalReceived(connection.get()); | 778 ConnectionResponseReceived(connection.get(), kDefaultMaxPacketSize); |
| 701 } | 779 } |
| 702 | 780 |
| 781 } // namespace weave | |
| 782 | |
| 703 } // namespace proximity_auth | 783 } // namespace proximity_auth |
| OLD | NEW |