OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/arc/bluetooth/arc_bluetooth_bridge.h" |
| 6 |
| 7 #include <string> |
| 8 #include <utility> |
| 9 #include <vector> |
| 10 |
| 11 #include "base/message_loop/message_loop.h" |
| 12 #include "base/run_loop.h" |
| 13 #include "components/arc/bluetooth/bluetooth_type_converters.h" |
| 14 #include "components/arc/common/bluetooth.mojom.h" |
| 15 #include "components/arc/test/fake_arc_bridge_service.h" |
| 16 #include "components/arc/test/fake_bluetooth_instance.h" |
| 17 #include "device/bluetooth/dbus/bluez_dbus_manager.h" |
| 18 #include "device/bluetooth/dbus/fake_bluetooth_adapter_client.h" |
| 19 #include "device/bluetooth/dbus/fake_bluetooth_device_client.h" |
| 20 #include "device/bluetooth/dbus/fake_bluetooth_gatt_characteristic_client.h" |
| 21 #include "device/bluetooth/dbus/fake_bluetooth_gatt_descriptor_client.h" |
| 22 #include "device/bluetooth/dbus/fake_bluetooth_gatt_service_client.h" |
| 23 #include "mojo/public/cpp/bindings/array.h" |
| 24 #include "testing/gtest/include/gtest/gtest.h" |
| 25 |
| 26 namespace arc { |
| 27 |
| 28 class ArcBluetoothBridgeTest : public testing::Test { |
| 29 protected: |
| 30 void AddTestDevice() { |
| 31 bluez::BluezDBusManager* dbus_manager = bluez::BluezDBusManager::Get(); |
| 32 auto* fake_bluetooth_device_client = |
| 33 static_cast<bluez::FakeBluetoothDeviceClient*>( |
| 34 dbus_manager->GetBluetoothDeviceClient()); |
| 35 auto* fake_bluetooth_gatt_service_client = |
| 36 static_cast<bluez::FakeBluetoothGattServiceClient*>( |
| 37 dbus_manager->GetBluetoothGattServiceClient()); |
| 38 auto* fake_bluetooth_gatt_characteristic_client = |
| 39 static_cast<bluez::FakeBluetoothGattCharacteristicClient*>( |
| 40 dbus_manager->GetBluetoothGattCharacteristicClient()); |
| 41 |
| 42 fake_bluetooth_device_client->CreateDevice( |
| 43 dbus::ObjectPath(bluez::FakeBluetoothAdapterClient::kAdapterPath), |
| 44 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kLowEnergyPath)); |
| 45 fake_bluetooth_gatt_service_client->ExposeHeartRateService( |
| 46 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kLowEnergyPath)); |
| 47 fake_bluetooth_gatt_characteristic_client->ExposeHeartRateCharacteristics( |
| 48 fake_bluetooth_gatt_service_client->GetHeartRateServicePath()); |
| 49 } |
| 50 |
| 51 void OnAdapterInitialized(scoped_refptr<device::BluetoothAdapter> adapter) { |
| 52 adapter_ = adapter; |
| 53 get_adapter_run_loop_.Quit(); |
| 54 } |
| 55 |
| 56 void SetUp() override { |
| 57 std::unique_ptr<bluez::BluezDBusManagerSetter> dbus_setter = |
| 58 bluez::BluezDBusManager::GetSetterForTesting(); |
| 59 auto fake_bluetooth_device_client = |
| 60 base::MakeUnique<bluez::FakeBluetoothDeviceClient>(); |
| 61 fake_bluetooth_device_client->RemoveAllDevices(); |
| 62 dbus_setter->SetBluetoothDeviceClient( |
| 63 std::move(fake_bluetooth_device_client)); |
| 64 dbus_setter->SetBluetoothGattServiceClient( |
| 65 base::MakeUnique<bluez::FakeBluetoothGattServiceClient>()); |
| 66 dbus_setter->SetBluetoothGattCharacteristicClient( |
| 67 base::MakeUnique<bluez::FakeBluetoothGattCharacteristicClient>()); |
| 68 dbus_setter->SetBluetoothGattDescriptorClient( |
| 69 base::MakeUnique<bluez::FakeBluetoothGattDescriptorClient>()); |
| 70 |
| 71 fake_arc_bridge_service_.reset(new FakeArcBridgeService()); |
| 72 fake_bluetooth_instance_.reset(new FakeBluetoothInstance()); |
| 73 fake_arc_bridge_service_->bluetooth()->SetInstance( |
| 74 fake_bluetooth_instance_.get(), 2); |
| 75 arc_bluetooth_bridge_.reset( |
| 76 new ArcBluetoothBridge(fake_arc_bridge_service_.get())); |
| 77 |
| 78 device::BluetoothAdapterFactory::GetAdapter(base::Bind( |
| 79 &ArcBluetoothBridgeTest::OnAdapterInitialized, base::Unretained(this))); |
| 80 // We will quit the loop once we get the adapter. |
| 81 get_adapter_run_loop_.Run(); |
| 82 } |
| 83 |
| 84 std::unique_ptr<FakeArcBridgeService> fake_arc_bridge_service_; |
| 85 std::unique_ptr<FakeBluetoothInstance> fake_bluetooth_instance_; |
| 86 std::unique_ptr<ArcBluetoothBridge> arc_bluetooth_bridge_; |
| 87 scoped_refptr<device::BluetoothAdapter> adapter_; |
| 88 base::MessageLoop message_loop_; |
| 89 base::RunLoop get_adapter_run_loop_; |
| 90 }; |
| 91 |
| 92 // When we add device to bluez::FakeBluetoothDeviceClient, ArcBluetoothBridge |
| 93 // should send new device data to Android. This test will then check |
| 94 // the correctness of the device properties sent via arc bridge. |
| 95 TEST_F(ArcBluetoothBridgeTest, DeviceFound) { |
| 96 EXPECT_EQ(0u, fake_bluetooth_instance_->device_found_data().size()); |
| 97 AddTestDevice(); |
| 98 EXPECT_EQ(1u, fake_bluetooth_instance_->device_found_data().size()); |
| 99 const mojo::Array<mojom::BluetoothPropertyPtr>& prop = |
| 100 fake_bluetooth_instance_->device_found_data().back(); |
| 101 |
| 102 EXPECT_EQ(7u, prop.size()); |
| 103 EXPECT_TRUE(prop[0]->is_bdname()); |
| 104 EXPECT_EQ(std::string(bluez::FakeBluetoothDeviceClient::kLowEnergyName), |
| 105 prop[0]->get_bdname()); |
| 106 EXPECT_TRUE(prop[1]->is_bdaddr()); |
| 107 EXPECT_EQ(std::string(bluez::FakeBluetoothDeviceClient::kLowEnergyAddress), |
| 108 prop[1]->get_bdaddr()->To<std::string>()); |
| 109 EXPECT_TRUE(prop[2]->is_uuids()); |
| 110 EXPECT_EQ(1u, prop[2]->get_uuids().size()); |
| 111 EXPECT_EQ(bluez::FakeBluetoothGattServiceClient::kHeartRateServiceUUID, |
| 112 prop[2]->get_uuids()[0].To<device::BluetoothUUID>().value()); |
| 113 EXPECT_TRUE(prop[3]->is_device_class()); |
| 114 EXPECT_EQ(bluez::FakeBluetoothDeviceClient::kLowEnergyClass, |
| 115 prop[3]->get_device_class()); |
| 116 EXPECT_TRUE(prop[4]->is_device_type()); |
| 117 // bluez::FakeBluetoothDeviceClient does not return proper device type. |
| 118 EXPECT_TRUE(prop[5]->is_remote_friendly_name()); |
| 119 EXPECT_EQ(std::string(bluez::FakeBluetoothDeviceClient::kLowEnergyName), |
| 120 prop[5]->get_remote_friendly_name()); |
| 121 EXPECT_TRUE(prop[6]->is_remote_rssi()); |
| 122 } |
| 123 |
| 124 // Invoke OnDiscoveryStarted to send cached device to BT instance, |
| 125 // and check correctness of the Advertising data sent via arc bridge. |
| 126 TEST_F(ArcBluetoothBridgeTest, LEDeviceFound) { |
| 127 EXPECT_EQ(0u, fake_bluetooth_instance_->device_found_data().size()); |
| 128 AddTestDevice(); |
| 129 EXPECT_EQ(1u, fake_bluetooth_instance_->le_device_found_data().size()); |
| 130 |
| 131 const mojom::BluetoothAddressPtr& addr = |
| 132 fake_bluetooth_instance_->le_device_found_data().back()->addr(); |
| 133 const mojo::Array<mojom::BluetoothAdvertisingDataPtr>& adv_data = |
| 134 fake_bluetooth_instance_->le_device_found_data().back()->adv_data(); |
| 135 |
| 136 EXPECT_EQ(std::string(bluez::FakeBluetoothDeviceClient::kLowEnergyAddress), |
| 137 addr->To<std::string>()); |
| 138 EXPECT_EQ(1u, adv_data.size()); |
| 139 |
| 140 EXPECT_TRUE(adv_data[0]->is_local_name()); |
| 141 EXPECT_EQ(std::string(bluez::FakeBluetoothDeviceClient::kLowEnergyName), |
| 142 adv_data[0]->get_local_name().To<std::string>()); |
| 143 } |
| 144 |
| 145 // Invoke GetGattDB and check correctness of the GattDB sent via arc bridge. |
| 146 TEST_F(ArcBluetoothBridgeTest, GetGattDB) { |
| 147 AddTestDevice(); |
| 148 |
| 149 arc_bluetooth_bridge_->GetGattDB(mojom::BluetoothAddress::From( |
| 150 std::string(bluez::FakeBluetoothDeviceClient::kLowEnergyAddress))); |
| 151 EXPECT_EQ(1u, fake_bluetooth_instance_->gatt_db_result().size()); |
| 152 |
| 153 const mojom::BluetoothAddressPtr& addr = |
| 154 fake_bluetooth_instance_->gatt_db_result().back()->remote_addr(); |
| 155 EXPECT_EQ(std::string(bluez::FakeBluetoothDeviceClient::kLowEnergyAddress), |
| 156 addr->To<std::string>()); |
| 157 |
| 158 // HeartRateService in bluez::FakeBluetoothDeviceClient consists of |
| 159 // Service: HeartRateService |
| 160 // Characteristic: HeartRateMeasurement |
| 161 // Descriptor: ClientCharacteristicConfiguration |
| 162 // Characteristic: BodySensorLocation |
| 163 // Characteristic: HeartRateControlPoint |
| 164 const mojo::Array<mojom::BluetoothGattDBElementPtr>& db = |
| 165 fake_bluetooth_instance_->gatt_db_result().back()->db(); |
| 166 EXPECT_EQ(5u, db.size()); |
| 167 |
| 168 EXPECT_EQ(device::BluetoothUUID( |
| 169 bluez::FakeBluetoothGattServiceClient::kHeartRateServiceUUID), |
| 170 db[0]->uuid.To<device::BluetoothUUID>()); |
| 171 EXPECT_EQ(mojom::BluetoothGattDBAttributeType::BTGATT_DB_PRIMARY_SERVICE, |
| 172 db[0]->type); |
| 173 |
| 174 EXPECT_EQ(device::BluetoothUUID(bluez::FakeBluetoothGattCharacteristicClient:: |
| 175 kHeartRateMeasurementUUID), |
| 176 db[1]->uuid.To<device::BluetoothUUID>()); |
| 177 EXPECT_EQ(mojom::BluetoothGattDBAttributeType::BTGATT_DB_CHARACTERISTIC, |
| 178 db[1]->type); |
| 179 EXPECT_EQ(device::BluetoothGattCharacteristic::PROPERTY_NOTIFY, |
| 180 db[1]->properties); |
| 181 |
| 182 EXPECT_EQ(device::BluetoothUUID(bluez::FakeBluetoothGattDescriptorClient:: |
| 183 kClientCharacteristicConfigurationUUID), |
| 184 db[2]->uuid.To<device::BluetoothUUID>()); |
| 185 EXPECT_EQ(mojom::BluetoothGattDBAttributeType::BTGATT_DB_DESCRIPTOR, |
| 186 db[2]->type); |
| 187 |
| 188 EXPECT_EQ(device::BluetoothUUID(bluez::FakeBluetoothGattCharacteristicClient:: |
| 189 kBodySensorLocationUUID), |
| 190 db[3]->uuid.To<device::BluetoothUUID>()); |
| 191 EXPECT_EQ(mojom::BluetoothGattDBAttributeType::BTGATT_DB_CHARACTERISTIC, |
| 192 db[3]->type); |
| 193 EXPECT_EQ(device::BluetoothGattCharacteristic::PROPERTY_READ, |
| 194 db[3]->properties); |
| 195 |
| 196 EXPECT_EQ(device::BluetoothUUID(bluez::FakeBluetoothGattCharacteristicClient:: |
| 197 kHeartRateControlPointUUID), |
| 198 db[4]->uuid.To<device::BluetoothUUID>()); |
| 199 EXPECT_EQ(mojom::BluetoothGattDBAttributeType::BTGATT_DB_CHARACTERISTIC, |
| 200 db[4]->type); |
| 201 EXPECT_EQ(device::BluetoothGattCharacteristic::PROPERTY_WRITE, |
| 202 db[4]->properties); |
| 203 } |
| 204 |
| 205 } // namespace arc |
OLD | NEW |