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

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

Issue 2708513002: bluetooth: Post a task when sending GATT events to simulate another thread. (Closed)
Patch Set: Remove unnecessary if statement Created 3 years, 9 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 "base/run_loop.h"
6 #include "build/build_config.h"
5 #include "device/bluetooth/bluetooth_gatt_service.h" 7 #include "device/bluetooth/bluetooth_gatt_service.h"
6
7 #include "build/build_config.h"
8 #include "device/bluetooth/bluetooth_remote_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"
(...skipping 15 matching lines...) Expand all
33 StartLowEnergyDiscoverySession(); 33 StartLowEnergyDiscoverySession();
34 // 2 devices to verify unique IDs across them. 34 // 2 devices to verify unique IDs across them.
35 BluetoothDevice* device1 = SimulateLowEnergyDevice(3); 35 BluetoothDevice* device1 = SimulateLowEnergyDevice(3);
36 BluetoothDevice* device2 = SimulateLowEnergyDevice(4); 36 BluetoothDevice* device2 = SimulateLowEnergyDevice(4);
37 device1->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), 37 device1->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
38 GetConnectErrorCallback(Call::NOT_EXPECTED)); 38 GetConnectErrorCallback(Call::NOT_EXPECTED));
39 device2->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), 39 device2->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
40 GetConnectErrorCallback(Call::NOT_EXPECTED)); 40 GetConnectErrorCallback(Call::NOT_EXPECTED));
41 SimulateGattConnection(device1); 41 SimulateGattConnection(device1);
42 SimulateGattConnection(device2); 42 SimulateGattConnection(device2);
43 base::RunLoop().RunUntilIdle();
43 44
44 // 2 duplicate UUIDs creating 2 service instances on each device. 45 // 2 duplicate UUIDs creating 2 service instances on each device.
45 std::vector<std::string> services; 46 std::vector<std::string> services;
46 std::string uuid = "00000000-0000-1000-8000-00805f9b34fb"; 47 std::string uuid = "00000000-0000-1000-8000-00805f9b34fb";
47 services.push_back(uuid); 48 services.push_back(uuid);
48 services.push_back(uuid); 49 services.push_back(uuid);
49 SimulateGattServicesDiscovered(device1, services); 50 SimulateGattServicesDiscovered(device1, services);
50 SimulateGattServicesDiscovered(device2, services); 51 SimulateGattServicesDiscovered(device2, services);
52 base::RunLoop().RunUntilIdle();
51 BluetoothRemoteGattService* service1 = device1->GetGattServices()[0]; 53 BluetoothRemoteGattService* service1 = device1->GetGattServices()[0];
52 BluetoothRemoteGattService* service2 = device1->GetGattServices()[1]; 54 BluetoothRemoteGattService* service2 = device1->GetGattServices()[1];
53 BluetoothRemoteGattService* service3 = device2->GetGattServices()[0]; 55 BluetoothRemoteGattService* service3 = device2->GetGattServices()[0];
54 BluetoothRemoteGattService* service4 = device2->GetGattServices()[1]; 56 BluetoothRemoteGattService* service4 = device2->GetGattServices()[1];
55 57
56 // All IDs are unique, even though they have the same UUID. 58 // All IDs are unique, even though they have the same UUID.
57 EXPECT_NE(service1->GetIdentifier(), service2->GetIdentifier()); 59 EXPECT_NE(service1->GetIdentifier(), service2->GetIdentifier());
58 EXPECT_NE(service1->GetIdentifier(), service3->GetIdentifier()); 60 EXPECT_NE(service1->GetIdentifier(), service3->GetIdentifier());
59 EXPECT_NE(service1->GetIdentifier(), service4->GetIdentifier()); 61 EXPECT_NE(service1->GetIdentifier(), service4->GetIdentifier());
60 62
61 EXPECT_NE(service2->GetIdentifier(), service3->GetIdentifier()); 63 EXPECT_NE(service2->GetIdentifier(), service3->GetIdentifier());
62 EXPECT_NE(service2->GetIdentifier(), service4->GetIdentifier()); 64 EXPECT_NE(service2->GetIdentifier(), service4->GetIdentifier());
63 65
64 EXPECT_NE(service3->GetIdentifier(), service4->GetIdentifier()); 66 EXPECT_NE(service3->GetIdentifier(), service4->GetIdentifier());
65 } 67 }
66 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) 68 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN)
67 69
68 #if defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) 70 #if defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN)
69 TEST_F(BluetoothRemoteGattServiceTest, GetUUID) { 71 TEST_F(BluetoothRemoteGattServiceTest, GetUUID) {
70 if (!PlatformSupportsLowEnergy()) { 72 if (!PlatformSupportsLowEnergy()) {
71 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; 73 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
72 return; 74 return;
73 } 75 }
74 InitWithFakeAdapter(); 76 InitWithFakeAdapter();
75 StartLowEnergyDiscoverySession(); 77 StartLowEnergyDiscoverySession();
76 BluetoothDevice* device = SimulateLowEnergyDevice(3); 78 BluetoothDevice* device = SimulateLowEnergyDevice(3);
77 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), 79 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
78 GetConnectErrorCallback(Call::NOT_EXPECTED)); 80 GetConnectErrorCallback(Call::NOT_EXPECTED));
79 SimulateGattConnection(device); 81 SimulateGattConnection(device);
82 base::RunLoop().RunUntilIdle();
80 83
81 // Create multiple instances with the same UUID. 84 // Create multiple instances with the same UUID.
82 BluetoothUUID uuid("00000000-0000-1000-8000-00805f9b34fb"); 85 BluetoothUUID uuid("00000000-0000-1000-8000-00805f9b34fb");
83 std::vector<std::string> services; 86 std::vector<std::string> services;
84 services.push_back(uuid.canonical_value()); 87 services.push_back(uuid.canonical_value());
85 services.push_back(uuid.canonical_value()); 88 services.push_back(uuid.canonical_value());
86 SimulateGattServicesDiscovered(device, services); 89 SimulateGattServicesDiscovered(device, services);
90 base::RunLoop().RunUntilIdle();
87 91
88 // Each has the same UUID. 92 // Each has the same UUID.
89 EXPECT_EQ(uuid, device->GetGattServices()[0]->GetUUID()); 93 EXPECT_EQ(uuid, device->GetGattServices()[0]->GetUUID());
90 EXPECT_EQ(uuid, device->GetGattServices()[1]->GetUUID()); 94 EXPECT_EQ(uuid, device->GetGattServices()[1]->GetUUID());
91 } 95 }
92 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) 96 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN)
93 97
94 #if defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) 98 #if defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN)
95 TEST_F(BluetoothRemoteGattServiceTest, GetCharacteristics_FindNone) { 99 TEST_F(BluetoothRemoteGattServiceTest, GetCharacteristics_FindNone) {
96 if (!PlatformSupportsLowEnergy()) { 100 if (!PlatformSupportsLowEnergy()) {
97 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; 101 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
98 return; 102 return;
99 } 103 }
100 InitWithFakeAdapter(); 104 InitWithFakeAdapter();
101 StartLowEnergyDiscoverySession(); 105 StartLowEnergyDiscoverySession();
102 BluetoothDevice* device = SimulateLowEnergyDevice(3); 106 BluetoothDevice* device = SimulateLowEnergyDevice(3);
103 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), 107 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
104 GetConnectErrorCallback(Call::NOT_EXPECTED)); 108 GetConnectErrorCallback(Call::NOT_EXPECTED));
105 SimulateGattConnection(device); 109 SimulateGattConnection(device);
110 base::RunLoop().RunUntilIdle();
106 111
107 // Simulate a service, with no Characteristics: 112 // Simulate a service, with no Characteristics:
108 std::vector<std::string> services; 113 std::vector<std::string> services;
109 services.push_back("00000000-0000-1000-8000-00805f9b34fb"); 114 services.push_back("00000000-0000-1000-8000-00805f9b34fb");
110 SimulateGattServicesDiscovered(device, services); 115 SimulateGattServicesDiscovered(device, services);
116 base::RunLoop().RunUntilIdle();
111 BluetoothRemoteGattService* service = device->GetGattServices()[0]; 117 BluetoothRemoteGattService* service = device->GetGattServices()[0];
112 118
113 EXPECT_EQ(0u, service->GetCharacteristics().size()); 119 EXPECT_EQ(0u, service->GetCharacteristics().size());
114 } 120 }
115 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) 121 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN)
116 122
117 #if defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) 123 #if defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN)
118 TEST_F(BluetoothRemoteGattServiceTest, 124 TEST_F(BluetoothRemoteGattServiceTest,
119 GetCharacteristics_and_GetCharacteristic) { 125 GetCharacteristics_and_GetCharacteristic) {
120 if (!PlatformSupportsLowEnergy()) { 126 if (!PlatformSupportsLowEnergy()) {
121 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; 127 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
122 return; 128 return;
123 } 129 }
124 InitWithFakeAdapter(); 130 InitWithFakeAdapter();
125 StartLowEnergyDiscoverySession(); 131 StartLowEnergyDiscoverySession();
126 BluetoothDevice* device = SimulateLowEnergyDevice(3); 132 BluetoothDevice* device = SimulateLowEnergyDevice(3);
127 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), 133 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
128 GetConnectErrorCallback(Call::NOT_EXPECTED)); 134 GetConnectErrorCallback(Call::NOT_EXPECTED));
129 SimulateGattConnection(device); 135 SimulateGattConnection(device);
136 base::RunLoop().RunUntilIdle();
130 137
131 // Simulate a service, with several Characteristics: 138 // Simulate a service, with several Characteristics:
132 std::vector<std::string> services; 139 std::vector<std::string> services;
133 services.push_back("00000000-0000-1000-8000-00805f9b34fb"); 140 services.push_back("00000000-0000-1000-8000-00805f9b34fb");
134 SimulateGattServicesDiscovered(device, services); 141 SimulateGattServicesDiscovered(device, services);
142 base::RunLoop().RunUntilIdle();
135 BluetoothRemoteGattService* service = device->GetGattServices()[0]; 143 BluetoothRemoteGattService* service = device->GetGattServices()[0];
136 std::string characteristic_uuid1 = "11111111-0000-1000-8000-00805f9b34fb"; 144 std::string characteristic_uuid1 = "11111111-0000-1000-8000-00805f9b34fb";
137 std::string characteristic_uuid2 = "22222222-0000-1000-8000-00805f9b34fb"; 145 std::string characteristic_uuid2 = "22222222-0000-1000-8000-00805f9b34fb";
138 std::string characteristic_uuid3 = characteristic_uuid2; // Duplicate UUID. 146 std::string characteristic_uuid3 = characteristic_uuid2; // Duplicate UUID.
139 std::string characteristic_uuid4 = "33333333-0000-1000-8000-00805f9b34fb"; 147 std::string characteristic_uuid4 = "33333333-0000-1000-8000-00805f9b34fb";
140 SimulateGattCharacteristic(service, characteristic_uuid1, /* properties */ 0); 148 SimulateGattCharacteristic(service, characteristic_uuid1, /* properties */ 0);
141 SimulateGattCharacteristic(service, characteristic_uuid2, /* properties */ 0); 149 SimulateGattCharacteristic(service, characteristic_uuid2, /* properties */ 0);
142 SimulateGattCharacteristic(service, characteristic_uuid3, /* properties */ 0); 150 SimulateGattCharacteristic(service, characteristic_uuid3, /* properties */ 0);
143 SimulateGattCharacteristic(service, characteristic_uuid4, /* properties */ 0); 151 SimulateGattCharacteristic(service, characteristic_uuid4, /* properties */ 0);
144 152
(...skipping 27 matching lines...) Expand all
172 if (!PlatformSupportsLowEnergy()) { 180 if (!PlatformSupportsLowEnergy()) {
173 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; 181 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
174 return; 182 return;
175 } 183 }
176 InitWithFakeAdapter(); 184 InitWithFakeAdapter();
177 StartLowEnergyDiscoverySession(); 185 StartLowEnergyDiscoverySession();
178 BluetoothDevice* device = SimulateLowEnergyDevice(3); 186 BluetoothDevice* device = SimulateLowEnergyDevice(3);
179 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), 187 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
180 GetConnectErrorCallback(Call::NOT_EXPECTED)); 188 GetConnectErrorCallback(Call::NOT_EXPECTED));
181 SimulateGattConnection(device); 189 SimulateGattConnection(device);
190 base::RunLoop().RunUntilIdle();
182 191
183 TestBluetoothAdapterObserver observer(adapter_); 192 TestBluetoothAdapterObserver observer(adapter_);
184 193
185 // Simulate a service, with several Characteristics: 194 // Simulate a service, with several Characteristics:
186 std::vector<std::string> services; 195 std::vector<std::string> services;
187 services.push_back("00000000-0000-1000-8000-00805f9b34fb"); 196 services.push_back("00000000-0000-1000-8000-00805f9b34fb");
188 SimulateGattServicesDiscovered(device, services); 197 SimulateGattServicesDiscovered(device, services);
198 base::RunLoop().RunUntilIdle();
189 BluetoothRemoteGattService* service = device->GetGattServices()[0]; 199 BluetoothRemoteGattService* service = device->GetGattServices()[0];
190 std::string characteristic_uuid1 = "11111111-0000-1000-8000-00805f9b34fb"; 200 std::string characteristic_uuid1 = "11111111-0000-1000-8000-00805f9b34fb";
191 std::string characteristic_uuid2 = "22222222-0000-1000-8000-00805f9b34fb"; 201 std::string characteristic_uuid2 = "22222222-0000-1000-8000-00805f9b34fb";
192 std::string characteristic_uuid3 = characteristic_uuid2; // Duplicate UUID. 202 std::string characteristic_uuid3 = characteristic_uuid2; // Duplicate UUID.
193 std::string characteristic_uuid4 = "33333333-0000-1000-8000-00805f9b34fb"; 203 std::string characteristic_uuid4 = "33333333-0000-1000-8000-00805f9b34fb";
194 SimulateGattCharacteristic(service, characteristic_uuid1, /* properties */ 0); 204 SimulateGattCharacteristic(service, characteristic_uuid1, /* properties */ 0);
195 SimulateGattCharacteristic(service, characteristic_uuid2, /* properties */ 0); 205 SimulateGattCharacteristic(service, characteristic_uuid2, /* properties */ 0);
196 SimulateGattCharacteristic(service, characteristic_uuid3, /* properties */ 0); 206 SimulateGattCharacteristic(service, characteristic_uuid3, /* properties */ 0);
197 SimulateGattCharacteristic(service, characteristic_uuid4, /* properties */ 0); 207 SimulateGattCharacteristic(service, characteristic_uuid4, /* properties */ 0);
198 #if !defined(OS_WIN) 208 #if !defined(OS_WIN)
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
243 if (!PlatformSupportsLowEnergy()) { 253 if (!PlatformSupportsLowEnergy()) {
244 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; 254 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
245 return; 255 return;
246 } 256 }
247 InitWithFakeAdapter(); 257 InitWithFakeAdapter();
248 StartLowEnergyDiscoverySession(); 258 StartLowEnergyDiscoverySession();
249 BluetoothDevice* device = SimulateLowEnergyDevice(3); 259 BluetoothDevice* device = SimulateLowEnergyDevice(3);
250 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), 260 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
251 GetConnectErrorCallback(Call::NOT_EXPECTED)); 261 GetConnectErrorCallback(Call::NOT_EXPECTED));
252 SimulateGattConnection(device); 262 SimulateGattConnection(device);
263 base::RunLoop().RunUntilIdle();
253 264
254 TestBluetoothAdapterObserver observer(adapter_); 265 TestBluetoothAdapterObserver observer(adapter_);
255 266
256 // Simulate two primary GATT services. 267 // Simulate two primary GATT services.
257 std::vector<std::string> services; 268 std::vector<std::string> services;
258 services.push_back("00000000-0000-1000-8000-00805f9b34fb"); 269 services.push_back("00000000-0000-1000-8000-00805f9b34fb");
259 services.push_back("01010101-0101-1000-8000-00805f9b34fb"); 270 services.push_back("01010101-0101-1000-8000-00805f9b34fb");
260 SimulateGattServicesDiscovered(device, services); 271 SimulateGattServicesDiscovered(device, services);
261 EXPECT_EQ(2u, device->GetGattServices().size()); 272 EXPECT_EQ(2u, device->GetGattServices().size());
262 273
263 // Simulate remove of a primary service. 274 // Simulate remove of a primary service.
264 BluetoothRemoteGattService* service1 = device->GetGattServices()[0]; 275 BluetoothRemoteGattService* service1 = device->GetGattServices()[0];
265 BluetoothRemoteGattService* service2 = device->GetGattServices()[1]; 276 BluetoothRemoteGattService* service2 = device->GetGattServices()[1];
266 std::string removed_service = service1->GetIdentifier(); 277 std::string removed_service = service1->GetIdentifier();
267 SimulateGattServiceRemoved(device->GetGattService(removed_service)); 278 SimulateGattServiceRemoved(device->GetGattService(removed_service));
268 EXPECT_EQ(1, observer.gatt_service_removed_count()); 279 EXPECT_EQ(1, observer.gatt_service_removed_count());
269 EXPECT_EQ(1u, device->GetGattServices().size()); 280 EXPECT_EQ(1u, device->GetGattServices().size());
270 EXPECT_FALSE(device->GetGattService(removed_service)); 281 EXPECT_FALSE(device->GetGattService(removed_service));
271 EXPECT_EQ(device->GetGattServices()[0], service2); 282 EXPECT_EQ(device->GetGattServices()[0], service2);
272 } 283 }
273 #endif // defined(OS_WIN) || defined(OS_MACOSX) 284 #endif // defined(OS_WIN) || defined(OS_MACOSX)
274 285
275 } // namespace device 286 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698