| 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(0, error_callback_count_); | |
| 196 EXPECT_EQ(empty_vector, last_read_value_); | 195 EXPECT_EQ(empty_vector, last_read_value_); |
| 197 EXPECT_EQ(empty_vector, characteristic1_->GetValue()); | 196 EXPECT_EQ(empty_vector, characteristic1_->GetValue()); |
| 198 } | 197 } |
| 199 #endif // defined(OS_ANDROID) | 198 #endif // defined(OS_ANDROID) |
| 200 | 199 |
| 201 #if defined(OS_ANDROID) | 200 #if defined(OS_ANDROID) |
| 202 // Tests WriteRemoteCharacteristic with empty value buffer. | 201 // Tests WriteRemoteCharacteristic with empty value buffer. |
| 203 TEST_F(BluetoothGattCharacteristicTest, WriteRemoteCharacteristic_Empty) { | 202 TEST_F(BluetoothGattCharacteristicTest, WriteRemoteCharacteristic_Empty) { |
| 204 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); | 203 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); |
| 205 | 204 |
| 206 std::vector<uint8_t> empty_vector; | 205 std::vector<uint8_t> empty_vector; |
| 207 characteristic1_->WriteRemoteCharacteristic(empty_vector, GetCallback(), | 206 characteristic1_->WriteRemoteCharacteristic( |
| 208 GetGattErrorCallback()); | 207 empty_vector, GetCallback(Call::EXPECTED), |
| 208 GetGattErrorCallback(Call::NOT_EXPECTED)); |
| 209 EXPECT_EQ(1, gatt_write_characteristic_attempts_); | 209 EXPECT_EQ(1, gatt_write_characteristic_attempts_); |
| 210 SimulateGattCharacteristicWrite(characteristic1_); | 210 SimulateGattCharacteristicWrite(characteristic1_); |
| 211 | 211 |
| 212 EXPECT_EQ(1, callback_count_); | |
| 213 EXPECT_EQ(0, error_callback_count_); | |
| 214 EXPECT_EQ(empty_vector, last_write_value_); | 212 EXPECT_EQ(empty_vector, last_write_value_); |
| 215 } | 213 } |
| 216 #endif // defined(OS_ANDROID) | 214 #endif // defined(OS_ANDROID) |
| 217 | 215 |
| 218 #if defined(OS_ANDROID) | 216 #if defined(OS_ANDROID) |
| 219 // Tests ReadRemoteCharacteristic and GetValue with non-empty value buffer. | 217 // Tests ReadRemoteCharacteristic and GetValue with non-empty value buffer. |
| 220 TEST_F(BluetoothGattCharacteristicTest, ReadRemoteCharacteristic) { | 218 TEST_F(BluetoothGattCharacteristicTest, ReadRemoteCharacteristic) { |
| 221 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); | 219 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); |
| 222 | 220 |
| 223 characteristic1_->ReadRemoteCharacteristic(GetReadValueCallback(), | 221 characteristic1_->ReadRemoteCharacteristic( |
| 224 GetGattErrorCallback()); | 222 GetReadValueCallback(Call::EXPECTED), |
| 223 GetGattErrorCallback(Call::NOT_EXPECTED)); |
| 225 EXPECT_EQ(1, gatt_read_characteristic_attempts_); | 224 EXPECT_EQ(1, gatt_read_characteristic_attempts_); |
| 226 | 225 |
| 227 uint8_t values[] = {0, 1, 2, 3, 4, 0xf, 0xf0, 0xff}; | 226 uint8_t values[] = {0, 1, 2, 3, 4, 0xf, 0xf0, 0xff}; |
| 228 std::vector<uint8_t> test_vector(values, values + arraysize(values)); | 227 std::vector<uint8_t> test_vector(values, values + arraysize(values)); |
| 229 SimulateGattCharacteristicRead(characteristic1_, test_vector); | 228 SimulateGattCharacteristicRead(characteristic1_, test_vector); |
| 230 | 229 |
| 231 // Duplicate read reported from OS shouldn't cause a problem: | 230 // Duplicate read reported from OS shouldn't cause a problem: |
| 232 std::vector<uint8_t> empty_vector; | 231 std::vector<uint8_t> empty_vector; |
| 233 SimulateGattCharacteristicRead(characteristic1_, empty_vector); | 232 SimulateGattCharacteristicRead(characteristic1_, empty_vector); |
| 234 | 233 |
| 235 EXPECT_EQ(1, callback_count_); | |
| 236 EXPECT_EQ(0, error_callback_count_); | |
| 237 EXPECT_EQ(test_vector, last_read_value_); | 234 EXPECT_EQ(test_vector, last_read_value_); |
| 238 EXPECT_EQ(test_vector, characteristic1_->GetValue()); | 235 EXPECT_EQ(test_vector, characteristic1_->GetValue()); |
| 239 } | 236 } |
| 240 #endif // defined(OS_ANDROID) | 237 #endif // defined(OS_ANDROID) |
| 241 | 238 |
| 242 #if defined(OS_ANDROID) | 239 #if defined(OS_ANDROID) |
| 243 // Tests WriteRemoteCharacteristic with non-empty value buffer. | 240 // Tests WriteRemoteCharacteristic with non-empty value buffer. |
| 244 TEST_F(BluetoothGattCharacteristicTest, WriteRemoteCharacteristic) { | 241 TEST_F(BluetoothGattCharacteristicTest, WriteRemoteCharacteristic) { |
| 245 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); | 242 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); |
| 246 | 243 |
| 247 uint8_t values[] = {0, 1, 2, 3, 4, 0xf, 0xf0, 0xff}; | 244 uint8_t values[] = {0, 1, 2, 3, 4, 0xf, 0xf0, 0xff}; |
| 248 std::vector<uint8_t> test_vector(values, values + arraysize(values)); | 245 std::vector<uint8_t> test_vector(values, values + arraysize(values)); |
| 249 characteristic1_->WriteRemoteCharacteristic(test_vector, GetCallback(), | 246 characteristic1_->WriteRemoteCharacteristic( |
| 250 GetGattErrorCallback()); | 247 test_vector, GetCallback(Call::EXPECTED), |
| 248 GetGattErrorCallback(Call::NOT_EXPECTED)); |
| 251 EXPECT_EQ(1, gatt_write_characteristic_attempts_); | 249 EXPECT_EQ(1, gatt_write_characteristic_attempts_); |
| 252 | 250 |
| 253 SimulateGattCharacteristicWrite(characteristic1_); | 251 SimulateGattCharacteristicWrite(characteristic1_); |
| 254 | 252 |
| 255 EXPECT_EQ(1, callback_count_); | |
| 256 EXPECT_EQ(0, error_callback_count_); | |
| 257 EXPECT_EQ(test_vector, last_write_value_); | 253 EXPECT_EQ(test_vector, last_write_value_); |
| 258 } | 254 } |
| 259 #endif // defined(OS_ANDROID) | 255 #endif // defined(OS_ANDROID) |
| 260 | 256 |
| 261 #if defined(OS_ANDROID) | 257 #if defined(OS_ANDROID) |
| 262 // Tests ReadRemoteCharacteristic and GetValue multiple times. | 258 // Tests ReadRemoteCharacteristic and GetValue multiple times. |
| 263 TEST_F(BluetoothGattCharacteristicTest, ReadRemoteCharacteristic_Twice) { | 259 TEST_F(BluetoothGattCharacteristicTest, ReadRemoteCharacteristic_Twice) { |
| 264 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); | 260 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); |
| 265 | 261 |
| 266 characteristic1_->ReadRemoteCharacteristic(GetReadValueCallback(), | 262 characteristic1_->ReadRemoteCharacteristic( |
| 267 GetGattErrorCallback()); | 263 GetReadValueCallback(Call::EXPECTED), |
| 264 GetGattErrorCallback(Call::NOT_EXPECTED)); |
| 268 EXPECT_EQ(1, gatt_read_characteristic_attempts_); | 265 EXPECT_EQ(1, gatt_read_characteristic_attempts_); |
| 269 | 266 |
| 270 uint8_t values[] = {0, 1, 2, 3, 4, 0xf, 0xf0, 0xff}; | 267 uint8_t values[] = {0, 1, 2, 3, 4, 0xf, 0xf0, 0xff}; |
| 271 std::vector<uint8_t> test_vector(values, values + arraysize(values)); | 268 std::vector<uint8_t> test_vector(values, values + arraysize(values)); |
| 272 SimulateGattCharacteristicRead(characteristic1_, test_vector); | 269 SimulateGattCharacteristicRead(characteristic1_, test_vector); |
| 273 EXPECT_EQ(1, callback_count_); | 270 EXPECT_EQ(1, callback_count_); |
| 274 EXPECT_EQ(0, error_callback_count_); | 271 EXPECT_EQ(0, error_callback_count_); |
| 275 EXPECT_EQ(test_vector, last_read_value_); | 272 EXPECT_EQ(test_vector, last_read_value_); |
| 276 EXPECT_EQ(test_vector, characteristic1_->GetValue()); | 273 EXPECT_EQ(test_vector, characteristic1_->GetValue()); |
| 277 | 274 |
| 278 // Read again, with different value: | 275 // Read again, with different value: |
| 279 ResetEventCounts(); | 276 ResetEventCounts(); |
| 280 characteristic1_->ReadRemoteCharacteristic(GetReadValueCallback(), | 277 characteristic1_->ReadRemoteCharacteristic( |
| 281 GetGattErrorCallback()); | 278 GetReadValueCallback(Call::EXPECTED), |
| 279 GetGattErrorCallback(Call::NOT_EXPECTED)); |
| 282 EXPECT_EQ(1, gatt_read_characteristic_attempts_); | 280 EXPECT_EQ(1, gatt_read_characteristic_attempts_); |
| 283 std::vector<uint8_t> empty_vector; | 281 std::vector<uint8_t> empty_vector; |
| 284 SimulateGattCharacteristicRead(characteristic1_, empty_vector); | 282 SimulateGattCharacteristicRead(characteristic1_, empty_vector); |
| 285 EXPECT_EQ(1, callback_count_); | 283 EXPECT_EQ(1, callback_count_); |
| 286 EXPECT_EQ(0, error_callback_count_); | 284 EXPECT_EQ(0, error_callback_count_); |
| 287 EXPECT_EQ(empty_vector, last_read_value_); | 285 EXPECT_EQ(empty_vector, last_read_value_); |
| 288 EXPECT_EQ(empty_vector, characteristic1_->GetValue()); | 286 EXPECT_EQ(empty_vector, characteristic1_->GetValue()); |
| 289 } | 287 } |
| 290 #endif // defined(OS_ANDROID) | 288 #endif // defined(OS_ANDROID) |
| 291 | 289 |
| 292 #if defined(OS_ANDROID) | 290 #if defined(OS_ANDROID) |
| 293 // Tests WriteRemoteCharacteristic multiple times. | 291 // Tests WriteRemoteCharacteristic multiple times. |
| 294 TEST_F(BluetoothGattCharacteristicTest, WriteRemoteCharacteristic_Twice) { | 292 TEST_F(BluetoothGattCharacteristicTest, WriteRemoteCharacteristic_Twice) { |
| 295 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); | 293 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); |
| 296 | 294 |
| 297 uint8_t values[] = {0, 1, 2, 3, 4, 0xf, 0xf0, 0xff}; | 295 uint8_t values[] = {0, 1, 2, 3, 4, 0xf, 0xf0, 0xff}; |
| 298 std::vector<uint8_t> test_vector(values, values + arraysize(values)); | 296 std::vector<uint8_t> test_vector(values, values + arraysize(values)); |
| 299 characteristic1_->WriteRemoteCharacteristic(test_vector, GetCallback(), | 297 characteristic1_->WriteRemoteCharacteristic( |
| 300 GetGattErrorCallback()); | 298 test_vector, GetCallback(Call::EXPECTED), |
| 299 GetGattErrorCallback(Call::NOT_EXPECTED)); |
| 301 EXPECT_EQ(1, gatt_write_characteristic_attempts_); | 300 EXPECT_EQ(1, gatt_write_characteristic_attempts_); |
| 302 | 301 |
| 303 SimulateGattCharacteristicWrite(characteristic1_); | 302 SimulateGattCharacteristicWrite(characteristic1_); |
| 304 EXPECT_EQ(1, callback_count_); | 303 EXPECT_EQ(1, callback_count_); |
| 305 EXPECT_EQ(0, error_callback_count_); | 304 EXPECT_EQ(0, error_callback_count_); |
| 306 EXPECT_EQ(test_vector, last_write_value_); | 305 EXPECT_EQ(test_vector, last_write_value_); |
| 307 | 306 |
| 308 // Write again, with different value: | 307 // Write again, with different value: |
| 309 ResetEventCounts(); | 308 ResetEventCounts(); |
| 310 std::vector<uint8_t> empty_vector; | 309 std::vector<uint8_t> empty_vector; |
| 311 characteristic1_->WriteRemoteCharacteristic(empty_vector, GetCallback(), | 310 characteristic1_->WriteRemoteCharacteristic( |
| 312 GetGattErrorCallback()); | 311 empty_vector, GetCallback(Call::EXPECTED), |
| 312 GetGattErrorCallback(Call::NOT_EXPECTED)); |
| 313 EXPECT_EQ(1, gatt_write_characteristic_attempts_); | 313 EXPECT_EQ(1, gatt_write_characteristic_attempts_); |
| 314 SimulateGattCharacteristicWrite(characteristic1_); | 314 SimulateGattCharacteristicWrite(characteristic1_); |
| 315 EXPECT_EQ(1, callback_count_); | 315 EXPECT_EQ(1, callback_count_); |
| 316 EXPECT_EQ(0, error_callback_count_); | 316 EXPECT_EQ(0, error_callback_count_); |
| 317 EXPECT_EQ(empty_vector, last_write_value_); | 317 EXPECT_EQ(empty_vector, last_write_value_); |
| 318 } | 318 } |
| 319 #endif // defined(OS_ANDROID) | 319 #endif // defined(OS_ANDROID) |
| 320 | 320 |
| 321 #if defined(OS_ANDROID) | 321 #if defined(OS_ANDROID) |
| 322 // Tests ReadRemoteCharacteristic on two characteristics. | 322 // Tests ReadRemoteCharacteristic on two characteristics. |
| 323 TEST_F(BluetoothGattCharacteristicTest, | 323 TEST_F(BluetoothGattCharacteristicTest, |
| 324 ReadRemoteCharacteristic_MultipleCharacteristics) { | 324 ReadRemoteCharacteristic_MultipleCharacteristics) { |
| 325 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); | 325 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); |
| 326 | 326 |
| 327 characteristic1_->ReadRemoteCharacteristic(GetReadValueCallback(), | 327 characteristic1_->ReadRemoteCharacteristic( |
| 328 GetGattErrorCallback()); | 328 GetReadValueCallback(Call::EXPECTED), |
| 329 characteristic2_->ReadRemoteCharacteristic(GetReadValueCallback(), | 329 GetGattErrorCallback(Call::NOT_EXPECTED)); |
| 330 GetGattErrorCallback()); | 330 characteristic2_->ReadRemoteCharacteristic( |
| 331 GetReadValueCallback(Call::EXPECTED), |
| 332 GetGattErrorCallback(Call::NOT_EXPECTED)); |
| 331 EXPECT_EQ(2, gatt_read_characteristic_attempts_); | 333 EXPECT_EQ(2, gatt_read_characteristic_attempts_); |
| 332 EXPECT_EQ(0, callback_count_); | 334 EXPECT_EQ(0, callback_count_); |
| 333 EXPECT_EQ(0, error_callback_count_); | 335 EXPECT_EQ(0, error_callback_count_); |
| 334 | 336 |
| 335 std::vector<uint8_t> test_vector1; | 337 std::vector<uint8_t> test_vector1; |
| 336 test_vector1.push_back(111); | 338 test_vector1.push_back(111); |
| 337 SimulateGattCharacteristicRead(characteristic1_, test_vector1); | 339 SimulateGattCharacteristicRead(characteristic1_, test_vector1); |
| 338 EXPECT_EQ(test_vector1, last_read_value_); | 340 EXPECT_EQ(test_vector1, last_read_value_); |
| 339 | 341 |
| 340 std::vector<uint8_t> test_vector2; | 342 std::vector<uint8_t> test_vector2; |
| 341 test_vector2.push_back(222); | 343 test_vector2.push_back(222); |
| 342 SimulateGattCharacteristicRead(characteristic2_, test_vector2); | 344 SimulateGattCharacteristicRead(characteristic2_, test_vector2); |
| 343 EXPECT_EQ(test_vector2, last_read_value_); | 345 EXPECT_EQ(test_vector2, last_read_value_); |
| 344 | 346 |
| 345 EXPECT_EQ(2, callback_count_); | 347 EXPECT_EQ(2, callback_count_); |
| 346 EXPECT_EQ(0, error_callback_count_); | 348 EXPECT_EQ(0, error_callback_count_); |
| 347 EXPECT_EQ(test_vector1, characteristic1_->GetValue()); | 349 EXPECT_EQ(test_vector1, characteristic1_->GetValue()); |
| 348 EXPECT_EQ(test_vector2, characteristic2_->GetValue()); | 350 EXPECT_EQ(test_vector2, characteristic2_->GetValue()); |
| 349 } | 351 } |
| 350 #endif // defined(OS_ANDROID) | 352 #endif // defined(OS_ANDROID) |
| 351 | 353 |
| 352 #if defined(OS_ANDROID) | 354 #if defined(OS_ANDROID) |
| 353 // Tests WriteRemoteCharacteristic on two characteristics. | 355 // Tests WriteRemoteCharacteristic on two characteristics. |
| 354 TEST_F(BluetoothGattCharacteristicTest, | 356 TEST_F(BluetoothGattCharacteristicTest, |
| 355 WriteRemoteCharacteristic_MultipleCharacteristics) { | 357 WriteRemoteCharacteristic_MultipleCharacteristics) { |
| 356 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); | 358 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); |
| 357 | 359 |
| 358 std::vector<uint8_t> test_vector1; | 360 std::vector<uint8_t> test_vector1; |
| 359 test_vector1.push_back(111); | 361 test_vector1.push_back(111); |
| 360 characteristic1_->WriteRemoteCharacteristic(test_vector1, GetCallback(), | 362 characteristic1_->WriteRemoteCharacteristic( |
| 361 GetGattErrorCallback()); | 363 test_vector1, GetCallback(Call::EXPECTED), |
| 364 GetGattErrorCallback(Call::NOT_EXPECTED)); |
| 362 EXPECT_EQ(test_vector1, last_write_value_); | 365 EXPECT_EQ(test_vector1, last_write_value_); |
| 363 | 366 |
| 364 std::vector<uint8_t> test_vector2; | 367 std::vector<uint8_t> test_vector2; |
| 365 test_vector2.push_back(222); | 368 test_vector2.push_back(222); |
| 366 characteristic2_->WriteRemoteCharacteristic(test_vector2, GetCallback(), | 369 characteristic2_->WriteRemoteCharacteristic( |
| 367 GetGattErrorCallback()); | 370 test_vector2, GetCallback(Call::EXPECTED), |
| 371 GetGattErrorCallback(Call::NOT_EXPECTED)); |
| 368 EXPECT_EQ(test_vector2, last_write_value_); | 372 EXPECT_EQ(test_vector2, last_write_value_); |
| 369 | 373 |
| 370 EXPECT_EQ(2, gatt_write_characteristic_attempts_); | 374 EXPECT_EQ(2, gatt_write_characteristic_attempts_); |
| 371 EXPECT_EQ(0, callback_count_); | 375 EXPECT_EQ(0, callback_count_); |
| 372 EXPECT_EQ(0, error_callback_count_); | 376 EXPECT_EQ(0, error_callback_count_); |
| 373 | 377 |
| 374 SimulateGattCharacteristicWrite(characteristic1_); | 378 SimulateGattCharacteristicWrite(characteristic1_); |
| 375 SimulateGattCharacteristicWrite(characteristic2_); | 379 SimulateGattCharacteristicWrite(characteristic2_); |
| 376 | 380 |
| 377 EXPECT_EQ(2, callback_count_); | 381 EXPECT_EQ(2, callback_count_); |
| 378 EXPECT_EQ(0, error_callback_count_); | 382 EXPECT_EQ(0, error_callback_count_); |
| 379 } | 383 } |
| 380 #endif // defined(OS_ANDROID) | 384 #endif // defined(OS_ANDROID) |
| 381 | 385 |
| 382 #if defined(OS_ANDROID) | 386 #if defined(OS_ANDROID) |
| 383 // Tests ReadRemoteCharacteristic asynchronous error. | 387 // Tests ReadRemoteCharacteristic asynchronous error. |
| 384 TEST_F(BluetoothGattCharacteristicTest, ReadError) { | 388 TEST_F(BluetoothGattCharacteristicTest, ReadError) { |
| 385 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); | 389 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); |
| 386 | 390 |
| 387 characteristic1_->ReadRemoteCharacteristic(GetReadValueCallback(), | 391 characteristic1_->ReadRemoteCharacteristic( |
| 388 GetGattErrorCallback()); | 392 GetReadValueCallback(Call::NOT_EXPECTED), |
| 393 GetGattErrorCallback(Call::EXPECTED)); |
| 389 SimulateGattCharacteristicReadError( | 394 SimulateGattCharacteristicReadError( |
| 390 characteristic1_, BluetoothGattService::GATT_ERROR_INVALID_LENGTH); | 395 characteristic1_, BluetoothGattService::GATT_ERROR_INVALID_LENGTH); |
| 391 SimulateGattCharacteristicReadError(characteristic1_, | 396 SimulateGattCharacteristicReadError(characteristic1_, |
| 392 BluetoothGattService::GATT_ERROR_FAILED); | 397 BluetoothGattService::GATT_ERROR_FAILED); |
| 393 EXPECT_EQ(0, callback_count_); | |
| 394 EXPECT_EQ(1, error_callback_count_); | |
| 395 EXPECT_EQ(BluetoothGattService::GATT_ERROR_INVALID_LENGTH, | 398 EXPECT_EQ(BluetoothGattService::GATT_ERROR_INVALID_LENGTH, |
| 396 last_gatt_error_code_); | 399 last_gatt_error_code_); |
| 397 } | 400 } |
| 398 #endif // defined(OS_ANDROID) | 401 #endif // defined(OS_ANDROID) |
| 399 | 402 |
| 400 #if defined(OS_ANDROID) | 403 #if defined(OS_ANDROID) |
| 401 // Tests WriteRemoteCharacteristic asynchronous error. | 404 // Tests WriteRemoteCharacteristic asynchronous error. |
| 402 TEST_F(BluetoothGattCharacteristicTest, WriteError) { | 405 TEST_F(BluetoothGattCharacteristicTest, WriteError) { |
| 403 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); | 406 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); |
| 404 | 407 |
| 405 std::vector<uint8_t> empty_vector; | 408 std::vector<uint8_t> empty_vector; |
| 406 characteristic1_->WriteRemoteCharacteristic(empty_vector, GetCallback(), | 409 characteristic1_->WriteRemoteCharacteristic( |
| 407 GetGattErrorCallback()); | 410 empty_vector, GetCallback(Call::NOT_EXPECTED), |
| 411 GetGattErrorCallback(Call::EXPECTED)); |
| 408 SimulateGattCharacteristicWriteError( | 412 SimulateGattCharacteristicWriteError( |
| 409 characteristic1_, BluetoothGattService::GATT_ERROR_INVALID_LENGTH); | 413 characteristic1_, BluetoothGattService::GATT_ERROR_INVALID_LENGTH); |
| 410 SimulateGattCharacteristicWriteError(characteristic1_, | 414 SimulateGattCharacteristicWriteError(characteristic1_, |
| 411 BluetoothGattService::GATT_ERROR_FAILED); | 415 BluetoothGattService::GATT_ERROR_FAILED); |
| 412 EXPECT_EQ(0, callback_count_); | 416 |
| 413 EXPECT_EQ(1, error_callback_count_); | |
| 414 EXPECT_EQ(BluetoothGattService::GATT_ERROR_INVALID_LENGTH, | 417 EXPECT_EQ(BluetoothGattService::GATT_ERROR_INVALID_LENGTH, |
| 415 last_gatt_error_code_); | 418 last_gatt_error_code_); |
| 416 } | 419 } |
| 417 #endif // defined(OS_ANDROID) | 420 #endif // defined(OS_ANDROID) |
| 418 | 421 |
| 419 #if defined(OS_ANDROID) | 422 #if defined(OS_ANDROID) |
| 420 // Tests ReadRemoteCharacteristic synchronous error. | 423 // Tests ReadRemoteCharacteristic synchronous error. |
| 421 TEST_F(BluetoothGattCharacteristicTest, ReadSynchronousError) { | 424 TEST_F(BluetoothGattCharacteristicTest, ReadSynchronousError) { |
| 422 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); | 425 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); |
| 423 | 426 |
| 424 SimulateGattCharacteristicReadWillFailSynchronouslyOnce(characteristic1_); | 427 SimulateGattCharacteristicReadWillFailSynchronouslyOnce(characteristic1_); |
| 425 characteristic1_->ReadRemoteCharacteristic(GetReadValueCallback(), | 428 characteristic1_->ReadRemoteCharacteristic( |
| 426 GetGattErrorCallback()); | 429 GetReadValueCallback(Call::NOT_EXPECTED), |
| 430 GetGattErrorCallback(Call::EXPECTED)); |
| 427 EXPECT_EQ(0, gatt_read_characteristic_attempts_); | 431 EXPECT_EQ(0, gatt_read_characteristic_attempts_); |
| 428 base::RunLoop().RunUntilIdle(); | 432 base::RunLoop().RunUntilIdle(); |
| 429 EXPECT_EQ(0, callback_count_); | 433 EXPECT_EQ(0, callback_count_); |
| 430 EXPECT_EQ(1, error_callback_count_); | 434 EXPECT_EQ(1, error_callback_count_); |
| 431 EXPECT_EQ(BluetoothGattService::GATT_ERROR_FAILED, last_gatt_error_code_); | 435 EXPECT_EQ(BluetoothGattService::GATT_ERROR_FAILED, last_gatt_error_code_); |
| 432 | 436 |
| 433 // After failing once, can succeed: | 437 // After failing once, can succeed: |
| 434 ResetEventCounts(); | 438 ResetEventCounts(); |
| 435 characteristic1_->ReadRemoteCharacteristic(GetReadValueCallback(), | 439 characteristic1_->ReadRemoteCharacteristic( |
| 436 GetGattErrorCallback()); | 440 GetReadValueCallback(Call::EXPECTED), |
| 441 GetGattErrorCallback(Call::NOT_EXPECTED)); |
| 437 EXPECT_EQ(1, gatt_read_characteristic_attempts_); | 442 EXPECT_EQ(1, gatt_read_characteristic_attempts_); |
| 438 std::vector<uint8_t> empty_vector; | 443 std::vector<uint8_t> empty_vector; |
| 439 SimulateGattCharacteristicRead(characteristic1_, empty_vector); | 444 SimulateGattCharacteristicRead(characteristic1_, empty_vector); |
| 440 EXPECT_EQ(1, callback_count_); | 445 EXPECT_EQ(1, callback_count_); |
| 441 EXPECT_EQ(0, error_callback_count_); | 446 EXPECT_EQ(0, error_callback_count_); |
| 442 } | 447 } |
| 443 #endif // defined(OS_ANDROID) | 448 #endif // defined(OS_ANDROID) |
| 444 | 449 |
| 445 #if defined(OS_ANDROID) | 450 #if defined(OS_ANDROID) |
| 446 // Tests WriteRemoteCharacteristic synchronous error. | 451 // Tests WriteRemoteCharacteristic synchronous error. |
| 447 TEST_F(BluetoothGattCharacteristicTest, WriteSynchronousError) { | 452 TEST_F(BluetoothGattCharacteristicTest, WriteSynchronousError) { |
| 448 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); | 453 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); |
| 449 | 454 |
| 450 SimulateGattCharacteristicWriteWillFailSynchronouslyOnce(characteristic1_); | 455 SimulateGattCharacteristicWriteWillFailSynchronouslyOnce(characteristic1_); |
| 451 std::vector<uint8_t> empty_vector; | 456 std::vector<uint8_t> empty_vector; |
| 452 characteristic1_->WriteRemoteCharacteristic(empty_vector, GetCallback(), | 457 characteristic1_->WriteRemoteCharacteristic( |
| 453 GetGattErrorCallback()); | 458 empty_vector, GetCallback(Call::NOT_EXPECTED), |
| 459 GetGattErrorCallback(Call::EXPECTED)); |
| 454 EXPECT_EQ(0, gatt_write_characteristic_attempts_); | 460 EXPECT_EQ(0, gatt_write_characteristic_attempts_); |
| 455 base::RunLoop().RunUntilIdle(); | 461 base::RunLoop().RunUntilIdle(); |
| 456 EXPECT_EQ(0, callback_count_); | 462 EXPECT_EQ(0, callback_count_); |
| 457 EXPECT_EQ(1, error_callback_count_); | 463 EXPECT_EQ(1, error_callback_count_); |
| 458 EXPECT_EQ(BluetoothGattService::GATT_ERROR_FAILED, last_gatt_error_code_); | 464 EXPECT_EQ(BluetoothGattService::GATT_ERROR_FAILED, last_gatt_error_code_); |
| 459 | 465 |
| 460 // After failing once, can succeed: | 466 // After failing once, can succeed: |
| 461 ResetEventCounts(); | 467 ResetEventCounts(); |
| 462 characteristic1_->WriteRemoteCharacteristic(empty_vector, GetCallback(), | 468 characteristic1_->WriteRemoteCharacteristic( |
| 463 GetGattErrorCallback()); | 469 empty_vector, GetCallback(Call::EXPECTED), |
| 470 GetGattErrorCallback(Call::NOT_EXPECTED)); |
| 464 EXPECT_EQ(1, gatt_write_characteristic_attempts_); | 471 EXPECT_EQ(1, gatt_write_characteristic_attempts_); |
| 465 SimulateGattCharacteristicWrite(characteristic1_); | 472 SimulateGattCharacteristicWrite(characteristic1_); |
| 466 EXPECT_EQ(1, callback_count_); | 473 EXPECT_EQ(1, callback_count_); |
| 467 EXPECT_EQ(0, error_callback_count_); | 474 EXPECT_EQ(0, error_callback_count_); |
| 468 } | 475 } |
| 469 #endif // defined(OS_ANDROID) | 476 #endif // defined(OS_ANDROID) |
| 470 | 477 |
| 471 #if defined(OS_ANDROID) | 478 #if defined(OS_ANDROID) |
| 472 // Tests ReadRemoteCharacteristic error with a pending read operation. | 479 // Tests ReadRemoteCharacteristic error with a pending read operation. |
| 473 TEST_F(BluetoothGattCharacteristicTest, ReadRemoteCharacteristic_ReadPending) { | 480 TEST_F(BluetoothGattCharacteristicTest, ReadRemoteCharacteristic_ReadPending) { |
| 474 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); | 481 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); |
| 475 | 482 |
| 476 characteristic1_->ReadRemoteCharacteristic(GetReadValueCallback(), | 483 characteristic1_->ReadRemoteCharacteristic( |
| 477 GetGattErrorCallback()); | 484 GetReadValueCallback(Call::EXPECTED), |
| 478 characteristic1_->ReadRemoteCharacteristic(GetReadValueCallback(), | 485 GetGattErrorCallback(Call::NOT_EXPECTED)); |
| 479 GetGattErrorCallback()); | 486 characteristic1_->ReadRemoteCharacteristic( |
| 487 GetReadValueCallback(Call::NOT_EXPECTED), |
| 488 GetGattErrorCallback(Call::EXPECTED)); |
| 489 |
| 490 base::RunLoop().RunUntilIdle(); |
| 491 |
| 480 EXPECT_EQ(0, callback_count_); | 492 EXPECT_EQ(0, callback_count_); |
| 481 EXPECT_EQ(1, error_callback_count_); | 493 EXPECT_EQ(1, error_callback_count_); |
| 482 EXPECT_EQ(BluetoothGattService::GATT_ERROR_IN_PROGRESS, | 494 EXPECT_EQ(BluetoothGattService::GATT_ERROR_IN_PROGRESS, |
| 483 last_gatt_error_code_); | 495 last_gatt_error_code_); |
| 484 | 496 |
| 485 // Initial read should still succeed: | 497 // Initial read should still succeed: |
| 486 ResetEventCounts(); | 498 ResetEventCounts(); |
| 487 std::vector<uint8_t> empty_vector; | 499 std::vector<uint8_t> empty_vector; |
| 488 SimulateGattCharacteristicRead(characteristic1_, empty_vector); | 500 SimulateGattCharacteristicRead(characteristic1_, empty_vector); |
| 489 EXPECT_EQ(1, callback_count_); | 501 EXPECT_EQ(1, callback_count_); |
| 490 EXPECT_EQ(0, error_callback_count_); | 502 EXPECT_EQ(0, error_callback_count_); |
| 491 } | 503 } |
| 492 #endif // defined(OS_ANDROID) | 504 #endif // defined(OS_ANDROID) |
| 493 | 505 |
| 494 #if defined(OS_ANDROID) | 506 #if defined(OS_ANDROID) |
| 495 // Tests WriteRemoteCharacteristic error with a pending write operation. | 507 // Tests WriteRemoteCharacteristic error with a pending write operation. |
| 496 TEST_F(BluetoothGattCharacteristicTest, | 508 TEST_F(BluetoothGattCharacteristicTest, |
| 497 WriteRemoteCharacteristic_WritePending) { | 509 WriteRemoteCharacteristic_WritePending) { |
| 498 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); | 510 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); |
| 499 | 511 |
| 500 std::vector<uint8_t> empty_vector; | 512 std::vector<uint8_t> empty_vector; |
| 501 characteristic1_->WriteRemoteCharacteristic(empty_vector, GetCallback(), | 513 characteristic1_->WriteRemoteCharacteristic( |
| 502 GetGattErrorCallback()); | 514 empty_vector, GetCallback(Call::EXPECTED), |
| 503 characteristic1_->WriteRemoteCharacteristic(empty_vector, GetCallback(), | 515 GetGattErrorCallback(Call::NOT_EXPECTED)); |
| 504 GetGattErrorCallback()); | 516 characteristic1_->WriteRemoteCharacteristic( |
| 517 empty_vector, GetCallback(Call::NOT_EXPECTED), |
| 518 GetGattErrorCallback(Call::EXPECTED)); |
| 519 |
| 520 base::RunLoop().RunUntilIdle(); |
| 521 |
| 505 EXPECT_EQ(0, callback_count_); | 522 EXPECT_EQ(0, callback_count_); |
| 506 EXPECT_EQ(1, error_callback_count_); | 523 EXPECT_EQ(1, error_callback_count_); |
| 507 EXPECT_EQ(BluetoothGattService::GATT_ERROR_IN_PROGRESS, | 524 EXPECT_EQ(BluetoothGattService::GATT_ERROR_IN_PROGRESS, |
| 508 last_gatt_error_code_); | 525 last_gatt_error_code_); |
| 509 | 526 |
| 510 // Initial write should still succeed: | 527 // Initial write should still succeed: |
| 511 ResetEventCounts(); | 528 ResetEventCounts(); |
| 512 SimulateGattCharacteristicWrite(characteristic1_); | 529 SimulateGattCharacteristicWrite(characteristic1_); |
| 513 EXPECT_EQ(1, callback_count_); | 530 EXPECT_EQ(1, callback_count_); |
| 514 EXPECT_EQ(0, error_callback_count_); | 531 EXPECT_EQ(0, error_callback_count_); |
| 515 } | 532 } |
| 516 #endif // defined(OS_ANDROID) | 533 #endif // defined(OS_ANDROID) |
| 517 | 534 |
| 518 #if defined(OS_ANDROID) | 535 #if defined(OS_ANDROID) |
| 519 // Tests ReadRemoteCharacteristic error with a pending write operation. | 536 // Tests ReadRemoteCharacteristic error with a pending write operation. |
| 520 TEST_F(BluetoothGattCharacteristicTest, ReadRemoteCharacteristic_WritePending) { | 537 TEST_F(BluetoothGattCharacteristicTest, ReadRemoteCharacteristic_WritePending) { |
| 521 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); | 538 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); |
| 522 | 539 |
| 523 std::vector<uint8_t> empty_vector; | 540 std::vector<uint8_t> empty_vector; |
| 524 characteristic1_->WriteRemoteCharacteristic(empty_vector, GetCallback(), | 541 characteristic1_->WriteRemoteCharacteristic( |
| 525 GetGattErrorCallback()); | 542 empty_vector, GetCallback(Call::EXPECTED), |
| 526 characteristic1_->ReadRemoteCharacteristic(GetReadValueCallback(), | 543 GetGattErrorCallback(Call::NOT_EXPECTED)); |
| 527 GetGattErrorCallback()); | 544 characteristic1_->ReadRemoteCharacteristic( |
| 545 GetReadValueCallback(Call::NOT_EXPECTED), |
| 546 GetGattErrorCallback(Call::EXPECTED)); |
| 547 |
| 548 base::RunLoop().RunUntilIdle(); |
| 549 |
| 528 EXPECT_EQ(0, callback_count_); | 550 EXPECT_EQ(0, callback_count_); |
| 529 EXPECT_EQ(1, error_callback_count_); | 551 EXPECT_EQ(1, error_callback_count_); |
| 530 EXPECT_EQ(BluetoothGattService::GATT_ERROR_IN_PROGRESS, | 552 EXPECT_EQ(BluetoothGattService::GATT_ERROR_IN_PROGRESS, |
| 531 last_gatt_error_code_); | 553 last_gatt_error_code_); |
| 532 | 554 |
| 533 // Initial write should still succeed: | 555 // Initial write should still succeed: |
| 534 ResetEventCounts(); | 556 ResetEventCounts(); |
| 535 SimulateGattCharacteristicWrite(characteristic1_); | 557 SimulateGattCharacteristicWrite(characteristic1_); |
| 536 EXPECT_EQ(1, callback_count_); | 558 EXPECT_EQ(1, callback_count_); |
| 537 EXPECT_EQ(0, error_callback_count_); | 559 EXPECT_EQ(0, error_callback_count_); |
| 538 } | 560 } |
| 539 #endif // defined(OS_ANDROID) | 561 #endif // defined(OS_ANDROID) |
| 540 | 562 |
| 541 #if defined(OS_ANDROID) | 563 #if defined(OS_ANDROID) |
| 542 // Tests WriteRemoteCharacteristic error with a pending Read operation. | 564 // Tests WriteRemoteCharacteristic error with a pending Read operation. |
| 543 TEST_F(BluetoothGattCharacteristicTest, WriteRemoteCharacteristic_ReadPending) { | 565 TEST_F(BluetoothGattCharacteristicTest, WriteRemoteCharacteristic_ReadPending) { |
| 544 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); | 566 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); |
| 545 | 567 |
| 546 std::vector<uint8_t> empty_vector; | 568 std::vector<uint8_t> empty_vector; |
| 547 characteristic1_->ReadRemoteCharacteristic(GetReadValueCallback(), | 569 characteristic1_->ReadRemoteCharacteristic( |
| 548 GetGattErrorCallback()); | 570 GetReadValueCallback(Call::EXPECTED), |
| 549 characteristic1_->WriteRemoteCharacteristic(empty_vector, GetCallback(), | 571 GetGattErrorCallback(Call::NOT_EXPECTED)); |
| 550 GetGattErrorCallback()); | 572 characteristic1_->WriteRemoteCharacteristic( |
| 573 empty_vector, GetCallback(Call::NOT_EXPECTED), |
| 574 GetGattErrorCallback(Call::EXPECTED)); |
| 575 base::RunLoop().RunUntilIdle(); |
| 576 |
| 551 EXPECT_EQ(0, callback_count_); | 577 EXPECT_EQ(0, callback_count_); |
| 552 EXPECT_EQ(1, error_callback_count_); | 578 EXPECT_EQ(1, error_callback_count_); |
| 553 EXPECT_EQ(BluetoothGattService::GATT_ERROR_IN_PROGRESS, | 579 EXPECT_EQ(BluetoothGattService::GATT_ERROR_IN_PROGRESS, |
| 554 last_gatt_error_code_); | 580 last_gatt_error_code_); |
| 555 | 581 |
| 556 // Initial read should still succeed: | 582 // Initial read should still succeed: |
| 557 ResetEventCounts(); | 583 ResetEventCounts(); |
| 558 SimulateGattCharacteristicRead(characteristic1_, empty_vector); | 584 SimulateGattCharacteristicRead(characteristic1_, empty_vector); |
| 559 EXPECT_EQ(1, callback_count_); | 585 EXPECT_EQ(1, callback_count_); |
| 560 EXPECT_EQ(0, error_callback_count_); | 586 EXPECT_EQ(0, error_callback_count_); |
| 561 } | 587 } |
| 562 #endif // defined(OS_ANDROID) | 588 #endif // defined(OS_ANDROID) |
| 563 | 589 |
| 564 #if defined(OS_ANDROID) | 590 #if defined(OS_ANDROID) |
| 565 // Tests StartNotifySession success. | 591 // Tests StartNotifySession success. |
| 566 TEST_F(BluetoothGattCharacteristicTest, StartNotifySession) { | 592 TEST_F(BluetoothGattCharacteristicTest, StartNotifySession) { |
| 567 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); | 593 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); |
| 568 | 594 |
| 569 characteristic1_->StartNotifySession(GetNotifyCallback(), | 595 characteristic1_->StartNotifySession( |
| 570 GetGattErrorCallback()); | 596 GetNotifyCallback(Call::EXPECTED), |
| 597 GetGattErrorCallback(Call::NOT_EXPECTED)); |
| 571 EXPECT_EQ(1, gatt_notify_characteristic_attempts_); | 598 EXPECT_EQ(1, gatt_notify_characteristic_attempts_); |
| 572 EXPECT_EQ(0, callback_count_); | 599 EXPECT_EQ(0, callback_count_); |
| 573 SimulateGattNotifySessionStarted(characteristic1_); | 600 SimulateGattNotifySessionStarted(characteristic1_); |
| 574 EXPECT_EQ(1, callback_count_); | 601 EXPECT_EQ(1, callback_count_); |
| 575 EXPECT_EQ(0, error_callback_count_); | 602 EXPECT_EQ(0, error_callback_count_); |
| 576 ASSERT_EQ(1u, notify_sessions_.size()); | 603 ASSERT_EQ(1u, notify_sessions_.size()); |
| 577 ASSERT_TRUE(notify_sessions_[0]); | 604 ASSERT_TRUE(notify_sessions_[0]); |
| 578 EXPECT_EQ(characteristic1_->GetIdentifier(), | 605 EXPECT_EQ(characteristic1_->GetIdentifier(), |
| 579 notify_sessions_[0]->GetCharacteristicIdentifier()); | 606 notify_sessions_[0]->GetCharacteristicIdentifier()); |
| 580 EXPECT_TRUE(notify_sessions_[0]->IsActive()); | 607 EXPECT_TRUE(notify_sessions_[0]->IsActive()); |
| 581 } | 608 } |
| 582 #endif // defined(OS_ANDROID) | 609 #endif // defined(OS_ANDROID) |
| 583 | 610 |
| 584 #if defined(OS_ANDROID) | 611 #if defined(OS_ANDROID) |
| 585 // Tests StartNotifySession synchronous failure. | 612 // Tests StartNotifySession synchronous failure. |
| 586 TEST_F(BluetoothGattCharacteristicTest, StartNotifySession_SynchronousError) { | 613 TEST_F(BluetoothGattCharacteristicTest, StartNotifySession_SynchronousError) { |
| 587 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); | 614 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); |
| 588 | 615 |
| 589 SimulateGattCharacteristicSetNotifyWillFailSynchronouslyOnce( | 616 SimulateGattCharacteristicSetNotifyWillFailSynchronouslyOnce( |
| 590 characteristic1_); | 617 characteristic1_); |
| 591 characteristic1_->StartNotifySession(GetNotifyCallback(), | 618 characteristic1_->StartNotifySession(GetNotifyCallback(Call::NOT_EXPECTED), |
| 592 GetGattErrorCallback()); | 619 GetGattErrorCallback(Call::EXPECTED)); |
| 593 EXPECT_EQ(0, error_callback_count_); | 620 EXPECT_EQ(0, error_callback_count_); |
| 594 base::RunLoop().RunUntilIdle(); | 621 base::RunLoop().RunUntilIdle(); |
| 595 EXPECT_EQ(0, gatt_notify_characteristic_attempts_); | 622 EXPECT_EQ(0, gatt_notify_characteristic_attempts_); |
| 596 EXPECT_EQ(0, callback_count_); | 623 EXPECT_EQ(0, callback_count_); |
| 597 EXPECT_EQ(1, error_callback_count_); | 624 EXPECT_EQ(1, error_callback_count_); |
| 598 ASSERT_EQ(0u, notify_sessions_.size()); | 625 ASSERT_EQ(0u, notify_sessions_.size()); |
| 599 } | 626 } |
| 600 #endif // defined(OS_ANDROID) | 627 #endif // defined(OS_ANDROID) |
| 601 | 628 |
| 602 #if defined(OS_ANDROID) | 629 #if defined(OS_ANDROID) |
| 603 // Tests multiple StartNotifySession success. | 630 // Tests multiple StartNotifySession success. |
| 604 TEST_F(BluetoothGattCharacteristicTest, StartNotifySession_Multiple) { | 631 TEST_F(BluetoothGattCharacteristicTest, StartNotifySession_Multiple) { |
| 605 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); | 632 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); |
| 606 | 633 |
| 607 characteristic1_->StartNotifySession(GetNotifyCallback(), | 634 characteristic1_->StartNotifySession( |
| 608 GetGattErrorCallback()); | 635 GetNotifyCallback(Call::EXPECTED), |
| 609 characteristic1_->StartNotifySession(GetNotifyCallback(), | 636 GetGattErrorCallback(Call::NOT_EXPECTED)); |
| 610 GetGattErrorCallback()); | 637 characteristic1_->StartNotifySession( |
| 638 GetNotifyCallback(Call::EXPECTED), |
| 639 GetGattErrorCallback(Call::NOT_EXPECTED)); |
| 611 #if defined(OS_ANDROID) | 640 #if defined(OS_ANDROID) |
| 612 // TODO(crbug.com/551634): Decide when implementing IsNotifying if Android | 641 // TODO(crbug.com/551634): Decide when implementing IsNotifying if Android |
| 613 // should trust the notification request always worked, or if we should always | 642 // should trust the notification request always worked, or if we should always |
| 614 // redundantly set the value to the OS. | 643 // redundantly set the value to the OS. |
| 615 EXPECT_EQ(2, gatt_notify_characteristic_attempts_); | 644 EXPECT_EQ(2, gatt_notify_characteristic_attempts_); |
| 616 #else | 645 #else |
| 617 EXPECT_EQ(1, gatt_notify_characteristic_attempts_); | 646 EXPECT_EQ(1, gatt_notify_characteristic_attempts_); |
| 618 #endif | 647 #endif |
| 619 EXPECT_EQ(0, callback_count_); | 648 EXPECT_EQ(0, callback_count_); |
| 620 SimulateGattNotifySessionStarted(characteristic1_); | 649 SimulateGattNotifySessionStarted(characteristic1_); |
| 621 EXPECT_EQ(2, callback_count_); | 650 EXPECT_EQ(2, callback_count_); |
| 622 EXPECT_EQ(0, error_callback_count_); | 651 EXPECT_EQ(0, error_callback_count_); |
| 623 ASSERT_EQ(2u, notify_sessions_.size()); | 652 ASSERT_EQ(2u, notify_sessions_.size()); |
| 624 ASSERT_TRUE(notify_sessions_[0]); | 653 ASSERT_TRUE(notify_sessions_[0]); |
| 625 ASSERT_TRUE(notify_sessions_[1]); | 654 ASSERT_TRUE(notify_sessions_[1]); |
| 626 EXPECT_EQ(characteristic1_->GetIdentifier(), | 655 EXPECT_EQ(characteristic1_->GetIdentifier(), |
| 627 notify_sessions_[0]->GetCharacteristicIdentifier()); | 656 notify_sessions_[0]->GetCharacteristicIdentifier()); |
| 628 EXPECT_EQ(characteristic1_->GetIdentifier(), | 657 EXPECT_EQ(characteristic1_->GetIdentifier(), |
| 629 notify_sessions_[1]->GetCharacteristicIdentifier()); | 658 notify_sessions_[1]->GetCharacteristicIdentifier()); |
| 630 EXPECT_TRUE(notify_sessions_[0]->IsActive()); | 659 EXPECT_TRUE(notify_sessions_[0]->IsActive()); |
| 631 EXPECT_TRUE(notify_sessions_[1]->IsActive()); | 660 EXPECT_TRUE(notify_sessions_[1]->IsActive()); |
| 632 } | 661 } |
| 633 #endif // defined(OS_ANDROID) | 662 #endif // defined(OS_ANDROID) |
| 634 | 663 |
| 635 } // namespace device | 664 } // namespace device |
| OLD | NEW |