| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/bluez/bluetooth_socket_bluez.h" | 5 #include "device/bluetooth/bluez/bluetooth_socket_bluez.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
| 11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
| 12 #include "base/run_loop.h" |
| 12 #include "device/bluetooth/bluetooth_adapter.h" | 13 #include "device/bluetooth/bluetooth_adapter.h" |
| 13 #include "device/bluetooth/bluetooth_adapter_factory.h" | 14 #include "device/bluetooth/bluetooth_adapter_factory.h" |
| 14 #include "device/bluetooth/bluetooth_device.h" | 15 #include "device/bluetooth/bluetooth_device.h" |
| 15 #include "device/bluetooth/bluetooth_socket.h" | 16 #include "device/bluetooth/bluetooth_socket.h" |
| 16 #include "device/bluetooth/bluetooth_socket_thread.h" | 17 #include "device/bluetooth/bluetooth_socket_thread.h" |
| 17 #include "device/bluetooth/bluetooth_uuid.h" | 18 #include "device/bluetooth/bluetooth_uuid.h" |
| 18 #include "device/bluetooth/bluez/bluetooth_adapter_bluez.h" | 19 #include "device/bluetooth/bluez/bluetooth_adapter_bluez.h" |
| 19 #include "device/bluetooth/bluez/bluetooth_device_bluez.h" | 20 #include "device/bluetooth/bluez/bluetooth_device_bluez.h" |
| 20 #include "device/bluetooth/dbus/bluez_dbus_manager.h" | 21 #include "device/bluetooth/dbus/bluez_dbus_manager.h" |
| 21 #include "device/bluetooth/dbus/fake_bluetooth_adapter_client.h" | 22 #include "device/bluetooth/dbus/fake_bluetooth_adapter_client.h" |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 dbus_setter->SetBluetoothProfileManagerClient( | 76 dbus_setter->SetBluetoothProfileManagerClient( |
| 76 std::unique_ptr<bluez::BluetoothProfileManagerClient>( | 77 std::unique_ptr<bluez::BluetoothProfileManagerClient>( |
| 77 new bluez::FakeBluetoothProfileManagerClient)); | 78 new bluez::FakeBluetoothProfileManagerClient)); |
| 78 | 79 |
| 79 BluetoothSocketThread::Get(); | 80 BluetoothSocketThread::Get(); |
| 80 | 81 |
| 81 // Grab a pointer to the adapter. | 82 // Grab a pointer to the adapter. |
| 82 device::BluetoothAdapterFactory::GetAdapter(base::Bind( | 83 device::BluetoothAdapterFactory::GetAdapter(base::Bind( |
| 83 &BluetoothSocketBlueZTest::AdapterCallback, base::Unretained(this))); | 84 &BluetoothSocketBlueZTest::AdapterCallback, base::Unretained(this))); |
| 84 | 85 |
| 85 base::MessageLoop::current()->Run(); | 86 base::RunLoop().Run(); |
| 86 | 87 |
| 87 ASSERT_TRUE(adapter_.get() != nullptr); | 88 ASSERT_TRUE(adapter_.get() != nullptr); |
| 88 ASSERT_TRUE(adapter_->IsInitialized()); | 89 ASSERT_TRUE(adapter_->IsInitialized()); |
| 89 ASSERT_TRUE(adapter_->IsPresent()); | 90 ASSERT_TRUE(adapter_->IsPresent()); |
| 90 | 91 |
| 91 // Turn on the adapter. | 92 // Turn on the adapter. |
| 92 adapter_->SetPowered(true, base::Bind(&base::DoNothing), | 93 adapter_->SetPowered(true, base::Bind(&base::DoNothing), |
| 93 base::Bind(&base::DoNothing)); | 94 base::Bind(&base::DoNothing)); |
| 94 ASSERT_TRUE(adapter_->IsPowered()); | 95 ASSERT_TRUE(adapter_->IsPowered()); |
| 95 } | 96 } |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 193 BluetoothDevice* device = adapter_->GetDevice( | 194 BluetoothDevice* device = adapter_->GetDevice( |
| 194 bluez::FakeBluetoothDeviceClient::kPairedDeviceAddress); | 195 bluez::FakeBluetoothDeviceClient::kPairedDeviceAddress); |
| 195 ASSERT_TRUE(device != nullptr); | 196 ASSERT_TRUE(device != nullptr); |
| 196 | 197 |
| 197 device->ConnectToService( | 198 device->ConnectToService( |
| 198 BluetoothUUID(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid), | 199 BluetoothUUID(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid), |
| 199 base::Bind(&BluetoothSocketBlueZTest::ConnectToServiceSuccessCallback, | 200 base::Bind(&BluetoothSocketBlueZTest::ConnectToServiceSuccessCallback, |
| 200 base::Unretained(this)), | 201 base::Unretained(this)), |
| 201 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback, | 202 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback, |
| 202 base::Unretained(this))); | 203 base::Unretained(this))); |
| 203 message_loop_.Run(); | 204 base::RunLoop().Run(); |
| 204 | 205 |
| 205 EXPECT_EQ(1U, success_callback_count_); | 206 EXPECT_EQ(1U, success_callback_count_); |
| 206 EXPECT_EQ(0U, error_callback_count_); | 207 EXPECT_EQ(0U, error_callback_count_); |
| 207 EXPECT_TRUE(last_socket_.get() != nullptr); | 208 EXPECT_TRUE(last_socket_.get() != nullptr); |
| 208 | 209 |
| 209 // Take ownership of the socket for the remainder of the test. | 210 // Take ownership of the socket for the remainder of the test. |
| 210 scoped_refptr<BluetoothSocket> socket = last_socket_; | 211 scoped_refptr<BluetoothSocket> socket = last_socket_; |
| 211 last_socket_ = nullptr; | 212 last_socket_ = nullptr; |
| 212 success_callback_count_ = 0; | 213 success_callback_count_ = 0; |
| 213 error_callback_count_ = 0; | 214 error_callback_count_ = 0; |
| 214 | 215 |
| 215 // Send data to the socket, expect all of the data to be sent. | 216 // Send data to the socket, expect all of the data to be sent. |
| 216 scoped_refptr<net::StringIOBuffer> write_buffer( | 217 scoped_refptr<net::StringIOBuffer> write_buffer( |
| 217 new net::StringIOBuffer("test")); | 218 new net::StringIOBuffer("test")); |
| 218 | 219 |
| 219 socket->Send(write_buffer.get(), write_buffer->size(), | 220 socket->Send(write_buffer.get(), write_buffer->size(), |
| 220 base::Bind(&BluetoothSocketBlueZTest::SendSuccessCallback, | 221 base::Bind(&BluetoothSocketBlueZTest::SendSuccessCallback, |
| 221 base::Unretained(this)), | 222 base::Unretained(this)), |
| 222 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback, | 223 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback, |
| 223 base::Unretained(this))); | 224 base::Unretained(this))); |
| 224 message_loop_.Run(); | 225 base::RunLoop().Run(); |
| 225 | 226 |
| 226 EXPECT_EQ(1U, success_callback_count_); | 227 EXPECT_EQ(1U, success_callback_count_); |
| 227 EXPECT_EQ(0U, error_callback_count_); | 228 EXPECT_EQ(0U, error_callback_count_); |
| 228 EXPECT_EQ(last_bytes_sent_, write_buffer->size()); | 229 EXPECT_EQ(last_bytes_sent_, write_buffer->size()); |
| 229 | 230 |
| 230 success_callback_count_ = 0; | 231 success_callback_count_ = 0; |
| 231 error_callback_count_ = 0; | 232 error_callback_count_ = 0; |
| 232 | 233 |
| 233 // Receive data from the socket, and fetch the buffer from the callback; since | 234 // Receive data from the socket, and fetch the buffer from the callback; since |
| 234 // the fake is an echo server, we expect to receive what we wrote. | 235 // the fake is an echo server, we expect to receive what we wrote. |
| 235 socket->Receive(4096, | 236 socket->Receive(4096, |
| 236 base::Bind(&BluetoothSocketBlueZTest::ReceiveSuccessCallback, | 237 base::Bind(&BluetoothSocketBlueZTest::ReceiveSuccessCallback, |
| 237 base::Unretained(this)), | 238 base::Unretained(this)), |
| 238 base::Bind(&BluetoothSocketBlueZTest::ReceiveErrorCallback, | 239 base::Bind(&BluetoothSocketBlueZTest::ReceiveErrorCallback, |
| 239 base::Unretained(this))); | 240 base::Unretained(this))); |
| 240 message_loop_.Run(); | 241 base::RunLoop().Run(); |
| 241 | 242 |
| 242 EXPECT_EQ(1U, success_callback_count_); | 243 EXPECT_EQ(1U, success_callback_count_); |
| 243 EXPECT_EQ(0U, error_callback_count_); | 244 EXPECT_EQ(0U, error_callback_count_); |
| 244 EXPECT_EQ(4, last_bytes_received_); | 245 EXPECT_EQ(4, last_bytes_received_); |
| 245 EXPECT_TRUE(last_io_buffer_.get() != nullptr); | 246 EXPECT_TRUE(last_io_buffer_.get() != nullptr); |
| 246 | 247 |
| 247 // Take ownership of the received buffer. | 248 // Take ownership of the received buffer. |
| 248 scoped_refptr<net::IOBuffer> read_buffer = last_io_buffer_; | 249 scoped_refptr<net::IOBuffer> read_buffer = last_io_buffer_; |
| 249 last_io_buffer_ = nullptr; | 250 last_io_buffer_ = nullptr; |
| 250 success_callback_count_ = 0; | 251 success_callback_count_ = 0; |
| 251 error_callback_count_ = 0; | 252 error_callback_count_ = 0; |
| 252 | 253 |
| 253 std::string data = std::string(read_buffer->data(), last_bytes_received_); | 254 std::string data = std::string(read_buffer->data(), last_bytes_received_); |
| 254 EXPECT_EQ("test", data); | 255 EXPECT_EQ("test", data); |
| 255 | 256 |
| 256 read_buffer = nullptr; | 257 read_buffer = nullptr; |
| 257 | 258 |
| 258 // Receive data again; the socket will have been closed, this should cause a | 259 // Receive data again; the socket will have been closed, this should cause a |
| 259 // disconnected error to be returned via the error callback. | 260 // disconnected error to be returned via the error callback. |
| 260 socket->Receive(4096, | 261 socket->Receive(4096, |
| 261 base::Bind(&BluetoothSocketBlueZTest::ReceiveSuccessCallback, | 262 base::Bind(&BluetoothSocketBlueZTest::ReceiveSuccessCallback, |
| 262 base::Unretained(this)), | 263 base::Unretained(this)), |
| 263 base::Bind(&BluetoothSocketBlueZTest::ReceiveErrorCallback, | 264 base::Bind(&BluetoothSocketBlueZTest::ReceiveErrorCallback, |
| 264 base::Unretained(this))); | 265 base::Unretained(this))); |
| 265 message_loop_.Run(); | 266 base::RunLoop().Run(); |
| 266 | 267 |
| 267 EXPECT_EQ(0U, success_callback_count_); | 268 EXPECT_EQ(0U, success_callback_count_); |
| 268 EXPECT_EQ(1U, error_callback_count_); | 269 EXPECT_EQ(1U, error_callback_count_); |
| 269 EXPECT_EQ(BluetoothSocket::kDisconnected, last_reason_); | 270 EXPECT_EQ(BluetoothSocket::kDisconnected, last_reason_); |
| 270 EXPECT_EQ(net::ErrorToString(net::OK), last_message_); | 271 EXPECT_EQ(net::ErrorToString(net::OK), last_message_); |
| 271 | 272 |
| 272 success_callback_count_ = 0; | 273 success_callback_count_ = 0; |
| 273 error_callback_count_ = 0; | 274 error_callback_count_ = 0; |
| 274 | 275 |
| 275 // Send data again; since the socket is closed we should get a system error | 276 // Send data again; since the socket is closed we should get a system error |
| 276 // equivalent to the connection reset error. | 277 // equivalent to the connection reset error. |
| 277 write_buffer = new net::StringIOBuffer("second test"); | 278 write_buffer = new net::StringIOBuffer("second test"); |
| 278 | 279 |
| 279 socket->Send(write_buffer.get(), write_buffer->size(), | 280 socket->Send(write_buffer.get(), write_buffer->size(), |
| 280 base::Bind(&BluetoothSocketBlueZTest::SendSuccessCallback, | 281 base::Bind(&BluetoothSocketBlueZTest::SendSuccessCallback, |
| 281 base::Unretained(this)), | 282 base::Unretained(this)), |
| 282 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback, | 283 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback, |
| 283 base::Unretained(this))); | 284 base::Unretained(this))); |
| 284 message_loop_.Run(); | 285 base::RunLoop().Run(); |
| 285 | 286 |
| 286 EXPECT_EQ(0U, success_callback_count_); | 287 EXPECT_EQ(0U, success_callback_count_); |
| 287 EXPECT_EQ(1U, error_callback_count_); | 288 EXPECT_EQ(1U, error_callback_count_); |
| 288 EXPECT_EQ(net::ErrorToString(net::ERR_CONNECTION_RESET), last_message_); | 289 EXPECT_EQ(net::ErrorToString(net::ERR_CONNECTION_RESET), last_message_); |
| 289 | 290 |
| 290 success_callback_count_ = 0; | 291 success_callback_count_ = 0; |
| 291 error_callback_count_ = 0; | 292 error_callback_count_ = 0; |
| 292 | 293 |
| 293 // Close our end of the socket. | 294 // Close our end of the socket. |
| 294 socket->Disconnect(base::Bind(&BluetoothSocketBlueZTest::SuccessCallback, | 295 socket->Disconnect(base::Bind(&BluetoothSocketBlueZTest::SuccessCallback, |
| 295 base::Unretained(this))); | 296 base::Unretained(this))); |
| 296 | 297 |
| 297 message_loop_.Run(); | 298 base::RunLoop().Run(); |
| 298 EXPECT_EQ(1U, success_callback_count_); | 299 EXPECT_EQ(1U, success_callback_count_); |
| 299 } | 300 } |
| 300 | 301 |
| 301 TEST_F(BluetoothSocketBlueZTest, Listen) { | 302 TEST_F(BluetoothSocketBlueZTest, Listen) { |
| 302 adapter_->CreateRfcommService( | 303 adapter_->CreateRfcommService( |
| 303 BluetoothUUID(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid), | 304 BluetoothUUID(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid), |
| 304 BluetoothAdapter::ServiceOptions(), | 305 BluetoothAdapter::ServiceOptions(), |
| 305 base::Bind(&BluetoothSocketBlueZTest::CreateServiceSuccessCallback, | 306 base::Bind(&BluetoothSocketBlueZTest::CreateServiceSuccessCallback, |
| 306 base::Unretained(this)), | 307 base::Unretained(this)), |
| 307 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback, | 308 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback, |
| 308 base::Unretained(this))); | 309 base::Unretained(this))); |
| 309 | 310 |
| 310 message_loop_.Run(); | 311 base::RunLoop().Run(); |
| 311 | 312 |
| 312 EXPECT_EQ(1U, success_callback_count_); | 313 EXPECT_EQ(1U, success_callback_count_); |
| 313 EXPECT_EQ(0U, error_callback_count_); | 314 EXPECT_EQ(0U, error_callback_count_); |
| 314 EXPECT_TRUE(last_socket_.get() != nullptr); | 315 EXPECT_TRUE(last_socket_.get() != nullptr); |
| 315 | 316 |
| 316 // Take ownership of the socket for the remainder of the test. | 317 // Take ownership of the socket for the remainder of the test. |
| 317 scoped_refptr<BluetoothSocket> server_socket = last_socket_; | 318 scoped_refptr<BluetoothSocket> server_socket = last_socket_; |
| 318 last_socket_ = nullptr; | 319 last_socket_ = nullptr; |
| 319 success_callback_count_ = 0; | 320 success_callback_count_ = 0; |
| 320 error_callback_count_ = 0; | 321 error_callback_count_ = 0; |
| 321 | 322 |
| 322 // Simulate an incoming connection by just calling the ConnectProfile method | 323 // Simulate an incoming connection by just calling the ConnectProfile method |
| 323 // of the underlying fake device client (from the BlueZ point of view, | 324 // of the underlying fake device client (from the BlueZ point of view, |
| 324 // outgoing and incoming look the same). | 325 // outgoing and incoming look the same). |
| 325 // | 326 // |
| 326 // This is done before the Accept() call to simulate a pending call at the | 327 // This is done before the Accept() call to simulate a pending call at the |
| 327 // point that Accept() is called. | 328 // point that Accept() is called. |
| 328 bluez::FakeBluetoothDeviceClient* fake_bluetooth_device_client = | 329 bluez::FakeBluetoothDeviceClient* fake_bluetooth_device_client = |
| 329 static_cast<bluez::FakeBluetoothDeviceClient*>( | 330 static_cast<bluez::FakeBluetoothDeviceClient*>( |
| 330 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()); | 331 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()); |
| 331 BluetoothDevice* device = adapter_->GetDevice( | 332 BluetoothDevice* device = adapter_->GetDevice( |
| 332 bluez::FakeBluetoothDeviceClient::kPairedDeviceAddress); | 333 bluez::FakeBluetoothDeviceClient::kPairedDeviceAddress); |
| 333 ASSERT_TRUE(device != nullptr); | 334 ASSERT_TRUE(device != nullptr); |
| 334 fake_bluetooth_device_client->ConnectProfile( | 335 fake_bluetooth_device_client->ConnectProfile( |
| 335 static_cast<BluetoothDeviceBlueZ*>(device)->object_path(), | 336 static_cast<BluetoothDeviceBlueZ*>(device)->object_path(), |
| 336 bluez::FakeBluetoothProfileManagerClient::kRfcommUuid, | 337 bluez::FakeBluetoothProfileManagerClient::kRfcommUuid, |
| 337 base::Bind(&base::DoNothing), base::Bind(&DoNothingDBusErrorCallback)); | 338 base::Bind(&base::DoNothing), base::Bind(&DoNothingDBusErrorCallback)); |
| 338 | 339 |
| 339 message_loop_.RunUntilIdle(); | 340 base::RunLoop().RunUntilIdle(); |
| 340 | 341 |
| 341 server_socket->Accept( | 342 server_socket->Accept( |
| 342 base::Bind(&BluetoothSocketBlueZTest::AcceptSuccessCallback, | 343 base::Bind(&BluetoothSocketBlueZTest::AcceptSuccessCallback, |
| 343 base::Unretained(this)), | 344 base::Unretained(this)), |
| 344 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback, | 345 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback, |
| 345 base::Unretained(this))); | 346 base::Unretained(this))); |
| 346 | 347 |
| 347 message_loop_.Run(); | 348 base::RunLoop().Run(); |
| 348 | 349 |
| 349 EXPECT_EQ(1U, success_callback_count_); | 350 EXPECT_EQ(1U, success_callback_count_); |
| 350 EXPECT_EQ(0U, error_callback_count_); | 351 EXPECT_EQ(0U, error_callback_count_); |
| 351 EXPECT_TRUE(last_socket_.get() != nullptr); | 352 EXPECT_TRUE(last_socket_.get() != nullptr); |
| 352 | 353 |
| 353 // Take ownership of the client socket for the remainder of the test. | 354 // Take ownership of the client socket for the remainder of the test. |
| 354 scoped_refptr<BluetoothSocket> client_socket = last_socket_; | 355 scoped_refptr<BluetoothSocket> client_socket = last_socket_; |
| 355 last_socket_ = nullptr; | 356 last_socket_ = nullptr; |
| 356 success_callback_count_ = 0; | 357 success_callback_count_ = 0; |
| 357 error_callback_count_ = 0; | 358 error_callback_count_ = 0; |
| 358 | 359 |
| 359 // Close our end of the client socket. | 360 // Close our end of the client socket. |
| 360 client_socket->Disconnect(base::Bind( | 361 client_socket->Disconnect(base::Bind( |
| 361 &BluetoothSocketBlueZTest::SuccessCallback, base::Unretained(this))); | 362 &BluetoothSocketBlueZTest::SuccessCallback, base::Unretained(this))); |
| 362 | 363 |
| 363 message_loop_.Run(); | 364 base::RunLoop().Run(); |
| 364 | 365 |
| 365 EXPECT_EQ(1U, success_callback_count_); | 366 EXPECT_EQ(1U, success_callback_count_); |
| 366 client_socket = nullptr; | 367 client_socket = nullptr; |
| 367 success_callback_count_ = 0; | 368 success_callback_count_ = 0; |
| 368 error_callback_count_ = 0; | 369 error_callback_count_ = 0; |
| 369 | 370 |
| 370 // Run a second connection test, this time calling Accept() before the | 371 // Run a second connection test, this time calling Accept() before the |
| 371 // incoming connection comes in. | 372 // incoming connection comes in. |
| 372 server_socket->Accept( | 373 server_socket->Accept( |
| 373 base::Bind(&BluetoothSocketBlueZTest::AcceptSuccessCallback, | 374 base::Bind(&BluetoothSocketBlueZTest::AcceptSuccessCallback, |
| 374 base::Unretained(this)), | 375 base::Unretained(this)), |
| 375 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback, | 376 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback, |
| 376 base::Unretained(this))); | 377 base::Unretained(this))); |
| 377 | 378 |
| 378 message_loop_.RunUntilIdle(); | 379 base::RunLoop().RunUntilIdle(); |
| 379 | 380 |
| 380 fake_bluetooth_device_client->ConnectProfile( | 381 fake_bluetooth_device_client->ConnectProfile( |
| 381 static_cast<BluetoothDeviceBlueZ*>(device)->object_path(), | 382 static_cast<BluetoothDeviceBlueZ*>(device)->object_path(), |
| 382 bluez::FakeBluetoothProfileManagerClient::kRfcommUuid, | 383 bluez::FakeBluetoothProfileManagerClient::kRfcommUuid, |
| 383 base::Bind(&base::DoNothing), base::Bind(&DoNothingDBusErrorCallback)); | 384 base::Bind(&base::DoNothing), base::Bind(&DoNothingDBusErrorCallback)); |
| 384 | 385 |
| 385 message_loop_.Run(); | 386 base::RunLoop().Run(); |
| 386 | 387 |
| 387 EXPECT_EQ(1U, success_callback_count_); | 388 EXPECT_EQ(1U, success_callback_count_); |
| 388 EXPECT_EQ(0U, error_callback_count_); | 389 EXPECT_EQ(0U, error_callback_count_); |
| 389 EXPECT_TRUE(last_socket_.get() != nullptr); | 390 EXPECT_TRUE(last_socket_.get() != nullptr); |
| 390 | 391 |
| 391 // Take ownership of the client socket for the remainder of the test. | 392 // Take ownership of the client socket for the remainder of the test. |
| 392 client_socket = last_socket_; | 393 client_socket = last_socket_; |
| 393 last_socket_ = nullptr; | 394 last_socket_ = nullptr; |
| 394 success_callback_count_ = 0; | 395 success_callback_count_ = 0; |
| 395 error_callback_count_ = 0; | 396 error_callback_count_ = 0; |
| 396 | 397 |
| 397 // Close our end of the client socket. | 398 // Close our end of the client socket. |
| 398 client_socket->Disconnect(base::Bind( | 399 client_socket->Disconnect(base::Bind( |
| 399 &BluetoothSocketBlueZTest::SuccessCallback, base::Unretained(this))); | 400 &BluetoothSocketBlueZTest::SuccessCallback, base::Unretained(this))); |
| 400 | 401 |
| 401 message_loop_.Run(); | 402 base::RunLoop().Run(); |
| 402 | 403 |
| 403 EXPECT_EQ(1U, success_callback_count_); | 404 EXPECT_EQ(1U, success_callback_count_); |
| 404 client_socket = nullptr; | 405 client_socket = nullptr; |
| 405 success_callback_count_ = 0; | 406 success_callback_count_ = 0; |
| 406 error_callback_count_ = 0; | 407 error_callback_count_ = 0; |
| 407 | 408 |
| 408 // Now close the server socket. | 409 // Now close the server socket. |
| 409 server_socket->Disconnect( | 410 server_socket->Disconnect( |
| 410 base::Bind(&BluetoothSocketBlueZTest::ImmediateSuccessCallback, | 411 base::Bind(&BluetoothSocketBlueZTest::ImmediateSuccessCallback, |
| 411 base::Unretained(this))); | 412 base::Unretained(this))); |
| 412 | 413 |
| 413 message_loop_.RunUntilIdle(); | 414 base::RunLoop().RunUntilIdle(); |
| 414 | 415 |
| 415 EXPECT_EQ(1U, success_callback_count_); | 416 EXPECT_EQ(1U, success_callback_count_); |
| 416 } | 417 } |
| 417 | 418 |
| 418 TEST_F(BluetoothSocketBlueZTest, ListenBeforeAdapterStart) { | 419 TEST_F(BluetoothSocketBlueZTest, ListenBeforeAdapterStart) { |
| 419 // Start off with an invisible adapter, register the profile, then make | 420 // Start off with an invisible adapter, register the profile, then make |
| 420 // the adapter visible. | 421 // the adapter visible. |
| 421 bluez::FakeBluetoothAdapterClient* fake_bluetooth_adapter_client = | 422 bluez::FakeBluetoothAdapterClient* fake_bluetooth_adapter_client = |
| 422 static_cast<bluez::FakeBluetoothAdapterClient*>( | 423 static_cast<bluez::FakeBluetoothAdapterClient*>( |
| 423 bluez::BluezDBusManager::Get()->GetBluetoothAdapterClient()); | 424 bluez::BluezDBusManager::Get()->GetBluetoothAdapterClient()); |
| 424 fake_bluetooth_adapter_client->SetVisible(false); | 425 fake_bluetooth_adapter_client->SetVisible(false); |
| 425 | 426 |
| 426 adapter_->CreateRfcommService( | 427 adapter_->CreateRfcommService( |
| 427 BluetoothUUID(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid), | 428 BluetoothUUID(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid), |
| 428 BluetoothAdapter::ServiceOptions(), | 429 BluetoothAdapter::ServiceOptions(), |
| 429 base::Bind(&BluetoothSocketBlueZTest::CreateServiceSuccessCallback, | 430 base::Bind(&BluetoothSocketBlueZTest::CreateServiceSuccessCallback, |
| 430 base::Unretained(this)), | 431 base::Unretained(this)), |
| 431 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback, | 432 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback, |
| 432 base::Unretained(this))); | 433 base::Unretained(this))); |
| 433 message_loop_.Run(); | 434 base::RunLoop().Run(); |
| 434 | 435 |
| 435 EXPECT_EQ(1U, success_callback_count_); | 436 EXPECT_EQ(1U, success_callback_count_); |
| 436 EXPECT_EQ(0U, error_callback_count_); | 437 EXPECT_EQ(0U, error_callback_count_); |
| 437 EXPECT_TRUE(last_socket_.get() != nullptr); | 438 EXPECT_TRUE(last_socket_.get() != nullptr); |
| 438 | 439 |
| 439 // Take ownership of the socket for the remainder of the test. | 440 // Take ownership of the socket for the remainder of the test. |
| 440 scoped_refptr<BluetoothSocket> socket = last_socket_; | 441 scoped_refptr<BluetoothSocket> socket = last_socket_; |
| 441 last_socket_ = nullptr; | 442 last_socket_ = nullptr; |
| 442 success_callback_count_ = 0; | 443 success_callback_count_ = 0; |
| 443 error_callback_count_ = 0; | 444 error_callback_count_ = 0; |
| 444 | 445 |
| 445 // But there shouldn't be a profile registered yet. | 446 // But there shouldn't be a profile registered yet. |
| 446 bluez::FakeBluetoothProfileManagerClient* | 447 bluez::FakeBluetoothProfileManagerClient* |
| 447 fake_bluetooth_profile_manager_client = | 448 fake_bluetooth_profile_manager_client = |
| 448 static_cast<bluez::FakeBluetoothProfileManagerClient*>( | 449 static_cast<bluez::FakeBluetoothProfileManagerClient*>( |
| 449 bluez::BluezDBusManager::Get() | 450 bluez::BluezDBusManager::Get() |
| 450 ->GetBluetoothProfileManagerClient()); | 451 ->GetBluetoothProfileManagerClient()); |
| 451 bluez::FakeBluetoothProfileServiceProvider* profile_service_provider = | 452 bluez::FakeBluetoothProfileServiceProvider* profile_service_provider = |
| 452 fake_bluetooth_profile_manager_client->GetProfileServiceProvider( | 453 fake_bluetooth_profile_manager_client->GetProfileServiceProvider( |
| 453 bluez::FakeBluetoothProfileManagerClient::kRfcommUuid); | 454 bluez::FakeBluetoothProfileManagerClient::kRfcommUuid); |
| 454 EXPECT_TRUE(profile_service_provider == nullptr); | 455 EXPECT_TRUE(profile_service_provider == nullptr); |
| 455 | 456 |
| 456 // Make the adapter visible. This should register a profile. | 457 // Make the adapter visible. This should register a profile. |
| 457 fake_bluetooth_adapter_client->SetVisible(true); | 458 fake_bluetooth_adapter_client->SetVisible(true); |
| 458 | 459 |
| 459 message_loop_.RunUntilIdle(); | 460 base::RunLoop().RunUntilIdle(); |
| 460 | 461 |
| 461 profile_service_provider = | 462 profile_service_provider = |
| 462 fake_bluetooth_profile_manager_client->GetProfileServiceProvider( | 463 fake_bluetooth_profile_manager_client->GetProfileServiceProvider( |
| 463 bluez::FakeBluetoothProfileManagerClient::kRfcommUuid); | 464 bluez::FakeBluetoothProfileManagerClient::kRfcommUuid); |
| 464 EXPECT_TRUE(profile_service_provider != nullptr); | 465 EXPECT_TRUE(profile_service_provider != nullptr); |
| 465 | 466 |
| 466 // Cleanup the socket. | 467 // Cleanup the socket. |
| 467 socket->Disconnect( | 468 socket->Disconnect( |
| 468 base::Bind(&BluetoothSocketBlueZTest::ImmediateSuccessCallback, | 469 base::Bind(&BluetoothSocketBlueZTest::ImmediateSuccessCallback, |
| 469 base::Unretained(this))); | 470 base::Unretained(this))); |
| 470 | 471 |
| 471 message_loop_.RunUntilIdle(); | 472 base::RunLoop().RunUntilIdle(); |
| 472 | 473 |
| 473 EXPECT_EQ(1U, success_callback_count_); | 474 EXPECT_EQ(1U, success_callback_count_); |
| 474 } | 475 } |
| 475 | 476 |
| 476 TEST_F(BluetoothSocketBlueZTest, ListenAcrossAdapterRestart) { | 477 TEST_F(BluetoothSocketBlueZTest, ListenAcrossAdapterRestart) { |
| 477 // The fake adapter starts off visible by default. | 478 // The fake adapter starts off visible by default. |
| 478 bluez::FakeBluetoothAdapterClient* fake_bluetooth_adapter_client = | 479 bluez::FakeBluetoothAdapterClient* fake_bluetooth_adapter_client = |
| 479 static_cast<bluez::FakeBluetoothAdapterClient*>( | 480 static_cast<bluez::FakeBluetoothAdapterClient*>( |
| 480 bluez::BluezDBusManager::Get()->GetBluetoothAdapterClient()); | 481 bluez::BluezDBusManager::Get()->GetBluetoothAdapterClient()); |
| 481 | 482 |
| 482 adapter_->CreateRfcommService( | 483 adapter_->CreateRfcommService( |
| 483 BluetoothUUID(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid), | 484 BluetoothUUID(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid), |
| 484 BluetoothAdapter::ServiceOptions(), | 485 BluetoothAdapter::ServiceOptions(), |
| 485 base::Bind(&BluetoothSocketBlueZTest::CreateServiceSuccessCallback, | 486 base::Bind(&BluetoothSocketBlueZTest::CreateServiceSuccessCallback, |
| 486 base::Unretained(this)), | 487 base::Unretained(this)), |
| 487 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback, | 488 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback, |
| 488 base::Unretained(this))); | 489 base::Unretained(this))); |
| 489 message_loop_.Run(); | 490 base::RunLoop().Run(); |
| 490 | 491 |
| 491 EXPECT_EQ(1U, success_callback_count_); | 492 EXPECT_EQ(1U, success_callback_count_); |
| 492 EXPECT_EQ(0U, error_callback_count_); | 493 EXPECT_EQ(0U, error_callback_count_); |
| 493 EXPECT_TRUE(last_socket_.get() != nullptr); | 494 EXPECT_TRUE(last_socket_.get() != nullptr); |
| 494 | 495 |
| 495 // Take ownership of the socket for the remainder of the test. | 496 // Take ownership of the socket for the remainder of the test. |
| 496 scoped_refptr<BluetoothSocket> socket = last_socket_; | 497 scoped_refptr<BluetoothSocket> socket = last_socket_; |
| 497 last_socket_ = nullptr; | 498 last_socket_ = nullptr; |
| 498 success_callback_count_ = 0; | 499 success_callback_count_ = 0; |
| 499 error_callback_count_ = 0; | 500 error_callback_count_ = 0; |
| 500 | 501 |
| 501 // Make sure the profile was registered with the daemon. | 502 // Make sure the profile was registered with the daemon. |
| 502 bluez::FakeBluetoothProfileManagerClient* | 503 bluez::FakeBluetoothProfileManagerClient* |
| 503 fake_bluetooth_profile_manager_client = | 504 fake_bluetooth_profile_manager_client = |
| 504 static_cast<bluez::FakeBluetoothProfileManagerClient*>( | 505 static_cast<bluez::FakeBluetoothProfileManagerClient*>( |
| 505 bluez::BluezDBusManager::Get() | 506 bluez::BluezDBusManager::Get() |
| 506 ->GetBluetoothProfileManagerClient()); | 507 ->GetBluetoothProfileManagerClient()); |
| 507 bluez::FakeBluetoothProfileServiceProvider* profile_service_provider = | 508 bluez::FakeBluetoothProfileServiceProvider* profile_service_provider = |
| 508 fake_bluetooth_profile_manager_client->GetProfileServiceProvider( | 509 fake_bluetooth_profile_manager_client->GetProfileServiceProvider( |
| 509 bluez::FakeBluetoothProfileManagerClient::kRfcommUuid); | 510 bluez::FakeBluetoothProfileManagerClient::kRfcommUuid); |
| 510 EXPECT_TRUE(profile_service_provider != nullptr); | 511 EXPECT_TRUE(profile_service_provider != nullptr); |
| 511 | 512 |
| 512 // Make the adapter invisible, and fiddle with the profile fake to unregister | 513 // Make the adapter invisible, and fiddle with the profile fake to unregister |
| 513 // the profile since this doesn't happen automatically. | 514 // the profile since this doesn't happen automatically. |
| 514 fake_bluetooth_adapter_client->SetVisible(false); | 515 fake_bluetooth_adapter_client->SetVisible(false); |
| 515 | 516 |
| 516 message_loop_.RunUntilIdle(); | 517 base::RunLoop().RunUntilIdle(); |
| 517 | 518 |
| 518 // Then make the adapter visible again. This should re-register the profile. | 519 // Then make the adapter visible again. This should re-register the profile. |
| 519 fake_bluetooth_adapter_client->SetVisible(true); | 520 fake_bluetooth_adapter_client->SetVisible(true); |
| 520 | 521 |
| 521 message_loop_.RunUntilIdle(); | 522 base::RunLoop().RunUntilIdle(); |
| 522 | 523 |
| 523 profile_service_provider = | 524 profile_service_provider = |
| 524 fake_bluetooth_profile_manager_client->GetProfileServiceProvider( | 525 fake_bluetooth_profile_manager_client->GetProfileServiceProvider( |
| 525 bluez::FakeBluetoothProfileManagerClient::kRfcommUuid); | 526 bluez::FakeBluetoothProfileManagerClient::kRfcommUuid); |
| 526 EXPECT_TRUE(profile_service_provider != nullptr); | 527 EXPECT_TRUE(profile_service_provider != nullptr); |
| 527 | 528 |
| 528 // Cleanup the socket. | 529 // Cleanup the socket. |
| 529 socket->Disconnect( | 530 socket->Disconnect( |
| 530 base::Bind(&BluetoothSocketBlueZTest::ImmediateSuccessCallback, | 531 base::Bind(&BluetoothSocketBlueZTest::ImmediateSuccessCallback, |
| 531 base::Unretained(this))); | 532 base::Unretained(this))); |
| 532 | 533 |
| 533 message_loop_.RunUntilIdle(); | 534 base::RunLoop().RunUntilIdle(); |
| 534 | 535 |
| 535 EXPECT_EQ(1U, success_callback_count_); | 536 EXPECT_EQ(1U, success_callback_count_); |
| 536 } | 537 } |
| 537 | 538 |
| 538 TEST_F(BluetoothSocketBlueZTest, PairedConnectFails) { | 539 TEST_F(BluetoothSocketBlueZTest, PairedConnectFails) { |
| 539 BluetoothDevice* device = adapter_->GetDevice( | 540 BluetoothDevice* device = adapter_->GetDevice( |
| 540 bluez::FakeBluetoothDeviceClient::kPairedUnconnectableDeviceAddress); | 541 bluez::FakeBluetoothDeviceClient::kPairedUnconnectableDeviceAddress); |
| 541 ASSERT_TRUE(device != nullptr); | 542 ASSERT_TRUE(device != nullptr); |
| 542 | 543 |
| 543 device->ConnectToService( | 544 device->ConnectToService( |
| 544 BluetoothUUID(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid), | 545 BluetoothUUID(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid), |
| 545 base::Bind(&BluetoothSocketBlueZTest::ConnectToServiceSuccessCallback, | 546 base::Bind(&BluetoothSocketBlueZTest::ConnectToServiceSuccessCallback, |
| 546 base::Unretained(this)), | 547 base::Unretained(this)), |
| 547 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback, | 548 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback, |
| 548 base::Unretained(this))); | 549 base::Unretained(this))); |
| 549 message_loop_.Run(); | 550 base::RunLoop().Run(); |
| 550 | 551 |
| 551 EXPECT_EQ(0U, success_callback_count_); | 552 EXPECT_EQ(0U, success_callback_count_); |
| 552 EXPECT_EQ(1U, error_callback_count_); | 553 EXPECT_EQ(1U, error_callback_count_); |
| 553 EXPECT_TRUE(last_socket_.get() == nullptr); | 554 EXPECT_TRUE(last_socket_.get() == nullptr); |
| 554 | 555 |
| 555 device->ConnectToService( | 556 device->ConnectToService( |
| 556 BluetoothUUID(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid), | 557 BluetoothUUID(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid), |
| 557 base::Bind(&BluetoothSocketBlueZTest::ConnectToServiceSuccessCallback, | 558 base::Bind(&BluetoothSocketBlueZTest::ConnectToServiceSuccessCallback, |
| 558 base::Unretained(this)), | 559 base::Unretained(this)), |
| 559 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback, | 560 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback, |
| 560 base::Unretained(this))); | 561 base::Unretained(this))); |
| 561 message_loop_.Run(); | 562 base::RunLoop().Run(); |
| 562 | 563 |
| 563 EXPECT_EQ(0U, success_callback_count_); | 564 EXPECT_EQ(0U, success_callback_count_); |
| 564 EXPECT_EQ(2U, error_callback_count_); | 565 EXPECT_EQ(2U, error_callback_count_); |
| 565 EXPECT_TRUE(last_socket_.get() == nullptr); | 566 EXPECT_TRUE(last_socket_.get() == nullptr); |
| 566 } | 567 } |
| 567 | 568 |
| 568 TEST_F(BluetoothSocketBlueZTest, SocketListenTwice) { | 569 TEST_F(BluetoothSocketBlueZTest, SocketListenTwice) { |
| 569 adapter_->CreateRfcommService( | 570 adapter_->CreateRfcommService( |
| 570 BluetoothUUID(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid), | 571 BluetoothUUID(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid), |
| 571 BluetoothAdapter::ServiceOptions(), | 572 BluetoothAdapter::ServiceOptions(), |
| 572 base::Bind(&BluetoothSocketBlueZTest::CreateServiceSuccessCallback, | 573 base::Bind(&BluetoothSocketBlueZTest::CreateServiceSuccessCallback, |
| 573 base::Unretained(this)), | 574 base::Unretained(this)), |
| 574 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback, | 575 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback, |
| 575 base::Unretained(this))); | 576 base::Unretained(this))); |
| 576 | 577 |
| 577 message_loop_.Run(); | 578 base::RunLoop().Run(); |
| 578 | 579 |
| 579 EXPECT_EQ(1U, success_callback_count_); | 580 EXPECT_EQ(1U, success_callback_count_); |
| 580 EXPECT_EQ(0U, error_callback_count_); | 581 EXPECT_EQ(0U, error_callback_count_); |
| 581 EXPECT_TRUE(last_socket_.get() != nullptr); | 582 EXPECT_TRUE(last_socket_.get() != nullptr); |
| 582 | 583 |
| 583 // Take control of this socket. | 584 // Take control of this socket. |
| 584 scoped_refptr<BluetoothSocket> server_socket; | 585 scoped_refptr<BluetoothSocket> server_socket; |
| 585 server_socket.swap(last_socket_); | 586 server_socket.swap(last_socket_); |
| 586 | 587 |
| 587 server_socket->Accept( | 588 server_socket->Accept( |
| 588 base::Bind(&BluetoothSocketBlueZTest::AcceptSuccessCallback, | 589 base::Bind(&BluetoothSocketBlueZTest::AcceptSuccessCallback, |
| 589 base::Unretained(this)), | 590 base::Unretained(this)), |
| 590 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback, | 591 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback, |
| 591 base::Unretained(this))); | 592 base::Unretained(this))); |
| 592 | 593 |
| 593 server_socket->Close(); | 594 server_socket->Close(); |
| 594 | 595 |
| 595 server_socket = nullptr; | 596 server_socket = nullptr; |
| 596 | 597 |
| 597 message_loop_.RunUntilIdle(); | 598 base::RunLoop().RunUntilIdle(); |
| 598 | 599 |
| 599 EXPECT_EQ(1U, success_callback_count_); | 600 EXPECT_EQ(1U, success_callback_count_); |
| 600 EXPECT_EQ(1U, error_callback_count_); | 601 EXPECT_EQ(1U, error_callback_count_); |
| 601 | 602 |
| 602 adapter_->CreateRfcommService( | 603 adapter_->CreateRfcommService( |
| 603 BluetoothUUID(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid), | 604 BluetoothUUID(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid), |
| 604 BluetoothAdapter::ServiceOptions(), | 605 BluetoothAdapter::ServiceOptions(), |
| 605 base::Bind(&BluetoothSocketBlueZTest::CreateServiceSuccessCallback, | 606 base::Bind(&BluetoothSocketBlueZTest::CreateServiceSuccessCallback, |
| 606 base::Unretained(this)), | 607 base::Unretained(this)), |
| 607 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback, | 608 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback, |
| 608 base::Unretained(this))); | 609 base::Unretained(this))); |
| 609 | 610 |
| 610 message_loop_.Run(); | 611 base::RunLoop().Run(); |
| 611 | 612 |
| 612 EXPECT_EQ(2U, success_callback_count_); | 613 EXPECT_EQ(2U, success_callback_count_); |
| 613 EXPECT_EQ(1U, error_callback_count_); | 614 EXPECT_EQ(1U, error_callback_count_); |
| 614 EXPECT_TRUE(last_socket_.get() != nullptr); | 615 EXPECT_TRUE(last_socket_.get() != nullptr); |
| 615 | 616 |
| 616 // Take control of this socket. | 617 // Take control of this socket. |
| 617 server_socket.swap(last_socket_); | 618 server_socket.swap(last_socket_); |
| 618 | 619 |
| 619 server_socket->Accept( | 620 server_socket->Accept( |
| 620 base::Bind(&BluetoothSocketBlueZTest::AcceptSuccessCallback, | 621 base::Bind(&BluetoothSocketBlueZTest::AcceptSuccessCallback, |
| 621 base::Unretained(this)), | 622 base::Unretained(this)), |
| 622 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback, | 623 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback, |
| 623 base::Unretained(this))); | 624 base::Unretained(this))); |
| 624 | 625 |
| 625 server_socket->Close(); | 626 server_socket->Close(); |
| 626 | 627 |
| 627 server_socket = nullptr; | 628 server_socket = nullptr; |
| 628 | 629 |
| 629 message_loop_.RunUntilIdle(); | 630 base::RunLoop().RunUntilIdle(); |
| 630 | 631 |
| 631 EXPECT_EQ(2U, success_callback_count_); | 632 EXPECT_EQ(2U, success_callback_count_); |
| 632 EXPECT_EQ(2U, error_callback_count_); | 633 EXPECT_EQ(2U, error_callback_count_); |
| 633 } | 634 } |
| 634 | 635 |
| 635 } // namespace bluez | 636 } // namespace bluez |
| OLD | NEW |