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 std::unique_ptr<FakeArcBridgeService> fake_arc_bridge_service_; | |
rkc
2016/08/01 19:24:39
Move to the bottom of the section, along with the
puthik_chromium
2016/08/01 22:13:04
Done.
| |
52 std::unique_ptr<FakeBluetoothInstance> fake_bluetooth_instance_; | |
53 std::unique_ptr<ArcBluetoothBridge> arc_bluetooth_bridge_; | |
54 scoped_refptr<device::BluetoothAdapter> adapter_; | |
55 | |
56 void OnAdapterInitialized(scoped_refptr<device::BluetoothAdapter> adapter) { | |
57 adapter_ = adapter; | |
58 get_adapter_run_loop_.Quit(); | |
59 } | |
60 | |
61 void SetUp() override { | |
62 std::unique_ptr<bluez::BluezDBusManagerSetter> dbus_setter = | |
63 bluez::BluezDBusManager::GetSetterForTesting(); | |
64 auto fake_bluetooth_device_client = | |
65 base::MakeUnique<bluez::FakeBluetoothDeviceClient>(); | |
66 fake_bluetooth_device_client->RemoveAllDevices(); | |
67 dbus_setter->SetBluetoothDeviceClient( | |
68 std::move(fake_bluetooth_device_client)); | |
69 dbus_setter->SetBluetoothGattServiceClient( | |
70 base::MakeUnique<bluez::FakeBluetoothGattServiceClient>()); | |
71 dbus_setter->SetBluetoothGattCharacteristicClient( | |
72 base::MakeUnique<bluez::FakeBluetoothGattCharacteristicClient>()); | |
73 dbus_setter->SetBluetoothGattDescriptorClient( | |
74 base::MakeUnique<bluez::FakeBluetoothGattDescriptorClient>()); | |
75 | |
76 fake_arc_bridge_service_.reset(new FakeArcBridgeService()); | |
77 fake_bluetooth_instance_.reset(new FakeBluetoothInstance()); | |
78 fake_arc_bridge_service_->bluetooth()->SetInstance( | |
79 fake_bluetooth_instance_.get(), 2); | |
80 arc_bluetooth_bridge_.reset( | |
81 new ArcBluetoothBridge(fake_arc_bridge_service_.get())); | |
82 | |
83 device::BluetoothAdapterFactory::GetAdapter(base::Bind( | |
84 &ArcBluetoothBridgeTest::OnAdapterInitialized, base::Unretained(this))); | |
85 // We will quit the loop once we get the adapter. | |
86 get_adapter_run_loop_.Run(); | |
87 } | |
88 | |
89 base::MessageLoop message_loop_; | |
90 base::RunLoop get_adapter_run_loop_; | |
91 }; | |
92 | |
93 // When we add device to bluez::FakeBluetoothDeviceClient, ArcBluetoothBridge | |
94 // should send new device data to Android. This test will then check | |
95 // the correctness of the device properties sent via arc bridge. | |
96 TEST_F(ArcBluetoothBridgeTest, DeviceFound) { | |
97 EXPECT_EQ(0u, fake_bluetooth_instance_->device_found_data().size()); | |
98 AddTestDevice(); | |
99 EXPECT_EQ(1u, fake_bluetooth_instance_->device_found_data().size()); | |
100 const mojo::Array<mojom::BluetoothPropertyPtr>& prop = | |
101 fake_bluetooth_instance_->device_found_data().back(); | |
102 | |
103 EXPECT_EQ(7u, prop.size()); | |
104 EXPECT_TRUE(prop[0]->is_bdname()); | |
105 EXPECT_EQ(std::string(bluez::FakeBluetoothDeviceClient::kLowEnergyName), | |
106 prop[0]->get_bdname()); | |
107 EXPECT_TRUE(prop[1]->is_bdaddr()); | |
108 EXPECT_EQ(std::string(bluez::FakeBluetoothDeviceClient::kLowEnergyAddress), | |
109 prop[1]->get_bdaddr()->To<std::string>()); | |
110 EXPECT_TRUE(prop[2]->is_uuids()); | |
111 EXPECT_EQ(1u, prop[2]->get_uuids().size()); | |
112 EXPECT_EQ(bluez::FakeBluetoothGattServiceClient::kHeartRateServiceUUID, | |
113 prop[2]->get_uuids()[0].To<device::BluetoothUUID>().value()); | |
114 EXPECT_TRUE(prop[3]->is_device_class()); | |
115 EXPECT_EQ(bluez::FakeBluetoothDeviceClient::kLowEnergyClass, | |
116 prop[3]->get_device_class()); | |
117 EXPECT_TRUE(prop[4]->is_device_type()); | |
118 // bluez::FakeBluetoothDeviceClient does not return proper device type. | |
119 EXPECT_TRUE(prop[5]->is_remote_friendly_name()); | |
120 EXPECT_EQ(std::string(bluez::FakeBluetoothDeviceClient::kLowEnergyName), | |
121 prop[5]->get_remote_friendly_name()); | |
122 EXPECT_TRUE(prop[6]->is_remote_rssi()); | |
123 } | |
124 | |
125 // Invoke OnDiscoveryStarted to send cached device to BT instance, | |
126 // and check correctness of the Advertising data sent via arc bridge. | |
127 TEST_F(ArcBluetoothBridgeTest, LEDeviceFound) { | |
128 EXPECT_EQ(0u, fake_bluetooth_instance_->device_found_data().size()); | |
129 AddTestDevice(); | |
130 EXPECT_EQ(1u, fake_bluetooth_instance_->le_device_found_data().size()); | |
131 | |
132 const mojom::BluetoothAddressPtr& addr = | |
133 fake_bluetooth_instance_->le_device_found_data().back()->addr(); | |
134 const mojo::Array<mojom::BluetoothAdvertisingDataPtr>& adv_data = | |
135 fake_bluetooth_instance_->le_device_found_data().back()->adv_data(); | |
136 | |
137 EXPECT_EQ(std::string(bluez::FakeBluetoothDeviceClient::kLowEnergyAddress), | |
138 addr->To<std::string>()); | |
139 EXPECT_EQ(1u, adv_data.size()); | |
140 | |
141 EXPECT_TRUE(adv_data[0]->is_local_name()); | |
142 EXPECT_EQ(std::string(bluez::FakeBluetoothDeviceClient::kLowEnergyName), | |
143 adv_data[0]->get_local_name().To<std::string>()); | |
144 } | |
145 | |
146 // Invoke GetGattDB and check correctness of the GattDB sent via arc bridge. | |
147 TEST_F(ArcBluetoothBridgeTest, GetGattDB) { | |
148 AddTestDevice(); | |
149 | |
150 arc_bluetooth_bridge_->GetGattDB(mojom::BluetoothAddress::From( | |
151 std::string(bluez::FakeBluetoothDeviceClient::kLowEnergyAddress))); | |
152 EXPECT_EQ(1u, fake_bluetooth_instance_->gatt_db_result().size()); | |
153 | |
154 const mojom::BluetoothAddressPtr& addr = | |
155 fake_bluetooth_instance_->gatt_db_result().back()->remote_addr(); | |
156 EXPECT_EQ(std::string(bluez::FakeBluetoothDeviceClient::kLowEnergyAddress), | |
157 addr->To<std::string>()); | |
158 | |
159 // HeartRateService in bluez::FakeBluetoothDeviceClient consists of | |
160 // Service: HeartRateService | |
161 // Characteristic: HeartRateMeasurement | |
162 // Descriptor: ClientCharacteristicConfiguration | |
163 // Characteristic: BodySensorLocation | |
164 // Characteristic: HeartRateControlPoint | |
165 const mojo::Array<mojom::BluetoothGattDBElementPtr>& db = | |
166 fake_bluetooth_instance_->gatt_db_result().back()->db(); | |
167 EXPECT_EQ(5u, db.size()); | |
168 | |
169 EXPECT_EQ(device::BluetoothUUID( | |
170 bluez::FakeBluetoothGattServiceClient::kHeartRateServiceUUID), | |
171 db[0]->uuid.To<device::BluetoothUUID>()); | |
172 EXPECT_EQ(mojom::BluetoothGattDBAttributeType::BTGATT_DB_PRIMARY_SERVICE, | |
173 db[0]->type); | |
174 | |
175 EXPECT_EQ(device::BluetoothUUID(bluez::FakeBluetoothGattCharacteristicClient:: | |
176 kHeartRateMeasurementUUID), | |
177 db[1]->uuid.To<device::BluetoothUUID>()); | |
178 EXPECT_EQ(mojom::BluetoothGattDBAttributeType::BTGATT_DB_CHARACTERISTIC, | |
179 db[1]->type); | |
180 EXPECT_EQ(device::BluetoothGattCharacteristic::PROPERTY_NOTIFY, | |
181 db[1]->properties); | |
182 | |
183 EXPECT_EQ(device::BluetoothUUID(bluez::FakeBluetoothGattDescriptorClient:: | |
184 kClientCharacteristicConfigurationUUID), | |
185 db[2]->uuid.To<device::BluetoothUUID>()); | |
186 EXPECT_EQ(mojom::BluetoothGattDBAttributeType::BTGATT_DB_DESCRIPTOR, | |
187 db[2]->type); | |
188 | |
189 EXPECT_EQ(device::BluetoothUUID(bluez::FakeBluetoothGattCharacteristicClient:: | |
190 kBodySensorLocationUUID), | |
191 db[3]->uuid.To<device::BluetoothUUID>()); | |
192 EXPECT_EQ(mojom::BluetoothGattDBAttributeType::BTGATT_DB_CHARACTERISTIC, | |
193 db[3]->type); | |
194 EXPECT_EQ(device::BluetoothGattCharacteristic::PROPERTY_READ, | |
195 db[3]->properties); | |
196 | |
197 EXPECT_EQ(device::BluetoothUUID(bluez::FakeBluetoothGattCharacteristicClient:: | |
198 kHeartRateControlPointUUID), | |
199 db[4]->uuid.To<device::BluetoothUUID>()); | |
200 EXPECT_EQ(mojom::BluetoothGattDBAttributeType::BTGATT_DB_CHARACTERISTIC, | |
201 db[4]->type); | |
202 EXPECT_EQ(device::BluetoothGattCharacteristic::PROPERTY_WRITE, | |
203 db[4]->properties); | |
204 } | |
205 | |
206 } // namespace arc | |
OLD | NEW |