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

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

Issue 2767813002: Bluetooth: macOS: Implementing read/write for descriptors (Closed)
Patch Set: . Created 3 years, 8 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 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 "base/run_loop.h" 5 #include "base/run_loop.h"
6 #include "device/bluetooth/bluetooth_remote_gatt_characteristic.h" 6 #include "device/bluetooth/bluetooth_remote_gatt_characteristic.h"
7 #include "device/bluetooth/bluetooth_remote_gatt_service.h" 7 #include "device/bluetooth/bluetooth_remote_gatt_service.h"
8 #include "testing/gtest/include/gtest/gtest.h" 8 #include "testing/gtest/include/gtest/gtest.h"
9 9
10 #if defined(OS_ANDROID) 10 #if defined(OS_ANDROID)
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after
181 181
182 // Swap as needed to have descriptor1 be the one with uuid1. 182 // Swap as needed to have descriptor1 be the one with uuid1.
183 if (descriptor2->GetUUID() == uuid1) 183 if (descriptor2->GetUUID() == uuid1)
184 std::swap(descriptor1, descriptor2); 184 std::swap(descriptor1, descriptor2);
185 185
186 EXPECT_EQ(uuid1, descriptor1->GetUUID()); 186 EXPECT_EQ(uuid1, descriptor1->GetUUID());
187 EXPECT_EQ(uuid2, descriptor2->GetUUID()); 187 EXPECT_EQ(uuid2, descriptor2->GetUUID());
188 } 188 }
189 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) 189 #endif // defined(OS_ANDROID) || defined(OS_MACOSX)
190 190
191 #if defined(OS_ANDROID) 191 #if defined(OS_ANDROID) || defined(OS_MACOSX)
192 // Tests ReadRemoteDescriptor and GetValue with empty value buffer. 192 // Tests ReadRemoteDescriptor and GetValue with empty value buffer.
193 TEST_F(BluetoothRemoteGattDescriptorTest, ReadRemoteDescriptor_Empty) { 193 TEST_F(BluetoothRemoteGattDescriptorTest, ReadRemoteDescriptor_Empty) {
194 if (!PlatformSupportsLowEnergy()) {
195 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
196 return;
197 }
194 ASSERT_NO_FATAL_FAILURE(FakeDescriptorBoilerplate()); 198 ASSERT_NO_FATAL_FAILURE(FakeDescriptorBoilerplate());
195 199
196 descriptor1_->ReadRemoteDescriptor(GetReadValueCallback(Call::EXPECTED), 200 descriptor1_->ReadRemoteDescriptor(GetReadValueCallback(Call::EXPECTED),
197 GetGattErrorCallback(Call::NOT_EXPECTED)); 201 GetGattErrorCallback(Call::NOT_EXPECTED));
198 EXPECT_EQ(1, gatt_read_descriptor_attempts_); 202 EXPECT_EQ(1, gatt_read_descriptor_attempts_);
199 std::vector<uint8_t> empty_vector; 203 std::vector<uint8_t> empty_vector;
200 SimulateGattDescriptorRead(descriptor1_, empty_vector); 204 SimulateGattDescriptorRead(descriptor1_, empty_vector);
201 base::RunLoop().RunUntilIdle(); 205 base::RunLoop().RunUntilIdle();
202 206
203 // Duplicate read reported from OS shouldn't cause a problem: 207 // Duplicate read reported from OS shouldn't cause a problem:
204 SimulateGattDescriptorRead(descriptor1_, empty_vector); 208 SimulateGattDescriptorRead(descriptor1_, empty_vector);
205 base::RunLoop().RunUntilIdle(); 209 base::RunLoop().RunUntilIdle();
206 210
207 EXPECT_EQ(empty_vector, last_read_value_); 211 EXPECT_EQ(empty_vector, last_read_value_);
208 EXPECT_EQ(empty_vector, descriptor1_->GetValue()); 212 EXPECT_EQ(empty_vector, descriptor1_->GetValue());
209 } 213 }
210 #endif // defined(OS_ANDROID) 214 #endif // defined(OS_ANDROID) || defined(OS_MACOSX)
211 215
212 #if defined(OS_ANDROID) 216 #if defined(OS_ANDROID) || defined(OS_MACOSX)
213 // Tests WriteRemoteDescriptor with empty value buffer. 217 // Tests WriteRemoteDescriptor with empty value buffer.
214 TEST_F(BluetoothRemoteGattDescriptorTest, WriteRemoteDescriptor_Empty) { 218 TEST_F(BluetoothRemoteGattDescriptorTest, WriteRemoteDescriptor_Empty) {
219 if (!PlatformSupportsLowEnergy()) {
220 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
221 return;
222 }
215 ASSERT_NO_FATAL_FAILURE(FakeDescriptorBoilerplate()); 223 ASSERT_NO_FATAL_FAILURE(FakeDescriptorBoilerplate());
216 224
217 std::vector<uint8_t> empty_vector; 225 std::vector<uint8_t> empty_vector;
218 descriptor1_->WriteRemoteDescriptor(empty_vector, GetCallback(Call::EXPECTED), 226 descriptor1_->WriteRemoteDescriptor(empty_vector, GetCallback(Call::EXPECTED),
219 GetGattErrorCallback(Call::NOT_EXPECTED)); 227 GetGattErrorCallback(Call::NOT_EXPECTED));
220 EXPECT_EQ(1, gatt_write_descriptor_attempts_); 228 EXPECT_EQ(1, gatt_write_descriptor_attempts_);
221 SimulateGattDescriptorWrite(descriptor1_); 229 SimulateGattDescriptorWrite(descriptor1_);
222 base::RunLoop().RunUntilIdle(); 230 base::RunLoop().RunUntilIdle();
223 231
224 // Duplicate write reported from OS shouldn't cause a problem: 232 // Duplicate write reported from OS shouldn't cause a problem:
225 SimulateGattDescriptorWrite(descriptor1_); 233 SimulateGattDescriptorWrite(descriptor1_);
226 base::RunLoop().RunUntilIdle(); 234 base::RunLoop().RunUntilIdle();
227 235
228 EXPECT_EQ(empty_vector, last_write_value_); 236 EXPECT_EQ(empty_vector, last_write_value_);
229 } 237 }
230 #endif // defined(OS_ANDROID) 238 #endif // defined(OS_ANDROID) || defined(OS_MACOSX)
231 239
232 #if defined(OS_ANDROID) 240 #if defined(OS_ANDROID)
233 // Tests ReadRemoteDescriptor completing after Chrome objects are deleted. 241 // Tests ReadRemoteDescriptor completing after Chrome objects are deleted.
242 // macOS: Not applicable: This can never happen if CBPeripheral delegate is set
243 // to nil.
234 TEST_F(BluetoothRemoteGattDescriptorTest, ReadRemoteDescriptor_AfterDeleted) { 244 TEST_F(BluetoothRemoteGattDescriptorTest, ReadRemoteDescriptor_AfterDeleted) {
245 if (!PlatformSupportsLowEnergy()) {
246 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
247 return;
248 }
235 ASSERT_NO_FATAL_FAILURE(FakeDescriptorBoilerplate()); 249 ASSERT_NO_FATAL_FAILURE(FakeDescriptorBoilerplate());
236 250
237 descriptor1_->ReadRemoteDescriptor(GetReadValueCallback(Call::NOT_EXPECTED), 251 descriptor1_->ReadRemoteDescriptor(GetReadValueCallback(Call::NOT_EXPECTED),
238 GetGattErrorCallback(Call::NOT_EXPECTED)); 252 GetGattErrorCallback(Call::NOT_EXPECTED));
239 253
240 RememberDescriptorForSubsequentAction(descriptor1_); 254 RememberDescriptorForSubsequentAction(descriptor1_);
241 DeleteDevice(device_); // TODO(576906) delete only the descriptor. 255 DeleteDevice(device_); // TODO(576906) delete only the descriptor.
242 256
243 std::vector<uint8_t> empty_vector; 257 std::vector<uint8_t> empty_vector;
244 SimulateGattDescriptorRead(/* use remembered descriptor */ nullptr, 258 SimulateGattDescriptorRead(/* use remembered descriptor */ nullptr,
245 empty_vector); 259 empty_vector);
246 base::RunLoop().RunUntilIdle(); 260 base::RunLoop().RunUntilIdle();
247 EXPECT_TRUE("Did not crash!"); 261 EXPECT_TRUE("Did not crash!");
248 } 262 }
249 #endif // defined(OS_ANDROID) 263 #endif // defined(OS_ANDROID)
250 264
251 #if defined(OS_ANDROID) 265 #if defined(OS_ANDROID)
252 // Tests WriteRemoteDescriptor completing after Chrome objects are deleted. 266 // Tests WriteRemoteDescriptor completing after Chrome objects are deleted.
267 // macOS: Not applicable: This can never happen if CBPeripheral delegate is set
268 // to nil.
253 TEST_F(BluetoothRemoteGattDescriptorTest, WriteRemoteDescriptor_AfterDeleted) { 269 TEST_F(BluetoothRemoteGattDescriptorTest, WriteRemoteDescriptor_AfterDeleted) {
270 if (!PlatformSupportsLowEnergy()) {
271 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
272 return;
273 }
254 ASSERT_NO_FATAL_FAILURE(FakeDescriptorBoilerplate()); 274 ASSERT_NO_FATAL_FAILURE(FakeDescriptorBoilerplate());
255 275
256 std::vector<uint8_t> empty_vector; 276 std::vector<uint8_t> empty_vector;
257 descriptor1_->WriteRemoteDescriptor(empty_vector, 277 descriptor1_->WriteRemoteDescriptor(empty_vector,
258 GetCallback(Call::NOT_EXPECTED), 278 GetCallback(Call::NOT_EXPECTED),
259 GetGattErrorCallback(Call::NOT_EXPECTED)); 279 GetGattErrorCallback(Call::NOT_EXPECTED));
260 280
261 RememberDescriptorForSubsequentAction(descriptor1_); 281 RememberDescriptorForSubsequentAction(descriptor1_);
262 DeleteDevice(device_); // TODO(576906) delete only the descriptor. 282 DeleteDevice(device_); // TODO(576906) delete only the descriptor.
263 283
264 SimulateGattDescriptorWrite(/* use remembered descriptor */ nullptr); 284 SimulateGattDescriptorWrite(/* use remembered descriptor */ nullptr);
265 base::RunLoop().RunUntilIdle(); 285 base::RunLoop().RunUntilIdle();
266 EXPECT_TRUE("Did not crash!"); 286 EXPECT_TRUE("Did not crash!");
267 } 287 }
268 #endif // defined(OS_ANDROID) 288 #endif // defined(OS_ANDROID)
269 289
270 #if defined(OS_ANDROID) 290 #if defined(OS_ANDROID) || defined(OS_MACOSX)
271 // Tests ReadRemoteDescriptor and GetValue with non-empty value buffer. 291 // Tests ReadRemoteDescriptor and GetValue with non-empty value buffer.
272 TEST_F(BluetoothRemoteGattDescriptorTest, ReadRemoteDescriptor) { 292 TEST_F(BluetoothRemoteGattDescriptorTest, ReadRemoteDescriptor) {
293 if (!PlatformSupportsLowEnergy()) {
294 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
295 return;
296 }
273 ASSERT_NO_FATAL_FAILURE(FakeDescriptorBoilerplate()); 297 ASSERT_NO_FATAL_FAILURE(FakeDescriptorBoilerplate());
274 298
275 descriptor1_->ReadRemoteDescriptor(GetReadValueCallback(Call::EXPECTED), 299 descriptor1_->ReadRemoteDescriptor(GetReadValueCallback(Call::EXPECTED),
276 GetGattErrorCallback(Call::NOT_EXPECTED)); 300 GetGattErrorCallback(Call::NOT_EXPECTED));
277 EXPECT_EQ(1, gatt_read_descriptor_attempts_); 301 EXPECT_EQ(1, gatt_read_descriptor_attempts_);
278 302
279 uint8_t values[] = {0, 1, 2, 3, 4, 0xf, 0xf0, 0xff}; 303 uint8_t values[] = {0, 1, 2, 3, 4, 0xf, 0xf0, 0xff};
280 std::vector<uint8_t> test_vector(values, values + arraysize(values)); 304 std::vector<uint8_t> test_vector(values, values + arraysize(values));
281 SimulateGattDescriptorRead(descriptor1_, test_vector); 305 SimulateGattDescriptorRead(descriptor1_, test_vector);
282 base::RunLoop().RunUntilIdle(); 306 base::RunLoop().RunUntilIdle();
283 307
284 // Duplicate read reported from OS shouldn't cause a problem: 308 // Duplicate read reported from OS shouldn't cause a problem:
285 std::vector<uint8_t> empty_vector; 309 std::vector<uint8_t> empty_vector;
286 SimulateGattDescriptorRead(descriptor1_, empty_vector); 310 SimulateGattDescriptorRead(descriptor1_, empty_vector);
287 base::RunLoop().RunUntilIdle(); 311 base::RunLoop().RunUntilIdle();
288 312
289 EXPECT_EQ(test_vector, last_read_value_); 313 EXPECT_EQ(test_vector, last_read_value_);
290 EXPECT_EQ(test_vector, descriptor1_->GetValue()); 314 EXPECT_EQ(test_vector, descriptor1_->GetValue());
291 } 315 }
292 #endif // defined(OS_ANDROID) 316 #endif // defined(OS_ANDROID) || defined(OS_MACOSX)
293 317
294 #if defined(OS_ANDROID) 318 #if defined(OS_ANDROID) || defined(OS_MACOSX)
295 // Tests WriteRemoteDescriptor with non-empty value buffer. 319 // Tests WriteRemoteDescriptor with non-empty value buffer.
296 TEST_F(BluetoothRemoteGattDescriptorTest, WriteRemoteDescriptor) { 320 TEST_F(BluetoothRemoteGattDescriptorTest, WriteRemoteDescriptor) {
321 if (!PlatformSupportsLowEnergy()) {
322 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
323 return;
324 }
297 ASSERT_NO_FATAL_FAILURE(FakeDescriptorBoilerplate()); 325 ASSERT_NO_FATAL_FAILURE(FakeDescriptorBoilerplate());
298 326
299 uint8_t values[] = {0, 1, 2, 3, 4, 0xf, 0xf0, 0xff}; 327 uint8_t values[] = {0, 1, 2, 3, 4, 0xf, 0xf0, 0xff};
300 std::vector<uint8_t> test_vector(values, values + arraysize(values)); 328 std::vector<uint8_t> test_vector(values, values + arraysize(values));
301 descriptor1_->WriteRemoteDescriptor(test_vector, GetCallback(Call::EXPECTED), 329 descriptor1_->WriteRemoteDescriptor(test_vector, GetCallback(Call::EXPECTED),
302 GetGattErrorCallback(Call::NOT_EXPECTED)); 330 GetGattErrorCallback(Call::NOT_EXPECTED));
303 EXPECT_EQ(1, gatt_write_descriptor_attempts_); 331 EXPECT_EQ(1, gatt_write_descriptor_attempts_);
304 332
305 SimulateGattDescriptorWrite(descriptor1_); 333 SimulateGattDescriptorWrite(descriptor1_);
306 base::RunLoop().RunUntilIdle(); 334 base::RunLoop().RunUntilIdle();
307 335
308 EXPECT_EQ(test_vector, last_write_value_); 336 EXPECT_EQ(test_vector, last_write_value_);
309 } 337 }
310 #endif // defined(OS_ANDROID) 338 #endif // defined(OS_ANDROID) || defined(OS_MACOSX)
311 339
312 #if defined(OS_ANDROID) 340 #if defined(OS_ANDROID) || defined(OS_MACOSX)
313 // Tests ReadRemoteDescriptor and GetValue multiple times. 341 // Tests ReadRemoteDescriptor and GetValue multiple times.
314 TEST_F(BluetoothRemoteGattDescriptorTest, ReadRemoteDescriptor_Twice) { 342 TEST_F(BluetoothRemoteGattDescriptorTest, ReadRemoteDescriptor_Twice) {
343 if (!PlatformSupportsLowEnergy()) {
344 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
345 return;
346 }
315 ASSERT_NO_FATAL_FAILURE(FakeDescriptorBoilerplate()); 347 ASSERT_NO_FATAL_FAILURE(FakeDescriptorBoilerplate());
316 348
317 descriptor1_->ReadRemoteDescriptor(GetReadValueCallback(Call::EXPECTED), 349 descriptor1_->ReadRemoteDescriptor(GetReadValueCallback(Call::EXPECTED),
318 GetGattErrorCallback(Call::NOT_EXPECTED)); 350 GetGattErrorCallback(Call::NOT_EXPECTED));
319 EXPECT_EQ(1, gatt_read_descriptor_attempts_); 351 EXPECT_EQ(1, gatt_read_descriptor_attempts_);
320 352
321 uint8_t values[] = {0, 1, 2, 3, 4, 0xf, 0xf0, 0xff}; 353 uint8_t values[] = {0, 1, 2, 3, 4, 0xf, 0xf0, 0xff};
322 std::vector<uint8_t> test_vector(values, values + arraysize(values)); 354 std::vector<uint8_t> test_vector(values, values + arraysize(values));
323 SimulateGattDescriptorRead(descriptor1_, test_vector); 355 SimulateGattDescriptorRead(descriptor1_, test_vector);
324 base::RunLoop().RunUntilIdle(); 356 base::RunLoop().RunUntilIdle();
325 EXPECT_EQ(1, callback_count_); 357 EXPECT_EQ(1, callback_count_);
326 EXPECT_EQ(0, error_callback_count_); 358 EXPECT_EQ(0, error_callback_count_);
327 EXPECT_EQ(test_vector, last_read_value_); 359 EXPECT_EQ(test_vector, last_read_value_);
328 EXPECT_EQ(test_vector, descriptor1_->GetValue()); 360 EXPECT_EQ(test_vector, descriptor1_->GetValue());
329 361
330 // Read again, with different value: 362 // Read again, with different value:
331 ResetEventCounts(); 363 ResetEventCounts();
332 descriptor1_->ReadRemoteDescriptor(GetReadValueCallback(Call::EXPECTED), 364 descriptor1_->ReadRemoteDescriptor(GetReadValueCallback(Call::EXPECTED),
333 GetGattErrorCallback(Call::NOT_EXPECTED)); 365 GetGattErrorCallback(Call::NOT_EXPECTED));
334 EXPECT_EQ(1, gatt_read_descriptor_attempts_); 366 EXPECT_EQ(1, gatt_read_descriptor_attempts_);
335 std::vector<uint8_t> empty_vector; 367 std::vector<uint8_t> empty_vector;
336 SimulateGattDescriptorRead(descriptor1_, empty_vector); 368 SimulateGattDescriptorRead(descriptor1_, empty_vector);
337 base::RunLoop().RunUntilIdle(); 369 base::RunLoop().RunUntilIdle();
338 EXPECT_EQ(1, callback_count_); 370 EXPECT_EQ(1, callback_count_);
339 EXPECT_EQ(0, error_callback_count_); 371 EXPECT_EQ(0, error_callback_count_);
340 EXPECT_EQ(empty_vector, last_read_value_); 372 EXPECT_EQ(empty_vector, last_read_value_);
341 EXPECT_EQ(empty_vector, descriptor1_->GetValue()); 373 EXPECT_EQ(empty_vector, descriptor1_->GetValue());
342 } 374 }
343 #endif // defined(OS_ANDROID) 375 #endif // defined(OS_ANDROID) || defined(OS_MACOSX)
344 376
345 #if defined(OS_ANDROID) 377 #if defined(OS_ANDROID) || defined(OS_MACOSX)
346 // Tests WriteRemoteDescriptor multiple times. 378 // Tests WriteRemoteDescriptor multiple times.
347 TEST_F(BluetoothRemoteGattDescriptorTest, WriteRemoteDescriptor_Twice) { 379 TEST_F(BluetoothRemoteGattDescriptorTest, WriteRemoteDescriptor_Twice) {
380 if (!PlatformSupportsLowEnergy()) {
381 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
382 return;
383 }
348 ASSERT_NO_FATAL_FAILURE(FakeDescriptorBoilerplate()); 384 ASSERT_NO_FATAL_FAILURE(FakeDescriptorBoilerplate());
349 385
350 uint8_t values[] = {0, 1, 2, 3, 4, 0xf, 0xf0, 0xff}; 386 uint8_t values[] = {0, 1, 2, 3, 4, 0xf, 0xf0, 0xff};
351 std::vector<uint8_t> test_vector(values, values + arraysize(values)); 387 std::vector<uint8_t> test_vector(values, values + arraysize(values));
352 descriptor1_->WriteRemoteDescriptor(test_vector, GetCallback(Call::EXPECTED), 388 descriptor1_->WriteRemoteDescriptor(test_vector, GetCallback(Call::EXPECTED),
353 GetGattErrorCallback(Call::NOT_EXPECTED)); 389 GetGattErrorCallback(Call::NOT_EXPECTED));
354 EXPECT_EQ(1, gatt_write_descriptor_attempts_); 390 EXPECT_EQ(1, gatt_write_descriptor_attempts_);
355 391
356 SimulateGattDescriptorWrite(descriptor1_); 392 SimulateGattDescriptorWrite(descriptor1_);
357 base::RunLoop().RunUntilIdle(); 393 base::RunLoop().RunUntilIdle();
358 EXPECT_EQ(1, callback_count_); 394 EXPECT_EQ(1, callback_count_);
359 EXPECT_EQ(0, error_callback_count_); 395 EXPECT_EQ(0, error_callback_count_);
360 EXPECT_EQ(test_vector, last_write_value_); 396 EXPECT_EQ(test_vector, last_write_value_);
361 397
362 // Write again, with different value: 398 // Write again, with different value:
363 ResetEventCounts(); 399 ResetEventCounts();
364 std::vector<uint8_t> empty_vector; 400 std::vector<uint8_t> empty_vector;
365 descriptor1_->WriteRemoteDescriptor(empty_vector, GetCallback(Call::EXPECTED), 401 descriptor1_->WriteRemoteDescriptor(empty_vector, GetCallback(Call::EXPECTED),
366 GetGattErrorCallback(Call::NOT_EXPECTED)); 402 GetGattErrorCallback(Call::NOT_EXPECTED));
367 EXPECT_EQ(1, gatt_write_descriptor_attempts_); 403 EXPECT_EQ(1, gatt_write_descriptor_attempts_);
368 SimulateGattDescriptorWrite(descriptor1_); 404 SimulateGattDescriptorWrite(descriptor1_);
369 base::RunLoop().RunUntilIdle(); 405 base::RunLoop().RunUntilIdle();
370 EXPECT_EQ(1, callback_count_); 406 EXPECT_EQ(1, callback_count_);
371 EXPECT_EQ(0, error_callback_count_); 407 EXPECT_EQ(0, error_callback_count_);
372 EXPECT_EQ(empty_vector, last_write_value_); 408 EXPECT_EQ(empty_vector, last_write_value_);
373 } 409 }
374 #endif // defined(OS_ANDROID) 410 #endif // defined(OS_ANDROID) || defined(OS_MACOSX)
375 411
376 #if defined(OS_ANDROID) 412 #if defined(OS_ANDROID) || defined(OS_MACOSX)
377 // Tests ReadRemoteDescriptor on two descriptors. 413 // Tests ReadRemoteDescriptor on two descriptors.
378 TEST_F(BluetoothRemoteGattDescriptorTest, 414 TEST_F(BluetoothRemoteGattDescriptorTest,
379 ReadRemoteDescriptor_MultipleDescriptors) { 415 ReadRemoteDescriptor_MultipleDescriptors) {
416 if (!PlatformSupportsLowEnergy()) {
417 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
418 return;
419 }
380 ASSERT_NO_FATAL_FAILURE(FakeDescriptorBoilerplate()); 420 ASSERT_NO_FATAL_FAILURE(FakeDescriptorBoilerplate());
381 421
382 descriptor1_->ReadRemoteDescriptor(GetReadValueCallback(Call::EXPECTED), 422 descriptor1_->ReadRemoteDescriptor(GetReadValueCallback(Call::EXPECTED),
383 GetGattErrorCallback(Call::NOT_EXPECTED)); 423 GetGattErrorCallback(Call::NOT_EXPECTED));
384 descriptor2_->ReadRemoteDescriptor(GetReadValueCallback(Call::EXPECTED), 424 descriptor2_->ReadRemoteDescriptor(GetReadValueCallback(Call::EXPECTED),
385 GetGattErrorCallback(Call::NOT_EXPECTED)); 425 GetGattErrorCallback(Call::NOT_EXPECTED));
386 EXPECT_EQ(2, gatt_read_descriptor_attempts_); 426 EXPECT_EQ(2, gatt_read_descriptor_attempts_);
387 EXPECT_EQ(0, callback_count_); 427 EXPECT_EQ(0, callback_count_);
388 EXPECT_EQ(0, error_callback_count_); 428 EXPECT_EQ(0, error_callback_count_);
389 429
390 std::vector<uint8_t> test_vector1; 430 std::vector<uint8_t> test_vector1;
391 test_vector1.push_back(111); 431 test_vector1.push_back(111);
392 SimulateGattDescriptorRead(descriptor1_, test_vector1); 432 SimulateGattDescriptorRead(descriptor1_, test_vector1);
393 base::RunLoop().RunUntilIdle(); 433 base::RunLoop().RunUntilIdle();
394 EXPECT_EQ(test_vector1, last_read_value_); 434 EXPECT_EQ(test_vector1, last_read_value_);
395 435
396 std::vector<uint8_t> test_vector2; 436 std::vector<uint8_t> test_vector2;
397 test_vector2.push_back(222); 437 test_vector2.push_back(222);
398 SimulateGattDescriptorRead(descriptor2_, test_vector2); 438 SimulateGattDescriptorRead(descriptor2_, test_vector2);
399 base::RunLoop().RunUntilIdle(); 439 base::RunLoop().RunUntilIdle();
400 EXPECT_EQ(test_vector2, last_read_value_); 440 EXPECT_EQ(test_vector2, last_read_value_);
401 441
402 EXPECT_EQ(2, callback_count_); 442 EXPECT_EQ(2, callback_count_);
403 EXPECT_EQ(0, error_callback_count_); 443 EXPECT_EQ(0, error_callback_count_);
404 EXPECT_EQ(test_vector1, descriptor1_->GetValue()); 444 EXPECT_EQ(test_vector1, descriptor1_->GetValue());
405 EXPECT_EQ(test_vector2, descriptor2_->GetValue()); 445 EXPECT_EQ(test_vector2, descriptor2_->GetValue());
406 } 446 }
407 #endif // defined(OS_ANDROID) 447 #endif // defined(OS_ANDROID) || defined(OS_MACOSX)
408 448
409 #if defined(OS_ANDROID) 449 #if defined(OS_ANDROID) || defined(OS_MACOSX)
410 // Tests WriteRemoteDescriptor on two descriptors. 450 // Tests WriteRemoteDescriptor on two descriptors.
411 TEST_F(BluetoothRemoteGattDescriptorTest, 451 TEST_F(BluetoothRemoteGattDescriptorTest,
412 WriteRemoteDescriptor_MultipleDescriptors) { 452 WriteRemoteDescriptor_MultipleDescriptors) {
453 if (!PlatformSupportsLowEnergy()) {
454 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
455 return;
456 }
413 ASSERT_NO_FATAL_FAILURE(FakeDescriptorBoilerplate()); 457 ASSERT_NO_FATAL_FAILURE(FakeDescriptorBoilerplate());
414 458
415 std::vector<uint8_t> test_vector1; 459 std::vector<uint8_t> test_vector1;
416 test_vector1.push_back(111); 460 test_vector1.push_back(111);
417 descriptor1_->WriteRemoteDescriptor(test_vector1, GetCallback(Call::EXPECTED), 461 descriptor1_->WriteRemoteDescriptor(test_vector1, GetCallback(Call::EXPECTED),
418 GetGattErrorCallback(Call::NOT_EXPECTED)); 462 GetGattErrorCallback(Call::NOT_EXPECTED));
419 EXPECT_EQ(test_vector1, last_write_value_); 463 EXPECT_EQ(test_vector1, last_write_value_);
420 464
421 std::vector<uint8_t> test_vector2; 465 std::vector<uint8_t> test_vector2;
422 test_vector2.push_back(222); 466 test_vector2.push_back(222);
423 descriptor2_->WriteRemoteDescriptor(test_vector2, GetCallback(Call::EXPECTED), 467 descriptor2_->WriteRemoteDescriptor(test_vector2, GetCallback(Call::EXPECTED),
424 GetGattErrorCallback(Call::NOT_EXPECTED)); 468 GetGattErrorCallback(Call::NOT_EXPECTED));
425 EXPECT_EQ(test_vector2, last_write_value_); 469 EXPECT_EQ(test_vector2, last_write_value_);
426 470
427 EXPECT_EQ(2, gatt_write_descriptor_attempts_); 471 EXPECT_EQ(2, gatt_write_descriptor_attempts_);
428 EXPECT_EQ(0, callback_count_); 472 EXPECT_EQ(0, callback_count_);
429 EXPECT_EQ(0, error_callback_count_); 473 EXPECT_EQ(0, error_callback_count_);
430 474
431 SimulateGattDescriptorWrite(descriptor1_); 475 SimulateGattDescriptorWrite(descriptor1_);
432 SimulateGattDescriptorWrite(descriptor2_); 476 SimulateGattDescriptorWrite(descriptor2_);
433 base::RunLoop().RunUntilIdle(); 477 base::RunLoop().RunUntilIdle();
434 478
435 EXPECT_EQ(2, callback_count_); 479 EXPECT_EQ(2, callback_count_);
436 EXPECT_EQ(0, error_callback_count_); 480 EXPECT_EQ(0, error_callback_count_);
437 } 481 }
438 #endif // defined(OS_ANDROID) 482 #endif // defined(OS_ANDROID) || defined(OS_MACOSX)
439 483
440 #if defined(OS_ANDROID) 484 #if defined(OS_ANDROID) || defined(OS_MACOSX)
441 // Tests ReadRemoteDescriptor asynchronous error. 485 // Tests ReadRemoteDescriptor asynchronous error.
442 TEST_F(BluetoothRemoteGattDescriptorTest, ReadError) { 486 TEST_F(BluetoothRemoteGattDescriptorTest, ReadError) {
487 if (!PlatformSupportsLowEnergy()) {
488 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
489 return;
490 }
443 ASSERT_NO_FATAL_FAILURE(FakeDescriptorBoilerplate()); 491 ASSERT_NO_FATAL_FAILURE(FakeDescriptorBoilerplate());
444 492
445 descriptor1_->ReadRemoteDescriptor(GetReadValueCallback(Call::NOT_EXPECTED), 493 descriptor1_->ReadRemoteDescriptor(GetReadValueCallback(Call::NOT_EXPECTED),
446 GetGattErrorCallback(Call::EXPECTED)); 494 GetGattErrorCallback(Call::EXPECTED));
447 SimulateGattDescriptorReadError( 495 SimulateGattDescriptorReadError(
448 descriptor1_, BluetoothRemoteGattService::GATT_ERROR_INVALID_LENGTH); 496 descriptor1_, BluetoothRemoteGattService::GATT_ERROR_INVALID_LENGTH);
449 SimulateGattDescriptorReadError( 497 SimulateGattDescriptorReadError(
450 descriptor1_, BluetoothRemoteGattService::GATT_ERROR_FAILED); 498 descriptor1_, BluetoothRemoteGattService::GATT_ERROR_FAILED);
451 base::RunLoop().RunUntilIdle(); 499 base::RunLoop().RunUntilIdle();
452 EXPECT_EQ(BluetoothRemoteGattService::GATT_ERROR_INVALID_LENGTH, 500 EXPECT_EQ(BluetoothRemoteGattService::GATT_ERROR_INVALID_LENGTH,
453 last_gatt_error_code_); 501 last_gatt_error_code_);
454 } 502 }
455 #endif // defined(OS_ANDROID) 503 #endif // defined(OS_ANDROID) || defined(OS_MACOSX)
456 504
457 #if defined(OS_ANDROID) 505 #if defined(OS_ANDROID) || defined(OS_MACOSX)
458 // Tests WriteRemoteDescriptor asynchronous error. 506 // Tests WriteRemoteDescriptor asynchronous error.
459 TEST_F(BluetoothRemoteGattDescriptorTest, WriteError) { 507 TEST_F(BluetoothRemoteGattDescriptorTest, WriteError) {
508 if (!PlatformSupportsLowEnergy()) {
509 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
510 return;
511 }
460 ASSERT_NO_FATAL_FAILURE(FakeDescriptorBoilerplate()); 512 ASSERT_NO_FATAL_FAILURE(FakeDescriptorBoilerplate());
461 513
462 std::vector<uint8_t> empty_vector; 514 std::vector<uint8_t> empty_vector;
463 descriptor1_->WriteRemoteDescriptor(empty_vector, 515 descriptor1_->WriteRemoteDescriptor(empty_vector,
464 GetCallback(Call::NOT_EXPECTED), 516 GetCallback(Call::NOT_EXPECTED),
465 GetGattErrorCallback(Call::EXPECTED)); 517 GetGattErrorCallback(Call::EXPECTED));
466 SimulateGattDescriptorWriteError( 518 SimulateGattDescriptorWriteError(
467 descriptor1_, BluetoothRemoteGattService::GATT_ERROR_INVALID_LENGTH); 519 descriptor1_, BluetoothRemoteGattService::GATT_ERROR_INVALID_LENGTH);
468 SimulateGattDescriptorWriteError( 520 SimulateGattDescriptorWriteError(
469 descriptor1_, BluetoothRemoteGattService::GATT_ERROR_FAILED); 521 descriptor1_, BluetoothRemoteGattService::GATT_ERROR_FAILED);
470 base::RunLoop().RunUntilIdle(); 522 base::RunLoop().RunUntilIdle();
471 523
472 EXPECT_EQ(BluetoothRemoteGattService::GATT_ERROR_INVALID_LENGTH, 524 EXPECT_EQ(BluetoothRemoteGattService::GATT_ERROR_INVALID_LENGTH,
473 last_gatt_error_code_); 525 last_gatt_error_code_);
474 } 526 }
475 #endif // defined(OS_ANDROID) 527 #endif // defined(OS_ANDROID) || defined(OS_MACOSX)
476 528
477 #if defined(OS_ANDROID) 529 #if defined(OS_ANDROID)
478 // Tests ReadRemoteDescriptor synchronous error. 530 // Tests ReadRemoteDescriptor synchronous error.
531 // Test not relevant for macOS since descriptor read cannot generate
532 // synchronous error.
479 TEST_F(BluetoothRemoteGattDescriptorTest, ReadSynchronousError) { 533 TEST_F(BluetoothRemoteGattDescriptorTest, ReadSynchronousError) {
534 if (!PlatformSupportsLowEnergy()) {
535 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
536 return;
537 }
480 ASSERT_NO_FATAL_FAILURE(FakeDescriptorBoilerplate()); 538 ASSERT_NO_FATAL_FAILURE(FakeDescriptorBoilerplate());
481 539
482 SimulateGattDescriptorReadWillFailSynchronouslyOnce(descriptor1_); 540 SimulateGattDescriptorReadWillFailSynchronouslyOnce(descriptor1_);
483 descriptor1_->ReadRemoteDescriptor(GetReadValueCallback(Call::NOT_EXPECTED), 541 descriptor1_->ReadRemoteDescriptor(GetReadValueCallback(Call::NOT_EXPECTED),
484 GetGattErrorCallback(Call::EXPECTED)); 542 GetGattErrorCallback(Call::EXPECTED));
485 EXPECT_EQ(0, gatt_read_descriptor_attempts_); 543 EXPECT_EQ(0, gatt_read_descriptor_attempts_);
486 base::RunLoop().RunUntilIdle(); 544 base::RunLoop().RunUntilIdle();
487 EXPECT_EQ(0, callback_count_); 545 EXPECT_EQ(0, callback_count_);
488 EXPECT_EQ(1, error_callback_count_); 546 EXPECT_EQ(1, error_callback_count_);
489 EXPECT_EQ(BluetoothRemoteGattService::GATT_ERROR_FAILED, 547 EXPECT_EQ(BluetoothRemoteGattService::GATT_ERROR_FAILED,
490 last_gatt_error_code_); 548 last_gatt_error_code_);
491 549
492 // After failing once, can succeed: 550 // After failing once, can succeed:
493 ResetEventCounts(); 551 ResetEventCounts();
494 descriptor1_->ReadRemoteDescriptor(GetReadValueCallback(Call::EXPECTED), 552 descriptor1_->ReadRemoteDescriptor(GetReadValueCallback(Call::EXPECTED),
495 GetGattErrorCallback(Call::NOT_EXPECTED)); 553 GetGattErrorCallback(Call::NOT_EXPECTED));
496 EXPECT_EQ(1, gatt_read_descriptor_attempts_); 554 EXPECT_EQ(1, gatt_read_descriptor_attempts_);
497 std::vector<uint8_t> empty_vector; 555 std::vector<uint8_t> empty_vector;
498 SimulateGattDescriptorRead(descriptor1_, empty_vector); 556 SimulateGattDescriptorRead(descriptor1_, empty_vector);
499 base::RunLoop().RunUntilIdle(); 557 base::RunLoop().RunUntilIdle();
500 EXPECT_EQ(1, callback_count_); 558 EXPECT_EQ(1, callback_count_);
501 EXPECT_EQ(0, error_callback_count_); 559 EXPECT_EQ(0, error_callback_count_);
502 } 560 }
503 #endif // defined(OS_ANDROID) 561 #endif // defined(OS_ANDROID)
504 562
505 #if defined(OS_ANDROID) 563 #if defined(OS_ANDROID)
506 // Tests WriteRemoteDescriptor synchronous error. 564 // Tests WriteRemoteDescriptor synchronous error.
565 // Test not relevant for macOS since descriptor write cannot generate
566 // synchronous error.
507 TEST_F(BluetoothRemoteGattDescriptorTest, WriteSynchronousError) { 567 TEST_F(BluetoothRemoteGattDescriptorTest, WriteSynchronousError) {
568 if (!PlatformSupportsLowEnergy()) {
569 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
570 return;
571 }
508 ASSERT_NO_FATAL_FAILURE(FakeDescriptorBoilerplate()); 572 ASSERT_NO_FATAL_FAILURE(FakeDescriptorBoilerplate());
509 573
510 SimulateGattDescriptorWriteWillFailSynchronouslyOnce(descriptor1_); 574 SimulateGattDescriptorWriteWillFailSynchronouslyOnce(descriptor1_);
511 std::vector<uint8_t> empty_vector; 575 std::vector<uint8_t> empty_vector;
512 descriptor1_->WriteRemoteDescriptor(empty_vector, 576 descriptor1_->WriteRemoteDescriptor(empty_vector,
513 GetCallback(Call::NOT_EXPECTED), 577 GetCallback(Call::NOT_EXPECTED),
514 GetGattErrorCallback(Call::EXPECTED)); 578 GetGattErrorCallback(Call::EXPECTED));
515 EXPECT_EQ(0, gatt_write_descriptor_attempts_); 579 EXPECT_EQ(0, gatt_write_descriptor_attempts_);
516 base::RunLoop().RunUntilIdle(); 580 base::RunLoop().RunUntilIdle();
517 EXPECT_EQ(0, callback_count_); 581 EXPECT_EQ(0, callback_count_);
518 EXPECT_EQ(1, error_callback_count_); 582 EXPECT_EQ(1, error_callback_count_);
519 EXPECT_EQ(BluetoothRemoteGattService::GATT_ERROR_FAILED, 583 EXPECT_EQ(BluetoothRemoteGattService::GATT_ERROR_FAILED,
520 last_gatt_error_code_); 584 last_gatt_error_code_);
521 585
522 // After failing once, can succeed: 586 // After failing once, can succeed:
523 ResetEventCounts(); 587 ResetEventCounts();
524 descriptor1_->WriteRemoteDescriptor(empty_vector, GetCallback(Call::EXPECTED), 588 descriptor1_->WriteRemoteDescriptor(empty_vector, GetCallback(Call::EXPECTED),
525 GetGattErrorCallback(Call::NOT_EXPECTED)); 589 GetGattErrorCallback(Call::NOT_EXPECTED));
526 EXPECT_EQ(1, gatt_write_descriptor_attempts_); 590 EXPECT_EQ(1, gatt_write_descriptor_attempts_);
527 SimulateGattDescriptorWrite(descriptor1_); 591 SimulateGattDescriptorWrite(descriptor1_);
528 base::RunLoop().RunUntilIdle(); 592 base::RunLoop().RunUntilIdle();
529 EXPECT_EQ(1, callback_count_); 593 EXPECT_EQ(1, callback_count_);
530 EXPECT_EQ(0, error_callback_count_); 594 EXPECT_EQ(0, error_callback_count_);
531 } 595 }
532 #endif // defined(OS_ANDROID) 596 #endif // defined(OS_ANDROID)
533 597
534 #if defined(OS_ANDROID) 598 #if defined(OS_ANDROID) || defined(OS_MACOSX)
535 // Tests ReadRemoteDescriptor error with a pending read operation. 599 // Tests ReadRemoteDescriptor error with a pending read operation.
536 TEST_F(BluetoothRemoteGattDescriptorTest, ReadRemoteDescriptor_ReadPending) { 600 TEST_F(BluetoothRemoteGattDescriptorTest, ReadRemoteDescriptor_ReadPending) {
601 if (!PlatformSupportsLowEnergy()) {
602 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
603 return;
604 }
537 ASSERT_NO_FATAL_FAILURE(FakeDescriptorBoilerplate()); 605 ASSERT_NO_FATAL_FAILURE(FakeDescriptorBoilerplate());
538 606
539 descriptor1_->ReadRemoteDescriptor(GetReadValueCallback(Call::EXPECTED), 607 descriptor1_->ReadRemoteDescriptor(GetReadValueCallback(Call::EXPECTED),
540 GetGattErrorCallback(Call::NOT_EXPECTED)); 608 GetGattErrorCallback(Call::NOT_EXPECTED));
541 descriptor1_->ReadRemoteDescriptor(GetReadValueCallback(Call::NOT_EXPECTED), 609 descriptor1_->ReadRemoteDescriptor(GetReadValueCallback(Call::NOT_EXPECTED),
542 GetGattErrorCallback(Call::EXPECTED)); 610 GetGattErrorCallback(Call::EXPECTED));
543 611
544 base::RunLoop().RunUntilIdle(); 612 base::RunLoop().RunUntilIdle();
545 613
546 EXPECT_EQ(0, callback_count_); 614 EXPECT_EQ(0, callback_count_);
547 EXPECT_EQ(1, error_callback_count_); 615 EXPECT_EQ(1, error_callback_count_);
548 EXPECT_EQ(BluetoothRemoteGattService::GATT_ERROR_IN_PROGRESS, 616 EXPECT_EQ(BluetoothRemoteGattService::GATT_ERROR_IN_PROGRESS,
549 last_gatt_error_code_); 617 last_gatt_error_code_);
550 618
551 // Initial read should still succeed: 619 // Initial read should still succeed:
552 ResetEventCounts(); 620 ResetEventCounts();
553 std::vector<uint8_t> empty_vector; 621 std::vector<uint8_t> empty_vector;
554 SimulateGattDescriptorRead(descriptor1_, empty_vector); 622 SimulateGattDescriptorRead(descriptor1_, empty_vector);
555 base::RunLoop().RunUntilIdle(); 623 base::RunLoop().RunUntilIdle();
556 EXPECT_EQ(1, callback_count_); 624 EXPECT_EQ(1, callback_count_);
557 EXPECT_EQ(0, error_callback_count_); 625 EXPECT_EQ(0, error_callback_count_);
558 } 626 }
559 #endif // defined(OS_ANDROID) 627 #endif // defined(OS_ANDROID) || defined(OS_MACOSX)
560 628
561 #if defined(OS_ANDROID) 629 #if defined(OS_ANDROID) || defined(OS_MACOSX)
562 // Tests WriteRemoteDescriptor error with a pending write operation. 630 // Tests WriteRemoteDescriptor error with a pending write operation.
563 TEST_F(BluetoothRemoteGattDescriptorTest, WriteRemoteDescriptor_WritePending) { 631 TEST_F(BluetoothRemoteGattDescriptorTest, WriteRemoteDescriptor_WritePending) {
632 if (!PlatformSupportsLowEnergy()) {
633 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
634 return;
635 }
564 ASSERT_NO_FATAL_FAILURE(FakeDescriptorBoilerplate()); 636 ASSERT_NO_FATAL_FAILURE(FakeDescriptorBoilerplate());
565 637
566 std::vector<uint8_t> empty_vector; 638 std::vector<uint8_t> empty_vector;
567 descriptor1_->WriteRemoteDescriptor(empty_vector, GetCallback(Call::EXPECTED), 639 descriptor1_->WriteRemoteDescriptor(empty_vector, GetCallback(Call::EXPECTED),
568 GetGattErrorCallback(Call::NOT_EXPECTED)); 640 GetGattErrorCallback(Call::NOT_EXPECTED));
569 descriptor1_->WriteRemoteDescriptor(empty_vector, 641 descriptor1_->WriteRemoteDescriptor(empty_vector,
570 GetCallback(Call::NOT_EXPECTED), 642 GetCallback(Call::NOT_EXPECTED),
571 GetGattErrorCallback(Call::EXPECTED)); 643 GetGattErrorCallback(Call::EXPECTED));
572 644
573 base::RunLoop().RunUntilIdle(); 645 base::RunLoop().RunUntilIdle();
574 646
575 EXPECT_EQ(0, callback_count_); 647 EXPECT_EQ(0, callback_count_);
576 EXPECT_EQ(1, error_callback_count_); 648 EXPECT_EQ(1, error_callback_count_);
577 EXPECT_EQ(BluetoothRemoteGattService::GATT_ERROR_IN_PROGRESS, 649 EXPECT_EQ(BluetoothRemoteGattService::GATT_ERROR_IN_PROGRESS,
578 last_gatt_error_code_); 650 last_gatt_error_code_);
579 651
580 // Initial write should still succeed: 652 // Initial write should still succeed:
581 ResetEventCounts(); 653 ResetEventCounts();
582 SimulateGattDescriptorWrite(descriptor1_); 654 SimulateGattDescriptorWrite(descriptor1_);
583 base::RunLoop().RunUntilIdle(); 655 base::RunLoop().RunUntilIdle();
584 EXPECT_EQ(1, callback_count_); 656 EXPECT_EQ(1, callback_count_);
585 EXPECT_EQ(0, error_callback_count_); 657 EXPECT_EQ(0, error_callback_count_);
586 } 658 }
587 #endif // defined(OS_ANDROID) 659 #endif // defined(OS_ANDROID) || defined(OS_MACOSX)
588 660
589 #if defined(OS_ANDROID) 661 #if defined(OS_ANDROID) || defined(OS_MACOSX)
590 // Tests ReadRemoteDescriptor error with a pending write operation. 662 // Tests ReadRemoteDescriptor error with a pending write operation.
591 TEST_F(BluetoothRemoteGattDescriptorTest, ReadRemoteDescriptor_WritePending) { 663 TEST_F(BluetoothRemoteGattDescriptorTest, ReadRemoteDescriptor_WritePending) {
664 if (!PlatformSupportsLowEnergy()) {
665 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
666 return;
667 }
592 ASSERT_NO_FATAL_FAILURE(FakeDescriptorBoilerplate()); 668 ASSERT_NO_FATAL_FAILURE(FakeDescriptorBoilerplate());
593 669
594 std::vector<uint8_t> empty_vector; 670 std::vector<uint8_t> empty_vector;
595 descriptor1_->WriteRemoteDescriptor(empty_vector, GetCallback(Call::EXPECTED), 671 descriptor1_->WriteRemoteDescriptor(empty_vector, GetCallback(Call::EXPECTED),
596 GetGattErrorCallback(Call::NOT_EXPECTED)); 672 GetGattErrorCallback(Call::NOT_EXPECTED));
597 descriptor1_->ReadRemoteDescriptor(GetReadValueCallback(Call::NOT_EXPECTED), 673 descriptor1_->ReadRemoteDescriptor(GetReadValueCallback(Call::NOT_EXPECTED),
598 GetGattErrorCallback(Call::EXPECTED)); 674 GetGattErrorCallback(Call::EXPECTED));
599 675
600 base::RunLoop().RunUntilIdle(); 676 base::RunLoop().RunUntilIdle();
601 677
602 EXPECT_EQ(0, callback_count_); 678 EXPECT_EQ(0, callback_count_);
603 EXPECT_EQ(1, error_callback_count_); 679 EXPECT_EQ(1, error_callback_count_);
604 EXPECT_EQ(BluetoothRemoteGattService::GATT_ERROR_IN_PROGRESS, 680 EXPECT_EQ(BluetoothRemoteGattService::GATT_ERROR_IN_PROGRESS,
605 last_gatt_error_code_); 681 last_gatt_error_code_);
606 682
607 // Initial write should still succeed: 683 // Initial write should still succeed:
608 ResetEventCounts(); 684 ResetEventCounts();
609 SimulateGattDescriptorWrite(descriptor1_); 685 SimulateGattDescriptorWrite(descriptor1_);
610 base::RunLoop().RunUntilIdle(); 686 base::RunLoop().RunUntilIdle();
611 EXPECT_EQ(1, callback_count_); 687 EXPECT_EQ(1, callback_count_);
612 EXPECT_EQ(0, error_callback_count_); 688 EXPECT_EQ(0, error_callback_count_);
613 } 689 }
614 #endif // defined(OS_ANDROID) 690 #endif // defined(OS_ANDROID) || defined(OS_MACOSX)
615 691
616 #if defined(OS_ANDROID) 692 #if defined(OS_ANDROID) || defined(OS_MACOSX)
617 // Tests WriteRemoteDescriptor error with a pending Read operation. 693 // Tests WriteRemoteDescriptor error with a pending Read operation.
618 TEST_F(BluetoothRemoteGattDescriptorTest, WriteRemoteDescriptor_ReadPending) { 694 TEST_F(BluetoothRemoteGattDescriptorTest, WriteRemoteDescriptor_ReadPending) {
695 if (!PlatformSupportsLowEnergy()) {
696 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
697 return;
698 }
619 ASSERT_NO_FATAL_FAILURE(FakeDescriptorBoilerplate()); 699 ASSERT_NO_FATAL_FAILURE(FakeDescriptorBoilerplate());
620 700
621 std::vector<uint8_t> empty_vector; 701 std::vector<uint8_t> empty_vector;
622 descriptor1_->ReadRemoteDescriptor(GetReadValueCallback(Call::EXPECTED), 702 descriptor1_->ReadRemoteDescriptor(GetReadValueCallback(Call::EXPECTED),
623 GetGattErrorCallback(Call::NOT_EXPECTED)); 703 GetGattErrorCallback(Call::NOT_EXPECTED));
624 descriptor1_->WriteRemoteDescriptor(empty_vector, 704 descriptor1_->WriteRemoteDescriptor(empty_vector,
625 GetCallback(Call::NOT_EXPECTED), 705 GetCallback(Call::NOT_EXPECTED),
626 GetGattErrorCallback(Call::EXPECTED)); 706 GetGattErrorCallback(Call::EXPECTED));
627 base::RunLoop().RunUntilIdle(); 707 base::RunLoop().RunUntilIdle();
628 708
629 EXPECT_EQ(0, callback_count_); 709 EXPECT_EQ(0, callback_count_);
630 EXPECT_EQ(1, error_callback_count_); 710 EXPECT_EQ(1, error_callback_count_);
631 EXPECT_EQ(BluetoothRemoteGattService::GATT_ERROR_IN_PROGRESS, 711 EXPECT_EQ(BluetoothRemoteGattService::GATT_ERROR_IN_PROGRESS,
632 last_gatt_error_code_); 712 last_gatt_error_code_);
633 713
634 // Initial read should still succeed: 714 // Initial read should still succeed:
635 ResetEventCounts(); 715 ResetEventCounts();
636 SimulateGattDescriptorRead(descriptor1_, empty_vector); 716 SimulateGattDescriptorRead(descriptor1_, empty_vector);
637 base::RunLoop().RunUntilIdle(); 717 base::RunLoop().RunUntilIdle();
638 EXPECT_EQ(1, callback_count_); 718 EXPECT_EQ(1, callback_count_);
639 EXPECT_EQ(0, error_callback_count_); 719 EXPECT_EQ(0, error_callback_count_);
640 } 720 }
641 #endif // defined(OS_ANDROID) 721 #endif // defined(OS_ANDROID) || defined(OS_MACOSX)
642 722
643 #if defined(OS_ANDROID) 723 #if defined(OS_ANDROID)
644 // Tests that read requests after a device disconnects but before the 724 // Tests that read requests after a device disconnects but before the
645 // disconnect task runs do not result in a crash. 725 // disconnect task runs do not result in a crash.
646 // macOS: Does not apply. All events arrive on the UI Thread. 726 // macOS: Does not apply. All events arrive on the UI Thread.
647 // TODO(crbug.com/694102): Enable this test on Windows. 727 // TODO(crbug.com/694102): Enable this test on Windows.
648 TEST_F(BluetoothRemoteGattDescriptorTest, ReadDuringDisconnect) { 728 TEST_F(BluetoothRemoteGattDescriptorTest, ReadDuringDisconnect) {
649 if (!PlatformSupportsLowEnergy()) { 729 if (!PlatformSupportsLowEnergy()) {
650 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; 730 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
651 return; 731 return;
(...skipping 30 matching lines...) Expand all
682 descriptor1_->WriteRemoteDescriptor( 762 descriptor1_->WriteRemoteDescriptor(
683 std::vector<uint8_t>(), GetCallback(Call::NOT_EXPECTED), 763 std::vector<uint8_t>(), GetCallback(Call::NOT_EXPECTED),
684 // TODO(crbug.com/621901): Expect an error. 764 // TODO(crbug.com/621901): Expect an error.
685 GetGattErrorCallback(Call::NOT_EXPECTED)); 765 GetGattErrorCallback(Call::NOT_EXPECTED));
686 766
687 base::RunLoop().RunUntilIdle(); 767 base::RunLoop().RunUntilIdle();
688 // TODO(crbug.com/621901): Test that an error was returned. 768 // TODO(crbug.com/621901): Test that an error was returned.
689 } 769 }
690 #endif // defined(OS_ANDROID) 770 #endif // defined(OS_ANDROID)
691 771
772 #if defined(OS_MACOSX)
773 // Tests NSString for descriptor value for macOS.
774 // https://developer.apple.com/reference/corebluetooth/cbdescriptor
775 TEST_F(BluetoothRemoteGattDescriptorTest, ReadRemoteDescriptor_NSString) {
776 if (!PlatformSupportsLowEnergy()) {
777 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
778 return;
779 }
780 ASSERT_NO_FATAL_FAILURE(FakeDescriptorBoilerplate());
781
782 descriptor1_->ReadRemoteDescriptor(GetReadValueCallback(Call::EXPECTED),
783 GetGattErrorCallback(Call::NOT_EXPECTED));
784 EXPECT_EQ(1, gatt_read_descriptor_attempts_);
785
786 std::string test_string = "Hello";
787 SimulateGattDescriptorReadNSString(descriptor1_, test_string);
788 base::RunLoop().RunUntilIdle();
789
790 std::vector<uint8_t> test_vector(test_string.begin(), test_string.end());
791 EXPECT_EQ(test_vector, last_read_value_);
792 EXPECT_EQ(test_vector, descriptor1_->GetValue());
793 }
794
795 // Tests NSNumber for descriptor value for macOS.
796 // https://developer.apple.com/reference/corebluetooth/cbdescriptor
797 TEST_F(BluetoothRemoteGattDescriptorTest, ReadRemoteDescriptor_NSNumber) {
798 if (!PlatformSupportsLowEnergy()) {
799 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
800 return;
801 }
802 ASSERT_NO_FATAL_FAILURE(FakeDescriptorBoilerplate());
803
804 descriptor1_->ReadRemoteDescriptor(GetReadValueCallback(Call::EXPECTED),
805 GetGattErrorCallback(Call::NOT_EXPECTED));
806 EXPECT_EQ(1, gatt_read_descriptor_attempts_);
807
808 const short test_number = 0x1234;
809 SimulateGattDescriptorReadNSNumber(descriptor1_, test_number);
810 base::RunLoop().RunUntilIdle();
811
812 uint8_t values[] = {0x34, 0x12};
813 std::vector<uint8_t> test_vector(values, values + arraysize(values));
814 EXPECT_EQ(test_vector, last_read_value_);
815 EXPECT_EQ(test_vector, descriptor1_->GetValue());
816 }
817 #endif // defined(OS_MACOSX)
818
692 } // namespace device 819 } // namespace device
OLDNEW
« no previous file with comments | « device/bluetooth/bluetooth_remote_gatt_descriptor_mac.mm ('k') | device/bluetooth/test/bluetooth_test_mac.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698