Index: chromeos/components/tether/keep_alive_scheduler_unittest.cc |
diff --git a/chromeos/components/tether/keep_alive_scheduler_unittest.cc b/chromeos/components/tether/keep_alive_scheduler_unittest.cc |
index 736c2f701e125fde737be52bb131c2add36b6d0e..36352344156431b71c83ca5a9672224dfa3732c3 100644 |
--- a/chromeos/components/tether/keep_alive_scheduler_unittest.cc |
+++ b/chromeos/components/tether/keep_alive_scheduler_unittest.cc |
@@ -8,8 +8,10 @@ |
#include <vector> |
#include "base/timer/mock_timer.h" |
+#include "chromeos/components/tether/device_id_tether_network_guid_map.h" |
#include "chromeos/components/tether/fake_active_host.h" |
#include "chromeos/components/tether/fake_ble_connection_manager.h" |
+#include "chromeos/components/tether/fake_host_scan_cache.h" |
#include "components/cryptauth/remote_device_test_util.h" |
#include "testing/gtest/include/gtest/gtest.h" |
@@ -38,7 +40,10 @@ class FakeKeepAliveOperation : public KeepAliveOperation { |
~FakeKeepAliveOperation() override { handler_->OnOperationDeleted(); } |
- void SendOperationFinishedEvent() { OnOperationFinished(); } |
+ void SendOperationFinishedEvent(std::unique_ptr<DeviceStatus> device_status) { |
+ device_status_ = std::move(device_status); |
+ OnOperationFinished(); |
+ } |
cryptauth::RemoteDevice remote_device() { return remote_device_; } |
@@ -78,6 +83,22 @@ class FakeKeepAliveOperationFactory : public KeepAliveOperation::Factory, |
FakeKeepAliveOperation* last_created_; |
}; |
+DeviceStatus CreateFakeDeviceStatus(const std::string& cell_provider, |
+ int battery_percentage, |
+ int connection_strength) { |
+ WifiStatus wifi_status; |
+ wifi_status.set_status_code( |
+ WifiStatus_StatusCode::WifiStatus_StatusCode_NOT_CONNECTED); |
+ |
+ DeviceStatus device_status; |
+ device_status.set_battery_percentage(battery_percentage); |
+ device_status.set_cell_provider(cell_provider); |
+ device_status.set_connection_strength(connection_strength); |
+ device_status.mutable_wifi_status()->CopyFrom(wifi_status); |
+ |
+ return device_status; |
+} |
+ |
} // namespace |
class KeepAliveSchedulerTest : public testing::Test { |
@@ -88,6 +109,9 @@ class KeepAliveSchedulerTest : public testing::Test { |
void SetUp() override { |
fake_active_host_ = base::MakeUnique<FakeActiveHost>(); |
fake_ble_connection_manager_ = base::MakeUnique<FakeBleConnectionManager>(); |
+ fake_host_scan_cache_ = base::MakeUnique<FakeHostScanCache>(); |
+ device_id_tether_network_guid_map_ = |
+ base::MakeUnique<DeviceIdTetherNetworkGuidMap>(); |
mock_timer_ = new base::MockTimer(true /* retain_user_task */, |
true /* is_repeating */); |
@@ -98,6 +122,7 @@ class KeepAliveSchedulerTest : public testing::Test { |
scheduler_ = base::WrapUnique(new KeepAliveScheduler( |
fake_active_host_.get(), fake_ble_connection_manager_.get(), |
+ fake_host_scan_cache_.get(), device_id_tether_network_guid_map_.get(), |
base::WrapUnique(mock_timer_))); |
} |
@@ -111,10 +136,37 @@ class KeepAliveSchedulerTest : public testing::Test { |
} |
} |
+ void SendOperationFinishedEventFromLastCreatedOperation( |
+ const std::string& cell_provider, |
+ int battery_percentage, |
+ int connection_strength) { |
+ fake_operation_factory_->last_created()->SendOperationFinishedEvent( |
+ base::MakeUnique<DeviceStatus>(CreateFakeDeviceStatus( |
+ cell_provider, battery_percentage, connection_strength))); |
+ } |
+ |
+ void VerifyCacheUpdated(const cryptauth::RemoteDevice& remote_device, |
+ const std::string& carrier, |
+ int battery_percentage, |
+ int signal_strength) { |
+ const FakeHostScanCache::CacheEntry* entry = |
+ fake_host_scan_cache_->GetCacheEntry( |
+ device_id_tether_network_guid_map_->GetTetherNetworkGuidForDeviceId( |
+ remote_device.GetDeviceId())); |
+ ASSERT_TRUE(entry); |
+ EXPECT_EQ(carrier, entry->carrier); |
+ EXPECT_EQ(battery_percentage, entry->battery_percentage); |
+ EXPECT_EQ(signal_strength, entry->signal_strength); |
+ } |
+ |
const std::vector<cryptauth::RemoteDevice> test_devices_; |
std::unique_ptr<FakeActiveHost> fake_active_host_; |
std::unique_ptr<FakeBleConnectionManager> fake_ble_connection_manager_; |
+ std::unique_ptr<FakeHostScanCache> fake_host_scan_cache_; |
+ // TODO(hansberry): Use a fake for this when a real mapping scheme is created. |
+ std::unique_ptr<DeviceIdTetherNetworkGuidMap> |
+ device_id_tether_network_guid_map_; |
base::MockTimer* mock_timer_; |
std::unique_ptr<FakeKeepAliveOperationFactory> fake_operation_factory_; |
@@ -148,12 +200,15 @@ TEST_F(KeepAliveSchedulerTest, TestSendTickle_OneActiveHost) { |
VerifyTimerRunning(true /* is_running */); |
// Ensure that once the operation is finished, it is deleted. |
- fake_operation_factory_->last_created()->SendOperationFinishedEvent(); |
+ SendOperationFinishedEventFromLastCreatedOperation( |
+ "cellProvider", 50 /* battery_percentage */, 2 /* connection_strength */); |
EXPECT_EQ(1u, fake_operation_factory_->num_created()); |
EXPECT_EQ(1u, fake_operation_factory_->num_deleted()); |
VerifyTimerRunning(true /* is_running */); |
+ VerifyCacheUpdated(test_devices_[0], "cellProvider", |
+ 50 /* battery_percentage */, 50 /* signal_strength */); |
- // Fire the timer; this should result in another tickle being sent. |
+ // Fire the timer; this should result in tickle #2 being sent. |
mock_timer_->Fire(); |
EXPECT_EQ(2u, fake_operation_factory_->num_created()); |
EXPECT_EQ(test_devices_[0], |
@@ -161,16 +216,37 @@ TEST_F(KeepAliveSchedulerTest, TestSendTickle_OneActiveHost) { |
EXPECT_EQ(1u, fake_operation_factory_->num_deleted()); |
VerifyTimerRunning(true /* is_running */); |
- // Finish this operation. |
- fake_operation_factory_->last_created()->SendOperationFinishedEvent(); |
+ // Finish tickle operation #2. |
+ SendOperationFinishedEventFromLastCreatedOperation( |
+ "cellProvider", 40 /* battery_percentage */, 3 /* connection_strength */); |
EXPECT_EQ(2u, fake_operation_factory_->num_created()); |
EXPECT_EQ(2u, fake_operation_factory_->num_deleted()); |
VerifyTimerRunning(true /* is_running */); |
+ VerifyCacheUpdated(test_devices_[0], "cellProvider", |
+ 40 /* battery_percentage */, 75 /* signal_strength */); |
+ |
+ // Fire the timer; this should result in tickle #3 being sent. |
+ mock_timer_->Fire(); |
+ EXPECT_EQ(3u, fake_operation_factory_->num_created()); |
+ EXPECT_EQ(test_devices_[0], |
+ fake_operation_factory_->last_created()->remote_device()); |
+ EXPECT_EQ(2u, fake_operation_factory_->num_deleted()); |
+ VerifyTimerRunning(true /* is_running */); |
+ |
+ // Finish tickler operation #3. This time, simulate a failure to receive a |
+ // DeviceStatus back. |
+ fake_operation_factory_->last_created()->SendOperationFinishedEvent(nullptr); |
+ EXPECT_EQ(3u, fake_operation_factory_->num_created()); |
+ EXPECT_EQ(3u, fake_operation_factory_->num_deleted()); |
+ VerifyTimerRunning(true /* is_running */); |
+ // The same data returned by tickle #2 should be present. |
+ VerifyCacheUpdated(test_devices_[0], "cellProvider", |
+ 40 /* battery_percentage */, 75 /* signal_strength */); |
// Disconnect that device. |
fake_active_host_->SetActiveHostDisconnected(); |
- EXPECT_EQ(2u, fake_operation_factory_->num_created()); |
- EXPECT_EQ(2u, fake_operation_factory_->num_deleted()); |
+ EXPECT_EQ(3u, fake_operation_factory_->num_created()); |
+ EXPECT_EQ(3u, fake_operation_factory_->num_deleted()); |
VerifyTimerRunning(false /* is_running */); |
} |
@@ -221,10 +297,13 @@ TEST_F(KeepAliveSchedulerTest, TestSendTickle_MultipleActiveHosts) { |
VerifyTimerRunning(true /* is_running */); |
// Ensure that once the second operation is finished, it is deleted. |
- fake_operation_factory_->last_created()->SendOperationFinishedEvent(); |
+ SendOperationFinishedEventFromLastCreatedOperation( |
+ "cellProvider", 80 /* battery_percentage */, 4 /* connection_strength */); |
EXPECT_EQ(2u, fake_operation_factory_->num_created()); |
EXPECT_EQ(2u, fake_operation_factory_->num_deleted()); |
VerifyTimerRunning(true /* is_running */); |
+ VerifyCacheUpdated(test_devices_[1], "cellProvider", |
+ 80 /* battery_percentage */, 100 /* signal_strength */); |
// Disconnect that device. |
fake_active_host_->SetActiveHostDisconnected(); |