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