Commit a2662610 authored by Kyle Horimoto's avatar Kyle Horimoto Committed by Commit Bot

[CrOS MultiDevice] Sort eligible devices before returning them.

This CL sorts returned devices from most-recently-updated to
least-recently-updated. This increases the likelihood that users who go
through multi-device setup will set up the correct device, since usually
the most-recently-updated the device is the one that users want to set.

Bug: 870133
Change-Id: I954f3bde8ccb62c1f796ec62e8f12af65012e528
Reviewed-on: https://chromium-review.googlesource.com/c/1285769Reviewed-by: default avatarRyan Hansberry <hansberry@chromium.org>
Commit-Queue: Kyle Horimoto <khorimoto@chromium.org>
Cr-Commit-Position: refs/heads/master@{#600426}
parent a1d5b2b0
......@@ -157,6 +157,17 @@ void MultiDeviceSetupImpl::GetEligibleHostDevices(
eligible_remote_devices.push_back(remote_device_ref.GetRemoteDevice());
}
// Sort from most-recently-updated to least-recently-updated. The timestamp
// used is provided by the back-end and indicates the last time at which the
// device's metadata was updated on the server. Note that this does not
// provide us with the last time that a user actually used this device, but it
// is a good estimate.
std::sort(eligible_remote_devices.begin(), eligible_remote_devices.end(),
[](const auto& first_device, const auto& second_device) {
return first_device.last_update_time_millis >
second_device.last_update_time_millis;
});
std::move(callback).Run(eligible_remote_devices);
}
......
......@@ -1218,6 +1218,22 @@ TEST_F(MultiDeviceSetupImplTest, TestSetHostDeviceWithoutAuthToken) {
test_devices()[0], observer.get(), 1u /* expected_observer_index */);
}
TEST_F(MultiDeviceSetupImplTest, GetEligibleHostDevicesSortedByTimestamp) {
cryptauth::RemoteDeviceRefList devices = test_devices();
// Update the devices in the list such that they are sorted from
// least-recently-updated to most-recently-updated. This is the opposite order
// that is expected from GetEligibleDevices();
for (size_t i = 0; i < devices.size(); ++i)
GetMutableRemoteDevice(devices[i])->last_update_time_millis = i;
fake_eligible_host_devices_provider()->set_eligible_host_devices(devices);
// Now, reverse the original list and verify that the returned devices are
// sorted in the correct order.
std::reverse(devices.begin(), devices.end());
EXPECT_EQ(RefListToRawList(devices), CallGetEligibleHostDevices());
}
} // namespace multidevice_setup
} // namespace chromeos
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