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/memory/scoped_ptr.h"
10 #include "base/message_loop/message_loop.h"
11 #include "device/hid/hid_connection.h"
12 #include "device/hid/hid_service.h"
13 #include "net/base/io_buffer.h"
14 #include "testing/gtest/include/gtest/gtest.h"
20 using net::IOBufferWithSize
;
22 const int kUSBLUFADemoVID
= 0x03eb;
23 const int kUSBLUFADemoPID
= 0x204f;
24 const uint64_t kReport
= 0x0903a65d030f8ec9ULL
;
27 void Read(scoped_refptr
<HidConnection
> conn
);
29 void OnRead(scoped_refptr
<HidConnection
> conn
,
30 scoped_refptr
<IOBufferWithSize
> buffer
,
38 uint64_t* data
= reinterpret_cast<uint64_t*>(buffer
->data());
39 EXPECT_EQ(kReport
, *data
);
41 base::MessageLoop::current()->Quit();
48 if (g_read_times
< 3){
49 base::MessageLoop::current()->PostTask(FROM_HERE
, base::Bind(Read
, conn
));
51 base::MessageLoop::current()->Quit();
55 void Read(scoped_refptr
<HidConnection
> conn
) {
56 scoped_refptr
<IOBufferWithSize
> buffer(new IOBufferWithSize(8));
57 conn
->Read(buffer
, base::Bind(OnRead
, conn
, buffer
));
60 void OnWriteNormal(bool success
,
63 base::MessageLoop::current()->Quit();
66 void WriteNormal(scoped_refptr
<HidConnection
> conn
) {
67 scoped_refptr
<IOBufferWithSize
> buffer(new IOBufferWithSize(8));
68 *(int64_t*)buffer
->data() = kReport
;
70 conn
->Write(0, buffer
, base::Bind(OnWriteNormal
));
75 class HidConnectionTest
: public testing::Test
{
77 virtual void SetUp() OVERRIDE
{
78 message_loop_
.reset(new base::MessageLoopForIO());
79 service_
.reset(HidService::CreateInstance());
80 ASSERT_TRUE(service_
);
82 std::vector
<HidDeviceInfo
> devices
;
83 service_
->GetDevices(&devices
);
84 device_id_
= kInvalidHidDeviceId
;
85 for (std::vector
<HidDeviceInfo
>::iterator it
= devices
.begin();
88 if (it
->vendor_id
== kUSBLUFADemoVID
&&
89 it
->product_id
== kUSBLUFADemoPID
) {
90 device_id_
= it
->device_id
;
96 virtual void TearDown() OVERRIDE
{
98 message_loop_
.reset(NULL
);
101 HidDeviceId device_id_
;
102 scoped_ptr
<base::MessageLoopForIO
> message_loop_
;
103 scoped_ptr
<HidService
> service_
;
106 TEST_F(HidConnectionTest
, Create
) {
107 scoped_refptr
<HidConnection
> connection
= service_
->Connect(device_id_
);
108 ASSERT_TRUE(connection
|| device_id_
== kInvalidHidDeviceId
);
111 TEST_F(HidConnectionTest
, Read
) {
112 scoped_refptr
<HidConnection
> connection
= service_
->Connect(device_id_
);
114 message_loop_
->PostTask(FROM_HERE
, base::Bind(Read
, connection
));
115 message_loop_
->Run();
119 TEST_F(HidConnectionTest
, Write
) {
120 scoped_refptr
<HidConnection
> connection
= service_
->Connect(device_id_
);
123 message_loop_
->PostTask(FROM_HERE
, base::Bind(WriteNormal
, connection
));
124 message_loop_
->Run();
128 } // namespace device