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

Side by Side Diff: device/bluetooth/bluez/bluetooth_gatt_bluez_unittest.cc

Issue 1979633004: Invoke GattDiscoveryCompleteForService by observing ServicesResolved property (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@remove_attr
Patch Set: Update comments. Created 4 years, 7 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 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 <stddef.h> 5 #include <stddef.h>
6 #include <stdint.h> 6 #include <stdint.h>
7 7
8 #include <memory> 8 #include <memory>
9 #include <utility> 9 #include <utility>
10 10
(...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after
318 bluez::FakeBluetoothGattServiceClient::kHeartRateServiceUUID), 318 bluez::FakeBluetoothGattServiceClient::kHeartRateServiceUUID),
319 observer.last_gatt_service_uuid()); 319 observer.last_gatt_service_uuid());
320 320
321 EXPECT_EQ(NULL, device->GetGattService(observer.last_gatt_service_id())); 321 EXPECT_EQ(NULL, device->GetGattService(observer.last_gatt_service_id()));
322 322
323 // Expose the service again. 323 // Expose the service again.
324 observer.last_gatt_service_uuid() = BluetoothUUID(); 324 observer.last_gatt_service_uuid() = BluetoothUUID();
325 observer.last_gatt_service_id().clear(); 325 observer.last_gatt_service_id().clear();
326 fake_bluetooth_gatt_service_client_->ExposeHeartRateService( 326 fake_bluetooth_gatt_service_client_->ExposeHeartRateService(
327 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kLowEnergyPath)); 327 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kLowEnergyPath));
328
328 EXPECT_EQ(2, observer.gatt_service_added_count()); 329 EXPECT_EQ(2, observer.gatt_service_added_count());
329 EXPECT_EQ(1, observer.gatt_service_removed_count()); 330 EXPECT_EQ(1, observer.gatt_service_removed_count());
330 EXPECT_FALSE(observer.last_gatt_service_id().empty()); 331 EXPECT_FALSE(observer.last_gatt_service_id().empty());
331 EXPECT_EQ(1U, device->GetGattServices().size()); 332 EXPECT_EQ(1U, device->GetGattServices().size());
332 EXPECT_EQ(BluetoothUUID( 333 EXPECT_EQ(BluetoothUUID(
333 bluez::FakeBluetoothGattServiceClient::kHeartRateServiceUUID), 334 bluez::FakeBluetoothGattServiceClient::kHeartRateServiceUUID),
334 observer.last_gatt_service_uuid()); 335 observer.last_gatt_service_uuid());
335 336
336 // The object |service| points to should have been deallocated. |device| 337 // The object |service| points to should have been deallocated. |device|
337 // should contain a brand new instance. 338 // should contain a brand new instance.
(...skipping 14 matching lines...) Expand all
352 EXPECT_EQ(2, observer.gatt_service_added_count()); 353 EXPECT_EQ(2, observer.gatt_service_added_count());
353 EXPECT_EQ(2, observer.gatt_service_removed_count()); 354 EXPECT_EQ(2, observer.gatt_service_removed_count());
354 EXPECT_FALSE(observer.last_gatt_service_id().empty()); 355 EXPECT_FALSE(observer.last_gatt_service_id().empty());
355 EXPECT_EQ(BluetoothUUID( 356 EXPECT_EQ(BluetoothUUID(
356 bluez::FakeBluetoothGattServiceClient::kHeartRateServiceUUID), 357 bluez::FakeBluetoothGattServiceClient::kHeartRateServiceUUID),
357 observer.last_gatt_service_uuid()); 358 observer.last_gatt_service_uuid());
358 EXPECT_EQ(NULL, adapter_->GetDevice( 359 EXPECT_EQ(NULL, adapter_->GetDevice(
359 bluez::FakeBluetoothDeviceClient::kLowEnergyAddress)); 360 bluez::FakeBluetoothDeviceClient::kLowEnergyAddress));
360 } 361 }
361 362
363 TEST_F(BluetoothGattBlueZTest, DiscoveryCompleteForCashedGattService) {
rkc 2016/05/17 18:38:50 s/Cashed/Cached
Miao 2016/05/18 03:26:58 Done.
364 // This tests if the notifications on service discovered complete are invoked
365 // with the cached services and added to the GATT service map of |device|.
366 TestBluetoothAdapterObserver observer(adapter_);
367
368 // Create the device and pre-expose the fake Heart Rate service. This will
369 // synchronously expose characteristics.
370 fake_bluetooth_device_client_->CreateDevice(
371 dbus::ObjectPath(bluez::FakeBluetoothAdapterClient::kAdapterPath),
372 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kCachedLowEnergyPath));
373 BluetoothDevice* device =
374 adapter_->GetDevice(bluez::FakeBluetoothDeviceClient::kLowEnergyAddress);
375 ASSERT_TRUE(device);
376
377 device::BluetoothRemoteGattService* service = device->GetGattServices()[0];
378 EXPECT_EQ(1u, device->GetGattServices().size());
379 EXPECT_EQ(1, observer.gatt_discovery_complete_count());
380 EXPECT_EQ(device, service->GetDevice());
381 EXPECT_EQ(service, device->GetGattService(service->GetIdentifier()));
382 EXPECT_EQ(3U, service->GetCharacteristics().size());
383 }
384
385 TEST_F(BluetoothGattBlueZTest, DiscoveryCompleteForNewGattService) {
386 // This tests the discovery complete notification on a newly-added GATT
387 // service.
388 fake_bluetooth_device_client_->CreateDevice(
389 dbus::ObjectPath(bluez::FakeBluetoothAdapterClient::kAdapterPath),
390 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kLowEnergyPath));
391 BluetoothDevice* device =
392 adapter_->GetDevice(bluez::FakeBluetoothDeviceClient::kLowEnergyAddress);
393
394 ASSERT_TRUE(device);
395
396 TestBluetoothAdapterObserver observer(adapter_);
scheib 2016/05/17 19:24:47 Move the observer up to before CreateDevice and ve
Miao 2016/05/18 03:26:58 Done.
397
398 // Expose the fake Heart Rate service. This will asynchronously expose
399 // characteristics.
400 fake_bluetooth_gatt_service_client_->ExposeHeartRateService(
401 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kLowEnergyPath));
402
403 // Run the message loop so that the characteristics/descriptors appear.
404 base::MessageLoop::current()->Run();
405
406 // The discovery completed event of a newly-added GATT service should not be
407 // fired until ServicesResolved property becomes true. And the new service
408 // will be added immediately to the GATT service map of |device|.
409 BluetoothRemoteGattService* service = device->GetGattServices()[0];
410 EXPECT_EQ(1u, device->GetGattServices().size());
411 EXPECT_EQ(1, observer.gatt_discovery_complete_count());
412 EXPECT_EQ(device, service->GetDevice());
413 EXPECT_EQ(service, device->GetGattService(service->GetIdentifier()));
414 EXPECT_EQ(3U, service->GetCharacteristics().size());
415 }
416
362 TEST_F(BluetoothGattBlueZTest, ServicesDiscovered) { 417 TEST_F(BluetoothGattBlueZTest, ServicesDiscovered) {
363 // Create a fake LE device. We store the device pointer here because this is a 418 // Create a fake LE device. We store the device pointer here because this is a
364 // test. It's unsafe to do this in production as the device might get deleted. 419 // test. It's unsafe to do this in production as the device might get deleted.
365 fake_bluetooth_device_client_->CreateDevice( 420 fake_bluetooth_device_client_->CreateDevice(
366 dbus::ObjectPath(bluez::FakeBluetoothAdapterClient::kAdapterPath), 421 dbus::ObjectPath(bluez::FakeBluetoothAdapterClient::kAdapterPath),
367 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kLowEnergyPath)); 422 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kLowEnergyPath));
368 BluetoothDevice* device = 423 BluetoothDevice* device =
369 adapter_->GetDevice(bluez::FakeBluetoothDeviceClient::kLowEnergyAddress); 424 adapter_->GetDevice(bluez::FakeBluetoothDeviceClient::kLowEnergyAddress);
370 bluez::FakeBluetoothDeviceClient::Properties* properties = 425 bluez::FakeBluetoothDeviceClient::Properties* properties =
371 fake_bluetooth_device_client_->GetProperties( 426 fake_bluetooth_device_client_->GetProperties(
372 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kLowEnergyPath)); 427 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kLowEnergyPath));
373 428
374 ASSERT_TRUE(device); 429 ASSERT_TRUE(device);
375 430
376 TestBluetoothAdapterObserver observer(adapter_); 431 TestBluetoothAdapterObserver observer(adapter_);
377 432
378 // Expose the fake Heart Rate Service. 433 // Expose the fake Heart Rate Service.
379 fake_bluetooth_gatt_service_client_->ExposeHeartRateService( 434 fake_bluetooth_gatt_service_client_->ExposeHeartRateService(
380 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kLowEnergyPath)); 435 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kLowEnergyPath));
381 // Notify that all services have been discovered. 436 // Run the message loop so that the characteristics/descriptors appear.
382 properties->services_resolved.ReplaceValue(true); 437 base::MessageLoop::current()->Run();
383
384 EXPECT_TRUE(device->IsGattServicesDiscoveryComplete()); 438 EXPECT_TRUE(device->IsGattServicesDiscoveryComplete());
385 EXPECT_EQ(1u, device->GetGattServices().size()); 439 EXPECT_EQ(1u, device->GetGattServices().size());
386 EXPECT_EQ(device, observer.last_device()); 440 EXPECT_EQ(device, observer.last_device());
387 EXPECT_EQ(bluez::FakeBluetoothDeviceClient::kLowEnergyAddress, 441 EXPECT_EQ(bluez::FakeBluetoothDeviceClient::kLowEnergyAddress,
388 observer.last_device_address()); 442 observer.last_device_address());
389 443
390 // Disconnect from the device: 444 // Disconnect from the device:
391 device->Disconnect(base::Bind(&BluetoothGattBlueZTest::SuccessCallback, 445 device->Disconnect(base::Bind(&BluetoothGattBlueZTest::SuccessCallback,
392 base::Unretained(this)), 446 base::Unretained(this)),
393 base::Bind(&BluetoothGattBlueZTest::ErrorCallback, 447 base::Bind(&BluetoothGattBlueZTest::ErrorCallback,
(...skipping 11 matching lines...) Expand all
405 base::Bind(&BluetoothGattBlueZTest::GattConnectionCallback, 459 base::Bind(&BluetoothGattBlueZTest::GattConnectionCallback,
406 base::Unretained(this)), 460 base::Unretained(this)),
407 base::Bind(&BluetoothGattBlueZTest::ConnectErrorCallback, 461 base::Bind(&BluetoothGattBlueZTest::ConnectErrorCallback,
408 base::Unretained(this))); 462 base::Unretained(this)));
409 properties->connected.ReplaceValue(true); 463 properties->connected.ReplaceValue(true);
410 EXPECT_TRUE(device->IsConnected()); 464 EXPECT_TRUE(device->IsConnected());
411 465
412 // Verify that service discovery can be done again: 466 // Verify that service discovery can be done again:
413 fake_bluetooth_gatt_service_client_->ExposeHeartRateService( 467 fake_bluetooth_gatt_service_client_->ExposeHeartRateService(
414 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kLowEnergyPath)); 468 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kLowEnergyPath));
415 properties->services_resolved.ReplaceValue(true); 469 // Run the message loop so that the characteristics/descriptors appear.
470 base::MessageLoop::current()->Run();
416 EXPECT_TRUE(device->IsGattServicesDiscoveryComplete()); 471 EXPECT_TRUE(device->IsGattServicesDiscoveryComplete());
417 EXPECT_EQ(1u, device->GetGattServices().size()); 472 EXPECT_EQ(1u, device->GetGattServices().size());
418 } 473 }
419 474
420 TEST_F(BluetoothGattBlueZTest, GattCharacteristicAddedAndRemoved) { 475 TEST_F(BluetoothGattBlueZTest, GattCharacteristicAddedAndRemoved) {
421 fake_bluetooth_device_client_->CreateDevice( 476 fake_bluetooth_device_client_->CreateDevice(
422 dbus::ObjectPath(bluez::FakeBluetoothAdapterClient::kAdapterPath), 477 dbus::ObjectPath(bluez::FakeBluetoothAdapterClient::kAdapterPath),
423 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kLowEnergyPath)); 478 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kLowEnergyPath));
424 BluetoothDevice* device = 479 BluetoothDevice* device =
425 adapter_->GetDevice(bluez::FakeBluetoothDeviceClient::kLowEnergyAddress); 480 adapter_->GetDevice(bluez::FakeBluetoothDeviceClient::kLowEnergyAddress);
426 ASSERT_TRUE(device); 481 ASSERT_TRUE(device);
427 482
428 TestBluetoothAdapterObserver observer(adapter_); 483 TestBluetoothAdapterObserver observer(adapter_);
429 484
430 // Expose the fake Heart Rate service. This will asynchronously expose 485 // Expose the fake Heart Rate service. This will asynchronously expose
431 // characteristics. 486 // characteristics.
432 fake_bluetooth_gatt_service_client_->ExposeHeartRateService( 487 fake_bluetooth_gatt_service_client_->ExposeHeartRateService(
433 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kLowEnergyPath)); 488 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kLowEnergyPath));
434 ASSERT_EQ(1, observer.gatt_service_added_count()); 489 ASSERT_EQ(1, observer.gatt_service_added_count());
435 490
436 BluetoothRemoteGattService* service = 491 BluetoothRemoteGattService* service =
437 device->GetGattService(observer.last_gatt_service_id()); 492 device->GetGattService(observer.last_gatt_service_id());
438 493
439 EXPECT_EQ(0, observer.gatt_service_changed_count()); 494 EXPECT_EQ(0, observer.gatt_service_changed_count());
scheib 2016/05/17 19:24:47 Does this line need to be removed? Did behavior ch
Miao 2016/05/18 03:26:58 The Behavior doesn't not change. It is unclear to
440 EXPECT_EQ(0, observer.gatt_discovery_complete_count());
441 EXPECT_EQ(0, observer.gatt_characteristic_added_count()); 495 EXPECT_EQ(0, observer.gatt_characteristic_added_count());
442 EXPECT_EQ(0, observer.gatt_characteristic_removed_count()); 496 EXPECT_EQ(0, observer.gatt_characteristic_removed_count());
443 EXPECT_EQ(0, observer.gatt_characteristic_value_changed_count()); 497 EXPECT_EQ(0, observer.gatt_characteristic_value_changed_count());
444 EXPECT_TRUE(service->GetCharacteristics().empty()); 498 EXPECT_TRUE(service->GetCharacteristics().empty());
445 499
446 // Run the message loop so that the characteristics appear. 500 // Run the message loop so that the characteristics appear.
447 base::MessageLoop::current()->Run(); 501 base::MessageLoop::current()->Run();
448 502
449 // 3 characteristics should appear. Only 1 of the characteristics sends 503 // 3 characteristics should appear. Only 1 of the characteristics sends
450 // value changed signals. Service changed should be fired once for 504 // value changed signals. Service changed should be fired once for
(...skipping 838 matching lines...) Expand 10 before | Expand all | Expand 10 after
1289 1343
1290 EXPECT_EQ(1, success_callback_count_); 1344 EXPECT_EQ(1, success_callback_count_);
1291 EXPECT_EQ(0, error_callback_count_); 1345 EXPECT_EQ(0, error_callback_count_);
1292 EXPECT_EQ(1, observer.gatt_characteristic_value_changed_count()); 1346 EXPECT_EQ(1, observer.gatt_characteristic_value_changed_count());
1293 EXPECT_TRUE(characteristic->IsNotifying()); 1347 EXPECT_TRUE(characteristic->IsNotifying());
1294 EXPECT_EQ(1U, update_sessions_.size()); 1348 EXPECT_EQ(1U, update_sessions_.size());
1295 EXPECT_TRUE(update_sessions_[0]->IsActive()); 1349 EXPECT_TRUE(update_sessions_[0]->IsActive());
1296 } 1350 }
1297 1351
1298 } // namespace bluez 1352 } // namespace bluez
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698