| 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 "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/memory/ref_counted.h" | 6 #include "base/memory/ref_counted.h" |
| 7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
| 8 #include "chromeos/dbus/dbus_thread_manager.h" | 8 #include "chromeos/dbus/dbus_thread_manager.h" |
| 9 #include "chromeos/dbus/fake_bluetooth_adapter_client.h" | 9 #include "chromeos/dbus/fake_bluetooth_adapter_client.h" |
| 10 #include "chromeos/dbus/fake_bluetooth_agent_manager_client.h" | 10 #include "chromeos/dbus/fake_bluetooth_agent_manager_client.h" |
| 11 #include "chromeos/dbus/fake_bluetooth_device_client.h" | 11 #include "chromeos/dbus/fake_bluetooth_device_client.h" |
| 12 #include "chromeos/dbus/fake_bluetooth_gatt_service_client.h" | 12 #include "chromeos/dbus/fake_bluetooth_gatt_service_client.h" |
| 13 #include "chromeos/dbus/fake_bluetooth_input_client.h" | 13 #include "chromeos/dbus/fake_bluetooth_input_client.h" |
| 14 #include "chromeos/dbus/fake_bluetooth_profile_manager_client.h" | 14 #include "chromeos/dbus/fake_bluetooth_profile_manager_client.h" |
| 15 #include "chromeos/dbus/fake_bluetooth_profile_service_provider.h" |
| 15 #include "device/bluetooth/bluetooth_adapter.h" | 16 #include "device/bluetooth/bluetooth_adapter.h" |
| 16 #include "device/bluetooth/bluetooth_adapter_chromeos.h" | 17 #include "device/bluetooth/bluetooth_adapter_chromeos.h" |
| 17 #include "device/bluetooth/bluetooth_adapter_factory.h" | 18 #include "device/bluetooth/bluetooth_adapter_factory.h" |
| 18 #include "device/bluetooth/bluetooth_device.h" | 19 #include "device/bluetooth/bluetooth_device.h" |
| 19 #include "device/bluetooth/bluetooth_device_chromeos.h" | 20 #include "device/bluetooth/bluetooth_device_chromeos.h" |
| 20 #include "device/bluetooth/bluetooth_socket.h" | 21 #include "device/bluetooth/bluetooth_socket.h" |
| 21 #include "device/bluetooth/bluetooth_socket_chromeos.h" | 22 #include "device/bluetooth/bluetooth_socket_chromeos.h" |
| 22 #include "device/bluetooth/bluetooth_socket_thread.h" | 23 #include "device/bluetooth/bluetooth_socket_thread.h" |
| 23 #include "device/bluetooth/bluetooth_uuid.h" | 24 #include "device/bluetooth/bluetooth_uuid.h" |
| 24 #include "net/base/io_buffer.h" | 25 #include "net/base/io_buffer.h" |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 ++error_callback_count_; | 138 ++error_callback_count_; |
| 138 last_reason_ = reason; | 139 last_reason_ = reason; |
| 139 last_message_ = error_message; | 140 last_message_ = error_message; |
| 140 | 141 |
| 141 message_loop_.Quit(); | 142 message_loop_.Quit(); |
| 142 } | 143 } |
| 143 | 144 |
| 144 void CreateServiceSuccessCallback(scoped_refptr<BluetoothSocket> socket) { | 145 void CreateServiceSuccessCallback(scoped_refptr<BluetoothSocket> socket) { |
| 145 ++success_callback_count_; | 146 ++success_callback_count_; |
| 146 last_socket_ = socket; | 147 last_socket_ = socket; |
| 148 |
| 149 if (message_loop_.is_running()) |
| 150 message_loop_.Quit(); |
| 147 } | 151 } |
| 148 | 152 |
| 149 void AcceptSuccessCallback(const BluetoothDevice* device, | 153 void AcceptSuccessCallback(const BluetoothDevice* device, |
| 150 scoped_refptr<BluetoothSocket> socket) { | 154 scoped_refptr<BluetoothSocket> socket) { |
| 151 ++success_callback_count_; | 155 ++success_callback_count_; |
| 152 last_device_ = device; | 156 last_device_ = device; |
| 153 last_socket_ = socket; | 157 last_socket_ = socket; |
| 154 | 158 |
| 155 message_loop_.Quit(); | 159 message_loop_.Quit(); |
| 156 } | 160 } |
| (...skipping 23 matching lines...) Expand all Loading... |
| 180 BluetoothDevice* device = adapter_->GetDevice( | 184 BluetoothDevice* device = adapter_->GetDevice( |
| 181 FakeBluetoothDeviceClient::kPairedDeviceAddress); | 185 FakeBluetoothDeviceClient::kPairedDeviceAddress); |
| 182 ASSERT_TRUE(device != NULL); | 186 ASSERT_TRUE(device != NULL); |
| 183 | 187 |
| 184 device->ConnectToService( | 188 device->ConnectToService( |
| 185 BluetoothUUID(FakeBluetoothProfileManagerClient::kRfcommUuid), | 189 BluetoothUUID(FakeBluetoothProfileManagerClient::kRfcommUuid), |
| 186 base::Bind(&BluetoothSocketChromeOSTest::ConnectToServiceSuccessCallback, | 190 base::Bind(&BluetoothSocketChromeOSTest::ConnectToServiceSuccessCallback, |
| 187 base::Unretained(this)), | 191 base::Unretained(this)), |
| 188 base::Bind(&BluetoothSocketChromeOSTest::ErrorCallback, | 192 base::Bind(&BluetoothSocketChromeOSTest::ErrorCallback, |
| 189 base::Unretained(this))); | 193 base::Unretained(this))); |
| 190 | |
| 191 message_loop_.Run(); | 194 message_loop_.Run(); |
| 192 | 195 |
| 193 EXPECT_EQ(1U, success_callback_count_); | 196 EXPECT_EQ(1U, success_callback_count_); |
| 194 EXPECT_EQ(0U, error_callback_count_); | 197 EXPECT_EQ(0U, error_callback_count_); |
| 195 EXPECT_TRUE(last_socket_.get() != NULL); | 198 EXPECT_TRUE(last_socket_.get() != NULL); |
| 196 | 199 |
| 197 // Take ownership of the socket for the remainder of the test. | 200 // Take ownership of the socket for the remainder of the test. |
| 198 scoped_refptr<BluetoothSocket> socket = last_socket_; | 201 scoped_refptr<BluetoothSocket> socket = last_socket_; |
| 199 last_socket_ = NULL; | 202 last_socket_ = NULL; |
| 200 success_callback_count_ = 0; | 203 success_callback_count_ = 0; |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 290 | 293 |
| 291 TEST_F(BluetoothSocketChromeOSTest, Listen) { | 294 TEST_F(BluetoothSocketChromeOSTest, Listen) { |
| 292 adapter_->CreateRfcommService( | 295 adapter_->CreateRfcommService( |
| 293 BluetoothUUID(FakeBluetoothProfileManagerClient::kRfcommUuid), | 296 BluetoothUUID(FakeBluetoothProfileManagerClient::kRfcommUuid), |
| 294 BluetoothAdapter::ServiceOptions(), | 297 BluetoothAdapter::ServiceOptions(), |
| 295 base::Bind(&BluetoothSocketChromeOSTest::CreateServiceSuccessCallback, | 298 base::Bind(&BluetoothSocketChromeOSTest::CreateServiceSuccessCallback, |
| 296 base::Unretained(this)), | 299 base::Unretained(this)), |
| 297 base::Bind(&BluetoothSocketChromeOSTest::ErrorCallback, | 300 base::Bind(&BluetoothSocketChromeOSTest::ErrorCallback, |
| 298 base::Unretained(this))); | 301 base::Unretained(this))); |
| 299 | 302 |
| 303 message_loop_.Run(); |
| 304 |
| 300 EXPECT_EQ(1U, success_callback_count_); | 305 EXPECT_EQ(1U, success_callback_count_); |
| 301 EXPECT_EQ(0U, error_callback_count_); | 306 EXPECT_EQ(0U, error_callback_count_); |
| 302 EXPECT_TRUE(last_socket_.get() != NULL); | 307 EXPECT_TRUE(last_socket_.get() != NULL); |
| 303 | 308 |
| 304 // Take ownership of the socket for the remainder of the test. | 309 // Take ownership of the socket for the remainder of the test. |
| 305 scoped_refptr<BluetoothSocket> server_socket = last_socket_; | 310 scoped_refptr<BluetoothSocket> server_socket = last_socket_; |
| 306 last_socket_ = NULL; | 311 last_socket_ = NULL; |
| 307 success_callback_count_ = 0; | 312 success_callback_count_ = 0; |
| 308 error_callback_count_ = 0; | 313 error_callback_count_ = 0; |
| 309 | 314 |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 409 DBusThreadManager::Get()->GetBluetoothAdapterClient()); | 414 DBusThreadManager::Get()->GetBluetoothAdapterClient()); |
| 410 fake_bluetooth_adapter_client->SetVisible(false); | 415 fake_bluetooth_adapter_client->SetVisible(false); |
| 411 | 416 |
| 412 adapter_->CreateRfcommService( | 417 adapter_->CreateRfcommService( |
| 413 BluetoothUUID(FakeBluetoothProfileManagerClient::kRfcommUuid), | 418 BluetoothUUID(FakeBluetoothProfileManagerClient::kRfcommUuid), |
| 414 BluetoothAdapter::ServiceOptions(), | 419 BluetoothAdapter::ServiceOptions(), |
| 415 base::Bind(&BluetoothSocketChromeOSTest::CreateServiceSuccessCallback, | 420 base::Bind(&BluetoothSocketChromeOSTest::CreateServiceSuccessCallback, |
| 416 base::Unretained(this)), | 421 base::Unretained(this)), |
| 417 base::Bind(&BluetoothSocketChromeOSTest::ErrorCallback, | 422 base::Bind(&BluetoothSocketChromeOSTest::ErrorCallback, |
| 418 base::Unretained(this))); | 423 base::Unretained(this))); |
| 424 message_loop_.Run(); |
| 419 | 425 |
| 420 EXPECT_EQ(1U, success_callback_count_); | 426 EXPECT_EQ(1U, success_callback_count_); |
| 421 EXPECT_EQ(0U, error_callback_count_); | 427 EXPECT_EQ(0U, error_callback_count_); |
| 422 EXPECT_TRUE(last_socket_.get() != NULL); | 428 EXPECT_TRUE(last_socket_.get() != NULL); |
| 423 | 429 |
| 424 // Take ownership of the socket for the remainder of the test. | 430 // Take ownership of the socket for the remainder of the test. |
| 425 scoped_refptr<BluetoothSocket> socket = last_socket_; | 431 scoped_refptr<BluetoothSocket> socket = last_socket_; |
| 426 last_socket_ = NULL; | 432 last_socket_ = NULL; |
| 427 success_callback_count_ = 0; | 433 success_callback_count_ = 0; |
| 428 error_callback_count_ = 0; | 434 error_callback_count_ = 0; |
| 429 | 435 |
| 430 // But there shouldn't be a profile registered yet. | 436 // But there shouldn't be a profile registered yet. |
| 431 FakeBluetoothProfileManagerClient* fake_bluetooth_profile_manager_client = | 437 FakeBluetoothProfileManagerClient* fake_bluetooth_profile_manager_client = |
| 432 static_cast<FakeBluetoothProfileManagerClient*>( | 438 static_cast<FakeBluetoothProfileManagerClient*>( |
| 433 DBusThreadManager::Get()->GetBluetoothProfileManagerClient()); | 439 DBusThreadManager::Get()->GetBluetoothProfileManagerClient()); |
| 434 FakeBluetoothProfileServiceProvider* profile_service_provider = | 440 FakeBluetoothProfileServiceProvider* profile_service_provider = |
| 435 fake_bluetooth_profile_manager_client->GetProfileServiceProvider( | 441 fake_bluetooth_profile_manager_client->GetProfileServiceProvider( |
| 436 FakeBluetoothProfileManagerClient::kRfcommUuid); | 442 FakeBluetoothProfileManagerClient::kRfcommUuid); |
| 437 EXPECT_TRUE(profile_service_provider == NULL); | 443 EXPECT_TRUE(profile_service_provider == NULL); |
| 438 | 444 |
| 439 // Make the adapter visible. This should register a profile. | 445 // Make the adapter visible. This should register a profile. |
| 440 fake_bluetooth_adapter_client->SetVisible(true); | 446 fake_bluetooth_adapter_client->SetVisible(true); |
| 441 | 447 |
| 448 message_loop_.RunUntilIdle(); |
| 449 |
| 442 profile_service_provider = | 450 profile_service_provider = |
| 443 fake_bluetooth_profile_manager_client->GetProfileServiceProvider( | 451 fake_bluetooth_profile_manager_client->GetProfileServiceProvider( |
| 444 FakeBluetoothProfileManagerClient::kRfcommUuid); | 452 FakeBluetoothProfileManagerClient::kRfcommUuid); |
| 445 EXPECT_TRUE(profile_service_provider != NULL); | 453 EXPECT_TRUE(profile_service_provider != NULL); |
| 446 | 454 |
| 447 // Cleanup the socket. | 455 // Cleanup the socket. |
| 448 socket->Disconnect( | 456 socket->Disconnect( |
| 449 base::Bind(&BluetoothSocketChromeOSTest::ImmediateSuccessCallback, | 457 base::Bind(&BluetoothSocketChromeOSTest::ImmediateSuccessCallback, |
| 450 base::Unretained(this))); | 458 base::Unretained(this))); |
| 451 | 459 |
| 452 EXPECT_EQ(1U, success_callback_count_); | 460 EXPECT_EQ(1U, success_callback_count_); |
| 453 } | 461 } |
| 454 | 462 |
| 455 TEST_F(BluetoothSocketChromeOSTest, ListenAcrossAdapterRestart) { | 463 TEST_F(BluetoothSocketChromeOSTest, ListenAcrossAdapterRestart) { |
| 456 // The fake adapter starts off visible by default. | 464 // The fake adapter starts off visible by default. |
| 457 FakeBluetoothAdapterClient* fake_bluetooth_adapter_client = | 465 FakeBluetoothAdapterClient* fake_bluetooth_adapter_client = |
| 458 static_cast<FakeBluetoothAdapterClient*>( | 466 static_cast<FakeBluetoothAdapterClient*>( |
| 459 DBusThreadManager::Get()->GetBluetoothAdapterClient()); | 467 DBusThreadManager::Get()->GetBluetoothAdapterClient()); |
| 460 | 468 |
| 461 adapter_->CreateRfcommService( | 469 adapter_->CreateRfcommService( |
| 462 BluetoothUUID(FakeBluetoothProfileManagerClient::kRfcommUuid), | 470 BluetoothUUID(FakeBluetoothProfileManagerClient::kRfcommUuid), |
| 463 BluetoothAdapter::ServiceOptions(), | 471 BluetoothAdapter::ServiceOptions(), |
| 464 base::Bind(&BluetoothSocketChromeOSTest::CreateServiceSuccessCallback, | 472 base::Bind(&BluetoothSocketChromeOSTest::CreateServiceSuccessCallback, |
| 465 base::Unretained(this)), | 473 base::Unretained(this)), |
| 466 base::Bind(&BluetoothSocketChromeOSTest::ErrorCallback, | 474 base::Bind(&BluetoothSocketChromeOSTest::ErrorCallback, |
| 467 base::Unretained(this))); | 475 base::Unretained(this))); |
| 476 message_loop_.Run(); |
| 468 | 477 |
| 469 EXPECT_EQ(1U, success_callback_count_); | 478 EXPECT_EQ(1U, success_callback_count_); |
| 470 EXPECT_EQ(0U, error_callback_count_); | 479 EXPECT_EQ(0U, error_callback_count_); |
| 471 EXPECT_TRUE(last_socket_.get() != NULL); | 480 EXPECT_TRUE(last_socket_.get() != NULL); |
| 472 | 481 |
| 473 // Take ownership of the socket for the remainder of the test. | 482 // Take ownership of the socket for the remainder of the test. |
| 474 scoped_refptr<BluetoothSocket> socket = last_socket_; | 483 scoped_refptr<BluetoothSocket> socket = last_socket_; |
| 475 last_socket_ = NULL; | 484 last_socket_ = NULL; |
| 476 success_callback_count_ = 0; | 485 success_callback_count_ = 0; |
| 477 error_callback_count_ = 0; | 486 error_callback_count_ = 0; |
| 478 | 487 |
| 479 // Make sure the profile was registered with the daemon. | 488 // Make sure the profile was registered with the daemon. |
| 480 FakeBluetoothProfileManagerClient* fake_bluetooth_profile_manager_client = | 489 FakeBluetoothProfileManagerClient* fake_bluetooth_profile_manager_client = |
| 481 static_cast<FakeBluetoothProfileManagerClient*>( | 490 static_cast<FakeBluetoothProfileManagerClient*>( |
| 482 DBusThreadManager::Get()->GetBluetoothProfileManagerClient()); | 491 DBusThreadManager::Get()->GetBluetoothProfileManagerClient()); |
| 483 FakeBluetoothProfileServiceProvider* profile_service_provider = | 492 FakeBluetoothProfileServiceProvider* profile_service_provider = |
| 484 fake_bluetooth_profile_manager_client->GetProfileServiceProvider( | 493 fake_bluetooth_profile_manager_client->GetProfileServiceProvider( |
| 485 FakeBluetoothProfileManagerClient::kRfcommUuid); | 494 FakeBluetoothProfileManagerClient::kRfcommUuid); |
| 486 EXPECT_TRUE(profile_service_provider != NULL); | 495 EXPECT_TRUE(profile_service_provider != NULL); |
| 487 | 496 |
| 488 // Make the adapter invisible, and fiddle with the profile fake to unregister | 497 // Make the adapter invisible, and fiddle with the profile fake to unregister |
| 489 // the profile since this doesn't happen automatically. | 498 // the profile since this doesn't happen automatically. |
| 490 fake_bluetooth_adapter_client->SetVisible(false); | 499 fake_bluetooth_adapter_client->SetVisible(false); |
| 491 fake_bluetooth_profile_manager_client->UnregisterProfile( | 500 |
| 492 static_cast<BluetoothSocketChromeOS*>(socket.get())->object_path(), | 501 message_loop_.RunUntilIdle(); |
| 493 base::Bind(&base::DoNothing), | |
| 494 base::Bind(&DoNothingDBusErrorCallback)); | |
| 495 | 502 |
| 496 // Then make the adapter visible again. This should re-register the profile. | 503 // Then make the adapter visible again. This should re-register the profile. |
| 497 fake_bluetooth_adapter_client->SetVisible(true); | 504 fake_bluetooth_adapter_client->SetVisible(true); |
| 498 | 505 |
| 506 message_loop_.RunUntilIdle(); |
| 507 |
| 499 profile_service_provider = | 508 profile_service_provider = |
| 500 fake_bluetooth_profile_manager_client->GetProfileServiceProvider( | 509 fake_bluetooth_profile_manager_client->GetProfileServiceProvider( |
| 501 FakeBluetoothProfileManagerClient::kRfcommUuid); | 510 FakeBluetoothProfileManagerClient::kRfcommUuid); |
| 502 EXPECT_TRUE(profile_service_provider != NULL); | 511 EXPECT_TRUE(profile_service_provider != NULL); |
| 503 | 512 |
| 504 // Cleanup the socket. | 513 // Cleanup the socket. |
| 505 socket->Disconnect( | 514 socket->Disconnect( |
| 506 base::Bind(&BluetoothSocketChromeOSTest::ImmediateSuccessCallback, | 515 base::Bind(&BluetoothSocketChromeOSTest::ImmediateSuccessCallback, |
| 507 base::Unretained(this))); | 516 base::Unretained(this))); |
| 508 | 517 |
| 509 EXPECT_EQ(1U, success_callback_count_); | 518 EXPECT_EQ(1U, success_callback_count_); |
| 510 } | 519 } |
| 511 | 520 |
| 512 } // namespace chromeos | 521 } // namespace chromeos |
| OLD | NEW |