| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/message_loop/message_loop.h" | 6 #include "base/message_loop/message_loop.h" |
| 7 #include "base/run_loop.h" | 7 #include "base/run_loop.h" |
| 8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "device/test/usb_test_gadget.h" | 9 #include "device/test/usb_test_gadget.h" |
| 10 #include "device/usb/usb_device.h" | 10 #include "device/usb/usb_device.h" |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 74 base::RunLoop run_loop_; | 74 base::RunLoop run_loop_; |
| 75 UsbTransferStatus status_; | 75 UsbTransferStatus status_; |
| 76 size_t transferred_; | 76 size_t transferred_; |
| 77 }; | 77 }; |
| 78 | 78 |
| 79 TEST_F(UsbDeviceHandleTest, InterruptTransfer) { | 79 TEST_F(UsbDeviceHandleTest, InterruptTransfer) { |
| 80 if (!handle_.get()) { | 80 if (!handle_.get()) { |
| 81 return; | 81 return; |
| 82 } | 82 } |
| 83 | 83 |
| 84 ASSERT_TRUE(handle_->ClaimInterface(0)); |
| 85 |
| 84 scoped_refptr<net::IOBufferWithSize> in_buffer(new net::IOBufferWithSize(64)); | 86 scoped_refptr<net::IOBufferWithSize> in_buffer(new net::IOBufferWithSize(64)); |
| 85 TestCompletionCallback in_completion; | 87 TestCompletionCallback in_completion; |
| 86 handle_->InterruptTransfer(USB_DIRECTION_INBOUND, | 88 handle_->InterruptTransfer(USB_DIRECTION_INBOUND, |
| 87 0x81, | 89 0x81, |
| 88 in_buffer.get(), | 90 in_buffer.get(), |
| 89 in_buffer->size(), | 91 in_buffer->size(), |
| 90 5000, // 5 second timeout | 92 5000, // 5 second timeout |
| 91 in_completion.callback()); | 93 in_completion.callback()); |
| 92 | 94 |
| 93 scoped_refptr<net::IOBufferWithSize> out_buffer( | 95 scoped_refptr<net::IOBufferWithSize> out_buffer( |
| 94 new net::IOBufferWithSize(in_buffer->size())); | 96 new net::IOBufferWithSize(in_buffer->size())); |
| 95 TestCompletionCallback out_completion; | 97 TestCompletionCallback out_completion; |
| 96 for (int i = 0; i < out_buffer->size(); ++i) { | 98 for (int i = 0; i < out_buffer->size(); ++i) { |
| 97 out_buffer->data()[i] = i; | 99 out_buffer->data()[i] = i; |
| 98 } | 100 } |
| 99 | 101 |
| 100 handle_->InterruptTransfer(USB_DIRECTION_OUTBOUND, | 102 handle_->InterruptTransfer(USB_DIRECTION_OUTBOUND, |
| 101 0x01, | 103 0x01, |
| 102 out_buffer.get(), | 104 out_buffer.get(), |
| 103 out_buffer->size(), | 105 out_buffer->size(), |
| 104 5000, // 5 second timeout | 106 5000, // 5 second timeout |
| 105 out_completion.callback()); | 107 out_completion.callback()); |
| 106 out_completion.WaitForResult(); | 108 out_completion.WaitForResult(); |
| 107 ASSERT_EQ(USB_TRANSFER_COMPLETED, out_completion.status()); | 109 ASSERT_EQ(USB_TRANSFER_COMPLETED, out_completion.status()); |
| 108 ASSERT_EQ(static_cast<size_t>(out_buffer->size()), | 110 EXPECT_EQ(static_cast<size_t>(out_buffer->size()), |
| 109 out_completion.transferred()); | 111 out_completion.transferred()); |
| 110 | 112 |
| 111 in_completion.WaitForResult(); | 113 in_completion.WaitForResult(); |
| 112 ASSERT_EQ(USB_TRANSFER_COMPLETED, in_completion.status()); | 114 ASSERT_EQ(USB_TRANSFER_COMPLETED, in_completion.status()); |
| 113 ASSERT_EQ(static_cast<size_t>(in_buffer->size()), | 115 EXPECT_EQ(static_cast<size_t>(in_buffer->size()), |
| 114 in_completion.transferred()); | 116 in_completion.transferred()); |
| 115 for (int i = 0; i < in_buffer->size(); ++i) { | 117 for (size_t i = 0; i < in_completion.transferred(); ++i) { |
| 116 ASSERT_EQ(out_buffer->data()[i], in_buffer->data()[i]); | 118 EXPECT_EQ(out_buffer->data()[i], in_buffer->data()[i]); |
| 117 } | 119 } |
| 118 } | 120 } |
| 119 | 121 |
| 120 TEST_F(UsbDeviceHandleTest, BulkTransfer) { | 122 TEST_F(UsbDeviceHandleTest, BulkTransfer) { |
| 121 if (!handle_.get()) { | 123 if (!handle_.get()) { |
| 122 return; | 124 return; |
| 123 } | 125 } |
| 124 | 126 |
| 127 ASSERT_TRUE(handle_->ClaimInterface(1)); |
| 128 |
| 125 scoped_refptr<net::IOBufferWithSize> in_buffer( | 129 scoped_refptr<net::IOBufferWithSize> in_buffer( |
| 126 new net::IOBufferWithSize(512)); | 130 new net::IOBufferWithSize(512)); |
| 127 TestCompletionCallback in_completion; | 131 TestCompletionCallback in_completion; |
| 128 handle_->BulkTransfer(USB_DIRECTION_INBOUND, | 132 handle_->BulkTransfer(USB_DIRECTION_INBOUND, 0x82, in_buffer.get(), |
| 129 0x81, | |
| 130 in_buffer.get(), | |
| 131 in_buffer->size(), | 133 in_buffer->size(), |
| 132 5000, // 5 second timeout | 134 5000, // 5 second timeout |
| 133 in_completion.callback()); | 135 in_completion.callback()); |
| 134 | 136 |
| 135 scoped_refptr<net::IOBufferWithSize> out_buffer( | 137 scoped_refptr<net::IOBufferWithSize> out_buffer( |
| 136 new net::IOBufferWithSize(in_buffer->size())); | 138 new net::IOBufferWithSize(in_buffer->size())); |
| 137 TestCompletionCallback out_completion; | 139 TestCompletionCallback out_completion; |
| 138 for (int i = 0; i < out_buffer->size(); ++i) { | 140 for (int i = 0; i < out_buffer->size(); ++i) { |
| 139 out_buffer->data()[i] = i; | 141 out_buffer->data()[i] = i; |
| 140 } | 142 } |
| 141 | 143 |
| 142 handle_->BulkTransfer(USB_DIRECTION_OUTBOUND, | 144 handle_->BulkTransfer(USB_DIRECTION_OUTBOUND, 0x02, out_buffer.get(), |
| 143 0x01, | |
| 144 out_buffer.get(), | |
| 145 out_buffer->size(), | 145 out_buffer->size(), |
| 146 5000, // 5 second timeout | 146 5000, // 5 second timeout |
| 147 out_completion.callback()); | 147 out_completion.callback()); |
| 148 out_completion.WaitForResult(); | 148 out_completion.WaitForResult(); |
| 149 ASSERT_EQ(USB_TRANSFER_COMPLETED, out_completion.status()); | 149 ASSERT_EQ(USB_TRANSFER_COMPLETED, out_completion.status()); |
| 150 ASSERT_EQ(static_cast<size_t>(out_buffer->size()), | 150 EXPECT_EQ(static_cast<size_t>(out_buffer->size()), |
| 151 out_completion.transferred()); | 151 out_completion.transferred()); |
| 152 | 152 |
| 153 in_completion.WaitForResult(); | 153 in_completion.WaitForResult(); |
| 154 ASSERT_EQ(USB_TRANSFER_COMPLETED, in_completion.status()); | 154 ASSERT_EQ(USB_TRANSFER_COMPLETED, in_completion.status()); |
| 155 ASSERT_EQ(static_cast<size_t>(in_buffer->size()), | 155 EXPECT_EQ(static_cast<size_t>(in_buffer->size()), |
| 156 in_completion.transferred()); | 156 in_completion.transferred()); |
| 157 for (int i = 0; i < in_buffer->size(); ++i) { | 157 for (size_t i = 0; i < in_completion.transferred(); ++i) { |
| 158 ASSERT_EQ(out_buffer->data()[i], in_buffer->data()[i]); | 158 EXPECT_EQ(out_buffer->data()[i], in_buffer->data()[i]); |
| 159 } | 159 } |
| 160 } | 160 } |
| 161 | 161 |
| 162 } // namespace | 162 } // namespace |
| 163 | 163 |
| 164 } // namespace device | 164 } // namespace device |
| OLD | NEW |