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

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

Issue 660573007: Open HID connections asynchronously. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 2 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
« no previous file with comments | « no previous file | device/hid/hid_service.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <string> 5 #include <string>
6 #include <vector> 6 #include <vector>
7 7
8 #include "base/bind.h" 8 #include "base/bind.h"
9 #include "base/callback.h" 9 #include "base/callback.h"
10 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
11 #include "base/run_loop.h" 11 #include "base/run_loop.h"
12 #include "device/hid/hid_connection.h" 12 #include "device/hid/hid_connection.h"
13 #include "device/hid/hid_service.h" 13 #include "device/hid/hid_service.h"
14 #include "device/test/usb_test_gadget.h" 14 #include "device/test/usb_test_gadget.h"
15 #include "net/base/io_buffer.h" 15 #include "net/base/io_buffer.h"
16 #include "testing/gtest/include/gtest/gtest.h" 16 #include "testing/gtest/include/gtest/gtest.h"
17 17
18 namespace device { 18 namespace device {
19 19
20 namespace { 20 namespace {
21 21
22 using net::IOBufferWithSize; 22 using net::IOBufferWithSize;
23 23
24 class TestCompletionCallback { 24 class TestConnectCallback {
25 public: 25 public:
26 TestCompletionCallback() 26 TestConnectCallback()
27 : read_callback_(base::Bind(&TestCompletionCallback::SetReadResult, 27 : callback_(base::Bind(&TestConnectCallback::SetConnection,
28 base::Unretained(this))), 28 base::Unretained(this))) {}
29 write_callback_(base::Bind(&TestCompletionCallback::SetWriteResult, 29 ~TestConnectCallback() {}
30
31 void SetConnection(scoped_refptr<HidConnection> connection) {
32 connection_ = connection;
33 run_loop_.Quit();
34 }
35
36 scoped_refptr<HidConnection> WaitForConnection() {
37 run_loop_.Run();
38 return connection_;
39 }
40
41 const HidService::ConnectCallback& callback() { return callback_; }
42
43 private:
44 HidService::ConnectCallback callback_;
45 base::RunLoop run_loop_;
46 scoped_refptr<HidConnection> connection_;
47 };
48
49 class TestIoCallback {
50 public:
51 TestIoCallback()
52 : read_callback_(
53 base::Bind(&TestIoCallback::SetReadResult, base::Unretained(this))),
54 write_callback_(base::Bind(&TestIoCallback::SetWriteResult,
30 base::Unretained(this))) {} 55 base::Unretained(this))) {}
31 ~TestCompletionCallback() {} 56 ~TestIoCallback() {}
32 57
33 void SetReadResult(bool success, 58 void SetReadResult(bool success,
34 scoped_refptr<net::IOBuffer> buffer, 59 scoped_refptr<net::IOBuffer> buffer,
35 size_t size) { 60 size_t size) {
36 result_ = success; 61 result_ = success;
37 buffer_ = buffer; 62 buffer_ = buffer;
38 size_ = size; 63 size_ = size;
39 run_loop_.Quit(); 64 run_loop_.Quit();
40 } 65 }
41 66
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
121 146
122 scoped_ptr<base::MessageLoopForIO> message_loop_; 147 scoped_ptr<base::MessageLoopForIO> message_loop_;
123 HidService* service_; 148 HidService* service_;
124 scoped_ptr<UsbTestGadget> test_gadget_; 149 scoped_ptr<UsbTestGadget> test_gadget_;
125 HidDeviceId device_id_; 150 HidDeviceId device_id_;
126 }; 151 };
127 152
128 TEST_F(HidConnectionTest, ReadWrite) { 153 TEST_F(HidConnectionTest, ReadWrite) {
129 if (!UsbTestGadget::IsTestEnabled()) return; 154 if (!UsbTestGadget::IsTestEnabled()) return;
130 155
131 scoped_refptr<HidConnection> conn = service_->Connect(device_id_); 156 TestConnectCallback connect_callback;
157 service_->Connect(device_id_, connect_callback.callback());
158 scoped_refptr<HidConnection> conn = connect_callback.WaitForConnection();
132 ASSERT_TRUE(conn.get()); 159 ASSERT_TRUE(conn.get());
133 160
134 for (int i = 0; i < 8; ++i) { 161 for (int i = 0; i < 8; ++i) {
135 scoped_refptr<IOBufferWithSize> buffer(new IOBufferWithSize(9)); 162 scoped_refptr<IOBufferWithSize> buffer(new IOBufferWithSize(9));
136 buffer->data()[0] = 0; 163 buffer->data()[0] = 0;
137 for (int j = 1; j < buffer->size(); ++j) { 164 for (int j = 1; j < buffer->size(); ++j) {
138 buffer->data()[j] = i + j - 1; 165 buffer->data()[j] = i + j - 1;
139 } 166 }
140 167
141 TestCompletionCallback write_callback; 168 TestIoCallback write_callback;
142 conn->Write(buffer, buffer->size(), write_callback.write_callback()); 169 conn->Write(buffer, buffer->size(), write_callback.write_callback());
143 ASSERT_TRUE(write_callback.WaitForResult()); 170 ASSERT_TRUE(write_callback.WaitForResult());
144 171
145 TestCompletionCallback read_callback; 172 TestIoCallback read_callback;
146 conn->Read(read_callback.read_callback()); 173 conn->Read(read_callback.read_callback());
147 ASSERT_TRUE(read_callback.WaitForResult()); 174 ASSERT_TRUE(read_callback.WaitForResult());
148 ASSERT_EQ(9UL, read_callback.size()); 175 ASSERT_EQ(9UL, read_callback.size());
149 ASSERT_EQ(0, read_callback.buffer()->data()[0]); 176 ASSERT_EQ(0, read_callback.buffer()->data()[0]);
150 for (int j = 1; j < buffer->size(); ++j) { 177 for (int j = 1; j < buffer->size(); ++j) {
151 ASSERT_EQ(i + j - 1, read_callback.buffer()->data()[j]); 178 ASSERT_EQ(i + j - 1, read_callback.buffer()->data()[j]);
152 } 179 }
153 } 180 }
154 181
155 conn->Close(); 182 conn->Close();
156 } 183 }
157 184
158 } // namespace device 185 } // namespace device
OLDNEW
« no previous file with comments | « no previous file | device/hid/hid_service.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698