Commit 8b6bdd4b authored by ortuno's avatar ortuno Committed by Commit bot

bluetooth: Fix crash when trying to read or write when operation pending

A return statement was missing so the callback would get called twice.

To avoid future bugs in which callbacks are called unexpectedly, we
add a new parameter to the getters of the mock callbacks. If
Call::EXPECTED is passed then the callback is expected to be called,
otherwise we the callback was not expected to be called and the test
will fail.

We add a TearDown implementation to BluetoothTestBase to check
no unexpected calls have been made to callbacks.

BUG=557571

Review URL: https://codereview.chromium.org/1465863003

Cr-Commit-Position: refs/heads/master@{#361040}
parent ccdd6cc5
......@@ -139,7 +139,6 @@ class TestPairingDelegate : public BluetoothDevice::PairingDelegate {
void AuthorizePairing(BluetoothDevice* device) override {}
};
TEST(BluetoothAdapterTest, NoDefaultPairingDelegate) {
scoped_refptr<BluetoothAdapter> adapter = new TestBluetoothAdapter();
......@@ -465,9 +464,8 @@ TEST_F(BluetoothTest, DiscoverySession) {
EXPECT_TRUE(discovery_sessions_[0]->IsActive());
ResetEventCounts();
discovery_sessions_[0]->Stop(GetCallback(), GetErrorCallback());
EXPECT_EQ(1, callback_count_);
EXPECT_EQ(0, error_callback_count_);
discovery_sessions_[0]->Stop(GetCallback(Call::EXPECTED),
GetErrorCallback(Call::NOT_EXPECTED));
EXPECT_FALSE(adapter_->IsDiscovering());
EXPECT_FALSE(discovery_sessions_[0]->IsActive());
}
......@@ -488,7 +486,7 @@ TEST_F(BluetoothTest, NoPermissions) {
return;
}
StartLowEnergyDiscoverySession();
StartLowEnergyDiscoverySessionExpectedToFail();
EXPECT_EQ(0, callback_count_);
EXPECT_EQ(1, error_callback_count_);
......
......@@ -122,11 +122,9 @@ TEST_F(BluetoothTest, CreateGattConnection) {
BluetoothDevice* device = DiscoverLowEnergyDevice(3);
ResetEventCounts();
device->CreateGattConnection(GetGattConnectionCallback(),
GetConnectErrorCallback());
device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
GetConnectErrorCallback(Call::NOT_EXPECTED));
SimulateGattConnection(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());
......@@ -144,8 +142,8 @@ TEST_F(BluetoothTest, BluetoothGattConnection) {
// CreateGattConnection
ResetEventCounts();
device->CreateGattConnection(GetGattConnectionCallback(),
GetConnectErrorCallback());
device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
GetConnectErrorCallback(Call::NOT_EXPECTED));
EXPECT_EQ(1, gatt_connection_attempts_);
SimulateGattConnection(device);
EXPECT_EQ(1, callback_count_);
......@@ -156,10 +154,10 @@ TEST_F(BluetoothTest, BluetoothGattConnection) {
// Connect again once already connected.
ResetEventCounts();
device->CreateGattConnection(GetGattConnectionCallback(),
GetConnectErrorCallback());
device->CreateGattConnection(GetGattConnectionCallback(),
GetConnectErrorCallback());
device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
GetConnectErrorCallback(Call::NOT_EXPECTED));
device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
GetConnectErrorCallback(Call::NOT_EXPECTED));
EXPECT_EQ(0, gatt_connection_attempts_);
EXPECT_EQ(2, callback_count_);
EXPECT_EQ(0, error_callback_count_);
......@@ -205,8 +203,8 @@ TEST_F(BluetoothTest,
// CreateGattConnection, & multiple connections from platform only invoke
// callbacks once:
ResetEventCounts();
device->CreateGattConnection(GetGattConnectionCallback(),
GetConnectErrorCallback());
device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
GetConnectErrorCallback(Call::NOT_EXPECTED));
SimulateGattConnection(device);
SimulateGattConnection(device);
EXPECT_EQ(1, gatt_connection_attempts_);
......@@ -215,10 +213,7 @@ TEST_F(BluetoothTest,
EXPECT_TRUE(gatt_connections_[0]->IsConnected());
// Become disconnected:
ResetEventCounts();
SimulateGattDisconnection(device);
EXPECT_EQ(0, callback_count_);
EXPECT_EQ(0, error_callback_count_);
EXPECT_FALSE(gatt_connections_[0]->IsConnected());
}
#endif // defined(OS_ANDROID)
......@@ -231,18 +226,16 @@ TEST_F(BluetoothTest, BluetoothGattConnection_AlreadyConnected) {
BluetoothDevice* device = DiscoverLowEnergyDevice(3);
// Be already connected:
device->CreateGattConnection(GetGattConnectionCallback(),
GetConnectErrorCallback());
device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
GetConnectErrorCallback(Call::NOT_EXPECTED));
SimulateGattConnection(device);
EXPECT_TRUE(gatt_connections_[0]->IsConnected());
// Then CreateGattConnection:
ResetEventCounts();
device->CreateGattConnection(GetGattConnectionCallback(),
GetConnectErrorCallback());
device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
GetConnectErrorCallback(Call::NOT_EXPECTED));
EXPECT_EQ(0, gatt_connection_attempts_);
EXPECT_EQ(1, callback_count_);
EXPECT_EQ(0, error_callback_count_);
EXPECT_TRUE(gatt_connections_[1]->IsConnected());
}
#endif // defined(OS_ANDROID)
......@@ -256,8 +249,8 @@ TEST_F(BluetoothTest,
BluetoothDevice* device = DiscoverLowEnergyDevice(3);
// Create connection:
device->CreateGattConnection(GetGattConnectionCallback(),
GetConnectErrorCallback());
device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
GetConnectErrorCallback(Call::NOT_EXPECTED));
SimulateGattConnection(device);
// Disconnect connection:
......@@ -265,8 +258,8 @@ TEST_F(BluetoothTest,
SimulateGattDisconnection(device);
// Create 2nd connection:
device->CreateGattConnection(GetGattConnectionCallback(),
GetConnectErrorCallback());
device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
GetConnectErrorCallback(Call::NOT_EXPECTED));
SimulateGattConnection(device);
EXPECT_FALSE(gatt_connections_[0]->IsConnected())
......@@ -284,10 +277,10 @@ TEST_F(BluetoothTest, BluetoothGattConnection_DisconnectWhenObjectsDestroyed) {
BluetoothDevice* device = DiscoverLowEnergyDevice(3);
// Create multiple connections and simulate connection complete:
device->CreateGattConnection(GetGattConnectionCallback(),
GetConnectErrorCallback());
device->CreateGattConnection(GetGattConnectionCallback(),
GetConnectErrorCallback());
device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
GetConnectErrorCallback(Call::NOT_EXPECTED));
device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
GetConnectErrorCallback(Call::NOT_EXPECTED));
SimulateGattConnection(device);
// Delete all CreateGattConnection objects, observe disconnection:
......@@ -305,10 +298,10 @@ TEST_F(BluetoothTest, BluetoothGattConnection_DisconnectInProgress) {
BluetoothDevice* device = DiscoverLowEnergyDevice(3);
// Create multiple connections and simulate connection complete:
device->CreateGattConnection(GetGattConnectionCallback(),
GetConnectErrorCallback());
device->CreateGattConnection(GetGattConnectionCallback(),
GetConnectErrorCallback());
device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
GetConnectErrorCallback(Call::NOT_EXPECTED));
device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
GetConnectErrorCallback(Call::NOT_EXPECTED));
SimulateGattConnection(device);
// Disconnect all CreateGattConnection objects & create a new connection.
......@@ -319,8 +312,8 @@ TEST_F(BluetoothTest, BluetoothGattConnection_DisconnectInProgress) {
EXPECT_EQ(1, gatt_disconnection_attempts_);
// Create a connection.
device->CreateGattConnection(GetGattConnectionCallback(),
GetConnectErrorCallback());
device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
GetConnectErrorCallback(Call::NOT_EXPECTED));
EXPECT_EQ(0, gatt_connection_attempts_); // No connection attempt.
EXPECT_EQ(1, callback_count_); // Device is assumed still connected.
EXPECT_EQ(0, error_callback_count_);
......@@ -328,10 +321,7 @@ TEST_F(BluetoothTest, BluetoothGattConnection_DisconnectInProgress) {
EXPECT_TRUE(gatt_connections_.back()->IsConnected());
// Actually disconnect:
ResetEventCounts();
SimulateGattDisconnection(device);
EXPECT_EQ(0, callback_count_);
EXPECT_EQ(0, error_callback_count_);
for (BluetoothGattConnection* connection : gatt_connections_)
EXPECT_FALSE(connection->IsConnected());
}
......@@ -346,12 +336,10 @@ TEST_F(BluetoothTest, BluetoothGattConnection_SimulateDisconnect) {
BluetoothDevice* device = DiscoverLowEnergyDevice(3);
ResetEventCounts();
device->CreateGattConnection(GetGattConnectionCallback(),
GetConnectErrorCallback());
device->CreateGattConnection(GetGattConnectionCallback(Call::NOT_EXPECTED),
GetConnectErrorCallback(Call::EXPECTED));
EXPECT_EQ(1, gatt_connection_attempts_);
SimulateGattDisconnection(device);
EXPECT_EQ(0, callback_count_);
EXPECT_EQ(1, error_callback_count_);
EXPECT_EQ(BluetoothDevice::ERROR_FAILED, last_connect_error_code_);
for (BluetoothGattConnection* connection : gatt_connections_)
EXPECT_FALSE(connection->IsConnected());
......@@ -366,8 +354,8 @@ TEST_F(BluetoothTest, BluetoothGattConnection_DisconnectGatt_SimulateConnect) {
BluetoothDevice* device = DiscoverLowEnergyDevice(3);
ResetEventCounts();
device->CreateGattConnection(GetGattConnectionCallback(),
GetConnectErrorCallback());
device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
GetConnectErrorCallback(Call::NOT_EXPECTED));
device->DisconnectGatt();
EXPECT_EQ(1, gatt_connection_attempts_);
EXPECT_EQ(1, gatt_disconnection_attempts_);
......@@ -391,14 +379,12 @@ TEST_F(BluetoothTest,
BluetoothDevice* device = DiscoverLowEnergyDevice(3);
ResetEventCounts();
device->CreateGattConnection(GetGattConnectionCallback(),
GetConnectErrorCallback());
device->CreateGattConnection(GetGattConnectionCallback(Call::NOT_EXPECTED),
GetConnectErrorCallback(Call::EXPECTED));
device->DisconnectGatt();
EXPECT_EQ(1, gatt_connection_attempts_);
EXPECT_EQ(1, gatt_disconnection_attempts_);
SimulateGattDisconnection(device);
EXPECT_EQ(0, callback_count_);
EXPECT_EQ(1, error_callback_count_);
EXPECT_EQ(BluetoothDevice::ERROR_FAILED, last_connect_error_code_);
for (BluetoothGattConnection* connection : gatt_connections_)
EXPECT_FALSE(connection->IsConnected());
......@@ -414,13 +400,11 @@ TEST_F(BluetoothTest, BluetoothGattConnection_ErrorAfterConnection) {
BluetoothDevice* device = DiscoverLowEnergyDevice(3);
ResetEventCounts();
device->CreateGattConnection(GetGattConnectionCallback(),
GetConnectErrorCallback());
device->CreateGattConnection(GetGattConnectionCallback(Call::NOT_EXPECTED),
GetConnectErrorCallback(Call::EXPECTED));
EXPECT_EQ(1, gatt_connection_attempts_);
SimulateGattConnectionError(device, BluetoothDevice::ERROR_AUTH_FAILED);
SimulateGattConnectionError(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 (BluetoothGattConnection* connection : gatt_connections_)
EXPECT_FALSE(connection->IsConnected());
......@@ -432,8 +416,8 @@ TEST_F(BluetoothTest, GattServices_ObserversCalls) {
InitWithFakeAdapter();
StartLowEnergyDiscoverySession();
BluetoothDevice* device = DiscoverLowEnergyDevice(3);
device->CreateGattConnection(GetGattConnectionCallback(),
GetConnectErrorCallback());
device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
GetConnectErrorCallback(Call::NOT_EXPECTED));
TestBluetoothAdapterObserver observer(adapter_);
ResetEventCounts();
SimulateGattConnection(device);
......@@ -454,8 +438,8 @@ TEST_F(BluetoothTest, GetGattServices_and_GetGattService) {
InitWithFakeAdapter();
StartLowEnergyDiscoverySession();
BluetoothDevice* device = DiscoverLowEnergyDevice(3);
device->CreateGattConnection(GetGattConnectionCallback(),
GetConnectErrorCallback());
device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
GetConnectErrorCallback(Call::NOT_EXPECTED));
ResetEventCounts();
SimulateGattConnection(device);
EXPECT_EQ(1, gatt_discovery_attempts_);
......@@ -483,8 +467,8 @@ TEST_F(BluetoothTest, GetGattServices_DiscoveryError) {
InitWithFakeAdapter();
StartLowEnergyDiscoverySession();
BluetoothDevice* device = DiscoverLowEnergyDevice(3);
device->CreateGattConnection(GetGattConnectionCallback(),
GetConnectErrorCallback());
device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
GetConnectErrorCallback(Call::NOT_EXPECTED));
ResetEventCounts();
SimulateGattConnection(device);
EXPECT_EQ(1, gatt_discovery_attempts_);
......
......@@ -24,8 +24,8 @@ class BluetoothGattCharacteristicTest : public BluetoothTest {
InitWithFakeAdapter();
StartLowEnergyDiscoverySession();
device_ = DiscoverLowEnergyDevice(3);
device_->CreateGattConnection(GetGattConnectionCallback(),
GetConnectErrorCallback());
device_->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
GetConnectErrorCallback(Call::NOT_EXPECTED));
SimulateGattConnection(device_);
std::vector<std::string> services;
std::string uuid("00000000-0000-1000-8000-00805f9b34fb");
......@@ -55,10 +55,10 @@ TEST_F(BluetoothGattCharacteristicTest, GetIdentifier) {
// 2 devices to verify unique IDs across them.
BluetoothDevice* device1 = DiscoverLowEnergyDevice(3);
BluetoothDevice* device2 = DiscoverLowEnergyDevice(4);
device1->CreateGattConnection(GetGattConnectionCallback(),
GetConnectErrorCallback());
device2->CreateGattConnection(GetGattConnectionCallback(),
GetConnectErrorCallback());
device1->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
GetConnectErrorCallback(Call::NOT_EXPECTED));
device2->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
GetConnectErrorCallback(Call::NOT_EXPECTED));
SimulateGattConnection(device1);
SimulateGattConnection(device2);
......@@ -116,8 +116,8 @@ TEST_F(BluetoothGattCharacteristicTest, GetUUID) {
InitWithFakeAdapter();
StartLowEnergyDiscoverySession();
BluetoothDevice* device = DiscoverLowEnergyDevice(3);
device->CreateGattConnection(GetGattConnectionCallback(),
GetConnectErrorCallback());
device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
GetConnectErrorCallback(Call::NOT_EXPECTED));
SimulateGattConnection(device);
std::vector<std::string> services;
services.push_back("00000000-0000-1000-8000-00805f9b34fb");
......@@ -154,8 +154,8 @@ TEST_F(BluetoothGattCharacteristicTest, GetProperties) {
InitWithFakeAdapter();
StartLowEnergyDiscoverySession();
BluetoothDevice* device = DiscoverLowEnergyDevice(3);
device->CreateGattConnection(GetGattConnectionCallback(),
GetConnectErrorCallback());
device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
GetConnectErrorCallback(Call::NOT_EXPECTED));
SimulateGattConnection(device);
std::vector<std::string> services;
std::string uuid("00000000-0000-1000-8000-00805f9b34fb");
......@@ -182,8 +182,9 @@ TEST_F(BluetoothGattCharacteristicTest, GetProperties) {
TEST_F(BluetoothGattCharacteristicTest, ReadRemoteCharacteristic_Empty) {
ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate());
characteristic1_->ReadRemoteCharacteristic(GetReadValueCallback(),
GetGattErrorCallback());
characteristic1_->ReadRemoteCharacteristic(
GetReadValueCallback(Call::EXPECTED),
GetGattErrorCallback(Call::NOT_EXPECTED));
EXPECT_EQ(1, gatt_read_characteristic_attempts_);
std::vector<uint8_t> empty_vector;
SimulateGattCharacteristicRead(characteristic1_, empty_vector);
......@@ -191,8 +192,6 @@ TEST_F(BluetoothGattCharacteristicTest, ReadRemoteCharacteristic_Empty) {
// Duplicate read reported from OS shouldn't cause a problem:
SimulateGattCharacteristicRead(characteristic1_, empty_vector);
EXPECT_EQ(1, callback_count_);
EXPECT_EQ(0, error_callback_count_);
EXPECT_EQ(empty_vector, last_read_value_);
EXPECT_EQ(empty_vector, characteristic1_->GetValue());
}
......@@ -204,13 +203,12 @@ TEST_F(BluetoothGattCharacteristicTest, WriteRemoteCharacteristic_Empty) {
ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate());
std::vector<uint8_t> empty_vector;
characteristic1_->WriteRemoteCharacteristic(empty_vector, GetCallback(),
GetGattErrorCallback());
characteristic1_->WriteRemoteCharacteristic(
empty_vector, GetCallback(Call::EXPECTED),
GetGattErrorCallback(Call::NOT_EXPECTED));
EXPECT_EQ(1, gatt_write_characteristic_attempts_);
SimulateGattCharacteristicWrite(characteristic1_);
EXPECT_EQ(1, callback_count_);
EXPECT_EQ(0, error_callback_count_);
EXPECT_EQ(empty_vector, last_write_value_);
}
#endif // defined(OS_ANDROID)
......@@ -220,8 +218,9 @@ TEST_F(BluetoothGattCharacteristicTest, WriteRemoteCharacteristic_Empty) {
TEST_F(BluetoothGattCharacteristicTest, ReadRemoteCharacteristic) {
ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate());
characteristic1_->ReadRemoteCharacteristic(GetReadValueCallback(),
GetGattErrorCallback());
characteristic1_->ReadRemoteCharacteristic(
GetReadValueCallback(Call::EXPECTED),
GetGattErrorCallback(Call::NOT_EXPECTED));
EXPECT_EQ(1, gatt_read_characteristic_attempts_);
uint8_t values[] = {0, 1, 2, 3, 4, 0xf, 0xf0, 0xff};
......@@ -232,8 +231,6 @@ TEST_F(BluetoothGattCharacteristicTest, ReadRemoteCharacteristic) {
std::vector<uint8_t> empty_vector;
SimulateGattCharacteristicRead(characteristic1_, empty_vector);
EXPECT_EQ(1, callback_count_);
EXPECT_EQ(0, error_callback_count_);
EXPECT_EQ(test_vector, last_read_value_);
EXPECT_EQ(test_vector, characteristic1_->GetValue());
}
......@@ -246,14 +243,13 @@ TEST_F(BluetoothGattCharacteristicTest, WriteRemoteCharacteristic) {
uint8_t values[] = {0, 1, 2, 3, 4, 0xf, 0xf0, 0xff};
std::vector<uint8_t> test_vector(values, values + arraysize(values));
characteristic1_->WriteRemoteCharacteristic(test_vector, GetCallback(),
GetGattErrorCallback());
characteristic1_->WriteRemoteCharacteristic(
test_vector, GetCallback(Call::EXPECTED),
GetGattErrorCallback(Call::NOT_EXPECTED));
EXPECT_EQ(1, gatt_write_characteristic_attempts_);
SimulateGattCharacteristicWrite(characteristic1_);
EXPECT_EQ(1, callback_count_);
EXPECT_EQ(0, error_callback_count_);
EXPECT_EQ(test_vector, last_write_value_);
}
#endif // defined(OS_ANDROID)
......@@ -263,8 +259,9 @@ TEST_F(BluetoothGattCharacteristicTest, WriteRemoteCharacteristic) {
TEST_F(BluetoothGattCharacteristicTest, ReadRemoteCharacteristic_Twice) {
ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate());
characteristic1_->ReadRemoteCharacteristic(GetReadValueCallback(),
GetGattErrorCallback());
characteristic1_->ReadRemoteCharacteristic(
GetReadValueCallback(Call::EXPECTED),
GetGattErrorCallback(Call::NOT_EXPECTED));
EXPECT_EQ(1, gatt_read_characteristic_attempts_);
uint8_t values[] = {0, 1, 2, 3, 4, 0xf, 0xf0, 0xff};
......@@ -277,8 +274,9 @@ TEST_F(BluetoothGattCharacteristicTest, ReadRemoteCharacteristic_Twice) {
// Read again, with different value:
ResetEventCounts();
characteristic1_->ReadRemoteCharacteristic(GetReadValueCallback(),
GetGattErrorCallback());
characteristic1_->ReadRemoteCharacteristic(
GetReadValueCallback(Call::EXPECTED),
GetGattErrorCallback(Call::NOT_EXPECTED));
EXPECT_EQ(1, gatt_read_characteristic_attempts_);
std::vector<uint8_t> empty_vector;
SimulateGattCharacteristicRead(characteristic1_, empty_vector);
......@@ -296,8 +294,9 @@ TEST_F(BluetoothGattCharacteristicTest, WriteRemoteCharacteristic_Twice) {
uint8_t values[] = {0, 1, 2, 3, 4, 0xf, 0xf0, 0xff};
std::vector<uint8_t> test_vector(values, values + arraysize(values));
characteristic1_->WriteRemoteCharacteristic(test_vector, GetCallback(),
GetGattErrorCallback());
characteristic1_->WriteRemoteCharacteristic(
test_vector, GetCallback(Call::EXPECTED),
GetGattErrorCallback(Call::NOT_EXPECTED));
EXPECT_EQ(1, gatt_write_characteristic_attempts_);
SimulateGattCharacteristicWrite(characteristic1_);
......@@ -308,8 +307,9 @@ TEST_F(BluetoothGattCharacteristicTest, WriteRemoteCharacteristic_Twice) {
// Write again, with different value:
ResetEventCounts();
std::vector<uint8_t> empty_vector;
characteristic1_->WriteRemoteCharacteristic(empty_vector, GetCallback(),
GetGattErrorCallback());
characteristic1_->WriteRemoteCharacteristic(
empty_vector, GetCallback(Call::EXPECTED),
GetGattErrorCallback(Call::NOT_EXPECTED));
EXPECT_EQ(1, gatt_write_characteristic_attempts_);
SimulateGattCharacteristicWrite(characteristic1_);
EXPECT_EQ(1, callback_count_);
......@@ -324,10 +324,12 @@ TEST_F(BluetoothGattCharacteristicTest,
ReadRemoteCharacteristic_MultipleCharacteristics) {
ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate());
characteristic1_->ReadRemoteCharacteristic(GetReadValueCallback(),
GetGattErrorCallback());
characteristic2_->ReadRemoteCharacteristic(GetReadValueCallback(),
GetGattErrorCallback());
characteristic1_->ReadRemoteCharacteristic(
GetReadValueCallback(Call::EXPECTED),
GetGattErrorCallback(Call::NOT_EXPECTED));
characteristic2_->ReadRemoteCharacteristic(
GetReadValueCallback(Call::EXPECTED),
GetGattErrorCallback(Call::NOT_EXPECTED));
EXPECT_EQ(2, gatt_read_characteristic_attempts_);
EXPECT_EQ(0, callback_count_);
EXPECT_EQ(0, error_callback_count_);
......@@ -357,14 +359,16 @@ TEST_F(BluetoothGattCharacteristicTest,
std::vector<uint8_t> test_vector1;
test_vector1.push_back(111);
characteristic1_->WriteRemoteCharacteristic(test_vector1, GetCallback(),
GetGattErrorCallback());
characteristic1_->WriteRemoteCharacteristic(
test_vector1, GetCallback(Call::EXPECTED),
GetGattErrorCallback(Call::NOT_EXPECTED));
EXPECT_EQ(test_vector1, last_write_value_);
std::vector<uint8_t> test_vector2;
test_vector2.push_back(222);
characteristic2_->WriteRemoteCharacteristic(test_vector2, GetCallback(),
GetGattErrorCallback());
characteristic2_->WriteRemoteCharacteristic(
test_vector2, GetCallback(Call::EXPECTED),
GetGattErrorCallback(Call::NOT_EXPECTED));
EXPECT_EQ(test_vector2, last_write_value_);
EXPECT_EQ(2, gatt_write_characteristic_attempts_);
......@@ -384,14 +388,13 @@ TEST_F(BluetoothGattCharacteristicTest,
TEST_F(BluetoothGattCharacteristicTest, ReadError) {
ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate());
characteristic1_->ReadRemoteCharacteristic(GetReadValueCallback(),
GetGattErrorCallback());
characteristic1_->ReadRemoteCharacteristic(
GetReadValueCallback(Call::NOT_EXPECTED),
GetGattErrorCallback(Call::EXPECTED));
SimulateGattCharacteristicReadError(
characteristic1_, BluetoothGattService::GATT_ERROR_INVALID_LENGTH);
SimulateGattCharacteristicReadError(characteristic1_,
BluetoothGattService::GATT_ERROR_FAILED);
EXPECT_EQ(0, callback_count_);
EXPECT_EQ(1, error_callback_count_);
EXPECT_EQ(BluetoothGattService::GATT_ERROR_INVALID_LENGTH,
last_gatt_error_code_);
}
......@@ -403,14 +406,14 @@ TEST_F(BluetoothGattCharacteristicTest, WriteError) {
ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate());
std::vector<uint8_t> empty_vector;
characteristic1_->WriteRemoteCharacteristic(empty_vector, GetCallback(),
GetGattErrorCallback());
characteristic1_->WriteRemoteCharacteristic(
empty_vector, GetCallback(Call::NOT_EXPECTED),
GetGattErrorCallback(Call::EXPECTED));
SimulateGattCharacteristicWriteError(
characteristic1_, BluetoothGattService::GATT_ERROR_INVALID_LENGTH);
SimulateGattCharacteristicWriteError(characteristic1_,
BluetoothGattService::GATT_ERROR_FAILED);
EXPECT_EQ(0, callback_count_);
EXPECT_EQ(1, error_callback_count_);
EXPECT_EQ(BluetoothGattService::GATT_ERROR_INVALID_LENGTH,
last_gatt_error_code_);
}
......@@ -422,8 +425,9 @@ TEST_F(BluetoothGattCharacteristicTest, ReadSynchronousError) {
ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate());
SimulateGattCharacteristicReadWillFailSynchronouslyOnce(characteristic1_);
characteristic1_->ReadRemoteCharacteristic(GetReadValueCallback(),
GetGattErrorCallback());
characteristic1_->ReadRemoteCharacteristic(
GetReadValueCallback(Call::NOT_EXPECTED),
GetGattErrorCallback(Call::EXPECTED));
EXPECT_EQ(0, gatt_read_characteristic_attempts_);
base::RunLoop().RunUntilIdle();
EXPECT_EQ(0, callback_count_);
......@@ -432,8 +436,9 @@ TEST_F(BluetoothGattCharacteristicTest, ReadSynchronousError) {
// After failing once, can succeed:
ResetEventCounts();
characteristic1_->ReadRemoteCharacteristic(GetReadValueCallback(),
GetGattErrorCallback());
characteristic1_->ReadRemoteCharacteristic(
GetReadValueCallback(Call::EXPECTED),
GetGattErrorCallback(Call::NOT_EXPECTED));
EXPECT_EQ(1, gatt_read_characteristic_attempts_);
std::vector<uint8_t> empty_vector;
SimulateGattCharacteristicRead(characteristic1_, empty_vector);
......@@ -449,8 +454,9 @@ TEST_F(BluetoothGattCharacteristicTest, WriteSynchronousError) {
SimulateGattCharacteristicWriteWillFailSynchronouslyOnce(characteristic1_);
std::vector<uint8_t> empty_vector;
characteristic1_->WriteRemoteCharacteristic(empty_vector, GetCallback(),
GetGattErrorCallback());
characteristic1_->WriteRemoteCharacteristic(
empty_vector, GetCallback(Call::NOT_EXPECTED),
GetGattErrorCallback(Call::EXPECTED));
EXPECT_EQ(0, gatt_write_characteristic_attempts_);
base::RunLoop().RunUntilIdle();
EXPECT_EQ(0, callback_count_);
......@@ -459,8 +465,9 @@ TEST_F(BluetoothGattCharacteristicTest, WriteSynchronousError) {
// After failing once, can succeed:
ResetEventCounts();
characteristic1_->WriteRemoteCharacteristic(empty_vector, GetCallback(),
GetGattErrorCallback());
characteristic1_->WriteRemoteCharacteristic(
empty_vector, GetCallback(Call::EXPECTED),
GetGattErrorCallback(Call::NOT_EXPECTED));
EXPECT_EQ(1, gatt_write_characteristic_attempts_);
SimulateGattCharacteristicWrite(characteristic1_);
EXPECT_EQ(1, callback_count_);
......@@ -473,10 +480,15 @@ TEST_F(BluetoothGattCharacteristicTest, WriteSynchronousError) {
TEST_F(BluetoothGattCharacteristicTest, ReadRemoteCharacteristic_ReadPending) {
ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate());
characteristic1_->ReadRemoteCharacteristic(GetReadValueCallback(),
GetGattErrorCallback());
characteristic1_->ReadRemoteCharacteristic(GetReadValueCallback(),
GetGattErrorCallback());
characteristic1_->ReadRemoteCharacteristic(
GetReadValueCallback(Call::EXPECTED),
GetGattErrorCallback(Call::NOT_EXPECTED));
characteristic1_->ReadRemoteCharacteristic(
GetReadValueCallback(Call::NOT_EXPECTED),
GetGattErrorCallback(Call::EXPECTED));
base::RunLoop().RunUntilIdle();
EXPECT_EQ(0, callback_count_);
EXPECT_EQ(1, error_callback_count_);
EXPECT_EQ(BluetoothGattService::GATT_ERROR_IN_PROGRESS,
......@@ -498,10 +510,15 @@ TEST_F(BluetoothGattCharacteristicTest,
ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate());
std::vector<uint8_t> empty_vector;
characteristic1_->WriteRemoteCharacteristic(empty_vector, GetCallback(),
GetGattErrorCallback());
characteristic1_->WriteRemoteCharacteristic(empty_vector, GetCallback(),
GetGattErrorCallback());
characteristic1_->WriteRemoteCharacteristic(
empty_vector, GetCallback(Call::EXPECTED),
GetGattErrorCallback(Call::NOT_EXPECTED));
characteristic1_->WriteRemoteCharacteristic(
empty_vector, GetCallback(Call::NOT_EXPECTED),
GetGattErrorCallback(Call::EXPECTED));
base::RunLoop().RunUntilIdle();
EXPECT_EQ(0, callback_count_);
EXPECT_EQ(1, error_callback_count_);
EXPECT_EQ(BluetoothGattService::GATT_ERROR_IN_PROGRESS,
......@@ -521,10 +538,15 @@ TEST_F(BluetoothGattCharacteristicTest, ReadRemoteCharacteristic_WritePending) {
ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate());
std::vector<uint8_t> empty_vector;
characteristic1_->WriteRemoteCharacteristic(empty_vector, GetCallback(),
GetGattErrorCallback());
characteristic1_->ReadRemoteCharacteristic(GetReadValueCallback(),
GetGattErrorCallback());
characteristic1_->WriteRemoteCharacteristic(
empty_vector, GetCallback(Call::EXPECTED),
GetGattErrorCallback(Call::NOT_EXPECTED));
characteristic1_->ReadRemoteCharacteristic(
GetReadValueCallback(Call::NOT_EXPECTED),
GetGattErrorCallback(Call::EXPECTED));
base::RunLoop().RunUntilIdle();
EXPECT_EQ(0, callback_count_);
EXPECT_EQ(1, error_callback_count_);
EXPECT_EQ(BluetoothGattService::GATT_ERROR_IN_PROGRESS,
......@@ -544,10 +566,14 @@ TEST_F(BluetoothGattCharacteristicTest, WriteRemoteCharacteristic_ReadPending) {
ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate());
std::vector<uint8_t> empty_vector;
characteristic1_->ReadRemoteCharacteristic(GetReadValueCallback(),
GetGattErrorCallback());
characteristic1_->WriteRemoteCharacteristic(empty_vector, GetCallback(),
GetGattErrorCallback());
characteristic1_->ReadRemoteCharacteristic(
GetReadValueCallback(Call::EXPECTED),
GetGattErrorCallback(Call::NOT_EXPECTED));
characteristic1_->WriteRemoteCharacteristic(
empty_vector, GetCallback(Call::NOT_EXPECTED),
GetGattErrorCallback(Call::EXPECTED));
base::RunLoop().RunUntilIdle();
EXPECT_EQ(0, callback_count_);
EXPECT_EQ(1, error_callback_count_);
EXPECT_EQ(BluetoothGattService::GATT_ERROR_IN_PROGRESS,
......@@ -566,8 +592,9 @@ TEST_F(BluetoothGattCharacteristicTest, WriteRemoteCharacteristic_ReadPending) {
TEST_F(BluetoothGattCharacteristicTest, StartNotifySession) {
ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate());
characteristic1_->StartNotifySession(GetNotifyCallback(),
GetGattErrorCallback());
characteristic1_->StartNotifySession(
GetNotifyCallback(Call::EXPECTED),
GetGattErrorCallback(Call::NOT_EXPECTED));
EXPECT_EQ(1, gatt_notify_characteristic_attempts_);
EXPECT_EQ(0, callback_count_);
SimulateGattNotifySessionStarted(characteristic1_);
......@@ -588,8 +615,8 @@ TEST_F(BluetoothGattCharacteristicTest, StartNotifySession_SynchronousError) {
SimulateGattCharacteristicSetNotifyWillFailSynchronouslyOnce(
characteristic1_);
characteristic1_->StartNotifySession(GetNotifyCallback(),
GetGattErrorCallback());
characteristic1_->StartNotifySession(GetNotifyCallback(Call::NOT_EXPECTED),
GetGattErrorCallback(Call::EXPECTED));
EXPECT_EQ(0, error_callback_count_);
base::RunLoop().RunUntilIdle();
EXPECT_EQ(0, gatt_notify_characteristic_attempts_);
......@@ -604,10 +631,12 @@ TEST_F(BluetoothGattCharacteristicTest, StartNotifySession_SynchronousError) {
TEST_F(BluetoothGattCharacteristicTest, StartNotifySession_Multiple) {
ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate());
characteristic1_->StartNotifySession(GetNotifyCallback(),
GetGattErrorCallback());
characteristic1_->StartNotifySession(GetNotifyCallback(),
GetGattErrorCallback());
characteristic1_->StartNotifySession(
GetNotifyCallback(Call::EXPECTED),
GetGattErrorCallback(Call::NOT_EXPECTED));
characteristic1_->StartNotifySession(
GetNotifyCallback(Call::EXPECTED),
GetGattErrorCallback(Call::NOT_EXPECTED));
#if defined(OS_ANDROID)
// TODO(crbug.com/551634): Decide when implementing IsNotifying if Android
// should trust the notification request always worked, or if we should always
......
......@@ -26,10 +26,10 @@ TEST_F(BluetoothGattServiceTest, GetIdentifier) {
// 2 devices to verify unique IDs across them.
BluetoothDevice* device1 = DiscoverLowEnergyDevice(3);
BluetoothDevice* device2 = DiscoverLowEnergyDevice(4);
device1->CreateGattConnection(GetGattConnectionCallback(),
GetConnectErrorCallback());
device2->CreateGattConnection(GetGattConnectionCallback(),
GetConnectErrorCallback());
device1->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
GetConnectErrorCallback(Call::NOT_EXPECTED));
device2->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
GetConnectErrorCallback(Call::NOT_EXPECTED));
SimulateGattConnection(device1);
SimulateGattConnection(device2);
......@@ -62,8 +62,8 @@ TEST_F(BluetoothGattServiceTest, GetUUID) {
InitWithFakeAdapter();
StartLowEnergyDiscoverySession();
BluetoothDevice* device = DiscoverLowEnergyDevice(3);
device->CreateGattConnection(GetGattConnectionCallback(),
GetConnectErrorCallback());
device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
GetConnectErrorCallback(Call::NOT_EXPECTED));
SimulateGattConnection(device);
// Create multiple instances with the same UUID.
......@@ -84,8 +84,8 @@ TEST_F(BluetoothGattServiceTest, GetCharacteristics_FindNone) {
InitWithFakeAdapter();
StartLowEnergyDiscoverySession();
BluetoothDevice* device = DiscoverLowEnergyDevice(3);
device->CreateGattConnection(GetGattConnectionCallback(),
GetConnectErrorCallback());
device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
GetConnectErrorCallback(Call::NOT_EXPECTED));
SimulateGattConnection(device);
// Simulate a service, with no Characteristics:
......@@ -103,8 +103,8 @@ TEST_F(BluetoothGattServiceTest, GetCharacteristics_and_GetCharacteristic) {
InitWithFakeAdapter();
StartLowEnergyDiscoverySession();
BluetoothDevice* device = DiscoverLowEnergyDevice(3);
device->CreateGattConnection(GetGattConnectionCallback(),
GetConnectErrorCallback());
device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
GetConnectErrorCallback(Call::NOT_EXPECTED));
SimulateGattConnection(device);
// Simulate a service, with several Characteristics:
......
......@@ -144,7 +144,10 @@ void BluetoothRemoteGattCharacteristicAndroid::ReadRemoteCharacteristic(
const ValueCallback& callback,
const ErrorCallback& error_callback) {
if (read_pending_ || write_pending_) {
error_callback.Run(BluetoothGattService::GATT_ERROR_IN_PROGRESS);
base::MessageLoop::current()->PostTask(
FROM_HERE, base::Bind(error_callback,
BluetoothGattService::GATT_ERROR_IN_PROGRESS));
return;
}
if (!Java_ChromeBluetoothRemoteGattCharacteristic_readRemoteCharacteristic(
......@@ -166,7 +169,10 @@ void BluetoothRemoteGattCharacteristicAndroid::WriteRemoteCharacteristic(
const base::Closure& callback,
const ErrorCallback& error_callback) {
if (read_pending_ || write_pending_) {
error_callback.Run(BluetoothGattService::GATT_ERROR_IN_PROGRESS);
base::MessageLoop::current()->PostTask(
FROM_HERE, base::Bind(error_callback,
BluetoothGattService::GATT_ERROR_IN_PROGRESS));
return;
}
JNIEnv* env = AttachCurrentThread();
......
......@@ -34,10 +34,27 @@ void BluetoothTestBase::StartLowEnergyDiscoverySession() {
adapter_->StartDiscoverySessionWithFilter(
make_scoped_ptr(new BluetoothDiscoveryFilter(
BluetoothDiscoveryFilter::Transport::TRANSPORT_LE)),
GetDiscoverySessionCallback(), GetErrorCallback());
GetDiscoverySessionCallback(Call::EXPECTED),
GetErrorCallback(Call::NOT_EXPECTED));
base::RunLoop().RunUntilIdle();
}
void BluetoothTestBase::StartLowEnergyDiscoverySessionExpectedToFail() {
adapter_->StartDiscoverySessionWithFilter(
make_scoped_ptr(new BluetoothDiscoveryFilter(
BluetoothDiscoveryFilter::Transport::TRANSPORT_LE)),
GetDiscoverySessionCallback(Call::NOT_EXPECTED),
GetErrorCallback(Call::EXPECTED));
base::RunLoop().RunUntilIdle();
}
void BluetoothTestBase::TearDown() {
EXPECT_EQ(expected_success_callback_calls_, actual_success_callback_calls_);
EXPECT_EQ(expected_error_callback_calls_, actual_error_callback_calls_);
EXPECT_FALSE(unexpected_success_callback_);
EXPECT_FALSE(unexpected_error_callback_);
}
bool BluetoothTestBase::DenyPermission() {
return false;
}
......@@ -52,92 +69,156 @@ void BluetoothTestBase::DeleteDevice(BluetoothDevice* device) {
adapter_->DeleteDeviceForTesting(device->GetAddress());
}
void BluetoothTestBase::Callback() {
void BluetoothTestBase::Callback(Call expected) {
++callback_count_;
if (expected == Call::EXPECTED)
++actual_success_callback_calls_;
else
unexpected_success_callback_ = true;
}
void BluetoothTestBase::DiscoverySessionCallback(
Call expected,
scoped_ptr<BluetoothDiscoverySession> discovery_session) {
++callback_count_;
discovery_sessions_.push_back(discovery_session.release());
if (expected == Call::EXPECTED)
++actual_success_callback_calls_;
else
unexpected_success_callback_ = true;
}
void BluetoothTestBase::GattConnectionCallback(
Call expected,
scoped_ptr<BluetoothGattConnection> connection) {
++callback_count_;
gatt_connections_.push_back(connection.release());
if (expected == Call::EXPECTED)
++actual_success_callback_calls_;
else
unexpected_success_callback_ = true;
}
void BluetoothTestBase::NotifyCallback(
Call expected,
scoped_ptr<BluetoothGattNotifySession> notify_session) {
++callback_count_;
notify_sessions_.push_back(notify_session.release());
if (expected == Call::EXPECTED)
++actual_success_callback_calls_;
else
unexpected_success_callback_ = true;
}
void BluetoothTestBase::ReadValueCallback(const std::vector<uint8>& value) {
void BluetoothTestBase::ReadValueCallback(Call expected,
const std::vector<uint8>& value) {
++callback_count_;
last_read_value_ = value;
if (expected == Call::EXPECTED)
++actual_success_callback_calls_;
else
unexpected_success_callback_ = true;
}
void BluetoothTestBase::ErrorCallback() {
void BluetoothTestBase::ErrorCallback(Call expected) {
++error_callback_count_;
if (expected == Call::EXPECTED)
++actual_error_callback_calls_;
else
unexpected_error_callback_ = true;
}
void BluetoothTestBase::ConnectErrorCallback(
Call expected,
enum BluetoothDevice::ConnectErrorCode error_code) {
++error_callback_count_;
last_connect_error_code_ = error_code;
if (expected == Call::EXPECTED)
++actual_error_callback_calls_;
else
unexpected_error_callback_ = true;
}
void BluetoothTestBase::GattErrorCallback(
Call expected,
BluetoothGattService::GattErrorCode error_code) {
++error_callback_count_;
last_gatt_error_code_ = error_code;
if (expected == Call::EXPECTED)
++actual_error_callback_calls_;
else
unexpected_error_callback_ = true;
}
base::Closure BluetoothTestBase::GetCallback() {
return base::Bind(&BluetoothTestBase::Callback, weak_factory_.GetWeakPtr());
base::Closure BluetoothTestBase::GetCallback(Call expected) {
if (expected == Call::EXPECTED)
++expected_success_callback_calls_;
return base::Bind(&BluetoothTestBase::Callback, weak_factory_.GetWeakPtr(),
expected);
}
BluetoothAdapter::DiscoverySessionCallback
BluetoothTestBase::GetDiscoverySessionCallback() {
BluetoothTestBase::GetDiscoverySessionCallback(Call expected) {
if (expected == Call::EXPECTED)
++expected_success_callback_calls_;
return base::Bind(&BluetoothTestBase::DiscoverySessionCallback,
weak_factory_.GetWeakPtr());
weak_factory_.GetWeakPtr(), expected);
}
BluetoothDevice::GattConnectionCallback
BluetoothTestBase::GetGattConnectionCallback() {
BluetoothTestBase::GetGattConnectionCallback(Call expected) {
if (expected == Call::EXPECTED)
++expected_success_callback_calls_;
return base::Bind(&BluetoothTestBase::GattConnectionCallback,
weak_factory_.GetWeakPtr());
weak_factory_.GetWeakPtr(), expected);
}
BluetoothGattCharacteristic::NotifySessionCallback
BluetoothTestBase::GetNotifyCallback() {
BluetoothTestBase::GetNotifyCallback(Call expected) {
if (expected == Call::EXPECTED)
++expected_success_callback_calls_;
return base::Bind(&BluetoothTestBase::NotifyCallback,
weak_factory_.GetWeakPtr());
weak_factory_.GetWeakPtr(), expected);
}
BluetoothGattCharacteristic::ValueCallback
BluetoothTestBase::GetReadValueCallback() {
BluetoothTestBase::GetReadValueCallback(Call expected) {
if (expected == Call::EXPECTED)
++expected_success_callback_calls_;
return base::Bind(&BluetoothTestBase::ReadValueCallback,
weak_factory_.GetWeakPtr());
weak_factory_.GetWeakPtr(), expected);
}
BluetoothAdapter::ErrorCallback BluetoothTestBase::GetErrorCallback() {
BluetoothAdapter::ErrorCallback BluetoothTestBase::GetErrorCallback(
Call expected) {
if (expected == Call::EXPECTED)
++expected_error_callback_calls_;
return base::Bind(&BluetoothTestBase::ErrorCallback,
weak_factory_.GetWeakPtr());
weak_factory_.GetWeakPtr(), expected);
}
BluetoothDevice::ConnectErrorCallback
BluetoothTestBase::GetConnectErrorCallback() {
BluetoothTestBase::GetConnectErrorCallback(Call expected) {
if (expected == Call::EXPECTED)
++expected_error_callback_calls_;
return base::Bind(&BluetoothTestBase::ConnectErrorCallback,
weak_factory_.GetWeakPtr());
weak_factory_.GetWeakPtr(), expected);
}
base::Callback<void(BluetoothGattService::GattErrorCode)>
BluetoothTestBase::GetGattErrorCallback() {
BluetoothTestBase::GetGattErrorCallback(Call expected) {
if (expected == Call::EXPECTED)
++expected_error_callback_calls_;
return base::Bind(&BluetoothTestBase::GattErrorCallback,
weak_factory_.GetWeakPtr());
weak_factory_.GetWeakPtr(), expected);
}
void BluetoothTestBase::ResetEventCounts() {
......
......@@ -29,6 +29,8 @@ class BluetoothDevice;
// BluetoothTest.
class BluetoothTestBase : public testing::Test {
public:
enum class Call { EXPECTED, NOT_EXPECTED };
static const std::string kTestAdapterName;
static const std::string kTestAdapterAddress;
......@@ -46,10 +48,20 @@ class BluetoothTestBase : public testing::Test {
BluetoothTestBase();
~BluetoothTestBase() override;
// Checks that no unexpected calls have been made to callbacks.
// Overrides of this method should always call the parent's class method.
void TearDown() override;
// Calls adapter_->StartDiscoverySessionWithFilter with Low Energy transport,
// and this fixture's callbacks. Then RunLoop().RunUntilIdle().
// and this fixture's callbacks expecting success.
// Then RunLoop().RunUntilIdle().
void StartLowEnergyDiscoverySession();
// Calls adapter_->StartDiscoverySessionWithFilter with Low Energy transport,
// and this fixture's callbacks expecting error.
// Then RunLoop().RunUntilIdle().
void StartLowEnergyDiscoverySessionExpectedToFail();
// Check if Low Energy is available. On Mac, we require OS X >= 10.10.
virtual bool PlatformSupportsLowEnergy() = 0;
......@@ -148,31 +160,38 @@ class BluetoothTestBase : public testing::Test {
virtual void DeleteDevice(BluetoothDevice* device);
// Callbacks that increment |callback_count_|, |error_callback_count_|:
void Callback();
void DiscoverySessionCallback(scoped_ptr<BluetoothDiscoverySession>);
void GattConnectionCallback(scoped_ptr<BluetoothGattConnection>);
void NotifyCallback(scoped_ptr<BluetoothGattNotifySession>);
void ReadValueCallback(const std::vector<uint8>& value);
void ErrorCallback();
void ConnectErrorCallback(enum BluetoothDevice::ConnectErrorCode);
void GattErrorCallback(BluetoothGattService::GattErrorCode);
void Callback(Call expected);
void DiscoverySessionCallback(Call expected,
scoped_ptr<BluetoothDiscoverySession>);
void GattConnectionCallback(Call expected,
scoped_ptr<BluetoothGattConnection>);
void NotifyCallback(Call expected, scoped_ptr<BluetoothGattNotifySession>);
void ReadValueCallback(Call expected, const std::vector<uint8>& value);
void ErrorCallback(Call expected);
void ConnectErrorCallback(Call expected,
enum BluetoothDevice::ConnectErrorCode);
void GattErrorCallback(Call expected, BluetoothGattService::GattErrorCode);
// Accessors to get callbacks bound to this fixture:
base::Closure GetCallback();
BluetoothAdapter::DiscoverySessionCallback GetDiscoverySessionCallback();
BluetoothDevice::GattConnectionCallback GetGattConnectionCallback();
BluetoothGattCharacteristic::NotifySessionCallback GetNotifyCallback();
BluetoothGattCharacteristic::ValueCallback GetReadValueCallback();
BluetoothAdapter::ErrorCallback GetErrorCallback();
BluetoothDevice::ConnectErrorCallback GetConnectErrorCallback();
base::Closure GetCallback(Call expected);
BluetoothAdapter::DiscoverySessionCallback GetDiscoverySessionCallback(
Call expected);
BluetoothDevice::GattConnectionCallback GetGattConnectionCallback(
Call expected);
BluetoothGattCharacteristic::NotifySessionCallback GetNotifyCallback(
Call expected);
BluetoothGattCharacteristic::ValueCallback GetReadValueCallback(
Call expected);
BluetoothAdapter::ErrorCallback GetErrorCallback(Call expected);
BluetoothDevice::ConnectErrorCallback GetConnectErrorCallback(Call expected);
base::Callback<void(BluetoothGattService::GattErrorCode)>
GetGattErrorCallback();
GetGattErrorCallback(Call expected);
// Reset all event count members to 0.
void ResetEventCounts();
// A Message loop is required by some implementations that will PostTasks and
// by base::RunLoop().RunUntilIdle() use in this fixuture.
// by base::RunLoop().RunUntilIdle() use in this fixture.
base::MessageLoop message_loop_;
scoped_refptr<BluetoothAdapter> adapter_;
......@@ -184,6 +203,7 @@ class BluetoothTestBase : public testing::Test {
std::vector<uint8> last_read_value_;
std::vector<uint8> last_write_value_;
BluetoothGattService::GattErrorCode last_gatt_error_code_;
int callback_count_ = 0;
int error_callback_count_ = 0;
int gatt_connection_attempts_ = 0;
......@@ -192,6 +212,16 @@ class BluetoothTestBase : public testing::Test {
int gatt_notify_characteristic_attempts_ = 0;
int gatt_read_characteristic_attempts_ = 0;
int gatt_write_characteristic_attempts_ = 0;
// The following values are used to make sure the correct callbacks
// have been called. They are not reset when calling ResetEventCounts().
int expected_success_callback_calls_ = 0;
int expected_error_callback_calls_ = 0;
int actual_success_callback_calls_ = 0;
int actual_error_callback_calls_ = 0;
bool unexpected_success_callback_ = false;
bool unexpected_error_callback_ = false;
base::WeakPtrFactory<BluetoothTestBase> weak_factory_;
};
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment