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 BluetoothExperimentalChromeOSTest : public testing::Test { |
| 27 public: |
| 28 virtual void SetUp() { |
| 29 if (!CommandLine::ForCurrentProcess()->HasSwitch( |
| 30 chromeos::switches::kEnableExperimentalBluetooth)) |
| 31 CommandLine::ForCurrentProcess()->AppendSwitch( |
| 32 chromeos::switches::kEnableExperimentalBluetooth); |
| 33 |
| 34 mock_dbus_thread_manager_ = |
| 35 new MockDBusThreadManagerWithoutGMock(); |
| 36 DBusThreadManager::InitializeForTesting(mock_dbus_thread_manager_); |
| 37 |
| 38 fake_bluetooth_adapter_client_ = |
| 39 mock_dbus_thread_manager_->fake_bluetooth_adapter_client(); |
| 40 fake_bluetooth_device_client_ = |
| 41 mock_dbus_thread_manager_->fake_bluetooth_device_client(); |
| 42 |
| 43 callback_count_ = 0; |
| 44 error_callback_count_ = 0; |
| 45 } |
| 46 |
| 47 virtual void TearDown() { |
| 48 adapter_ = NULL; |
| 49 DBusThreadManager::Shutdown(); |
| 50 } |
| 51 |
| 52 // Generic callbacks |
| 53 void Callback() { |
| 54 ++callback_count_; |
| 55 } |
| 56 |
| 57 void ErrorCallback() { |
| 58 ++error_callback_count_; |
| 59 } |
| 60 |
| 61 protected: |
| 62 // Call to fill the adapter_ member with a BluetoothAdapter instance. |
| 63 void GetAdapter() { |
| 64 BluetoothAdapterFactory::GetAdapter( |
| 65 base::Bind(&BluetoothExperimentalChromeOSTest::SetAdapter, |
| 66 base::Unretained(this))); |
| 67 ASSERT_TRUE(adapter_ != NULL); |
| 68 ASSERT_TRUE(adapter_->IsInitialized()); |
| 69 } |
| 70 |
| 71 void SetAdapter(scoped_refptr<BluetoothAdapter> adapter) { |
| 72 adapter_ = adapter; |
| 73 } |
| 74 |
| 75 FakeBluetoothAdapterClient* fake_bluetooth_adapter_client_; |
| 76 FakeBluetoothDeviceClient* fake_bluetooth_device_client_; |
| 77 MockDBusThreadManagerWithoutGMock* mock_dbus_thread_manager_; |
| 78 scoped_refptr<BluetoothAdapter> adapter_; |
| 79 |
| 80 int callback_count_; |
| 81 int error_callback_count_; |
| 82 }; |
| 83 |
| 84 class TestObserver : public BluetoothAdapter::Observer { |
| 85 public: |
| 86 TestObserver(scoped_refptr<BluetoothAdapter> adapter) |
| 87 : present_changed_count_(0), |
| 88 powered_changed_count_(0), |
| 89 discovering_changed_count_(0), |
| 90 last_present_(false), |
| 91 last_powered_(false), |
| 92 last_discovering_(false), |
| 93 device_added_count_(0), |
| 94 device_changed_count_(0), |
| 95 device_removed_count_(0), |
| 96 last_device_(NULL), |
| 97 adapter_(adapter) { |
| 98 } |
| 99 virtual ~TestObserver() {} |
| 100 |
| 101 virtual void AdapterPresentChanged(BluetoothAdapter* adapter, |
| 102 bool present) OVERRIDE { |
| 103 EXPECT_EQ(adapter_, adapter); |
| 104 |
| 105 ++present_changed_count_; |
| 106 last_present_ = present; |
| 107 } |
| 108 |
| 109 virtual void AdapterPoweredChanged(BluetoothAdapter* adapter, |
| 110 bool powered) OVERRIDE { |
| 111 EXPECT_EQ(adapter_, adapter); |
| 112 |
| 113 ++powered_changed_count_; |
| 114 last_powered_ = powered; |
| 115 } |
| 116 |
| 117 virtual void AdapterDiscoveringChanged(BluetoothAdapter* adapter, |
| 118 bool discovering) OVERRIDE { |
| 119 EXPECT_EQ(adapter_, adapter); |
| 120 |
| 121 ++discovering_changed_count_; |
| 122 last_discovering_ = discovering; |
| 123 |
| 124 QuitMessageLoop(); |
| 125 } |
| 126 |
| 127 virtual void DeviceAdded(BluetoothAdapter* adapter, |
| 128 BluetoothDevice* device) OVERRIDE { |
| 129 EXPECT_EQ(adapter_, adapter); |
| 130 |
| 131 ++device_added_count_; |
| 132 last_device_ = device; |
| 133 last_device_address_ = device->GetAddress(); |
| 134 |
| 135 QuitMessageLoop(); |
| 136 } |
| 137 |
| 138 virtual void DeviceChanged(BluetoothAdapter* adapter, |
| 139 BluetoothDevice* device) OVERRIDE { |
| 140 EXPECT_EQ(adapter_, adapter); |
| 141 |
| 142 ++device_changed_count_; |
| 143 last_device_ = device; |
| 144 last_device_address_ = device->GetAddress(); |
| 145 |
| 146 QuitMessageLoop(); |
| 147 } |
| 148 |
| 149 virtual void DeviceRemoved(BluetoothAdapter* adapter, |
| 150 BluetoothDevice* device) OVERRIDE { |
| 151 EXPECT_EQ(adapter_, adapter); |
| 152 |
| 153 ++device_removed_count_; |
| 154 // Can't save device, it may be freed |
| 155 last_device_address_ = device->GetAddress(); |
| 156 |
| 157 QuitMessageLoop(); |
| 158 } |
| 159 |
| 160 int present_changed_count_; |
| 161 int powered_changed_count_; |
| 162 int discovering_changed_count_; |
| 163 bool last_present_; |
| 164 bool last_powered_; |
| 165 bool last_discovering_; |
| 166 int device_added_count_; |
| 167 int device_changed_count_; |
| 168 int device_removed_count_; |
| 169 BluetoothDevice* last_device_; |
| 170 std::string last_device_address_; |
| 171 |
| 172 private: |
| 173 // Some tests use a message loop since background processing is simulated; |
| 174 // break out of those loops. |
| 175 void QuitMessageLoop() { |
| 176 if (MessageLoop::current() && MessageLoop::current()->is_running()) |
| 177 MessageLoop::current()->Quit(); |
| 178 } |
| 179 |
| 180 scoped_refptr<BluetoothAdapter> adapter_; |
| 181 }; |
| 182 |
| 183 TEST_F(BluetoothExperimentalChromeOSTest, AlreadyPresent) { |
| 184 GetAdapter(); |
| 185 |
| 186 // This verifies that the class gets the list of adapters when created; |
| 187 // and initializes with an existing adapter if there is one. |
| 188 EXPECT_TRUE(adapter_->IsPresent()); |
| 189 EXPECT_FALSE(adapter_->IsPowered()); |
| 190 EXPECT_EQ(FakeBluetoothAdapterClient::kAdapterAddress, |
| 191 adapter_->GetAddress()); |
| 192 EXPECT_EQ(FakeBluetoothAdapterClient::kAdapterName, adapter_->GetName()); |
| 193 EXPECT_FALSE(adapter_->IsDiscovering()); |
| 194 |
| 195 // There should be a device |
| 196 BluetoothAdapter::DeviceList devices = adapter_->GetDevices(); |
| 197 EXPECT_EQ(1U, devices.size()); |
| 198 EXPECT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress, |
| 199 devices[0]->GetAddress()); |
| 200 } |
| 201 |
| 202 TEST_F(BluetoothExperimentalChromeOSTest, BecomePresent) { |
| 203 fake_bluetooth_adapter_client_->SetVisible(false); |
| 204 GetAdapter(); |
| 205 ASSERT_FALSE(adapter_->IsPresent()); |
| 206 |
| 207 // Install an observer; expect the AdapterPresentChanged to be called |
| 208 // with true, and IsPresent() to return true. |
| 209 TestObserver observer(adapter_); |
| 210 adapter_->AddObserver(&observer); |
| 211 |
| 212 fake_bluetooth_adapter_client_->SetVisible(true); |
| 213 |
| 214 EXPECT_EQ(1, observer.present_changed_count_); |
| 215 EXPECT_TRUE(observer.last_present_); |
| 216 |
| 217 EXPECT_TRUE(adapter_->IsPresent()); |
| 218 |
| 219 // We should have had a device announced. |
| 220 EXPECT_EQ(1, observer.device_added_count_); |
| 221 EXPECT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress, |
| 222 observer.last_device_address_); |
| 223 |
| 224 // Other callbacks shouldn't be called if the values are false. |
| 225 EXPECT_EQ(0, observer.powered_changed_count_); |
| 226 EXPECT_EQ(0, observer.discovering_changed_count_); |
| 227 EXPECT_FALSE(adapter_->IsPowered()); |
| 228 EXPECT_FALSE(adapter_->IsDiscovering()); |
| 229 } |
| 230 |
| 231 TEST_F(BluetoothExperimentalChromeOSTest, BecomeNotPresent) { |
| 232 GetAdapter(); |
| 233 ASSERT_TRUE(adapter_->IsPresent()); |
| 234 |
| 235 // Install an observer; expect the AdapterPresentChanged to be called |
| 236 // with false, and IsPresent() to return false. |
| 237 TestObserver observer(adapter_); |
| 238 adapter_->AddObserver(&observer); |
| 239 |
| 240 fake_bluetooth_adapter_client_->SetVisible(false); |
| 241 |
| 242 EXPECT_EQ(1, observer.present_changed_count_); |
| 243 EXPECT_FALSE(observer.last_present_); |
| 244 |
| 245 EXPECT_FALSE(adapter_->IsPresent()); |
| 246 |
| 247 // We should have had a device removed. |
| 248 EXPECT_EQ(1, observer.device_removed_count_); |
| 249 EXPECT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress, |
| 250 observer.last_device_address_); |
| 251 |
| 252 // Other callbacks shouldn't be called since the values are false. |
| 253 EXPECT_EQ(0, observer.powered_changed_count_); |
| 254 EXPECT_EQ(0, observer.discovering_changed_count_); |
| 255 EXPECT_FALSE(adapter_->IsPowered()); |
| 256 EXPECT_FALSE(adapter_->IsDiscovering()); |
| 257 } |
| 258 |
| 259 TEST_F(BluetoothExperimentalChromeOSTest, SecondAdapter) { |
| 260 GetAdapter(); |
| 261 ASSERT_TRUE(adapter_->IsPresent()); |
| 262 |
| 263 // Install an observer, then add a second adapter. Nothing should change, |
| 264 // we ignore the second adapter. |
| 265 TestObserver observer(adapter_); |
| 266 adapter_->AddObserver(&observer); |
| 267 |
| 268 fake_bluetooth_adapter_client_->SetSecondVisible(true); |
| 269 |
| 270 EXPECT_EQ(0, observer.present_changed_count_); |
| 271 |
| 272 EXPECT_TRUE(adapter_->IsPresent()); |
| 273 EXPECT_EQ(FakeBluetoothAdapterClient::kAdapterAddress, |
| 274 adapter_->GetAddress()); |
| 275 |
| 276 // Try removing the first adapter, we should now act as if the adapter |
| 277 // is no longer present rather than fall back to the second. |
| 278 fake_bluetooth_adapter_client_->SetVisible(false); |
| 279 |
| 280 EXPECT_EQ(1, observer.present_changed_count_); |
| 281 EXPECT_FALSE(observer.last_present_); |
| 282 |
| 283 EXPECT_FALSE(adapter_->IsPresent()); |
| 284 |
| 285 // We should have had a device removed. |
| 286 EXPECT_EQ(1, observer.device_removed_count_); |
| 287 EXPECT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress, |
| 288 observer.last_device_address_); |
| 289 |
| 290 // Other callbacks shouldn't be called since the values are false. |
| 291 EXPECT_EQ(0, observer.powered_changed_count_); |
| 292 EXPECT_EQ(0, observer.discovering_changed_count_); |
| 293 EXPECT_FALSE(adapter_->IsPowered()); |
| 294 EXPECT_FALSE(adapter_->IsDiscovering()); |
| 295 |
| 296 observer.device_removed_count_ = 0; |
| 297 |
| 298 // Removing the second adapter shouldn't set anything either. |
| 299 fake_bluetooth_adapter_client_->SetSecondVisible(false); |
| 300 |
| 301 EXPECT_EQ(0, observer.device_removed_count_); |
| 302 EXPECT_EQ(0, observer.powered_changed_count_); |
| 303 EXPECT_EQ(0, observer.discovering_changed_count_); |
| 304 } |
| 305 |
| 306 TEST_F(BluetoothExperimentalChromeOSTest, BecomePowered) { |
| 307 GetAdapter(); |
| 308 ASSERT_FALSE(adapter_->IsPowered()); |
| 309 |
| 310 // Install an observer; expect the AdapterPoweredChanged to be called |
| 311 // with true, and IsPowered() to return true. |
| 312 TestObserver observer(adapter_); |
| 313 adapter_->AddObserver(&observer); |
| 314 |
| 315 adapter_->SetPowered( |
| 316 true, |
| 317 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 318 base::Unretained(this)), |
| 319 base::Bind(&BluetoothExperimentalChromeOSTest::ErrorCallback, |
| 320 base::Unretained(this))); |
| 321 EXPECT_EQ(1, callback_count_); |
| 322 EXPECT_EQ(0, error_callback_count_); |
| 323 |
| 324 EXPECT_EQ(1, observer.powered_changed_count_); |
| 325 EXPECT_TRUE(observer.last_powered_); |
| 326 |
| 327 EXPECT_TRUE(adapter_->IsPowered()); |
| 328 } |
| 329 |
| 330 TEST_F(BluetoothExperimentalChromeOSTest, BecomeNotPowered) { |
| 331 GetAdapter(); |
| 332 adapter_->SetPowered( |
| 333 true, |
| 334 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 335 base::Unretained(this)), |
| 336 base::Bind(&BluetoothExperimentalChromeOSTest::ErrorCallback, |
| 337 base::Unretained(this))); |
| 338 EXPECT_EQ(1, callback_count_); |
| 339 EXPECT_EQ(0, error_callback_count_); |
| 340 callback_count_ = 0; |
| 341 |
| 342 ASSERT_TRUE(adapter_->IsPowered()); |
| 343 |
| 344 // Install an observer; expect the AdapterPoweredChanged to be called |
| 345 // with false, and IsPowered() to return false. |
| 346 TestObserver observer(adapter_); |
| 347 adapter_->AddObserver(&observer); |
| 348 |
| 349 adapter_->SetPowered( |
| 350 false, |
| 351 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 352 base::Unretained(this)), |
| 353 base::Bind(&BluetoothExperimentalChromeOSTest::ErrorCallback, |
| 354 base::Unretained(this))); |
| 355 EXPECT_EQ(1, callback_count_); |
| 356 EXPECT_EQ(0, error_callback_count_); |
| 357 |
| 358 EXPECT_EQ(1, observer.powered_changed_count_); |
| 359 EXPECT_FALSE(observer.last_powered_); |
| 360 |
| 361 EXPECT_FALSE(adapter_->IsPowered()); |
| 362 } |
| 363 |
| 364 TEST_F(BluetoothExperimentalChromeOSTest, StopDiscovery) { |
| 365 base::MessageLoop message_loop(MessageLoop::TYPE_DEFAULT); |
| 366 |
| 367 GetAdapter(); |
| 368 |
| 369 adapter_->SetPowered( |
| 370 true, |
| 371 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 372 base::Unretained(this)), |
| 373 base::Bind(&BluetoothExperimentalChromeOSTest::ErrorCallback, |
| 374 base::Unretained(this))); |
| 375 adapter_->StartDiscovering( |
| 376 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 377 base::Unretained(this)), |
| 378 base::Bind(&BluetoothExperimentalChromeOSTest::ErrorCallback, |
| 379 base::Unretained(this))); |
| 380 EXPECT_EQ(2, callback_count_); |
| 381 EXPECT_EQ(0, error_callback_count_); |
| 382 callback_count_ = 0; |
| 383 |
| 384 ASSERT_TRUE(adapter_->IsPowered()); |
| 385 ASSERT_TRUE(adapter_->IsDiscovering()); |
| 386 |
| 387 // Install an observer; aside from the callback, expect the |
| 388 // AdapterDiscoveringChanged method to be called and no longer to be |
| 389 // discovering, |
| 390 TestObserver observer(adapter_); |
| 391 adapter_->AddObserver(&observer); |
| 392 |
| 393 adapter_->StopDiscovering( |
| 394 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 395 base::Unretained(this)), |
| 396 base::Bind(&BluetoothExperimentalChromeOSTest::ErrorCallback, |
| 397 base::Unretained(this))); |
| 398 EXPECT_EQ(1, callback_count_); |
| 399 EXPECT_EQ(0, error_callback_count_); |
| 400 |
| 401 EXPECT_EQ(1, observer.discovering_changed_count_); |
| 402 EXPECT_FALSE(observer.last_discovering_); |
| 403 |
| 404 EXPECT_FALSE(adapter_->IsDiscovering()); |
| 405 |
| 406 message_loop.RunUntilIdle(); |
| 407 } |
| 408 |
| 409 TEST_F(BluetoothExperimentalChromeOSTest, StopDiscoveryAfterTwoStarts) { |
| 410 base::MessageLoop message_loop(MessageLoop::TYPE_DEFAULT); |
| 411 |
| 412 GetAdapter(); |
| 413 |
| 414 adapter_->SetPowered( |
| 415 true, |
| 416 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 417 base::Unretained(this)), |
| 418 base::Bind(&BluetoothExperimentalChromeOSTest::ErrorCallback, |
| 419 base::Unretained(this))); |
| 420 adapter_->StartDiscovering( |
| 421 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 422 base::Unretained(this)), |
| 423 base::Bind(&BluetoothExperimentalChromeOSTest::ErrorCallback, |
| 424 base::Unretained(this))); |
| 425 EXPECT_EQ(2, callback_count_); |
| 426 EXPECT_EQ(0, error_callback_count_); |
| 427 callback_count_ = 0; |
| 428 |
| 429 ASSERT_TRUE(adapter_->IsPowered()); |
| 430 ASSERT_TRUE(adapter_->IsDiscovering()); |
| 431 |
| 432 // Install an observer and start discovering again; only the callback |
| 433 // should be called since we were already discovering to begin with. |
| 434 TestObserver observer(adapter_); |
| 435 adapter_->AddObserver(&observer); |
| 436 |
| 437 adapter_->StartDiscovering( |
| 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 callback_count_ = 0; |
| 445 |
| 446 EXPECT_EQ(0, observer.discovering_changed_count_); |
| 447 |
| 448 // Stop discovering; only the callback should be called since we're still |
| 449 // discovering. The adapter should be still discovering. |
| 450 adapter_->StopDiscovering( |
| 451 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 452 base::Unretained(this)), |
| 453 base::Bind(&BluetoothExperimentalChromeOSTest::ErrorCallback, |
| 454 base::Unretained(this))); |
| 455 EXPECT_EQ(1, callback_count_); |
| 456 EXPECT_EQ(0, error_callback_count_); |
| 457 callback_count_ = 0; |
| 458 |
| 459 EXPECT_EQ(0, observer.discovering_changed_count_); |
| 460 |
| 461 EXPECT_TRUE(adapter_->IsDiscovering()); |
| 462 |
| 463 // Stop discovering one more time; aside from the callback, expect the |
| 464 // AdapterDiscoveringChanged method to be called and no longer to be |
| 465 // discovering, |
| 466 adapter_->StopDiscovering( |
| 467 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 468 base::Unretained(this)), |
| 469 base::Bind(&BluetoothExperimentalChromeOSTest::ErrorCallback, |
| 470 base::Unretained(this))); |
| 471 EXPECT_EQ(1, callback_count_); |
| 472 EXPECT_EQ(0, error_callback_count_); |
| 473 |
| 474 EXPECT_EQ(1, observer.discovering_changed_count_); |
| 475 EXPECT_FALSE(observer.last_discovering_); |
| 476 |
| 477 EXPECT_FALSE(adapter_->IsDiscovering()); |
| 478 |
| 479 message_loop.RunUntilIdle(); |
| 480 } |
| 481 |
| 482 TEST_F(BluetoothExperimentalChromeOSTest, Discovery) { |
| 483 // Test a simulated discovery session. |
| 484 base::MessageLoop message_loop(MessageLoop::TYPE_DEFAULT); |
| 485 |
| 486 fake_bluetooth_device_client_->SetDiscoverySimulationIntervalMs(10); |
| 487 GetAdapter(); |
| 488 |
| 489 TestObserver observer(adapter_); |
| 490 adapter_->AddObserver(&observer); |
| 491 |
| 492 adapter_->SetPowered( |
| 493 true, |
| 494 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 495 base::Unretained(this)), |
| 496 base::Bind(&BluetoothExperimentalChromeOSTest::ErrorCallback, |
| 497 base::Unretained(this))); |
| 498 adapter_->StartDiscovering( |
| 499 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 500 base::Unretained(this)), |
| 501 base::Bind(&BluetoothExperimentalChromeOSTest::ErrorCallback, |
| 502 base::Unretained(this))); |
| 503 EXPECT_EQ(2, callback_count_); |
| 504 EXPECT_EQ(0, error_callback_count_); |
| 505 callback_count_ = 0; |
| 506 |
| 507 ASSERT_TRUE(adapter_->IsPowered()); |
| 508 ASSERT_TRUE(adapter_->IsDiscovering()); |
| 509 |
| 510 // First device to appear should be an Apple Mouse. |
| 511 message_loop.Run(); |
| 512 |
| 513 EXPECT_EQ(1, observer.device_added_count_); |
| 514 EXPECT_EQ(FakeBluetoothDeviceClient::kAppleMouseAddress, |
| 515 observer.last_device_address_); |
| 516 |
| 517 // Next we should get another two devices... |
| 518 message_loop.Run(); |
| 519 EXPECT_EQ(3, observer.device_added_count_); |
| 520 |
| 521 // Okay, let's run forward until a device is actually removed... |
| 522 while (!observer.device_removed_count_) |
| 523 message_loop.Run(); |
| 524 |
| 525 EXPECT_EQ(1, observer.device_removed_count_); |
| 526 EXPECT_EQ(FakeBluetoothDeviceClient::kVanishingDeviceAddress, |
| 527 observer.last_device_address_); |
| 528 |
| 529 message_loop.RunUntilIdle(); |
| 530 } |
| 531 |
| 532 TEST_F(BluetoothExperimentalChromeOSTest, PoweredAndDiscovering) { |
| 533 base::MessageLoop message_loop(MessageLoop::TYPE_DEFAULT); |
| 534 |
| 535 GetAdapter(); |
| 536 adapter_->SetPowered( |
| 537 true, |
| 538 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 539 base::Unretained(this)), |
| 540 base::Bind(&BluetoothExperimentalChromeOSTest::ErrorCallback, |
| 541 base::Unretained(this))); |
| 542 adapter_->StartDiscovering( |
| 543 base::Bind(&BluetoothExperimentalChromeOSTest::Callback, |
| 544 base::Unretained(this)), |
| 545 base::Bind(&BluetoothExperimentalChromeOSTest::ErrorCallback, |
| 546 base::Unretained(this))); |
| 547 EXPECT_EQ(2, callback_count_); |
| 548 EXPECT_EQ(0, error_callback_count_); |
| 549 callback_count_ = 0; |
| 550 |
| 551 // Stop the timers that the simulation uses |
| 552 fake_bluetooth_device_client_->EndDiscoverySimulation( |
| 553 FakeBluetoothAdapterClient::kAdapterPath); |
| 554 message_loop.RunUntilIdle(); |
| 555 |
| 556 ASSERT_TRUE(adapter_->IsPowered()); |
| 557 ASSERT_TRUE(adapter_->IsDiscovering()); |
| 558 |
| 559 fake_bluetooth_adapter_client_->SetVisible(false); |
| 560 ASSERT_FALSE(adapter_->IsPresent()); |
| 561 |
| 562 // Install an observer; expect the AdapterPresentChanged, |
| 563 // AdapterPoweredChanged and AdapterDiscoveringChanged methods to be called |
| 564 // with true, and IsPresent(), IsPowered() and IsDiscovering() to all |
| 565 // return true. |
| 566 TestObserver observer(adapter_); |
| 567 adapter_->AddObserver(&observer); |
| 568 |
| 569 fake_bluetooth_adapter_client_->SetVisible(true); |
| 570 |
| 571 EXPECT_EQ(1, observer.present_changed_count_); |
| 572 EXPECT_TRUE(observer.last_present_); |
| 573 EXPECT_TRUE(adapter_->IsPresent()); |
| 574 |
| 575 EXPECT_EQ(1, observer.powered_changed_count_); |
| 576 EXPECT_TRUE(observer.last_powered_); |
| 577 EXPECT_TRUE(adapter_->IsPowered()); |
| 578 |
| 579 EXPECT_EQ(1, observer.discovering_changed_count_); |
| 580 EXPECT_TRUE(observer.last_discovering_); |
| 581 EXPECT_TRUE(adapter_->IsDiscovering()); |
| 582 |
| 583 observer.present_changed_count_ = 0; |
| 584 observer.powered_changed_count_ = 0; |
| 585 observer.discovering_changed_count_ = 0; |
| 586 |
| 587 // Now mark the adapter not present again. Expect the methods to be called |
| 588 // again, to reset the properties back to false |
| 589 fake_bluetooth_adapter_client_->SetVisible(false); |
| 590 |
| 591 EXPECT_EQ(1, observer.present_changed_count_); |
| 592 EXPECT_FALSE(observer.last_present_); |
| 593 EXPECT_FALSE(adapter_->IsPresent()); |
| 594 |
| 595 EXPECT_EQ(1, observer.powered_changed_count_); |
| 596 EXPECT_FALSE(observer.last_powered_); |
| 597 EXPECT_FALSE(adapter_->IsPowered()); |
| 598 |
| 599 EXPECT_EQ(1, observer.discovering_changed_count_); |
| 600 EXPECT_FALSE(observer.last_discovering_); |
| 601 EXPECT_FALSE(adapter_->IsDiscovering()); |
| 602 } |
| 603 |
| 604 // trusted property set during pair |
| 605 // paired property announced via change |
| 606 // |
| 607 // connected property set during connect |
| 608 |
| 609 TEST_F(BluetoothExperimentalChromeOSTest, DeviceProperties) { |
| 610 GetAdapter(); |
| 611 |
| 612 BluetoothAdapter::DeviceList devices = adapter_->GetDevices(); |
| 613 ASSERT_EQ(1U, devices.size()); |
| 614 ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress, |
| 615 devices[0]->GetAddress()); |
| 616 |
| 617 // Verify the other device properties. |
| 618 EXPECT_EQ(UTF8ToUTF16(FakeBluetoothDeviceClient::kPairedDeviceName), |
| 619 devices[0]->GetName()); |
| 620 EXPECT_EQ(BluetoothDevice::DEVICE_COMPUTER, devices[0]->GetDeviceType()); |
| 621 EXPECT_TRUE(devices[0]->IsPaired()); |
| 622 EXPECT_FALSE(devices[0]->IsConnected()); |
| 623 EXPECT_FALSE(devices[0]->IsConnectable()); |
| 624 EXPECT_FALSE(devices[0]->IsConnecting()); |
| 625 |
| 626 BluetoothDevice::ServiceList uuids = devices[0]->GetServices(); |
| 627 ASSERT_EQ(2U, uuids.size()); |
| 628 EXPECT_EQ(uuids[0], "00001800-0000-1000-8000-00805f9b34fb"); |
| 629 EXPECT_EQ(uuids[1], "00001801-0000-1000-8000-00805f9b34fb"); |
| 630 } |
| 631 |
| 632 TEST_F(BluetoothExperimentalChromeOSTest, DeviceClassChanged) { |
| 633 // Simulate a change of class of a device, as sometimes occurs |
| 634 // during discovery. |
| 635 GetAdapter(); |
| 636 |
| 637 BluetoothAdapter::DeviceList devices = adapter_->GetDevices(); |
| 638 ASSERT_EQ(1U, devices.size()); |
| 639 ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress, |
| 640 devices[0]->GetAddress()); |
| 641 ASSERT_EQ(BluetoothDevice::DEVICE_COMPUTER, devices[0]->GetDeviceType()); |
| 642 |
| 643 // Install an observer; expect the DeviceChanged method to be called when |
| 644 // we change the class of the device. |
| 645 TestObserver observer(adapter_); |
| 646 adapter_->AddObserver(&observer); |
| 647 |
| 648 FakeBluetoothDeviceClient::Properties* properties = |
| 649 fake_bluetooth_device_client_->GetProperties( |
| 650 FakeBluetoothDeviceClient::kPairedDevicePath); |
| 651 |
| 652 properties->bluetooth_class.ReplaceValue(0x002580); |
| 653 properties->NotifyPropertyChanged(properties->bluetooth_class.name()); |
| 654 |
| 655 EXPECT_EQ(1, observer.device_changed_count_); |
| 656 EXPECT_EQ(devices[0], observer.last_device_); |
| 657 |
| 658 EXPECT_EQ(BluetoothDevice::DEVICE_MOUSE, devices[0]->GetDeviceType()); |
| 659 } |
| 660 |
| 661 TEST_F(BluetoothExperimentalChromeOSTest, DeviceNameChanged) { |
| 662 // Simulate a change of name of a device. |
| 663 GetAdapter(); |
| 664 |
| 665 BluetoothAdapter::DeviceList devices = adapter_->GetDevices(); |
| 666 ASSERT_EQ(1U, devices.size()); |
| 667 ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress, |
| 668 devices[0]->GetAddress()); |
| 669 ASSERT_EQ(UTF8ToUTF16(FakeBluetoothDeviceClient::kPairedDeviceName), |
| 670 devices[0]->GetName()); |
| 671 |
| 672 // Install an observer; expect the DeviceChanged method to be called when |
| 673 // we change the alias of the device. |
| 674 TestObserver observer(adapter_); |
| 675 adapter_->AddObserver(&observer); |
| 676 |
| 677 FakeBluetoothDeviceClient::Properties* properties = |
| 678 fake_bluetooth_device_client_->GetProperties( |
| 679 FakeBluetoothDeviceClient::kPairedDevicePath); |
| 680 |
| 681 static const std::string new_name("New Device Name"); |
| 682 properties->alias.ReplaceValue(new_name); |
| 683 properties->NotifyPropertyChanged(properties->alias.name()); |
| 684 |
| 685 EXPECT_EQ(1, observer.device_changed_count_); |
| 686 EXPECT_EQ(devices[0], observer.last_device_); |
| 687 |
| 688 EXPECT_EQ(UTF8ToUTF16(new_name), devices[0]->GetName()); |
| 689 } |
| 690 |
| 691 TEST_F(BluetoothExperimentalChromeOSTest, DeviceUuidsChanged) { |
| 692 // Simulate a change of advertised services of a device. |
| 693 GetAdapter(); |
| 694 |
| 695 BluetoothAdapter::DeviceList devices = adapter_->GetDevices(); |
| 696 ASSERT_EQ(1U, devices.size()); |
| 697 ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress, |
| 698 devices[0]->GetAddress()); |
| 699 |
| 700 BluetoothDevice::ServiceList uuids = devices[0]->GetServices(); |
| 701 ASSERT_EQ(2U, uuids.size()); |
| 702 ASSERT_EQ(uuids[0], "00001800-0000-1000-8000-00805f9b34fb"); |
| 703 ASSERT_EQ(uuids[1], "00001801-0000-1000-8000-00805f9b34fb"); |
| 704 |
| 705 // Install an observer; expect the DeviceChanged method to be called when |
| 706 // we change the class of the device. |
| 707 TestObserver observer(adapter_); |
| 708 adapter_->AddObserver(&observer); |
| 709 |
| 710 FakeBluetoothDeviceClient::Properties* properties = |
| 711 fake_bluetooth_device_client_->GetProperties( |
| 712 FakeBluetoothDeviceClient::kPairedDevicePath); |
| 713 |
| 714 uuids.push_back("0000110c-0000-1000-8000-00805f9b34fb"); |
| 715 uuids.push_back("0000110e-0000-1000-8000-00805f9b34fb"); |
| 716 uuids.push_back("0000110a-0000-1000-8000-00805f9b34fb"); |
| 717 |
| 718 properties->uuids.ReplaceValue(uuids); |
| 719 properties->NotifyPropertyChanged(properties->uuids.name()); |
| 720 |
| 721 EXPECT_EQ(1, observer.device_changed_count_); |
| 722 EXPECT_EQ(devices[0], observer.last_device_); |
| 723 |
| 724 // Fetching the value should give the new one. |
| 725 uuids = devices[0]->GetServices(); |
| 726 ASSERT_EQ(5U, uuids.size()); |
| 727 EXPECT_EQ(uuids[0], "00001800-0000-1000-8000-00805f9b34fb"); |
| 728 EXPECT_EQ(uuids[1], "00001801-0000-1000-8000-00805f9b34fb"); |
| 729 EXPECT_EQ(uuids[2], "0000110c-0000-1000-8000-00805f9b34fb"); |
| 730 EXPECT_EQ(uuids[3], "0000110e-0000-1000-8000-00805f9b34fb"); |
| 731 EXPECT_EQ(uuids[4], "0000110a-0000-1000-8000-00805f9b34fb"); |
| 732 } |
| 733 |
| 734 TEST_F(BluetoothExperimentalChromeOSTest, ForgetDevice) { |
| 735 GetAdapter(); |
| 736 |
| 737 BluetoothAdapter::DeviceList devices = adapter_->GetDevices(); |
| 738 ASSERT_EQ(1U, devices.size()); |
| 739 ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress, |
| 740 devices[0]->GetAddress()); |
| 741 |
| 742 std::string address = devices[0]->GetAddress(); |
| 743 |
| 744 // Install an observer; expect the DeviceRemoved method to be called |
| 745 // with the device we remove. |
| 746 TestObserver observer(adapter_); |
| 747 adapter_->AddObserver(&observer); |
| 748 |
| 749 devices[0]->Forget( |
| 750 base::Bind(&BluetoothExperimentalChromeOSTest::ErrorCallback, |
| 751 base::Unretained(this))); |
| 752 EXPECT_EQ(0, error_callback_count_); |
| 753 |
| 754 EXPECT_EQ(1, observer.device_removed_count_); |
| 755 EXPECT_EQ(address, observer.last_device_address_); |
| 756 |
| 757 // GetDevices shouldn't return the device either. |
| 758 devices = adapter_->GetDevices(); |
| 759 ASSERT_EQ(0U, devices.size()); |
| 760 } |
| 761 |
| 762 } // namespace chromeos |
OLD | NEW |