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" |
11 #include "build/build_config.h" | 11 #include "build/build_config.h" |
12 #include "device/bluetooth/bluetooth_gatt_service.h" | 12 #include "device/bluetooth/bluetooth_gatt_service.h" |
13 #include "device/bluetooth/test/test_bluetooth_adapter_observer.h" | 13 #include "device/bluetooth/test/test_bluetooth_adapter_observer.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
15 | 15 |
16 #if defined(OS_ANDROID) | 16 #if defined(OS_ANDROID) |
17 #include "device/bluetooth/test/bluetooth_test_android.h" | 17 #include "device/bluetooth/test/bluetooth_test_android.h" |
18 #elif defined(OS_MACOSX) | 18 #elif defined(OS_MACOSX) |
19 #include "device/bluetooth/test/bluetooth_test_mac.h" | 19 #include "device/bluetooth/test/bluetooth_test_mac.h" |
| 20 #elif defined(OS_WIN) |
| 21 #include "device/bluetooth/test/bluetooth_test_win.h" |
20 #endif | 22 #endif |
21 | 23 |
22 namespace device { | 24 namespace device { |
23 | 25 |
24 #if defined(OS_ANDROID) || defined(OS_MACOSX) | 26 #if defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) |
25 class BluetoothGattCharacteristicTest : public BluetoothTest { | 27 class BluetoothGattCharacteristicTest : public BluetoothTest { |
26 public: | 28 public: |
27 // Creates adapter_, device_, service_, characteristic1_, & characteristic2_. | 29 // Creates adapter_, device_, service_, characteristic1_, & characteristic2_. |
28 // |properties| will be used for each characteristic. | 30 // |properties| will be used for each characteristic. |
29 void FakeCharacteristicBoilerplate(int properties = 0) { | 31 void FakeCharacteristicBoilerplate(int properties = 0) { |
30 InitWithFakeAdapter(); | 32 InitWithFakeAdapter(); |
31 StartLowEnergyDiscoverySession(); | 33 StartLowEnergyDiscoverySession(); |
32 device_ = DiscoverLowEnergyDevice(3); | 34 device_ = DiscoverLowEnergyDevice(3); |
33 device_->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 35 device_->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
34 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 36 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 EXPECT_EQ(expected_byte1, last_write_value_[1]); | 118 EXPECT_EQ(expected_byte1, last_write_value_[1]); |
117 } | 119 } |
118 | 120 |
119 BluetoothDevice* device_ = nullptr; | 121 BluetoothDevice* device_ = nullptr; |
120 BluetoothGattService* service_ = nullptr; | 122 BluetoothGattService* service_ = nullptr; |
121 BluetoothGattCharacteristic* characteristic1_ = nullptr; | 123 BluetoothGattCharacteristic* characteristic1_ = nullptr; |
122 BluetoothGattCharacteristic* characteristic2_ = nullptr; | 124 BluetoothGattCharacteristic* characteristic2_ = nullptr; |
123 }; | 125 }; |
124 #endif | 126 #endif |
125 | 127 |
126 #if defined(OS_ANDROID) | 128 #if defined(OS_ANDROID) || defined(OS_WIN) |
127 TEST_F(BluetoothGattCharacteristicTest, GetIdentifier) { | 129 TEST_F(BluetoothGattCharacteristicTest, GetIdentifier) { |
128 InitWithFakeAdapter(); | 130 InitWithFakeAdapter(); |
129 StartLowEnergyDiscoverySession(); | 131 StartLowEnergyDiscoverySession(); |
130 // 2 devices to verify unique IDs across them. | 132 // 2 devices to verify unique IDs across them. |
131 BluetoothDevice* device1 = DiscoverLowEnergyDevice(3); | 133 BluetoothDevice* device1 = DiscoverLowEnergyDevice(3); |
132 BluetoothDevice* device2 = DiscoverLowEnergyDevice(4); | 134 BluetoothDevice* device2 = DiscoverLowEnergyDevice(4); |
133 device1->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 135 device1->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
134 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 136 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
135 device2->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 137 device2->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
136 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 138 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
177 | 179 |
178 EXPECT_NE(char3->GetIdentifier(), char4->GetIdentifier()); | 180 EXPECT_NE(char3->GetIdentifier(), char4->GetIdentifier()); |
179 EXPECT_NE(char3->GetIdentifier(), char5->GetIdentifier()); | 181 EXPECT_NE(char3->GetIdentifier(), char5->GetIdentifier()); |
180 EXPECT_NE(char3->GetIdentifier(), char6->GetIdentifier()); | 182 EXPECT_NE(char3->GetIdentifier(), char6->GetIdentifier()); |
181 | 183 |
182 EXPECT_NE(char4->GetIdentifier(), char5->GetIdentifier()); | 184 EXPECT_NE(char4->GetIdentifier(), char5->GetIdentifier()); |
183 EXPECT_NE(char4->GetIdentifier(), char6->GetIdentifier()); | 185 EXPECT_NE(char4->GetIdentifier(), char6->GetIdentifier()); |
184 | 186 |
185 EXPECT_NE(char5->GetIdentifier(), char6->GetIdentifier()); | 187 EXPECT_NE(char5->GetIdentifier(), char6->GetIdentifier()); |
186 } | 188 } |
187 #endif // defined(OS_ANDROID) | 189 #endif // defined(OS_ANDROID) || defined(OS_WIN) |
188 | 190 |
189 #if defined(OS_ANDROID) | 191 #if defined(OS_ANDROID) || defined(OS_WIN) |
190 TEST_F(BluetoothGattCharacteristicTest, GetUUID) { | 192 TEST_F(BluetoothGattCharacteristicTest, GetUUID) { |
191 InitWithFakeAdapter(); | 193 InitWithFakeAdapter(); |
192 StartLowEnergyDiscoverySession(); | 194 StartLowEnergyDiscoverySession(); |
193 BluetoothDevice* device = DiscoverLowEnergyDevice(3); | 195 BluetoothDevice* device = DiscoverLowEnergyDevice(3); |
194 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 196 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
195 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 197 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
196 SimulateGattConnection(device); | 198 SimulateGattConnection(device); |
197 std::vector<std::string> services; | 199 std::vector<std::string> services; |
198 services.push_back("00000000-0000-1000-8000-00805f9b34fb"); | 200 services.push_back("00000000-0000-1000-8000-00805f9b34fb"); |
199 SimulateGattServicesDiscovered(device, services); | 201 SimulateGattServicesDiscovered(device, services); |
(...skipping 15 matching lines...) Expand all Loading... |
215 if (char2->GetUUID() == uuid1) { | 217 if (char2->GetUUID() == uuid1) { |
216 std::swap(char1, char2); | 218 std::swap(char1, char2); |
217 } else if (char3->GetUUID() == uuid1) { | 219 } else if (char3->GetUUID() == uuid1) { |
218 std::swap(char1, char3); | 220 std::swap(char1, char3); |
219 } | 221 } |
220 | 222 |
221 EXPECT_EQ(uuid1, char1->GetUUID()); | 223 EXPECT_EQ(uuid1, char1->GetUUID()); |
222 EXPECT_EQ(uuid2, char2->GetUUID()); | 224 EXPECT_EQ(uuid2, char2->GetUUID()); |
223 EXPECT_EQ(uuid2, char3->GetUUID()); | 225 EXPECT_EQ(uuid2, char3->GetUUID()); |
224 } | 226 } |
225 #endif // defined(OS_ANDROID) | 227 #endif // defined(OS_ANDROID) || defined(OS_WIN) |
226 | 228 |
227 #if defined(OS_ANDROID) | 229 #if defined(OS_ANDROID) || defined(OS_WIN) |
228 TEST_F(BluetoothGattCharacteristicTest, GetProperties) { | 230 TEST_F(BluetoothGattCharacteristicTest, GetProperties) { |
229 InitWithFakeAdapter(); | 231 InitWithFakeAdapter(); |
230 StartLowEnergyDiscoverySession(); | 232 StartLowEnergyDiscoverySession(); |
231 BluetoothDevice* device = DiscoverLowEnergyDevice(3); | 233 BluetoothDevice* device = DiscoverLowEnergyDevice(3); |
232 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 234 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
233 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 235 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
234 SimulateGattConnection(device); | 236 SimulateGattConnection(device); |
235 std::vector<std::string> services; | 237 std::vector<std::string> services; |
236 std::string uuid("00000000-0000-1000-8000-00805f9b34fb"); | 238 std::string uuid("00000000-0000-1000-8000-00805f9b34fb"); |
237 services.push_back(uuid); | 239 services.push_back(uuid); |
238 SimulateGattServicesDiscovered(device, services); | 240 SimulateGattServicesDiscovered(device, services); |
239 BluetoothGattService* service = device->GetGattServices()[0]; | 241 BluetoothGattService* service = device->GetGattServices()[0]; |
240 | 242 |
241 // Create two characteristics with different properties: | 243 // Create two characteristics with different properties: |
242 SimulateGattCharacteristic(service, uuid, /* properties */ 0); | 244 SimulateGattCharacteristic(service, uuid, /* properties */ 0); |
243 SimulateGattCharacteristic(service, uuid, /* properties */ 7); | 245 SimulateGattCharacteristic(service, uuid, /* properties */ 7); |
244 | 246 |
245 // Read the properties. Because ordering is unknown swap as necessary. | 247 // Read the properties. Because ordering is unknown swap as necessary. |
246 int properties1 = service->GetCharacteristics()[0]->GetProperties(); | 248 int properties1 = service->GetCharacteristics()[0]->GetProperties(); |
247 int properties2 = service->GetCharacteristics()[1]->GetProperties(); | 249 int properties2 = service->GetCharacteristics()[1]->GetProperties(); |
248 if (properties2 == 0) | 250 if (properties2 == 0) |
249 std::swap(properties1, properties2); | 251 std::swap(properties1, properties2); |
250 EXPECT_EQ(0, properties1); | 252 EXPECT_EQ(0, properties1); |
251 EXPECT_EQ(7, properties2); | 253 EXPECT_EQ(7, properties2); |
252 } | 254 } |
253 #endif // defined(OS_ANDROID) | 255 #endif // defined(OS_ANDROID) || defined(OS_WIN) |
254 | 256 |
255 #if defined(OS_ANDROID) | 257 #if defined(OS_ANDROID) || defined(OS_WIN) |
256 // Tests GetService. | 258 // Tests GetService. |
257 TEST_F(BluetoothGattCharacteristicTest, GetService) { | 259 TEST_F(BluetoothGattCharacteristicTest, GetService) { |
258 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); | 260 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); |
259 | 261 |
260 EXPECT_EQ(service_, characteristic1_->GetService()); | 262 EXPECT_EQ(service_, characteristic1_->GetService()); |
261 EXPECT_EQ(service_, characteristic2_->GetService()); | 263 EXPECT_EQ(service_, characteristic2_->GetService()); |
262 } | 264 } |
263 #endif // defined(OS_ANDROID) | 265 #endif // defined(OS_ANDROID) || defined(OS_WIN) |
264 | 266 |
265 #if defined(OS_ANDROID) | 267 #if defined(OS_ANDROID) |
266 // Tests ReadRemoteCharacteristic and GetValue with empty value buffer. | 268 // Tests ReadRemoteCharacteristic and GetValue with empty value buffer. |
267 TEST_F(BluetoothGattCharacteristicTest, ReadRemoteCharacteristic_Empty) { | 269 TEST_F(BluetoothGattCharacteristicTest, ReadRemoteCharacteristic_Empty) { |
268 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); | 270 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); |
269 | 271 |
270 characteristic1_->ReadRemoteCharacteristic( | 272 characteristic1_->ReadRemoteCharacteristic( |
271 GetReadValueCallback(Call::EXPECTED), | 273 GetReadValueCallback(Call::EXPECTED), |
272 GetGattErrorCallback(Call::NOT_EXPECTED)); | 274 GetGattErrorCallback(Call::NOT_EXPECTED)); |
273 EXPECT_EQ(1, gatt_read_characteristic_attempts_); | 275 EXPECT_EQ(1, gatt_read_characteristic_attempts_); |
(...skipping 617 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
891 ASSERT_TRUE(notify_sessions_[1]); | 893 ASSERT_TRUE(notify_sessions_[1]); |
892 EXPECT_EQ(characteristic1_->GetIdentifier(), | 894 EXPECT_EQ(characteristic1_->GetIdentifier(), |
893 notify_sessions_[0]->GetCharacteristicIdentifier()); | 895 notify_sessions_[0]->GetCharacteristicIdentifier()); |
894 EXPECT_EQ(characteristic1_->GetIdentifier(), | 896 EXPECT_EQ(characteristic1_->GetIdentifier(), |
895 notify_sessions_[1]->GetCharacteristicIdentifier()); | 897 notify_sessions_[1]->GetCharacteristicIdentifier()); |
896 EXPECT_TRUE(notify_sessions_[0]->IsActive()); | 898 EXPECT_TRUE(notify_sessions_[0]->IsActive()); |
897 EXPECT_TRUE(notify_sessions_[1]->IsActive()); | 899 EXPECT_TRUE(notify_sessions_[1]->IsActive()); |
898 } | 900 } |
899 #endif // defined(OS_ANDROID) | 901 #endif // defined(OS_ANDROID) |
900 | 902 |
901 #if defined(OS_ANDROID) | 903 #if defined(OS_ANDROID) || defined(OS_WIN) |
902 TEST_F(BluetoothGattCharacteristicTest, GetDescriptors_FindNone) { | 904 TEST_F(BluetoothGattCharacteristicTest, GetDescriptors_FindNone) { |
903 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); | 905 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); |
904 | 906 |
905 EXPECT_EQ(0u, characteristic1_->GetDescriptors().size()); | 907 EXPECT_EQ(0u, characteristic1_->GetDescriptors().size()); |
906 } | 908 } |
907 #endif // defined(OS_ANDROID) | 909 #endif // defined(OS_ANDROID) || defined(OS_WIN) |
908 | 910 |
909 #if defined(OS_ANDROID) | 911 #if defined(OS_ANDROID) || defined(OS_WIN) |
910 TEST_F(BluetoothGattCharacteristicTest, GetDescriptors_and_GetDescriptor) { | 912 TEST_F(BluetoothGattCharacteristicTest, GetDescriptors_and_GetDescriptor) { |
911 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); | 913 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); |
912 | 914 |
913 // Add several Descriptors: | 915 // Add several Descriptors: |
914 BluetoothUUID uuid1("11111111-0000-1000-8000-00805f9b34fb"); | 916 BluetoothUUID uuid1("11111111-0000-1000-8000-00805f9b34fb"); |
915 BluetoothUUID uuid2("22222222-0000-1000-8000-00805f9b34fb"); | 917 BluetoothUUID uuid2("22222222-0000-1000-8000-00805f9b34fb"); |
916 BluetoothUUID uuid3("33333333-0000-1000-8000-00805f9b34fb"); | 918 BluetoothUUID uuid3("33333333-0000-1000-8000-00805f9b34fb"); |
917 BluetoothUUID uuid4("44444444-0000-1000-8000-00805f9b34fb"); | 919 BluetoothUUID uuid4("44444444-0000-1000-8000-00805f9b34fb"); |
918 SimulateGattDescriptor(characteristic1_, uuid1.canonical_value()); | 920 SimulateGattDescriptor(characteristic1_, uuid1.canonical_value()); |
919 SimulateGattDescriptor(characteristic1_, uuid2.canonical_value()); | 921 SimulateGattDescriptor(characteristic1_, uuid2.canonical_value()); |
(...skipping 22 matching lines...) Expand all Loading... |
942 characteristic1_->GetDescriptor(c1_id1)); | 944 characteristic1_->GetDescriptor(c1_id1)); |
943 EXPECT_EQ(characteristic1_->GetDescriptor(c1_id1), | 945 EXPECT_EQ(characteristic1_->GetDescriptor(c1_id1), |
944 characteristic1_->GetDescriptor(c1_id1)); | 946 characteristic1_->GetDescriptor(c1_id1)); |
945 | 947 |
946 // Characteristic 1 has descriptor uuids 1 and 2 (we don't know the order). | 948 // Characteristic 1 has descriptor uuids 1 and 2 (we don't know the order). |
947 EXPECT_TRUE(c1_uuid1 == uuid1 || c1_uuid2 == uuid1); | 949 EXPECT_TRUE(c1_uuid1 == uuid1 || c1_uuid2 == uuid1); |
948 EXPECT_TRUE(c1_uuid1 == uuid2 || c1_uuid2 == uuid2); | 950 EXPECT_TRUE(c1_uuid1 == uuid2 || c1_uuid2 == uuid2); |
949 // ... but not uuid 3 | 951 // ... but not uuid 3 |
950 EXPECT_FALSE(c1_uuid1 == uuid3 || c1_uuid2 == uuid3); | 952 EXPECT_FALSE(c1_uuid1 == uuid3 || c1_uuid2 == uuid3); |
951 } | 953 } |
952 #endif // defined(OS_ANDROID) | 954 #endif // defined(OS_ANDROID) || defined(OS_WIN) |
953 | 955 |
954 } // namespace device | 956 } // namespace device |
OLD | NEW |