Commit ad995528 authored by James Vecore's avatar James Vecore Committed by Commit Bot

[Nearby] Only fire device name change if it was changed

The change ensure that the device name change callback is only called
if the device name actually changes. Previously it was spamming the log
andy time the device changed.

Fixed: 1133045
Change-Id: I0b1e11741e83a180c9977fc0f01bf63a249fd91c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2441307Reviewed-by: default avatarRyan Hansberry <hansberry@chromium.org>
Commit-Queue: James Vecore <vecore@google.com>
Cr-Commit-Position: refs/heads/master@{#812301}
parent 71a48ae3
...@@ -167,8 +167,12 @@ void BluetoothClassicMedium::DeviceAdded( ...@@ -167,8 +167,12 @@ void BluetoothClassicMedium::DeviceAdded(
const std::string& address = device->address; const std::string& address = device->address;
if (base::Contains(discovered_bluetooth_devices_map_, address)) { if (base::Contains(discovered_bluetooth_devices_map_, address)) {
auto& bluetooth_device = discovered_bluetooth_devices_map_.at(address); auto& bluetooth_device = discovered_bluetooth_devices_map_.at(address);
bool name_changed = device->name.has_value() &&
device->name.value() != bluetooth_device.GetName();
bluetooth_device.UpdateDeviceInfo(std::move(device)); bluetooth_device.UpdateDeviceInfo(std::move(device));
discovery_callback_->device_name_changed_cb(bluetooth_device); if (name_changed) {
discovery_callback_->device_name_changed_cb(bluetooth_device);
}
} else { } else {
discovered_bluetooth_devices_map_.emplace(address, std::move(device)); discovered_bluetooth_devices_map_.emplace(address, std::move(device));
discovery_callback_->device_discovered_cb( discovery_callback_->device_discovered_cb(
......
...@@ -124,7 +124,6 @@ class BluetoothClassicMediumTest : public testing::Test { ...@@ -124,7 +124,6 @@ class BluetoothClassicMediumTest : public testing::Test {
base::OnceClosure on_device_name_changed_callback_; base::OnceClosure on_device_name_changed_callback_;
base::OnceClosure on_device_lost_callback_; base::OnceClosure on_device_lost_callback_;
private:
bluetooth::mojom::DeviceInfoPtr CreateDeviceInfo(const std::string& address, bluetooth::mojom::DeviceInfoPtr CreateDeviceInfo(const std::string& address,
const std::string& name) { const std::string& name) {
auto device_info = bluetooth::mojom::DeviceInfo::New(); auto device_info = bluetooth::mojom::DeviceInfo::New();
...@@ -134,6 +133,7 @@ class BluetoothClassicMediumTest : public testing::Test { ...@@ -134,6 +133,7 @@ class BluetoothClassicMediumTest : public testing::Test {
return device_info; return device_info;
} }
private:
base::test::TaskEnvironment task_environment_; base::test::TaskEnvironment task_environment_;
}; };
...@@ -217,6 +217,18 @@ TEST_F(BluetoothClassicMediumTest, TestDiscovery_DeviceNameChanged) { ...@@ -217,6 +217,18 @@ TEST_F(BluetoothClassicMediumTest, TestDiscovery_DeviceNameChanged) {
EXPECT_EQ(last_device_name_changed_, last_device_discovered_); EXPECT_EQ(last_device_name_changed_, last_device_discovered_);
// It is possible for DeviceChanged to trigger without a name change. This
// previously caused a name change event. Here we verify if the name
// does not change then we do not see the name change event.
last_device_name_changed_ = nullptr;
// We have to call NotifyDeviceChanged directly since we don't expect the
// callback to be invoked.
base::RunLoop run_loop;
fake_adapter_->NotifyDeviceChanged(
CreateDeviceInfo(kDeviceAddress1, kDeviceName2));
run_loop.RunUntilIdle();
EXPECT_EQ(nullptr, last_device_name_changed_);
StopDiscovery(); StopDiscovery();
} }
......
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