| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "device/bluetooth/bluetooth_gatt_characteristic.h" | 5 #include "device/bluetooth/bluetooth_gatt_characteristic.h" |
| 6 | 6 |
| 7 #include "base/run_loop.h" | 7 #include "base/run_loop.h" |
| 8 #include "device/bluetooth/bluetooth_gatt_service.h" | 8 #include "device/bluetooth/bluetooth_gatt_service.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 | 10 |
| 11 #if defined(OS_ANDROID) | 11 #if defined(OS_ANDROID) |
| 12 #include "device/bluetooth/test/bluetooth_test_android.h" | 12 #include "device/bluetooth/test/bluetooth_test_android.h" |
| 13 #elif defined(OS_MACOSX) | 13 #elif defined(OS_MACOSX) |
| 14 #include "device/bluetooth/test/bluetooth_test_mac.h" | 14 #include "device/bluetooth/test/bluetooth_test_mac.h" |
| 15 #endif | 15 #endif |
| 16 | 16 |
| 17 namespace device { | 17 namespace device { |
| 18 | 18 |
| 19 #if defined(OS_ANDROID) || defined(OS_MACOSX) | 19 #if defined(OS_ANDROID) || defined(OS_MACOSX) |
| 20 class BluetoothGattCharacteristicTest : public BluetoothTest { | 20 class BluetoothGattCharacteristicTest : public BluetoothTest { |
| 21 public: | 21 public: |
| 22 // Create adapter_, device_, service, and provides. | 22 // Create adapter_, device_, service, and provides. |
| 23 void FakeCharacteristicBoilerplate() { | 23 void FakeCharacteristicBoilerplate() { |
| 24 InitWithFakeAdapter(); | 24 InitWithFakeAdapter(); |
| 25 StartLowEnergyDiscoverySession(); | 25 StartLowEnergyDiscoverySession(); |
| 26 device_ = DiscoverLowEnergyDevice(3); | 26 device_ = DiscoverLowEnergyDevice(3); |
| 27 device_->CreateGattConnection(GetGattConnectionCallback(), | 27 device_->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
| 28 GetConnectErrorCallback()); | 28 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
| 29 SimulateGattConnection(device_); | 29 SimulateGattConnection(device_); |
| 30 std::vector<std::string> services; | 30 std::vector<std::string> services; |
| 31 std::string uuid("00000000-0000-1000-8000-00805f9b34fb"); | 31 std::string uuid("00000000-0000-1000-8000-00805f9b34fb"); |
| 32 services.push_back(uuid); | 32 services.push_back(uuid); |
| 33 SimulateGattServicesDiscovered(device_, services); | 33 SimulateGattServicesDiscovered(device_, services); |
| 34 ASSERT_EQ(1u, device_->GetGattServices().size()); | 34 ASSERT_EQ(1u, device_->GetGattServices().size()); |
| 35 service_ = device_->GetGattServices()[0]; | 35 service_ = device_->GetGattServices()[0]; |
| 36 SimulateGattCharacteristic(service_, uuid, /* properties */ 0); | 36 SimulateGattCharacteristic(service_, uuid, /* properties */ 0); |
| 37 SimulateGattCharacteristic(service_, uuid, /* properties */ 0); | 37 SimulateGattCharacteristic(service_, uuid, /* properties */ 0); |
| 38 characteristic1_ = service_->GetCharacteristics()[0]; | 38 characteristic1_ = service_->GetCharacteristics()[0]; |
| 39 characteristic2_ = service_->GetCharacteristics()[1]; | 39 characteristic2_ = service_->GetCharacteristics()[1]; |
| 40 ASSERT_EQ(2u, service_->GetCharacteristics().size()); | 40 ASSERT_EQ(2u, service_->GetCharacteristics().size()); |
| 41 ResetEventCounts(); | 41 ResetEventCounts(); |
| 42 } | 42 } |
| 43 | 43 |
| 44 BluetoothDevice* device_ = nullptr; | 44 BluetoothDevice* device_ = nullptr; |
| 45 BluetoothGattService* service_ = nullptr; | 45 BluetoothGattService* service_ = nullptr; |
| 46 BluetoothGattCharacteristic* characteristic1_ = nullptr; | 46 BluetoothGattCharacteristic* characteristic1_ = nullptr; |
| 47 BluetoothGattCharacteristic* characteristic2_ = nullptr; | 47 BluetoothGattCharacteristic* characteristic2_ = nullptr; |
| 48 }; | 48 }; |
| 49 #endif | 49 #endif |
| 50 | 50 |
| 51 #if defined(OS_ANDROID) | 51 #if defined(OS_ANDROID) |
| 52 TEST_F(BluetoothGattCharacteristicTest, GetIdentifier) { | 52 TEST_F(BluetoothGattCharacteristicTest, GetIdentifier) { |
| 53 InitWithFakeAdapter(); | 53 InitWithFakeAdapter(); |
| 54 StartLowEnergyDiscoverySession(); | 54 StartLowEnergyDiscoverySession(); |
| 55 // 2 devices to verify unique IDs across them. | 55 // 2 devices to verify unique IDs across them. |
| 56 BluetoothDevice* device1 = DiscoverLowEnergyDevice(3); | 56 BluetoothDevice* device1 = DiscoverLowEnergyDevice(3); |
| 57 BluetoothDevice* device2 = DiscoverLowEnergyDevice(4); | 57 BluetoothDevice* device2 = DiscoverLowEnergyDevice(4); |
| 58 device1->CreateGattConnection(GetGattConnectionCallback(), | 58 device1->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
| 59 GetConnectErrorCallback()); | 59 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
| 60 device2->CreateGattConnection(GetGattConnectionCallback(), | 60 device2->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
| 61 GetConnectErrorCallback()); | 61 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
| 62 SimulateGattConnection(device1); | 62 SimulateGattConnection(device1); |
| 63 SimulateGattConnection(device2); | 63 SimulateGattConnection(device2); |
| 64 | 64 |
| 65 // 3 services (all with same UUID). | 65 // 3 services (all with same UUID). |
| 66 // 1 on the first device (to test characteristic instances across devices). | 66 // 1 on the first device (to test characteristic instances across devices). |
| 67 // 2 on the second device (to test same device, multiple service instances). | 67 // 2 on the second device (to test same device, multiple service instances). |
| 68 std::vector<std::string> services; | 68 std::vector<std::string> services; |
| 69 std::string uuid = "00000000-0000-1000-8000-00805f9b34fb"; | 69 std::string uuid = "00000000-0000-1000-8000-00805f9b34fb"; |
| 70 services.push_back(uuid); | 70 services.push_back(uuid); |
| 71 SimulateGattServicesDiscovered(device1, services); | 71 SimulateGattServicesDiscovered(device1, services); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 | 109 |
| 110 EXPECT_NE(char5->GetIdentifier(), char6->GetIdentifier()); | 110 EXPECT_NE(char5->GetIdentifier(), char6->GetIdentifier()); |
| 111 } | 111 } |
| 112 #endif // defined(OS_ANDROID) | 112 #endif // defined(OS_ANDROID) |
| 113 | 113 |
| 114 #if defined(OS_ANDROID) | 114 #if defined(OS_ANDROID) |
| 115 TEST_F(BluetoothGattCharacteristicTest, GetUUID) { | 115 TEST_F(BluetoothGattCharacteristicTest, GetUUID) { |
| 116 InitWithFakeAdapter(); | 116 InitWithFakeAdapter(); |
| 117 StartLowEnergyDiscoverySession(); | 117 StartLowEnergyDiscoverySession(); |
| 118 BluetoothDevice* device = DiscoverLowEnergyDevice(3); | 118 BluetoothDevice* device = DiscoverLowEnergyDevice(3); |
| 119 device->CreateGattConnection(GetGattConnectionCallback(), | 119 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
| 120 GetConnectErrorCallback()); | 120 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
| 121 SimulateGattConnection(device); | 121 SimulateGattConnection(device); |
| 122 std::vector<std::string> services; | 122 std::vector<std::string> services; |
| 123 services.push_back("00000000-0000-1000-8000-00805f9b34fb"); | 123 services.push_back("00000000-0000-1000-8000-00805f9b34fb"); |
| 124 SimulateGattServicesDiscovered(device, services); | 124 SimulateGattServicesDiscovered(device, services); |
| 125 BluetoothGattService* service = device->GetGattServices()[0]; | 125 BluetoothGattService* service = device->GetGattServices()[0]; |
| 126 | 126 |
| 127 // Create 3 characteristics. Two of them are duplicates. | 127 // Create 3 characteristics. Two of them are duplicates. |
| 128 std::string uuid_str1("11111111-0000-1000-8000-00805f9b34fb"); | 128 std::string uuid_str1("11111111-0000-1000-8000-00805f9b34fb"); |
| 129 std::string uuid_str2("22222222-0000-1000-8000-00805f9b34fb"); | 129 std::string uuid_str2("22222222-0000-1000-8000-00805f9b34fb"); |
| 130 BluetoothUUID uuid1(uuid_str1); | 130 BluetoothUUID uuid1(uuid_str1); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 147 EXPECT_EQ(uuid2, char2->GetUUID()); | 147 EXPECT_EQ(uuid2, char2->GetUUID()); |
| 148 EXPECT_EQ(uuid2, char3->GetUUID()); | 148 EXPECT_EQ(uuid2, char3->GetUUID()); |
| 149 } | 149 } |
| 150 #endif // defined(OS_ANDROID) | 150 #endif // defined(OS_ANDROID) |
| 151 | 151 |
| 152 #if defined(OS_ANDROID) | 152 #if defined(OS_ANDROID) |
| 153 TEST_F(BluetoothGattCharacteristicTest, GetProperties) { | 153 TEST_F(BluetoothGattCharacteristicTest, GetProperties) { |
| 154 InitWithFakeAdapter(); | 154 InitWithFakeAdapter(); |
| 155 StartLowEnergyDiscoverySession(); | 155 StartLowEnergyDiscoverySession(); |
| 156 BluetoothDevice* device = DiscoverLowEnergyDevice(3); | 156 BluetoothDevice* device = DiscoverLowEnergyDevice(3); |
| 157 device->CreateGattConnection(GetGattConnectionCallback(), | 157 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
| 158 GetConnectErrorCallback()); | 158 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
| 159 SimulateGattConnection(device); | 159 SimulateGattConnection(device); |
| 160 std::vector<std::string> services; | 160 std::vector<std::string> services; |
| 161 std::string uuid("00000000-0000-1000-8000-00805f9b34fb"); | 161 std::string uuid("00000000-0000-1000-8000-00805f9b34fb"); |
| 162 services.push_back(uuid); | 162 services.push_back(uuid); |
| 163 SimulateGattServicesDiscovered(device, services); | 163 SimulateGattServicesDiscovered(device, services); |
| 164 BluetoothGattService* service = device->GetGattServices()[0]; | 164 BluetoothGattService* service = device->GetGattServices()[0]; |
| 165 | 165 |
| 166 // Create two characteristics with different properties: | 166 // Create two characteristics with different properties: |
| 167 SimulateGattCharacteristic(service, uuid, /* properties */ 0); | 167 SimulateGattCharacteristic(service, uuid, /* properties */ 0); |
| 168 SimulateGattCharacteristic(service, uuid, /* properties */ 7); | 168 SimulateGattCharacteristic(service, uuid, /* properties */ 7); |
| 169 | 169 |
| 170 // Read the properties. Because ordering is unknown swap as necessary. | 170 // Read the properties. Because ordering is unknown swap as necessary. |
| 171 int properties1 = service->GetCharacteristics()[0]->GetProperties(); | 171 int properties1 = service->GetCharacteristics()[0]->GetProperties(); |
| 172 int properties2 = service->GetCharacteristics()[1]->GetProperties(); | 172 int properties2 = service->GetCharacteristics()[1]->GetProperties(); |
| 173 if (properties2 == 0) | 173 if (properties2 == 0) |
| 174 std::swap(properties1, properties2); | 174 std::swap(properties1, properties2); |
| 175 EXPECT_EQ(0, properties1); | 175 EXPECT_EQ(0, properties1); |
| 176 EXPECT_EQ(7, properties2); | 176 EXPECT_EQ(7, properties2); |
| 177 } | 177 } |
| 178 #endif // defined(OS_ANDROID) | 178 #endif // defined(OS_ANDROID) |
| 179 | 179 |
| 180 #if defined(OS_ANDROID) | 180 #if defined(OS_ANDROID) |
| 181 // Tests ReadRemoteCharacteristic and GetValue with empty value buffer. | 181 // Tests ReadRemoteCharacteristic and GetValue with empty value buffer. |
| 182 TEST_F(BluetoothGattCharacteristicTest, ReadRemoteCharacteristic_Empty) { | 182 TEST_F(BluetoothGattCharacteristicTest, ReadRemoteCharacteristic_Empty) { |
| 183 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); | 183 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); |
| 184 | 184 |
| 185 characteristic1_->ReadRemoteCharacteristic(GetReadValueCallback(), | 185 characteristic1_->ReadRemoteCharacteristic( |
| 186 GetGattErrorCallback()); | 186 GetReadValueCallback(Call::EXPECTED), |
| 187 GetGattErrorCallback(Call::NOT_EXPECTED)); |
| 187 EXPECT_EQ(1, gatt_read_characteristic_attempts_); | 188 EXPECT_EQ(1, gatt_read_characteristic_attempts_); |
| 188 std::vector<uint8_t> empty_vector; | 189 std::vector<uint8_t> empty_vector; |
| 189 SimulateGattCharacteristicRead(characteristic1_, empty_vector); | 190 SimulateGattCharacteristicRead(characteristic1_, empty_vector); |
| 190 | 191 |
| 191 // Duplicate read reported from OS shouldn't cause a problem: | 192 // Duplicate read reported from OS shouldn't cause a problem: |
| 192 SimulateGattCharacteristicRead(characteristic1_, empty_vector); | 193 SimulateGattCharacteristicRead(characteristic1_, empty_vector); |
| 193 | 194 |
| 194 EXPECT_EQ(1, callback_count_); | 195 EXPECT_EQ(1, callback_count_); |
| 195 EXPECT_EQ(0, error_callback_count_); | 196 EXPECT_EQ(0, error_callback_count_); |
| 196 EXPECT_EQ(empty_vector, last_read_value_); | 197 EXPECT_EQ(empty_vector, last_read_value_); |
| 197 EXPECT_EQ(empty_vector, characteristic1_->GetValue()); | 198 EXPECT_EQ(empty_vector, characteristic1_->GetValue()); |
| 198 } | 199 } |
| 199 #endif // defined(OS_ANDROID) | 200 #endif // defined(OS_ANDROID) |
| 200 | 201 |
| 201 #if defined(OS_ANDROID) | 202 #if defined(OS_ANDROID) |
| 202 // Tests WriteRemoteCharacteristic with empty value buffer. | 203 // Tests WriteRemoteCharacteristic with empty value buffer. |
| 203 TEST_F(BluetoothGattCharacteristicTest, WriteRemoteCharacteristic_Empty) { | 204 TEST_F(BluetoothGattCharacteristicTest, WriteRemoteCharacteristic_Empty) { |
| 204 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); | 205 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); |
| 205 | 206 |
| 206 std::vector<uint8_t> empty_vector; | 207 std::vector<uint8_t> empty_vector; |
| 207 characteristic1_->WriteRemoteCharacteristic(empty_vector, GetCallback(), | 208 characteristic1_->WriteRemoteCharacteristic( |
| 208 GetGattErrorCallback()); | 209 empty_vector, GetCallback(Call::EXPECTED), |
| 210 GetGattErrorCallback(Call::NOT_EXPECTED)); |
| 209 EXPECT_EQ(1, gatt_write_characteristic_attempts_); | 211 EXPECT_EQ(1, gatt_write_characteristic_attempts_); |
| 210 SimulateGattCharacteristicWrite(characteristic1_); | 212 SimulateGattCharacteristicWrite(characteristic1_); |
| 211 | 213 |
| 212 EXPECT_EQ(1, callback_count_); | 214 EXPECT_EQ(1, callback_count_); |
| 213 EXPECT_EQ(0, error_callback_count_); | 215 EXPECT_EQ(0, error_callback_count_); |
| 214 EXPECT_EQ(empty_vector, last_write_value_); | 216 EXPECT_EQ(empty_vector, last_write_value_); |
| 215 } | 217 } |
| 216 #endif // defined(OS_ANDROID) | 218 #endif // defined(OS_ANDROID) |
| 217 | 219 |
| 218 #if defined(OS_ANDROID) | 220 #if defined(OS_ANDROID) |
| 219 // Tests ReadRemoteCharacteristic and GetValue with non-empty value buffer. | 221 // Tests ReadRemoteCharacteristic and GetValue with non-empty value buffer. |
| 220 TEST_F(BluetoothGattCharacteristicTest, ReadRemoteCharacteristic) { | 222 TEST_F(BluetoothGattCharacteristicTest, ReadRemoteCharacteristic) { |
| 221 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); | 223 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); |
| 222 | 224 |
| 223 characteristic1_->ReadRemoteCharacteristic(GetReadValueCallback(), | 225 characteristic1_->ReadRemoteCharacteristic( |
| 224 GetGattErrorCallback()); | 226 GetReadValueCallback(Call::EXPECTED), |
| 227 GetGattErrorCallback(Call::NOT_EXPECTED)); |
| 225 EXPECT_EQ(1, gatt_read_characteristic_attempts_); | 228 EXPECT_EQ(1, gatt_read_characteristic_attempts_); |
| 226 | 229 |
| 227 uint8_t values[] = {0, 1, 2, 3, 4, 0xf, 0xf0, 0xff}; | 230 uint8_t values[] = {0, 1, 2, 3, 4, 0xf, 0xf0, 0xff}; |
| 228 std::vector<uint8_t> test_vector(values, values + arraysize(values)); | 231 std::vector<uint8_t> test_vector(values, values + arraysize(values)); |
| 229 SimulateGattCharacteristicRead(characteristic1_, test_vector); | 232 SimulateGattCharacteristicRead(characteristic1_, test_vector); |
| 230 | 233 |
| 231 // Duplicate read reported from OS shouldn't cause a problem: | 234 // Duplicate read reported from OS shouldn't cause a problem: |
| 232 std::vector<uint8_t> empty_vector; | 235 std::vector<uint8_t> empty_vector; |
| 233 SimulateGattCharacteristicRead(characteristic1_, empty_vector); | 236 SimulateGattCharacteristicRead(characteristic1_, empty_vector); |
| 234 | 237 |
| 235 EXPECT_EQ(1, callback_count_); | 238 EXPECT_EQ(1, callback_count_); |
| 236 EXPECT_EQ(0, error_callback_count_); | 239 EXPECT_EQ(0, error_callback_count_); |
| 237 EXPECT_EQ(test_vector, last_read_value_); | 240 EXPECT_EQ(test_vector, last_read_value_); |
| 238 EXPECT_EQ(test_vector, characteristic1_->GetValue()); | 241 EXPECT_EQ(test_vector, characteristic1_->GetValue()); |
| 239 } | 242 } |
| 240 #endif // defined(OS_ANDROID) | 243 #endif // defined(OS_ANDROID) |
| 241 | 244 |
| 242 #if defined(OS_ANDROID) | 245 #if defined(OS_ANDROID) |
| 243 // Tests WriteRemoteCharacteristic with non-empty value buffer. | 246 // Tests WriteRemoteCharacteristic with non-empty value buffer. |
| 244 TEST_F(BluetoothGattCharacteristicTest, WriteRemoteCharacteristic) { | 247 TEST_F(BluetoothGattCharacteristicTest, WriteRemoteCharacteristic) { |
| 245 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); | 248 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); |
| 246 | 249 |
| 247 uint8_t values[] = {0, 1, 2, 3, 4, 0xf, 0xf0, 0xff}; | 250 uint8_t values[] = {0, 1, 2, 3, 4, 0xf, 0xf0, 0xff}; |
| 248 std::vector<uint8_t> test_vector(values, values + arraysize(values)); | 251 std::vector<uint8_t> test_vector(values, values + arraysize(values)); |
| 249 characteristic1_->WriteRemoteCharacteristic(test_vector, GetCallback(), | 252 characteristic1_->WriteRemoteCharacteristic( |
| 250 GetGattErrorCallback()); | 253 test_vector, GetCallback(Call::EXPECTED), |
| 254 GetGattErrorCallback(Call::NOT_EXPECTED)); |
| 251 EXPECT_EQ(1, gatt_write_characteristic_attempts_); | 255 EXPECT_EQ(1, gatt_write_characteristic_attempts_); |
| 252 | 256 |
| 253 SimulateGattCharacteristicWrite(characteristic1_); | 257 SimulateGattCharacteristicWrite(characteristic1_); |
| 254 | 258 |
| 255 EXPECT_EQ(1, callback_count_); | 259 EXPECT_EQ(1, callback_count_); |
| 256 EXPECT_EQ(0, error_callback_count_); | 260 EXPECT_EQ(0, error_callback_count_); |
| 257 EXPECT_EQ(test_vector, last_write_value_); | 261 EXPECT_EQ(test_vector, last_write_value_); |
| 258 } | 262 } |
| 259 #endif // defined(OS_ANDROID) | 263 #endif // defined(OS_ANDROID) |
| 260 | 264 |
| 261 #if defined(OS_ANDROID) | 265 #if defined(OS_ANDROID) |
| 262 // Tests ReadRemoteCharacteristic and GetValue multiple times. | 266 // Tests ReadRemoteCharacteristic and GetValue multiple times. |
| 263 TEST_F(BluetoothGattCharacteristicTest, ReadRemoteCharacteristic_Twice) { | 267 TEST_F(BluetoothGattCharacteristicTest, ReadRemoteCharacteristic_Twice) { |
| 264 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); | 268 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); |
| 265 | 269 |
| 266 characteristic1_->ReadRemoteCharacteristic(GetReadValueCallback(), | 270 characteristic1_->ReadRemoteCharacteristic( |
| 267 GetGattErrorCallback()); | 271 GetReadValueCallback(Call::EXPECTED), |
| 272 GetGattErrorCallback(Call::NOT_EXPECTED)); |
| 268 EXPECT_EQ(1, gatt_read_characteristic_attempts_); | 273 EXPECT_EQ(1, gatt_read_characteristic_attempts_); |
| 269 | 274 |
| 270 uint8_t values[] = {0, 1, 2, 3, 4, 0xf, 0xf0, 0xff}; | 275 uint8_t values[] = {0, 1, 2, 3, 4, 0xf, 0xf0, 0xff}; |
| 271 std::vector<uint8_t> test_vector(values, values + arraysize(values)); | 276 std::vector<uint8_t> test_vector(values, values + arraysize(values)); |
| 272 SimulateGattCharacteristicRead(characteristic1_, test_vector); | 277 SimulateGattCharacteristicRead(characteristic1_, test_vector); |
| 273 EXPECT_EQ(1, callback_count_); | 278 EXPECT_EQ(1, callback_count_); |
| 274 EXPECT_EQ(0, error_callback_count_); | 279 EXPECT_EQ(0, error_callback_count_); |
| 275 EXPECT_EQ(test_vector, last_read_value_); | 280 EXPECT_EQ(test_vector, last_read_value_); |
| 276 EXPECT_EQ(test_vector, characteristic1_->GetValue()); | 281 EXPECT_EQ(test_vector, characteristic1_->GetValue()); |
| 277 | 282 |
| 278 // Read again, with different value: | 283 // Read again, with different value: |
| 279 ResetEventCounts(); | 284 ResetEventCounts(); |
| 280 characteristic1_->ReadRemoteCharacteristic(GetReadValueCallback(), | 285 characteristic1_->ReadRemoteCharacteristic( |
| 281 GetGattErrorCallback()); | 286 GetReadValueCallback(Call::EXPECTED), |
| 287 GetGattErrorCallback(Call::NOT_EXPECTED)); |
| 282 EXPECT_EQ(1, gatt_read_characteristic_attempts_); | 288 EXPECT_EQ(1, gatt_read_characteristic_attempts_); |
| 283 std::vector<uint8_t> empty_vector; | 289 std::vector<uint8_t> empty_vector; |
| 284 SimulateGattCharacteristicRead(characteristic1_, empty_vector); | 290 SimulateGattCharacteristicRead(characteristic1_, empty_vector); |
| 285 EXPECT_EQ(1, callback_count_); | 291 EXPECT_EQ(1, callback_count_); |
| 286 EXPECT_EQ(0, error_callback_count_); | 292 EXPECT_EQ(0, error_callback_count_); |
| 287 EXPECT_EQ(empty_vector, last_read_value_); | 293 EXPECT_EQ(empty_vector, last_read_value_); |
| 288 EXPECT_EQ(empty_vector, characteristic1_->GetValue()); | 294 EXPECT_EQ(empty_vector, characteristic1_->GetValue()); |
| 289 } | 295 } |
| 290 #endif // defined(OS_ANDROID) | 296 #endif // defined(OS_ANDROID) |
| 291 | 297 |
| 292 #if defined(OS_ANDROID) | 298 #if defined(OS_ANDROID) |
| 293 // Tests WriteRemoteCharacteristic multiple times. | 299 // Tests WriteRemoteCharacteristic multiple times. |
| 294 TEST_F(BluetoothGattCharacteristicTest, WriteRemoteCharacteristic_Twice) { | 300 TEST_F(BluetoothGattCharacteristicTest, WriteRemoteCharacteristic_Twice) { |
| 295 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); | 301 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); |
| 296 | 302 |
| 297 uint8_t values[] = {0, 1, 2, 3, 4, 0xf, 0xf0, 0xff}; | 303 uint8_t values[] = {0, 1, 2, 3, 4, 0xf, 0xf0, 0xff}; |
| 298 std::vector<uint8_t> test_vector(values, values + arraysize(values)); | 304 std::vector<uint8_t> test_vector(values, values + arraysize(values)); |
| 299 characteristic1_->WriteRemoteCharacteristic(test_vector, GetCallback(), | 305 characteristic1_->WriteRemoteCharacteristic( |
| 300 GetGattErrorCallback()); | 306 test_vector, GetCallback(Call::EXPECTED), |
| 307 GetGattErrorCallback(Call::NOT_EXPECTED)); |
| 301 EXPECT_EQ(1, gatt_write_characteristic_attempts_); | 308 EXPECT_EQ(1, gatt_write_characteristic_attempts_); |
| 302 | 309 |
| 303 SimulateGattCharacteristicWrite(characteristic1_); | 310 SimulateGattCharacteristicWrite(characteristic1_); |
| 304 EXPECT_EQ(1, callback_count_); | 311 EXPECT_EQ(1, callback_count_); |
| 305 EXPECT_EQ(0, error_callback_count_); | 312 EXPECT_EQ(0, error_callback_count_); |
| 306 EXPECT_EQ(test_vector, last_write_value_); | 313 EXPECT_EQ(test_vector, last_write_value_); |
| 307 | 314 |
| 308 // Write again, with different value: | 315 // Write again, with different value: |
| 309 ResetEventCounts(); | 316 ResetEventCounts(); |
| 310 std::vector<uint8_t> empty_vector; | 317 std::vector<uint8_t> empty_vector; |
| 311 characteristic1_->WriteRemoteCharacteristic(empty_vector, GetCallback(), | 318 characteristic1_->WriteRemoteCharacteristic( |
| 312 GetGattErrorCallback()); | 319 empty_vector, GetCallback(Call::EXPECTED), |
| 320 GetGattErrorCallback(Call::NOT_EXPECTED)); |
| 313 EXPECT_EQ(1, gatt_write_characteristic_attempts_); | 321 EXPECT_EQ(1, gatt_write_characteristic_attempts_); |
| 314 SimulateGattCharacteristicWrite(characteristic1_); | 322 SimulateGattCharacteristicWrite(characteristic1_); |
| 315 EXPECT_EQ(1, callback_count_); | 323 EXPECT_EQ(1, callback_count_); |
| 316 EXPECT_EQ(0, error_callback_count_); | 324 EXPECT_EQ(0, error_callback_count_); |
| 317 EXPECT_EQ(empty_vector, last_write_value_); | 325 EXPECT_EQ(empty_vector, last_write_value_); |
| 318 } | 326 } |
| 319 #endif // defined(OS_ANDROID) | 327 #endif // defined(OS_ANDROID) |
| 320 | 328 |
| 321 #if defined(OS_ANDROID) | 329 #if defined(OS_ANDROID) |
| 322 // Tests ReadRemoteCharacteristic on two characteristics. | 330 // Tests ReadRemoteCharacteristic on two characteristics. |
| 323 TEST_F(BluetoothGattCharacteristicTest, | 331 TEST_F(BluetoothGattCharacteristicTest, |
| 324 ReadRemoteCharacteristic_MultipleCharacteristics) { | 332 ReadRemoteCharacteristic_MultipleCharacteristics) { |
| 325 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); | 333 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); |
| 326 | 334 |
| 327 characteristic1_->ReadRemoteCharacteristic(GetReadValueCallback(), | 335 characteristic1_->ReadRemoteCharacteristic( |
| 328 GetGattErrorCallback()); | 336 GetReadValueCallback(Call::EXPECTED), |
| 329 characteristic2_->ReadRemoteCharacteristic(GetReadValueCallback(), | 337 GetGattErrorCallback(Call::NOT_EXPECTED)); |
| 330 GetGattErrorCallback()); | 338 characteristic2_->ReadRemoteCharacteristic( |
| 339 GetReadValueCallback(Call::EXPECTED), |
| 340 GetGattErrorCallback(Call::NOT_EXPECTED)); |
| 331 EXPECT_EQ(2, gatt_read_characteristic_attempts_); | 341 EXPECT_EQ(2, gatt_read_characteristic_attempts_); |
| 332 EXPECT_EQ(0, callback_count_); | 342 EXPECT_EQ(0, callback_count_); |
| 333 EXPECT_EQ(0, error_callback_count_); | 343 EXPECT_EQ(0, error_callback_count_); |
| 334 | 344 |
| 335 std::vector<uint8_t> test_vector1; | 345 std::vector<uint8_t> test_vector1; |
| 336 test_vector1.push_back(111); | 346 test_vector1.push_back(111); |
| 337 SimulateGattCharacteristicRead(characteristic1_, test_vector1); | 347 SimulateGattCharacteristicRead(characteristic1_, test_vector1); |
| 338 EXPECT_EQ(test_vector1, last_read_value_); | 348 EXPECT_EQ(test_vector1, last_read_value_); |
| 339 | 349 |
| 340 std::vector<uint8_t> test_vector2; | 350 std::vector<uint8_t> test_vector2; |
| 341 test_vector2.push_back(222); | 351 test_vector2.push_back(222); |
| 342 SimulateGattCharacteristicRead(characteristic2_, test_vector2); | 352 SimulateGattCharacteristicRead(characteristic2_, test_vector2); |
| 343 EXPECT_EQ(test_vector2, last_read_value_); | 353 EXPECT_EQ(test_vector2, last_read_value_); |
| 344 | 354 |
| 345 EXPECT_EQ(2, callback_count_); | 355 EXPECT_EQ(2, callback_count_); |
| 346 EXPECT_EQ(0, error_callback_count_); | 356 EXPECT_EQ(0, error_callback_count_); |
| 347 EXPECT_EQ(test_vector1, characteristic1_->GetValue()); | 357 EXPECT_EQ(test_vector1, characteristic1_->GetValue()); |
| 348 EXPECT_EQ(test_vector2, characteristic2_->GetValue()); | 358 EXPECT_EQ(test_vector2, characteristic2_->GetValue()); |
| 349 } | 359 } |
| 350 #endif // defined(OS_ANDROID) | 360 #endif // defined(OS_ANDROID) |
| 351 | 361 |
| 352 #if defined(OS_ANDROID) | 362 #if defined(OS_ANDROID) |
| 353 // Tests WriteRemoteCharacteristic on two characteristics. | 363 // Tests WriteRemoteCharacteristic on two characteristics. |
| 354 TEST_F(BluetoothGattCharacteristicTest, | 364 TEST_F(BluetoothGattCharacteristicTest, |
| 355 WriteRemoteCharacteristic_MultipleCharacteristics) { | 365 WriteRemoteCharacteristic_MultipleCharacteristics) { |
| 356 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); | 366 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); |
| 357 | 367 |
| 358 std::vector<uint8_t> test_vector1; | 368 std::vector<uint8_t> test_vector1; |
| 359 test_vector1.push_back(111); | 369 test_vector1.push_back(111); |
| 360 characteristic1_->WriteRemoteCharacteristic(test_vector1, GetCallback(), | 370 characteristic1_->WriteRemoteCharacteristic( |
| 361 GetGattErrorCallback()); | 371 test_vector1, GetCallback(Call::EXPECTED), |
| 372 GetGattErrorCallback(Call::NOT_EXPECTED)); |
| 362 EXPECT_EQ(test_vector1, last_write_value_); | 373 EXPECT_EQ(test_vector1, last_write_value_); |
| 363 | 374 |
| 364 std::vector<uint8_t> test_vector2; | 375 std::vector<uint8_t> test_vector2; |
| 365 test_vector2.push_back(222); | 376 test_vector2.push_back(222); |
| 366 characteristic2_->WriteRemoteCharacteristic(test_vector2, GetCallback(), | 377 characteristic2_->WriteRemoteCharacteristic( |
| 367 GetGattErrorCallback()); | 378 test_vector2, GetCallback(Call::EXPECTED), |
| 379 GetGattErrorCallback(Call::NOT_EXPECTED)); |
| 368 EXPECT_EQ(test_vector2, last_write_value_); | 380 EXPECT_EQ(test_vector2, last_write_value_); |
| 369 | 381 |
| 370 EXPECT_EQ(2, gatt_write_characteristic_attempts_); | 382 EXPECT_EQ(2, gatt_write_characteristic_attempts_); |
| 371 EXPECT_EQ(0, callback_count_); | 383 EXPECT_EQ(0, callback_count_); |
| 372 EXPECT_EQ(0, error_callback_count_); | 384 EXPECT_EQ(0, error_callback_count_); |
| 373 | 385 |
| 374 SimulateGattCharacteristicWrite(characteristic1_); | 386 SimulateGattCharacteristicWrite(characteristic1_); |
| 375 SimulateGattCharacteristicWrite(characteristic2_); | 387 SimulateGattCharacteristicWrite(characteristic2_); |
| 376 | 388 |
| 377 EXPECT_EQ(2, callback_count_); | 389 EXPECT_EQ(2, callback_count_); |
| 378 EXPECT_EQ(0, error_callback_count_); | 390 EXPECT_EQ(0, error_callback_count_); |
| 379 } | 391 } |
| 380 #endif // defined(OS_ANDROID) | 392 #endif // defined(OS_ANDROID) |
| 381 | 393 |
| 382 #if defined(OS_ANDROID) | 394 #if defined(OS_ANDROID) |
| 383 // Tests ReadRemoteCharacteristic asynchronous error. | 395 // Tests ReadRemoteCharacteristic asynchronous error. |
| 384 TEST_F(BluetoothGattCharacteristicTest, ReadError) { | 396 TEST_F(BluetoothGattCharacteristicTest, ReadError) { |
| 385 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); | 397 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); |
| 386 | 398 |
| 387 characteristic1_->ReadRemoteCharacteristic(GetReadValueCallback(), | 399 characteristic1_->ReadRemoteCharacteristic( |
| 388 GetGattErrorCallback()); | 400 GetReadValueCallback(Call::NOT_EXPECTED), |
| 401 GetGattErrorCallback(Call::EXPECTED)); |
| 389 SimulateGattCharacteristicReadError( | 402 SimulateGattCharacteristicReadError( |
| 390 characteristic1_, BluetoothGattService::GATT_ERROR_INVALID_LENGTH); | 403 characteristic1_, BluetoothGattService::GATT_ERROR_INVALID_LENGTH); |
| 391 SimulateGattCharacteristicReadError(characteristic1_, | 404 SimulateGattCharacteristicReadError(characteristic1_, |
| 392 BluetoothGattService::GATT_ERROR_FAILED); | 405 BluetoothGattService::GATT_ERROR_FAILED); |
| 393 EXPECT_EQ(0, callback_count_); | 406 EXPECT_EQ(0, callback_count_); |
| 394 EXPECT_EQ(1, error_callback_count_); | 407 EXPECT_EQ(1, error_callback_count_); |
| 395 EXPECT_EQ(BluetoothGattService::GATT_ERROR_INVALID_LENGTH, | 408 EXPECT_EQ(BluetoothGattService::GATT_ERROR_INVALID_LENGTH, |
| 396 last_gatt_error_code_); | 409 last_gatt_error_code_); |
| 397 } | 410 } |
| 398 #endif // defined(OS_ANDROID) | 411 #endif // defined(OS_ANDROID) |
| 399 | 412 |
| 400 #if defined(OS_ANDROID) | 413 #if defined(OS_ANDROID) |
| 401 // Tests WriteRemoteCharacteristic asynchronous error. | 414 // Tests WriteRemoteCharacteristic asynchronous error. |
| 402 TEST_F(BluetoothGattCharacteristicTest, WriteError) { | 415 TEST_F(BluetoothGattCharacteristicTest, WriteError) { |
| 403 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); | 416 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); |
| 404 | 417 |
| 405 std::vector<uint8_t> empty_vector; | 418 std::vector<uint8_t> empty_vector; |
| 406 characteristic1_->WriteRemoteCharacteristic(empty_vector, GetCallback(), | 419 characteristic1_->WriteRemoteCharacteristic( |
| 407 GetGattErrorCallback()); | 420 empty_vector, GetCallback(Call::NOT_EXPECTED), |
| 421 GetGattErrorCallback(Call::EXPECTED)); |
| 408 SimulateGattCharacteristicWriteError( | 422 SimulateGattCharacteristicWriteError( |
| 409 characteristic1_, BluetoothGattService::GATT_ERROR_INVALID_LENGTH); | 423 characteristic1_, BluetoothGattService::GATT_ERROR_INVALID_LENGTH); |
| 410 SimulateGattCharacteristicWriteError(characteristic1_, | 424 SimulateGattCharacteristicWriteError(characteristic1_, |
| 411 BluetoothGattService::GATT_ERROR_FAILED); | 425 BluetoothGattService::GATT_ERROR_FAILED); |
| 412 EXPECT_EQ(0, callback_count_); | 426 EXPECT_EQ(0, callback_count_); |
| 413 EXPECT_EQ(1, error_callback_count_); | 427 EXPECT_EQ(1, error_callback_count_); |
| 414 EXPECT_EQ(BluetoothGattService::GATT_ERROR_INVALID_LENGTH, | 428 EXPECT_EQ(BluetoothGattService::GATT_ERROR_INVALID_LENGTH, |
| 415 last_gatt_error_code_); | 429 last_gatt_error_code_); |
| 416 } | 430 } |
| 417 #endif // defined(OS_ANDROID) | 431 #endif // defined(OS_ANDROID) |
| 418 | 432 |
| 419 #if defined(OS_ANDROID) | 433 #if defined(OS_ANDROID) |
| 420 // Tests ReadRemoteCharacteristic synchronous error. | 434 // Tests ReadRemoteCharacteristic synchronous error. |
| 421 TEST_F(BluetoothGattCharacteristicTest, ReadSynchronousError) { | 435 TEST_F(BluetoothGattCharacteristicTest, ReadSynchronousError) { |
| 422 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); | 436 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); |
| 423 | 437 |
| 424 SimulateGattCharacteristicReadWillFailSynchronouslyOnce(characteristic1_); | 438 SimulateGattCharacteristicReadWillFailSynchronouslyOnce(characteristic1_); |
| 425 characteristic1_->ReadRemoteCharacteristic(GetReadValueCallback(), | 439 characteristic1_->ReadRemoteCharacteristic( |
| 426 GetGattErrorCallback()); | 440 GetReadValueCallback(Call::NOT_EXPECTED), |
| 441 GetGattErrorCallback(Call::EXPECTED)); |
| 427 EXPECT_EQ(0, gatt_read_characteristic_attempts_); | 442 EXPECT_EQ(0, gatt_read_characteristic_attempts_); |
| 428 base::RunLoop().RunUntilIdle(); | 443 base::RunLoop().RunUntilIdle(); |
| 429 EXPECT_EQ(0, callback_count_); | 444 EXPECT_EQ(0, callback_count_); |
| 430 EXPECT_EQ(1, error_callback_count_); | 445 EXPECT_EQ(1, error_callback_count_); |
| 431 EXPECT_EQ(BluetoothGattService::GATT_ERROR_FAILED, last_gatt_error_code_); | 446 EXPECT_EQ(BluetoothGattService::GATT_ERROR_FAILED, last_gatt_error_code_); |
| 432 | 447 |
| 433 // After failing once, can succeed: | 448 // After failing once, can succeed: |
| 434 ResetEventCounts(); | 449 ResetEventCounts(); |
| 435 characteristic1_->ReadRemoteCharacteristic(GetReadValueCallback(), | 450 characteristic1_->ReadRemoteCharacteristic( |
| 436 GetGattErrorCallback()); | 451 GetReadValueCallback(Call::EXPECTED), |
| 452 GetGattErrorCallback(Call::NOT_EXPECTED)); |
| 437 EXPECT_EQ(1, gatt_read_characteristic_attempts_); | 453 EXPECT_EQ(1, gatt_read_characteristic_attempts_); |
| 438 std::vector<uint8_t> empty_vector; | 454 std::vector<uint8_t> empty_vector; |
| 439 SimulateGattCharacteristicRead(characteristic1_, empty_vector); | 455 SimulateGattCharacteristicRead(characteristic1_, empty_vector); |
| 440 EXPECT_EQ(1, callback_count_); | 456 EXPECT_EQ(1, callback_count_); |
| 441 EXPECT_EQ(0, error_callback_count_); | 457 EXPECT_EQ(0, error_callback_count_); |
| 442 } | 458 } |
| 443 #endif // defined(OS_ANDROID) | 459 #endif // defined(OS_ANDROID) |
| 444 | 460 |
| 445 #if defined(OS_ANDROID) | 461 #if defined(OS_ANDROID) |
| 446 // Tests WriteRemoteCharacteristic synchronous error. | 462 // Tests WriteRemoteCharacteristic synchronous error. |
| 447 TEST_F(BluetoothGattCharacteristicTest, WriteSynchronousError) { | 463 TEST_F(BluetoothGattCharacteristicTest, WriteSynchronousError) { |
| 448 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); | 464 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); |
| 449 | 465 |
| 450 SimulateGattCharacteristicWriteWillFailSynchronouslyOnce(characteristic1_); | 466 SimulateGattCharacteristicWriteWillFailSynchronouslyOnce(characteristic1_); |
| 451 std::vector<uint8_t> empty_vector; | 467 std::vector<uint8_t> empty_vector; |
| 452 characteristic1_->WriteRemoteCharacteristic(empty_vector, GetCallback(), | 468 characteristic1_->WriteRemoteCharacteristic( |
| 453 GetGattErrorCallback()); | 469 empty_vector, GetCallback(Call::NOT_EXPECTED), |
| 470 GetGattErrorCallback(Call::EXPECTED)); |
| 454 EXPECT_EQ(0, gatt_write_characteristic_attempts_); | 471 EXPECT_EQ(0, gatt_write_characteristic_attempts_); |
| 455 base::RunLoop().RunUntilIdle(); | 472 base::RunLoop().RunUntilIdle(); |
| 456 EXPECT_EQ(0, callback_count_); | 473 EXPECT_EQ(0, callback_count_); |
| 457 EXPECT_EQ(1, error_callback_count_); | 474 EXPECT_EQ(1, error_callback_count_); |
| 458 EXPECT_EQ(BluetoothGattService::GATT_ERROR_FAILED, last_gatt_error_code_); | 475 EXPECT_EQ(BluetoothGattService::GATT_ERROR_FAILED, last_gatt_error_code_); |
| 459 | 476 |
| 460 // After failing once, can succeed: | 477 // After failing once, can succeed: |
| 461 ResetEventCounts(); | 478 ResetEventCounts(); |
| 462 characteristic1_->WriteRemoteCharacteristic(empty_vector, GetCallback(), | 479 characteristic1_->WriteRemoteCharacteristic( |
| 463 GetGattErrorCallback()); | 480 empty_vector, GetCallback(Call::EXPECTED), |
| 481 GetGattErrorCallback(Call::NOT_EXPECTED)); |
| 464 EXPECT_EQ(1, gatt_write_characteristic_attempts_); | 482 EXPECT_EQ(1, gatt_write_characteristic_attempts_); |
| 465 SimulateGattCharacteristicWrite(characteristic1_); | 483 SimulateGattCharacteristicWrite(characteristic1_); |
| 466 EXPECT_EQ(1, callback_count_); | 484 EXPECT_EQ(1, callback_count_); |
| 467 EXPECT_EQ(0, error_callback_count_); | 485 EXPECT_EQ(0, error_callback_count_); |
| 468 } | 486 } |
| 469 #endif // defined(OS_ANDROID) | 487 #endif // defined(OS_ANDROID) |
| 470 | 488 |
| 471 #if defined(OS_ANDROID) | 489 #if defined(OS_ANDROID) |
| 472 // Tests ReadRemoteCharacteristic error with a pending read operation. | 490 // Tests ReadRemoteCharacteristic error with a pending read operation. |
| 473 TEST_F(BluetoothGattCharacteristicTest, ReadRemoteCharacteristic_ReadPending) { | 491 TEST_F(BluetoothGattCharacteristicTest, ReadRemoteCharacteristic_ReadPending) { |
| 474 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); | 492 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); |
| 475 | 493 |
| 476 characteristic1_->ReadRemoteCharacteristic(GetReadValueCallback(), | 494 characteristic1_->ReadRemoteCharacteristic( |
| 477 GetGattErrorCallback()); | 495 GetReadValueCallback(Call::EXPECTED), |
| 478 characteristic1_->ReadRemoteCharacteristic(GetReadValueCallback(), | 496 GetGattErrorCallback(Call::NOT_EXPECTED)); |
| 479 GetGattErrorCallback()); | 497 characteristic1_->ReadRemoteCharacteristic( |
| 498 GetReadValueCallback(Call::NOT_EXPECTED), |
| 499 GetGattErrorCallback(Call::EXPECTED)); |
| 500 |
| 501 base::RunLoop().RunUntilIdle(); |
| 502 |
| 480 EXPECT_EQ(0, callback_count_); | 503 EXPECT_EQ(0, callback_count_); |
| 481 EXPECT_EQ(1, error_callback_count_); | 504 EXPECT_EQ(1, error_callback_count_); |
| 482 EXPECT_EQ(BluetoothGattService::GATT_ERROR_IN_PROGRESS, | 505 EXPECT_EQ(BluetoothGattService::GATT_ERROR_IN_PROGRESS, |
| 483 last_gatt_error_code_); | 506 last_gatt_error_code_); |
| 484 | 507 |
| 485 // Initial read should still succeed: | 508 // Initial read should still succeed: |
| 486 ResetEventCounts(); | 509 ResetEventCounts(); |
| 487 std::vector<uint8_t> empty_vector; | 510 std::vector<uint8_t> empty_vector; |
| 488 SimulateGattCharacteristicRead(characteristic1_, empty_vector); | 511 SimulateGattCharacteristicRead(characteristic1_, empty_vector); |
| 489 EXPECT_EQ(1, callback_count_); | 512 EXPECT_EQ(1, callback_count_); |
| 490 EXPECT_EQ(0, error_callback_count_); | 513 EXPECT_EQ(0, error_callback_count_); |
| 491 } | 514 } |
| 492 #endif // defined(OS_ANDROID) | 515 #endif // defined(OS_ANDROID) |
| 493 | 516 |
| 494 #if defined(OS_ANDROID) | 517 #if defined(OS_ANDROID) |
| 495 // Tests WriteRemoteCharacteristic error with a pending write operation. | 518 // Tests WriteRemoteCharacteristic error with a pending write operation. |
| 496 TEST_F(BluetoothGattCharacteristicTest, | 519 TEST_F(BluetoothGattCharacteristicTest, |
| 497 WriteRemoteCharacteristic_WritePending) { | 520 WriteRemoteCharacteristic_WritePending) { |
| 498 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); | 521 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); |
| 499 | 522 |
| 500 std::vector<uint8_t> empty_vector; | 523 std::vector<uint8_t> empty_vector; |
| 501 characteristic1_->WriteRemoteCharacteristic(empty_vector, GetCallback(), | 524 characteristic1_->WriteRemoteCharacteristic( |
| 502 GetGattErrorCallback()); | 525 empty_vector, GetCallback(Call::EXPECTED), |
| 503 characteristic1_->WriteRemoteCharacteristic(empty_vector, GetCallback(), | 526 GetGattErrorCallback(Call::NOT_EXPECTED)); |
| 504 GetGattErrorCallback()); | 527 characteristic1_->WriteRemoteCharacteristic( |
| 528 empty_vector, GetCallback(Call::NOT_EXPECTED), |
| 529 GetGattErrorCallback(Call::EXPECTED)); |
| 530 |
| 531 base::RunLoop().RunUntilIdle(); |
| 532 |
| 505 EXPECT_EQ(0, callback_count_); | 533 EXPECT_EQ(0, callback_count_); |
| 506 EXPECT_EQ(1, error_callback_count_); | 534 EXPECT_EQ(1, error_callback_count_); |
| 507 EXPECT_EQ(BluetoothGattService::GATT_ERROR_IN_PROGRESS, | 535 EXPECT_EQ(BluetoothGattService::GATT_ERROR_IN_PROGRESS, |
| 508 last_gatt_error_code_); | 536 last_gatt_error_code_); |
| 509 | 537 |
| 510 // Initial write should still succeed: | 538 // Initial write should still succeed: |
| 511 ResetEventCounts(); | 539 ResetEventCounts(); |
| 512 SimulateGattCharacteristicWrite(characteristic1_); | 540 SimulateGattCharacteristicWrite(characteristic1_); |
| 513 EXPECT_EQ(1, callback_count_); | 541 EXPECT_EQ(1, callback_count_); |
| 514 EXPECT_EQ(0, error_callback_count_); | 542 EXPECT_EQ(0, error_callback_count_); |
| 515 } | 543 } |
| 516 #endif // defined(OS_ANDROID) | 544 #endif // defined(OS_ANDROID) |
| 517 | 545 |
| 518 #if defined(OS_ANDROID) | 546 #if defined(OS_ANDROID) |
| 519 // Tests ReadRemoteCharacteristic error with a pending write operation. | 547 // Tests ReadRemoteCharacteristic error with a pending write operation. |
| 520 TEST_F(BluetoothGattCharacteristicTest, ReadRemoteCharacteristic_WritePending) { | 548 TEST_F(BluetoothGattCharacteristicTest, ReadRemoteCharacteristic_WritePending) { |
| 521 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); | 549 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); |
| 522 | 550 |
| 523 std::vector<uint8_t> empty_vector; | 551 std::vector<uint8_t> empty_vector; |
| 524 characteristic1_->WriteRemoteCharacteristic(empty_vector, GetCallback(), | 552 characteristic1_->WriteRemoteCharacteristic( |
| 525 GetGattErrorCallback()); | 553 empty_vector, GetCallback(Call::EXPECTED), |
| 526 characteristic1_->ReadRemoteCharacteristic(GetReadValueCallback(), | 554 GetGattErrorCallback(Call::NOT_EXPECTED)); |
| 527 GetGattErrorCallback()); | 555 characteristic1_->ReadRemoteCharacteristic( |
| 556 GetReadValueCallback(Call::NOT_EXPECTED), |
| 557 GetGattErrorCallback(Call::EXPECTED)); |
| 558 |
| 559 base::RunLoop().RunUntilIdle(); |
| 560 |
| 528 EXPECT_EQ(0, callback_count_); | 561 EXPECT_EQ(0, callback_count_); |
| 529 EXPECT_EQ(1, error_callback_count_); | 562 EXPECT_EQ(1, error_callback_count_); |
| 530 EXPECT_EQ(BluetoothGattService::GATT_ERROR_IN_PROGRESS, | 563 EXPECT_EQ(BluetoothGattService::GATT_ERROR_IN_PROGRESS, |
| 531 last_gatt_error_code_); | 564 last_gatt_error_code_); |
| 532 | 565 |
| 533 // Initial write should still succeed: | 566 // Initial write should still succeed: |
| 534 ResetEventCounts(); | 567 ResetEventCounts(); |
| 535 SimulateGattCharacteristicWrite(characteristic1_); | 568 SimulateGattCharacteristicWrite(characteristic1_); |
| 536 EXPECT_EQ(1, callback_count_); | 569 EXPECT_EQ(1, callback_count_); |
| 537 EXPECT_EQ(0, error_callback_count_); | 570 EXPECT_EQ(0, error_callback_count_); |
| 538 } | 571 } |
| 539 #endif // defined(OS_ANDROID) | 572 #endif // defined(OS_ANDROID) |
| 540 | 573 |
| 541 #if defined(OS_ANDROID) | 574 #if defined(OS_ANDROID) |
| 542 // Tests WriteRemoteCharacteristic error with a pending Read operation. | 575 // Tests WriteRemoteCharacteristic error with a pending Read operation. |
| 543 TEST_F(BluetoothGattCharacteristicTest, WriteRemoteCharacteristic_ReadPending) { | 576 TEST_F(BluetoothGattCharacteristicTest, WriteRemoteCharacteristic_ReadPending) { |
| 544 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); | 577 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); |
| 545 | 578 |
| 546 std::vector<uint8_t> empty_vector; | 579 std::vector<uint8_t> empty_vector; |
| 547 characteristic1_->ReadRemoteCharacteristic(GetReadValueCallback(), | 580 characteristic1_->ReadRemoteCharacteristic( |
| 548 GetGattErrorCallback()); | 581 GetReadValueCallback(Call::EXPECTED), |
| 549 characteristic1_->WriteRemoteCharacteristic(empty_vector, GetCallback(), | 582 GetGattErrorCallback(Call::NOT_EXPECTED)); |
| 550 GetGattErrorCallback()); | 583 characteristic1_->WriteRemoteCharacteristic( |
| 584 empty_vector, GetCallback(Call::NOT_EXPECTED), |
| 585 GetGattErrorCallback(Call::EXPECTED)); |
| 586 base::RunLoop().RunUntilIdle(); |
| 587 |
| 551 EXPECT_EQ(0, callback_count_); | 588 EXPECT_EQ(0, callback_count_); |
| 552 EXPECT_EQ(1, error_callback_count_); | 589 EXPECT_EQ(1, error_callback_count_); |
| 553 EXPECT_EQ(BluetoothGattService::GATT_ERROR_IN_PROGRESS, | 590 EXPECT_EQ(BluetoothGattService::GATT_ERROR_IN_PROGRESS, |
| 554 last_gatt_error_code_); | 591 last_gatt_error_code_); |
| 555 | 592 |
| 556 // Initial read should still succeed: | 593 // Initial read should still succeed: |
| 557 ResetEventCounts(); | 594 ResetEventCounts(); |
| 558 SimulateGattCharacteristicRead(characteristic1_, empty_vector); | 595 SimulateGattCharacteristicRead(characteristic1_, empty_vector); |
| 559 EXPECT_EQ(1, callback_count_); | 596 EXPECT_EQ(1, callback_count_); |
| 560 EXPECT_EQ(0, error_callback_count_); | 597 EXPECT_EQ(0, error_callback_count_); |
| 561 } | 598 } |
| 562 #endif // defined(OS_ANDROID) | 599 #endif // defined(OS_ANDROID) |
| 563 | 600 |
| 564 #if defined(OS_ANDROID) | 601 #if defined(OS_ANDROID) |
| 565 // Tests StartNotifySession success. | 602 // Tests StartNotifySession success. |
| 566 TEST_F(BluetoothGattCharacteristicTest, StartNotifySession) { | 603 TEST_F(BluetoothGattCharacteristicTest, StartNotifySession) { |
| 567 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); | 604 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); |
| 568 | 605 |
| 569 characteristic1_->StartNotifySession(GetNotifyCallback(), | 606 characteristic1_->StartNotifySession( |
| 570 GetGattErrorCallback()); | 607 GetNotifyCallback(Call::EXPECTED), |
| 608 GetGattErrorCallback(Call::NOT_EXPECTED)); |
| 571 EXPECT_EQ(1, gatt_notify_characteristic_attempts_); | 609 EXPECT_EQ(1, gatt_notify_characteristic_attempts_); |
| 572 EXPECT_EQ(0, callback_count_); | 610 EXPECT_EQ(0, callback_count_); |
| 573 SimulateGattNotifySessionStarted(characteristic1_); | 611 SimulateGattNotifySessionStarted(characteristic1_); |
| 574 EXPECT_EQ(1, callback_count_); | 612 EXPECT_EQ(1, callback_count_); |
| 575 EXPECT_EQ(0, error_callback_count_); | 613 EXPECT_EQ(0, error_callback_count_); |
| 576 ASSERT_EQ(1u, notify_sessions_.size()); | 614 ASSERT_EQ(1u, notify_sessions_.size()); |
| 577 ASSERT_TRUE(notify_sessions_[0]); | 615 ASSERT_TRUE(notify_sessions_[0]); |
| 578 EXPECT_EQ(characteristic1_->GetIdentifier(), | 616 EXPECT_EQ(characteristic1_->GetIdentifier(), |
| 579 notify_sessions_[0]->GetCharacteristicIdentifier()); | 617 notify_sessions_[0]->GetCharacteristicIdentifier()); |
| 580 EXPECT_TRUE(notify_sessions_[0]->IsActive()); | 618 EXPECT_TRUE(notify_sessions_[0]->IsActive()); |
| 581 } | 619 } |
| 582 #endif // defined(OS_ANDROID) | 620 #endif // defined(OS_ANDROID) |
| 583 | 621 |
| 584 #if defined(OS_ANDROID) | 622 #if defined(OS_ANDROID) |
| 585 // Tests StartNotifySession synchronous failure. | 623 // Tests StartNotifySession synchronous failure. |
| 586 TEST_F(BluetoothGattCharacteristicTest, StartNotifySession_SynchronousError) { | 624 TEST_F(BluetoothGattCharacteristicTest, StartNotifySession_SynchronousError) { |
| 587 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); | 625 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); |
| 588 | 626 |
| 589 SimulateGattCharacteristicSetNotifyWillFailSynchronouslyOnce( | 627 SimulateGattCharacteristicSetNotifyWillFailSynchronouslyOnce( |
| 590 characteristic1_); | 628 characteristic1_); |
| 591 characteristic1_->StartNotifySession(GetNotifyCallback(), | 629 characteristic1_->StartNotifySession(GetNotifyCallback(Call::NOT_EXPECTED), |
| 592 GetGattErrorCallback()); | 630 GetGattErrorCallback(Call::EXPECTED)); |
| 593 EXPECT_EQ(0, error_callback_count_); | 631 EXPECT_EQ(0, error_callback_count_); |
| 594 base::RunLoop().RunUntilIdle(); | 632 base::RunLoop().RunUntilIdle(); |
| 595 EXPECT_EQ(0, gatt_notify_characteristic_attempts_); | 633 EXPECT_EQ(0, gatt_notify_characteristic_attempts_); |
| 596 EXPECT_EQ(0, callback_count_); | 634 EXPECT_EQ(0, callback_count_); |
| 597 EXPECT_EQ(1, error_callback_count_); | 635 EXPECT_EQ(1, error_callback_count_); |
| 598 ASSERT_EQ(0u, notify_sessions_.size()); | 636 ASSERT_EQ(0u, notify_sessions_.size()); |
| 599 } | 637 } |
| 600 #endif // defined(OS_ANDROID) | 638 #endif // defined(OS_ANDROID) |
| 601 | 639 |
| 602 #if defined(OS_ANDROID) | 640 #if defined(OS_ANDROID) |
| 603 // Tests multiple StartNotifySession success. | 641 // Tests multiple StartNotifySession success. |
| 604 TEST_F(BluetoothGattCharacteristicTest, StartNotifySession_Multiple) { | 642 TEST_F(BluetoothGattCharacteristicTest, StartNotifySession_Multiple) { |
| 605 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); | 643 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); |
| 606 | 644 |
| 607 characteristic1_->StartNotifySession(GetNotifyCallback(), | 645 characteristic1_->StartNotifySession( |
| 608 GetGattErrorCallback()); | 646 GetNotifyCallback(Call::EXPECTED), |
| 609 characteristic1_->StartNotifySession(GetNotifyCallback(), | 647 GetGattErrorCallback(Call::NOT_EXPECTED)); |
| 610 GetGattErrorCallback()); | 648 characteristic1_->StartNotifySession( |
| 649 GetNotifyCallback(Call::EXPECTED), |
| 650 GetGattErrorCallback(Call::NOT_EXPECTED)); |
| 611 #if defined(OS_ANDROID) | 651 #if defined(OS_ANDROID) |
| 612 // TODO(crbug.com/551634): Decide when implementing IsNotifying if Android | 652 // TODO(crbug.com/551634): Decide when implementing IsNotifying if Android |
| 613 // should trust the notification request always worked, or if we should always | 653 // should trust the notification request always worked, or if we should always |
| 614 // redundantly set the value to the OS. | 654 // redundantly set the value to the OS. |
| 615 EXPECT_EQ(2, gatt_notify_characteristic_attempts_); | 655 EXPECT_EQ(2, gatt_notify_characteristic_attempts_); |
| 616 #else | 656 #else |
| 617 EXPECT_EQ(1, gatt_notify_characteristic_attempts_); | 657 EXPECT_EQ(1, gatt_notify_characteristic_attempts_); |
| 618 #endif | 658 #endif |
| 619 EXPECT_EQ(0, callback_count_); | 659 EXPECT_EQ(0, callback_count_); |
| 620 SimulateGattNotifySessionStarted(characteristic1_); | 660 SimulateGattNotifySessionStarted(characteristic1_); |
| 621 EXPECT_EQ(2, callback_count_); | 661 EXPECT_EQ(2, callback_count_); |
| 622 EXPECT_EQ(0, error_callback_count_); | 662 EXPECT_EQ(0, error_callback_count_); |
| 623 ASSERT_EQ(2u, notify_sessions_.size()); | 663 ASSERT_EQ(2u, notify_sessions_.size()); |
| 624 ASSERT_TRUE(notify_sessions_[0]); | 664 ASSERT_TRUE(notify_sessions_[0]); |
| 625 ASSERT_TRUE(notify_sessions_[1]); | 665 ASSERT_TRUE(notify_sessions_[1]); |
| 626 EXPECT_EQ(characteristic1_->GetIdentifier(), | 666 EXPECT_EQ(characteristic1_->GetIdentifier(), |
| 627 notify_sessions_[0]->GetCharacteristicIdentifier()); | 667 notify_sessions_[0]->GetCharacteristicIdentifier()); |
| 628 EXPECT_EQ(characteristic1_->GetIdentifier(), | 668 EXPECT_EQ(characteristic1_->GetIdentifier(), |
| 629 notify_sessions_[1]->GetCharacteristicIdentifier()); | 669 notify_sessions_[1]->GetCharacteristicIdentifier()); |
| 630 EXPECT_TRUE(notify_sessions_[0]->IsActive()); | 670 EXPECT_TRUE(notify_sessions_[0]->IsActive()); |
| 631 EXPECT_TRUE(notify_sessions_[1]->IsActive()); | 671 EXPECT_TRUE(notify_sessions_[1]->IsActive()); |
| 632 } | 672 } |
| 633 #endif // defined(OS_ANDROID) | 673 #endif // defined(OS_ANDROID) |
| 634 | 674 |
| 635 } // namespace device | 675 } // namespace device |
| OLD | NEW |