Upstream version 9.38.198.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     : callback_(base::Bind(&TestCompletionCallback::SetResult,
28                 base::Unretained(this))) {}
29
30   void SetResult(bool success, size_t size) {
31     result_ = success;
32     transferred_ = size;
33     run_loop_.Quit();
34   }
35
36   bool WaitForResult() {
37     run_loop_.Run();
38     return result_;
39   }
40
41   const HidConnection::IOCallback& callback() const { return callback_; }
42   size_t transferred() const { return transferred_; }
43
44  private:
45   const HidConnection::IOCallback callback_;
46   base::RunLoop run_loop_;
47   bool result_;
48   size_t transferred_;
49 };
50
51 }  // namespace
52
53 class HidConnectionTest : public testing::Test {
54  protected:
55   virtual void SetUp() OVERRIDE {
56     if (!UsbTestGadget::IsTestEnabled()) return;
57
58     message_loop_.reset(new base::MessageLoopForIO());
59     service_.reset(HidService::Create(message_loop_->message_loop_proxy()));
60     ASSERT_TRUE(service_);
61
62     test_gadget_ = UsbTestGadget::Claim();
63     ASSERT_TRUE(test_gadget_);
64     ASSERT_TRUE(test_gadget_->SetType(UsbTestGadget::HID_ECHO));
65
66     device_id_ = kInvalidHidDeviceId;
67
68     base::RunLoop run_loop;
69     message_loop_->PostDelayedTask(
70         FROM_HERE,
71         base::Bind(&HidConnectionTest::FindDevice,
72                    base::Unretained(this), run_loop.QuitClosure(), 5),
73         base::TimeDelta::FromMilliseconds(250));
74     run_loop.Run();
75
76     ASSERT_NE(device_id_, kInvalidHidDeviceId);
77   }
78
79   void FindDevice(const base::Closure& done, int retries) {
80     std::vector<HidDeviceInfo> devices;
81     service_->GetDevices(&devices);
82
83     for (std::vector<HidDeviceInfo>::iterator it = devices.begin();
84          it != devices.end();
85          ++it) {
86       if (it->serial_number == test_gadget_->GetSerial()) {
87         device_id_ = it->device_id;
88         break;
89       }
90     }
91
92     if (device_id_ == kInvalidHidDeviceId && --retries > 0) {
93       message_loop_->PostDelayedTask(
94           FROM_HERE,
95           base::Bind(&HidConnectionTest::FindDevice, base::Unretained(this),
96                      done, retries),
97           base::TimeDelta::FromMilliseconds(10));
98     } else {
99       message_loop_->PostTask(FROM_HERE, done);
100     }
101   }
102
103   scoped_ptr<base::MessageLoopForIO> message_loop_;
104   scoped_ptr<HidService> service_;
105   scoped_ptr<UsbTestGadget> test_gadget_;
106   HidDeviceId device_id_;
107 };
108
109 TEST_F(HidConnectionTest, ReadWrite) {
110   if (!UsbTestGadget::IsTestEnabled()) return;
111
112   scoped_refptr<HidConnection> conn = service_->Connect(device_id_);
113   ASSERT_TRUE(conn);
114
115   for (int i = 0; i < 8; ++i) {
116     scoped_refptr<IOBufferWithSize> write_buffer(new IOBufferWithSize(8));
117     *(int64_t*)write_buffer->data() = i;
118
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());
123
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());
130   }
131 }
132
133 }  // namespace device