| 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/host_scan_cache.h" | 5 #include "chromeos/components/tether/host_scan_cache.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <unordered_map> | 8 #include <unordered_map> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 51 const int kTetherBatteryPercentage0 = 20; | 51 const int kTetherBatteryPercentage0 = 20; |
| 52 const int kTetherBatteryPercentage1 = 40; | 52 const int kTetherBatteryPercentage1 = 40; |
| 53 const int kTetherBatteryPercentage2 = 60; | 53 const int kTetherBatteryPercentage2 = 60; |
| 54 const int kTetherBatteryPercentage3 = 80; | 54 const int kTetherBatteryPercentage3 = 80; |
| 55 | 55 |
| 56 const int kTetherSignalStrength0 = 25; | 56 const int kTetherSignalStrength0 = 25; |
| 57 const int kTetherSignalStrength1 = 50; | 57 const int kTetherSignalStrength1 = 50; |
| 58 const int kTetherSignalStrength2 = 75; | 58 const int kTetherSignalStrength2 = 75; |
| 59 const int kTetherSignalStrength3 = 100; | 59 const int kTetherSignalStrength3 = 100; |
| 60 | 60 |
| 61 const bool kTetherSetupRequired0 = true; |
| 62 const bool kTetherSetupRequired1 = false; |
| 63 const bool kTetherSetupRequired2 = true; |
| 64 const bool kTetherSetupRequired3 = false; |
| 65 |
| 61 // MockTimer which invokes a callback in its destructor. | 66 // MockTimer which invokes a callback in its destructor. |
| 62 class ExtendedMockTimer : public base::MockTimer { | 67 class ExtendedMockTimer : public base::MockTimer { |
| 63 public: | 68 public: |
| 64 explicit ExtendedMockTimer(const base::Closure& destructor_callback) | 69 explicit ExtendedMockTimer(const base::Closure& destructor_callback) |
| 65 : base::MockTimer(true /* retain_user_task */, false /* is_repeating */), | 70 : base::MockTimer(true /* retain_user_task */, false /* is_repeating */), |
| 66 destructor_callback_(destructor_callback) {} | 71 destructor_callback_(destructor_callback) {} |
| 67 | 72 |
| 68 ~ExtendedMockTimer() override { destructor_callback_.Run(); } | 73 ~ExtendedMockTimer() override { destructor_callback_.Run(); } |
| 69 | 74 |
| 70 private: | 75 private: |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 198 // at the top of this test file. | 203 // at the top of this test file. |
| 199 void SetCacheScanResultForDeviceIndex(int32_t index) { | 204 void SetCacheScanResultForDeviceIndex(int32_t index) { |
| 200 // There are 4 sets of test constants. | 205 // There are 4 sets of test constants. |
| 201 ASSERT_TRUE(index >= 0 && index <= 3); | 206 ASSERT_TRUE(index >= 0 && index <= 3); |
| 202 | 207 |
| 203 std::string tether_network_guid; | 208 std::string tether_network_guid; |
| 204 std::string device_name; | 209 std::string device_name; |
| 205 std::string carrier; | 210 std::string carrier; |
| 206 int battery_percentage; | 211 int battery_percentage; |
| 207 int signal_strength; | 212 int signal_strength; |
| 213 bool setup_required; |
| 208 | 214 |
| 209 switch (index) { | 215 switch (index) { |
| 210 case 0: | 216 case 0: |
| 211 tether_network_guid = kTetherGuid0; | 217 tether_network_guid = kTetherGuid0; |
| 212 device_name = kTetherDeviceName0; | 218 device_name = kTetherDeviceName0; |
| 213 carrier = kTetherCarrier0; | 219 carrier = kTetherCarrier0; |
| 214 battery_percentage = kTetherBatteryPercentage0; | 220 battery_percentage = kTetherBatteryPercentage0; |
| 215 signal_strength = kTetherSignalStrength0; | 221 signal_strength = kTetherSignalStrength0; |
| 222 setup_required = kTetherSetupRequired0; |
| 216 break; | 223 break; |
| 217 case 1: | 224 case 1: |
| 218 tether_network_guid = kTetherGuid1; | 225 tether_network_guid = kTetherGuid1; |
| 219 device_name = kTetherDeviceName1; | 226 device_name = kTetherDeviceName1; |
| 220 carrier = kTetherCarrier1; | 227 carrier = kTetherCarrier1; |
| 221 battery_percentage = kTetherBatteryPercentage1; | 228 battery_percentage = kTetherBatteryPercentage1; |
| 222 signal_strength = kTetherSignalStrength1; | 229 signal_strength = kTetherSignalStrength1; |
| 230 setup_required = kTetherSetupRequired1; |
| 223 break; | 231 break; |
| 224 case 2: | 232 case 2: |
| 225 tether_network_guid = kTetherGuid2; | 233 tether_network_guid = kTetherGuid2; |
| 226 device_name = kTetherDeviceName2; | 234 device_name = kTetherDeviceName2; |
| 227 carrier = kTetherCarrier2; | 235 carrier = kTetherCarrier2; |
| 228 battery_percentage = kTetherBatteryPercentage2; | 236 battery_percentage = kTetherBatteryPercentage2; |
| 229 signal_strength = kTetherSignalStrength2; | 237 signal_strength = kTetherSignalStrength2; |
| 238 setup_required = kTetherSetupRequired2; |
| 230 break; | 239 break; |
| 231 case 3: | 240 case 3: |
| 232 tether_network_guid = kTetherGuid3; | 241 tether_network_guid = kTetherGuid3; |
| 233 device_name = kTetherDeviceName3; | 242 device_name = kTetherDeviceName3; |
| 234 carrier = kTetherCarrier3; | 243 carrier = kTetherCarrier3; |
| 235 battery_percentage = kTetherBatteryPercentage3; | 244 battery_percentage = kTetherBatteryPercentage3; |
| 236 signal_strength = kTetherSignalStrength3; | 245 signal_strength = kTetherSignalStrength3; |
| 246 setup_required = kTetherSetupRequired3; |
| 237 break; | 247 break; |
| 238 default: | 248 default: |
| 239 NOTREACHED(); | 249 NOTREACHED(); |
| 240 // Set values for |battery_percentage| and |signal_strength| here to | 250 // Set values for |battery_percentage|, |signal_strength| and |
| 241 // prevent a compiler warning which says that they may be unset at this | 251 // |setup_required| here to prevent a compiler warning which says that |
| 242 // point. | 252 // they may be unset at this point. |
| 243 battery_percentage = 0; | 253 battery_percentage = 0; |
| 244 signal_strength = 0; | 254 signal_strength = 0; |
| 255 setup_required = false; |
| 245 break; | 256 break; |
| 246 } | 257 } |
| 247 | 258 |
| 248 SetHostScanResult(tether_network_guid, device_name, carrier, | 259 SetHostScanResult(tether_network_guid, device_name, carrier, |
| 249 battery_percentage, signal_strength); | 260 battery_percentage, signal_strength, setup_required); |
| 250 } | 261 } |
| 251 | 262 |
| 252 void SetHostScanResult(const std::string& tether_network_guid, | 263 void SetHostScanResult(const std::string& tether_network_guid, |
| 253 const std::string& device_name, | 264 const std::string& device_name, |
| 254 const std::string& carrier, | 265 const std::string& carrier, |
| 255 int battery_percentage, | 266 int battery_percentage, |
| 256 int signal_strength) { | 267 int signal_strength, |
| 268 bool setup_required) { |
| 257 test_timer_factory_->set_tether_network_guid_for_next_timer( | 269 test_timer_factory_->set_tether_network_guid_for_next_timer( |
| 258 tether_network_guid); | 270 tether_network_guid); |
| 259 host_scan_cache_->SetHostScanResult(tether_network_guid, device_name, | 271 host_scan_cache_->SetHostScanResult(tether_network_guid, device_name, |
| 260 carrier, battery_percentage, | 272 carrier, battery_percentage, |
| 261 signal_strength); | 273 signal_strength, setup_required); |
| 262 expected_cache_->SetHostScanResult(tether_network_guid, device_name, | 274 expected_cache_->SetHostScanResult(tether_network_guid, device_name, |
| 263 carrier, battery_percentage, | 275 carrier, battery_percentage, |
| 264 signal_strength); | 276 signal_strength, setup_required); |
| 265 } | 277 } |
| 266 | 278 |
| 267 void RemoveHostScanResult(const std::string& tether_network_guid) { | 279 void RemoveHostScanResult(const std::string& tether_network_guid) { |
| 268 host_scan_cache_->RemoveHostScanResult(tether_network_guid); | 280 host_scan_cache_->RemoveHostScanResult(tether_network_guid); |
| 269 expected_cache_->RemoveHostScanResult(tether_network_guid); | 281 expected_cache_->RemoveHostScanResult(tether_network_guid); |
| 270 } | 282 } |
| 271 | 283 |
| 272 void ClearCacheExceptForActiveHost() { | 284 void ClearCacheExceptForActiveHost() { |
| 273 host_scan_cache_->ClearCacheExceptForActiveHost(); | 285 host_scan_cache_->ClearCacheExceptForActiveHost(); |
| 274 expected_cache_->ClearCacheExceptForActiveHost(); | 286 expected_cache_->ClearCacheExceptForActiveHost(); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 293 // corresponding entry in NetworkStateHandler. | 305 // corresponding entry in NetworkStateHandler. |
| 294 const NetworkState* tether_network_state = | 306 const NetworkState* tether_network_state = |
| 295 network_state_handler()->GetNetworkStateFromGuid(tether_network_guid); | 307 network_state_handler()->GetNetworkStateFromGuid(tether_network_guid); |
| 296 ASSERT_TRUE(tether_network_state); | 308 ASSERT_TRUE(tether_network_state); |
| 297 EXPECT_EQ(cache_entry.device_name, tether_network_state->name()); | 309 EXPECT_EQ(cache_entry.device_name, tether_network_state->name()); |
| 298 EXPECT_EQ(cache_entry.carrier, tether_network_state->carrier()); | 310 EXPECT_EQ(cache_entry.carrier, tether_network_state->carrier()); |
| 299 EXPECT_EQ(cache_entry.battery_percentage, | 311 EXPECT_EQ(cache_entry.battery_percentage, |
| 300 tether_network_state->battery_percentage()); | 312 tether_network_state->battery_percentage()); |
| 301 EXPECT_EQ(cache_entry.signal_strength, | 313 EXPECT_EQ(cache_entry.signal_strength, |
| 302 tether_network_state->signal_strength()); | 314 tether_network_state->signal_strength()); |
| 315 EXPECT_EQ(cache_entry.setup_required, |
| 316 host_scan_cache_->DoesHostRequireSetup(tether_network_guid)); |
| 303 EXPECT_EQ(HasConnectedToHost(tether_network_guid), | 317 EXPECT_EQ(HasConnectedToHost(tether_network_guid), |
| 304 tether_network_state->tether_has_connected_to_host()); | 318 tether_network_state->tether_has_connected_to_host()); |
| 305 | 319 |
| 306 // Ensure that each entry has an actively-running Timer. | 320 // Ensure that each entry has an actively-running Timer. |
| 307 auto timer_map_it = | 321 auto timer_map_it = |
| 308 test_timer_factory_->tether_network_guid_to_timer_map().begin(); | 322 test_timer_factory_->tether_network_guid_to_timer_map().begin(); |
| 309 EXPECT_NE(timer_map_it, | 323 EXPECT_NE(timer_map_it, |
| 310 test_timer_factory_->tether_network_guid_to_timer_map().end()); | 324 test_timer_factory_->tether_network_guid_to_timer_map().end()); |
| 311 EXPECT_TRUE(timer_map_it->second->IsRunning()); | 325 EXPECT_TRUE(timer_map_it->second->IsRunning()); |
| 312 } | 326 } |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 366 } | 380 } |
| 367 | 381 |
| 368 TEST_F(HostScanCacheTest, TestSetScanResultThenUpdateAndRemove) { | 382 TEST_F(HostScanCacheTest, TestSetScanResultThenUpdateAndRemove) { |
| 369 SetCacheScanResultForDeviceIndex(0); | 383 SetCacheScanResultForDeviceIndex(0); |
| 370 EXPECT_EQ(1u, expected_cache_->size()); | 384 EXPECT_EQ(1u, expected_cache_->size()); |
| 371 VerifyCacheMatchesNetworkStack(); | 385 VerifyCacheMatchesNetworkStack(); |
| 372 | 386 |
| 373 // Change the fields for tether network with GUID |kTetherGuid0| to the | 387 // Change the fields for tether network with GUID |kTetherGuid0| to the |
| 374 // fields corresponding to |kTetherGuid1|. | 388 // fields corresponding to |kTetherGuid1|. |
| 375 SetHostScanResult(kTetherGuid0, kTetherDeviceName0, kTetherCarrier1, | 389 SetHostScanResult(kTetherGuid0, kTetherDeviceName0, kTetherCarrier1, |
| 376 kTetherBatteryPercentage1, kTetherSignalStrength1); | 390 kTetherBatteryPercentage1, kTetherSignalStrength1, |
| 391 kTetherSetupRequired1); |
| 377 EXPECT_EQ(1u, expected_cache_->size()); | 392 EXPECT_EQ(1u, expected_cache_->size()); |
| 378 VerifyCacheMatchesNetworkStack(); | 393 VerifyCacheMatchesNetworkStack(); |
| 379 | 394 |
| 380 // Now, remove that result. | 395 // Now, remove that result. |
| 381 RemoveHostScanResult(kTetherGuid0); | 396 RemoveHostScanResult(kTetherGuid0); |
| 382 EXPECT_TRUE(expected_cache_->empty()); | 397 EXPECT_TRUE(expected_cache_->empty()); |
| 383 VerifyCacheMatchesNetworkStack(); | 398 VerifyCacheMatchesNetworkStack(); |
| 384 } | 399 } |
| 385 | 400 |
| 386 TEST_F(HostScanCacheTest, TestSetScanResult_SetActiveHost_ThenClear) { | 401 TEST_F(HostScanCacheTest, TestSetScanResult_SetActiveHost_ThenClear) { |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 439 | 454 |
| 440 // Simulate a connection to device 1. | 455 // Simulate a connection to device 1. |
| 441 SetActiveHost(kTetherGuid1); | 456 SetActiveHost(kTetherGuid1); |
| 442 SetHasConnectedToHost(kTetherGuid1); | 457 SetHasConnectedToHost(kTetherGuid1); |
| 443 VerifyCacheMatchesNetworkStack(); | 458 VerifyCacheMatchesNetworkStack(); |
| 444 } | 459 } |
| 445 | 460 |
| 446 } // namespace tether | 461 } // namespace tether |
| 447 | 462 |
| 448 } // namespace chromeos | 463 } // namespace chromeos |
| OLD | NEW |