Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(5)

Side by Side Diff: tools/battor_agent/battor_agent_unittest.cc

Issue 2390893002: [BattOr] Make BattOr able to return firmware version. (Closed)
Patch Set: [BattOr] Make BattOr able to return firmware version. Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 #include <memory>
charliea (OOO until 10-5) 2016/10/20 12:23:28 I think there should probably be one empty line af
rnephew (Reviews Here) 2016/10/20 18:03:44 Done.
4 5
5 #include "tools/battor_agent/battor_agent.h" 6 #include "tools/battor_agent/battor_agent.h"
6 7
7 #include "base/test/test_simple_task_runner.h" 8 #include "base/test/test_simple_task_runner.h"
8 #include "base/threading/thread_task_runner_handle.h" 9 #include "base/threading/thread_task_runner_handle.h"
9 #include "testing/gmock/include/gmock/gmock.h" 10 #include "testing/gmock/include/gmock/gmock.h"
10 #include "testing/gtest/include/gtest/gtest.h" 11 #include "testing/gtest/include/gtest/gtest.h"
11 #include "tools/battor_agent/battor_protocol_types.h" 12 #include "tools/battor_agent/battor_protocol_types.h"
12 13
13 using namespace testing; 14 using namespace testing;
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
110 is_command_complete_ = true; 111 is_command_complete_ = true;
111 command_error_ = error; 112 command_error_ = error;
112 trace_ = trace; 113 trace_ = trace;
113 } 114 }
114 115
115 void OnRecordClockSyncMarkerComplete(BattOrError error) override { 116 void OnRecordClockSyncMarkerComplete(BattOrError error) override {
116 is_command_complete_ = true; 117 is_command_complete_ = true;
117 command_error_ = error; 118 command_error_ = error;
118 } 119 }
119 120
121 void OnGetFirmwareGitHashComplete(const std::string& git_hash,
122 BattOrError error) override {
123 is_command_complete_ = true;
124 command_error_ = error;
125 git_hash_ = git_hash;
126 }
127
120 void OnBytesSent(bool success) { 128 void OnBytesSent(bool success) {
121 agent_->OnBytesSent(success); 129 agent_->OnBytesSent(success);
122 task_runner_->RunUntilIdle(); 130 task_runner_->RunUntilIdle();
123 } 131 }
124 132
125 void OnMessageRead(bool success, 133 void OnMessageRead(bool success,
126 BattOrMessageType type, 134 BattOrMessageType type,
127 std::unique_ptr<std::vector<char>> bytes) { 135 std::unique_ptr<std::vector<char>> bytes) {
128 agent_->OnMessageRead(success, type, std::move(bytes)); 136 agent_->OnMessageRead(success, type, std::move(bytes));
129 task_runner_->RunUntilIdle(); 137 task_runner_->RunUntilIdle();
(...skipping 22 matching lines...) Expand all
152 160
153 // States required to StopTracing. 161 // States required to StopTracing.
154 EEPROM_REQUEST_SENT, 162 EEPROM_REQUEST_SENT,
155 EEPROM_RECEIVED, 163 EEPROM_RECEIVED,
156 SAMPLES_REQUEST_SENT, 164 SAMPLES_REQUEST_SENT,
157 CALIBRATION_FRAME_RECEIVED, 165 CALIBRATION_FRAME_RECEIVED,
158 166
159 // States required to RecordClockSyncMarker. 167 // States required to RecordClockSyncMarker.
160 CURRENT_SAMPLE_REQUEST_SENT, 168 CURRENT_SAMPLE_REQUEST_SENT,
161 RECORD_CLOCK_SYNC_MARKER_COMPLETE, 169 RECORD_CLOCK_SYNC_MARKER_COMPLETE,
170
171 // States required to GetFirmwareGitHash.
172 READ_GIT_HASH_REQUEST_SENT,
charliea (OOO until 10-5) 2016/10/20 12:23:28 I think probably GIT_FIRMWARE_HASH_REQUEST_[SENT,R
rnephew (Reviews Here) 2016/10/20 18:03:44 Done.
173 READ_GIT_HASH_RECEIVED,
162 }; 174 };
163 175
164 // Runs BattOrAgent::StartTracing until it reaches the specified state by 176 // Runs BattOrAgent::StartTracing until it reaches the specified state by
165 // feeding it the callbacks it needs to progress. 177 // feeding it the callbacks it needs to progress.
166 void RunStartTracingTo(BattOrAgentState end_state) { 178 void RunStartTracingTo(BattOrAgentState end_state) {
167 is_command_complete_ = false; 179 is_command_complete_ = false;
168 180
169 GetAgent()->StartTracing(); 181 GetAgent()->StartTracing();
170 GetTaskRunner()->RunUntilIdle(); 182 GetTaskRunner()->RunUntilIdle();
171 183
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
241 return; 253 return;
242 254
243 DCHECK(end_state == BattOrAgentState::CALIBRATION_FRAME_RECEIVED); 255 DCHECK(end_state == BattOrAgentState::CALIBRATION_FRAME_RECEIVED);
244 256
245 BattOrFrameHeader cal_frame_header{0, sizeof(RawBattOrSample)}; 257 BattOrFrameHeader cal_frame_header{0, sizeof(RawBattOrSample)};
246 RawBattOrSample cal_frame[] = {RawBattOrSample{1, 1}}; 258 RawBattOrSample cal_frame[] = {RawBattOrSample{1, 1}};
247 OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, 259 OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES,
248 CreateFrame(cal_frame_header, cal_frame, 1)); 260 CreateFrame(cal_frame_header, cal_frame, 1));
249 } 261 }
250 262
263 // Runs BattOrAgent::GetFirmwareGitHash until it reaches the specified
charliea (OOO until 10-5) 2016/10/20 12:23:28 I think it probably makes sense to move this below
rnephew (Reviews Here) 2016/10/20 18:03:44 Done.
264 // state by feeding it the callbacks it needs to progress.
265 void RunGetFirmwareGitHashTo(BattOrAgentState end_state) {
266 is_command_complete_ = false;
267
268 GetAgent()->GetFirmwareGitHash();
269 GetTaskRunner()->RunUntilIdle();
270
271 GetAgent()->OnConnectionOpened(true);
272 GetTaskRunner()->RunUntilIdle();
273
274 if (end_state == BattOrAgentState::CONNECTED)
275 return;
276
277 OnBytesSent(true);
278 if (end_state == BattOrAgentState::READ_GIT_HASH_REQUEST_SENT)
279 return;
280
281 DCHECK(end_state == BattOrAgentState::READ_GIT_HASH_RECEIVED);
282
283 std::unique_ptr<std::vector<char>> git_hash_vector(
284 new std::vector<char>{'G', 'I', 'T', 'H', 'A', 'S', 'H'});
285 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK,
286 std::move(git_hash_vector));
287 }
288
251 // Runs BattOrAgent::RecordClockSyncMarker until it reaches the specified 289 // Runs BattOrAgent::RecordClockSyncMarker until it reaches the specified
252 // state by feeding it the callbacks it needs to progress. 290 // state by feeding it the callbacks it needs to progress.
253 void RunRecordClockSyncMarkerTo(BattOrAgentState end_state) { 291 void RunRecordClockSyncMarkerTo(BattOrAgentState end_state) {
254 is_command_complete_ = false; 292 is_command_complete_ = false;
255 293
256 GetAgent()->RecordClockSyncMarker(kClockSyncId); 294 GetAgent()->RecordClockSyncMarker(kClockSyncId);
257 GetTaskRunner()->RunUntilIdle(); 295 GetTaskRunner()->RunUntilIdle();
258 296
259 GetAgent()->OnConnectionOpened(true); 297 GetAgent()->OnConnectionOpened(true);
260 GetTaskRunner()->RunUntilIdle(); 298 GetTaskRunner()->RunUntilIdle();
(...skipping 14 matching lines...) Expand all
275 313
276 TestableBattOrAgent* GetAgent() { return agent_.get(); } 314 TestableBattOrAgent* GetAgent() { return agent_.get(); }
277 315
278 scoped_refptr<base::TestSimpleTaskRunner> GetTaskRunner() { 316 scoped_refptr<base::TestSimpleTaskRunner> GetTaskRunner() {
279 return task_runner_; 317 return task_runner_;
280 } 318 }
281 319
282 bool IsCommandComplete() { return is_command_complete_; } 320 bool IsCommandComplete() { return is_command_complete_; }
283 BattOrError GetCommandError() { return command_error_; } 321 BattOrError GetCommandError() { return command_error_; }
284 std::string GetTrace() { return trace_; } 322 std::string GetTrace() { return trace_; }
323 std::string GetGitHash() { return git_hash_; }
285 324
286 private: 325 private:
287 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; 326 scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
288 // Needed to support ThreadTaskRunnerHandle::Get() in code under test. 327 // Needed to support ThreadTaskRunnerHandle::Get() in code under test.
289 base::ThreadTaskRunnerHandle thread_task_runner_handle_; 328 base::ThreadTaskRunnerHandle thread_task_runner_handle_;
290 329
291 std::unique_ptr<TestableBattOrAgent> agent_; 330 std::unique_ptr<TestableBattOrAgent> agent_;
292 bool is_command_complete_; 331 bool is_command_complete_;
293 BattOrError command_error_; 332 BattOrError command_error_;
294 std::string trace_; 333 std::string trace_;
334 std::string git_hash_;
295 }; 335 };
296 336
297 TEST_F(BattOrAgentTest, StartTracing) { 337 TEST_F(BattOrAgentTest, StartTracing) {
298 testing::InSequence s; 338 testing::InSequence s;
299 EXPECT_CALL(*GetAgent()->GetConnection(), Open()); 339 EXPECT_CALL(*GetAgent()->GetConnection(), Open());
300 340
301 EXPECT_CALL(*GetAgent()->GetConnection(), Flush()); 341 EXPECT_CALL(*GetAgent()->GetConnection(), Flush());
302 BattOrControlMessage init_msg{BATTOR_CONTROL_MESSAGE_TYPE_INIT, 0, 0}; 342 BattOrControlMessage init_msg{BATTOR_CONTROL_MESSAGE_TYPE_INIT, 0, 0};
303 EXPECT_CALL( 343 EXPECT_CALL(
304 *GetAgent()->GetConnection(), 344 *GetAgent()->GetConnection(),
(...skipping 581 matching lines...) Expand 10 before | Expand all | Expand 10 after
886 RunRecordClockSyncMarkerTo(BattOrAgentState::CURRENT_SAMPLE_REQUEST_SENT); 926 RunRecordClockSyncMarkerTo(BattOrAgentState::CURRENT_SAMPLE_REQUEST_SENT);
887 927
888 uint32_t current_sample = 1; 928 uint32_t current_sample = 1;
889 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL, 929 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL,
890 ToCharVector(current_sample)); 930 ToCharVector(current_sample));
891 931
892 EXPECT_TRUE(IsCommandComplete()); 932 EXPECT_TRUE(IsCommandComplete());
893 EXPECT_EQ(BATTOR_ERROR_UNEXPECTED_MESSAGE, GetCommandError()); 933 EXPECT_EQ(BATTOR_ERROR_UNEXPECTED_MESSAGE, GetCommandError());
894 } 934 }
895 935
936 TEST_F(BattOrAgentTest, GetFirmwareGitHash) {
937 RunGetFirmwareGitHashTo(BattOrAgentState::READ_GIT_HASH_RECEIVED);
938 EXPECT_TRUE(IsCommandComplete());
939 EXPECT_EQ(BATTOR_ERROR_NONE, GetCommandError());
940 EXPECT_EQ("GITHASH", GetGitHash());
941 }
942
943 TEST_F(BattOrAgentTest, GetFirmwareGitHashFailsWithoutConnection) {
944 GetAgent()->GetFirmwareGitHash();
945 GetTaskRunner()->RunUntilIdle();
946
947 GetAgent()->OnConnectionOpened(false);
948 GetTaskRunner()->RunUntilIdle();
949
950 EXPECT_TRUE(IsCommandComplete());
951 EXPECT_EQ(BATTOR_ERROR_CONNECTION_FAILED, GetCommandError());
952 }
953
954 TEST_F(BattOrAgentTest, GetFirmwareGitHashFailsIfReadHasWrongType) {
955 RunGetFirmwareGitHashTo(BattOrAgentState::READ_GIT_HASH_REQUEST_SENT);
956
957 uint32_t current_sample = 1;
958 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL,
959 ToCharVector(current_sample));
960
961 EXPECT_TRUE(IsCommandComplete());
962 EXPECT_EQ(BATTOR_ERROR_UNEXPECTED_MESSAGE, GetCommandError());
963 }
896 } // namespace battor 964 } // namespace battor
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698