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_service.h" | 5 #include "device/bluetooth/bluetooth_gatt_service.h" |
6 | 6 |
7 #include "build/build_config.h" | 7 #include "build/build_config.h" |
8 #include "device/bluetooth/bluetooth_gatt_characteristic.h" | 8 #include "device/bluetooth/bluetooth_gatt_characteristic.h" |
| 9 #include "device/bluetooth/test/test_bluetooth_adapter_observer.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
10 | 11 |
11 #if defined(OS_ANDROID) | 12 #if defined(OS_ANDROID) |
12 #include "device/bluetooth/test/bluetooth_test_android.h" | 13 #include "device/bluetooth/test/bluetooth_test_android.h" |
13 #elif defined(OS_MACOSX) | 14 #elif defined(OS_MACOSX) |
14 #include "device/bluetooth/test/bluetooth_test_mac.h" | 15 #include "device/bluetooth/test/bluetooth_test_mac.h" |
| 16 #elif defined(OS_WIN) |
| 17 #include "device/bluetooth/test/bluetooth_test_win.h" |
15 #endif | 18 #endif |
16 | 19 |
17 namespace device { | 20 namespace device { |
18 | 21 |
19 #if defined(OS_ANDROID) || defined(OS_MACOSX) | 22 #if defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) |
20 class BluetoothGattServiceTest : public BluetoothTest {}; | 23 class BluetoothGattServiceTest : public BluetoothTest {}; |
21 #endif | 24 #endif |
22 | 25 |
23 #if defined(OS_ANDROID) | 26 #if defined(OS_ANDROID) || defined(OS_WIN) |
24 TEST_F(BluetoothGattServiceTest, GetIdentifier) { | 27 TEST_F(BluetoothGattServiceTest, GetIdentifier) { |
25 InitWithFakeAdapter(); | 28 InitWithFakeAdapter(); |
26 StartLowEnergyDiscoverySession(); | 29 StartLowEnergyDiscoverySession(); |
27 // 2 devices to verify unique IDs across them. | 30 // 2 devices to verify unique IDs across them. |
28 BluetoothDevice* device1 = DiscoverLowEnergyDevice(3); | 31 BluetoothDevice* device1 = DiscoverLowEnergyDevice(3); |
29 BluetoothDevice* device2 = DiscoverLowEnergyDevice(4); | 32 BluetoothDevice* device2 = DiscoverLowEnergyDevice(4); |
30 device1->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 33 device1->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
31 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 34 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
32 device2->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 35 device2->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
33 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 36 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
(...skipping 15 matching lines...) Expand all Loading... |
49 // All IDs are unique, even though they have the same UUID. | 52 // All IDs are unique, even though they have the same UUID. |
50 EXPECT_NE(service1->GetIdentifier(), service2->GetIdentifier()); | 53 EXPECT_NE(service1->GetIdentifier(), service2->GetIdentifier()); |
51 EXPECT_NE(service1->GetIdentifier(), service3->GetIdentifier()); | 54 EXPECT_NE(service1->GetIdentifier(), service3->GetIdentifier()); |
52 EXPECT_NE(service1->GetIdentifier(), service4->GetIdentifier()); | 55 EXPECT_NE(service1->GetIdentifier(), service4->GetIdentifier()); |
53 | 56 |
54 EXPECT_NE(service2->GetIdentifier(), service3->GetIdentifier()); | 57 EXPECT_NE(service2->GetIdentifier(), service3->GetIdentifier()); |
55 EXPECT_NE(service2->GetIdentifier(), service4->GetIdentifier()); | 58 EXPECT_NE(service2->GetIdentifier(), service4->GetIdentifier()); |
56 | 59 |
57 EXPECT_NE(service3->GetIdentifier(), service4->GetIdentifier()); | 60 EXPECT_NE(service3->GetIdentifier(), service4->GetIdentifier()); |
58 } | 61 } |
59 #endif // defined(OS_ANDROID) | 62 #endif // defined(OS_ANDROID) || defined(OS_WIN) |
60 | 63 |
61 #if defined(OS_ANDROID) | 64 #if defined(OS_ANDROID) || defined(OS_WIN) |
62 TEST_F(BluetoothGattServiceTest, GetUUID) { | 65 TEST_F(BluetoothGattServiceTest, GetUUID) { |
63 InitWithFakeAdapter(); | 66 InitWithFakeAdapter(); |
64 StartLowEnergyDiscoverySession(); | 67 StartLowEnergyDiscoverySession(); |
65 BluetoothDevice* device = DiscoverLowEnergyDevice(3); | 68 BluetoothDevice* device = DiscoverLowEnergyDevice(3); |
66 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 69 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
67 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 70 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
68 SimulateGattConnection(device); | 71 SimulateGattConnection(device); |
69 | 72 |
70 // Create multiple instances with the same UUID. | 73 // Create multiple instances with the same UUID. |
71 BluetoothUUID uuid("00000000-0000-1000-8000-00805f9b34fb"); | 74 BluetoothUUID uuid("00000000-0000-1000-8000-00805f9b34fb"); |
72 std::vector<std::string> services; | 75 std::vector<std::string> services; |
73 services.push_back(uuid.canonical_value()); | 76 services.push_back(uuid.canonical_value()); |
74 services.push_back(uuid.canonical_value()); | 77 services.push_back(uuid.canonical_value()); |
75 SimulateGattServicesDiscovered(device, services); | 78 SimulateGattServicesDiscovered(device, services); |
76 | 79 |
77 // Each has the same UUID. | 80 // Each has the same UUID. |
78 EXPECT_EQ(uuid, device->GetGattServices()[0]->GetUUID()); | 81 EXPECT_EQ(uuid, device->GetGattServices()[0]->GetUUID()); |
79 EXPECT_EQ(uuid, device->GetGattServices()[1]->GetUUID()); | 82 EXPECT_EQ(uuid, device->GetGattServices()[1]->GetUUID()); |
80 } | 83 } |
81 #endif // defined(OS_ANDROID) | 84 #endif // defined(OS_ANDROID) || defined(OS_WIN) |
82 | 85 |
83 #if defined(OS_ANDROID) | 86 #if defined(OS_ANDROID) || defined(OS_WIN) |
84 TEST_F(BluetoothGattServiceTest, GetCharacteristics_FindNone) { | 87 TEST_F(BluetoothGattServiceTest, GetCharacteristics_FindNone) { |
85 InitWithFakeAdapter(); | 88 InitWithFakeAdapter(); |
86 StartLowEnergyDiscoverySession(); | 89 StartLowEnergyDiscoverySession(); |
87 BluetoothDevice* device = DiscoverLowEnergyDevice(3); | 90 BluetoothDevice* device = DiscoverLowEnergyDevice(3); |
88 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 91 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
89 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 92 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
90 SimulateGattConnection(device); | 93 SimulateGattConnection(device); |
91 | 94 |
92 // Simulate a service, with no Characteristics: | 95 // Simulate a service, with no Characteristics: |
93 std::vector<std::string> services; | 96 std::vector<std::string> services; |
94 services.push_back("00000000-0000-1000-8000-00805f9b34fb"); | 97 services.push_back("00000000-0000-1000-8000-00805f9b34fb"); |
95 SimulateGattServicesDiscovered(device, services); | 98 SimulateGattServicesDiscovered(device, services); |
96 BluetoothGattService* service = device->GetGattServices()[0]; | 99 BluetoothGattService* service = device->GetGattServices()[0]; |
97 | 100 |
98 EXPECT_EQ(0u, service->GetCharacteristics().size()); | 101 EXPECT_EQ(0u, service->GetCharacteristics().size()); |
99 } | 102 } |
100 #endif // defined(OS_ANDROID) | 103 #endif // defined(OS_ANDROID) || defined(OS_WIN) |
101 | 104 |
102 #if defined(OS_ANDROID) | 105 #if defined(OS_ANDROID) || defined(OS_WIN) |
103 TEST_F(BluetoothGattServiceTest, GetCharacteristics_and_GetCharacteristic) { | 106 TEST_F(BluetoothGattServiceTest, GetCharacteristics_and_GetCharacteristic) { |
104 InitWithFakeAdapter(); | 107 InitWithFakeAdapter(); |
105 StartLowEnergyDiscoverySession(); | 108 StartLowEnergyDiscoverySession(); |
106 BluetoothDevice* device = DiscoverLowEnergyDevice(3); | 109 BluetoothDevice* device = DiscoverLowEnergyDevice(3); |
107 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 110 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
108 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 111 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
109 SimulateGattConnection(device); | 112 SimulateGattConnection(device); |
110 | 113 |
111 // Simulate a service, with several Characteristics: | 114 // Simulate a service, with several Characteristics: |
112 std::vector<std::string> services; | 115 std::vector<std::string> services; |
(...skipping 25 matching lines...) Expand all Loading... |
138 EXPECT_EQ(char_uuid3, service->GetCharacteristic(char_id3)->GetUUID()); | 141 EXPECT_EQ(char_uuid3, service->GetCharacteristic(char_id3)->GetUUID()); |
139 EXPECT_EQ(char_uuid4, service->GetCharacteristic(char_id4)->GetUUID()); | 142 EXPECT_EQ(char_uuid4, service->GetCharacteristic(char_id4)->GetUUID()); |
140 | 143 |
141 // GetCharacteristics & GetCharacteristic return the same object for the same | 144 // GetCharacteristics & GetCharacteristic return the same object for the same |
142 // ID: | 145 // ID: |
143 EXPECT_EQ(service->GetCharacteristics()[0], | 146 EXPECT_EQ(service->GetCharacteristics()[0], |
144 service->GetCharacteristic(char_id1)); | 147 service->GetCharacteristic(char_id1)); |
145 EXPECT_EQ(service->GetCharacteristic(char_id1), | 148 EXPECT_EQ(service->GetCharacteristic(char_id1), |
146 service->GetCharacteristic(char_id1)); | 149 service->GetCharacteristic(char_id1)); |
147 } | 150 } |
148 #endif // defined(OS_ANDROID) | 151 #endif // defined(OS_ANDROID) || defined(OS_WIN) |
| 152 |
| 153 #if defined(OS_WIN) |
| 154 TEST_F(BluetoothGattServiceTest, GetCharacteristic_CharacteristicRemoved) { |
| 155 InitWithFakeAdapter(); |
| 156 StartLowEnergyDiscoverySession(); |
| 157 BluetoothDevice* device = DiscoverLowEnergyDevice(3); |
| 158 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
| 159 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
| 160 SimulateGattConnection(device); |
| 161 |
| 162 TestBluetoothAdapterObserver observer(adapter_); |
| 163 |
| 164 // Simulate a service, with several Characteristics: |
| 165 std::vector<std::string> services; |
| 166 services.push_back("00000000-0000-1000-8000-00805f9b34fb"); |
| 167 SimulateGattServicesDiscovered(device, services); |
| 168 BluetoothGattService* service = device->GetGattServices()[0]; |
| 169 std::string characteristic_uuid1 = "11111111-0000-1000-8000-00805f9b34fb"; |
| 170 std::string characteristic_uuid2 = "22222222-0000-1000-8000-00805f9b34fb"; |
| 171 std::string characteristic_uuid3 = characteristic_uuid2; // Duplicate UUID. |
| 172 std::string characteristic_uuid4 = "33333333-0000-1000-8000-00805f9b34fb"; |
| 173 SimulateGattCharacteristic(service, characteristic_uuid1, /* properties */ 0); |
| 174 SimulateGattCharacteristic(service, characteristic_uuid2, /* properties */ 0); |
| 175 SimulateGattCharacteristic(service, characteristic_uuid3, /* properties */ 0); |
| 176 SimulateGattCharacteristic(service, characteristic_uuid4, /* properties */ 0); |
| 177 |
| 178 // Simulate remove of characteristics one by one. |
| 179 EXPECT_EQ(4u, service->GetCharacteristics().size()); |
| 180 std::string removed_char = service->GetCharacteristics()[0]->GetIdentifier(); |
| 181 SimulateGattCharacteristicRemoved(service, |
| 182 service->GetCharacteristic(removed_char)); |
| 183 EXPECT_EQ(1, observer.gatt_characteristic_removed_count()); |
| 184 EXPECT_FALSE(service->GetCharacteristic(removed_char)); |
| 185 EXPECT_EQ(3u, service->GetCharacteristics().size()); |
| 186 removed_char = service->GetCharacteristics()[0]->GetIdentifier(); |
| 187 SimulateGattCharacteristicRemoved(service, |
| 188 service->GetCharacteristic(removed_char)); |
| 189 EXPECT_EQ(2, observer.gatt_characteristic_removed_count()); |
| 190 EXPECT_FALSE(service->GetCharacteristic(removed_char)); |
| 191 EXPECT_EQ(2u, service->GetCharacteristics().size()); |
| 192 removed_char = service->GetCharacteristics()[0]->GetIdentifier(); |
| 193 SimulateGattCharacteristicRemoved(service, |
| 194 service->GetCharacteristic(removed_char)); |
| 195 EXPECT_EQ(3, observer.gatt_characteristic_removed_count()); |
| 196 EXPECT_FALSE(service->GetCharacteristic(removed_char)); |
| 197 EXPECT_EQ(1u, service->GetCharacteristics().size()); |
| 198 removed_char = service->GetCharacteristics()[0]->GetIdentifier(); |
| 199 SimulateGattCharacteristicRemoved(service, |
| 200 service->GetCharacteristic(removed_char)); |
| 201 EXPECT_EQ(4, observer.gatt_characteristic_removed_count()); |
| 202 EXPECT_FALSE(service->GetCharacteristic(removed_char)); |
| 203 EXPECT_EQ(0u, service->GetCharacteristics().size()); |
| 204 |
| 205 EXPECT_EQ(4, observer.gatt_service_changed_count()); |
| 206 } |
| 207 #endif // defined(OS_WIN) |
| 208 |
| 209 #if defined(OS_WIN) |
| 210 TEST_F(BluetoothGattServiceTest, SimulateGattServiceRemove) { |
| 211 InitWithFakeAdapter(); |
| 212 StartLowEnergyDiscoverySession(); |
| 213 BluetoothDevice* device = DiscoverLowEnergyDevice(3); |
| 214 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
| 215 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
| 216 SimulateGattConnection(device); |
| 217 |
| 218 TestBluetoothAdapterObserver observer(adapter_); |
| 219 |
| 220 // Simulate two primary GATT services. |
| 221 std::vector<std::string> services; |
| 222 services.push_back("00000000-0000-1000-8000-00805f9b34fb"); |
| 223 services.push_back("01010101-0101-1000-8000-00805f9b34fb"); |
| 224 SimulateGattServicesDiscovered(device, services); |
| 225 EXPECT_EQ(2u, device->GetGattServices().size()); |
| 226 |
| 227 // Simulate remove of a primary service. |
| 228 BluetoothGattService* service1 = device->GetGattServices()[0]; |
| 229 BluetoothGattService* service2 = device->GetGattServices()[1]; |
| 230 std::string removed_service = service1->GetIdentifier(); |
| 231 SimulateGattServiceRemoved(device->GetGattService(removed_service)); |
| 232 EXPECT_EQ(1, observer.gatt_service_removed_count()); |
| 233 EXPECT_EQ(1u, device->GetGattServices().size()); |
| 234 EXPECT_FALSE(device->GetGattService(removed_service)); |
| 235 EXPECT_EQ(device->GetGattServices()[0], service2); |
| 236 } |
| 237 #endif // defined(OS_WIN) |
149 | 238 |
150 } // namespace device | 239 } // namespace device |
OLD | NEW |