1 // Copyright (c) 2012 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 <string> 6 7 #include "base/bind.h" 8 #include "base/memory/ref_counted.h" 9 #include "base/test/test_simple_task_runner.h" 10 #include "device/bluetooth/bluetooth_adapter.h" 11 #include "device/bluetooth/bluetooth_adapter_win.h" 12 #include "device/bluetooth/bluetooth_device.h" 13 #include "device/bluetooth/bluetooth_task_manager_win.h" 14 #include "testing/gtest/include/gtest/gtest.h" 15 16 namespace { 17 18 const char kAdapterAddress[] = "A1:B2:C3:D4:E5:F6"; 19 const char kAdapterName[] = "Bluetooth Adapter Name"; 20 21 22 void MakeDeviceState(const std::string& name, 23 const std::string& address, 24 device::BluetoothTaskManagerWin::DeviceState* state) { 25 state->name = name; 26 state->address = address; 27 state->bluetooth_class = 0; 28 state->authenticated = false; 29 state->connected = false; 30 } 31 32 class AdapterObserver : public device::BluetoothAdapter::Observer { 33 public: 34 AdapterObserver() 35 : num_present_changed_(0), 36 num_powered_changed_(0), 37 num_discovering_changed_(0), 38 num_device_added_(0) { 39 } 40 41 virtual void AdapterPresentChanged( 42 device::BluetoothAdapter* adapter, bool present) OVERRIDE { 43 num_present_changed_++; 44 } 45 46 virtual void AdapterPoweredChanged( 47 device::BluetoothAdapter* adapter, bool powered) OVERRIDE { 48 num_powered_changed_++; 49 } 50 51 virtual void AdapterDiscoveringChanged( 52 device::BluetoothAdapter* adapter, bool discovering) OVERRIDE { 53 num_discovering_changed_++; 54 } 55 56 virtual void DeviceAdded( 57 device::BluetoothAdapter* adapter, 58 device::BluetoothDevice* device) OVERRIDE { 59 num_device_added_++; 60 } 61 62 int num_present_changed() const { 63 return num_present_changed_; 64 } 65 66 int num_powered_changed() const { 67 return num_powered_changed_; 68 } 69 70 int num_discovering_changed() const { 71 return num_discovering_changed_; 72 } 73 74 int num_device_added() const { 75 return num_device_added_; 76 } 77 78 private: 79 int num_present_changed_; 80 int num_powered_changed_; 81 int num_discovering_changed_; 82 int num_device_added_; 83 }; 84 85 } // namespace 86 87 namespace device { 88 89 class BluetoothAdapterWinTest : public testing::Test { 90 public: 91 BluetoothAdapterWinTest() 92 : ui_task_runner_(new base::TestSimpleTaskRunner()), 93 bluetooth_task_runner_(new base::TestSimpleTaskRunner()), 94 adapter_(new BluetoothAdapterWin( 95 base::Bind(&BluetoothAdapterWinTest::RunInitCallback, 96 base::Unretained(this)))), 97 adapter_win_(static_cast<BluetoothAdapterWin*>(adapter_.get())), 98 init_callback_called_(false) { 99 adapter_win_->InitForTest(ui_task_runner_, bluetooth_task_runner_); 100 } 101 102 virtual void SetUp() OVERRIDE { 103 adapter_win_->AddObserver(&adapter_observer_); 104 num_start_discovery_callbacks_ = 0; 105 num_start_discovery_error_callbacks_ = 0; 106 num_stop_discovery_callbacks_ = 0; 107 num_stop_discovery_error_callbacks_ = 0; 108 } 109 110 virtual void TearDown() OVERRIDE { 111 adapter_win_->RemoveObserver(&adapter_observer_); 112 } 113 114 void RunInitCallback() { 115 init_callback_called_ = true; 116 } 117 118 void IncrementNumStartDiscoveryCallbacks() { 119 num_start_discovery_callbacks_++; 120 } 121 122 void IncrementNumStartDiscoveryErrorCallbacks() { 123 num_start_discovery_error_callbacks_++; 124 } 125 126 void IncrementNumStopDiscoveryCallbacks() { 127 num_stop_discovery_callbacks_++; 128 } 129 130 void IncrementNumStopDiscoveryErrorCallbacks() { 131 num_stop_discovery_error_callbacks_++; 132 } 133 134 void CallAddDiscoverySession( 135 const base::Closure& callback, 136 const BluetoothAdapter::ErrorCallback& error_callback) { 137 adapter_win_->AddDiscoverySession(callback, error_callback); 138 } 139 140 void CallRemoveDiscoverySession( 141 const base::Closure& callback, 142 const BluetoothAdapter::ErrorCallback& error_callback) { 143 adapter_win_->RemoveDiscoverySession(callback, error_callback); 144 } 145 146 protected: 147 scoped_refptr<base::TestSimpleTaskRunner> ui_task_runner_; 148 scoped_refptr<base::TestSimpleTaskRunner> bluetooth_task_runner_; 149 scoped_refptr<BluetoothAdapter> adapter_; 150 BluetoothAdapterWin* adapter_win_; 151 AdapterObserver adapter_observer_; 152 bool init_callback_called_; 153 int num_start_discovery_callbacks_; 154 int num_start_discovery_error_callbacks_; 155 int num_stop_discovery_callbacks_; 156 int num_stop_discovery_error_callbacks_; 157 }; 158 159 TEST_F(BluetoothAdapterWinTest, AdapterNotPresent) { 160 BluetoothTaskManagerWin::AdapterState state; 161 adapter_win_->AdapterStateChanged(state); 162 EXPECT_FALSE(adapter_win_->IsPresent()); 163 } 164 165 TEST_F(BluetoothAdapterWinTest, AdapterPresent) { 166 BluetoothTaskManagerWin::AdapterState state; 167 state.address = kAdapterAddress; 168 state.name = kAdapterName; 169 adapter_win_->AdapterStateChanged(state); 170 EXPECT_TRUE(adapter_win_->IsPresent()); 171 } 172 173 TEST_F(BluetoothAdapterWinTest, AdapterPresentChanged) { 174 BluetoothTaskManagerWin::AdapterState state; 175 state.address = kAdapterAddress; 176 state.name = kAdapterName; 177 adapter_win_->AdapterStateChanged(state); 178 EXPECT_EQ(1, adapter_observer_.num_present_changed()); 179 adapter_win_->AdapterStateChanged(state); 180 EXPECT_EQ(1, adapter_observer_.num_present_changed()); 181 BluetoothTaskManagerWin::AdapterState empty_state; 182 adapter_win_->AdapterStateChanged(empty_state); 183 EXPECT_EQ(2, adapter_observer_.num_present_changed()); 184 } 185 186 TEST_F(BluetoothAdapterWinTest, AdapterPoweredChanged) { 187 BluetoothTaskManagerWin::AdapterState state; 188 state.powered = true; 189 adapter_win_->AdapterStateChanged(state); 190 EXPECT_EQ(1, adapter_observer_.num_powered_changed()); 191 adapter_win_->AdapterStateChanged(state); 192 EXPECT_EQ(1, adapter_observer_.num_powered_changed()); 193 state.powered = false; 194 adapter_win_->AdapterStateChanged(state); 195 EXPECT_EQ(2, adapter_observer_.num_powered_changed()); 196 } 197 198 TEST_F(BluetoothAdapterWinTest, AdapterInitialized) { 199 EXPECT_FALSE(adapter_win_->IsInitialized()); 200 EXPECT_FALSE(init_callback_called_); 201 BluetoothTaskManagerWin::AdapterState state; 202 adapter_win_->AdapterStateChanged(state); 203 EXPECT_TRUE(adapter_win_->IsInitialized()); 204 EXPECT_TRUE(init_callback_called_); 205 } 206 207 TEST_F(BluetoothAdapterWinTest, SingleStartDiscovery) { 208 bluetooth_task_runner_->ClearPendingTasks(); 209 CallAddDiscoverySession( 210 base::Bind(&BluetoothAdapterWinTest::IncrementNumStartDiscoveryCallbacks, 211 base::Unretained(this)), 212 BluetoothAdapter::ErrorCallback()); 213 EXPECT_TRUE(ui_task_runner_->GetPendingTasks().empty()); 214 EXPECT_EQ(1, bluetooth_task_runner_->GetPendingTasks().size()); 215 EXPECT_FALSE(adapter_->IsDiscovering()); 216 EXPECT_EQ(0, num_start_discovery_callbacks_); 217 adapter_win_->DiscoveryStarted(true); 218 ui_task_runner_->RunPendingTasks(); 219 EXPECT_TRUE(adapter_->IsDiscovering()); 220 EXPECT_EQ(1, num_start_discovery_callbacks_); 221 EXPECT_EQ(1, adapter_observer_.num_discovering_changed()); 222 } 223 224 TEST_F(BluetoothAdapterWinTest, SingleStartDiscoveryFailure) { 225 CallAddDiscoverySession( 226 base::Closure(), 227 base::Bind( 228 &BluetoothAdapterWinTest::IncrementNumStartDiscoveryErrorCallbacks, 229 base::Unretained(this))); 230 adapter_win_->DiscoveryStarted(false); 231 ui_task_runner_->RunPendingTasks(); 232 EXPECT_FALSE(adapter_->IsDiscovering()); 233 EXPECT_EQ(1, num_start_discovery_error_callbacks_); 234 EXPECT_EQ(0, adapter_observer_.num_discovering_changed()); 235 } 236 237 TEST_F(BluetoothAdapterWinTest, MultipleStartDiscoveries) { 238 bluetooth_task_runner_->ClearPendingTasks(); 239 int num_discoveries = 5; 240 for (int i = 0; i < num_discoveries; i++) { 241 CallAddDiscoverySession( 242 base::Bind( 243 &BluetoothAdapterWinTest::IncrementNumStartDiscoveryCallbacks, 244 base::Unretained(this)), 245 BluetoothAdapter::ErrorCallback()); 246 EXPECT_EQ(1, bluetooth_task_runner_->GetPendingTasks().size()); 247 } 248 EXPECT_TRUE(ui_task_runner_->GetPendingTasks().empty()); 249 EXPECT_FALSE(adapter_->IsDiscovering()); 250 EXPECT_EQ(0, num_start_discovery_callbacks_); 251 adapter_win_->DiscoveryStarted(true); 252 ui_task_runner_->RunPendingTasks(); 253 EXPECT_TRUE(adapter_->IsDiscovering()); 254 EXPECT_EQ(num_discoveries, num_start_discovery_callbacks_); 255 EXPECT_EQ(1, adapter_observer_.num_discovering_changed()); 256 } 257 258 TEST_F(BluetoothAdapterWinTest, MultipleStartDiscoveriesFailure) { 259 int num_discoveries = 5; 260 for (int i = 0; i < num_discoveries; i++) { 261 CallAddDiscoverySession( 262 base::Closure(), 263 base::Bind( 264 &BluetoothAdapterWinTest::IncrementNumStartDiscoveryErrorCallbacks, 265 base::Unretained(this))); 266 } 267 adapter_win_->DiscoveryStarted(false); 268 ui_task_runner_->RunPendingTasks(); 269 EXPECT_FALSE(adapter_->IsDiscovering()); 270 EXPECT_EQ(num_discoveries, num_start_discovery_error_callbacks_); 271 EXPECT_EQ(0, adapter_observer_.num_discovering_changed()); 272 } 273 274 TEST_F(BluetoothAdapterWinTest, MultipleStartDiscoveriesAfterDiscovering) { 275 CallAddDiscoverySession( 276 base::Bind(&BluetoothAdapterWinTest::IncrementNumStartDiscoveryCallbacks, 277 base::Unretained(this)), 278 BluetoothAdapter::ErrorCallback()); 279 adapter_win_->DiscoveryStarted(true); 280 ui_task_runner_->RunPendingTasks(); 281 EXPECT_TRUE(adapter_->IsDiscovering()); 282 EXPECT_EQ(1, num_start_discovery_callbacks_); 283 284 bluetooth_task_runner_->ClearPendingTasks(); 285 for (int i = 0; i < 5; i++) { 286 int num_start_discovery_callbacks = num_start_discovery_callbacks_; 287 CallAddDiscoverySession( 288 base::Bind( 289 &BluetoothAdapterWinTest::IncrementNumStartDiscoveryCallbacks, 290 base::Unretained(this)), 291 BluetoothAdapter::ErrorCallback()); 292 EXPECT_TRUE(adapter_->IsDiscovering()); 293 EXPECT_TRUE(bluetooth_task_runner_->GetPendingTasks().empty()); 294 EXPECT_TRUE(ui_task_runner_->GetPendingTasks().empty()); 295 EXPECT_EQ(num_start_discovery_callbacks + 1, 296 num_start_discovery_callbacks_); 297 } 298 EXPECT_EQ(1, adapter_observer_.num_discovering_changed()); 299 } 300 301 TEST_F(BluetoothAdapterWinTest, StartDiscoveryAfterDiscoveringFailure) { 302 CallAddDiscoverySession( 303 base::Closure(), 304 base::Bind( 305 &BluetoothAdapterWinTest::IncrementNumStartDiscoveryErrorCallbacks, 306 base::Unretained(this))); 307 adapter_win_->DiscoveryStarted(false); 308 ui_task_runner_->RunPendingTasks(); 309 EXPECT_FALSE(adapter_->IsDiscovering()); 310 EXPECT_EQ(1, num_start_discovery_error_callbacks_); 311 312 CallAddDiscoverySession( 313 base::Bind(&BluetoothAdapterWinTest::IncrementNumStartDiscoveryCallbacks, 314 base::Unretained(this)), 315 BluetoothAdapter::ErrorCallback()); 316 adapter_win_->DiscoveryStarted(true); 317 ui_task_runner_->RunPendingTasks(); 318 EXPECT_TRUE(adapter_->IsDiscovering()); 319 EXPECT_EQ(1, num_start_discovery_callbacks_); 320 } 321 322 TEST_F(BluetoothAdapterWinTest, SingleStopDiscovery) { 323 CallAddDiscoverySession( 324 base::Closure(), BluetoothAdapter::ErrorCallback()); 325 adapter_win_->DiscoveryStarted(true); 326 ui_task_runner_->ClearPendingTasks(); 327 CallRemoveDiscoverySession( 328 base::Bind(&BluetoothAdapterWinTest::IncrementNumStopDiscoveryCallbacks, 329 base::Unretained(this)), 330 BluetoothAdapter::ErrorCallback()); 331 EXPECT_TRUE(adapter_->IsDiscovering()); 332 EXPECT_EQ(0, num_stop_discovery_callbacks_); 333 bluetooth_task_runner_->ClearPendingTasks(); 334 adapter_win_->DiscoveryStopped(); 335 ui_task_runner_->RunPendingTasks(); 336 EXPECT_FALSE(adapter_->IsDiscovering()); 337 EXPECT_EQ(1, num_stop_discovery_callbacks_); 338 EXPECT_TRUE(bluetooth_task_runner_->GetPendingTasks().empty()); 339 EXPECT_EQ(2, adapter_observer_.num_discovering_changed()); 340 } 341 342 TEST_F(BluetoothAdapterWinTest, MultipleStopDiscoveries) { 343 int num_discoveries = 5; 344 for (int i = 0; i < num_discoveries; i++) { 345 CallAddDiscoverySession( 346 base::Closure(), BluetoothAdapter::ErrorCallback()); 347 } 348 adapter_win_->DiscoveryStarted(true); 349 ui_task_runner_->ClearPendingTasks(); 350 bluetooth_task_runner_->ClearPendingTasks(); 351 for (int i = 0; i < num_discoveries - 1; i++) { 352 CallRemoveDiscoverySession( 353 base::Bind(&BluetoothAdapterWinTest::IncrementNumStopDiscoveryCallbacks, 354 base::Unretained(this)), 355 BluetoothAdapter::ErrorCallback()); 356 EXPECT_TRUE(bluetooth_task_runner_->GetPendingTasks().empty()); 357 ui_task_runner_->RunPendingTasks(); 358 EXPECT_EQ(i + 1, num_stop_discovery_callbacks_); 359 } 360 CallRemoveDiscoverySession( 361 base::Bind(&BluetoothAdapterWinTest::IncrementNumStopDiscoveryCallbacks, 362 base::Unretained(this)), 363 BluetoothAdapter::ErrorCallback()); 364 EXPECT_EQ(1, bluetooth_task_runner_->GetPendingTasks().size()); 365 EXPECT_TRUE(adapter_->IsDiscovering()); 366 adapter_win_->DiscoveryStopped(); 367 ui_task_runner_->RunPendingTasks(); 368 EXPECT_FALSE(adapter_->IsDiscovering()); 369 EXPECT_EQ(num_discoveries, num_stop_discovery_callbacks_); 370 EXPECT_EQ(2, adapter_observer_.num_discovering_changed()); 371 } 372 373 TEST_F(BluetoothAdapterWinTest, 374 StartDiscoveryAndStartDiscoveryAndStopDiscoveries) { 375 CallAddDiscoverySession( 376 base::Bind(&BluetoothAdapterWinTest::IncrementNumStartDiscoveryCallbacks, 377 base::Unretained(this)), 378 BluetoothAdapter::ErrorCallback()); 379 adapter_win_->DiscoveryStarted(true); 380 CallAddDiscoverySession( 381 base::Bind(&BluetoothAdapterWinTest::IncrementNumStartDiscoveryCallbacks, 382 base::Unretained(this)), 383 BluetoothAdapter::ErrorCallback()); 384 ui_task_runner_->ClearPendingTasks(); 385 bluetooth_task_runner_->ClearPendingTasks(); 386 CallRemoveDiscoverySession( 387 base::Bind(&BluetoothAdapterWinTest::IncrementNumStopDiscoveryCallbacks, 388 base::Unretained(this)), 389 BluetoothAdapter::ErrorCallback()); 390 EXPECT_TRUE(bluetooth_task_runner_->GetPendingTasks().empty()); 391 CallRemoveDiscoverySession( 392 base::Bind(&BluetoothAdapterWinTest::IncrementNumStopDiscoveryCallbacks, 393 base::Unretained(this)), 394 BluetoothAdapter::ErrorCallback()); 395 EXPECT_EQ(1, bluetooth_task_runner_->GetPendingTasks().size()); 396 } 397 398 TEST_F(BluetoothAdapterWinTest, 399 StartDiscoveryAndStopDiscoveryAndStartDiscovery) { 400 CallAddDiscoverySession( 401 base::Closure(), BluetoothAdapter::ErrorCallback()); 402 adapter_win_->DiscoveryStarted(true); 403 EXPECT_TRUE(adapter_->IsDiscovering()); 404 CallRemoveDiscoverySession( 405 base::Closure(), BluetoothAdapter::ErrorCallback()); 406 adapter_win_->DiscoveryStopped(); 407 EXPECT_FALSE(adapter_->IsDiscovering()); 408 CallAddDiscoverySession( 409 base::Closure(), BluetoothAdapter::ErrorCallback()); 410 adapter_win_->DiscoveryStarted(true); 411 EXPECT_TRUE(adapter_->IsDiscovering()); 412 } 413 414 TEST_F(BluetoothAdapterWinTest, StartDiscoveryBeforeDiscoveryStopped) { 415 CallAddDiscoverySession( 416 base::Closure(), BluetoothAdapter::ErrorCallback()); 417 adapter_win_->DiscoveryStarted(true); 418 CallRemoveDiscoverySession( 419 base::Closure(), BluetoothAdapter::ErrorCallback()); 420 CallAddDiscoverySession( 421 base::Closure(), BluetoothAdapter::ErrorCallback()); 422 bluetooth_task_runner_->ClearPendingTasks(); 423 adapter_win_->DiscoveryStopped(); 424 EXPECT_EQ(1, bluetooth_task_runner_->GetPendingTasks().size()); 425 } 426 427 TEST_F(BluetoothAdapterWinTest, StopDiscoveryWithoutStartDiscovery) { 428 CallRemoveDiscoverySession( 429 base::Closure(), 430 base::Bind( 431 &BluetoothAdapterWinTest::IncrementNumStopDiscoveryErrorCallbacks, 432 base::Unretained(this))); 433 EXPECT_EQ(1, num_stop_discovery_error_callbacks_); 434 } 435 436 TEST_F(BluetoothAdapterWinTest, StopDiscoveryBeforeDiscoveryStarted) { 437 CallAddDiscoverySession( 438 base::Closure(), BluetoothAdapter::ErrorCallback()); 439 CallRemoveDiscoverySession( 440 base::Closure(), BluetoothAdapter::ErrorCallback()); 441 bluetooth_task_runner_->ClearPendingTasks(); 442 adapter_win_->DiscoveryStarted(true); 443 EXPECT_EQ(1, bluetooth_task_runner_->GetPendingTasks().size()); 444 } 445 446 TEST_F(BluetoothAdapterWinTest, StartAndStopBeforeDiscoveryStarted) { 447 int num_expected_start_discoveries = 3; 448 int num_expected_stop_discoveries = 2; 449 for (int i = 0; i < num_expected_start_discoveries; i++) { 450 CallAddDiscoverySession( 451 base::Bind( 452 &BluetoothAdapterWinTest::IncrementNumStartDiscoveryCallbacks, 453 base::Unretained(this)), 454 BluetoothAdapter::ErrorCallback()); 455 } 456 for (int i = 0; i < num_expected_stop_discoveries; i++) { 457 CallRemoveDiscoverySession( 458 base::Bind( 459 &BluetoothAdapterWinTest::IncrementNumStopDiscoveryCallbacks, 460 base::Unretained(this)), 461 BluetoothAdapter::ErrorCallback()); 462 } 463 bluetooth_task_runner_->ClearPendingTasks(); 464 adapter_win_->DiscoveryStarted(true); 465 EXPECT_TRUE(bluetooth_task_runner_->GetPendingTasks().empty()); 466 ui_task_runner_->RunPendingTasks(); 467 EXPECT_EQ(num_expected_start_discoveries, num_start_discovery_callbacks_); 468 EXPECT_EQ(num_expected_stop_discoveries, num_stop_discovery_callbacks_); 469 } 470 471 TEST_F(BluetoothAdapterWinTest, StopDiscoveryBeforeDiscoveryStartedAndFailed) { 472 CallAddDiscoverySession( 473 base::Closure(), 474 base::Bind( 475 &BluetoothAdapterWinTest::IncrementNumStartDiscoveryErrorCallbacks, 476 base::Unretained(this))); 477 CallRemoveDiscoverySession( 478 base::Bind( 479 &BluetoothAdapterWinTest::IncrementNumStopDiscoveryCallbacks, 480 base::Unretained(this)), 481 BluetoothAdapter::ErrorCallback()); 482 ui_task_runner_->ClearPendingTasks(); 483 adapter_win_->DiscoveryStarted(false); 484 ui_task_runner_->RunPendingTasks(); 485 EXPECT_EQ(1, num_start_discovery_error_callbacks_); 486 EXPECT_EQ(1, num_stop_discovery_callbacks_); 487 EXPECT_EQ(0, adapter_observer_.num_discovering_changed()); 488 } 489 490 TEST_F(BluetoothAdapterWinTest, DevicesDiscovered) { 491 BluetoothTaskManagerWin::DeviceState* android_phone_state = 492 new BluetoothTaskManagerWin::DeviceState(); 493 MakeDeviceState("phone", "android phone address", android_phone_state); 494 BluetoothTaskManagerWin::DeviceState* laptop_state = 495 new BluetoothTaskManagerWin::DeviceState(); 496 MakeDeviceState("laptop", "laptop address", laptop_state); 497 BluetoothTaskManagerWin::DeviceState* iphone_state = 498 new BluetoothTaskManagerWin::DeviceState(); 499 MakeDeviceState("phone", "iphone address", iphone_state); 500 ScopedVector<BluetoothTaskManagerWin::DeviceState> devices; 501 devices.push_back(android_phone_state); 502 devices.push_back(laptop_state); 503 devices.push_back(iphone_state); 504 505 adapter_win_->DevicesDiscovered(devices); 506 EXPECT_EQ(3, adapter_observer_.num_device_added()); 507 } 508 509 } // namespace device 510