| 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 <memory> |
| 6 |
| 5 #include "tools/battor_agent/battor_agent.h" | 7 #include "tools/battor_agent/battor_agent.h" |
| 6 | 8 |
| 7 #include "base/test/test_simple_task_runner.h" | 9 #include "base/test/test_simple_task_runner.h" |
| 8 #include "base/threading/thread_task_runner_handle.h" | 10 #include "base/threading/thread_task_runner_handle.h" |
| 9 #include "testing/gmock/include/gmock/gmock.h" | 11 #include "testing/gmock/include/gmock/gmock.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 11 #include "tools/battor_agent/battor_protocol_types.h" | 13 #include "tools/battor_agent/battor_protocol_types.h" |
| 12 | 14 |
| 13 using namespace testing; | 15 using namespace testing; |
| 14 | 16 |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 is_command_complete_ = true; | 112 is_command_complete_ = true; |
| 111 command_error_ = error; | 113 command_error_ = error; |
| 112 trace_ = trace; | 114 trace_ = trace; |
| 113 } | 115 } |
| 114 | 116 |
| 115 void OnRecordClockSyncMarkerComplete(BattOrError error) override { | 117 void OnRecordClockSyncMarkerComplete(BattOrError error) override { |
| 116 is_command_complete_ = true; | 118 is_command_complete_ = true; |
| 117 command_error_ = error; | 119 command_error_ = error; |
| 118 } | 120 } |
| 119 | 121 |
| 122 void OnGetFirmwareGitHashComplete(const std::string& firmware_git_hash, |
| 123 BattOrError error) override { |
| 124 is_command_complete_ = true; |
| 125 command_error_ = error; |
| 126 firmware_git_hash_ = firmware_git_hash; |
| 127 } |
| 128 |
| 120 void OnBytesSent(bool success) { | 129 void OnBytesSent(bool success) { |
| 121 agent_->OnBytesSent(success); | 130 agent_->OnBytesSent(success); |
| 122 task_runner_->RunUntilIdle(); | 131 task_runner_->RunUntilIdle(); |
| 123 } | 132 } |
| 124 | 133 |
| 125 void OnMessageRead(bool success, | 134 void OnMessageRead(bool success, |
| 126 BattOrMessageType type, | 135 BattOrMessageType type, |
| 127 std::unique_ptr<std::vector<char>> bytes) { | 136 std::unique_ptr<std::vector<char>> bytes) { |
| 128 agent_->OnMessageRead(success, type, std::move(bytes)); | 137 agent_->OnMessageRead(success, type, std::move(bytes)); |
| 129 task_runner_->RunUntilIdle(); | 138 task_runner_->RunUntilIdle(); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 152 | 161 |
| 153 // States required to StopTracing. | 162 // States required to StopTracing. |
| 154 EEPROM_REQUEST_SENT, | 163 EEPROM_REQUEST_SENT, |
| 155 EEPROM_RECEIVED, | 164 EEPROM_RECEIVED, |
| 156 SAMPLES_REQUEST_SENT, | 165 SAMPLES_REQUEST_SENT, |
| 157 CALIBRATION_FRAME_RECEIVED, | 166 CALIBRATION_FRAME_RECEIVED, |
| 158 | 167 |
| 159 // States required to RecordClockSyncMarker. | 168 // States required to RecordClockSyncMarker. |
| 160 CURRENT_SAMPLE_REQUEST_SENT, | 169 CURRENT_SAMPLE_REQUEST_SENT, |
| 161 RECORD_CLOCK_SYNC_MARKER_COMPLETE, | 170 RECORD_CLOCK_SYNC_MARKER_COMPLETE, |
| 171 |
| 172 // States required to GetFirmwareGitHash. |
| 173 GIT_FIRMWARE_HASH_REQUEST_SENT, |
| 174 READ_GIT_HASH_RECEIVED, |
| 162 }; | 175 }; |
| 163 | 176 |
| 164 // Runs BattOrAgent::StartTracing until it reaches the specified state by | 177 // Runs BattOrAgent::StartTracing until it reaches the specified state by |
| 165 // feeding it the callbacks it needs to progress. | 178 // feeding it the callbacks it needs to progress. |
| 166 void RunStartTracingTo(BattOrAgentState end_state) { | 179 void RunStartTracingTo(BattOrAgentState end_state) { |
| 167 is_command_complete_ = false; | 180 is_command_complete_ = false; |
| 168 | 181 |
| 169 GetAgent()->StartTracing(); | 182 GetAgent()->StartTracing(); |
| 170 GetTaskRunner()->RunUntilIdle(); | 183 GetTaskRunner()->RunUntilIdle(); |
| 171 | 184 |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 266 if (end_state == BattOrAgentState::CURRENT_SAMPLE_REQUEST_SENT) | 279 if (end_state == BattOrAgentState::CURRENT_SAMPLE_REQUEST_SENT) |
| 267 return; | 280 return; |
| 268 | 281 |
| 269 DCHECK(end_state == BattOrAgentState::RECORD_CLOCK_SYNC_MARKER_COMPLETE); | 282 DCHECK(end_state == BattOrAgentState::RECORD_CLOCK_SYNC_MARKER_COMPLETE); |
| 270 | 283 |
| 271 uint32_t current_sample = 1; | 284 uint32_t current_sample = 1; |
| 272 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, | 285 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, |
| 273 ToCharVector(current_sample)); | 286 ToCharVector(current_sample)); |
| 274 } | 287 } |
| 275 | 288 |
| 289 // Runs BattOrAgent::GetFirmwareGitHash until it reaches the specified |
| 290 // state by feeding it the callbacks it needs to progress. |
| 291 void RunGetFirmwareGitHashTo(BattOrAgentState end_state) { |
| 292 is_command_complete_ = false; |
| 293 |
| 294 GetAgent()->GetFirmwareGitHash(); |
| 295 GetTaskRunner()->RunUntilIdle(); |
| 296 |
| 297 GetAgent()->OnConnectionOpened(true); |
| 298 GetTaskRunner()->RunUntilIdle(); |
| 299 |
| 300 if (end_state == BattOrAgentState::CONNECTED) |
| 301 return; |
| 302 |
| 303 OnBytesSent(true); |
| 304 if (end_state == BattOrAgentState::GIT_FIRMWARE_HASH_REQUEST_SENT) |
| 305 return; |
| 306 |
| 307 DCHECK(end_state == BattOrAgentState::READ_GIT_HASH_RECEIVED); |
| 308 |
| 309 std::unique_ptr<std::vector<char>> firmware_git_hash_vector( |
| 310 new std::vector<char>{'G', 'I', 'T', 'H', 'A', 'S', 'H'}); |
| 311 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, |
| 312 std::move(firmware_git_hash_vector)); |
| 313 } |
| 314 |
| 276 TestableBattOrAgent* GetAgent() { return agent_.get(); } | 315 TestableBattOrAgent* GetAgent() { return agent_.get(); } |
| 277 | 316 |
| 278 scoped_refptr<base::TestSimpleTaskRunner> GetTaskRunner() { | 317 scoped_refptr<base::TestSimpleTaskRunner> GetTaskRunner() { |
| 279 return task_runner_; | 318 return task_runner_; |
| 280 } | 319 } |
| 281 | 320 |
| 282 bool IsCommandComplete() { return is_command_complete_; } | 321 bool IsCommandComplete() { return is_command_complete_; } |
| 283 BattOrError GetCommandError() { return command_error_; } | 322 BattOrError GetCommandError() { return command_error_; } |
| 284 std::string GetTrace() { return trace_; } | 323 std::string GetTrace() { return trace_; } |
| 324 std::string GetGitHash() { return firmware_git_hash_; } |
| 285 | 325 |
| 286 private: | 326 private: |
| 287 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | 327 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
| 288 // Needed to support ThreadTaskRunnerHandle::Get() in code under test. | 328 // Needed to support ThreadTaskRunnerHandle::Get() in code under test. |
| 289 base::ThreadTaskRunnerHandle thread_task_runner_handle_; | 329 base::ThreadTaskRunnerHandle thread_task_runner_handle_; |
| 290 | 330 |
| 291 std::unique_ptr<TestableBattOrAgent> agent_; | 331 std::unique_ptr<TestableBattOrAgent> agent_; |
| 292 bool is_command_complete_; | 332 bool is_command_complete_; |
| 293 BattOrError command_error_; | 333 BattOrError command_error_; |
| 294 std::string trace_; | 334 std::string trace_; |
| 335 std::string firmware_git_hash_; |
| 295 }; | 336 }; |
| 296 | 337 |
| 297 TEST_F(BattOrAgentTest, StartTracing) { | 338 TEST_F(BattOrAgentTest, StartTracing) { |
| 298 testing::InSequence s; | 339 testing::InSequence s; |
| 299 EXPECT_CALL(*GetAgent()->GetConnection(), Open()); | 340 EXPECT_CALL(*GetAgent()->GetConnection(), Open()); |
| 300 | 341 |
| 301 EXPECT_CALL(*GetAgent()->GetConnection(), Flush()); | 342 EXPECT_CALL(*GetAgent()->GetConnection(), Flush()); |
| 302 BattOrControlMessage init_msg{BATTOR_CONTROL_MESSAGE_TYPE_INIT, 0, 0}; | 343 BattOrControlMessage init_msg{BATTOR_CONTROL_MESSAGE_TYPE_INIT, 0, 0}; |
| 303 EXPECT_CALL( | 344 EXPECT_CALL( |
| 304 *GetAgent()->GetConnection(), | 345 *GetAgent()->GetConnection(), |
| (...skipping 581 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 886 RunRecordClockSyncMarkerTo(BattOrAgentState::CURRENT_SAMPLE_REQUEST_SENT); | 927 RunRecordClockSyncMarkerTo(BattOrAgentState::CURRENT_SAMPLE_REQUEST_SENT); |
| 887 | 928 |
| 888 uint32_t current_sample = 1; | 929 uint32_t current_sample = 1; |
| 889 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL, | 930 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL, |
| 890 ToCharVector(current_sample)); | 931 ToCharVector(current_sample)); |
| 891 | 932 |
| 892 EXPECT_TRUE(IsCommandComplete()); | 933 EXPECT_TRUE(IsCommandComplete()); |
| 893 EXPECT_EQ(BATTOR_ERROR_UNEXPECTED_MESSAGE, GetCommandError()); | 934 EXPECT_EQ(BATTOR_ERROR_UNEXPECTED_MESSAGE, GetCommandError()); |
| 894 } | 935 } |
| 895 | 936 |
| 937 TEST_F(BattOrAgentTest, GetFirmwareGitHash) { |
| 938 RunGetFirmwareGitHashTo(BattOrAgentState::READ_GIT_HASH_RECEIVED); |
| 939 EXPECT_TRUE(IsCommandComplete()); |
| 940 EXPECT_EQ(BATTOR_ERROR_NONE, GetCommandError()); |
| 941 EXPECT_EQ("GITHASH", GetGitHash()); |
| 942 } |
| 943 |
| 944 TEST_F(BattOrAgentTest, GetFirmwareGitHashFailsWithoutConnection) { |
| 945 GetAgent()->GetFirmwareGitHash(); |
| 946 GetTaskRunner()->RunUntilIdle(); |
| 947 |
| 948 GetAgent()->OnConnectionOpened(false); |
| 949 GetTaskRunner()->RunUntilIdle(); |
| 950 |
| 951 EXPECT_TRUE(IsCommandComplete()); |
| 952 EXPECT_EQ(BATTOR_ERROR_CONNECTION_FAILED, GetCommandError()); |
| 953 } |
| 954 |
| 955 TEST_F(BattOrAgentTest, GetFirmwareGitHashFailsIfReadHasWrongType) { |
| 956 RunGetFirmwareGitHashTo(BattOrAgentState::GIT_FIRMWARE_HASH_REQUEST_SENT); |
| 957 |
| 958 uint32_t current_sample = 1; |
| 959 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL, |
| 960 ToCharVector(current_sample)); |
| 961 |
| 962 EXPECT_TRUE(IsCommandComplete()); |
| 963 EXPECT_EQ(BATTOR_ERROR_UNEXPECTED_MESSAGE, GetCommandError()); |
| 964 } |
| 896 } // namespace battor | 965 } // namespace battor |
| OLD | NEW |