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

Unified Diff: device/bluetooth/bluetooth_remote_gatt_characteristic_unittest.cc

Issue 2638653002: Bluetooth: macOS: DidModifyServices can happens while scanning (Closed)
Patch Set: More unit tests Created 3 years, 8 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_remote_gatt_characteristic_unittest.cc
diff --git a/device/bluetooth/bluetooth_remote_gatt_characteristic_unittest.cc b/device/bluetooth/bluetooth_remote_gatt_characteristic_unittest.cc
index 0e779e75f4c5618f1590778f9dd139f9b10b323d..3f5f181a032751c55cab895a99f77f5045d8d0a2 100644
--- a/device/bluetooth/bluetooth_remote_gatt_characteristic_unittest.cc
+++ b/device/bluetooth/bluetooth_remote_gatt_characteristic_unittest.cc
@@ -2301,7 +2301,7 @@ TEST_F(BluetoothRemoteGattCharacteristicTest, ReadDuringDisconnect) {
EXPECT_EQ(BluetoothRemoteGattService::GATT_ERROR_FAILED,
last_gatt_error_code_);
}
-#endif
+#endif // defined(OS_ANDROID)
#if defined(OS_ANDROID)
// Tests that write requests after a device disconnects but before the
@@ -2326,7 +2326,7 @@ TEST_F(BluetoothRemoteGattCharacteristicTest, WriteDuringDisconnect) {
EXPECT_EQ(BluetoothRemoteGattService::GATT_ERROR_FAILED,
last_gatt_error_code_);
}
-#endif
+#endif // defined(OS_ANDROID)
#if defined(OS_ANDROID)
// Tests that start notifications requests after a device disconnects but before
@@ -2355,7 +2355,7 @@ TEST_F(BluetoothRemoteGattCharacteristicTest,
EXPECT_EQ(BluetoothRemoteGattService::GATT_ERROR_FAILED,
last_gatt_error_code_);
}
-#endif
+#endif // defined(OS_ANDROID)
#if defined(OS_ANDROID)
// Tests that stop notifications requests after a device disconnects but before
@@ -2376,7 +2376,7 @@ TEST_F(BluetoothRemoteGattCharacteristicTest,
notify_sessions_[0]->Stop(GetStopNotifyCallback(Call::EXPECTED));
base::RunLoop().RunUntilIdle();
}
-#endif
+#endif // defined(OS_ANDROID)
#if defined(OS_ANDROID)
// Tests that deleting notify sessions after a device disconnects but before the
@@ -2397,5 +2397,119 @@ TEST_F(BluetoothRemoteGattCharacteristicTest,
notify_sessions_.clear();
base::RunLoop().RunUntilIdle();
}
-#endif
+#endif // defined(OS_ANDROID)
+
+#if defined(OS_MACOSX)
+// Tests to receive a services changed notification from macOS, while
+// discovering descriptors. This test simulate having 2 descriptor scan at the
+// same time. Only once both descriptor scanning is done, the gatt device is
+// ready.
+// Android: This test doesn't apply to Android because there is no services
+// changed event that could arrive during a discovery procedure.
+TEST_F(BluetoothRemoteGattCharacteristicTest,
+ SimulateDeviceModificationWhileDiscoveringDescriptors) {
+ if (!PlatformSupportsLowEnergy()) {
+ LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
+ return;
+ }
+ InitWithFakeAdapter();
+ StartLowEnergyDiscoverySession();
+ BluetoothDevice* device = SimulateLowEnergyDevice(3);
+ device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
+ GetConnectErrorCallback(Call::NOT_EXPECTED));
+
+ TestBluetoothAdapterObserver observer(adapter_);
+
+ // Starts first discovery process.
+ SimulateGattConnection(device);
+ EXPECT_EQ(1, observer.device_changed_count());
+ AddServicesToDevice(device, {kTestUUIDHeartRate});
+ SimulateDidDiscoverServices(device);
+ EXPECT_EQ(1u, device->GetGattServices().size());
+ BluetoothRemoteGattService* service = device->GetGattServices()[0];
+ std::string characteristic_uuid = "11111111-0000-1000-8000-00805f9b34fb";
+ AddCharacteristicToService(service, characteristic_uuid, /* properties */ 0);
+ SimulateDidDiscoverCharacteristics(service);
+ EXPECT_EQ(1u, service->GetCharacteristics().size());
+ BluetoothRemoteGattCharacteristic* characteristic =
+ service->GetCharacteristics()[0];
+ std::string descriptor_uuid = "22222222-0000-1000-8000-00805f9b34fb";
+ AddDescriptorToCharacteristic(characteristic, descriptor_uuid);
+ // Now waiting for descriptor discovery.
+
+ // Starts second discovery process.
+ SimulateGattServicesChanged(device);
+ EXPECT_EQ(2, observer.device_changed_count());
+ SimulateDidDiscoverServices(device);
+ SimulateDidDiscoverCharacteristics(service);
+ // Now waiting for a second descriptor discovery.
+
+ // Finish discovery process.
+ // First system call to -[id<CBPeripheralDelegate>
+ // peripheral:didDiscoverDescriptorsForCharacteristic:error:]
+ SimulateDidDiscoverDescriptors(characteristic);
+ EXPECT_EQ(0, observer.gatt_service_changed_count());
+ EXPECT_EQ(1u, service->GetCharacteristics().size());
+ EXPECT_EQ(1u, characteristic->GetDescriptors().size());
+ EXPECT_EQ(2, observer.device_changed_count());
+
+ // Second system call to -[id<CBPeripheralDelegate>
+ // peripheral:didDiscoverDescriptorsForCharacteristic:error:]
+ // Finish second discovery process.
+ observer.Reset();
+ SimulateDidDiscoverDescriptors(characteristic);
+ EXPECT_EQ(1, observer.gatt_service_changed_count());
+ EXPECT_EQ(1, observer.device_changed_count());
+}
+#endif // defined(OS_MACOSX)
+
+#if defined(OS_MACOSX)
+// Simulates to receive an extra discovery descriptor notifications from macOS.
+// Those notifications should be ignored.
+// Android: This test doesn't apply to Android because there is no services
+// changed event that could arrive during a discovery procedure.
+TEST_F(BluetoothRemoteGattCharacteristicTest, ExtraDidDiscoverDescriptorsCall) {
+ if (!PlatformSupportsLowEnergy()) {
+ LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
+ return;
+ }
+ InitWithFakeAdapter();
+ StartLowEnergyDiscoverySession();
+ BluetoothDevice* device = SimulateLowEnergyDevice(3);
+ device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
+ GetConnectErrorCallback(Call::NOT_EXPECTED));
+
+ TestBluetoothAdapterObserver observer(adapter_);
+
+ // Starts first discovery process.
+ SimulateGattConnection(device);
+ AddServicesToDevice(device, {kTestUUIDHeartRate});
+ SimulateDidDiscoverServices(device);
+ EXPECT_EQ(1u, device->GetGattServices().size());
+ BluetoothRemoteGattService* service = device->GetGattServices()[0];
+ std::string characteristic_uuid = "11111111-0000-1000-8000-00805f9b34fb";
+ AddCharacteristicToService(service, characteristic_uuid, /* properties */ 0);
+ SimulateDidDiscoverCharacteristics(service);
+ EXPECT_EQ(1u, service->GetCharacteristics().size());
+ BluetoothRemoteGattCharacteristic* characteristic =
+ service->GetCharacteristics()[0];
+ std::string descriptor_uuid = "22222222-0000-1000-8000-00805f9b34fb";
+ AddDescriptorToCharacteristic(characteristic, descriptor_uuid);
+ SimulateDidDiscoverDescriptors(characteristic);
+ EXPECT_EQ(1, observer.gatt_service_changed_count());
+ EXPECT_EQ(1u, service->GetCharacteristics().size());
+ EXPECT_EQ(1u, characteristic->GetDescriptors().size());
+
+ observer.Reset();
+ SimulateDidDiscoverDescriptors(characteristic); // Extra system call.
+ SimulateGattServicesChanged(device);
+ SimulateDidDiscoverDescriptors(characteristic); // Extra system call.
+ SimulateDidDiscoverServices(device);
+ SimulateDidDiscoverDescriptors(characteristic); // Extra system call.
+ SimulateDidDiscoverCharacteristics(service);
+ SimulateDidDiscoverDescriptors(characteristic);
+ SimulateDidDiscoverDescriptors(characteristic); // Extra system call.
+ EXPECT_EQ(2, observer.device_changed_count());
+}
+#endif // defined(OS_MACOSX)
} // namespace device

Powered by Google App Engine
This is Rietveld 408576698