| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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_scanner.h" | 5 #include "chromeos/components/tether/host_scanner.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
| 12 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
| 13 #include "base/test/scoped_task_environment.h" | 13 #include "base/test/scoped_task_environment.h" |
| 14 #include "chromeos/components/tether/device_id_tether_network_guid_map.h" | 14 #include "chromeos/components/tether/device_id_tether_network_guid_map.h" |
| 15 #include "chromeos/components/tether/fake_ble_connection_manager.h" | 15 #include "chromeos/components/tether/fake_ble_connection_manager.h" |
| 16 #include "chromeos/components/tether/fake_host_scan_cache.h" | |
| 17 #include "chromeos/components/tether/fake_notification_presenter.h" | 16 #include "chromeos/components/tether/fake_notification_presenter.h" |
| 18 #include "chromeos/components/tether/fake_tether_host_fetcher.h" | 17 #include "chromeos/components/tether/fake_tether_host_fetcher.h" |
| 19 #include "chromeos/components/tether/host_scan_device_prioritizer.h" | 18 #include "chromeos/components/tether/host_scan_device_prioritizer.h" |
| 20 #include "chromeos/components/tether/host_scanner.h" | 19 #include "chromeos/components/tether/host_scanner.h" |
| 21 #include "chromeos/components/tether/mock_tether_host_response_recorder.h" | 20 #include "chromeos/components/tether/mock_tether_host_response_recorder.h" |
| 22 #include "chromeos/dbus/dbus_thread_manager.h" | 21 #include "chromeos/dbus/dbus_thread_manager.h" |
| 22 #include "chromeos/network/network_state.h" |
| 23 #include "chromeos/network/network_state_handler.h" |
| 24 #include "chromeos/network/network_state_test.h" |
| 25 #include "chromeos/network/network_type_pattern.h" |
| 23 #include "components/cryptauth/remote_device_test_util.h" | 26 #include "components/cryptauth/remote_device_test_util.h" |
| 24 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
| 25 | 28 |
| 26 namespace chromeos { | 29 namespace chromeos { |
| 27 | 30 |
| 28 namespace tether { | 31 namespace tether { |
| 29 | 32 |
| 30 namespace { | 33 namespace { |
| 31 | 34 |
| 32 class FakeHostScanDevicePrioritizer : public HostScanDevicePrioritizer { | 35 class FakeHostScanDevicePrioritizer : public HostScanDevicePrioritizer { |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 const std::vector<cryptauth::RemoteDevice>& expected_devices_; | 95 const std::vector<cryptauth::RemoteDevice>& expected_devices_; |
| 93 std::vector<FakeHostScannerOperation*> created_operations_; | 96 std::vector<FakeHostScannerOperation*> created_operations_; |
| 94 }; | 97 }; |
| 95 | 98 |
| 96 std::string GenerateCellProviderForDevice( | 99 std::string GenerateCellProviderForDevice( |
| 97 const cryptauth::RemoteDevice& remote_device) { | 100 const cryptauth::RemoteDevice& remote_device) { |
| 98 // Return a string unique to |remote_device|. | 101 // Return a string unique to |remote_device|. |
| 99 return "cellProvider" + remote_device.GetTruncatedDeviceIdForLogs(); | 102 return "cellProvider" + remote_device.GetTruncatedDeviceIdForLogs(); |
| 100 } | 103 } |
| 101 | 104 |
| 102 const char kDoNotSetStringField[] = "doNotSetField"; | 105 DeviceStatus CreateFakeDeviceStatus(const std::string& cell_provider_name) { |
| 103 const int kDoNotSetIntField = -100; | |
| 104 | |
| 105 // Creates a DeviceStatus object using the parameters provided. If | |
| 106 // |kDoNotSetStringField| or |kDoNotSetIntField| are passed, these fields will | |
| 107 // not be set in the output. | |
| 108 DeviceStatus CreateFakeDeviceStatus(const std::string& cell_provider_name, | |
| 109 int battery_percentage, | |
| 110 int connection_strength) { | |
| 111 // TODO(khorimoto): Once a ConnectedWifiSsid field is added as a property of | |
| 112 // Tether networks, give an option to pass a parameter for that field as well. | |
| 113 WifiStatus wifi_status; | 106 WifiStatus wifi_status; |
| 114 wifi_status.set_status_code( | 107 wifi_status.set_status_code( |
| 115 WifiStatus_StatusCode::WifiStatus_StatusCode_CONNECTED); | 108 WifiStatus_StatusCode::WifiStatus_StatusCode_CONNECTED); |
| 116 wifi_status.set_ssid("Google A"); | 109 wifi_status.set_ssid("Google A"); |
| 117 | 110 |
| 118 DeviceStatus device_status; | 111 DeviceStatus device_status; |
| 119 if (battery_percentage != kDoNotSetIntField) { | 112 device_status.set_battery_percentage(75); |
| 120 device_status.set_battery_percentage(battery_percentage); | 113 device_status.set_cell_provider(cell_provider_name); |
| 121 } | 114 device_status.set_connection_strength(4); |
| 122 if (cell_provider_name != kDoNotSetStringField) { | |
| 123 device_status.set_cell_provider(cell_provider_name); | |
| 124 } | |
| 125 if (connection_strength != kDoNotSetIntField) { | |
| 126 device_status.set_connection_strength(connection_strength); | |
| 127 } | |
| 128 | |
| 129 device_status.mutable_wifi_status()->CopyFrom(wifi_status); | 115 device_status.mutable_wifi_status()->CopyFrom(wifi_status); |
| 130 | 116 |
| 131 return device_status; | 117 return device_status; |
| 132 } | 118 } |
| 133 | 119 |
| 134 std::vector<HostScannerOperation::ScannedDeviceInfo> | 120 std::vector<HostScannerOperation::ScannedDeviceInfo> |
| 135 CreateFakeScannedDeviceInfos( | 121 CreateFakeScannedDeviceInfos( |
| 136 const std::vector<cryptauth::RemoteDevice>& remote_devices) { | 122 const std::vector<cryptauth::RemoteDevice>& remote_devices) { |
| 137 // At least 4 ScannedDeviceInfos should be created to ensure that all 4 cases | |
| 138 // described below are tested. | |
| 139 EXPECT_GT(remote_devices.size(), 3u); | |
| 140 | |
| 141 std::vector<HostScannerOperation::ScannedDeviceInfo> scanned_device_infos; | 123 std::vector<HostScannerOperation::ScannedDeviceInfo> scanned_device_infos; |
| 142 | |
| 143 for (size_t i = 0; i < remote_devices.size(); ++i) { | 124 for (size_t i = 0; i < remote_devices.size(); ++i) { |
| 144 // Four field possibilities: | |
| 145 // i % 4 == 0: Field is not supplied. | |
| 146 // i % 4 == 1: Field is below the minimum value (int fields only). | |
| 147 // i % 4 == 2: Field is within the valid range (int fields only). | |
| 148 // i % 4 == 3: Field is above the maximium value (int fields only). | |
| 149 std::string cell_provider_name; | |
| 150 int battery_percentage; | |
| 151 int connection_strength; | |
| 152 switch (i % 4) { | |
| 153 case 0: | |
| 154 cell_provider_name = kDoNotSetStringField; | |
| 155 battery_percentage = kDoNotSetIntField; | |
| 156 connection_strength = kDoNotSetIntField; | |
| 157 break; | |
| 158 case 1: | |
| 159 cell_provider_name = GenerateCellProviderForDevice(remote_devices[i]); | |
| 160 battery_percentage = -1 - i; | |
| 161 connection_strength = -1 - i; | |
| 162 break; | |
| 163 case 2: | |
| 164 cell_provider_name = GenerateCellProviderForDevice(remote_devices[i]); | |
| 165 battery_percentage = (50 + i) % 100; // Valid range is [0, 100]. | |
| 166 connection_strength = (1 + i) % 4; // Valid range is [0, 4]. | |
| 167 break; | |
| 168 case 3: | |
| 169 cell_provider_name = GenerateCellProviderForDevice(remote_devices[i]); | |
| 170 battery_percentage = 101 + i; | |
| 171 connection_strength = 101 + i; | |
| 172 break; | |
| 173 default: | |
| 174 NOTREACHED(); | |
| 175 // Set values for |battery_percentage| and |connection_strength| here to | |
| 176 // prevent a compiler warning which says that they may be unset at this | |
| 177 // point. | |
| 178 battery_percentage = 0; | |
| 179 connection_strength = 0; | |
| 180 break; | |
| 181 } | |
| 182 | |
| 183 DeviceStatus device_status = CreateFakeDeviceStatus( | 125 DeviceStatus device_status = CreateFakeDeviceStatus( |
| 184 cell_provider_name, battery_percentage, connection_strength); | 126 GenerateCellProviderForDevice(remote_devices[i])); |
| 185 | |
| 186 // Require set-up for odd-numbered device indices. | 127 // Require set-up for odd-numbered device indices. |
| 187 bool set_up_required = i % 2 == 0; | 128 bool set_up_required = i % 2 == 0; |
| 188 | |
| 189 scanned_device_infos.push_back(HostScannerOperation::ScannedDeviceInfo( | 129 scanned_device_infos.push_back(HostScannerOperation::ScannedDeviceInfo( |
| 190 remote_devices[i], device_status, set_up_required)); | 130 remote_devices[i], device_status, set_up_required)); |
| 191 } | 131 } |
| 192 | |
| 193 return scanned_device_infos; | 132 return scanned_device_infos; |
| 194 } | 133 } |
| 195 | 134 |
| 196 } // namespace | 135 } // namespace |
| 197 | 136 |
| 198 class HostScannerTest : public testing::Test { | 137 class HostScannerTest : public NetworkStateTest { |
| 199 protected: | 138 protected: |
| 200 HostScannerTest() | 139 HostScannerTest() |
| 201 : test_devices_(cryptauth::GenerateTestRemoteDevices(4)), | 140 : test_devices_(cryptauth::GenerateTestRemoteDevices(4)), |
| 202 test_scanned_device_infos(CreateFakeScannedDeviceInfos(test_devices_)) { | 141 test_scanned_device_infos(CreateFakeScannedDeviceInfos(test_devices_)) { |
| 203 } | 142 } |
| 204 | 143 |
| 205 void SetUp() override { | 144 void SetUp() override { |
| 145 DBusThreadManager::Initialize(); |
| 146 NetworkStateTest::SetUp(); |
| 147 network_state_handler()->SetTetherTechnologyState( |
| 148 NetworkStateHandler::TECHNOLOGY_ENABLED); |
| 149 |
| 206 scanned_device_infos_so_far_.clear(); | 150 scanned_device_infos_so_far_.clear(); |
| 207 | 151 |
| 208 fake_tether_host_fetcher_ = base::MakeUnique<FakeTetherHostFetcher>( | 152 fake_tether_host_fetcher_ = base::MakeUnique<FakeTetherHostFetcher>( |
| 209 test_devices_, false /* synchronously_reply_with_results */); | 153 test_devices_, false /* synchronously_reply_with_results */); |
| 210 fake_ble_connection_manager_ = base::MakeUnique<FakeBleConnectionManager>(); | 154 fake_ble_connection_manager_ = base::MakeUnique<FakeBleConnectionManager>(); |
| 211 fake_host_scan_device_prioritizer_ = | 155 fake_host_scan_device_prioritizer_ = |
| 212 base::MakeUnique<FakeHostScanDevicePrioritizer>(); | 156 base::MakeUnique<FakeHostScanDevicePrioritizer>(); |
| 213 mock_tether_host_response_recorder_ = | 157 mock_tether_host_response_recorder_ = |
| 214 base::MakeUnique<MockTetherHostResponseRecorder>(); | 158 base::MakeUnique<MockTetherHostResponseRecorder>(); |
| 215 fake_notification_presenter_ = | 159 fake_notification_presenter_ = |
| 216 base::MakeUnique<FakeNotificationPresenter>(); | 160 base::MakeUnique<FakeNotificationPresenter>(); |
| 217 device_id_tether_network_guid_map_ = | 161 device_id_tether_network_guid_map_ = |
| 218 base::MakeUnique<DeviceIdTetherNetworkGuidMap>(); | 162 base::MakeUnique<DeviceIdTetherNetworkGuidMap>(); |
| 219 fake_host_scan_cache_ = base::MakeUnique<FakeHostScanCache>(); | |
| 220 | 163 |
| 221 fake_host_scanner_operation_factory_ = | 164 fake_host_scanner_operation_factory_ = |
| 222 base::WrapUnique(new FakeHostScannerOperationFactory(test_devices_)); | 165 base::WrapUnique(new FakeHostScannerOperationFactory(test_devices_)); |
| 223 HostScannerOperation::Factory::SetInstanceForTesting( | 166 HostScannerOperation::Factory::SetInstanceForTesting( |
| 224 fake_host_scanner_operation_factory_.get()); | 167 fake_host_scanner_operation_factory_.get()); |
| 225 | 168 |
| 226 host_scanner_ = base::WrapUnique(new HostScanner( | 169 host_scanner_ = base::WrapUnique(new HostScanner( |
| 227 fake_tether_host_fetcher_.get(), fake_ble_connection_manager_.get(), | 170 fake_tether_host_fetcher_.get(), fake_ble_connection_manager_.get(), |
| 228 fake_host_scan_device_prioritizer_.get(), | 171 fake_host_scan_device_prioritizer_.get(), |
| 229 mock_tether_host_response_recorder_.get(), | 172 mock_tether_host_response_recorder_.get(), network_state_handler(), |
| 230 fake_notification_presenter_.get(), | 173 fake_notification_presenter_.get(), |
| 231 device_id_tether_network_guid_map_.get(), fake_host_scan_cache_.get())); | 174 device_id_tether_network_guid_map_.get())); |
| 232 } | 175 } |
| 233 | 176 |
| 234 void TearDown() override { | 177 void TearDown() override { |
| 178 ShutdownNetworkState(); |
| 179 NetworkStateTest::TearDown(); |
| 180 DBusThreadManager::Shutdown(); |
| 181 |
| 235 HostScannerOperation::Factory::SetInstanceForTesting(nullptr); | 182 HostScannerOperation::Factory::SetInstanceForTesting(nullptr); |
| 236 } | 183 } |
| 237 | 184 |
| 238 // Causes |fake_operation| to receive the scan result in | 185 // Causes |fake_operation| to receive the scan result in |
| 239 // |test_scanned_device_infos| vector at the index |test_device_index| with | 186 // |test_scanned_device_infos| vector at the index |test_device_index| with |
| 240 // the "final result" value of |is_final_scan_result|. | 187 // the "final result" value of |is_final_scan_result|. |
| 241 void ReceiveScanResultAndVerifySuccess( | 188 void ReceiveScanResultAndVerifySuccess( |
| 242 FakeHostScannerOperation& fake_operation, | 189 FakeHostScannerOperation* fake_operation, |
| 243 size_t test_device_index, | 190 size_t test_device_index, |
| 244 bool is_final_scan_result) { | 191 bool is_final_scan_result) { |
| 245 bool already_in_list = false; | 192 scanned_device_infos_so_far_.push_back( |
| 193 test_scanned_device_infos[test_device_index]); |
| 194 fake_operation->SendScannedDeviceListUpdate(scanned_device_infos_so_far_, |
| 195 is_final_scan_result); |
| 196 EXPECT_EQ(scanned_device_infos_so_far_, |
| 197 host_scanner_->most_recent_scan_results()); |
| 198 |
| 199 NetworkStateHandler::NetworkStateList tether_networks; |
| 200 network_state_handler()->GetVisibleNetworkListByType( |
| 201 NetworkTypePattern::Tether(), &tether_networks); |
| 202 EXPECT_EQ(scanned_device_infos_so_far_.size(), tether_networks.size()); |
| 246 for (auto& scanned_device_info : scanned_device_infos_so_far_) { | 203 for (auto& scanned_device_info : scanned_device_infos_so_far_) { |
| 247 if (scanned_device_info.remote_device.GetDeviceId() == | 204 cryptauth::RemoteDevice remote_device = scanned_device_info.remote_device; |
| 248 test_devices_[test_device_index].GetDeviceId()) { | 205 const NetworkState* tether_network = |
| 249 already_in_list = true; | 206 network_state_handler()->GetNetworkStateFromGuid( |
| 250 break; | 207 remote_device.GetDeviceId()); |
| 251 } | 208 ASSERT_TRUE(tether_network); |
| 209 EXPECT_EQ(remote_device.name, tether_network->name()); |
| 252 } | 210 } |
| 253 | 211 |
| 254 if (!already_in_list) { | |
| 255 scanned_device_infos_so_far_.push_back( | |
| 256 test_scanned_device_infos[test_device_index]); | |
| 257 } | |
| 258 | |
| 259 fake_operation.SendScannedDeviceListUpdate(scanned_device_infos_so_far_, | |
| 260 is_final_scan_result); | |
| 261 VerifyScanResultsMatchCache(); | |
| 262 | |
| 263 if (scanned_device_infos_so_far_.size() == 1) { | 212 if (scanned_device_infos_so_far_.size() == 1) { |
| 264 EXPECT_EQ(FakeNotificationPresenter::PotentialHotspotNotificationState:: | 213 EXPECT_EQ(FakeNotificationPresenter::PotentialHotspotNotificationState:: |
| 265 SINGLE_HOTSPOT_NEARBY_SHOWN, | 214 SINGLE_HOTSPOT_NEARBY_SHOWN, |
| 266 fake_notification_presenter_->potential_hotspot_state()); | 215 fake_notification_presenter_->potential_hotspot_state()); |
| 267 } else { | 216 } else { |
| 268 EXPECT_EQ(FakeNotificationPresenter::PotentialHotspotNotificationState:: | 217 EXPECT_EQ(FakeNotificationPresenter::PotentialHotspotNotificationState:: |
| 269 MULTIPLE_HOTSPOTS_NEARBY_SHOWN, | 218 MULTIPLE_HOTSPOTS_NEARBY_SHOWN, |
| 270 fake_notification_presenter_->potential_hotspot_state()); | 219 fake_notification_presenter_->potential_hotspot_state()); |
| 271 } | 220 } |
| 272 } | 221 } |
| 273 | 222 |
| 274 void VerifyScanResultsMatchCache() { | 223 base::test::ScopedTaskEnvironment scoped_task_environment_; |
| 275 ASSERT_EQ(scanned_device_infos_so_far_.size(), | |
| 276 fake_host_scan_cache_->size()); | |
| 277 for (auto& scanned_device_info : scanned_device_infos_so_far_) { | |
| 278 std::string tether_network_guid = | |
| 279 device_id_tether_network_guid_map_->GetTetherNetworkGuidForDeviceId( | |
| 280 scanned_device_info.remote_device.GetDeviceId()); | |
| 281 const FakeHostScanCache::CacheEntry* cache_item = | |
| 282 fake_host_scan_cache_->GetCacheEntry(tether_network_guid); | |
| 283 ASSERT_TRUE(cache_item); | |
| 284 VerifyScannedDeviceInfoAndCacheEntryAreEquivalent(scanned_device_info, | |
| 285 *cache_item); | |
| 286 } | |
| 287 } | |
| 288 | |
| 289 void VerifyScannedDeviceInfoAndCacheEntryAreEquivalent( | |
| 290 const HostScannerOperation::ScannedDeviceInfo& scanned_device_info, | |
| 291 const FakeHostScanCache::CacheEntry& cache_item) { | |
| 292 EXPECT_EQ(scanned_device_info.remote_device.name, cache_item.device_name); | |
| 293 | |
| 294 const DeviceStatus& status = scanned_device_info.device_status; | |
| 295 if (!status.has_cell_provider() || status.cell_provider().empty()) | |
| 296 EXPECT_EQ("unknown-carrier", cache_item.carrier); | |
| 297 else | |
| 298 EXPECT_EQ(status.cell_provider(), cache_item.carrier); | |
| 299 | |
| 300 if (!status.has_battery_percentage() || status.battery_percentage() > 100) | |
| 301 EXPECT_EQ(100, cache_item.battery_percentage); | |
| 302 else if (status.battery_percentage() < 0) | |
| 303 EXPECT_EQ(0, cache_item.battery_percentage); | |
| 304 else | |
| 305 EXPECT_EQ(status.battery_percentage(), cache_item.battery_percentage); | |
| 306 | |
| 307 if (!status.has_connection_strength() || status.connection_strength() > 4) | |
| 308 EXPECT_EQ(100, cache_item.signal_strength); | |
| 309 else if (status.connection_strength() < 0) | |
| 310 EXPECT_EQ(0, cache_item.signal_strength); | |
| 311 else | |
| 312 EXPECT_EQ(status.connection_strength() * 25, cache_item.signal_strength); | |
| 313 } | |
| 314 | 224 |
| 315 const std::vector<cryptauth::RemoteDevice> test_devices_; | 225 const std::vector<cryptauth::RemoteDevice> test_devices_; |
| 316 const std::vector<HostScannerOperation::ScannedDeviceInfo> | 226 const std::vector<HostScannerOperation::ScannedDeviceInfo> |
| 317 test_scanned_device_infos; | 227 test_scanned_device_infos; |
| 318 | 228 |
| 319 std::unique_ptr<FakeTetherHostFetcher> fake_tether_host_fetcher_; | 229 std::unique_ptr<FakeTetherHostFetcher> fake_tether_host_fetcher_; |
| 320 std::unique_ptr<FakeBleConnectionManager> fake_ble_connection_manager_; | 230 std::unique_ptr<FakeBleConnectionManager> fake_ble_connection_manager_; |
| 321 std::unique_ptr<HostScanDevicePrioritizer> fake_host_scan_device_prioritizer_; | 231 std::unique_ptr<HostScanDevicePrioritizer> fake_host_scan_device_prioritizer_; |
| 322 std::unique_ptr<MockTetherHostResponseRecorder> | 232 std::unique_ptr<MockTetherHostResponseRecorder> |
| 323 mock_tether_host_response_recorder_; | 233 mock_tether_host_response_recorder_; |
| 324 std::unique_ptr<FakeNotificationPresenter> fake_notification_presenter_; | 234 std::unique_ptr<FakeNotificationPresenter> fake_notification_presenter_; |
| 325 // TODO(hansberry): Use a fake for this when a real mapping scheme is created. | 235 // TODO(hansberry): Use a fake for this when a real mapping scheme is created. |
| 326 std::unique_ptr<DeviceIdTetherNetworkGuidMap> | 236 std::unique_ptr<DeviceIdTetherNetworkGuidMap> |
| 327 device_id_tether_network_guid_map_; | 237 device_id_tether_network_guid_map_; |
| 328 std::unique_ptr<FakeHostScanCache> fake_host_scan_cache_; | |
| 329 | 238 |
| 330 std::unique_ptr<FakeHostScannerOperationFactory> | 239 std::unique_ptr<FakeHostScannerOperationFactory> |
| 331 fake_host_scanner_operation_factory_; | 240 fake_host_scanner_operation_factory_; |
| 332 | 241 |
| 333 std::vector<HostScannerOperation::ScannedDeviceInfo> | 242 std::vector<HostScannerOperation::ScannedDeviceInfo> |
| 334 scanned_device_infos_so_far_; | 243 scanned_device_infos_so_far_; |
| 335 | 244 |
| 336 std::unique_ptr<HostScanner> host_scanner_; | 245 std::unique_ptr<HostScanner> host_scanner_; |
| 337 | 246 |
| 338 private: | 247 private: |
| 339 DISALLOW_COPY_AND_ASSIGN(HostScannerTest); | 248 DISALLOW_COPY_AND_ASSIGN(HostScannerTest); |
| 340 }; | 249 }; |
| 341 | 250 |
| 342 TEST_F(HostScannerTest, TestScan_ResultsFromAllDevices) { | 251 TEST_F(HostScannerTest, TestScan_ResultsFromAllDevices) { |
| 343 EXPECT_FALSE(host_scanner_->IsScanActive()); | 252 EXPECT_FALSE(host_scanner_->IsScanActive()); |
| 344 host_scanner_->StartScan(); | 253 host_scanner_->StartScan(); |
| 345 EXPECT_TRUE(host_scanner_->IsScanActive()); | 254 EXPECT_TRUE(host_scanner_->IsScanActive()); |
| 346 | 255 |
| 347 fake_tether_host_fetcher_->InvokePendingCallbacks(); | 256 fake_tether_host_fetcher_->InvokePendingCallbacks(); |
| 348 ASSERT_EQ(1u, | 257 ASSERT_EQ(1u, |
| 349 fake_host_scanner_operation_factory_->created_operations().size()); | 258 fake_host_scanner_operation_factory_->created_operations().size()); |
| 350 EXPECT_TRUE(host_scanner_->IsScanActive()); | 259 EXPECT_TRUE(host_scanner_->IsScanActive()); |
| 351 | 260 |
| 352 ReceiveScanResultAndVerifySuccess( | 261 ReceiveScanResultAndVerifySuccess( |
| 353 *fake_host_scanner_operation_factory_->created_operations()[0], | 262 fake_host_scanner_operation_factory_->created_operations()[0], |
| 354 0u /* test_device_index */, false /* is_final_scan_result */); | 263 0u /* test_device_index */, false /* is_final_scan_result */); |
| 355 EXPECT_TRUE(host_scanner_->IsScanActive()); | 264 EXPECT_TRUE(host_scanner_->IsScanActive()); |
| 356 ReceiveScanResultAndVerifySuccess( | 265 ReceiveScanResultAndVerifySuccess( |
| 357 *fake_host_scanner_operation_factory_->created_operations()[0], | 266 fake_host_scanner_operation_factory_->created_operations()[0], |
| 358 1u /* test_device_index */, false /* is_final_scan_result */); | 267 1u /* test_device_index */, false /* is_final_scan_result */); |
| 359 EXPECT_TRUE(host_scanner_->IsScanActive()); | 268 EXPECT_TRUE(host_scanner_->IsScanActive()); |
| 360 ReceiveScanResultAndVerifySuccess( | 269 ReceiveScanResultAndVerifySuccess( |
| 361 *fake_host_scanner_operation_factory_->created_operations()[0], | 270 fake_host_scanner_operation_factory_->created_operations()[0], |
| 362 2u /* test_device_index */, false /* is_final_scan_result */); | 271 2u /* test_device_index */, false /* is_final_scan_result */); |
| 363 EXPECT_TRUE(host_scanner_->IsScanActive()); | 272 EXPECT_TRUE(host_scanner_->IsScanActive()); |
| 364 ReceiveScanResultAndVerifySuccess( | 273 ReceiveScanResultAndVerifySuccess( |
| 365 *fake_host_scanner_operation_factory_->created_operations()[0], | 274 fake_host_scanner_operation_factory_->created_operations()[0], |
| 366 3u /* test_device_index */, true /* is_final_scan_result */); | 275 3u /* test_device_index */, true /* is_final_scan_result */); |
| 367 EXPECT_FALSE(host_scanner_->IsScanActive()); | 276 EXPECT_FALSE(host_scanner_->IsScanActive()); |
| 368 } | 277 } |
| 369 | 278 |
| 370 TEST_F(HostScannerTest, TestScan_ResultsFromNoDevices) { | 279 TEST_F(HostScannerTest, TestScan_ResultsFromNoDevices) { |
| 371 EXPECT_FALSE(host_scanner_->IsScanActive()); | 280 EXPECT_FALSE(host_scanner_->IsScanActive()); |
| 372 host_scanner_->StartScan(); | 281 host_scanner_->StartScan(); |
| 373 EXPECT_TRUE(host_scanner_->IsScanActive()); | 282 EXPECT_TRUE(host_scanner_->IsScanActive()); |
| 374 | 283 |
| 375 fake_tether_host_fetcher_->InvokePendingCallbacks(); | 284 fake_tether_host_fetcher_->InvokePendingCallbacks(); |
| 376 ASSERT_EQ(1u, | 285 ASSERT_EQ(1u, |
| 377 fake_host_scanner_operation_factory_->created_operations().size()); | 286 fake_host_scanner_operation_factory_->created_operations().size()); |
| 378 EXPECT_TRUE(host_scanner_->IsScanActive()); | 287 EXPECT_TRUE(host_scanner_->IsScanActive()); |
| 379 | 288 |
| 380 fake_host_scanner_operation_factory_->created_operations()[0] | 289 fake_host_scanner_operation_factory_->created_operations()[0] |
| 381 ->SendScannedDeviceListUpdate( | 290 ->SendScannedDeviceListUpdate( |
| 382 std::vector<HostScannerOperation::ScannedDeviceInfo>(), | 291 std::vector<HostScannerOperation::ScannedDeviceInfo>(), |
| 383 true /* is_final_scan_result */); | 292 true /* is_final_scan_result */); |
| 384 EXPECT_EQ(0u, fake_host_scan_cache_->size()); | 293 EXPECT_TRUE(host_scanner_->most_recent_scan_results().empty()); |
| 385 EXPECT_FALSE(host_scanner_->IsScanActive()); | 294 EXPECT_FALSE(host_scanner_->IsScanActive()); |
| 386 } | 295 } |
| 387 | 296 |
| 388 TEST_F(HostScannerTest, TestScan_ResultsFromSomeDevices) { | 297 TEST_F(HostScannerTest, TestScan_ResultsFromSomeDevices) { |
| 389 EXPECT_FALSE(host_scanner_->IsScanActive()); | 298 EXPECT_FALSE(host_scanner_->IsScanActive()); |
| 390 host_scanner_->StartScan(); | 299 host_scanner_->StartScan(); |
| 391 EXPECT_TRUE(host_scanner_->IsScanActive()); | 300 EXPECT_TRUE(host_scanner_->IsScanActive()); |
| 392 | 301 |
| 393 fake_tether_host_fetcher_->InvokePendingCallbacks(); | 302 fake_tether_host_fetcher_->InvokePendingCallbacks(); |
| 394 ASSERT_EQ(1u, | 303 ASSERT_EQ(1u, |
| 395 fake_host_scanner_operation_factory_->created_operations().size()); | 304 fake_host_scanner_operation_factory_->created_operations().size()); |
| 396 EXPECT_TRUE(host_scanner_->IsScanActive()); | 305 EXPECT_TRUE(host_scanner_->IsScanActive()); |
| 397 | 306 |
| 398 // Only receive updates from the 0th and 1st device. | 307 // Only receive updates from the 0th and 1st device. |
| 399 ReceiveScanResultAndVerifySuccess( | 308 ReceiveScanResultAndVerifySuccess( |
| 400 *fake_host_scanner_operation_factory_->created_operations()[0], | 309 fake_host_scanner_operation_factory_->created_operations()[0], |
| 401 0u /* test_device_index */, false /* is_final_scan_result */); | 310 0u /* test_device_index */, false /* is_final_scan_result */); |
| 402 EXPECT_TRUE(host_scanner_->IsScanActive()); | 311 EXPECT_TRUE(host_scanner_->IsScanActive()); |
| 403 ReceiveScanResultAndVerifySuccess( | 312 ReceiveScanResultAndVerifySuccess( |
| 404 *fake_host_scanner_operation_factory_->created_operations()[0], | 313 fake_host_scanner_operation_factory_->created_operations()[0], |
| 405 1u /* test_device_index */, false /* is_final_scan_result */); | 314 1u /* test_device_index */, false /* is_final_scan_result */); |
| 406 EXPECT_TRUE(host_scanner_->IsScanActive()); | 315 EXPECT_TRUE(host_scanner_->IsScanActive()); |
| 407 | 316 |
| 408 fake_host_scanner_operation_factory_->created_operations()[0] | 317 fake_host_scanner_operation_factory_->created_operations()[0] |
| 409 ->SendScannedDeviceListUpdate(scanned_device_infos_so_far_, | 318 ->SendScannedDeviceListUpdate(scanned_device_infos_so_far_, |
| 410 true /* is_final_scan_result */); | 319 true /* is_final_scan_result */); |
| 411 EXPECT_EQ(scanned_device_infos_so_far_.size(), fake_host_scan_cache_->size()); | 320 EXPECT_EQ(scanned_device_infos_so_far_, |
| 321 host_scanner_->most_recent_scan_results()); |
| 412 EXPECT_FALSE(host_scanner_->IsScanActive()); | 322 EXPECT_FALSE(host_scanner_->IsScanActive()); |
| 413 } | 323 } |
| 414 | 324 |
| 415 TEST_F(HostScannerTest, TestScan_MultipleScanCallsDuringOperation) { | 325 TEST_F(HostScannerTest, TestScan_MultipleScanCallsDuringOperation) { |
| 416 EXPECT_FALSE(host_scanner_->IsScanActive()); | 326 EXPECT_FALSE(host_scanner_->IsScanActive()); |
| 417 host_scanner_->StartScan(); | 327 host_scanner_->StartScan(); |
| 418 EXPECT_TRUE(host_scanner_->IsScanActive()); | 328 EXPECT_TRUE(host_scanner_->IsScanActive()); |
| 419 | 329 |
| 420 // Call StartScan() again before the tether host fetcher has finished. This | 330 // Call StartScan() again before the tether host fetcher has finished. This |
| 421 // should be a no-op. | 331 // should be a no-op. |
| 422 host_scanner_->StartScan(); | 332 host_scanner_->StartScan(); |
| 423 EXPECT_TRUE(host_scanner_->IsScanActive()); | 333 EXPECT_TRUE(host_scanner_->IsScanActive()); |
| 424 | 334 |
| 425 // No devices should have been received yet. | 335 // No devices should have been received yet. |
| 426 EXPECT_EQ(0u, fake_host_scan_cache_->size()); | 336 EXPECT_EQ(std::vector<HostScannerOperation::ScannedDeviceInfo>(), |
| 337 host_scanner_->most_recent_scan_results()); |
| 427 | 338 |
| 428 fake_tether_host_fetcher_->InvokePendingCallbacks(); | 339 fake_tether_host_fetcher_->InvokePendingCallbacks(); |
| 429 ASSERT_EQ(1u, | 340 ASSERT_EQ(1u, |
| 430 fake_host_scanner_operation_factory_->created_operations().size()); | 341 fake_host_scanner_operation_factory_->created_operations().size()); |
| 431 EXPECT_TRUE(host_scanner_->IsScanActive()); | 342 EXPECT_TRUE(host_scanner_->IsScanActive()); |
| 432 | 343 |
| 433 // Call StartScan again after the tether host fetcher has finished but before | 344 // Call StartScan again after the tether host fetcher has finished but before |
| 434 // the final scan result has been received. This should be a no-op. | 345 // the final scan result has been received. This should be a no-op. |
| 435 host_scanner_->StartScan(); | 346 host_scanner_->StartScan(); |
| 436 EXPECT_TRUE(host_scanner_->IsScanActive()); | 347 EXPECT_TRUE(host_scanner_->IsScanActive()); |
| 437 | 348 |
| 438 // No devices should have been received yet. | 349 // No devices should have been received yet. |
| 439 EXPECT_EQ(0u, fake_host_scan_cache_->size()); | 350 EXPECT_EQ(std::vector<HostScannerOperation::ScannedDeviceInfo>(), |
| 351 host_scanner_->most_recent_scan_results()); |
| 440 | 352 |
| 441 // Receive updates from the 0th device. | 353 // Receive updates from the 0th device. |
| 442 ReceiveScanResultAndVerifySuccess( | 354 ReceiveScanResultAndVerifySuccess( |
| 443 *fake_host_scanner_operation_factory_->created_operations()[0], | 355 fake_host_scanner_operation_factory_->created_operations()[0], |
| 444 0u /* test_device_index */, false /* is_final_scan_result */); | 356 0u /* test_device_index */, false /* is_final_scan_result */); |
| 445 EXPECT_TRUE(host_scanner_->IsScanActive()); | 357 EXPECT_TRUE(host_scanner_->IsScanActive()); |
| 446 | 358 |
| 447 // Call StartScan again after a scan result has been received but before | 359 // Call StartScan again after a scan result has been received but before |
| 448 // the final scan result ha been received. This should be a no-op. | 360 // the final scan result ha been received. This should be a no-op. |
| 449 host_scanner_->StartScan(); | 361 host_scanner_->StartScan(); |
| 450 EXPECT_TRUE(host_scanner_->IsScanActive()); | 362 EXPECT_TRUE(host_scanner_->IsScanActive()); |
| 451 | 363 |
| 452 // The scanned devices so far should be the same (i.e., they should not have | 364 // No devices should have been received yet. |
| 453 // been affected by the extra call to StartScan()). | 365 EXPECT_EQ(scanned_device_infos_so_far_, |
| 454 EXPECT_EQ(scanned_device_infos_so_far_.size(), fake_host_scan_cache_->size()); | 366 host_scanner_->most_recent_scan_results()); |
| 455 | 367 |
| 456 // Finally, finish the scan. | 368 // Finally, finish the scan. |
| 457 fake_host_scanner_operation_factory_->created_operations()[0] | 369 fake_host_scanner_operation_factory_->created_operations()[0] |
| 458 ->SendScannedDeviceListUpdate(scanned_device_infos_so_far_, | 370 ->SendScannedDeviceListUpdate(scanned_device_infos_so_far_, |
| 459 true /* is_final_scan_result */); | 371 true /* is_final_scan_result */); |
| 460 EXPECT_EQ(scanned_device_infos_so_far_.size(), fake_host_scan_cache_->size()); | 372 EXPECT_EQ(scanned_device_infos_so_far_, |
| 373 host_scanner_->most_recent_scan_results()); |
| 461 EXPECT_FALSE(host_scanner_->IsScanActive()); | 374 EXPECT_FALSE(host_scanner_->IsScanActive()); |
| 462 } | 375 } |
| 463 | 376 |
| 464 TEST_F(HostScannerTest, TestScan_MultipleCompleteScanSessions) { | |
| 465 // Start the first scan session. | |
| 466 EXPECT_FALSE(host_scanner_->IsScanActive()); | |
| 467 host_scanner_->StartScan(); | |
| 468 EXPECT_TRUE(host_scanner_->IsScanActive()); | |
| 469 | |
| 470 fake_tether_host_fetcher_->InvokePendingCallbacks(); | |
| 471 ASSERT_EQ(1u, | |
| 472 fake_host_scanner_operation_factory_->created_operations().size()); | |
| 473 EXPECT_TRUE(host_scanner_->IsScanActive()); | |
| 474 | |
| 475 // Receive updates from devices 0-3. | |
| 476 ReceiveScanResultAndVerifySuccess( | |
| 477 *fake_host_scanner_operation_factory_->created_operations()[0], | |
| 478 0u /* test_device_index */, false /* is_final_scan_result */); | |
| 479 EXPECT_TRUE(host_scanner_->IsScanActive()); | |
| 480 ReceiveScanResultAndVerifySuccess( | |
| 481 *fake_host_scanner_operation_factory_->created_operations()[0], | |
| 482 1u /* test_device_index */, false /* is_final_scan_result */); | |
| 483 EXPECT_TRUE(host_scanner_->IsScanActive()); | |
| 484 ReceiveScanResultAndVerifySuccess( | |
| 485 *fake_host_scanner_operation_factory_->created_operations()[0], | |
| 486 2u /* test_device_index */, false /* is_final_scan_result */); | |
| 487 EXPECT_TRUE(host_scanner_->IsScanActive()); | |
| 488 ReceiveScanResultAndVerifySuccess( | |
| 489 *fake_host_scanner_operation_factory_->created_operations()[0], | |
| 490 3u /* test_device_index */, false /* is_final_scan_result */); | |
| 491 EXPECT_TRUE(host_scanner_->IsScanActive()); | |
| 492 | |
| 493 // Finish the first scan. | |
| 494 fake_host_scanner_operation_factory_->created_operations()[0] | |
| 495 ->SendScannedDeviceListUpdate(scanned_device_infos_so_far_, | |
| 496 true /* is_final_scan_result */); | |
| 497 EXPECT_EQ(scanned_device_infos_so_far_.size(), fake_host_scan_cache_->size()); | |
| 498 EXPECT_FALSE(host_scanner_->IsScanActive()); | |
| 499 | |
| 500 // Simulate device 0 connecting; it is now considered the active host. | |
| 501 fake_host_scan_cache_->set_active_host_tether_network_guid( | |
| 502 device_id_tether_network_guid_map_->GetTetherNetworkGuidForDeviceId( | |
| 503 test_devices_[0].GetDeviceId())); | |
| 504 | |
| 505 // Now, start the second scan session. | |
| 506 EXPECT_FALSE(host_scanner_->IsScanActive()); | |
| 507 host_scanner_->StartScan(); | |
| 508 EXPECT_TRUE(host_scanner_->IsScanActive()); | |
| 509 | |
| 510 fake_tether_host_fetcher_->InvokePendingCallbacks(); | |
| 511 ASSERT_EQ(2u, | |
| 512 fake_host_scanner_operation_factory_->created_operations().size()); | |
| 513 EXPECT_TRUE(host_scanner_->IsScanActive()); | |
| 514 | |
| 515 // The cache should have been cleared except for the active host. Since the | |
| 516 // active host is device 0, clear |scanned_device_list_so_far_| from device 1 | |
| 517 // onward and verify that the cache is equivalent. | |
| 518 scanned_device_infos_so_far_.erase(scanned_device_infos_so_far_.begin() + 1, | |
| 519 scanned_device_infos_so_far_.end()); | |
| 520 VerifyScanResultsMatchCache(); | |
| 521 | |
| 522 // Receive results from devices 0 and 2. This simulates devices 1 and 3 being | |
| 523 // out of range during the second scan. | |
| 524 ReceiveScanResultAndVerifySuccess( | |
| 525 *fake_host_scanner_operation_factory_->created_operations()[1], | |
| 526 0u /* test_device_index */, false /* is_final_scan_result */); | |
| 527 EXPECT_TRUE(host_scanner_->IsScanActive()); | |
| 528 ReceiveScanResultAndVerifySuccess( | |
| 529 *fake_host_scanner_operation_factory_->created_operations()[1], | |
| 530 2u /* test_device_index */, false /* is_final_scan_result */); | |
| 531 EXPECT_TRUE(host_scanner_->IsScanActive()); | |
| 532 | |
| 533 // Finish the second scan. | |
| 534 fake_host_scanner_operation_factory_->created_operations()[1] | |
| 535 ->SendScannedDeviceListUpdate(scanned_device_infos_so_far_, | |
| 536 true /* is_final_scan_result */); | |
| 537 EXPECT_EQ(scanned_device_infos_so_far_.size(), fake_host_scan_cache_->size()); | |
| 538 EXPECT_FALSE(host_scanner_->IsScanActive()); | |
| 539 } | |
| 540 | |
| 541 } // namespace tether | 377 } // namespace tether |
| 542 | 378 |
| 543 } // namespace chromeos | 379 } // namespace chromeos |
| OLD | NEW |