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, StartTracingFailsIfResetSendFails) { |
|
charliea (OOO until 10-5)
2016/06/15 14:28:50
I think that this test should be deleted.
| |
| 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()); | 352 EXPECT_EQ(BATTOR_ERROR_SEND_ERROR, GetCommandError()); |
| 363 } | 353 } |
| 364 | 354 |
| 365 TEST_F(BattOrAgentTest, StartTracingFailsIfInitSendFails) { | 355 TEST_F(BattOrAgentTest, StartTracingFailsIfInitSendFails) { |
| 366 RunStartTracingTo(BattOrAgentState::RESET_SENT); | 356 RunStartTracingTo(BattOrAgentState::CONNECTED); |
| 367 OnBytesSent(false); | 357 OnBytesSent(false); |
| 368 | 358 |
| 369 EXPECT_TRUE(IsCommandComplete()); | 359 EXPECT_TRUE(IsCommandComplete()); |
| 370 EXPECT_EQ(BATTOR_ERROR_SEND_ERROR, GetCommandError()); | 360 EXPECT_EQ(BATTOR_ERROR_SEND_ERROR, GetCommandError()); |
| 371 } | 361 } |
| 372 | 362 |
| 373 TEST_F(BattOrAgentTest, StartTracingFailsIfInitAckReadFails) { | 363 TEST_F(BattOrAgentTest, StartTracingFailsIfInitAckReadFails) { |
| 374 RunStartTracingTo(BattOrAgentState::INIT_SENT); | 364 RunStartTracingTo(BattOrAgentState::INIT_SENT); |
| 375 OnMessageRead(false, BATTOR_MESSAGE_TYPE_CONTROL_ACK, nullptr); | 365 |
| 366 for (int i = 0; i < 20; i++) { | |
| 367 OnMessageRead(false, BATTOR_MESSAGE_TYPE_CONTROL_ACK, nullptr); | |
| 368 | |
| 369 // Bytes will be sent because INIT will be retried. | |
| 370 OnBytesSent(true); | |
| 371 } | |
| 376 | 372 |
| 377 EXPECT_TRUE(IsCommandComplete()); | 373 EXPECT_TRUE(IsCommandComplete()); |
| 378 EXPECT_EQ(BATTOR_ERROR_RECEIVE_ERROR, GetCommandError()); | 374 EXPECT_EQ(BATTOR_ERROR_TOO_MANY_INIT_RETRIES, GetCommandError()); |
| 379 } | 375 } |
| 380 | 376 |
| 381 TEST_F(BattOrAgentTest, StartTracingFailsIfInitWrongAckRead) { | 377 TEST_F(BattOrAgentTest, StartTracingFailsIfInitWrongAckRead) { |
| 382 RunStartTracingTo(BattOrAgentState::INIT_SENT); | 378 RunStartTracingTo(BattOrAgentState::INIT_SENT); |
| 383 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, | 379 |
| 384 ToCharVector(kStartTracingAck)); | 380 for (int i = 0; i < 20; i++) { |
| 381 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, | |
| 382 ToCharVector(kStartTracingAck)); | |
| 383 | |
| 384 // Bytes will be sent because INIT will be retried. | |
| 385 OnBytesSent(true); | |
| 386 } | |
| 385 | 387 |
| 386 EXPECT_TRUE(IsCommandComplete()); | 388 EXPECT_TRUE(IsCommandComplete()); |
| 387 EXPECT_EQ(BATTOR_ERROR_UNEXPECTED_MESSAGE, GetCommandError()); | 389 EXPECT_EQ(BATTOR_ERROR_TOO_MANY_INIT_RETRIES, GetCommandError()); |
| 388 } | 390 } |
| 389 | 391 |
| 390 TEST_F(BattOrAgentTest, StartTracingFailsIfSetGainSendFails) { | 392 TEST_F(BattOrAgentTest, StartTracingFailsIfSetGainSendFails) { |
| 391 RunStartTracingTo(BattOrAgentState::RESET_SENT); | 393 RunStartTracingTo(BattOrAgentState::INIT_SENT); |
| 392 OnBytesSent(false); | 394 OnBytesSent(false); |
| 393 | 395 |
| 394 EXPECT_TRUE(IsCommandComplete()); | 396 EXPECT_TRUE(IsCommandComplete()); |
| 395 EXPECT_EQ(BATTOR_ERROR_SEND_ERROR, GetCommandError()); | 397 EXPECT_EQ(BATTOR_ERROR_SEND_ERROR, GetCommandError()); |
| 396 } | 398 } |
| 397 | 399 |
| 398 TEST_F(BattOrAgentTest, StartTracingFailsIfSetGainAckReadFails) { | 400 TEST_F(BattOrAgentTest, StartTracingFailsIfSetGainAckReadFails) { |
| 399 RunStartTracingTo(BattOrAgentState::SET_GAIN_SENT); | 401 RunStartTracingTo(BattOrAgentState::SET_GAIN_SENT); |
| 400 OnMessageRead(false, BATTOR_MESSAGE_TYPE_CONTROL_ACK, nullptr); | 402 OnMessageRead(false, BATTOR_MESSAGE_TYPE_CONTROL_ACK, nullptr); |
| 401 | 403 |
| 402 EXPECT_TRUE(IsCommandComplete()); | 404 EXPECT_TRUE(IsCommandComplete()); |
| 403 EXPECT_EQ(BATTOR_ERROR_RECEIVE_ERROR, GetCommandError()); | 405 EXPECT_EQ(BATTOR_ERROR_RECEIVE_ERROR, GetCommandError()); |
| 404 } | 406 } |
| 405 | 407 |
| 406 TEST_F(BattOrAgentTest, StartTracingFailsIfSetGainWrongAckRead) { | 408 TEST_F(BattOrAgentTest, StartTracingFailsIfSetGainWrongAckRead) { |
| 407 RunStartTracingTo(BattOrAgentState::SET_GAIN_SENT); | 409 RunStartTracingTo(BattOrAgentState::SET_GAIN_SENT); |
| 408 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, | 410 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, |
| 409 ToCharVector(kStartTracingAck)); | 411 ToCharVector(kStartTracingAck)); |
| 410 | 412 |
| 411 EXPECT_TRUE(IsCommandComplete()); | 413 EXPECT_TRUE(IsCommandComplete()); |
| 412 EXPECT_EQ(BATTOR_ERROR_UNEXPECTED_MESSAGE, GetCommandError()); | 414 EXPECT_EQ(BATTOR_ERROR_UNEXPECTED_MESSAGE, GetCommandError()); |
| 413 } | 415 } |
| 414 | 416 |
| 415 TEST_F(BattOrAgentTest, StartTracingFailsIfStartTracingSendFails) { | 417 TEST_F(BattOrAgentTest, StartTracingFailsIfStartTracingSendFails) { |
| 416 RunStartTracingTo(BattOrAgentState::RESET_SENT); | 418 RunStartTracingTo(BattOrAgentState::INIT_SENT); |
| 417 OnBytesSent(false); | 419 OnBytesSent(false); |
| 418 | 420 |
| 419 EXPECT_TRUE(IsCommandComplete()); | 421 EXPECT_TRUE(IsCommandComplete()); |
| 420 EXPECT_EQ(BATTOR_ERROR_SEND_ERROR, GetCommandError()); | 422 EXPECT_EQ(BATTOR_ERROR_SEND_ERROR, GetCommandError()); |
| 421 } | 423 } |
| 422 | 424 |
| 423 TEST_F(BattOrAgentTest, StartTracingFailsIfStartTracingAckReadFails) { | 425 TEST_F(BattOrAgentTest, StartTracingFailsIfStartTracingAckReadFails) { |
| 424 RunStartTracingTo(BattOrAgentState::START_TRACING_SENT); | 426 RunStartTracingTo(BattOrAgentState::START_TRACING_SENT); |
| 425 OnMessageRead(false, BATTOR_MESSAGE_TYPE_CONTROL_ACK, nullptr); | 427 OnMessageRead(false, BATTOR_MESSAGE_TYPE_CONTROL_ACK, nullptr); |
| 426 | 428 |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 566 } | 568 } |
| 567 | 569 |
| 568 TEST_F(BattOrAgentTest, StopTracingFailsIfRequestSamplesFails) { | 570 TEST_F(BattOrAgentTest, StopTracingFailsIfRequestSamplesFails) { |
| 569 RunStopTracingTo(BattOrAgentState::EEPROM_RECEIVED); | 571 RunStopTracingTo(BattOrAgentState::EEPROM_RECEIVED); |
| 570 OnBytesSent(false); | 572 OnBytesSent(false); |
| 571 | 573 |
| 572 EXPECT_TRUE(IsCommandComplete()); | 574 EXPECT_TRUE(IsCommandComplete()); |
| 573 EXPECT_EQ(BATTOR_ERROR_SEND_ERROR, GetCommandError()); | 575 EXPECT_EQ(BATTOR_ERROR_SEND_ERROR, GetCommandError()); |
| 574 } | 576 } |
| 575 | 577 |
| 576 TEST_F(BattOrAgentTest, StopTracingSucceedsWithOneCalibrationFrameReadFailure) { | 578 TEST_F(BattOrAgentTest, StopTracingSucceedsWithOneCalibrationFrameReadFailure) { |
|
charliea (OOO until 10-5)
2016/06/15 14:28:50
I would add a test (up by the StartTracing tests)
aschulman
2016/06/15 16:47:41
Done.
| |
| 577 RunStopTracingTo(BattOrAgentState::SAMPLES_REQUEST_SENT); | 579 RunStopTracingTo(BattOrAgentState::SAMPLES_REQUEST_SENT); |
| 578 | 580 |
| 579 // Make a read fail in order to make sure that the agent will retry. | 581 // Make a read fail in order to make sure that the agent will retry. |
| 580 OnMessageRead(false, BATTOR_MESSAGE_TYPE_SAMPLES, nullptr); | 582 OnMessageRead(false, BATTOR_MESSAGE_TYPE_SAMPLES, nullptr); |
| 581 | 583 |
| 582 BattOrFrameHeader cal_frame_header{0, sizeof(RawBattOrSample)}; | 584 BattOrFrameHeader cal_frame_header{0, sizeof(RawBattOrSample)}; |
| 583 RawBattOrSample cal_frame[] = {RawBattOrSample{1, 1}}; | 585 RawBattOrSample cal_frame[] = {RawBattOrSample{1, 1}}; |
| 584 OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, | 586 OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, |
| 585 CreateFrame(cal_frame_header, cal_frame, 1)); | 587 CreateFrame(cal_frame_header, cal_frame, 1)); |
| 586 | 588 |
| (...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 867 | 869 |
| 868 uint32_t current_sample = 1; | 870 uint32_t current_sample = 1; |
| 869 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL, | 871 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL, |
| 870 ToCharVector(current_sample)); | 872 ToCharVector(current_sample)); |
| 871 | 873 |
| 872 EXPECT_TRUE(IsCommandComplete()); | 874 EXPECT_TRUE(IsCommandComplete()); |
| 873 EXPECT_EQ(BATTOR_ERROR_UNEXPECTED_MESSAGE, GetCommandError()); | 875 EXPECT_EQ(BATTOR_ERROR_UNEXPECTED_MESSAGE, GetCommandError()); |
| 874 } | 876 } |
| 875 | 877 |
| 876 } // namespace battor | 878 } // namespace battor |
| OLD | NEW |