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_connection_impl.h" | 5 #include "tools/battor_agent/battor_connection_impl.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/memory/ptr_util.h" |
9 #include "base/memory/weak_ptr.h" | 10 #include "base/memory/weak_ptr.h" |
10 #include "base/test/test_simple_task_runner.h" | 11 #include "base/test/test_simple_task_runner.h" |
11 #include "base/thread_task_runner_handle.h" | 12 #include "base/thread_task_runner_handle.h" |
12 #include "device/serial/serial.mojom.h" | 13 #include "device/serial/serial.mojom.h" |
13 #include "device/serial/test_serial_io_handler.h" | 14 #include "device/serial/test_serial_io_handler.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
15 #include "tools/battor_agent/battor_protocol_types.h" | 16 #include "tools/battor_agent/battor_protocol_types.h" |
16 | 17 |
17 namespace { | 18 namespace { |
18 | 19 |
(...skipping 22 matching lines...) Expand all Loading... |
41 public BattOrConnection::Listener { | 42 public BattOrConnection::Listener { |
42 public: | 43 public: |
43 BattOrConnectionImplTest() | 44 BattOrConnectionImplTest() |
44 : task_runner_(new base::TestSimpleTaskRunner()), | 45 : task_runner_(new base::TestSimpleTaskRunner()), |
45 thread_task_runner_handle_(task_runner_) {} | 46 thread_task_runner_handle_(task_runner_) {} |
46 | 47 |
47 void OnConnectionOpened(bool success) override { open_success_ = success; }; | 48 void OnConnectionOpened(bool success) override { open_success_ = success; }; |
48 void OnBytesSent(bool success) override { send_success_ = success; } | 49 void OnBytesSent(bool success) override { send_success_ = success; } |
49 void OnMessageRead(bool success, | 50 void OnMessageRead(bool success, |
50 BattOrMessageType type, | 51 BattOrMessageType type, |
51 scoped_ptr<std::vector<char>> bytes) override { | 52 std::unique_ptr<std::vector<char>> bytes) override { |
52 is_read_complete_ = true; | 53 is_read_complete_ = true; |
53 read_success_ = success; | 54 read_success_ = success; |
54 read_type_ = type; | 55 read_type_ = type; |
55 read_bytes_ = std::move(bytes); | 56 read_bytes_ = std::move(bytes); |
56 } | 57 } |
57 | 58 |
58 protected: | 59 protected: |
59 void SetUp() override { | 60 void SetUp() override { |
60 connection_.reset(new TestableBattOrConnection(this)); | 61 connection_.reset(new TestableBattOrConnection(this)); |
61 task_runner_->ClearPendingTasks(); | 62 task_runner_->ClearPendingTasks(); |
62 } | 63 } |
63 | 64 |
64 void OpenConnection() { | 65 void OpenConnection() { |
65 connection_->Open(); | 66 connection_->Open(); |
66 task_runner_->RunUntilIdle(); | 67 task_runner_->RunUntilIdle(); |
67 } | 68 } |
68 | 69 |
69 void ReadMessage(BattOrMessageType type) { | 70 void ReadMessage(BattOrMessageType type) { |
70 is_read_complete_ = false; | 71 is_read_complete_ = false; |
71 connection_->ReadMessage(type); | 72 connection_->ReadMessage(type); |
72 task_runner_->RunUntilIdle(); | 73 task_runner_->RunUntilIdle(); |
73 } | 74 } |
74 | 75 |
75 // Reads the specified number of bytes directly from the serial connection. | 76 // Reads the specified number of bytes directly from the serial connection. |
76 scoped_refptr<net::IOBuffer> ReadMessageRaw(int bytes_to_read) { | 77 scoped_refptr<net::IOBuffer> ReadMessageRaw(int bytes_to_read) { |
77 scoped_refptr<net::IOBuffer> buffer( | 78 scoped_refptr<net::IOBuffer> buffer( |
78 new net::IOBuffer((size_t)bytes_to_read)); | 79 new net::IOBuffer((size_t)bytes_to_read)); |
79 | 80 |
80 connection_->GetIoHandler()->Read(make_scoped_ptr(new device::ReceiveBuffer( | 81 connection_->GetIoHandler()->Read( |
81 buffer, bytes_to_read, base::Bind(&NullReadCallback)))); | 82 base::WrapUnique(new device::ReceiveBuffer( |
| 83 buffer, bytes_to_read, base::Bind(&NullReadCallback)))); |
82 task_runner_->RunUntilIdle(); | 84 task_runner_->RunUntilIdle(); |
83 | 85 |
84 return buffer; | 86 return buffer; |
85 } | 87 } |
86 | 88 |
87 void SendControlMessage(BattOrControlMessageType type, | 89 void SendControlMessage(BattOrControlMessageType type, |
88 uint16_t param1, | 90 uint16_t param1, |
89 uint16_t param2) { | 91 uint16_t param2) { |
90 BattOrControlMessage msg{type, param1, param2}; | 92 BattOrControlMessage msg{type, param1, param2}; |
91 connection_->SendBytes(BATTOR_MESSAGE_TYPE_CONTROL, | 93 connection_->SendBytes(BATTOR_MESSAGE_TYPE_CONTROL, |
92 reinterpret_cast<char*>(&msg), sizeof(msg)); | 94 reinterpret_cast<char*>(&msg), sizeof(msg)); |
93 task_runner_->RunUntilIdle(); | 95 task_runner_->RunUntilIdle(); |
94 } | 96 } |
95 | 97 |
96 // Writes the specified bytes directly to the serial connection. | 98 // Writes the specified bytes directly to the serial connection. |
97 void SendBytesRaw(const char* data, uint16_t bytes_to_send) { | 99 void SendBytesRaw(const char* data, uint16_t bytes_to_send) { |
98 std::vector<char> data_vector(data, data + bytes_to_send); | 100 std::vector<char> data_vector(data, data + bytes_to_send); |
99 connection_->GetIoHandler()->Write(make_scoped_ptr( | 101 connection_->GetIoHandler()->Write(base::WrapUnique( |
100 new device::SendBuffer(data_vector, base::Bind(&NullWriteCallback)))); | 102 new device::SendBuffer(data_vector, base::Bind(&NullWriteCallback)))); |
101 task_runner_->RunUntilIdle(); | 103 task_runner_->RunUntilIdle(); |
102 } | 104 } |
103 | 105 |
104 bool GetOpenSuccess() { return open_success_; } | 106 bool GetOpenSuccess() { return open_success_; } |
105 bool GetSendSuccess() { return send_success_; } | 107 bool GetSendSuccess() { return send_success_; } |
106 bool IsReadComplete() { return is_read_complete_; } | 108 bool IsReadComplete() { return is_read_complete_; } |
107 bool GetReadSuccess() { return read_success_; } | 109 bool GetReadSuccess() { return read_success_; } |
108 BattOrMessageType GetReadType() { return read_type_; } | 110 BattOrMessageType GetReadType() { return read_type_; } |
109 std::vector<char>* GetReadMessage() { return read_bytes_.get(); } | 111 std::vector<char>* GetReadMessage() { return read_bytes_.get(); } |
110 | 112 |
111 private: | 113 private: |
112 scoped_ptr<TestableBattOrConnection> connection_; | 114 std::unique_ptr<TestableBattOrConnection> connection_; |
113 | 115 |
114 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | 116 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
115 base::ThreadTaskRunnerHandle thread_task_runner_handle_; | 117 base::ThreadTaskRunnerHandle thread_task_runner_handle_; |
116 | 118 |
117 // Result from the last connect command. | 119 // Result from the last connect command. |
118 bool open_success_; | 120 bool open_success_; |
119 // Result from the last send command. | 121 // Result from the last send command. |
120 bool send_success_; | 122 bool send_success_; |
121 // Results from the last read command. | 123 // Results from the last read command. |
122 bool is_read_complete_; | 124 bool is_read_complete_; |
123 bool read_success_; | 125 bool read_success_; |
124 BattOrMessageType read_type_; | 126 BattOrMessageType read_type_; |
125 scoped_ptr<std::vector<char>> read_bytes_; | 127 std::unique_ptr<std::vector<char>> read_bytes_; |
126 }; | 128 }; |
127 | 129 |
128 TEST_F(BattOrConnectionImplTest, InitSendsCorrectBytes) { | 130 TEST_F(BattOrConnectionImplTest, InitSendsCorrectBytes) { |
129 OpenConnection(); | 131 OpenConnection(); |
130 ASSERT_TRUE(GetOpenSuccess()); | 132 ASSERT_TRUE(GetOpenSuccess()); |
131 | 133 |
132 SendControlMessage(BATTOR_CONTROL_MESSAGE_TYPE_INIT, 0, 0); | 134 SendControlMessage(BATTOR_CONTROL_MESSAGE_TYPE_INIT, 0, 0); |
133 | 135 |
134 const char expected_data[] = { | 136 const char expected_data[] = { |
135 BATTOR_CONTROL_BYTE_START, BATTOR_MESSAGE_TYPE_CONTROL, | 137 BATTOR_CONTROL_BYTE_START, BATTOR_MESSAGE_TYPE_CONTROL, |
(...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
387 BATTOR_CONTROL_BYTE_END, | 389 BATTOR_CONTROL_BYTE_END, |
388 }; | 390 }; |
389 SendBytesRaw(data, 3); | 391 SendBytesRaw(data, 3); |
390 ReadMessage(BATTOR_MESSAGE_TYPE_PRINT); | 392 ReadMessage(BATTOR_MESSAGE_TYPE_PRINT); |
391 | 393 |
392 ASSERT_TRUE(IsReadComplete()); | 394 ASSERT_TRUE(IsReadComplete()); |
393 ASSERT_FALSE(GetReadSuccess()); | 395 ASSERT_FALSE(GetReadSuccess()); |
394 } | 396 } |
395 | 397 |
396 } // namespace battor | 398 } // namespace battor |
OLD | NEW |