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

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

Issue 1898643002: Refactor device::BluetoothGattXXX classes to split into remote/local. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: minor cleanup Created 4 years, 8 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
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_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_remote_gatt_characteristic.h"
9 #include "device/bluetooth/test/test_bluetooth_adapter_observer.h" 9 #include "device/bluetooth/test/test_bluetooth_adapter_observer.h"
10 #include "testing/gtest/include/gtest/gtest.h" 10 #include "testing/gtest/include/gtest/gtest.h"
11 11
12 #if defined(OS_ANDROID) 12 #if defined(OS_ANDROID)
13 #include "device/bluetooth/test/bluetooth_test_android.h" 13 #include "device/bluetooth/test/bluetooth_test_android.h"
14 #elif defined(OS_MACOSX) 14 #elif defined(OS_MACOSX)
15 #include "device/bluetooth/test/bluetooth_test_mac.h" 15 #include "device/bluetooth/test/bluetooth_test_mac.h"
16 #elif defined(OS_WIN) 16 #elif defined(OS_WIN)
17 #include "device/bluetooth/test/bluetooth_test_win.h" 17 #include "device/bluetooth/test/bluetooth_test_win.h"
18 #endif 18 #endif
19 19
20 namespace device { 20 namespace device {
21 21
22 #if defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) 22 #if defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN)
23 class BluetoothGattServiceTest : public BluetoothTest {}; 23 class BluetoothRemoteGattServiceTest : public BluetoothTest {};
24 #endif 24 #endif
25 25
26 #if defined(OS_ANDROID) || defined(OS_WIN) 26 #if defined(OS_ANDROID) || defined(OS_WIN)
27 TEST_F(BluetoothGattServiceTest, GetIdentifier) { 27 TEST_F(BluetoothRemoteGattServiceTest, GetIdentifier) {
28 InitWithFakeAdapter(); 28 InitWithFakeAdapter();
29 StartLowEnergyDiscoverySession(); 29 StartLowEnergyDiscoverySession();
30 // 2 devices to verify unique IDs across them. 30 // 2 devices to verify unique IDs across them.
31 BluetoothDevice* device1 = DiscoverLowEnergyDevice(3); 31 BluetoothDevice* device1 = DiscoverLowEnergyDevice(3);
32 BluetoothDevice* device2 = DiscoverLowEnergyDevice(4); 32 BluetoothDevice* device2 = DiscoverLowEnergyDevice(4);
33 device1->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), 33 device1->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
34 GetConnectErrorCallback(Call::NOT_EXPECTED)); 34 GetConnectErrorCallback(Call::NOT_EXPECTED));
35 device2->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), 35 device2->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
36 GetConnectErrorCallback(Call::NOT_EXPECTED)); 36 GetConnectErrorCallback(Call::NOT_EXPECTED));
37 SimulateGattConnection(device1); 37 SimulateGattConnection(device1);
38 SimulateGattConnection(device2); 38 SimulateGattConnection(device2);
39 39
40 // 2 duplicate UUIDs creating 2 service instances on each device. 40 // 2 duplicate UUIDs creating 2 service instances on each device.
41 std::vector<std::string> services; 41 std::vector<std::string> services;
42 std::string uuid = "00000000-0000-1000-8000-00805f9b34fb"; 42 std::string uuid = "00000000-0000-1000-8000-00805f9b34fb";
43 services.push_back(uuid); 43 services.push_back(uuid);
44 services.push_back(uuid); 44 services.push_back(uuid);
45 SimulateGattServicesDiscovered(device1, services); 45 SimulateGattServicesDiscovered(device1, services);
46 SimulateGattServicesDiscovered(device2, services); 46 SimulateGattServicesDiscovered(device2, services);
47 BluetoothGattService* service1 = device1->GetGattServices()[0]; 47 BluetoothRemoteGattService* service1 = device1->GetGattServices()[0];
48 BluetoothGattService* service2 = device1->GetGattServices()[1]; 48 BluetoothRemoteGattService* service2 = device1->GetGattServices()[1];
49 BluetoothGattService* service3 = device2->GetGattServices()[0]; 49 BluetoothRemoteGattService* service3 = device2->GetGattServices()[0];
50 BluetoothGattService* service4 = device2->GetGattServices()[1]; 50 BluetoothRemoteGattService* service4 = device2->GetGattServices()[1];
51 51
52 // All IDs are unique, even though they have the same UUID. 52 // All IDs are unique, even though they have the same UUID.
53 EXPECT_NE(service1->GetIdentifier(), service2->GetIdentifier()); 53 EXPECT_NE(service1->GetIdentifier(), service2->GetIdentifier());
54 EXPECT_NE(service1->GetIdentifier(), service3->GetIdentifier()); 54 EXPECT_NE(service1->GetIdentifier(), service3->GetIdentifier());
55 EXPECT_NE(service1->GetIdentifier(), service4->GetIdentifier()); 55 EXPECT_NE(service1->GetIdentifier(), service4->GetIdentifier());
56 56
57 EXPECT_NE(service2->GetIdentifier(), service3->GetIdentifier()); 57 EXPECT_NE(service2->GetIdentifier(), service3->GetIdentifier());
58 EXPECT_NE(service2->GetIdentifier(), service4->GetIdentifier()); 58 EXPECT_NE(service2->GetIdentifier(), service4->GetIdentifier());
59 59
60 EXPECT_NE(service3->GetIdentifier(), service4->GetIdentifier()); 60 EXPECT_NE(service3->GetIdentifier(), service4->GetIdentifier());
61 } 61 }
62 #endif // defined(OS_ANDROID) || defined(OS_WIN) 62 #endif // defined(OS_ANDROID) || defined(OS_WIN)
63 63
64 #if defined(OS_ANDROID) || defined(OS_WIN) 64 #if defined(OS_ANDROID) || defined(OS_WIN)
65 TEST_F(BluetoothGattServiceTest, GetUUID) { 65 TEST_F(BluetoothRemoteGattServiceTest, GetUUID) {
66 InitWithFakeAdapter(); 66 InitWithFakeAdapter();
67 StartLowEnergyDiscoverySession(); 67 StartLowEnergyDiscoverySession();
68 BluetoothDevice* device = DiscoverLowEnergyDevice(3); 68 BluetoothDevice* device = DiscoverLowEnergyDevice(3);
69 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), 69 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
70 GetConnectErrorCallback(Call::NOT_EXPECTED)); 70 GetConnectErrorCallback(Call::NOT_EXPECTED));
71 SimulateGattConnection(device); 71 SimulateGattConnection(device);
72 72
73 // Create multiple instances with the same UUID. 73 // Create multiple instances with the same UUID.
74 BluetoothUUID uuid("00000000-0000-1000-8000-00805f9b34fb"); 74 BluetoothUUID uuid("00000000-0000-1000-8000-00805f9b34fb");
75 std::vector<std::string> services; 75 std::vector<std::string> services;
76 services.push_back(uuid.canonical_value()); 76 services.push_back(uuid.canonical_value());
77 services.push_back(uuid.canonical_value()); 77 services.push_back(uuid.canonical_value());
78 SimulateGattServicesDiscovered(device, services); 78 SimulateGattServicesDiscovered(device, services);
79 79
80 // Each has the same UUID. 80 // Each has the same UUID.
81 EXPECT_EQ(uuid, device->GetGattServices()[0]->GetUUID()); 81 EXPECT_EQ(uuid, device->GetGattServices()[0]->GetUUID());
82 EXPECT_EQ(uuid, device->GetGattServices()[1]->GetUUID()); 82 EXPECT_EQ(uuid, device->GetGattServices()[1]->GetUUID());
83 } 83 }
84 #endif // defined(OS_ANDROID) || defined(OS_WIN) 84 #endif // defined(OS_ANDROID) || defined(OS_WIN)
85 85
86 #if defined(OS_ANDROID) || defined(OS_WIN) 86 #if defined(OS_ANDROID) || defined(OS_WIN)
87 TEST_F(BluetoothGattServiceTest, GetCharacteristics_FindNone) { 87 TEST_F(BluetoothRemoteGattServiceTest, GetCharacteristics_FindNone) {
88 InitWithFakeAdapter(); 88 InitWithFakeAdapter();
89 StartLowEnergyDiscoverySession(); 89 StartLowEnergyDiscoverySession();
90 BluetoothDevice* device = DiscoverLowEnergyDevice(3); 90 BluetoothDevice* device = DiscoverLowEnergyDevice(3);
91 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), 91 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
92 GetConnectErrorCallback(Call::NOT_EXPECTED)); 92 GetConnectErrorCallback(Call::NOT_EXPECTED));
93 SimulateGattConnection(device); 93 SimulateGattConnection(device);
94 94
95 // Simulate a service, with no Characteristics: 95 // Simulate a service, with no Characteristics:
96 std::vector<std::string> services; 96 std::vector<std::string> services;
97 services.push_back("00000000-0000-1000-8000-00805f9b34fb"); 97 services.push_back("00000000-0000-1000-8000-00805f9b34fb");
98 SimulateGattServicesDiscovered(device, services); 98 SimulateGattServicesDiscovered(device, services);
99 BluetoothGattService* service = device->GetGattServices()[0]; 99 BluetoothRemoteGattService* service = device->GetGattServices()[0];
100 100
101 EXPECT_EQ(0u, service->GetCharacteristics().size()); 101 EXPECT_EQ(0u, service->GetCharacteristics().size());
102 } 102 }
103 #endif // defined(OS_ANDROID) || defined(OS_WIN) 103 #endif // defined(OS_ANDROID) || defined(OS_WIN)
104 104
105 #if defined(OS_ANDROID) || defined(OS_WIN) 105 #if defined(OS_ANDROID) || defined(OS_WIN)
106 TEST_F(BluetoothGattServiceTest, GetCharacteristics_and_GetCharacteristic) { 106 TEST_F(BluetoothRemoteGattServiceTest,
107 GetCharacteristics_and_GetCharacteristic) {
107 InitWithFakeAdapter(); 108 InitWithFakeAdapter();
108 StartLowEnergyDiscoverySession(); 109 StartLowEnergyDiscoverySession();
109 BluetoothDevice* device = DiscoverLowEnergyDevice(3); 110 BluetoothDevice* device = DiscoverLowEnergyDevice(3);
110 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), 111 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
111 GetConnectErrorCallback(Call::NOT_EXPECTED)); 112 GetConnectErrorCallback(Call::NOT_EXPECTED));
112 SimulateGattConnection(device); 113 SimulateGattConnection(device);
113 114
114 // Simulate a service, with several Characteristics: 115 // Simulate a service, with several Characteristics:
115 std::vector<std::string> services; 116 std::vector<std::string> services;
116 services.push_back("00000000-0000-1000-8000-00805f9b34fb"); 117 services.push_back("00000000-0000-1000-8000-00805f9b34fb");
117 SimulateGattServicesDiscovered(device, services); 118 SimulateGattServicesDiscovered(device, services);
118 BluetoothGattService* service = device->GetGattServices()[0]; 119 BluetoothRemoteGattService* service = device->GetGattServices()[0];
119 std::string characteristic_uuid1 = "11111111-0000-1000-8000-00805f9b34fb"; 120 std::string characteristic_uuid1 = "11111111-0000-1000-8000-00805f9b34fb";
120 std::string characteristic_uuid2 = "22222222-0000-1000-8000-00805f9b34fb"; 121 std::string characteristic_uuid2 = "22222222-0000-1000-8000-00805f9b34fb";
121 std::string characteristic_uuid3 = characteristic_uuid2; // Duplicate UUID. 122 std::string characteristic_uuid3 = characteristic_uuid2; // Duplicate UUID.
122 std::string characteristic_uuid4 = "33333333-0000-1000-8000-00805f9b34fb"; 123 std::string characteristic_uuid4 = "33333333-0000-1000-8000-00805f9b34fb";
123 SimulateGattCharacteristic(service, characteristic_uuid1, /* properties */ 0); 124 SimulateGattCharacteristic(service, characteristic_uuid1, /* properties */ 0);
124 SimulateGattCharacteristic(service, characteristic_uuid2, /* properties */ 0); 125 SimulateGattCharacteristic(service, characteristic_uuid2, /* properties */ 0);
125 SimulateGattCharacteristic(service, characteristic_uuid3, /* properties */ 0); 126 SimulateGattCharacteristic(service, characteristic_uuid3, /* properties */ 0);
126 SimulateGattCharacteristic(service, characteristic_uuid4, /* properties */ 0); 127 SimulateGattCharacteristic(service, characteristic_uuid4, /* properties */ 0);
127 128
128 // Verify that GetCharacteristic can retrieve characteristics again by ID, 129 // Verify that GetCharacteristic can retrieve characteristics again by ID,
(...skipping 15 matching lines...) Expand all
144 // GetCharacteristics & GetCharacteristic return the same object for the same 145 // GetCharacteristics & GetCharacteristic return the same object for the same
145 // ID: 146 // ID:
146 EXPECT_EQ(service->GetCharacteristics()[0], 147 EXPECT_EQ(service->GetCharacteristics()[0],
147 service->GetCharacteristic(char_id1)); 148 service->GetCharacteristic(char_id1));
148 EXPECT_EQ(service->GetCharacteristic(char_id1), 149 EXPECT_EQ(service->GetCharacteristic(char_id1),
149 service->GetCharacteristic(char_id1)); 150 service->GetCharacteristic(char_id1));
150 } 151 }
151 #endif // defined(OS_ANDROID) || defined(OS_WIN) 152 #endif // defined(OS_ANDROID) || defined(OS_WIN)
152 153
153 #if defined(OS_WIN) 154 #if defined(OS_WIN)
154 TEST_F(BluetoothGattServiceTest, GetCharacteristic_CharacteristicRemoved) { 155 TEST_F(BluetoothRemoteGattServiceTest,
156 GetCharacteristic_CharacteristicRemoved) {
155 InitWithFakeAdapter(); 157 InitWithFakeAdapter();
156 StartLowEnergyDiscoverySession(); 158 StartLowEnergyDiscoverySession();
157 BluetoothDevice* device = DiscoverLowEnergyDevice(3); 159 BluetoothDevice* device = DiscoverLowEnergyDevice(3);
158 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), 160 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
159 GetConnectErrorCallback(Call::NOT_EXPECTED)); 161 GetConnectErrorCallback(Call::NOT_EXPECTED));
160 SimulateGattConnection(device); 162 SimulateGattConnection(device);
161 163
162 TestBluetoothAdapterObserver observer(adapter_); 164 TestBluetoothAdapterObserver observer(adapter_);
163 165
164 // Simulate a service, with several Characteristics: 166 // Simulate a service, with several Characteristics:
165 std::vector<std::string> services; 167 std::vector<std::string> services;
166 services.push_back("00000000-0000-1000-8000-00805f9b34fb"); 168 services.push_back("00000000-0000-1000-8000-00805f9b34fb");
167 SimulateGattServicesDiscovered(device, services); 169 SimulateGattServicesDiscovered(device, services);
168 BluetoothGattService* service = device->GetGattServices()[0]; 170 BluetoothRemoteGattService* service = device->GetGattServices()[0];
169 std::string characteristic_uuid1 = "11111111-0000-1000-8000-00805f9b34fb"; 171 std::string characteristic_uuid1 = "11111111-0000-1000-8000-00805f9b34fb";
170 std::string characteristic_uuid2 = "22222222-0000-1000-8000-00805f9b34fb"; 172 std::string characteristic_uuid2 = "22222222-0000-1000-8000-00805f9b34fb";
171 std::string characteristic_uuid3 = characteristic_uuid2; // Duplicate UUID. 173 std::string characteristic_uuid3 = characteristic_uuid2; // Duplicate UUID.
172 std::string characteristic_uuid4 = "33333333-0000-1000-8000-00805f9b34fb"; 174 std::string characteristic_uuid4 = "33333333-0000-1000-8000-00805f9b34fb";
173 SimulateGattCharacteristic(service, characteristic_uuid1, /* properties */ 0); 175 SimulateGattCharacteristic(service, characteristic_uuid1, /* properties */ 0);
174 SimulateGattCharacteristic(service, characteristic_uuid2, /* properties */ 0); 176 SimulateGattCharacteristic(service, characteristic_uuid2, /* properties */ 0);
175 SimulateGattCharacteristic(service, characteristic_uuid3, /* properties */ 0); 177 SimulateGattCharacteristic(service, characteristic_uuid3, /* properties */ 0);
176 SimulateGattCharacteristic(service, characteristic_uuid4, /* properties */ 0); 178 SimulateGattCharacteristic(service, characteristic_uuid4, /* properties */ 0);
177 179
178 // Simulate remove of characteristics one by one. 180 // Simulate remove of characteristics one by one.
(...skipping 21 matching lines...) Expand all
200 service->GetCharacteristic(removed_char)); 202 service->GetCharacteristic(removed_char));
201 EXPECT_EQ(4, observer.gatt_characteristic_removed_count()); 203 EXPECT_EQ(4, observer.gatt_characteristic_removed_count());
202 EXPECT_FALSE(service->GetCharacteristic(removed_char)); 204 EXPECT_FALSE(service->GetCharacteristic(removed_char));
203 EXPECT_EQ(0u, service->GetCharacteristics().size()); 205 EXPECT_EQ(0u, service->GetCharacteristics().size());
204 206
205 EXPECT_EQ(4, observer.gatt_service_changed_count()); 207 EXPECT_EQ(4, observer.gatt_service_changed_count());
206 } 208 }
207 #endif // defined(OS_WIN) 209 #endif // defined(OS_WIN)
208 210
209 #if defined(OS_WIN) 211 #if defined(OS_WIN)
210 TEST_F(BluetoothGattServiceTest, SimulateGattServiceRemove) { 212 TEST_F(BluetoothRemoteGattServiceTest, SimulateGattServiceRemove) {
211 InitWithFakeAdapter(); 213 InitWithFakeAdapter();
212 StartLowEnergyDiscoverySession(); 214 StartLowEnergyDiscoverySession();
213 BluetoothDevice* device = DiscoverLowEnergyDevice(3); 215 BluetoothDevice* device = DiscoverLowEnergyDevice(3);
214 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), 216 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
215 GetConnectErrorCallback(Call::NOT_EXPECTED)); 217 GetConnectErrorCallback(Call::NOT_EXPECTED));
216 SimulateGattConnection(device); 218 SimulateGattConnection(device);
217 219
218 TestBluetoothAdapterObserver observer(adapter_); 220 TestBluetoothAdapterObserver observer(adapter_);
219 221
220 // Simulate two primary GATT services. 222 // Simulate two primary GATT services.
221 std::vector<std::string> services; 223 std::vector<std::string> services;
222 services.push_back("00000000-0000-1000-8000-00805f9b34fb"); 224 services.push_back("00000000-0000-1000-8000-00805f9b34fb");
223 services.push_back("01010101-0101-1000-8000-00805f9b34fb"); 225 services.push_back("01010101-0101-1000-8000-00805f9b34fb");
224 SimulateGattServicesDiscovered(device, services); 226 SimulateGattServicesDiscovered(device, services);
225 EXPECT_EQ(2u, device->GetGattServices().size()); 227 EXPECT_EQ(2u, device->GetGattServices().size());
226 228
227 // Simulate remove of a primary service. 229 // Simulate remove of a primary service.
228 BluetoothGattService* service1 = device->GetGattServices()[0]; 230 BluetoothRemoteGattService* service1 = device->GetGattServices()[0];
229 BluetoothGattService* service2 = device->GetGattServices()[1]; 231 BluetoothRemoteGattService* service2 = device->GetGattServices()[1];
230 std::string removed_service = service1->GetIdentifier(); 232 std::string removed_service = service1->GetIdentifier();
231 SimulateGattServiceRemoved(device->GetGattService(removed_service)); 233 SimulateGattServiceRemoved(device->GetGattService(removed_service));
232 EXPECT_EQ(1, observer.gatt_service_removed_count()); 234 EXPECT_EQ(1, observer.gatt_service_removed_count());
233 EXPECT_EQ(1u, device->GetGattServices().size()); 235 EXPECT_EQ(1u, device->GetGattServices().size());
234 EXPECT_FALSE(device->GetGattService(removed_service)); 236 EXPECT_FALSE(device->GetGattService(removed_service));
235 EXPECT_EQ(device->GetGattServices()[0], service2); 237 EXPECT_EQ(device->GetGattServices()[0], service2);
236 } 238 }
237 #endif // defined(OS_WIN) 239 #endif // defined(OS_WIN)
238 240
239 } // namespace device 241 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698