OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 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/command_line.h" |
| 6 #include "base/message_loop.h" |
| 7 #include "base/utf_string_conversions.h" |
| 8 #include "chromeos/chromeos_switches.h" |
| 9 #include "chromeos/dbus/fake_bluetooth_adapter_client.h" |
| 10 #include "chromeos/dbus/fake_bluetooth_device_client.h" |
| 11 #include "chromeos/dbus/mock_dbus_thread_manager_without_gmock.h" |
| 12 #include "dbus/object_path.h" |
| 13 #include "device/bluetooth/bluetooth_adapter.h" |
| 14 #include "device/bluetooth/bluetooth_adapter_experimental_chromeos.h" |
| 15 #include "device/bluetooth/bluetooth_adapter_factory.h" |
| 16 #include "device/bluetooth/bluetooth_device.h" |
| 17 #include "device/bluetooth/bluetooth_device_experimental_chromeos.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 |
| 20 using device::BluetoothAdapter; |
| 21 using device::BluetoothAdapterFactory; |
| 22 using device::BluetoothDevice; |
| 23 |
| 24 namespace chromeos { |
| 25 |
| 26 class TestObserver : public BluetoothAdapter::Observer { |
| 27 public: |
| 28 TestObserver(scoped_refptr<BluetoothAdapter> adapter) |
| 29 : present_changed_count_(0), |
| 30 powered_changed_count_(0), |
| 31 discovering_changed_count_(0), |
| 32 last_present_(false), |
| 33 last_powered_(false), |
| 34 last_discovering_(false), |
| 35 device_added_count_(0), |
| 36 device_changed_count_(0), |
| 37 device_removed_count_(0), |
| 38 last_device_(NULL), |
| 39 adapter_(adapter) { |
| 40 } |
| 41 virtual ~TestObserver() {} |
| 42 |
| 43 virtual void AdapterPresentChanged(BluetoothAdapter* adapter, |
| 44 bool present) OVERRIDE { |
| 45 EXPECT_EQ(adapter_, adapter); |
| 46 |
| 47 ++present_changed_count_; |
| 48 last_present_ = present; |
| 49 } |
| 50 |
| 51 virtual void AdapterPoweredChanged(BluetoothAdapter* adapter, |
| 52 bool powered) OVERRIDE { |
| 53 EXPECT_EQ(adapter_, adapter); |
| 54 |
| 55 ++powered_changed_count_; |
| 56 last_powered_ = powered; |
| 57 } |
| 58 |
| 59 virtual void AdapterDiscoveringChanged(BluetoothAdapter* adapter, |
| 60 bool discovering) OVERRIDE { |
| 61 EXPECT_EQ(adapter_, adapter); |
| 62 |
| 63 ++discovering_changed_count_; |
| 64 last_discovering_ = discovering; |
| 65 } |
| 66 |
| 67 virtual void DeviceAdded(BluetoothAdapter* adapter, |
| 68 BluetoothDevice* device) OVERRIDE { |
| 69 EXPECT_EQ(adapter_, adapter); |
| 70 |
| 71 ++device_added_count_; |
| 72 last_device_ = device; |
| 73 last_device_address_ = device->GetAddress(); |
| 74 |
| 75 QuitMessageLoop(); |
| 76 } |
| 77 |
| 78 virtual void DeviceChanged(BluetoothAdapter* adapter, |
| 79 BluetoothDevice* device) OVERRIDE { |
| 80 EXPECT_EQ(adapter_, adapter); |
| 81 |
| 82 ++device_changed_count_; |
| 83 last_device_ = device; |
| 84 last_device_address_ = device->GetAddress(); |
| 85 |
| 86 QuitMessageLoop(); |
| 87 } |
| 88 |
| 89 virtual void DeviceRemoved(BluetoothAdapter* adapter, |
| 90 BluetoothDevice* device) OVERRIDE { |
| 91 EXPECT_EQ(adapter_, adapter); |
| 92 |
| 93 ++device_removed_count_; |
| 94 // Can't save device, it may be freed |
| 95 last_device_address_ = device->GetAddress(); |
| 96 |
| 97 QuitMessageLoop(); |
| 98 } |
| 99 |
| 100 int present_changed_count_; |
| 101 int powered_changed_count_; |
| 102 int discovering_changed_count_; |
| 103 bool last_present_; |
| 104 bool last_powered_; |
| 105 bool last_discovering_; |
| 106 int device_added_count_; |
| 107 int device_changed_count_; |
| 108 int device_removed_count_; |
| 109 BluetoothDevice* last_device_; |
| 110 std::string last_device_address_; |
| 111 |
| 112 private: |
| 113 // Some tests use a message loop since background processing is simulated; |
| 114 // break out of those loops. |
| 115 void QuitMessageLoop() { |
| 116 if (MessageLoop::current() && MessageLoop::current()->is_running()) |
| 117 MessageLoop::current()->Quit(); |
| 118 } |
| 119 |
| 120 scoped_refptr<BluetoothAdapter> adapter_; |
| 121 }; |
| 122 |
| 123 class TestPairingDelegate : public BluetoothDevice::PairingDelegate { |
| 124 public: |
| 125 TestPairingDelegate() |
| 126 : call_count_(0), |
| 127 request_pincode_count_(0), |
| 128 request_passkey_count_(0), |
| 129 display_pincode_count_(0), |
| 130 display_passkey_count_(0), |
| 131 confirm_passkey_count_(0), |
| 132 dismiss_count_(0) {} |
| 133 virtual ~TestPairingDelegate() {} |
| 134 |
| 135 void RequestPinCode(BluetoothDevice* device) OVERRIDE { |
| 136 ++call_count_; |
| 137 ++request_pincode_count_; |
| 138 QuitMessageLoop(); |
| 139 } |
| 140 |
| 141 void RequestPasskey(BluetoothDevice* device) OVERRIDE { |
| 142 ++call_count_; |
| 143 ++request_passkey_count_; |
| 144 QuitMessageLoop(); |
| 145 } |
| 146 |
| 147 void DisplayPinCode(BluetoothDevice* device, |
| 148 const std::string& pincode) OVERRIDE { |
| 149 ++call_count_; |
| 150 ++display_pincode_count_; |
| 151 last_pincode_ = pincode; |
| 152 QuitMessageLoop(); |
| 153 } |
| 154 |
| 155 void DisplayPasskey(BluetoothDevice* device, |
| 156 uint32 passkey) OVERRIDE { |
| 157 ++call_count_; |
| 158 ++display_passkey_count_; |
| 159 last_passkey_ = passkey; |
| 160 QuitMessageLoop(); |
| 161 } |
| 162 |
| 163 void ConfirmPasskey(BluetoothDevice* device, |
| 164 uint32 passkey) OVERRIDE { |
| 165 ++call_count_; |
| 166 ++confirm_passkey_count_; |
| 167 last_passkey_ = passkey; |
| 168 QuitMessageLoop(); |
| 169 } |
| 170 |
| 171 void DismissDisplayOrConfirm() OVERRIDE { |
| 172 ++call_count_; |
| 173 ++dismiss_count_; |
| 174 QuitMessageLoop(); |
| 175 } |
| 176 |
| 177 int call_count_; |
| 178 int request_pincode_count_; |
| 179 int request_passkey_count_; |
| 180 int display_pincode_count_; |
| 181 int display_passkey_count_; |
| 182 int confirm_passkey_count_; |
| 183 int dismiss_count_; |
| 184 uint32 last_passkey_; |
| 185 std::string last_pincode_; |
| 186 |
| 187 private: |
| 188 // Some tests use a message loop since background processing is simulated; |
| 189 // break out of those loops. |
| 190 void QuitMessageLoop() { |
| 191 if (MessageLoop::current() && MessageLoop::current()->is_running()) |
| 192 MessageLoop::current()->Quit(); |
| 193 } |
| 194 }; |
| 195 |
| 196 class BluetoothExperimentalChromeOSTest : public testing::Test { |
| 197 public: |
| 198 virtual void SetUp() { |
| 199 if (!CommandLine::ForCurrentProcess()->HasSwitch( |
| 200 chromeos::switches::kEnableExperimentalBluetooth)) |
| 201 CommandLine::ForCurrentProcess()->AppendSwitch( |
| 202 chromeos::switches::kEnableExperimentalBluetooth); |
| 203 |
| 204 mock_dbus_thread_manager_ = |
| 205 new MockDBusThreadManagerWithoutGMock(); |
| 206 DBusThreadManager::InitializeForTesting(mock_dbus_thread_manager_); |
| 207 |
| 208 fake_bluetooth_adapter_client_ = |
| 209 mock_dbus_thread_manager_->fake_bluetooth_adapter_client(); |
| 210 fake_bluetooth_device_client_ = |
| 211 mock_dbus_thread_manager_->fake_bluetooth_device_client(); |
| 212 |
| 213 callback_count_ = 0; |
| 214 error_callback_count_ = 0; |
| 215 last_connect_error_ = BluetoothDevice::ERROR_UNKNOWN; |
| 216 } |
| 217 |
| 218 virtual void TearDown() { |
| 219 adapter_ = NULL; |
| 220 DBusThreadManager::Shutdown(); |
| 221 } |
| 222 |
| 223 // Generic callbacks |
| 224 void Callback() { |
| 225 ++callback_count_; |
| 226 } |
| 227 |
| 228 void ErrorCallback() { |
| 229 ++error_callback_count_; |
| 230 } |
| 231 |
| 232 void ConnectErrorCallback(enum BluetoothDevice::ConnectErrorCode error) { |
| 233 ++error_callback_count_; |
| 234 last_connect_error_ = error; |
| 235 } |
| 236 |
| 237 // Call to fill the adapter_ member with a BluetoothAdapter instance. |
| 238 void GetAdapter() { |
| 239 adapter_ = new BluetoothAdapterExperimentalChromeOS(); |
| 240 ASSERT_TRUE(adapter_ != NULL); |
| 241 ASSERT_TRUE(adapter_->IsInitialized()); |
| 242 } |
| 243 |
| 244 // Run a discovery phase until the named device is detected, or if the named |
| 245 // device is not created, the discovery process ends without finding it. |
| 246 // |
| 247 // The correct behavior of discovery is tested by the "Discovery" test case |
| 248 // without using this function. |
| 249 void DiscoverDevice(const std::string& address) { |
| 250 ASSERT_TRUE(adapter_ != NULL); |
| 251 |
| 252 if (MessageLoop::current() == NULL) { |
| 253 base::MessageLoop message_loop(MessageLoop::TYPE_DEFAULT); |
| 254 DiscoverDevices(); |
| 255 return; |
| 256 } |
| 257 |
| 258 fake_bluetooth_device_client_->SetSimulationIntervalMs(10); |
| 259 |
| 260 TestObserver observer(adapter_); |
| 261 adapter_->AddObserver(&observer); |
| 262 |
| 263 adapter_->SetPowered( |
| 264 true, |
| 265 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 266 base::Unretained(this)), |
| 267 base::Bind(&BluetoothExperimentalChromeOSTest::ErrorCallback, |
| 268 base::Unretained(this))); |
| 269 adapter_->StartDiscovering( |
| 270 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 271 base::Unretained(this)), |
| 272 base::Bind(&BluetoothExperimentalChromeOSTest::ErrorCallback, |
| 273 base::Unretained(this))); |
| 274 ASSERT_EQ(2, callback_count_); |
| 275 ASSERT_EQ(0, error_callback_count_); |
| 276 callback_count_ = 0; |
| 277 |
| 278 ASSERT_TRUE(adapter_->IsPowered()); |
| 279 ASSERT_TRUE(adapter_->IsDiscovering()); |
| 280 |
| 281 while (!observer.device_removed_count_ && |
| 282 observer.last_device_address_ != address) |
| 283 MessageLoop::current()->Run(); |
| 284 |
| 285 adapter_->StopDiscovering( |
| 286 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 287 base::Unretained(this)), |
| 288 base::Bind(&BluetoothExperimentalChromeOSTest::ErrorCallback, |
| 289 base::Unretained(this))); |
| 290 ASSERT_EQ(1, callback_count_); |
| 291 ASSERT_EQ(0, error_callback_count_); |
| 292 callback_count_ = 0; |
| 293 |
| 294 ASSERT_FALSE(adapter_->IsDiscovering()); |
| 295 |
| 296 adapter_->RemoveObserver(&observer); |
| 297 } |
| 298 |
| 299 // Run a discovery phase so we have devices that can be paired with. |
| 300 void DiscoverDevices() { |
| 301 // Pass an invalid address for the device so that the discovery process |
| 302 // completes with all devices. |
| 303 DiscoverDevice("does not exist"); |
| 304 } |
| 305 |
| 306 protected: |
| 307 FakeBluetoothAdapterClient* fake_bluetooth_adapter_client_; |
| 308 FakeBluetoothDeviceClient* fake_bluetooth_device_client_; |
| 309 MockDBusThreadManagerWithoutGMock* mock_dbus_thread_manager_; |
| 310 scoped_refptr<BluetoothAdapter> adapter_; |
| 311 |
| 312 int callback_count_; |
| 313 int error_callback_count_; |
| 314 enum BluetoothDevice::ConnectErrorCode last_connect_error_; |
| 315 }; |
| 316 |
| 317 TEST_F(BluetoothExperimentalChromeOSTest, AlreadyPresent) { |
| 318 GetAdapter(); |
| 319 |
| 320 // This verifies that the class gets the list of adapters when created; |
| 321 // and initializes with an existing adapter if there is one. |
| 322 EXPECT_TRUE(adapter_->IsPresent()); |
| 323 EXPECT_FALSE(adapter_->IsPowered()); |
| 324 EXPECT_EQ(FakeBluetoothAdapterClient::kAdapterAddress, |
| 325 adapter_->GetAddress()); |
| 326 EXPECT_EQ(FakeBluetoothAdapterClient::kAdapterName, adapter_->GetName()); |
| 327 EXPECT_FALSE(adapter_->IsDiscovering()); |
| 328 |
| 329 // There should be a device |
| 330 BluetoothAdapter::DeviceList devices = adapter_->GetDevices(); |
| 331 EXPECT_EQ(1U, devices.size()); |
| 332 EXPECT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress, |
| 333 devices[0]->GetAddress()); |
| 334 } |
| 335 |
| 336 TEST_F(BluetoothExperimentalChromeOSTest, BecomePresent) { |
| 337 fake_bluetooth_adapter_client_->SetVisible(false); |
| 338 GetAdapter(); |
| 339 ASSERT_FALSE(adapter_->IsPresent()); |
| 340 |
| 341 // Install an observer; expect the AdapterPresentChanged to be called |
| 342 // with true, and IsPresent() to return true. |
| 343 TestObserver observer(adapter_); |
| 344 adapter_->AddObserver(&observer); |
| 345 |
| 346 fake_bluetooth_adapter_client_->SetVisible(true); |
| 347 |
| 348 EXPECT_EQ(1, observer.present_changed_count_); |
| 349 EXPECT_TRUE(observer.last_present_); |
| 350 |
| 351 EXPECT_TRUE(adapter_->IsPresent()); |
| 352 |
| 353 // We should have had a device announced. |
| 354 EXPECT_EQ(1, observer.device_added_count_); |
| 355 EXPECT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress, |
| 356 observer.last_device_address_); |
| 357 |
| 358 // Other callbacks shouldn't be called if the values are false. |
| 359 EXPECT_EQ(0, observer.powered_changed_count_); |
| 360 EXPECT_EQ(0, observer.discovering_changed_count_); |
| 361 EXPECT_FALSE(adapter_->IsPowered()); |
| 362 EXPECT_FALSE(adapter_->IsDiscovering()); |
| 363 } |
| 364 |
| 365 TEST_F(BluetoothExperimentalChromeOSTest, BecomeNotPresent) { |
| 366 GetAdapter(); |
| 367 ASSERT_TRUE(adapter_->IsPresent()); |
| 368 |
| 369 // Install an observer; expect the AdapterPresentChanged to be called |
| 370 // with false, and IsPresent() to return false. |
| 371 TestObserver observer(adapter_); |
| 372 adapter_->AddObserver(&observer); |
| 373 |
| 374 fake_bluetooth_adapter_client_->SetVisible(false); |
| 375 |
| 376 EXPECT_EQ(1, observer.present_changed_count_); |
| 377 EXPECT_FALSE(observer.last_present_); |
| 378 |
| 379 EXPECT_FALSE(adapter_->IsPresent()); |
| 380 |
| 381 // We should have had a device removed. |
| 382 EXPECT_EQ(1, observer.device_removed_count_); |
| 383 EXPECT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress, |
| 384 observer.last_device_address_); |
| 385 |
| 386 // Other callbacks shouldn't be called since the values are false. |
| 387 EXPECT_EQ(0, observer.powered_changed_count_); |
| 388 EXPECT_EQ(0, observer.discovering_changed_count_); |
| 389 EXPECT_FALSE(adapter_->IsPowered()); |
| 390 EXPECT_FALSE(adapter_->IsDiscovering()); |
| 391 } |
| 392 |
| 393 TEST_F(BluetoothExperimentalChromeOSTest, SecondAdapter) { |
| 394 GetAdapter(); |
| 395 ASSERT_TRUE(adapter_->IsPresent()); |
| 396 |
| 397 // Install an observer, then add a second adapter. Nothing should change, |
| 398 // we ignore the second adapter. |
| 399 TestObserver observer(adapter_); |
| 400 adapter_->AddObserver(&observer); |
| 401 |
| 402 fake_bluetooth_adapter_client_->SetSecondVisible(true); |
| 403 |
| 404 EXPECT_EQ(0, observer.present_changed_count_); |
| 405 |
| 406 EXPECT_TRUE(adapter_->IsPresent()); |
| 407 EXPECT_EQ(FakeBluetoothAdapterClient::kAdapterAddress, |
| 408 adapter_->GetAddress()); |
| 409 |
| 410 // Try removing the first adapter, we should now act as if the adapter |
| 411 // is no longer present rather than fall back to the second. |
| 412 fake_bluetooth_adapter_client_->SetVisible(false); |
| 413 |
| 414 EXPECT_EQ(1, observer.present_changed_count_); |
| 415 EXPECT_FALSE(observer.last_present_); |
| 416 |
| 417 EXPECT_FALSE(adapter_->IsPresent()); |
| 418 |
| 419 // We should have had a device removed. |
| 420 EXPECT_EQ(1, observer.device_removed_count_); |
| 421 EXPECT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress, |
| 422 observer.last_device_address_); |
| 423 |
| 424 // Other callbacks shouldn't be called since the values are false. |
| 425 EXPECT_EQ(0, observer.powered_changed_count_); |
| 426 EXPECT_EQ(0, observer.discovering_changed_count_); |
| 427 EXPECT_FALSE(adapter_->IsPowered()); |
| 428 EXPECT_FALSE(adapter_->IsDiscovering()); |
| 429 |
| 430 observer.device_removed_count_ = 0; |
| 431 |
| 432 // Removing the second adapter shouldn't set anything either. |
| 433 fake_bluetooth_adapter_client_->SetSecondVisible(false); |
| 434 |
| 435 EXPECT_EQ(0, observer.device_removed_count_); |
| 436 EXPECT_EQ(0, observer.powered_changed_count_); |
| 437 EXPECT_EQ(0, observer.discovering_changed_count_); |
| 438 } |
| 439 |
| 440 TEST_F(BluetoothExperimentalChromeOSTest, BecomePowered) { |
| 441 GetAdapter(); |
| 442 ASSERT_FALSE(adapter_->IsPowered()); |
| 443 |
| 444 // Install an observer; expect the AdapterPoweredChanged to be called |
| 445 // with true, and IsPowered() to return true. |
| 446 TestObserver observer(adapter_); |
| 447 adapter_->AddObserver(&observer); |
| 448 |
| 449 adapter_->SetPowered( |
| 450 true, |
| 451 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 452 base::Unretained(this)), |
| 453 base::Bind(&BluetoothExperimentalChromeOSTest::ErrorCallback, |
| 454 base::Unretained(this))); |
| 455 EXPECT_EQ(1, callback_count_); |
| 456 EXPECT_EQ(0, error_callback_count_); |
| 457 |
| 458 EXPECT_EQ(1, observer.powered_changed_count_); |
| 459 EXPECT_TRUE(observer.last_powered_); |
| 460 |
| 461 EXPECT_TRUE(adapter_->IsPowered()); |
| 462 } |
| 463 |
| 464 TEST_F(BluetoothExperimentalChromeOSTest, BecomeNotPowered) { |
| 465 GetAdapter(); |
| 466 adapter_->SetPowered( |
| 467 true, |
| 468 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 469 base::Unretained(this)), |
| 470 base::Bind(&BluetoothExperimentalChromeOSTest::ErrorCallback, |
| 471 base::Unretained(this))); |
| 472 EXPECT_EQ(1, callback_count_); |
| 473 EXPECT_EQ(0, error_callback_count_); |
| 474 callback_count_ = 0; |
| 475 |
| 476 ASSERT_TRUE(adapter_->IsPowered()); |
| 477 |
| 478 // Install an observer; expect the AdapterPoweredChanged to be called |
| 479 // with false, and IsPowered() to return false. |
| 480 TestObserver observer(adapter_); |
| 481 adapter_->AddObserver(&observer); |
| 482 |
| 483 adapter_->SetPowered( |
| 484 false, |
| 485 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 486 base::Unretained(this)), |
| 487 base::Bind(&BluetoothExperimentalChromeOSTest::ErrorCallback, |
| 488 base::Unretained(this))); |
| 489 EXPECT_EQ(1, callback_count_); |
| 490 EXPECT_EQ(0, error_callback_count_); |
| 491 |
| 492 EXPECT_EQ(1, observer.powered_changed_count_); |
| 493 EXPECT_FALSE(observer.last_powered_); |
| 494 |
| 495 EXPECT_FALSE(adapter_->IsPowered()); |
| 496 } |
| 497 |
| 498 TEST_F(BluetoothExperimentalChromeOSTest, StopDiscovery) { |
| 499 base::MessageLoop message_loop(MessageLoop::TYPE_DEFAULT); |
| 500 |
| 501 GetAdapter(); |
| 502 |
| 503 adapter_->SetPowered( |
| 504 true, |
| 505 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 506 base::Unretained(this)), |
| 507 base::Bind(&BluetoothExperimentalChromeOSTest::ErrorCallback, |
| 508 base::Unretained(this))); |
| 509 adapter_->StartDiscovering( |
| 510 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 511 base::Unretained(this)), |
| 512 base::Bind(&BluetoothExperimentalChromeOSTest::ErrorCallback, |
| 513 base::Unretained(this))); |
| 514 EXPECT_EQ(2, callback_count_); |
| 515 EXPECT_EQ(0, error_callback_count_); |
| 516 callback_count_ = 0; |
| 517 |
| 518 ASSERT_TRUE(adapter_->IsPowered()); |
| 519 ASSERT_TRUE(adapter_->IsDiscovering()); |
| 520 |
| 521 // Install an observer; aside from the callback, expect the |
| 522 // AdapterDiscoveringChanged method to be called and no longer to be |
| 523 // discovering, |
| 524 TestObserver observer(adapter_); |
| 525 adapter_->AddObserver(&observer); |
| 526 |
| 527 adapter_->StopDiscovering( |
| 528 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 529 base::Unretained(this)), |
| 530 base::Bind(&BluetoothExperimentalChromeOSTest::ErrorCallback, |
| 531 base::Unretained(this))); |
| 532 EXPECT_EQ(1, callback_count_); |
| 533 EXPECT_EQ(0, error_callback_count_); |
| 534 |
| 535 EXPECT_EQ(1, observer.discovering_changed_count_); |
| 536 EXPECT_FALSE(observer.last_discovering_); |
| 537 |
| 538 EXPECT_FALSE(adapter_->IsDiscovering()); |
| 539 } |
| 540 |
| 541 TEST_F(BluetoothExperimentalChromeOSTest, StopDiscoveryAfterTwoStarts) { |
| 542 base::MessageLoop message_loop(MessageLoop::TYPE_DEFAULT); |
| 543 |
| 544 GetAdapter(); |
| 545 |
| 546 adapter_->SetPowered( |
| 547 true, |
| 548 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 549 base::Unretained(this)), |
| 550 base::Bind(&BluetoothExperimentalChromeOSTest::ErrorCallback, |
| 551 base::Unretained(this))); |
| 552 adapter_->StartDiscovering( |
| 553 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 554 base::Unretained(this)), |
| 555 base::Bind(&BluetoothExperimentalChromeOSTest::ErrorCallback, |
| 556 base::Unretained(this))); |
| 557 EXPECT_EQ(2, callback_count_); |
| 558 EXPECT_EQ(0, error_callback_count_); |
| 559 callback_count_ = 0; |
| 560 |
| 561 ASSERT_TRUE(adapter_->IsPowered()); |
| 562 ASSERT_TRUE(adapter_->IsDiscovering()); |
| 563 |
| 564 // Install an observer and start discovering again; only the callback |
| 565 // should be called since we were already discovering to begin with. |
| 566 TestObserver observer(adapter_); |
| 567 adapter_->AddObserver(&observer); |
| 568 |
| 569 adapter_->StartDiscovering( |
| 570 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 571 base::Unretained(this)), |
| 572 base::Bind(&BluetoothExperimentalChromeOSTest::ErrorCallback, |
| 573 base::Unretained(this))); |
| 574 EXPECT_EQ(1, callback_count_); |
| 575 EXPECT_EQ(0, error_callback_count_); |
| 576 callback_count_ = 0; |
| 577 |
| 578 EXPECT_EQ(0, observer.discovering_changed_count_); |
| 579 |
| 580 // Stop discovering; only the callback should be called since we're still |
| 581 // discovering. The adapter should be still discovering. |
| 582 adapter_->StopDiscovering( |
| 583 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 584 base::Unretained(this)), |
| 585 base::Bind(&BluetoothExperimentalChromeOSTest::ErrorCallback, |
| 586 base::Unretained(this))); |
| 587 EXPECT_EQ(1, callback_count_); |
| 588 EXPECT_EQ(0, error_callback_count_); |
| 589 callback_count_ = 0; |
| 590 |
| 591 EXPECT_EQ(0, observer.discovering_changed_count_); |
| 592 |
| 593 EXPECT_TRUE(adapter_->IsDiscovering()); |
| 594 |
| 595 // Stop discovering one more time; aside from the callback, expect the |
| 596 // AdapterDiscoveringChanged method to be called and no longer to be |
| 597 // discovering, |
| 598 adapter_->StopDiscovering( |
| 599 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 600 base::Unretained(this)), |
| 601 base::Bind(&BluetoothExperimentalChromeOSTest::ErrorCallback, |
| 602 base::Unretained(this))); |
| 603 EXPECT_EQ(1, callback_count_); |
| 604 EXPECT_EQ(0, error_callback_count_); |
| 605 |
| 606 EXPECT_EQ(1, observer.discovering_changed_count_); |
| 607 EXPECT_FALSE(observer.last_discovering_); |
| 608 |
| 609 EXPECT_FALSE(adapter_->IsDiscovering()); |
| 610 } |
| 611 |
| 612 TEST_F(BluetoothExperimentalChromeOSTest, Discovery) { |
| 613 // Test a simulated discovery session. |
| 614 base::MessageLoop message_loop(MessageLoop::TYPE_DEFAULT); |
| 615 |
| 616 fake_bluetooth_device_client_->SetSimulationIntervalMs(10); |
| 617 GetAdapter(); |
| 618 |
| 619 TestObserver observer(adapter_); |
| 620 adapter_->AddObserver(&observer); |
| 621 |
| 622 adapter_->SetPowered( |
| 623 true, |
| 624 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 625 base::Unretained(this)), |
| 626 base::Bind(&BluetoothExperimentalChromeOSTest::ErrorCallback, |
| 627 base::Unretained(this))); |
| 628 adapter_->StartDiscovering( |
| 629 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 630 base::Unretained(this)), |
| 631 base::Bind(&BluetoothExperimentalChromeOSTest::ErrorCallback, |
| 632 base::Unretained(this))); |
| 633 EXPECT_EQ(2, callback_count_); |
| 634 EXPECT_EQ(0, error_callback_count_); |
| 635 callback_count_ = 0; |
| 636 |
| 637 ASSERT_TRUE(adapter_->IsPowered()); |
| 638 ASSERT_TRUE(adapter_->IsDiscovering()); |
| 639 |
| 640 // First device to appear should be an Apple Mouse. |
| 641 message_loop.Run(); |
| 642 |
| 643 EXPECT_EQ(1, observer.device_added_count_); |
| 644 EXPECT_EQ(FakeBluetoothDeviceClient::kAppleMouseAddress, |
| 645 observer.last_device_address_); |
| 646 |
| 647 // Next we should get another two devices... |
| 648 message_loop.Run(); |
| 649 EXPECT_EQ(3, observer.device_added_count_); |
| 650 |
| 651 // Okay, let's run forward until a device is actually removed... |
| 652 while (!observer.device_removed_count_) |
| 653 message_loop.Run(); |
| 654 |
| 655 EXPECT_EQ(1, observer.device_removed_count_); |
| 656 EXPECT_EQ(FakeBluetoothDeviceClient::kVanishingDeviceAddress, |
| 657 observer.last_device_address_); |
| 658 } |
| 659 |
| 660 TEST_F(BluetoothExperimentalChromeOSTest, PoweredAndDiscovering) { |
| 661 base::MessageLoop message_loop(MessageLoop::TYPE_DEFAULT); |
| 662 |
| 663 GetAdapter(); |
| 664 adapter_->SetPowered( |
| 665 true, |
| 666 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 667 base::Unretained(this)), |
| 668 base::Bind(&BluetoothExperimentalChromeOSTest::ErrorCallback, |
| 669 base::Unretained(this))); |
| 670 adapter_->StartDiscovering( |
| 671 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 672 base::Unretained(this)), |
| 673 base::Bind(&BluetoothExperimentalChromeOSTest::ErrorCallback, |
| 674 base::Unretained(this))); |
| 675 EXPECT_EQ(2, callback_count_); |
| 676 EXPECT_EQ(0, error_callback_count_); |
| 677 callback_count_ = 0; |
| 678 |
| 679 // Stop the timers that the simulation uses |
| 680 fake_bluetooth_device_client_->EndDiscoverySimulation( |
| 681 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath)); |
| 682 |
| 683 ASSERT_TRUE(adapter_->IsPowered()); |
| 684 ASSERT_TRUE(adapter_->IsDiscovering()); |
| 685 |
| 686 fake_bluetooth_adapter_client_->SetVisible(false); |
| 687 ASSERT_FALSE(adapter_->IsPresent()); |
| 688 |
| 689 // Install an observer; expect the AdapterPresentChanged, |
| 690 // AdapterPoweredChanged and AdapterDiscoveringChanged methods to be called |
| 691 // with true, and IsPresent(), IsPowered() and IsDiscovering() to all |
| 692 // return true. |
| 693 TestObserver observer(adapter_); |
| 694 adapter_->AddObserver(&observer); |
| 695 |
| 696 fake_bluetooth_adapter_client_->SetVisible(true); |
| 697 |
| 698 EXPECT_EQ(1, observer.present_changed_count_); |
| 699 EXPECT_TRUE(observer.last_present_); |
| 700 EXPECT_TRUE(adapter_->IsPresent()); |
| 701 |
| 702 EXPECT_EQ(1, observer.powered_changed_count_); |
| 703 EXPECT_TRUE(observer.last_powered_); |
| 704 EXPECT_TRUE(adapter_->IsPowered()); |
| 705 |
| 706 EXPECT_EQ(1, observer.discovering_changed_count_); |
| 707 EXPECT_TRUE(observer.last_discovering_); |
| 708 EXPECT_TRUE(adapter_->IsDiscovering()); |
| 709 |
| 710 observer.present_changed_count_ = 0; |
| 711 observer.powered_changed_count_ = 0; |
| 712 observer.discovering_changed_count_ = 0; |
| 713 |
| 714 // Now mark the adapter not present again. Expect the methods to be called |
| 715 // again, to reset the properties back to false |
| 716 fake_bluetooth_adapter_client_->SetVisible(false); |
| 717 |
| 718 EXPECT_EQ(1, observer.present_changed_count_); |
| 719 EXPECT_FALSE(observer.last_present_); |
| 720 EXPECT_FALSE(adapter_->IsPresent()); |
| 721 |
| 722 EXPECT_EQ(1, observer.powered_changed_count_); |
| 723 EXPECT_FALSE(observer.last_powered_); |
| 724 EXPECT_FALSE(adapter_->IsPowered()); |
| 725 |
| 726 EXPECT_EQ(1, observer.discovering_changed_count_); |
| 727 EXPECT_FALSE(observer.last_discovering_); |
| 728 EXPECT_FALSE(adapter_->IsDiscovering()); |
| 729 } |
| 730 |
| 731 TEST_F(BluetoothExperimentalChromeOSTest, DeviceProperties) { |
| 732 GetAdapter(); |
| 733 |
| 734 BluetoothAdapter::DeviceList devices = adapter_->GetDevices(); |
| 735 ASSERT_EQ(1U, devices.size()); |
| 736 ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress, |
| 737 devices[0]->GetAddress()); |
| 738 |
| 739 // Verify the other device properties. |
| 740 EXPECT_EQ(UTF8ToUTF16(FakeBluetoothDeviceClient::kPairedDeviceName), |
| 741 devices[0]->GetName()); |
| 742 EXPECT_EQ(BluetoothDevice::DEVICE_COMPUTER, devices[0]->GetDeviceType()); |
| 743 EXPECT_TRUE(devices[0]->IsPaired()); |
| 744 EXPECT_FALSE(devices[0]->IsConnected()); |
| 745 EXPECT_FALSE(devices[0]->IsConnectable()); |
| 746 EXPECT_FALSE(devices[0]->IsConnecting()); |
| 747 |
| 748 BluetoothDevice::ServiceList uuids = devices[0]->GetServices(); |
| 749 ASSERT_EQ(2U, uuids.size()); |
| 750 EXPECT_EQ(uuids[0], "00001800-0000-1000-8000-00805f9b34fb"); |
| 751 EXPECT_EQ(uuids[1], "00001801-0000-1000-8000-00805f9b34fb"); |
| 752 } |
| 753 |
| 754 TEST_F(BluetoothExperimentalChromeOSTest, DeviceClassChanged) { |
| 755 // Simulate a change of class of a device, as sometimes occurs |
| 756 // during discovery. |
| 757 GetAdapter(); |
| 758 |
| 759 BluetoothAdapter::DeviceList devices = adapter_->GetDevices(); |
| 760 ASSERT_EQ(1U, devices.size()); |
| 761 ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress, |
| 762 devices[0]->GetAddress()); |
| 763 ASSERT_EQ(BluetoothDevice::DEVICE_COMPUTER, devices[0]->GetDeviceType()); |
| 764 |
| 765 // Install an observer; expect the DeviceChanged method to be called when |
| 766 // we change the class of the device. |
| 767 TestObserver observer(adapter_); |
| 768 adapter_->AddObserver(&observer); |
| 769 |
| 770 FakeBluetoothDeviceClient::Properties* properties = |
| 771 fake_bluetooth_device_client_->GetProperties( |
| 772 dbus::ObjectPath(FakeBluetoothDeviceClient::kPairedDevicePath)); |
| 773 |
| 774 properties->bluetooth_class.ReplaceValue(0x002580); |
| 775 properties->NotifyPropertyChanged(properties->bluetooth_class.name()); |
| 776 |
| 777 EXPECT_EQ(1, observer.device_changed_count_); |
| 778 EXPECT_EQ(devices[0], observer.last_device_); |
| 779 |
| 780 EXPECT_EQ(BluetoothDevice::DEVICE_MOUSE, devices[0]->GetDeviceType()); |
| 781 } |
| 782 |
| 783 TEST_F(BluetoothExperimentalChromeOSTest, DeviceNameChanged) { |
| 784 // Simulate a change of name of a device. |
| 785 GetAdapter(); |
| 786 |
| 787 BluetoothAdapter::DeviceList devices = adapter_->GetDevices(); |
| 788 ASSERT_EQ(1U, devices.size()); |
| 789 ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress, |
| 790 devices[0]->GetAddress()); |
| 791 ASSERT_EQ(UTF8ToUTF16(FakeBluetoothDeviceClient::kPairedDeviceName), |
| 792 devices[0]->GetName()); |
| 793 |
| 794 // Install an observer; expect the DeviceChanged method to be called when |
| 795 // we change the alias of the device. |
| 796 TestObserver observer(adapter_); |
| 797 adapter_->AddObserver(&observer); |
| 798 |
| 799 FakeBluetoothDeviceClient::Properties* properties = |
| 800 fake_bluetooth_device_client_->GetProperties( |
| 801 dbus::ObjectPath(FakeBluetoothDeviceClient::kPairedDevicePath)); |
| 802 |
| 803 static const std::string new_name("New Device Name"); |
| 804 properties->alias.ReplaceValue(new_name); |
| 805 properties->NotifyPropertyChanged(properties->alias.name()); |
| 806 |
| 807 EXPECT_EQ(1, observer.device_changed_count_); |
| 808 EXPECT_EQ(devices[0], observer.last_device_); |
| 809 |
| 810 EXPECT_EQ(UTF8ToUTF16(new_name), devices[0]->GetName()); |
| 811 } |
| 812 |
| 813 TEST_F(BluetoothExperimentalChromeOSTest, DeviceUuidsChanged) { |
| 814 // Simulate a change of advertised services of a device. |
| 815 GetAdapter(); |
| 816 |
| 817 BluetoothAdapter::DeviceList devices = adapter_->GetDevices(); |
| 818 ASSERT_EQ(1U, devices.size()); |
| 819 ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress, |
| 820 devices[0]->GetAddress()); |
| 821 |
| 822 BluetoothDevice::ServiceList uuids = devices[0]->GetServices(); |
| 823 ASSERT_EQ(2U, uuids.size()); |
| 824 ASSERT_EQ(uuids[0], "00001800-0000-1000-8000-00805f9b34fb"); |
| 825 ASSERT_EQ(uuids[1], "00001801-0000-1000-8000-00805f9b34fb"); |
| 826 |
| 827 // Install an observer; expect the DeviceChanged method to be called when |
| 828 // we change the class of the device. |
| 829 TestObserver observer(adapter_); |
| 830 adapter_->AddObserver(&observer); |
| 831 |
| 832 FakeBluetoothDeviceClient::Properties* properties = |
| 833 fake_bluetooth_device_client_->GetProperties( |
| 834 dbus::ObjectPath(FakeBluetoothDeviceClient::kPairedDevicePath)); |
| 835 |
| 836 uuids.push_back("0000110c-0000-1000-8000-00805f9b34fb"); |
| 837 uuids.push_back("0000110e-0000-1000-8000-00805f9b34fb"); |
| 838 uuids.push_back("0000110a-0000-1000-8000-00805f9b34fb"); |
| 839 |
| 840 properties->uuids.ReplaceValue(uuids); |
| 841 properties->NotifyPropertyChanged(properties->uuids.name()); |
| 842 |
| 843 EXPECT_EQ(1, observer.device_changed_count_); |
| 844 EXPECT_EQ(devices[0], observer.last_device_); |
| 845 |
| 846 // Fetching the value should give the new one. |
| 847 uuids = devices[0]->GetServices(); |
| 848 ASSERT_EQ(5U, uuids.size()); |
| 849 EXPECT_EQ(uuids[0], "00001800-0000-1000-8000-00805f9b34fb"); |
| 850 EXPECT_EQ(uuids[1], "00001801-0000-1000-8000-00805f9b34fb"); |
| 851 EXPECT_EQ(uuids[2], "0000110c-0000-1000-8000-00805f9b34fb"); |
| 852 EXPECT_EQ(uuids[3], "0000110e-0000-1000-8000-00805f9b34fb"); |
| 853 EXPECT_EQ(uuids[4], "0000110a-0000-1000-8000-00805f9b34fb"); |
| 854 } |
| 855 |
| 856 TEST_F(BluetoothExperimentalChromeOSTest, ForgetDevice) { |
| 857 GetAdapter(); |
| 858 |
| 859 BluetoothAdapter::DeviceList devices = adapter_->GetDevices(); |
| 860 ASSERT_EQ(1U, devices.size()); |
| 861 ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress, |
| 862 devices[0]->GetAddress()); |
| 863 |
| 864 std::string address = devices[0]->GetAddress(); |
| 865 |
| 866 // Install an observer; expect the DeviceRemoved method to be called |
| 867 // with the device we remove. |
| 868 TestObserver observer(adapter_); |
| 869 adapter_->AddObserver(&observer); |
| 870 |
| 871 devices[0]->Forget( |
| 872 base::Bind(&BluetoothExperimentalChromeOSTest::ErrorCallback, |
| 873 base::Unretained(this))); |
| 874 EXPECT_EQ(0, error_callback_count_); |
| 875 |
| 876 EXPECT_EQ(1, observer.device_removed_count_); |
| 877 EXPECT_EQ(address, observer.last_device_address_); |
| 878 |
| 879 // GetDevices shouldn't return the device either. |
| 880 devices = adapter_->GetDevices(); |
| 881 ASSERT_EQ(0U, devices.size()); |
| 882 } |
| 883 |
| 884 TEST_F(BluetoothExperimentalChromeOSTest, ConnectPairedDevice) { |
| 885 GetAdapter(); |
| 886 |
| 887 BluetoothDevice* device = adapter_->GetDevice( |
| 888 FakeBluetoothDeviceClient::kPairedDeviceAddress); |
| 889 ASSERT_TRUE(device != NULL); |
| 890 ASSERT_TRUE(device->IsPaired()); |
| 891 |
| 892 TestObserver observer(adapter_); |
| 893 adapter_->AddObserver(&observer); |
| 894 |
| 895 // Connect without a pairing delegate; since the device is already Paired |
| 896 // this should succeed and the device should become connected. |
| 897 device->Connect( |
| 898 NULL, |
| 899 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 900 base::Unretained(this)), |
| 901 base::Bind(&BluetoothExperimentalChromeOSTest::ConnectErrorCallback, |
| 902 base::Unretained(this))); |
| 903 |
| 904 EXPECT_EQ(1, callback_count_); |
| 905 EXPECT_EQ(0, error_callback_count_); |
| 906 |
| 907 EXPECT_EQ(1, observer.device_changed_count_); |
| 908 EXPECT_EQ(device, observer.last_device_); |
| 909 |
| 910 EXPECT_TRUE(device->IsConnected()); |
| 911 EXPECT_FALSE(device->IsConnecting()); |
| 912 } |
| 913 |
| 914 TEST_F(BluetoothExperimentalChromeOSTest, ConnectUnpairableDevice) { |
| 915 GetAdapter(); |
| 916 DiscoverDevices(); |
| 917 |
| 918 BluetoothDevice* device = adapter_->GetDevice( |
| 919 FakeBluetoothDeviceClient::kMicrosoftMouseAddress); |
| 920 ASSERT_TRUE(device != NULL); |
| 921 ASSERT_FALSE(device->IsPaired()); |
| 922 |
| 923 TestObserver observer(adapter_); |
| 924 adapter_->AddObserver(&observer); |
| 925 |
| 926 // Connect without a pairing delegate; since the device does not require |
| 927 // pairing, this should succeed and the device should become connected. |
| 928 device->Connect( |
| 929 NULL, |
| 930 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 931 base::Unretained(this)), |
| 932 base::Bind(&BluetoothExperimentalChromeOSTest::ConnectErrorCallback, |
| 933 base::Unretained(this))); |
| 934 |
| 935 EXPECT_EQ(1, callback_count_); |
| 936 EXPECT_EQ(0, error_callback_count_); |
| 937 |
| 938 EXPECT_EQ(1, observer.device_changed_count_); |
| 939 EXPECT_EQ(device, observer.last_device_); |
| 940 |
| 941 EXPECT_TRUE(device->IsConnected()); |
| 942 EXPECT_FALSE(device->IsConnecting()); |
| 943 } |
| 944 |
| 945 TEST_F(BluetoothExperimentalChromeOSTest, ConnectConnectedDevice) { |
| 946 GetAdapter(); |
| 947 |
| 948 BluetoothDevice* device = adapter_->GetDevice( |
| 949 FakeBluetoothDeviceClient::kPairedDeviceAddress); |
| 950 ASSERT_TRUE(device != NULL); |
| 951 ASSERT_TRUE(device->IsPaired()); |
| 952 |
| 953 device->Connect( |
| 954 NULL, |
| 955 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 956 base::Unretained(this)), |
| 957 base::Bind(&BluetoothExperimentalChromeOSTest::ConnectErrorCallback, |
| 958 base::Unretained(this))); |
| 959 |
| 960 ASSERT_EQ(1, callback_count_); |
| 961 ASSERT_EQ(0, error_callback_count_); |
| 962 callback_count_ = 0; |
| 963 |
| 964 ASSERT_TRUE(device->IsConnected()); |
| 965 |
| 966 // Connect again; since the device is already Connected, this shouldn't do |
| 967 // anything, not even the observer method should be called. |
| 968 TestObserver observer(adapter_); |
| 969 adapter_->AddObserver(&observer); |
| 970 |
| 971 device->Connect( |
| 972 NULL, |
| 973 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 974 base::Unretained(this)), |
| 975 base::Bind(&BluetoothExperimentalChromeOSTest::ConnectErrorCallback, |
| 976 base::Unretained(this))); |
| 977 |
| 978 EXPECT_EQ(1, callback_count_); |
| 979 EXPECT_EQ(0, error_callback_count_); |
| 980 |
| 981 EXPECT_EQ(0, observer.device_changed_count_); |
| 982 |
| 983 EXPECT_TRUE(device->IsConnected()); |
| 984 EXPECT_FALSE(device->IsConnecting()); |
| 985 } |
| 986 |
| 987 TEST_F(BluetoothExperimentalChromeOSTest, ConnectDeviceFails) { |
| 988 GetAdapter(); |
| 989 DiscoverDevices(); |
| 990 |
| 991 BluetoothDevice* device = adapter_->GetDevice( |
| 992 FakeBluetoothDeviceClient::kAppleMouseAddress); |
| 993 ASSERT_TRUE(device != NULL); |
| 994 ASSERT_FALSE(device->IsPaired()); |
| 995 |
| 996 TestObserver observer(adapter_); |
| 997 adapter_->AddObserver(&observer); |
| 998 |
| 999 // Connect without a pairing delegate; since the device requires pairing, |
| 1000 // this should fail with an error. |
| 1001 device->Connect( |
| 1002 NULL, |
| 1003 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 1004 base::Unretained(this)), |
| 1005 base::Bind(&BluetoothExperimentalChromeOSTest::ConnectErrorCallback, |
| 1006 base::Unretained(this))); |
| 1007 |
| 1008 EXPECT_EQ(0, callback_count_); |
| 1009 EXPECT_EQ(1, error_callback_count_); |
| 1010 EXPECT_EQ(BluetoothDevice::ERROR_FAILED, last_connect_error_); |
| 1011 |
| 1012 EXPECT_EQ(0, observer.device_changed_count_); |
| 1013 |
| 1014 EXPECT_FALSE(device->IsConnected()); |
| 1015 EXPECT_FALSE(device->IsConnecting()); |
| 1016 } |
| 1017 |
| 1018 TEST_F(BluetoothExperimentalChromeOSTest, DisconnectDevice) { |
| 1019 GetAdapter(); |
| 1020 |
| 1021 BluetoothDevice* device = adapter_->GetDevice( |
| 1022 FakeBluetoothDeviceClient::kPairedDeviceAddress); |
| 1023 ASSERT_TRUE(device != NULL); |
| 1024 ASSERT_TRUE(device->IsPaired()); |
| 1025 |
| 1026 device->Connect( |
| 1027 NULL, |
| 1028 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 1029 base::Unretained(this)), |
| 1030 base::Bind(&BluetoothExperimentalChromeOSTest::ConnectErrorCallback, |
| 1031 base::Unretained(this))); |
| 1032 |
| 1033 ASSERT_EQ(1, callback_count_); |
| 1034 ASSERT_EQ(0, error_callback_count_); |
| 1035 callback_count_ = 0; |
| 1036 |
| 1037 ASSERT_TRUE(device->IsConnected()); |
| 1038 ASSERT_FALSE(device->IsConnecting()); |
| 1039 |
| 1040 // Disconnect the device, we should see the observer method fire and the |
| 1041 // device get dropped. |
| 1042 TestObserver observer(adapter_); |
| 1043 adapter_->AddObserver(&observer); |
| 1044 |
| 1045 device->Disconnect( |
| 1046 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 1047 base::Unretained(this)), |
| 1048 base::Bind(&BluetoothExperimentalChromeOSTest::ErrorCallback, |
| 1049 base::Unretained(this))); |
| 1050 |
| 1051 EXPECT_EQ(1, callback_count_); |
| 1052 EXPECT_EQ(0, error_callback_count_); |
| 1053 |
| 1054 EXPECT_EQ(1, observer.device_changed_count_); |
| 1055 EXPECT_EQ(device, observer.last_device_); |
| 1056 |
| 1057 EXPECT_FALSE(device->IsConnected()); |
| 1058 } |
| 1059 |
| 1060 TEST_F(BluetoothExperimentalChromeOSTest, DisconnectUnconnectedDevice) { |
| 1061 GetAdapter(); |
| 1062 |
| 1063 BluetoothDevice* device = adapter_->GetDevice( |
| 1064 FakeBluetoothDeviceClient::kPairedDeviceAddress); |
| 1065 ASSERT_TRUE(device != NULL); |
| 1066 ASSERT_TRUE(device->IsPaired()); |
| 1067 ASSERT_FALSE(device->IsConnected()); |
| 1068 |
| 1069 // Disconnect the device, we should see the observer method fire and the |
| 1070 // device get dropped. |
| 1071 TestObserver observer(adapter_); |
| 1072 adapter_->AddObserver(&observer); |
| 1073 |
| 1074 device->Disconnect( |
| 1075 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 1076 base::Unretained(this)), |
| 1077 base::Bind(&BluetoothExperimentalChromeOSTest::ErrorCallback, |
| 1078 base::Unretained(this))); |
| 1079 |
| 1080 EXPECT_EQ(0, callback_count_); |
| 1081 EXPECT_EQ(1, error_callback_count_); |
| 1082 |
| 1083 EXPECT_EQ(0, observer.device_changed_count_); |
| 1084 |
| 1085 EXPECT_FALSE(device->IsConnected()); |
| 1086 } |
| 1087 |
| 1088 TEST_F(BluetoothExperimentalChromeOSTest, PairAppleMouse) { |
| 1089 base::MessageLoop message_loop(MessageLoop::TYPE_DEFAULT); |
| 1090 fake_bluetooth_device_client_->SetSimulationIntervalMs(10); |
| 1091 |
| 1092 GetAdapter(); |
| 1093 DiscoverDevices(); |
| 1094 |
| 1095 // The Apple Mouse requires no PIN or Passkey to pair; this is equivalent |
| 1096 // to Simple Secure Pairing or a device with a fixed 0000 PIN. |
| 1097 BluetoothDevice* device = adapter_->GetDevice( |
| 1098 FakeBluetoothDeviceClient::kAppleMouseAddress); |
| 1099 ASSERT_TRUE(device != NULL); |
| 1100 ASSERT_FALSE(device->IsPaired()); |
| 1101 |
| 1102 TestObserver observer(adapter_); |
| 1103 adapter_->AddObserver(&observer); |
| 1104 |
| 1105 TestPairingDelegate pairing_delegate; |
| 1106 device->Connect( |
| 1107 &pairing_delegate, |
| 1108 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 1109 base::Unretained(this)), |
| 1110 base::Bind(&BluetoothExperimentalChromeOSTest::ConnectErrorCallback, |
| 1111 base::Unretained(this))); |
| 1112 |
| 1113 EXPECT_EQ(0, pairing_delegate.call_count_); |
| 1114 EXPECT_TRUE(device->IsConnecting()); |
| 1115 |
| 1116 message_loop.Run(); |
| 1117 |
| 1118 EXPECT_EQ(1, callback_count_); |
| 1119 EXPECT_EQ(0, error_callback_count_); |
| 1120 |
| 1121 // One change for connected, and one for paired. |
| 1122 EXPECT_EQ(2, observer.device_changed_count_); |
| 1123 EXPECT_EQ(device, observer.last_device_); |
| 1124 |
| 1125 EXPECT_TRUE(device->IsConnected()); |
| 1126 EXPECT_FALSE(device->IsConnecting()); |
| 1127 |
| 1128 EXPECT_TRUE(device->IsPaired()); |
| 1129 |
| 1130 // Pairing dialog should be dismissed |
| 1131 EXPECT_EQ(1, pairing_delegate.call_count_); |
| 1132 EXPECT_EQ(1, pairing_delegate.dismiss_count_); |
| 1133 |
| 1134 // Make sure the trusted property has been set to true. |
| 1135 FakeBluetoothDeviceClient::Properties* properties = |
| 1136 fake_bluetooth_device_client_->GetProperties( |
| 1137 dbus::ObjectPath(FakeBluetoothDeviceClient::kAppleMousePath)); |
| 1138 EXPECT_TRUE(properties->trusted.value()); |
| 1139 } |
| 1140 |
| 1141 TEST_F(BluetoothExperimentalChromeOSTest, PairAppleKeyboard) { |
| 1142 base::MessageLoop message_loop(MessageLoop::TYPE_DEFAULT); |
| 1143 fake_bluetooth_device_client_->SetSimulationIntervalMs(10); |
| 1144 |
| 1145 GetAdapter(); |
| 1146 DiscoverDevices(); |
| 1147 |
| 1148 // The Apple Keyboard requires that we display a randomly generated |
| 1149 // PIN on the screen. |
| 1150 BluetoothDevice* device = adapter_->GetDevice( |
| 1151 FakeBluetoothDeviceClient::kAppleKeyboardAddress); |
| 1152 ASSERT_TRUE(device != NULL); |
| 1153 ASSERT_FALSE(device->IsPaired()); |
| 1154 |
| 1155 TestObserver observer(adapter_); |
| 1156 adapter_->AddObserver(&observer); |
| 1157 |
| 1158 TestPairingDelegate pairing_delegate; |
| 1159 device->Connect( |
| 1160 &pairing_delegate, |
| 1161 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 1162 base::Unretained(this)), |
| 1163 base::Bind(&BluetoothExperimentalChromeOSTest::ConnectErrorCallback, |
| 1164 base::Unretained(this))); |
| 1165 |
| 1166 EXPECT_EQ(1, pairing_delegate.call_count_); |
| 1167 EXPECT_EQ(1, pairing_delegate.display_pincode_count_); |
| 1168 EXPECT_EQ("123456", pairing_delegate.last_pincode_); |
| 1169 EXPECT_TRUE(device->IsConnecting()); |
| 1170 |
| 1171 message_loop.Run(); |
| 1172 |
| 1173 EXPECT_EQ(1, callback_count_); |
| 1174 EXPECT_EQ(0, error_callback_count_); |
| 1175 |
| 1176 // One change for connected, and one for paired. |
| 1177 EXPECT_EQ(2, observer.device_changed_count_); |
| 1178 EXPECT_EQ(device, observer.last_device_); |
| 1179 |
| 1180 EXPECT_TRUE(device->IsConnected()); |
| 1181 EXPECT_FALSE(device->IsConnecting()); |
| 1182 |
| 1183 EXPECT_TRUE(device->IsPaired()); |
| 1184 |
| 1185 // Pairing dialog should be dismissed |
| 1186 EXPECT_EQ(2, pairing_delegate.call_count_); |
| 1187 EXPECT_EQ(1, pairing_delegate.dismiss_count_); |
| 1188 |
| 1189 // Make sure the trusted property has been set to true. |
| 1190 FakeBluetoothDeviceClient::Properties* properties = |
| 1191 fake_bluetooth_device_client_->GetProperties( |
| 1192 dbus::ObjectPath(FakeBluetoothDeviceClient::kAppleKeyboardPath)); |
| 1193 EXPECT_TRUE(properties->trusted.value()); |
| 1194 } |
| 1195 |
| 1196 TEST_F(BluetoothExperimentalChromeOSTest, PairMotorolaKeyboard) { |
| 1197 base::MessageLoop message_loop(MessageLoop::TYPE_DEFAULT); |
| 1198 fake_bluetooth_device_client_->SetSimulationIntervalMs(10); |
| 1199 |
| 1200 GetAdapter(); |
| 1201 DiscoverDevices(); |
| 1202 |
| 1203 // The Motorola Keyboard requires that we display a randomly generated |
| 1204 // Passkey on the screen, and notifies us as it's typed in. |
| 1205 BluetoothDevice* device = adapter_->GetDevice( |
| 1206 FakeBluetoothDeviceClient::kMotorolaKeyboardAddress); |
| 1207 ASSERT_TRUE(device != NULL); |
| 1208 ASSERT_FALSE(device->IsPaired()); |
| 1209 |
| 1210 TestObserver observer(adapter_); |
| 1211 adapter_->AddObserver(&observer); |
| 1212 |
| 1213 TestPairingDelegate pairing_delegate; |
| 1214 device->Connect( |
| 1215 &pairing_delegate, |
| 1216 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 1217 base::Unretained(this)), |
| 1218 base::Bind(&BluetoothExperimentalChromeOSTest::ConnectErrorCallback, |
| 1219 base::Unretained(this))); |
| 1220 |
| 1221 EXPECT_EQ(1, pairing_delegate.call_count_); |
| 1222 EXPECT_EQ(1, pairing_delegate.display_passkey_count_); |
| 1223 EXPECT_EQ(123456U, pairing_delegate.last_passkey_); |
| 1224 EXPECT_TRUE(device->IsConnecting()); |
| 1225 |
| 1226 // TODO(keybuk): verify we get typing notifications |
| 1227 |
| 1228 message_loop.Run(); |
| 1229 |
| 1230 EXPECT_EQ(1, callback_count_); |
| 1231 EXPECT_EQ(0, error_callback_count_); |
| 1232 |
| 1233 // One change for connected, and one for paired. |
| 1234 EXPECT_EQ(2, observer.device_changed_count_); |
| 1235 EXPECT_EQ(device, observer.last_device_); |
| 1236 |
| 1237 EXPECT_TRUE(device->IsConnected()); |
| 1238 EXPECT_FALSE(device->IsConnecting()); |
| 1239 |
| 1240 EXPECT_TRUE(device->IsPaired()); |
| 1241 |
| 1242 // Pairing dialog should be dismissed |
| 1243 EXPECT_EQ(2, pairing_delegate.call_count_); |
| 1244 EXPECT_EQ(1, pairing_delegate.dismiss_count_); |
| 1245 |
| 1246 // Make sure the trusted property has been set to true. |
| 1247 FakeBluetoothDeviceClient::Properties* properties = |
| 1248 fake_bluetooth_device_client_->GetProperties( |
| 1249 dbus::ObjectPath(FakeBluetoothDeviceClient::kMotorolaKeyboardPath)); |
| 1250 EXPECT_TRUE(properties->trusted.value()); |
| 1251 } |
| 1252 |
| 1253 TEST_F(BluetoothExperimentalChromeOSTest, PairSonyHeadphones) { |
| 1254 base::MessageLoop message_loop(MessageLoop::TYPE_DEFAULT); |
| 1255 fake_bluetooth_device_client_->SetSimulationIntervalMs(10); |
| 1256 |
| 1257 GetAdapter(); |
| 1258 DiscoverDevices(); |
| 1259 |
| 1260 // The Sony Headphones fake requires that the user enters a PIN for them. |
| 1261 BluetoothDevice* device = adapter_->GetDevice( |
| 1262 FakeBluetoothDeviceClient::kSonyHeadphonesAddress); |
| 1263 ASSERT_TRUE(device != NULL); |
| 1264 ASSERT_FALSE(device->IsPaired()); |
| 1265 |
| 1266 TestObserver observer(adapter_); |
| 1267 adapter_->AddObserver(&observer); |
| 1268 |
| 1269 TestPairingDelegate pairing_delegate; |
| 1270 device->Connect( |
| 1271 &pairing_delegate, |
| 1272 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 1273 base::Unretained(this)), |
| 1274 base::Bind(&BluetoothExperimentalChromeOSTest::ConnectErrorCallback, |
| 1275 base::Unretained(this))); |
| 1276 |
| 1277 EXPECT_EQ(1, pairing_delegate.call_count_); |
| 1278 EXPECT_EQ(1, pairing_delegate.request_pincode_count_); |
| 1279 EXPECT_TRUE(device->IsConnecting()); |
| 1280 |
| 1281 // Set the PIN. |
| 1282 device->SetPinCode("1234"); |
| 1283 message_loop.Run(); |
| 1284 |
| 1285 EXPECT_EQ(1, callback_count_); |
| 1286 EXPECT_EQ(0, error_callback_count_); |
| 1287 |
| 1288 // One change for connected, and one for paired. |
| 1289 EXPECT_EQ(2, observer.device_changed_count_); |
| 1290 EXPECT_EQ(device, observer.last_device_); |
| 1291 |
| 1292 EXPECT_TRUE(device->IsConnected()); |
| 1293 EXPECT_FALSE(device->IsConnecting()); |
| 1294 |
| 1295 EXPECT_TRUE(device->IsPaired()); |
| 1296 |
| 1297 // Pairing dialog should be dismissed |
| 1298 EXPECT_EQ(2, pairing_delegate.call_count_); |
| 1299 EXPECT_EQ(1, pairing_delegate.dismiss_count_); |
| 1300 |
| 1301 // Make sure the trusted property has been set to true. |
| 1302 FakeBluetoothDeviceClient::Properties* properties = |
| 1303 fake_bluetooth_device_client_->GetProperties( |
| 1304 dbus::ObjectPath(FakeBluetoothDeviceClient::kSonyHeadphonesPath)); |
| 1305 EXPECT_TRUE(properties->trusted.value()); |
| 1306 } |
| 1307 |
| 1308 TEST_F(BluetoothExperimentalChromeOSTest, PairPhone) { |
| 1309 base::MessageLoop message_loop(MessageLoop::TYPE_DEFAULT); |
| 1310 fake_bluetooth_device_client_->SetSimulationIntervalMs(10); |
| 1311 |
| 1312 GetAdapter(); |
| 1313 DiscoverDevices(); |
| 1314 |
| 1315 // The fake phone requests that we confirm a displayed passkey. |
| 1316 BluetoothDevice* device = adapter_->GetDevice( |
| 1317 FakeBluetoothDeviceClient::kPhoneAddress); |
| 1318 ASSERT_TRUE(device != NULL); |
| 1319 ASSERT_FALSE(device->IsPaired()); |
| 1320 |
| 1321 TestObserver observer(adapter_); |
| 1322 adapter_->AddObserver(&observer); |
| 1323 |
| 1324 TestPairingDelegate pairing_delegate; |
| 1325 device->Connect( |
| 1326 &pairing_delegate, |
| 1327 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 1328 base::Unretained(this)), |
| 1329 base::Bind(&BluetoothExperimentalChromeOSTest::ConnectErrorCallback, |
| 1330 base::Unretained(this))); |
| 1331 |
| 1332 EXPECT_EQ(1, pairing_delegate.call_count_); |
| 1333 EXPECT_EQ(1, pairing_delegate.confirm_passkey_count_); |
| 1334 EXPECT_EQ(123456U, pairing_delegate.last_passkey_); |
| 1335 EXPECT_TRUE(device->IsConnecting()); |
| 1336 |
| 1337 // Confirm the passkey. |
| 1338 device->ConfirmPairing(); |
| 1339 message_loop.Run(); |
| 1340 |
| 1341 EXPECT_EQ(1, callback_count_); |
| 1342 EXPECT_EQ(0, error_callback_count_); |
| 1343 |
| 1344 // One change for connected, and one for paired. |
| 1345 EXPECT_EQ(2, observer.device_changed_count_); |
| 1346 EXPECT_EQ(device, observer.last_device_); |
| 1347 |
| 1348 EXPECT_TRUE(device->IsConnected()); |
| 1349 EXPECT_FALSE(device->IsConnecting()); |
| 1350 |
| 1351 EXPECT_TRUE(device->IsPaired()); |
| 1352 |
| 1353 // Pairing dialog should be dismissed |
| 1354 EXPECT_EQ(2, pairing_delegate.call_count_); |
| 1355 EXPECT_EQ(1, pairing_delegate.dismiss_count_); |
| 1356 |
| 1357 // Make sure the trusted property has been set to true. |
| 1358 FakeBluetoothDeviceClient::Properties* properties = |
| 1359 fake_bluetooth_device_client_->GetProperties( |
| 1360 dbus::ObjectPath(FakeBluetoothDeviceClient::kPhonePath)); |
| 1361 EXPECT_TRUE(properties->trusted.value()); |
| 1362 } |
| 1363 |
| 1364 TEST_F(BluetoothExperimentalChromeOSTest, PairWeirdDevice) { |
| 1365 base::MessageLoop message_loop(MessageLoop::TYPE_DEFAULT); |
| 1366 fake_bluetooth_device_client_->SetSimulationIntervalMs(10); |
| 1367 |
| 1368 GetAdapter(); |
| 1369 DiscoverDevices(); |
| 1370 |
| 1371 // Use the "weird device" fake that requires that the user enters a Passkey, |
| 1372 // this would be some kind of device that has a display, but doesn't use |
| 1373 // "just works" - maybe a car? |
| 1374 BluetoothDevice* device = adapter_->GetDevice( |
| 1375 FakeBluetoothDeviceClient::kWeirdDeviceAddress); |
| 1376 ASSERT_TRUE(device != NULL); |
| 1377 ASSERT_FALSE(device->IsPaired()); |
| 1378 |
| 1379 TestObserver observer(adapter_); |
| 1380 adapter_->AddObserver(&observer); |
| 1381 |
| 1382 TestPairingDelegate pairing_delegate; |
| 1383 device->Connect( |
| 1384 &pairing_delegate, |
| 1385 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 1386 base::Unretained(this)), |
| 1387 base::Bind(&BluetoothExperimentalChromeOSTest::ConnectErrorCallback, |
| 1388 base::Unretained(this))); |
| 1389 |
| 1390 EXPECT_EQ(1, pairing_delegate.call_count_); |
| 1391 EXPECT_EQ(1, pairing_delegate.request_passkey_count_); |
| 1392 EXPECT_TRUE(device->IsConnecting()); |
| 1393 |
| 1394 // Set the Passkey. |
| 1395 device->SetPasskey(1234); |
| 1396 message_loop.Run(); |
| 1397 |
| 1398 EXPECT_EQ(1, callback_count_); |
| 1399 EXPECT_EQ(0, error_callback_count_); |
| 1400 |
| 1401 // One change for connected, and one for paired. |
| 1402 EXPECT_EQ(2, observer.device_changed_count_); |
| 1403 EXPECT_EQ(device, observer.last_device_); |
| 1404 |
| 1405 EXPECT_TRUE(device->IsConnected()); |
| 1406 EXPECT_FALSE(device->IsConnecting()); |
| 1407 |
| 1408 EXPECT_TRUE(device->IsPaired()); |
| 1409 |
| 1410 // Pairing dialog should be dismissed |
| 1411 EXPECT_EQ(2, pairing_delegate.call_count_); |
| 1412 EXPECT_EQ(1, pairing_delegate.dismiss_count_); |
| 1413 |
| 1414 // Make sure the trusted property has been set to true. |
| 1415 FakeBluetoothDeviceClient::Properties* properties = |
| 1416 fake_bluetooth_device_client_->GetProperties( |
| 1417 dbus::ObjectPath(FakeBluetoothDeviceClient::kWeirdDevicePath)); |
| 1418 EXPECT_TRUE(properties->trusted.value()); |
| 1419 } |
| 1420 |
| 1421 TEST_F(BluetoothExperimentalChromeOSTest, PairingFails) { |
| 1422 base::MessageLoop message_loop(MessageLoop::TYPE_DEFAULT); |
| 1423 fake_bluetooth_device_client_->SetSimulationIntervalMs(10); |
| 1424 |
| 1425 GetAdapter(); |
| 1426 DiscoverDevice(FakeBluetoothDeviceClient::kVanishingDeviceAddress); |
| 1427 |
| 1428 // The vanishing device times out during pairing |
| 1429 BluetoothDevice* device = adapter_->GetDevice( |
| 1430 FakeBluetoothDeviceClient::kVanishingDeviceAddress); |
| 1431 ASSERT_TRUE(device != NULL); |
| 1432 ASSERT_FALSE(device->IsPaired()); |
| 1433 |
| 1434 TestObserver observer(adapter_); |
| 1435 adapter_->AddObserver(&observer); |
| 1436 |
| 1437 TestPairingDelegate pairing_delegate; |
| 1438 device->Connect( |
| 1439 &pairing_delegate, |
| 1440 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 1441 base::Unretained(this)), |
| 1442 base::Bind(&BluetoothExperimentalChromeOSTest::ConnectErrorCallback, |
| 1443 base::Unretained(this))); |
| 1444 |
| 1445 EXPECT_EQ(0, pairing_delegate.call_count_); |
| 1446 EXPECT_TRUE(device->IsConnecting()); |
| 1447 |
| 1448 // Run the loop to get the error.. |
| 1449 message_loop.Run(); |
| 1450 |
| 1451 EXPECT_EQ(0, callback_count_); |
| 1452 EXPECT_EQ(1, error_callback_count_); |
| 1453 |
| 1454 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_TIMEOUT, last_connect_error_); |
| 1455 |
| 1456 EXPECT_FALSE(device->IsConnected()); |
| 1457 EXPECT_FALSE(device->IsConnecting()); |
| 1458 EXPECT_FALSE(device->IsPaired()); |
| 1459 |
| 1460 // Pairing dialog should be dismissed |
| 1461 EXPECT_EQ(1, pairing_delegate.call_count_); |
| 1462 EXPECT_EQ(1, pairing_delegate.dismiss_count_); |
| 1463 } |
| 1464 |
| 1465 TEST_F(BluetoothExperimentalChromeOSTest, PairingFailsAtConnection) { |
| 1466 base::MessageLoop message_loop(MessageLoop::TYPE_DEFAULT); |
| 1467 fake_bluetooth_device_client_->SetSimulationIntervalMs(10); |
| 1468 |
| 1469 GetAdapter(); |
| 1470 DiscoverDevices(); |
| 1471 |
| 1472 // Everything seems to go according to plan with the Microsoft Mouse, it |
| 1473 // pairs with 0000, but then you can't make connections to it after. |
| 1474 BluetoothDevice* device = adapter_->GetDevice( |
| 1475 FakeBluetoothDeviceClient::kMicrosoftMouseAddress); |
| 1476 ASSERT_TRUE(device != NULL); |
| 1477 ASSERT_FALSE(device->IsPaired()); |
| 1478 |
| 1479 TestObserver observer(adapter_); |
| 1480 adapter_->AddObserver(&observer); |
| 1481 |
| 1482 TestPairingDelegate pairing_delegate; |
| 1483 device->Connect( |
| 1484 &pairing_delegate, |
| 1485 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 1486 base::Unretained(this)), |
| 1487 base::Bind(&BluetoothExperimentalChromeOSTest::ConnectErrorCallback, |
| 1488 base::Unretained(this))); |
| 1489 |
| 1490 EXPECT_EQ(0, pairing_delegate.call_count_); |
| 1491 EXPECT_TRUE(device->IsConnecting()); |
| 1492 |
| 1493 message_loop.Run(); |
| 1494 |
| 1495 EXPECT_EQ(0, callback_count_); |
| 1496 EXPECT_EQ(1, error_callback_count_); |
| 1497 EXPECT_EQ(BluetoothDevice::ERROR_FAILED, last_connect_error_); |
| 1498 |
| 1499 // Just one change for paired, the device should not be connected. |
| 1500 EXPECT_EQ(1, observer.device_changed_count_); |
| 1501 EXPECT_EQ(device, observer.last_device_); |
| 1502 |
| 1503 EXPECT_FALSE(device->IsConnected()); |
| 1504 EXPECT_FALSE(device->IsConnecting()); |
| 1505 |
| 1506 EXPECT_TRUE(device->IsPaired()); |
| 1507 |
| 1508 // Pairing dialog should be dismissed |
| 1509 EXPECT_EQ(1, pairing_delegate.call_count_); |
| 1510 EXPECT_EQ(1, pairing_delegate.dismiss_count_); |
| 1511 |
| 1512 // Make sure the trusted property has been set to true still (since pairing |
| 1513 // worked). |
| 1514 FakeBluetoothDeviceClient::Properties* properties = |
| 1515 fake_bluetooth_device_client_->GetProperties( |
| 1516 dbus::ObjectPath(FakeBluetoothDeviceClient::kMicrosoftMousePath)); |
| 1517 EXPECT_TRUE(properties->trusted.value()); |
| 1518 } |
| 1519 |
| 1520 TEST_F(BluetoothExperimentalChromeOSTest, PairingRejectedAtPinCode) { |
| 1521 base::MessageLoop message_loop(MessageLoop::TYPE_DEFAULT); |
| 1522 fake_bluetooth_device_client_->SetSimulationIntervalMs(10); |
| 1523 |
| 1524 GetAdapter(); |
| 1525 DiscoverDevices(); |
| 1526 |
| 1527 // Reject the pairing after we receive a request for the PIN code. |
| 1528 BluetoothDevice* device = adapter_->GetDevice( |
| 1529 FakeBluetoothDeviceClient::kSonyHeadphonesAddress); |
| 1530 ASSERT_TRUE(device != NULL); |
| 1531 ASSERT_FALSE(device->IsPaired()); |
| 1532 |
| 1533 TestObserver observer(adapter_); |
| 1534 adapter_->AddObserver(&observer); |
| 1535 |
| 1536 TestPairingDelegate pairing_delegate; |
| 1537 device->Connect( |
| 1538 &pairing_delegate, |
| 1539 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 1540 base::Unretained(this)), |
| 1541 base::Bind(&BluetoothExperimentalChromeOSTest::ConnectErrorCallback, |
| 1542 base::Unretained(this))); |
| 1543 |
| 1544 EXPECT_EQ(1, pairing_delegate.call_count_); |
| 1545 EXPECT_EQ(1, pairing_delegate.request_pincode_count_); |
| 1546 EXPECT_TRUE(device->IsConnecting()); |
| 1547 |
| 1548 // Reject the pairing. |
| 1549 device->RejectPairing(); |
| 1550 message_loop.Run(); |
| 1551 |
| 1552 EXPECT_EQ(0, callback_count_); |
| 1553 EXPECT_EQ(1, error_callback_count_); |
| 1554 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_REJECTED, last_connect_error_); |
| 1555 |
| 1556 // Should be no changes. |
| 1557 EXPECT_EQ(0, observer.device_changed_count_); |
| 1558 EXPECT_FALSE(device->IsConnected()); |
| 1559 EXPECT_FALSE(device->IsConnecting()); |
| 1560 EXPECT_FALSE(device->IsPaired()); |
| 1561 |
| 1562 // Pairing dialog should be dismissed |
| 1563 EXPECT_EQ(2, pairing_delegate.call_count_); |
| 1564 EXPECT_EQ(1, pairing_delegate.dismiss_count_); |
| 1565 } |
| 1566 |
| 1567 TEST_F(BluetoothExperimentalChromeOSTest, PairingCancelledAtPinCode) { |
| 1568 base::MessageLoop message_loop(MessageLoop::TYPE_DEFAULT); |
| 1569 fake_bluetooth_device_client_->SetSimulationIntervalMs(10); |
| 1570 |
| 1571 GetAdapter(); |
| 1572 DiscoverDevices(); |
| 1573 |
| 1574 // Cancel the pairing after we receive a request for the PIN code. |
| 1575 BluetoothDevice* device = adapter_->GetDevice( |
| 1576 FakeBluetoothDeviceClient::kSonyHeadphonesAddress); |
| 1577 ASSERT_TRUE(device != NULL); |
| 1578 ASSERT_FALSE(device->IsPaired()); |
| 1579 |
| 1580 TestObserver observer(adapter_); |
| 1581 adapter_->AddObserver(&observer); |
| 1582 |
| 1583 TestPairingDelegate pairing_delegate; |
| 1584 device->Connect( |
| 1585 &pairing_delegate, |
| 1586 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 1587 base::Unretained(this)), |
| 1588 base::Bind(&BluetoothExperimentalChromeOSTest::ConnectErrorCallback, |
| 1589 base::Unretained(this))); |
| 1590 |
| 1591 EXPECT_EQ(1, pairing_delegate.call_count_); |
| 1592 EXPECT_EQ(1, pairing_delegate.request_pincode_count_); |
| 1593 EXPECT_TRUE(device->IsConnecting()); |
| 1594 |
| 1595 // Cancel the pairing. |
| 1596 device->CancelPairing(); |
| 1597 message_loop.Run(); |
| 1598 |
| 1599 EXPECT_EQ(0, callback_count_); |
| 1600 EXPECT_EQ(1, error_callback_count_); |
| 1601 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_CANCELED, last_connect_error_); |
| 1602 |
| 1603 // Should be no changes. |
| 1604 EXPECT_EQ(0, observer.device_changed_count_); |
| 1605 EXPECT_FALSE(device->IsConnected()); |
| 1606 EXPECT_FALSE(device->IsConnecting()); |
| 1607 EXPECT_FALSE(device->IsPaired()); |
| 1608 |
| 1609 // Pairing dialog should be dismissed |
| 1610 EXPECT_EQ(2, pairing_delegate.call_count_); |
| 1611 EXPECT_EQ(1, pairing_delegate.dismiss_count_); |
| 1612 } |
| 1613 |
| 1614 TEST_F(BluetoothExperimentalChromeOSTest, PairingRejectedAtPasskey) { |
| 1615 base::MessageLoop message_loop(MessageLoop::TYPE_DEFAULT); |
| 1616 fake_bluetooth_device_client_->SetSimulationIntervalMs(10); |
| 1617 |
| 1618 GetAdapter(); |
| 1619 DiscoverDevices(); |
| 1620 |
| 1621 // Reject the pairing after we receive a request for the passkey. |
| 1622 BluetoothDevice* device = adapter_->GetDevice( |
| 1623 FakeBluetoothDeviceClient::kWeirdDeviceAddress); |
| 1624 ASSERT_TRUE(device != NULL); |
| 1625 ASSERT_FALSE(device->IsPaired()); |
| 1626 |
| 1627 TestObserver observer(adapter_); |
| 1628 adapter_->AddObserver(&observer); |
| 1629 |
| 1630 TestPairingDelegate pairing_delegate; |
| 1631 device->Connect( |
| 1632 &pairing_delegate, |
| 1633 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 1634 base::Unretained(this)), |
| 1635 base::Bind(&BluetoothExperimentalChromeOSTest::ConnectErrorCallback, |
| 1636 base::Unretained(this))); |
| 1637 |
| 1638 EXPECT_EQ(1, pairing_delegate.call_count_); |
| 1639 EXPECT_EQ(1, pairing_delegate.request_passkey_count_); |
| 1640 EXPECT_TRUE(device->IsConnecting()); |
| 1641 |
| 1642 // Reject the pairing. |
| 1643 device->RejectPairing(); |
| 1644 message_loop.Run(); |
| 1645 |
| 1646 EXPECT_EQ(0, callback_count_); |
| 1647 EXPECT_EQ(1, error_callback_count_); |
| 1648 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_REJECTED, last_connect_error_); |
| 1649 |
| 1650 // Should be no changes. |
| 1651 EXPECT_EQ(0, observer.device_changed_count_); |
| 1652 EXPECT_FALSE(device->IsConnected()); |
| 1653 EXPECT_FALSE(device->IsConnecting()); |
| 1654 EXPECT_FALSE(device->IsPaired()); |
| 1655 |
| 1656 // Pairing dialog should be dismissed |
| 1657 EXPECT_EQ(2, pairing_delegate.call_count_); |
| 1658 EXPECT_EQ(1, pairing_delegate.dismiss_count_); |
| 1659 } |
| 1660 |
| 1661 TEST_F(BluetoothExperimentalChromeOSTest, PairingCancelledAtPasskey) { |
| 1662 base::MessageLoop message_loop(MessageLoop::TYPE_DEFAULT); |
| 1663 fake_bluetooth_device_client_->SetSimulationIntervalMs(10); |
| 1664 |
| 1665 GetAdapter(); |
| 1666 DiscoverDevices(); |
| 1667 |
| 1668 // Cancel the pairing after we receive a request for the passkey. |
| 1669 BluetoothDevice* device = adapter_->GetDevice( |
| 1670 FakeBluetoothDeviceClient::kWeirdDeviceAddress); |
| 1671 ASSERT_TRUE(device != NULL); |
| 1672 ASSERT_FALSE(device->IsPaired()); |
| 1673 |
| 1674 TestObserver observer(adapter_); |
| 1675 adapter_->AddObserver(&observer); |
| 1676 |
| 1677 TestPairingDelegate pairing_delegate; |
| 1678 device->Connect( |
| 1679 &pairing_delegate, |
| 1680 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 1681 base::Unretained(this)), |
| 1682 base::Bind(&BluetoothExperimentalChromeOSTest::ConnectErrorCallback, |
| 1683 base::Unretained(this))); |
| 1684 |
| 1685 EXPECT_EQ(1, pairing_delegate.call_count_); |
| 1686 EXPECT_EQ(1, pairing_delegate.request_passkey_count_); |
| 1687 EXPECT_TRUE(device->IsConnecting()); |
| 1688 |
| 1689 // Cancel the pairing. |
| 1690 device->CancelPairing(); |
| 1691 message_loop.Run(); |
| 1692 |
| 1693 EXPECT_EQ(0, callback_count_); |
| 1694 EXPECT_EQ(1, error_callback_count_); |
| 1695 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_CANCELED, last_connect_error_); |
| 1696 |
| 1697 // Should be no changes. |
| 1698 EXPECT_EQ(0, observer.device_changed_count_); |
| 1699 EXPECT_FALSE(device->IsConnected()); |
| 1700 EXPECT_FALSE(device->IsConnecting()); |
| 1701 EXPECT_FALSE(device->IsPaired()); |
| 1702 |
| 1703 // Pairing dialog should be dismissed |
| 1704 EXPECT_EQ(2, pairing_delegate.call_count_); |
| 1705 EXPECT_EQ(1, pairing_delegate.dismiss_count_); |
| 1706 } |
| 1707 |
| 1708 TEST_F(BluetoothExperimentalChromeOSTest, PairingRejectedAtConfirmation) { |
| 1709 base::MessageLoop message_loop(MessageLoop::TYPE_DEFAULT); |
| 1710 fake_bluetooth_device_client_->SetSimulationIntervalMs(10); |
| 1711 |
| 1712 GetAdapter(); |
| 1713 DiscoverDevices(); |
| 1714 |
| 1715 // Reject the pairing after we receive a request for passkey confirmation. |
| 1716 BluetoothDevice* device = adapter_->GetDevice( |
| 1717 FakeBluetoothDeviceClient::kPhoneAddress); |
| 1718 ASSERT_TRUE(device != NULL); |
| 1719 ASSERT_FALSE(device->IsPaired()); |
| 1720 |
| 1721 TestObserver observer(adapter_); |
| 1722 adapter_->AddObserver(&observer); |
| 1723 |
| 1724 TestPairingDelegate pairing_delegate; |
| 1725 device->Connect( |
| 1726 &pairing_delegate, |
| 1727 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 1728 base::Unretained(this)), |
| 1729 base::Bind(&BluetoothExperimentalChromeOSTest::ConnectErrorCallback, |
| 1730 base::Unretained(this))); |
| 1731 |
| 1732 EXPECT_EQ(1, pairing_delegate.call_count_); |
| 1733 EXPECT_EQ(1, pairing_delegate.confirm_passkey_count_); |
| 1734 EXPECT_TRUE(device->IsConnecting()); |
| 1735 |
| 1736 // Reject the pairing. |
| 1737 device->RejectPairing(); |
| 1738 message_loop.Run(); |
| 1739 |
| 1740 EXPECT_EQ(0, callback_count_); |
| 1741 EXPECT_EQ(1, error_callback_count_); |
| 1742 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_REJECTED, last_connect_error_); |
| 1743 |
| 1744 // Should be no changes. |
| 1745 EXPECT_EQ(0, observer.device_changed_count_); |
| 1746 EXPECT_FALSE(device->IsConnected()); |
| 1747 EXPECT_FALSE(device->IsConnecting()); |
| 1748 EXPECT_FALSE(device->IsPaired()); |
| 1749 |
| 1750 // Pairing dialog should be dismissed |
| 1751 EXPECT_EQ(2, pairing_delegate.call_count_); |
| 1752 EXPECT_EQ(1, pairing_delegate.dismiss_count_); |
| 1753 } |
| 1754 |
| 1755 TEST_F(BluetoothExperimentalChromeOSTest, PairingCancelledAtConfirmation) { |
| 1756 base::MessageLoop message_loop(MessageLoop::TYPE_DEFAULT); |
| 1757 fake_bluetooth_device_client_->SetSimulationIntervalMs(10); |
| 1758 |
| 1759 GetAdapter(); |
| 1760 DiscoverDevices(); |
| 1761 |
| 1762 // Cancel the pairing after we receive a request for the passkey. |
| 1763 BluetoothDevice* device = adapter_->GetDevice( |
| 1764 FakeBluetoothDeviceClient::kPhoneAddress); |
| 1765 ASSERT_TRUE(device != NULL); |
| 1766 ASSERT_FALSE(device->IsPaired()); |
| 1767 |
| 1768 TestObserver observer(adapter_); |
| 1769 adapter_->AddObserver(&observer); |
| 1770 |
| 1771 TestPairingDelegate pairing_delegate; |
| 1772 device->Connect( |
| 1773 &pairing_delegate, |
| 1774 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 1775 base::Unretained(this)), |
| 1776 base::Bind(&BluetoothExperimentalChromeOSTest::ConnectErrorCallback, |
| 1777 base::Unretained(this))); |
| 1778 |
| 1779 EXPECT_EQ(1, pairing_delegate.call_count_); |
| 1780 EXPECT_EQ(1, pairing_delegate.confirm_passkey_count_); |
| 1781 EXPECT_TRUE(device->IsConnecting()); |
| 1782 |
| 1783 // Cancel the pairing. |
| 1784 device->CancelPairing(); |
| 1785 message_loop.Run(); |
| 1786 |
| 1787 EXPECT_EQ(0, callback_count_); |
| 1788 EXPECT_EQ(1, error_callback_count_); |
| 1789 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_CANCELED, last_connect_error_); |
| 1790 |
| 1791 // Should be no changes. |
| 1792 EXPECT_EQ(0, observer.device_changed_count_); |
| 1793 EXPECT_FALSE(device->IsConnected()); |
| 1794 EXPECT_FALSE(device->IsConnecting()); |
| 1795 EXPECT_FALSE(device->IsPaired()); |
| 1796 |
| 1797 // Pairing dialog should be dismissed |
| 1798 EXPECT_EQ(2, pairing_delegate.call_count_); |
| 1799 EXPECT_EQ(1, pairing_delegate.dismiss_count_); |
| 1800 } |
| 1801 |
| 1802 TEST_F(BluetoothExperimentalChromeOSTest, PairingCancelledInFlight) { |
| 1803 base::MessageLoop message_loop(MessageLoop::TYPE_DEFAULT); |
| 1804 fake_bluetooth_device_client_->SetSimulationIntervalMs(10); |
| 1805 |
| 1806 GetAdapter(); |
| 1807 DiscoverDevices(); |
| 1808 |
| 1809 // Cancel the pairing while we're waiting for the remote host. |
| 1810 BluetoothDevice* device = adapter_->GetDevice( |
| 1811 FakeBluetoothDeviceClient::kAppleMouseAddress); |
| 1812 ASSERT_TRUE(device != NULL); |
| 1813 ASSERT_FALSE(device->IsPaired()); |
| 1814 |
| 1815 TestObserver observer(adapter_); |
| 1816 adapter_->AddObserver(&observer); |
| 1817 |
| 1818 TestPairingDelegate pairing_delegate; |
| 1819 device->Connect( |
| 1820 &pairing_delegate, |
| 1821 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 1822 base::Unretained(this)), |
| 1823 base::Bind(&BluetoothExperimentalChromeOSTest::ConnectErrorCallback, |
| 1824 base::Unretained(this))); |
| 1825 |
| 1826 EXPECT_EQ(0, pairing_delegate.call_count_); |
| 1827 EXPECT_TRUE(device->IsConnecting()); |
| 1828 |
| 1829 // Cancel the pairing. |
| 1830 device->CancelPairing(); |
| 1831 message_loop.Run(); |
| 1832 |
| 1833 EXPECT_EQ(0, callback_count_); |
| 1834 EXPECT_EQ(1, error_callback_count_); |
| 1835 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_CANCELED, last_connect_error_); |
| 1836 |
| 1837 // Should be no changes. |
| 1838 EXPECT_EQ(0, observer.device_changed_count_); |
| 1839 EXPECT_FALSE(device->IsConnected()); |
| 1840 EXPECT_FALSE(device->IsConnecting()); |
| 1841 EXPECT_FALSE(device->IsPaired()); |
| 1842 |
| 1843 // Pairing dialog should be dismissed |
| 1844 EXPECT_EQ(1, pairing_delegate.call_count_); |
| 1845 EXPECT_EQ(1, pairing_delegate.dismiss_count_); |
| 1846 } |
| 1847 |
| 1848 } // namespace chromeos |
OLD | NEW |