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_scan_device_prioritizer.h" | 5 #include "chromeos/components/tether/host_scan_device_prioritizer.h" |
6 | 6 |
| 7 #include "chromeos/components/tether/tether_host_response_recorder.h" |
7 #include "components/cryptauth/remote_device_test_util.h" | 8 #include "components/cryptauth/remote_device_test_util.h" |
8 #include "components/prefs/testing_pref_service.h" | 9 #include "components/prefs/testing_pref_service.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
10 | 11 |
11 namespace chromeos { | 12 namespace chromeos { |
12 | 13 |
13 namespace tether { | 14 namespace tether { |
14 | 15 |
15 class HostScanDevicePrioritizerTest : public testing::Test { | 16 class HostScanDevicePrioritizerTest : public testing::Test { |
16 protected: | 17 protected: |
17 HostScanDevicePrioritizerTest() | 18 HostScanDevicePrioritizerTest() |
18 : test_devices_(cryptauth::GenerateTestRemoteDevices(10)) {} | 19 : test_devices_(cryptauth::GenerateTestRemoteDevices(10)) {} |
19 | 20 |
20 void SetUp() override { | 21 void SetUp() override { |
21 pref_service_ = base::MakeUnique<TestingPrefServiceSimple>(); | 22 pref_service_ = base::MakeUnique<TestingPrefServiceSimple>(); |
22 HostScanDevicePrioritizer::RegisterPrefs(pref_service_->registry()); | 23 TetherHostResponseRecorder::RegisterPrefs(pref_service_->registry()); |
23 | 24 |
24 prioritizer_ = | 25 recorder_ = |
25 base::MakeUnique<HostScanDevicePrioritizer>(pref_service_.get()); | 26 base::MakeUnique<TetherHostResponseRecorder>(pref_service_.get()); |
| 27 |
| 28 prioritizer_ = base::MakeUnique<HostScanDevicePrioritizer>(recorder_.get()); |
26 } | 29 } |
27 | 30 |
28 const std::vector<cryptauth::RemoteDevice> test_devices_; | 31 const std::vector<cryptauth::RemoteDevice> test_devices_; |
29 | 32 |
30 std::unique_ptr<TestingPrefServiceSimple> pref_service_; | 33 std::unique_ptr<TestingPrefServiceSimple> pref_service_; |
| 34 std::unique_ptr<TetherHostResponseRecorder> recorder_; |
| 35 |
31 std::unique_ptr<HostScanDevicePrioritizer> prioritizer_; | 36 std::unique_ptr<HostScanDevicePrioritizer> prioritizer_; |
32 | 37 |
33 private: | 38 private: |
34 DISALLOW_COPY_AND_ASSIGN(HostScanDevicePrioritizerTest); | 39 DISALLOW_COPY_AND_ASSIGN(HostScanDevicePrioritizerTest); |
35 }; | 40 }; |
36 | 41 |
37 TEST_F(HostScanDevicePrioritizerTest, TestOnlyTetherAvailabilityResponses) { | 42 TEST_F(HostScanDevicePrioritizerTest, TestOnlyTetherAvailabilityResponses) { |
38 // Receive TetherAvailabilityResponses from devices 0-4. | 43 // Receive TetherAvailabilityResponses from devices 0-4. |
39 prioritizer_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[0]); | 44 recorder_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[0]); |
40 prioritizer_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[1]); | 45 recorder_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[1]); |
41 prioritizer_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[2]); | 46 recorder_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[2]); |
42 prioritizer_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[3]); | 47 recorder_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[3]); |
43 prioritizer_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[4]); | 48 recorder_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[4]); |
44 | 49 |
45 // Do not receive a ConnectTetheringResponse. | 50 // Do not receive a ConnectTetheringResponse. |
46 | 51 |
47 std::vector<cryptauth::RemoteDevice> test_vector = | 52 std::vector<cryptauth::RemoteDevice> test_vector = |
48 std::vector<cryptauth::RemoteDevice>{test_devices_[6], test_devices_[5], | 53 std::vector<cryptauth::RemoteDevice>{test_devices_[6], test_devices_[5], |
49 test_devices_[4], test_devices_[3], | 54 test_devices_[4], test_devices_[3], |
50 test_devices_[2], test_devices_[1], | 55 test_devices_[2], test_devices_[1], |
51 test_devices_[0]}; | 56 test_devices_[0]}; |
52 | 57 |
53 prioritizer_->SortByHostScanOrder(&test_vector); | 58 prioritizer_->SortByHostScanOrder(&test_vector); |
54 EXPECT_EQ((std::vector<cryptauth::RemoteDevice>{ | 59 EXPECT_EQ((std::vector<cryptauth::RemoteDevice>{ |
55 test_devices_[4], test_devices_[3], test_devices_[2], | 60 test_devices_[4], test_devices_[3], test_devices_[2], |
56 test_devices_[1], test_devices_[0], test_devices_[6], | 61 test_devices_[1], test_devices_[0], test_devices_[6], |
57 test_devices_[5]}), | 62 test_devices_[5]}), |
58 test_vector); | 63 test_vector); |
59 } | 64 } |
60 | 65 |
61 TEST_F(HostScanDevicePrioritizerTest, TestBothTypesOfResponses) { | 66 TEST_F(HostScanDevicePrioritizerTest, TestBothTypesOfResponses) { |
62 // Receive TetherAvailabilityResponses from devices 0-4. | 67 // Receive TetherAvailabilityResponses from devices 0-4. |
63 prioritizer_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[0]); | 68 recorder_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[0]); |
64 prioritizer_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[1]); | 69 recorder_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[1]); |
65 prioritizer_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[2]); | 70 recorder_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[2]); |
66 prioritizer_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[3]); | 71 recorder_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[3]); |
67 prioritizer_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[4]); | 72 recorder_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[4]); |
68 | 73 |
69 // Receive ConnectTetheringResponse from device 0. | 74 // Receive ConnectTetheringResponse from device 0. |
70 prioritizer_->RecordSuccessfulConnectTetheringResponse(test_devices_[0]); | 75 recorder_->RecordSuccessfulConnectTetheringResponse(test_devices_[0]); |
71 | 76 |
72 std::vector<cryptauth::RemoteDevice> test_vector = | 77 std::vector<cryptauth::RemoteDevice> test_vector = |
73 std::vector<cryptauth::RemoteDevice>{test_devices_[6], test_devices_[5], | 78 std::vector<cryptauth::RemoteDevice>{test_devices_[6], test_devices_[5], |
74 test_devices_[4], test_devices_[3], | 79 test_devices_[4], test_devices_[3], |
75 test_devices_[2], test_devices_[1], | 80 test_devices_[2], test_devices_[1], |
76 test_devices_[0]}; | 81 test_devices_[0]}; |
77 | 82 |
78 prioritizer_->SortByHostScanOrder(&test_vector); | 83 prioritizer_->SortByHostScanOrder(&test_vector); |
79 EXPECT_EQ((std::vector<cryptauth::RemoteDevice>{ | 84 EXPECT_EQ((std::vector<cryptauth::RemoteDevice>{ |
80 test_devices_[0], test_devices_[4], test_devices_[3], | 85 test_devices_[0], test_devices_[4], test_devices_[3], |
81 test_devices_[2], test_devices_[1], test_devices_[6], | 86 test_devices_[2], test_devices_[1], test_devices_[6], |
82 test_devices_[5]}), | 87 test_devices_[5]}), |
83 test_vector); | 88 test_vector); |
84 } | 89 } |
85 | 90 |
86 TEST_F(HostScanDevicePrioritizerTest, TestBothTypesOfResponses_DifferentOrder) { | 91 TEST_F(HostScanDevicePrioritizerTest, TestBothTypesOfResponses_DifferentOrder) { |
87 // Receive different order. | 92 // Receive different order. |
88 prioritizer_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[0]); | 93 recorder_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[0]); |
89 prioritizer_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[2]); | 94 recorder_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[2]); |
90 prioritizer_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[1]); | 95 recorder_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[1]); |
91 prioritizer_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[4]); | 96 recorder_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[4]); |
92 prioritizer_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[3]); | 97 recorder_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[3]); |
93 | 98 |
94 // Receive ConnectTetheringResponse from device 1. | 99 // Receive ConnectTetheringResponse from device 1. |
95 prioritizer_->RecordSuccessfulConnectTetheringResponse(test_devices_[1]); | 100 recorder_->RecordSuccessfulConnectTetheringResponse(test_devices_[1]); |
96 | 101 |
97 std::vector<cryptauth::RemoteDevice> test_vector = | 102 std::vector<cryptauth::RemoteDevice> test_vector = |
98 std::vector<cryptauth::RemoteDevice>{test_devices_[9], test_devices_[8], | 103 std::vector<cryptauth::RemoteDevice>{test_devices_[9], test_devices_[8], |
99 test_devices_[7], test_devices_[6], | 104 test_devices_[7], test_devices_[6], |
100 test_devices_[5], test_devices_[4], | 105 test_devices_[5], test_devices_[4], |
101 test_devices_[3], test_devices_[2], | 106 test_devices_[3], test_devices_[2], |
102 test_devices_[1], test_devices_[0]}; | 107 test_devices_[1], test_devices_[0]}; |
103 | 108 |
104 prioritizer_->SortByHostScanOrder(&test_vector); | 109 prioritizer_->SortByHostScanOrder(&test_vector); |
105 EXPECT_EQ((std::vector<cryptauth::RemoteDevice>{ | 110 EXPECT_EQ((std::vector<cryptauth::RemoteDevice>{ |
106 test_devices_[1], test_devices_[3], test_devices_[4], | 111 test_devices_[1], test_devices_[3], test_devices_[4], |
107 test_devices_[2], test_devices_[0], test_devices_[9], | 112 test_devices_[2], test_devices_[0], test_devices_[9], |
108 test_devices_[8], test_devices_[7], test_devices_[6], | 113 test_devices_[8], test_devices_[7], test_devices_[6], |
109 test_devices_[5]}), | 114 test_devices_[5]}), |
110 test_vector); | 115 test_vector); |
111 } | 116 } |
112 | 117 |
113 } // namespace tether | 118 } // namespace tether |
114 | 119 |
115 } // namespace cryptauth | 120 } // namespace cryptauth |
OLD | NEW |