| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chromeos/components/tether/keep_alive_scheduler.h" | 5 #include "chromeos/components/tether/keep_alive_scheduler.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/timer/mock_timer.h" | 10 #include "base/timer/mock_timer.h" |
| 11 #include "chromeos/components/tether/device_id_tether_network_guid_map.h" |
| 11 #include "chromeos/components/tether/fake_active_host.h" | 12 #include "chromeos/components/tether/fake_active_host.h" |
| 12 #include "chromeos/components/tether/fake_ble_connection_manager.h" | 13 #include "chromeos/components/tether/fake_ble_connection_manager.h" |
| 14 #include "chromeos/components/tether/fake_host_scan_cache.h" |
| 13 #include "components/cryptauth/remote_device_test_util.h" | 15 #include "components/cryptauth/remote_device_test_util.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 17 |
| 16 namespace chromeos { | 18 namespace chromeos { |
| 17 | 19 |
| 18 namespace tether { | 20 namespace tether { |
| 19 | 21 |
| 20 namespace { | 22 namespace { |
| 21 | 23 |
| 22 const char kTetherNetworkGuid[] = "tetherNetworkGuid"; | 24 const char kTetherNetworkGuid[] = "tetherNetworkGuid"; |
| 23 const char kWifiNetworkGuid[] = "wifiNetworkGuid"; | 25 const char kWifiNetworkGuid[] = "wifiNetworkGuid"; |
| 24 | 26 |
| 25 class OperationDeletedHandler { | 27 class OperationDeletedHandler { |
| 26 public: | 28 public: |
| 27 virtual void OnOperationDeleted() = 0; | 29 virtual void OnOperationDeleted() = 0; |
| 28 }; | 30 }; |
| 29 | 31 |
| 30 class FakeKeepAliveOperation : public KeepAliveOperation { | 32 class FakeKeepAliveOperation : public KeepAliveOperation { |
| 31 public: | 33 public: |
| 32 FakeKeepAliveOperation(const cryptauth::RemoteDevice& device_to_connect, | 34 FakeKeepAliveOperation(const cryptauth::RemoteDevice& device_to_connect, |
| 33 BleConnectionManager* connection_manager, | 35 BleConnectionManager* connection_manager, |
| 34 OperationDeletedHandler* handler) | 36 OperationDeletedHandler* handler) |
| 35 : KeepAliveOperation(device_to_connect, connection_manager), | 37 : KeepAliveOperation(device_to_connect, connection_manager), |
| 36 handler_(handler), | 38 handler_(handler), |
| 37 remote_device_(device_to_connect) {} | 39 remote_device_(device_to_connect) {} |
| 38 | 40 |
| 39 ~FakeKeepAliveOperation() override { handler_->OnOperationDeleted(); } | 41 ~FakeKeepAliveOperation() override { handler_->OnOperationDeleted(); } |
| 40 | 42 |
| 41 void SendOperationFinishedEvent() { OnOperationFinished(); } | 43 void SendOperationFinishedEvent(std::unique_ptr<DeviceStatus> device_status) { |
| 44 device_status_ = std::move(device_status); |
| 45 OnOperationFinished(); |
| 46 } |
| 42 | 47 |
| 43 cryptauth::RemoteDevice remote_device() { return remote_device_; } | 48 cryptauth::RemoteDevice remote_device() { return remote_device_; } |
| 44 | 49 |
| 45 private: | 50 private: |
| 46 OperationDeletedHandler* handler_; | 51 OperationDeletedHandler* handler_; |
| 47 const cryptauth::RemoteDevice remote_device_; | 52 const cryptauth::RemoteDevice remote_device_; |
| 48 }; | 53 }; |
| 49 | 54 |
| 50 class FakeKeepAliveOperationFactory : public KeepAliveOperation::Factory, | 55 class FakeKeepAliveOperationFactory : public KeepAliveOperation::Factory, |
| 51 public OperationDeletedHandler { | 56 public OperationDeletedHandler { |
| (...skipping 19 matching lines...) Expand all Loading... |
| 71 new FakeKeepAliveOperation(device_to_connect, connection_manager, this); | 76 new FakeKeepAliveOperation(device_to_connect, connection_manager, this); |
| 72 return base::WrapUnique(last_created_); | 77 return base::WrapUnique(last_created_); |
| 73 } | 78 } |
| 74 | 79 |
| 75 private: | 80 private: |
| 76 uint32_t num_created_; | 81 uint32_t num_created_; |
| 77 uint32_t num_deleted_; | 82 uint32_t num_deleted_; |
| 78 FakeKeepAliveOperation* last_created_; | 83 FakeKeepAliveOperation* last_created_; |
| 79 }; | 84 }; |
| 80 | 85 |
| 86 DeviceStatus CreateFakeDeviceStatus(const std::string& cell_provider, |
| 87 int battery_percentage, |
| 88 int connection_strength) { |
| 89 WifiStatus wifi_status; |
| 90 wifi_status.set_status_code( |
| 91 WifiStatus_StatusCode::WifiStatus_StatusCode_NOT_CONNECTED); |
| 92 |
| 93 DeviceStatus device_status; |
| 94 device_status.set_battery_percentage(battery_percentage); |
| 95 device_status.set_cell_provider(cell_provider); |
| 96 device_status.set_connection_strength(connection_strength); |
| 97 device_status.mutable_wifi_status()->CopyFrom(wifi_status); |
| 98 |
| 99 return device_status; |
| 100 } |
| 101 |
| 81 } // namespace | 102 } // namespace |
| 82 | 103 |
| 83 class KeepAliveSchedulerTest : public testing::Test { | 104 class KeepAliveSchedulerTest : public testing::Test { |
| 84 protected: | 105 protected: |
| 85 KeepAliveSchedulerTest() | 106 KeepAliveSchedulerTest() |
| 86 : test_devices_(cryptauth::GenerateTestRemoteDevices(2)) {} | 107 : test_devices_(cryptauth::GenerateTestRemoteDevices(2)) {} |
| 87 | 108 |
| 88 void SetUp() override { | 109 void SetUp() override { |
| 89 fake_active_host_ = base::MakeUnique<FakeActiveHost>(); | 110 fake_active_host_ = base::MakeUnique<FakeActiveHost>(); |
| 90 fake_ble_connection_manager_ = base::MakeUnique<FakeBleConnectionManager>(); | 111 fake_ble_connection_manager_ = base::MakeUnique<FakeBleConnectionManager>(); |
| 112 fake_host_scan_cache_ = base::MakeUnique<FakeHostScanCache>(); |
| 113 device_id_tether_network_guid_map_ = |
| 114 base::MakeUnique<DeviceIdTetherNetworkGuidMap>(); |
| 91 mock_timer_ = new base::MockTimer(true /* retain_user_task */, | 115 mock_timer_ = new base::MockTimer(true /* retain_user_task */, |
| 92 true /* is_repeating */); | 116 true /* is_repeating */); |
| 93 | 117 |
| 94 fake_operation_factory_ = | 118 fake_operation_factory_ = |
| 95 base::WrapUnique(new FakeKeepAliveOperationFactory()); | 119 base::WrapUnique(new FakeKeepAliveOperationFactory()); |
| 96 KeepAliveOperation::Factory::SetInstanceForTesting( | 120 KeepAliveOperation::Factory::SetInstanceForTesting( |
| 97 fake_operation_factory_.get()); | 121 fake_operation_factory_.get()); |
| 98 | 122 |
| 99 scheduler_ = base::WrapUnique(new KeepAliveScheduler( | 123 scheduler_ = base::WrapUnique(new KeepAliveScheduler( |
| 100 fake_active_host_.get(), fake_ble_connection_manager_.get(), | 124 fake_active_host_.get(), fake_ble_connection_manager_.get(), |
| 125 fake_host_scan_cache_.get(), device_id_tether_network_guid_map_.get(), |
| 101 base::WrapUnique(mock_timer_))); | 126 base::WrapUnique(mock_timer_))); |
| 102 } | 127 } |
| 103 | 128 |
| 104 void VerifyTimerRunning(bool is_running) { | 129 void VerifyTimerRunning(bool is_running) { |
| 105 EXPECT_EQ(is_running, mock_timer_->IsRunning()); | 130 EXPECT_EQ(is_running, mock_timer_->IsRunning()); |
| 106 | 131 |
| 107 if (is_running) { | 132 if (is_running) { |
| 108 EXPECT_EQ(base::TimeDelta::FromMinutes( | 133 EXPECT_EQ(base::TimeDelta::FromMinutes( |
| 109 KeepAliveScheduler::kKeepAliveIntervalMinutes), | 134 KeepAliveScheduler::kKeepAliveIntervalMinutes), |
| 110 mock_timer_->GetCurrentDelay()); | 135 mock_timer_->GetCurrentDelay()); |
| 111 } | 136 } |
| 112 } | 137 } |
| 113 | 138 |
| 139 void SendOperationFinishedEventFromLastCreatedOperation( |
| 140 const std::string& cell_provider, |
| 141 int battery_percentage, |
| 142 int connection_strength) { |
| 143 fake_operation_factory_->last_created()->SendOperationFinishedEvent( |
| 144 base::MakeUnique<DeviceStatus>(CreateFakeDeviceStatus( |
| 145 cell_provider, battery_percentage, connection_strength))); |
| 146 } |
| 147 |
| 148 void VerifyCacheUpdated(const cryptauth::RemoteDevice& remote_device, |
| 149 const std::string& carrier, |
| 150 int battery_percentage, |
| 151 int signal_strength) { |
| 152 const FakeHostScanCache::CacheEntry* entry = |
| 153 fake_host_scan_cache_->GetCacheEntry( |
| 154 device_id_tether_network_guid_map_->GetTetherNetworkGuidForDeviceId( |
| 155 remote_device.GetDeviceId())); |
| 156 ASSERT_TRUE(entry); |
| 157 EXPECT_EQ(carrier, entry->carrier); |
| 158 EXPECT_EQ(battery_percentage, entry->battery_percentage); |
| 159 EXPECT_EQ(signal_strength, entry->signal_strength); |
| 160 } |
| 161 |
| 114 const std::vector<cryptauth::RemoteDevice> test_devices_; | 162 const std::vector<cryptauth::RemoteDevice> test_devices_; |
| 115 | 163 |
| 116 std::unique_ptr<FakeActiveHost> fake_active_host_; | 164 std::unique_ptr<FakeActiveHost> fake_active_host_; |
| 117 std::unique_ptr<FakeBleConnectionManager> fake_ble_connection_manager_; | 165 std::unique_ptr<FakeBleConnectionManager> fake_ble_connection_manager_; |
| 166 std::unique_ptr<FakeHostScanCache> fake_host_scan_cache_; |
| 167 // TODO(hansberry): Use a fake for this when a real mapping scheme is created. |
| 168 std::unique_ptr<DeviceIdTetherNetworkGuidMap> |
| 169 device_id_tether_network_guid_map_; |
| 118 base::MockTimer* mock_timer_; | 170 base::MockTimer* mock_timer_; |
| 119 | 171 |
| 120 std::unique_ptr<FakeKeepAliveOperationFactory> fake_operation_factory_; | 172 std::unique_ptr<FakeKeepAliveOperationFactory> fake_operation_factory_; |
| 121 | 173 |
| 122 std::unique_ptr<KeepAliveScheduler> scheduler_; | 174 std::unique_ptr<KeepAliveScheduler> scheduler_; |
| 123 | 175 |
| 124 private: | 176 private: |
| 125 DISALLOW_COPY_AND_ASSIGN(KeepAliveSchedulerTest); | 177 DISALLOW_COPY_AND_ASSIGN(KeepAliveSchedulerTest); |
| 126 }; | 178 }; |
| 127 | 179 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 141 fake_active_host_->SetActiveHostConnected(test_devices_[0].GetDeviceId(), | 193 fake_active_host_->SetActiveHostConnected(test_devices_[0].GetDeviceId(), |
| 142 std::string(kTetherNetworkGuid), | 194 std::string(kTetherNetworkGuid), |
| 143 std::string(kWifiNetworkGuid)); | 195 std::string(kWifiNetworkGuid)); |
| 144 EXPECT_EQ(1u, fake_operation_factory_->num_created()); | 196 EXPECT_EQ(1u, fake_operation_factory_->num_created()); |
| 145 EXPECT_EQ(test_devices_[0], | 197 EXPECT_EQ(test_devices_[0], |
| 146 fake_operation_factory_->last_created()->remote_device()); | 198 fake_operation_factory_->last_created()->remote_device()); |
| 147 EXPECT_FALSE(fake_operation_factory_->num_deleted()); | 199 EXPECT_FALSE(fake_operation_factory_->num_deleted()); |
| 148 VerifyTimerRunning(true /* is_running */); | 200 VerifyTimerRunning(true /* is_running */); |
| 149 | 201 |
| 150 // Ensure that once the operation is finished, it is deleted. | 202 // Ensure that once the operation is finished, it is deleted. |
| 151 fake_operation_factory_->last_created()->SendOperationFinishedEvent(); | 203 SendOperationFinishedEventFromLastCreatedOperation( |
| 204 "cellProvider", 50 /* battery_percentage */, 2 /* connection_strength */); |
| 152 EXPECT_EQ(1u, fake_operation_factory_->num_created()); | 205 EXPECT_EQ(1u, fake_operation_factory_->num_created()); |
| 153 EXPECT_EQ(1u, fake_operation_factory_->num_deleted()); | 206 EXPECT_EQ(1u, fake_operation_factory_->num_deleted()); |
| 154 VerifyTimerRunning(true /* is_running */); | 207 VerifyTimerRunning(true /* is_running */); |
| 208 VerifyCacheUpdated(test_devices_[0], "cellProvider", |
| 209 50 /* battery_percentage */, 50 /* signal_strength */); |
| 155 | 210 |
| 156 // Fire the timer; this should result in another tickle being sent. | 211 // Fire the timer; this should result in tickle #2 being sent. |
| 157 mock_timer_->Fire(); | 212 mock_timer_->Fire(); |
| 158 EXPECT_EQ(2u, fake_operation_factory_->num_created()); | 213 EXPECT_EQ(2u, fake_operation_factory_->num_created()); |
| 159 EXPECT_EQ(test_devices_[0], | 214 EXPECT_EQ(test_devices_[0], |
| 160 fake_operation_factory_->last_created()->remote_device()); | 215 fake_operation_factory_->last_created()->remote_device()); |
| 161 EXPECT_EQ(1u, fake_operation_factory_->num_deleted()); | 216 EXPECT_EQ(1u, fake_operation_factory_->num_deleted()); |
| 162 VerifyTimerRunning(true /* is_running */); | 217 VerifyTimerRunning(true /* is_running */); |
| 163 | 218 |
| 164 // Finish this operation. | 219 // Finish tickle operation #2. |
| 165 fake_operation_factory_->last_created()->SendOperationFinishedEvent(); | 220 SendOperationFinishedEventFromLastCreatedOperation( |
| 221 "cellProvider", 40 /* battery_percentage */, 3 /* connection_strength */); |
| 166 EXPECT_EQ(2u, fake_operation_factory_->num_created()); | 222 EXPECT_EQ(2u, fake_operation_factory_->num_created()); |
| 167 EXPECT_EQ(2u, fake_operation_factory_->num_deleted()); | 223 EXPECT_EQ(2u, fake_operation_factory_->num_deleted()); |
| 168 VerifyTimerRunning(true /* is_running */); | 224 VerifyTimerRunning(true /* is_running */); |
| 225 VerifyCacheUpdated(test_devices_[0], "cellProvider", |
| 226 40 /* battery_percentage */, 75 /* signal_strength */); |
| 227 |
| 228 // Fire the timer; this should result in tickle #3 being sent. |
| 229 mock_timer_->Fire(); |
| 230 EXPECT_EQ(3u, fake_operation_factory_->num_created()); |
| 231 EXPECT_EQ(test_devices_[0], |
| 232 fake_operation_factory_->last_created()->remote_device()); |
| 233 EXPECT_EQ(2u, fake_operation_factory_->num_deleted()); |
| 234 VerifyTimerRunning(true /* is_running */); |
| 235 |
| 236 // Finish tickler operation #3. This time, simulate a failure to receive a |
| 237 // DeviceStatus back. |
| 238 fake_operation_factory_->last_created()->SendOperationFinishedEvent(nullptr); |
| 239 EXPECT_EQ(3u, fake_operation_factory_->num_created()); |
| 240 EXPECT_EQ(3u, fake_operation_factory_->num_deleted()); |
| 241 VerifyTimerRunning(true /* is_running */); |
| 242 // The same data returned by tickle #2 should be present. |
| 243 VerifyCacheUpdated(test_devices_[0], "cellProvider", |
| 244 40 /* battery_percentage */, 75 /* signal_strength */); |
| 169 | 245 |
| 170 // Disconnect that device. | 246 // Disconnect that device. |
| 171 fake_active_host_->SetActiveHostDisconnected(); | 247 fake_active_host_->SetActiveHostDisconnected(); |
| 172 EXPECT_EQ(2u, fake_operation_factory_->num_created()); | 248 EXPECT_EQ(3u, fake_operation_factory_->num_created()); |
| 173 EXPECT_EQ(2u, fake_operation_factory_->num_deleted()); | 249 EXPECT_EQ(3u, fake_operation_factory_->num_deleted()); |
| 174 VerifyTimerRunning(false /* is_running */); | 250 VerifyTimerRunning(false /* is_running */); |
| 175 } | 251 } |
| 176 | 252 |
| 177 TEST_F(KeepAliveSchedulerTest, TestSendTickle_MultipleActiveHosts) { | 253 TEST_F(KeepAliveSchedulerTest, TestSendTickle_MultipleActiveHosts) { |
| 178 EXPECT_FALSE(fake_operation_factory_->num_created()); | 254 EXPECT_FALSE(fake_operation_factory_->num_created()); |
| 179 EXPECT_FALSE(fake_operation_factory_->num_deleted()); | 255 EXPECT_FALSE(fake_operation_factory_->num_deleted()); |
| 180 VerifyTimerRunning(false /* is_running */); | 256 VerifyTimerRunning(false /* is_running */); |
| 181 | 257 |
| 182 // Start connecting to a device. No operation should be started. | 258 // Start connecting to a device. No operation should be started. |
| 183 fake_active_host_->SetActiveHostConnecting(test_devices_[0].GetDeviceId(), | 259 fake_active_host_->SetActiveHostConnecting(test_devices_[0].GetDeviceId(), |
| (...skipping 30 matching lines...) Expand all Loading... |
| 214 fake_active_host_->SetActiveHostConnected(test_devices_[1].GetDeviceId(), | 290 fake_active_host_->SetActiveHostConnected(test_devices_[1].GetDeviceId(), |
| 215 std::string(kTetherNetworkGuid), | 291 std::string(kTetherNetworkGuid), |
| 216 std::string(kWifiNetworkGuid)); | 292 std::string(kWifiNetworkGuid)); |
| 217 EXPECT_EQ(2u, fake_operation_factory_->num_created()); | 293 EXPECT_EQ(2u, fake_operation_factory_->num_created()); |
| 218 EXPECT_EQ(test_devices_[1], | 294 EXPECT_EQ(test_devices_[1], |
| 219 fake_operation_factory_->last_created()->remote_device()); | 295 fake_operation_factory_->last_created()->remote_device()); |
| 220 EXPECT_EQ(1u, fake_operation_factory_->num_deleted()); | 296 EXPECT_EQ(1u, fake_operation_factory_->num_deleted()); |
| 221 VerifyTimerRunning(true /* is_running */); | 297 VerifyTimerRunning(true /* is_running */); |
| 222 | 298 |
| 223 // Ensure that once the second operation is finished, it is deleted. | 299 // Ensure that once the second operation is finished, it is deleted. |
| 224 fake_operation_factory_->last_created()->SendOperationFinishedEvent(); | 300 SendOperationFinishedEventFromLastCreatedOperation( |
| 301 "cellProvider", 80 /* battery_percentage */, 4 /* connection_strength */); |
| 225 EXPECT_EQ(2u, fake_operation_factory_->num_created()); | 302 EXPECT_EQ(2u, fake_operation_factory_->num_created()); |
| 226 EXPECT_EQ(2u, fake_operation_factory_->num_deleted()); | 303 EXPECT_EQ(2u, fake_operation_factory_->num_deleted()); |
| 227 VerifyTimerRunning(true /* is_running */); | 304 VerifyTimerRunning(true /* is_running */); |
| 305 VerifyCacheUpdated(test_devices_[1], "cellProvider", |
| 306 80 /* battery_percentage */, 100 /* signal_strength */); |
| 228 | 307 |
| 229 // Disconnect that device. | 308 // Disconnect that device. |
| 230 fake_active_host_->SetActiveHostDisconnected(); | 309 fake_active_host_->SetActiveHostDisconnected(); |
| 231 EXPECT_EQ(2u, fake_operation_factory_->num_created()); | 310 EXPECT_EQ(2u, fake_operation_factory_->num_created()); |
| 232 EXPECT_EQ(2u, fake_operation_factory_->num_deleted()); | 311 EXPECT_EQ(2u, fake_operation_factory_->num_deleted()); |
| 233 VerifyTimerRunning(false /* is_running */); | 312 VerifyTimerRunning(false /* is_running */); |
| 234 } | 313 } |
| 235 | 314 |
| 236 } // namespace tether | 315 } // namespace tether |
| 237 | 316 |
| 238 } // namespace cryptauth | 317 } // namespace cryptauth |
| OLD | NEW |