Index: device/bluetooth/bluetooth_device_unittest.cc |
diff --git a/device/bluetooth/bluetooth_device_unittest.cc b/device/bluetooth/bluetooth_device_unittest.cc |
index 4cea1b6fa6e48e13990e2564085aae1aad3bb134..6150db739ab5da350a32fcd0741787fd3182a728 100644 |
--- a/device/bluetooth/bluetooth_device_unittest.cc |
+++ b/device/bluetooth/bluetooth_device_unittest.cc |
@@ -125,4 +125,175 @@ TEST_F(BluetoothTest, LowEnergyDeviceNoUUIDs) { |
// also require build configuration to generate string resources into a .pak |
// file. |
+#if defined(OS_ANDROID) |
+// Basic CreateGattConnection test. |
+TEST_F(BluetoothTest, CreateGattConnection) { |
+ InitWithFakeAdapter(); |
+ TestBluetoothAdapterObserver observer(adapter_); |
+ |
+ // Get a device. |
+ adapter_->StartDiscoverySession(GetDiscoverySessionCallback(), |
+ GetErrorCallback()); |
+ base::RunLoop().RunUntilIdle(); |
+ DiscoverLowEnergyDevice(3); |
+ base::RunLoop().RunUntilIdle(); |
+ BluetoothDevice* device = observer.last_device(); |
+ |
+ callback_count_ = error_callback_count_ = 0; |
+ device->CreateGattConnection(GetGattConnectionCallback(), |
+ GetConnectErrorCallback()); |
+ CompleteGattConnection(device); |
+ EXPECT_EQ(1, callback_count_); |
+ EXPECT_EQ(0, error_callback_count_); |
+ ASSERT_EQ(1u, gatt_connections_.size()); |
+ EXPECT_TRUE(device->IsGattConnected()); |
+ EXPECT_TRUE(gatt_connections_[0]->IsConnected()); |
+} |
+#endif // defined(OS_ANDROID) |
+ |
+#if defined(OS_ANDROID) |
+// Creates BluetoothGattConnection instances and tests that the interface |
+// functions even when some Disconnect and the BluetoothDevice is destroyed. |
+TEST_F(BluetoothTest, BluetoothGattConnection) { |
+ InitWithFakeAdapter(); |
+ TestBluetoothAdapterObserver observer(adapter_); |
+ |
+ // Get a device. |
+ adapter_->StartDiscoverySession(GetDiscoverySessionCallback(), |
+ GetErrorCallback()); |
+ base::RunLoop().RunUntilIdle(); |
+ DiscoverLowEnergyDevice(3); |
+ base::RunLoop().RunUntilIdle(); |
+ BluetoothDevice* device = observer.last_device(); |
+ std::string device_address = device->GetAddress(); |
+ |
+ // CreateGattConnection |
+ callback_count_ = error_callback_count_ = 0; |
+ device->CreateGattConnection(GetGattConnectionCallback(), |
+ GetConnectErrorCallback()); |
+ CompleteGattConnection(device); |
+ EXPECT_EQ(1, callback_count_); |
+ EXPECT_EQ(0, error_callback_count_); |
+ ASSERT_EQ(1u, gatt_connections_.size()); |
+ EXPECT_TRUE(device->IsGattConnected()); |
+ EXPECT_TRUE(gatt_connections_[0]->IsConnected()); |
+ |
+ // Connect again once already connected. |
+ device->CreateGattConnection(GetGattConnectionCallback(), |
+ GetConnectErrorCallback()); |
+ device->CreateGattConnection(GetGattConnectionCallback(), |
+ GetConnectErrorCallback()); |
+ EXPECT_EQ(3, callback_count_); |
+ EXPECT_EQ(0, error_callback_count_); |
+ ASSERT_EQ(3u, gatt_connections_.size()); |
+ |
+ // Test GetDeviceAddress |
+ EXPECT_EQ(device_address, gatt_connections_[0]->GetDeviceAddress()); |
+ |
+ // Test IsConnected |
+ EXPECT_TRUE(gatt_connections_[0]->IsConnected()); |
+ EXPECT_TRUE(gatt_connections_[1]->IsConnected()); |
+ EXPECT_TRUE(gatt_connections_[2]->IsConnected()); |
+ |
+ // Disconnect & Delete connection objects. Device stays connected. |
+ gatt_connections_[0]->Disconnect(); // Disconnect first. |
+ gatt_connections_.pop_back(); // Delete last. |
+ EXPECT_FALSE(gatt_connections_[0]->IsConnected()); |
+ EXPECT_TRUE(gatt_connections_[1]->IsConnected()); |
+ EXPECT_TRUE(device->IsGattConnected()); |
+ |
+ // Delete device, connection objects should all be disconnected. |
+ DeleteDevice(device); |
+ EXPECT_FALSE(gatt_connections_[0]->IsConnected()); |
+ EXPECT_FALSE(gatt_connections_[1]->IsConnected()); |
+ |
+ // Test GetDeviceAddress after device deleted. |
+ EXPECT_EQ(device_address, gatt_connections_[0]->GetDeviceAddress()); |
+ EXPECT_EQ(device_address, gatt_connections_[1]->GetDeviceAddress()); |
+} |
+#endif // defined(OS_ANDROID) |
+ |
+#if defined(OS_ANDROID) |
+// BluetoothGattConnection with several connect / disconnects. |
+TEST_F(BluetoothTest, BluetoothGattConnection_ConnectDisconnect) { |
+ InitWithFakeAdapter(); |
+ TestBluetoothAdapterObserver observer(adapter_); |
+ |
+ // Get a device. |
+ adapter_->StartDiscoverySession(GetDiscoverySessionCallback(), |
+ GetErrorCallback()); |
+ base::RunLoop().RunUntilIdle(); |
+ DiscoverLowEnergyDevice(3); |
+ base::RunLoop().RunUntilIdle(); |
+ BluetoothDevice* device = observer.last_device(); |
+ |
+ // CreateGattConnection, & multiple connection s from platform only invoke |
Jeffrey Yasskin
2015/09/16 23:42:49
sp: connection s
scheib
2015/09/20 02:53:35
Done.
|
+ // callbacks once: |
+ callback_count_ = error_callback_count_ = 0; |
+ device->CreateGattConnection(GetGattConnectionCallback(), |
+ GetConnectErrorCallback()); |
+ CompleteGattConnection(device); |
+ CompleteGattConnection(device); |
+ EXPECT_EQ(1, callback_count_); |
+ EXPECT_EQ(0, error_callback_count_); |
+ |
+ // Become disconnected: |
+ CompleteGattDisconnection(device); |
+ EXPECT_EQ(1, callback_count_); |
+ EXPECT_EQ(0, error_callback_count_); |
+ EXPECT_FALSE(gatt_connections_[0]->IsConnected()); |
+ |
+ // Be already connected, then CreateGattConnection: |
+ callback_count_ = error_callback_count_ = 0; |
+ CompleteGattConnection(device); |
+ device->CreateGattConnection(GetGattConnectionCallback(), |
+ GetConnectErrorCallback()); |
+ EXPECT_EQ(1, callback_count_); |
Jeffrey Yasskin
2015/09/16 23:42:49
Please add:
EXPECT_FALSE(gatt_connections_[0]->Is
scheib
2015/09/20 02:53:35
Done.
|
+ EXPECT_EQ(0, error_callback_count_); |
+ |
+ // Disconnect all CreateGattConnection objects. But, don't yet simulate |
Jeffrey Yasskin
2015/09/16 23:42:49
Please also add a test in which the last connectio
scheib
2015/09/20 02:53:35
Done.
|
+ // the device disconnecting. |
+ callback_count_ = error_callback_count_ = 0; |
+ for (auto connection : gatt_connections_) |
Jeffrey Yasskin
2015/09/16 23:42:49
Can you write BluetoothGattConnection* for these f
scheib
2015/09/20 02:53:35
Done.
|
+ connection->Disconnect(); |
Jeffrey Yasskin
2015/09/16 23:42:49
Can you check that the implementation appropriatel
scheib
2015/09/20 02:53:35
Done.
|
+ device->CreateGattConnection(GetGattConnectionCallback(), |
+ GetConnectErrorCallback()); |
+ EXPECT_EQ(1, callback_count_); // Device is assumed still connected. |
Jeffrey Yasskin
2015/09/16 23:42:49
Check that gatt_connections_.back()->IsConnected()
scheib
2015/09/20 02:53:35
Done.
|
+ EXPECT_EQ(0, error_callback_count_); |
+ callback_count_ = error_callback_count_ = 0; |
+ |
+ // Actually disconnect: |
+ CompleteGattDisconnection(device); |
+ EXPECT_EQ(0, callback_count_); |
+ EXPECT_EQ(0, error_callback_count_); |
+ for (auto connection : gatt_connections_) |
+ EXPECT_FALSE(connection->IsConnected()); |
Jeffrey Yasskin
2015/09/16 23:42:49
These were already false before the CompleteGattDi
scheib
2015/09/20 02:53:35
The last connection reports connected. (It connect
|
+ |
+ // CreateGattConnection, but receive notice that device disconnected before |
+ // it ever connects: |
+ callback_count_ = error_callback_count_ = 0; |
+ device->CreateGattConnection(GetGattConnectionCallback(), |
+ GetConnectErrorCallback()); |
+ CompleteGattDisconnection(device); |
Jeffrey Yasskin
2015/09/16 23:42:49
Does this happen naturally if you call device->Dis
scheib
2015/09/20 02:53:34
I was planning on not failing a connect until the
Jeffrey Yasskin
2015/09/22 01:22:10
Right, that's fine. Calls to CompleteGattDisconnec
scheib
2015/09/22 18:21:48
I've added 2 more test blocks:
device->CreateGattC
|
+ EXPECT_EQ(0, callback_count_); |
+ EXPECT_EQ(1, error_callback_count_); |
+ EXPECT_EQ(BluetoothDevice::ERROR_FAILED, last_connect_error_code_); |
+ for (auto connection : gatt_connections_) |
+ EXPECT_FALSE(connection->IsConnected()); |
+ |
+ // CreateGattConnection, but error connecting. Also, Multiple errors only |
Jeffrey Yasskin
2015/09/16 23:42:49
s/Multiple/multiple/
scheib
2015/09/20 02:53:35
Done.
|
+ // invoke callbacks once: |
+ callback_count_ = error_callback_count_ = 0; |
+ device->CreateGattConnection(GetGattConnectionCallback(), |
+ GetConnectErrorCallback()); |
+ FailGattConnection(device, BluetoothDevice::ERROR_AUTH_FAILED); |
+ FailGattConnection(device, BluetoothDevice::ERROR_FAILED); |
+ EXPECT_EQ(0, callback_count_); |
+ EXPECT_EQ(1, error_callback_count_); |
+ EXPECT_EQ(BluetoothDevice::ERROR_AUTH_FAILED, last_connect_error_code_); |
+ for (auto connection : gatt_connections_) |
+ EXPECT_FALSE(connection->IsConnected()); |
+} |
+#endif // defined(OS_ANDROID) |
+ |
} // namespace device |