Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(56)

Side by Side Diff: chromeos/components/tether/host_scanner_unittest.cc

Issue 2852693004: [CrOS Tether] Create HostScanCache, which caches scan results and inserts them into the network sta… (Closed)
Patch Set: stevenjb@ comments. Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chromeos/components/tether/host_scanner.cc ('k') | chromeos/components/tether/initializer.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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);
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 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 }
224 314
225 const std::vector<cryptauth::RemoteDevice> test_devices_; 315 const std::vector<cryptauth::RemoteDevice> test_devices_;
226 const std::vector<HostScannerOperation::ScannedDeviceInfo> 316 const std::vector<HostScannerOperation::ScannedDeviceInfo>
227 test_scanned_device_infos; 317 test_scanned_device_infos;
228 318
229 std::unique_ptr<FakeTetherHostFetcher> fake_tether_host_fetcher_; 319 std::unique_ptr<FakeTetherHostFetcher> fake_tether_host_fetcher_;
230 std::unique_ptr<FakeBleConnectionManager> fake_ble_connection_manager_; 320 std::unique_ptr<FakeBleConnectionManager> fake_ble_connection_manager_;
231 std::unique_ptr<HostScanDevicePrioritizer> fake_host_scan_device_prioritizer_; 321 std::unique_ptr<HostScanDevicePrioritizer> fake_host_scan_device_prioritizer_;
232 std::unique_ptr<MockTetherHostResponseRecorder> 322 std::unique_ptr<MockTetherHostResponseRecorder>
233 mock_tether_host_response_recorder_; 323 mock_tether_host_response_recorder_;
234 std::unique_ptr<FakeNotificationPresenter> fake_notification_presenter_; 324 std::unique_ptr<FakeNotificationPresenter> fake_notification_presenter_;
235 // TODO(hansberry): Use a fake for this when a real mapping scheme is created. 325 // TODO(hansberry): Use a fake for this when a real mapping scheme is created.
236 std::unique_ptr<DeviceIdTetherNetworkGuidMap> 326 std::unique_ptr<DeviceIdTetherNetworkGuidMap>
237 device_id_tether_network_guid_map_; 327 device_id_tether_network_guid_map_;
328 std::unique_ptr<FakeHostScanCache> fake_host_scan_cache_;
238 329
239 std::unique_ptr<FakeHostScannerOperationFactory> 330 std::unique_ptr<FakeHostScannerOperationFactory>
240 fake_host_scanner_operation_factory_; 331 fake_host_scanner_operation_factory_;
241 332
242 std::vector<HostScannerOperation::ScannedDeviceInfo> 333 std::vector<HostScannerOperation::ScannedDeviceInfo>
243 scanned_device_infos_so_far_; 334 scanned_device_infos_so_far_;
244 335
245 std::unique_ptr<HostScanner> host_scanner_; 336 std::unique_ptr<HostScanner> host_scanner_;
246 337
247 private: 338 private:
248 DISALLOW_COPY_AND_ASSIGN(HostScannerTest); 339 DISALLOW_COPY_AND_ASSIGN(HostScannerTest);
249 }; 340 };
250 341
251 TEST_F(HostScannerTest, TestScan_ResultsFromAllDevices) { 342 TEST_F(HostScannerTest, TestScan_ResultsFromAllDevices) {
252 EXPECT_FALSE(host_scanner_->IsScanActive()); 343 EXPECT_FALSE(host_scanner_->IsScanActive());
253 host_scanner_->StartScan(); 344 host_scanner_->StartScan();
254 EXPECT_TRUE(host_scanner_->IsScanActive()); 345 EXPECT_TRUE(host_scanner_->IsScanActive());
255 346
256 fake_tether_host_fetcher_->InvokePendingCallbacks(); 347 fake_tether_host_fetcher_->InvokePendingCallbacks();
257 ASSERT_EQ(1u, 348 ASSERT_EQ(1u,
258 fake_host_scanner_operation_factory_->created_operations().size()); 349 fake_host_scanner_operation_factory_->created_operations().size());
259 EXPECT_TRUE(host_scanner_->IsScanActive()); 350 EXPECT_TRUE(host_scanner_->IsScanActive());
260 351
261 ReceiveScanResultAndVerifySuccess( 352 ReceiveScanResultAndVerifySuccess(
262 fake_host_scanner_operation_factory_->created_operations()[0], 353 *fake_host_scanner_operation_factory_->created_operations()[0],
263 0u /* test_device_index */, false /* is_final_scan_result */); 354 0u /* test_device_index */, false /* is_final_scan_result */);
264 EXPECT_TRUE(host_scanner_->IsScanActive()); 355 EXPECT_TRUE(host_scanner_->IsScanActive());
265 ReceiveScanResultAndVerifySuccess( 356 ReceiveScanResultAndVerifySuccess(
266 fake_host_scanner_operation_factory_->created_operations()[0], 357 *fake_host_scanner_operation_factory_->created_operations()[0],
267 1u /* test_device_index */, false /* is_final_scan_result */); 358 1u /* test_device_index */, false /* is_final_scan_result */);
268 EXPECT_TRUE(host_scanner_->IsScanActive()); 359 EXPECT_TRUE(host_scanner_->IsScanActive());
269 ReceiveScanResultAndVerifySuccess( 360 ReceiveScanResultAndVerifySuccess(
270 fake_host_scanner_operation_factory_->created_operations()[0], 361 *fake_host_scanner_operation_factory_->created_operations()[0],
271 2u /* test_device_index */, false /* is_final_scan_result */); 362 2u /* test_device_index */, false /* is_final_scan_result */);
272 EXPECT_TRUE(host_scanner_->IsScanActive()); 363 EXPECT_TRUE(host_scanner_->IsScanActive());
273 ReceiveScanResultAndVerifySuccess( 364 ReceiveScanResultAndVerifySuccess(
274 fake_host_scanner_operation_factory_->created_operations()[0], 365 *fake_host_scanner_operation_factory_->created_operations()[0],
275 3u /* test_device_index */, true /* is_final_scan_result */); 366 3u /* test_device_index */, true /* is_final_scan_result */);
276 EXPECT_FALSE(host_scanner_->IsScanActive()); 367 EXPECT_FALSE(host_scanner_->IsScanActive());
277 } 368 }
278 369
279 TEST_F(HostScannerTest, TestScan_ResultsFromNoDevices) { 370 TEST_F(HostScannerTest, TestScan_ResultsFromNoDevices) {
280 EXPECT_FALSE(host_scanner_->IsScanActive()); 371 EXPECT_FALSE(host_scanner_->IsScanActive());
281 host_scanner_->StartScan(); 372 host_scanner_->StartScan();
282 EXPECT_TRUE(host_scanner_->IsScanActive()); 373 EXPECT_TRUE(host_scanner_->IsScanActive());
283 374
284 fake_tether_host_fetcher_->InvokePendingCallbacks(); 375 fake_tether_host_fetcher_->InvokePendingCallbacks();
285 ASSERT_EQ(1u, 376 ASSERT_EQ(1u,
286 fake_host_scanner_operation_factory_->created_operations().size()); 377 fake_host_scanner_operation_factory_->created_operations().size());
287 EXPECT_TRUE(host_scanner_->IsScanActive()); 378 EXPECT_TRUE(host_scanner_->IsScanActive());
288 379
289 fake_host_scanner_operation_factory_->created_operations()[0] 380 fake_host_scanner_operation_factory_->created_operations()[0]
290 ->SendScannedDeviceListUpdate( 381 ->SendScannedDeviceListUpdate(
291 std::vector<HostScannerOperation::ScannedDeviceInfo>(), 382 std::vector<HostScannerOperation::ScannedDeviceInfo>(),
292 true /* is_final_scan_result */); 383 true /* is_final_scan_result */);
293 EXPECT_TRUE(host_scanner_->most_recent_scan_results().empty()); 384 EXPECT_EQ(0u, fake_host_scan_cache_->size());
294 EXPECT_FALSE(host_scanner_->IsScanActive()); 385 EXPECT_FALSE(host_scanner_->IsScanActive());
295 } 386 }
296 387
297 TEST_F(HostScannerTest, TestScan_ResultsFromSomeDevices) { 388 TEST_F(HostScannerTest, TestScan_ResultsFromSomeDevices) {
298 EXPECT_FALSE(host_scanner_->IsScanActive()); 389 EXPECT_FALSE(host_scanner_->IsScanActive());
299 host_scanner_->StartScan(); 390 host_scanner_->StartScan();
300 EXPECT_TRUE(host_scanner_->IsScanActive()); 391 EXPECT_TRUE(host_scanner_->IsScanActive());
301 392
302 fake_tether_host_fetcher_->InvokePendingCallbacks(); 393 fake_tether_host_fetcher_->InvokePendingCallbacks();
303 ASSERT_EQ(1u, 394 ASSERT_EQ(1u,
304 fake_host_scanner_operation_factory_->created_operations().size()); 395 fake_host_scanner_operation_factory_->created_operations().size());
305 EXPECT_TRUE(host_scanner_->IsScanActive()); 396 EXPECT_TRUE(host_scanner_->IsScanActive());
306 397
307 // Only receive updates from the 0th and 1st device. 398 // Only receive updates from the 0th and 1st device.
308 ReceiveScanResultAndVerifySuccess( 399 ReceiveScanResultAndVerifySuccess(
309 fake_host_scanner_operation_factory_->created_operations()[0], 400 *fake_host_scanner_operation_factory_->created_operations()[0],
310 0u /* test_device_index */, false /* is_final_scan_result */); 401 0u /* test_device_index */, false /* is_final_scan_result */);
311 EXPECT_TRUE(host_scanner_->IsScanActive()); 402 EXPECT_TRUE(host_scanner_->IsScanActive());
312 ReceiveScanResultAndVerifySuccess( 403 ReceiveScanResultAndVerifySuccess(
313 fake_host_scanner_operation_factory_->created_operations()[0], 404 *fake_host_scanner_operation_factory_->created_operations()[0],
314 1u /* test_device_index */, false /* is_final_scan_result */); 405 1u /* test_device_index */, false /* is_final_scan_result */);
315 EXPECT_TRUE(host_scanner_->IsScanActive()); 406 EXPECT_TRUE(host_scanner_->IsScanActive());
316 407
317 fake_host_scanner_operation_factory_->created_operations()[0] 408 fake_host_scanner_operation_factory_->created_operations()[0]
318 ->SendScannedDeviceListUpdate(scanned_device_infos_so_far_, 409 ->SendScannedDeviceListUpdate(scanned_device_infos_so_far_,
319 true /* is_final_scan_result */); 410 true /* is_final_scan_result */);
320 EXPECT_EQ(scanned_device_infos_so_far_, 411 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()); 412 EXPECT_FALSE(host_scanner_->IsScanActive());
323 } 413 }
324 414
325 TEST_F(HostScannerTest, TestScan_MultipleScanCallsDuringOperation) { 415 TEST_F(HostScannerTest, TestScan_MultipleScanCallsDuringOperation) {
326 EXPECT_FALSE(host_scanner_->IsScanActive()); 416 EXPECT_FALSE(host_scanner_->IsScanActive());
327 host_scanner_->StartScan(); 417 host_scanner_->StartScan();
328 EXPECT_TRUE(host_scanner_->IsScanActive()); 418 EXPECT_TRUE(host_scanner_->IsScanActive());
329 419
330 // Call StartScan() again before the tether host fetcher has finished. This 420 // Call StartScan() again before the tether host fetcher has finished. This
331 // should be a no-op. 421 // should be a no-op.
332 host_scanner_->StartScan(); 422 host_scanner_->StartScan();
333 EXPECT_TRUE(host_scanner_->IsScanActive()); 423 EXPECT_TRUE(host_scanner_->IsScanActive());
334 424
335 // No devices should have been received yet. 425 // No devices should have been received yet.
336 EXPECT_EQ(std::vector<HostScannerOperation::ScannedDeviceInfo>(), 426 EXPECT_EQ(0u, fake_host_scan_cache_->size());
337 host_scanner_->most_recent_scan_results());
338 427
339 fake_tether_host_fetcher_->InvokePendingCallbacks(); 428 fake_tether_host_fetcher_->InvokePendingCallbacks();
340 ASSERT_EQ(1u, 429 ASSERT_EQ(1u,
341 fake_host_scanner_operation_factory_->created_operations().size()); 430 fake_host_scanner_operation_factory_->created_operations().size());
342 EXPECT_TRUE(host_scanner_->IsScanActive()); 431 EXPECT_TRUE(host_scanner_->IsScanActive());
343 432
344 // Call StartScan again after the tether host fetcher has finished but before 433 // 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. 434 // the final scan result has been received. This should be a no-op.
346 host_scanner_->StartScan(); 435 host_scanner_->StartScan();
347 EXPECT_TRUE(host_scanner_->IsScanActive()); 436 EXPECT_TRUE(host_scanner_->IsScanActive());
348 437
349 // No devices should have been received yet. 438 // No devices should have been received yet.
350 EXPECT_EQ(std::vector<HostScannerOperation::ScannedDeviceInfo>(), 439 EXPECT_EQ(0u, fake_host_scan_cache_->size());
351 host_scanner_->most_recent_scan_results());
352 440
353 // Receive updates from the 0th device. 441 // Receive updates from the 0th device.
354 ReceiveScanResultAndVerifySuccess( 442 ReceiveScanResultAndVerifySuccess(
355 fake_host_scanner_operation_factory_->created_operations()[0], 443 *fake_host_scanner_operation_factory_->created_operations()[0],
356 0u /* test_device_index */, false /* is_final_scan_result */); 444 0u /* test_device_index */, false /* is_final_scan_result */);
357 EXPECT_TRUE(host_scanner_->IsScanActive()); 445 EXPECT_TRUE(host_scanner_->IsScanActive());
358 446
359 // Call StartScan again after a scan result has been received but before 447 // 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. 448 // the final scan result ha been received. This should be a no-op.
361 host_scanner_->StartScan(); 449 host_scanner_->StartScan();
362 EXPECT_TRUE(host_scanner_->IsScanActive()); 450 EXPECT_TRUE(host_scanner_->IsScanActive());
363 451
364 // No devices should have been received yet. 452 // The scanned devices so far should be the same (i.e., they should not have
365 EXPECT_EQ(scanned_device_infos_so_far_, 453 // been affected by the extra call to StartScan()).
366 host_scanner_->most_recent_scan_results()); 454 EXPECT_EQ(scanned_device_infos_so_far_.size(), fake_host_scan_cache_->size());
367 455
368 // Finally, finish the scan. 456 // Finally, finish the scan.
369 fake_host_scanner_operation_factory_->created_operations()[0] 457 fake_host_scanner_operation_factory_->created_operations()[0]
370 ->SendScannedDeviceListUpdate(scanned_device_infos_so_far_, 458 ->SendScannedDeviceListUpdate(scanned_device_infos_so_far_,
371 true /* is_final_scan_result */); 459 true /* is_final_scan_result */);
372 EXPECT_EQ(scanned_device_infos_so_far_, 460 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()); 461 EXPECT_FALSE(host_scanner_->IsScanActive());
375 } 462 }
376 463
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
377 } // namespace tether 541 } // namespace tether
378 542
379 } // namespace chromeos 543 } // namespace chromeos
OLDNEW
« no previous file with comments | « chromeos/components/tether/host_scanner.cc ('k') | chromeos/components/tether/initializer.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698