OLD | NEW |
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 "base/scoped_observer.h" | 12 #include "base/scoped_observer.h" |
| 13 #include "base/test/test_io_thread.h" |
13 #include "device/hid/hid_connection.h" | 14 #include "device/hid/hid_connection.h" |
14 #include "device/hid/hid_service.h" | 15 #include "device/hid/hid_service.h" |
15 #include "device/test/usb_test_gadget.h" | 16 #include "device/test/usb_test_gadget.h" |
16 #include "net/base/io_buffer.h" | 17 #include "net/base/io_buffer.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
18 | 19 |
19 namespace device { | 20 namespace device { |
20 | 21 |
21 namespace { | 22 namespace { |
22 | 23 |
23 using net::IOBufferWithSize; | 24 using net::IOBufferWithSize; |
24 | 25 |
| 26 void ClaimTestDevice(scoped_ptr<UsbTestGadget>* gadget) { |
| 27 base::MessageLoop::ScopedNestableTaskAllower allow( |
| 28 base::MessageLoop::current()); |
| 29 *gadget = UsbTestGadget::Claim(); |
| 30 ASSERT_TRUE(*gadget); |
| 31 ASSERT_TRUE((*gadget)->SetType(UsbTestGadget::HID_ECHO)); |
| 32 } |
| 33 |
| 34 void UnclaimTestDevice(scoped_ptr<UsbTestGadget> gadget) { |
| 35 base::MessageLoop::ScopedNestableTaskAllower allow( |
| 36 base::MessageLoop::current()); |
| 37 ASSERT_TRUE(gadget->Unclaim()); |
| 38 } |
| 39 |
25 // Helper class that can be used to block until a HID device with a particular | 40 // Helper class that can be used to block until a HID device with a particular |
26 // serial number is available. Example usage: | 41 // serial number is available. Example usage: |
27 // | 42 // |
28 // DeviceCatcher device_catcher("ABC123"); | 43 // DeviceCatcher device_catcher("ABC123"); |
29 // HidDeviceId device_id = device_catcher.WaitForDevice(); | 44 // HidDeviceId device_id = device_catcher.WaitForDevice(); |
30 // /* Call HidService::Connect(device_id) to open the device. */ | 45 // /* Call HidService::Connect(device_id) to open the device. */ |
31 // | 46 // |
32 class DeviceCatcher : HidService::Observer { | 47 class DeviceCatcher : HidService::Observer { |
33 public: | 48 public: |
34 DeviceCatcher(const std::string& serial_number) | 49 DeviceCatcher(const std::string& serial_number) |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 HidConnection::WriteCallback write_callback_; | 154 HidConnection::WriteCallback write_callback_; |
140 }; | 155 }; |
141 | 156 |
142 } // namespace | 157 } // namespace |
143 | 158 |
144 class HidConnectionTest : public testing::Test { | 159 class HidConnectionTest : public testing::Test { |
145 protected: | 160 protected: |
146 void SetUp() override { | 161 void SetUp() override { |
147 if (!UsbTestGadget::IsTestEnabled()) return; | 162 if (!UsbTestGadget::IsTestEnabled()) return; |
148 | 163 |
149 message_loop_.reset(new base::MessageLoopForIO()); | 164 message_loop_.reset(new base::MessageLoopForUI()); |
150 service_ = HidService::GetInstance(message_loop_->message_loop_proxy()); | 165 io_thread_.reset(new base::TestIOThread(base::TestIOThread::kAutoStart)); |
| 166 |
| 167 service_ = HidService::GetInstance(io_thread_->task_runner()); |
151 ASSERT_TRUE(service_); | 168 ASSERT_TRUE(service_); |
152 | 169 |
153 test_gadget_ = UsbTestGadget::Claim(); | 170 io_thread_->PostTaskAndWait(FROM_HERE, |
| 171 base::Bind(&ClaimTestDevice, &test_gadget_)); |
154 ASSERT_TRUE(test_gadget_); | 172 ASSERT_TRUE(test_gadget_); |
155 ASSERT_TRUE(test_gadget_->SetType(UsbTestGadget::HID_ECHO)); | |
156 | 173 |
157 DeviceCatcher device_catcher(test_gadget_->GetSerialNumber()); | 174 DeviceCatcher device_catcher(test_gadget_->GetSerialNumber()); |
158 device_id_ = device_catcher.WaitForDevice(); | 175 device_id_ = device_catcher.WaitForDevice(); |
159 ASSERT_NE(device_id_, kInvalidHidDeviceId); | 176 ASSERT_NE(device_id_, kInvalidHidDeviceId); |
160 } | 177 } |
161 | 178 |
162 scoped_ptr<base::MessageLoopForIO> message_loop_; | 179 void TearDown() override { |
| 180 if (io_thread_) { |
| 181 io_thread_->PostTaskAndWait( |
| 182 FROM_HERE, |
| 183 base::Bind(&UnclaimTestDevice, base::Passed(&test_gadget_))); |
| 184 } |
| 185 } |
| 186 |
| 187 scoped_ptr<base::MessageLoopForUI> message_loop_; |
| 188 scoped_ptr<base::TestIOThread> io_thread_; |
163 HidService* service_; | 189 HidService* service_; |
164 scoped_ptr<UsbTestGadget> test_gadget_; | 190 scoped_ptr<UsbTestGadget> test_gadget_; |
165 HidDeviceId device_id_; | 191 HidDeviceId device_id_; |
166 }; | 192 }; |
167 | 193 |
168 TEST_F(HidConnectionTest, ReadWrite) { | 194 TEST_F(HidConnectionTest, ReadWrite) { |
169 if (!UsbTestGadget::IsTestEnabled()) return; | 195 if (!UsbTestGadget::IsTestEnabled()) return; |
170 | 196 |
171 TestConnectCallback connect_callback; | 197 TestConnectCallback connect_callback; |
172 service_->Connect(device_id_, connect_callback.callback()); | 198 service_->Connect(device_id_, connect_callback.callback()); |
(...skipping 19 matching lines...) Expand all Loading... |
192 ASSERT_EQ(0, read_callback.buffer()->data()[0]); | 218 ASSERT_EQ(0, read_callback.buffer()->data()[0]); |
193 for (unsigned char j = 1; j < kBufferSize; ++j) { | 219 for (unsigned char j = 1; j < kBufferSize; ++j) { |
194 ASSERT_EQ(i + j - 1, read_callback.buffer()->data()[j]); | 220 ASSERT_EQ(i + j - 1, read_callback.buffer()->data()[j]); |
195 } | 221 } |
196 } | 222 } |
197 | 223 |
198 conn->Close(); | 224 conn->Close(); |
199 } | 225 } |
200 | 226 |
201 } // namespace device | 227 } // namespace device |
OLD | NEW |