| OLD | NEW | 
|---|
|  | (Empty) | 
| 1 // Copyright 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 // TODO(rockot): Enable tests once ChromeOS HID testing hardware is in place. |  | 
| 17 #if defined(OS_CHROMEOS) |  | 
| 18 #define MAYBE_Create DISABLED_Create |  | 
| 19 #define MAYBE_Read DISABLED_Read |  | 
| 20 #define MAYBE_Write DISABLED_Write |  | 
| 21 #else |  | 
| 22 #define MAYBE_Create Create |  | 
| 23 #define MAYBE_Read Read |  | 
| 24 #define MAYBE_Write Write |  | 
| 25 #endif |  | 
| 26 |  | 
| 27 namespace device { |  | 
| 28 |  | 
| 29 namespace { |  | 
| 30 |  | 
| 31 using net::IOBuffer; |  | 
| 32 |  | 
| 33 const int kUSBLUFADemoVID = 0x03eb; |  | 
| 34 const int kUSBLUFADemoPID = 0x204f; |  | 
| 35 const uint64_t kReport = 0x0903a65d030f8ec9ULL; |  | 
| 36 |  | 
| 37 int g_read_times = 0; |  | 
| 38 void Read(scoped_refptr<HidConnection> conn); |  | 
| 39 |  | 
| 40 void OnRead(scoped_refptr<HidConnection> conn, |  | 
| 41             scoped_refptr<net::IOBuffer> buffer, |  | 
| 42             bool success, |  | 
| 43             size_t bytes) { |  | 
| 44   EXPECT_TRUE(success); |  | 
| 45   if (success) { |  | 
| 46     g_read_times++; |  | 
| 47     EXPECT_EQ(8U, bytes); |  | 
| 48     if (bytes == 8) { |  | 
| 49       uint64_t* data = reinterpret_cast<uint64_t*>(buffer->data()); |  | 
| 50       EXPECT_EQ(kReport, *data); |  | 
| 51     } else { |  | 
| 52       base::MessageLoop::current()->Quit(); |  | 
| 53     } |  | 
| 54   } else { |  | 
| 55     LOG(ERROR) << "~"; |  | 
| 56     g_read_times++; |  | 
| 57   } |  | 
| 58 |  | 
| 59   if (g_read_times < 3){ |  | 
| 60     base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind(Read, conn)); |  | 
| 61   } else { |  | 
| 62     base::MessageLoop::current()->Quit(); |  | 
| 63   } |  | 
| 64 } |  | 
| 65 |  | 
| 66 void Read(scoped_refptr<HidConnection> conn) { |  | 
| 67   scoped_refptr<IOBuffer> buffer(new IOBuffer(8)); |  | 
| 68   conn->Read(buffer, 8, base::Bind(OnRead, conn, buffer)); |  | 
| 69 } |  | 
| 70 |  | 
| 71 void OnWriteNormal(bool success, |  | 
| 72                    size_t bytes) { |  | 
| 73   ASSERT_TRUE(success); |  | 
| 74   base::MessageLoop::current()->Quit(); |  | 
| 75 } |  | 
| 76 |  | 
| 77 void WriteNormal(scoped_refptr<HidConnection> conn) { |  | 
| 78   scoped_refptr<IOBuffer> buffer(new IOBuffer(8)); |  | 
| 79   *(int64_t*)buffer->data() = kReport; |  | 
| 80 |  | 
| 81   conn->Write(buffer, 8, base::Bind(OnWriteNormal)); |  | 
| 82 } |  | 
| 83 |  | 
| 84 }  // namespace |  | 
| 85 |  | 
| 86 class HidConnectionTest : public testing::Test { |  | 
| 87  protected: |  | 
| 88   virtual void SetUp() OVERRIDE { |  | 
| 89     message_loop_.reset(new base::MessageLoopForIO()); |  | 
| 90     service_.reset(HidService::CreateInstance()); |  | 
| 91     ASSERT_TRUE(service_); |  | 
| 92 |  | 
| 93     std::vector<HidDeviceInfo> devices; |  | 
| 94     service_->GetDevices(&devices); |  | 
| 95     ASSERT_GT(devices.size(), 0U) << "No device found"; |  | 
| 96     for (std::vector<HidDeviceInfo>::iterator it = devices.begin(); |  | 
| 97         it != devices.end(); |  | 
| 98         ++it) { |  | 
| 99       if (it->vendor_id == kUSBLUFADemoVID && |  | 
| 100           it->product_id == kUSBLUFADemoPID) { |  | 
| 101         device_id_ = it->device_id; |  | 
| 102         return; |  | 
| 103       } |  | 
| 104     } |  | 
| 105     ASSERT_TRUE(false) << "Failed to find target device"; |  | 
| 106   } |  | 
| 107 |  | 
| 108   virtual void TearDown() OVERRIDE { |  | 
| 109     service_.reset(NULL); |  | 
| 110     message_loop_.reset(NULL); |  | 
| 111   } |  | 
| 112 |  | 
| 113   std::string device_id_; |  | 
| 114   scoped_ptr<base::MessageLoopForIO> message_loop_; |  | 
| 115   scoped_ptr<HidService> service_; |  | 
| 116 }; |  | 
| 117 |  | 
| 118 TEST_F(HidConnectionTest, MAYBE_Create) { |  | 
| 119   scoped_refptr<HidConnection> connection = service_->Connect(device_id_); |  | 
| 120   ASSERT_TRUE(connection); |  | 
| 121 } |  | 
| 122 |  | 
| 123 TEST_F(HidConnectionTest, MAYBE_Read) { |  | 
| 124   scoped_refptr<HidConnection> connection = service_->Connect(device_id_); |  | 
| 125 |  | 
| 126   ASSERT_TRUE(connection); |  | 
| 127   message_loop_->PostTask(FROM_HERE, base::Bind(Read, connection)); |  | 
| 128   message_loop_->Run(); |  | 
| 129 } |  | 
| 130 |  | 
| 131 TEST_F(HidConnectionTest, MAYBE_Write) { |  | 
| 132   scoped_refptr<HidConnection> connection = service_->Connect(device_id_); |  | 
| 133 |  | 
| 134   ASSERT_TRUE(connection); |  | 
| 135   message_loop_->PostTask(FROM_HERE, base::Bind(WriteNormal, connection)); |  | 
| 136   message_loop_->Run(); |  | 
| 137 } |  | 
| 138 |  | 
| 139 }  // namespace device |  | 
| OLD | NEW | 
|---|