Chromium Code Reviews| 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/threading/thread_task_runner_handle.h" | 8 #include "base/threading/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 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 60 : BattOrConnection(listener) {} | 60 : BattOrConnection(listener) {} |
| 61 ~MockBattOrConnection() override {} | 61 ~MockBattOrConnection() override {} |
| 62 | 62 |
| 63 MOCK_METHOD0(Open, void()); | 63 MOCK_METHOD0(Open, void()); |
| 64 MOCK_METHOD0(Close, void()); | 64 MOCK_METHOD0(Close, void()); |
| 65 MOCK_METHOD3(SendBytes, | 65 MOCK_METHOD3(SendBytes, |
| 66 void(BattOrMessageType type, | 66 void(BattOrMessageType type, |
| 67 const void* buffer, | 67 const void* buffer, |
| 68 size_t bytes_to_send)); | 68 size_t bytes_to_send)); |
| 69 MOCK_METHOD1(ReadMessage, void(BattOrMessageType type)); | 69 MOCK_METHOD1(ReadMessage, void(BattOrMessageType type)); |
| 70 MOCK_METHOD0(CancelReadMessage, void()); | |
| 70 MOCK_METHOD0(Flush, void()); | 71 MOCK_METHOD0(Flush, void()); |
| 71 | 72 |
| 72 private: | 73 private: |
| 73 DISALLOW_COPY_AND_ASSIGN(MockBattOrConnection); | 74 DISALLOW_COPY_AND_ASSIGN(MockBattOrConnection); |
| 74 }; | 75 }; |
| 75 | 76 |
| 76 } // namespace | 77 } // namespace |
| 77 | 78 |
| 78 // TestableBattOrAgent uses a fake BattOrConnection to be testable. | 79 // TestableBattOrAgent uses a fake BattOrConnection to be testable. |
| 79 class TestableBattOrAgent : public BattOrAgent { | 80 class TestableBattOrAgent : public BattOrAgent { |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 135 is_command_complete_ = false; | 136 is_command_complete_ = false; |
| 136 command_error_ = BATTOR_ERROR_NONE; | 137 command_error_ = BATTOR_ERROR_NONE; |
| 137 } | 138 } |
| 138 | 139 |
| 139 // Possible states that the BattOrAgent can be in. | 140 // Possible states that the BattOrAgent can be in. |
| 140 enum class BattOrAgentState { | 141 enum class BattOrAgentState { |
| 141 // States required to connect to a BattOr. | 142 // States required to connect to a BattOr. |
| 142 CONNECTED, | 143 CONNECTED, |
| 143 | 144 |
| 144 // States required to StartTracing. | 145 // States required to StartTracing. |
| 145 RESET_SENT, | |
| 146 INIT_SENT, | 146 INIT_SENT, |
| 147 INIT_ACKED, | 147 INIT_ACKED, |
| 148 SET_GAIN_SENT, | 148 SET_GAIN_SENT, |
| 149 GAIN_ACKED, | 149 GAIN_ACKED, |
| 150 START_TRACING_SENT, | 150 START_TRACING_SENT, |
| 151 START_TRACING_COMPLETE, | 151 START_TRACING_COMPLETE, |
| 152 | 152 |
| 153 // States required to StopTracing. | 153 // States required to StopTracing. |
| 154 EEPROM_REQUEST_SENT, | 154 EEPROM_REQUEST_SENT, |
| 155 EEPROM_RECEIVED, | 155 EEPROM_RECEIVED, |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 169 GetAgent()->StartTracing(); | 169 GetAgent()->StartTracing(); |
| 170 GetTaskRunner()->RunUntilIdle(); | 170 GetTaskRunner()->RunUntilIdle(); |
| 171 | 171 |
| 172 GetAgent()->OnConnectionOpened(true); | 172 GetAgent()->OnConnectionOpened(true); |
| 173 GetTaskRunner()->RunUntilIdle(); | 173 GetTaskRunner()->RunUntilIdle(); |
| 174 | 174 |
| 175 if (end_state == BattOrAgentState::CONNECTED) | 175 if (end_state == BattOrAgentState::CONNECTED) |
| 176 return; | 176 return; |
| 177 | 177 |
| 178 OnBytesSent(true); | 178 OnBytesSent(true); |
| 179 if (end_state == BattOrAgentState::RESET_SENT) | |
| 180 return; | |
| 181 | |
| 182 OnBytesSent(true); | |
| 183 if (end_state == BattOrAgentState::INIT_SENT) | 179 if (end_state == BattOrAgentState::INIT_SENT) |
| 184 return; | 180 return; |
| 185 | 181 |
| 186 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, | 182 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, |
| 187 ToCharVector(kInitAck)); | 183 ToCharVector(kInitAck)); |
| 188 if (end_state == BattOrAgentState::INIT_ACKED) | 184 if (end_state == BattOrAgentState::INIT_ACKED) |
| 189 return; | 185 return; |
| 190 | 186 |
| 191 OnBytesSent(true); | 187 OnBytesSent(true); |
| 192 if (end_state == BattOrAgentState::SET_GAIN_SENT) | 188 if (end_state == BattOrAgentState::SET_GAIN_SENT) |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 295 std::unique_ptr<TestableBattOrAgent> agent_; | 291 std::unique_ptr<TestableBattOrAgent> agent_; |
| 296 bool is_command_complete_; | 292 bool is_command_complete_; |
| 297 BattOrError command_error_; | 293 BattOrError command_error_; |
| 298 std::string trace_; | 294 std::string trace_; |
| 299 }; | 295 }; |
| 300 | 296 |
| 301 TEST_F(BattOrAgentTest, StartTracing) { | 297 TEST_F(BattOrAgentTest, StartTracing) { |
| 302 testing::InSequence s; | 298 testing::InSequence s; |
| 303 EXPECT_CALL(*GetAgent()->GetConnection(), Open()); | 299 EXPECT_CALL(*GetAgent()->GetConnection(), Open()); |
| 304 | 300 |
| 305 BattOrControlMessage reset_msg{BATTOR_CONTROL_MESSAGE_TYPE_RESET, 0, 0}; | |
| 306 EXPECT_CALL( | |
| 307 *GetAgent()->GetConnection(), | |
| 308 SendBytes(BATTOR_MESSAGE_TYPE_CONTROL, | |
| 309 BufferEq(&reset_msg, sizeof(reset_msg)), sizeof(reset_msg))); | |
| 310 | |
| 311 EXPECT_CALL(*GetAgent()->GetConnection(), Flush()); | 301 EXPECT_CALL(*GetAgent()->GetConnection(), Flush()); |
| 312 BattOrControlMessage init_msg{BATTOR_CONTROL_MESSAGE_TYPE_INIT, 0, 0}; | 302 BattOrControlMessage init_msg{BATTOR_CONTROL_MESSAGE_TYPE_INIT, 0, 0}; |
| 313 EXPECT_CALL( | 303 EXPECT_CALL( |
| 314 *GetAgent()->GetConnection(), | 304 *GetAgent()->GetConnection(), |
| 315 SendBytes(BATTOR_MESSAGE_TYPE_CONTROL, | 305 SendBytes(BATTOR_MESSAGE_TYPE_CONTROL, |
| 316 BufferEq(&init_msg, sizeof(init_msg)), sizeof(init_msg))); | 306 BufferEq(&init_msg, sizeof(init_msg)), sizeof(init_msg))); |
| 317 | 307 |
| 318 EXPECT_CALL(*GetAgent()->GetConnection(), | 308 EXPECT_CALL(*GetAgent()->GetConnection(), |
| 319 ReadMessage(BATTOR_MESSAGE_TYPE_CONTROL_ACK)); | 309 ReadMessage(BATTOR_MESSAGE_TYPE_CONTROL_ACK)); |
| 320 | 310 |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 347 GetAgent()->StartTracing(); | 337 GetAgent()->StartTracing(); |
| 348 GetTaskRunner()->RunUntilIdle(); | 338 GetTaskRunner()->RunUntilIdle(); |
| 349 | 339 |
| 350 GetAgent()->OnConnectionOpened(false); | 340 GetAgent()->OnConnectionOpened(false); |
| 351 GetTaskRunner()->RunUntilIdle(); | 341 GetTaskRunner()->RunUntilIdle(); |
| 352 | 342 |
| 353 EXPECT_TRUE(IsCommandComplete()); | 343 EXPECT_TRUE(IsCommandComplete()); |
| 354 EXPECT_EQ(BATTOR_ERROR_CONNECTION_FAILED, GetCommandError()); | 344 EXPECT_EQ(BATTOR_ERROR_CONNECTION_FAILED, GetCommandError()); |
| 355 } | 345 } |
| 356 | 346 |
| 357 TEST_F(BattOrAgentTest, StartTracingFailsIfResetSendFails) { | 347 TEST_F(BattOrAgentTest, StartTracingFailsIfInitSendFails) { |
| 358 RunStartTracingTo(BattOrAgentState::CONNECTED); | 348 RunStartTracingTo(BattOrAgentState::CONNECTED); |
| 359 OnBytesSent(false); | 349 OnBytesSent(false); |
| 360 | 350 |
| 361 EXPECT_TRUE(IsCommandComplete()); | 351 EXPECT_TRUE(IsCommandComplete()); |
| 362 EXPECT_EQ(BATTOR_ERROR_SEND_ERROR, GetCommandError()); | |
| 363 } | |
| 364 | |
| 365 TEST_F(BattOrAgentTest, StartTracingFailsIfInitSendFails) { | |
| 366 RunStartTracingTo(BattOrAgentState::RESET_SENT); | |
| 367 OnBytesSent(false); | |
| 368 | |
| 369 EXPECT_TRUE(IsCommandComplete()); | |
| 370 EXPECT_EQ(BATTOR_ERROR_SEND_ERROR, GetCommandError()); | 352 EXPECT_EQ(BATTOR_ERROR_SEND_ERROR, GetCommandError()); |
| 371 } | 353 } |
| 372 | 354 |
| 373 TEST_F(BattOrAgentTest, StartTracingFailsIfInitAckReadFails) { | 355 TEST_F(BattOrAgentTest, StartTracingFailsIfInitAckReadFails) { |
| 374 RunStartTracingTo(BattOrAgentState::INIT_SENT); | 356 RunStartTracingTo(BattOrAgentState::INIT_SENT); |
| 375 OnMessageRead(false, BATTOR_MESSAGE_TYPE_CONTROL_ACK, nullptr); | 357 |
| 358 for (int i = 0; i < 20; i++) { | |
| 359 OnMessageRead(false, BATTOR_MESSAGE_TYPE_CONTROL_ACK, nullptr); | |
| 360 | |
| 361 // Bytes will be sent because INIT will be retried. | |
| 362 OnBytesSent(true); | |
| 363 } | |
| 376 | 364 |
| 377 EXPECT_TRUE(IsCommandComplete()); | 365 EXPECT_TRUE(IsCommandComplete()); |
| 378 EXPECT_EQ(BATTOR_ERROR_RECEIVE_ERROR, GetCommandError()); | 366 EXPECT_EQ(BATTOR_ERROR_TOO_MANY_INIT_RETRIES, GetCommandError()); |
| 379 } | 367 } |
| 380 | 368 |
| 381 TEST_F(BattOrAgentTest, StartTracingFailsIfInitWrongAckRead) { | 369 TEST_F(BattOrAgentTest, StartTracingFailsIfInitWrongAckRead) { |
| 382 RunStartTracingTo(BattOrAgentState::INIT_SENT); | 370 RunStartTracingTo(BattOrAgentState::INIT_SENT); |
| 383 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, | 371 |
| 384 ToCharVector(kStartTracingAck)); | 372 for (int i = 0; i < 20; i++) { |
| 373 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, | |
| 374 ToCharVector(kStartTracingAck)); | |
| 375 | |
| 376 // Bytes will be sent because INIT will be retried. | |
| 377 OnBytesSent(true); | |
| 378 } | |
| 385 | 379 |
| 386 EXPECT_TRUE(IsCommandComplete()); | 380 EXPECT_TRUE(IsCommandComplete()); |
| 387 EXPECT_EQ(BATTOR_ERROR_UNEXPECTED_MESSAGE, GetCommandError()); | 381 EXPECT_EQ(BATTOR_ERROR_TOO_MANY_INIT_RETRIES, GetCommandError()); |
| 388 } | 382 } |
| 389 | 383 |
| 390 TEST_F(BattOrAgentTest, StartTracingFailsIfSetGainSendFails) { | 384 TEST_F(BattOrAgentTest, StartTracingFailsIfSetGainSendFails) { |
| 391 RunStartTracingTo(BattOrAgentState::RESET_SENT); | 385 RunStartTracingTo(BattOrAgentState::INIT_SENT); |
| 392 OnBytesSent(false); | 386 OnBytesSent(false); |
| 393 | 387 |
| 394 EXPECT_TRUE(IsCommandComplete()); | 388 EXPECT_TRUE(IsCommandComplete()); |
| 395 EXPECT_EQ(BATTOR_ERROR_SEND_ERROR, GetCommandError()); | 389 EXPECT_EQ(BATTOR_ERROR_SEND_ERROR, GetCommandError()); |
| 396 } | 390 } |
| 397 | 391 |
| 398 TEST_F(BattOrAgentTest, StartTracingFailsIfSetGainAckReadFails) { | 392 TEST_F(BattOrAgentTest, StartTracingFailsIfSetGainAckReadFails) { |
| 399 RunStartTracingTo(BattOrAgentState::SET_GAIN_SENT); | 393 RunStartTracingTo(BattOrAgentState::SET_GAIN_SENT); |
| 400 OnMessageRead(false, BATTOR_MESSAGE_TYPE_CONTROL_ACK, nullptr); | 394 OnMessageRead(false, BATTOR_MESSAGE_TYPE_CONTROL_ACK, nullptr); |
| 401 | 395 |
| 402 EXPECT_TRUE(IsCommandComplete()); | 396 EXPECT_TRUE(IsCommandComplete()); |
| 403 EXPECT_EQ(BATTOR_ERROR_RECEIVE_ERROR, GetCommandError()); | 397 EXPECT_EQ(BATTOR_ERROR_RECEIVE_ERROR, GetCommandError()); |
| 404 } | 398 } |
| 405 | 399 |
| 406 TEST_F(BattOrAgentTest, StartTracingFailsIfSetGainWrongAckRead) { | 400 TEST_F(BattOrAgentTest, StartTracingFailsIfSetGainWrongAckRead) { |
| 407 RunStartTracingTo(BattOrAgentState::SET_GAIN_SENT); | 401 RunStartTracingTo(BattOrAgentState::SET_GAIN_SENT); |
| 408 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, | 402 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, |
| 409 ToCharVector(kStartTracingAck)); | 403 ToCharVector(kStartTracingAck)); |
| 410 | 404 |
| 411 EXPECT_TRUE(IsCommandComplete()); | 405 EXPECT_TRUE(IsCommandComplete()); |
| 412 EXPECT_EQ(BATTOR_ERROR_UNEXPECTED_MESSAGE, GetCommandError()); | 406 EXPECT_EQ(BATTOR_ERROR_UNEXPECTED_MESSAGE, GetCommandError()); |
| 413 } | 407 } |
| 414 | 408 |
| 415 TEST_F(BattOrAgentTest, StartTracingFailsIfStartTracingSendFails) { | 409 TEST_F(BattOrAgentTest, StartTracingFailsIfStartTracingSendFails) { |
| 416 RunStartTracingTo(BattOrAgentState::RESET_SENT); | 410 RunStartTracingTo(BattOrAgentState::INIT_SENT); |
| 417 OnBytesSent(false); | 411 OnBytesSent(false); |
| 418 | 412 |
| 419 EXPECT_TRUE(IsCommandComplete()); | 413 EXPECT_TRUE(IsCommandComplete()); |
| 420 EXPECT_EQ(BATTOR_ERROR_SEND_ERROR, GetCommandError()); | 414 EXPECT_EQ(BATTOR_ERROR_SEND_ERROR, GetCommandError()); |
| 421 } | 415 } |
| 422 | 416 |
| 423 TEST_F(BattOrAgentTest, StartTracingFailsIfStartTracingAckReadFails) { | 417 TEST_F(BattOrAgentTest, StartTracingFailsIfStartTracingAckReadFails) { |
| 424 RunStartTracingTo(BattOrAgentState::START_TRACING_SENT); | 418 RunStartTracingTo(BattOrAgentState::START_TRACING_SENT); |
| 425 OnMessageRead(false, BATTOR_MESSAGE_TYPE_CONTROL_ACK, nullptr); | 419 OnMessageRead(false, BATTOR_MESSAGE_TYPE_CONTROL_ACK, nullptr); |
| 426 | 420 |
| 427 EXPECT_TRUE(IsCommandComplete()); | 421 EXPECT_TRUE(IsCommandComplete()); |
| 428 EXPECT_EQ(BATTOR_ERROR_RECEIVE_ERROR, GetCommandError()); | 422 EXPECT_EQ(BATTOR_ERROR_RECEIVE_ERROR, GetCommandError()); |
| 429 } | 423 } |
| 430 | 424 |
| 431 TEST_F(BattOrAgentTest, StartTracingFailsIfStartTracingWrongAckRead) { | 425 TEST_F(BattOrAgentTest, StartTracingFailsIfStartTracingWrongAckRead) { |
| 432 RunStartTracingTo(BattOrAgentState::START_TRACING_SENT); | 426 RunStartTracingTo(BattOrAgentState::START_TRACING_SENT); |
| 433 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, ToCharVector(kInitAck)); | 427 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, ToCharVector(kInitAck)); |
| 434 | 428 |
| 435 EXPECT_TRUE(IsCommandComplete()); | 429 EXPECT_TRUE(IsCommandComplete()); |
| 436 EXPECT_EQ(BATTOR_ERROR_UNEXPECTED_MESSAGE, GetCommandError()); | 430 EXPECT_EQ(BATTOR_ERROR_UNEXPECTED_MESSAGE, GetCommandError()); |
| 437 } | 431 } |
| 438 | 432 |
| 433 TEST_F(BattOrAgentTest, StartTracingSucceedsWithOneInitFailure) { | |
| 434 RunStartTracingTo(BattOrAgentState::INIT_SENT); | |
| 435 | |
| 436 // Instead of an INIT ACK get some samples. This will force an init retry. | |
|
charliea (OOO until 10-5)
2016/06/15 18:56:34
Maybe "Send some samples instead of an INIT ACK. T
aschulman
2016/06/15 23:37:09
Done.
| |
| 437 BattOrFrameHeader frame_header{1, 3 * sizeof(RawBattOrSample)}; | |
| 438 RawBattOrSample frame[] = { | |
| 439 RawBattOrSample{1, 1}, RawBattOrSample{2, 2}, RawBattOrSample{3, 3}, | |
| 440 }; | |
| 441 OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, | |
| 442 CreateFrame(frame_header, frame, 3)); | |
| 443 | |
| 444 OnBytesSent(true); | |
| 445 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, ToCharVector(kInitAck)); | |
| 446 | |
| 447 OnBytesSent(true); | |
| 448 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, | |
| 449 ToCharVector(kSetGainAck)); | |
| 450 | |
| 451 OnBytesSent(true); | |
| 452 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, | |
| 453 ToCharVector(kStartTracingAck)); | |
| 454 | |
| 455 EXPECT_TRUE(IsCommandComplete()); | |
| 456 EXPECT_EQ(BATTOR_ERROR_NONE, GetCommandError()); | |
| 457 } | |
| 458 | |
| 439 TEST_F(BattOrAgentTest, StopTracing) { | 459 TEST_F(BattOrAgentTest, StopTracing) { |
| 440 testing::InSequence s; | 460 testing::InSequence s; |
| 441 EXPECT_CALL(*GetAgent()->GetConnection(), Open()); | 461 EXPECT_CALL(*GetAgent()->GetConnection(), Open()); |
| 442 | 462 |
| 443 BattOrControlMessage request_eeprom_msg{ | 463 BattOrControlMessage request_eeprom_msg{ |
| 444 BATTOR_CONTROL_MESSAGE_TYPE_READ_EEPROM, sizeof(BattOrEEPROM), 0}; | 464 BATTOR_CONTROL_MESSAGE_TYPE_READ_EEPROM, sizeof(BattOrEEPROM), 0}; |
| 445 EXPECT_CALL( | 465 EXPECT_CALL( |
| 446 *GetAgent()->GetConnection(), | 466 *GetAgent()->GetConnection(), |
| 447 SendBytes(BATTOR_MESSAGE_TYPE_CONTROL, | 467 SendBytes(BATTOR_MESSAGE_TYPE_CONTROL, |
| 448 BufferEq(&request_eeprom_msg, sizeof(request_eeprom_msg)), | 468 BufferEq(&request_eeprom_msg, sizeof(request_eeprom_msg)), |
| (...skipping 418 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 867 | 887 |
| 868 uint32_t current_sample = 1; | 888 uint32_t current_sample = 1; |
| 869 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL, | 889 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL, |
| 870 ToCharVector(current_sample)); | 890 ToCharVector(current_sample)); |
| 871 | 891 |
| 872 EXPECT_TRUE(IsCommandComplete()); | 892 EXPECT_TRUE(IsCommandComplete()); |
| 873 EXPECT_EQ(BATTOR_ERROR_UNEXPECTED_MESSAGE, GetCommandError()); | 893 EXPECT_EQ(BATTOR_ERROR_UNEXPECTED_MESSAGE, GetCommandError()); |
| 874 } | 894 } |
| 875 | 895 |
| 876 } // namespace battor | 896 } // namespace battor |
| OLD | NEW |