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

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

Issue 2741253002: [CrOS Tether] Create KeepAliveScheduler, a class which schedules keep-alive tickles to be sent to a… (Closed)
Patch Set: Add missing dependency. Created 3 years, 9 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/keep_alive_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
(Empty)
1 // Copyright 2017 The Chromium Authors. All rights reserved.
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/keep_alive_scheduler.h"
6
7 #include <memory>
8 #include <vector>
9
10 #include "base/timer/mock_timer.h"
11 #include "chromeos/components/tether/fake_active_host.h"
12 #include "chromeos/components/tether/fake_ble_connection_manager.h"
13 #include "components/cryptauth/remote_device_test_util.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15
16 namespace chromeos {
17
18 namespace tether {
19
20 namespace {
21
22 const char kWifiNetworkId[] = "wifiNetworkId";
23
24 class OperationDeletedHandler {
25 public:
26 virtual void OnOperationDeleted() = 0;
27 };
28
29 class FakeKeepAliveOperation : public KeepAliveOperation {
30 public:
31 FakeKeepAliveOperation(const cryptauth::RemoteDevice& device_to_connect,
32 BleConnectionManager* connection_manager,
33 OperationDeletedHandler* handler)
34 : KeepAliveOperation(device_to_connect, connection_manager),
35 handler_(handler),
36 remote_device_(device_to_connect) {}
37
38 ~FakeKeepAliveOperation() override { handler_->OnOperationDeleted(); }
39
40 void SendOperationFinishedEvent() { OnOperationFinished(); }
41
42 cryptauth::RemoteDevice remote_device() { return remote_device_; }
43
44 private:
45 OperationDeletedHandler* handler_;
46 const cryptauth::RemoteDevice remote_device_;
47 };
48
49 class FakeKeepAliveOperationFactory : public KeepAliveOperation::Factory,
50 public OperationDeletedHandler {
51 public:
52 FakeKeepAliveOperationFactory()
53 : num_created_(0), num_deleted_(0), last_created_(nullptr) {}
54 ~FakeKeepAliveOperationFactory() {}
55
56 uint32_t num_created() { return num_created_; }
57
58 uint32_t num_deleted() { return num_deleted_; }
59
60 FakeKeepAliveOperation* last_created() { return last_created_; }
61
62 void OnOperationDeleted() override { num_deleted_++; }
63
64 protected:
65 std::unique_ptr<KeepAliveOperation> BuildInstance(
66 const cryptauth::RemoteDevice& device_to_connect,
67 BleConnectionManager* connection_manager) override {
68 num_created_++;
69 last_created_ =
70 new FakeKeepAliveOperation(device_to_connect, connection_manager, this);
71 return base::WrapUnique(last_created_);
72 }
73
74 private:
75 uint32_t num_created_;
76 uint32_t num_deleted_;
77 FakeKeepAliveOperation* last_created_;
78 };
79
80 } // namespace
81
82 class KeepAliveSchedulerTest : public testing::Test {
83 protected:
84 KeepAliveSchedulerTest()
85 : test_devices_(cryptauth::GenerateTestRemoteDevices(2)) {}
86
87 void SetUp() override {
88 fake_active_host_ = base::MakeUnique<FakeActiveHost>();
89 fake_ble_connection_manager_ = base::MakeUnique<FakeBleConnectionManager>();
90 mock_timer_ = new base::MockTimer(true /* retain_user_task */,
91 true /* is_repeating */);
92
93 fake_operation_factory_ =
94 base::WrapUnique(new FakeKeepAliveOperationFactory());
95 KeepAliveOperation::Factory::SetInstanceForTesting(
96 fake_operation_factory_.get());
97
98 scheduler_ = base::WrapUnique(new KeepAliveScheduler(
99 fake_active_host_.get(), fake_ble_connection_manager_.get(),
100 base::WrapUnique(mock_timer_)));
101 }
102
103 void VerifyTimerRunning(bool is_running) {
104 EXPECT_EQ(is_running, mock_timer_->IsRunning());
105
106 if (is_running) {
107 EXPECT_EQ(base::TimeDelta::FromMinutes(
108 KeepAliveScheduler::kKeepAliveIntervalMinutes),
109 mock_timer_->GetCurrentDelay());
110 }
111 }
112
113 const std::vector<cryptauth::RemoteDevice> test_devices_;
114
115 std::unique_ptr<FakeActiveHost> fake_active_host_;
116 std::unique_ptr<FakeBleConnectionManager> fake_ble_connection_manager_;
117 base::MockTimer* mock_timer_;
118
119 std::unique_ptr<FakeKeepAliveOperationFactory> fake_operation_factory_;
120
121 std::unique_ptr<KeepAliveScheduler> scheduler_;
122
123 private:
124 DISALLOW_COPY_AND_ASSIGN(KeepAliveSchedulerTest);
125 };
126
127 TEST_F(KeepAliveSchedulerTest, TestSendTickle_OneActiveHost) {
128 EXPECT_FALSE(fake_operation_factory_->num_created());
129 EXPECT_FALSE(fake_operation_factory_->num_deleted());
130 VerifyTimerRunning(false /* is_running */);
131
132 // Start connecting to a device. No operation should be started.
133 fake_active_host_->SetActiveHostConnecting(test_devices_[0].GetDeviceId());
134 EXPECT_FALSE(fake_operation_factory_->num_created());
135 EXPECT_FALSE(fake_operation_factory_->num_deleted());
136 VerifyTimerRunning(false /* is_running */);
137
138 // Connect to the device; the operation should be started.
139 fake_active_host_->SetActiveHostConnected(test_devices_[0].GetDeviceId(),
140 std::string(kWifiNetworkId));
141 EXPECT_EQ(1u, fake_operation_factory_->num_created());
142 EXPECT_EQ(test_devices_[0],
143 fake_operation_factory_->last_created()->remote_device());
144 EXPECT_FALSE(fake_operation_factory_->num_deleted());
145 VerifyTimerRunning(true /* is_running */);
146
147 // Ensure that once the operation is finished, it is deleted.
148 fake_operation_factory_->last_created()->SendOperationFinishedEvent();
149 EXPECT_EQ(1u, fake_operation_factory_->num_created());
150 EXPECT_EQ(1u, fake_operation_factory_->num_deleted());
151 VerifyTimerRunning(true /* is_running */);
152
153 // Fire the timer; this should result in another tickle being sent.
154 mock_timer_->Fire();
155 EXPECT_EQ(2u, fake_operation_factory_->num_created());
156 EXPECT_EQ(test_devices_[0],
157 fake_operation_factory_->last_created()->remote_device());
158 EXPECT_EQ(1u, fake_operation_factory_->num_deleted());
159 VerifyTimerRunning(true /* is_running */);
160
161 // Finish this operation.
162 fake_operation_factory_->last_created()->SendOperationFinishedEvent();
163 EXPECT_EQ(2u, fake_operation_factory_->num_created());
164 EXPECT_EQ(2u, fake_operation_factory_->num_deleted());
165 VerifyTimerRunning(true /* is_running */);
166
167 // Disconnect that device.
168 fake_active_host_->SetActiveHostDisconnected();
169 EXPECT_EQ(2u, fake_operation_factory_->num_created());
170 EXPECT_EQ(2u, fake_operation_factory_->num_deleted());
171 VerifyTimerRunning(false /* is_running */);
172 }
173
174 TEST_F(KeepAliveSchedulerTest, TestSendTickle_MultipleActiveHosts) {
175 EXPECT_FALSE(fake_operation_factory_->num_created());
176 EXPECT_FALSE(fake_operation_factory_->num_deleted());
177 VerifyTimerRunning(false /* is_running */);
178
179 // Start connecting to a device. No operation should be started.
180 fake_active_host_->SetActiveHostConnecting(test_devices_[0].GetDeviceId());
181 EXPECT_FALSE(fake_operation_factory_->num_created());
182 EXPECT_FALSE(fake_operation_factory_->num_deleted());
183 VerifyTimerRunning(false /* is_running */);
184
185 // Connect to the device; the operation should be started.
186 fake_active_host_->SetActiveHostConnected(test_devices_[0].GetDeviceId(),
187 std::string(kWifiNetworkId));
188 EXPECT_EQ(1u, fake_operation_factory_->num_created());
189 EXPECT_EQ(test_devices_[0],
190 fake_operation_factory_->last_created()->remote_device());
191 EXPECT_FALSE(fake_operation_factory_->num_deleted());
192 VerifyTimerRunning(true /* is_running */);
193
194 // Disconnect that device before the operation is finished. It should still be
195 // deleted.
196 fake_active_host_->SetActiveHostDisconnected();
197 EXPECT_EQ(1u, fake_operation_factory_->num_created());
198 EXPECT_EQ(1u, fake_operation_factory_->num_deleted());
199 VerifyTimerRunning(false /* is_running */);
200
201 // Start connecting to a different. No operation should be started.
202 fake_active_host_->SetActiveHostConnecting(test_devices_[1].GetDeviceId());
203 EXPECT_EQ(1u, fake_operation_factory_->num_created());
204 EXPECT_EQ(1u, fake_operation_factory_->num_deleted());
205 VerifyTimerRunning(false /* is_running */);
206
207 // Connect to the second device; the operation should be started.
208 fake_active_host_->SetActiveHostConnected(test_devices_[1].GetDeviceId(),
209 std::string(kWifiNetworkId));
210 EXPECT_EQ(2u, fake_operation_factory_->num_created());
211 EXPECT_EQ(test_devices_[1],
212 fake_operation_factory_->last_created()->remote_device());
213 EXPECT_EQ(1u, fake_operation_factory_->num_deleted());
214 VerifyTimerRunning(true /* is_running */);
215
216 // Ensure that once the second operation is finished, it is deleted.
217 fake_operation_factory_->last_created()->SendOperationFinishedEvent();
218 EXPECT_EQ(2u, fake_operation_factory_->num_created());
219 EXPECT_EQ(2u, fake_operation_factory_->num_deleted());
220 VerifyTimerRunning(true /* is_running */);
221
222 // Disconnect that device.
223 fake_active_host_->SetActiveHostDisconnected();
224 EXPECT_EQ(2u, fake_operation_factory_->num_created());
225 EXPECT_EQ(2u, fake_operation_factory_->num_deleted());
226 VerifyTimerRunning(false /* is_running */);
227 }
228
229 } // namespace tether
230
231 } // namespace cryptauth
OLDNEW
« no previous file with comments | « chromeos/components/tether/keep_alive_scheduler.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698