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