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 : callback_(base::Bind(&TestCompletionCallback::SetResult,
28 base::Unretained(this))) {}
30 void SetResult(bool success, size_t size) {
36 bool WaitForResult() {
41 const HidConnection::IOCallback& callback() const { return callback_; }
42 size_t transferred() const { return transferred_; }
45 const HidConnection::IOCallback callback_;
46 base::RunLoop run_loop_;
53 class HidConnectionTest : public testing::Test {
55 virtual void SetUp() OVERRIDE {
56 if (!UsbTestGadget::IsTestEnabled()) return;
58 message_loop_.reset(new base::MessageLoopForIO());
59 service_.reset(HidService::Create(message_loop_->message_loop_proxy()));
60 ASSERT_TRUE(service_);
62 test_gadget_ = UsbTestGadget::Claim();
63 ASSERT_TRUE(test_gadget_);
64 ASSERT_TRUE(test_gadget_->SetType(UsbTestGadget::HID_ECHO));
66 device_id_ = kInvalidHidDeviceId;
68 base::RunLoop run_loop;
69 message_loop_->PostDelayedTask(
71 base::Bind(&HidConnectionTest::FindDevice,
72 base::Unretained(this), run_loop.QuitClosure(), 5),
73 base::TimeDelta::FromMilliseconds(250));
76 ASSERT_NE(device_id_, kInvalidHidDeviceId);
79 void FindDevice(const base::Closure& done, int retries) {
80 std::vector<HidDeviceInfo> devices;
81 service_->GetDevices(&devices);
83 for (std::vector<HidDeviceInfo>::iterator it = devices.begin();
86 if (it->serial_number == test_gadget_->GetSerial()) {
87 device_id_ = it->device_id;
92 if (device_id_ == kInvalidHidDeviceId && --retries > 0) {
93 message_loop_->PostDelayedTask(
95 base::Bind(&HidConnectionTest::FindDevice, base::Unretained(this),
97 base::TimeDelta::FromMilliseconds(10));
99 message_loop_->PostTask(FROM_HERE, done);
103 scoped_ptr<base::MessageLoopForIO> message_loop_;
104 scoped_ptr<HidService> service_;
105 scoped_ptr<UsbTestGadget> test_gadget_;
106 HidDeviceId device_id_;
109 TEST_F(HidConnectionTest, ReadWrite) {
110 if (!UsbTestGadget::IsTestEnabled()) return;
112 scoped_refptr<HidConnection> conn = service_->Connect(device_id_);
115 for (int i = 0; i < 8; ++i) {
116 scoped_refptr<IOBufferWithSize> write_buffer(new IOBufferWithSize(8));
117 *(int64_t*)write_buffer->data() = i;
119 TestCompletionCallback write_callback;
120 conn->Write(0, write_buffer, write_callback.callback());
121 ASSERT_TRUE(write_callback.WaitForResult());
122 ASSERT_EQ(8UL, write_callback.transferred());
124 scoped_refptr<IOBufferWithSize> read_buffer(new IOBufferWithSize(8));
125 TestCompletionCallback read_callback;
126 conn->Read(read_buffer, read_callback.callback());
127 ASSERT_TRUE(read_callback.WaitForResult());
128 ASSERT_EQ(8UL, read_callback.transferred());
129 ASSERT_EQ(i, *(int64_t*)read_buffer->data());
133 } // namespace device