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 "base/test/simple_test_clock.h" | 14 #include "base/test/simple_test_clock.h" |
15 #include "base/time/time.h" | 15 #include "base/time/time.h" |
16 #include "chromeos/components/tether/device_id_tether_network_guid_map.h" | 16 #include "chromeos/components/tether/device_id_tether_network_guid_map.h" |
17 #include "chromeos/components/tether/fake_ble_connection_manager.h" | 17 #include "chromeos/components/tether/fake_ble_connection_manager.h" |
18 #include "chromeos/components/tether/fake_host_scan_cache.h" | 18 #include "chromeos/components/tether/fake_host_scan_cache.h" |
19 #include "chromeos/components/tether/fake_notification_presenter.h" | 19 #include "chromeos/components/tether/fake_notification_presenter.h" |
20 #include "chromeos/components/tether/fake_tether_host_fetcher.h" | 20 #include "chromeos/components/tether/fake_tether_host_fetcher.h" |
21 #include "chromeos/components/tether/host_scan_device_prioritizer.h" | 21 #include "chromeos/components/tether/host_scan_device_prioritizer.h" |
22 #include "chromeos/components/tether/host_scanner.h" | 22 #include "chromeos/components/tether/host_scanner.h" |
23 #include "chromeos/components/tether/mock_tether_host_response_recorder.h" | 23 #include "chromeos/components/tether/mock_tether_host_response_recorder.h" |
24 #include "chromeos/components/tether/proto_test_util.h" | 24 #include "chromeos/components/tether/proto_test_util.h" |
25 #include "chromeos/dbus/dbus_thread_manager.h" | 25 #include "chromeos/dbus/dbus_thread_manager.h" |
| 26 #include "chromeos/network/network_state_test.h" |
26 #include "components/cryptauth/remote_device_test_util.h" | 27 #include "components/cryptauth/remote_device_test_util.h" |
27 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
28 | 29 |
29 namespace chromeos { | 30 namespace chromeos { |
30 | 31 |
31 namespace tether { | 32 namespace tether { |
32 | 33 |
33 namespace { | 34 namespace { |
34 | 35 |
35 class TestObserver : public HostScanner::Observer { | 36 class TestObserver : public HostScanner::Observer { |
36 public: | 37 public: |
37 void ScanFinished() override { scan_finished_count_++; } | 38 void ScanFinished() override { scan_finished_count_++; } |
38 | 39 |
39 int scan_finished_count() { return scan_finished_count_; } | 40 int scan_finished_count() { return scan_finished_count_; } |
40 | 41 |
41 private: | 42 private: |
42 int scan_finished_count_ = 0; | 43 int scan_finished_count_ = 0; |
43 }; | 44 }; |
44 | 45 |
45 class FakeHostScanDevicePrioritizer : public HostScanDevicePrioritizer { | 46 class FakeHostScanDevicePrioritizer : public HostScanDevicePrioritizer { |
46 public: | 47 public: |
47 FakeHostScanDevicePrioritizer() : HostScanDevicePrioritizer(nullptr) {} | 48 FakeHostScanDevicePrioritizer(NetworkStateHandler* network_state_handler) |
| 49 : HostScanDevicePrioritizer( |
| 50 network_state_handler, |
| 51 nullptr /* tether_host_response_recorder */, |
| 52 nullptr /* device_id_tether_network_guid_map */) {} |
48 ~FakeHostScanDevicePrioritizer() override {} | 53 ~FakeHostScanDevicePrioritizer() override {} |
49 | 54 |
50 // Simply leave |remote_devices| as-is. | 55 // Simply leave |remote_devices| as-is. |
51 void SortByHostScanOrder( | 56 void SortByHostScanOrder( |
52 std::vector<cryptauth::RemoteDevice>* remote_devices) const override {} | 57 std::vector<cryptauth::RemoteDevice>* remote_devices) const override {} |
53 }; | 58 }; |
54 | 59 |
55 class FakeHostScannerOperation : public HostScannerOperation { | 60 class FakeHostScannerOperation : public HostScannerOperation { |
56 public: | 61 public: |
57 FakeHostScannerOperation( | 62 FakeHostScannerOperation( |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
169 | 174 |
170 scanned_device_infos.push_back(HostScannerOperation::ScannedDeviceInfo( | 175 scanned_device_infos.push_back(HostScannerOperation::ScannedDeviceInfo( |
171 remote_devices[i], device_status, setup_required)); | 176 remote_devices[i], device_status, setup_required)); |
172 } | 177 } |
173 | 178 |
174 return scanned_device_infos; | 179 return scanned_device_infos; |
175 } | 180 } |
176 | 181 |
177 } // namespace | 182 } // namespace |
178 | 183 |
179 class HostScannerTest : public testing::Test { | 184 class HostScannerTest : public NetworkStateTest { |
180 protected: | 185 protected: |
181 HostScannerTest() | 186 HostScannerTest() |
182 : test_devices_(cryptauth::GenerateTestRemoteDevices(4)), | 187 : test_devices_(cryptauth::GenerateTestRemoteDevices(4)), |
183 test_scanned_device_infos(CreateFakeScannedDeviceInfos(test_devices_)) { | 188 test_scanned_device_infos(CreateFakeScannedDeviceInfos(test_devices_)) { |
184 } | 189 } |
185 | 190 |
186 void SetUp() override { | 191 void SetUp() override { |
| 192 DBusThreadManager::Initialize(); |
| 193 NetworkStateTest::SetUp(); |
| 194 |
187 scanned_device_infos_so_far_.clear(); | 195 scanned_device_infos_so_far_.clear(); |
188 | 196 |
189 fake_tether_host_fetcher_ = base::MakeUnique<FakeTetherHostFetcher>( | 197 fake_tether_host_fetcher_ = base::MakeUnique<FakeTetherHostFetcher>( |
190 test_devices_, false /* synchronously_reply_with_results */); | 198 test_devices_, false /* synchronously_reply_with_results */); |
191 fake_ble_connection_manager_ = base::MakeUnique<FakeBleConnectionManager>(); | 199 fake_ble_connection_manager_ = base::MakeUnique<FakeBleConnectionManager>(); |
192 fake_host_scan_device_prioritizer_ = | 200 fake_host_scan_device_prioritizer_ = |
193 base::MakeUnique<FakeHostScanDevicePrioritizer>(); | 201 base::MakeUnique<FakeHostScanDevicePrioritizer>( |
| 202 network_state_handler()); |
194 mock_tether_host_response_recorder_ = | 203 mock_tether_host_response_recorder_ = |
195 base::MakeUnique<MockTetherHostResponseRecorder>(); | 204 base::MakeUnique<MockTetherHostResponseRecorder>(); |
196 fake_notification_presenter_ = | 205 fake_notification_presenter_ = |
197 base::MakeUnique<FakeNotificationPresenter>(); | 206 base::MakeUnique<FakeNotificationPresenter>(); |
198 device_id_tether_network_guid_map_ = | 207 device_id_tether_network_guid_map_ = |
199 base::MakeUnique<DeviceIdTetherNetworkGuidMap>(); | 208 base::MakeUnique<DeviceIdTetherNetworkGuidMap>(); |
200 fake_host_scan_cache_ = base::MakeUnique<FakeHostScanCache>(); | 209 fake_host_scan_cache_ = base::MakeUnique<FakeHostScanCache>(); |
201 | 210 |
202 fake_host_scanner_operation_factory_ = | 211 fake_host_scanner_operation_factory_ = |
203 base::WrapUnique(new FakeHostScannerOperationFactory(test_devices_)); | 212 base::WrapUnique(new FakeHostScannerOperationFactory(test_devices_)); |
(...skipping 10 matching lines...) Expand all Loading... |
214 device_id_tether_network_guid_map_.get(), fake_host_scan_cache_.get(), | 223 device_id_tether_network_guid_map_.get(), fake_host_scan_cache_.get(), |
215 test_clock_.get())); | 224 test_clock_.get())); |
216 | 225 |
217 test_observer_ = base::MakeUnique<TestObserver>(); | 226 test_observer_ = base::MakeUnique<TestObserver>(); |
218 host_scanner_->AddObserver(test_observer_.get()); | 227 host_scanner_->AddObserver(test_observer_.get()); |
219 } | 228 } |
220 | 229 |
221 void TearDown() override { | 230 void TearDown() override { |
222 host_scanner_->RemoveObserver(test_observer_.get()); | 231 host_scanner_->RemoveObserver(test_observer_.get()); |
223 HostScannerOperation::Factory::SetInstanceForTesting(nullptr); | 232 HostScannerOperation::Factory::SetInstanceForTesting(nullptr); |
| 233 |
| 234 fake_host_scan_device_prioritizer_.reset(); |
| 235 ShutdownNetworkState(); |
| 236 NetworkStateTest::TearDown(); |
| 237 DBusThreadManager::Shutdown(); |
224 } | 238 } |
225 | 239 |
226 // Causes |fake_operation| to receive the scan result in | 240 // Causes |fake_operation| to receive the scan result in |
227 // |test_scanned_device_infos| vector at the index |test_device_index| with | 241 // |test_scanned_device_infos| vector at the index |test_device_index| with |
228 // the "final result" value of |is_final_scan_result|. | 242 // the "final result" value of |is_final_scan_result|. |
229 void ReceiveScanResultAndVerifySuccess( | 243 void ReceiveScanResultAndVerifySuccess( |
230 FakeHostScannerOperation& fake_operation, | 244 FakeHostScannerOperation& fake_operation, |
231 size_t test_device_index, | 245 size_t test_device_index, |
232 bool is_final_scan_result) { | 246 bool is_final_scan_result) { |
233 bool already_in_list = false; | 247 bool already_in_list = false; |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
298 if (!status.has_connection_strength() || status.connection_strength() > 4) | 312 if (!status.has_connection_strength() || status.connection_strength() > 4) |
299 EXPECT_EQ(100, cache_item.signal_strength); | 313 EXPECT_EQ(100, cache_item.signal_strength); |
300 else if (status.connection_strength() < 0) | 314 else if (status.connection_strength() < 0) |
301 EXPECT_EQ(0, cache_item.signal_strength); | 315 EXPECT_EQ(0, cache_item.signal_strength); |
302 else | 316 else |
303 EXPECT_EQ(status.connection_strength() * 25, cache_item.signal_strength); | 317 EXPECT_EQ(status.connection_strength() * 25, cache_item.signal_strength); |
304 | 318 |
305 EXPECT_EQ(scanned_device_info.setup_required, cache_item.setup_required); | 319 EXPECT_EQ(scanned_device_info.setup_required, cache_item.setup_required); |
306 } | 320 } |
307 | 321 |
| 322 const base::test::ScopedTaskEnvironment scoped_task_environment_; |
308 const std::vector<cryptauth::RemoteDevice> test_devices_; | 323 const std::vector<cryptauth::RemoteDevice> test_devices_; |
309 const std::vector<HostScannerOperation::ScannedDeviceInfo> | 324 const std::vector<HostScannerOperation::ScannedDeviceInfo> |
310 test_scanned_device_infos; | 325 test_scanned_device_infos; |
311 | 326 |
312 std::unique_ptr<FakeTetherHostFetcher> fake_tether_host_fetcher_; | 327 std::unique_ptr<FakeTetherHostFetcher> fake_tether_host_fetcher_; |
313 std::unique_ptr<FakeBleConnectionManager> fake_ble_connection_manager_; | 328 std::unique_ptr<FakeBleConnectionManager> fake_ble_connection_manager_; |
314 std::unique_ptr<HostScanDevicePrioritizer> fake_host_scan_device_prioritizer_; | 329 std::unique_ptr<HostScanDevicePrioritizer> fake_host_scan_device_prioritizer_; |
315 std::unique_ptr<MockTetherHostResponseRecorder> | 330 std::unique_ptr<MockTetherHostResponseRecorder> |
316 mock_tether_host_response_recorder_; | 331 mock_tether_host_response_recorder_; |
317 std::unique_ptr<FakeNotificationPresenter> fake_notification_presenter_; | 332 std::unique_ptr<FakeNotificationPresenter> fake_notification_presenter_; |
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
566 EXPECT_TRUE(host_scanner_->HasRecentlyScanned()); | 581 EXPECT_TRUE(host_scanner_->HasRecentlyScanned()); |
567 | 582 |
568 // Move past the limit. | 583 // Move past the limit. |
569 test_clock_->Advance(base::TimeDelta::FromSeconds(1)); | 584 test_clock_->Advance(base::TimeDelta::FromSeconds(1)); |
570 EXPECT_FALSE(host_scanner_->HasRecentlyScanned()); | 585 EXPECT_FALSE(host_scanner_->HasRecentlyScanned()); |
571 } | 586 } |
572 | 587 |
573 } // namespace tether | 588 } // namespace tether |
574 | 589 |
575 } // namespace chromeos | 590 } // namespace chromeos |
OLD | NEW |