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