| OLD | NEW | 
|    1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. |    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 |    2 // Use of this source code is governed by a BSD-style license that can be | 
|    3 // found in the LICENSE file. |    3 // found in the LICENSE file. | 
|    4  |    4  | 
|    5 #include <string> |    5 #include <string> | 
|    6 #include <vector> |    6 #include <vector> | 
|    7  |    7  | 
|    8 #include "base/bind.h" |    8 #include "base/bind.h" | 
|    9 #include "base/memory/scoped_ptr.h" |    9 #include "base/memory/scoped_ptr.h" | 
|   10 #include "base/message_loop/message_loop.h" |   10 #include "base/message_loop/message_loop.h" | 
|   11 #include "device/hid/hid_connection.h" |   11 #include "device/hid/hid_connection.h" | 
|   12 #include "device/hid/hid_service.h" |   12 #include "device/hid/hid_service.h" | 
|   13 #include "net/base/io_buffer.h" |   13 #include "net/base/io_buffer.h" | 
|   14 #include "testing/gtest/include/gtest/gtest.h" |   14 #include "testing/gtest/include/gtest/gtest.h" | 
|   15  |   15  | 
|   16 namespace device { |   16 namespace device { | 
|   17  |   17  | 
|   18 namespace { |   18 namespace { | 
|   19  |   19  | 
|   20 using net::IOBuffer; |   20 using net::IOBufferWithSize; | 
|   21  |   21  | 
|   22 const int kUSBLUFADemoVID = 0x03eb; |   22 const int kUSBLUFADemoVID = 0x03eb; | 
|   23 const int kUSBLUFADemoPID = 0x204f; |   23 const int kUSBLUFADemoPID = 0x204f; | 
|   24 const uint64_t kReport = 0x0903a65d030f8ec9ULL; |   24 const uint64_t kReport = 0x0903a65d030f8ec9ULL; | 
|   25  |   25  | 
|   26 int g_read_times = 0; |   26 int g_read_times = 0; | 
|   27 void Read(scoped_refptr<HidConnection> conn); |   27 void Read(scoped_refptr<HidConnection> conn); | 
|   28  |   28  | 
|   29 void OnRead(scoped_refptr<HidConnection> conn, |   29 void OnRead(scoped_refptr<HidConnection> conn, | 
|   30             scoped_refptr<net::IOBuffer> buffer, |   30             scoped_refptr<IOBufferWithSize> buffer, | 
|   31             bool success, |   31             bool success, | 
|   32             size_t bytes) { |   32             size_t bytes) { | 
|   33   EXPECT_TRUE(success); |   33   EXPECT_TRUE(success); | 
|   34   if (success) { |   34   if (success) { | 
|   35     g_read_times++; |   35     g_read_times++; | 
|   36     EXPECT_EQ(8U, bytes); |   36     EXPECT_EQ(8U, bytes); | 
|   37     if (bytes == 8) { |   37     if (bytes == 8) { | 
|   38       uint64_t* data = reinterpret_cast<uint64_t*>(buffer->data()); |   38       uint64_t* data = reinterpret_cast<uint64_t*>(buffer->data()); | 
|   39       EXPECT_EQ(kReport, *data); |   39       EXPECT_EQ(kReport, *data); | 
|   40     } else { |   40     } else { | 
|   41       base::MessageLoop::current()->Quit(); |   41       base::MessageLoop::current()->Quit(); | 
|   42     } |   42     } | 
|   43   } else { |   43   } else { | 
|   44     LOG(ERROR) << "~"; |   44     LOG(ERROR) << "~"; | 
|   45     g_read_times++; |   45     g_read_times++; | 
|   46   } |   46   } | 
|   47  |   47  | 
|   48   if (g_read_times < 3){ |   48   if (g_read_times < 3){ | 
|   49     base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind(Read, conn)); |   49     base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind(Read, conn)); | 
|   50   } else { |   50   } else { | 
|   51     base::MessageLoop::current()->Quit(); |   51     base::MessageLoop::current()->Quit(); | 
|   52   } |   52   } | 
|   53 } |   53 } | 
|   54  |   54  | 
|   55 void Read(scoped_refptr<HidConnection> conn) { |   55 void Read(scoped_refptr<HidConnection> conn) { | 
|   56   scoped_refptr<IOBuffer> buffer(new IOBuffer(8)); |   56   scoped_refptr<IOBufferWithSize> buffer(new IOBufferWithSize(8)); | 
|   57   conn->Read(buffer, 8, base::Bind(OnRead, conn, buffer)); |   57   conn->Read(buffer, base::Bind(OnRead, conn, buffer)); | 
|   58 } |   58 } | 
|   59  |   59  | 
|   60 void OnWriteNormal(bool success, |   60 void OnWriteNormal(bool success, | 
|   61                    size_t bytes) { |   61                    size_t bytes) { | 
|   62   ASSERT_TRUE(success); |   62   ASSERT_TRUE(success); | 
|   63   base::MessageLoop::current()->Quit(); |   63   base::MessageLoop::current()->Quit(); | 
|   64 } |   64 } | 
|   65  |   65  | 
|   66 void WriteNormal(scoped_refptr<HidConnection> conn) { |   66 void WriteNormal(scoped_refptr<HidConnection> conn) { | 
|   67   scoped_refptr<IOBuffer> buffer(new IOBuffer(8)); |   67   scoped_refptr<IOBufferWithSize> buffer(new IOBufferWithSize(8)); | 
|   68   *(int64_t*)buffer->data() = kReport; |   68   *(int64_t*)buffer->data() = kReport; | 
|   69  |   69  | 
|   70   conn->Write(buffer, 8, base::Bind(OnWriteNormal)); |   70   conn->Write(0, buffer, base::Bind(OnWriteNormal)); | 
|   71 } |   71 } | 
|   72  |   72  | 
|   73 }  // namespace |   73 }  // namespace | 
|   74  |   74  | 
|   75 class HidConnectionTest : public testing::Test { |   75 class HidConnectionTest : public testing::Test { | 
|   76  protected: |   76  protected: | 
|   77   virtual void SetUp() OVERRIDE { |   77   virtual void SetUp() OVERRIDE { | 
|   78     message_loop_.reset(new base::MessageLoopForIO()); |   78     message_loop_.reset(new base::MessageLoopForIO()); | 
|   79     service_.reset(HidService::CreateInstance()); |   79     service_.reset(HidService::CreateInstance()); | 
|   80     ASSERT_TRUE(service_); |   80     ASSERT_TRUE(service_); | 
|   81  |   81  | 
|   82     std::vector<HidDeviceInfo> devices; |   82     std::vector<HidDeviceInfo> devices; | 
|   83     service_->GetDevices(&devices); |   83     service_->GetDevices(&devices); | 
 |   84     device_id_ = kInvalidHidDeviceId; | 
|   84     for (std::vector<HidDeviceInfo>::iterator it = devices.begin(); |   85     for (std::vector<HidDeviceInfo>::iterator it = devices.begin(); | 
|   85         it != devices.end(); |   86         it != devices.end(); | 
|   86         ++it) { |   87         ++it) { | 
|   87       if (it->vendor_id == kUSBLUFADemoVID && |   88       if (it->vendor_id == kUSBLUFADemoVID && | 
|   88           it->product_id == kUSBLUFADemoPID) { |   89           it->product_id == kUSBLUFADemoPID) { | 
|   89         device_id_ = it->device_id; |   90         device_id_ = it->device_id; | 
|   90         return; |   91         return; | 
|   91       } |   92       } | 
|   92     } |   93     } | 
|   93   } |   94   } | 
|   94  |   95  | 
|   95   virtual void TearDown() OVERRIDE { |   96   virtual void TearDown() OVERRIDE { | 
|   96     service_.reset(NULL); |   97     service_.reset(NULL); | 
|   97     message_loop_.reset(NULL); |   98     message_loop_.reset(NULL); | 
|   98   } |   99   } | 
|   99  |  100  | 
|  100   std::string device_id_; |  101   HidDeviceId device_id_; | 
|  101   scoped_ptr<base::MessageLoopForIO> message_loop_; |  102   scoped_ptr<base::MessageLoopForIO> message_loop_; | 
|  102   scoped_ptr<HidService> service_; |  103   scoped_ptr<HidService> service_; | 
|  103 }; |  104 }; | 
|  104  |  105  | 
|  105 TEST_F(HidConnectionTest, Create) { |  106 TEST_F(HidConnectionTest, Create) { | 
|  106   scoped_refptr<HidConnection> connection = service_->Connect(device_id_); |  107   scoped_refptr<HidConnection> connection = service_->Connect(device_id_); | 
|  107   ASSERT_TRUE(connection || device_id_.empty()); |  108   ASSERT_TRUE(connection || device_id_ == kInvalidHidDeviceId); | 
|  108 } |  109 } | 
|  109  |  110  | 
|  110 TEST_F(HidConnectionTest, Read) { |  111 TEST_F(HidConnectionTest, Read) { | 
|  111   scoped_refptr<HidConnection> connection = service_->Connect(device_id_); |  112   scoped_refptr<HidConnection> connection = service_->Connect(device_id_); | 
|  112  |  113   if (connection) { | 
|  113   if (!device_id_.empty()) { |  | 
|  114     ASSERT_TRUE(connection); |  | 
|  115     message_loop_->PostTask(FROM_HERE, base::Bind(Read, connection)); |  114     message_loop_->PostTask(FROM_HERE, base::Bind(Read, connection)); | 
|  116     message_loop_->Run(); |  115     message_loop_->Run(); | 
|  117   } |  116   } | 
|  118 } |  117 } | 
|  119  |  118  | 
|  120 TEST_F(HidConnectionTest, Write) { |  119 TEST_F(HidConnectionTest, Write) { | 
|  121   scoped_refptr<HidConnection> connection = service_->Connect(device_id_); |  120   scoped_refptr<HidConnection> connection = service_->Connect(device_id_); | 
|  122  |  121  | 
|  123   if (!device_id_.empty()) { |  122   if (connection) { | 
|  124     ASSERT_TRUE(connection); |  | 
|  125     message_loop_->PostTask(FROM_HERE, base::Bind(WriteNormal, connection)); |  123     message_loop_->PostTask(FROM_HERE, base::Bind(WriteNormal, connection)); | 
|  126     message_loop_->Run(); |  124     message_loop_->Run(); | 
|  127   } |  125   } | 
|  128 } |  126 } | 
|  129  |  127  | 
|  130 }  // namespace device |  128 }  // namespace device | 
| OLD | NEW |