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

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

Issue 2613473002: bluetooth: bluez: Implement BluetoothRemoteGattCharacteristicBluez::SubscribeToNotifications and Un… (Closed)
Patch Set: Also include Chrome OS as it uses Bluez. Created 3 years, 11 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 1398 matching lines...) Expand 10 before | Expand all | Expand 10 after
1409 base::Unretained(this))); 1409 base::Unretained(this)));
1410 characteristic->StartNotifySession( 1410 characteristic->StartNotifySession(
1411 base::Bind(&BluetoothGattBlueZTest::NotifySessionCallback, 1411 base::Bind(&BluetoothGattBlueZTest::NotifySessionCallback,
1412 base::Unretained(this)), 1412 base::Unretained(this)),
1413 base::Bind(&BluetoothGattBlueZTest::ServiceErrorCallback, 1413 base::Bind(&BluetoothGattBlueZTest::ServiceErrorCallback,
1414 base::Unretained(this))); 1414 base::Unretained(this)));
1415 EXPECT_EQ(0, success_callback_count_); 1415 EXPECT_EQ(0, success_callback_count_);
1416 EXPECT_EQ(0, error_callback_count_); 1416 EXPECT_EQ(0, error_callback_count_);
1417 EXPECT_EQ(1, observer.gatt_characteristic_value_changed_count()); 1417 EXPECT_EQ(1, observer.gatt_characteristic_value_changed_count());
1418 EXPECT_TRUE(update_sessions_.empty()); 1418 EXPECT_TRUE(update_sessions_.empty());
1419 EXPECT_TRUE(characteristic->IsNotifying()); 1419 EXPECT_FALSE(characteristic->IsNotifying());
1420 1420
1421 // Run the main loop. The initial call should complete. The queued call should 1421 // Run the main loop. The initial call should complete. The queued call should
1422 // succeed immediately. 1422 // succeed immediately.
1423 base::RunLoop().Run(); 1423 base::RunLoop().Run();
1424 1424
1425 EXPECT_TRUE(characteristic->IsNotifying());
1425 EXPECT_EQ(3, success_callback_count_); 1426 EXPECT_EQ(3, success_callback_count_);
1426 EXPECT_EQ(0, error_callback_count_); 1427 EXPECT_EQ(0, error_callback_count_);
1427 EXPECT_EQ(1, observer.gatt_characteristic_value_changed_count()); 1428 EXPECT_EQ(1, observer.gatt_characteristic_value_changed_count());
1428 EXPECT_EQ(3U, update_sessions_.size()); 1429 EXPECT_EQ(3U, update_sessions_.size());
1429 1430
1430 // Notifications should be getting sent regularly now. 1431 // Notifications should be getting sent regularly now.
1431 base::RunLoop().Run(); 1432 base::RunLoop().Run();
1432 EXPECT_GT(observer.gatt_characteristic_value_changed_count(), 1); 1433 EXPECT_GT(observer.gatt_characteristic_value_changed_count(), 1);
1433 1434
1434 // Stop one of the sessions. The session should become inactive but the 1435 // Stop one of the sessions. The session should become inactive but the
1435 // characteristic should still be notifying. 1436 // characteristic should still be notifying.
1436 BluetoothGattNotifySession* session = update_sessions_[0]; 1437 BluetoothGattNotifySession* session = update_sessions_[0];
1437 EXPECT_TRUE(session->IsActive()); 1438 EXPECT_TRUE(session->IsActive());
1438 session->Stop(base::Bind(&BluetoothGattBlueZTest::SuccessCallback, 1439 session->Stop(base::Bind(&BluetoothGattBlueZTest::SuccessCallback,
1439 base::Unretained(this))); 1440 base::Unretained(this)));
1441
1442 // Run message loop to stop the notify session.
1443 base::RunLoop().Run();
1444
1440 EXPECT_EQ(4, success_callback_count_); 1445 EXPECT_EQ(4, success_callback_count_);
1441 EXPECT_EQ(0, error_callback_count_); 1446 EXPECT_EQ(0, error_callback_count_);
1442 EXPECT_FALSE(session->IsActive()); 1447 EXPECT_FALSE(session->IsActive());
1443 EXPECT_EQ(characteristic->GetIdentifier(), 1448 EXPECT_EQ(characteristic->GetIdentifier(),
1444 session->GetCharacteristicIdentifier()); 1449 session->GetCharacteristicIdentifier());
1445 EXPECT_TRUE(characteristic->IsNotifying()); 1450 EXPECT_TRUE(characteristic->IsNotifying());
1446 1451
1447 // Delete another session. Characteristic should still be notifying. 1452 // Delete another session. Characteristic should still be notifying.
1448 update_sessions_.pop_back(); 1453 update_sessions_.pop_back();
1449 EXPECT_EQ(2U, update_sessions_.size()); 1454 EXPECT_EQ(2U, update_sessions_.size());
1450 EXPECT_TRUE(characteristic->IsNotifying()); 1455 EXPECT_TRUE(characteristic->IsNotifying());
1451 EXPECT_FALSE(update_sessions_[0]->IsActive()); 1456 EXPECT_FALSE(update_sessions_[0]->IsActive());
1452 EXPECT_TRUE(update_sessions_[1]->IsActive()); 1457 EXPECT_TRUE(update_sessions_[1]->IsActive());
1453 1458
1454 // Clear the last session. 1459 // Clear the last session.
1455 update_sessions_.clear(); 1460 update_sessions_.clear();
1456 EXPECT_TRUE(update_sessions_.empty()); 1461 EXPECT_TRUE(update_sessions_.empty());
1462
1463 // Run message loop in order to do proper cleanup of sessions.
1464 base::RunLoop().RunUntilIdle();
1465
1457 EXPECT_FALSE(characteristic->IsNotifying()); 1466 EXPECT_FALSE(characteristic->IsNotifying());
1458 1467
1459 success_callback_count_ = 0; 1468 success_callback_count_ = 0;
1460 observer.Reset(); 1469 observer.Reset();
1461 1470
1462 // Enable notifications again. 1471 // Enable notifications again.
1463 characteristic->StartNotifySession( 1472 characteristic->StartNotifySession(
1464 base::Bind(&BluetoothGattBlueZTest::NotifySessionCallback, 1473 base::Bind(&BluetoothGattBlueZTest::NotifySessionCallback,
1465 base::Unretained(this)), 1474 base::Unretained(this)),
1466 base::Bind(&BluetoothGattBlueZTest::ServiceErrorCallback, 1475 base::Bind(&BluetoothGattBlueZTest::ServiceErrorCallback,
1467 base::Unretained(this))); 1476 base::Unretained(this)));
1468 EXPECT_EQ(0, success_callback_count_); 1477 EXPECT_EQ(0, success_callback_count_);
1469 EXPECT_EQ(0, error_callback_count_); 1478 EXPECT_EQ(0, error_callback_count_);
1470 EXPECT_EQ(1, observer.gatt_characteristic_value_changed_count()); 1479 EXPECT_EQ(1, observer.gatt_characteristic_value_changed_count());
1471 EXPECT_TRUE(update_sessions_.empty()); 1480 EXPECT_TRUE(update_sessions_.empty());
1472 EXPECT_TRUE(characteristic->IsNotifying()); 1481 EXPECT_FALSE(characteristic->IsNotifying());
1473 1482
1474 // Run the message loop. Notifications should begin. 1483 // Run the message loop. Notifications should begin.
1475 base::RunLoop().Run(); 1484 base::RunLoop().Run();
1476 1485
1477 EXPECT_EQ(1, success_callback_count_); 1486 EXPECT_EQ(1, success_callback_count_);
1478 EXPECT_EQ(0, error_callback_count_); 1487 EXPECT_EQ(0, error_callback_count_);
1479 EXPECT_EQ(1, observer.gatt_characteristic_value_changed_count()); 1488 EXPECT_EQ(1, observer.gatt_characteristic_value_changed_count());
1480 EXPECT_EQ(1U, update_sessions_.size()); 1489 EXPECT_EQ(1U, update_sessions_.size());
1481 EXPECT_TRUE(update_sessions_[0]->IsActive()); 1490 EXPECT_TRUE(update_sessions_[0]->IsActive());
1482 EXPECT_TRUE(characteristic->IsNotifying()); 1491 EXPECT_TRUE(characteristic->IsNotifying());
1483 1492
1484 // Check that notifications are happening. 1493 // Check that notifications are happening.
1485 base::RunLoop().Run(); 1494 base::RunLoop().Run();
1486 EXPECT_GT(observer.gatt_characteristic_value_changed_count(), 1); 1495 EXPECT_GT(observer.gatt_characteristic_value_changed_count(), 1);
1487 1496
1488 // Request another session. This should return immediately. 1497 // Request another session. This should return immediately.
1489 characteristic->StartNotifySession( 1498 characteristic->StartNotifySession(
1490 base::Bind(&BluetoothGattBlueZTest::NotifySessionCallback, 1499 base::Bind(&BluetoothGattBlueZTest::NotifySessionCallback,
1491 base::Unretained(this)), 1500 base::Unretained(this)),
1492 base::Bind(&BluetoothGattBlueZTest::ServiceErrorCallback, 1501 base::Bind(&BluetoothGattBlueZTest::ServiceErrorCallback,
1493 base::Unretained(this))); 1502 base::Unretained(this)));
1503
1504 // Run message loop to stop the notify session.
1505 base::RunLoop().Run();
1506
1494 EXPECT_EQ(2, success_callback_count_); 1507 EXPECT_EQ(2, success_callback_count_);
1495 EXPECT_EQ(0, error_callback_count_); 1508 EXPECT_EQ(0, error_callback_count_);
1496 EXPECT_EQ(2U, update_sessions_.size()); 1509 EXPECT_EQ(2U, update_sessions_.size());
1497 EXPECT_TRUE(update_sessions_[0]->IsActive()); 1510 EXPECT_TRUE(update_sessions_[0]->IsActive());
1498 EXPECT_TRUE(update_sessions_[1]->IsActive()); 1511 EXPECT_TRUE(update_sessions_[1]->IsActive());
1499 EXPECT_TRUE(characteristic->IsNotifying()); 1512 EXPECT_TRUE(characteristic->IsNotifying());
1500 1513
1501 // Hide the characteristic. The sessions should become inactive. 1514 // Hide the characteristic. The sessions should become inactive.
1502 fake_bluetooth_gatt_characteristic_client_->HideHeartRateCharacteristics(); 1515 fake_bluetooth_gatt_characteristic_client_->HideHeartRateCharacteristics();
1503 EXPECT_EQ(2U, update_sessions_.size()); 1516 EXPECT_EQ(2U, update_sessions_.size());
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
1557 base::Bind(&BluetoothGattBlueZTest::NotifySessionCallback, 1570 base::Bind(&BluetoothGattBlueZTest::NotifySessionCallback,
1558 base::Unretained(this)), 1571 base::Unretained(this)),
1559 base::Bind(&BluetoothGattBlueZTest::ServiceErrorCallback, 1572 base::Bind(&BluetoothGattBlueZTest::ServiceErrorCallback,
1560 base::Unretained(this))); 1573 base::Unretained(this)));
1561 1574
1562 // The operation still hasn't completed but we should have received the first 1575 // The operation still hasn't completed but we should have received the first
1563 // notification. 1576 // notification.
1564 EXPECT_EQ(0, success_callback_count_); 1577 EXPECT_EQ(0, success_callback_count_);
1565 EXPECT_EQ(0, error_callback_count_); 1578 EXPECT_EQ(0, error_callback_count_);
1566 EXPECT_EQ(1, observer.gatt_characteristic_value_changed_count()); 1579 EXPECT_EQ(1, observer.gatt_characteristic_value_changed_count());
1567 EXPECT_TRUE(characteristic->IsNotifying()); 1580 EXPECT_FALSE(characteristic->IsNotifying());
1568 EXPECT_TRUE(update_sessions_.empty()); 1581 EXPECT_TRUE(update_sessions_.empty());
1569 1582
1570 // Run the main loop. The initial call should complete. The queued calls 1583 // Run the main loop. The initial call should complete. The queued calls
1571 // should succeed immediately. 1584 // should succeed immediately.
1572 base::RunLoop().Run(); 1585 base::RunLoop().Run();
1573 1586
1574 EXPECT_EQ(4, success_callback_count_); 1587 EXPECT_EQ(4, success_callback_count_);
1575 EXPECT_EQ(0, error_callback_count_); 1588 EXPECT_EQ(0, error_callback_count_);
1576 EXPECT_EQ(1, observer.gatt_characteristic_value_changed_count()); 1589 EXPECT_EQ(1, observer.gatt_characteristic_value_changed_count());
1577 EXPECT_TRUE(characteristic->IsNotifying()); 1590 EXPECT_TRUE(characteristic->IsNotifying());
1578 EXPECT_EQ(4U, update_sessions_.size()); 1591 EXPECT_EQ(4U, update_sessions_.size());
1579 1592
1580 for (int i = 0; i < 4; i++) 1593 for (int i = 0; i < 4; i++)
1581 EXPECT_TRUE(update_sessions_[0]->IsActive()); 1594 EXPECT_TRUE(update_sessions_[0]->IsActive());
1582 1595
1583 // Stop notifications directly through the client. The sessions should get 1596 // Stop notifications directly through the client. The sessions should get
1584 // marked as inactive. 1597 // marked as inactive.
1585 fake_bluetooth_gatt_characteristic_client_->StopNotify( 1598 fake_bluetooth_gatt_characteristic_client_->StopNotify(
1586 fake_bluetooth_gatt_characteristic_client_->GetHeartRateMeasurementPath(), 1599 fake_bluetooth_gatt_characteristic_client_->GetHeartRateMeasurementPath(),
1587 base::Bind(&BluetoothGattBlueZTest::SuccessCallback, 1600 base::Bind(&BluetoothGattBlueZTest::SuccessCallback,
1588 base::Unretained(this)), 1601 base::Unretained(this)),
1589 base::Bind(&BluetoothGattBlueZTest::DBusErrorCallback, 1602 base::Bind(&BluetoothGattBlueZTest::DBusErrorCallback,
1590 base::Unretained(this))); 1603 base::Unretained(this)));
1591 EXPECT_EQ(5, success_callback_count_); 1604 EXPECT_EQ(5, success_callback_count_);
1592 EXPECT_EQ(0, error_callback_count_); 1605 EXPECT_EQ(0, error_callback_count_);
1606
1607 // Run message loop to stop the notify session.
1608 base::RunLoop().RunUntilIdle();
1609
1593 EXPECT_FALSE(characteristic->IsNotifying()); 1610 EXPECT_FALSE(characteristic->IsNotifying());
1594 EXPECT_EQ(4U, update_sessions_.size()); 1611 EXPECT_EQ(4U, update_sessions_.size());
1595 1612
1596 for (int i = 0; i < 4; i++) 1613 for (int i = 0; i < 4; i++)
1597 EXPECT_FALSE(update_sessions_[0]->IsActive()); 1614 EXPECT_FALSE(update_sessions_[0]->IsActive());
1598 1615
1599 // It should be possible to restart notifications and the call should reset 1616 // It should be possible to restart notifications and the call should reset
1600 // the session count and make a request through the client. 1617 // the session count and make a request through the client.
1601 update_sessions_.clear(); 1618 update_sessions_.clear();
1602 success_callback_count_ = 0; 1619 success_callback_count_ = 0;
1603 observer.Reset(); 1620 observer.Reset();
1604 characteristic->StartNotifySession( 1621 characteristic->StartNotifySession(
1605 base::Bind(&BluetoothGattBlueZTest::NotifySessionCallback, 1622 base::Bind(&BluetoothGattBlueZTest::NotifySessionCallback,
1606 base::Unretained(this)), 1623 base::Unretained(this)),
1607 base::Bind(&BluetoothGattBlueZTest::ServiceErrorCallback, 1624 base::Bind(&BluetoothGattBlueZTest::ServiceErrorCallback,
1608 base::Unretained(this))); 1625 base::Unretained(this)));
1609 1626
1627 // Run message loop to start the notify session.
1628 base::RunLoop().RunUntilIdle();
1629
1610 EXPECT_EQ(0, success_callback_count_); 1630 EXPECT_EQ(0, success_callback_count_);
1611 EXPECT_EQ(0, error_callback_count_); 1631 EXPECT_EQ(0, error_callback_count_);
1612 EXPECT_EQ(1, observer.gatt_characteristic_value_changed_count()); 1632 EXPECT_EQ(1, observer.gatt_characteristic_value_changed_count());
1613 EXPECT_TRUE(characteristic->IsNotifying()); 1633 EXPECT_FALSE(characteristic->IsNotifying());
1614 EXPECT_TRUE(update_sessions_.empty()); 1634 EXPECT_TRUE(update_sessions_.empty());
1615 1635
1616 base::RunLoop().Run(); 1636 base::RunLoop().Run();
1617 1637
1618 EXPECT_EQ(1, success_callback_count_); 1638 EXPECT_EQ(1, success_callback_count_);
1619 EXPECT_EQ(0, error_callback_count_); 1639 EXPECT_EQ(0, error_callback_count_);
1620 EXPECT_EQ(1, observer.gatt_characteristic_value_changed_count()); 1640 EXPECT_EQ(1, observer.gatt_characteristic_value_changed_count());
1621 EXPECT_TRUE(characteristic->IsNotifying()); 1641 EXPECT_TRUE(characteristic->IsNotifying());
1622 EXPECT_EQ(1U, update_sessions_.size()); 1642 EXPECT_EQ(1U, update_sessions_.size());
1623 EXPECT_TRUE(update_sessions_[0]->IsActive()); 1643 EXPECT_TRUE(update_sessions_[0]->IsActive());
1624 } 1644 }
1625 1645
1626 } // namespace bluez 1646 } // namespace bluez
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698