diff options
author | reillyg <reillyg@chromium.org> | 2014-12-09 14:03:37 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2014-12-09 22:04:29 +0000 |
commit | 5b2961f7d25bb6c0fdb364b12de0e7ebed4706bb (patch) | |
tree | 2abb1e368534f5a9a922ccb6b054936bd956716e /device/hid/hid_connection_unittest.cc | |
parent | ec6b033cd9fe790bcebde7b5c1cd3490784b891a (diff) | |
download | chromium_src-5b2961f7d25bb6c0fdb364b12de0e7ebed4706bb.zip chromium_src-5b2961f7d25bb6c0fdb364b12de0e7ebed4706bb.tar.gz chromium_src-5b2961f7d25bb6c0fdb364b12de0e7ebed4706bb.tar.bz2 |
Return result from HidService::GetDevices asynchronously.
On Linux HID enumeration happens asynchronously. This change updates
HidService and HidDeviceManager to track an asynchronous enumeration
and not return any results to API clients until the first enumeration
has been completed.
In preparation for broadcasting device add/remove events to apps this
change also switches HidDeviceManager to maintaining its resource ID
mappings using a HidService::Observer. This also made the switch to an
asynchronous GetApiDevices API easier.
BUG=376719
Review URL: https://codereview.chromium.org/782153003
Cr-Commit-Position: refs/heads/master@{#307564}
Diffstat (limited to 'device/hid/hid_connection_unittest.cc')
-rw-r--r-- | device/hid/hid_connection_unittest.cc | 85 |
1 files changed, 51 insertions, 34 deletions
diff --git a/device/hid/hid_connection_unittest.cc b/device/hid/hid_connection_unittest.cc index fbacb52..f4bf5de 100644 --- a/device/hid/hid_connection_unittest.cc +++ b/device/hid/hid_connection_unittest.cc @@ -9,6 +9,7 @@ #include "base/callback.h" #include "base/memory/scoped_ptr.h" #include "base/run_loop.h" +#include "base/scoped_observer.h" #include "device/hid/hid_connection.h" #include "device/hid/hid_service.h" #include "device/test/usb_test_gadget.h" @@ -21,6 +22,54 @@ namespace { using net::IOBufferWithSize; +// Helper class that can be used to block until a HID device with a particular +// serial number is available. Example usage: +// +// DeviceCatcher device_catcher("ABC123"); +// HidDeviceId device_id = device_catcher.WaitForDevice(); +// /* Call HidService::Connect(device_id) to open the device. */ +// +class DeviceCatcher : HidService::Observer { + public: + DeviceCatcher(const std::string& serial_number) + : serial_number_(serial_number), observer_(this) { + HidService* hid_service = HidService::GetInstance( + base::MessageLoop::current()->message_loop_proxy()); + observer_.Add(hid_service); + hid_service->GetDevices(base::Bind(&DeviceCatcher::OnEnumerationComplete, + base::Unretained(this))); + } + + const HidDeviceId& WaitForDevice() { + run_loop_.Run(); + observer_.RemoveAll(); + return device_id_; + } + + private: + void OnEnumerationComplete(const std::vector<HidDeviceInfo>& devices) { + for (const HidDeviceInfo& device_info : devices) { + if (device_info.serial_number == serial_number_) { + device_id_ = device_info.device_id; + run_loop_.Quit(); + break; + } + } + } + + void OnDeviceAdded(const HidDeviceInfo& device_info) override { + if (device_info.serial_number == serial_number_) { + device_id_ = device_info.device_id; + run_loop_.Quit(); + } + } + + std::string serial_number_; + ScopedObserver<device::HidService, device::HidService::Observer> observer_; + base::RunLoop run_loop_; + HidDeviceId device_id_; +}; + class TestConnectCallback { public: TestConnectCallback() @@ -105,43 +154,11 @@ class HidConnectionTest : public testing::Test { ASSERT_TRUE(test_gadget_); ASSERT_TRUE(test_gadget_->SetType(UsbTestGadget::HID_ECHO)); - device_id_ = kInvalidHidDeviceId; - - base::RunLoop run_loop; - message_loop_->PostDelayedTask( - FROM_HERE, - base::Bind(&HidConnectionTest::FindDevice, - base::Unretained(this), run_loop.QuitClosure(), 5), - base::TimeDelta::FromMilliseconds(250)); - run_loop.Run(); - + DeviceCatcher device_catcher(test_gadget_->GetSerialNumber()); + device_id_ = device_catcher.WaitForDevice(); ASSERT_NE(device_id_, kInvalidHidDeviceId); } - void FindDevice(const base::Closure& done, int retries) { - std::vector<HidDeviceInfo> devices; - service_->GetDevices(&devices); - - for (std::vector<HidDeviceInfo>::iterator it = devices.begin(); - it != devices.end(); - ++it) { - if (it->serial_number == test_gadget_->GetSerialNumber()) { - device_id_ = it->device_id; - break; - } - } - - if (device_id_ == kInvalidHidDeviceId && --retries > 0) { - message_loop_->PostDelayedTask( - FROM_HERE, - base::Bind(&HidConnectionTest::FindDevice, base::Unretained(this), - done, retries), - base::TimeDelta::FromMilliseconds(10)); - } else { - message_loop_->PostTask(FROM_HERE, done); - } - } - scoped_ptr<base::MessageLoopForIO> message_loop_; HidService* service_; scoped_ptr<UsbTestGadget> test_gadget_; |