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

Unified Diff: device/bluetooth/bluetooth_device_unittest.cc

Issue 2641133003: Bluetooth: macOS: Adding counter for service discovery callbacks. (Closed)
Patch Set: Splitting test Created 3 years, 10 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 side-by-side diff with in-line comments
Download patch
Index: device/bluetooth/bluetooth_device_unittest.cc
diff --git a/device/bluetooth/bluetooth_device_unittest.cc b/device/bluetooth/bluetooth_device_unittest.cc
index 19826117c94d04328119fa93535cd1fdb4cc2c1b..d603530457459a50b002be76ade9190f207097d9 100644
--- a/device/bluetooth/bluetooth_device_unittest.cc
+++ b/device/bluetooth/bluetooth_device_unittest.cc
@@ -395,6 +395,99 @@ TEST_F(BluetoothTest, GetUUIDs_Connection) {
}
#endif // defined(OS_ANDROID) || defined(OS_MACOSX)
+#if defined(OS_MACOSX)
+// Tests that receiving 2 notifications in a row from macOS that services has
+// changed is handled correctly. Each notification should generate a notfication
+// that the gatt device has changed, and each notification should ask to macOS
+// to scan for services. Only after the second service scan is received, the
+// device changed notification should be sent and the characteristic discovery
+// procedure should be started.
+// Android: This test doesn't apply to Android because there is no services
+// changed event that could arrive during a discovery procedure.
+TEST_F(BluetoothTest, TwoPendingServiceDiscoveryRequests) {
+ if (!PlatformSupportsLowEnergy()) {
+ LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
+ return;
+ }
+
+ InitWithFakeAdapter();
+ TestBluetoothAdapterObserver observer(adapter_);
+
+ BluetoothDevice* device = SimulateLowEnergyDevice(1);
+ device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
+ GetConnectErrorCallback(Call::NOT_EXPECTED));
+ SimulateGattConnection(device);
+ EXPECT_EQ(1, observer.device_changed_count());
+ EXPECT_FALSE(device->IsGattServicesDiscoveryComplete());
+
+ observer.Reset();
+ SimulateGattServicesChanged(device);
+ EXPECT_EQ(1, observer.device_changed_count());
+ EXPECT_FALSE(device->IsGattServicesDiscoveryComplete());
+
+ // Fist system call to
+ // -[id<CBPeripheralDelegate> peripheral:didDiscoverServices:]
+ observer.Reset();
+ std::vector<std::string> services1;
+ services1.push_back(kTestUUIDHeartRate);
+ SimulateGattServicesDiscovered(device, services1);
ortuno 2017/02/28 23:28:11 optional: You could do: SimulateGattServicesDisco
jlebel 2017/02/28 23:52:16 Done.
+ EXPECT_EQ(0, observer.device_changed_count());
+ EXPECT_FALSE(device->IsGattServicesDiscoveryComplete());
+ EXPECT_EQ(gatt_characteristic_discovery_attempts_, 0);
+
+ // Second system call to
+ // -[id<CBPeripheralDelegate> peripheral:didDiscoverServices:]
+ std::vector<std::string> services2;
+ services2.push_back(kTestUUIDImmediateAlert);
+ SimulateGattServicesDiscovered(device, services2);
+ EXPECT_EQ(1, observer.device_changed_count());
+ EXPECT_TRUE(device->IsGattServicesDiscoveryComplete());
+ EXPECT_EQ(gatt_characteristic_discovery_attempts_, 2);
ortuno 2017/02/28 02:06:50 Shouldn't there be only one characteristic discove
jlebel 2017/02/28 23:16:18 We discover characteristics for kTestUUIDHeartRate
ortuno 2017/02/28 23:28:11 Ah that makes sense. Thanks.
+
+ EXPECT_EQ(2u, device->GetGattServices().size());
+}
+
+// Simulate an unexpected call to -[id<CBPeripheralDelegate>
+// peripheral:didDiscoverServices:]. This should not happen, but if it does
+// (buggy device?), a discovery cycle should be done.
+TEST_F(BluetoothTest, ExtraDidDiscoverServicesCall) {
+ if (!PlatformSupportsLowEnergy()) {
+ LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
+ return;
+ }
+
+ InitWithFakeAdapter();
+ TestBluetoothAdapterObserver observer(adapter_);
+
+ BluetoothDevice* device = SimulateLowEnergyDevice(1);
+ device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
+ GetConnectErrorCallback(Call::NOT_EXPECTED));
+ SimulateGattConnection(device);
+ EXPECT_EQ(1, observer.device_changed_count());
+ EXPECT_FALSE(device->IsGattServicesDiscoveryComplete());
+
+ // Legitimate system call to
+ // -[id<CBPeripheralDelegate> peripheral:didDiscoverServices:]
+ observer.Reset();
+ SimulateGattServicesDiscovered(device, {});
+ EXPECT_EQ(1, observer.device_changed_count());
+ EXPECT_TRUE(device->IsGattServicesDiscoveryComplete());
+ EXPECT_EQ(gatt_characteristic_discovery_attempts_, 0);
ortuno 2017/02/28 02:06:50 I'm surprised the legitimate system call doesn't r
jlebel 2017/02/28 23:16:18 We have no service, therefore, -[MockCBPeripehral
ortuno 2017/02/28 23:28:11 Ah right. optional: Add a service to make it clear
jlebel 2017/02/28 23:52:16 If I do that, this DCHECK fails: https://cs.chromi
jlebel 2017/03/04 16:59:02 Done.
+ EXPECT_EQ(0u, device->GetGattServices().size());
+
+ // Unexpected system call to
+ // -[id<CBPeripheralDelegate> peripheral:didDiscoverServices:]
+ std::vector<std::string> services3;
+ services3.push_back(kTestUUIDLinkLoss);
+ SimulateGattServicesDiscovered(device, services3);
+ EXPECT_EQ(2, observer.device_changed_count());
+ EXPECT_TRUE(device->IsGattServicesDiscoveryComplete());
+ EXPECT_EQ(gatt_characteristic_discovery_attempts_, 1);
+
+ EXPECT_EQ(1u, device->GetGattServices().size());
+}
+#endif // defined(OS_MACOSX)
+
#if defined(OS_ANDROID) || defined(OS_MACOSX)
// Tests Advertisement Data is updated correctly when we start discovery
// during a connection.

Powered by Google App Engine
This is Rietveld 408576698