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