Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 Loading... | |
| 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 CompleteGattConnection(device); | |
| 175 EXPECT_EQ(1, callback_count_); | |
| 176 EXPECT_EQ(0, error_callback_count_); | |
| 177 ASSERT_EQ(1u, gatt_connections_.size()); | |
| 178 EXPECT_TRUE(device->IsGattConnected()); | |
| 179 EXPECT_TRUE(gatt_connections_[0]->IsConnected()); | |
| 180 | |
| 181 // Connect again once already connected. | |
| 182 device->CreateGattConnection(GetGattConnectionCallback(), | |
| 183 GetConnectErrorCallback()); | |
| 184 device->CreateGattConnection(GetGattConnectionCallback(), | |
| 185 GetConnectErrorCallback()); | |
| 186 EXPECT_EQ(3, callback_count_); | |
| 187 EXPECT_EQ(0, error_callback_count_); | |
| 188 ASSERT_EQ(3u, gatt_connections_.size()); | |
| 189 | |
| 190 // Test GetDeviceAddress | |
| 191 EXPECT_EQ(device_address, gatt_connections_[0]->GetDeviceAddress()); | |
| 192 | |
| 193 // Test IsConnected | |
| 194 EXPECT_TRUE(gatt_connections_[0]->IsConnected()); | |
| 195 EXPECT_TRUE(gatt_connections_[1]->IsConnected()); | |
| 196 EXPECT_TRUE(gatt_connections_[2]->IsConnected()); | |
| 197 | |
| 198 // Disconnect & Delete connection objects. Device stays connected. | |
| 199 gatt_connections_[0]->Disconnect(); // Disconnect first. | |
| 200 gatt_connections_.pop_back(); // Delete last. | |
| 201 EXPECT_FALSE(gatt_connections_[0]->IsConnected()); | |
| 202 EXPECT_TRUE(gatt_connections_[1]->IsConnected()); | |
| 203 EXPECT_TRUE(device->IsGattConnected()); | |
| 204 | |
| 205 // Delete device, connection objects should all be disconnected. | |
| 206 DeleteDevice(device); | |
| 207 EXPECT_FALSE(gatt_connections_[0]->IsConnected()); | |
| 208 EXPECT_FALSE(gatt_connections_[1]->IsConnected()); | |
| 209 | |
| 210 // Test GetDeviceAddress after device deleted. | |
| 211 EXPECT_EQ(device_address, gatt_connections_[0]->GetDeviceAddress()); | |
| 212 EXPECT_EQ(device_address, gatt_connections_[1]->GetDeviceAddress()); | |
| 213 } | |
| 214 #endif // defined(OS_ANDROID) | |
| 215 | |
| 216 #if defined(OS_ANDROID) | |
| 217 // BluetoothGattConnection with several connect / disconnects. | |
| 218 TEST_F(BluetoothTest, BluetoothGattConnection_ConnectDisconnect) { | |
| 219 InitWithFakeAdapter(); | |
| 220 TestBluetoothAdapterObserver observer(adapter_); | |
| 221 | |
| 222 // Get a device. | |
| 223 adapter_->StartDiscoverySession(GetDiscoverySessionCallback(), | |
| 224 GetErrorCallback()); | |
| 225 base::RunLoop().RunUntilIdle(); | |
| 226 DiscoverLowEnergyDevice(3); | |
| 227 base::RunLoop().RunUntilIdle(); | |
| 228 BluetoothDevice* device = observer.last_device(); | |
| 229 | |
| 230 // CreateGattConnection, & multiple connection s from platform only invoke | |
|
Jeffrey Yasskin
2015/09/16 23:42:49
sp: connection s
scheib
2015/09/20 02:53:35
Done.
| |
| 231 // callbacks once: | |
| 232 callback_count_ = error_callback_count_ = 0; | |
| 233 device->CreateGattConnection(GetGattConnectionCallback(), | |
| 234 GetConnectErrorCallback()); | |
| 235 CompleteGattConnection(device); | |
| 236 CompleteGattConnection(device); | |
| 237 EXPECT_EQ(1, callback_count_); | |
| 238 EXPECT_EQ(0, error_callback_count_); | |
| 239 | |
| 240 // Become disconnected: | |
| 241 CompleteGattDisconnection(device); | |
| 242 EXPECT_EQ(1, callback_count_); | |
| 243 EXPECT_EQ(0, error_callback_count_); | |
| 244 EXPECT_FALSE(gatt_connections_[0]->IsConnected()); | |
| 245 | |
| 246 // Be already connected, then CreateGattConnection: | |
| 247 callback_count_ = error_callback_count_ = 0; | |
| 248 CompleteGattConnection(device); | |
| 249 device->CreateGattConnection(GetGattConnectionCallback(), | |
| 250 GetConnectErrorCallback()); | |
| 251 EXPECT_EQ(1, callback_count_); | |
|
Jeffrey Yasskin
2015/09/16 23:42:49
Please add:
EXPECT_FALSE(gatt_connections_[0]->Is
scheib
2015/09/20 02:53:35
Done.
| |
| 252 EXPECT_EQ(0, error_callback_count_); | |
| 253 | |
| 254 // Disconnect all CreateGattConnection objects. But, don't yet simulate | |
|
Jeffrey Yasskin
2015/09/16 23:42:49
Please also add a test in which the last connectio
scheib
2015/09/20 02:53:35
Done.
| |
| 255 // the device disconnecting. | |
| 256 callback_count_ = error_callback_count_ = 0; | |
| 257 for (auto connection : gatt_connections_) | |
|
Jeffrey Yasskin
2015/09/16 23:42:49
Can you write BluetoothGattConnection* for these f
scheib
2015/09/20 02:53:35
Done.
| |
| 258 connection->Disconnect(); | |
|
Jeffrey Yasskin
2015/09/16 23:42:49
Can you check that the implementation appropriatel
scheib
2015/09/20 02:53:35
Done.
| |
| 259 device->CreateGattConnection(GetGattConnectionCallback(), | |
| 260 GetConnectErrorCallback()); | |
| 261 EXPECT_EQ(1, callback_count_); // Device is assumed still connected. | |
|
Jeffrey Yasskin
2015/09/16 23:42:49
Check that gatt_connections_.back()->IsConnected()
scheib
2015/09/20 02:53:35
Done.
| |
| 262 EXPECT_EQ(0, error_callback_count_); | |
| 263 callback_count_ = error_callback_count_ = 0; | |
| 264 | |
| 265 // Actually disconnect: | |
| 266 CompleteGattDisconnection(device); | |
| 267 EXPECT_EQ(0, callback_count_); | |
| 268 EXPECT_EQ(0, error_callback_count_); | |
| 269 for (auto connection : gatt_connections_) | |
| 270 EXPECT_FALSE(connection->IsConnected()); | |
|
Jeffrey Yasskin
2015/09/16 23:42:49
These were already false before the CompleteGattDi
scheib
2015/09/20 02:53:35
The last connection reports connected. (It connect
| |
| 271 | |
| 272 // CreateGattConnection, but receive notice that device disconnected before | |
| 273 // it ever connects: | |
| 274 callback_count_ = error_callback_count_ = 0; | |
| 275 device->CreateGattConnection(GetGattConnectionCallback(), | |
| 276 GetConnectErrorCallback()); | |
| 277 CompleteGattDisconnection(device); | |
|
Jeffrey Yasskin
2015/09/16 23:42:49
Does this happen naturally if you call device->Dis
scheib
2015/09/20 02:53:34
I was planning on not failing a connect until the
Jeffrey Yasskin
2015/09/22 01:22:10
Right, that's fine. Calls to CompleteGattDisconnec
scheib
2015/09/22 18:21:48
I've added 2 more test blocks:
device->CreateGattC
| |
| 278 EXPECT_EQ(0, callback_count_); | |
| 279 EXPECT_EQ(1, error_callback_count_); | |
| 280 EXPECT_EQ(BluetoothDevice::ERROR_FAILED, last_connect_error_code_); | |
| 281 for (auto connection : gatt_connections_) | |
| 282 EXPECT_FALSE(connection->IsConnected()); | |
| 283 | |
| 284 // CreateGattConnection, but error connecting. Also, Multiple errors only | |
|
Jeffrey Yasskin
2015/09/16 23:42:49
s/Multiple/multiple/
scheib
2015/09/20 02:53:35
Done.
| |
| 285 // invoke callbacks once: | |
| 286 callback_count_ = error_callback_count_ = 0; | |
| 287 device->CreateGattConnection(GetGattConnectionCallback(), | |
| 288 GetConnectErrorCallback()); | |
| 289 FailGattConnection(device, BluetoothDevice::ERROR_AUTH_FAILED); | |
| 290 FailGattConnection(device, BluetoothDevice::ERROR_FAILED); | |
| 291 EXPECT_EQ(0, callback_count_); | |
| 292 EXPECT_EQ(1, error_callback_count_); | |
| 293 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_FAILED, last_connect_error_code_); | |
| 294 for (auto connection : gatt_connections_) | |
| 295 EXPECT_FALSE(connection->IsConnected()); | |
| 296 } | |
| 297 #endif // defined(OS_ANDROID) | |
| 298 | |
| 128 } // namespace device | 299 } // namespace device |
| OLD | NEW |