OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <list> |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/run_loop.h" |
| 9 #include "base/test/test_io_thread.h" |
| 10 #include "device/base/mock_device_client.h" |
| 11 #include "device/hid/mock_hid_service.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 #include "u2f_enumerate.h" |
| 14 #include "u2f_hid_device.h" |
| 15 |
| 16 namespace { |
| 17 #if defined(OS_MACOSX) |
| 18 const uint64_t kTestDeviceId = 42; |
| 19 #else |
| 20 const char* kTestDeviceId = "device"; |
| 21 #endif |
| 22 } // namespace |
| 23 |
| 24 namespace device { |
| 25 |
| 26 class TestEnumerateCallback : HidService::Observer { |
| 27 public: |
| 28 TestEnumerateCallback() |
| 29 : closure_(), |
| 30 callback_(base::Bind(&TestEnumerateCallback::ReceivedCallback, |
| 31 base::Unretained(this))), |
| 32 run_loop_() {} |
| 33 ~TestEnumerateCallback() {} |
| 34 |
| 35 void ReceivedCallback(std::list<std::unique_ptr<U2fDevice>> devices) { |
| 36 devices_ = std::move(devices); |
| 37 closure_.Run(); |
| 38 } |
| 39 |
| 40 std::list<std::unique_ptr<U2fDevice>>& WaitForCallback() { |
| 41 closure_ = run_loop_.QuitClosure(); |
| 42 run_loop_.Run(); |
| 43 return devices_; |
| 44 } |
| 45 |
| 46 U2fEnumerate::U2fEnumerateCallback& callback() { return callback_; } |
| 47 |
| 48 private: |
| 49 std::list<std::unique_ptr<U2fDevice>> devices_; |
| 50 base::Closure closure_; |
| 51 U2fEnumerate::U2fEnumerateCallback callback_; |
| 52 base::RunLoop run_loop_; |
| 53 }; |
| 54 |
| 55 class U2fEnumerateTest : public testing::Test { |
| 56 public: |
| 57 void SetUp() override { |
| 58 message_loop_.reset(new base::MessageLoopForUI()); |
| 59 io_thread_.reset(new base::TestIOThread(base::TestIOThread::kAutoStart)); |
| 60 device_client_.reset(new MockDeviceClient()); |
| 61 // Add one FIDO device and one non-FIDO device |
| 62 HidCollectionInfo c_info; |
| 63 c_info.usage = |
| 64 HidUsageAndPage(1, static_cast<HidUsageAndPage::Page>(0xf1d0)); |
| 65 auto device0 = make_scoped_refptr<HidDeviceInfo>( |
| 66 new HidDeviceInfo(kTestDeviceId, 0, 0, "Test Fido Device", "123FIDO", |
| 67 kHIDBusTypeUSB, c_info, 64, 64, 0)); |
| 68 device_client_->hid_service()->AddDevice(device0); |
| 69 device_client_->hid_service()->FirstEnumerationComplete(); |
| 70 } |
| 71 |
| 72 protected: |
| 73 std::unique_ptr<base::MessageLoopForUI> message_loop_; |
| 74 std::unique_ptr<base::TestIOThread> io_thread_; |
| 75 std::unique_ptr<device::MockDeviceClient> device_client_; |
| 76 }; |
| 77 |
| 78 TEST_F(U2fEnumerateTest, TestEnumerateHidDevice) { |
| 79 device::U2fEnumerate e; |
| 80 TestEnumerateCallback callback; |
| 81 e.EnumerateU2fDevices(std::move(callback.callback())); |
| 82 std::list<std::unique_ptr<U2fDevice>>& u2f_devices = |
| 83 callback.WaitForCallback(); |
| 84 EXPECT_EQ(static_cast<size_t>(1), u2f_devices.size()); |
| 85 }; |
| 86 |
| 87 } // namespace device |
OLD | NEW |