| 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 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 | 97 |
| 98 void OnStartTracingComplete(BattOrError error) override { | 98 void OnStartTracingComplete(BattOrError error) override { |
| 99 is_command_complete_ = true; | 99 is_command_complete_ = true; |
| 100 command_error_ = error; | 100 command_error_ = error; |
| 101 } | 101 } |
| 102 | 102 |
| 103 void OnStopTracingComplete(const std::string& trace, | 103 void OnStopTracingComplete(const std::string& trace, |
| 104 BattOrError error) override { | 104 BattOrError error) override { |
| 105 is_command_complete_ = true; | 105 is_command_complete_ = true; |
| 106 command_error_ = error; | 106 command_error_ = error; |
| 107 trace_ = trace; |
| 107 } | 108 } |
| 108 | 109 |
| 109 protected: | 110 protected: |
| 110 void SetUp() override { | 111 void SetUp() override { |
| 111 agent_.reset(new TestableBattOrAgent(this)); | 112 agent_.reset(new TestableBattOrAgent(this)); |
| 112 task_runner_->ClearPendingTasks(); | 113 task_runner_->ClearPendingTasks(); |
| 113 is_command_complete_ = false; | 114 is_command_complete_ = false; |
| 114 command_error_ = BATTOR_ERROR_NONE; | 115 command_error_ = BATTOR_ERROR_NONE; |
| 115 } | 116 } |
| 116 | 117 |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 | 210 |
| 210 if (end_state == BattOrAgentState::CONNECTED) | 211 if (end_state == BattOrAgentState::CONNECTED) |
| 211 return; | 212 return; |
| 212 | 213 |
| 213 GetAgent()->OnBytesSent(true); | 214 GetAgent()->OnBytesSent(true); |
| 214 GetTaskRunner()->RunUntilIdle(); | 215 GetTaskRunner()->RunUntilIdle(); |
| 215 | 216 |
| 216 if (end_state == BattOrAgentState::EEPROM_REQUEST_SENT) | 217 if (end_state == BattOrAgentState::EEPROM_REQUEST_SENT) |
| 217 return; | 218 return; |
| 218 | 219 |
| 220 BattOrEEPROM eeprom; |
| 221 eeprom.r1 = 1; |
| 222 eeprom.r2 = 1; |
| 223 eeprom.r3 = 1; |
| 224 eeprom.low_gain = 1; |
| 225 eeprom.low_gain_correction_offset = 0; |
| 226 eeprom.low_gain_correction_factor = 1; |
| 227 eeprom.sd_sample_rate = 1000; |
| 228 |
| 219 GetAgent()->OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, | 229 GetAgent()->OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, |
| 220 ToCharVector(BattOrEEPROM())); | 230 ToCharVector(eeprom)); |
| 221 GetTaskRunner()->RunUntilIdle(); | 231 GetTaskRunner()->RunUntilIdle(); |
| 222 | 232 |
| 223 if (end_state == BattOrAgentState::EEPROM_RECEIVED) | 233 if (end_state == BattOrAgentState::EEPROM_RECEIVED) |
| 224 return; | 234 return; |
| 225 | 235 |
| 226 GetAgent()->OnBytesSent(true); | 236 GetAgent()->OnBytesSent(true); |
| 227 GetTaskRunner()->RunUntilIdle(); | 237 GetTaskRunner()->RunUntilIdle(); |
| 228 | 238 |
| 229 if (end_state == BattOrAgentState::SAMPLES_REQUEST_SENT) | 239 if (end_state == BattOrAgentState::SAMPLES_REQUEST_SENT) |
| 230 return; | 240 return; |
| 231 | 241 |
| 232 DCHECK(end_state == BattOrAgentState::CALIBRATION_FRAME_SENT); | 242 DCHECK(end_state == BattOrAgentState::CALIBRATION_FRAME_SENT); |
| 233 | 243 |
| 234 BattOrFrameHeader cal_frame_header{0, sizeof(RawBattOrSample)}; | 244 BattOrFrameHeader cal_frame_header{0, sizeof(RawBattOrSample)}; |
| 235 RawBattOrSample cal_frame[] = {RawBattOrSample{1, 1}}; | 245 RawBattOrSample cal_frame[] = {RawBattOrSample{1, 1}}; |
| 236 GetAgent()->OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, | 246 GetAgent()->OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, |
| 237 CreateFrame(cal_frame_header, cal_frame, 1)); | 247 CreateFrame(cal_frame_header, cal_frame, 1)); |
| 238 GetTaskRunner()->RunUntilIdle(); | 248 GetTaskRunner()->RunUntilIdle(); |
| 239 } | 249 } |
| 240 | 250 |
| 241 TestableBattOrAgent* GetAgent() { return agent_.get(); } | 251 TestableBattOrAgent* GetAgent() { return agent_.get(); } |
| 242 | 252 |
| 243 scoped_refptr<base::TestSimpleTaskRunner> GetTaskRunner() { | 253 scoped_refptr<base::TestSimpleTaskRunner> GetTaskRunner() { |
| 244 return task_runner_; | 254 return task_runner_; |
| 245 } | 255 } |
| 246 | 256 |
| 247 bool IsCommandComplete() { return is_command_complete_; } | 257 bool IsCommandComplete() { return is_command_complete_; } |
| 248 BattOrError GetCommandError() { return command_error_; } | 258 BattOrError GetCommandError() { return command_error_; } |
| 259 std::string GetTrace() { return trace_; } |
| 249 | 260 |
| 250 private: | 261 private: |
| 251 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | 262 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
| 252 // Needed to support ThreadTaskRunnerHandle::Get() in code under test. | 263 // Needed to support ThreadTaskRunnerHandle::Get() in code under test. |
| 253 base::ThreadTaskRunnerHandle thread_task_runner_handle_; | 264 base::ThreadTaskRunnerHandle thread_task_runner_handle_; |
| 254 | 265 |
| 255 scoped_ptr<TestableBattOrAgent> agent_; | 266 scoped_ptr<TestableBattOrAgent> agent_; |
| 256 bool is_command_complete_; | 267 bool is_command_complete_; |
| 257 BattOrError command_error_; | 268 BattOrError command_error_; |
| 258 std::string stop_tracing_trace_; | 269 std::string trace_; |
| 259 }; | 270 }; |
| 260 | 271 |
| 261 TEST_F(BattOrAgentTest, StartTracing) { | 272 TEST_F(BattOrAgentTest, StartTracing) { |
| 262 testing::InSequence s; | 273 testing::InSequence s; |
| 263 EXPECT_CALL(*GetAgent()->GetConnection(), Open()); | 274 EXPECT_CALL(*GetAgent()->GetConnection(), Open()); |
| 264 | 275 |
| 265 BattOrControlMessage reset_msg{BATTOR_CONTROL_MESSAGE_TYPE_RESET, 0, 0}; | 276 BattOrControlMessage reset_msg{BATTOR_CONTROL_MESSAGE_TYPE_RESET, 0, 0}; |
| 266 EXPECT_CALL( | 277 EXPECT_CALL( |
| 267 *GetAgent()->GetConnection(), | 278 *GetAgent()->GetConnection(), |
| 268 SendBytes(BATTOR_MESSAGE_TYPE_CONTROL, | 279 SendBytes(BATTOR_MESSAGE_TYPE_CONTROL, |
| (...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 460 CreateFrame(frame_header2, frame2, 1)); | 471 CreateFrame(frame_header2, frame2, 1)); |
| 461 | 472 |
| 462 // Send an empty last frame to indicate that we're done. | 473 // Send an empty last frame to indicate that we're done. |
| 463 BattOrFrameHeader frame_header3{0, 0 * sizeof(RawBattOrSample)}; | 474 BattOrFrameHeader frame_header3{0, 0 * sizeof(RawBattOrSample)}; |
| 464 RawBattOrSample frame3[] = {}; | 475 RawBattOrSample frame3[] = {}; |
| 465 GetAgent()->OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, | 476 GetAgent()->OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, |
| 466 CreateFrame(frame_header3, frame3, 0)); | 477 CreateFrame(frame_header3, frame3, 0)); |
| 467 | 478 |
| 468 EXPECT_TRUE(IsCommandComplete()); | 479 EXPECT_TRUE(IsCommandComplete()); |
| 469 EXPECT_EQ(BATTOR_ERROR_NONE, GetCommandError()); | 480 EXPECT_EQ(BATTOR_ERROR_NONE, GetCommandError()); |
| 481 EXPECT_EQ("0.00 -0.3 -0.6\n1.00 0.3 0.6\n2.00 0.9 1.8\n3.00 -0.3 -0.6\n", |
| 482 GetTrace()); |
| 470 } | 483 } |
| 471 | 484 |
| 472 TEST_F(BattOrAgentTest, StopTracingFailsWithoutConnection) { | 485 TEST_F(BattOrAgentTest, StopTracingFailsWithoutConnection) { |
| 473 GetAgent()->StopTracing(); | 486 GetAgent()->StopTracing(); |
| 474 GetTaskRunner()->RunUntilIdle(); | 487 GetTaskRunner()->RunUntilIdle(); |
| 475 | 488 |
| 476 GetAgent()->OnConnectionOpened(false); | 489 GetAgent()->OnConnectionOpened(false); |
| 477 GetTaskRunner()->RunUntilIdle(); | 490 GetTaskRunner()->RunUntilIdle(); |
| 478 | 491 |
| 479 EXPECT_TRUE(IsCommandComplete()); | 492 EXPECT_TRUE(IsCommandComplete()); |
| (...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 734 | 747 |
| 735 GetAgent()->OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, | 748 GetAgent()->OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, |
| 736 std::move(frame_bytes)); | 749 std::move(frame_bytes)); |
| 737 GetTaskRunner()->RunUntilIdle(); | 750 GetTaskRunner()->RunUntilIdle(); |
| 738 | 751 |
| 739 EXPECT_TRUE(IsCommandComplete()); | 752 EXPECT_TRUE(IsCommandComplete()); |
| 740 EXPECT_EQ(BATTOR_ERROR_UNEXPECTED_MESSAGE, GetCommandError()); | 753 EXPECT_EQ(BATTOR_ERROR_UNEXPECTED_MESSAGE, GetCommandError()); |
| 741 } | 754 } |
| 742 | 755 |
| 743 } // namespace battor | 756 } // namespace battor |
| OLD | NEW |