Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(257)

Side by Side Diff: device/hid/hid_connection_unittest.cc

Issue 143883005: HID backend (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@hid-impl-base
Patch Set: Really fix tests for lack of testing hardware; adapt to ancient libudev in the cros distro Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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/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"
15
16 namespace device {
17
18 namespace {
19
20 using net::IOBuffer;
21
22 const int kUSBLUFADemoVID = 0x03eb;
23 const int kUSBLUFADemoPID = 0x204f;
24 const uint64_t kReport = 0x0903a65d030f8ec9ULL;
25
26 int g_read_times = 0;
27 void Read(scoped_refptr<HidConnection> conn);
28
29 void OnRead(scoped_refptr<HidConnection> conn,
30 scoped_refptr<net::IOBuffer> 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();
42 }
43 } else {
44 LOG(ERROR) << "~";
45 g_read_times++;
46 }
47
48 if (g_read_times < 3){
49 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind(Read, conn));
50 } else {
51 base::MessageLoop::current()->Quit();
52 }
53 }
54
55 void Read(scoped_refptr<HidConnection> conn) {
56 scoped_refptr<IOBuffer> buffer(new IOBuffer(8));
57 conn->Read(buffer, 8, base::Bind(OnRead, conn, buffer));
58 }
59
60 void OnWriteNormal(bool success,
61 size_t bytes) {
62 ASSERT_TRUE(success);
63 base::MessageLoop::current()->Quit();
64 }
65
66 void WriteNormal(scoped_refptr<HidConnection> conn) {
67 scoped_refptr<IOBuffer> buffer(new IOBuffer(8));
68 *(int64_t*)buffer->data() = kReport;
69
70 conn->Write(buffer, 8, base::Bind(OnWriteNormal));
71 }
72
73 } // namespace
74
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_);
81
82 std::vector<HidDeviceInfo> devices;
83 service_->GetDevices(&devices);
84 ASSERT_GT(devices.size(), 0U) << "No device found";
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;
92 }
93 }
94 }
95
96 virtual void TearDown() OVERRIDE {
97 service_.reset(NULL);
98 message_loop_.reset(NULL);
99 }
100
101 std::string device_id_;
102 scoped_ptr<base::MessageLoopForIO> message_loop_;
103 scoped_ptr<HidService> service_;
104 };
105
106 TEST_F(HidConnectionTest, Create) {
107 scoped_refptr<HidConnection> connection = service_->Connect(device_id_);
108 ASSERT_TRUE(connection || device_id_.empty());
109 }
110
111 TEST_F(HidConnectionTest, Read) {
112 scoped_refptr<HidConnection> connection = service_->Connect(device_id_);
113
114 if (!device_id_.empty()) {
115 ASSERT_TRUE(connection);
116 message_loop_->PostTask(FROM_HERE, base::Bind(Read, connection));
117 message_loop_->Run();
118 }
119 }
120
121 TEST_F(HidConnectionTest, Write) {
122 scoped_refptr<HidConnection> connection = service_->Connect(device_id_);
123
124 if (!device_id_.empty()) {
125 ASSERT_TRUE(connection);
126 message_loop_->PostTask(FROM_HERE, base::Bind(WriteNormal, connection));
127 message_loop_->Run();
128 }
129 }
130
131 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698