Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(536)

Side by Side Diff: chromeos/components/tether/host_scan_scheduler_unittest.cc

Issue 2587783003: [CrOS Tether] Fix miscellaneous issues with HostScanScheduler. (Closed)
Patch Set: hansberry@ comments. Created 3 years, 12 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chromeos/components/tether/host_scan_scheduler.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
23
24 class HostScanSchedulerTest : public testing::Test { 22 class HostScanSchedulerTest : public testing::Test {
25 protected: 23 protected:
26 class TestContext : public HostScanScheduler::Context { 24 class TestDelegate : public HostScanScheduler::Delegate {
27 public: 25 public:
28 TestContext(HostScanSchedulerTest* test) 26 TestDelegate(HostScanSchedulerTest* test)
29 : test_(test), 27 : test_(test),
30 observer_(nullptr), 28 observer_(nullptr),
31 is_authenticated_user_logged_in(true), 29 is_authenticated_user_logged_in(true),
32 is_network_connected_or_connecting(false), 30 is_network_connected_or_connecting(false),
33 are_tether_hosts_synced(true) {} 31 are_tether_hosts_synced(true) {}
34 32
35 void AddObserver(HostScanScheduler* host_scan_scheduler) override { 33 void AddObserver(HostScanScheduler* host_scan_scheduler) override {
36 observer_ = host_scan_scheduler; 34 observer_ = host_scan_scheduler;
37 } 35 }
38 36
(...skipping 14 matching lines...) Expand all
53 51
54 bool AreTetherHostsSynced() const override { 52 bool AreTetherHostsSynced() const override {
55 return are_tether_hosts_synced; 53 return are_tether_hosts_synced;
56 } 54 }
57 55
58 bool IsObserverSet() const { 56 bool IsObserverSet() const {
59 return observer_ != nullptr && 57 return observer_ != nullptr &&
60 observer_ == test_->host_scan_scheduler_.get(); 58 observer_ == test_->host_scan_scheduler_.get();
61 } 59 }
62 60
63 void SetIsAuthenticatedUserLoggedIn(bool value) { 61 void set_is_authenticated_user_logged_in(bool value) {
64 is_authenticated_user_logged_in = value; 62 is_authenticated_user_logged_in = value;
65 } 63 }
66 64
67 void SetIsNetworkConnectedOrConnecting(bool value) { 65 void set_is_network_connected_or_connecting(bool value) {
68 is_network_connected_or_connecting = value; 66 is_network_connected_or_connecting = value;
69 } 67 }
70 68
71 void AreTetherHostsSynced(bool value) { are_tether_hosts_synced = value; } 69 void set_are_tether_hosts_synced(bool value) {
70 are_tether_hosts_synced = value;
71 }
72 72
73 private: 73 private:
74 const HostScanSchedulerTest* test_; 74 const HostScanSchedulerTest* test_;
75 75
76 HostScanScheduler* observer_; 76 HostScanScheduler* observer_;
77 bool is_authenticated_user_logged_in; 77 bool is_authenticated_user_logged_in;
78 bool is_network_connected_or_connecting; 78 bool is_network_connected_or_connecting;
79 bool are_tether_hosts_synced; 79 bool are_tether_hosts_synced;
80 }; 80 };
81 81
82 class MockHostScanner : public HostScanner { 82 class FakeHostScanner : public HostScanner {
83 public: 83 public:
84 MockHostScanner() : num_scans_started_(0) {} 84 FakeHostScanner() : num_scans_started_(0) {}
85 ~MockHostScanner() override {} 85 ~FakeHostScanner() override {}
86 86
87 void StartScan() override { num_scans_started_++; } 87 void StartScan() override { num_scans_started_++; }
88 88
89 int num_scans_started() { return num_scans_started_; } 89 int num_scans_started() { return num_scans_started_; }
90 90
91 private: 91 private:
92 int num_scans_started_; 92 int num_scans_started_;
93 }; 93 };
94 94
95 HostScanSchedulerTest() {} 95 HostScanSchedulerTest() {}
96 96
97 void SetUp() override { 97 void SetUp() override {
98 test_context_ = new TestContext(this); 98 test_delegate_ = new TestDelegate(this);
99 mock_host_scanner_ = new MockHostScanner(); 99 fake_host_scanner_ = new FakeHostScanner();
100 100
101 host_scan_scheduler_.reset(new HostScanScheduler( 101 host_scan_scheduler_.reset(
102 base::WrapUnique(test_context_), base::WrapUnique(mock_host_scanner_))); 102 new HostScanScheduler(base::WrapUnique(test_delegate_),
103 base::WrapUnique(fake_host_scanner_)));
103 104
104 EXPECT_FALSE(test_context_->IsObserverSet()); 105 EXPECT_FALSE(test_delegate_->IsObserverSet());
105 host_scan_scheduler_->InitializeAutomaticScans(); 106 host_scan_scheduler_->InitializeAutomaticScans();
106 EXPECT_TRUE(test_context_->IsObserverSet()); 107 EXPECT_TRUE(test_delegate_->IsObserverSet());
107 } 108 }
108 109
109 void TearDown() override { 110 void TearDown() override {
110 EXPECT_TRUE(test_context_->IsObserverSet()); 111 EXPECT_TRUE(test_delegate_->IsObserverSet());
111 host_scan_scheduler_.reset(); 112 host_scan_scheduler_.reset();
112 } 113 }
113 114
114 void TestScheduleScanNowIfPossible(bool is_authenticated_user_logged_in, 115 void TestScheduleScanNowIfPossible(bool is_authenticated_user_logged_in,
115 bool is_network_connected_or_connecting, 116 bool is_network_connected_or_connecting,
116 bool are_tether_hosts_synced, 117 bool are_tether_hosts_synced,
117 int num_expected_scans) { 118 int num_expected_scans) {
118 test_context_->SetIsAuthenticatedUserLoggedIn( 119 test_delegate_->set_is_authenticated_user_logged_in(
119 is_authenticated_user_logged_in); 120 is_authenticated_user_logged_in);
120 test_context_->SetIsNetworkConnectedOrConnecting( 121 test_delegate_->set_is_network_connected_or_connecting(
121 is_network_connected_or_connecting); 122 is_network_connected_or_connecting);
122 test_context_->AreTetherHostsSynced(are_tether_hosts_synced); 123 test_delegate_->set_are_tether_hosts_synced(are_tether_hosts_synced);
123 host_scan_scheduler_->ScheduleScanNowIfPossible(); 124 host_scan_scheduler_->ScheduleScanNowIfPossible();
124 EXPECT_EQ(num_expected_scans, mock_host_scanner_->num_scans_started()); 125 EXPECT_EQ(num_expected_scans, fake_host_scanner_->num_scans_started());
125 } 126 }
126 127
127 std::unique_ptr<HostScanScheduler> host_scan_scheduler_; 128 std::unique_ptr<HostScanScheduler> host_scan_scheduler_;
128 129
129 TestContext* test_context_; 130 TestDelegate* test_delegate_;
130 MockHostScanner* mock_host_scanner_; 131 FakeHostScanner* fake_host_scanner_;
131 }; 132 };
132 133
133 TEST_F(HostScanSchedulerTest, TestObserverAddedAndRemoved) { 134 TEST_F(HostScanSchedulerTest, TestObserverAddedAndRemoved) {
134 // Intentionally empty. This test just ensures that adding/removing observers 135 // Intentionally empty. This test just ensures that adding/removing observers
135 // (in setUp() and tearDown()) works. 136 // (in setUp() and tearDown()) works.
136 } 137 }
137 138
138 TEST_F(HostScanSchedulerTest, TestScheduleScanNowIfPossible) { 139 TEST_F(HostScanSchedulerTest, TestScheduleScanNowIfPossible) {
139 EXPECT_EQ(0, mock_host_scanner_->num_scans_started()); 140 EXPECT_EQ(0, fake_host_scanner_->num_scans_started());
140 141
141 // A scan should only be started when an authenticated user is logged in, 142 // 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 // the network is not connected/connecting, and tether hosts are synced.
143 TestScheduleScanNowIfPossible(false, false, false, 0); 144 TestScheduleScanNowIfPossible(false, false, false, 0);
144 TestScheduleScanNowIfPossible(true, false, false, 0); 145 TestScheduleScanNowIfPossible(true, false, false, 0);
145 TestScheduleScanNowIfPossible(false, true, false, 0); 146 TestScheduleScanNowIfPossible(false, true, false, 0);
146 TestScheduleScanNowIfPossible(false, false, true, 0); 147 TestScheduleScanNowIfPossible(false, false, true, 0);
147 TestScheduleScanNowIfPossible(true, true, false, 0); 148 TestScheduleScanNowIfPossible(true, true, false, 0);
148 TestScheduleScanNowIfPossible(true, false, true, 1); 149 TestScheduleScanNowIfPossible(true, false, true, 1);
149 TestScheduleScanNowIfPossible(false, true, true, 1); 150 TestScheduleScanNowIfPossible(false, true, true, 1);
150 TestScheduleScanNowIfPossible(true, true, true, 1); 151 TestScheduleScanNowIfPossible(true, true, true, 1);
151 } 152 }
152 153
153 TEST_F(HostScanSchedulerTest, TestLoggedInStateChange) { 154 TEST_F(HostScanSchedulerTest, TestLoggedInStateChange) {
154 EXPECT_EQ(0, mock_host_scanner_->num_scans_started()); 155 EXPECT_EQ(0, fake_host_scanner_->num_scans_started());
155 156
156 test_context_->SetIsAuthenticatedUserLoggedIn(true); 157 test_delegate_->set_is_authenticated_user_logged_in(true);
157 test_context_->SetIsNetworkConnectedOrConnecting(false); 158 test_delegate_->set_is_network_connected_or_connecting(false);
158 test_context_->AreTetherHostsSynced(true); 159 test_delegate_->set_are_tether_hosts_synced(true);
159 160
160 host_scan_scheduler_->LoggedInStateChanged(); 161 host_scan_scheduler_->LoggedInStateChanged();
161 EXPECT_EQ(1, mock_host_scanner_->num_scans_started()); 162 EXPECT_EQ(1, fake_host_scanner_->num_scans_started());
162 163
163 // Change a condition so that it should not scan, and re-trigger; there should 164 // Change a condition so that it should not scan, and re-trigger; there should
164 // still be only 1 started scan. 165 // still be only 1 started scan.
165 test_context_->SetIsAuthenticatedUserLoggedIn(false); 166 test_delegate_->set_is_authenticated_user_logged_in(false);
166 host_scan_scheduler_->LoggedInStateChanged(); 167 host_scan_scheduler_->LoggedInStateChanged();
167 EXPECT_EQ(1, mock_host_scanner_->num_scans_started()); 168 EXPECT_EQ(1, fake_host_scanner_->num_scans_started());
168 } 169 }
169 170
170 TEST_F(HostScanSchedulerTest, TestSuspendDone) { 171 TEST_F(HostScanSchedulerTest, TestSuspendDone) {
171 EXPECT_EQ(0, mock_host_scanner_->num_scans_started()); 172 EXPECT_EQ(0, fake_host_scanner_->num_scans_started());
172 173
173 test_context_->SetIsAuthenticatedUserLoggedIn(true); 174 test_delegate_->set_is_authenticated_user_logged_in(true);
174 test_context_->SetIsNetworkConnectedOrConnecting(false); 175 test_delegate_->set_is_network_connected_or_connecting(false);
175 test_context_->AreTetherHostsSynced(true); 176 test_delegate_->set_are_tether_hosts_synced(true);
176 177
177 host_scan_scheduler_->SuspendDone(base::TimeDelta::FromSeconds(1)); 178 host_scan_scheduler_->SuspendDone(base::TimeDelta::FromSeconds(1));
178 EXPECT_EQ(1, mock_host_scanner_->num_scans_started()); 179 EXPECT_EQ(1, fake_host_scanner_->num_scans_started());
179 180
180 // Change a condition so that it should not scan, and re-trigger; there should 181 // Change a condition so that it should not scan, and re-trigger; there should
181 // still be only 1 started scan. 182 // still be only 1 started scan.
182 test_context_->SetIsAuthenticatedUserLoggedIn(false); 183 test_delegate_->set_is_authenticated_user_logged_in(false);
183 host_scan_scheduler_->SuspendDone(base::TimeDelta::FromSeconds(1)); 184 host_scan_scheduler_->SuspendDone(base::TimeDelta::FromSeconds(1));
184 EXPECT_EQ(1, mock_host_scanner_->num_scans_started()); 185 EXPECT_EQ(1, fake_host_scanner_->num_scans_started());
185 } 186 }
186 187
187 TEST_F(HostScanSchedulerTest, TestNetworkConnectionStateChanged) { 188 TEST_F(HostScanSchedulerTest, TestNetworkConnectionStateChanged) {
188 EXPECT_EQ(0, mock_host_scanner_->num_scans_started()); 189 EXPECT_EQ(0, fake_host_scanner_->num_scans_started());
189 190
190 test_context_->SetIsAuthenticatedUserLoggedIn(true); 191 test_delegate_->set_is_authenticated_user_logged_in(true);
191 test_context_->SetIsNetworkConnectedOrConnecting(false); 192 test_delegate_->set_is_network_connected_or_connecting(false);
192 test_context_->AreTetherHostsSynced(true); 193 test_delegate_->set_are_tether_hosts_synced(true);
193 194
194 host_scan_scheduler_->NetworkConnectionStateChanged(nullptr); 195 host_scan_scheduler_->NetworkConnectionStateChanged(nullptr);
195 EXPECT_EQ(1, mock_host_scanner_->num_scans_started()); 196 EXPECT_EQ(1, fake_host_scanner_->num_scans_started());
196 197
197 // Change a condition so that it should not scan, and re-trigger; there should 198 // Change a condition so that it should not scan, and re-trigger; there should
198 // still be only 1 started scan. 199 // still be only 1 started scan.
199 test_context_->SetIsNetworkConnectedOrConnecting(true); 200 test_delegate_->set_is_network_connected_or_connecting(true);
200 host_scan_scheduler_->NetworkConnectionStateChanged(nullptr); 201 host_scan_scheduler_->NetworkConnectionStateChanged(nullptr);
201 EXPECT_EQ(1, mock_host_scanner_->num_scans_started()); 202 EXPECT_EQ(1, fake_host_scanner_->num_scans_started());
202 } 203 }
203 204
204 TEST_F(HostScanSchedulerTest, TestOnSyncFinished) { 205 TEST_F(HostScanSchedulerTest, TestOnSyncFinished) {
205 EXPECT_EQ(0, mock_host_scanner_->num_scans_started()); 206 EXPECT_EQ(0, fake_host_scanner_->num_scans_started());
206 207
207 test_context_->SetIsAuthenticatedUserLoggedIn(true); 208 test_delegate_->set_is_authenticated_user_logged_in(true);
208 test_context_->SetIsNetworkConnectedOrConnecting(false); 209 test_delegate_->set_is_network_connected_or_connecting(false);
209 test_context_->AreTetherHostsSynced(true); 210 test_delegate_->set_are_tether_hosts_synced(true);
210 211
211 host_scan_scheduler_->OnSyncFinished( 212 host_scan_scheduler_->OnSyncFinished(
212 cryptauth::CryptAuthDeviceManager::SyncResult::SUCCESS, 213 cryptauth::CryptAuthDeviceManager::SyncResult::SUCCESS,
213 cryptauth::CryptAuthDeviceManager::DeviceChangeResult::CHANGED); 214 cryptauth::CryptAuthDeviceManager::DeviceChangeResult::CHANGED);
214 EXPECT_EQ(1, mock_host_scanner_->num_scans_started()); 215 EXPECT_EQ(1, fake_host_scanner_->num_scans_started());
215 216
216 // Change a condition so that it should not scan, and re-trigger; there should 217 // Change a condition so that it should not scan, and re-trigger; there should
217 // still be only 1 started scan. 218 // still be only 1 started scan.
218 test_context_->AreTetherHostsSynced(false); 219 test_delegate_->set_are_tether_hosts_synced(false);
219 host_scan_scheduler_->OnSyncFinished( 220 host_scan_scheduler_->OnSyncFinished(
220 cryptauth::CryptAuthDeviceManager::SyncResult::SUCCESS, 221 cryptauth::CryptAuthDeviceManager::SyncResult::SUCCESS,
221 cryptauth::CryptAuthDeviceManager::DeviceChangeResult::UNCHANGED); 222 cryptauth::CryptAuthDeviceManager::DeviceChangeResult::UNCHANGED);
222 EXPECT_EQ(1, mock_host_scanner_->num_scans_started()); 223 EXPECT_EQ(1, fake_host_scanner_->num_scans_started());
223 } 224 }
224 225
225 } // namespace tether 226 } // namespace tether
226 227
227 } // namespace chromeos 228 } // namespace chromeos
OLDNEW
« no previous file with comments | « chromeos/components/tether/host_scan_scheduler.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698