OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "tools/battor_agent/battor_agent.h" | 5 #include "tools/battor_agent/battor_agent.h" |
6 | 6 |
7 #include "base/test/test_simple_task_runner.h" | 7 #include "base/test/test_simple_task_runner.h" |
8 #include "base/thread_task_runner_handle.h" | 8 #include "base/thread_task_runner_handle.h" |
9 #include "testing/gmock/include/gmock/gmock.h" | 9 #include "testing/gmock/include/gmock/gmock.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
43 MockBattOrConnection(BattOrConnection::Listener* listener) | 43 MockBattOrConnection(BattOrConnection::Listener* listener) |
44 : BattOrConnection(listener) {} | 44 : BattOrConnection(listener) {} |
45 ~MockBattOrConnection() override {} | 45 ~MockBattOrConnection() override {} |
46 | 46 |
47 MOCK_METHOD0(Open, void()); | 47 MOCK_METHOD0(Open, void()); |
48 MOCK_METHOD0(Close, void()); | 48 MOCK_METHOD0(Close, void()); |
49 MOCK_METHOD3(SendBytes, | 49 MOCK_METHOD3(SendBytes, |
50 void(BattOrMessageType type, | 50 void(BattOrMessageType type, |
51 const void* buffer, | 51 const void* buffer, |
52 size_t bytes_to_send)); | 52 size_t bytes_to_send)); |
53 MOCK_METHOD1(ReadBytes, void(size_t bytes_to_read)); | 53 MOCK_METHOD1(ReadMessage, void(BattOrMessageType type)); |
54 MOCK_METHOD0(Flush, void()); | 54 MOCK_METHOD0(Flush, void()); |
55 | 55 |
56 private: | 56 private: |
57 DISALLOW_COPY_AND_ASSIGN(MockBattOrConnection); | 57 DISALLOW_COPY_AND_ASSIGN(MockBattOrConnection); |
58 }; | 58 }; |
59 | 59 |
60 } // namespace | 60 } // namespace |
61 | 61 |
62 // TestableBattOrAgent uses a fake BattOrConnection to be testable. | 62 // TestableBattOrAgent uses a fake BattOrConnection to be testable. |
63 class TestableBattOrAgent : public BattOrAgent { | 63 class TestableBattOrAgent : public BattOrAgent { |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 | 121 |
122 if (end_state == BattOrAgentState::RESET_SENT) | 122 if (end_state == BattOrAgentState::RESET_SENT) |
123 return; | 123 return; |
124 | 124 |
125 GetAgent()->OnBytesSent(true); | 125 GetAgent()->OnBytesSent(true); |
126 GetTaskRunner()->RunUntilIdle(); | 126 GetTaskRunner()->RunUntilIdle(); |
127 | 127 |
128 if (end_state == BattOrAgentState::INIT_SENT) | 128 if (end_state == BattOrAgentState::INIT_SENT) |
129 return; | 129 return; |
130 | 130 |
131 GetAgent()->OnBytesRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, | 131 GetAgent()->OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, |
132 AckToCharVector(kInitAck)); | 132 AckToCharVector(kInitAck)); |
133 GetTaskRunner()->RunUntilIdle(); | 133 GetTaskRunner()->RunUntilIdle(); |
134 | 134 |
135 if (end_state == BattOrAgentState::INIT_ACKED) | 135 if (end_state == BattOrAgentState::INIT_ACKED) |
136 return; | 136 return; |
137 | 137 |
138 GetAgent()->OnBytesSent(true); | 138 GetAgent()->OnBytesSent(true); |
139 GetTaskRunner()->RunUntilIdle(); | 139 GetTaskRunner()->RunUntilIdle(); |
140 | 140 |
141 if (end_state == BattOrAgentState::SET_GAIN_SENT) | 141 if (end_state == BattOrAgentState::SET_GAIN_SENT) |
142 return; | 142 return; |
143 | 143 |
144 GetAgent()->OnBytesRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, | 144 GetAgent()->OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, |
145 AckToCharVector(kSetGainAck)); | 145 AckToCharVector(kSetGainAck)); |
146 GetTaskRunner()->RunUntilIdle(); | 146 GetTaskRunner()->RunUntilIdle(); |
147 | 147 |
148 if (end_state == BattOrAgentState::GAIN_ACKED) | 148 if (end_state == BattOrAgentState::GAIN_ACKED) |
149 return; | 149 return; |
150 | 150 |
151 GetAgent()->OnBytesSent(true); | 151 GetAgent()->OnBytesSent(true); |
152 GetTaskRunner()->RunUntilIdle(); | 152 GetTaskRunner()->RunUntilIdle(); |
153 | 153 |
154 if (end_state == BattOrAgentState::START_TRACING_SENT) | 154 if (end_state == BattOrAgentState::START_TRACING_SENT) |
155 return; | 155 return; |
156 | 156 |
157 // Make sure that we're actually forwarding to a state in the start tracing | 157 // Make sure that we're actually forwarding to a state in the start tracing |
158 // state machine. | 158 // state machine. |
159 DCHECK(end_state == BattOrAgentState::START_TRACING_COMPLETE); | 159 DCHECK(end_state == BattOrAgentState::START_TRACING_COMPLETE); |
160 | 160 |
161 GetAgent()->OnBytesRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, | 161 GetAgent()->OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, |
162 AckToCharVector(kStartTracingAck)); | 162 AckToCharVector(kStartTracingAck)); |
163 GetTaskRunner()->RunUntilIdle(); | 163 GetTaskRunner()->RunUntilIdle(); |
164 } | 164 } |
165 | 165 |
166 TestableBattOrAgent* GetAgent() { return agent_.get(); } | 166 TestableBattOrAgent* GetAgent() { return agent_.get(); } |
167 | 167 |
168 scoped_refptr<base::TestSimpleTaskRunner> GetTaskRunner() { | 168 scoped_refptr<base::TestSimpleTaskRunner> GetTaskRunner() { |
169 return task_runner_; | 169 return task_runner_; |
170 } | 170 } |
171 | 171 |
172 BattOrError GetStartTracingError() { return start_tracing_error_; } | 172 BattOrError GetStartTracingError() { return start_tracing_error_; } |
(...skipping 21 matching lines...) Expand all Loading... |
194 SendBytes(BATTOR_MESSAGE_TYPE_CONTROL, | 194 SendBytes(BATTOR_MESSAGE_TYPE_CONTROL, |
195 BufferEq(&reset_msg, sizeof(reset_msg)), sizeof(reset_msg))); | 195 BufferEq(&reset_msg, sizeof(reset_msg)), sizeof(reset_msg))); |
196 | 196 |
197 EXPECT_CALL(*GetAgent()->GetConnection(), Flush()); | 197 EXPECT_CALL(*GetAgent()->GetConnection(), Flush()); |
198 BattOrControlMessage init_msg{BATTOR_CONTROL_MESSAGE_TYPE_INIT, 0, 0}; | 198 BattOrControlMessage init_msg{BATTOR_CONTROL_MESSAGE_TYPE_INIT, 0, 0}; |
199 EXPECT_CALL( | 199 EXPECT_CALL( |
200 *GetAgent()->GetConnection(), | 200 *GetAgent()->GetConnection(), |
201 SendBytes(BATTOR_MESSAGE_TYPE_CONTROL, | 201 SendBytes(BATTOR_MESSAGE_TYPE_CONTROL, |
202 BufferEq(&init_msg, sizeof(init_msg)), sizeof(init_msg))); | 202 BufferEq(&init_msg, sizeof(init_msg)), sizeof(init_msg))); |
203 | 203 |
204 EXPECT_CALL(*GetAgent()->GetConnection(), ReadBytes(sizeof(kInitAck))); | 204 EXPECT_CALL(*GetAgent()->GetConnection(), |
| 205 ReadMessage(BATTOR_MESSAGE_TYPE_CONTROL_ACK)); |
205 | 206 |
206 BattOrControlMessage set_gain_msg{BATTOR_CONTROL_MESSAGE_TYPE_SET_GAIN, | 207 BattOrControlMessage set_gain_msg{BATTOR_CONTROL_MESSAGE_TYPE_SET_GAIN, |
207 BATTOR_GAIN_LOW, 0}; | 208 BATTOR_GAIN_LOW, 0}; |
208 EXPECT_CALL(*GetAgent()->GetConnection(), | 209 EXPECT_CALL(*GetAgent()->GetConnection(), |
209 SendBytes(BATTOR_MESSAGE_TYPE_CONTROL, | 210 SendBytes(BATTOR_MESSAGE_TYPE_CONTROL, |
210 BufferEq(&set_gain_msg, sizeof(set_gain_msg)), | 211 BufferEq(&set_gain_msg, sizeof(set_gain_msg)), |
211 sizeof(set_gain_msg))); | 212 sizeof(set_gain_msg))); |
212 | 213 |
213 EXPECT_CALL(*GetAgent()->GetConnection(), ReadBytes(sizeof(kSetGainAck))); | 214 EXPECT_CALL(*GetAgent()->GetConnection(), |
| 215 ReadMessage(BATTOR_MESSAGE_TYPE_CONTROL_ACK)); |
214 | 216 |
215 BattOrControlMessage start_tracing_msg{ | 217 BattOrControlMessage start_tracing_msg{ |
216 BATTOR_CONTROL_MESSAGE_TYPE_START_SAMPLING_SD, 0, 0}; | 218 BATTOR_CONTROL_MESSAGE_TYPE_START_SAMPLING_SD, 0, 0}; |
217 EXPECT_CALL(*GetAgent()->GetConnection(), | 219 EXPECT_CALL(*GetAgent()->GetConnection(), |
218 SendBytes(BATTOR_MESSAGE_TYPE_CONTROL, | 220 SendBytes(BATTOR_MESSAGE_TYPE_CONTROL, |
219 BufferEq(&start_tracing_msg, sizeof(start_tracing_msg)), | 221 BufferEq(&start_tracing_msg, sizeof(start_tracing_msg)), |
220 sizeof(start_tracing_msg))); | 222 sizeof(start_tracing_msg))); |
221 | 223 |
222 EXPECT_CALL(*GetAgent()->GetConnection(), | 224 EXPECT_CALL(*GetAgent()->GetConnection(), |
223 ReadBytes(sizeof(kStartTracingAck))); | 225 ReadMessage(BATTOR_MESSAGE_TYPE_CONTROL_ACK)); |
224 | 226 |
225 RunStartTracingTo(BattOrAgentState::START_TRACING_COMPLETE); | 227 RunStartTracingTo(BattOrAgentState::START_TRACING_COMPLETE); |
226 EXPECT_TRUE(IsStartTracingComplete()); | 228 EXPECT_TRUE(IsStartTracingComplete()); |
227 EXPECT_EQ(BATTOR_ERROR_NONE, GetStartTracingError()); | 229 EXPECT_EQ(BATTOR_ERROR_NONE, GetStartTracingError()); |
228 } | 230 } |
229 | 231 |
230 TEST_F(BattOrAgentTest, StartTracingFailsWithoutConnection) { | 232 TEST_F(BattOrAgentTest, StartTracingFailsWithoutConnection) { |
231 GetAgent()->StartTracing(); | 233 GetAgent()->StartTracing(); |
232 GetTaskRunner()->RunUntilIdle(); | 234 GetTaskRunner()->RunUntilIdle(); |
233 | 235 |
(...skipping 17 matching lines...) Expand all Loading... |
251 RunStartTracingTo(BattOrAgentState::RESET_SENT); | 253 RunStartTracingTo(BattOrAgentState::RESET_SENT); |
252 GetAgent()->OnBytesSent(false); | 254 GetAgent()->OnBytesSent(false); |
253 GetTaskRunner()->RunUntilIdle(); | 255 GetTaskRunner()->RunUntilIdle(); |
254 | 256 |
255 EXPECT_TRUE(IsStartTracingComplete()); | 257 EXPECT_TRUE(IsStartTracingComplete()); |
256 EXPECT_EQ(BATTOR_ERROR_SEND_ERROR, GetStartTracingError()); | 258 EXPECT_EQ(BATTOR_ERROR_SEND_ERROR, GetStartTracingError()); |
257 } | 259 } |
258 | 260 |
259 TEST_F(BattOrAgentTest, StartTracingFailsIfInitAckReadFails) { | 261 TEST_F(BattOrAgentTest, StartTracingFailsIfInitAckReadFails) { |
260 RunStartTracingTo(BattOrAgentState::INIT_SENT); | 262 RunStartTracingTo(BattOrAgentState::INIT_SENT); |
261 GetAgent()->OnBytesRead(false, BATTOR_MESSAGE_TYPE_CONTROL_ACK, nullptr); | 263 GetAgent()->OnMessageRead(false, BATTOR_MESSAGE_TYPE_CONTROL_ACK, nullptr); |
262 GetTaskRunner()->RunUntilIdle(); | 264 GetTaskRunner()->RunUntilIdle(); |
263 | 265 |
264 EXPECT_TRUE(IsStartTracingComplete()); | 266 EXPECT_TRUE(IsStartTracingComplete()); |
265 EXPECT_EQ(BATTOR_ERROR_RECEIVE_ERROR, GetStartTracingError()); | 267 EXPECT_EQ(BATTOR_ERROR_RECEIVE_ERROR, GetStartTracingError()); |
266 } | 268 } |
267 | 269 |
268 TEST_F(BattOrAgentTest, StartTracingFailsIfInitWrongAckRead) { | 270 TEST_F(BattOrAgentTest, StartTracingFailsIfInitWrongAckRead) { |
269 RunStartTracingTo(BattOrAgentState::INIT_SENT); | 271 RunStartTracingTo(BattOrAgentState::INIT_SENT); |
270 GetAgent()->OnBytesRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, | 272 GetAgent()->OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, |
271 AckToCharVector(kStartTracingAck)); | 273 AckToCharVector(kStartTracingAck)); |
272 GetTaskRunner()->RunUntilIdle(); | 274 GetTaskRunner()->RunUntilIdle(); |
273 | 275 |
274 EXPECT_TRUE(IsStartTracingComplete()); | 276 EXPECT_TRUE(IsStartTracingComplete()); |
275 EXPECT_EQ(BATTOR_ERROR_UNEXPECTED_MESSAGE, GetStartTracingError()); | 277 EXPECT_EQ(BATTOR_ERROR_UNEXPECTED_MESSAGE, GetStartTracingError()); |
276 } | 278 } |
277 | 279 |
278 TEST_F(BattOrAgentTest, StartTracingFailsIfSetGainSendFails) { | 280 TEST_F(BattOrAgentTest, StartTracingFailsIfSetGainSendFails) { |
279 RunStartTracingTo(BattOrAgentState::RESET_SENT); | 281 RunStartTracingTo(BattOrAgentState::RESET_SENT); |
280 GetAgent()->OnBytesSent(false); | 282 GetAgent()->OnBytesSent(false); |
281 GetTaskRunner()->RunUntilIdle(); | 283 GetTaskRunner()->RunUntilIdle(); |
282 | 284 |
283 EXPECT_TRUE(IsStartTracingComplete()); | 285 EXPECT_TRUE(IsStartTracingComplete()); |
284 EXPECT_EQ(BATTOR_ERROR_SEND_ERROR, GetStartTracingError()); | 286 EXPECT_EQ(BATTOR_ERROR_SEND_ERROR, GetStartTracingError()); |
285 } | 287 } |
286 | 288 |
287 TEST_F(BattOrAgentTest, StartTracingFailsIfSetGainAckReadFails) { | 289 TEST_F(BattOrAgentTest, StartTracingFailsIfSetGainAckReadFails) { |
288 RunStartTracingTo(BattOrAgentState::SET_GAIN_SENT); | 290 RunStartTracingTo(BattOrAgentState::SET_GAIN_SENT); |
289 GetAgent()->OnBytesRead(false, BATTOR_MESSAGE_TYPE_CONTROL_ACK, nullptr); | 291 GetAgent()->OnMessageRead(false, BATTOR_MESSAGE_TYPE_CONTROL_ACK, nullptr); |
290 GetTaskRunner()->RunUntilIdle(); | 292 GetTaskRunner()->RunUntilIdle(); |
291 | 293 |
292 EXPECT_TRUE(IsStartTracingComplete()); | 294 EXPECT_TRUE(IsStartTracingComplete()); |
293 EXPECT_EQ(BATTOR_ERROR_RECEIVE_ERROR, GetStartTracingError()); | 295 EXPECT_EQ(BATTOR_ERROR_RECEIVE_ERROR, GetStartTracingError()); |
294 } | 296 } |
295 | 297 |
296 TEST_F(BattOrAgentTest, StartTracingFailsIfSetGainWrongAckRead) { | 298 TEST_F(BattOrAgentTest, StartTracingFailsIfSetGainWrongAckRead) { |
297 RunStartTracingTo(BattOrAgentState::SET_GAIN_SENT); | 299 RunStartTracingTo(BattOrAgentState::SET_GAIN_SENT); |
298 GetAgent()->OnBytesRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, | 300 GetAgent()->OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, |
299 AckToCharVector(kStartTracingAck)); | 301 AckToCharVector(kStartTracingAck)); |
300 GetTaskRunner()->RunUntilIdle(); | 302 GetTaskRunner()->RunUntilIdle(); |
301 | 303 |
302 EXPECT_TRUE(IsStartTracingComplete()); | 304 EXPECT_TRUE(IsStartTracingComplete()); |
303 EXPECT_EQ(BATTOR_ERROR_UNEXPECTED_MESSAGE, GetStartTracingError()); | 305 EXPECT_EQ(BATTOR_ERROR_UNEXPECTED_MESSAGE, GetStartTracingError()); |
304 } | 306 } |
305 | 307 |
306 TEST_F(BattOrAgentTest, StartTracingFailsIfStartTracingSendFails) { | 308 TEST_F(BattOrAgentTest, StartTracingFailsIfStartTracingSendFails) { |
307 RunStartTracingTo(BattOrAgentState::RESET_SENT); | 309 RunStartTracingTo(BattOrAgentState::RESET_SENT); |
308 GetAgent()->OnBytesSent(false); | 310 GetAgent()->OnBytesSent(false); |
309 GetTaskRunner()->RunUntilIdle(); | 311 GetTaskRunner()->RunUntilIdle(); |
310 | 312 |
311 EXPECT_TRUE(IsStartTracingComplete()); | 313 EXPECT_TRUE(IsStartTracingComplete()); |
312 EXPECT_EQ(BATTOR_ERROR_SEND_ERROR, GetStartTracingError()); | 314 EXPECT_EQ(BATTOR_ERROR_SEND_ERROR, GetStartTracingError()); |
313 } | 315 } |
314 | 316 |
315 TEST_F(BattOrAgentTest, StartTracingFailsIfStartTracingAckReadFails) { | 317 TEST_F(BattOrAgentTest, StartTracingFailsIfStartTracingAckReadFails) { |
316 RunStartTracingTo(BattOrAgentState::START_TRACING_SENT); | 318 RunStartTracingTo(BattOrAgentState::START_TRACING_SENT); |
317 GetAgent()->OnBytesRead(false, BATTOR_MESSAGE_TYPE_CONTROL_ACK, nullptr); | 319 GetAgent()->OnMessageRead(false, BATTOR_MESSAGE_TYPE_CONTROL_ACK, nullptr); |
318 GetTaskRunner()->RunUntilIdle(); | 320 GetTaskRunner()->RunUntilIdle(); |
319 | 321 |
320 EXPECT_TRUE(IsStartTracingComplete()); | 322 EXPECT_TRUE(IsStartTracingComplete()); |
321 EXPECT_EQ(BATTOR_ERROR_RECEIVE_ERROR, GetStartTracingError()); | 323 EXPECT_EQ(BATTOR_ERROR_RECEIVE_ERROR, GetStartTracingError()); |
322 } | 324 } |
323 | 325 |
324 TEST_F(BattOrAgentTest, StartTracingFailsIfStartTracingWrongAckRead) { | 326 TEST_F(BattOrAgentTest, StartTracingFailsIfStartTracingWrongAckRead) { |
325 RunStartTracingTo(BattOrAgentState::START_TRACING_SENT); | 327 RunStartTracingTo(BattOrAgentState::START_TRACING_SENT); |
326 GetAgent()->OnBytesRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, | 328 GetAgent()->OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, |
327 AckToCharVector(kInitAck)); | 329 AckToCharVector(kInitAck)); |
328 GetTaskRunner()->RunUntilIdle(); | 330 GetTaskRunner()->RunUntilIdle(); |
329 | 331 |
330 EXPECT_TRUE(IsStartTracingComplete()); | 332 EXPECT_TRUE(IsStartTracingComplete()); |
331 EXPECT_EQ(BATTOR_ERROR_UNEXPECTED_MESSAGE, GetStartTracingError()); | 333 EXPECT_EQ(BATTOR_ERROR_UNEXPECTED_MESSAGE, GetStartTracingError()); |
332 } | 334 } |
333 | 335 |
334 } // namespace battor | 336 } // namespace battor |
OLD | NEW |