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

Side by Side Diff: device/usb/usb_device_handle_unittest.cc

Issue 980023002: Move device/usb classes from the FILE thread to UI thread. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add more thread assertions. Created 5 years, 8 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 | « device/usb/usb_device_handle_impl.cc ('k') | device/usb/usb_device_impl.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 2014 The Chromium Authors. All rights reserved. 1 // Copyright 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 "base/bind.h" 5 #include "base/bind.h"
6 #include "base/message_loop/message_loop.h" 6 #include "base/message_loop/message_loop.h"
7 #include "base/run_loop.h" 7 #include "base/run_loop.h"
8 #include "base/strings/utf_string_conversions.h" 8 #include "base/strings/utf_string_conversions.h"
9 #include "base/test/test_io_thread.h"
9 #include "device/test/usb_test_gadget.h" 10 #include "device/test/usb_test_gadget.h"
10 #include "device/usb/usb_device.h" 11 #include "device/usb/usb_device.h"
11 #include "device/usb/usb_device_handle.h" 12 #include "device/usb/usb_device_handle.h"
12 #include "testing/gtest/include/gtest/gtest.h" 13 #include "testing/gtest/include/gtest/gtest.h"
13 14
14 namespace device { 15 namespace device {
15 16
16 namespace { 17 namespace {
17 18
18 class UsbDeviceHandleTest : public ::testing::Test { 19 class UsbDeviceHandleTest : public ::testing::Test {
19 public: 20 public:
20 void SetUp() override { 21 void SetUp() override {
21 if (!UsbTestGadget::IsTestEnabled()) { 22 message_loop_.reset(new base::MessageLoopForUI);
22 return; 23 io_thread_.reset(new base::TestIOThread(base::TestIOThread::kAutoStart));
23 }
24
25 message_loop_.reset(new base::MessageLoopForIO);
26
27 gadget_ = UsbTestGadget::Claim();
28 ASSERT_TRUE(gadget_.get());
29
30 ASSERT_TRUE(gadget_->SetType(UsbTestGadget::ECHO));
31
32 handle_ = gadget_->GetDevice()->Open();
33 ASSERT_TRUE(handle_.get());
34 }
35
36 void TearDown() override {
37 if (handle_.get()) {
38 handle_->Close();
39 }
40 gadget_.reset(NULL);
41 message_loop_.reset(NULL);
42 } 24 }
43 25
44 protected: 26 protected:
45 scoped_refptr<UsbDeviceHandle> handle_; 27 scoped_ptr<base::TestIOThread> io_thread_;
46 28
47 private: 29 private:
48 scoped_ptr<UsbTestGadget> gadget_;
49 scoped_ptr<base::MessageLoop> message_loop_; 30 scoped_ptr<base::MessageLoop> message_loop_;
50 }; 31 };
51 32
33 class TestOpenCallback {
34 public:
35 TestOpenCallback()
36 : callback_(
37 base::Bind(&TestOpenCallback::SetResult, base::Unretained(this))) {}
38
39 scoped_refptr<UsbDeviceHandle> WaitForResult() {
40 run_loop_.Run();
41 return device_handle_;
42 }
43
44 const UsbDevice::OpenCallback& callback() const { return callback_; }
45
46 private:
47 void SetResult(scoped_refptr<UsbDeviceHandle> device_handle) {
48 device_handle_ = device_handle;
49 run_loop_.Quit();
50 }
51
52 const UsbDevice::OpenCallback callback_;
53 base::RunLoop run_loop_;
54 scoped_refptr<UsbDeviceHandle> device_handle_;
55 };
56
57 class TestResultCallback {
58 public:
59 TestResultCallback()
60 : callback_(base::Bind(&TestResultCallback::SetResult,
61 base::Unretained(this))) {}
62
63 bool WaitForResult() {
64 run_loop_.Run();
65 return success_;
66 }
67
68 const UsbDeviceHandle::ResultCallback& callback() const { return callback_; }
69
70 private:
71 void SetResult(bool success) {
72 success_ = success;
73 run_loop_.Quit();
74 }
75
76 const UsbDeviceHandle::ResultCallback callback_;
77 base::RunLoop run_loop_;
78 bool success_;
79 };
80
52 class TestCompletionCallback { 81 class TestCompletionCallback {
53 public: 82 public:
54 TestCompletionCallback() 83 TestCompletionCallback()
55 : callback_(base::Bind(&TestCompletionCallback::SetResult, 84 : callback_(base::Bind(&TestCompletionCallback::SetResult,
56 base::Unretained(this))) {} 85 base::Unretained(this))) {}
57 86
87 void WaitForResult() { run_loop_.Run(); }
88
89 const UsbDeviceHandle::TransferCallback& callback() const {
90 return callback_;
91 }
92 UsbTransferStatus status() const { return status_; }
93 size_t transferred() const { return transferred_; }
94
95 private:
58 void SetResult(UsbTransferStatus status, 96 void SetResult(UsbTransferStatus status,
59 scoped_refptr<net::IOBuffer> buffer, 97 scoped_refptr<net::IOBuffer> buffer,
60 size_t transferred) { 98 size_t transferred) {
61 status_ = status; 99 status_ = status;
62 transferred_ = transferred; 100 transferred_ = transferred;
63 run_loop_.Quit(); 101 run_loop_.Quit();
64 } 102 }
65 103
66 void WaitForResult() { run_loop_.Run(); } 104 const UsbDeviceHandle::TransferCallback callback_;
67
68 const UsbTransferCallback& callback() const { return callback_; }
69 UsbTransferStatus status() const { return status_; }
70 size_t transferred() const { return transferred_; }
71
72 private:
73 const UsbTransferCallback callback_;
74 base::RunLoop run_loop_; 105 base::RunLoop run_loop_;
75 UsbTransferStatus status_; 106 UsbTransferStatus status_;
76 size_t transferred_; 107 size_t transferred_;
77 }; 108 };
78 109
79 TEST_F(UsbDeviceHandleTest, InterruptTransfer) { 110 TEST_F(UsbDeviceHandleTest, InterruptTransfer) {
80 if (!handle_.get()) { 111 if (!UsbTestGadget::IsTestEnabled()) {
81 return; 112 return;
82 } 113 }
83 114
84 ASSERT_TRUE(handle_->ClaimInterface(0)); 115 scoped_ptr<UsbTestGadget> gadget =
116 UsbTestGadget::Claim(io_thread_->task_runner());
117 ASSERT_TRUE(gadget.get());
118 ASSERT_TRUE(gadget->SetType(UsbTestGadget::ECHO));
119
120 TestOpenCallback open_device;
121 gadget->GetDevice()->Open(open_device.callback());
122 scoped_refptr<UsbDeviceHandle> handle = open_device.WaitForResult();
123 ASSERT_TRUE(handle.get());
124
125 TestResultCallback claim_interface;
126 handle->ClaimInterface(0, claim_interface.callback());
127 ASSERT_TRUE(claim_interface.WaitForResult());
85 128
86 scoped_refptr<net::IOBufferWithSize> in_buffer(new net::IOBufferWithSize(64)); 129 scoped_refptr<net::IOBufferWithSize> in_buffer(new net::IOBufferWithSize(64));
87 TestCompletionCallback in_completion; 130 TestCompletionCallback in_completion;
88 handle_->InterruptTransfer(USB_DIRECTION_INBOUND, 131 handle->InterruptTransfer(USB_DIRECTION_INBOUND, 0x81, in_buffer.get(),
89 0x81, 132 in_buffer->size(),
90 in_buffer.get(), 133 5000, // 5 second timeout
91 in_buffer->size(), 134 in_completion.callback());
92 5000, // 5 second timeout
93 in_completion.callback());
94 135
95 scoped_refptr<net::IOBufferWithSize> out_buffer( 136 scoped_refptr<net::IOBufferWithSize> out_buffer(
96 new net::IOBufferWithSize(in_buffer->size())); 137 new net::IOBufferWithSize(in_buffer->size()));
97 TestCompletionCallback out_completion; 138 TestCompletionCallback out_completion;
98 for (int i = 0; i < out_buffer->size(); ++i) { 139 for (int i = 0; i < out_buffer->size(); ++i) {
99 out_buffer->data()[i] = i; 140 out_buffer->data()[i] = i;
100 } 141 }
101 142
102 handle_->InterruptTransfer(USB_DIRECTION_OUTBOUND, 143 handle->InterruptTransfer(USB_DIRECTION_OUTBOUND, 0x01, out_buffer.get(),
103 0x01, 144 out_buffer->size(),
104 out_buffer.get(), 145 5000, // 5 second timeout
105 out_buffer->size(), 146 out_completion.callback());
106 5000, // 5 second timeout
107 out_completion.callback());
108 out_completion.WaitForResult(); 147 out_completion.WaitForResult();
109 ASSERT_EQ(USB_TRANSFER_COMPLETED, out_completion.status()); 148 ASSERT_EQ(USB_TRANSFER_COMPLETED, out_completion.status());
110 EXPECT_EQ(static_cast<size_t>(out_buffer->size()), 149 EXPECT_EQ(static_cast<size_t>(out_buffer->size()),
111 out_completion.transferred()); 150 out_completion.transferred());
112 151
113 in_completion.WaitForResult(); 152 in_completion.WaitForResult();
114 ASSERT_EQ(USB_TRANSFER_COMPLETED, in_completion.status()); 153 ASSERT_EQ(USB_TRANSFER_COMPLETED, in_completion.status());
115 EXPECT_EQ(static_cast<size_t>(in_buffer->size()), 154 EXPECT_EQ(static_cast<size_t>(in_buffer->size()),
116 in_completion.transferred()); 155 in_completion.transferred());
117 for (size_t i = 0; i < in_completion.transferred(); ++i) { 156 for (size_t i = 0; i < in_completion.transferred(); ++i) {
118 EXPECT_EQ(out_buffer->data()[i], in_buffer->data()[i]); 157 EXPECT_EQ(out_buffer->data()[i], in_buffer->data()[i]);
119 } 158 }
159
160 handle->Close();
120 } 161 }
121 162
122 TEST_F(UsbDeviceHandleTest, BulkTransfer) { 163 TEST_F(UsbDeviceHandleTest, BulkTransfer) {
123 if (!handle_.get()) { 164 if (!UsbTestGadget::IsTestEnabled()) {
124 return; 165 return;
125 } 166 }
126 167
127 ASSERT_TRUE(handle_->ClaimInterface(1)); 168 scoped_ptr<UsbTestGadget> gadget =
169 UsbTestGadget::Claim(io_thread_->task_runner());
170 ASSERT_TRUE(gadget.get());
171 ASSERT_TRUE(gadget->SetType(UsbTestGadget::ECHO));
172
173 TestOpenCallback open_device;
174 gadget->GetDevice()->Open(open_device.callback());
175 scoped_refptr<UsbDeviceHandle> handle = open_device.WaitForResult();
176 ASSERT_TRUE(handle.get());
177
178 TestResultCallback claim_interface;
179 handle->ClaimInterface(1, claim_interface.callback());
180 ASSERT_TRUE(claim_interface.WaitForResult());
128 181
129 scoped_refptr<net::IOBufferWithSize> in_buffer( 182 scoped_refptr<net::IOBufferWithSize> in_buffer(
130 new net::IOBufferWithSize(512)); 183 new net::IOBufferWithSize(512));
131 TestCompletionCallback in_completion; 184 TestCompletionCallback in_completion;
132 handle_->BulkTransfer(USB_DIRECTION_INBOUND, 0x82, in_buffer.get(), 185 handle->BulkTransfer(USB_DIRECTION_INBOUND, 0x82, in_buffer.get(),
133 in_buffer->size(), 186 in_buffer->size(),
134 5000, // 5 second timeout 187 5000, // 5 second timeout
135 in_completion.callback()); 188 in_completion.callback());
136 189
137 scoped_refptr<net::IOBufferWithSize> out_buffer( 190 scoped_refptr<net::IOBufferWithSize> out_buffer(
138 new net::IOBufferWithSize(in_buffer->size())); 191 new net::IOBufferWithSize(in_buffer->size()));
139 TestCompletionCallback out_completion; 192 TestCompletionCallback out_completion;
140 for (int i = 0; i < out_buffer->size(); ++i) { 193 for (int i = 0; i < out_buffer->size(); ++i) {
141 out_buffer->data()[i] = i; 194 out_buffer->data()[i] = i;
142 } 195 }
143 196
144 handle_->BulkTransfer(USB_DIRECTION_OUTBOUND, 0x02, out_buffer.get(), 197 handle->BulkTransfer(USB_DIRECTION_OUTBOUND, 0x02, out_buffer.get(),
145 out_buffer->size(), 198 out_buffer->size(),
146 5000, // 5 second timeout 199 5000, // 5 second timeout
147 out_completion.callback()); 200 out_completion.callback());
148 out_completion.WaitForResult(); 201 out_completion.WaitForResult();
149 ASSERT_EQ(USB_TRANSFER_COMPLETED, out_completion.status()); 202 ASSERT_EQ(USB_TRANSFER_COMPLETED, out_completion.status());
150 EXPECT_EQ(static_cast<size_t>(out_buffer->size()), 203 EXPECT_EQ(static_cast<size_t>(out_buffer->size()),
151 out_completion.transferred()); 204 out_completion.transferred());
152 205
153 in_completion.WaitForResult(); 206 in_completion.WaitForResult();
154 ASSERT_EQ(USB_TRANSFER_COMPLETED, in_completion.status()); 207 ASSERT_EQ(USB_TRANSFER_COMPLETED, in_completion.status());
155 EXPECT_EQ(static_cast<size_t>(in_buffer->size()), 208 EXPECT_EQ(static_cast<size_t>(in_buffer->size()),
156 in_completion.transferred()); 209 in_completion.transferred());
157 for (size_t i = 0; i < in_completion.transferred(); ++i) { 210 for (size_t i = 0; i < in_completion.transferred(); ++i) {
158 EXPECT_EQ(out_buffer->data()[i], in_buffer->data()[i]); 211 EXPECT_EQ(out_buffer->data()[i], in_buffer->data()[i]);
159 } 212 }
213
214 handle->Close();
160 } 215 }
161 216
162 } // namespace 217 } // namespace
163 218
164 } // namespace device 219 } // namespace device
OLDNEW
« no previous file with comments | « device/usb/usb_device_handle_impl.cc ('k') | device/usb/usb_device_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698