Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(188)

Side by Side Diff: device/bluetooth/bluetooth_gatt_chromeos_unittest.cc

Issue 1341103004: Handle change of BLE address after pairing (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: ***ADDED BY MISTAKE, OMMIT *** Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "base/memory/scoped_vector.h" 5 #include "base/memory/scoped_vector.h"
6 #include "base/message_loop/message_loop.h" 6 #include "base/message_loop/message_loop.h"
7 #include "base/run_loop.h" 7 #include "base/run_loop.h"
8 #include "dbus/object_path.h" 8 #include "dbus/object_path.h"
9 #include "device/bluetooth/bluetooth_adapter.h" 9 #include "device/bluetooth/bluetooth_adapter.h"
10 #include "device/bluetooth/bluetooth_adapter_factory.h" 10 #include "device/bluetooth/bluetooth_adapter_factory.h"
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
106 adapter_->SetPowered( 106 adapter_->SetPowered(
107 true, 107 true,
108 base::Bind(&base::DoNothing), 108 base::Bind(&base::DoNothing),
109 base::Bind(&base::DoNothing)); 109 base::Bind(&base::DoNothing));
110 ASSERT_TRUE(adapter_->IsPowered()); 110 ASSERT_TRUE(adapter_->IsPowered());
111 } 111 }
112 112
113 void TearDown() override { 113 void TearDown() override {
114 adapter_ = NULL; 114 adapter_ = NULL;
115 update_sessions_.clear(); 115 update_sessions_.clear();
116 gatt_conn_.reset();
117 bluez::BluezDBusManager::Shutdown(); 116 bluez::BluezDBusManager::Shutdown();
118 } 117 }
119 118
120 void GetAdapter() { 119 void GetAdapter() {
121 device::BluetoothAdapterFactory::GetAdapter( 120 device::BluetoothAdapterFactory::GetAdapter(
122 base::Bind(&BluetoothGattChromeOSTest::AdapterCallback, 121 base::Bind(&BluetoothGattChromeOSTest::AdapterCallback,
123 base::Unretained(this))); 122 base::Unretained(this)));
124 ASSERT_TRUE(adapter_.get() != NULL); 123 ASSERT_TRUE(adapter_.get() != NULL);
125 ASSERT_TRUE(adapter_->IsInitialized()); 124 ASSERT_TRUE(adapter_->IsInitialized());
126 ASSERT_TRUE(adapter_->IsPresent()); 125 ASSERT_TRUE(adapter_->IsPresent());
127 } 126 }
128 127
129 void AdapterCallback(scoped_refptr<BluetoothAdapter> adapter) { 128 void AdapterCallback(scoped_refptr<BluetoothAdapter> adapter) {
130 adapter_ = adapter; 129 adapter_ = adapter;
131 } 130 }
132 131
133 void SuccessCallback() { 132 void SuccessCallback() {
134 ++success_callback_count_; 133 ++success_callback_count_;
135 } 134 }
136 135
137 void ValueCallback(const std::vector<uint8>& value) { 136 void ValueCallback(const std::vector<uint8>& value) {
138 ++success_callback_count_; 137 ++success_callback_count_;
139 last_read_value_ = value; 138 last_read_value_ = value;
140 } 139 }
141 140
142 void GattConnectionCallback(scoped_ptr<BluetoothGattConnection> conn) { 141 void GattConnectionCallback() { ++success_callback_count_; }
143 ++success_callback_count_;
144 gatt_conn_ = conn.Pass();
145 }
146 142
147 void NotifySessionCallback(scoped_ptr<BluetoothGattNotifySession> session) { 143 void NotifySessionCallback(scoped_ptr<BluetoothGattNotifySession> session) {
148 ++success_callback_count_; 144 ++success_callback_count_;
149 update_sessions_.push_back(session.release()); 145 update_sessions_.push_back(session.release());
150 QuitMessageLoop(); 146 QuitMessageLoop();
151 } 147 }
152 148
153 void ServiceErrorCallback(BluetoothGattService::GattErrorCode err) { 149 void ServiceErrorCallback(BluetoothGattService::GattErrorCode err) {
154 ++error_callback_count_; 150 ++error_callback_count_;
155 last_service_error_ = err; 151 last_service_error_ = err;
(...skipping 20 matching lines...) Expand all
176 } 172 }
177 173
178 base::MessageLoop message_loop_; 174 base::MessageLoop message_loop_;
179 175
180 bluez::FakeBluetoothDeviceClient* fake_bluetooth_device_client_; 176 bluez::FakeBluetoothDeviceClient* fake_bluetooth_device_client_;
181 bluez::FakeBluetoothGattServiceClient* fake_bluetooth_gatt_service_client_; 177 bluez::FakeBluetoothGattServiceClient* fake_bluetooth_gatt_service_client_;
182 bluez::FakeBluetoothGattCharacteristicClient* 178 bluez::FakeBluetoothGattCharacteristicClient*
183 fake_bluetooth_gatt_characteristic_client_; 179 fake_bluetooth_gatt_characteristic_client_;
184 bluez::FakeBluetoothGattDescriptorClient* 180 bluez::FakeBluetoothGattDescriptorClient*
185 fake_bluetooth_gatt_descriptor_client_; 181 fake_bluetooth_gatt_descriptor_client_;
186 scoped_ptr<device::BluetoothGattConnection> gatt_conn_;
187 ScopedVector<BluetoothGattNotifySession> update_sessions_; 182 ScopedVector<BluetoothGattNotifySession> update_sessions_;
188 scoped_refptr<BluetoothAdapter> adapter_; 183 scoped_refptr<BluetoothAdapter> adapter_;
189 184
190 int success_callback_count_; 185 int success_callback_count_;
191 int error_callback_count_; 186 int error_callback_count_;
192 std::vector<uint8> last_read_value_; 187 std::vector<uint8> last_read_value_;
193 BluetoothGattService::GattErrorCode last_service_error_; 188 BluetoothGattService::GattErrorCode last_service_error_;
194 }; 189 };
195 190
196 TEST_F(BluetoothGattChromeOSTest, GattConnection) { 191 TEST_F(BluetoothGattChromeOSTest, GattConnection) {
192 scoped_ptr<device::BluetoothGattConnection> gatt_conn_;
193
197 fake_bluetooth_device_client_->CreateDevice( 194 fake_bluetooth_device_client_->CreateDevice(
198 dbus::ObjectPath(bluez::FakeBluetoothAdapterClient::kAdapterPath), 195 dbus::ObjectPath(bluez::FakeBluetoothAdapterClient::kAdapterPath),
199 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kLowEnergyPath)); 196 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kLowEnergyPath));
200 BluetoothDevice* device = 197 BluetoothDevice* device =
201 adapter_->GetDevice(bluez::FakeBluetoothDeviceClient::kLowEnergyAddress); 198 adapter_->GetDevice(bluez::FakeBluetoothDeviceClient::kLowEnergyAddress);
202 ASSERT_TRUE(device); 199 ASSERT_TRUE(device);
203 ASSERT_FALSE(device->IsConnected()); 200 ASSERT_FALSE(device->IsConnected());
204 ASSERT_FALSE(gatt_conn_.get()); 201 ASSERT_FALSE(gatt_conn_.get());
205 ASSERT_EQ(0, success_callback_count_); 202 ASSERT_EQ(0, success_callback_count_);
206 ASSERT_EQ(0, error_callback_count_); 203 ASSERT_EQ(0, error_callback_count_);
207 204
208 device->CreateGattConnection( 205 gatt_conn_ = device->CreateGattConnection(
209 base::Bind(&BluetoothGattChromeOSTest::GattConnectionCallback, 206 base::Bind(&BluetoothGattChromeOSTest::GattConnectionCallback,
210 base::Unretained(this)), 207 base::Unretained(this)),
211 base::Bind(&BluetoothGattChromeOSTest::ConnectErrorCallback, 208 base::Bind(&BluetoothGattChromeOSTest::ConnectErrorCallback,
212 base::Unretained(this))); 209 base::Unretained(this)));
213 210
214 EXPECT_EQ(1, success_callback_count_); 211 EXPECT_EQ(1, success_callback_count_);
215 EXPECT_EQ(0, error_callback_count_); 212 EXPECT_EQ(0, error_callback_count_);
216 EXPECT_TRUE(device->IsConnected()); 213 EXPECT_TRUE(device->IsConnected());
217 ASSERT_TRUE(gatt_conn_.get()); 214 ASSERT_TRUE(gatt_conn_.get());
218 EXPECT_TRUE(gatt_conn_->IsConnected()); 215 EXPECT_TRUE(gatt_conn_->IsConnected());
216 EXPECT_FALSE(gatt_conn_->InProgress());
219 EXPECT_EQ(bluez::FakeBluetoothDeviceClient::kLowEnergyAddress, 217 EXPECT_EQ(bluez::FakeBluetoothDeviceClient::kLowEnergyAddress,
220 gatt_conn_->GetDeviceAddress()); 218 gatt_conn_->GetDeviceAddress());
221 219
222 gatt_conn_->Disconnect(); 220 gatt_conn_->Disconnect();
223 EXPECT_TRUE(device->IsConnected()); 221 EXPECT_FALSE(device->IsConnected());
224 EXPECT_FALSE(gatt_conn_->IsConnected()); 222 EXPECT_FALSE(gatt_conn_->IsConnected());
225 223
226 device->CreateGattConnection( 224 gatt_conn_ = device->CreateGattConnection(
227 base::Bind(&BluetoothGattChromeOSTest::GattConnectionCallback, 225 base::Bind(&BluetoothGattChromeOSTest::GattConnectionCallback,
228 base::Unretained(this)), 226 base::Unretained(this)),
229 base::Bind(&BluetoothGattChromeOSTest::ConnectErrorCallback, 227 base::Bind(&BluetoothGattChromeOSTest::ConnectErrorCallback,
230 base::Unretained(this))); 228 base::Unretained(this)));
231 229
232 EXPECT_EQ(2, success_callback_count_); 230 EXPECT_EQ(2, success_callback_count_);
233 EXPECT_EQ(0, error_callback_count_); 231 EXPECT_EQ(0, error_callback_count_);
234 EXPECT_TRUE(device->IsConnected()); 232 EXPECT_TRUE(device->IsConnected());
235 ASSERT_TRUE(gatt_conn_.get()); 233 ASSERT_TRUE(gatt_conn_.get());
236 EXPECT_TRUE(gatt_conn_->IsConnected()); 234 EXPECT_TRUE(gatt_conn_->IsConnected());
237 235
238 device->Disconnect( 236 device->Disconnect(
239 base::Bind(&BluetoothGattChromeOSTest::SuccessCallback, 237 base::Bind(&BluetoothGattChromeOSTest::SuccessCallback,
240 base::Unretained(this)), 238 base::Unretained(this)),
241 base::Bind(&BluetoothGattChromeOSTest::ErrorCallback, 239 base::Bind(&BluetoothGattChromeOSTest::ErrorCallback,
242 base::Unretained(this))); 240 base::Unretained(this)));
243 241
244 EXPECT_EQ(3, success_callback_count_); 242 EXPECT_EQ(3, success_callback_count_);
245 EXPECT_EQ(0, error_callback_count_); 243 EXPECT_EQ(0, error_callback_count_);
244 EXPECT_FALSE(device->IsConnected());
246 ASSERT_TRUE(gatt_conn_.get()); 245 ASSERT_TRUE(gatt_conn_.get());
247 EXPECT_FALSE(gatt_conn_->IsConnected()); 246 EXPECT_FALSE(gatt_conn_->IsConnected());
248 247
249 device->CreateGattConnection( 248 gatt_conn_ = device->CreateGattConnection(
250 base::Bind(&BluetoothGattChromeOSTest::GattConnectionCallback, 249 base::Bind(&BluetoothGattChromeOSTest::GattConnectionCallback,
251 base::Unretained(this)), 250 base::Unretained(this)),
252 base::Bind(&BluetoothGattChromeOSTest::ConnectErrorCallback, 251 base::Bind(&BluetoothGattChromeOSTest::ConnectErrorCallback,
253 base::Unretained(this))); 252 base::Unretained(this)));
254 253
255 EXPECT_EQ(4, success_callback_count_); 254 EXPECT_EQ(4, success_callback_count_);
256 EXPECT_EQ(0, error_callback_count_); 255 EXPECT_EQ(0, error_callback_count_);
257 EXPECT_TRUE(device->IsConnected()); 256 EXPECT_TRUE(device->IsConnected());
258 EXPECT_TRUE(gatt_conn_->IsConnected()); 257 EXPECT_TRUE(gatt_conn_->IsConnected());
259 258
260 fake_bluetooth_device_client_->RemoveDevice( 259 fake_bluetooth_device_client_->RemoveDevice(
261 dbus::ObjectPath(bluez::FakeBluetoothAdapterClient::kAdapterPath), 260 dbus::ObjectPath(bluez::FakeBluetoothAdapterClient::kAdapterPath),
262 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kLowEnergyPath)); 261 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kLowEnergyPath));
263 ASSERT_TRUE(gatt_conn_.get()); 262 ASSERT_TRUE(gatt_conn_.get());
264 EXPECT_FALSE(gatt_conn_->IsConnected()); 263 EXPECT_FALSE(gatt_conn_->IsConnected());
265 } 264 }
266 265
266 TEST_F(BluetoothGattChromeOSTest, MultipleGattConnections) {
267 scoped_ptr<device::BluetoothGattConnection> gatt_conn_1;
268 scoped_ptr<device::BluetoothGattConnection> gatt_conn_2;
269
270 fake_bluetooth_device_client_->CreateDevice(
271 dbus::ObjectPath(bluez::FakeBluetoothAdapterClient::kAdapterPath),
272 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kLowEnergyPath));
273 BluetoothDevice* device =
274 adapter_->GetDevice(bluez::FakeBluetoothDeviceClient::kLowEnergyAddress);
275 ASSERT_TRUE(device);
276 ASSERT_FALSE(device->IsConnected());
277 ASSERT_FALSE(gatt_conn_1.get());
278 ASSERT_FALSE(gatt_conn_2.get());
279 ASSERT_EQ(0, success_callback_count_);
280 ASSERT_EQ(0, error_callback_count_);
281
282 // Connect first gatt client
283 gatt_conn_1 = device->CreateGattConnection(
284 base::Bind(&BluetoothGattChromeOSTest::GattConnectionCallback,
285 base::Unretained(this)),
286 base::Bind(&BluetoothGattChromeOSTest::ConnectErrorCallback,
287 base::Unretained(this)));
288
289 EXPECT_EQ(1, success_callback_count_);
290 EXPECT_EQ(0, error_callback_count_);
291 EXPECT_TRUE(device->IsConnected());
292 ASSERT_TRUE(gatt_conn_1.get());
293 EXPECT_TRUE(gatt_conn_1->IsConnected());
294 EXPECT_FALSE(gatt_conn_1->InProgress());
295 EXPECT_EQ(bluez::FakeBluetoothDeviceClient::kLowEnergyAddress,
296 gatt_conn_1->GetDeviceAddress());
297
298 // Connect second gatt client
299 gatt_conn_2 = device->CreateGattConnection(
300 base::Bind(&BluetoothGattChromeOSTest::GattConnectionCallback,
301 base::Unretained(this)),
302 base::Bind(&BluetoothGattChromeOSTest::ConnectErrorCallback,
303 base::Unretained(this)));
304
305 EXPECT_EQ(2, success_callback_count_);
306 EXPECT_EQ(0, error_callback_count_);
307 EXPECT_TRUE(device->IsConnected());
308 ASSERT_TRUE(gatt_conn_2.get());
309 EXPECT_TRUE(gatt_conn_2->IsConnected());
310 EXPECT_FALSE(gatt_conn_2->InProgress());
311 EXPECT_EQ(bluez::FakeBluetoothDeviceClient::kLowEnergyAddress,
312 gatt_conn_2->GetDeviceAddress());
313
314 // Disconnect first client
315 gatt_conn_1->Disconnect();
316 EXPECT_TRUE(device->IsConnected());
317 EXPECT_FALSE(gatt_conn_1->IsConnected());
318 EXPECT_TRUE(gatt_conn_2->IsConnected());
319
320 // Connect firt client again
321 gatt_conn_1 = device->CreateGattConnection(
322 base::Bind(&BluetoothGattChromeOSTest::GattConnectionCallback,
323 base::Unretained(this)),
324 base::Bind(&BluetoothGattChromeOSTest::ConnectErrorCallback,
325 base::Unretained(this)));
326
327 EXPECT_EQ(3, success_callback_count_);
328 EXPECT_EQ(0, error_callback_count_);
329 EXPECT_TRUE(device->IsConnected());
330 ASSERT_TRUE(gatt_conn_1.get());
331 EXPECT_TRUE(gatt_conn_1->IsConnected());
332 EXPECT_TRUE(gatt_conn_2->IsConnected());
333
334 gatt_conn_1->Disconnect();
335 EXPECT_TRUE(device->IsConnected());
336 EXPECT_FALSE(gatt_conn_1->IsConnected());
337 EXPECT_TRUE(gatt_conn_2->IsConnected());
338
339 gatt_conn_2->Disconnect();
340 EXPECT_FALSE(device->IsConnected());
341 EXPECT_FALSE(gatt_conn_1->IsConnected());
342 EXPECT_FALSE(gatt_conn_2->IsConnected());
343
344 // Connect both clients again
345 gatt_conn_1 = device->CreateGattConnection(
346 base::Bind(&BluetoothGattChromeOSTest::GattConnectionCallback,
347 base::Unretained(this)),
348 base::Bind(&BluetoothGattChromeOSTest::ConnectErrorCallback,
349 base::Unretained(this)));
350 gatt_conn_2 = device->CreateGattConnection(
351 base::Bind(&BluetoothGattChromeOSTest::GattConnectionCallback,
352 base::Unretained(this)),
353 base::Bind(&BluetoothGattChromeOSTest::ConnectErrorCallback,
354 base::Unretained(this)));
355
356 EXPECT_EQ(5, success_callback_count_);
357 EXPECT_TRUE(device->IsConnected());
358 EXPECT_TRUE(gatt_conn_1->IsConnected());
359 EXPECT_TRUE(gatt_conn_2->IsConnected());
360
361 // Disconnect whole device
362 device->Disconnect(base::Bind(&BluetoothGattChromeOSTest::SuccessCallback,
363 base::Unretained(this)),
364 base::Bind(&BluetoothGattChromeOSTest::ErrorCallback,
365 base::Unretained(this)));
366
367 EXPECT_EQ(6, success_callback_count_);
368 EXPECT_EQ(0, error_callback_count_);
369 EXPECT_FALSE(device->IsConnected());
370 ASSERT_TRUE(gatt_conn_1.get());
371 EXPECT_FALSE(gatt_conn_1->IsConnected());
372 ASSERT_TRUE(gatt_conn_1.get());
373 EXPECT_FALSE(gatt_conn_1->IsConnected());
374
375 // Connect both clients again
376 gatt_conn_1 = device->CreateGattConnection(
377 base::Bind(&BluetoothGattChromeOSTest::GattConnectionCallback,
378 base::Unretained(this)),
379 base::Bind(&BluetoothGattChromeOSTest::ConnectErrorCallback,
380 base::Unretained(this)));
381 gatt_conn_2 = device->CreateGattConnection(
382 base::Bind(&BluetoothGattChromeOSTest::GattConnectionCallback,
383 base::Unretained(this)),
384 base::Bind(&BluetoothGattChromeOSTest::ConnectErrorCallback,
385 base::Unretained(this)));
386
387 EXPECT_EQ(8, success_callback_count_);
388 EXPECT_TRUE(device->IsConnected());
389 EXPECT_TRUE(gatt_conn_1->IsConnected());
390 EXPECT_TRUE(gatt_conn_2->IsConnected());
391
392 fake_bluetooth_device_client_->RemoveDevice(
393 dbus::ObjectPath(bluez::FakeBluetoothAdapterClient::kAdapterPath),
394 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kLowEnergyPath));
395 ASSERT_TRUE(gatt_conn_1.get());
396 EXPECT_FALSE(gatt_conn_1->IsConnected());
397 ASSERT_TRUE(gatt_conn_2.get());
398 EXPECT_FALSE(gatt_conn_2->IsConnected());
399 }
400
267 TEST_F(BluetoothGattChromeOSTest, GattServiceAddedAndRemoved) { 401 TEST_F(BluetoothGattChromeOSTest, GattServiceAddedAndRemoved) {
268 // Create a fake LE device. We store the device pointer here because this is a 402 // Create a fake LE device. We store the device pointer here because this is a
269 // test. It's unsafe to do this in production as the device might get deleted. 403 // test. It's unsafe to do this in production as the device might get deleted.
270 fake_bluetooth_device_client_->CreateDevice( 404 fake_bluetooth_device_client_->CreateDevice(
271 dbus::ObjectPath(bluez::FakeBluetoothAdapterClient::kAdapterPath), 405 dbus::ObjectPath(bluez::FakeBluetoothAdapterClient::kAdapterPath),
272 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kLowEnergyPath)); 406 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kLowEnergyPath));
273 BluetoothDevice* device = 407 BluetoothDevice* device =
274 adapter_->GetDevice(bluez::FakeBluetoothDeviceClient::kLowEnergyAddress); 408 adapter_->GetDevice(bluez::FakeBluetoothDeviceClient::kLowEnergyAddress);
275 ASSERT_TRUE(device); 409 ASSERT_TRUE(device);
276 410
(...skipping 951 matching lines...) Expand 10 before | Expand all | Expand 10 after
1228 1362
1229 EXPECT_EQ(1, success_callback_count_); 1363 EXPECT_EQ(1, success_callback_count_);
1230 EXPECT_EQ(0, error_callback_count_); 1364 EXPECT_EQ(0, error_callback_count_);
1231 EXPECT_EQ(1, observer.gatt_characteristic_value_changed_count()); 1365 EXPECT_EQ(1, observer.gatt_characteristic_value_changed_count());
1232 EXPECT_TRUE(characteristic->IsNotifying()); 1366 EXPECT_TRUE(characteristic->IsNotifying());
1233 EXPECT_EQ(1U, update_sessions_.size()); 1367 EXPECT_EQ(1U, update_sessions_.size());
1234 EXPECT_TRUE(update_sessions_[0]->IsActive()); 1368 EXPECT_TRUE(update_sessions_[0]->IsActive());
1235 } 1369 }
1236 1370
1237 } // namespace chromeos 1371 } // namespace chromeos
OLDNEW
« no previous file with comments | « device/bluetooth/bluetooth_device_chromeos.cc ('k') | device/bluetooth/bluetooth_gatt_connection.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698