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 std::unique_ptr<FakeArcBridgeService> fake_arc_bridge_service_; | |
40 std::unique_ptr<FakeBluetoothInstance> fake_bluetooth_instance_; | |
41 std::unique_ptr<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 base::WrapUnique(fake_bluetooth_device_client_)); | |
Luis Héctor Chávez
2016/07/29 23:09:09
This pattern looks very bad (you are keeping a ref
rkc
2016/07/30 22:40:55
The problem with this pattern is holding on to the
puthik_chromium
2016/08/01 18:02:48
I changed it to use the get the FakeBTClient from
| |
62 dbus_setter->SetBluetoothGattServiceClient( | |
63 base::WrapUnique(fake_bluetooth_gatt_service_client_)); | |
64 dbus_setter->SetBluetoothGattCharacteristicClient( | |
65 base::WrapUnique(fake_bluetooth_gatt_characteristic_client_)); | |
66 dbus_setter->SetBluetoothGattDescriptorClient( | |
67 base::WrapUnique(fake_bluetooth_gatt_descriptor_client_)); | |
68 | |
69 fake_arc_bridge_service_.reset(new FakeArcBridgeService()); | |
70 fake_bluetooth_instance_.reset(new FakeBluetoothInstance()); | |
71 fake_arc_bridge_service_->bluetooth()->SetInstance( | |
72 fake_bluetooth_instance_.get(), 2); | |
73 arc_bluetooth_bridge_.reset( | |
74 new ArcBluetoothBridge(fake_arc_bridge_service_.get())); | |
75 | |
76 device::BluetoothAdapterFactory::GetAdapter(base::Bind( | |
77 &ArcBluetoothBridgeTest::OnAdapterInitialized, base::Unretained(this))); | |
78 // We will quit the loop once we get the adapter. | |
79 get_adapter_run_loop_.Run(); | |
80 } | |
81 | |
82 base::MessageLoop message_loop_; | |
83 base::RunLoop get_adapter_run_loop_; | |
84 bluez::FakeBluetoothDeviceClient* fake_bluetooth_device_client_; | |
85 bluez::FakeBluetoothGattServiceClient* fake_bluetooth_gatt_service_client_; | |
86 bluez::FakeBluetoothGattCharacteristicClient* | |
87 fake_bluetooth_gatt_characteristic_client_; | |
88 bluez::FakeBluetoothGattDescriptorClient* | |
89 fake_bluetooth_gatt_descriptor_client_; | |
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((size_t)0, fake_bluetooth_instance_->device_found_data().size()); | |
Luis Héctor Chávez
2016/07/29 23:09:09
Try to avoid C-style casts (since they are forbidd
puthik_chromium
2016/08/01 18:02:48
Done.
Also, git cl lint does not complain about (s
Luis Héctor Chávez
2016/08/01 18:10:57
Maybe it should :/ I'll ask around.
| |
97 AddTestDevice(); | |
98 EXPECT_EQ((size_t)1, fake_bluetooth_instance_->device_found_data().size()); | |
rkc
2016/07/30 22:40:55
As Luis said, all these casts need to go. When com
puthik_chromium
2016/08/01 18:02:48
Done.
| |
99 const mojo::Array<mojom::BluetoothPropertyPtr>& prop = | |
100 fake_bluetooth_instance_->device_found_data().back(); | |
101 | |
102 EXPECT_EQ((size_t)7, 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((size_t)1, 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((size_t)0, fake_bluetooth_instance_->device_found_data().size()); | |
128 AddTestDevice(); | |
129 EXPECT_EQ((size_t)1, 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((size_t)1, 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((size_t)1, 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((size_t)5, 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 |