OLD | NEW |
---|---|
1 // Copyright 2014 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_scheduler.h" | 5 #include "chromeos/components/tether/host_scan_scheduler.h" |
6 | 6 |
7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
8 #include "chromeos/components/tether/host_scanner.h" | 8 #include "chromeos/components/tether/host_scanner.h" |
9 #include "chromeos/dbus/power_manager_client.h" | 9 #include "chromeos/dbus/power_manager_client.h" |
10 #include "chromeos/login/login_state.h" | 10 #include "chromeos/login/login_state.h" |
11 #include "chromeos/network/network_handler.h" | 11 #include "chromeos/network/network_handler.h" |
12 #include "chromeos/network/network_state.h" | 12 #include "chromeos/network/network_state.h" |
13 #include "chromeos/network/network_state_handler.h" | 13 #include "chromeos/network/network_state_handler.h" |
14 #include "components/cryptauth/cryptauth_device_manager.h" | 14 #include "components/cryptauth/cryptauth_device_manager.h" |
15 #include "testing/gmock/include/gmock/gmock.h" | 15 #include "testing/gmock/include/gmock/gmock.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
17 | 17 |
18 namespace chromeos { | 18 namespace chromeos { |
19 | 19 |
20 namespace tether { | 20 namespace tether { |
21 | 21 |
22 namespace {} // namespace | 22 namespace {} // namespace |
Ryan Hansberry
2016/12/19 21:13:57
Is this namespace needed?
Kyle Horimoto
2016/12/19 21:17:46
Done.
| |
23 | 23 |
24 class HostScanSchedulerTest : public testing::Test { | 24 class HostScanSchedulerTest : public testing::Test { |
25 protected: | 25 protected: |
26 class TestContext : public HostScanScheduler::Context { | 26 class TestDelegate : public HostScanScheduler::Delegate { |
27 public: | 27 public: |
28 TestContext(HostScanSchedulerTest* test) | 28 TestDelegate(HostScanSchedulerTest* test) |
29 : test_(test), | 29 : test_(test), |
30 observer_(nullptr), | 30 observer_(nullptr), |
31 is_authenticated_user_logged_in(true), | 31 is_authenticated_user_logged_in(true), |
32 is_network_connected_or_connecting(false), | 32 is_network_connected_or_connecting(false), |
33 are_tether_hosts_synced(true) {} | 33 are_tether_hosts_synced(true) {} |
34 | 34 |
35 void AddObserver(HostScanScheduler* host_scan_scheduler) override { | 35 void AddObserver(HostScanScheduler* host_scan_scheduler) override { |
36 observer_ = host_scan_scheduler; | 36 observer_ = host_scan_scheduler; |
37 } | 37 } |
38 | 38 |
(...skipping 14 matching lines...) Expand all Loading... | |
53 | 53 |
54 bool AreTetherHostsSynced() const override { | 54 bool AreTetherHostsSynced() const override { |
55 return are_tether_hosts_synced; | 55 return are_tether_hosts_synced; |
56 } | 56 } |
57 | 57 |
58 bool IsObserverSet() const { | 58 bool IsObserverSet() const { |
59 return observer_ != nullptr && | 59 return observer_ != nullptr && |
60 observer_ == test_->host_scan_scheduler_.get(); | 60 observer_ == test_->host_scan_scheduler_.get(); |
61 } | 61 } |
62 | 62 |
63 void SetIsAuthenticatedUserLoggedIn(bool value) { | 63 void set_is_authenticated_user_logged_in(bool value) { |
64 is_authenticated_user_logged_in = value; | 64 is_authenticated_user_logged_in = value; |
65 } | 65 } |
66 | 66 |
67 void SetIsNetworkConnectedOrConnecting(bool value) { | 67 void set_is_network_connected_or_connecting(bool value) { |
68 is_network_connected_or_connecting = value; | 68 is_network_connected_or_connecting = value; |
69 } | 69 } |
70 | 70 |
71 void AreTetherHostsSynced(bool value) { are_tether_hosts_synced = value; } | 71 void set_are_tether_hosts_synced(bool value) { |
72 are_tether_hosts_synced = value; | |
73 } | |
72 | 74 |
73 private: | 75 private: |
74 const HostScanSchedulerTest* test_; | 76 const HostScanSchedulerTest* test_; |
75 | 77 |
76 HostScanScheduler* observer_; | 78 HostScanScheduler* observer_; |
77 bool is_authenticated_user_logged_in; | 79 bool is_authenticated_user_logged_in; |
78 bool is_network_connected_or_connecting; | 80 bool is_network_connected_or_connecting; |
79 bool are_tether_hosts_synced; | 81 bool are_tether_hosts_synced; |
80 }; | 82 }; |
81 | 83 |
82 class MockHostScanner : public HostScanner { | 84 class FakeHostScanner : public HostScanner { |
83 public: | 85 public: |
84 MockHostScanner() : num_scans_started_(0) {} | 86 FakeHostScanner() : num_scans_started_(0) {} |
85 ~MockHostScanner() override {} | 87 ~FakeHostScanner() override {} |
86 | 88 |
87 void StartScan() override { num_scans_started_++; } | 89 void StartScan() override { num_scans_started_++; } |
88 | 90 |
89 int num_scans_started() { return num_scans_started_; } | 91 int num_scans_started() { return num_scans_started_; } |
90 | 92 |
91 private: | 93 private: |
92 int num_scans_started_; | 94 int num_scans_started_; |
93 }; | 95 }; |
94 | 96 |
95 HostScanSchedulerTest() {} | 97 HostScanSchedulerTest() {} |
96 | 98 |
97 void SetUp() override { | 99 void SetUp() override { |
98 test_context_ = new TestContext(this); | 100 test_delegate_ = new TestDelegate(this); |
99 mock_host_scanner_ = new MockHostScanner(); | 101 fake_host_scanner_ = new FakeHostScanner(); |
100 | 102 |
101 host_scan_scheduler_.reset(new HostScanScheduler( | 103 host_scan_scheduler_.reset( |
102 base::WrapUnique(test_context_), base::WrapUnique(mock_host_scanner_))); | 104 new HostScanScheduler(base::WrapUnique(test_delegate_), |
105 base::WrapUnique(fake_host_scanner_))); | |
103 | 106 |
104 EXPECT_FALSE(test_context_->IsObserverSet()); | 107 EXPECT_FALSE(test_delegate_->IsObserverSet()); |
105 host_scan_scheduler_->InitializeAutomaticScans(); | 108 host_scan_scheduler_->InitializeAutomaticScans(); |
106 EXPECT_TRUE(test_context_->IsObserverSet()); | 109 EXPECT_TRUE(test_delegate_->IsObserverSet()); |
107 } | 110 } |
108 | 111 |
109 void TearDown() override { | 112 void TearDown() override { |
110 EXPECT_TRUE(test_context_->IsObserverSet()); | 113 EXPECT_TRUE(test_delegate_->IsObserverSet()); |
111 host_scan_scheduler_.reset(); | 114 host_scan_scheduler_.reset(); |
112 } | 115 } |
113 | 116 |
114 void TestScheduleScanNowIfPossible(bool is_authenticated_user_logged_in, | 117 void TestScheduleScanNowIfPossible(bool is_authenticated_user_logged_in, |
115 bool is_network_connected_or_connecting, | 118 bool is_network_connected_or_connecting, |
116 bool are_tether_hosts_synced, | 119 bool are_tether_hosts_synced, |
117 int num_expected_scans) { | 120 int num_expected_scans) { |
118 test_context_->SetIsAuthenticatedUserLoggedIn( | 121 test_delegate_->set_is_authenticated_user_logged_in( |
119 is_authenticated_user_logged_in); | 122 is_authenticated_user_logged_in); |
120 test_context_->SetIsNetworkConnectedOrConnecting( | 123 test_delegate_->set_is_network_connected_or_connecting( |
121 is_network_connected_or_connecting); | 124 is_network_connected_or_connecting); |
122 test_context_->AreTetherHostsSynced(are_tether_hosts_synced); | 125 test_delegate_->set_are_tether_hosts_synced(are_tether_hosts_synced); |
123 host_scan_scheduler_->ScheduleScanNowIfPossible(); | 126 host_scan_scheduler_->ScheduleScanNowIfPossible(); |
124 EXPECT_EQ(num_expected_scans, mock_host_scanner_->num_scans_started()); | 127 EXPECT_EQ(num_expected_scans, fake_host_scanner_->num_scans_started()); |
125 } | 128 } |
126 | 129 |
127 std::unique_ptr<HostScanScheduler> host_scan_scheduler_; | 130 std::unique_ptr<HostScanScheduler> host_scan_scheduler_; |
128 | 131 |
129 TestContext* test_context_; | 132 TestDelegate* test_delegate_; |
130 MockHostScanner* mock_host_scanner_; | 133 FakeHostScanner* fake_host_scanner_; |
131 }; | 134 }; |
132 | 135 |
133 TEST_F(HostScanSchedulerTest, TestObserverAddedAndRemoved) { | 136 TEST_F(HostScanSchedulerTest, TestObserverAddedAndRemoved) { |
134 // Intentionally empty. This test just ensures that adding/removing observers | 137 // Intentionally empty. This test just ensures that adding/removing observers |
135 // (in setUp() and tearDown()) works. | 138 // (in setUp() and tearDown()) works. |
136 } | 139 } |
137 | 140 |
138 TEST_F(HostScanSchedulerTest, TestScheduleScanNowIfPossible) { | 141 TEST_F(HostScanSchedulerTest, TestScheduleScanNowIfPossible) { |
139 EXPECT_EQ(0, mock_host_scanner_->num_scans_started()); | 142 EXPECT_EQ(0, fake_host_scanner_->num_scans_started()); |
140 | 143 |
141 // A scan should only be started when an authenticated user is logged in, | 144 // A scan should only be started when an authenticated user is logged in, |
142 // the network is not connected/connecting, and tether hosts are synced. | 145 // the network is not connected/connecting, and tether hosts are synced. |
143 TestScheduleScanNowIfPossible(false, false, false, 0); | 146 TestScheduleScanNowIfPossible(false, false, false, 0); |
144 TestScheduleScanNowIfPossible(true, false, false, 0); | 147 TestScheduleScanNowIfPossible(true, false, false, 0); |
145 TestScheduleScanNowIfPossible(false, true, false, 0); | 148 TestScheduleScanNowIfPossible(false, true, false, 0); |
146 TestScheduleScanNowIfPossible(false, false, true, 0); | 149 TestScheduleScanNowIfPossible(false, false, true, 0); |
147 TestScheduleScanNowIfPossible(true, true, false, 0); | 150 TestScheduleScanNowIfPossible(true, true, false, 0); |
148 TestScheduleScanNowIfPossible(true, false, true, 1); | 151 TestScheduleScanNowIfPossible(true, false, true, 1); |
149 TestScheduleScanNowIfPossible(false, true, true, 1); | 152 TestScheduleScanNowIfPossible(false, true, true, 1); |
150 TestScheduleScanNowIfPossible(true, true, true, 1); | 153 TestScheduleScanNowIfPossible(true, true, true, 1); |
151 } | 154 } |
152 | 155 |
153 TEST_F(HostScanSchedulerTest, TestLoggedInStateChange) { | 156 TEST_F(HostScanSchedulerTest, TestLoggedInStateChange) { |
154 EXPECT_EQ(0, mock_host_scanner_->num_scans_started()); | 157 EXPECT_EQ(0, fake_host_scanner_->num_scans_started()); |
155 | 158 |
156 test_context_->SetIsAuthenticatedUserLoggedIn(true); | 159 test_delegate_->set_is_authenticated_user_logged_in(true); |
157 test_context_->SetIsNetworkConnectedOrConnecting(false); | 160 test_delegate_->set_is_network_connected_or_connecting(false); |
158 test_context_->AreTetherHostsSynced(true); | 161 test_delegate_->set_are_tether_hosts_synced(true); |
159 | 162 |
160 host_scan_scheduler_->LoggedInStateChanged(); | 163 host_scan_scheduler_->LoggedInStateChanged(); |
161 EXPECT_EQ(1, mock_host_scanner_->num_scans_started()); | 164 EXPECT_EQ(1, fake_host_scanner_->num_scans_started()); |
162 | 165 |
163 // Change a condition so that it should not scan, and re-trigger; there should | 166 // Change a condition so that it should not scan, and re-trigger; there should |
164 // still be only 1 started scan. | 167 // still be only 1 started scan. |
165 test_context_->SetIsAuthenticatedUserLoggedIn(false); | 168 test_delegate_->set_is_authenticated_user_logged_in(false); |
166 host_scan_scheduler_->LoggedInStateChanged(); | 169 host_scan_scheduler_->LoggedInStateChanged(); |
167 EXPECT_EQ(1, mock_host_scanner_->num_scans_started()); | 170 EXPECT_EQ(1, fake_host_scanner_->num_scans_started()); |
168 } | 171 } |
169 | 172 |
170 TEST_F(HostScanSchedulerTest, TestSuspendDone) { | 173 TEST_F(HostScanSchedulerTest, TestSuspendDone) { |
171 EXPECT_EQ(0, mock_host_scanner_->num_scans_started()); | 174 EXPECT_EQ(0, fake_host_scanner_->num_scans_started()); |
172 | 175 |
173 test_context_->SetIsAuthenticatedUserLoggedIn(true); | 176 test_delegate_->set_is_authenticated_user_logged_in(true); |
174 test_context_->SetIsNetworkConnectedOrConnecting(false); | 177 test_delegate_->set_is_network_connected_or_connecting(false); |
175 test_context_->AreTetherHostsSynced(true); | 178 test_delegate_->set_are_tether_hosts_synced(true); |
176 | 179 |
177 host_scan_scheduler_->SuspendDone(base::TimeDelta::FromSeconds(1)); | 180 host_scan_scheduler_->SuspendDone(base::TimeDelta::FromSeconds(1)); |
178 EXPECT_EQ(1, mock_host_scanner_->num_scans_started()); | 181 EXPECT_EQ(1, fake_host_scanner_->num_scans_started()); |
179 | 182 |
180 // Change a condition so that it should not scan, and re-trigger; there should | 183 // Change a condition so that it should not scan, and re-trigger; there should |
181 // still be only 1 started scan. | 184 // still be only 1 started scan. |
182 test_context_->SetIsAuthenticatedUserLoggedIn(false); | 185 test_delegate_->set_is_authenticated_user_logged_in(false); |
183 host_scan_scheduler_->SuspendDone(base::TimeDelta::FromSeconds(1)); | 186 host_scan_scheduler_->SuspendDone(base::TimeDelta::FromSeconds(1)); |
184 EXPECT_EQ(1, mock_host_scanner_->num_scans_started()); | 187 EXPECT_EQ(1, fake_host_scanner_->num_scans_started()); |
185 } | 188 } |
186 | 189 |
187 TEST_F(HostScanSchedulerTest, TestNetworkConnectionStateChanged) { | 190 TEST_F(HostScanSchedulerTest, TestNetworkConnectionStateChanged) { |
188 EXPECT_EQ(0, mock_host_scanner_->num_scans_started()); | 191 EXPECT_EQ(0, fake_host_scanner_->num_scans_started()); |
189 | 192 |
190 test_context_->SetIsAuthenticatedUserLoggedIn(true); | 193 test_delegate_->set_is_authenticated_user_logged_in(true); |
191 test_context_->SetIsNetworkConnectedOrConnecting(false); | 194 test_delegate_->set_is_network_connected_or_connecting(false); |
192 test_context_->AreTetherHostsSynced(true); | 195 test_delegate_->set_are_tether_hosts_synced(true); |
193 | 196 |
194 host_scan_scheduler_->NetworkConnectionStateChanged(nullptr); | 197 host_scan_scheduler_->NetworkConnectionStateChanged(nullptr); |
195 EXPECT_EQ(1, mock_host_scanner_->num_scans_started()); | 198 EXPECT_EQ(1, fake_host_scanner_->num_scans_started()); |
196 | 199 |
197 // Change a condition so that it should not scan, and re-trigger; there should | 200 // Change a condition so that it should not scan, and re-trigger; there should |
198 // still be only 1 started scan. | 201 // still be only 1 started scan. |
199 test_context_->SetIsNetworkConnectedOrConnecting(true); | 202 test_delegate_->set_is_network_connected_or_connecting(true); |
200 host_scan_scheduler_->NetworkConnectionStateChanged(nullptr); | 203 host_scan_scheduler_->NetworkConnectionStateChanged(nullptr); |
201 EXPECT_EQ(1, mock_host_scanner_->num_scans_started()); | 204 EXPECT_EQ(1, fake_host_scanner_->num_scans_started()); |
202 } | 205 } |
203 | 206 |
204 TEST_F(HostScanSchedulerTest, TestOnSyncFinished) { | 207 TEST_F(HostScanSchedulerTest, TestOnSyncFinished) { |
205 EXPECT_EQ(0, mock_host_scanner_->num_scans_started()); | 208 EXPECT_EQ(0, fake_host_scanner_->num_scans_started()); |
206 | 209 |
207 test_context_->SetIsAuthenticatedUserLoggedIn(true); | 210 test_delegate_->set_is_authenticated_user_logged_in(true); |
208 test_context_->SetIsNetworkConnectedOrConnecting(false); | 211 test_delegate_->set_is_network_connected_or_connecting(false); |
209 test_context_->AreTetherHostsSynced(true); | 212 test_delegate_->set_are_tether_hosts_synced(true); |
210 | 213 |
211 host_scan_scheduler_->OnSyncFinished( | 214 host_scan_scheduler_->OnSyncFinished( |
212 cryptauth::CryptAuthDeviceManager::SyncResult::SUCCESS, | 215 cryptauth::CryptAuthDeviceManager::SyncResult::SUCCESS, |
213 cryptauth::CryptAuthDeviceManager::DeviceChangeResult::CHANGED); | 216 cryptauth::CryptAuthDeviceManager::DeviceChangeResult::CHANGED); |
214 EXPECT_EQ(1, mock_host_scanner_->num_scans_started()); | 217 EXPECT_EQ(1, fake_host_scanner_->num_scans_started()); |
215 | 218 |
216 // Change a condition so that it should not scan, and re-trigger; there should | 219 // Change a condition so that it should not scan, and re-trigger; there should |
217 // still be only 1 started scan. | 220 // still be only 1 started scan. |
218 test_context_->AreTetherHostsSynced(false); | 221 test_delegate_->set_are_tether_hosts_synced(false); |
219 host_scan_scheduler_->OnSyncFinished( | 222 host_scan_scheduler_->OnSyncFinished( |
220 cryptauth::CryptAuthDeviceManager::SyncResult::SUCCESS, | 223 cryptauth::CryptAuthDeviceManager::SyncResult::SUCCESS, |
221 cryptauth::CryptAuthDeviceManager::DeviceChangeResult::UNCHANGED); | 224 cryptauth::CryptAuthDeviceManager::DeviceChangeResult::UNCHANGED); |
222 EXPECT_EQ(1, mock_host_scanner_->num_scans_started()); | 225 EXPECT_EQ(1, fake_host_scanner_->num_scans_started()); |
223 } | 226 } |
224 | 227 |
225 } // namespace tether | 228 } // namespace tether |
226 | 229 |
227 } // namespace chromeos | 230 } // namespace chromeos |
OLD | NEW |