| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/bind.h" | |
| 6 #include "base/message_loop/message_loop.h" | |
| 7 #include "device/bluetooth/bluetooth_adapter.h" | |
| 8 #include "device/bluetooth/bluetooth_adapter_chromeos.h" | |
| 9 #include "device/bluetooth/bluetooth_adapter_factory.h" | |
| 10 #include "device/bluetooth/bluetooth_adapter_profile_chromeos.h" | |
| 11 #include "device/bluetooth/bluetooth_uuid.h" | |
| 12 #include "device/bluetooth/dbus/bluetooth_profile_service_provider.h" | |
| 13 #include "device/bluetooth/dbus/bluez_dbus_manager.h" | |
| 14 #include "device/bluetooth/dbus/fake_bluetooth_adapter_client.h" | |
| 15 #include "device/bluetooth/dbus/fake_bluetooth_agent_manager_client.h" | |
| 16 #include "device/bluetooth/dbus/fake_bluetooth_device_client.h" | |
| 17 #include "device/bluetooth/dbus/fake_bluetooth_profile_manager_client.h" | |
| 18 #include "testing/gtest/include/gtest/gtest.h" | |
| 19 | |
| 20 using device::BluetoothAdapter; | |
| 21 using device::BluetoothUUID; | |
| 22 | |
| 23 namespace chromeos { | |
| 24 | |
| 25 class BluetoothAdapterProfileChromeOSTest : public testing::Test { | |
| 26 public: | |
| 27 BluetoothAdapterProfileChromeOSTest() | |
| 28 : success_callback_count_(0), | |
| 29 error_callback_count_(0), | |
| 30 fake_delegate_paired_( | |
| 31 bluez::FakeBluetoothDeviceClient::kPairedDevicePath), | |
| 32 fake_delegate_autopair_( | |
| 33 bluez::FakeBluetoothDeviceClient::kLegacyAutopairPath), | |
| 34 fake_delegate_listen_(""), | |
| 35 profile_user_ptr_(nullptr) {} | |
| 36 | |
| 37 void SetUp() override { | |
| 38 scoped_ptr<bluez::BluezDBusManagerSetter> dbus_setter = | |
| 39 bluez::BluezDBusManager::GetSetterForTesting(); | |
| 40 | |
| 41 dbus_setter->SetBluetoothAdapterClient( | |
| 42 scoped_ptr<bluez::BluetoothAdapterClient>( | |
| 43 new bluez::FakeBluetoothAdapterClient)); | |
| 44 dbus_setter->SetBluetoothAgentManagerClient( | |
| 45 scoped_ptr<bluez::BluetoothAgentManagerClient>( | |
| 46 new bluez::FakeBluetoothAgentManagerClient)); | |
| 47 dbus_setter->SetBluetoothDeviceClient( | |
| 48 scoped_ptr<bluez::BluetoothDeviceClient>( | |
| 49 new bluez::FakeBluetoothDeviceClient)); | |
| 50 dbus_setter->SetBluetoothProfileManagerClient( | |
| 51 scoped_ptr<bluez::BluetoothProfileManagerClient>( | |
| 52 new bluez::FakeBluetoothProfileManagerClient)); | |
| 53 | |
| 54 // Grab a pointer to the adapter. | |
| 55 device::BluetoothAdapterFactory::GetAdapter( | |
| 56 base::Bind(&BluetoothAdapterProfileChromeOSTest::AdapterCallback, | |
| 57 base::Unretained(this))); | |
| 58 ASSERT_TRUE(adapter_.get() != nullptr); | |
| 59 ASSERT_TRUE(adapter_->IsInitialized()); | |
| 60 ASSERT_TRUE(adapter_->IsPresent()); | |
| 61 | |
| 62 // Turn on the adapter. | |
| 63 adapter_->SetPowered(true, base::Bind(&base::DoNothing), | |
| 64 base::Bind(&base::DoNothing)); | |
| 65 ASSERT_TRUE(adapter_->IsPowered()); | |
| 66 } | |
| 67 | |
| 68 void TearDown() override { | |
| 69 profile_.reset(); | |
| 70 adapter_ = nullptr; | |
| 71 bluez::BluezDBusManager::Shutdown(); | |
| 72 } | |
| 73 | |
| 74 void AdapterCallback(scoped_refptr<BluetoothAdapter> adapter) { | |
| 75 adapter_ = adapter; | |
| 76 } | |
| 77 | |
| 78 class FakeDelegate : public bluez::BluetoothProfileServiceProvider::Delegate { | |
| 79 public: | |
| 80 FakeDelegate(const std::string& device_path) : connections_(0) { | |
| 81 device_path_ = dbus::ObjectPath(device_path); | |
| 82 } | |
| 83 | |
| 84 // bluez::BluetoothProfileServiceProvider::Delegate: | |
| 85 void Released() override { | |
| 86 // noop | |
| 87 } | |
| 88 | |
| 89 void NewConnection( | |
| 90 const dbus::ObjectPath& device_path, | |
| 91 scoped_ptr<dbus::FileDescriptor> fd, | |
| 92 const bluez::BluetoothProfileServiceProvider::Delegate::Options& | |
| 93 options, | |
| 94 const ConfirmationCallback& callback) override { | |
| 95 ++connections_; | |
| 96 fd->CheckValidity(); | |
| 97 close(fd->TakeValue()); | |
| 98 callback.Run(SUCCESS); | |
| 99 if (device_path_.value() != "") | |
| 100 ASSERT_EQ(device_path_, device_path); | |
| 101 } | |
| 102 | |
| 103 void RequestDisconnection(const dbus::ObjectPath& device_path, | |
| 104 const ConfirmationCallback& callback) override { | |
| 105 ++disconnections_; | |
| 106 } | |
| 107 | |
| 108 void Cancel() override { | |
| 109 // noop | |
| 110 } | |
| 111 | |
| 112 unsigned int connections_; | |
| 113 unsigned int disconnections_; | |
| 114 dbus::ObjectPath device_path_; | |
| 115 }; | |
| 116 | |
| 117 void ProfileSuccessCallback( | |
| 118 scoped_ptr<BluetoothAdapterProfileChromeOS> profile) { | |
| 119 profile_.swap(profile); | |
| 120 ++success_callback_count_; | |
| 121 } | |
| 122 | |
| 123 void ProfileUserSuccessCallback(BluetoothAdapterProfileChromeOS* profile) { | |
| 124 profile_user_ptr_ = profile; | |
| 125 ++success_callback_count_; | |
| 126 } | |
| 127 | |
| 128 void MatchedProfileCallback(BluetoothAdapterProfileChromeOS* profile) { | |
| 129 ASSERT_EQ(profile_user_ptr_, profile); | |
| 130 ++success_callback_count_; | |
| 131 } | |
| 132 | |
| 133 void DBusConnectSuccessCallback() { ++success_callback_count_; } | |
| 134 | |
| 135 void DBusErrorCallback(const std::string& error_name, | |
| 136 const std::string& error_message) { | |
| 137 ++error_callback_count_; | |
| 138 } | |
| 139 | |
| 140 void BasicErrorCallback(const std::string& error_message) { | |
| 141 ++error_callback_count_; | |
| 142 } | |
| 143 | |
| 144 protected: | |
| 145 base::MessageLoop message_loop_; | |
| 146 | |
| 147 scoped_refptr<BluetoothAdapter> adapter_; | |
| 148 | |
| 149 unsigned int success_callback_count_; | |
| 150 unsigned int error_callback_count_; | |
| 151 | |
| 152 FakeDelegate fake_delegate_paired_; | |
| 153 FakeDelegate fake_delegate_autopair_; | |
| 154 FakeDelegate fake_delegate_listen_; | |
| 155 | |
| 156 scoped_ptr<BluetoothAdapterProfileChromeOS> profile_; | |
| 157 | |
| 158 // unowned pointer as expected to be used by clients of | |
| 159 // BluetoothAdapterChromeOS::UseProfile like BluetoothSocketChromeOS | |
| 160 BluetoothAdapterProfileChromeOS* profile_user_ptr_; | |
| 161 }; | |
| 162 | |
| 163 TEST_F(BluetoothAdapterProfileChromeOSTest, DelegateCount) { | |
| 164 BluetoothUUID uuid(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid); | |
| 165 bluez::BluetoothProfileManagerClient::Options options; | |
| 166 | |
| 167 options.require_authentication.reset(new bool(false)); | |
| 168 | |
| 169 BluetoothAdapterProfileChromeOS::Register( | |
| 170 uuid, options, | |
| 171 base::Bind(&BluetoothAdapterProfileChromeOSTest::ProfileSuccessCallback, | |
| 172 base::Unretained(this)), | |
| 173 base::Bind(&BluetoothAdapterProfileChromeOSTest::DBusErrorCallback, | |
| 174 base::Unretained(this))); | |
| 175 | |
| 176 message_loop_.RunUntilIdle(); | |
| 177 | |
| 178 EXPECT_TRUE(profile_); | |
| 179 EXPECT_EQ(1U, success_callback_count_); | |
| 180 EXPECT_EQ(0U, error_callback_count_); | |
| 181 | |
| 182 EXPECT_EQ(0U, profile_->DelegateCount()); | |
| 183 | |
| 184 profile_->SetDelegate(fake_delegate_paired_.device_path_, | |
| 185 &fake_delegate_paired_); | |
| 186 | |
| 187 EXPECT_EQ(1U, profile_->DelegateCount()); | |
| 188 | |
| 189 profile_->RemoveDelegate(fake_delegate_autopair_.device_path_, | |
| 190 base::Bind(&base::DoNothing)); | |
| 191 | |
| 192 EXPECT_EQ(1U, profile_->DelegateCount()); | |
| 193 | |
| 194 profile_->RemoveDelegate(fake_delegate_paired_.device_path_, | |
| 195 base::Bind(&base::DoNothing)); | |
| 196 | |
| 197 EXPECT_EQ(0U, profile_->DelegateCount()); | |
| 198 } | |
| 199 | |
| 200 TEST_F(BluetoothAdapterProfileChromeOSTest, BlackHole) { | |
| 201 BluetoothUUID uuid(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid); | |
| 202 bluez::BluetoothProfileManagerClient::Options options; | |
| 203 | |
| 204 options.require_authentication.reset(new bool(false)); | |
| 205 | |
| 206 BluetoothAdapterProfileChromeOS::Register( | |
| 207 uuid, options, | |
| 208 base::Bind(&BluetoothAdapterProfileChromeOSTest::ProfileSuccessCallback, | |
| 209 base::Unretained(this)), | |
| 210 base::Bind(&BluetoothAdapterProfileChromeOSTest::DBusErrorCallback, | |
| 211 base::Unretained(this))); | |
| 212 | |
| 213 message_loop_.RunUntilIdle(); | |
| 214 | |
| 215 EXPECT_TRUE(profile_); | |
| 216 EXPECT_EQ(1U, success_callback_count_); | |
| 217 EXPECT_EQ(0U, error_callback_count_); | |
| 218 | |
| 219 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->ConnectProfile( | |
| 220 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kPairedDevicePath), | |
| 221 bluez::FakeBluetoothProfileManagerClient::kRfcommUuid, | |
| 222 base::Bind( | |
| 223 &BluetoothAdapterProfileChromeOSTest::DBusConnectSuccessCallback, | |
| 224 base::Unretained(this)), | |
| 225 base::Bind(&BluetoothAdapterProfileChromeOSTest::DBusErrorCallback, | |
| 226 base::Unretained(this))); | |
| 227 | |
| 228 message_loop_.RunUntilIdle(); | |
| 229 | |
| 230 EXPECT_EQ(1U, success_callback_count_); | |
| 231 EXPECT_EQ(1U, error_callback_count_); | |
| 232 | |
| 233 EXPECT_EQ(0U, fake_delegate_paired_.connections_); | |
| 234 } | |
| 235 | |
| 236 TEST_F(BluetoothAdapterProfileChromeOSTest, Routing) { | |
| 237 BluetoothUUID uuid(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid); | |
| 238 bluez::BluetoothProfileManagerClient::Options options; | |
| 239 | |
| 240 options.require_authentication.reset(new bool(false)); | |
| 241 | |
| 242 BluetoothAdapterProfileChromeOS::Register( | |
| 243 uuid, options, | |
| 244 base::Bind(&BluetoothAdapterProfileChromeOSTest::ProfileSuccessCallback, | |
| 245 base::Unretained(this)), | |
| 246 base::Bind(&BluetoothAdapterProfileChromeOSTest::DBusErrorCallback, | |
| 247 base::Unretained(this))); | |
| 248 | |
| 249 message_loop_.RunUntilIdle(); | |
| 250 | |
| 251 ASSERT_TRUE(profile_); | |
| 252 ASSERT_EQ(1U, success_callback_count_); | |
| 253 ASSERT_EQ(0U, error_callback_count_); | |
| 254 | |
| 255 profile_->SetDelegate(fake_delegate_paired_.device_path_, | |
| 256 &fake_delegate_paired_); | |
| 257 profile_->SetDelegate(fake_delegate_autopair_.device_path_, | |
| 258 &fake_delegate_autopair_); | |
| 259 profile_->SetDelegate(fake_delegate_listen_.device_path_, | |
| 260 &fake_delegate_listen_); | |
| 261 | |
| 262 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->ConnectProfile( | |
| 263 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kPairedDevicePath), | |
| 264 bluez::FakeBluetoothProfileManagerClient::kRfcommUuid, | |
| 265 base::Bind( | |
| 266 &BluetoothAdapterProfileChromeOSTest::DBusConnectSuccessCallback, | |
| 267 base::Unretained(this)), | |
| 268 base::Bind(&BluetoothAdapterProfileChromeOSTest::DBusErrorCallback, | |
| 269 base::Unretained(this))); | |
| 270 | |
| 271 message_loop_.RunUntilIdle(); | |
| 272 | |
| 273 EXPECT_EQ(2U, success_callback_count_); | |
| 274 EXPECT_EQ(0U, error_callback_count_); | |
| 275 | |
| 276 EXPECT_EQ(1U, fake_delegate_paired_.connections_); | |
| 277 | |
| 278 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->ConnectProfile( | |
| 279 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kLegacyAutopairPath), | |
| 280 bluez::FakeBluetoothProfileManagerClient::kRfcommUuid, | |
| 281 base::Bind( | |
| 282 &BluetoothAdapterProfileChromeOSTest::DBusConnectSuccessCallback, | |
| 283 base::Unretained(this)), | |
| 284 base::Bind(&BluetoothAdapterProfileChromeOSTest::DBusErrorCallback, | |
| 285 base::Unretained(this))); | |
| 286 | |
| 287 message_loop_.RunUntilIdle(); | |
| 288 | |
| 289 EXPECT_EQ(3U, success_callback_count_); | |
| 290 EXPECT_EQ(0U, error_callback_count_); | |
| 291 | |
| 292 EXPECT_EQ(1U, fake_delegate_autopair_.connections_); | |
| 293 | |
| 294 // Incoming connections look the same from BlueZ. | |
| 295 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->ConnectProfile( | |
| 296 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kDisplayPinCodePath), | |
| 297 bluez::FakeBluetoothProfileManagerClient::kRfcommUuid, | |
| 298 base::Bind( | |
| 299 &BluetoothAdapterProfileChromeOSTest::DBusConnectSuccessCallback, | |
| 300 base::Unretained(this)), | |
| 301 base::Bind(&BluetoothAdapterProfileChromeOSTest::DBusErrorCallback, | |
| 302 base::Unretained(this))); | |
| 303 | |
| 304 message_loop_.RunUntilIdle(); | |
| 305 | |
| 306 EXPECT_EQ(4U, success_callback_count_); | |
| 307 EXPECT_EQ(0U, error_callback_count_); | |
| 308 | |
| 309 EXPECT_EQ(1U, fake_delegate_listen_.connections_); | |
| 310 } | |
| 311 | |
| 312 TEST_F(BluetoothAdapterProfileChromeOSTest, SimultaneousRegister) { | |
| 313 BluetoothUUID uuid(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid); | |
| 314 bluez::BluetoothProfileManagerClient::Options options; | |
| 315 BluetoothAdapterChromeOS* adapter = | |
| 316 static_cast<BluetoothAdapterChromeOS*>(adapter_.get()); | |
| 317 | |
| 318 options.require_authentication.reset(new bool(false)); | |
| 319 | |
| 320 success_callback_count_ = 0; | |
| 321 error_callback_count_ = 0; | |
| 322 | |
| 323 adapter->UseProfile( | |
| 324 uuid, fake_delegate_paired_.device_path_, options, &fake_delegate_paired_, | |
| 325 base::Bind( | |
| 326 &BluetoothAdapterProfileChromeOSTest::ProfileUserSuccessCallback, | |
| 327 base::Unretained(this)), | |
| 328 base::Bind(&BluetoothAdapterProfileChromeOSTest::BasicErrorCallback, | |
| 329 base::Unretained(this))); | |
| 330 | |
| 331 adapter->UseProfile( | |
| 332 uuid, fake_delegate_autopair_.device_path_, options, | |
| 333 &fake_delegate_autopair_, | |
| 334 base::Bind(&BluetoothAdapterProfileChromeOSTest::MatchedProfileCallback, | |
| 335 base::Unretained(this)), | |
| 336 base::Bind(&BluetoothAdapterProfileChromeOSTest::BasicErrorCallback, | |
| 337 base::Unretained(this))); | |
| 338 | |
| 339 message_loop_.RunUntilIdle(); | |
| 340 | |
| 341 EXPECT_TRUE(profile_user_ptr_); | |
| 342 EXPECT_EQ(2U, success_callback_count_); | |
| 343 EXPECT_EQ(0U, error_callback_count_); | |
| 344 | |
| 345 adapter->ReleaseProfile(fake_delegate_paired_.device_path_, | |
| 346 profile_user_ptr_); | |
| 347 adapter->ReleaseProfile(fake_delegate_autopair_.device_path_, | |
| 348 profile_user_ptr_); | |
| 349 | |
| 350 message_loop_.RunUntilIdle(); | |
| 351 } | |
| 352 | |
| 353 TEST_F(BluetoothAdapterProfileChromeOSTest, SimultaneousRegisterFail) { | |
| 354 BluetoothUUID uuid( | |
| 355 bluez::FakeBluetoothProfileManagerClient::kUnregisterableUuid); | |
| 356 bluez::BluetoothProfileManagerClient::Options options; | |
| 357 BluetoothAdapterChromeOS* adapter = | |
| 358 static_cast<BluetoothAdapterChromeOS*>(adapter_.get()); | |
| 359 | |
| 360 options.require_authentication.reset(new bool(false)); | |
| 361 | |
| 362 success_callback_count_ = 0; | |
| 363 error_callback_count_ = 0; | |
| 364 | |
| 365 adapter->UseProfile( | |
| 366 uuid, fake_delegate_paired_.device_path_, options, &fake_delegate_paired_, | |
| 367 base::Bind( | |
| 368 &BluetoothAdapterProfileChromeOSTest::ProfileUserSuccessCallback, | |
| 369 base::Unretained(this)), | |
| 370 base::Bind(&BluetoothAdapterProfileChromeOSTest::BasicErrorCallback, | |
| 371 base::Unretained(this))); | |
| 372 | |
| 373 adapter->UseProfile( | |
| 374 uuid, fake_delegate_autopair_.device_path_, options, | |
| 375 &fake_delegate_autopair_, | |
| 376 base::Bind(&BluetoothAdapterProfileChromeOSTest::MatchedProfileCallback, | |
| 377 base::Unretained(this)), | |
| 378 base::Bind(&BluetoothAdapterProfileChromeOSTest::BasicErrorCallback, | |
| 379 base::Unretained(this))); | |
| 380 | |
| 381 message_loop_.RunUntilIdle(); | |
| 382 | |
| 383 EXPECT_FALSE(profile_user_ptr_); | |
| 384 EXPECT_EQ(0U, success_callback_count_); | |
| 385 EXPECT_EQ(2U, error_callback_count_); | |
| 386 } | |
| 387 | |
| 388 } // namespace chromeos | |
| OLD | NEW |