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

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

Issue 1256313002: bluetooth: android: Implement & test CreateGattConnection. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added 2 more connect/disconnect test blocks Created 5 years, 3 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 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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_device.h" 5 #include "device/bluetooth/bluetooth_device.h"
6 6
7 #include "base/macros.h" 7 #include "base/macros.h"
8 #include "base/run_loop.h" 8 #include "base/run_loop.h"
9 #include "base/strings/utf_string_conversions.h" 9 #include "base/strings/utf_string_conversions.h"
10 #include "device/bluetooth/test/test_bluetooth_adapter_observer.h" 10 #include "device/bluetooth/test/test_bluetooth_adapter_observer.h"
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
118 } 118 }
119 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) 119 #endif // defined(OS_ANDROID) || defined(OS_MACOSX)
120 120
121 // TODO(scheib): Test with a device with no name. http://crbug.com/506415 121 // TODO(scheib): Test with a device with no name. http://crbug.com/506415
122 // BluetoothDevice::GetAddressWithLocalizedDeviceTypeName() will run, which 122 // BluetoothDevice::GetAddressWithLocalizedDeviceTypeName() will run, which
123 // requires string resources to be loaded. For that, something like 123 // requires string resources to be loaded. For that, something like
124 // InitSharedInstance must be run. See unittest files that call that. It will 124 // InitSharedInstance must be run. See unittest files that call that. It will
125 // also require build configuration to generate string resources into a .pak 125 // also require build configuration to generate string resources into a .pak
126 // file. 126 // file.
127 127
128 #if defined(OS_ANDROID)
129 // Basic CreateGattConnection test.
130 TEST_F(BluetoothTest, CreateGattConnection) {
131 InitWithFakeAdapter();
132 TestBluetoothAdapterObserver observer(adapter_);
133
134 // Get a device.
135 adapter_->StartDiscoverySession(GetDiscoverySessionCallback(),
136 GetErrorCallback());
137 base::RunLoop().RunUntilIdle();
138 DiscoverLowEnergyDevice(3);
139 base::RunLoop().RunUntilIdle();
140 BluetoothDevice* device = observer.last_device();
141
142 callback_count_ = error_callback_count_ = 0;
143 device->CreateGattConnection(GetGattConnectionCallback(),
144 GetConnectErrorCallback());
145 CompleteGattConnection(device);
146 EXPECT_EQ(1, callback_count_);
147 EXPECT_EQ(0, error_callback_count_);
148 ASSERT_EQ(1u, gatt_connections_.size());
149 EXPECT_TRUE(device->IsGattConnected());
150 EXPECT_TRUE(gatt_connections_[0]->IsConnected());
151 }
152 #endif // defined(OS_ANDROID)
153
154 #if defined(OS_ANDROID)
155 // Creates BluetoothGattConnection instances and tests that the interface
156 // functions even when some Disconnect and the BluetoothDevice is destroyed.
157 TEST_F(BluetoothTest, BluetoothGattConnection) {
158 InitWithFakeAdapter();
159 TestBluetoothAdapterObserver observer(adapter_);
160
161 // Get a device.
162 adapter_->StartDiscoverySession(GetDiscoverySessionCallback(),
163 GetErrorCallback());
164 base::RunLoop().RunUntilIdle();
165 DiscoverLowEnergyDevice(3);
166 base::RunLoop().RunUntilIdle();
167 BluetoothDevice* device = observer.last_device();
168 std::string device_address = device->GetAddress();
169
170 // CreateGattConnection
171 callback_count_ = error_callback_count_ = 0;
172 device->CreateGattConnection(GetGattConnectionCallback(),
173 GetConnectErrorCallback());
174 EXPECT_EQ(1, gatt_connection_attempt_count_);
175 CompleteGattConnection(device);
176 EXPECT_EQ(1, callback_count_);
177 EXPECT_EQ(0, error_callback_count_);
178 ASSERT_EQ(1u, gatt_connections_.size());
179 EXPECT_TRUE(device->IsGattConnected());
180 EXPECT_TRUE(gatt_connections_[0]->IsConnected());
181
182 // Connect again once already connected.
183 device->CreateGattConnection(GetGattConnectionCallback(),
184 GetConnectErrorCallback());
185 device->CreateGattConnection(GetGattConnectionCallback(),
186 GetConnectErrorCallback());
187 EXPECT_EQ(1, gatt_connection_attempt_count_);
188 EXPECT_EQ(3, callback_count_);
189 EXPECT_EQ(0, error_callback_count_);
190 ASSERT_EQ(3u, gatt_connections_.size());
191
192 // Test GetDeviceAddress
193 EXPECT_EQ(device_address, gatt_connections_[0]->GetDeviceAddress());
194
195 // Test IsConnected
196 EXPECT_TRUE(gatt_connections_[0]->IsConnected());
197 EXPECT_TRUE(gatt_connections_[1]->IsConnected());
198 EXPECT_TRUE(gatt_connections_[2]->IsConnected());
199
200 // Disconnect & Delete connection objects. Device stays connected.
201 gatt_connections_[0]->Disconnect(); // Disconnect first.
202 gatt_connections_.pop_back(); // Delete last.
203 EXPECT_FALSE(gatt_connections_[0]->IsConnected());
204 EXPECT_TRUE(gatt_connections_[1]->IsConnected());
205 EXPECT_TRUE(device->IsGattConnected());
206 EXPECT_EQ(0, gatt_disconnection_attempt_count_);
207
208 // Delete device, connection objects should all be disconnected.
209 DeleteDevice(device);
210 EXPECT_FALSE(gatt_connections_[0]->IsConnected());
211 EXPECT_FALSE(gatt_connections_[1]->IsConnected());
212
213 // Test GetDeviceAddress after device deleted.
214 EXPECT_EQ(device_address, gatt_connections_[0]->GetDeviceAddress());
215 EXPECT_EQ(device_address, gatt_connections_[1]->GetDeviceAddress());
216 }
217 #endif // defined(OS_ANDROID)
218
219 #if defined(OS_ANDROID)
220 // BluetoothGattConnection with several connect / disconnects.
221 TEST_F(BluetoothTest, BluetoothGattConnection_ConnectDisconnect) {
222 InitWithFakeAdapter();
223 TestBluetoothAdapterObserver observer(adapter_);
224
225 // Get a device.
226 adapter_->StartDiscoverySession(GetDiscoverySessionCallback(),
227 GetErrorCallback());
228 base::RunLoop().RunUntilIdle();
229 DiscoverLowEnergyDevice(3);
230 base::RunLoop().RunUntilIdle();
231 BluetoothDevice* device = observer.last_device();
232
233 // CreateGattConnection, & multiple connections from platform only invoke
234 // callbacks once:
235 callback_count_ = error_callback_count_ = 0;
236 device->CreateGattConnection(GetGattConnectionCallback(),
237 GetConnectErrorCallback());
238 CompleteGattConnection(device);
239 CompleteGattConnection(device);
240 EXPECT_EQ(1, gatt_connection_attempt_count_);
241 EXPECT_EQ(1, callback_count_);
242 EXPECT_EQ(0, error_callback_count_);
243 EXPECT_TRUE(gatt_connections_[0]->IsConnected());
244
245 // Become disconnected:
246 CompleteGattDisconnection(device);
247 EXPECT_EQ(1, callback_count_);
248 EXPECT_EQ(0, error_callback_count_);
249 EXPECT_FALSE(gatt_connections_[0]->IsConnected());
250
251 // Be already connected, then CreateGattConnection:
252 callback_count_ = error_callback_count_ = 0;
253 CompleteGattConnection(device);
254 device->CreateGattConnection(GetGattConnectionCallback(),
255 GetConnectErrorCallback());
256 EXPECT_EQ(1, gatt_connection_attempt_count_);
257 EXPECT_EQ(1, callback_count_);
258 EXPECT_EQ(0, error_callback_count_);
259 EXPECT_FALSE(gatt_connections_[0]->IsConnected())
260 << "The disconnected connection shouldn't become connected when another "
261 "connection is created.";
262 EXPECT_TRUE(gatt_connections_[1]->IsConnected());
263
264 // Delete all CreateGattConnection objects, observe disconnection:
265 callback_count_ = error_callback_count_ = 0;
266 gatt_connections_.clear();
267 EXPECT_EQ(1, gatt_disconnection_attempt_count_);
268 CompleteGattDisconnection(device);
269
270 // Reconnect for following test:
271 device->CreateGattConnection(GetGattConnectionCallback(),
272 GetConnectErrorCallback());
273 device->CreateGattConnection(GetGattConnectionCallback(),
274 GetConnectErrorCallback());
275 CompleteGattConnection(device);
276
277 // Disconnect all CreateGattConnection objects & create a new connection.
278 // But, don't yet simulate the device disconnecting.
279 callback_count_ = error_callback_count_ = gatt_connection_attempt_count_ =
280 gatt_disconnection_attempt_count_ = 0;
281 for (BluetoothGattConnection* connection : gatt_connections_)
282 connection->Disconnect();
283 EXPECT_EQ(1, gatt_disconnection_attempt_count_);
284 device->CreateGattConnection(GetGattConnectionCallback(),
285 GetConnectErrorCallback());
286 EXPECT_EQ(0, gatt_connection_attempt_count_); // No connection attempt.
287 EXPECT_EQ(1, callback_count_); // Device is assumed still connected.
288 EXPECT_EQ(0, error_callback_count_);
289 callback_count_ = error_callback_count_ = 0;
290 EXPECT_FALSE(gatt_connections_.front()->IsConnected());
291 EXPECT_TRUE(gatt_connections_.back()->IsConnected());
292
293 // Actually disconnect:
294 CompleteGattDisconnection(device);
295 EXPECT_EQ(0, callback_count_);
296 EXPECT_EQ(0, error_callback_count_);
297 for (BluetoothGattConnection* connection : gatt_connections_)
298 EXPECT_FALSE(connection->IsConnected());
299
300 // CreateGattConnection, but receive notice that device disconnected before
301 // it ever connects:
302 callback_count_ = error_callback_count_ = 0;
303 last_connect_error_code_ = BluetoothDevice::ERROR_UNKNOWN;
304 device->CreateGattConnection(GetGattConnectionCallback(),
305 GetConnectErrorCallback());
306 EXPECT_EQ(1, gatt_connection_attempt_count_);
307 CompleteGattDisconnection(device);
308 EXPECT_EQ(0, callback_count_);
309 EXPECT_EQ(1, error_callback_count_);
310 EXPECT_EQ(BluetoothDevice::ERROR_FAILED, last_connect_error_code_);
311 for (BluetoothGattConnection* connection : gatt_connections_)
312 EXPECT_FALSE(connection->IsConnected());
313
314 // CreateGattConnection & DisconnectGatt, then simulate connection.
Jeffrey Yasskin 2015/09/23 00:11:11 This test has gotten very long. Can you split it i
scheib 2015/09/23 03:23:00 Done. I already have a clean up CL, I'll merge thi
315 callback_count_ = error_callback_count_ = gatt_connection_attempt_count_ =
316 gatt_disconnection_attempt_count_ = 0;
317 device->CreateGattConnection(GetGattConnectionCallback(),
318 GetConnectErrorCallback());
319 device->DisconnectGatt();
320 EXPECT_EQ(1, gatt_connection_attempt_count_);
321 EXPECT_EQ(1, gatt_disconnection_attempt_count_);
322 CompleteGattConnection(device);
323 EXPECT_EQ(1, callback_count_);
324 EXPECT_EQ(0, error_callback_count_);
325 EXPECT_TRUE(gatt_connections_.back()->IsConnected());
326 callback_count_ = error_callback_count_ = gatt_connection_attempt_count_ =
327 gatt_disconnection_attempt_count_ = 0;
328 CompleteGattDisconnection(device);
329 EXPECT_EQ(0, callback_count_);
330 EXPECT_EQ(0, error_callback_count_);
331
332 // CreateGattConnection & DisconnectGatt, then simulate disconnection.
333 callback_count_ = error_callback_count_ = gatt_connection_attempt_count_ =
334 gatt_disconnection_attempt_count_ = 0;
335 last_connect_error_code_ = BluetoothDevice::ERROR_UNKNOWN;
336 device->CreateGattConnection(GetGattConnectionCallback(),
337 GetConnectErrorCallback());
338 device->DisconnectGatt();
339 EXPECT_EQ(1, gatt_connection_attempt_count_);
340 EXPECT_EQ(1, gatt_disconnection_attempt_count_);
341 CompleteGattDisconnection(device);
342 EXPECT_EQ(0, callback_count_);
343 EXPECT_EQ(1, error_callback_count_);
344 EXPECT_EQ(BluetoothDevice::ERROR_FAILED, last_connect_error_code_);
345 for (BluetoothGattConnection* connection : gatt_connections_)
346 EXPECT_FALSE(connection->IsConnected());
347
348 // CreateGattConnection, but error connecting. Also, multiple errors only
349 // invoke callbacks once:
350 callback_count_ = error_callback_count_ = 0;
351 device->CreateGattConnection(GetGattConnectionCallback(),
352 GetConnectErrorCallback());
353 EXPECT_EQ(2, gatt_connection_attempt_count_);
354 FailGattConnection(device, BluetoothDevice::ERROR_AUTH_FAILED);
355 FailGattConnection(device, BluetoothDevice::ERROR_FAILED);
356 EXPECT_EQ(0, callback_count_);
357 EXPECT_EQ(1, error_callback_count_);
358 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_FAILED, last_connect_error_code_);
359 for (BluetoothGattConnection* connection : gatt_connections_)
360 EXPECT_FALSE(connection->IsConnected());
361 }
362 #endif // defined(OS_ANDROID)
363
128 } // namespace device 364 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698