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/tether_host_response_recorder.h" | 5 #include "chromeos/components/tether/tether_host_response_recorder.h" |
6 | 6 |
7 #include "components/cryptauth/remote_device_test_util.h" | 7 #include "components/cryptauth/remote_device_test_util.h" |
8 #include "components/prefs/testing_pref_service.h" | 8 #include "components/prefs/testing_pref_service.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
10 | 10 |
11 namespace chromeos { | 11 namespace chromeos { |
12 | 12 |
13 namespace tether { | 13 namespace tether { |
14 | 14 |
15 namespace { | |
16 | |
17 class TestObserver : public TetherHostResponseRecorder::Observer { | |
18 public: | |
19 TestObserver() : num_callbacks_(0) {} | |
20 ~TestObserver() {} | |
21 | |
22 uint32_t num_callbacks() { return num_callbacks_; } | |
23 | |
24 // TetherHostResponseRecorder::Observer: | |
25 void OnPreviouslyConnectedHostIdsChanged() override { num_callbacks_++; } | |
26 | |
27 private: | |
28 uint32_t num_callbacks_; | |
29 }; | |
30 | |
31 } // namespace | |
32 | |
33 class TetherHostResponseRecorderTest : public testing::Test { | 15 class TetherHostResponseRecorderTest : public testing::Test { |
34 protected: | 16 protected: |
35 TetherHostResponseRecorderTest() | 17 TetherHostResponseRecorderTest() |
36 : test_devices_(cryptauth::GenerateTestRemoteDevices(10)) {} | 18 : test_devices_(cryptauth::GenerateTestRemoteDevices(10)) {} |
37 | 19 |
38 void SetUp() override { | 20 void SetUp() override { |
39 pref_service_ = base::MakeUnique<TestingPrefServiceSimple>(); | 21 pref_service_ = base::MakeUnique<TestingPrefServiceSimple>(); |
40 TetherHostResponseRecorder::RegisterPrefs(pref_service_->registry()); | 22 TetherHostResponseRecorder::RegisterPrefs(pref_service_->registry()); |
41 | 23 |
42 recorder_ = | 24 recorder_ = |
43 base::MakeUnique<TetherHostResponseRecorder>(pref_service_.get()); | 25 base::MakeUnique<TetherHostResponseRecorder>(pref_service_.get()); |
44 | |
45 test_observer_ = base::WrapUnique(new TestObserver()); | |
46 recorder_->AddObserver(test_observer_.get()); | |
47 } | 26 } |
48 | 27 |
49 const std::vector<cryptauth::RemoteDevice> test_devices_; | 28 const std::vector<cryptauth::RemoteDevice> test_devices_; |
50 | 29 |
51 std::unique_ptr<TestingPrefServiceSimple> pref_service_; | 30 std::unique_ptr<TestingPrefServiceSimple> pref_service_; |
52 std::unique_ptr<TestObserver> test_observer_; | |
53 | |
54 std::unique_ptr<TetherHostResponseRecorder> recorder_; | 31 std::unique_ptr<TetherHostResponseRecorder> recorder_; |
55 | 32 |
56 private: | 33 private: |
57 DISALLOW_COPY_AND_ASSIGN(TetherHostResponseRecorderTest); | 34 DISALLOW_COPY_AND_ASSIGN(TetherHostResponseRecorderTest); |
58 }; | 35 }; |
59 | 36 |
60 TEST_F(TetherHostResponseRecorderTest, TestTetherAvailabilityResponses) { | 37 TEST_F(TetherHostResponseRecorderTest, TestTetherAvailabilityResponses) { |
61 // Receive TetherAvailabilityResponses from devices in the following order: | 38 // Receive TetherAvailabilityResponses from devices in the following order: |
62 // 0, 2, 4, 6, 8, 1, 3, 5, 7, 9 | 39 // 0, 2, 4, 6, 8, 1, 3, 5, 7, 9 |
63 recorder_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[0]); | 40 recorder_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[0]); |
(...skipping 10 matching lines...) Expand all Loading... |
74 // The order, from most recent to least recent, should be: | 51 // The order, from most recent to least recent, should be: |
75 // 9, 7, 5, 3, 1, 8, 6, 4, 2, 0 | 52 // 9, 7, 5, 3, 1, 8, 6, 4, 2, 0 |
76 EXPECT_EQ( | 53 EXPECT_EQ( |
77 (std::vector<std::string>{ | 54 (std::vector<std::string>{ |
78 test_devices_[9].GetDeviceId(), test_devices_[7].GetDeviceId(), | 55 test_devices_[9].GetDeviceId(), test_devices_[7].GetDeviceId(), |
79 test_devices_[5].GetDeviceId(), test_devices_[3].GetDeviceId(), | 56 test_devices_[5].GetDeviceId(), test_devices_[3].GetDeviceId(), |
80 test_devices_[1].GetDeviceId(), test_devices_[8].GetDeviceId(), | 57 test_devices_[1].GetDeviceId(), test_devices_[8].GetDeviceId(), |
81 test_devices_[6].GetDeviceId(), test_devices_[4].GetDeviceId(), | 58 test_devices_[6].GetDeviceId(), test_devices_[4].GetDeviceId(), |
82 test_devices_[2].GetDeviceId(), test_devices_[0].GetDeviceId()}), | 59 test_devices_[2].GetDeviceId(), test_devices_[0].GetDeviceId()}), |
83 recorder_->GetPreviouslyAvailableHostIds()); | 60 recorder_->GetPreviouslyAvailableHostIds()); |
84 | |
85 EXPECT_EQ(0u, test_observer_->num_callbacks()); | |
86 } | 61 } |
87 | 62 |
88 TEST_F(TetherHostResponseRecorderTest, TestConnectTetheringResponses) { | 63 TEST_F(TetherHostResponseRecorderTest, TestConnectTetheringResponses) { |
89 // Receive TetherAvailabilityResponses from devices in the following order: | 64 // Receive TetherAvailabilityResponses from devices in the following order: |
90 // 0, 2, 4, 6, 8, 1, 3, 5, 7, 9 | 65 // 0, 2, 4, 6, 8, 1, 3, 5, 7, 9 |
91 recorder_->RecordSuccessfulConnectTetheringResponse(test_devices_[0]); | 66 recorder_->RecordSuccessfulConnectTetheringResponse(test_devices_[0]); |
92 EXPECT_EQ(1u, test_observer_->num_callbacks()); | |
93 recorder_->RecordSuccessfulConnectTetheringResponse(test_devices_[2]); | 67 recorder_->RecordSuccessfulConnectTetheringResponse(test_devices_[2]); |
94 EXPECT_EQ(2u, test_observer_->num_callbacks()); | |
95 recorder_->RecordSuccessfulConnectTetheringResponse(test_devices_[4]); | 68 recorder_->RecordSuccessfulConnectTetheringResponse(test_devices_[4]); |
96 EXPECT_EQ(3u, test_observer_->num_callbacks()); | |
97 recorder_->RecordSuccessfulConnectTetheringResponse(test_devices_[6]); | 69 recorder_->RecordSuccessfulConnectTetheringResponse(test_devices_[6]); |
98 EXPECT_EQ(4u, test_observer_->num_callbacks()); | |
99 recorder_->RecordSuccessfulConnectTetheringResponse(test_devices_[8]); | 70 recorder_->RecordSuccessfulConnectTetheringResponse(test_devices_[8]); |
100 EXPECT_EQ(5u, test_observer_->num_callbacks()); | |
101 recorder_->RecordSuccessfulConnectTetheringResponse(test_devices_[1]); | 71 recorder_->RecordSuccessfulConnectTetheringResponse(test_devices_[1]); |
102 EXPECT_EQ(6u, test_observer_->num_callbacks()); | |
103 recorder_->RecordSuccessfulConnectTetheringResponse(test_devices_[3]); | 72 recorder_->RecordSuccessfulConnectTetheringResponse(test_devices_[3]); |
104 EXPECT_EQ(7u, test_observer_->num_callbacks()); | |
105 recorder_->RecordSuccessfulConnectTetheringResponse(test_devices_[5]); | 73 recorder_->RecordSuccessfulConnectTetheringResponse(test_devices_[5]); |
106 EXPECT_EQ(8u, test_observer_->num_callbacks()); | |
107 recorder_->RecordSuccessfulConnectTetheringResponse(test_devices_[7]); | 74 recorder_->RecordSuccessfulConnectTetheringResponse(test_devices_[7]); |
108 EXPECT_EQ(9u, test_observer_->num_callbacks()); | |
109 recorder_->RecordSuccessfulConnectTetheringResponse(test_devices_[9]); | 75 recorder_->RecordSuccessfulConnectTetheringResponse(test_devices_[9]); |
110 EXPECT_EQ(10u, test_observer_->num_callbacks()); | |
111 | 76 |
112 // The order, from most recent to least recent, should be: | 77 // The order, from most recent to least recent, should be: |
113 // 9, 7, 5, 3, 1, 8, 6, 4, 2, 0 | 78 // 9, 7, 5, 3, 1, 8, 6, 4, 2, 0 |
114 EXPECT_EQ( | 79 EXPECT_EQ( |
115 (std::vector<std::string>{ | 80 (std::vector<std::string>{ |
116 test_devices_[9].GetDeviceId(), test_devices_[7].GetDeviceId(), | 81 test_devices_[9].GetDeviceId(), test_devices_[7].GetDeviceId(), |
117 test_devices_[5].GetDeviceId(), test_devices_[3].GetDeviceId(), | 82 test_devices_[5].GetDeviceId(), test_devices_[3].GetDeviceId(), |
118 test_devices_[1].GetDeviceId(), test_devices_[8].GetDeviceId(), | 83 test_devices_[1].GetDeviceId(), test_devices_[8].GetDeviceId(), |
119 test_devices_[6].GetDeviceId(), test_devices_[4].GetDeviceId(), | 84 test_devices_[6].GetDeviceId(), test_devices_[4].GetDeviceId(), |
120 test_devices_[2].GetDeviceId(), test_devices_[0].GetDeviceId()}), | 85 test_devices_[2].GetDeviceId(), test_devices_[0].GetDeviceId()}), |
121 recorder_->GetPreviouslyConnectedHostIds()); | 86 recorder_->GetPreviouslyConnectedHostIds()); |
122 | |
123 EXPECT_EQ(10u, test_observer_->num_callbacks()); | |
124 } | 87 } |
125 | 88 |
126 TEST_F(TetherHostResponseRecorderTest, TestBothResponseTypes) { | 89 TEST_F(TetherHostResponseRecorderTest, TestBothResponseTypes) { |
127 // Receive TetherAvailabilityResponses from devices 0, 1, and 2. | 90 // Receive TetherAvailabilityResponses from devices 0, 1, and 2. |
128 recorder_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[0]); | 91 recorder_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[0]); |
129 recorder_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[1]); | 92 recorder_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[1]); |
130 recorder_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[2]); | 93 recorder_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[2]); |
131 | 94 |
132 // Receive a ConnectTetheringResponse from device 2. | 95 // Receive a ConnectTetheringResponse from device 2. |
133 recorder_->RecordSuccessfulConnectTetheringResponse(test_devices_[2]); | 96 recorder_->RecordSuccessfulConnectTetheringResponse(test_devices_[2]); |
134 EXPECT_EQ(1u, test_observer_->num_callbacks()); | |
135 | 97 |
136 // Receive TetherAvailabilityResponses from devices 0, 1, and 3. | 98 // Receive TetherAvailabilityResponses from devices 0, 1, and 3. |
137 recorder_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[0]); | 99 recorder_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[0]); |
138 recorder_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[1]); | 100 recorder_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[1]); |
139 recorder_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[3]); | 101 recorder_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[3]); |
140 | 102 |
141 // Receive a ConnectTetheringResponse from device 0. | 103 // Receive a ConnectTetheringResponse from device 0. |
142 recorder_->RecordSuccessfulConnectTetheringResponse(test_devices_[0]); | 104 recorder_->RecordSuccessfulConnectTetheringResponse(test_devices_[0]); |
143 EXPECT_EQ(2u, test_observer_->num_callbacks()); | |
144 | |
145 // Receive another ConnectTetheringResponse from device 0. Since it was | |
146 // already in the front of the list, this should not trigger a callback. | |
147 recorder_->RecordSuccessfulConnectTetheringResponse(test_devices_[0]); | |
148 EXPECT_EQ(2u, test_observer_->num_callbacks()); | |
149 | 105 |
150 // The order for TetherAvailabilityResponses, from most recent to least | 106 // The order for TetherAvailabilityResponses, from most recent to least |
151 // recent, should be: | 107 // recent, should be: |
152 // 3, 1, 0, 2 | 108 // 3, 1, 0, 2 |
153 EXPECT_EQ( | 109 EXPECT_EQ( |
154 (std::vector<std::string>{ | 110 (std::vector<std::string>{ |
155 test_devices_[3].GetDeviceId(), test_devices_[1].GetDeviceId(), | 111 test_devices_[3].GetDeviceId(), test_devices_[1].GetDeviceId(), |
156 test_devices_[0].GetDeviceId(), test_devices_[2].GetDeviceId()}), | 112 test_devices_[0].GetDeviceId(), test_devices_[2].GetDeviceId()}), |
157 recorder_->GetPreviouslyAvailableHostIds()); | 113 recorder_->GetPreviouslyAvailableHostIds()); |
158 | 114 |
159 // The order for ConnectTetheringResponses, from most recent to least | 115 // The order for ConnectTetheringResponses, from most recent to least |
160 // recent, should be: | 116 // recent, should be: |
161 // 0, 2 | 117 // 0, 2 |
162 EXPECT_EQ((std::vector<std::string>{test_devices_[0].GetDeviceId(), | 118 EXPECT_EQ((std::vector<std::string>{test_devices_[0].GetDeviceId(), |
163 test_devices_[2].GetDeviceId()}), | 119 test_devices_[2].GetDeviceId()}), |
164 recorder_->GetPreviouslyConnectedHostIds()); | 120 recorder_->GetPreviouslyConnectedHostIds()); |
165 | |
166 EXPECT_EQ(2u, test_observer_->num_callbacks()); | |
167 } | 121 } |
168 | 122 |
169 } // namespace tether | 123 } // namespace tether |
170 | 124 |
171 } // namespace chromeos | 125 } // namespace cryptauth |
OLD | NEW |