1 // Copyright (c) 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
9 #include "base/callback.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/run_loop.h"
12 #include "device/hid/hid_connection.h"
13 #include "device/hid/hid_service.h"
14 #include "device/test/usb_test_gadget.h"
15 #include "net/base/io_buffer.h"
16 #include "testing/gtest/include/gtest/gtest.h"
22 using net::IOBufferWithSize
;
24 class TestCompletionCallback
{
26 TestCompletionCallback()
27 : read_callback_(base::Bind(&TestCompletionCallback::SetReadResult
,
28 base::Unretained(this))),
29 write_callback_(base::Bind(&TestCompletionCallback::SetWriteResult
,
30 base::Unretained(this))) {}
31 ~TestCompletionCallback() {}
33 void SetReadResult(bool success
,
34 scoped_refptr
<net::IOBuffer
> buffer
,
42 void SetWriteResult(bool success
) {
47 bool WaitForResult() {
52 const HidConnection::ReadCallback
& read_callback() { return read_callback_
; }
53 const HidConnection::WriteCallback
write_callback() {
54 return write_callback_
;
56 scoped_refptr
<net::IOBuffer
> buffer() const { return buffer_
; }
57 size_t size() const { return size_
; }
60 base::RunLoop run_loop_
;
63 scoped_refptr
<net::IOBuffer
> buffer_
;
64 HidConnection::ReadCallback read_callback_
;
65 HidConnection::WriteCallback write_callback_
;
70 class HidConnectionTest
: public testing::Test
{
72 virtual void SetUp() OVERRIDE
{
73 if (!UsbTestGadget::IsTestEnabled()) return;
75 message_loop_
.reset(new base::MessageLoopForIO());
76 service_
= HidService::GetInstance(message_loop_
->message_loop_proxy());
77 ASSERT_TRUE(service_
);
79 test_gadget_
= UsbTestGadget::Claim();
80 ASSERT_TRUE(test_gadget_
);
81 ASSERT_TRUE(test_gadget_
->SetType(UsbTestGadget::HID_ECHO
));
83 device_id_
= kInvalidHidDeviceId
;
85 base::RunLoop run_loop
;
86 message_loop_
->PostDelayedTask(
88 base::Bind(&HidConnectionTest::FindDevice
,
89 base::Unretained(this), run_loop
.QuitClosure(), 5),
90 base::TimeDelta::FromMilliseconds(250));
93 ASSERT_NE(device_id_
, kInvalidHidDeviceId
);
96 void FindDevice(const base::Closure
& done
, int retries
) {
97 std::vector
<HidDeviceInfo
> devices
;
98 service_
->GetDevices(&devices
);
100 for (std::vector
<HidDeviceInfo
>::iterator it
= devices
.begin();
103 if (it
->serial_number
== test_gadget_
->GetSerial()) {
104 device_id_
= it
->device_id
;
109 if (device_id_
== kInvalidHidDeviceId
&& --retries
> 0) {
110 message_loop_
->PostDelayedTask(
112 base::Bind(&HidConnectionTest::FindDevice
, base::Unretained(this),
114 base::TimeDelta::FromMilliseconds(10));
116 message_loop_
->PostTask(FROM_HERE
, done
);
120 scoped_ptr
<base::MessageLoopForIO
> message_loop_
;
121 HidService
* service_
;
122 scoped_ptr
<UsbTestGadget
> test_gadget_
;
123 HidDeviceId device_id_
;
126 TEST_F(HidConnectionTest
, ReadWrite
) {
127 if (!UsbTestGadget::IsTestEnabled()) return;
129 scoped_refptr
<HidConnection
> conn
= service_
->Connect(device_id_
);
130 ASSERT_TRUE(conn
.get());
132 for (int i
= 0; i
< 8; ++i
) {
133 scoped_refptr
<IOBufferWithSize
> buffer(new IOBufferWithSize(9));
134 buffer
->data()[0] = 0;
135 for (int j
= 1; j
< buffer
->size(); ++j
) {
136 buffer
->data()[j
] = i
+ j
- 1;
139 TestCompletionCallback write_callback
;
140 conn
->Write(buffer
, buffer
->size(), write_callback
.write_callback());
141 ASSERT_TRUE(write_callback
.WaitForResult());
143 TestCompletionCallback read_callback
;
144 conn
->Read(read_callback
.read_callback());
145 ASSERT_TRUE(read_callback
.WaitForResult());
146 ASSERT_EQ(9UL, read_callback
.size());
147 ASSERT_EQ(0, read_callback
.buffer()->data()[0]);
148 for (int j
= 1; j
< buffer
->size(); ++j
) {
149 ASSERT_EQ(i
+ j
- 1, read_callback
.buffer()->data()[j
]);
154 } // namespace device