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 so we have devices that can be paired with. |
| 253 void DiscoverDevices() { |
| 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 // Run forward until a device is removed; that's the last thing in the |
| 286 // simulation. |
| 287 while (!observer.device_removed_count_) |
| 288 MessageLoop::current()->Run(); |
| 289 |
| 290 adapter_->RemoveObserver(&observer); |
| 291 } |
| 292 |
| 293 protected: |
| 294 FakeBluetoothAdapterClient* fake_bluetooth_adapter_client_; |
| 295 FakeBluetoothDeviceClient* fake_bluetooth_device_client_; |
| 296 MockDBusThreadManagerWithoutGMock* mock_dbus_thread_manager_; |
| 297 scoped_refptr<BluetoothAdapter> adapter_; |
| 298 |
| 299 int callback_count_; |
| 300 int error_callback_count_; |
| 301 enum BluetoothDevice::ConnectErrorCode last_connect_error_; |
| 302 }; |
| 303 |
| 304 TEST_F(BluetoothExperimentalChromeOSTest, AlreadyPresent) { |
| 305 GetAdapter(); |
| 306 |
| 307 // This verifies that the class gets the list of adapters when created; |
| 308 // and initializes with an existing adapter if there is one. |
| 309 EXPECT_TRUE(adapter_->IsPresent()); |
| 310 EXPECT_FALSE(adapter_->IsPowered()); |
| 311 EXPECT_EQ(FakeBluetoothAdapterClient::kAdapterAddress, |
| 312 adapter_->GetAddress()); |
| 313 EXPECT_EQ(FakeBluetoothAdapterClient::kAdapterName, adapter_->GetName()); |
| 314 EXPECT_FALSE(adapter_->IsDiscovering()); |
| 315 |
| 316 // There should be a device |
| 317 BluetoothAdapter::DeviceList devices = adapter_->GetDevices(); |
| 318 EXPECT_EQ(1U, devices.size()); |
| 319 EXPECT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress, |
| 320 devices[0]->GetAddress()); |
| 321 } |
| 322 |
| 323 TEST_F(BluetoothExperimentalChromeOSTest, BecomePresent) { |
| 324 fake_bluetooth_adapter_client_->SetVisible(false); |
| 325 GetAdapter(); |
| 326 ASSERT_FALSE(adapter_->IsPresent()); |
| 327 |
| 328 // Install an observer; expect the AdapterPresentChanged to be called |
| 329 // with true, and IsPresent() to return true. |
| 330 TestObserver observer(adapter_); |
| 331 adapter_->AddObserver(&observer); |
| 332 |
| 333 fake_bluetooth_adapter_client_->SetVisible(true); |
| 334 |
| 335 EXPECT_EQ(1, observer.present_changed_count_); |
| 336 EXPECT_TRUE(observer.last_present_); |
| 337 |
| 338 EXPECT_TRUE(adapter_->IsPresent()); |
| 339 |
| 340 // We should have had a device announced. |
| 341 EXPECT_EQ(1, observer.device_added_count_); |
| 342 EXPECT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress, |
| 343 observer.last_device_address_); |
| 344 |
| 345 // Other callbacks shouldn't be called if the values are false. |
| 346 EXPECT_EQ(0, observer.powered_changed_count_); |
| 347 EXPECT_EQ(0, observer.discovering_changed_count_); |
| 348 EXPECT_FALSE(adapter_->IsPowered()); |
| 349 EXPECT_FALSE(adapter_->IsDiscovering()); |
| 350 } |
| 351 |
| 352 TEST_F(BluetoothExperimentalChromeOSTest, BecomeNotPresent) { |
| 353 GetAdapter(); |
| 354 ASSERT_TRUE(adapter_->IsPresent()); |
| 355 |
| 356 // Install an observer; expect the AdapterPresentChanged to be called |
| 357 // with false, and IsPresent() to return false. |
| 358 TestObserver observer(adapter_); |
| 359 adapter_->AddObserver(&observer); |
| 360 |
| 361 fake_bluetooth_adapter_client_->SetVisible(false); |
| 362 |
| 363 EXPECT_EQ(1, observer.present_changed_count_); |
| 364 EXPECT_FALSE(observer.last_present_); |
| 365 |
| 366 EXPECT_FALSE(adapter_->IsPresent()); |
| 367 |
| 368 // We should have had a device removed. |
| 369 EXPECT_EQ(1, observer.device_removed_count_); |
| 370 EXPECT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress, |
| 371 observer.last_device_address_); |
| 372 |
| 373 // Other callbacks shouldn't be called since the values are false. |
| 374 EXPECT_EQ(0, observer.powered_changed_count_); |
| 375 EXPECT_EQ(0, observer.discovering_changed_count_); |
| 376 EXPECT_FALSE(adapter_->IsPowered()); |
| 377 EXPECT_FALSE(adapter_->IsDiscovering()); |
| 378 } |
| 379 |
| 380 TEST_F(BluetoothExperimentalChromeOSTest, SecondAdapter) { |
| 381 GetAdapter(); |
| 382 ASSERT_TRUE(adapter_->IsPresent()); |
| 383 |
| 384 // Install an observer, then add a second adapter. Nothing should change, |
| 385 // we ignore the second adapter. |
| 386 TestObserver observer(adapter_); |
| 387 adapter_->AddObserver(&observer); |
| 388 |
| 389 fake_bluetooth_adapter_client_->SetSecondVisible(true); |
| 390 |
| 391 EXPECT_EQ(0, observer.present_changed_count_); |
| 392 |
| 393 EXPECT_TRUE(adapter_->IsPresent()); |
| 394 EXPECT_EQ(FakeBluetoothAdapterClient::kAdapterAddress, |
| 395 adapter_->GetAddress()); |
| 396 |
| 397 // Try removing the first adapter, we should now act as if the adapter |
| 398 // is no longer present rather than fall back to the second. |
| 399 fake_bluetooth_adapter_client_->SetVisible(false); |
| 400 |
| 401 EXPECT_EQ(1, observer.present_changed_count_); |
| 402 EXPECT_FALSE(observer.last_present_); |
| 403 |
| 404 EXPECT_FALSE(adapter_->IsPresent()); |
| 405 |
| 406 // We should have had a device removed. |
| 407 EXPECT_EQ(1, observer.device_removed_count_); |
| 408 EXPECT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress, |
| 409 observer.last_device_address_); |
| 410 |
| 411 // Other callbacks shouldn't be called since the values are false. |
| 412 EXPECT_EQ(0, observer.powered_changed_count_); |
| 413 EXPECT_EQ(0, observer.discovering_changed_count_); |
| 414 EXPECT_FALSE(adapter_->IsPowered()); |
| 415 EXPECT_FALSE(adapter_->IsDiscovering()); |
| 416 |
| 417 observer.device_removed_count_ = 0; |
| 418 |
| 419 // Removing the second adapter shouldn't set anything either. |
| 420 fake_bluetooth_adapter_client_->SetSecondVisible(false); |
| 421 |
| 422 EXPECT_EQ(0, observer.device_removed_count_); |
| 423 EXPECT_EQ(0, observer.powered_changed_count_); |
| 424 EXPECT_EQ(0, observer.discovering_changed_count_); |
| 425 } |
| 426 |
| 427 TEST_F(BluetoothExperimentalChromeOSTest, BecomePowered) { |
| 428 GetAdapter(); |
| 429 ASSERT_FALSE(adapter_->IsPowered()); |
| 430 |
| 431 // Install an observer; expect the AdapterPoweredChanged to be called |
| 432 // with true, and IsPowered() to return true. |
| 433 TestObserver observer(adapter_); |
| 434 adapter_->AddObserver(&observer); |
| 435 |
| 436 adapter_->SetPowered( |
| 437 true, |
| 438 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 439 base::Unretained(this)), |
| 440 base::Bind(&BluetoothExperimentalChromeOSTest::ErrorCallback, |
| 441 base::Unretained(this))); |
| 442 EXPECT_EQ(1, callback_count_); |
| 443 EXPECT_EQ(0, error_callback_count_); |
| 444 |
| 445 EXPECT_EQ(1, observer.powered_changed_count_); |
| 446 EXPECT_TRUE(observer.last_powered_); |
| 447 |
| 448 EXPECT_TRUE(adapter_->IsPowered()); |
| 449 } |
| 450 |
| 451 TEST_F(BluetoothExperimentalChromeOSTest, BecomeNotPowered) { |
| 452 GetAdapter(); |
| 453 adapter_->SetPowered( |
| 454 true, |
| 455 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 456 base::Unretained(this)), |
| 457 base::Bind(&BluetoothExperimentalChromeOSTest::ErrorCallback, |
| 458 base::Unretained(this))); |
| 459 EXPECT_EQ(1, callback_count_); |
| 460 EXPECT_EQ(0, error_callback_count_); |
| 461 callback_count_ = 0; |
| 462 |
| 463 ASSERT_TRUE(adapter_->IsPowered()); |
| 464 |
| 465 // Install an observer; expect the AdapterPoweredChanged to be called |
| 466 // with false, and IsPowered() to return false. |
| 467 TestObserver observer(adapter_); |
| 468 adapter_->AddObserver(&observer); |
| 469 |
| 470 adapter_->SetPowered( |
| 471 false, |
| 472 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 473 base::Unretained(this)), |
| 474 base::Bind(&BluetoothExperimentalChromeOSTest::ErrorCallback, |
| 475 base::Unretained(this))); |
| 476 EXPECT_EQ(1, callback_count_); |
| 477 EXPECT_EQ(0, error_callback_count_); |
| 478 |
| 479 EXPECT_EQ(1, observer.powered_changed_count_); |
| 480 EXPECT_FALSE(observer.last_powered_); |
| 481 |
| 482 EXPECT_FALSE(adapter_->IsPowered()); |
| 483 } |
| 484 |
| 485 TEST_F(BluetoothExperimentalChromeOSTest, StopDiscovery) { |
| 486 base::MessageLoop message_loop(MessageLoop::TYPE_DEFAULT); |
| 487 |
| 488 GetAdapter(); |
| 489 |
| 490 adapter_->SetPowered( |
| 491 true, |
| 492 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 493 base::Unretained(this)), |
| 494 base::Bind(&BluetoothExperimentalChromeOSTest::ErrorCallback, |
| 495 base::Unretained(this))); |
| 496 adapter_->StartDiscovering( |
| 497 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 498 base::Unretained(this)), |
| 499 base::Bind(&BluetoothExperimentalChromeOSTest::ErrorCallback, |
| 500 base::Unretained(this))); |
| 501 EXPECT_EQ(2, callback_count_); |
| 502 EXPECT_EQ(0, error_callback_count_); |
| 503 callback_count_ = 0; |
| 504 |
| 505 ASSERT_TRUE(adapter_->IsPowered()); |
| 506 ASSERT_TRUE(adapter_->IsDiscovering()); |
| 507 |
| 508 // Install an observer; aside from the callback, expect the |
| 509 // AdapterDiscoveringChanged method to be called and no longer to be |
| 510 // discovering, |
| 511 TestObserver observer(adapter_); |
| 512 adapter_->AddObserver(&observer); |
| 513 |
| 514 adapter_->StopDiscovering( |
| 515 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 516 base::Unretained(this)), |
| 517 base::Bind(&BluetoothExperimentalChromeOSTest::ErrorCallback, |
| 518 base::Unretained(this))); |
| 519 EXPECT_EQ(1, callback_count_); |
| 520 EXPECT_EQ(0, error_callback_count_); |
| 521 |
| 522 EXPECT_EQ(1, observer.discovering_changed_count_); |
| 523 EXPECT_FALSE(observer.last_discovering_); |
| 524 |
| 525 EXPECT_FALSE(adapter_->IsDiscovering()); |
| 526 |
| 527 message_loop.RunUntilIdle(); |
| 528 } |
| 529 |
| 530 TEST_F(BluetoothExperimentalChromeOSTest, StopDiscoveryAfterTwoStarts) { |
| 531 base::MessageLoop message_loop(MessageLoop::TYPE_DEFAULT); |
| 532 |
| 533 GetAdapter(); |
| 534 |
| 535 adapter_->SetPowered( |
| 536 true, |
| 537 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 538 base::Unretained(this)), |
| 539 base::Bind(&BluetoothExperimentalChromeOSTest::ErrorCallback, |
| 540 base::Unretained(this))); |
| 541 adapter_->StartDiscovering( |
| 542 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 543 base::Unretained(this)), |
| 544 base::Bind(&BluetoothExperimentalChromeOSTest::ErrorCallback, |
| 545 base::Unretained(this))); |
| 546 EXPECT_EQ(2, callback_count_); |
| 547 EXPECT_EQ(0, error_callback_count_); |
| 548 callback_count_ = 0; |
| 549 |
| 550 ASSERT_TRUE(adapter_->IsPowered()); |
| 551 ASSERT_TRUE(adapter_->IsDiscovering()); |
| 552 |
| 553 // Install an observer and start discovering again; only the callback |
| 554 // should be called since we were already discovering to begin with. |
| 555 TestObserver observer(adapter_); |
| 556 adapter_->AddObserver(&observer); |
| 557 |
| 558 adapter_->StartDiscovering( |
| 559 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 560 base::Unretained(this)), |
| 561 base::Bind(&BluetoothExperimentalChromeOSTest::ErrorCallback, |
| 562 base::Unretained(this))); |
| 563 EXPECT_EQ(1, callback_count_); |
| 564 EXPECT_EQ(0, error_callback_count_); |
| 565 callback_count_ = 0; |
| 566 |
| 567 EXPECT_EQ(0, observer.discovering_changed_count_); |
| 568 |
| 569 // Stop discovering; only the callback should be called since we're still |
| 570 // discovering. The adapter should be still discovering. |
| 571 adapter_->StopDiscovering( |
| 572 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 573 base::Unretained(this)), |
| 574 base::Bind(&BluetoothExperimentalChromeOSTest::ErrorCallback, |
| 575 base::Unretained(this))); |
| 576 EXPECT_EQ(1, callback_count_); |
| 577 EXPECT_EQ(0, error_callback_count_); |
| 578 callback_count_ = 0; |
| 579 |
| 580 EXPECT_EQ(0, observer.discovering_changed_count_); |
| 581 |
| 582 EXPECT_TRUE(adapter_->IsDiscovering()); |
| 583 |
| 584 // Stop discovering one more time; aside from the callback, expect the |
| 585 // AdapterDiscoveringChanged method to be called and no longer to be |
| 586 // discovering, |
| 587 adapter_->StopDiscovering( |
| 588 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 589 base::Unretained(this)), |
| 590 base::Bind(&BluetoothExperimentalChromeOSTest::ErrorCallback, |
| 591 base::Unretained(this))); |
| 592 EXPECT_EQ(1, callback_count_); |
| 593 EXPECT_EQ(0, error_callback_count_); |
| 594 |
| 595 EXPECT_EQ(1, observer.discovering_changed_count_); |
| 596 EXPECT_FALSE(observer.last_discovering_); |
| 597 |
| 598 EXPECT_FALSE(adapter_->IsDiscovering()); |
| 599 |
| 600 message_loop.RunUntilIdle(); |
| 601 } |
| 602 |
| 603 TEST_F(BluetoothExperimentalChromeOSTest, Discovery) { |
| 604 // Test a simulated discovery session. |
| 605 base::MessageLoop message_loop(MessageLoop::TYPE_DEFAULT); |
| 606 |
| 607 fake_bluetooth_device_client_->SetSimulationIntervalMs(10); |
| 608 GetAdapter(); |
| 609 |
| 610 TestObserver observer(adapter_); |
| 611 adapter_->AddObserver(&observer); |
| 612 |
| 613 adapter_->SetPowered( |
| 614 true, |
| 615 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 616 base::Unretained(this)), |
| 617 base::Bind(&BluetoothExperimentalChromeOSTest::ErrorCallback, |
| 618 base::Unretained(this))); |
| 619 adapter_->StartDiscovering( |
| 620 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 621 base::Unretained(this)), |
| 622 base::Bind(&BluetoothExperimentalChromeOSTest::ErrorCallback, |
| 623 base::Unretained(this))); |
| 624 EXPECT_EQ(2, callback_count_); |
| 625 EXPECT_EQ(0, error_callback_count_); |
| 626 callback_count_ = 0; |
| 627 |
| 628 ASSERT_TRUE(adapter_->IsPowered()); |
| 629 ASSERT_TRUE(adapter_->IsDiscovering()); |
| 630 |
| 631 // First device to appear should be an Apple Mouse. |
| 632 message_loop.Run(); |
| 633 |
| 634 EXPECT_EQ(1, observer.device_added_count_); |
| 635 EXPECT_EQ(FakeBluetoothDeviceClient::kAppleMouseAddress, |
| 636 observer.last_device_address_); |
| 637 |
| 638 // Next we should get another two devices... |
| 639 message_loop.Run(); |
| 640 EXPECT_EQ(3, observer.device_added_count_); |
| 641 |
| 642 // Okay, let's run forward until a device is actually removed... |
| 643 while (!observer.device_removed_count_) |
| 644 message_loop.Run(); |
| 645 |
| 646 EXPECT_EQ(1, observer.device_removed_count_); |
| 647 EXPECT_EQ(FakeBluetoothDeviceClient::kVanishingDeviceAddress, |
| 648 observer.last_device_address_); |
| 649 |
| 650 message_loop.RunUntilIdle(); |
| 651 } |
| 652 |
| 653 TEST_F(BluetoothExperimentalChromeOSTest, PoweredAndDiscovering) { |
| 654 base::MessageLoop message_loop(MessageLoop::TYPE_DEFAULT); |
| 655 |
| 656 GetAdapter(); |
| 657 adapter_->SetPowered( |
| 658 true, |
| 659 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 660 base::Unretained(this)), |
| 661 base::Bind(&BluetoothExperimentalChromeOSTest::ErrorCallback, |
| 662 base::Unretained(this))); |
| 663 adapter_->StartDiscovering( |
| 664 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 665 base::Unretained(this)), |
| 666 base::Bind(&BluetoothExperimentalChromeOSTest::ErrorCallback, |
| 667 base::Unretained(this))); |
| 668 EXPECT_EQ(2, callback_count_); |
| 669 EXPECT_EQ(0, error_callback_count_); |
| 670 callback_count_ = 0; |
| 671 |
| 672 // Stop the timers that the simulation uses |
| 673 fake_bluetooth_device_client_->EndDiscoverySimulation( |
| 674 FakeBluetoothAdapterClient::kAdapterPath); |
| 675 message_loop.RunUntilIdle(); |
| 676 |
| 677 ASSERT_TRUE(adapter_->IsPowered()); |
| 678 ASSERT_TRUE(adapter_->IsDiscovering()); |
| 679 |
| 680 fake_bluetooth_adapter_client_->SetVisible(false); |
| 681 ASSERT_FALSE(adapter_->IsPresent()); |
| 682 |
| 683 // Install an observer; expect the AdapterPresentChanged, |
| 684 // AdapterPoweredChanged and AdapterDiscoveringChanged methods to be called |
| 685 // with true, and IsPresent(), IsPowered() and IsDiscovering() to all |
| 686 // return true. |
| 687 TestObserver observer(adapter_); |
| 688 adapter_->AddObserver(&observer); |
| 689 |
| 690 fake_bluetooth_adapter_client_->SetVisible(true); |
| 691 |
| 692 EXPECT_EQ(1, observer.present_changed_count_); |
| 693 EXPECT_TRUE(observer.last_present_); |
| 694 EXPECT_TRUE(adapter_->IsPresent()); |
| 695 |
| 696 EXPECT_EQ(1, observer.powered_changed_count_); |
| 697 EXPECT_TRUE(observer.last_powered_); |
| 698 EXPECT_TRUE(adapter_->IsPowered()); |
| 699 |
| 700 EXPECT_EQ(1, observer.discovering_changed_count_); |
| 701 EXPECT_TRUE(observer.last_discovering_); |
| 702 EXPECT_TRUE(adapter_->IsDiscovering()); |
| 703 |
| 704 observer.present_changed_count_ = 0; |
| 705 observer.powered_changed_count_ = 0; |
| 706 observer.discovering_changed_count_ = 0; |
| 707 |
| 708 // Now mark the adapter not present again. Expect the methods to be called |
| 709 // again, to reset the properties back to false |
| 710 fake_bluetooth_adapter_client_->SetVisible(false); |
| 711 |
| 712 EXPECT_EQ(1, observer.present_changed_count_); |
| 713 EXPECT_FALSE(observer.last_present_); |
| 714 EXPECT_FALSE(adapter_->IsPresent()); |
| 715 |
| 716 EXPECT_EQ(1, observer.powered_changed_count_); |
| 717 EXPECT_FALSE(observer.last_powered_); |
| 718 EXPECT_FALSE(adapter_->IsPowered()); |
| 719 |
| 720 EXPECT_EQ(1, observer.discovering_changed_count_); |
| 721 EXPECT_FALSE(observer.last_discovering_); |
| 722 EXPECT_FALSE(adapter_->IsDiscovering()); |
| 723 } |
| 724 |
| 725 TEST_F(BluetoothExperimentalChromeOSTest, DeviceProperties) { |
| 726 GetAdapter(); |
| 727 |
| 728 BluetoothAdapter::DeviceList devices = adapter_->GetDevices(); |
| 729 ASSERT_EQ(1U, devices.size()); |
| 730 ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress, |
| 731 devices[0]->GetAddress()); |
| 732 |
| 733 // Verify the other device properties. |
| 734 EXPECT_EQ(UTF8ToUTF16(FakeBluetoothDeviceClient::kPairedDeviceName), |
| 735 devices[0]->GetName()); |
| 736 EXPECT_EQ(BluetoothDevice::DEVICE_COMPUTER, devices[0]->GetDeviceType()); |
| 737 EXPECT_TRUE(devices[0]->IsPaired()); |
| 738 EXPECT_FALSE(devices[0]->IsConnected()); |
| 739 EXPECT_FALSE(devices[0]->IsConnectable()); |
| 740 EXPECT_FALSE(devices[0]->IsConnecting()); |
| 741 |
| 742 BluetoothDevice::ServiceList uuids = devices[0]->GetServices(); |
| 743 ASSERT_EQ(2U, uuids.size()); |
| 744 EXPECT_EQ(uuids[0], "00001800-0000-1000-8000-00805f9b34fb"); |
| 745 EXPECT_EQ(uuids[1], "00001801-0000-1000-8000-00805f9b34fb"); |
| 746 } |
| 747 |
| 748 TEST_F(BluetoothExperimentalChromeOSTest, DeviceClassChanged) { |
| 749 // Simulate a change of class of a device, as sometimes occurs |
| 750 // during discovery. |
| 751 GetAdapter(); |
| 752 |
| 753 BluetoothAdapter::DeviceList devices = adapter_->GetDevices(); |
| 754 ASSERT_EQ(1U, devices.size()); |
| 755 ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress, |
| 756 devices[0]->GetAddress()); |
| 757 ASSERT_EQ(BluetoothDevice::DEVICE_COMPUTER, devices[0]->GetDeviceType()); |
| 758 |
| 759 // Install an observer; expect the DeviceChanged method to be called when |
| 760 // we change the class of the device. |
| 761 TestObserver observer(adapter_); |
| 762 adapter_->AddObserver(&observer); |
| 763 |
| 764 FakeBluetoothDeviceClient::Properties* properties = |
| 765 fake_bluetooth_device_client_->GetProperties( |
| 766 FakeBluetoothDeviceClient::kPairedDevicePath); |
| 767 |
| 768 properties->bluetooth_class.ReplaceValue(0x002580); |
| 769 properties->NotifyPropertyChanged(properties->bluetooth_class.name()); |
| 770 |
| 771 EXPECT_EQ(1, observer.device_changed_count_); |
| 772 EXPECT_EQ(devices[0], observer.last_device_); |
| 773 |
| 774 EXPECT_EQ(BluetoothDevice::DEVICE_MOUSE, devices[0]->GetDeviceType()); |
| 775 } |
| 776 |
| 777 TEST_F(BluetoothExperimentalChromeOSTest, DeviceNameChanged) { |
| 778 // Simulate a change of name of a device. |
| 779 GetAdapter(); |
| 780 |
| 781 BluetoothAdapter::DeviceList devices = adapter_->GetDevices(); |
| 782 ASSERT_EQ(1U, devices.size()); |
| 783 ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress, |
| 784 devices[0]->GetAddress()); |
| 785 ASSERT_EQ(UTF8ToUTF16(FakeBluetoothDeviceClient::kPairedDeviceName), |
| 786 devices[0]->GetName()); |
| 787 |
| 788 // Install an observer; expect the DeviceChanged method to be called when |
| 789 // we change the alias of the device. |
| 790 TestObserver observer(adapter_); |
| 791 adapter_->AddObserver(&observer); |
| 792 |
| 793 FakeBluetoothDeviceClient::Properties* properties = |
| 794 fake_bluetooth_device_client_->GetProperties( |
| 795 FakeBluetoothDeviceClient::kPairedDevicePath); |
| 796 |
| 797 static const std::string new_name("New Device Name"); |
| 798 properties->alias.ReplaceValue(new_name); |
| 799 properties->NotifyPropertyChanged(properties->alias.name()); |
| 800 |
| 801 EXPECT_EQ(1, observer.device_changed_count_); |
| 802 EXPECT_EQ(devices[0], observer.last_device_); |
| 803 |
| 804 EXPECT_EQ(UTF8ToUTF16(new_name), devices[0]->GetName()); |
| 805 } |
| 806 |
| 807 TEST_F(BluetoothExperimentalChromeOSTest, DeviceUuidsChanged) { |
| 808 // Simulate a change of advertised services of a device. |
| 809 GetAdapter(); |
| 810 |
| 811 BluetoothAdapter::DeviceList devices = adapter_->GetDevices(); |
| 812 ASSERT_EQ(1U, devices.size()); |
| 813 ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress, |
| 814 devices[0]->GetAddress()); |
| 815 |
| 816 BluetoothDevice::ServiceList uuids = devices[0]->GetServices(); |
| 817 ASSERT_EQ(2U, uuids.size()); |
| 818 ASSERT_EQ(uuids[0], "00001800-0000-1000-8000-00805f9b34fb"); |
| 819 ASSERT_EQ(uuids[1], "00001801-0000-1000-8000-00805f9b34fb"); |
| 820 |
| 821 // Install an observer; expect the DeviceChanged method to be called when |
| 822 // we change the class of the device. |
| 823 TestObserver observer(adapter_); |
| 824 adapter_->AddObserver(&observer); |
| 825 |
| 826 FakeBluetoothDeviceClient::Properties* properties = |
| 827 fake_bluetooth_device_client_->GetProperties( |
| 828 FakeBluetoothDeviceClient::kPairedDevicePath); |
| 829 |
| 830 uuids.push_back("0000110c-0000-1000-8000-00805f9b34fb"); |
| 831 uuids.push_back("0000110e-0000-1000-8000-00805f9b34fb"); |
| 832 uuids.push_back("0000110a-0000-1000-8000-00805f9b34fb"); |
| 833 |
| 834 properties->uuids.ReplaceValue(uuids); |
| 835 properties->NotifyPropertyChanged(properties->uuids.name()); |
| 836 |
| 837 EXPECT_EQ(1, observer.device_changed_count_); |
| 838 EXPECT_EQ(devices[0], observer.last_device_); |
| 839 |
| 840 // Fetching the value should give the new one. |
| 841 uuids = devices[0]->GetServices(); |
| 842 ASSERT_EQ(5U, uuids.size()); |
| 843 EXPECT_EQ(uuids[0], "00001800-0000-1000-8000-00805f9b34fb"); |
| 844 EXPECT_EQ(uuids[1], "00001801-0000-1000-8000-00805f9b34fb"); |
| 845 EXPECT_EQ(uuids[2], "0000110c-0000-1000-8000-00805f9b34fb"); |
| 846 EXPECT_EQ(uuids[3], "0000110e-0000-1000-8000-00805f9b34fb"); |
| 847 EXPECT_EQ(uuids[4], "0000110a-0000-1000-8000-00805f9b34fb"); |
| 848 } |
| 849 |
| 850 TEST_F(BluetoothExperimentalChromeOSTest, ForgetDevice) { |
| 851 GetAdapter(); |
| 852 |
| 853 BluetoothAdapter::DeviceList devices = adapter_->GetDevices(); |
| 854 ASSERT_EQ(1U, devices.size()); |
| 855 ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress, |
| 856 devices[0]->GetAddress()); |
| 857 |
| 858 std::string address = devices[0]->GetAddress(); |
| 859 |
| 860 // Install an observer; expect the DeviceRemoved method to be called |
| 861 // with the device we remove. |
| 862 TestObserver observer(adapter_); |
| 863 adapter_->AddObserver(&observer); |
| 864 |
| 865 devices[0]->Forget( |
| 866 base::Bind(&BluetoothExperimentalChromeOSTest::ErrorCallback, |
| 867 base::Unretained(this))); |
| 868 EXPECT_EQ(0, error_callback_count_); |
| 869 |
| 870 EXPECT_EQ(1, observer.device_removed_count_); |
| 871 EXPECT_EQ(address, observer.last_device_address_); |
| 872 |
| 873 // GetDevices shouldn't return the device either. |
| 874 devices = adapter_->GetDevices(); |
| 875 ASSERT_EQ(0U, devices.size()); |
| 876 } |
| 877 |
| 878 TEST_F(BluetoothExperimentalChromeOSTest, ConnectPairedDevice) { |
| 879 GetAdapter(); |
| 880 |
| 881 BluetoothDevice* device = adapter_->GetDevice( |
| 882 FakeBluetoothDeviceClient::kPairedDeviceAddress); |
| 883 ASSERT_TRUE(device != NULL); |
| 884 ASSERT_TRUE(device->IsPaired()); |
| 885 |
| 886 TestObserver observer(adapter_); |
| 887 adapter_->AddObserver(&observer); |
| 888 |
| 889 // Connect without a pairing delegate; since the device is already Paired |
| 890 // this should succeed and the device should become connected. |
| 891 device->Connect( |
| 892 NULL, |
| 893 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 894 base::Unretained(this)), |
| 895 base::Bind(&BluetoothExperimentalChromeOSTest::ConnectErrorCallback, |
| 896 base::Unretained(this))); |
| 897 |
| 898 EXPECT_EQ(1, callback_count_); |
| 899 EXPECT_EQ(0, error_callback_count_); |
| 900 |
| 901 EXPECT_EQ(1, observer.device_changed_count_); |
| 902 EXPECT_EQ(device, observer.last_device_); |
| 903 |
| 904 EXPECT_TRUE(device->IsConnected()); |
| 905 EXPECT_FALSE(device->IsConnecting()); |
| 906 } |
| 907 |
| 908 TEST_F(BluetoothExperimentalChromeOSTest, ConnectUnpairableDevice) { |
| 909 GetAdapter(); |
| 910 DiscoverDevices(); |
| 911 |
| 912 BluetoothDevice* device = adapter_->GetDevice( |
| 913 FakeBluetoothDeviceClient::kMicrosoftMouseAddress); |
| 914 ASSERT_TRUE(device != NULL); |
| 915 ASSERT_FALSE(device->IsPaired()); |
| 916 |
| 917 TestObserver observer(adapter_); |
| 918 adapter_->AddObserver(&observer); |
| 919 |
| 920 // Connect without a pairing delegate; since the device does not require |
| 921 // pairing, this should succeed and the device should become connected. |
| 922 device->Connect( |
| 923 NULL, |
| 924 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 925 base::Unretained(this)), |
| 926 base::Bind(&BluetoothExperimentalChromeOSTest::ConnectErrorCallback, |
| 927 base::Unretained(this))); |
| 928 |
| 929 EXPECT_EQ(1, callback_count_); |
| 930 EXPECT_EQ(0, error_callback_count_); |
| 931 |
| 932 EXPECT_EQ(1, observer.device_changed_count_); |
| 933 EXPECT_EQ(device, observer.last_device_); |
| 934 |
| 935 EXPECT_TRUE(device->IsConnected()); |
| 936 EXPECT_FALSE(device->IsConnecting()); |
| 937 } |
| 938 |
| 939 TEST_F(BluetoothExperimentalChromeOSTest, ConnectConnectedDevice) { |
| 940 GetAdapter(); |
| 941 |
| 942 BluetoothDevice* device = adapter_->GetDevice( |
| 943 FakeBluetoothDeviceClient::kPairedDeviceAddress); |
| 944 ASSERT_TRUE(device != NULL); |
| 945 ASSERT_TRUE(device->IsPaired()); |
| 946 |
| 947 device->Connect( |
| 948 NULL, |
| 949 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 950 base::Unretained(this)), |
| 951 base::Bind(&BluetoothExperimentalChromeOSTest::ConnectErrorCallback, |
| 952 base::Unretained(this))); |
| 953 |
| 954 ASSERT_EQ(1, callback_count_); |
| 955 ASSERT_EQ(0, error_callback_count_); |
| 956 callback_count_ = 0; |
| 957 |
| 958 ASSERT_TRUE(device->IsConnected()); |
| 959 |
| 960 // Connect again; since the device is already Connected, this shouldn't do |
| 961 // anything, not even the observer method should be called. |
| 962 TestObserver observer(adapter_); |
| 963 adapter_->AddObserver(&observer); |
| 964 |
| 965 device->Connect( |
| 966 NULL, |
| 967 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 968 base::Unretained(this)), |
| 969 base::Bind(&BluetoothExperimentalChromeOSTest::ConnectErrorCallback, |
| 970 base::Unretained(this))); |
| 971 |
| 972 EXPECT_EQ(1, callback_count_); |
| 973 EXPECT_EQ(0, error_callback_count_); |
| 974 |
| 975 EXPECT_EQ(0, observer.device_changed_count_); |
| 976 |
| 977 EXPECT_TRUE(device->IsConnected()); |
| 978 EXPECT_FALSE(device->IsConnecting()); |
| 979 } |
| 980 |
| 981 TEST_F(BluetoothExperimentalChromeOSTest, ConnectDeviceFails) { |
| 982 GetAdapter(); |
| 983 DiscoverDevices(); |
| 984 |
| 985 BluetoothDevice* device = adapter_->GetDevice( |
| 986 FakeBluetoothDeviceClient::kAppleMouseAddress); |
| 987 ASSERT_TRUE(device != NULL); |
| 988 ASSERT_FALSE(device->IsPaired()); |
| 989 |
| 990 TestObserver observer(adapter_); |
| 991 adapter_->AddObserver(&observer); |
| 992 |
| 993 // Connect without a pairing delegate; since the device requires pairing, |
| 994 // this should fail with an error. |
| 995 device->Connect( |
| 996 NULL, |
| 997 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 998 base::Unretained(this)), |
| 999 base::Bind(&BluetoothExperimentalChromeOSTest::ConnectErrorCallback, |
| 1000 base::Unretained(this))); |
| 1001 |
| 1002 EXPECT_EQ(0, callback_count_); |
| 1003 EXPECT_EQ(1, error_callback_count_); |
| 1004 EXPECT_EQ(BluetoothDevice::ERROR_FAILED, last_connect_error_); |
| 1005 |
| 1006 EXPECT_EQ(0, observer.device_changed_count_); |
| 1007 |
| 1008 EXPECT_FALSE(device->IsConnected()); |
| 1009 EXPECT_FALSE(device->IsConnecting()); |
| 1010 } |
| 1011 |
| 1012 TEST_F(BluetoothExperimentalChromeOSTest, PairAppleMouse) { |
| 1013 base::MessageLoop message_loop(MessageLoop::TYPE_DEFAULT); |
| 1014 fake_bluetooth_device_client_->SetSimulationIntervalMs(10); |
| 1015 |
| 1016 GetAdapter(); |
| 1017 DiscoverDevices(); |
| 1018 |
| 1019 // The Apple Mouse requires no PIN or Passkey to pair; this is equivalent |
| 1020 // to Simple Secure Pairing or a device with a fixed 0000 PIN. |
| 1021 BluetoothDevice* device = adapter_->GetDevice( |
| 1022 FakeBluetoothDeviceClient::kAppleMouseAddress); |
| 1023 ASSERT_TRUE(device != NULL); |
| 1024 ASSERT_FALSE(device->IsPaired()); |
| 1025 |
| 1026 TestObserver observer(adapter_); |
| 1027 adapter_->AddObserver(&observer); |
| 1028 |
| 1029 TestPairingDelegate pairing_delegate; |
| 1030 device->Connect( |
| 1031 &pairing_delegate, |
| 1032 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 1033 base::Unretained(this)), |
| 1034 base::Bind(&BluetoothExperimentalChromeOSTest::ConnectErrorCallback, |
| 1035 base::Unretained(this))); |
| 1036 |
| 1037 EXPECT_EQ(0, pairing_delegate.call_count_); |
| 1038 EXPECT_TRUE(device->IsConnecting()); |
| 1039 |
| 1040 message_loop.Run(); |
| 1041 |
| 1042 EXPECT_EQ(1, callback_count_); |
| 1043 EXPECT_EQ(0, error_callback_count_); |
| 1044 |
| 1045 // One change for connected, and one for paired. |
| 1046 EXPECT_EQ(2, observer.device_changed_count_); |
| 1047 EXPECT_EQ(device, observer.last_device_); |
| 1048 |
| 1049 EXPECT_TRUE(device->IsConnected()); |
| 1050 EXPECT_FALSE(device->IsConnecting()); |
| 1051 |
| 1052 EXPECT_TRUE(device->IsPaired()); |
| 1053 |
| 1054 // Pairing dialog should be dismissed |
| 1055 EXPECT_EQ(1, pairing_delegate.call_count_); |
| 1056 EXPECT_EQ(1, pairing_delegate.dismiss_count_); |
| 1057 |
| 1058 // Make sure the trusted property has been set to true. |
| 1059 FakeBluetoothDeviceClient::Properties* properties = |
| 1060 fake_bluetooth_device_client_->GetProperties( |
| 1061 FakeBluetoothDeviceClient::kAppleMousePath); |
| 1062 EXPECT_TRUE(properties->trusted.value()); |
| 1063 |
| 1064 message_loop.RunUntilIdle(); |
| 1065 } |
| 1066 |
| 1067 TEST_F(BluetoothExperimentalChromeOSTest, PairAppleKeyboard) { |
| 1068 base::MessageLoop message_loop(MessageLoop::TYPE_DEFAULT); |
| 1069 fake_bluetooth_device_client_->SetSimulationIntervalMs(10); |
| 1070 |
| 1071 GetAdapter(); |
| 1072 DiscoverDevices(); |
| 1073 |
| 1074 // The Apple Keyboard requires that we display a randomly generated |
| 1075 // PIN on the screen. |
| 1076 BluetoothDevice* device = adapter_->GetDevice( |
| 1077 FakeBluetoothDeviceClient::kAppleKeyboardAddress); |
| 1078 ASSERT_TRUE(device != NULL); |
| 1079 ASSERT_FALSE(device->IsPaired()); |
| 1080 |
| 1081 TestObserver observer(adapter_); |
| 1082 adapter_->AddObserver(&observer); |
| 1083 |
| 1084 TestPairingDelegate pairing_delegate; |
| 1085 device->Connect( |
| 1086 &pairing_delegate, |
| 1087 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 1088 base::Unretained(this)), |
| 1089 base::Bind(&BluetoothExperimentalChromeOSTest::ConnectErrorCallback, |
| 1090 base::Unretained(this))); |
| 1091 |
| 1092 EXPECT_EQ(1, pairing_delegate.call_count_); |
| 1093 EXPECT_EQ(1, pairing_delegate.display_pincode_count_); |
| 1094 EXPECT_EQ("123456", pairing_delegate.last_pincode_); |
| 1095 EXPECT_TRUE(device->IsConnecting()); |
| 1096 |
| 1097 message_loop.Run(); |
| 1098 |
| 1099 EXPECT_EQ(1, callback_count_); |
| 1100 EXPECT_EQ(0, error_callback_count_); |
| 1101 |
| 1102 // One change for connected, and one for paired. |
| 1103 EXPECT_EQ(2, observer.device_changed_count_); |
| 1104 EXPECT_EQ(device, observer.last_device_); |
| 1105 |
| 1106 EXPECT_TRUE(device->IsConnected()); |
| 1107 EXPECT_FALSE(device->IsConnecting()); |
| 1108 |
| 1109 EXPECT_TRUE(device->IsPaired()); |
| 1110 |
| 1111 // Pairing dialog should be dismissed |
| 1112 EXPECT_EQ(2, pairing_delegate.call_count_); |
| 1113 EXPECT_EQ(1, pairing_delegate.dismiss_count_); |
| 1114 |
| 1115 // Make sure the trusted property has been set to true. |
| 1116 FakeBluetoothDeviceClient::Properties* properties = |
| 1117 fake_bluetooth_device_client_->GetProperties( |
| 1118 FakeBluetoothDeviceClient::kAppleKeyboardPath); |
| 1119 EXPECT_TRUE(properties->trusted.value()); |
| 1120 |
| 1121 message_loop.RunUntilIdle(); |
| 1122 } |
| 1123 |
| 1124 TEST_F(BluetoothExperimentalChromeOSTest, PairMotorolaKeyboard) { |
| 1125 base::MessageLoop message_loop(MessageLoop::TYPE_DEFAULT); |
| 1126 fake_bluetooth_device_client_->SetSimulationIntervalMs(10); |
| 1127 |
| 1128 GetAdapter(); |
| 1129 DiscoverDevices(); |
| 1130 |
| 1131 // The Motorola Keyboard fake requires that the user enters a PIN for it. |
| 1132 BluetoothDevice* device = adapter_->GetDevice( |
| 1133 FakeBluetoothDeviceClient::kMotorolaKeyboardAddress); |
| 1134 ASSERT_TRUE(device != NULL); |
| 1135 ASSERT_FALSE(device->IsPaired()); |
| 1136 |
| 1137 TestObserver observer(adapter_); |
| 1138 adapter_->AddObserver(&observer); |
| 1139 |
| 1140 TestPairingDelegate pairing_delegate; |
| 1141 device->Connect( |
| 1142 &pairing_delegate, |
| 1143 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 1144 base::Unretained(this)), |
| 1145 base::Bind(&BluetoothExperimentalChromeOSTest::ConnectErrorCallback, |
| 1146 base::Unretained(this))); |
| 1147 |
| 1148 EXPECT_EQ(1, pairing_delegate.call_count_); |
| 1149 EXPECT_EQ(1, pairing_delegate.request_pincode_count_); |
| 1150 EXPECT_TRUE(device->IsConnecting()); |
| 1151 |
| 1152 // Set the PIN. |
| 1153 device->SetPinCode("123456"); |
| 1154 message_loop.Run(); |
| 1155 |
| 1156 EXPECT_EQ(1, callback_count_); |
| 1157 EXPECT_EQ(0, error_callback_count_); |
| 1158 |
| 1159 // One change for connected, and one for paired. |
| 1160 EXPECT_EQ(2, observer.device_changed_count_); |
| 1161 EXPECT_EQ(device, observer.last_device_); |
| 1162 |
| 1163 EXPECT_TRUE(device->IsConnected()); |
| 1164 EXPECT_FALSE(device->IsConnecting()); |
| 1165 |
| 1166 EXPECT_TRUE(device->IsPaired()); |
| 1167 |
| 1168 // Pairing dialog should be dismissed |
| 1169 EXPECT_EQ(2, pairing_delegate.call_count_); |
| 1170 EXPECT_EQ(1, pairing_delegate.dismiss_count_); |
| 1171 |
| 1172 // Make sure the trusted property has been set to true. |
| 1173 FakeBluetoothDeviceClient::Properties* properties = |
| 1174 fake_bluetooth_device_client_->GetProperties( |
| 1175 FakeBluetoothDeviceClient::kMotorolaKeyboardPath); |
| 1176 EXPECT_TRUE(properties->trusted.value()); |
| 1177 |
| 1178 message_loop.RunUntilIdle(); |
| 1179 } |
| 1180 |
| 1181 TEST_F(BluetoothExperimentalChromeOSTest, PairPhone) { |
| 1182 base::MessageLoop message_loop(MessageLoop::TYPE_DEFAULT); |
| 1183 fake_bluetooth_device_client_->SetSimulationIntervalMs(10); |
| 1184 |
| 1185 GetAdapter(); |
| 1186 DiscoverDevices(); |
| 1187 |
| 1188 // The fake phone requests that we confirm a displayed passkey. |
| 1189 BluetoothDevice* device = adapter_->GetDevice( |
| 1190 FakeBluetoothDeviceClient::kPhoneAddress); |
| 1191 ASSERT_TRUE(device != NULL); |
| 1192 ASSERT_FALSE(device->IsPaired()); |
| 1193 |
| 1194 TestObserver observer(adapter_); |
| 1195 adapter_->AddObserver(&observer); |
| 1196 |
| 1197 TestPairingDelegate pairing_delegate; |
| 1198 device->Connect( |
| 1199 &pairing_delegate, |
| 1200 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 1201 base::Unretained(this)), |
| 1202 base::Bind(&BluetoothExperimentalChromeOSTest::ConnectErrorCallback, |
| 1203 base::Unretained(this))); |
| 1204 |
| 1205 EXPECT_EQ(1, pairing_delegate.call_count_); |
| 1206 EXPECT_EQ(1, pairing_delegate.confirm_passkey_count_); |
| 1207 EXPECT_EQ(123456U, pairing_delegate.last_passkey_); |
| 1208 EXPECT_TRUE(device->IsConnecting()); |
| 1209 |
| 1210 // Confirm the passkey. |
| 1211 device->ConfirmPairing(); |
| 1212 message_loop.Run(); |
| 1213 |
| 1214 EXPECT_EQ(1, callback_count_); |
| 1215 EXPECT_EQ(0, error_callback_count_); |
| 1216 |
| 1217 // One change for connected, and one for paired. |
| 1218 EXPECT_EQ(2, observer.device_changed_count_); |
| 1219 EXPECT_EQ(device, observer.last_device_); |
| 1220 |
| 1221 EXPECT_TRUE(device->IsConnected()); |
| 1222 EXPECT_FALSE(device->IsConnecting()); |
| 1223 |
| 1224 EXPECT_TRUE(device->IsPaired()); |
| 1225 |
| 1226 // Pairing dialog should be dismissed |
| 1227 EXPECT_EQ(2, pairing_delegate.call_count_); |
| 1228 EXPECT_EQ(1, pairing_delegate.dismiss_count_); |
| 1229 |
| 1230 // Make sure the trusted property has been set to true. |
| 1231 FakeBluetoothDeviceClient::Properties* properties = |
| 1232 fake_bluetooth_device_client_->GetProperties( |
| 1233 FakeBluetoothDeviceClient::kPhonePath); |
| 1234 EXPECT_TRUE(properties->trusted.value()); |
| 1235 |
| 1236 message_loop.RunUntilIdle(); |
| 1237 } |
| 1238 |
| 1239 // register agent fails |
| 1240 |
| 1241 // pair fails with error (vanishing device) |
| 1242 // connect after pair fails with error (ms mouse) |
| 1243 |
| 1244 // cancel or reject request pincode/passkey/confirmation |
| 1245 // cancel pairing while in-flight |
| 1246 |
| 1247 } // namespace chromeos |
OLD | NEW |