| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2014 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 <string> |
| 6 #include <vector> |
| 7 |
| 8 #include "base/bind.h" |
| 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/message_loop/message_loop.h" |
| 11 #include "device/hid/hid_connection.h" |
| 12 #include "device/hid/hid_service.h" |
| 13 #include "net/base/io_buffer.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 |
| 16 namespace device { |
| 17 |
| 18 namespace { |
| 19 |
| 20 const int kUSBLUFADemoVID = 0x03eb; |
| 21 const int kUSBLUFADemoPID = 0x204f; |
| 22 const uint64_t kReport = 0x0903a65d030f8ec9ULL; |
| 23 |
| 24 int g_read_times = 0; |
| 25 void Read(scoped_refptr<HidConnection> conn); |
| 26 |
| 27 void OnRead(scoped_refptr<HidConnection> conn, |
| 28 bool success, scoped_refptr<net::IOBuffer> buffer, size_t bytes) { |
| 29 if (success) { |
| 30 g_read_times++; |
| 31 EXPECT_EQ(8U, bytes); |
| 32 if (bytes == 8) { |
| 33 uint64_t* data = reinterpret_cast<uint64_t*>(buffer->data()); |
| 34 EXPECT_EQ(kReport, *data); |
| 35 } else { |
| 36 base::MessageLoop::current()->Quit(); |
| 37 } |
| 38 } |
| 39 |
| 40 if (g_read_times < 3){ |
| 41 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind(Read, conn)); |
| 42 } else { |
| 43 base::MessageLoop::current()->Quit(); |
| 44 } |
| 45 } |
| 46 |
| 47 void Read(scoped_refptr<HidConnection> conn) { |
| 48 conn->Read(base::Bind(OnRead, conn)); |
| 49 } |
| 50 |
| 51 } // namespace |
| 52 |
| 53 |
| 54 TEST(HidConnectionTest, Read) { |
| 55 base::MessageLoopForIO message_loop; |
| 56 HidService* service = HidService::GetInstance(); |
| 57 ASSERT_TRUE(service); |
| 58 |
| 59 std::vector<HidDeviceInfo> devices; |
| 60 service->GetDevices(&devices); |
| 61 std::string target_device; |
| 62 ASSERT_GT(devices.size(), 0U) << "No device found"; |
| 63 |
| 64 for (std::vector<HidDeviceInfo>::iterator it = devices.begin(); |
| 65 it != devices.end(); |
| 66 ++it) { |
| 67 if (it->vendor_id == kUSBLUFADemoVID && it->product_id == kUSBLUFADemoPID) { |
| 68 target_device = it->device_id; |
| 69 break; |
| 70 } |
| 71 } |
| 72 |
| 73 ASSERT_NE(std::string(""), target_device); |
| 74 |
| 75 scoped_refptr<HidConnection> connection = service->Connect(target_device); |
| 76 |
| 77 ASSERT_TRUE(connection); |
| 78 |
| 79 message_loop.PostTask(FROM_HERE, base::Bind(Read, connection)); |
| 80 message_loop.Run(); |
| 81 } |
| 82 |
| 83 } // namespace device |
| OLD | NEW |