| Index: device/hid/hid_connection_unittest.cc
|
| diff --git a/device/hid/hid_connection_unittest.cc b/device/hid/hid_connection_unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..c52c1686607dd534f7e907a96018172190303ba7
|
| --- /dev/null
|
| +++ b/device/hid/hid_connection_unittest.cc
|
| @@ -0,0 +1,83 @@
|
| +// Copyright (c) 2014 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 <string>
|
| +#include <vector>
|
| +
|
| +#include "base/bind.h"
|
| +#include "base/memory/scoped_ptr.h"
|
| +#include "base/message_loop/message_loop.h"
|
| +#include "device/hid/hid_connection.h"
|
| +#include "device/hid/hid_service.h"
|
| +#include "net/base/io_buffer.h"
|
| +#include "testing/gtest/include/gtest/gtest.h"
|
| +
|
| +namespace device {
|
| +
|
| +namespace {
|
| +
|
| +const int kUSBLUFADemoVID = 0x03eb;
|
| +const int kUSBLUFADemoPID = 0x204f;
|
| +const uint64_t kReport = 0x0903a65d030f8ec9ULL;
|
| +
|
| +int g_read_times = 0;
|
| +void Read(scoped_refptr<HidConnection> conn);
|
| +
|
| +void OnRead(scoped_refptr<HidConnection> conn,
|
| + bool success, scoped_refptr<net::IOBuffer> buffer, size_t bytes) {
|
| + if (success) {
|
| + g_read_times++;
|
| + EXPECT_EQ(8U, bytes);
|
| + if (bytes == 8) {
|
| + uint64_t* data = reinterpret_cast<uint64_t*>(buffer->data());
|
| + EXPECT_EQ(kReport, *data);
|
| + } else {
|
| + base::MessageLoop::current()->Quit();
|
| + }
|
| + }
|
| +
|
| + if (g_read_times < 3){
|
| + base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind(Read, conn));
|
| + } else {
|
| + base::MessageLoop::current()->Quit();
|
| + }
|
| +}
|
| +
|
| +void Read(scoped_refptr<HidConnection> conn) {
|
| + conn->Read(base::Bind(OnRead, conn));
|
| +}
|
| +
|
| +} // namespace
|
| +
|
| +
|
| +TEST(HidConnectionTest, Read) {
|
| + base::MessageLoopForIO message_loop;
|
| + HidService* service = HidService::GetInstance();
|
| + ASSERT_TRUE(service);
|
| +
|
| + std::vector<HidDeviceInfo> devices;
|
| + service->GetDevices(&devices);
|
| + std::string target_device;
|
| + ASSERT_GT(devices.size(), 0U) << "No device found";
|
| +
|
| + for (std::vector<HidDeviceInfo>::iterator it = devices.begin();
|
| + it != devices.end();
|
| + ++it) {
|
| + if (it->vendor_id == kUSBLUFADemoVID && it->product_id == kUSBLUFADemoPID) {
|
| + target_device = it->device_id;
|
| + break;
|
| + }
|
| + }
|
| +
|
| + ASSERT_NE(std::string(""), target_device);
|
| +
|
| + scoped_refptr<HidConnection> connection = service->Connect(target_device);
|
| +
|
| + ASSERT_TRUE(connection);
|
| +
|
| + message_loop.PostTask(FROM_HERE, base::Bind(Read, connection));
|
| + message_loop.Run();
|
| +}
|
| +
|
| +} // namespace device
|
|
|