| 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" |
| 15 #include "chromeos/components/tether/proto_test_util.h" |
| 13 #include "components/cryptauth/remote_device_test_util.h" | 16 #include "components/cryptauth/remote_device_test_util.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 18 |
| 16 namespace chromeos { | 19 namespace chromeos { |
| 17 | 20 |
| 18 namespace tether { | 21 namespace tether { |
| 19 | 22 |
| 20 namespace { | 23 namespace { |
| 21 | 24 |
| 22 const char kTetherNetworkGuid[] = "tetherNetworkGuid"; | 25 const char kTetherNetworkGuid[] = "tetherNetworkGuid"; |
| 23 const char kWifiNetworkGuid[] = "wifiNetworkGuid"; | 26 const char kWifiNetworkGuid[] = "wifiNetworkGuid"; |
| 24 | 27 |
| 25 class OperationDeletedHandler { | 28 class OperationDeletedHandler { |
| 26 public: | 29 public: |
| 27 virtual void OnOperationDeleted() = 0; | 30 virtual void OnOperationDeleted() = 0; |
| 28 }; | 31 }; |
| 29 | 32 |
| 30 class FakeKeepAliveOperation : public KeepAliveOperation { | 33 class FakeKeepAliveOperation : public KeepAliveOperation { |
| 31 public: | 34 public: |
| 32 FakeKeepAliveOperation(const cryptauth::RemoteDevice& device_to_connect, | 35 FakeKeepAliveOperation(const cryptauth::RemoteDevice& device_to_connect, |
| 33 BleConnectionManager* connection_manager, | 36 BleConnectionManager* connection_manager, |
| 34 OperationDeletedHandler* handler) | 37 OperationDeletedHandler* handler) |
| 35 : KeepAliveOperation(device_to_connect, connection_manager), | 38 : KeepAliveOperation(device_to_connect, connection_manager), |
| 36 handler_(handler), | 39 handler_(handler), |
| 37 remote_device_(device_to_connect) {} | 40 remote_device_(device_to_connect) {} |
| 38 | 41 |
| 39 ~FakeKeepAliveOperation() override { handler_->OnOperationDeleted(); } | 42 ~FakeKeepAliveOperation() override { handler_->OnOperationDeleted(); } |
| 40 | 43 |
| 41 void SendOperationFinishedEvent() { OnOperationFinished(); } | 44 void SendOperationFinishedEvent(std::unique_ptr<DeviceStatus> device_status) { |
| 45 device_status_ = std::move(device_status); |
| 46 OnOperationFinished(); |
| 47 } |
| 42 | 48 |
| 43 cryptauth::RemoteDevice remote_device() { return remote_device_; } | 49 cryptauth::RemoteDevice remote_device() { return remote_device_; } |
| 44 | 50 |
| 45 private: | 51 private: |
| 46 OperationDeletedHandler* handler_; | 52 OperationDeletedHandler* handler_; |
| 47 const cryptauth::RemoteDevice remote_device_; | 53 const cryptauth::RemoteDevice remote_device_; |
| 48 }; | 54 }; |
| 49 | 55 |
| 50 class FakeKeepAliveOperationFactory : public KeepAliveOperation::Factory, | 56 class FakeKeepAliveOperationFactory : public KeepAliveOperation::Factory, |
| 51 public OperationDeletedHandler { | 57 public OperationDeletedHandler { |
| (...skipping 29 matching lines...) Expand all Loading... |
| 81 } // namespace | 87 } // namespace |
| 82 | 88 |
| 83 class KeepAliveSchedulerTest : public testing::Test { | 89 class KeepAliveSchedulerTest : public testing::Test { |
| 84 protected: | 90 protected: |
| 85 KeepAliveSchedulerTest() | 91 KeepAliveSchedulerTest() |
| 86 : test_devices_(cryptauth::GenerateTestRemoteDevices(2)) {} | 92 : test_devices_(cryptauth::GenerateTestRemoteDevices(2)) {} |
| 87 | 93 |
| 88 void SetUp() override { | 94 void SetUp() override { |
| 89 fake_active_host_ = base::MakeUnique<FakeActiveHost>(); | 95 fake_active_host_ = base::MakeUnique<FakeActiveHost>(); |
| 90 fake_ble_connection_manager_ = base::MakeUnique<FakeBleConnectionManager>(); | 96 fake_ble_connection_manager_ = base::MakeUnique<FakeBleConnectionManager>(); |
| 97 fake_host_scan_cache_ = base::MakeUnique<FakeHostScanCache>(); |
| 98 device_id_tether_network_guid_map_ = |
| 99 base::MakeUnique<DeviceIdTetherNetworkGuidMap>(); |
| 91 mock_timer_ = new base::MockTimer(true /* retain_user_task */, | 100 mock_timer_ = new base::MockTimer(true /* retain_user_task */, |
| 92 true /* is_repeating */); | 101 true /* is_repeating */); |
| 93 | 102 |
| 94 fake_operation_factory_ = | 103 fake_operation_factory_ = |
| 95 base::WrapUnique(new FakeKeepAliveOperationFactory()); | 104 base::WrapUnique(new FakeKeepAliveOperationFactory()); |
| 96 KeepAliveOperation::Factory::SetInstanceForTesting( | 105 KeepAliveOperation::Factory::SetInstanceForTesting( |
| 97 fake_operation_factory_.get()); | 106 fake_operation_factory_.get()); |
| 98 | 107 |
| 99 scheduler_ = base::WrapUnique(new KeepAliveScheduler( | 108 scheduler_ = base::WrapUnique(new KeepAliveScheduler( |
| 100 fake_active_host_.get(), fake_ble_connection_manager_.get(), | 109 fake_active_host_.get(), fake_ble_connection_manager_.get(), |
| 110 fake_host_scan_cache_.get(), device_id_tether_network_guid_map_.get(), |
| 101 base::WrapUnique(mock_timer_))); | 111 base::WrapUnique(mock_timer_))); |
| 102 } | 112 } |
| 103 | 113 |
| 104 void VerifyTimerRunning(bool is_running) { | 114 void VerifyTimerRunning(bool is_running) { |
| 105 EXPECT_EQ(is_running, mock_timer_->IsRunning()); | 115 EXPECT_EQ(is_running, mock_timer_->IsRunning()); |
| 106 | 116 |
| 107 if (is_running) { | 117 if (is_running) { |
| 108 EXPECT_EQ(base::TimeDelta::FromMinutes( | 118 EXPECT_EQ(base::TimeDelta::FromMinutes( |
| 109 KeepAliveScheduler::kKeepAliveIntervalMinutes), | 119 KeepAliveScheduler::kKeepAliveIntervalMinutes), |
| 110 mock_timer_->GetCurrentDelay()); | 120 mock_timer_->GetCurrentDelay()); |
| 111 } | 121 } |
| 112 } | 122 } |
| 113 | 123 |
| 124 void SendOperationFinishedEventFromLastCreatedOperation( |
| 125 const std::string& cell_provider, |
| 126 int battery_percentage, |
| 127 int connection_strength) { |
| 128 fake_operation_factory_->last_created()->SendOperationFinishedEvent( |
| 129 base::MakeUnique<DeviceStatus>(CreateTestDeviceStatus( |
| 130 cell_provider, battery_percentage, connection_strength))); |
| 131 } |
| 132 |
| 133 void VerifyCacheUpdated(const cryptauth::RemoteDevice& remote_device, |
| 134 const std::string& carrier, |
| 135 int battery_percentage, |
| 136 int signal_strength) { |
| 137 const FakeHostScanCache::CacheEntry* entry = |
| 138 fake_host_scan_cache_->GetCacheEntry( |
| 139 device_id_tether_network_guid_map_->GetTetherNetworkGuidForDeviceId( |
| 140 remote_device.GetDeviceId())); |
| 141 ASSERT_TRUE(entry); |
| 142 EXPECT_EQ(carrier, entry->carrier); |
| 143 EXPECT_EQ(battery_percentage, entry->battery_percentage); |
| 144 EXPECT_EQ(signal_strength, entry->signal_strength); |
| 145 } |
| 146 |
| 114 const std::vector<cryptauth::RemoteDevice> test_devices_; | 147 const std::vector<cryptauth::RemoteDevice> test_devices_; |
| 115 | 148 |
| 116 std::unique_ptr<FakeActiveHost> fake_active_host_; | 149 std::unique_ptr<FakeActiveHost> fake_active_host_; |
| 117 std::unique_ptr<FakeBleConnectionManager> fake_ble_connection_manager_; | 150 std::unique_ptr<FakeBleConnectionManager> fake_ble_connection_manager_; |
| 151 std::unique_ptr<FakeHostScanCache> fake_host_scan_cache_; |
| 152 // TODO(hansberry): Use a fake for this when a real mapping scheme is created. |
| 153 std::unique_ptr<DeviceIdTetherNetworkGuidMap> |
| 154 device_id_tether_network_guid_map_; |
| 118 base::MockTimer* mock_timer_; | 155 base::MockTimer* mock_timer_; |
| 119 | 156 |
| 120 std::unique_ptr<FakeKeepAliveOperationFactory> fake_operation_factory_; | 157 std::unique_ptr<FakeKeepAliveOperationFactory> fake_operation_factory_; |
| 121 | 158 |
| 122 std::unique_ptr<KeepAliveScheduler> scheduler_; | 159 std::unique_ptr<KeepAliveScheduler> scheduler_; |
| 123 | 160 |
| 124 private: | 161 private: |
| 125 DISALLOW_COPY_AND_ASSIGN(KeepAliveSchedulerTest); | 162 DISALLOW_COPY_AND_ASSIGN(KeepAliveSchedulerTest); |
| 126 }; | 163 }; |
| 127 | 164 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 141 fake_active_host_->SetActiveHostConnected(test_devices_[0].GetDeviceId(), | 178 fake_active_host_->SetActiveHostConnected(test_devices_[0].GetDeviceId(), |
| 142 std::string(kTetherNetworkGuid), | 179 std::string(kTetherNetworkGuid), |
| 143 std::string(kWifiNetworkGuid)); | 180 std::string(kWifiNetworkGuid)); |
| 144 EXPECT_EQ(1u, fake_operation_factory_->num_created()); | 181 EXPECT_EQ(1u, fake_operation_factory_->num_created()); |
| 145 EXPECT_EQ(test_devices_[0], | 182 EXPECT_EQ(test_devices_[0], |
| 146 fake_operation_factory_->last_created()->remote_device()); | 183 fake_operation_factory_->last_created()->remote_device()); |
| 147 EXPECT_FALSE(fake_operation_factory_->num_deleted()); | 184 EXPECT_FALSE(fake_operation_factory_->num_deleted()); |
| 148 VerifyTimerRunning(true /* is_running */); | 185 VerifyTimerRunning(true /* is_running */); |
| 149 | 186 |
| 150 // Ensure that once the operation is finished, it is deleted. | 187 // Ensure that once the operation is finished, it is deleted. |
| 151 fake_operation_factory_->last_created()->SendOperationFinishedEvent(); | 188 SendOperationFinishedEventFromLastCreatedOperation( |
| 189 "cellProvider", 50 /* battery_percentage */, 2 /* connection_strength */); |
| 152 EXPECT_EQ(1u, fake_operation_factory_->num_created()); | 190 EXPECT_EQ(1u, fake_operation_factory_->num_created()); |
| 153 EXPECT_EQ(1u, fake_operation_factory_->num_deleted()); | 191 EXPECT_EQ(1u, fake_operation_factory_->num_deleted()); |
| 154 VerifyTimerRunning(true /* is_running */); | 192 VerifyTimerRunning(true /* is_running */); |
| 193 VerifyCacheUpdated(test_devices_[0], "cellProvider", |
| 194 50 /* battery_percentage */, 50 /* signal_strength */); |
| 155 | 195 |
| 156 // Fire the timer; this should result in another tickle being sent. | 196 // Fire the timer; this should result in tickle #2 being sent. |
| 157 mock_timer_->Fire(); | 197 mock_timer_->Fire(); |
| 158 EXPECT_EQ(2u, fake_operation_factory_->num_created()); | 198 EXPECT_EQ(2u, fake_operation_factory_->num_created()); |
| 159 EXPECT_EQ(test_devices_[0], | 199 EXPECT_EQ(test_devices_[0], |
| 160 fake_operation_factory_->last_created()->remote_device()); | 200 fake_operation_factory_->last_created()->remote_device()); |
| 161 EXPECT_EQ(1u, fake_operation_factory_->num_deleted()); | 201 EXPECT_EQ(1u, fake_operation_factory_->num_deleted()); |
| 162 VerifyTimerRunning(true /* is_running */); | 202 VerifyTimerRunning(true /* is_running */); |
| 163 | 203 |
| 164 // Finish this operation. | 204 // Finish tickle operation #2. |
| 165 fake_operation_factory_->last_created()->SendOperationFinishedEvent(); | 205 SendOperationFinishedEventFromLastCreatedOperation( |
| 206 "cellProvider", 40 /* battery_percentage */, 3 /* connection_strength */); |
| 166 EXPECT_EQ(2u, fake_operation_factory_->num_created()); | 207 EXPECT_EQ(2u, fake_operation_factory_->num_created()); |
| 167 EXPECT_EQ(2u, fake_operation_factory_->num_deleted()); | 208 EXPECT_EQ(2u, fake_operation_factory_->num_deleted()); |
| 168 VerifyTimerRunning(true /* is_running */); | 209 VerifyTimerRunning(true /* is_running */); |
| 210 VerifyCacheUpdated(test_devices_[0], "cellProvider", |
| 211 40 /* battery_percentage */, 75 /* signal_strength */); |
| 212 |
| 213 // Fire the timer; this should result in tickle #3 being sent. |
| 214 mock_timer_->Fire(); |
| 215 EXPECT_EQ(3u, fake_operation_factory_->num_created()); |
| 216 EXPECT_EQ(test_devices_[0], |
| 217 fake_operation_factory_->last_created()->remote_device()); |
| 218 EXPECT_EQ(2u, fake_operation_factory_->num_deleted()); |
| 219 VerifyTimerRunning(true /* is_running */); |
| 220 |
| 221 // Finish tickler operation #3. This time, simulate a failure to receive a |
| 222 // DeviceStatus back. |
| 223 fake_operation_factory_->last_created()->SendOperationFinishedEvent(nullptr); |
| 224 EXPECT_EQ(3u, fake_operation_factory_->num_created()); |
| 225 EXPECT_EQ(3u, fake_operation_factory_->num_deleted()); |
| 226 VerifyTimerRunning(true /* is_running */); |
| 227 // The same data returned by tickle #2 should be present. |
| 228 VerifyCacheUpdated(test_devices_[0], "cellProvider", |
| 229 40 /* battery_percentage */, 75 /* signal_strength */); |
| 169 | 230 |
| 170 // Disconnect that device. | 231 // Disconnect that device. |
| 171 fake_active_host_->SetActiveHostDisconnected(); | 232 fake_active_host_->SetActiveHostDisconnected(); |
| 172 EXPECT_EQ(2u, fake_operation_factory_->num_created()); | 233 EXPECT_EQ(3u, fake_operation_factory_->num_created()); |
| 173 EXPECT_EQ(2u, fake_operation_factory_->num_deleted()); | 234 EXPECT_EQ(3u, fake_operation_factory_->num_deleted()); |
| 174 VerifyTimerRunning(false /* is_running */); | 235 VerifyTimerRunning(false /* is_running */); |
| 175 } | 236 } |
| 176 | 237 |
| 177 TEST_F(KeepAliveSchedulerTest, TestSendTickle_MultipleActiveHosts) { | 238 TEST_F(KeepAliveSchedulerTest, TestSendTickle_MultipleActiveHosts) { |
| 178 EXPECT_FALSE(fake_operation_factory_->num_created()); | 239 EXPECT_FALSE(fake_operation_factory_->num_created()); |
| 179 EXPECT_FALSE(fake_operation_factory_->num_deleted()); | 240 EXPECT_FALSE(fake_operation_factory_->num_deleted()); |
| 180 VerifyTimerRunning(false /* is_running */); | 241 VerifyTimerRunning(false /* is_running */); |
| 181 | 242 |
| 182 // Start connecting to a device. No operation should be started. | 243 // Start connecting to a device. No operation should be started. |
| 183 fake_active_host_->SetActiveHostConnecting(test_devices_[0].GetDeviceId(), | 244 fake_active_host_->SetActiveHostConnecting(test_devices_[0].GetDeviceId(), |
| (...skipping 30 matching lines...) Expand all Loading... |
| 214 fake_active_host_->SetActiveHostConnected(test_devices_[1].GetDeviceId(), | 275 fake_active_host_->SetActiveHostConnected(test_devices_[1].GetDeviceId(), |
| 215 std::string(kTetherNetworkGuid), | 276 std::string(kTetherNetworkGuid), |
| 216 std::string(kWifiNetworkGuid)); | 277 std::string(kWifiNetworkGuid)); |
| 217 EXPECT_EQ(2u, fake_operation_factory_->num_created()); | 278 EXPECT_EQ(2u, fake_operation_factory_->num_created()); |
| 218 EXPECT_EQ(test_devices_[1], | 279 EXPECT_EQ(test_devices_[1], |
| 219 fake_operation_factory_->last_created()->remote_device()); | 280 fake_operation_factory_->last_created()->remote_device()); |
| 220 EXPECT_EQ(1u, fake_operation_factory_->num_deleted()); | 281 EXPECT_EQ(1u, fake_operation_factory_->num_deleted()); |
| 221 VerifyTimerRunning(true /* is_running */); | 282 VerifyTimerRunning(true /* is_running */); |
| 222 | 283 |
| 223 // Ensure that once the second operation is finished, it is deleted. | 284 // Ensure that once the second operation is finished, it is deleted. |
| 224 fake_operation_factory_->last_created()->SendOperationFinishedEvent(); | 285 SendOperationFinishedEventFromLastCreatedOperation( |
| 286 "cellProvider", 80 /* battery_percentage */, 4 /* connection_strength */); |
| 225 EXPECT_EQ(2u, fake_operation_factory_->num_created()); | 287 EXPECT_EQ(2u, fake_operation_factory_->num_created()); |
| 226 EXPECT_EQ(2u, fake_operation_factory_->num_deleted()); | 288 EXPECT_EQ(2u, fake_operation_factory_->num_deleted()); |
| 227 VerifyTimerRunning(true /* is_running */); | 289 VerifyTimerRunning(true /* is_running */); |
| 290 VerifyCacheUpdated(test_devices_[1], "cellProvider", |
| 291 80 /* battery_percentage */, 100 /* signal_strength */); |
| 228 | 292 |
| 229 // Disconnect that device. | 293 // Disconnect that device. |
| 230 fake_active_host_->SetActiveHostDisconnected(); | 294 fake_active_host_->SetActiveHostDisconnected(); |
| 231 EXPECT_EQ(2u, fake_operation_factory_->num_created()); | 295 EXPECT_EQ(2u, fake_operation_factory_->num_created()); |
| 232 EXPECT_EQ(2u, fake_operation_factory_->num_deleted()); | 296 EXPECT_EQ(2u, fake_operation_factory_->num_deleted()); |
| 233 VerifyTimerRunning(false /* is_running */); | 297 VerifyTimerRunning(false /* is_running */); |
| 234 } | 298 } |
| 235 | 299 |
| 236 } // namespace tether | 300 } // namespace tether |
| 237 | 301 |
| 238 } // namespace cryptauth | 302 } // namespace cryptauth |
| OLD | NEW |