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

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

Issue 2885143002: Use the task scheduler in the new Windows USB backend (Closed)
Patch Set: Created 3 years, 7 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
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 "device/hid/hid_connection.h" 5 #include "device/hid/hid_connection.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <memory> 9 #include <memory>
10 #include <string> 10 #include <string>
11 #include <vector> 11 #include <vector>
12 12
13 #include "base/bind.h" 13 #include "base/bind.h"
14 #include "base/callback.h" 14 #include "base/callback.h"
15 #include "base/message_loop/message_loop.h"
16 #include "base/run_loop.h" 15 #include "base/run_loop.h"
17 #include "base/scoped_observer.h" 16 #include "base/scoped_observer.h"
18 #include "base/strings/utf_string_conversions.h" 17 #include "base/strings/utf_string_conversions.h"
18 #include "base/test/scoped_task_environment.h"
19 #include "base/test/test_io_thread.h" 19 #include "base/test/test_io_thread.h"
20 #include "device/hid/hid_service.h" 20 #include "device/hid/hid_service.h"
21 #include "device/test/test_device_client.h" 21 #include "device/test/test_device_client.h"
22 #include "device/test/usb_test_gadget.h" 22 #include "device/test/usb_test_gadget.h"
23 #include "device/usb/usb_device.h" 23 #include "device/usb/usb_device.h"
24 #include "net/base/io_buffer.h" 24 #include "net/base/io_buffer.h"
25 #include "testing/gtest/include/gtest/gtest.h" 25 #include "testing/gtest/include/gtest/gtest.h"
26 26
27 namespace device { 27 namespace device {
28 28
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
142 bool result_; 142 bool result_;
143 size_t size_; 143 size_t size_;
144 scoped_refptr<net::IOBuffer> buffer_; 144 scoped_refptr<net::IOBuffer> buffer_;
145 HidConnection::ReadCallback read_callback_; 145 HidConnection::ReadCallback read_callback_;
146 HidConnection::WriteCallback write_callback_; 146 HidConnection::WriteCallback write_callback_;
147 }; 147 };
148 148
149 } // namespace 149 } // namespace
150 150
151 class HidConnectionTest : public testing::Test { 151 class HidConnectionTest : public testing::Test {
152 public:
153 HidConnectionTest()
154 : scoped_task_environment_(
155 base::test::ScopedTaskEnvironment::MainThreadType::UI),
156 io_thread_(base::TestIOThread::kAutoStart) {}
157
152 protected: 158 protected:
153 void SetUp() override { 159 void SetUp() override {
154 if (!UsbTestGadget::IsTestEnabled()) return; 160 if (!UsbTestGadget::IsTestEnabled()) return;
155 161
156 message_loop_.reset(new base::MessageLoopForUI());
157 io_thread_.reset(new base::TestIOThread(base::TestIOThread::kAutoStart));
158 device_client_.reset(new TestDeviceClient(io_thread_->task_runner()));
159
160 service_ = DeviceClient::Get()->GetHidService(); 162 service_ = DeviceClient::Get()->GetHidService();
161 ASSERT_TRUE(service_); 163 ASSERT_TRUE(service_);
162 164
163 test_gadget_ = UsbTestGadget::Claim(io_thread_->task_runner()); 165 test_gadget_ = UsbTestGadget::Claim(io_thread_.task_runner());
164 ASSERT_TRUE(test_gadget_); 166 ASSERT_TRUE(test_gadget_);
165 ASSERT_TRUE(test_gadget_->SetType(UsbTestGadget::HID_ECHO)); 167 ASSERT_TRUE(test_gadget_->SetType(UsbTestGadget::HID_ECHO));
166 168
167 DeviceCatcher device_catcher(service_, 169 DeviceCatcher device_catcher(service_,
168 test_gadget_->GetDevice()->serial_number()); 170 test_gadget_->GetDevice()->serial_number());
169 device_id_ = device_catcher.WaitForDevice(); 171 device_id_ = device_catcher.WaitForDevice();
170 ASSERT_NE(device_id_, kInvalidHidDeviceId); 172 ASSERT_NE(device_id_, kInvalidHidDeviceId);
171 } 173 }
172 174
173 std::unique_ptr<base::MessageLoopForUI> message_loop_; 175 base::test::ScopedTaskEnvironment scoped_task_environment_;
mcasas 2017/05/16 21:24:24 Probably can be made const, and |io_thread_| too.
Reilly Grant (use Gerrit) 2017/05/16 21:59:08 These fields can be made const because we don't ca
mcasas 2017/05/16 23:19:48 I like const fields because it tells me that they'
174 std::unique_ptr<base::TestIOThread> io_thread_; 176 base::TestIOThread io_thread_;
175 std::unique_ptr<TestDeviceClient> device_client_; 177 TestDeviceClient device_client_;
176 HidService* service_; 178 HidService* service_;
177 std::unique_ptr<UsbTestGadget> test_gadget_; 179 std::unique_ptr<UsbTestGadget> test_gadget_;
178 HidDeviceId device_id_; 180 HidDeviceId device_id_;
179 }; 181 };
180 182
181 TEST_F(HidConnectionTest, ReadWrite) { 183 TEST_F(HidConnectionTest, ReadWrite) {
182 if (!UsbTestGadget::IsTestEnabled()) return; 184 if (!UsbTestGadget::IsTestEnabled()) return;
183 185
184 TestConnectCallback connect_callback; 186 TestConnectCallback connect_callback;
185 service_->Connect(device_id_, connect_callback.callback()); 187 service_->Connect(device_id_, connect_callback.callback());
(...skipping 19 matching lines...) Expand all
205 ASSERT_EQ(0, read_callback.buffer()->data()[0]); 207 ASSERT_EQ(0, read_callback.buffer()->data()[0]);
206 for (unsigned char j = 1; j < kBufferSize; ++j) { 208 for (unsigned char j = 1; j < kBufferSize; ++j) {
207 ASSERT_EQ(i + j - 1, read_callback.buffer()->data()[j]); 209 ASSERT_EQ(i + j - 1, read_callback.buffer()->data()[j]);
208 } 210 }
209 } 211 }
210 212
211 conn->Close(); 213 conn->Close();
212 } 214 }
213 215
214 } // namespace device 216 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698