| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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/arc/bluetooth/arc_bluetooth_bridge.h" | 5 #include "components/arc/bluetooth/arc_bluetooth_bridge.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <utility> | 8 #include <utility> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
| 12 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
| 13 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
| 14 #include "components/arc/bluetooth/bluetooth_type_converters.h" | 14 #include "components/arc/bluetooth/bluetooth_type_converters.h" |
| 15 #include "components/arc/common/bluetooth.mojom.h" | 15 #include "components/arc/common/bluetooth.mojom.h" |
| 16 #include "components/arc/test/fake_arc_bridge_service.h" | 16 #include "components/arc/test/fake_arc_bridge_service.h" |
| 17 #include "components/arc/test/fake_bluetooth_instance.h" | 17 #include "components/arc/test/fake_bluetooth_instance.h" |
| 18 #include "device/bluetooth/dbus/bluez_dbus_manager.h" | 18 #include "device/bluetooth/dbus/bluez_dbus_manager.h" |
| 19 #include "device/bluetooth/dbus/fake_bluetooth_adapter_client.h" | 19 #include "device/bluetooth/dbus/fake_bluetooth_adapter_client.h" |
| 20 #include "device/bluetooth/dbus/fake_bluetooth_device_client.h" | 20 #include "device/bluetooth/dbus/fake_bluetooth_device_client.h" |
| 21 #include "device/bluetooth/dbus/fake_bluetooth_gatt_characteristic_client.h" | 21 #include "device/bluetooth/dbus/fake_bluetooth_gatt_characteristic_client.h" |
| 22 #include "device/bluetooth/dbus/fake_bluetooth_gatt_descriptor_client.h" | 22 #include "device/bluetooth/dbus/fake_bluetooth_gatt_descriptor_client.h" |
| 23 #include "device/bluetooth/dbus/fake_bluetooth_gatt_service_client.h" | 23 #include "device/bluetooth/dbus/fake_bluetooth_gatt_service_client.h" |
| 24 #include "device/bluetooth/dbus/fake_bluetooth_le_advertising_manager_client.h" | |
| 25 #include "mojo/public/cpp/bindings/array.h" | 24 #include "mojo/public/cpp/bindings/array.h" |
| 26 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
| 27 | 26 |
| 28 namespace arc { | 27 namespace arc { |
| 29 | 28 |
| 30 constexpr int kFailureAdvHandle = -1; | |
| 31 | |
| 32 class ArcBluetoothBridgeTest : public testing::Test { | 29 class ArcBluetoothBridgeTest : public testing::Test { |
| 33 protected: | 30 protected: |
| 34 void AddTestDevice() { | 31 void AddTestDevice() { |
| 35 bluez::BluezDBusManager* dbus_manager = bluez::BluezDBusManager::Get(); | 32 bluez::BluezDBusManager* dbus_manager = bluez::BluezDBusManager::Get(); |
| 36 auto* fake_bluetooth_device_client = | 33 auto* fake_bluetooth_device_client = |
| 37 static_cast<bluez::FakeBluetoothDeviceClient*>( | 34 static_cast<bluez::FakeBluetoothDeviceClient*>( |
| 38 dbus_manager->GetBluetoothDeviceClient()); | 35 dbus_manager->GetBluetoothDeviceClient()); |
| 39 auto* fake_bluetooth_gatt_service_client = | 36 auto* fake_bluetooth_gatt_service_client = |
| 40 static_cast<bluez::FakeBluetoothGattServiceClient*>( | 37 static_cast<bluez::FakeBluetoothGattServiceClient*>( |
| 41 dbus_manager->GetBluetoothGattServiceClient()); | 38 dbus_manager->GetBluetoothGattServiceClient()); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 fake_bluetooth_instance_.get(), 2); | 75 fake_bluetooth_instance_.get(), 2); |
| 79 arc_bluetooth_bridge_.reset( | 76 arc_bluetooth_bridge_.reset( |
| 80 new ArcBluetoothBridge(fake_arc_bridge_service_.get())); | 77 new ArcBluetoothBridge(fake_arc_bridge_service_.get())); |
| 81 | 78 |
| 82 device::BluetoothAdapterFactory::GetAdapter(base::Bind( | 79 device::BluetoothAdapterFactory::GetAdapter(base::Bind( |
| 83 &ArcBluetoothBridgeTest::OnAdapterInitialized, base::Unretained(this))); | 80 &ArcBluetoothBridgeTest::OnAdapterInitialized, base::Unretained(this))); |
| 84 // We will quit the loop once we get the adapter. | 81 // We will quit the loop once we get the adapter. |
| 85 get_adapter_run_loop_.Run(); | 82 get_adapter_run_loop_.Run(); |
| 86 } | 83 } |
| 87 | 84 |
| 88 // Helper methods for multi advertisement tests. | |
| 89 int32_t ReserveAdvertisementHandle() { | |
| 90 constexpr int kSentinelHandle = -2; | |
| 91 last_adv_handle_ = kSentinelHandle; | |
| 92 arc_bluetooth_bridge_->ReserveAdvertisementHandle( | |
| 93 base::Bind(&ArcBluetoothBridgeTest::ReserveAdvertisementHandleCallback, | |
| 94 base::Unretained(this))); | |
| 95 | |
| 96 base::RunLoop().RunUntilIdle(); | |
| 97 // Make sure the callback was called. | |
| 98 EXPECT_NE(kSentinelHandle, last_adv_handle_); | |
| 99 return last_adv_handle_; | |
| 100 } | |
| 101 | |
| 102 mojom::BluetoothGattStatus BroadcastAdvertisement( | |
| 103 int adv_handle, | |
| 104 std::unique_ptr<device::BluetoothAdvertisement::Data> data) { | |
| 105 last_status_ = mojom::BluetoothGattStatus::GATT_REQUEST_NOT_SUPPORTED; | |
| 106 arc_bluetooth_bridge_->BroadcastAdvertisement( | |
| 107 adv_handle, std::move(data), | |
| 108 base::Bind(&ArcBluetoothBridgeTest::StatusSetterCallback, | |
| 109 base::Unretained(this))); | |
| 110 | |
| 111 base::RunLoop().RunUntilIdle(); | |
| 112 EXPECT_NE(mojom::BluetoothGattStatus::GATT_REQUEST_NOT_SUPPORTED, | |
| 113 last_status_); | |
| 114 return last_status_; | |
| 115 } | |
| 116 | |
| 117 mojom::BluetoothGattStatus ReleaseAdvertisementHandle(int adv_handle) { | |
| 118 last_status_ = mojom::BluetoothGattStatus::GATT_REQUEST_NOT_SUPPORTED; | |
| 119 arc_bluetooth_bridge_->ReleaseAdvertisementHandle( | |
| 120 adv_handle, base::Bind(&ArcBluetoothBridgeTest::StatusSetterCallback, | |
| 121 base::Unretained(this))); | |
| 122 | |
| 123 base::RunLoop().RunUntilIdle(); | |
| 124 EXPECT_NE(mojom::BluetoothGattStatus::GATT_REQUEST_NOT_SUPPORTED, | |
| 125 last_status_); | |
| 126 return last_status_; | |
| 127 } | |
| 128 | |
| 129 void ReserveAdvertisementHandleCallback(mojom::BluetoothGattStatus status, | |
| 130 int32_t adv_handle) { | |
| 131 if (status == mojom::BluetoothGattStatus::GATT_FAILURE) | |
| 132 last_adv_handle_ = kFailureAdvHandle; | |
| 133 else | |
| 134 last_adv_handle_ = adv_handle; | |
| 135 } | |
| 136 | |
| 137 void StatusSetterCallback(mojom::BluetoothGattStatus status) { | |
| 138 last_status_ = status; | |
| 139 } | |
| 140 | |
| 141 int NumActiveAdvertisements() { | |
| 142 bluez::FakeBluetoothLEAdvertisingManagerClient* adv_client = | |
| 143 static_cast<bluez::FakeBluetoothLEAdvertisingManagerClient*>( | |
| 144 bluez::BluezDBusManager::Get() | |
| 145 ->GetBluetoothLEAdvertisingManagerClient()); | |
| 146 return adv_client->currently_registered(); | |
| 147 } | |
| 148 | |
| 149 int last_adv_handle_; | |
| 150 mojom::BluetoothGattStatus last_status_; | |
| 151 | |
| 152 std::unique_ptr<FakeArcBridgeService> fake_arc_bridge_service_; | 85 std::unique_ptr<FakeArcBridgeService> fake_arc_bridge_service_; |
| 153 std::unique_ptr<FakeBluetoothInstance> fake_bluetooth_instance_; | 86 std::unique_ptr<FakeBluetoothInstance> fake_bluetooth_instance_; |
| 154 std::unique_ptr<ArcBluetoothBridge> arc_bluetooth_bridge_; | 87 std::unique_ptr<ArcBluetoothBridge> arc_bluetooth_bridge_; |
| 155 scoped_refptr<device::BluetoothAdapter> adapter_; | 88 scoped_refptr<device::BluetoothAdapter> adapter_; |
| 156 base::MessageLoop message_loop_; | 89 base::MessageLoop message_loop_; |
| 157 base::RunLoop get_adapter_run_loop_; | 90 base::RunLoop get_adapter_run_loop_; |
| 158 }; | 91 }; |
| 159 | 92 |
| 160 // When we add device to bluez::FakeBluetoothDeviceClient, ArcBluetoothBridge | 93 // When we add device to bluez::FakeBluetoothDeviceClient, ArcBluetoothBridge |
| 161 // should send new device data to Android. This test will then check | 94 // should send new device data to Android. This test will then check |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 271 | 204 |
| 272 EXPECT_EQ(device::BluetoothUUID(bluez::FakeBluetoothGattCharacteristicClient:: | 205 EXPECT_EQ(device::BluetoothUUID(bluez::FakeBluetoothGattCharacteristicClient:: |
| 273 kHeartRateControlPointUUID), | 206 kHeartRateControlPointUUID), |
| 274 db[4]->uuid); | 207 db[4]->uuid); |
| 275 EXPECT_EQ(mojom::BluetoothGattDBAttributeType::BTGATT_DB_CHARACTERISTIC, | 208 EXPECT_EQ(mojom::BluetoothGattDBAttributeType::BTGATT_DB_CHARACTERISTIC, |
| 276 db[4]->type); | 209 db[4]->type); |
| 277 EXPECT_EQ(device::BluetoothGattCharacteristic::PROPERTY_WRITE, | 210 EXPECT_EQ(device::BluetoothGattCharacteristic::PROPERTY_WRITE, |
| 278 db[4]->properties); | 211 db[4]->properties); |
| 279 } | 212 } |
| 280 | 213 |
| 281 // Invoke multi advertisement methods and make sure they are going down to the | |
| 282 // D-Bus clients. | |
| 283 TEST_F(ArcBluetoothBridgeTest, SingleAdvertisement) { | |
| 284 int32_t handle = ReserveAdvertisementHandle(); | |
| 285 EXPECT_NE(kFailureAdvHandle, handle); | |
| 286 EXPECT_EQ(0, NumActiveAdvertisements()); | |
| 287 | |
| 288 auto adv_data = base::MakeUnique<device::BluetoothAdvertisement::Data>( | |
| 289 device::BluetoothAdvertisement::ADVERTISEMENT_TYPE_BROADCAST); | |
| 290 mojom::BluetoothGattStatus status = | |
| 291 BroadcastAdvertisement(handle, std::move(adv_data)); | |
| 292 EXPECT_EQ(mojom::BluetoothGattStatus::GATT_SUCCESS, status); | |
| 293 EXPECT_EQ(1, NumActiveAdvertisements()); | |
| 294 | |
| 295 status = ReleaseAdvertisementHandle(handle); | |
| 296 EXPECT_EQ(mojom::BluetoothGattStatus::GATT_SUCCESS, status); | |
| 297 EXPECT_EQ(0, NumActiveAdvertisements()); | |
| 298 } | |
| 299 | |
| 300 // Invoke multi advertisement methods and make sure they are going down to the | |
| 301 // D-Bus clients. | |
| 302 TEST_F(ArcBluetoothBridgeTest, MultiAdvertisement) { | |
| 303 int32_t handle = ReserveAdvertisementHandle(); | |
| 304 EXPECT_NE(kFailureAdvHandle, handle); | |
| 305 EXPECT_EQ(0, NumActiveAdvertisements()); | |
| 306 | |
| 307 auto adv_data = base::MakeUnique<device::BluetoothAdvertisement::Data>( | |
| 308 device::BluetoothAdvertisement::ADVERTISEMENT_TYPE_BROADCAST); | |
| 309 mojom::BluetoothGattStatus status = | |
| 310 BroadcastAdvertisement(handle, std::move(adv_data)); | |
| 311 EXPECT_EQ(mojom::BluetoothGattStatus::GATT_SUCCESS, status); | |
| 312 EXPECT_EQ(1, NumActiveAdvertisements()); | |
| 313 | |
| 314 int32_t handle2 = ReserveAdvertisementHandle(); | |
| 315 EXPECT_NE(kFailureAdvHandle, handle2); | |
| 316 auto adv_data2 = base::MakeUnique<device::BluetoothAdvertisement::Data>( | |
| 317 device::BluetoothAdvertisement::ADVERTISEMENT_TYPE_PERIPHERAL); | |
| 318 status = BroadcastAdvertisement(handle2, std::move(adv_data2)); | |
| 319 EXPECT_EQ(mojom::BluetoothGattStatus::GATT_SUCCESS, status); | |
| 320 EXPECT_EQ(2, NumActiveAdvertisements()); | |
| 321 | |
| 322 status = ReleaseAdvertisementHandle(handle); | |
| 323 EXPECT_EQ(mojom::BluetoothGattStatus::GATT_SUCCESS, status); | |
| 324 EXPECT_EQ(1, NumActiveAdvertisements()); | |
| 325 | |
| 326 status = ReleaseAdvertisementHandle(handle2); | |
| 327 EXPECT_EQ(mojom::BluetoothGattStatus::GATT_SUCCESS, status); | |
| 328 EXPECT_EQ(0, NumActiveAdvertisements()); | |
| 329 } | |
| 330 | |
| 331 // This tests that we support releasing reserved but unused handles. | |
| 332 // TODO(ejcaruso): When Chrome supports more handles, make sure we | |
| 333 // will stop reserving handles before we use all of Chrome's. | |
| 334 TEST_F(ArcBluetoothBridgeTest, ReleaseUnusedHandles) { | |
| 335 constexpr size_t kMaxBluezAdvertisements = | |
| 336 bluez::FakeBluetoothLEAdvertisingManagerClient::kMaxBluezAdvertisements; | |
| 337 std::vector<int32_t> reserved_handles; | |
| 338 | |
| 339 for (size_t i = 0; i < kMaxBluezAdvertisements; i++) { | |
| 340 int32_t handle = ReserveAdvertisementHandle(); | |
| 341 if (handle == kFailureAdvHandle) | |
| 342 break; | |
| 343 reserved_handles.push_back(handle); | |
| 344 } | |
| 345 EXPECT_GT(reserved_handles.size(), 1Ul); | |
| 346 EXPECT_LE(reserved_handles.size(), kMaxBluezAdvertisements); | |
| 347 EXPECT_EQ(0, NumActiveAdvertisements()); | |
| 348 | |
| 349 for (int32_t handle : reserved_handles) { | |
| 350 EXPECT_EQ(ReleaseAdvertisementHandle(handle), | |
| 351 mojom::BluetoothGattStatus::GATT_SUCCESS); | |
| 352 } | |
| 353 } | |
| 354 | |
| 355 } // namespace arc | 214 } // namespace arc |
| OLD | NEW |