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 <vector> | 8 #include <vector> |
9 | 9 |
| 10 #include "base/message_loop/message_loop.h" |
| 11 #include "base/run_loop.h" |
10 #include "components/arc/bluetooth/bluetooth_type_converters.h" | 12 #include "components/arc/bluetooth/bluetooth_type_converters.h" |
11 #include "components/arc/common/bluetooth.mojom.h" | 13 #include "components/arc/common/bluetooth.mojom.h" |
12 #include "components/arc/test/fake_arc_bridge_service.h" | 14 #include "components/arc/test/fake_arc_bridge_service.h" |
13 #include "components/arc/test/fake_bluetooth_instance.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" |
14 #include "device/bluetooth/test/mock_bluetooth_adapter.h" | 19 #include "device/bluetooth/test/mock_bluetooth_adapter.h" |
15 #include "device/bluetooth/test/mock_bluetooth_device.h" | 20 #include "device/bluetooth/test/mock_bluetooth_device.h" |
16 #include "device/bluetooth/test/mock_bluetooth_gatt_characteristic.h" | 21 #include "device/bluetooth/test/mock_bluetooth_gatt_characteristic.h" |
17 #include "device/bluetooth/test/mock_bluetooth_gatt_descriptor.h" | 22 #include "device/bluetooth/test/mock_bluetooth_gatt_descriptor.h" |
18 #include "device/bluetooth/test/mock_bluetooth_gatt_service.h" | 23 #include "device/bluetooth/test/mock_bluetooth_gatt_service.h" |
19 #include "mojo/public/cpp/bindings/array.h" | 24 #include "mojo/public/cpp/bindings/array.h" |
20 #include "testing/gmock/include/gmock/gmock.h" | 25 #include "testing/gmock/include/gmock/gmock.h" |
21 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
22 | 27 |
23 namespace arc { | 28 namespace arc { |
24 | 29 |
25 namespace { | 30 namespace { |
26 | 31 |
27 const char kAddressStr[] = "1A:2B:3C:4D:5E:6F"; | 32 const char kAddressStr[] = "1A:2B:3C:4D:5E:6F"; |
28 const char kDeviceName[] = "DeviceName"; | 33 const char kDeviceName[] = "DeviceName"; |
29 const uint32_t kDeviceClass = 0x2580; | 34 const char kServiceUUID[] = "0000180f-0000-1000-8000-00805f9b34fb"; |
30 const int16_t kDeviceRssi = -45; | 35 const char kCharUUID[] = "00002a19-0000-1000-8000-00805f9b34fb"; |
31 const char kServiceID[] = "/org/bluez/hci0/dev_1A_2B_3C_4D_5E_6F/service180f"; | |
32 const char kServiceUUID[] = "180f"; | |
33 const char kCharID[] = | |
34 "/org/bluez/hci0/dev_1A_2B_3C_4D_5E_6F/service180f/char2a19"; | |
35 const char kCharUUID[] = "2a19"; | |
36 const uint16_t kCharUUIDInt = 0x2a19; | 36 const uint16_t kCharUUIDInt = 0x2a19; |
37 | 37 |
38 class FakeArcBluetoothBridge : public ArcBluetoothBridge { | |
39 public: | |
40 FakeArcBluetoothBridge(ArcBridgeService* bridge_service, | |
41 device::MockBluetoothAdapter* adapter) | |
42 : ArcBluetoothBridge(bridge_service, true /* test_flag */) { | |
43 SetAdapterForTest(adapter); | |
44 } | |
45 | |
46 void OnAdapterInitialized(scoped_refptr<device::BluetoothAdapter> adapter) {} | |
47 }; | |
48 | |
49 } // namespace | 38 } // namespace |
50 | 39 |
51 class ArcBluetoothBridgeTest : public testing::Test { | 40 class ArcBluetoothBridgeTest : public testing::Test { |
52 public: | 41 public: |
53 ArcBluetoothBridgeTest() {} | 42 ArcBluetoothBridgeTest() |
| 43 : fake_arc_bridge_service_(nullptr), |
| 44 fake_bluetooth_instance_(nullptr), |
| 45 arc_bluetooth_bridge_(nullptr), |
| 46 fake_bluetooth_device_client_(nullptr) {} |
54 | 47 |
55 protected: | 48 protected: |
| 49 void AddTestDevice() { |
| 50 device_uuids_.emplace_back(kServiceUUID); |
| 51 device_uuids_.emplace_back(kCharUUID); |
| 52 |
| 53 fake_bluetooth_device_client_->CreateTestDevice( |
| 54 dbus::ObjectPath(bluez::FakeBluetoothAdapterClient::kAdapterPath), |
| 55 kDeviceName, kDeviceName, kAddressStr, device_uuids_); |
| 56 |
| 57 remote_addr_ = mojom::BluetoothAddress::From(std::string(kAddressStr)); |
| 58 |
| 59 service_id_ = mojom::BluetoothGattServiceID::New(); |
| 60 service_id_->is_primary = true; |
| 61 service_id_->id = mojom::BluetoothGattID::New(); |
| 62 service_id_->id->inst_id = 0; |
| 63 service_id_->id->uuid = |
| 64 mojom::BluetoothUUID::From(device::BluetoothUUID(kServiceUUID)); |
| 65 |
| 66 char_id_ = mojom::BluetoothGattID::New(); |
| 67 char_id_->inst_id = 0; |
| 68 char_id_->uuid = |
| 69 mojom::BluetoothUUID::From(device::BluetoothUUID(kCharUUID)); |
| 70 }; |
| 71 |
56 FakeArcBridgeService* fake_arc_bridge_service_; | 72 FakeArcBridgeService* fake_arc_bridge_service_; |
57 FakeArcBluetoothBridge* fake_arc_bluetooth_bridge_; | 73 FakeBluetoothInstance* fake_bluetooth_instance_; |
58 FakeBluetoothInstance* fake_bluetooth_instance; | 74 ArcBluetoothBridge* arc_bluetooth_bridge_; |
59 device::MockBluetoothAdapter* fake_adapter_; | 75 scoped_refptr<device::BluetoothAdapter> adapter_; |
60 device::MockBluetoothDevice* fake_device_; | 76 std::vector<std::string> device_uuids_; |
61 device::MockBluetoothGattService* fake_service_; | |
62 device::MockBluetoothGattCharacteristic* fake_char_; | |
63 std::vector<device::BluetoothUUID> device_uuids_; | |
64 | 77 |
65 mojom::BluetoothAddressPtr remote_addr; | 78 mojom::BluetoothAddressPtr remote_addr_; |
66 mojom::BluetoothGattServiceIDPtr service_id; | 79 mojom::BluetoothGattServiceIDPtr service_id_; |
67 mojom::BluetoothGattIDPtr char_id; | 80 mojom::BluetoothGattIDPtr char_id_; |
68 | 81 |
69 private: | 82 private: |
| 83 void OnAdapterInitialized(scoped_refptr<device::BluetoothAdapter> adapter) { |
| 84 adapter_ = adapter; |
| 85 get_adapter_run_loop_.Quit(); |
| 86 } |
| 87 |
70 void SetUp() override { | 88 void SetUp() override { |
| 89 std::unique_ptr<bluez::BluezDBusManagerSetter> dbus_setter = |
| 90 bluez::BluezDBusManager::GetSetterForTesting(); |
| 91 fake_bluetooth_device_client_ = new bluez::FakeBluetoothDeviceClient; |
| 92 fake_bluetooth_device_client_->RemoveAllDevices(); |
| 93 dbus_setter->SetBluetoothDeviceClient( |
| 94 std::unique_ptr<bluez::BluetoothDeviceClient>( |
| 95 fake_bluetooth_device_client_)); |
| 96 |
71 fake_arc_bridge_service_ = new FakeArcBridgeService(); | 97 fake_arc_bridge_service_ = new FakeArcBridgeService(); |
| 98 fake_bluetooth_instance_ = static_cast<FakeBluetoothInstance*>( |
| 99 fake_arc_bridge_service_->bluetooth_instance()); |
72 | 100 |
73 fake_adapter_ = new testing::NiceMock<device::MockBluetoothAdapter>(); | 101 arc_bluetooth_bridge_ = new ArcBluetoothBridge(fake_arc_bridge_service_); |
74 fake_arc_bluetooth_bridge_ = | |
75 new FakeArcBluetoothBridge(fake_arc_bridge_service_, fake_adapter_); | |
76 | 102 |
77 fake_device_ = new testing::NiceMock<device::MockBluetoothDevice>( | 103 device::BluetoothAdapterFactory::GetAdapter(base::Bind( |
78 fake_adapter_, 0 /* bluetooth_class */, kDeviceName, kAddressStr, | 104 &ArcBluetoothBridgeTest::OnAdapterInitialized, base::Unretained(this))); |
79 false /* paired */, false /* connected */); | 105 // We will quit the loop once we get the adapter. |
80 ON_CALL(*fake_device_, GetBluetoothClass()) | 106 get_adapter_run_loop_.Run(); |
81 .WillByDefault(testing::Return(kDeviceClass)); | |
82 ON_CALL(*fake_device_, GetInquiryRSSI()) | |
83 .WillByDefault(testing::Return(kDeviceRssi)); | |
84 | |
85 device_uuids_.emplace_back(device::BluetoothUUID(kServiceUUID)); | |
86 device_uuids_.emplace_back(device::BluetoothUUID(kCharUUID)); | |
87 ON_CALL(*fake_device_, GetUUIDs()) | |
88 .WillByDefault(testing::Return(device_uuids_)); | |
89 | |
90 fake_adapter_->AddMockDevice(base::WrapUnique(fake_device_)); | |
91 ON_CALL(*fake_adapter_, GetDevices()) | |
92 .WillByDefault(testing::Return(fake_adapter_->GetConstMockDevices())); | |
93 ON_CALL(*fake_adapter_, GetDevice("1A:2B:3C:4D:5E:6F")) | |
94 .WillByDefault(testing::Return(fake_device_)); | |
95 | |
96 fake_service_ = new testing::NiceMock<device::MockBluetoothGattService>( | |
97 fake_device_, kServiceID, device::BluetoothUUID(kServiceUUID), | |
98 true /* is_primary */, true /* is_local */); | |
99 fake_device_->AddMockService(base::WrapUnique(fake_service_)); | |
100 ON_CALL(*fake_device_, GetGattServices()) | |
101 .WillByDefault(testing::Return(fake_device_->GetMockServices())); | |
102 | |
103 fake_char_ = new testing::NiceMock<device::MockBluetoothGattCharacteristic>( | |
104 fake_service_, kCharID, device::BluetoothUUID(kCharUUID), | |
105 true /*is_local*/, device::BluetoothGattCharacteristic::PROPERTY_READ, | |
106 device::BluetoothGattCharacteristic::PERMISSION_READ); | |
107 fake_service_->AddMockCharacteristic(base::WrapUnique(fake_char_)); | |
108 ON_CALL(*fake_service_, GetCharacteristics()) | |
109 .WillByDefault( | |
110 testing::Return(fake_service_->GetMockCharacteristics())); | |
111 | |
112 remote_addr = mojom::BluetoothAddress::From(std::string(kAddressStr)); | |
113 | |
114 service_id = mojom::BluetoothGattServiceID::New(); | |
115 service_id->is_primary = true; | |
116 service_id->id = mojom::BluetoothGattID::New(); | |
117 service_id->id->inst_id = 0; | |
118 service_id->id->uuid = | |
119 mojom::BluetoothUUID::From(device::BluetoothUUID(kServiceUUID)); | |
120 | |
121 char_id = mojom::BluetoothGattID::New(); | |
122 char_id->inst_id = 0; | |
123 char_id->uuid = | |
124 mojom::BluetoothUUID::From(device::BluetoothUUID(kCharUUID)); | |
125 | |
126 mojom::BluetoothInstancePtr bluetooth_instance_ptr; | |
127 fake_bluetooth_instance = new FakeBluetoothInstance(); | |
128 fake_arc_bluetooth_bridge_->SetBluetoothInstanceForTest( | |
129 fake_bluetooth_instance); | |
130 } | 107 } |
131 | 108 |
132 void TearDown() override { | 109 void TearDown() override { |
133 fake_arc_bluetooth_bridge_->SetAdapterForTest(nullptr); | 110 delete arc_bluetooth_bridge_; |
134 fake_arc_bluetooth_bridge_->SetBluetoothInstanceForTest(nullptr); | |
135 delete fake_arc_bridge_service_; | 111 delete fake_arc_bridge_service_; |
136 } | 112 } |
| 113 |
| 114 base::MessageLoop message_loop_; |
| 115 base::RunLoop get_adapter_run_loop_; |
| 116 bluez::FakeBluetoothDeviceClient* fake_bluetooth_device_client_; |
137 }; | 117 }; |
138 | 118 |
139 // Invoke OnDiscoveryStarted to send cached device to BT instance, | 119 // Invoke OnDiscoveryStarted to send cached device to BT instance, |
140 // and check correctness of the device properties sent via arc bridge. | 120 // and check correctness of the device properties sent via arc bridge. |
141 TEST_F(ArcBluetoothBridgeTest, DeviceFound) { | 121 TEST_F(ArcBluetoothBridgeTest, DeviceFound) { |
142 fake_arc_bluetooth_bridge_->OnDiscoveryStarted(nullptr); | 122 AddTestDevice(); |
143 | 123 arc_bluetooth_bridge_->OnDiscoveryStarted(nullptr); |
144 EXPECT_EQ((size_t)1, fake_bluetooth_instance->device_found_data().size()); | 124 EXPECT_EQ((size_t)1, fake_bluetooth_instance_->device_found_data().size()); |
145 const mojo::Array<mojom::BluetoothPropertyPtr>& prop = | 125 const mojo::Array<mojom::BluetoothPropertyPtr>& prop = |
146 fake_bluetooth_instance->device_found_data().back(); | 126 fake_bluetooth_instance_->device_found_data().back(); |
147 | 127 |
148 EXPECT_EQ((size_t)7, prop.size()); | 128 EXPECT_EQ((size_t)7, prop.size()); |
149 EXPECT_TRUE(prop[0]->is_bdname()); | 129 EXPECT_TRUE(prop[0]->is_bdname()); |
150 EXPECT_EQ(std::string(kDeviceName), prop[0]->get_bdname()); | 130 EXPECT_EQ(std::string(kDeviceName), prop[0]->get_bdname()); |
151 EXPECT_TRUE(prop[1]->is_bdaddr()); | 131 EXPECT_TRUE(prop[1]->is_bdaddr()); |
152 EXPECT_EQ(std::string(kAddressStr), prop[1]->get_bdaddr()->To<std::string>()); | 132 EXPECT_EQ(std::string(kAddressStr), prop[1]->get_bdaddr()->To<std::string>()); |
153 EXPECT_TRUE(prop[2]->is_uuids()); | 133 EXPECT_TRUE(prop[2]->is_uuids()); |
154 EXPECT_EQ(device_uuids_, | 134 EXPECT_EQ( |
155 prop[2]->get_uuids().To<std::vector<device::BluetoothUUID>>()); | 135 device_uuids_[0], |
| 136 prop[2]->get_uuids().To<std::vector<device::BluetoothUUID>>()[0].value()); |
| 137 EXPECT_EQ( |
| 138 device_uuids_[1], |
| 139 prop[2]->get_uuids().To<std::vector<device::BluetoothUUID>>()[1].value()); |
156 EXPECT_TRUE(prop[3]->is_device_class()); | 140 EXPECT_TRUE(prop[3]->is_device_class()); |
157 EXPECT_EQ(kDeviceClass, prop[3]->get_device_class()); | |
158 EXPECT_TRUE(prop[4]->is_device_type()); | 141 EXPECT_TRUE(prop[4]->is_device_type()); |
159 EXPECT_EQ(mojom::BluetoothDeviceType::DUAL, prop[4]->get_device_type()); | 142 EXPECT_EQ(mojom::BluetoothDeviceType::DUAL, prop[4]->get_device_type()); |
160 EXPECT_TRUE(prop[5]->is_remote_friendly_name()); | 143 EXPECT_TRUE(prop[5]->is_remote_friendly_name()); |
161 EXPECT_EQ(std::string(kDeviceName), prop[5]->get_remote_friendly_name()); | 144 EXPECT_EQ(std::string(kDeviceName), prop[5]->get_remote_friendly_name()); |
162 EXPECT_TRUE(prop[6]->is_remote_rssi()); | 145 EXPECT_TRUE(prop[6]->is_remote_rssi()); |
163 EXPECT_EQ(kDeviceRssi, prop[6]->get_remote_rssi()); | |
164 } | 146 } |
165 | 147 |
166 // Invoke OnDiscoveryStarted to send cached device to BT instance, | 148 // Invoke OnDiscoveryStarted to send cached device to BT instance, |
167 // and check correctness of the Advertising data sent via arc bridge. | 149 // and check correctness of the Advertising data sent via arc bridge. |
168 TEST_F(ArcBluetoothBridgeTest, LEDeviceFound) { | 150 TEST_F(ArcBluetoothBridgeTest, LEDeviceFound) { |
| 151 fake_arc_bridge_service_->set_bluetooth_version(0); |
| 152 AddTestDevice(); |
| 153 arc_bluetooth_bridge_->OnDiscoveryStarted(nullptr); |
169 // Check that we check bluetooth_instance version correctly. | 154 // Check that we check bluetooth_instance version correctly. |
170 fake_arc_bluetooth_bridge_->OnDiscoveryStarted(nullptr); | 155 EXPECT_EQ((size_t)0, fake_bluetooth_instance_->le_device_found_data().size()); |
171 EXPECT_EQ((size_t)0, fake_bluetooth_instance->le_device_found_data().size()); | |
172 | 156 |
173 fake_arc_bluetooth_bridge_->SetBluetoothVersionForTest(1); | 157 fake_arc_bridge_service_->set_bluetooth_version(1); |
174 fake_arc_bluetooth_bridge_->OnDiscoveryStarted(nullptr); | 158 arc_bluetooth_bridge_->OnDiscoveryStarted(nullptr); |
175 EXPECT_EQ((size_t)1, fake_bluetooth_instance->le_device_found_data().size()); | 159 EXPECT_EQ((size_t)1, fake_bluetooth_instance_->le_device_found_data().size()); |
176 | 160 |
177 const mojom::BluetoothAddressPtr& addr = | 161 const mojom::BluetoothAddressPtr& addr = |
178 fake_bluetooth_instance->le_device_found_data().back()->addr(); | 162 fake_bluetooth_instance_->le_device_found_data().back()->addr(); |
179 int32_t rssi = fake_bluetooth_instance->le_device_found_data().back()->rssi(); | 163 // int32_t rssi = |
| 164 // fake_bluetooth_instance_->le_device_found_data().back()->rssi(); |
180 const mojo::Array<mojom::BluetoothAdvertisingDataPtr>& adv_data = | 165 const mojo::Array<mojom::BluetoothAdvertisingDataPtr>& adv_data = |
181 fake_bluetooth_instance->le_device_found_data().back()->adv_data(); | 166 fake_bluetooth_instance_->le_device_found_data().back()->adv_data(); |
182 | 167 |
183 EXPECT_EQ(std::string(kAddressStr), addr->To<std::string>()); | 168 EXPECT_EQ(std::string(kAddressStr), addr->To<std::string>()); |
184 EXPECT_EQ(kDeviceRssi, rssi); | |
185 EXPECT_EQ((size_t)1, adv_data.size()); | 169 EXPECT_EQ((size_t)1, adv_data.size()); |
186 | 170 |
187 EXPECT_TRUE(adv_data[0]->is_local_name()); | 171 EXPECT_TRUE(adv_data[0]->is_local_name()); |
188 EXPECT_EQ(std::string(kDeviceName), | 172 EXPECT_EQ(std::string(kDeviceName), |
189 adv_data[0]->get_local_name().To<std::string>()); | 173 adv_data[0]->get_local_name().To<std::string>()); |
190 } | 174 } |
191 | 175 |
192 // Invoke GetGattDB and check correctness of the GattDB sent via arc bridge. | 176 // Invoke GetGattDB and check correctness of the GattDB sent via arc bridge. |
193 TEST_F(ArcBluetoothBridgeTest, GetGattDB) { | 177 TEST_F(ArcBluetoothBridgeTest, GetGattDB) { |
194 fake_arc_bluetooth_bridge_->GetGattDB(remote_addr.Clone()); | 178 AddTestDevice(); |
| 179 arc_bluetooth_bridge_->GetGattDB(remote_addr_.Clone()); |
195 | 180 |
196 EXPECT_EQ((size_t)1, fake_bluetooth_instance->gatt_db_result().size()); | 181 EXPECT_EQ((size_t)1, fake_bluetooth_instance_->gatt_db_result().size()); |
197 | 182 |
198 const mojom::BluetoothAddressPtr& addr = | 183 const mojom::BluetoothAddressPtr& addr = |
199 fake_bluetooth_instance->gatt_db_result().back()->remote_addr(); | 184 fake_bluetooth_instance_->gatt_db_result().back()->remote_addr(); |
200 const mojo::Array<mojom::BluetoothGattDBElementPtr>& db = | 185 const mojo::Array<mojom::BluetoothGattDBElementPtr>& db = |
201 fake_bluetooth_instance->gatt_db_result().back()->db(); | 186 fake_bluetooth_instance_->gatt_db_result().back()->db(); |
202 | 187 |
203 EXPECT_EQ(std::string(kAddressStr), addr->To<std::string>()); | 188 EXPECT_EQ(std::string(kAddressStr), addr->To<std::string>()); |
204 EXPECT_EQ((size_t)2, db.size()); | 189 EXPECT_EQ((size_t)2, db.size()); |
205 | 190 |
206 EXPECT_EQ(device::BluetoothUUID(kServiceUUID), | 191 EXPECT_EQ(device::BluetoothUUID(kServiceUUID), |
207 db[0]->uuid.To<device::BluetoothUUID>()); | 192 db[0]->uuid.To<device::BluetoothUUID>()); |
208 EXPECT_EQ(mojom::BluetoothGattDBAttributeType::BTGATT_DB_PRIMARY_SERVICE, | 193 EXPECT_EQ(mojom::BluetoothGattDBAttributeType::BTGATT_DB_PRIMARY_SERVICE, |
209 db[0]->type); | 194 db[0]->type); |
210 EXPECT_EQ(kCharUUIDInt, db[0]->start_handle); | 195 EXPECT_EQ(kCharUUIDInt, db[0]->start_handle); |
211 EXPECT_EQ(kCharUUIDInt, db[0]->end_handle); | 196 EXPECT_EQ(kCharUUIDInt, db[0]->end_handle); |
212 | 197 |
213 EXPECT_EQ(device::BluetoothUUID(kCharUUID), | 198 EXPECT_EQ(device::BluetoothUUID(kCharUUID), |
214 db[1]->uuid.To<device::BluetoothUUID>()); | 199 db[1]->uuid.To<device::BluetoothUUID>()); |
215 EXPECT_EQ(mojom::BluetoothGattDBAttributeType::BTGATT_DB_CHARACTERISTIC, | 200 EXPECT_EQ(mojom::BluetoothGattDBAttributeType::BTGATT_DB_CHARACTERISTIC, |
216 db[1]->type); | 201 db[1]->type); |
217 EXPECT_EQ(device::BluetoothGattCharacteristic::PROPERTY_READ, | 202 EXPECT_EQ(device::BluetoothGattCharacteristic::PROPERTY_READ, |
218 db[1]->properties); | 203 db[1]->properties); |
219 } | 204 } |
220 | 205 |
221 } // namespace arc | 206 } // namespace arc |
OLD | NEW |