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