| 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 "mojo/public/cpp/bindings/array.h" | 24 #include "mojo/public/cpp/bindings/array.h" |
| 25 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
| 26 | 26 |
| 27 namespace { |
| 28 constexpr int16_t kTestRssi = -50; |
| 29 constexpr int16_t kTestRssi2 = -70; |
| 30 } |
| 31 |
| 27 namespace arc { | 32 namespace arc { |
| 28 | 33 |
| 29 class ArcBluetoothBridgeTest : public testing::Test { | 34 class ArcBluetoothBridgeTest : public testing::Test { |
| 30 protected: | 35 protected: |
| 31 void AddTestDevice() { | 36 void AddTestDevice() { |
| 32 bluez::BluezDBusManager* dbus_manager = bluez::BluezDBusManager::Get(); | 37 bluez::BluezDBusManager* dbus_manager = bluez::BluezDBusManager::Get(); |
| 33 auto* fake_bluetooth_device_client = | 38 auto* fake_bluetooth_device_client = |
| 34 static_cast<bluez::FakeBluetoothDeviceClient*>( | 39 static_cast<bluez::FakeBluetoothDeviceClient*>( |
| 35 dbus_manager->GetBluetoothDeviceClient()); | 40 dbus_manager->GetBluetoothDeviceClient()); |
| 36 auto* fake_bluetooth_gatt_service_client = | 41 auto* fake_bluetooth_gatt_service_client = |
| 37 static_cast<bluez::FakeBluetoothGattServiceClient*>( | 42 static_cast<bluez::FakeBluetoothGattServiceClient*>( |
| 38 dbus_manager->GetBluetoothGattServiceClient()); | 43 dbus_manager->GetBluetoothGattServiceClient()); |
| 39 auto* fake_bluetooth_gatt_characteristic_client = | 44 auto* fake_bluetooth_gatt_characteristic_client = |
| 40 static_cast<bluez::FakeBluetoothGattCharacteristicClient*>( | 45 static_cast<bluez::FakeBluetoothGattCharacteristicClient*>( |
| 41 dbus_manager->GetBluetoothGattCharacteristicClient()); | 46 dbus_manager->GetBluetoothGattCharacteristicClient()); |
| 42 | 47 |
| 43 fake_bluetooth_device_client->CreateDevice( | 48 fake_bluetooth_device_client->CreateDevice( |
| 44 dbus::ObjectPath(bluez::FakeBluetoothAdapterClient::kAdapterPath), | 49 dbus::ObjectPath(bluez::FakeBluetoothAdapterClient::kAdapterPath), |
| 45 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kLowEnergyPath)); | 50 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kLowEnergyPath)); |
| 46 fake_bluetooth_gatt_service_client->ExposeHeartRateService( | 51 fake_bluetooth_gatt_service_client->ExposeHeartRateService( |
| 47 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kLowEnergyPath)); | 52 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kLowEnergyPath)); |
| 48 fake_bluetooth_gatt_characteristic_client->ExposeHeartRateCharacteristics( | 53 fake_bluetooth_gatt_characteristic_client->ExposeHeartRateCharacteristics( |
| 49 fake_bluetooth_gatt_service_client->GetHeartRateServicePath()); | 54 fake_bluetooth_gatt_service_client->GetHeartRateServicePath()); |
| 55 |
| 56 ChangeTestDeviceRssi(kTestRssi); |
| 57 } |
| 58 |
| 59 void ChangeTestDeviceRssi(uint16_t rssi) { |
| 60 bluez::BluezDBusManager* dbus_manager = bluez::BluezDBusManager::Get(); |
| 61 auto* fake_bluetooth_device_client = |
| 62 static_cast<bluez::FakeBluetoothDeviceClient*>( |
| 63 dbus_manager->GetBluetoothDeviceClient()); |
| 64 fake_bluetooth_device_client->UpdateDeviceRSSI( |
| 65 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kLowEnergyPath), |
| 66 rssi); |
| 50 } | 67 } |
| 51 | 68 |
| 52 void OnAdapterInitialized(scoped_refptr<device::BluetoothAdapter> adapter) { | 69 void OnAdapterInitialized(scoped_refptr<device::BluetoothAdapter> adapter) { |
| 53 adapter_ = adapter; | 70 adapter_ = adapter; |
| 54 get_adapter_run_loop_.Quit(); | 71 get_adapter_run_loop_.Quit(); |
| 55 } | 72 } |
| 56 | 73 |
| 57 void SetUp() override { | 74 void SetUp() override { |
| 58 std::unique_ptr<bluez::BluezDBusManagerSetter> dbus_setter = | 75 std::unique_ptr<bluez::BluezDBusManagerSetter> dbus_setter = |
| 59 bluez::BluezDBusManager::GetSetterForTesting(); | 76 bluez::BluezDBusManager::GetSetterForTesting(); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 89 base::MessageLoop message_loop_; | 106 base::MessageLoop message_loop_; |
| 90 base::RunLoop get_adapter_run_loop_; | 107 base::RunLoop get_adapter_run_loop_; |
| 91 }; | 108 }; |
| 92 | 109 |
| 93 // When we add device to bluez::FakeBluetoothDeviceClient, ArcBluetoothBridge | 110 // When we add device to bluez::FakeBluetoothDeviceClient, ArcBluetoothBridge |
| 94 // should send new device data to Android. This test will then check | 111 // should send new device data to Android. This test will then check |
| 95 // the correctness of the device properties sent via arc bridge. | 112 // the correctness of the device properties sent via arc bridge. |
| 96 TEST_F(ArcBluetoothBridgeTest, DeviceFound) { | 113 TEST_F(ArcBluetoothBridgeTest, DeviceFound) { |
| 97 EXPECT_EQ(0u, fake_bluetooth_instance_->device_found_data().size()); | 114 EXPECT_EQ(0u, fake_bluetooth_instance_->device_found_data().size()); |
| 98 AddTestDevice(); | 115 AddTestDevice(); |
| 99 EXPECT_EQ(1u, fake_bluetooth_instance_->device_found_data().size()); | 116 EXPECT_EQ(2u, fake_bluetooth_instance_->device_found_data().size()); |
| 100 const mojo::Array<mojom::BluetoothPropertyPtr>& prop = | 117 const mojo::Array<mojom::BluetoothPropertyPtr>& prop = |
| 101 fake_bluetooth_instance_->device_found_data().back(); | 118 fake_bluetooth_instance_->device_found_data().back(); |
| 102 | 119 |
| 103 EXPECT_EQ(7u, prop.size()); | 120 EXPECT_EQ(7u, prop.size()); |
| 104 EXPECT_TRUE(prop[0]->is_bdname()); | 121 EXPECT_TRUE(prop[0]->is_bdname()); |
| 105 EXPECT_EQ(std::string(bluez::FakeBluetoothDeviceClient::kLowEnergyName), | 122 EXPECT_EQ(std::string(bluez::FakeBluetoothDeviceClient::kLowEnergyName), |
| 106 prop[0]->get_bdname()); | 123 prop[0]->get_bdname()); |
| 107 EXPECT_TRUE(prop[1]->is_bdaddr()); | 124 EXPECT_TRUE(prop[1]->is_bdaddr()); |
| 108 EXPECT_EQ(std::string(bluez::FakeBluetoothDeviceClient::kLowEnergyAddress), | 125 EXPECT_EQ(std::string(bluez::FakeBluetoothDeviceClient::kLowEnergyAddress), |
| 109 prop[1]->get_bdaddr()->To<std::string>()); | 126 prop[1]->get_bdaddr()->To<std::string>()); |
| 110 EXPECT_TRUE(prop[2]->is_uuids()); | 127 EXPECT_TRUE(prop[2]->is_uuids()); |
| 111 EXPECT_EQ(1u, prop[2]->get_uuids().size()); | 128 EXPECT_EQ(1u, prop[2]->get_uuids().size()); |
| 112 EXPECT_EQ(bluez::FakeBluetoothGattServiceClient::kHeartRateServiceUUID, | 129 EXPECT_EQ(bluez::FakeBluetoothGattServiceClient::kHeartRateServiceUUID, |
| 113 prop[2]->get_uuids()[0].value()); | 130 prop[2]->get_uuids()[0].value()); |
| 114 EXPECT_TRUE(prop[3]->is_device_class()); | 131 EXPECT_TRUE(prop[3]->is_device_class()); |
| 115 EXPECT_EQ(bluez::FakeBluetoothDeviceClient::kLowEnergyClass, | 132 EXPECT_EQ(bluez::FakeBluetoothDeviceClient::kLowEnergyClass, |
| 116 prop[3]->get_device_class()); | 133 prop[3]->get_device_class()); |
| 117 EXPECT_TRUE(prop[4]->is_device_type()); | 134 EXPECT_TRUE(prop[4]->is_device_type()); |
| 118 // bluez::FakeBluetoothDeviceClient does not return proper device type. | 135 // bluez::FakeBluetoothDeviceClient does not return proper device type. |
| 119 EXPECT_TRUE(prop[5]->is_remote_friendly_name()); | 136 EXPECT_TRUE(prop[5]->is_remote_friendly_name()); |
| 120 EXPECT_EQ(std::string(bluez::FakeBluetoothDeviceClient::kLowEnergyName), | 137 EXPECT_EQ(std::string(bluez::FakeBluetoothDeviceClient::kLowEnergyName), |
| 121 prop[5]->get_remote_friendly_name()); | 138 prop[5]->get_remote_friendly_name()); |
| 122 EXPECT_TRUE(prop[6]->is_remote_rssi()); | 139 EXPECT_TRUE(prop[6]->is_remote_rssi()); |
| 140 EXPECT_EQ(kTestRssi, prop[6]->get_remote_rssi()); |
| 141 |
| 142 ChangeTestDeviceRssi(kTestRssi2); |
| 143 EXPECT_EQ(3u, fake_bluetooth_instance_->device_found_data().size()); |
| 144 const mojo::Array<mojom::BluetoothPropertyPtr>& prop2 = |
| 145 fake_bluetooth_instance_->device_found_data().back(); |
| 146 EXPECT_EQ(7u, prop2.size()); |
| 147 EXPECT_TRUE(prop2[6]->is_remote_rssi()); |
| 148 EXPECT_EQ(kTestRssi2, prop2[6]->get_remote_rssi()); |
| 123 } | 149 } |
| 124 | 150 |
| 125 // Invoke OnDiscoveryStarted to send cached device to BT instance, | 151 // Invoke OnDiscoveryStarted to send cached device to BT instance, |
| 126 // and check correctness of the Advertising data sent via arc bridge. | 152 // and check correctness of the Advertising data sent via arc bridge. |
| 127 TEST_F(ArcBluetoothBridgeTest, LEDeviceFound) { | 153 TEST_F(ArcBluetoothBridgeTest, LEDeviceFound) { |
| 128 EXPECT_EQ(0u, fake_bluetooth_instance_->device_found_data().size()); | 154 EXPECT_EQ(0u, fake_bluetooth_instance_->le_device_found_data().size()); |
| 129 AddTestDevice(); | 155 AddTestDevice(); |
| 130 EXPECT_EQ(1u, fake_bluetooth_instance_->le_device_found_data().size()); | 156 EXPECT_EQ(1u, fake_bluetooth_instance_->le_device_found_data().size()); |
| 131 | 157 |
| 132 const mojom::BluetoothAddressPtr& addr = | 158 const auto& le_device_found_data = |
| 133 fake_bluetooth_instance_->le_device_found_data().back()->addr(); | 159 fake_bluetooth_instance_->le_device_found_data().back(); |
| 160 const mojom::BluetoothAddressPtr& addr = le_device_found_data->addr(); |
| 134 const mojo::Array<mojom::BluetoothAdvertisingDataPtr>& adv_data = | 161 const mojo::Array<mojom::BluetoothAdvertisingDataPtr>& adv_data = |
| 135 fake_bluetooth_instance_->le_device_found_data().back()->adv_data(); | 162 le_device_found_data->adv_data(); |
| 136 | 163 |
| 137 EXPECT_EQ(std::string(bluez::FakeBluetoothDeviceClient::kLowEnergyAddress), | 164 EXPECT_EQ(std::string(bluez::FakeBluetoothDeviceClient::kLowEnergyAddress), |
| 138 addr->To<std::string>()); | 165 addr->To<std::string>()); |
| 139 EXPECT_EQ(2u, adv_data.size()); | 166 EXPECT_EQ(2u, adv_data.size()); |
| 140 | 167 |
| 141 EXPECT_TRUE(adv_data[0]->is_local_name()); | 168 EXPECT_TRUE(adv_data[0]->is_local_name()); |
| 142 EXPECT_EQ(std::string(bluez::FakeBluetoothDeviceClient::kLowEnergyName), | 169 EXPECT_EQ(std::string(bluez::FakeBluetoothDeviceClient::kLowEnergyName), |
| 143 adv_data[0]->get_local_name().To<std::string>()); | 170 adv_data[0]->get_local_name().To<std::string>()); |
| 144 | 171 |
| 145 EXPECT_TRUE(adv_data[1]->is_service_uuids_16()); | 172 EXPECT_TRUE(adv_data[1]->is_service_uuids_16()); |
| 146 EXPECT_EQ(1u, adv_data[1]->get_service_uuids_16().size()); | 173 EXPECT_EQ(1u, adv_data[1]->get_service_uuids_16().size()); |
| 147 | 174 |
| 148 std::string uuid16_str = | 175 std::string uuid16_str = |
| 149 base::StringPrintf("%04x", adv_data[1]->get_service_uuids_16()[0]); | 176 base::StringPrintf("%04x", adv_data[1]->get_service_uuids_16()[0]); |
| 150 EXPECT_EQ(bluez::FakeBluetoothGattServiceClient::kHeartRateServiceUUID, | 177 EXPECT_EQ(bluez::FakeBluetoothGattServiceClient::kHeartRateServiceUUID, |
| 151 device::BluetoothUUID(uuid16_str).canonical_value()); | 178 device::BluetoothUUID(uuid16_str).canonical_value()); |
| 179 |
| 180 EXPECT_EQ(kTestRssi, le_device_found_data->rssi()); |
| 181 |
| 182 ChangeTestDeviceRssi(kTestRssi2); |
| 183 EXPECT_EQ(2u, fake_bluetooth_instance_->le_device_found_data().size()); |
| 184 EXPECT_EQ(kTestRssi2, |
| 185 fake_bluetooth_instance_->le_device_found_data().back()->rssi()); |
| 152 } | 186 } |
| 153 | 187 |
| 154 // Invoke GetGattDB and check correctness of the GattDB sent via arc bridge. | 188 // Invoke GetGattDB and check correctness of the GattDB sent via arc bridge. |
| 155 TEST_F(ArcBluetoothBridgeTest, GetGattDB) { | 189 TEST_F(ArcBluetoothBridgeTest, GetGattDB) { |
| 156 AddTestDevice(); | 190 AddTestDevice(); |
| 157 | 191 |
| 158 arc_bluetooth_bridge_->GetGattDB(mojom::BluetoothAddress::From( | 192 arc_bluetooth_bridge_->GetGattDB(mojom::BluetoothAddress::From( |
| 159 std::string(bluez::FakeBluetoothDeviceClient::kLowEnergyAddress))); | 193 std::string(bluez::FakeBluetoothDeviceClient::kLowEnergyAddress))); |
| 160 EXPECT_EQ(1u, fake_bluetooth_instance_->gatt_db_result().size()); | 194 EXPECT_EQ(1u, fake_bluetooth_instance_->gatt_db_result().size()); |
| 161 | 195 |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 205 EXPECT_EQ(device::BluetoothUUID(bluez::FakeBluetoothGattCharacteristicClient:: | 239 EXPECT_EQ(device::BluetoothUUID(bluez::FakeBluetoothGattCharacteristicClient:: |
| 206 kHeartRateControlPointUUID), | 240 kHeartRateControlPointUUID), |
| 207 db[4]->uuid); | 241 db[4]->uuid); |
| 208 EXPECT_EQ(mojom::BluetoothGattDBAttributeType::BTGATT_DB_CHARACTERISTIC, | 242 EXPECT_EQ(mojom::BluetoothGattDBAttributeType::BTGATT_DB_CHARACTERISTIC, |
| 209 db[4]->type); | 243 db[4]->type); |
| 210 EXPECT_EQ(device::BluetoothGattCharacteristic::PROPERTY_WRITE, | 244 EXPECT_EQ(device::BluetoothGattCharacteristic::PROPERTY_WRITE, |
| 211 db[4]->properties); | 245 db[4]->properties); |
| 212 } | 246 } |
| 213 | 247 |
| 214 } // namespace arc | 248 } // namespace arc |
| OLD | NEW |