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