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

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

Issue 1869503004: Convert //tools to use std::unique_ptr (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase, change iwyu fixes for converted directories to include <memory> Created 4 years, 8 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
« no previous file with comments | « tools/battor_agent/battor_agent_bin.cc ('k') | tools/battor_agent/battor_connection.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 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"
11 #include "tools/battor_agent/battor_protocol_types.h" 11 #include "tools/battor_agent/battor_protocol_types.h"
12 12
13 using namespace testing; 13 using namespace testing;
14 14
15 using std::vector; 15 using std::vector;
16 16
17 namespace battor { 17 namespace battor {
18 18
19 namespace { 19 namespace {
20 20
21 BattOrControlMessageAck kInitAck{BATTOR_CONTROL_MESSAGE_TYPE_INIT, 0}; 21 BattOrControlMessageAck kInitAck{BATTOR_CONTROL_MESSAGE_TYPE_INIT, 0};
22 BattOrControlMessageAck kSetGainAck{BATTOR_CONTROL_MESSAGE_TYPE_SET_GAIN, 0}; 22 BattOrControlMessageAck kSetGainAck{BATTOR_CONTROL_MESSAGE_TYPE_SET_GAIN, 0};
23 BattOrControlMessageAck kStartTracingAck{ 23 BattOrControlMessageAck kStartTracingAck{
24 BATTOR_CONTROL_MESSAGE_TYPE_START_SAMPLING_SD, 0}; 24 BATTOR_CONTROL_MESSAGE_TYPE_START_SAMPLING_SD, 0};
25 const char kClockSyncId[] = "MY_MARKER"; 25 const char kClockSyncId[] = "MY_MARKER";
26 26
27 // Creates a byte vector copy of the specified object. 27 // Creates a byte vector copy of the specified object.
28 template <typename T> 28 template <typename T>
29 scoped_ptr<std::vector<char>> ToCharVector(const T& object) { 29 std::unique_ptr<std::vector<char>> ToCharVector(const T& object) {
30 return scoped_ptr<std::vector<char>>(new std::vector<char>( 30 return std::unique_ptr<std::vector<char>>(new std::vector<char>(
31 reinterpret_cast<const char*>(&object), 31 reinterpret_cast<const char*>(&object),
32 reinterpret_cast<const char*>(&object) + sizeof(T))); 32 reinterpret_cast<const char*>(&object) + sizeof(T)));
33 } 33 }
34 34
35 MATCHER_P2( 35 MATCHER_P2(
36 BufferEq, 36 BufferEq,
37 expected_buffer, 37 expected_buffer,
38 expected_buffer_size, 38 expected_buffer_size,
39 "Makes sure that the argument has the same contents as the buffer.") { 39 "Makes sure that the argument has the same contents as the buffer.") {
40 return memcmp(reinterpret_cast<const void*>(arg), 40 return memcmp(reinterpret_cast<const void*>(arg),
41 reinterpret_cast<const void*>(expected_buffer), 41 reinterpret_cast<const void*>(expected_buffer),
42 expected_buffer_size) == 0; 42 expected_buffer_size) == 0;
43 } 43 }
44 44
45 scoped_ptr<vector<char>> CreateFrame(const BattOrFrameHeader& frame_header, 45 std::unique_ptr<vector<char>> CreateFrame(const BattOrFrameHeader& frame_header,
46 const RawBattOrSample* samples, 46 const RawBattOrSample* samples,
47 const size_t& num_samples) { 47 const size_t& num_samples) {
48 scoped_ptr<vector<char>> bytes(new vector<char>( 48 std::unique_ptr<vector<char>> bytes(new vector<char>(
49 sizeof(BattOrFrameHeader) + sizeof(RawBattOrSample) * num_samples)); 49 sizeof(BattOrFrameHeader) + sizeof(RawBattOrSample) * num_samples));
50 memcpy(bytes->data(), &frame_header, sizeof(BattOrFrameHeader)); 50 memcpy(bytes->data(), &frame_header, sizeof(BattOrFrameHeader));
51 memcpy(bytes->data() + sizeof(BattOrFrameHeader), samples, 51 memcpy(bytes->data() + sizeof(BattOrFrameHeader), samples,
52 sizeof(RawBattOrSample) * num_samples); 52 sizeof(RawBattOrSample) * num_samples);
53 53
54 return bytes; 54 return bytes;
55 } 55 }
56 56
57 class MockBattOrConnection : public BattOrConnection { 57 class MockBattOrConnection : public BattOrConnection {
58 public: 58 public:
(...skipping 14 matching lines...) Expand all
73 DISALLOW_COPY_AND_ASSIGN(MockBattOrConnection); 73 DISALLOW_COPY_AND_ASSIGN(MockBattOrConnection);
74 }; 74 };
75 75
76 } // namespace 76 } // namespace
77 77
78 // TestableBattOrAgent uses a fake BattOrConnection to be testable. 78 // TestableBattOrAgent uses a fake BattOrConnection to be testable.
79 class TestableBattOrAgent : public BattOrAgent { 79 class TestableBattOrAgent : public BattOrAgent {
80 public: 80 public:
81 TestableBattOrAgent(BattOrAgent::Listener* listener) 81 TestableBattOrAgent(BattOrAgent::Listener* listener)
82 : BattOrAgent("/dev/test", listener, nullptr, nullptr) { 82 : BattOrAgent("/dev/test", listener, nullptr, nullptr) {
83 connection_ = scoped_ptr<BattOrConnection>(new MockBattOrConnection(this)); 83 connection_ =
84 std::unique_ptr<BattOrConnection>(new MockBattOrConnection(this));
84 } 85 }
85 86
86 MockBattOrConnection* GetConnection() { 87 MockBattOrConnection* GetConnection() {
87 return static_cast<MockBattOrConnection*>(connection_.get()); 88 return static_cast<MockBattOrConnection*>(connection_.get());
88 } 89 }
89 90
90 void OnActionTimeout() override {} 91 void OnActionTimeout() override {}
91 }; 92 };
92 93
93 // BattOrAgentTest provides a BattOrAgent and captures the results of its 94 // BattOrAgentTest provides a BattOrAgent and captures the results of its
(...skipping 21 matching lines...) Expand all
115 command_error_ = error; 116 command_error_ = error;
116 } 117 }
117 118
118 void OnBytesSent(bool success) { 119 void OnBytesSent(bool success) {
119 agent_->OnBytesSent(success); 120 agent_->OnBytesSent(success);
120 task_runner_->RunUntilIdle(); 121 task_runner_->RunUntilIdle();
121 } 122 }
122 123
123 void OnMessageRead(bool success, 124 void OnMessageRead(bool success,
124 BattOrMessageType type, 125 BattOrMessageType type,
125 scoped_ptr<std::vector<char>> bytes) { 126 std::unique_ptr<std::vector<char>> bytes) {
126 agent_->OnMessageRead(success, type, std::move(bytes)); 127 agent_->OnMessageRead(success, type, std::move(bytes));
127 task_runner_->RunUntilIdle(); 128 task_runner_->RunUntilIdle();
128 } 129 }
129 130
130 protected: 131 protected:
131 void SetUp() override { 132 void SetUp() override {
132 agent_.reset(new TestableBattOrAgent(this)); 133 agent_.reset(new TestableBattOrAgent(this));
133 task_runner_->ClearPendingTasks(); 134 task_runner_->ClearPendingTasks();
134 is_command_complete_ = false; 135 is_command_complete_ = false;
135 command_error_ = BATTOR_ERROR_NONE; 136 command_error_ = BATTOR_ERROR_NONE;
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after
284 285
285 bool IsCommandComplete() { return is_command_complete_; } 286 bool IsCommandComplete() { return is_command_complete_; }
286 BattOrError GetCommandError() { return command_error_; } 287 BattOrError GetCommandError() { return command_error_; }
287 std::string GetTrace() { return trace_; } 288 std::string GetTrace() { return trace_; }
288 289
289 private: 290 private:
290 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; 291 scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
291 // Needed to support ThreadTaskRunnerHandle::Get() in code under test. 292 // Needed to support ThreadTaskRunnerHandle::Get() in code under test.
292 base::ThreadTaskRunnerHandle thread_task_runner_handle_; 293 base::ThreadTaskRunnerHandle thread_task_runner_handle_;
293 294
294 scoped_ptr<TestableBattOrAgent> agent_; 295 std::unique_ptr<TestableBattOrAgent> agent_;
295 bool is_command_complete_; 296 bool is_command_complete_;
296 BattOrError command_error_; 297 BattOrError command_error_;
297 std::string trace_; 298 std::string trace_;
298 }; 299 };
299 300
300 TEST_F(BattOrAgentTest, StartTracing) { 301 TEST_F(BattOrAgentTest, StartTracing) {
301 testing::InSequence s; 302 testing::InSequence s;
302 EXPECT_CALL(*GetAgent()->GetConnection(), Open()); 303 EXPECT_CALL(*GetAgent()->GetConnection(), Open());
303 304
304 BattOrControlMessage reset_msg{BATTOR_CONTROL_MESSAGE_TYPE_RESET, 0, 0}; 305 BattOrControlMessage reset_msg{BATTOR_CONTROL_MESSAGE_TYPE_RESET, 0, 0};
(...skipping 397 matching lines...) Expand 10 before | Expand all | Expand 10 after
702 703
703 TEST_F(BattOrAgentTest, StopTracingFailsIfCalibrationFrameMissingByte) { 704 TEST_F(BattOrAgentTest, StopTracingFailsIfCalibrationFrameMissingByte) {
704 RunStopTracingTo(BattOrAgentState::SAMPLES_REQUEST_SENT); 705 RunStopTracingTo(BattOrAgentState::SAMPLES_REQUEST_SENT);
705 706
706 BattOrFrameHeader cal_frame_header{0, 2 * sizeof(RawBattOrSample)}; 707 BattOrFrameHeader cal_frame_header{0, 2 * sizeof(RawBattOrSample)};
707 RawBattOrSample cal_frame[] = { 708 RawBattOrSample cal_frame[] = {
708 RawBattOrSample{1, 1}, RawBattOrSample{2, 2}, 709 RawBattOrSample{1, 1}, RawBattOrSample{2, 2},
709 }; 710 };
710 711
711 // Remove the last byte from the frame to make it invalid. 712 // Remove the last byte from the frame to make it invalid.
712 scoped_ptr<vector<char>> cal_frame_bytes = 713 std::unique_ptr<vector<char>> cal_frame_bytes =
713 CreateFrame(cal_frame_header, cal_frame, 2); 714 CreateFrame(cal_frame_header, cal_frame, 2);
714 cal_frame_bytes->pop_back(); 715 cal_frame_bytes->pop_back();
715 716
716 OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, std::move(cal_frame_bytes)); 717 OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, std::move(cal_frame_bytes));
717 718
718 EXPECT_TRUE(IsCommandComplete()); 719 EXPECT_TRUE(IsCommandComplete());
719 EXPECT_EQ(BATTOR_ERROR_UNEXPECTED_MESSAGE, GetCommandError()); 720 EXPECT_EQ(BATTOR_ERROR_UNEXPECTED_MESSAGE, GetCommandError());
720 } 721 }
721 722
722 TEST_F(BattOrAgentTest, StopTracingFailsIfDataFrameMissingByte) { 723 TEST_F(BattOrAgentTest, StopTracingFailsIfDataFrameMissingByte) {
723 RunStopTracingTo(BattOrAgentState::SAMPLES_REQUEST_SENT); 724 RunStopTracingTo(BattOrAgentState::SAMPLES_REQUEST_SENT);
724 725
725 BattOrFrameHeader cal_frame_header{0, 1 * sizeof(RawBattOrSample)}; 726 BattOrFrameHeader cal_frame_header{0, 1 * sizeof(RawBattOrSample)};
726 RawBattOrSample cal_frame[] = { 727 RawBattOrSample cal_frame[] = {
727 RawBattOrSample{1, 1}, 728 RawBattOrSample{1, 1},
728 }; 729 };
729 OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, 730 OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES,
730 CreateFrame(cal_frame_header, cal_frame, 1)); 731 CreateFrame(cal_frame_header, cal_frame, 1));
731 732
732 BattOrFrameHeader frame_header{1, 1 * sizeof(RawBattOrSample)}; 733 BattOrFrameHeader frame_header{1, 1 * sizeof(RawBattOrSample)};
733 RawBattOrSample frame[] = {RawBattOrSample{1, 1}}; 734 RawBattOrSample frame[] = {RawBattOrSample{1, 1}};
734 735
735 // Remove the last byte from the frame to make it invalid. 736 // Remove the last byte from the frame to make it invalid.
736 scoped_ptr<vector<char>> frame_bytes = CreateFrame(frame_header, frame, 2); 737 std::unique_ptr<vector<char>> frame_bytes =
738 CreateFrame(frame_header, frame, 2);
737 frame_bytes->pop_back(); 739 frame_bytes->pop_back();
738 740
739 OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, std::move(frame_bytes)); 741 OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, std::move(frame_bytes));
740 742
741 EXPECT_TRUE(IsCommandComplete()); 743 EXPECT_TRUE(IsCommandComplete());
742 EXPECT_EQ(BATTOR_ERROR_UNEXPECTED_MESSAGE, GetCommandError()); 744 EXPECT_EQ(BATTOR_ERROR_UNEXPECTED_MESSAGE, GetCommandError());
743 } 745 }
744 746
745 TEST_F(BattOrAgentTest, StopTracingFailsIfFrameArrivesOutOfOrder) { 747 TEST_F(BattOrAgentTest, StopTracingFailsIfFrameArrivesOutOfOrder) {
746 RunStopTracingTo(BattOrAgentState::CALIBRATION_FRAME_RECEIVED); 748 RunStopTracingTo(BattOrAgentState::CALIBRATION_FRAME_RECEIVED);
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
865 867
866 uint32_t current_sample = 1; 868 uint32_t current_sample = 1;
867 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL, 869 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL,
868 ToCharVector(current_sample)); 870 ToCharVector(current_sample));
869 871
870 EXPECT_TRUE(IsCommandComplete()); 872 EXPECT_TRUE(IsCommandComplete());
871 EXPECT_EQ(BATTOR_ERROR_UNEXPECTED_MESSAGE, GetCommandError()); 873 EXPECT_EQ(BATTOR_ERROR_UNEXPECTED_MESSAGE, GetCommandError());
872 } 874 }
873 875
874 } // namespace battor 876 } // namespace battor
OLDNEW
« no previous file with comments | « tools/battor_agent/battor_agent_bin.cc ('k') | tools/battor_agent/battor_connection.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698