Add common GLFence::IsSupported() check
[chromium-blink-merge.git] / device / hid / hid_connection_unittest.cc
blob27254817715bed6d21a040bce8720d182b50cad2
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.
5 #include <string>
6 #include <vector>
8 #include "base/bind.h"
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"
16 namespace device {
18 namespace {
20 using net::IOBufferWithSize;
22 const int kUSBLUFADemoVID = 0x03eb;
23 const int kUSBLUFADemoPID = 0x204f;
24 const uint64_t kReport = 0x0903a65d030f8ec9ULL;
26 int g_read_times = 0;
27 void Read(scoped_refptr<HidConnection> conn);
29 void OnRead(scoped_refptr<HidConnection> conn,
30 scoped_refptr<IOBufferWithSize> buffer,
31 bool success,
32 size_t bytes) {
33 EXPECT_TRUE(success);
34 if (success) {
35 g_read_times++;
36 EXPECT_EQ(8U, bytes);
37 if (bytes == 8) {
38 uint64_t* data = reinterpret_cast<uint64_t*>(buffer->data());
39 EXPECT_EQ(kReport, *data);
40 } else {
41 base::MessageLoop::current()->Quit();
43 } else {
44 LOG(ERROR) << "~";
45 g_read_times++;
48 if (g_read_times < 3){
49 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind(Read, conn));
50 } else {
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,
61 size_t bytes) {
62 ASSERT_TRUE(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));
73 } // namespace
75 class HidConnectionTest : public testing::Test {
76 protected:
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();
86 it != devices.end();
87 ++it) {
88 if (it->vendor_id == kUSBLUFADemoVID &&
89 it->product_id == kUSBLUFADemoPID) {
90 device_id_ = it->device_id;
91 return;
96 virtual void TearDown() OVERRIDE {
97 service_.reset(NULL);
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_);
113 if (connection) {
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_);
122 if (connection) {
123 message_loop_->PostTask(FROM_HERE, base::Bind(WriteNormal, connection));
124 message_loop_->Run();
128 } // namespace device