Index: device/u2f/u2f_enumerate_unittest.cc |
diff --git a/device/u2f/u2f_enumerate_unittest.cc b/device/u2f/u2f_enumerate_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..e34dbda1152348c4ab1dd88bc3e7b603f98a5a5a |
--- /dev/null |
+++ b/device/u2f/u2f_enumerate_unittest.cc |
@@ -0,0 +1,87 @@ |
+// 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/run_loop.h" |
+#include "base/test/test_io_thread.h" |
+#include "device/base/mock_device_client.h" |
+#include "device/hid/mock_hid_service.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+#include "u2f_enumerate.h" |
+#include "u2f_hid_device.h" |
+ |
+namespace { |
+#if defined(OS_MACOSX) |
+const uint64_t kTestDeviceId = 42; |
+#else |
+const char* kTestDeviceId = "device"; |
+#endif |
+} // namespace |
+ |
+namespace device { |
+ |
+class TestEnumerateCallback : HidService::Observer { |
+ public: |
+ TestEnumerateCallback() |
+ : closure_(), |
+ callback_(base::Bind(&TestEnumerateCallback::ReceivedCallback, |
+ base::Unretained(this))), |
+ run_loop_() {} |
+ ~TestEnumerateCallback() {} |
+ |
+ void ReceivedCallback(std::list<std::unique_ptr<U2fDevice>> devices) { |
+ devices_ = std::move(devices); |
+ closure_.Run(); |
+ } |
+ |
+ std::list<std::unique_ptr<U2fDevice>>& WaitForCallback() { |
+ closure_ = run_loop_.QuitClosure(); |
+ run_loop_.Run(); |
+ return devices_; |
+ } |
+ |
+ U2fEnumerate::U2fEnumerateCallback& callback() { return callback_; } |
+ |
+ private: |
+ std::list<std::unique_ptr<U2fDevice>> devices_; |
+ base::Closure closure_; |
+ U2fEnumerate::U2fEnumerateCallback callback_; |
+ base::RunLoop run_loop_; |
+}; |
+ |
+class U2fEnumerateTest : public testing::Test { |
+ public: |
+ void SetUp() override { |
+ message_loop_.reset(new base::MessageLoopForUI()); |
+ io_thread_.reset(new base::TestIOThread(base::TestIOThread::kAutoStart)); |
+ device_client_.reset(new MockDeviceClient()); |
+ // Add one FIDO device and one non-FIDO device |
+ HidCollectionInfo c_info; |
+ c_info.usage = |
+ HidUsageAndPage(1, static_cast<HidUsageAndPage::Page>(0xf1d0)); |
+ auto device0 = make_scoped_refptr<HidDeviceInfo>( |
+ new HidDeviceInfo(kTestDeviceId, 0, 0, "Test Fido Device", "123FIDO", |
+ kHIDBusTypeUSB, c_info, 64, 64, 0)); |
+ device_client_->hid_service()->AddDevice(device0); |
+ device_client_->hid_service()->FirstEnumerationComplete(); |
+ } |
+ |
+ protected: |
+ std::unique_ptr<base::MessageLoopForUI> message_loop_; |
+ std::unique_ptr<base::TestIOThread> io_thread_; |
+ std::unique_ptr<device::MockDeviceClient> device_client_; |
+}; |
+ |
+TEST_F(U2fEnumerateTest, TestEnumerateHidDevice) { |
+ device::U2fEnumerate e; |
+ TestEnumerateCallback callback; |
+ e.EnumerateU2fDevices(std::move(callback.callback())); |
+ std::list<std::unique_ptr<U2fDevice>>& u2f_devices = |
+ callback.WaitForCallback(); |
+ EXPECT_EQ(static_cast<size_t>(1), u2f_devices.size()); |
+}; |
+ |
+} // namespace device |