OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "device/bluetooth/bluetooth_gatt_characteristic.h" | 5 #include "device/bluetooth/bluetooth_gatt_characteristic.h" |
6 | 6 |
7 #include <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 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 SimulateGattCharacteristic(service_, uuid, properties); | 45 SimulateGattCharacteristic(service_, uuid, properties); |
46 ASSERT_EQ(2u, service_->GetCharacteristics().size()); | 46 ASSERT_EQ(2u, service_->GetCharacteristics().size()); |
47 characteristic1_ = service_->GetCharacteristics()[0]; | 47 characteristic1_ = service_->GetCharacteristics()[0]; |
48 characteristic2_ = service_->GetCharacteristics()[1]; | 48 characteristic2_ = service_->GetCharacteristics()[1]; |
49 ResetEventCounts(); | 49 ResetEventCounts(); |
50 } | 50 } |
51 | 51 |
52 enum class StartNotifySetupError { | 52 enum class StartNotifySetupError { |
53 CHARACTERISTIC_PROPERTIES, | 53 CHARACTERISTIC_PROPERTIES, |
54 CONFIG_DESCRIPTOR_MISSING, | 54 CONFIG_DESCRIPTOR_MISSING, |
| 55 CONFIG_DESCRIPTOR_DUPLICATE, |
55 SET_NOTIFY, | 56 SET_NOTIFY, |
56 WRITE_DESCRIPTOR, | 57 WRITE_DESCRIPTOR, |
57 NONE | 58 NONE |
58 }; | 59 }; |
59 // Constructs characteristics with |properties|, calls StartNotifySession, | 60 // Constructs characteristics with |properties|, calls StartNotifySession, |
60 // and verifies the appropriate |expected_config_descriptor_value| is written. | 61 // and verifies the appropriate |expected_config_descriptor_value| is written. |
61 // Error scenarios in this boilerplate are tested by setting |error| to the | 62 // Error scenarios in this boilerplate are tested by setting |error| to the |
62 // setup stage to test. | 63 // setup stage to test. |
63 void StartNotifyBoilerplate( | 64 void StartNotifyBoilerplate( |
64 int properties, | 65 int properties, |
65 uint16_t expected_config_descriptor_value, | 66 uint16_t expected_config_descriptor_value, |
66 StartNotifySetupError error = StartNotifySetupError::NONE) { | 67 StartNotifySetupError error = StartNotifySetupError::NONE) { |
67 if (error == StartNotifySetupError::CHARACTERISTIC_PROPERTIES) { | 68 if (error == StartNotifySetupError::CHARACTERISTIC_PROPERTIES) { |
68 properties = 0; | 69 properties = 0; |
69 } | 70 } |
70 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(properties)); | 71 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(properties)); |
71 | 72 |
| 73 size_t expected_descriptors_count = 0; |
72 if (error != StartNotifySetupError::CONFIG_DESCRIPTOR_MISSING) { | 74 if (error != StartNotifySetupError::CONFIG_DESCRIPTOR_MISSING) { |
73 SimulateGattDescriptor( | 75 SimulateGattDescriptor( |
74 characteristic1_, | 76 characteristic1_, |
75 /* Client Characteristic Configuration descriptor's standard UUID: */ | 77 BluetoothGattDescriptor::ClientCharacteristicConfigurationUuid() |
76 "00002902-0000-1000-8000-00805F9B34FB"); | 78 .canonical_value()); |
77 ASSERT_EQ(1u, characteristic1_->GetDescriptors().size()); | 79 expected_descriptors_count++; |
78 } | 80 } |
| 81 if (error == StartNotifySetupError::CONFIG_DESCRIPTOR_DUPLICATE) { |
| 82 SimulateGattDescriptor( |
| 83 characteristic1_, |
| 84 BluetoothGattDescriptor::ClientCharacteristicConfigurationUuid() |
| 85 .canonical_value()); |
| 86 expected_descriptors_count++; |
| 87 } |
| 88 ASSERT_EQ(expected_descriptors_count, |
| 89 characteristic1_->GetDescriptors().size()); |
79 | 90 |
80 if (error == StartNotifySetupError::SET_NOTIFY) { | 91 if (error == StartNotifySetupError::SET_NOTIFY) { |
81 SimulateGattCharacteristicSetNotifyWillFailSynchronouslyOnce( | 92 SimulateGattCharacteristicSetNotifyWillFailSynchronouslyOnce( |
82 characteristic1_); | 93 characteristic1_); |
83 } | 94 } |
84 | 95 |
85 if (error == StartNotifySetupError::WRITE_DESCRIPTOR) { | 96 if (error == StartNotifySetupError::WRITE_DESCRIPTOR) { |
86 SimulateGattDescriptorWriteWillFailSynchronouslyOnce( | 97 SimulateGattDescriptorWriteWillFailSynchronouslyOnce( |
87 characteristic1_->GetDescriptors()[0]); | 98 characteristic1_->GetDescriptors()[0]); |
88 } | 99 } |
(...skipping 680 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
769 /* properties: NOTIFY */ 0x10, | 780 /* properties: NOTIFY */ 0x10, |
770 /* expected_config_descriptor_value: NOTIFY */ 1, | 781 /* expected_config_descriptor_value: NOTIFY */ 1, |
771 StartNotifySetupError::CONFIG_DESCRIPTOR_MISSING)); | 782 StartNotifySetupError::CONFIG_DESCRIPTOR_MISSING)); |
772 | 783 |
773 EXPECT_EQ(0, gatt_notify_characteristic_attempts_); | 784 EXPECT_EQ(0, gatt_notify_characteristic_attempts_); |
774 | 785 |
775 // The expected error callback is asynchronous: | 786 // The expected error callback is asynchronous: |
776 EXPECT_EQ(0, error_callback_count_); | 787 EXPECT_EQ(0, error_callback_count_); |
777 base::RunLoop().RunUntilIdle(); | 788 base::RunLoop().RunUntilIdle(); |
778 EXPECT_EQ(1, error_callback_count_); | 789 EXPECT_EQ(1, error_callback_count_); |
| 790 EXPECT_EQ(BluetoothGattService::GATT_ERROR_NOT_SUPPORTED, |
| 791 last_gatt_error_code_); |
779 } | 792 } |
780 #endif // defined(OS_ANDROID) | 793 #endif // defined(OS_ANDROID) |
781 | 794 |
| 795 #if defined(OS_ANDROID) |
| 796 // StartNotifySession fails if the characteristic has multiple Client |
| 797 // Characteristic Configuration descriptors. |
| 798 TEST_F(BluetoothGattCharacteristicTest, |
| 799 StartNotifySession_MultipleConfigDescriptor) { |
| 800 ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate( |
| 801 /* properties: NOTIFY */ 0x10, |
| 802 /* expected_config_descriptor_value: NOTIFY */ 1, |
| 803 StartNotifySetupError::CONFIG_DESCRIPTOR_DUPLICATE)); |
| 804 |
| 805 EXPECT_EQ(0, gatt_notify_characteristic_attempts_); |
| 806 |
| 807 // The expected error callback is asynchronous: |
| 808 EXPECT_EQ(0, error_callback_count_); |
| 809 base::RunLoop().RunUntilIdle(); |
| 810 EXPECT_EQ(1, error_callback_count_); |
| 811 EXPECT_EQ(BluetoothGattService::GATT_ERROR_FAILED, last_gatt_error_code_); |
| 812 } |
| 813 #endif // defined(OS_ANDROID) |
| 814 |
782 #if defined(OS_ANDROID) | 815 #if defined(OS_ANDROID) |
783 // StartNotifySession fails synchronously when failing to set a characteristic | 816 // StartNotifySession fails synchronously when failing to set a characteristic |
784 // to enable notifications. | 817 // to enable notifications. |
785 // Android: This is mBluetoothGatt.setCharacteristicNotification failing. | 818 // Android: This is mBluetoothGatt.setCharacteristicNotification failing. |
786 TEST_F(BluetoothGattCharacteristicTest, | 819 TEST_F(BluetoothGattCharacteristicTest, |
787 StartNotifySession_FailToSetCharacteristicNotification) { | 820 StartNotifySession_FailToSetCharacteristicNotification) { |
788 ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate( | 821 ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate( |
789 /* properties: NOTIFY */ 0x10, | 822 /* properties: NOTIFY */ 0x10, |
790 /* expected_config_descriptor_value: NOTIFY */ 1, | 823 /* expected_config_descriptor_value: NOTIFY */ 1, |
791 StartNotifySetupError::SET_NOTIFY)); | 824 StartNotifySetupError::SET_NOTIFY)); |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
848 } | 881 } |
849 #endif // defined(OS_ANDROID) | 882 #endif // defined(OS_ANDROID) |
850 | 883 |
851 #if defined(OS_ANDROID) | 884 #if defined(OS_ANDROID) |
852 // Tests multiple StartNotifySession success. | 885 // Tests multiple StartNotifySession success. |
853 TEST_F(BluetoothGattCharacteristicTest, StartNotifySession_Multiple) { | 886 TEST_F(BluetoothGattCharacteristicTest, StartNotifySession_Multiple) { |
854 ASSERT_NO_FATAL_FAILURE( | 887 ASSERT_NO_FATAL_FAILURE( |
855 FakeCharacteristicBoilerplate(/* properties: NOTIFY */ 0x10)); | 888 FakeCharacteristicBoilerplate(/* properties: NOTIFY */ 0x10)); |
856 SimulateGattDescriptor( | 889 SimulateGattDescriptor( |
857 characteristic1_, | 890 characteristic1_, |
858 /* Client Characteristic Configuration descriptor's standard UUID: */ | 891 BluetoothGattDescriptor::ClientCharacteristicConfigurationUuid() |
859 "00002902-0000-1000-8000-00805F9B34FB"); | 892 .canonical_value()); |
860 ASSERT_EQ(1u, characteristic1_->GetDescriptors().size()); | 893 ASSERT_EQ(1u, characteristic1_->GetDescriptors().size()); |
861 | 894 |
862 characteristic1_->StartNotifySession( | 895 characteristic1_->StartNotifySession( |
863 GetNotifyCallback(Call::EXPECTED), | 896 GetNotifyCallback(Call::EXPECTED), |
864 GetGattErrorCallback(Call::NOT_EXPECTED)); | 897 GetGattErrorCallback(Call::NOT_EXPECTED)); |
865 characteristic1_->StartNotifySession( | 898 characteristic1_->StartNotifySession( |
866 GetNotifyCallback(Call::EXPECTED), | 899 GetNotifyCallback(Call::EXPECTED), |
867 GetGattErrorCallback(Call::NOT_EXPECTED)); | 900 GetGattErrorCallback(Call::NOT_EXPECTED)); |
868 EXPECT_EQ(1, gatt_notify_characteristic_attempts_); | 901 EXPECT_EQ(1, gatt_notify_characteristic_attempts_); |
869 EXPECT_EQ(0, callback_count_); | 902 EXPECT_EQ(0, callback_count_); |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
970 characteristic1_->GetDescriptor(c1_id1)); | 1003 characteristic1_->GetDescriptor(c1_id1)); |
971 | 1004 |
972 // Characteristic 1 has descriptor uuids 1 and 2 (we don't know the order). | 1005 // Characteristic 1 has descriptor uuids 1 and 2 (we don't know the order). |
973 EXPECT_TRUE(c1_uuid1 == uuid1 || c1_uuid2 == uuid1); | 1006 EXPECT_TRUE(c1_uuid1 == uuid1 || c1_uuid2 == uuid1); |
974 EXPECT_TRUE(c1_uuid1 == uuid2 || c1_uuid2 == uuid2); | 1007 EXPECT_TRUE(c1_uuid1 == uuid2 || c1_uuid2 == uuid2); |
975 // ... but not uuid 3 | 1008 // ... but not uuid 3 |
976 EXPECT_FALSE(c1_uuid1 == uuid3 || c1_uuid2 == uuid3); | 1009 EXPECT_FALSE(c1_uuid1 == uuid3 || c1_uuid2 == uuid3); |
977 } | 1010 } |
978 #endif // defined(OS_ANDROID) || defined(OS_WIN) | 1011 #endif // defined(OS_ANDROID) || defined(OS_WIN) |
979 | 1012 |
| 1013 #if defined(OS_ANDROID) || defined(OS_WIN) |
| 1014 TEST_F(BluetoothGattCharacteristicTest, GetDescriptorsByUUID) { |
| 1015 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); |
| 1016 |
| 1017 // Add several Descriptors: |
| 1018 BluetoothUUID id1("11111111-0000-1000-8000-00805f9b34fb"); |
| 1019 BluetoothUUID id2("22222222-0000-1000-8000-00805f9b34fb"); |
| 1020 BluetoothUUID id3("33333333-0000-1000-8000-00805f9b34fb"); |
| 1021 SimulateGattDescriptor(characteristic1_, id1.canonical_value()); |
| 1022 SimulateGattDescriptor(characteristic1_, id2.canonical_value()); |
| 1023 SimulateGattDescriptor(characteristic2_, id3.canonical_value()); |
| 1024 SimulateGattDescriptor(characteristic2_, id3.canonical_value()); |
| 1025 |
| 1026 EXPECT_NE(characteristic2_->GetDescriptorsByUUID(id3).at(0)->GetIdentifier(), |
| 1027 characteristic2_->GetDescriptorsByUUID(id3).at(1)->GetIdentifier()); |
| 1028 |
| 1029 EXPECT_EQ(id1, characteristic1_->GetDescriptorsByUUID(id1).at(0)->GetUUID()); |
| 1030 EXPECT_EQ(id2, characteristic1_->GetDescriptorsByUUID(id2).at(0)->GetUUID()); |
| 1031 EXPECT_EQ(id3, characteristic2_->GetDescriptorsByUUID(id3).at(0)->GetUUID()); |
| 1032 EXPECT_EQ(id3, characteristic2_->GetDescriptorsByUUID(id3).at(1)->GetUUID()); |
| 1033 EXPECT_EQ(1u, characteristic1_->GetDescriptorsByUUID(id1).size()); |
| 1034 EXPECT_EQ(1u, characteristic1_->GetDescriptorsByUUID(id2).size()); |
| 1035 EXPECT_EQ(2u, characteristic2_->GetDescriptorsByUUID(id3).size()); |
| 1036 |
| 1037 EXPECT_EQ(0u, characteristic2_->GetDescriptorsByUUID(id1).size()); |
| 1038 EXPECT_EQ(0u, characteristic2_->GetDescriptorsByUUID(id2).size()); |
| 1039 EXPECT_EQ(0u, characteristic1_->GetDescriptorsByUUID(id3).size()); |
| 1040 } |
| 1041 #endif // defined(OS_ANDROID) || defined(OS_WIN) |
| 1042 |
980 } // namespace device | 1043 } // namespace device |
OLD | NEW |