Commit 4eab3f75 authored by Kyle Horimoto's avatar Kyle Horimoto Committed by Commit Bot

[CrOS MultiDevice] Add BleScanner::HasScanFilter().

Additionally, this CL adds new functionality to FakeBleScanner
and FakeBleAdvertiser for providing access to their stored
scan/advertisement requests.

Bug: 824568, 752273
Change-Id: I0407021b62605e6feea745ce51ed4136013bce6d
Reviewed-on: https://chromium-review.googlesource.com/1100479
Commit-Queue: Kyle Horimoto <khorimoto@chromium.org>
Reviewed-by: default avatarRyan Hansberry <hansberry@chromium.org>
Cr-Commit-Position: refs/heads/master@{#567333}
parent 63889211
...@@ -38,6 +38,10 @@ void BleScanner::RemoveScanFilter(const DeviceIdPair& scan_filter) { ...@@ -38,6 +38,10 @@ void BleScanner::RemoveScanFilter(const DeviceIdPair& scan_filter) {
HandleScanFilterChange(); HandleScanFilterChange();
} }
bool BleScanner::HasScanFilter(const DeviceIdPair& scan_filter) {
return base::ContainsKey(scan_filters_, scan_filter);
}
void BleScanner::NotifyReceivedAdvertisementFromDevice( void BleScanner::NotifyReceivedAdvertisementFromDevice(
const cryptauth::RemoteDeviceRef& remote_device, const cryptauth::RemoteDeviceRef& remote_device,
device::BluetoothDevice* bluetooth_device, device::BluetoothDevice* bluetooth_device,
......
...@@ -44,6 +44,8 @@ class BleScanner { ...@@ -44,6 +44,8 @@ class BleScanner {
// stop. // stop.
void RemoveScanFilter(const DeviceIdPair& scan_filter); void RemoveScanFilter(const DeviceIdPair& scan_filter);
bool HasScanFilter(const DeviceIdPair& scan_filter);
protected: protected:
BleScanner(Delegate* delegate); BleScanner(Delegate* delegate);
......
...@@ -126,6 +126,18 @@ class SecureChannelBleScannerImplTest : public testing::Test { ...@@ -126,6 +126,18 @@ class SecureChannelBleScannerImplTest : public testing::Test {
std::move(fake_service_data_provider)); std::move(fake_service_data_provider));
} }
void AddScanFilter(const DeviceIdPair& scan_filter) {
EXPECT_FALSE(ble_scanner_->HasScanFilter(scan_filter));
ble_scanner_->AddScanFilter(scan_filter);
EXPECT_TRUE(ble_scanner_->HasScanFilter(scan_filter));
}
void RemoveScanFilter(const DeviceIdPair& scan_filter) {
EXPECT_TRUE(ble_scanner_->HasScanFilter(scan_filter));
ble_scanner_->RemoveScanFilter(scan_filter);
EXPECT_FALSE(ble_scanner_->HasScanFilter(scan_filter));
}
void ProcessScanResultAndVerifyNoDeviceIdentified( void ProcessScanResultAndVerifyNoDeviceIdentified(
const std::string& service_data, const std::string& service_data,
bool is_new_device) { bool is_new_device) {
...@@ -187,8 +199,6 @@ class SecureChannelBleScannerImplTest : public testing::Test { ...@@ -187,8 +199,6 @@ class SecureChannelBleScannerImplTest : public testing::Test {
return fake_ble_synchronizer_->GetNumCommands(); return fake_ble_synchronizer_->GetNumCommands();
} }
BleScanner* ble_scanner() { return ble_scanner_.get(); }
FakeDiscoverySession* fake_discovery_session() { FakeDiscoverySession* fake_discovery_session() {
return fake_discovery_session_; return fake_discovery_session_;
} }
...@@ -246,7 +256,7 @@ TEST_F(SecureChannelBleScannerImplTest, UrelatedScanResults) { ...@@ -246,7 +256,7 @@ TEST_F(SecureChannelBleScannerImplTest, UrelatedScanResults) {
DeviceIdPair pair(test_devices()[0].GetDeviceId(), DeviceIdPair pair(test_devices()[0].GetDeviceId(),
test_devices()[1].GetDeviceId()); test_devices()[1].GetDeviceId());
ble_scanner()->AddScanFilter(pair); AddScanFilter(pair);
InvokeStartDiscoveryCallback(true /* success */, 0u /* command_index */); InvokeStartDiscoveryCallback(true /* success */, 0u /* command_index */);
EXPECT_TRUE(fake_discovery_session()); EXPECT_TRUE(fake_discovery_session());
...@@ -255,7 +265,7 @@ TEST_F(SecureChannelBleScannerImplTest, UrelatedScanResults) { ...@@ -255,7 +265,7 @@ TEST_F(SecureChannelBleScannerImplTest, UrelatedScanResults) {
ProcessScanResultAndVerifyNoDeviceIdentified("unrelatedServiceData", ProcessScanResultAndVerifyNoDeviceIdentified("unrelatedServiceData",
false /* is_new_device */); false /* is_new_device */);
ble_scanner()->RemoveScanFilter(pair); RemoveScanFilter(pair);
InvokeStopDiscoveryCallback(true /* success */, 1u /* command_index */); InvokeStopDiscoveryCallback(true /* success */, 1u /* command_index */);
EXPECT_FALSE(fake_discovery_session()); EXPECT_FALSE(fake_discovery_session());
} }
...@@ -264,7 +274,7 @@ TEST_F(SecureChannelBleScannerImplTest, IdentifyDevice_NewDevice_Background) { ...@@ -264,7 +274,7 @@ TEST_F(SecureChannelBleScannerImplTest, IdentifyDevice_NewDevice_Background) {
DeviceIdPair pair(test_devices()[0].GetDeviceId(), DeviceIdPair pair(test_devices()[0].GetDeviceId(),
test_devices()[1].GetDeviceId()); test_devices()[1].GetDeviceId());
ble_scanner()->AddScanFilter(pair); AddScanFilter(pair);
InvokeStartDiscoveryCallback(true /* success */, 0u /* command_index */); InvokeStartDiscoveryCallback(true /* success */, 0u /* command_index */);
EXPECT_TRUE(fake_discovery_session()); EXPECT_TRUE(fake_discovery_session());
...@@ -273,7 +283,7 @@ TEST_F(SecureChannelBleScannerImplTest, IdentifyDevice_NewDevice_Background) { ...@@ -273,7 +283,7 @@ TEST_F(SecureChannelBleScannerImplTest, IdentifyDevice_NewDevice_Background) {
true /* is_new_device */, test_devices()[0], true /* is_new_device */, test_devices()[0],
true /* is_background_advertisement */); true /* is_background_advertisement */);
ble_scanner()->RemoveScanFilter(pair); RemoveScanFilter(pair);
InvokeStopDiscoveryCallback(true /* success */, 1u /* command_index */); InvokeStopDiscoveryCallback(true /* success */, 1u /* command_index */);
EXPECT_FALSE(fake_discovery_session()); EXPECT_FALSE(fake_discovery_session());
} }
...@@ -283,7 +293,7 @@ TEST_F(SecureChannelBleScannerImplTest, ...@@ -283,7 +293,7 @@ TEST_F(SecureChannelBleScannerImplTest,
DeviceIdPair pair(test_devices()[0].GetDeviceId(), DeviceIdPair pair(test_devices()[0].GetDeviceId(),
test_devices()[1].GetDeviceId()); test_devices()[1].GetDeviceId());
ble_scanner()->AddScanFilter(pair); AddScanFilter(pair);
InvokeStartDiscoveryCallback(true /* success */, 0u /* command_index */); InvokeStartDiscoveryCallback(true /* success */, 0u /* command_index */);
EXPECT_TRUE(fake_discovery_session()); EXPECT_TRUE(fake_discovery_session());
...@@ -292,7 +302,7 @@ TEST_F(SecureChannelBleScannerImplTest, ...@@ -292,7 +302,7 @@ TEST_F(SecureChannelBleScannerImplTest,
false /* is_new_device */, test_devices()[0], false /* is_new_device */, test_devices()[0],
false /* is_background_advertisement */); false /* is_background_advertisement */);
ble_scanner()->RemoveScanFilter(pair); RemoveScanFilter(pair);
InvokeStopDiscoveryCallback(true /* success */, 1u /* command_index */); InvokeStopDiscoveryCallback(true /* success */, 1u /* command_index */);
EXPECT_FALSE(fake_discovery_session()); EXPECT_FALSE(fake_discovery_session());
} }
...@@ -303,8 +313,8 @@ TEST_F(SecureChannelBleScannerImplTest, IdentifyDevice_MultipleScans) { ...@@ -303,8 +313,8 @@ TEST_F(SecureChannelBleScannerImplTest, IdentifyDevice_MultipleScans) {
DeviceIdPair pair_2(test_devices()[2].GetDeviceId(), DeviceIdPair pair_2(test_devices()[2].GetDeviceId(),
test_devices()[1].GetDeviceId()); test_devices()[1].GetDeviceId());
ble_scanner()->AddScanFilter(pair_1); AddScanFilter(pair_1);
ble_scanner()->AddScanFilter(pair_2); AddScanFilter(pair_2);
InvokeStartDiscoveryCallback(true /* success */, 0u /* command_index */); InvokeStartDiscoveryCallback(true /* success */, 0u /* command_index */);
EXPECT_TRUE(fake_discovery_session()); EXPECT_TRUE(fake_discovery_session());
...@@ -314,7 +324,7 @@ TEST_F(SecureChannelBleScannerImplTest, IdentifyDevice_MultipleScans) { ...@@ -314,7 +324,7 @@ TEST_F(SecureChannelBleScannerImplTest, IdentifyDevice_MultipleScans) {
false /* is_background_advertisement */); false /* is_background_advertisement */);
// Remove the identified device from the list of scan filters. // Remove the identified device from the list of scan filters.
ble_scanner()->RemoveScanFilter(pair_1); RemoveScanFilter(pair_1);
// No additional BLE command should have been posted, since the existing scan // No additional BLE command should have been posted, since the existing scan
// should not have been stopped. // should not have been stopped.
...@@ -322,13 +332,13 @@ TEST_F(SecureChannelBleScannerImplTest, IdentifyDevice_MultipleScans) { ...@@ -322,13 +332,13 @@ TEST_F(SecureChannelBleScannerImplTest, IdentifyDevice_MultipleScans) {
EXPECT_TRUE(fake_discovery_session()); EXPECT_TRUE(fake_discovery_session());
// Remove the scan filter, and verify that the scan stopped. // Remove the scan filter, and verify that the scan stopped.
ble_scanner()->RemoveScanFilter(pair_2); RemoveScanFilter(pair_2);
InvokeStopDiscoveryCallback(true /* success */, 1u /* command_index */); InvokeStopDiscoveryCallback(true /* success */, 1u /* command_index */);
EXPECT_FALSE(fake_discovery_session()); EXPECT_FALSE(fake_discovery_session());
// Add the scan filter back again; this should start the discovery session // Add the scan filter back again; this should start the discovery session
// back up again. // back up again.
ble_scanner()->AddScanFilter(pair_2); AddScanFilter(pair_2);
InvokeStartDiscoveryCallback(true /* success */, 2u /* command_index */); InvokeStartDiscoveryCallback(true /* success */, 2u /* command_index */);
EXPECT_TRUE(fake_discovery_session()); EXPECT_TRUE(fake_discovery_session());
...@@ -338,7 +348,7 @@ TEST_F(SecureChannelBleScannerImplTest, IdentifyDevice_MultipleScans) { ...@@ -338,7 +348,7 @@ TEST_F(SecureChannelBleScannerImplTest, IdentifyDevice_MultipleScans) {
false /* is_background_advertisement */); false /* is_background_advertisement */);
// Remove the scan filter, and verify that the scan stopped. // Remove the scan filter, and verify that the scan stopped.
ble_scanner()->RemoveScanFilter(pair_2); RemoveScanFilter(pair_2);
InvokeStopDiscoveryCallback(true /* success */, 3u /* command_index */); InvokeStopDiscoveryCallback(true /* success */, 3u /* command_index */);
EXPECT_FALSE(fake_discovery_session()); EXPECT_FALSE(fake_discovery_session());
} }
...@@ -346,7 +356,7 @@ TEST_F(SecureChannelBleScannerImplTest, IdentifyDevice_MultipleScans) { ...@@ -346,7 +356,7 @@ TEST_F(SecureChannelBleScannerImplTest, IdentifyDevice_MultipleScans) {
TEST_F(SecureChannelBleScannerImplTest, StartAndStopFailures) { TEST_F(SecureChannelBleScannerImplTest, StartAndStopFailures) {
DeviceIdPair pair(test_devices()[0].GetDeviceId(), DeviceIdPair pair(test_devices()[0].GetDeviceId(),
test_devices()[1].GetDeviceId()); test_devices()[1].GetDeviceId());
ble_scanner()->AddScanFilter(pair); AddScanFilter(pair);
// A request was made to start discovery; simulate this request failing. // A request was made to start discovery; simulate this request failing.
InvokeStartDiscoveryCallback(false /* success */, 0u /* command_index */); InvokeStartDiscoveryCallback(false /* success */, 0u /* command_index */);
...@@ -362,7 +372,7 @@ TEST_F(SecureChannelBleScannerImplTest, StartAndStopFailures) { ...@@ -362,7 +372,7 @@ TEST_F(SecureChannelBleScannerImplTest, StartAndStopFailures) {
// Remove scan filters, which should trigger BleScanner to stop the // Remove scan filters, which should trigger BleScanner to stop the
// discovery session. // discovery session.
ble_scanner()->RemoveScanFilter(pair); RemoveScanFilter(pair);
// Simulate a failure to stop. // Simulate a failure to stop.
InvokeStopDiscoveryCallback(false /* success */, 3u /* command_index */); InvokeStopDiscoveryCallback(false /* success */, 3u /* command_index */);
...@@ -380,10 +390,10 @@ TEST_F(SecureChannelBleScannerImplTest, StartAndStopFailures) { ...@@ -380,10 +390,10 @@ TEST_F(SecureChannelBleScannerImplTest, StartAndStopFailures) {
TEST_F(SecureChannelBleScannerImplTest, StartAndStop_EdgeCases) { TEST_F(SecureChannelBleScannerImplTest, StartAndStop_EdgeCases) {
DeviceIdPair pair(test_devices()[0].GetDeviceId(), DeviceIdPair pair(test_devices()[0].GetDeviceId(),
test_devices()[1].GetDeviceId()); test_devices()[1].GetDeviceId());
ble_scanner()->AddScanFilter(pair); AddScanFilter(pair);
// Remove scan filters before the start discovery callback succeeds. // Remove scan filters before the start discovery callback succeeds.
ble_scanner()->RemoveScanFilter(pair); RemoveScanFilter(pair);
// Complete starting the discovery session. // Complete starting the discovery session.
InvokeStartDiscoveryCallback(true /* success */, 0u /* command_index */); InvokeStartDiscoveryCallback(true /* success */, 0u /* command_index */);
...@@ -398,10 +408,10 @@ TEST_F(SecureChannelBleScannerImplTest, StartAndStop_EdgeCases) { ...@@ -398,10 +408,10 @@ TEST_F(SecureChannelBleScannerImplTest, StartAndStop_EdgeCases) {
TEST_F(SecureChannelBleScannerImplTest, StartAndStopFailures_EdgeCases) { TEST_F(SecureChannelBleScannerImplTest, StartAndStopFailures_EdgeCases) {
DeviceIdPair pair(test_devices()[0].GetDeviceId(), DeviceIdPair pair(test_devices()[0].GetDeviceId(),
test_devices()[1].GetDeviceId()); test_devices()[1].GetDeviceId());
ble_scanner()->AddScanFilter(pair); AddScanFilter(pair);
// Remove scan filters before the start discovery callback succeeds. // Remove scan filters before the start discovery callback succeeds.
ble_scanner()->RemoveScanFilter(pair); RemoveScanFilter(pair);
// Fail the pending call to start a discovery session. // Fail the pending call to start a discovery session.
InvokeStartDiscoveryCallback(false /* success */, 0u /* command_index */); InvokeStartDiscoveryCallback(false /* success */, 0u /* command_index */);
......
...@@ -33,6 +33,16 @@ base::Optional<ConnectionPriority> FakeBleAdvertiser::GetPriorityForRequest( ...@@ -33,6 +33,16 @@ base::Optional<ConnectionPriority> FakeBleAdvertiser::GetPriorityForRequest(
return base::nullopt; return base::nullopt;
} }
std::vector<DeviceIdPair> FakeBleAdvertiser::GetAllRequestsForRemoteDevice(
const std::string& remote_device_id) {
std::vector<DeviceIdPair> all_requests_for_remote_device;
for (const auto& map_entry : request_to_priority_map()) {
if (map_entry.first.remote_device_id() == remote_device_id)
all_requests_for_remote_device.push_back(map_entry.first);
}
return all_requests_for_remote_device;
}
void FakeBleAdvertiser::NotifyAdvertisingSlotEnded( void FakeBleAdvertiser::NotifyAdvertisingSlotEnded(
const DeviceIdPair& device_id_pair, const DeviceIdPair& device_id_pair,
bool replaced_by_higher_priority_advertisement) { bool replaced_by_higher_priority_advertisement) {
......
...@@ -31,6 +31,9 @@ class FakeBleAdvertiser : public BleAdvertiser { ...@@ -31,6 +31,9 @@ class FakeBleAdvertiser : public BleAdvertiser {
base::Optional<ConnectionPriority> GetPriorityForRequest( base::Optional<ConnectionPriority> GetPriorityForRequest(
const DeviceIdPair& request) const; const DeviceIdPair& request) const;
std::vector<DeviceIdPair> GetAllRequestsForRemoteDevice(
const std::string& remote_device_id);
void NotifyAdvertisingSlotEnded( void NotifyAdvertisingSlotEnded(
const DeviceIdPair& device_id_pair, const DeviceIdPair& device_id_pair,
bool replaced_by_higher_priority_advertisement); bool replaced_by_higher_priority_advertisement);
......
...@@ -12,6 +12,16 @@ FakeBleScanner::FakeBleScanner(Delegate* delegate) : BleScanner(delegate) {} ...@@ -12,6 +12,16 @@ FakeBleScanner::FakeBleScanner(Delegate* delegate) : BleScanner(delegate) {}
FakeBleScanner::~FakeBleScanner() = default; FakeBleScanner::~FakeBleScanner() = default;
std::vector<DeviceIdPair> FakeBleScanner::GetAllScanFiltersForRemoteDevice(
const std::string& remote_device_id) {
std::vector<DeviceIdPair> all_scan_filters_for_remote_device;
for (const auto& scan_filter : scan_filters()) {
if (scan_filter.remote_device_id() == remote_device_id)
all_scan_filters_for_remote_device.push_back(scan_filter);
}
return all_scan_filters_for_remote_device;
}
void FakeBleScanner::HandleScanFilterChange() { void FakeBleScanner::HandleScanFilterChange() {
++num_scan_filter_changes_handled_; ++num_scan_filter_changes_handled_;
} }
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include "base/macros.h" #include "base/macros.h"
#include "chromeos/services/secure_channel/ble_scanner.h" #include "chromeos/services/secure_channel/ble_scanner.h"
#include "chromeos/services/secure_channel/device_id_pair.h"
namespace chromeos { namespace chromeos {
...@@ -25,7 +26,11 @@ class FakeBleScanner : public BleScanner { ...@@ -25,7 +26,11 @@ class FakeBleScanner : public BleScanner {
return num_scan_filter_changes_handled_; return num_scan_filter_changes_handled_;
} }
std::vector<DeviceIdPair> GetAllScanFiltersForRemoteDevice(
const std::string& remote_device_id);
// Public for testing. // Public for testing.
using BleScanner::scan_filters;
using BleScanner::NotifyReceivedAdvertisementFromDevice; using BleScanner::NotifyReceivedAdvertisementFromDevice;
private: private:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment