Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / device / hid / hid_connection_unittest.cc
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.
4
5 #include <string>
6 #include <vector>
7
8 #include "base/bind.h"
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"
17
18 namespace device {
19
20 namespace {
21
22 using net::IOBufferWithSize;
23
24 class TestCompletionCallback {
25  public:
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() {}
32
33   void SetReadResult(bool success,
34                      scoped_refptr<net::IOBuffer> buffer,
35                      size_t size) {
36     result_ = success;
37     buffer_ = buffer;
38     size_ = size;
39     run_loop_.Quit();
40   }
41
42   void SetWriteResult(bool success) {
43     result_ = success;
44     run_loop_.Quit();
45   }
46
47   bool WaitForResult() {
48     run_loop_.Run();
49     return result_;
50   }
51
52   const HidConnection::ReadCallback& read_callback() { return read_callback_; }
53   const HidConnection::WriteCallback write_callback() {
54     return write_callback_;
55   }
56   scoped_refptr<net::IOBuffer> buffer() const { return buffer_; }
57   size_t size() const { return size_; }
58
59  private:
60   base::RunLoop run_loop_;
61   bool result_;
62   size_t size_;
63   scoped_refptr<net::IOBuffer> buffer_;
64   HidConnection::ReadCallback read_callback_;
65   HidConnection::WriteCallback write_callback_;
66 };
67
68 }  // namespace
69
70 class HidConnectionTest : public testing::Test {
71  protected:
72   virtual void SetUp() OVERRIDE {
73     if (!UsbTestGadget::IsTestEnabled()) return;
74
75     message_loop_.reset(new base::MessageLoopForIO());
76     service_ = HidService::GetInstance(message_loop_->message_loop_proxy());
77     ASSERT_TRUE(service_);
78
79     test_gadget_ = UsbTestGadget::Claim();
80     ASSERT_TRUE(test_gadget_);
81     ASSERT_TRUE(test_gadget_->SetType(UsbTestGadget::HID_ECHO));
82
83     device_id_ = kInvalidHidDeviceId;
84
85     base::RunLoop run_loop;
86     message_loop_->PostDelayedTask(
87         FROM_HERE,
88         base::Bind(&HidConnectionTest::FindDevice,
89                    base::Unretained(this), run_loop.QuitClosure(), 5),
90         base::TimeDelta::FromMilliseconds(250));
91     run_loop.Run();
92
93     ASSERT_NE(device_id_, kInvalidHidDeviceId);
94   }
95
96   void FindDevice(const base::Closure& done, int retries) {
97     std::vector<HidDeviceInfo> devices;
98     service_->GetDevices(&devices);
99
100     for (std::vector<HidDeviceInfo>::iterator it = devices.begin();
101          it != devices.end();
102          ++it) {
103       if (it->serial_number == test_gadget_->GetSerialNumber()) {
104         device_id_ = it->device_id;
105         break;
106       }
107     }
108
109     if (device_id_ == kInvalidHidDeviceId && --retries > 0) {
110       message_loop_->PostDelayedTask(
111           FROM_HERE,
112           base::Bind(&HidConnectionTest::FindDevice, base::Unretained(this),
113                      done, retries),
114           base::TimeDelta::FromMilliseconds(10));
115     } else {
116       message_loop_->PostTask(FROM_HERE, done);
117     }
118   }
119
120   scoped_ptr<base::MessageLoopForIO> message_loop_;
121   HidService* service_;
122   scoped_ptr<UsbTestGadget> test_gadget_;
123   HidDeviceId device_id_;
124 };
125
126 TEST_F(HidConnectionTest, ReadWrite) {
127   if (!UsbTestGadget::IsTestEnabled()) return;
128
129   scoped_refptr<HidConnection> conn = service_->Connect(device_id_);
130   ASSERT_TRUE(conn.get());
131
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;
137     }
138
139     TestCompletionCallback write_callback;
140     conn->Write(buffer, buffer->size(), write_callback.write_callback());
141     ASSERT_TRUE(write_callback.WaitForResult());
142
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]);
150     }
151   }
152 }
153
154 }  // namespace device