Chromium Code Reviews| Index: device/bluetooth/bluetooth_adapter_mac_unittest.mm |
| diff --git a/device/bluetooth/bluetooth_adapter_mac_unittest.mm b/device/bluetooth/bluetooth_adapter_mac_unittest.mm |
| index 200f0ea34d9f0de14d3f825330b21b93e5b43f8f..22b6ce814729fd92bf771ca51b3ca5b445b0564a 100644 |
| --- a/device/bluetooth/bluetooth_adapter_mac_unittest.mm |
| +++ b/device/bluetooth/bluetooth_adapter_mac_unittest.mm |
| @@ -8,6 +8,7 @@ |
| #include "base/bind.h" |
| #include "base/memory/ref_counted.h" |
| +#include "base/strings/sys_string_conversions.h" |
| #include "base/test/test_simple_task_runner.h" |
| #include "build/build_config.h" |
| #include "device/bluetooth/bluetooth_adapter.h" |
| @@ -31,6 +32,12 @@ |
| namespace { |
| // |kTestHashAddress| is the hash corresponding to identifier |kTestNSUUID|. |
| const char* const kTestNSUUID = "00000000-1111-2222-3333-444444444444"; |
| +NSString* const kTestNSStringUUIDHeartRate = |
| + @"0000180d-0000-1000-8000-00805f9b34fb"; |
| +NSString* const kTestNSStringUUIDGlucose = |
| + @"00001808-0000-1000-8000-00805f9b34fb"; |
| +const char* kTestStringUUIDHeartRate = "0000180d-0000-1000-8000-00805f9b34fb"; |
|
Jeffrey Yasskin
2016/08/23 21:37:04
Use either "const char* const kTestString" or "con
|
| +const char* kTestStringUUIDGlucose = "00001808-0000-1000-8000-00805f9b34fb"; |
| const std::string kTestHashAddress = "D1:6F:E3:22:FD:5B"; |
| const int kTestRssi = 0; |
| } // namespace |
| @@ -127,6 +134,12 @@ class BluetoothAdapterMacTest : public testing::Test { |
| int NumDiscoverySessions() { return adapter_mac_->num_discovery_sessions_; } |
| + void ResetEventCounts() { |
| + callback_count_ = 0; |
| + error_callback_count_ = 0; |
| + [mock_central_manager_ resetEventCounts]; |
| + } |
| + |
| // Generic callbacks. |
| void Callback() { ++callback_count_; } |
| void ErrorCallback() { ++error_callback_count_; } |
| @@ -151,7 +164,7 @@ TEST_F(BluetoothAdapterMacTest, Poll) { |
| EXPECT_FALSE(ui_task_runner_->GetPendingTasks().empty()); |
| } |
| -TEST_F(BluetoothAdapterMacTest, AddDiscoverySessionWithLowEnergyFilter) { |
| +TEST_F(BluetoothAdapterMacTest, AddDiscoverySession_LowEnergyFilter) { |
| if (!SetMockCentralManager(CBCentralManagerStatePoweredOn)) |
| return; |
| EXPECT_EQ(0, [mock_central_manager_ scanForPeripheralsCallCount]); |
| @@ -159,59 +172,222 @@ TEST_F(BluetoothAdapterMacTest, AddDiscoverySessionWithLowEnergyFilter) { |
| std::unique_ptr<BluetoothDiscoveryFilter> discovery_filter( |
| new BluetoothDiscoveryFilter(BLUETOOTH_TRANSPORT_LE)); |
| + discovery_filter->AddUUID(BluetoothUUID(kTestStringUUIDHeartRate)); |
| + discovery_filter->AddUUID(BluetoothUUID(kTestStringUUIDGlucose)); |
| AddDiscoverySession(discovery_filter.get()); |
| EXPECT_EQ(1, callback_count_); |
| EXPECT_EQ(0, error_callback_count_); |
| EXPECT_EQ(1, NumDiscoverySessions()); |
| + NSSet* expected_uuids = [NSSet setWithArray:@[ |
| + [CBUUID UUIDWithString:kTestNSStringUUIDHeartRate], |
| + [CBUUID UUIDWithString:kTestNSStringUUIDGlucose] |
| + ]]; |
| + NSSet* actual_uuids = [NSSet setWithArray:[mock_central_manager_ scan_uuids]]; |
| + |
| + EXPECT_TRUE([actual_uuids isEqualToSet:expected_uuids]); |
| + |
| // Check that adding a discovery session resulted in |
| // scanForPeripheralsWithServices being called on the Central Manager. |
| EXPECT_EQ(1, [mock_central_manager_ scanForPeripheralsCallCount]); |
| } |
| -// TODO(krstnmnlsn): Test changing the filter when adding the second discovery |
| -// session (once we have that ability). |
| -TEST_F(BluetoothAdapterMacTest, AddSecondDiscoverySessionWithLowEnergyFilter) { |
| +TEST_F(BluetoothAdapterMacTest, AddDiscoverySession_TwiceWithNewUUIDs) { |
| + if (!SetMockCentralManager(CBCentralManagerStatePoweredOn)) |
| + return; |
| + std::unique_ptr<BluetoothDiscoveryFilter> discovery_filter1( |
| + new BluetoothDiscoveryFilter(BLUETOOTH_TRANSPORT_LE)); |
| + discovery_filter1->AddUUID(BluetoothUUID(kTestStringUUIDHeartRate)); |
| + AddDiscoverySession(discovery_filter1.get()); |
| + EXPECT_EQ(1, callback_count_); |
| + EXPECT_EQ(0, error_callback_count_); |
| + EXPECT_EQ(1, NumDiscoverySessions()); |
| + NSSet* expected_uuids1 = [NSSet |
| + setWithArray:@[ [CBUUID UUIDWithString:kTestNSStringUUIDHeartRate] ]]; |
| + NSSet* actual_uuids1 = |
| + [NSSet setWithArray:[mock_central_manager_ scan_uuids]]; |
| + EXPECT_TRUE([actual_uuids1 isEqualToSet:expected_uuids1]); |
| + |
| + // We replaced the success callback handed to AddDiscoverySession, so |
| + // |adapter_mac_| should remain in a discovering state indefinitely. |
| + EXPECT_TRUE(adapter_mac_->IsDiscovering()); |
| + |
| + // Add another session with different UUIDs. |
| + ResetEventCounts(); |
| + |
| + std::unique_ptr<BluetoothDiscoveryFilter> discovery_filter2( |
| + new BluetoothDiscoveryFilter(BLUETOOTH_TRANSPORT_LE)); |
| + discovery_filter2->AddUUID(BluetoothUUID(kTestStringUUIDGlucose)); |
| + AddDiscoverySession(discovery_filter2.get()); |
| + // Since the new filter had new UUIDs we update the scan. |
| + EXPECT_EQ(1, [mock_central_manager_ scanForPeripheralsCallCount]); |
| + |
| + NSSet* expected_uuids2 = [NSSet setWithArray:@[ |
| + [CBUUID UUIDWithString:kTestNSStringUUIDHeartRate], |
| + [CBUUID UUIDWithString:kTestNSStringUUIDGlucose] |
| + ]]; |
| + NSSet* actual_uuids2 = |
| + [NSSet setWithArray:[mock_central_manager_ scan_uuids]]; |
| + |
| + EXPECT_TRUE([actual_uuids2 isEqualToSet:expected_uuids2]); |
| + |
| + EXPECT_EQ(1, callback_count_); |
| + EXPECT_EQ(0, error_callback_count_); |
| + EXPECT_EQ(2, NumDiscoverySessions()); |
| +} |
| + |
| +TEST_F(BluetoothAdapterMacTest, AddDiscoverySession_TwiceWithSameUUIDs) { |
| if (!SetMockCentralManager(CBCentralManagerStatePoweredOn)) |
| return; |
| std::unique_ptr<BluetoothDiscoveryFilter> discovery_filter( |
| new BluetoothDiscoveryFilter(BLUETOOTH_TRANSPORT_LE)); |
| + discovery_filter->AddUUID(BluetoothUUID(kTestStringUUIDHeartRate)); |
| AddDiscoverySession(discovery_filter.get()); |
| EXPECT_EQ(1, callback_count_); |
| EXPECT_EQ(0, error_callback_count_); |
| EXPECT_EQ(1, NumDiscoverySessions()); |
| + NSSet* expected_uuids = [NSSet |
| + setWithArray:@[ [CBUUID UUIDWithString:kTestNSStringUUIDHeartRate] ]]; |
| + NSSet* actual_uuids = [NSSet setWithArray:[mock_central_manager_ scan_uuids]]; |
| + EXPECT_TRUE([actual_uuids isEqualToSet:expected_uuids]); |
| // We replaced the success callback handed to AddDiscoverySession, so |
| // |adapter_mac_| should remain in a discovering state indefinitely. |
| EXPECT_TRUE(adapter_mac_->IsDiscovering()); |
| + // Add another session with the same UUIDs. |
| + ResetEventCounts(); |
| + |
| AddDiscoverySession(discovery_filter.get()); |
| + // Since the new filter had no new UUIDs no need to update the scan. |
| + EXPECT_EQ(0, [mock_central_manager_ scanForPeripheralsCallCount]); |
| + actual_uuids = [NSSet setWithArray:[mock_central_manager_ scan_uuids]]; |
| + EXPECT_TRUE([actual_uuids isEqualToSet:expected_uuids]); |
| + |
| + EXPECT_EQ(1, callback_count_); |
| + EXPECT_EQ(0, error_callback_count_); |
| + EXPECT_EQ(2, NumDiscoverySessions()); |
| +} |
| + |
| +TEST_F(BluetoothAdapterMacTest, RemoveDiscoverySession_WithLowEnergyFilter) { |
| + if (!SetMockCentralManager(CBCentralManagerStatePoweredOn)) |
| + return; |
| + EXPECT_EQ(0, [mock_central_manager_ scanForPeripheralsCallCount]); |
| + |
| + std::unique_ptr<BluetoothDiscoveryFilter> discovery_filter( |
| + new BluetoothDiscoveryFilter(BLUETOOTH_TRANSPORT_LE)); |
| + AddDiscoverySession(discovery_filter.get()); |
| + EXPECT_EQ(1, callback_count_); |
| + EXPECT_EQ(0, error_callback_count_); |
| + EXPECT_EQ(1, NumDiscoverySessions()); |
| + |
| + ResetEventCounts(); |
| + RemoveDiscoverySession(discovery_filter.get()); |
| + EXPECT_EQ(1, callback_count_); |
| + EXPECT_EQ(0, error_callback_count_); |
| + EXPECT_EQ(0, NumDiscoverySessions()); |
| + |
| + // Check that removing the discovery session resulted in stopScan being called |
| + // on the Central Manager. |
| + EXPECT_EQ(1, [mock_central_manager_ stopScanCallCount]); |
| +} |
| + |
| +TEST_F(BluetoothAdapterMacTest, RemoveDiscoverySession_ForDifferentUUID) { |
| + if (!SetMockCentralManager(CBCentralManagerStatePoweredOn)) |
| + return; |
| + // Create filters. |
| + std::unique_ptr<BluetoothDiscoveryFilter> discovery_filter1( |
| + new BluetoothDiscoveryFilter(BLUETOOTH_TRANSPORT_LE)); |
| + discovery_filter1->AddUUID(BluetoothUUID(kTestStringUUIDHeartRate)); |
| + std::unique_ptr<BluetoothDiscoveryFilter> discovery_filter2( |
| + new BluetoothDiscoveryFilter(BLUETOOTH_TRANSPORT_LE)); |
| + discovery_filter2->AddUUID(BluetoothUUID(kTestStringUUIDGlucose)); |
| + |
| + AddDiscoverySession(discovery_filter1.get()); |
| + AddDiscoverySession(discovery_filter2.get()); |
| + |
| EXPECT_EQ(2, [mock_central_manager_ scanForPeripheralsCallCount]); |
| + EXPECT_TRUE(adapter_mac_->IsDiscovering()); |
| EXPECT_EQ(2, callback_count_); |
| EXPECT_EQ(0, error_callback_count_); |
| EXPECT_EQ(2, NumDiscoverySessions()); |
| + |
| + NSSet* expected_uuids1 = [NSSet setWithArray:@[ |
| + [CBUUID UUIDWithString:kTestNSStringUUIDHeartRate], |
| + [CBUUID UUIDWithString:kTestNSStringUUIDGlucose] |
| + ]]; |
| + NSSet* actual_uuids1 = |
| + [NSSet setWithArray:[mock_central_manager_ scan_uuids]]; |
| + |
| + EXPECT_TRUE([actual_uuids1 isEqualToSet:expected_uuids1]); |
| + |
| + // Remove one discovery session. |
| + ResetEventCounts(); |
| + |
| + RemoveDiscoverySession(discovery_filter1.get()); |
| + EXPECT_EQ(1, callback_count_); |
| + EXPECT_EQ(0, error_callback_count_); |
| + EXPECT_EQ(1, NumDiscoverySessions()); |
| + EXPECT_EQ(0, [mock_central_manager_ stopScanCallCount]); |
| + EXPECT_EQ(1, [mock_central_manager_ scanForPeripheralsCallCount]); |
| + |
| + // Since we removed the session that scanned for kTestStringUUIDHeartRate, |
| + // we expect to only be scanning for kTestStringUUIDGlucose. |
| + NSSet* expected_uuids2 = [NSSet |
| + setWithArray:@[ [CBUUID UUIDWithString:kTestNSStringUUIDGlucose] ]]; |
| + NSSet* actual_uuids2 = |
| + [NSSet setWithArray:[mock_central_manager_ scan_uuids]]; |
| + |
| + EXPECT_TRUE([actual_uuids2 isEqualToSet:expected_uuids2]); |
| + |
| + // Remove last discovery session. |
| + ResetEventCounts(); |
| + |
| + RemoveDiscoverySession(discovery_filter2.get()); |
| + EXPECT_EQ(1, callback_count_); |
| + EXPECT_EQ(0, error_callback_count_); |
| + EXPECT_EQ(0, NumDiscoverySessions()); |
| + EXPECT_EQ(1, [mock_central_manager_ stopScanCallCount]); |
| + EXPECT_EQ(0, [mock_central_manager_ scanForPeripheralsCallCount]); |
| } |
| -TEST_F(BluetoothAdapterMacTest, RemoveDiscoverySessionWithLowEnergyFilter) { |
| +TEST_F(BluetoothAdapterMacTest, RemoveDiscoverySession_ForSameUUID) { |
| + // Tests that removing a filter that has a UUID that another filter has |
| + // does not change scan. |
| if (!SetMockCentralManager(CBCentralManagerStatePoweredOn)) |
| return; |
| EXPECT_EQ(0, [mock_central_manager_ scanForPeripheralsCallCount]); |
| std::unique_ptr<BluetoothDiscoveryFilter> discovery_filter( |
| new BluetoothDiscoveryFilter(BLUETOOTH_TRANSPORT_LE)); |
| + discovery_filter->AddUUID(BluetoothUUID(kTestStringUUIDHeartRate)); |
| AddDiscoverySession(discovery_filter.get()); |
| + // Add discovery session with same filters. |
| + AddDiscoverySession(discovery_filter.get()); |
| + EXPECT_EQ(2, callback_count_); |
| + EXPECT_EQ(0, error_callback_count_); |
| + EXPECT_EQ(2, NumDiscoverySessions()); |
| + |
| + // Remove one discovery sesssion. We should not have called stop |
|
Jeffrey Yasskin
2016/08/23 21:37:04
Is "have called" the right tense here? It looks li
Jeffrey Yasskin
2016/08/23 21:37:04
sp: sesssion
|
| + // because the other filter still scans for the same UUID. |
| + ResetEventCounts(); |
| + |
| + RemoveDiscoverySession(discovery_filter.get()); |
| EXPECT_EQ(1, callback_count_); |
| EXPECT_EQ(0, error_callback_count_); |
| EXPECT_EQ(1, NumDiscoverySessions()); |
| + EXPECT_EQ(0, [mock_central_manager_ scanForPeripheralsCallCount]); |
| EXPECT_EQ(0, [mock_central_manager_ stopScanCallCount]); |
| + |
| + // Remove last discovery sesssion. |
| + ResetEventCounts(); |
| + |
| RemoveDiscoverySession(discovery_filter.get()); |
| - EXPECT_EQ(2, callback_count_); |
| + EXPECT_EQ(1, callback_count_); |
| EXPECT_EQ(0, error_callback_count_); |
| EXPECT_EQ(0, NumDiscoverySessions()); |
| - // Check that removing the discovery session resulted in stopScan being called |
| - // on the Central Manager. |
| EXPECT_EQ(1, [mock_central_manager_ stopScanCallCount]); |
| } |