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

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

Issue 2870393003: Tether: only scan when asked to by NetworkStateHandler. (Closed)
Patch Set: Remove PowerManagerClient callbacks and implement all NetworkStateHandler callbacks. 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
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 "base/test/simple_test_clock.h"
15 #include "base/time/time.h"
14 #include "chromeos/components/tether/device_id_tether_network_guid_map.h" 16 #include "chromeos/components/tether/device_id_tether_network_guid_map.h"
15 #include "chromeos/components/tether/fake_ble_connection_manager.h" 17 #include "chromeos/components/tether/fake_ble_connection_manager.h"
16 #include "chromeos/components/tether/fake_host_scan_cache.h" 18 #include "chromeos/components/tether/fake_host_scan_cache.h"
17 #include "chromeos/components/tether/fake_notification_presenter.h" 19 #include "chromeos/components/tether/fake_notification_presenter.h"
18 #include "chromeos/components/tether/fake_tether_host_fetcher.h" 20 #include "chromeos/components/tether/fake_tether_host_fetcher.h"
19 #include "chromeos/components/tether/host_scan_device_prioritizer.h" 21 #include "chromeos/components/tether/host_scan_device_prioritizer.h"
20 #include "chromeos/components/tether/host_scanner.h" 22 #include "chromeos/components/tether/host_scanner.h"
21 #include "chromeos/components/tether/mock_tether_host_response_recorder.h" 23 #include "chromeos/components/tether/mock_tether_host_response_recorder.h"
22 #include "chromeos/dbus/dbus_thread_manager.h" 24 #include "chromeos/dbus/dbus_thread_manager.h"
23 #include "components/cryptauth/remote_device_test_util.h" 25 #include "components/cryptauth/remote_device_test_util.h"
24 #include "testing/gtest/include/gtest/gtest.h" 26 #include "testing/gtest/include/gtest/gtest.h"
25 27
26 namespace chromeos { 28 namespace chromeos {
27 29
28 namespace tether { 30 namespace tether {
29 31
30 namespace { 32 namespace {
31 33
34 class TestObserver : public HostScanner::Observer {
35 public:
36 void ScanFinished() override { scan_finished_count_++; }
37
38 int scan_finished_count() { return scan_finished_count_; }
39
40 private:
41 int scan_finished_count_ = 0;
42 };
43
32 class FakeHostScanDevicePrioritizer : public HostScanDevicePrioritizer { 44 class FakeHostScanDevicePrioritizer : public HostScanDevicePrioritizer {
33 public: 45 public:
34 FakeHostScanDevicePrioritizer() : HostScanDevicePrioritizer(nullptr) {} 46 FakeHostScanDevicePrioritizer() : HostScanDevicePrioritizer(nullptr) {}
35 ~FakeHostScanDevicePrioritizer() override {} 47 ~FakeHostScanDevicePrioritizer() override {}
36 48
37 // Simply leave |remote_devices| as-is. 49 // Simply leave |remote_devices| as-is.
38 void SortByHostScanOrder( 50 void SortByHostScanOrder(
39 std::vector<cryptauth::RemoteDevice>* remote_devices) const override {} 51 std::vector<cryptauth::RemoteDevice>* remote_devices) const override {}
40 }; 52 };
41 53
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after
216 base::MakeUnique<FakeNotificationPresenter>(); 228 base::MakeUnique<FakeNotificationPresenter>();
217 device_id_tether_network_guid_map_ = 229 device_id_tether_network_guid_map_ =
218 base::MakeUnique<DeviceIdTetherNetworkGuidMap>(); 230 base::MakeUnique<DeviceIdTetherNetworkGuidMap>();
219 fake_host_scan_cache_ = base::MakeUnique<FakeHostScanCache>(); 231 fake_host_scan_cache_ = base::MakeUnique<FakeHostScanCache>();
220 232
221 fake_host_scanner_operation_factory_ = 233 fake_host_scanner_operation_factory_ =
222 base::WrapUnique(new FakeHostScannerOperationFactory(test_devices_)); 234 base::WrapUnique(new FakeHostScannerOperationFactory(test_devices_));
223 HostScannerOperation::Factory::SetInstanceForTesting( 235 HostScannerOperation::Factory::SetInstanceForTesting(
224 fake_host_scanner_operation_factory_.get()); 236 fake_host_scanner_operation_factory_.get());
225 237
238 test_clock_ = base::MakeUnique<base::SimpleTestClock>();
239
226 host_scanner_ = base::WrapUnique(new HostScanner( 240 host_scanner_ = base::WrapUnique(new HostScanner(
227 fake_tether_host_fetcher_.get(), fake_ble_connection_manager_.get(), 241 fake_tether_host_fetcher_.get(), fake_ble_connection_manager_.get(),
228 fake_host_scan_device_prioritizer_.get(), 242 fake_host_scan_device_prioritizer_.get(),
229 mock_tether_host_response_recorder_.get(), 243 mock_tether_host_response_recorder_.get(),
230 fake_notification_presenter_.get(), 244 fake_notification_presenter_.get(),
231 device_id_tether_network_guid_map_.get(), fake_host_scan_cache_.get())); 245 device_id_tether_network_guid_map_.get(), fake_host_scan_cache_.get(),
246 test_clock_.get()));
247
248 test_observer_ = base::MakeUnique<TestObserver>();
249 host_scanner_->AddObserver(test_observer_.get());
232 } 250 }
233 251
234 void TearDown() override { 252 void TearDown() override {
253 host_scanner_->RemoveObserver(test_observer_.get());
235 HostScannerOperation::Factory::SetInstanceForTesting(nullptr); 254 HostScannerOperation::Factory::SetInstanceForTesting(nullptr);
236 } 255 }
237 256
238 // Causes |fake_operation| to receive the scan result in 257 // Causes |fake_operation| to receive the scan result in
239 // |test_scanned_device_infos| vector at the index |test_device_index| with 258 // |test_scanned_device_infos| vector at the index |test_device_index| with
240 // the "final result" value of |is_final_scan_result|. 259 // the "final result" value of |is_final_scan_result|.
241 void ReceiveScanResultAndVerifySuccess( 260 void ReceiveScanResultAndVerifySuccess(
242 FakeHostScannerOperation& fake_operation, 261 FakeHostScannerOperation& fake_operation,
243 size_t test_device_index, 262 size_t test_device_index,
244 bool is_final_scan_result) { 263 bool is_final_scan_result) {
245 bool already_in_list = false; 264 bool already_in_list = false;
246 for (auto& scanned_device_info : scanned_device_infos_so_far_) { 265 for (auto& scanned_device_info : scanned_device_infos_so_far_) {
247 if (scanned_device_info.remote_device.GetDeviceId() == 266 if (scanned_device_info.remote_device.GetDeviceId() ==
248 test_devices_[test_device_index].GetDeviceId()) { 267 test_devices_[test_device_index].GetDeviceId()) {
249 already_in_list = true; 268 already_in_list = true;
250 break; 269 break;
251 } 270 }
252 } 271 }
253 272
254 if (!already_in_list) { 273 if (!already_in_list) {
255 scanned_device_infos_so_far_.push_back( 274 scanned_device_infos_so_far_.push_back(
256 test_scanned_device_infos[test_device_index]); 275 test_scanned_device_infos[test_device_index]);
257 } 276 }
258 277
278 int previous_scan_finished_count = test_observer_->scan_finished_count();
259 fake_operation.SendScannedDeviceListUpdate(scanned_device_infos_so_far_, 279 fake_operation.SendScannedDeviceListUpdate(scanned_device_infos_so_far_,
260 is_final_scan_result); 280 is_final_scan_result);
261 VerifyScanResultsMatchCache(); 281 VerifyScanResultsMatchCache();
282 EXPECT_EQ(previous_scan_finished_count + (is_final_scan_result ? 1 : 0),
283 test_observer_->scan_finished_count());
262 284
263 if (scanned_device_infos_so_far_.size() == 1) { 285 if (scanned_device_infos_so_far_.size() == 1) {
264 EXPECT_EQ(FakeNotificationPresenter::PotentialHotspotNotificationState:: 286 EXPECT_EQ(FakeNotificationPresenter::PotentialHotspotNotificationState::
265 SINGLE_HOTSPOT_NEARBY_SHOWN, 287 SINGLE_HOTSPOT_NEARBY_SHOWN,
266 fake_notification_presenter_->potential_hotspot_state()); 288 fake_notification_presenter_->potential_hotspot_state());
267 } else { 289 } else {
268 EXPECT_EQ(FakeNotificationPresenter::PotentialHotspotNotificationState:: 290 EXPECT_EQ(FakeNotificationPresenter::PotentialHotspotNotificationState::
269 MULTIPLE_HOTSPOTS_NEARBY_SHOWN, 291 MULTIPLE_HOTSPOTS_NEARBY_SHOWN,
270 fake_notification_presenter_->potential_hotspot_state()); 292 fake_notification_presenter_->potential_hotspot_state());
271 } 293 }
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
320 std::unique_ptr<FakeBleConnectionManager> fake_ble_connection_manager_; 342 std::unique_ptr<FakeBleConnectionManager> fake_ble_connection_manager_;
321 std::unique_ptr<HostScanDevicePrioritizer> fake_host_scan_device_prioritizer_; 343 std::unique_ptr<HostScanDevicePrioritizer> fake_host_scan_device_prioritizer_;
322 std::unique_ptr<MockTetherHostResponseRecorder> 344 std::unique_ptr<MockTetherHostResponseRecorder>
323 mock_tether_host_response_recorder_; 345 mock_tether_host_response_recorder_;
324 std::unique_ptr<FakeNotificationPresenter> fake_notification_presenter_; 346 std::unique_ptr<FakeNotificationPresenter> fake_notification_presenter_;
325 // TODO(hansberry): Use a fake for this when a real mapping scheme is created. 347 // TODO(hansberry): Use a fake for this when a real mapping scheme is created.
326 std::unique_ptr<DeviceIdTetherNetworkGuidMap> 348 std::unique_ptr<DeviceIdTetherNetworkGuidMap>
327 device_id_tether_network_guid_map_; 349 device_id_tether_network_guid_map_;
328 std::unique_ptr<FakeHostScanCache> fake_host_scan_cache_; 350 std::unique_ptr<FakeHostScanCache> fake_host_scan_cache_;
329 351
352 std::unique_ptr<base::SimpleTestClock> test_clock_;
353 std::unique_ptr<TestObserver> test_observer_;
354
330 std::unique_ptr<FakeHostScannerOperationFactory> 355 std::unique_ptr<FakeHostScannerOperationFactory>
331 fake_host_scanner_operation_factory_; 356 fake_host_scanner_operation_factory_;
332 357
333 std::vector<HostScannerOperation::ScannedDeviceInfo> 358 std::vector<HostScannerOperation::ScannedDeviceInfo>
334 scanned_device_infos_so_far_; 359 scanned_device_infos_so_far_;
335 360
336 std::unique_ptr<HostScanner> host_scanner_; 361 std::unique_ptr<HostScanner> host_scanner_;
337 362
338 private: 363 private:
339 DISALLOW_COPY_AND_ASSIGN(HostScannerTest); 364 DISALLOW_COPY_AND_ASSIGN(HostScannerTest);
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after
531 EXPECT_TRUE(host_scanner_->IsScanActive()); 556 EXPECT_TRUE(host_scanner_->IsScanActive());
532 557
533 // Finish the second scan. 558 // Finish the second scan.
534 fake_host_scanner_operation_factory_->created_operations()[1] 559 fake_host_scanner_operation_factory_->created_operations()[1]
535 ->SendScannedDeviceListUpdate(scanned_device_infos_so_far_, 560 ->SendScannedDeviceListUpdate(scanned_device_infos_so_far_,
536 true /* is_final_scan_result */); 561 true /* is_final_scan_result */);
537 EXPECT_EQ(scanned_device_infos_so_far_.size(), fake_host_scan_cache_->size()); 562 EXPECT_EQ(scanned_device_infos_so_far_.size(), fake_host_scan_cache_->size());
538 EXPECT_FALSE(host_scanner_->IsScanActive()); 563 EXPECT_FALSE(host_scanner_->IsScanActive());
539 } 564 }
540 565
566 TEST_F(HostScannerTest, HasRecentlyScanned) {
567 test_clock_->SetNow(base::Time::Now());
568
569 host_scanner_->StartScan();
570 fake_tether_host_fetcher_->InvokePendingCallbacks();
571
572 ReceiveScanResultAndVerifySuccess(
573 *fake_host_scanner_operation_factory_->created_operations()[0],
574 0u /* test_device_index */, false /* is_final_scan_result */);
575 ReceiveScanResultAndVerifySuccess(
576 *fake_host_scanner_operation_factory_->created_operations()[0],
577 1u /* test_device_index */, true /* is_final_scan_result */);
578
579 // We have just scanned.
580 EXPECT_TRUE(host_scanner_->HasRecentlyScanned());
581
582 int time_limit_minutes = HostScanCache::kNumMinutesBeforeCacheEntryExpires;
583 int time_limit_seconds = time_limit_minutes * 60;
584 int time_limit_half_seconds = time_limit_seconds / 2;
585 int time_limit_threshold_seconds =
586 time_limit_seconds - time_limit_half_seconds - 1;
587
588 // Move up some amount of time, but not the full limit.
589 test_clock_->Advance(base::TimeDelta::FromSeconds(time_limit_half_seconds));
590 EXPECT_TRUE(host_scanner_->HasRecentlyScanned());
591
592 // Move right up to the limit.
593 test_clock_->Advance(
594 base::TimeDelta::FromSeconds(time_limit_threshold_seconds));
595 EXPECT_TRUE(host_scanner_->HasRecentlyScanned());
596
597 // Move past the limit.
598 test_clock_->Advance(base::TimeDelta::FromSeconds(1));
599 EXPECT_FALSE(host_scanner_->HasRecentlyScanned());
600 }
601
541 } // namespace tether 602 } // namespace tether
542 603
543 } // namespace chromeos 604 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698