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

Side by Side Diff: device/bluetooth/bluetooth_remote_gatt_characteristic_unittest.cc

Issue 2708513002: bluetooth: Post a task when sending GATT events to simulate another thread. (Closed)
Patch Set: Remove unnecessary if statement Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <stdint.h> 5 #include <stdint.h>
6 #include <utility> 6 #include <utility>
7 7
8 #include "base/bind.h" 8 #include "base/bind.h"
9 #include "base/macros.h" 9 #include "base/macros.h"
10 #include "base/run_loop.h" 10 #include "base/run_loop.h"
(...skipping 18 matching lines...) Expand all
29 public: 29 public:
30 // Creates adapter_, device_, service_, characteristic1_, & characteristic2_. 30 // Creates adapter_, device_, service_, characteristic1_, & characteristic2_.
31 // |properties| will be used for each characteristic. 31 // |properties| will be used for each characteristic.
32 void FakeCharacteristicBoilerplate(int properties = 0) { 32 void FakeCharacteristicBoilerplate(int properties = 0) {
33 InitWithFakeAdapter(); 33 InitWithFakeAdapter();
34 StartLowEnergyDiscoverySession(); 34 StartLowEnergyDiscoverySession();
35 device_ = SimulateLowEnergyDevice(3); 35 device_ = SimulateLowEnergyDevice(3);
36 device_->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), 36 device_->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
37 GetConnectErrorCallback(Call::NOT_EXPECTED)); 37 GetConnectErrorCallback(Call::NOT_EXPECTED));
38 SimulateGattConnection(device_); 38 SimulateGattConnection(device_);
39 base::RunLoop().RunUntilIdle();
40
39 std::vector<std::string> services; 41 std::vector<std::string> services;
40 std::string uuid("00000000-0000-1000-8000-00805f9b34fb"); 42 std::string uuid("00000000-0000-1000-8000-00805f9b34fb");
41 services.push_back(uuid); 43 services.push_back(uuid);
42 SimulateGattServicesDiscovered(device_, services); 44 SimulateGattServicesDiscovered(device_, services);
45 base::RunLoop().RunUntilIdle();
43 ASSERT_EQ(1u, device_->GetGattServices().size()); 46 ASSERT_EQ(1u, device_->GetGattServices().size());
44 service_ = device_->GetGattServices()[0]; 47 service_ = device_->GetGattServices()[0];
45 SimulateGattCharacteristic(service_, uuid, properties); 48 SimulateGattCharacteristic(service_, uuid, properties);
46 SimulateGattCharacteristic(service_, uuid, properties); 49 SimulateGattCharacteristic(service_, uuid, properties);
47 ASSERT_EQ(2u, service_->GetCharacteristics().size()); 50 ASSERT_EQ(2u, service_->GetCharacteristics().size());
48 characteristic1_ = service_->GetCharacteristics()[0]; 51 characteristic1_ = service_->GetCharacteristics()[0];
49 characteristic2_ = service_->GetCharacteristics()[1]; 52 characteristic2_ = service_->GetCharacteristics()[1];
50 ResetEventCounts(); 53 ResetEventCounts();
51 } 54 }
52 55
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
105 GetGattErrorCallback(Call::EXPECTED)); 108 GetGattErrorCallback(Call::EXPECTED));
106 return; 109 return;
107 } 110 }
108 111
109 characteristic1_->StartNotifySession( 112 characteristic1_->StartNotifySession(
110 GetNotifyCallback(Call::EXPECTED), 113 GetNotifyCallback(Call::EXPECTED),
111 GetGattErrorCallback(Call::NOT_EXPECTED)); 114 GetGattErrorCallback(Call::NOT_EXPECTED));
112 115
113 EXPECT_EQ(0, callback_count_); 116 EXPECT_EQ(0, callback_count_);
114 SimulateGattNotifySessionStarted(characteristic1_); 117 SimulateGattNotifySessionStarted(characteristic1_);
118 base::RunLoop().RunUntilIdle();
115 ExpectedChangeNotifyValueAttempts(1); 119 ExpectedChangeNotifyValueAttempts(1);
116 EXPECT_EQ(1, callback_count_); 120 EXPECT_EQ(1, callback_count_);
117 EXPECT_EQ(0, error_callback_count_); 121 EXPECT_EQ(0, error_callback_count_);
118 ASSERT_EQ(1u, notify_sessions_.size()); 122 ASSERT_EQ(1u, notify_sessions_.size());
119 ASSERT_TRUE(notify_sessions_[0]); 123 ASSERT_TRUE(notify_sessions_[0]);
120 EXPECT_EQ(characteristic1_->GetIdentifier(), 124 EXPECT_EQ(characteristic1_->GetIdentifier(),
121 notify_sessions_[0]->GetCharacteristicIdentifier()); 125 notify_sessions_[0]->GetCharacteristicIdentifier());
122 EXPECT_TRUE(notify_sessions_[0]->IsActive()); 126 EXPECT_TRUE(notify_sessions_[0]->IsActive());
123 127
124 // Verify the Client Characteristic Configuration descriptor was written to. 128 // Verify the Client Characteristic Configuration descriptor was written to.
125 ExpectedNotifyValue(notify_value_state); 129 ExpectedNotifyValue(notify_value_state);
126 } 130 }
127 131
128 BluetoothDevice* device_ = nullptr; 132 BluetoothDevice* device_ = nullptr;
129 BluetoothRemoteGattService* service_ = nullptr; 133 BluetoothRemoteGattService* service_ = nullptr;
130 BluetoothRemoteGattCharacteristic* characteristic1_ = nullptr; 134 BluetoothRemoteGattCharacteristic* characteristic1_ = nullptr;
131 BluetoothRemoteGattCharacteristic* characteristic2_ = nullptr; 135 BluetoothRemoteGattCharacteristic* characteristic2_ = nullptr;
132 }; 136 };
133 #endif 137 #endif
134 138
135 #if defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) 139 #if defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN)
136 TEST_F(BluetoothRemoteGattCharacteristicTest, GetIdentifier) { 140 TEST_F(BluetoothRemoteGattCharacteristicTest, GetIdentifier) {
137 if (!PlatformSupportsLowEnergy()) { 141 if (!PlatformSupportsLowEnergy()) {
138 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; 142 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
139 return; 143 return;
140 } 144 }
141 InitWithFakeAdapter(); 145 InitWithFakeAdapter();
146
142 StartLowEnergyDiscoverySession(); 147 StartLowEnergyDiscoverySession();
143 // 2 devices to verify unique IDs across them. 148 // 2 devices to verify unique IDs across them.
144 BluetoothDevice* device1 = SimulateLowEnergyDevice(3); 149 BluetoothDevice* device1 = SimulateLowEnergyDevice(3);
145 BluetoothDevice* device2 = SimulateLowEnergyDevice(4); 150 BluetoothDevice* device2 = SimulateLowEnergyDevice(4);
146 device1->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), 151 device1->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
147 GetConnectErrorCallback(Call::NOT_EXPECTED)); 152 GetConnectErrorCallback(Call::NOT_EXPECTED));
148 device2->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), 153 device2->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
149 GetConnectErrorCallback(Call::NOT_EXPECTED)); 154 GetConnectErrorCallback(Call::NOT_EXPECTED));
150 SimulateGattConnection(device1); 155 SimulateGattConnection(device1);
151 SimulateGattConnection(device2); 156 SimulateGattConnection(device2);
157 base::RunLoop().RunUntilIdle();
152 158
153 // 3 services (all with same UUID). 159 // 3 services (all with same UUID).
154 // 1 on the first device (to test characteristic instances across devices). 160 // 1 on the first device (to test characteristic instances across devices).
155 // 2 on the second device (to test same device, multiple service instances). 161 // 2 on the second device (to test same device, multiple service instances).
156 std::vector<std::string> services; 162 std::vector<std::string> services;
157 std::string uuid = "00000000-0000-1000-8000-00805f9b34fb"; 163 std::string uuid = "00000000-0000-1000-8000-00805f9b34fb";
158 services.push_back(uuid); 164 services.push_back(uuid);
159 SimulateGattServicesDiscovered(device1, services); 165 SimulateGattServicesDiscovered(device1, services);
166 base::RunLoop().RunUntilIdle();
160 services.push_back(uuid); 167 services.push_back(uuid);
161 SimulateGattServicesDiscovered(device2, services); 168 SimulateGattServicesDiscovered(device2, services);
169 base::RunLoop().RunUntilIdle();
162 BluetoothRemoteGattService* service1 = device1->GetGattServices()[0]; 170 BluetoothRemoteGattService* service1 = device1->GetGattServices()[0];
163 BluetoothRemoteGattService* service2 = device2->GetGattServices()[0]; 171 BluetoothRemoteGattService* service2 = device2->GetGattServices()[0];
164 BluetoothRemoteGattService* service3 = device2->GetGattServices()[1]; 172 BluetoothRemoteGattService* service3 = device2->GetGattServices()[1];
165 // 6 characteristics (same UUID), 2 on each service. 173 // 6 characteristics (same UUID), 2 on each service.
166 SimulateGattCharacteristic(service1, uuid, /* properties */ 0); 174 SimulateGattCharacteristic(service1, uuid, /* properties */ 0);
167 SimulateGattCharacteristic(service1, uuid, /* properties */ 0); 175 SimulateGattCharacteristic(service1, uuid, /* properties */ 0);
168 SimulateGattCharacteristic(service2, uuid, /* properties */ 0); 176 SimulateGattCharacteristic(service2, uuid, /* properties */ 0);
169 SimulateGattCharacteristic(service2, uuid, /* properties */ 0); 177 SimulateGattCharacteristic(service2, uuid, /* properties */ 0);
170 SimulateGattCharacteristic(service3, uuid, /* properties */ 0); 178 SimulateGattCharacteristic(service3, uuid, /* properties */ 0);
171 SimulateGattCharacteristic(service3, uuid, /* properties */ 0); 179 SimulateGattCharacteristic(service3, uuid, /* properties */ 0);
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
204 if (!PlatformSupportsLowEnergy()) { 212 if (!PlatformSupportsLowEnergy()) {
205 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; 213 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
206 return; 214 return;
207 } 215 }
208 InitWithFakeAdapter(); 216 InitWithFakeAdapter();
209 StartLowEnergyDiscoverySession(); 217 StartLowEnergyDiscoverySession();
210 BluetoothDevice* device = SimulateLowEnergyDevice(3); 218 BluetoothDevice* device = SimulateLowEnergyDevice(3);
211 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), 219 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
212 GetConnectErrorCallback(Call::NOT_EXPECTED)); 220 GetConnectErrorCallback(Call::NOT_EXPECTED));
213 SimulateGattConnection(device); 221 SimulateGattConnection(device);
222 base::RunLoop().RunUntilIdle();
214 std::vector<std::string> services; 223 std::vector<std::string> services;
215 services.push_back("00000000-0000-1000-8000-00805f9b34fb"); 224 services.push_back("00000000-0000-1000-8000-00805f9b34fb");
216 SimulateGattServicesDiscovered(device, services); 225 SimulateGattServicesDiscovered(device, services);
226 base::RunLoop().RunUntilIdle();
217 BluetoothRemoteGattService* service = device->GetGattServices()[0]; 227 BluetoothRemoteGattService* service = device->GetGattServices()[0];
218 228
219 // Create 3 characteristics. Two of them are duplicates. 229 // Create 3 characteristics. Two of them are duplicates.
220 std::string uuid_str1("11111111-0000-1000-8000-00805f9b34fb"); 230 std::string uuid_str1("11111111-0000-1000-8000-00805f9b34fb");
221 std::string uuid_str2("22222222-0000-1000-8000-00805f9b34fb"); 231 std::string uuid_str2("22222222-0000-1000-8000-00805f9b34fb");
222 BluetoothUUID uuid1(uuid_str1); 232 BluetoothUUID uuid1(uuid_str1);
223 BluetoothUUID uuid2(uuid_str2); 233 BluetoothUUID uuid2(uuid_str2);
224 SimulateGattCharacteristic(service, uuid_str1, /* properties */ 0); 234 SimulateGattCharacteristic(service, uuid_str1, /* properties */ 0);
225 SimulateGattCharacteristic(service, uuid_str2, /* properties */ 0); 235 SimulateGattCharacteristic(service, uuid_str2, /* properties */ 0);
226 SimulateGattCharacteristic(service, uuid_str2, /* properties */ 0); 236 SimulateGattCharacteristic(service, uuid_str2, /* properties */ 0);
(...skipping 19 matching lines...) Expand all
246 if (!PlatformSupportsLowEnergy()) { 256 if (!PlatformSupportsLowEnergy()) {
247 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; 257 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
248 return; 258 return;
249 } 259 }
250 InitWithFakeAdapter(); 260 InitWithFakeAdapter();
251 StartLowEnergyDiscoverySession(); 261 StartLowEnergyDiscoverySession();
252 BluetoothDevice* device = SimulateLowEnergyDevice(3); 262 BluetoothDevice* device = SimulateLowEnergyDevice(3);
253 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), 263 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
254 GetConnectErrorCallback(Call::NOT_EXPECTED)); 264 GetConnectErrorCallback(Call::NOT_EXPECTED));
255 SimulateGattConnection(device); 265 SimulateGattConnection(device);
266 base::RunLoop().RunUntilIdle();
256 std::vector<std::string> services; 267 std::vector<std::string> services;
257 std::string uuid("00000000-0000-1000-8000-00805f9b34fb"); 268 std::string uuid("00000000-0000-1000-8000-00805f9b34fb");
258 services.push_back(uuid); 269 services.push_back(uuid);
259 SimulateGattServicesDiscovered(device, services); 270 SimulateGattServicesDiscovered(device, services);
271 base::RunLoop().RunUntilIdle();
260 BluetoothRemoteGattService* service = device->GetGattServices()[0]; 272 BluetoothRemoteGattService* service = device->GetGattServices()[0];
261 273
262 // Create two characteristics with different properties: 274 // Create two characteristics with different properties:
263 SimulateGattCharacteristic(service, uuid, /* properties */ 0); 275 SimulateGattCharacteristic(service, uuid, /* properties */ 0);
264 SimulateGattCharacteristic(service, uuid, /* properties */ 7); 276 SimulateGattCharacteristic(service, uuid, /* properties */ 7);
265 277
266 // Read the properties. Because ordering is unknown swap as necessary. 278 // Read the properties. Because ordering is unknown swap as necessary.
267 int properties1 = service->GetCharacteristics()[0]->GetProperties(); 279 int properties1 = service->GetCharacteristics()[0]->GetProperties();
268 int properties2 = service->GetCharacteristics()[1]->GetProperties(); 280 int properties2 = service->GetCharacteristics()[1]->GetProperties();
269 if (properties2 == 0) 281 if (properties2 == 0)
(...skipping 25 matching lines...) Expand all
295 return; 307 return;
296 } 308 }
297 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate( 309 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(
298 BluetoothRemoteGattCharacteristic::PROPERTY_READ)); 310 BluetoothRemoteGattCharacteristic::PROPERTY_READ));
299 311
300 characteristic1_->ReadRemoteCharacteristic( 312 characteristic1_->ReadRemoteCharacteristic(
301 GetReadValueCallback(Call::EXPECTED), 313 GetReadValueCallback(Call::EXPECTED),
302 GetGattErrorCallback(Call::NOT_EXPECTED)); 314 GetGattErrorCallback(Call::NOT_EXPECTED));
303 std::vector<uint8_t> empty_vector; 315 std::vector<uint8_t> empty_vector;
304 SimulateGattCharacteristicRead(characteristic1_, empty_vector); 316 SimulateGattCharacteristicRead(characteristic1_, empty_vector);
317 base::RunLoop().RunUntilIdle();
305 318
306 // Duplicate read reported from OS shouldn't cause a problem: 319 // Duplicate read reported from OS shouldn't cause a problem:
307 SimulateGattCharacteristicRead(characteristic1_, empty_vector); 320 SimulateGattCharacteristicRead(characteristic1_, empty_vector);
321 base::RunLoop().RunUntilIdle();
308 322
309 EXPECT_EQ(1, gatt_read_characteristic_attempts_); 323 EXPECT_EQ(1, gatt_read_characteristic_attempts_);
310 EXPECT_EQ(empty_vector, last_read_value_); 324 EXPECT_EQ(empty_vector, last_read_value_);
311 EXPECT_EQ(empty_vector, characteristic1_->GetValue()); 325 EXPECT_EQ(empty_vector, characteristic1_->GetValue());
312 } 326 }
313 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) 327 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN)
314 328
315 #if defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) 329 #if defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN)
316 // Tests WriteRemoteCharacteristic with empty value buffer. 330 // Tests WriteRemoteCharacteristic with empty value buffer.
317 TEST_F(BluetoothRemoteGattCharacteristicTest, WriteRemoteCharacteristic_Empty) { 331 TEST_F(BluetoothRemoteGattCharacteristicTest, WriteRemoteCharacteristic_Empty) {
318 if (!PlatformSupportsLowEnergy()) { 332 if (!PlatformSupportsLowEnergy()) {
319 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; 333 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
320 return; 334 return;
321 } 335 }
322 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate( 336 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(
323 BluetoothRemoteGattCharacteristic::PROPERTY_WRITE)); 337 BluetoothRemoteGattCharacteristic::PROPERTY_WRITE));
324 338
325 std::vector<uint8_t> empty_vector; 339 std::vector<uint8_t> empty_vector;
326 characteristic1_->WriteRemoteCharacteristic( 340 characteristic1_->WriteRemoteCharacteristic(
327 empty_vector, GetCallback(Call::EXPECTED), 341 empty_vector, GetCallback(Call::EXPECTED),
328 GetGattErrorCallback(Call::NOT_EXPECTED)); 342 GetGattErrorCallback(Call::NOT_EXPECTED));
329 SimulateGattCharacteristicWrite(characteristic1_); 343 SimulateGattCharacteristicWrite(characteristic1_);
344 base::RunLoop().RunUntilIdle();
330 EXPECT_EQ(1, gatt_write_characteristic_attempts_); 345 EXPECT_EQ(1, gatt_write_characteristic_attempts_);
331 346
332 // Duplicate write reported from OS shouldn't cause a problem: 347 // Duplicate write reported from OS shouldn't cause a problem:
333 SimulateGattCharacteristicWrite(characteristic1_); 348 SimulateGattCharacteristicWrite(characteristic1_);
349 base::RunLoop().RunUntilIdle();
334 350
335 EXPECT_EQ(empty_vector, last_write_value_); 351 EXPECT_EQ(empty_vector, last_write_value_);
336 } 352 }
337 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) 353 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN)
338 354
339 #if defined(OS_ANDROID) || defined(OS_WIN) 355 #if defined(OS_ANDROID) || defined(OS_WIN)
340 // Tests ReadRemoteCharacteristic completing after Chrome objects are deleted. 356 // Tests ReadRemoteCharacteristic completing after Chrome objects are deleted.
341 // macOS: Not applicable: This can never happen if CBPeripheral delegate is set 357 // macOS: Not applicable: This can never happen if CBPeripheral delegate is set
342 // to nil. 358 // to nil.
343 TEST_F(BluetoothRemoteGattCharacteristicTest, 359 TEST_F(BluetoothRemoteGattCharacteristicTest,
344 ReadRemoteCharacteristic_AfterDeleted) { 360 ReadRemoteCharacteristic_AfterDeleted) {
345 if (!PlatformSupportsLowEnergy()) { 361 if (!PlatformSupportsLowEnergy()) {
346 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; 362 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
347 return; 363 return;
348 } 364 }
349 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate( 365 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(
350 BluetoothRemoteGattCharacteristic::PROPERTY_READ)); 366 BluetoothRemoteGattCharacteristic::PROPERTY_READ));
351 367
352 characteristic1_->ReadRemoteCharacteristic( 368 characteristic1_->ReadRemoteCharacteristic(
353 GetReadValueCallback(Call::NOT_EXPECTED), 369 GetReadValueCallback(Call::NOT_EXPECTED),
354 GetGattErrorCallback(Call::EXPECTED)); 370 GetGattErrorCallback(Call::EXPECTED));
355 371
356 RememberCharacteristicForSubsequentAction(characteristic1_); 372 RememberCharacteristicForSubsequentAction(characteristic1_);
357 DeleteDevice(device_); // TODO(576906) delete only the characteristic. 373 DeleteDevice(device_); // TODO(576906) delete only the characteristic.
358 374
359 std::vector<uint8_t> empty_vector; 375 std::vector<uint8_t> empty_vector;
360 SimulateGattCharacteristicRead(/* use remembered characteristic */ nullptr, 376 SimulateGattCharacteristicRead(/* use remembered characteristic */ nullptr,
361 empty_vector); 377 empty_vector);
378 base::RunLoop().RunUntilIdle();
362 EXPECT_TRUE("Did not crash!"); 379 EXPECT_TRUE("Did not crash!");
363 } 380 }
364 #endif // defined(OS_ANDROID) || defined(OS_WIN) 381 #endif // defined(OS_ANDROID) || defined(OS_WIN)
365 382
366 // TODO(crbug.com/663131): Enable test on windows when disconnection is 383 // TODO(crbug.com/663131): Enable test on windows when disconnection is
367 // implemented. 384 // implemented.
368 #if defined(OS_ANDROID) || defined(OS_MACOSX) 385 #if defined(OS_ANDROID) || defined(OS_MACOSX)
369 TEST_F(BluetoothRemoteGattCharacteristicTest, 386 TEST_F(BluetoothRemoteGattCharacteristicTest,
370 ReadRemoteCharacteristic_Disconnected) { 387 ReadRemoteCharacteristic_Disconnected) {
371 if (!PlatformSupportsLowEnergy()) { 388 if (!PlatformSupportsLowEnergy()) {
(...skipping 10 matching lines...) Expand all
382 // Set up for receiving a read response after disconnection. 399 // Set up for receiving a read response after disconnection.
383 // On macOS no events arrive after disconnection so there is no point 400 // On macOS no events arrive after disconnection so there is no point
384 // in building the infrastructure to test this behavior. FYI 401 // in building the infrastructure to test this behavior. FYI
385 // the code CHECKs that responses arrive only when the device is connected. 402 // the code CHECKs that responses arrive only when the device is connected.
386 #if !defined(OS_MACOSX) 403 #if !defined(OS_MACOSX)
387 RememberCharacteristicForSubsequentAction(characteristic1_); 404 RememberCharacteristicForSubsequentAction(characteristic1_);
388 #endif 405 #endif
389 406
390 ASSERT_EQ(1u, adapter_->GetDevices().size()); 407 ASSERT_EQ(1u, adapter_->GetDevices().size());
391 SimulateGattDisconnection(adapter_->GetDevices()[0]); 408 SimulateGattDisconnection(adapter_->GetDevices()[0]);
409 base::RunLoop().RunUntilIdle();
392 410
393 EXPECT_EQ(BluetoothRemoteGattService::GATT_ERROR_FAILED, 411 EXPECT_EQ(BluetoothRemoteGattService::GATT_ERROR_FAILED,
394 last_gatt_error_code_); 412 last_gatt_error_code_);
395 413
396 // Dispatch read response after disconnection. See above explanation for why 414 // Dispatch read response after disconnection. See above explanation for why
397 // we don't do this in macOS. 415 // we don't do this in macOS.
398 #if !defined(OS_MACOSX) 416 #if !defined(OS_MACOSX)
399 std::vector<uint8_t> empty_vector; 417 std::vector<uint8_t> empty_vector;
400 SimulateGattCharacteristicRead(nullptr /* use remembered characteristic */, 418 SimulateGattCharacteristicRead(nullptr /* use remembered characteristic */,
401 empty_vector); 419 empty_vector);
420 base::RunLoop().RunUntilIdle();
402 #endif 421 #endif
403 } 422 }
404 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) 423 #endif // defined(OS_ANDROID) || defined(OS_MACOSX)
405 424
406 #if defined(OS_ANDROID) || defined(OS_WIN) 425 #if defined(OS_ANDROID) || defined(OS_WIN)
407 // Tests WriteRemoteCharacteristic completing after Chrome objects are deleted. 426 // Tests WriteRemoteCharacteristic completing after Chrome objects are deleted.
408 // macOS: Not applicable: This can never happen if CBPeripheral delegate is set 427 // macOS: Not applicable: This can never happen if CBPeripheral delegate is set
409 // to nil. 428 // to nil.
410 TEST_F(BluetoothRemoteGattCharacteristicTest, 429 TEST_F(BluetoothRemoteGattCharacteristicTest,
411 WriteRemoteCharacteristic_AfterDeleted) { 430 WriteRemoteCharacteristic_AfterDeleted) {
412 if (!PlatformSupportsLowEnergy()) { 431 if (!PlatformSupportsLowEnergy()) {
413 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; 432 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
414 return; 433 return;
415 } 434 }
416 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate( 435 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(
417 BluetoothRemoteGattCharacteristic::PROPERTY_WRITE)); 436 BluetoothRemoteGattCharacteristic::PROPERTY_WRITE));
418 437
419 std::vector<uint8_t> empty_vector; 438 std::vector<uint8_t> empty_vector;
420 characteristic1_->WriteRemoteCharacteristic( 439 characteristic1_->WriteRemoteCharacteristic(
421 empty_vector, GetCallback(Call::NOT_EXPECTED), 440 empty_vector, GetCallback(Call::NOT_EXPECTED),
422 GetGattErrorCallback(Call::EXPECTED)); 441 GetGattErrorCallback(Call::EXPECTED));
423 442
424 RememberCharacteristicForSubsequentAction(characteristic1_); 443 RememberCharacteristicForSubsequentAction(characteristic1_);
425 DeleteDevice(device_); // TODO(576906) delete only the characteristic. 444 DeleteDevice(device_); // TODO(576906) delete only the characteristic.
426 445
427 SimulateGattCharacteristicWrite(/* use remembered characteristic */ nullptr); 446 SimulateGattCharacteristicWrite(/* use remembered characteristic */ nullptr);
447 base::RunLoop().RunUntilIdle();
428 EXPECT_TRUE("Did not crash!"); 448 EXPECT_TRUE("Did not crash!");
429 } 449 }
430 #endif // defined(OS_ANDROID) || defined(OS_WIN) 450 #endif // defined(OS_ANDROID) || defined(OS_WIN)
431 451
432 // TODO(crbug.com/663131): Enable test on windows when disconnection is 452 // TODO(crbug.com/663131): Enable test on windows when disconnection is
433 // implemented. 453 // implemented.
434 #if defined(OS_ANDROID) || defined(OS_MACOSX) 454 #if defined(OS_ANDROID) || defined(OS_MACOSX)
435 TEST_F(BluetoothRemoteGattCharacteristicTest, 455 TEST_F(BluetoothRemoteGattCharacteristicTest,
436 WriteRemoteCharacteristic_Disconnected) { 456 WriteRemoteCharacteristic_Disconnected) {
437 if (!PlatformSupportsLowEnergy()) { 457 if (!PlatformSupportsLowEnergy()) {
(...skipping 11 matching lines...) Expand all
449 // Set up for receiving a write response after disconnection. 469 // Set up for receiving a write response after disconnection.
450 // On macOS no events arrive after disconnection so there is no point 470 // On macOS no events arrive after disconnection so there is no point
451 // in building the infrastructure to test this behavior. FYI 471 // in building the infrastructure to test this behavior. FYI
452 // the code CHECKs that responses arrive only when the device is connected. 472 // the code CHECKs that responses arrive only when the device is connected.
453 #if !defined(OS_MACOSX) 473 #if !defined(OS_MACOSX)
454 RememberCharacteristicForSubsequentAction(characteristic1_); 474 RememberCharacteristicForSubsequentAction(characteristic1_);
455 #endif // !defined(OS_MACOSX) 475 #endif // !defined(OS_MACOSX)
456 476
457 ASSERT_EQ(1u, adapter_->GetDevices().size()); 477 ASSERT_EQ(1u, adapter_->GetDevices().size());
458 SimulateGattDisconnection(adapter_->GetDevices()[0]); 478 SimulateGattDisconnection(adapter_->GetDevices()[0]);
479 base::RunLoop().RunUntilIdle();
459 480
460 EXPECT_EQ(BluetoothRemoteGattService::GATT_ERROR_FAILED, 481 EXPECT_EQ(BluetoothRemoteGattService::GATT_ERROR_FAILED,
461 last_gatt_error_code_); 482 last_gatt_error_code_);
462 483
463 // Dispatch write response after disconnection. See above explanation for why 484 // Dispatch write response after disconnection. See above explanation for why
464 // we don't do this in macOS. 485 // we don't do this in macOS.
465 #if !defined(OS_MACOSX) 486 #if !defined(OS_MACOSX)
466 SimulateGattCharacteristicWrite(/* use remembered characteristic */ nullptr); 487 SimulateGattCharacteristicWrite(/* use remembered characteristic */ nullptr);
488 base::RunLoop().RunUntilIdle();
467 #endif // !defined(OS_MACOSX) 489 #endif // !defined(OS_MACOSX)
468 } 490 }
469 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) 491 #endif // defined(OS_ANDROID) || defined(OS_MACOSX)
470 492
471 #if defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) 493 #if defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN)
472 // Tests ReadRemoteCharacteristic and GetValue with non-empty value buffer. 494 // Tests ReadRemoteCharacteristic and GetValue with non-empty value buffer.
473 TEST_F(BluetoothRemoteGattCharacteristicTest, ReadRemoteCharacteristic) { 495 TEST_F(BluetoothRemoteGattCharacteristicTest, ReadRemoteCharacteristic) {
474 if (!PlatformSupportsLowEnergy()) { 496 if (!PlatformSupportsLowEnergy()) {
475 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; 497 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
476 return; 498 return;
477 } 499 }
478 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate( 500 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(
479 BluetoothRemoteGattCharacteristic::PROPERTY_READ)); 501 BluetoothRemoteGattCharacteristic::PROPERTY_READ));
480 502
481 characteristic1_->ReadRemoteCharacteristic( 503 characteristic1_->ReadRemoteCharacteristic(
482 GetReadValueCallback(Call::EXPECTED), 504 GetReadValueCallback(Call::EXPECTED),
483 GetGattErrorCallback(Call::NOT_EXPECTED)); 505 GetGattErrorCallback(Call::NOT_EXPECTED));
484 506
485 std::vector<uint8_t> test_vector = {0, 1, 2, 3, 4, 0xf, 0xf0, 0xff}; 507 std::vector<uint8_t> test_vector = {0, 1, 2, 3, 4, 0xf, 0xf0, 0xff};
486 SimulateGattCharacteristicRead(characteristic1_, test_vector); 508 SimulateGattCharacteristicRead(characteristic1_, test_vector);
509 base::RunLoop().RunUntilIdle();
487 510
488 // Duplicate read reported from OS shouldn't cause a problem: 511 // Duplicate read reported from OS shouldn't cause a problem:
489 std::vector<uint8_t> empty_vector; 512 std::vector<uint8_t> empty_vector;
490 SimulateGattCharacteristicRead(characteristic1_, empty_vector); 513 SimulateGattCharacteristicRead(characteristic1_, empty_vector);
514 base::RunLoop().RunUntilIdle();
491 515
492 EXPECT_EQ(1, gatt_read_characteristic_attempts_); 516 EXPECT_EQ(1, gatt_read_characteristic_attempts_);
493 EXPECT_EQ(test_vector, last_read_value_); 517 EXPECT_EQ(test_vector, last_read_value_);
494 EXPECT_EQ(test_vector, characteristic1_->GetValue()); 518 EXPECT_EQ(test_vector, characteristic1_->GetValue());
495 } 519 }
496 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) 520 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN)
497 521
498 #if defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) 522 #if defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN)
499 // Callback that make sure GattCharacteristicValueChanged has been called 523 // Callback that make sure GattCharacteristicValueChanged has been called
500 // before the callback runs. 524 // before the callback runs.
(...skipping 18 matching lines...) Expand all
519 543
520 TestBluetoothAdapterObserver observer(adapter_); 544 TestBluetoothAdapterObserver observer(adapter_);
521 545
522 characteristic1_->ReadRemoteCharacteristic( 546 characteristic1_->ReadRemoteCharacteristic(
523 base::Bind(test_callback, GetReadValueCallback(Call::EXPECTED), 547 base::Bind(test_callback, GetReadValueCallback(Call::EXPECTED),
524 base::ConstRef(observer)), 548 base::ConstRef(observer)),
525 GetGattErrorCallback(Call::NOT_EXPECTED)); 549 GetGattErrorCallback(Call::NOT_EXPECTED));
526 550
527 std::vector<uint8_t> test_vector = {0, 1, 2, 3, 4, 0xf, 0xf0, 0xff}; 551 std::vector<uint8_t> test_vector = {0, 1, 2, 3, 4, 0xf, 0xf0, 0xff};
528 SimulateGattCharacteristicRead(characteristic1_, test_vector); 552 SimulateGattCharacteristicRead(characteristic1_, test_vector);
553 base::RunLoop().RunUntilIdle();
529 554
530 EXPECT_EQ(1, observer.gatt_characteristic_value_changed_count()); 555 EXPECT_EQ(1, observer.gatt_characteristic_value_changed_count());
531 EXPECT_EQ(characteristic1_->GetIdentifier(), 556 EXPECT_EQ(characteristic1_->GetIdentifier(),
532 observer.last_gatt_characteristic_id()); 557 observer.last_gatt_characteristic_id());
533 EXPECT_EQ(characteristic1_->GetUUID(), 558 EXPECT_EQ(characteristic1_->GetUUID(),
534 observer.last_gatt_characteristic_uuid()); 559 observer.last_gatt_characteristic_uuid());
535 EXPECT_EQ(test_vector, observer.last_changed_characteristic_value()); 560 EXPECT_EQ(test_vector, observer.last_changed_characteristic_value());
536 } 561 }
537 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) 562 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN)
538 563
539 #if defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) 564 #if defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN)
540 // Tests WriteRemoteCharacteristic with non-empty value buffer. 565 // Tests WriteRemoteCharacteristic with non-empty value buffer.
541 TEST_F(BluetoothRemoteGattCharacteristicTest, WriteRemoteCharacteristic) { 566 TEST_F(BluetoothRemoteGattCharacteristicTest, WriteRemoteCharacteristic) {
542 if (!PlatformSupportsLowEnergy()) { 567 if (!PlatformSupportsLowEnergy()) {
543 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; 568 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
544 return; 569 return;
545 } 570 }
546 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate( 571 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(
547 BluetoothRemoteGattCharacteristic::PROPERTY_WRITE)); 572 BluetoothRemoteGattCharacteristic::PROPERTY_WRITE));
548 573
549 TestBluetoothAdapterObserver observer(adapter_); 574 TestBluetoothAdapterObserver observer(adapter_);
550 575
551 uint8_t values[] = {0, 1, 2, 3, 4, 0xf, 0xf0, 0xff}; 576 uint8_t values[] = {0, 1, 2, 3, 4, 0xf, 0xf0, 0xff};
552 std::vector<uint8_t> test_vector(values, values + arraysize(values)); 577 std::vector<uint8_t> test_vector(values, values + arraysize(values));
553 characteristic1_->WriteRemoteCharacteristic( 578 characteristic1_->WriteRemoteCharacteristic(
554 test_vector, GetCallback(Call::EXPECTED), 579 test_vector, GetCallback(Call::EXPECTED),
555 GetGattErrorCallback(Call::NOT_EXPECTED)); 580 GetGattErrorCallback(Call::NOT_EXPECTED));
556 581
557 SimulateGattCharacteristicWrite(characteristic1_); 582 SimulateGattCharacteristicWrite(characteristic1_);
583 base::RunLoop().RunUntilIdle();
558 584
559 EXPECT_EQ(1, gatt_write_characteristic_attempts_); 585 EXPECT_EQ(1, gatt_write_characteristic_attempts_);
560 #if !defined(OS_WIN) 586 #if !defined(OS_WIN)
561 // TODO(crbug.com/653291): remove this #if once the bug on windows is fixed. 587 // TODO(crbug.com/653291): remove this #if once the bug on windows is fixed.
562 EXPECT_EQ(0, observer.gatt_characteristic_value_changed_count()); 588 EXPECT_EQ(0, observer.gatt_characteristic_value_changed_count());
563 #endif 589 #endif
564 EXPECT_EQ(test_vector, last_write_value_); 590 EXPECT_EQ(test_vector, last_write_value_);
565 } 591 }
566 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) 592 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN)
567 593
568 #if defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) 594 #if defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN)
569 // Tests ReadRemoteCharacteristic and GetValue multiple times. 595 // Tests ReadRemoteCharacteristic and GetValue multiple times.
570 TEST_F(BluetoothRemoteGattCharacteristicTest, ReadRemoteCharacteristic_Twice) { 596 TEST_F(BluetoothRemoteGattCharacteristicTest, ReadRemoteCharacteristic_Twice) {
571 if (!PlatformSupportsLowEnergy()) { 597 if (!PlatformSupportsLowEnergy()) {
572 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; 598 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
573 return; 599 return;
574 } 600 }
575 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate( 601 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(
576 BluetoothRemoteGattCharacteristic::PROPERTY_READ)); 602 BluetoothRemoteGattCharacteristic::PROPERTY_READ));
577 603
578 characteristic1_->ReadRemoteCharacteristic( 604 characteristic1_->ReadRemoteCharacteristic(
579 GetReadValueCallback(Call::EXPECTED), 605 GetReadValueCallback(Call::EXPECTED),
580 GetGattErrorCallback(Call::NOT_EXPECTED)); 606 GetGattErrorCallback(Call::NOT_EXPECTED));
581 607
582 uint8_t values[] = {0, 1, 2, 3, 4, 0xf, 0xf0, 0xff}; 608 uint8_t values[] = {0, 1, 2, 3, 4, 0xf, 0xf0, 0xff};
583 std::vector<uint8_t> test_vector(values, values + arraysize(values)); 609 std::vector<uint8_t> test_vector(values, values + arraysize(values));
584 SimulateGattCharacteristicRead(characteristic1_, test_vector); 610 SimulateGattCharacteristicRead(characteristic1_, test_vector);
611 base::RunLoop().RunUntilIdle();
585 EXPECT_EQ(1, gatt_read_characteristic_attempts_); 612 EXPECT_EQ(1, gatt_read_characteristic_attempts_);
586 EXPECT_EQ(1, callback_count_); 613 EXPECT_EQ(1, callback_count_);
587 EXPECT_EQ(0, error_callback_count_); 614 EXPECT_EQ(0, error_callback_count_);
588 EXPECT_EQ(test_vector, last_read_value_); 615 EXPECT_EQ(test_vector, last_read_value_);
589 EXPECT_EQ(test_vector, characteristic1_->GetValue()); 616 EXPECT_EQ(test_vector, characteristic1_->GetValue());
590 617
591 // Read again, with different value: 618 // Read again, with different value:
592 ResetEventCounts(); 619 ResetEventCounts();
593 characteristic1_->ReadRemoteCharacteristic( 620 characteristic1_->ReadRemoteCharacteristic(
594 GetReadValueCallback(Call::EXPECTED), 621 GetReadValueCallback(Call::EXPECTED),
595 GetGattErrorCallback(Call::NOT_EXPECTED)); 622 GetGattErrorCallback(Call::NOT_EXPECTED));
596 std::vector<uint8_t> empty_vector; 623 std::vector<uint8_t> empty_vector;
597 SimulateGattCharacteristicRead(characteristic1_, empty_vector); 624 SimulateGattCharacteristicRead(characteristic1_, empty_vector);
625 base::RunLoop().RunUntilIdle();
598 EXPECT_EQ(1, gatt_read_characteristic_attempts_); 626 EXPECT_EQ(1, gatt_read_characteristic_attempts_);
599 EXPECT_EQ(1, callback_count_); 627 EXPECT_EQ(1, callback_count_);
600 EXPECT_EQ(0, error_callback_count_); 628 EXPECT_EQ(0, error_callback_count_);
601 EXPECT_EQ(empty_vector, last_read_value_); 629 EXPECT_EQ(empty_vector, last_read_value_);
602 EXPECT_EQ(empty_vector, characteristic1_->GetValue()); 630 EXPECT_EQ(empty_vector, characteristic1_->GetValue());
603 } 631 }
604 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) 632 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN)
605 633
606 #if defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) 634 #if defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN)
607 // Tests WriteRemoteCharacteristic multiple times. 635 // Tests WriteRemoteCharacteristic multiple times.
608 TEST_F(BluetoothRemoteGattCharacteristicTest, WriteRemoteCharacteristic_Twice) { 636 TEST_F(BluetoothRemoteGattCharacteristicTest, WriteRemoteCharacteristic_Twice) {
609 if (!PlatformSupportsLowEnergy()) { 637 if (!PlatformSupportsLowEnergy()) {
610 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; 638 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
611 return; 639 return;
612 } 640 }
613 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate( 641 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(
614 BluetoothRemoteGattCharacteristic::PROPERTY_WRITE)); 642 BluetoothRemoteGattCharacteristic::PROPERTY_WRITE));
615 643
616 uint8_t values[] = {0, 1, 2, 3, 4, 0xf, 0xf0, 0xff}; 644 uint8_t values[] = {0, 1, 2, 3, 4, 0xf, 0xf0, 0xff};
617 std::vector<uint8_t> test_vector(values, values + arraysize(values)); 645 std::vector<uint8_t> test_vector(values, values + arraysize(values));
618 characteristic1_->WriteRemoteCharacteristic( 646 characteristic1_->WriteRemoteCharacteristic(
619 test_vector, GetCallback(Call::EXPECTED), 647 test_vector, GetCallback(Call::EXPECTED),
620 GetGattErrorCallback(Call::NOT_EXPECTED)); 648 GetGattErrorCallback(Call::NOT_EXPECTED));
621 649
622 SimulateGattCharacteristicWrite(characteristic1_); 650 SimulateGattCharacteristicWrite(characteristic1_);
651 base::RunLoop().RunUntilIdle();
623 EXPECT_EQ(1, gatt_write_characteristic_attempts_); 652 EXPECT_EQ(1, gatt_write_characteristic_attempts_);
624 EXPECT_EQ(1, callback_count_); 653 EXPECT_EQ(1, callback_count_);
625 EXPECT_EQ(0, error_callback_count_); 654 EXPECT_EQ(0, error_callback_count_);
626 EXPECT_EQ(test_vector, last_write_value_); 655 EXPECT_EQ(test_vector, last_write_value_);
627 656
628 // Write again, with different value: 657 // Write again, with different value:
629 ResetEventCounts(); 658 ResetEventCounts();
630 std::vector<uint8_t> empty_vector; 659 std::vector<uint8_t> empty_vector;
631 characteristic1_->WriteRemoteCharacteristic( 660 characteristic1_->WriteRemoteCharacteristic(
632 empty_vector, GetCallback(Call::EXPECTED), 661 empty_vector, GetCallback(Call::EXPECTED),
633 GetGattErrorCallback(Call::NOT_EXPECTED)); 662 GetGattErrorCallback(Call::NOT_EXPECTED));
634 663
635 SimulateGattCharacteristicWrite(characteristic1_); 664 SimulateGattCharacteristicWrite(characteristic1_);
665 base::RunLoop().RunUntilIdle();
636 EXPECT_EQ(1, gatt_write_characteristic_attempts_); 666 EXPECT_EQ(1, gatt_write_characteristic_attempts_);
637 EXPECT_EQ(1, callback_count_); 667 EXPECT_EQ(1, callback_count_);
638 EXPECT_EQ(0, error_callback_count_); 668 EXPECT_EQ(0, error_callback_count_);
639 EXPECT_EQ(empty_vector, last_write_value_); 669 EXPECT_EQ(empty_vector, last_write_value_);
640 } 670 }
641 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) 671 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN)
642 672
643 #if defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) 673 #if defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN)
644 // Tests ReadRemoteCharacteristic on two characteristics. 674 // Tests ReadRemoteCharacteristic on two characteristics.
645 TEST_F(BluetoothRemoteGattCharacteristicTest, 675 TEST_F(BluetoothRemoteGattCharacteristicTest,
(...skipping 10 matching lines...) Expand all
656 GetGattErrorCallback(Call::NOT_EXPECTED)); 686 GetGattErrorCallback(Call::NOT_EXPECTED));
657 characteristic2_->ReadRemoteCharacteristic( 687 characteristic2_->ReadRemoteCharacteristic(
658 GetReadValueCallback(Call::EXPECTED), 688 GetReadValueCallback(Call::EXPECTED),
659 GetGattErrorCallback(Call::NOT_EXPECTED)); 689 GetGattErrorCallback(Call::NOT_EXPECTED));
660 EXPECT_EQ(0, callback_count_); 690 EXPECT_EQ(0, callback_count_);
661 EXPECT_EQ(0, error_callback_count_); 691 EXPECT_EQ(0, error_callback_count_);
662 692
663 std::vector<uint8_t> test_vector1; 693 std::vector<uint8_t> test_vector1;
664 test_vector1.push_back(111); 694 test_vector1.push_back(111);
665 SimulateGattCharacteristicRead(characteristic1_, test_vector1); 695 SimulateGattCharacteristicRead(characteristic1_, test_vector1);
696 base::RunLoop().RunUntilIdle();
666 EXPECT_EQ(test_vector1, last_read_value_); 697 EXPECT_EQ(test_vector1, last_read_value_);
667 698
668 std::vector<uint8_t> test_vector2; 699 std::vector<uint8_t> test_vector2;
669 test_vector2.push_back(222); 700 test_vector2.push_back(222);
670 SimulateGattCharacteristicRead(characteristic2_, test_vector2); 701 SimulateGattCharacteristicRead(characteristic2_, test_vector2);
702 base::RunLoop().RunUntilIdle();
671 EXPECT_EQ(test_vector2, last_read_value_); 703 EXPECT_EQ(test_vector2, last_read_value_);
672 704
673 EXPECT_EQ(2, gatt_read_characteristic_attempts_); 705 EXPECT_EQ(2, gatt_read_characteristic_attempts_);
674 EXPECT_EQ(2, callback_count_); 706 EXPECT_EQ(2, callback_count_);
675 EXPECT_EQ(0, error_callback_count_); 707 EXPECT_EQ(0, error_callback_count_);
676 EXPECT_EQ(test_vector1, characteristic1_->GetValue()); 708 EXPECT_EQ(test_vector1, characteristic1_->GetValue());
677 EXPECT_EQ(test_vector2, characteristic2_->GetValue()); 709 EXPECT_EQ(test_vector2, characteristic2_->GetValue());
678 } 710 }
679 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) 711 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN)
680 712
(...skipping 23 matching lines...) Expand all
704 test_vector2, GetCallback(Call::EXPECTED), 736 test_vector2, GetCallback(Call::EXPECTED),
705 GetGattErrorCallback(Call::NOT_EXPECTED)); 737 GetGattErrorCallback(Call::NOT_EXPECTED));
706 #if defined(OS_ANDROID) || defined(OS_MACOSX) 738 #if defined(OS_ANDROID) || defined(OS_MACOSX)
707 EXPECT_EQ(test_vector2, last_write_value_); 739 EXPECT_EQ(test_vector2, last_write_value_);
708 #endif 740 #endif
709 741
710 EXPECT_EQ(0, callback_count_); 742 EXPECT_EQ(0, callback_count_);
711 EXPECT_EQ(0, error_callback_count_); 743 EXPECT_EQ(0, error_callback_count_);
712 744
713 SimulateGattCharacteristicWrite(characteristic1_); 745 SimulateGattCharacteristicWrite(characteristic1_);
746 base::RunLoop().RunUntilIdle();
714 #if !(defined(OS_ANDROID) || defined(OS_MACOSX)) 747 #if !(defined(OS_ANDROID) || defined(OS_MACOSX))
715 EXPECT_EQ(test_vector1, last_write_value_); 748 EXPECT_EQ(test_vector1, last_write_value_);
716 #endif 749 #endif
717 750
718 SimulateGattCharacteristicWrite(characteristic2_); 751 SimulateGattCharacteristicWrite(characteristic2_);
752 base::RunLoop().RunUntilIdle();
719 #if !(defined(OS_ANDROID) || defined(OS_MACOSX)) 753 #if !(defined(OS_ANDROID) || defined(OS_MACOSX))
720 EXPECT_EQ(test_vector2, last_write_value_); 754 EXPECT_EQ(test_vector2, last_write_value_);
721 #endif 755 #endif
722 756
723 EXPECT_EQ(2, gatt_write_characteristic_attempts_); 757 EXPECT_EQ(2, gatt_write_characteristic_attempts_);
724 EXPECT_EQ(2, callback_count_); 758 EXPECT_EQ(2, callback_count_);
725 EXPECT_EQ(0, error_callback_count_); 759 EXPECT_EQ(0, error_callback_count_);
726 760
727 // TODO(591740): Remove if define for OS_ANDROID in this test. 761 // TODO(591740): Remove if define for OS_ANDROID in this test.
728 } 762 }
(...skipping 11 matching lines...) Expand all
740 774
741 TestBluetoothAdapterObserver observer(adapter_); 775 TestBluetoothAdapterObserver observer(adapter_);
742 776
743 characteristic1_->ReadRemoteCharacteristic( 777 characteristic1_->ReadRemoteCharacteristic(
744 GetReadValueCallback(Call::NOT_EXPECTED), 778 GetReadValueCallback(Call::NOT_EXPECTED),
745 GetGattErrorCallback(Call::EXPECTED)); 779 GetGattErrorCallback(Call::EXPECTED));
746 SimulateGattCharacteristicReadError( 780 SimulateGattCharacteristicReadError(
747 characteristic1_, BluetoothRemoteGattService::GATT_ERROR_INVALID_LENGTH); 781 characteristic1_, BluetoothRemoteGattService::GATT_ERROR_INVALID_LENGTH);
748 SimulateGattCharacteristicReadError( 782 SimulateGattCharacteristicReadError(
749 characteristic1_, BluetoothRemoteGattService::GATT_ERROR_FAILED); 783 characteristic1_, BluetoothRemoteGattService::GATT_ERROR_FAILED);
784 base::RunLoop().RunUntilIdle();
750 EXPECT_EQ(BluetoothRemoteGattService::GATT_ERROR_INVALID_LENGTH, 785 EXPECT_EQ(BluetoothRemoteGattService::GATT_ERROR_INVALID_LENGTH,
751 last_gatt_error_code_); 786 last_gatt_error_code_);
752 EXPECT_EQ(0, observer.gatt_characteristic_value_changed_count()); 787 EXPECT_EQ(0, observer.gatt_characteristic_value_changed_count());
753 } 788 }
754 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) 789 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN)
755 790
756 #if defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) 791 #if defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN)
757 // Tests WriteRemoteCharacteristic asynchronous error. 792 // Tests WriteRemoteCharacteristic asynchronous error.
758 TEST_F(BluetoothRemoteGattCharacteristicTest, WriteError) { 793 TEST_F(BluetoothRemoteGattCharacteristicTest, WriteError) {
759 if (!PlatformSupportsLowEnergy()) { 794 if (!PlatformSupportsLowEnergy()) {
760 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; 795 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
761 return; 796 return;
762 } 797 }
763 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate( 798 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(
764 BluetoothRemoteGattCharacteristic::PROPERTY_WRITE)); 799 BluetoothRemoteGattCharacteristic::PROPERTY_WRITE));
765 800
766 std::vector<uint8_t> empty_vector; 801 std::vector<uint8_t> empty_vector;
767 characteristic1_->WriteRemoteCharacteristic( 802 characteristic1_->WriteRemoteCharacteristic(
768 empty_vector, GetCallback(Call::NOT_EXPECTED), 803 empty_vector, GetCallback(Call::NOT_EXPECTED),
769 GetGattErrorCallback(Call::EXPECTED)); 804 GetGattErrorCallback(Call::EXPECTED));
770 SimulateGattCharacteristicWriteError( 805 SimulateGattCharacteristicWriteError(
771 characteristic1_, BluetoothRemoteGattService::GATT_ERROR_INVALID_LENGTH); 806 characteristic1_, BluetoothRemoteGattService::GATT_ERROR_INVALID_LENGTH);
772 SimulateGattCharacteristicWriteError( 807 SimulateGattCharacteristicWriteError(
773 characteristic1_, BluetoothRemoteGattService::GATT_ERROR_FAILED); 808 characteristic1_, BluetoothRemoteGattService::GATT_ERROR_FAILED);
809 base::RunLoop().RunUntilIdle();
774 810
775 EXPECT_EQ(BluetoothRemoteGattService::GATT_ERROR_INVALID_LENGTH, 811 EXPECT_EQ(BluetoothRemoteGattService::GATT_ERROR_INVALID_LENGTH,
776 last_gatt_error_code_); 812 last_gatt_error_code_);
777 } 813 }
778 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) 814 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN)
779 815
780 #if defined(OS_ANDROID) 816 #if defined(OS_ANDROID)
781 // Tests ReadRemoteCharacteristic synchronous error. 817 // Tests ReadRemoteCharacteristic synchronous error.
782 // Test not relevant for macOS since characteristic read cannot generate 818 // Test not relevant for macOS since characteristic read cannot generate
783 // synchronous error. 819 // synchronous error.
(...skipping 12 matching lines...) Expand all
796 last_gatt_error_code_); 832 last_gatt_error_code_);
797 833
798 // After failing once, can succeed: 834 // After failing once, can succeed:
799 ResetEventCounts(); 835 ResetEventCounts();
800 characteristic1_->ReadRemoteCharacteristic( 836 characteristic1_->ReadRemoteCharacteristic(
801 GetReadValueCallback(Call::EXPECTED), 837 GetReadValueCallback(Call::EXPECTED),
802 GetGattErrorCallback(Call::NOT_EXPECTED)); 838 GetGattErrorCallback(Call::NOT_EXPECTED));
803 EXPECT_EQ(1, gatt_read_characteristic_attempts_); 839 EXPECT_EQ(1, gatt_read_characteristic_attempts_);
804 std::vector<uint8_t> empty_vector; 840 std::vector<uint8_t> empty_vector;
805 SimulateGattCharacteristicRead(characteristic1_, empty_vector); 841 SimulateGattCharacteristicRead(characteristic1_, empty_vector);
842 base::RunLoop().RunUntilIdle();
806 EXPECT_EQ(1, callback_count_); 843 EXPECT_EQ(1, callback_count_);
807 EXPECT_EQ(0, error_callback_count_); 844 EXPECT_EQ(0, error_callback_count_);
808 } 845 }
809 #endif // defined(OS_ANDROID) 846 #endif // defined(OS_ANDROID)
810 847
811 #if defined(OS_ANDROID) 848 #if defined(OS_ANDROID)
812 // Tests WriteRemoteCharacteristic synchronous error. 849 // Tests WriteRemoteCharacteristic synchronous error.
813 // This test doesn't apply to macOS synchronous API does exist. 850 // This test doesn't apply to macOS synchronous API does exist.
814 TEST_F(BluetoothRemoteGattCharacteristicTest, WriteSynchronousError) { 851 TEST_F(BluetoothRemoteGattCharacteristicTest, WriteSynchronousError) {
815 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); 852 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate());
(...skipping 10 matching lines...) Expand all
826 EXPECT_EQ(BluetoothRemoteGattService::GATT_ERROR_FAILED, 863 EXPECT_EQ(BluetoothRemoteGattService::GATT_ERROR_FAILED,
827 last_gatt_error_code_); 864 last_gatt_error_code_);
828 865
829 // After failing once, can succeed: 866 // After failing once, can succeed:
830 ResetEventCounts(); 867 ResetEventCounts();
831 characteristic1_->WriteRemoteCharacteristic( 868 characteristic1_->WriteRemoteCharacteristic(
832 empty_vector, GetCallback(Call::EXPECTED), 869 empty_vector, GetCallback(Call::EXPECTED),
833 GetGattErrorCallback(Call::NOT_EXPECTED)); 870 GetGattErrorCallback(Call::NOT_EXPECTED));
834 EXPECT_EQ(1, gatt_write_characteristic_attempts_); 871 EXPECT_EQ(1, gatt_write_characteristic_attempts_);
835 SimulateGattCharacteristicWrite(characteristic1_); 872 SimulateGattCharacteristicWrite(characteristic1_);
873 base::RunLoop().RunUntilIdle();
836 EXPECT_EQ(1, callback_count_); 874 EXPECT_EQ(1, callback_count_);
837 EXPECT_EQ(0, error_callback_count_); 875 EXPECT_EQ(0, error_callback_count_);
838 } 876 }
839 #endif // defined(OS_ANDROID) 877 #endif // defined(OS_ANDROID)
840 878
841 #if defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) 879 #if defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN)
842 // Tests ReadRemoteCharacteristic error with a pending read operation. 880 // Tests ReadRemoteCharacteristic error with a pending read operation.
843 TEST_F(BluetoothRemoteGattCharacteristicTest, 881 TEST_F(BluetoothRemoteGattCharacteristicTest,
844 ReadRemoteCharacteristic_ReadPending) { 882 ReadRemoteCharacteristic_ReadPending) {
845 if (!PlatformSupportsLowEnergy()) { 883 if (!PlatformSupportsLowEnergy()) {
(...skipping 14 matching lines...) Expand all
860 898
861 EXPECT_EQ(0, callback_count_); 899 EXPECT_EQ(0, callback_count_);
862 EXPECT_EQ(1, error_callback_count_); 900 EXPECT_EQ(1, error_callback_count_);
863 EXPECT_EQ(BluetoothRemoteGattService::GATT_ERROR_IN_PROGRESS, 901 EXPECT_EQ(BluetoothRemoteGattService::GATT_ERROR_IN_PROGRESS,
864 last_gatt_error_code_); 902 last_gatt_error_code_);
865 903
866 // Initial read should still succeed: 904 // Initial read should still succeed:
867 ResetEventCounts(); 905 ResetEventCounts();
868 std::vector<uint8_t> empty_vector; 906 std::vector<uint8_t> empty_vector;
869 SimulateGattCharacteristicRead(characteristic1_, empty_vector); 907 SimulateGattCharacteristicRead(characteristic1_, empty_vector);
908 base::RunLoop().RunUntilIdle();
870 EXPECT_EQ(1, callback_count_); 909 EXPECT_EQ(1, callback_count_);
871 EXPECT_EQ(0, error_callback_count_); 910 EXPECT_EQ(0, error_callback_count_);
872 } 911 }
873 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) 912 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN)
874 913
875 #if defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) 914 #if defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN)
876 // Tests WriteRemoteCharacteristic error with a pending write operation. 915 // Tests WriteRemoteCharacteristic error with a pending write operation.
877 TEST_F(BluetoothRemoteGattCharacteristicTest, 916 TEST_F(BluetoothRemoteGattCharacteristicTest,
878 WriteRemoteCharacteristic_WritePending) { 917 WriteRemoteCharacteristic_WritePending) {
879 if (!PlatformSupportsLowEnergy()) { 918 if (!PlatformSupportsLowEnergy()) {
(...skipping 14 matching lines...) Expand all
894 base::RunLoop().RunUntilIdle(); 933 base::RunLoop().RunUntilIdle();
895 934
896 EXPECT_EQ(0, callback_count_); 935 EXPECT_EQ(0, callback_count_);
897 EXPECT_EQ(1, error_callback_count_); 936 EXPECT_EQ(1, error_callback_count_);
898 EXPECT_EQ(BluetoothRemoteGattService::GATT_ERROR_IN_PROGRESS, 937 EXPECT_EQ(BluetoothRemoteGattService::GATT_ERROR_IN_PROGRESS,
899 last_gatt_error_code_); 938 last_gatt_error_code_);
900 939
901 // Initial write should still succeed: 940 // Initial write should still succeed:
902 ResetEventCounts(); 941 ResetEventCounts();
903 SimulateGattCharacteristicWrite(characteristic1_); 942 SimulateGattCharacteristicWrite(characteristic1_);
943 base::RunLoop().RunUntilIdle();
904 EXPECT_EQ(1, callback_count_); 944 EXPECT_EQ(1, callback_count_);
905 EXPECT_EQ(0, error_callback_count_); 945 EXPECT_EQ(0, error_callback_count_);
906 } 946 }
907 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) 947 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN)
908 948
909 #if defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) 949 #if defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN)
910 // Tests ReadRemoteCharacteristic error with a pending write operation. 950 // Tests ReadRemoteCharacteristic error with a pending write operation.
911 TEST_F(BluetoothRemoteGattCharacteristicTest, 951 TEST_F(BluetoothRemoteGattCharacteristicTest,
912 ReadRemoteCharacteristic_WritePending) { 952 ReadRemoteCharacteristic_WritePending) {
913 if (!PlatformSupportsLowEnergy()) { 953 if (!PlatformSupportsLowEnergy()) {
(...skipping 15 matching lines...) Expand all
929 base::RunLoop().RunUntilIdle(); 969 base::RunLoop().RunUntilIdle();
930 970
931 EXPECT_EQ(0, callback_count_); 971 EXPECT_EQ(0, callback_count_);
932 EXPECT_EQ(1, error_callback_count_); 972 EXPECT_EQ(1, error_callback_count_);
933 EXPECT_EQ(BluetoothRemoteGattService::GATT_ERROR_IN_PROGRESS, 973 EXPECT_EQ(BluetoothRemoteGattService::GATT_ERROR_IN_PROGRESS,
934 last_gatt_error_code_); 974 last_gatt_error_code_);
935 975
936 // Initial write should still succeed: 976 // Initial write should still succeed:
937 ResetEventCounts(); 977 ResetEventCounts();
938 SimulateGattCharacteristicWrite(characteristic1_); 978 SimulateGattCharacteristicWrite(characteristic1_);
979 base::RunLoop().RunUntilIdle();
939 EXPECT_EQ(1, callback_count_); 980 EXPECT_EQ(1, callback_count_);
940 EXPECT_EQ(0, error_callback_count_); 981 EXPECT_EQ(0, error_callback_count_);
941 } 982 }
942 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) 983 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN)
943 984
944 #if defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) 985 #if defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN)
945 // Tests WriteRemoteCharacteristic error with a pending Read operation. 986 // Tests WriteRemoteCharacteristic error with a pending Read operation.
946 TEST_F(BluetoothRemoteGattCharacteristicTest, 987 TEST_F(BluetoothRemoteGattCharacteristicTest,
947 WriteRemoteCharacteristic_ReadPending) { 988 WriteRemoteCharacteristic_ReadPending) {
948 if (!PlatformSupportsLowEnergy()) { 989 if (!PlatformSupportsLowEnergy()) {
(...skipping 14 matching lines...) Expand all
963 base::RunLoop().RunUntilIdle(); 1004 base::RunLoop().RunUntilIdle();
964 1005
965 EXPECT_EQ(0, callback_count_); 1006 EXPECT_EQ(0, callback_count_);
966 EXPECT_EQ(1, error_callback_count_); 1007 EXPECT_EQ(1, error_callback_count_);
967 EXPECT_EQ(BluetoothRemoteGattService::GATT_ERROR_IN_PROGRESS, 1008 EXPECT_EQ(BluetoothRemoteGattService::GATT_ERROR_IN_PROGRESS,
968 last_gatt_error_code_); 1009 last_gatt_error_code_);
969 1010
970 // Initial read should still succeed: 1011 // Initial read should still succeed:
971 ResetEventCounts(); 1012 ResetEventCounts();
972 SimulateGattCharacteristicRead(characteristic1_, empty_vector); 1013 SimulateGattCharacteristicRead(characteristic1_, empty_vector);
1014 base::RunLoop().RunUntilIdle();
973 EXPECT_EQ(1, callback_count_); 1015 EXPECT_EQ(1, callback_count_);
974 EXPECT_EQ(0, error_callback_count_); 1016 EXPECT_EQ(0, error_callback_count_);
975 } 1017 }
976 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) 1018 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN)
977 1019
978 #if defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) 1020 #if defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN)
979 // StartNotifySession fails if characteristic doesn't have Notify or Indicate 1021 // StartNotifySession fails if characteristic doesn't have Notify or Indicate
980 // property. 1022 // property.
981 TEST_F(BluetoothRemoteGattCharacteristicTest, 1023 TEST_F(BluetoothRemoteGattCharacteristicTest,
982 StartNotifySession_NoNotifyOrIndicate) { 1024 StartNotifySession_NoNotifyOrIndicate) {
(...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after
1224 GetGattErrorCallback(Call::EXPECTED)); 1266 GetGattErrorCallback(Call::EXPECTED));
1225 ExpectedChangeNotifyValueAttempts(1); 1267 ExpectedChangeNotifyValueAttempts(1);
1226 ExpectedNotifyValue(NotifyValueState::NOTIFY); 1268 ExpectedNotifyValue(NotifyValueState::NOTIFY);
1227 EXPECT_EQ(0, callback_count_); 1269 EXPECT_EQ(0, callback_count_);
1228 1270
1229 RememberCharacteristicForSubsequentAction(characteristic1_); 1271 RememberCharacteristicForSubsequentAction(characteristic1_);
1230 RememberCCCDescriptorForSubsequentAction(characteristic1_); 1272 RememberCCCDescriptorForSubsequentAction(characteristic1_);
1231 DeleteDevice(device_); // TODO(576906) delete only the characteristic. 1273 DeleteDevice(device_); // TODO(576906) delete only the characteristic.
1232 1274
1233 SimulateGattNotifySessionStarted(/* use remembered characteristic */ nullptr); 1275 SimulateGattNotifySessionStarted(/* use remembered characteristic */ nullptr);
1276 base::RunLoop().RunUntilIdle();
1234 EXPECT_EQ(0, callback_count_); 1277 EXPECT_EQ(0, callback_count_);
1235 EXPECT_EQ(1, error_callback_count_); 1278 EXPECT_EQ(1, error_callback_count_);
1236 ASSERT_EQ(0u, notify_sessions_.size()); 1279 ASSERT_EQ(0u, notify_sessions_.size());
1237 EXPECT_EQ(BluetoothRemoteGattService::GATT_ERROR_FAILED, 1280 EXPECT_EQ(BluetoothRemoteGattService::GATT_ERROR_FAILED,
1238 last_gatt_error_code_); 1281 last_gatt_error_code_);
1239 } 1282 }
1240 #endif // defined(OS_ANDROID) 1283 #endif // defined(OS_ANDROID)
1241 1284
1242 #if defined(OS_ANDROID) || defined(OS_MACOSX) 1285 #if defined(OS_ANDROID) || defined(OS_MACOSX)
1243 // Tests StartNotifySession completing before chrome objects are deleted. 1286 // Tests StartNotifySession completing before chrome objects are deleted.
(...skipping 12 matching lines...) Expand all
1256 ASSERT_EQ(1u, characteristic1_->GetDescriptors().size()); 1299 ASSERT_EQ(1u, characteristic1_->GetDescriptors().size());
1257 1300
1258 characteristic1_->StartNotifySession( 1301 characteristic1_->StartNotifySession(
1259 GetNotifyCallback(Call::EXPECTED), 1302 GetNotifyCallback(Call::EXPECTED),
1260 GetGattErrorCallback(Call::NOT_EXPECTED)); 1303 GetGattErrorCallback(Call::NOT_EXPECTED));
1261 ExpectedChangeNotifyValueAttempts(1); 1304 ExpectedChangeNotifyValueAttempts(1);
1262 ExpectedNotifyValue(NotifyValueState::NOTIFY); 1305 ExpectedNotifyValue(NotifyValueState::NOTIFY);
1263 EXPECT_EQ(0, callback_count_); 1306 EXPECT_EQ(0, callback_count_);
1264 1307
1265 SimulateGattNotifySessionStarted(characteristic1_); 1308 SimulateGattNotifySessionStarted(characteristic1_);
1309 base::RunLoop().RunUntilIdle();
1266 ASSERT_EQ(1u, notify_sessions_.size()); 1310 ASSERT_EQ(1u, notify_sessions_.size());
1267 1311
1268 std::string characteristic_identifier = characteristic1_->GetIdentifier(); 1312 std::string characteristic_identifier = characteristic1_->GetIdentifier();
1269 1313
1270 EXPECT_EQ(characteristic_identifier, 1314 EXPECT_EQ(characteristic_identifier,
1271 notify_sessions_[0]->GetCharacteristicIdentifier()); 1315 notify_sessions_[0]->GetCharacteristicIdentifier());
1272 EXPECT_EQ(characteristic1_, notify_sessions_[0]->GetCharacteristic()); 1316 EXPECT_EQ(characteristic1_, notify_sessions_[0]->GetCharacteristic());
1273 EXPECT_TRUE(notify_sessions_[0]->IsActive()); 1317 EXPECT_TRUE(notify_sessions_[0]->IsActive());
1274 1318
1275 DeleteDevice(device_); // TODO(576906) delete only the characteristic. 1319 DeleteDevice(device_); // TODO(576906) delete only the characteristic.
(...skipping 24 matching lines...) Expand all
1300 ASSERT_EQ(1u, characteristic1_->GetDescriptors().size()); 1344 ASSERT_EQ(1u, characteristic1_->GetDescriptors().size());
1301 1345
1302 characteristic1_->StartNotifySession( 1346 characteristic1_->StartNotifySession(
1303 GetReentrantStartNotifySessionSuccessCallback(Call::EXPECTED, 1347 GetReentrantStartNotifySessionSuccessCallback(Call::EXPECTED,
1304 characteristic1_), 1348 characteristic1_),
1305 GetReentrantStartNotifySessionErrorCallback( 1349 GetReentrantStartNotifySessionErrorCallback(
1306 Call::NOT_EXPECTED, characteristic1_, 1350 Call::NOT_EXPECTED, characteristic1_,
1307 false /* error_in_reentrant */)); 1351 false /* error_in_reentrant */));
1308 EXPECT_EQ(0, callback_count_); 1352 EXPECT_EQ(0, callback_count_);
1309 SimulateGattNotifySessionStarted(characteristic1_); 1353 SimulateGattNotifySessionStarted(characteristic1_);
1354 base::RunLoop().RunUntilIdle();
1310 ExpectedChangeNotifyValueAttempts(1); 1355 ExpectedChangeNotifyValueAttempts(1);
1311 ExpectedNotifyValue(NotifyValueState::NOTIFY); 1356 ExpectedNotifyValue(NotifyValueState::NOTIFY);
1312 1357
1313 // Simulate reentrant StartNotifySession request from 1358 // Simulate reentrant StartNotifySession request from
1314 // BluetoothTestBase::ReentrantStartNotifySessionSuccessCallback. 1359 // BluetoothTestBase::ReentrantStartNotifySessionSuccessCallback.
1315 base::RunLoop().RunUntilIdle(); 1360 base::RunLoop().RunUntilIdle();
1316 ExpectedChangeNotifyValueAttempts(1); 1361 ExpectedChangeNotifyValueAttempts(1);
1317 EXPECT_EQ(2, callback_count_); 1362 EXPECT_EQ(2, callback_count_);
1318 EXPECT_EQ(0, error_callback_count_); 1363 EXPECT_EQ(0, error_callback_count_);
1319 ASSERT_EQ(2u, notify_sessions_.size()); 1364 ASSERT_EQ(2u, notify_sessions_.size());
(...skipping 13 matching lines...) Expand all
1333 StartNotifySession_Reentrant_Error_Success) { 1378 StartNotifySession_Reentrant_Error_Success) {
1334 ASSERT_NO_FATAL_FAILURE( 1379 ASSERT_NO_FATAL_FAILURE(
1335 FakeCharacteristicBoilerplate(/* properties: NOTIFY */ 0x10)); 1380 FakeCharacteristicBoilerplate(/* properties: NOTIFY */ 0x10));
1336 SimulateGattDescriptor( 1381 SimulateGattDescriptor(
1337 characteristic1_, 1382 characteristic1_,
1338 BluetoothGattDescriptor::ClientCharacteristicConfigurationUuid().value()); 1383 BluetoothGattDescriptor::ClientCharacteristicConfigurationUuid().value());
1339 ASSERT_EQ(1u, characteristic1_->GetDescriptors().size()); 1384 ASSERT_EQ(1u, characteristic1_->GetDescriptors().size());
1340 1385
1341 SimulateGattNotifySessionStartError( 1386 SimulateGattNotifySessionStartError(
1342 characteristic1_, BluetoothRemoteGattService::GATT_ERROR_UNKNOWN); 1387 characteristic1_, BluetoothRemoteGattService::GATT_ERROR_UNKNOWN);
1388 base::RunLoop().RunUntilIdle();
1343 1389
1344 characteristic1_->StartNotifySession( 1390 characteristic1_->StartNotifySession(
1345 GetReentrantStartNotifySessionSuccessCallback(Call::NOT_EXPECTED, 1391 GetReentrantStartNotifySessionSuccessCallback(Call::NOT_EXPECTED,
1346 characteristic1_), 1392 characteristic1_),
1347 GetReentrantStartNotifySessionErrorCallback( 1393 GetReentrantStartNotifySessionErrorCallback(
1348 Call::EXPECTED, characteristic1_, false /* error_in_reentrant */)); 1394 Call::EXPECTED, characteristic1_, false /* error_in_reentrant */));
1349 EXPECT_EQ(0, callback_count_); 1395 EXPECT_EQ(0, callback_count_);
1350 SimulateGattNotifySessionStarted(characteristic1_); 1396 SimulateGattNotifySessionStarted(characteristic1_);
1397 base::RunLoop().RunUntilIdle();
1351 ExpectedChangeNotifyValueAttempts(0); 1398 ExpectedChangeNotifyValueAttempts(0);
1352 EXPECT_EQ(1, error_callback_count_); 1399 EXPECT_EQ(1, error_callback_count_);
1353 1400
1354 // Simulate reentrant StartNotifySession request from 1401 // Simulate reentrant StartNotifySession request from
1355 // BluetoothTestBase::ReentrantStartNotifySessionErrorCallback. 1402 // BluetoothTestBase::ReentrantStartNotifySessionErrorCallback.
1356 SimulateGattNotifySessionStarted(characteristic1_); 1403 SimulateGattNotifySessionStarted(characteristic1_);
1404 base::RunLoop().RunUntilIdle();
1357 ExpectedChangeNotifyValueAttempts(1); 1405 ExpectedChangeNotifyValueAttempts(1);
1358 ExpectedNotifyValue(NotifyValueState::NOTIFY); 1406 ExpectedNotifyValue(NotifyValueState::NOTIFY);
1359 EXPECT_EQ(1, callback_count_); 1407 EXPECT_EQ(1, callback_count_);
1360 EXPECT_EQ(1, error_callback_count_); 1408 EXPECT_EQ(1, error_callback_count_);
1361 ASSERT_EQ(1u, notify_sessions_.size()); 1409 ASSERT_EQ(1u, notify_sessions_.size());
1362 ASSERT_TRUE(notify_sessions_[0]); 1410 ASSERT_TRUE(notify_sessions_[0]);
1363 EXPECT_EQ(characteristic1_->GetIdentifier(), 1411 EXPECT_EQ(characteristic1_->GetIdentifier(),
1364 notify_sessions_[0]->GetCharacteristicIdentifier()); 1412 notify_sessions_[0]->GetCharacteristicIdentifier());
1365 EXPECT_TRUE(notify_sessions_[0]->IsActive()); 1413 EXPECT_TRUE(notify_sessions_[0]->IsActive());
1366 } 1414 }
(...skipping 14 matching lines...) Expand all
1381 SimulateGattNotifySessionStartError( 1429 SimulateGattNotifySessionStartError(
1382 characteristic1_, BluetoothRemoteGattService::GATT_ERROR_UNKNOWN); 1430 characteristic1_, BluetoothRemoteGattService::GATT_ERROR_UNKNOWN);
1383 1431
1384 characteristic1_->StartNotifySession( 1432 characteristic1_->StartNotifySession(
1385 GetReentrantStartNotifySessionSuccessCallback(Call::NOT_EXPECTED, 1433 GetReentrantStartNotifySessionSuccessCallback(Call::NOT_EXPECTED,
1386 characteristic1_), 1434 characteristic1_),
1387 GetReentrantStartNotifySessionErrorCallback( 1435 GetReentrantStartNotifySessionErrorCallback(
1388 Call::EXPECTED, characteristic1_, true /* error_in_reentrant */)); 1436 Call::EXPECTED, characteristic1_, true /* error_in_reentrant */));
1389 EXPECT_EQ(0, callback_count_); 1437 EXPECT_EQ(0, callback_count_);
1390 SimulateGattNotifySessionStarted(characteristic1_); 1438 SimulateGattNotifySessionStarted(characteristic1_);
1439 base::RunLoop().RunUntilIdle();
1391 ExpectedChangeNotifyValueAttempts(0); 1440 ExpectedChangeNotifyValueAttempts(0);
1392 1441
1393 // Simulate reentrant StartNotifySession request from 1442 // Simulate reentrant StartNotifySession request from
1394 // BluetoothTestBase::ReentrantStartNotifySessionErrorCallback. 1443 // BluetoothTestBase::ReentrantStartNotifySessionErrorCallback.
1395 SimulateGattNotifySessionStarted(characteristic1_); 1444 SimulateGattNotifySessionStarted(characteristic1_);
1445 base::RunLoop().RunUntilIdle();
1396 ExpectedChangeNotifyValueAttempts(0); 1446 ExpectedChangeNotifyValueAttempts(0);
1397 EXPECT_EQ(0, callback_count_); 1447 EXPECT_EQ(0, callback_count_);
1398 EXPECT_EQ(2, error_callback_count_); 1448 EXPECT_EQ(2, error_callback_count_);
1399 ASSERT_EQ(0u, notify_sessions_.size()); 1449 ASSERT_EQ(0u, notify_sessions_.size());
1400 } 1450 }
1401 #endif // defined(OS_WIN) 1451 #endif // defined(OS_WIN)
1402 1452
1403 #if defined(OS_ANDROID) || defined(OS_MACOSX) 1453 #if defined(OS_ANDROID) || defined(OS_MACOSX)
1404 // Tests StopNotifySession success on a characteristic that enabled Notify. 1454 // Tests StopNotifySession success on a characteristic that enabled Notify.
1405 TEST_F(BluetoothRemoteGattCharacteristicTest, StopNotifySession) { 1455 TEST_F(BluetoothRemoteGattCharacteristicTest, StopNotifySession) {
(...skipping 662 matching lines...) Expand 10 before | Expand all | Expand 10 after
2068 ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate( 2118 ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate(
2069 /* properties: NOTIFY */ 0x10, NotifyValueState::NOTIFY)); 2119 /* properties: NOTIFY */ 0x10, NotifyValueState::NOTIFY));
2070 2120
2071 TestBluetoothAdapterObserver observer(adapter_); 2121 TestBluetoothAdapterObserver observer(adapter_);
2072 2122
2073 std::vector<uint8_t> test_vector1, test_vector2; 2123 std::vector<uint8_t> test_vector1, test_vector2;
2074 test_vector1.push_back(111); 2124 test_vector1.push_back(111);
2075 test_vector2.push_back(222); 2125 test_vector2.push_back(222);
2076 2126
2077 SimulateGattCharacteristicChanged(characteristic1_, test_vector1); 2127 SimulateGattCharacteristicChanged(characteristic1_, test_vector1);
2128 base::RunLoop().RunUntilIdle();
2078 EXPECT_EQ(1, observer.gatt_characteristic_value_changed_count()); 2129 EXPECT_EQ(1, observer.gatt_characteristic_value_changed_count());
2079 EXPECT_EQ(test_vector1, characteristic1_->GetValue()); 2130 EXPECT_EQ(test_vector1, characteristic1_->GetValue());
2080 2131
2081 SimulateGattCharacteristicChanged(characteristic1_, test_vector2); 2132 SimulateGattCharacteristicChanged(characteristic1_, test_vector2);
2133 base::RunLoop().RunUntilIdle();
2082 EXPECT_EQ(2, observer.gatt_characteristic_value_changed_count()); 2134 EXPECT_EQ(2, observer.gatt_characteristic_value_changed_count());
2083 EXPECT_EQ(test_vector2, characteristic1_->GetValue()); 2135 EXPECT_EQ(test_vector2, characteristic1_->GetValue());
2084 } 2136 }
2085 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) 2137 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN)
2086 2138
2087 #if defined(OS_ANDROID) || defined(OS_WIN) 2139 #if defined(OS_ANDROID) || defined(OS_WIN)
2088 // Tests Characteristic Value changing after a Notify Session and objects being 2140 // Tests Characteristic Value changing after a Notify Session and objects being
2089 // destroyed. 2141 // destroyed.
2090 // macOS: Not applicable: This can never happen if CBPeripheral delegate is set 2142 // macOS: Not applicable: This can never happen if CBPeripheral delegate is set
2091 // to nil. 2143 // to nil.
2092 TEST_F(BluetoothRemoteGattCharacteristicTest, 2144 TEST_F(BluetoothRemoteGattCharacteristicTest,
2093 GattCharacteristicValueChanged_AfterDeleted) { 2145 GattCharacteristicValueChanged_AfterDeleted) {
2094 ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate( 2146 ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate(
2095 /* properties: NOTIFY */ 0x10, NotifyValueState::NOTIFY)); 2147 /* properties: NOTIFY */ 0x10, NotifyValueState::NOTIFY));
2096 TestBluetoothAdapterObserver observer(adapter_); 2148 TestBluetoothAdapterObserver observer(adapter_);
2097 2149
2098 RememberCharacteristicForSubsequentAction(characteristic1_); 2150 RememberCharacteristicForSubsequentAction(characteristic1_);
2099 DeleteDevice(device_); // TODO(576906) delete only the characteristic. 2151 DeleteDevice(device_); // TODO(576906) delete only the characteristic.
2100 2152
2101 std::vector<uint8_t> empty_vector; 2153 std::vector<uint8_t> empty_vector;
2102 SimulateGattCharacteristicChanged(/* use remembered characteristic */ nullptr, 2154 SimulateGattCharacteristicChanged(/* use remembered characteristic */ nullptr,
2103 empty_vector); 2155 empty_vector);
2156 base::RunLoop().RunUntilIdle();
2104 EXPECT_TRUE("Did not crash!"); 2157 EXPECT_TRUE("Did not crash!");
2105 EXPECT_EQ(0, observer.gatt_characteristic_value_changed_count()); 2158 EXPECT_EQ(0, observer.gatt_characteristic_value_changed_count());
2106 } 2159 }
2107 #endif // defined(OS_ANDROID) || defined(OS_WIN) 2160 #endif // defined(OS_ANDROID) || defined(OS_WIN)
2108 2161
2109 #if defined(OS_ANDROID) || defined(OS_WIN) 2162 #if defined(OS_ANDROID) || defined(OS_WIN)
2110 TEST_F(BluetoothRemoteGattCharacteristicTest, GetDescriptors_FindNone) { 2163 TEST_F(BluetoothRemoteGattCharacteristicTest, GetDescriptors_FindNone) {
2111 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); 2164 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate());
2112 2165
2113 EXPECT_EQ(0u, characteristic1_->GetDescriptors().size()); 2166 EXPECT_EQ(0u, characteristic1_->GetDescriptors().size());
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
2184 EXPECT_EQ(1u, characteristic1_->GetDescriptorsByUUID(id2).size()); 2237 EXPECT_EQ(1u, characteristic1_->GetDescriptorsByUUID(id2).size());
2185 EXPECT_EQ(2u, characteristic2_->GetDescriptorsByUUID(id3).size()); 2238 EXPECT_EQ(2u, characteristic2_->GetDescriptorsByUUID(id3).size());
2186 2239
2187 EXPECT_EQ(0u, characteristic2_->GetDescriptorsByUUID(id1).size()); 2240 EXPECT_EQ(0u, characteristic2_->GetDescriptorsByUUID(id1).size());
2188 EXPECT_EQ(0u, characteristic2_->GetDescriptorsByUUID(id2).size()); 2241 EXPECT_EQ(0u, characteristic2_->GetDescriptorsByUUID(id2).size());
2189 EXPECT_EQ(0u, characteristic1_->GetDescriptorsByUUID(id3).size()); 2242 EXPECT_EQ(0u, characteristic1_->GetDescriptorsByUUID(id3).size());
2190 } 2243 }
2191 #endif // defined(OS_ANDROID) || defined(OS_WIN) 2244 #endif // defined(OS_ANDROID) || defined(OS_WIN)
2192 2245
2193 } // namespace device 2246 } // namespace device
OLDNEW
« no previous file with comments | « device/bluetooth/bluetooth_device_unittest.cc ('k') | device/bluetooth/bluetooth_remote_gatt_descriptor_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698