Index: device/u2f/u2f_hid_device_unittest.cc |
diff --git a/device/u2f/u2f_hid_device_unittest.cc b/device/u2f/u2f_hid_device_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..37673507cc2a575b03cc5143d2b9bd0792e11469 |
--- /dev/null |
+++ b/device/u2f/u2f_hid_device_unittest.cc |
@@ -0,0 +1,120 @@ |
+// Copyright 2017 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include <list> |
+ |
+#include "base/bind.h" |
+#include "base/memory/ptr_util.h" |
+#include "base/run_loop.h" |
+#include "base/test/test_io_thread.h" |
+#include "device/hid/hid_device_filter.h" |
+#include "device/test/test_device_client.h" |
+#include "device/u2f/u2f_hid_device.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace device { |
+ |
+class U2fDeviceEnumerate { |
+ public: |
+ U2fDeviceEnumerate() |
+ : closure_(), |
+ callback_(base::Bind(&U2fDeviceEnumerate::ReceivedCallback, |
+ base::Unretained(this))), |
+ run_loop_() {} |
+ ~U2fDeviceEnumerate() {} |
+ |
+ void ReceivedCallback( |
+ const std::vector<scoped_refptr<HidDeviceInfo>>& devices) { |
+ std::list<std::unique_ptr<U2fDevice>> u2f_devices; |
+ filter_.SetUsagePage(0xf1d0); |
+ for (auto device_info : devices) { |
+ if (filter_.Matches(device_info)) { |
+ u2f_devices.push_front(base::MakeUnique<U2fHidDevice>(device_info)); |
+ } |
Reilly Grant (use Gerrit)
2017/03/01 22:48:33
No braces around single-line if.
Casey Piper
2017/03/04 02:06:28
Done.
|
+ } |
+ devices_ = std::move(u2f_devices); |
+ closure_.Run(); |
+ } |
+ |
+ std::list<std::unique_ptr<U2fDevice>>& WaitForCallback() { |
+ closure_ = run_loop_.QuitClosure(); |
+ run_loop_.Run(); |
+ return devices_; |
+ } |
+ |
+ const HidService::GetDevicesCallback& callback() { return callback_; } |
+ |
+ private: |
+ HidDeviceFilter filter_; |
+ std::list<std::unique_ptr<U2fDevice>> devices_; |
+ base::Closure closure_; |
+ HidService::GetDevicesCallback callback_; |
+ base::RunLoop run_loop_; |
+}; |
+ |
+class TestVersionCallback { |
+ public: |
+ TestVersionCallback() |
+ : closure_(), |
+ callback_(base::Bind(&TestVersionCallback::ReceivedCallback, |
+ base::Unretained(this))), |
+ run_loop_() {} |
+ ~TestVersionCallback() {} |
+ |
+ void ReceivedCallback(bool success, U2fDevice::ProtocolVersion version) { |
+ version_ = version; |
+ closure_.Run(); |
+ } |
+ |
+ U2fDevice::ProtocolVersion WaitForCallback() { |
+ closure_ = run_loop_.QuitClosure(); |
+ run_loop_.Run(); |
+ return version_; |
+ } |
+ |
+ const U2fDevice::VersionCallback& callback() { return callback_; } |
+ |
+ private: |
+ U2fDevice::ProtocolVersion version_; |
+ base::Closure closure_; |
+ U2fDevice::VersionCallback callback_; |
+ base::RunLoop run_loop_; |
+}; |
+ |
+class U2fHidDeviceTest : public testing::Test { |
+ public: |
+ void SetUp() override { |
+ if (!U2fHidDevice::IsTestEnabled()) |
+ return; |
+ message_loop_.reset(new base::MessageLoopForUI()); |
+ io_thread_.reset(new base::TestIOThread(base::TestIOThread::kAutoStart)); |
+ device_client_.reset( |
+ new device::TestDeviceClient(io_thread_->task_runner())); |
+ } |
+ |
+ protected: |
+ std::unique_ptr<base::MessageLoopForUI> message_loop_; |
+ std::unique_ptr<base::TestIOThread> io_thread_; |
+ std::unique_ptr<device::TestDeviceClient> device_client_; |
+}; |
+ |
+TEST_F(U2fHidDeviceTest, TestEnumerateHidDevice) { |
+ if (!U2fHidDevice::IsTestEnabled()) |
+ return; |
+ |
+ U2fDeviceEnumerate callback; |
+ HidService* hid_service = DeviceClient::Get()->GetHidService(); |
+ hid_service->GetDevices(callback.callback()); |
+ std::list<std::unique_ptr<U2fDevice>>& u2f_devices = |
+ callback.WaitForCallback(); |
+ |
+ for (auto it = u2f_devices.cbegin(); it != u2f_devices.end(); ++it) { |
Reilly Grant (use Gerrit)
2017/03/01 22:48:33
for (auto& device : u2f_devices)
Casey Piper
2017/03/04 02:06:28
Done.
|
+ TestVersionCallback vc; |
+ (*it)->Version(vc.callback()); |
+ U2fDevice::ProtocolVersion version = vc.WaitForCallback(); |
+ ASSERT_EQ(version, U2fDevice::ProtocolVersion::U2F_V2); |
Reilly Grant (use Gerrit)
2017/03/01 22:48:33
EXPECT_EQ since this if this fails the test won't
|
+ } |
+}; |
+ |
+} // namespace device |