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

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

Issue 1779083002: bluetooth: Test & make StartNotifySession reentrant. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@bta-notify-followup-descriptors-
Patch Set: addressed ortuno comments Created 4 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
« no previous file with comments | « no previous file | device/bluetooth/bluetooth_gatt_descriptor_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "device/bluetooth/bluetooth_gatt_characteristic.h" 5 #include "device/bluetooth/bluetooth_gatt_characteristic.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include "base/macros.h" 9 #include "base/macros.h"
10 #include "base/run_loop.h" 10 #include "base/run_loop.h"
(...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after
302 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate( 302 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(
303 BluetoothGattCharacteristic::PROPERTY_WRITE)); 303 BluetoothGattCharacteristic::PROPERTY_WRITE));
304 304
305 std::vector<uint8_t> empty_vector; 305 std::vector<uint8_t> empty_vector;
306 characteristic1_->WriteRemoteCharacteristic( 306 characteristic1_->WriteRemoteCharacteristic(
307 empty_vector, GetCallback(Call::EXPECTED), 307 empty_vector, GetCallback(Call::EXPECTED),
308 GetGattErrorCallback(Call::NOT_EXPECTED)); 308 GetGattErrorCallback(Call::NOT_EXPECTED));
309 EXPECT_EQ(1, gatt_write_characteristic_attempts_); 309 EXPECT_EQ(1, gatt_write_characteristic_attempts_);
310 SimulateGattCharacteristicWrite(characteristic1_); 310 SimulateGattCharacteristicWrite(characteristic1_);
311 311
312 // Duplicate write reported from OS shouldn't cause a problem:
313 SimulateGattCharacteristicWrite(characteristic1_);
314
312 EXPECT_EQ(empty_vector, last_write_value_); 315 EXPECT_EQ(empty_vector, last_write_value_);
313 } 316 }
314 #endif // defined(OS_ANDROID) || defined(OS_WIN) 317 #endif // defined(OS_ANDROID) || defined(OS_WIN)
315 318
316 #if defined(OS_ANDROID) || defined(OS_WIN) 319 #if defined(OS_ANDROID) || defined(OS_WIN)
317 // Tests ReadRemoteCharacteristic completing after Chrome objects are deleted. 320 // Tests ReadRemoteCharacteristic completing after Chrome objects are deleted.
318 TEST_F(BluetoothGattCharacteristicTest, ReadRemoteCharacteristic_AfterDeleted) { 321 TEST_F(BluetoothGattCharacteristicTest, ReadRemoteCharacteristic_AfterDeleted) {
319 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate( 322 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(
320 BluetoothGattCharacteristic::PROPERTY_READ)); 323 BluetoothGattCharacteristic::PROPERTY_READ));
321 324
322 characteristic1_->ReadRemoteCharacteristic( 325 characteristic1_->ReadRemoteCharacteristic(
323 GetReadValueCallback(Call::NOT_EXPECTED), 326 GetReadValueCallback(Call::NOT_EXPECTED),
324 GetGattErrorCallback(Call::NOT_EXPECTED)); 327 GetGattErrorCallback(Call::NOT_EXPECTED));
325 328
326 RememberCharacteristicForSubsequentAction(characteristic1_); 329 RememberCharacteristicForSubsequentAction(characteristic1_);
327 DeleteDevice(device_); 330 DeleteDevice(device_); // TODO(576906) delete only the characteristic.
328 331
329 std::vector<uint8_t> empty_vector; 332 std::vector<uint8_t> empty_vector;
330 SimulateGattCharacteristicRead(/* use remembered characteristic */ nullptr, 333 SimulateGattCharacteristicRead(/* use remembered characteristic */ nullptr,
331 empty_vector); 334 empty_vector);
332 EXPECT_TRUE("Did not crash!"); 335 EXPECT_TRUE("Did not crash!");
333 } 336 }
334 #endif // defined(OS_ANDROID) || defined(OS_WIN) 337 #endif // defined(OS_ANDROID) || defined(OS_WIN)
335 338
336 #if defined(OS_ANDROID) || defined(OS_WIN) 339 #if defined(OS_ANDROID) || defined(OS_WIN)
337 // Tests WriteRemoteCharacteristic completing after Chrome objects are deleted. 340 // Tests WriteRemoteCharacteristic completing after Chrome objects are deleted.
338 TEST_F(BluetoothGattCharacteristicTest, 341 TEST_F(BluetoothGattCharacteristicTest,
339 WriteRemoteCharacteristic_AfterDeleted) { 342 WriteRemoteCharacteristic_AfterDeleted) {
340 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate( 343 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(
341 BluetoothGattCharacteristic::PROPERTY_WRITE)); 344 BluetoothGattCharacteristic::PROPERTY_WRITE));
342 345
343 std::vector<uint8_t> empty_vector; 346 std::vector<uint8_t> empty_vector;
344 characteristic1_->WriteRemoteCharacteristic( 347 characteristic1_->WriteRemoteCharacteristic(
345 empty_vector, GetCallback(Call::NOT_EXPECTED), 348 empty_vector, GetCallback(Call::NOT_EXPECTED),
346 GetGattErrorCallback(Call::NOT_EXPECTED)); 349 GetGattErrorCallback(Call::NOT_EXPECTED));
347 350
348 RememberCharacteristicForSubsequentAction(characteristic1_); 351 RememberCharacteristicForSubsequentAction(characteristic1_);
349 DeleteDevice(device_); 352 DeleteDevice(device_); // TODO(576906) delete only the characteristic.
350 353
351 SimulateGattCharacteristicWrite(/* use remembered characteristic */ nullptr); 354 SimulateGattCharacteristicWrite(/* use remembered characteristic */ nullptr);
352 EXPECT_TRUE("Did not crash!"); 355 EXPECT_TRUE("Did not crash!");
353 } 356 }
354 #endif // defined(OS_ANDROID) || defined(OS_WIN) 357 #endif // defined(OS_ANDROID) || defined(OS_WIN)
355 358
356 #if defined(OS_ANDROID) || defined(OS_WIN) 359 #if defined(OS_ANDROID) || defined(OS_WIN)
357 // Tests ReadRemoteCharacteristic and GetValue with non-empty value buffer. 360 // Tests ReadRemoteCharacteristic and GetValue with non-empty value buffer.
358 TEST_F(BluetoothGattCharacteristicTest, ReadRemoteCharacteristic) { 361 TEST_F(BluetoothGattCharacteristicTest, ReadRemoteCharacteristic) {
359 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate( 362 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after
531 #endif 534 #endif
532 535
533 SimulateGattCharacteristicWrite(characteristic2_); 536 SimulateGattCharacteristicWrite(characteristic2_);
534 #ifndef OS_ANDROID 537 #ifndef OS_ANDROID
535 EXPECT_EQ(test_vector2, last_write_value_); 538 EXPECT_EQ(test_vector2, last_write_value_);
536 #endif 539 #endif
537 540
538 EXPECT_EQ(2, callback_count_); 541 EXPECT_EQ(2, callback_count_);
539 EXPECT_EQ(0, error_callback_count_); 542 EXPECT_EQ(0, error_callback_count_);
540 543
541 // TODO(crbug.com/591740): Remove if define for OS_ANDROID in this test. 544 // TODO(591740): Remove if define for OS_ANDROID in this test.
542 } 545 }
543 #endif // defined(OS_ANDROID) || defined(OS_WIN) 546 #endif // defined(OS_ANDROID) || defined(OS_WIN)
544 547
545 #if defined(OS_ANDROID) || defined(OS_WIN) 548 #if defined(OS_ANDROID) || defined(OS_WIN)
546 // Tests ReadRemoteCharacteristic asynchronous error. 549 // Tests ReadRemoteCharacteristic asynchronous error.
547 TEST_F(BluetoothGattCharacteristicTest, ReadError) { 550 TEST_F(BluetoothGattCharacteristicTest, ReadError) {
548 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate( 551 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(
549 BluetoothGattCharacteristic::PROPERTY_READ)); 552 BluetoothGattCharacteristic::PROPERTY_READ));
550 553
551 characteristic1_->ReadRemoteCharacteristic( 554 characteristic1_->ReadRemoteCharacteristic(
(...skipping 357 matching lines...) Expand 10 before | Expand all | Expand 10 after
909 EXPECT_EQ(characteristic1_->GetIdentifier(), 912 EXPECT_EQ(characteristic1_->GetIdentifier(),
910 notify_sessions_[0]->GetCharacteristicIdentifier()); 913 notify_sessions_[0]->GetCharacteristicIdentifier());
911 EXPECT_EQ(characteristic1_->GetIdentifier(), 914 EXPECT_EQ(characteristic1_->GetIdentifier(),
912 notify_sessions_[1]->GetCharacteristicIdentifier()); 915 notify_sessions_[1]->GetCharacteristicIdentifier());
913 EXPECT_TRUE(notify_sessions_[0]->IsActive()); 916 EXPECT_TRUE(notify_sessions_[0]->IsActive());
914 EXPECT_TRUE(notify_sessions_[1]->IsActive()); 917 EXPECT_TRUE(notify_sessions_[1]->IsActive());
915 } 918 }
916 #endif // defined(OS_ANDROID) 919 #endif // defined(OS_ANDROID)
917 920
918 #if defined(OS_ANDROID) 921 #if defined(OS_ANDROID)
922 // Tests multiple StartNotifySessions pending and then an error.
923 TEST_F(BluetoothGattCharacteristicTest, StartNotifySessionError_Multiple) {
924 ASSERT_NO_FATAL_FAILURE(
925 FakeCharacteristicBoilerplate(/* properties: NOTIFY */ 0x10));
926 SimulateGattDescriptor(
927 characteristic1_,
928 BluetoothGattDescriptor::ClientCharacteristicConfigurationUuid()
929 .canonical_value());
930 ASSERT_EQ(1u, characteristic1_->GetDescriptors().size());
931
932 characteristic1_->StartNotifySession(GetNotifyCallback(Call::NOT_EXPECTED),
933 GetGattErrorCallback(Call::EXPECTED));
934 characteristic1_->StartNotifySession(GetNotifyCallback(Call::NOT_EXPECTED),
935 GetGattErrorCallback(Call::EXPECTED));
936 EXPECT_EQ(1, gatt_notify_characteristic_attempts_);
937 EXPECT_EQ(0, callback_count_);
938 SimulateGattNotifySessionStartError(characteristic1_,
939 BluetoothGattService::GATT_ERROR_FAILED);
940 EXPECT_EQ(0, callback_count_);
941 EXPECT_EQ(2, error_callback_count_);
942 ASSERT_EQ(0u, notify_sessions_.size());
943 EXPECT_EQ(BluetoothGattService::GATT_ERROR_FAILED, last_gatt_error_code_);
944 }
945 #endif // defined(OS_ANDROID)
946
947 #if defined(OS_ANDROID)
948 // Tests StartNotifySession completing after chrome objects are deleted.
949 TEST_F(BluetoothGattCharacteristicTest, StartNotifySession_AfterDeleted) {
950 ASSERT_NO_FATAL_FAILURE(
951 FakeCharacteristicBoilerplate(/* properties: NOTIFY */ 0x10));
952 SimulateGattDescriptor(
953 characteristic1_,
954 BluetoothGattDescriptor::ClientCharacteristicConfigurationUuid()
955 .canonical_value());
956 ASSERT_EQ(1u, characteristic1_->GetDescriptors().size());
957
958 characteristic1_->StartNotifySession(GetNotifyCallback(Call::NOT_EXPECTED),
959 GetGattErrorCallback(Call::EXPECTED));
960 EXPECT_EQ(1, gatt_notify_characteristic_attempts_);
961 EXPECT_EQ(0, callback_count_);
962
963 RememberCharacteristicForSubsequentAction(characteristic1_);
964 RememberCCCDescriptorForSubsequentAction(characteristic1_);
965 DeleteDevice(device_); // TODO(576906) delete only the characteristic.
966
967 SimulateGattNotifySessionStarted(/* use remembered characteristic */ nullptr);
968 EXPECT_EQ(0, callback_count_);
969 EXPECT_EQ(1, error_callback_count_);
970 ASSERT_EQ(0u, notify_sessions_.size());
971 EXPECT_EQ(BluetoothGattService::GATT_ERROR_FAILED, last_gatt_error_code_);
972 }
973 #endif // defined(OS_ANDROID)
974
975 #if defined(OS_ANDROID)
919 // Tests Characteristic Value changes during a Notify Session. 976 // Tests Characteristic Value changes during a Notify Session.
920 TEST_F(BluetoothGattCharacteristicTest, GattCharacteristicValueChanged) { 977 TEST_F(BluetoothGattCharacteristicTest, GattCharacteristicValueChanged) {
921 ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate( 978 ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate(
922 /* properties: NOTIFY */ 0x10, 979 /* properties: NOTIFY */ 0x10,
923 /* expected_config_descriptor_value: NOTIFY */ 1)); 980 /* expected_config_descriptor_value: NOTIFY */ 1));
924 981
925 TestBluetoothAdapterObserver observer(adapter_); 982 TestBluetoothAdapterObserver observer(adapter_);
926 983
927 std::vector<uint8_t> test_vector1, test_vector2; 984 std::vector<uint8_t> test_vector1, test_vector2;
928 test_vector1.push_back(111); 985 test_vector1.push_back(111);
(...skipping 10 matching lines...) Expand all
939 #endif // defined(OS_ANDROID) 996 #endif // defined(OS_ANDROID)
940 997
941 #if defined(OS_ANDROID) 998 #if defined(OS_ANDROID)
942 // Tests Characteristic Value changing after a Notify Session and objects being 999 // Tests Characteristic Value changing after a Notify Session and objects being
943 // destroyed. 1000 // destroyed.
944 TEST_F(BluetoothGattCharacteristicTest, 1001 TEST_F(BluetoothGattCharacteristicTest,
945 GattCharacteristicValueChanged_AfterDeleted) { 1002 GattCharacteristicValueChanged_AfterDeleted) {
946 ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate( 1003 ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate(
947 /* properties: NOTIFY */ 0x10, 1004 /* properties: NOTIFY */ 0x10,
948 /* expected_config_descriptor_value: NOTIFY */ 1)); 1005 /* expected_config_descriptor_value: NOTIFY */ 1));
1006 TestBluetoothAdapterObserver observer(adapter_);
949 1007
950 RememberCharacteristicForSubsequentAction(characteristic1_); 1008 RememberCharacteristicForSubsequentAction(characteristic1_);
951 DeleteDevice(device_); 1009 DeleteDevice(device_); // TODO(576906) delete only the characteristic.
952 1010
953 std::vector<uint8_t> empty_vector; 1011 std::vector<uint8_t> empty_vector;
954 SimulateGattCharacteristicChanged(/* use remembered characteristic */ nullptr, 1012 SimulateGattCharacteristicChanged(/* use remembered characteristic */ nullptr,
955 empty_vector); 1013 empty_vector);
956 EXPECT_TRUE("Did not crash!"); 1014 EXPECT_TRUE("Did not crash!");
1015 EXPECT_EQ(0, observer.gatt_characteristic_value_changed_count());
957 } 1016 }
958 #endif // defined(OS_ANDROID) 1017 #endif // defined(OS_ANDROID)
959 1018
960 #if defined(OS_ANDROID) || defined(OS_WIN) 1019 #if defined(OS_ANDROID) || defined(OS_WIN)
961 TEST_F(BluetoothGattCharacteristicTest, GetDescriptors_FindNone) { 1020 TEST_F(BluetoothGattCharacteristicTest, GetDescriptors_FindNone) {
962 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); 1021 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate());
963 1022
964 EXPECT_EQ(0u, characteristic1_->GetDescriptors().size()); 1023 EXPECT_EQ(0u, characteristic1_->GetDescriptors().size());
965 } 1024 }
966 #endif // defined(OS_ANDROID) || defined(OS_WIN) 1025 #endif // defined(OS_ANDROID) || defined(OS_WIN)
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
1034 EXPECT_EQ(1u, characteristic1_->GetDescriptorsByUUID(id2).size()); 1093 EXPECT_EQ(1u, characteristic1_->GetDescriptorsByUUID(id2).size());
1035 EXPECT_EQ(2u, characteristic2_->GetDescriptorsByUUID(id3).size()); 1094 EXPECT_EQ(2u, characteristic2_->GetDescriptorsByUUID(id3).size());
1036 1095
1037 EXPECT_EQ(0u, characteristic2_->GetDescriptorsByUUID(id1).size()); 1096 EXPECT_EQ(0u, characteristic2_->GetDescriptorsByUUID(id1).size());
1038 EXPECT_EQ(0u, characteristic2_->GetDescriptorsByUUID(id2).size()); 1097 EXPECT_EQ(0u, characteristic2_->GetDescriptorsByUUID(id2).size());
1039 EXPECT_EQ(0u, characteristic1_->GetDescriptorsByUUID(id3).size()); 1098 EXPECT_EQ(0u, characteristic1_->GetDescriptorsByUUID(id3).size());
1040 } 1099 }
1041 #endif // defined(OS_ANDROID) || defined(OS_WIN) 1100 #endif // defined(OS_ANDROID) || defined(OS_WIN)
1042 1101
1043 } // namespace device 1102 } // namespace device
OLDNEW
« no previous file with comments | « no previous file | device/bluetooth/bluetooth_gatt_descriptor_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698