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

Side by Side Diff: components/proximity_auth/ble/bluetooth_low_energy_connection_unittest.cc

Issue 1898643002: Refactor device::BluetoothGattXXX classes to split into remote/local. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 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_connection.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/macros.h" 11 #include "base/macros.h"
12 #include "base/memory/ref_counted.h" 12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h" 13 #include "base/memory/scoped_ptr.h"
14 #include "base/message_loop/message_loop.h" 14 #include "base/message_loop/message_loop.h"
15 #include "base/run_loop.h" 15 #include "base/run_loop.h"
16 #include "base/test/test_simple_task_runner.h" 16 #include "base/test/test_simple_task_runner.h"
17 #include "components/proximity_auth/ble/bluetooth_low_energy_characteristics_fin der.h" 17 #include "components/proximity_auth/ble/bluetooth_low_energy_characteristics_fin der.h"
18 #include "components/proximity_auth/bluetooth_throttler.h" 18 #include "components/proximity_auth/bluetooth_throttler.h"
19 #include "components/proximity_auth/connection_finder.h" 19 #include "components/proximity_auth/connection_finder.h"
20 #include "components/proximity_auth/proximity_auth_test_util.h" 20 #include "components/proximity_auth/proximity_auth_test_util.h"
21 #include "components/proximity_auth/remote_device.h" 21 #include "components/proximity_auth/remote_device.h"
22 #include "components/proximity_auth/wire_message.h" 22 #include "components/proximity_auth/wire_message.h"
23 #include "device/bluetooth/bluetooth_adapter_factory.h" 23 #include "device/bluetooth/bluetooth_adapter_factory.h"
24 #include "device/bluetooth/bluetooth_gatt_characteristic.h" 24 #include "device/bluetooth/bluetooth_remote_gatt_characteristic.h"
25 #include "device/bluetooth/bluetooth_uuid.h" 25 #include "device/bluetooth/bluetooth_uuid.h"
26 #include "device/bluetooth/test/mock_bluetooth_adapter.h" 26 #include "device/bluetooth/test/mock_bluetooth_adapter.h"
27 #include "device/bluetooth/test/mock_bluetooth_device.h" 27 #include "device/bluetooth/test/mock_bluetooth_device.h"
28 #include "device/bluetooth/test/mock_bluetooth_discovery_session.h" 28 #include "device/bluetooth/test/mock_bluetooth_discovery_session.h"
29 #include "device/bluetooth/test/mock_bluetooth_gatt_characteristic.h" 29 #include "device/bluetooth/test/mock_bluetooth_gatt_characteristic.h"
30 #include "device/bluetooth/test/mock_bluetooth_gatt_connection.h" 30 #include "device/bluetooth/test/mock_bluetooth_gatt_connection.h"
31 #include "device/bluetooth/test/mock_bluetooth_gatt_notify_session.h" 31 #include "device/bluetooth/test/mock_bluetooth_gatt_notify_session.h"
32 #include "testing/gmock/include/gmock/gmock.h" 32 #include "testing/gmock/include/gmock/gmock.h"
33 #include "testing/gtest/include/gtest/gtest.h" 33 #include "testing/gtest/include/gtest/gtest.h"
34 34
35 using testing::_; 35 using testing::_;
36 using testing::AtLeast; 36 using testing::AtLeast;
37 using testing::NiceMock; 37 using testing::NiceMock;
38 using testing::Return; 38 using testing::Return;
39 using testing::StrictMock; 39 using testing::StrictMock;
40 using testing::SaveArg; 40 using testing::SaveArg;
41 41
42 namespace proximity_auth { 42 namespace proximity_auth {
43 namespace { 43 namespace {
44 44
45 const char kServiceUUID[] = "DEADBEEF-CAFE-FEED-FOOD-D15EA5EBEEEF"; 45 const char kServiceUUID[] = "DEADBEEF-CAFE-FEED-FOOD-D15EA5EBEEEF";
46 const char kToPeripheralCharUUID[] = "977c6674-1239-4e72-993b-502369b8bb5a"; 46 const char kToPeripheralCharUUID[] = "977c6674-1239-4e72-993b-502369b8bb5a";
47 const char kFromPeripheralCharUUID[] = "f4b904a2-a030-43b3-98a8-221c536c03cb"; 47 const char kFromPeripheralCharUUID[] = "f4b904a2-a030-43b3-98a8-221c536c03cb";
48 48
49 const char kServiceID[] = "service id"; 49 const char kServiceID[] = "service id";
50 const char kToPeripheralCharID[] = "to peripheral char id"; 50 const char kToPeripheralCharID[] = "to peripheral char id";
51 const char kFromPeripheralCharID[] = "from peripheral char id"; 51 const char kFromPeripheralCharID[] = "from peripheral char id";
52 52
53 const device::BluetoothGattCharacteristic::Properties 53 const device::BluetoothRemoteGattCharacteristic::Properties
54 kCharacteristicProperties = 54 kCharacteristicProperties =
55 device::BluetoothGattCharacteristic::PROPERTY_BROADCAST | 55 device::BluetoothRemoteGattCharacteristic::PROPERTY_BROADCAST |
56 device::BluetoothGattCharacteristic::PROPERTY_READ | 56 device::BluetoothRemoteGattCharacteristic::PROPERTY_READ |
57 device::BluetoothGattCharacteristic::PROPERTY_WRITE_WITHOUT_RESPONSE | 57 device::BluetoothRemoteGattCharacteristic::
58 device::BluetoothGattCharacteristic::PROPERTY_INDICATE; 58 PROPERTY_WRITE_WITHOUT_RESPONSE |
59 device::BluetoothRemoteGattCharacteristic::PROPERTY_INDICATE;
59 60
60 const int kMaxNumberOfTries = 3; 61 const int kMaxNumberOfTries = 3;
61 62
62 class MockBluetoothThrottler : public BluetoothThrottler { 63 class MockBluetoothThrottler : public BluetoothThrottler {
63 public: 64 public:
64 MockBluetoothThrottler() {} 65 MockBluetoothThrottler() {}
65 ~MockBluetoothThrottler() override {} 66 ~MockBluetoothThrottler() override {}
66 67
67 MOCK_CONST_METHOD0(GetDelay, base::TimeDelta()); 68 MOCK_CONST_METHOD0(GetDelay, base::TimeDelta());
68 MOCK_METHOD1(OnConnection, void(Connection* connection)); 69 MOCK_METHOD1(OnConnection, void(Connection* connection));
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
139 device_ = make_scoped_ptr(new NiceMock<device::MockBluetoothDevice>( 140 device_ = make_scoped_ptr(new NiceMock<device::MockBluetoothDevice>(
140 adapter_.get(), 0, kTestRemoteDeviceName, 141 adapter_.get(), 0, kTestRemoteDeviceName,
141 kTestRemoteDeviceBluetoothAddress, false, false)); 142 kTestRemoteDeviceBluetoothAddress, false, false));
142 143
143 service_ = make_scoped_ptr(new NiceMock<device::MockBluetoothGattService>( 144 service_ = make_scoped_ptr(new NiceMock<device::MockBluetoothGattService>(
144 device_.get(), kServiceID, service_uuid_, true, false)); 145 device_.get(), kServiceID, service_uuid_, true, false));
145 to_peripheral_char_ = 146 to_peripheral_char_ =
146 make_scoped_ptr(new NiceMock<device::MockBluetoothGattCharacteristic>( 147 make_scoped_ptr(new NiceMock<device::MockBluetoothGattCharacteristic>(
147 service_.get(), kToPeripheralCharID, to_peripheral_char_uuid_, 148 service_.get(), kToPeripheralCharID, to_peripheral_char_uuid_,
148 false, kCharacteristicProperties, 149 false, kCharacteristicProperties,
149 device::BluetoothGattCharacteristic::PERMISSION_NONE)); 150 device::BluetoothRemoteGattCharacteristic::PERMISSION_NONE));
150 151
151 from_peripheral_char_ = 152 from_peripheral_char_ =
152 make_scoped_ptr(new NiceMock<device::MockBluetoothGattCharacteristic>( 153 make_scoped_ptr(new NiceMock<device::MockBluetoothGattCharacteristic>(
153 service_.get(), kFromPeripheralCharID, from_peripheral_char_uuid_, 154 service_.get(), kFromPeripheralCharID, from_peripheral_char_uuid_,
154 false, kCharacteristicProperties, 155 false, kCharacteristicProperties,
155 device::BluetoothGattCharacteristic::PERMISSION_NONE)); 156 device::BluetoothRemoteGattCharacteristic::PERMISSION_NONE));
156 157
157 device::BluetoothAdapterFactory::SetAdapterForTesting(adapter_); 158 device::BluetoothAdapterFactory::SetAdapterForTesting(adapter_);
158 159
159 std::vector<const device::BluetoothDevice*> devices; 160 std::vector<const device::BluetoothDevice*> devices;
160 devices.push_back(device_.get()); 161 devices.push_back(device_.get());
161 ON_CALL(*adapter_, GetDevices()).WillByDefault(Return(devices)); 162 ON_CALL(*adapter_, GetDevices()).WillByDefault(Return(devices));
162 ON_CALL(*adapter_, GetDevice(kTestRemoteDeviceBluetoothAddress)) 163 ON_CALL(*adapter_, GetDevice(kTestRemoteDeviceBluetoothAddress))
163 .WillByDefault(Return(device_.get())); 164 .WillByDefault(Return(device_.get()));
164 ON_CALL(*device_, GetGattService(kServiceID)) 165 ON_CALL(*device_, GetGattService(kServiceID))
165 .WillByDefault(Return(service_.get())); 166 .WillByDefault(Return(service_.get()));
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after
369 device::BluetoothDevice::GattConnectionCallback 370 device::BluetoothDevice::GattConnectionCallback
370 create_gatt_connection_success_callback_; 371 create_gatt_connection_success_callback_;
371 device::BluetoothDevice::ConnectErrorCallback 372 device::BluetoothDevice::ConnectErrorCallback
372 create_gatt_connection_error_callback_; 373 create_gatt_connection_error_callback_;
373 374
374 BluetoothLowEnergyCharacteristicsFinder::SuccessCallback 375 BluetoothLowEnergyCharacteristicsFinder::SuccessCallback
375 characteristics_finder_success_callback_; 376 characteristics_finder_success_callback_;
376 BluetoothLowEnergyCharacteristicsFinder::ErrorCallback 377 BluetoothLowEnergyCharacteristicsFinder::ErrorCallback
377 characteristics_finder_error_callback_; 378 characteristics_finder_error_callback_;
378 379
379 device::BluetoothGattCharacteristic::NotifySessionCallback 380 device::BluetoothRemoteGattCharacteristic::NotifySessionCallback
380 notify_session_success_callback_; 381 notify_session_success_callback_;
381 device::BluetoothGattCharacteristic::ErrorCallback 382 device::BluetoothRemoteGattCharacteristic::ErrorCallback
382 notify_session_error_callback_; 383 notify_session_error_callback_;
383 384
384 base::Closure write_remote_characteristic_success_callback_; 385 base::Closure write_remote_characteristic_success_callback_;
385 device::BluetoothGattCharacteristic::ErrorCallback 386 device::BluetoothRemoteGattCharacteristic::ErrorCallback
386 write_remote_characteristic_error_callback_; 387 write_remote_characteristic_error_callback_;
387 }; 388 };
388 389
389 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, 390 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest,
390 CreateAndDestroyWithouthConnectCallDoesntCrash) { 391 CreateAndDestroyWithouthConnectCallDoesntCrash) {
391 BluetoothLowEnergyConnection connection( 392 BluetoothLowEnergyConnection connection(
392 remote_device_, adapter_, service_uuid_, bluetooth_throttler_.get(), 393 remote_device_, adapter_, service_uuid_, bluetooth_throttler_.get(),
393 kMaxNumberOfTries); 394 kMaxNumberOfTries);
394 } 395 }
395 396
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
461 scoped_ptr<MockBluetoothLowEnergyConnection> connection(CreateConnection()); 462 scoped_ptr<MockBluetoothLowEnergyConnection> connection(CreateConnection());
462 ConnectGatt(connection.get()); 463 ConnectGatt(connection.get());
463 CharacteristicsFound(connection.get()); 464 CharacteristicsFound(connection.get());
464 465
465 EXPECT_CALL(*to_peripheral_char_, WriteRemoteCharacteristic(_, _, _)) 466 EXPECT_CALL(*to_peripheral_char_, WriteRemoteCharacteristic(_, _, _))
466 .Times(0); 467 .Times(0);
467 EXPECT_FALSE(notify_session_success_callback_.is_null()); 468 EXPECT_FALSE(notify_session_success_callback_.is_null());
468 ASSERT_FALSE(notify_session_error_callback_.is_null()); 469 ASSERT_FALSE(notify_session_error_callback_.is_null());
469 470
470 notify_session_error_callback_.Run( 471 notify_session_error_callback_.Run(
471 device::BluetoothGattService::GATT_ERROR_UNKNOWN); 472 device::BluetoothRemoteGattService::GATT_ERROR_UNKNOWN);
472 473
473 EXPECT_EQ(connection->sub_status(), 474 EXPECT_EQ(connection->sub_status(),
474 BluetoothLowEnergyConnection::SubStatus::DISCONNECTED); 475 BluetoothLowEnergyConnection::SubStatus::DISCONNECTED);
475 EXPECT_EQ(connection->status(), Connection::DISCONNECTED); 476 EXPECT_EQ(connection->status(), Connection::DISCONNECTED);
476 } 477 }
477 478
478 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, 479 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest,
479 Connect_Fails_ErrorSendingInviteToConnectSignal) { 480 Connect_Fails_ErrorSendingInviteToConnectSignal) {
480 scoped_ptr<MockBluetoothLowEnergyConnection> connection(CreateConnection()); 481 scoped_ptr<MockBluetoothLowEnergyConnection> connection(CreateConnection());
481 ConnectGatt(connection.get()); 482 ConnectGatt(connection.get());
(...skipping 14 matching lines...) Expand all
496 497
497 for (int i = 0; i < kMaxNumberOfTries; i++) { 498 for (int i = 0; i < kMaxNumberOfTries; i++) {
498 const std::vector<uint8_t> kInviteToConnectSignal = ToByteVector( 499 const std::vector<uint8_t> kInviteToConnectSignal = ToByteVector(
499 static_cast<uint32_t>(BluetoothLowEnergyConnection::ControlSignal:: 500 static_cast<uint32_t>(BluetoothLowEnergyConnection::ControlSignal::
500 kInviteToConnectSignal)); 501 kInviteToConnectSignal));
501 EXPECT_EQ(last_value_written_on_to_peripheral_char_, 502 EXPECT_EQ(last_value_written_on_to_peripheral_char_,
502 kInviteToConnectSignal); 503 kInviteToConnectSignal);
503 ASSERT_FALSE(write_remote_characteristic_error_callback_.is_null()); 504 ASSERT_FALSE(write_remote_characteristic_error_callback_.is_null());
504 EXPECT_FALSE(write_remote_characteristic_success_callback_.is_null()); 505 EXPECT_FALSE(write_remote_characteristic_success_callback_.is_null());
505 write_remote_characteristic_error_callback_.Run( 506 write_remote_characteristic_error_callback_.Run(
506 device::BluetoothGattService::GATT_ERROR_UNKNOWN); 507 device::BluetoothRemoteGattService::GATT_ERROR_UNKNOWN);
507 } 508 }
508 509
509 EXPECT_EQ(connection->sub_status(), 510 EXPECT_EQ(connection->sub_status(),
510 BluetoothLowEnergyConnection::SubStatus::DISCONNECTED); 511 BluetoothLowEnergyConnection::SubStatus::DISCONNECTED);
511 EXPECT_EQ(connection->status(), Connection::DISCONNECTED); 512 EXPECT_EQ(connection->status(), Connection::DISCONNECTED);
512 } 513 }
513 514
514 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, 515 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest,
515 Receive_MessageSmallerThanCharacteristicSize) { 516 Receive_MessageSmallerThanCharacteristicSize) {
516 scoped_ptr<MockBluetoothLowEnergyConnection> connection(CreateConnection()); 517 scoped_ptr<MockBluetoothLowEnergyConnection> connection(CreateConnection());
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after
676 create_gatt_connection_success_callback_.Run( 677 create_gatt_connection_success_callback_.Run(
677 make_scoped_ptr(new NiceMock<device::MockBluetoothGattConnection>( 678 make_scoped_ptr(new NiceMock<device::MockBluetoothGattConnection>(
678 adapter_, kTestRemoteDeviceBluetoothAddress))); 679 adapter_, kTestRemoteDeviceBluetoothAddress)));
679 680
680 CharacteristicsFound(connection.get()); 681 CharacteristicsFound(connection.get());
681 NotifySessionStarted(connection.get()); 682 NotifySessionStarted(connection.get());
682 ResponseSignalReceived(connection.get()); 683 ResponseSignalReceived(connection.get());
683 } 684 }
684 685
685 } // namespace proximity_auth 686 } // namespace proximity_auth
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698