summaryrefslogtreecommitdiffstats
path: root/device/hid/hid_connection_unittest.cc
blob: b0361982a15391643f09eac22ac9af95a7a8b221 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Copyright (c) 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <string>
#include <vector>

#include "base/bind.h"
#include "base/callback.h"
#include "base/memory/scoped_ptr.h"
#include "base/run_loop.h"
#include "base/scoped_observer.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/test_io_thread.h"
#include "device/hid/hid_connection.h"
#include "device/hid/hid_service.h"
#include "device/test/usb_test_gadget.h"
#include "device/usb/usb_device.h"
#include "net/base/io_buffer.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace device {

namespace {

using net::IOBufferWithSize;

// Helper class that can be used to block until a HID device with a particular
// serial number is available. Example usage:
//
//   DeviceCatcher device_catcher("ABC123");
//   HidDeviceId device_id = device_catcher.WaitForDevice();
//   /* Call HidService::Connect(device_id) to open the device. */
//
class DeviceCatcher : HidService::Observer {
 public:
  DeviceCatcher(HidService* hid_service, const base::string16& serial_number)
      : serial_number_(base::UTF16ToUTF8(serial_number)), observer_(this) {
    observer_.Add(hid_service);
    hid_service->GetDevices(base::Bind(&DeviceCatcher::OnEnumerationComplete,
                                       base::Unretained(this)));
  }

  const HidDeviceId& WaitForDevice() {
    run_loop_.Run();
    observer_.RemoveAll();
    return device_id_;
  }

 private:
  void OnEnumerationComplete(
      const std::vector<scoped_refptr<HidDeviceInfo>>& devices) {
    for (const scoped_refptr<HidDeviceInfo>& device_info : devices) {
      if (device_info->serial_number() == serial_number_) {
        device_id_ = device_info->device_id();
        run_loop_.Quit();
        break;
      }
    }
  }

  void OnDeviceAdded(scoped_refptr<HidDeviceInfo> device_info) override {
    if (device_info->serial_number() == serial_number_) {
      device_id_ = device_info->device_id();
      run_loop_.Quit();
    }
  }

  std::string serial_number_;
  ScopedObserver<device::HidService, device::HidService::Observer> observer_;
  base::RunLoop run_loop_;
  HidDeviceId device_id_;
};

class TestConnectCallback {
 public:
  TestConnectCallback()
      : callback_(base::Bind(&TestConnectCallback::SetConnection,
                             base::Unretained(this))) {}
  ~TestConnectCallback() {}

  void SetConnection(scoped_refptr<HidConnection> connection) {
    connection_ = connection;
    run_loop_.Quit();
  }

  scoped_refptr<HidConnection> WaitForConnection() {
    run_loop_.Run();
    return connection_;
  }

  const HidService::ConnectCallback& callback() { return callback_; }

 private:
  HidService::ConnectCallback callback_;
  base::RunLoop run_loop_;
  scoped_refptr<HidConnection> connection_;
};

class TestIoCallback {
 public:
  TestIoCallback()
      : read_callback_(
            base::Bind(&TestIoCallback::SetReadResult, base::Unretained(this))),
        write_callback_(base::Bind(&TestIoCallback::SetWriteResult,
                                   base::Unretained(this))) {}
  ~TestIoCallback() {}

  void SetReadResult(bool success,
                     scoped_refptr<net::IOBuffer> buffer,
                     size_t size) {
    result_ = success;
    buffer_ = buffer;
    size_ = size;
    run_loop_.Quit();
  }

  void SetWriteResult(bool success) {
    result_ = success;
    run_loop_.Quit();
  }

  bool WaitForResult() {
    run_loop_.Run();
    return result_;
  }

  const HidConnection::ReadCallback& read_callback() { return read_callback_; }
  const HidConnection::WriteCallback write_callback() {
    return write_callback_;
  }
  scoped_refptr<net::IOBuffer> buffer() const { return buffer_; }
  size_t size() const { return size_; }

 private:
  base::RunLoop run_loop_;
  bool result_;
  size_t size_;
  scoped_refptr<net::IOBuffer> buffer_;
  HidConnection::ReadCallback read_callback_;
  HidConnection::WriteCallback write_callback_;
};

}  // namespace

class HidConnectionTest : public testing::Test {
 protected:
  void SetUp() override {
    if (!UsbTestGadget::IsTestEnabled()) return;

    message_loop_.reset(new base::MessageLoopForUI());
    io_thread_.reset(new base::TestIOThread(base::TestIOThread::kAutoStart));

    service_ = HidService::GetInstance(io_thread_->task_runner());
    ASSERT_TRUE(service_);

    test_gadget_ = UsbTestGadget::Claim(io_thread_->task_runner());
    ASSERT_TRUE(test_gadget_);
    ASSERT_TRUE(test_gadget_->SetType(UsbTestGadget::HID_ECHO));

    DeviceCatcher device_catcher(service_,
                                 test_gadget_->GetDevice()->serial_number());
    device_id_ = device_catcher.WaitForDevice();
    ASSERT_NE(device_id_, kInvalidHidDeviceId);
  }

  scoped_ptr<base::MessageLoopForUI> message_loop_;
  scoped_ptr<base::TestIOThread> io_thread_;
  HidService* service_;
  scoped_ptr<UsbTestGadget> test_gadget_;
  HidDeviceId device_id_;
};

TEST_F(HidConnectionTest, ReadWrite) {
  if (!UsbTestGadget::IsTestEnabled()) return;

  TestConnectCallback connect_callback;
  service_->Connect(device_id_, connect_callback.callback());
  scoped_refptr<HidConnection> conn = connect_callback.WaitForConnection();
  ASSERT_TRUE(conn.get());

  const char kBufferSize = 9;
  for (char i = 0; i < 8; ++i) {
    scoped_refptr<IOBufferWithSize> buffer(new IOBufferWithSize(kBufferSize));
    buffer->data()[0] = 0;
    for (unsigned char j = 1; j < kBufferSize; ++j) {
      buffer->data()[j] = i + j - 1;
    }

    TestIoCallback write_callback;
    conn->Write(buffer, buffer->size(), write_callback.write_callback());
    ASSERT_TRUE(write_callback.WaitForResult());

    TestIoCallback read_callback;
    conn->Read(read_callback.read_callback());
    ASSERT_TRUE(read_callback.WaitForResult());
    ASSERT_EQ(9UL, read_callback.size());
    ASSERT_EQ(0, read_callback.buffer()->data()[0]);
    for (unsigned char j = 1; j < kBufferSize; ++j) {
      ASSERT_EQ(i + j - 1, read_callback.buffer()->data()[j]);
    }
  }

  conn->Close();
}

}  // namespace device