| 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 "device/bluetooth/dbus/fake_bluetooth_le_advertising_manager_client.h" |
| 25 #include "mojo/public/cpp/bindings/array.h" | 25 #include "mojo/public/cpp/bindings/array.h" |
| 26 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
| 27 | 27 |
| 28 namespace { |
| 29 constexpr int16_t kTestRssi = -50; |
| 30 constexpr int16_t kTestRssi2 = -70; |
| 31 } |
| 32 |
| 28 namespace arc { | 33 namespace arc { |
| 29 | 34 |
| 30 constexpr int kFailureAdvHandle = -1; | 35 constexpr int kFailureAdvHandle = -1; |
| 31 | 36 |
| 32 class ArcBluetoothBridgeTest : public testing::Test { | 37 class ArcBluetoothBridgeTest : public testing::Test { |
| 33 protected: | 38 protected: |
| 34 void AddTestDevice() { | 39 void AddTestDevice() { |
| 35 bluez::BluezDBusManager* dbus_manager = bluez::BluezDBusManager::Get(); | 40 bluez::BluezDBusManager* dbus_manager = bluez::BluezDBusManager::Get(); |
| 36 auto* fake_bluetooth_device_client = | 41 auto* fake_bluetooth_device_client = |
| 37 static_cast<bluez::FakeBluetoothDeviceClient*>( | 42 static_cast<bluez::FakeBluetoothDeviceClient*>( |
| 38 dbus_manager->GetBluetoothDeviceClient()); | 43 dbus_manager->GetBluetoothDeviceClient()); |
| 39 auto* fake_bluetooth_gatt_service_client = | 44 auto* fake_bluetooth_gatt_service_client = |
| 40 static_cast<bluez::FakeBluetoothGattServiceClient*>( | 45 static_cast<bluez::FakeBluetoothGattServiceClient*>( |
| 41 dbus_manager->GetBluetoothGattServiceClient()); | 46 dbus_manager->GetBluetoothGattServiceClient()); |
| 42 auto* fake_bluetooth_gatt_characteristic_client = | 47 auto* fake_bluetooth_gatt_characteristic_client = |
| 43 static_cast<bluez::FakeBluetoothGattCharacteristicClient*>( | 48 static_cast<bluez::FakeBluetoothGattCharacteristicClient*>( |
| 44 dbus_manager->GetBluetoothGattCharacteristicClient()); | 49 dbus_manager->GetBluetoothGattCharacteristicClient()); |
| 45 | 50 |
| 46 fake_bluetooth_device_client->CreateDevice( | 51 fake_bluetooth_device_client->CreateDevice( |
| 47 dbus::ObjectPath(bluez::FakeBluetoothAdapterClient::kAdapterPath), | 52 dbus::ObjectPath(bluez::FakeBluetoothAdapterClient::kAdapterPath), |
| 48 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kLowEnergyPath)); | 53 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kLowEnergyPath)); |
| 49 fake_bluetooth_gatt_service_client->ExposeHeartRateService( | 54 fake_bluetooth_gatt_service_client->ExposeHeartRateService( |
| 50 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kLowEnergyPath)); | 55 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kLowEnergyPath)); |
| 51 fake_bluetooth_gatt_characteristic_client->ExposeHeartRateCharacteristics( | 56 fake_bluetooth_gatt_characteristic_client->ExposeHeartRateCharacteristics( |
| 52 fake_bluetooth_gatt_service_client->GetHeartRateServicePath()); | 57 fake_bluetooth_gatt_service_client->GetHeartRateServicePath()); |
| 58 |
| 59 ChangeTestDeviceRssi(kTestRssi); |
| 60 } |
| 61 |
| 62 void ChangeTestDeviceRssi(uint16_t rssi) { |
| 63 bluez::BluezDBusManager* dbus_manager = bluez::BluezDBusManager::Get(); |
| 64 auto* fake_bluetooth_device_client = |
| 65 static_cast<bluez::FakeBluetoothDeviceClient*>( |
| 66 dbus_manager->GetBluetoothDeviceClient()); |
| 67 fake_bluetooth_device_client->UpdateDeviceRSSI( |
| 68 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kLowEnergyPath), |
| 69 rssi); |
| 53 } | 70 } |
| 54 | 71 |
| 55 void OnAdapterInitialized(scoped_refptr<device::BluetoothAdapter> adapter) { | 72 void OnAdapterInitialized(scoped_refptr<device::BluetoothAdapter> adapter) { |
| 56 adapter_ = adapter; | 73 adapter_ = adapter; |
| 57 get_adapter_run_loop_.Quit(); | 74 get_adapter_run_loop_.Quit(); |
| 58 } | 75 } |
| 59 | 76 |
| 60 void SetUp() override { | 77 void SetUp() override { |
| 61 std::unique_ptr<bluez::BluezDBusManagerSetter> dbus_setter = | 78 std::unique_ptr<bluez::BluezDBusManagerSetter> dbus_setter = |
| 62 bluez::BluezDBusManager::GetSetterForTesting(); | 79 bluez::BluezDBusManager::GetSetterForTesting(); |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 base::MessageLoop message_loop_; | 173 base::MessageLoop message_loop_; |
| 157 base::RunLoop get_adapter_run_loop_; | 174 base::RunLoop get_adapter_run_loop_; |
| 158 }; | 175 }; |
| 159 | 176 |
| 160 // When we add device to bluez::FakeBluetoothDeviceClient, ArcBluetoothBridge | 177 // When we add device to bluez::FakeBluetoothDeviceClient, ArcBluetoothBridge |
| 161 // should send new device data to Android. This test will then check | 178 // should send new device data to Android. This test will then check |
| 162 // the correctness of the device properties sent via arc bridge. | 179 // the correctness of the device properties sent via arc bridge. |
| 163 TEST_F(ArcBluetoothBridgeTest, DeviceFound) { | 180 TEST_F(ArcBluetoothBridgeTest, DeviceFound) { |
| 164 EXPECT_EQ(0u, fake_bluetooth_instance_->device_found_data().size()); | 181 EXPECT_EQ(0u, fake_bluetooth_instance_->device_found_data().size()); |
| 165 AddTestDevice(); | 182 AddTestDevice(); |
| 166 EXPECT_EQ(1u, fake_bluetooth_instance_->device_found_data().size()); | 183 EXPECT_EQ(2u, fake_bluetooth_instance_->device_found_data().size()); |
| 167 const mojo::Array<mojom::BluetoothPropertyPtr>& prop = | 184 const mojo::Array<mojom::BluetoothPropertyPtr>& prop = |
| 168 fake_bluetooth_instance_->device_found_data().back(); | 185 fake_bluetooth_instance_->device_found_data().back(); |
| 169 | 186 |
| 170 EXPECT_EQ(7u, prop.size()); | 187 EXPECT_EQ(7u, prop.size()); |
| 171 EXPECT_TRUE(prop[0]->is_bdname()); | 188 EXPECT_TRUE(prop[0]->is_bdname()); |
| 172 EXPECT_EQ(std::string(bluez::FakeBluetoothDeviceClient::kLowEnergyName), | 189 EXPECT_EQ(std::string(bluez::FakeBluetoothDeviceClient::kLowEnergyName), |
| 173 prop[0]->get_bdname()); | 190 prop[0]->get_bdname()); |
| 174 EXPECT_TRUE(prop[1]->is_bdaddr()); | 191 EXPECT_TRUE(prop[1]->is_bdaddr()); |
| 175 EXPECT_EQ(std::string(bluez::FakeBluetoothDeviceClient::kLowEnergyAddress), | 192 EXPECT_EQ(std::string(bluez::FakeBluetoothDeviceClient::kLowEnergyAddress), |
| 176 prop[1]->get_bdaddr()->To<std::string>()); | 193 prop[1]->get_bdaddr()->To<std::string>()); |
| 177 EXPECT_TRUE(prop[2]->is_uuids()); | 194 EXPECT_TRUE(prop[2]->is_uuids()); |
| 178 EXPECT_EQ(1u, prop[2]->get_uuids().size()); | 195 EXPECT_EQ(1u, prop[2]->get_uuids().size()); |
| 179 EXPECT_EQ(bluez::FakeBluetoothGattServiceClient::kHeartRateServiceUUID, | 196 EXPECT_EQ(bluez::FakeBluetoothGattServiceClient::kHeartRateServiceUUID, |
| 180 prop[2]->get_uuids()[0].value()); | 197 prop[2]->get_uuids()[0].value()); |
| 181 EXPECT_TRUE(prop[3]->is_device_class()); | 198 EXPECT_TRUE(prop[3]->is_device_class()); |
| 182 EXPECT_EQ(bluez::FakeBluetoothDeviceClient::kLowEnergyClass, | 199 EXPECT_EQ(bluez::FakeBluetoothDeviceClient::kLowEnergyClass, |
| 183 prop[3]->get_device_class()); | 200 prop[3]->get_device_class()); |
| 184 EXPECT_TRUE(prop[4]->is_device_type()); | 201 EXPECT_TRUE(prop[4]->is_device_type()); |
| 185 // bluez::FakeBluetoothDeviceClient does not return proper device type. | 202 // bluez::FakeBluetoothDeviceClient does not return proper device type. |
| 186 EXPECT_TRUE(prop[5]->is_remote_friendly_name()); | 203 EXPECT_TRUE(prop[5]->is_remote_friendly_name()); |
| 187 EXPECT_EQ(std::string(bluez::FakeBluetoothDeviceClient::kLowEnergyName), | 204 EXPECT_EQ(std::string(bluez::FakeBluetoothDeviceClient::kLowEnergyName), |
| 188 prop[5]->get_remote_friendly_name()); | 205 prop[5]->get_remote_friendly_name()); |
| 189 EXPECT_TRUE(prop[6]->is_remote_rssi()); | 206 EXPECT_TRUE(prop[6]->is_remote_rssi()); |
| 207 EXPECT_EQ(kTestRssi, prop[6]->get_remote_rssi()); |
| 208 |
| 209 ChangeTestDeviceRssi(kTestRssi2); |
| 210 EXPECT_EQ(3u, fake_bluetooth_instance_->device_found_data().size()); |
| 211 const mojo::Array<mojom::BluetoothPropertyPtr>& prop2 = |
| 212 fake_bluetooth_instance_->device_found_data().back(); |
| 213 EXPECT_EQ(7u, prop2.size()); |
| 214 EXPECT_TRUE(prop2[6]->is_remote_rssi()); |
| 215 EXPECT_EQ(kTestRssi2, prop2[6]->get_remote_rssi()); |
| 190 } | 216 } |
| 191 | 217 |
| 192 // Invoke OnDiscoveryStarted to send cached device to BT instance, | 218 // Invoke OnDiscoveryStarted to send cached device to BT instance, |
| 193 // and check correctness of the Advertising data sent via arc bridge. | 219 // and check correctness of the Advertising data sent via arc bridge. |
| 194 TEST_F(ArcBluetoothBridgeTest, LEDeviceFound) { | 220 TEST_F(ArcBluetoothBridgeTest, LEDeviceFound) { |
| 195 EXPECT_EQ(0u, fake_bluetooth_instance_->device_found_data().size()); | 221 EXPECT_EQ(0u, fake_bluetooth_instance_->le_device_found_data().size()); |
| 196 AddTestDevice(); | 222 AddTestDevice(); |
| 197 EXPECT_EQ(1u, fake_bluetooth_instance_->le_device_found_data().size()); | 223 EXPECT_EQ(1u, fake_bluetooth_instance_->le_device_found_data().size()); |
| 198 | 224 |
| 199 const mojom::BluetoothAddressPtr& addr = | 225 const auto& le_device_found_data = |
| 200 fake_bluetooth_instance_->le_device_found_data().back()->addr(); | 226 fake_bluetooth_instance_->le_device_found_data().back(); |
| 227 const mojom::BluetoothAddressPtr& addr = le_device_found_data->addr(); |
| 201 const mojo::Array<mojom::BluetoothAdvertisingDataPtr>& adv_data = | 228 const mojo::Array<mojom::BluetoothAdvertisingDataPtr>& adv_data = |
| 202 fake_bluetooth_instance_->le_device_found_data().back()->adv_data(); | 229 le_device_found_data->adv_data(); |
| 203 | 230 |
| 204 EXPECT_EQ(std::string(bluez::FakeBluetoothDeviceClient::kLowEnergyAddress), | 231 EXPECT_EQ(std::string(bluez::FakeBluetoothDeviceClient::kLowEnergyAddress), |
| 205 addr->To<std::string>()); | 232 addr->To<std::string>()); |
| 206 EXPECT_EQ(2u, adv_data.size()); | 233 EXPECT_EQ(2u, adv_data.size()); |
| 207 | 234 |
| 208 EXPECT_TRUE(adv_data[0]->is_local_name()); | 235 EXPECT_TRUE(adv_data[0]->is_local_name()); |
| 209 EXPECT_EQ(std::string(bluez::FakeBluetoothDeviceClient::kLowEnergyName), | 236 EXPECT_EQ(std::string(bluez::FakeBluetoothDeviceClient::kLowEnergyName), |
| 210 adv_data[0]->get_local_name().To<std::string>()); | 237 adv_data[0]->get_local_name().To<std::string>()); |
| 211 | 238 |
| 212 EXPECT_TRUE(adv_data[1]->is_service_uuids()); | 239 EXPECT_TRUE(adv_data[1]->is_service_uuids()); |
| 213 EXPECT_EQ(1u, adv_data[1]->get_service_uuids().size()); | 240 EXPECT_EQ(1u, adv_data[1]->get_service_uuids().size()); |
| 214 EXPECT_EQ(bluez::FakeBluetoothGattServiceClient::kHeartRateServiceUUID, | 241 EXPECT_EQ(bluez::FakeBluetoothGattServiceClient::kHeartRateServiceUUID, |
| 215 adv_data[1]->get_service_uuids()[0].canonical_value()); | 242 adv_data[1]->get_service_uuids()[0].canonical_value()); |
| 243 |
| 244 EXPECT_EQ(kTestRssi, le_device_found_data->rssi()); |
| 245 |
| 246 ChangeTestDeviceRssi(kTestRssi2); |
| 247 EXPECT_EQ(2u, fake_bluetooth_instance_->le_device_found_data().size()); |
| 248 EXPECT_EQ(kTestRssi2, |
| 249 fake_bluetooth_instance_->le_device_found_data().back()->rssi()); |
| 216 } | 250 } |
| 217 | 251 |
| 218 // Invoke GetGattDB and check correctness of the GattDB sent via arc bridge. | 252 // Invoke GetGattDB and check correctness of the GattDB sent via arc bridge. |
| 219 TEST_F(ArcBluetoothBridgeTest, GetGattDB) { | 253 TEST_F(ArcBluetoothBridgeTest, GetGattDB) { |
| 220 AddTestDevice(); | 254 AddTestDevice(); |
| 221 | 255 |
| 222 arc_bluetooth_bridge_->GetGattDB(mojom::BluetoothAddress::From( | 256 arc_bluetooth_bridge_->GetGattDB(mojom::BluetoothAddress::From( |
| 223 std::string(bluez::FakeBluetoothDeviceClient::kLowEnergyAddress))); | 257 std::string(bluez::FakeBluetoothDeviceClient::kLowEnergyAddress))); |
| 224 EXPECT_EQ(1u, fake_bluetooth_instance_->gatt_db_result().size()); | 258 EXPECT_EQ(1u, fake_bluetooth_instance_->gatt_db_result().size()); |
| 225 | 259 |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 343 EXPECT_LE(reserved_handles.size(), kMaxBluezAdvertisements); | 377 EXPECT_LE(reserved_handles.size(), kMaxBluezAdvertisements); |
| 344 EXPECT_EQ(0, NumActiveAdvertisements()); | 378 EXPECT_EQ(0, NumActiveAdvertisements()); |
| 345 | 379 |
| 346 for (int32_t handle : reserved_handles) { | 380 for (int32_t handle : reserved_handles) { |
| 347 EXPECT_EQ(ReleaseAdvertisementHandle(handle), | 381 EXPECT_EQ(ReleaseAdvertisementHandle(handle), |
| 348 mojom::BluetoothGattStatus::GATT_SUCCESS); | 382 mojom::BluetoothGattStatus::GATT_SUCCESS); |
| 349 } | 383 } |
| 350 } | 384 } |
| 351 | 385 |
| 352 } // namespace arc | 386 } // namespace arc |
| OLD | NEW |