OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/memory/ref_counted.h" | 6 #include "base/memory/ref_counted.h" |
7 #include "base/run_loop.h" | 7 #include "base/run_loop.h" |
8 #include "device/bluetooth/bluetooth_adapter.h" | 8 #include "device/bluetooth/bluetooth_adapter.h" |
9 #include "device/bluetooth/bluetooth_device.h" | 9 #include "device/bluetooth/bluetooth_device.h" |
10 #include "device/bluetooth/bluetooth_discovery_session.h" | 10 #include "device/bluetooth/bluetooth_discovery_session.h" |
(...skipping 454 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
465 | 465 |
466 discovery_sessions_[0]->Stop(GetCallback(), GetErrorCallback()); | 466 discovery_sessions_[0]->Stop(GetCallback(), GetErrorCallback()); |
467 base::RunLoop().RunUntilIdle(); | 467 base::RunLoop().RunUntilIdle(); |
468 EXPECT_EQ(1, callback_count_--); | 468 EXPECT_EQ(1, callback_count_--); |
469 EXPECT_EQ(0, error_callback_count_); | 469 EXPECT_EQ(0, error_callback_count_); |
470 EXPECT_FALSE(adapter_->IsDiscovering()); | 470 EXPECT_FALSE(adapter_->IsDiscovering()); |
471 EXPECT_FALSE(discovery_sessions_[0]->IsActive()); | 471 EXPECT_FALSE(discovery_sessions_[0]->IsActive()); |
472 } | 472 } |
473 #endif // defined(OS_ANDROID) | 473 #endif // defined(OS_ANDROID) |
474 | 474 |
475 #if defined(OS_ANDROID) | 475 #if defined(OS_ANDROID) || defined(OS_MACOSX) |
476 // Discovers a device. | 476 // Discovers a device. |
477 TEST_F(BluetoothTest, DiscoverDevice) { | 477 TEST_F(BluetoothTest, DiscoverLowEnergyDevice) { |
| 478 if (!PlatformSupportsLowEnergy()) { |
| 479 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
| 480 return; |
| 481 } |
478 InitWithFakeAdapter(); | 482 InitWithFakeAdapter(); |
479 TestBluetoothAdapterObserver observer(adapter_); | 483 TestBluetoothAdapterObserver observer(adapter_); |
480 | 484 |
481 // Start discovery and find a device. | 485 // Start discovery and find a device. |
482 adapter_->StartDiscoverySession(GetDiscoverySessionCallback(), | 486 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter( |
483 GetErrorCallback()); | 487 new BluetoothDiscoveryFilter( |
| 488 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE)); |
| 489 adapter_->StartDiscoverySessionWithFilter(discovery_filter.Pass(), |
| 490 GetDiscoverySessionCallback(), |
| 491 GetErrorCallback()); |
484 base::RunLoop().RunUntilIdle(); | 492 base::RunLoop().RunUntilIdle(); |
485 DiscoverLowEnergyDevice(1); | 493 DiscoverLowEnergyDevice(1); |
486 base::RunLoop().RunUntilIdle(); | 494 base::RunLoop().RunUntilIdle(); |
487 EXPECT_EQ(1, observer.device_added_count()); | 495 EXPECT_EQ(1, observer.device_added_count()); |
488 BluetoothDevice* device = adapter_->GetDevice(observer.last_device_address()); | 496 BluetoothDevice* device = adapter_->GetDevice(observer.last_device_address()); |
489 EXPECT_TRUE(device); | 497 EXPECT_TRUE(device); |
490 } | 498 } |
491 #endif // defined(OS_ANDROID) | 499 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) |
492 | 500 |
493 #if defined(OS_ANDROID) | 501 #if defined(OS_ANDROID) || defined(OS_MACOSX) |
494 // Discovers the same device multiple times. | 502 // Discovers the same device multiple times. |
495 TEST_F(BluetoothTest, DiscoverDeviceTwice) { | 503 TEST_F(BluetoothTest, DiscoverLowEnergyDeviceTwice) { |
| 504 if (!PlatformSupportsLowEnergy()) { |
| 505 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
| 506 return; |
| 507 } |
496 InitWithFakeAdapter(); | 508 InitWithFakeAdapter(); |
497 TestBluetoothAdapterObserver observer(adapter_); | 509 TestBluetoothAdapterObserver observer(adapter_); |
498 | 510 |
499 // Start discovery and find a device. | 511 // Start discovery and find a device. |
500 adapter_->StartDiscoverySession(GetDiscoverySessionCallback(), | 512 adapter_->StartDiscoverySession(GetDiscoverySessionCallback(), |
501 GetErrorCallback()); | 513 GetErrorCallback()); |
502 base::RunLoop().RunUntilIdle(); | 514 base::RunLoop().RunUntilIdle(); |
503 DiscoverLowEnergyDevice(1); | 515 DiscoverLowEnergyDevice(1); |
504 base::RunLoop().RunUntilIdle(); | 516 base::RunLoop().RunUntilIdle(); |
505 EXPECT_EQ(1, observer.device_added_count()); | 517 EXPECT_EQ(1, observer.device_added_count()); |
506 BluetoothDevice* device = adapter_->GetDevice(observer.last_device_address()); | 518 BluetoothDevice* device = adapter_->GetDevice(observer.last_device_address()); |
507 EXPECT_TRUE(device); | 519 EXPECT_TRUE(device); |
508 | 520 |
509 // Find the same device again. This should not create a new device object. | 521 // Find the same device again. This should not create a new device object. |
510 observer.Reset(); | 522 observer.Reset(); |
511 DiscoverLowEnergyDevice(1); | 523 DiscoverLowEnergyDevice(1); |
512 base::RunLoop().RunUntilIdle(); | 524 base::RunLoop().RunUntilIdle(); |
513 EXPECT_EQ(0, observer.device_added_count()); | 525 EXPECT_EQ(0, observer.device_added_count()); |
514 EXPECT_EQ(0, observer.device_changed_count()); | |
515 EXPECT_EQ(1u, adapter_->GetDevices().size()); | 526 EXPECT_EQ(1u, adapter_->GetDevices().size()); |
516 } | 527 } |
517 #endif // defined(OS_ANDROID) | 528 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) |
518 | 529 |
519 #if defined(OS_ANDROID) | 530 #if defined(OS_ANDROID) || defined(OS_MACOSX) |
520 // Discovers a device, and then again with new Service UUIDs. | 531 // Discovers a device, and then again with new Service UUIDs. |
521 TEST_F(BluetoothTest, DiscoverDeviceWithUpdatedUUIDs) { | 532 TEST_F(BluetoothTest, DiscoverLowEnergyDeviceWithUpdatedUUIDs) { |
| 533 if (!PlatformSupportsLowEnergy()) { |
| 534 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
| 535 return; |
| 536 } |
522 InitWithFakeAdapter(); | 537 InitWithFakeAdapter(); |
523 TestBluetoothAdapterObserver observer(adapter_); | 538 TestBluetoothAdapterObserver observer(adapter_); |
524 | 539 |
525 // Start discovery and find a device. | 540 // Start discovery and find a device. |
526 adapter_->StartDiscoverySession(GetDiscoverySessionCallback(), | 541 adapter_->StartDiscoverySession(GetDiscoverySessionCallback(), |
527 GetErrorCallback()); | 542 GetErrorCallback()); |
528 base::RunLoop().RunUntilIdle(); | 543 base::RunLoop().RunUntilIdle(); |
529 DiscoverLowEnergyDevice(1); | 544 DiscoverLowEnergyDevice(1); |
530 base::RunLoop().RunUntilIdle(); | 545 base::RunLoop().RunUntilIdle(); |
531 BluetoothDevice* device = observer.last_device(); | 546 BluetoothDevice* device = observer.last_device(); |
532 | 547 |
533 // Check the initial UUIDs: | 548 // Check the initial UUIDs: |
534 EXPECT_TRUE(ContainsValue(device->GetUUIDs(), BluetoothUUID("1800"))); | 549 EXPECT_TRUE( |
535 EXPECT_FALSE(ContainsValue(device->GetUUIDs(), BluetoothUUID("1802"))); | 550 ContainsValue(device->GetUUIDs(), BluetoothUUID(kTestUUIDGenericAccess))); |
| 551 EXPECT_FALSE(ContainsValue(device->GetUUIDs(), |
| 552 BluetoothUUID(kTestUUIDImmediateAlert))); |
536 | 553 |
537 // Discover same device again with updated UUIDs: | 554 // Discover same device again with updated UUIDs: |
538 observer.Reset(); | 555 observer.Reset(); |
539 DiscoverLowEnergyDevice(2); | 556 DiscoverLowEnergyDevice(2); |
540 base::RunLoop().RunUntilIdle(); | 557 base::RunLoop().RunUntilIdle(); |
541 EXPECT_EQ(0, observer.device_added_count()); | 558 EXPECT_EQ(0, observer.device_added_count()); |
542 EXPECT_EQ(1, observer.device_changed_count()); | 559 EXPECT_EQ(1, observer.device_changed_count()); |
543 EXPECT_EQ(1u, adapter_->GetDevices().size()); | 560 EXPECT_EQ(1u, adapter_->GetDevices().size()); |
544 EXPECT_EQ(device, observer.last_device()); | 561 EXPECT_EQ(device, observer.last_device()); |
545 | 562 |
546 // Expect new UUIDs: | 563 // Expect new UUIDs: |
547 EXPECT_FALSE(ContainsValue(device->GetUUIDs(), BluetoothUUID("1800"))); | 564 EXPECT_FALSE( |
548 EXPECT_TRUE(ContainsValue(device->GetUUIDs(), BluetoothUUID("1802"))); | 565 ContainsValue(device->GetUUIDs(), BluetoothUUID(kTestUUIDGenericAccess))); |
| 566 EXPECT_TRUE(ContainsValue(device->GetUUIDs(), |
| 567 BluetoothUUID(kTestUUIDImmediateAlert))); |
549 | 568 |
550 // Discover same device again with empty UUIDs: | 569 // Discover same device again with empty UUIDs: |
551 observer.Reset(); | 570 observer.Reset(); |
552 DiscoverLowEnergyDevice(3); | 571 DiscoverLowEnergyDevice(3); |
553 base::RunLoop().RunUntilIdle(); | 572 base::RunLoop().RunUntilIdle(); |
554 EXPECT_EQ(0, observer.device_added_count()); | 573 EXPECT_EQ(0, observer.device_added_count()); |
555 EXPECT_EQ(1, observer.device_changed_count()); | 574 EXPECT_EQ(1, observer.device_changed_count()); |
556 EXPECT_EQ(1u, adapter_->GetDevices().size()); | 575 EXPECT_EQ(1u, adapter_->GetDevices().size()); |
557 EXPECT_EQ(device, observer.last_device()); | 576 EXPECT_EQ(device, observer.last_device()); |
558 | 577 |
559 // Expect empty UUIDs: | 578 // Expect empty UUIDs: |
560 EXPECT_EQ(0u, device->GetUUIDs().size()); | 579 EXPECT_EQ(0u, device->GetUUIDs().size()); |
561 } | 580 } |
562 #endif // defined(OS_ANDROID) | 581 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) |
563 | 582 |
564 #if defined(OS_ANDROID) | 583 #if defined(OS_ANDROID) || defined(OS_MACOSX) |
565 // Discovers multiple devices when addresses vary. | 584 // Discovers multiple devices when addresses vary. |
566 TEST_F(BluetoothTest, DiscoverMultipleDevices) { | 585 TEST_F(BluetoothTest, DiscoverMultipleLowEnergyDevices) { |
| 586 if (!PlatformSupportsLowEnergy()) { |
| 587 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
| 588 return; |
| 589 } |
567 InitWithFakeAdapter(); | 590 InitWithFakeAdapter(); |
568 TestBluetoothAdapterObserver observer(adapter_); | 591 TestBluetoothAdapterObserver observer(adapter_); |
569 | 592 |
570 // Start discovery and find a device. | 593 // Start discovery and find a device. |
571 adapter_->StartDiscoverySession(GetDiscoverySessionCallback(), | 594 adapter_->StartDiscoverySession(GetDiscoverySessionCallback(), |
572 GetErrorCallback()); | 595 GetErrorCallback()); |
573 base::RunLoop().RunUntilIdle(); | 596 base::RunLoop().RunUntilIdle(); |
574 DiscoverLowEnergyDevice(1); | 597 DiscoverLowEnergyDevice(1); |
575 DiscoverLowEnergyDevice(4); | 598 DiscoverLowEnergyDevice(4); |
576 base::RunLoop().RunUntilIdle(); | 599 base::RunLoop().RunUntilIdle(); |
577 EXPECT_EQ(2, observer.device_added_count()); | 600 EXPECT_EQ(2, observer.device_added_count()); |
578 EXPECT_EQ(2u, adapter_->GetDevices().size()); | 601 EXPECT_EQ(2u, adapter_->GetDevices().size()); |
579 } | 602 } |
580 #endif // defined(OS_ANDROID) | 603 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) |
581 | 604 |
582 } // namespace device | 605 } // namespace device |
OLD | NEW |