OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "device/bluetooth/bluetooth_gatt_service.h" | |
6 | |
7 #include "build/build_config.h" | |
8 #include "device/bluetooth/bluetooth_gatt_characteristic.h" | |
9 #include "device/bluetooth/test/test_bluetooth_adapter_observer.h" | |
10 #include "testing/gtest/include/gtest/gtest.h" | |
11 | |
12 #if defined(OS_ANDROID) | |
13 #include "device/bluetooth/test/bluetooth_test_android.h" | |
14 #elif defined(OS_MACOSX) | |
15 #include "device/bluetooth/test/bluetooth_test_mac.h" | |
16 #elif defined(OS_WIN) | |
17 #include "device/bluetooth/test/bluetooth_test_win.h" | |
18 #endif | |
19 | |
20 namespace device { | |
21 | |
22 #if defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) | |
23 class BluetoothGattServiceTest : public BluetoothTest {}; | |
24 #endif | |
25 | |
26 #if defined(OS_ANDROID) || defined(OS_WIN) | |
27 TEST_F(BluetoothGattServiceTest, GetIdentifier) { | |
28 InitWithFakeAdapter(); | |
29 StartLowEnergyDiscoverySession(); | |
30 // 2 devices to verify unique IDs across them. | |
31 BluetoothDevice* device1 = DiscoverLowEnergyDevice(3); | |
32 BluetoothDevice* device2 = DiscoverLowEnergyDevice(4); | |
33 device1->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | |
34 GetConnectErrorCallback(Call::NOT_EXPECTED)); | |
35 device2->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | |
36 GetConnectErrorCallback(Call::NOT_EXPECTED)); | |
37 SimulateGattConnection(device1); | |
38 SimulateGattConnection(device2); | |
39 | |
40 // 2 duplicate UUIDs creating 2 service instances on each device. | |
41 std::vector<std::string> services; | |
42 std::string uuid = "00000000-0000-1000-8000-00805f9b34fb"; | |
43 services.push_back(uuid); | |
44 services.push_back(uuid); | |
45 SimulateGattServicesDiscovered(device1, services); | |
46 SimulateGattServicesDiscovered(device2, services); | |
47 BluetoothGattService* service1 = device1->GetGattServices()[0]; | |
48 BluetoothGattService* service2 = device1->GetGattServices()[1]; | |
49 BluetoothGattService* service3 = device2->GetGattServices()[0]; | |
50 BluetoothGattService* service4 = device2->GetGattServices()[1]; | |
51 | |
52 // All IDs are unique, even though they have the same UUID. | |
53 EXPECT_NE(service1->GetIdentifier(), service2->GetIdentifier()); | |
54 EXPECT_NE(service1->GetIdentifier(), service3->GetIdentifier()); | |
55 EXPECT_NE(service1->GetIdentifier(), service4->GetIdentifier()); | |
56 | |
57 EXPECT_NE(service2->GetIdentifier(), service3->GetIdentifier()); | |
58 EXPECT_NE(service2->GetIdentifier(), service4->GetIdentifier()); | |
59 | |
60 EXPECT_NE(service3->GetIdentifier(), service4->GetIdentifier()); | |
61 } | |
62 #endif // defined(OS_ANDROID) || defined(OS_WIN) | |
63 | |
64 #if defined(OS_ANDROID) || defined(OS_WIN) | |
65 TEST_F(BluetoothGattServiceTest, GetUUID) { | |
66 InitWithFakeAdapter(); | |
67 StartLowEnergyDiscoverySession(); | |
68 BluetoothDevice* device = DiscoverLowEnergyDevice(3); | |
69 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | |
70 GetConnectErrorCallback(Call::NOT_EXPECTED)); | |
71 SimulateGattConnection(device); | |
72 | |
73 // Create multiple instances with the same UUID. | |
74 BluetoothUUID uuid("00000000-0000-1000-8000-00805f9b34fb"); | |
75 std::vector<std::string> services; | |
76 services.push_back(uuid.canonical_value()); | |
77 services.push_back(uuid.canonical_value()); | |
78 SimulateGattServicesDiscovered(device, services); | |
79 | |
80 // Each has the same UUID. | |
81 EXPECT_EQ(uuid, device->GetGattServices()[0]->GetUUID()); | |
82 EXPECT_EQ(uuid, device->GetGattServices()[1]->GetUUID()); | |
83 } | |
84 #endif // defined(OS_ANDROID) || defined(OS_WIN) | |
85 | |
86 #if defined(OS_ANDROID) || defined(OS_WIN) | |
87 TEST_F(BluetoothGattServiceTest, GetCharacteristics_FindNone) { | |
88 InitWithFakeAdapter(); | |
89 StartLowEnergyDiscoverySession(); | |
90 BluetoothDevice* device = DiscoverLowEnergyDevice(3); | |
91 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | |
92 GetConnectErrorCallback(Call::NOT_EXPECTED)); | |
93 SimulateGattConnection(device); | |
94 | |
95 // Simulate a service, with no Characteristics: | |
96 std::vector<std::string> services; | |
97 services.push_back("00000000-0000-1000-8000-00805f9b34fb"); | |
98 SimulateGattServicesDiscovered(device, services); | |
99 BluetoothGattService* service = device->GetGattServices()[0]; | |
100 | |
101 EXPECT_EQ(0u, service->GetCharacteristics().size()); | |
102 } | |
103 #endif // defined(OS_ANDROID) || defined(OS_WIN) | |
104 | |
105 #if defined(OS_ANDROID) || defined(OS_WIN) | |
106 TEST_F(BluetoothGattServiceTest, GetCharacteristics_and_GetCharacteristic) { | |
107 InitWithFakeAdapter(); | |
108 StartLowEnergyDiscoverySession(); | |
109 BluetoothDevice* device = DiscoverLowEnergyDevice(3); | |
110 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | |
111 GetConnectErrorCallback(Call::NOT_EXPECTED)); | |
112 SimulateGattConnection(device); | |
113 | |
114 // Simulate a service, with several Characteristics: | |
115 std::vector<std::string> services; | |
116 services.push_back("00000000-0000-1000-8000-00805f9b34fb"); | |
117 SimulateGattServicesDiscovered(device, services); | |
118 BluetoothGattService* service = device->GetGattServices()[0]; | |
119 std::string characteristic_uuid1 = "11111111-0000-1000-8000-00805f9b34fb"; | |
120 std::string characteristic_uuid2 = "22222222-0000-1000-8000-00805f9b34fb"; | |
121 std::string characteristic_uuid3 = characteristic_uuid2; // Duplicate UUID. | |
122 std::string characteristic_uuid4 = "33333333-0000-1000-8000-00805f9b34fb"; | |
123 SimulateGattCharacteristic(service, characteristic_uuid1, /* properties */ 0); | |
124 SimulateGattCharacteristic(service, characteristic_uuid2, /* properties */ 0); | |
125 SimulateGattCharacteristic(service, characteristic_uuid3, /* properties */ 0); | |
126 SimulateGattCharacteristic(service, characteristic_uuid4, /* properties */ 0); | |
127 | |
128 // Verify that GetCharacteristic can retrieve characteristics again by ID, | |
129 // and that the same Characteristics come back. | |
130 EXPECT_EQ(4u, service->GetCharacteristics().size()); | |
131 std::string char_id1 = service->GetCharacteristics()[0]->GetIdentifier(); | |
132 std::string char_id2 = service->GetCharacteristics()[1]->GetIdentifier(); | |
133 std::string char_id3 = service->GetCharacteristics()[2]->GetIdentifier(); | |
134 std::string char_id4 = service->GetCharacteristics()[3]->GetIdentifier(); | |
135 BluetoothUUID char_uuid1 = service->GetCharacteristics()[0]->GetUUID(); | |
136 BluetoothUUID char_uuid2 = service->GetCharacteristics()[1]->GetUUID(); | |
137 BluetoothUUID char_uuid3 = service->GetCharacteristics()[2]->GetUUID(); | |
138 BluetoothUUID char_uuid4 = service->GetCharacteristics()[3]->GetUUID(); | |
139 EXPECT_EQ(char_uuid1, service->GetCharacteristic(char_id1)->GetUUID()); | |
140 EXPECT_EQ(char_uuid2, service->GetCharacteristic(char_id2)->GetUUID()); | |
141 EXPECT_EQ(char_uuid3, service->GetCharacteristic(char_id3)->GetUUID()); | |
142 EXPECT_EQ(char_uuid4, service->GetCharacteristic(char_id4)->GetUUID()); | |
143 | |
144 // GetCharacteristics & GetCharacteristic return the same object for the same | |
145 // ID: | |
146 EXPECT_EQ(service->GetCharacteristics()[0], | |
147 service->GetCharacteristic(char_id1)); | |
148 EXPECT_EQ(service->GetCharacteristic(char_id1), | |
149 service->GetCharacteristic(char_id1)); | |
150 } | |
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) | |
238 | |
239 } // namespace device | |
OLD | NEW |