| 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/strings/utf_string_conversions.h" |
| 13 #include "base/test/test_io_thread.h" | 14 #include "base/test/test_io_thread.h" |
| 14 #include "device/hid/hid_connection.h" | 15 #include "device/hid/hid_connection.h" |
| 15 #include "device/hid/hid_service.h" | 16 #include "device/hid/hid_service.h" |
| 16 #include "device/test/usb_test_gadget.h" | 17 #include "device/test/usb_test_gadget.h" |
| 18 #include "device/usb/usb_device.h" |
| 17 #include "net/base/io_buffer.h" | 19 #include "net/base/io_buffer.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 21 |
| 20 namespace device { | 22 namespace device { |
| 21 | 23 |
| 22 namespace { | 24 namespace { |
| 23 | 25 |
| 24 using net::IOBufferWithSize; | 26 using net::IOBufferWithSize; |
| 25 | 27 |
| 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 | |
| 40 // Helper class that can be used to block until a HID device with a particular | 28 // Helper class that can be used to block until a HID device with a particular |
| 41 // serial number is available. Example usage: | 29 // serial number is available. Example usage: |
| 42 // | 30 // |
| 43 // DeviceCatcher device_catcher("ABC123"); | 31 // DeviceCatcher device_catcher("ABC123"); |
| 44 // HidDeviceId device_id = device_catcher.WaitForDevice(); | 32 // HidDeviceId device_id = device_catcher.WaitForDevice(); |
| 45 // /* Call HidService::Connect(device_id) to open the device. */ | 33 // /* Call HidService::Connect(device_id) to open the device. */ |
| 46 // | 34 // |
| 47 class DeviceCatcher : HidService::Observer { | 35 class DeviceCatcher : HidService::Observer { |
| 48 public: | 36 public: |
| 49 DeviceCatcher(const std::string& serial_number) | 37 DeviceCatcher(HidService* hid_service, const base::string16& serial_number) |
| 50 : serial_number_(serial_number), observer_(this) { | 38 : serial_number_(base::UTF16ToUTF8(serial_number)), observer_(this) { |
| 51 HidService* hid_service = HidService::GetInstance( | |
| 52 base::MessageLoop::current()->message_loop_proxy()); | |
| 53 observer_.Add(hid_service); | 39 observer_.Add(hid_service); |
| 54 hid_service->GetDevices(base::Bind(&DeviceCatcher::OnEnumerationComplete, | 40 hid_service->GetDevices(base::Bind(&DeviceCatcher::OnEnumerationComplete, |
| 55 base::Unretained(this))); | 41 base::Unretained(this))); |
| 56 } | 42 } |
| 57 | 43 |
| 58 const HidDeviceId& WaitForDevice() { | 44 const HidDeviceId& WaitForDevice() { |
| 59 run_loop_.Run(); | 45 run_loop_.Run(); |
| 60 observer_.RemoveAll(); | 46 observer_.RemoveAll(); |
| 61 return device_id_; | 47 return device_id_; |
| 62 } | 48 } |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 protected: | 147 protected: |
| 162 void SetUp() override { | 148 void SetUp() override { |
| 163 if (!UsbTestGadget::IsTestEnabled()) return; | 149 if (!UsbTestGadget::IsTestEnabled()) return; |
| 164 | 150 |
| 165 message_loop_.reset(new base::MessageLoopForUI()); | 151 message_loop_.reset(new base::MessageLoopForUI()); |
| 166 io_thread_.reset(new base::TestIOThread(base::TestIOThread::kAutoStart)); | 152 io_thread_.reset(new base::TestIOThread(base::TestIOThread::kAutoStart)); |
| 167 | 153 |
| 168 service_ = HidService::GetInstance(io_thread_->task_runner()); | 154 service_ = HidService::GetInstance(io_thread_->task_runner()); |
| 169 ASSERT_TRUE(service_); | 155 ASSERT_TRUE(service_); |
| 170 | 156 |
| 171 io_thread_->PostTaskAndWait(FROM_HERE, | 157 test_gadget_ = UsbTestGadget::Claim(io_thread_->task_runner()); |
| 172 base::Bind(&ClaimTestDevice, &test_gadget_)); | |
| 173 ASSERT_TRUE(test_gadget_); | 158 ASSERT_TRUE(test_gadget_); |
| 159 ASSERT_TRUE(test_gadget_->SetType(UsbTestGadget::HID_ECHO)); |
| 174 | 160 |
| 175 DeviceCatcher device_catcher(test_gadget_->GetSerialNumber()); | 161 DeviceCatcher device_catcher(service_, |
| 162 test_gadget_->GetDevice()->serial_number()); |
| 176 device_id_ = device_catcher.WaitForDevice(); | 163 device_id_ = device_catcher.WaitForDevice(); |
| 177 ASSERT_NE(device_id_, kInvalidHidDeviceId); | 164 ASSERT_NE(device_id_, kInvalidHidDeviceId); |
| 178 } | 165 } |
| 179 | 166 |
| 180 void TearDown() override { | |
| 181 if (io_thread_) { | |
| 182 io_thread_->PostTaskAndWait( | |
| 183 FROM_HERE, | |
| 184 base::Bind(&UnclaimTestDevice, base::Passed(&test_gadget_))); | |
| 185 } | |
| 186 } | |
| 187 | |
| 188 scoped_ptr<base::MessageLoopForUI> message_loop_; | 167 scoped_ptr<base::MessageLoopForUI> message_loop_; |
| 189 scoped_ptr<base::TestIOThread> io_thread_; | 168 scoped_ptr<base::TestIOThread> io_thread_; |
| 190 HidService* service_; | 169 HidService* service_; |
| 191 scoped_ptr<UsbTestGadget> test_gadget_; | 170 scoped_ptr<UsbTestGadget> test_gadget_; |
| 192 HidDeviceId device_id_; | 171 HidDeviceId device_id_; |
| 193 }; | 172 }; |
| 194 | 173 |
| 195 TEST_F(HidConnectionTest, ReadWrite) { | 174 TEST_F(HidConnectionTest, ReadWrite) { |
| 196 if (!UsbTestGadget::IsTestEnabled()) return; | 175 if (!UsbTestGadget::IsTestEnabled()) return; |
| 197 | 176 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 219 ASSERT_EQ(0, read_callback.buffer()->data()[0]); | 198 ASSERT_EQ(0, read_callback.buffer()->data()[0]); |
| 220 for (unsigned char j = 1; j < kBufferSize; ++j) { | 199 for (unsigned char j = 1; j < kBufferSize; ++j) { |
| 221 ASSERT_EQ(i + j - 1, read_callback.buffer()->data()[j]); | 200 ASSERT_EQ(i + j - 1, read_callback.buffer()->data()[j]); |
| 222 } | 201 } |
| 223 } | 202 } |
| 224 | 203 |
| 225 conn->Close(); | 204 conn->Close(); |
| 226 } | 205 } |
| 227 | 206 |
| 228 } // namespace device | 207 } // namespace device |
| OLD | NEW |