Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(587)

Side by Side Diff: device/hid/hid_connection_unittest.cc

Issue 499713002: Don't pass buffers to HidConnection::Read because it knows the size. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed rockot@'s nits. Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « device/hid/hid_connection_mac.cc ('k') | device/hid/hid_connection_win.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/callback.h" 9 #include "base/callback.h"
10 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
11 #include "base/run_loop.h" 11 #include "base/run_loop.h"
12 #include "device/hid/hid_connection.h" 12 #include "device/hid/hid_connection.h"
13 #include "device/hid/hid_service.h" 13 #include "device/hid/hid_service.h"
14 #include "device/test/usb_test_gadget.h" 14 #include "device/test/usb_test_gadget.h"
15 #include "net/base/io_buffer.h" 15 #include "net/base/io_buffer.h"
16 #include "testing/gtest/include/gtest/gtest.h" 16 #include "testing/gtest/include/gtest/gtest.h"
17 17
18 namespace device { 18 namespace device {
19 19
20 namespace { 20 namespace {
21 21
22 using net::IOBufferWithSize; 22 using net::IOBufferWithSize;
23 23
24 class TestCompletionCallback { 24 class TestCompletionCallback {
25 public: 25 public:
26 TestCompletionCallback() 26 TestCompletionCallback()
27 : callback_(base::Bind(&TestCompletionCallback::SetResult, 27 : read_callback_(base::Bind(&TestCompletionCallback::SetReadResult,
28 base::Unretained(this))) {} 28 base::Unretained(this))),
29 write_callback_(base::Bind(&TestCompletionCallback::SetWriteResult,
30 base::Unretained(this))) {}
31 ~TestCompletionCallback() {}
29 32
30 void SetResult(bool success, size_t size) { 33 void SetReadResult(bool success,
34 scoped_refptr<net::IOBuffer> buffer,
35 size_t size) {
31 result_ = success; 36 result_ = success;
32 transferred_ = size; 37 buffer_ = buffer;
38 size_ = size;
33 run_loop_.Quit(); 39 run_loop_.Quit();
34 } 40 }
35 41
42 void SetWriteResult(bool success) {
43 result_ = success;
44 run_loop_.Quit();
45 }
46
36 bool WaitForResult() { 47 bool WaitForResult() {
37 run_loop_.Run(); 48 run_loop_.Run();
38 return result_; 49 return result_;
39 } 50 }
40 51
41 const HidConnection::IOCallback& callback() const { return callback_; } 52 const HidConnection::ReadCallback& read_callback() { return read_callback_; }
42 size_t transferred() const { return transferred_; } 53 const HidConnection::WriteCallback write_callback() {
54 return write_callback_;
55 }
56 scoped_refptr<net::IOBuffer> buffer() const { return buffer_; }
57 size_t size() const { return size_; }
43 58
44 private: 59 private:
45 const HidConnection::IOCallback callback_;
46 base::RunLoop run_loop_; 60 base::RunLoop run_loop_;
47 bool result_; 61 bool result_;
48 size_t transferred_; 62 size_t size_;
63 scoped_refptr<net::IOBuffer> buffer_;
64 HidConnection::ReadCallback read_callback_;
65 HidConnection::WriteCallback write_callback_;
49 }; 66 };
50 67
51 } // namespace 68 } // namespace
52 69
53 class HidConnectionTest : public testing::Test { 70 class HidConnectionTest : public testing::Test {
54 protected: 71 protected:
55 virtual void SetUp() OVERRIDE { 72 virtual void SetUp() OVERRIDE {
56 if (!UsbTestGadget::IsTestEnabled()) return; 73 if (!UsbTestGadget::IsTestEnabled()) return;
57 74
58 message_loop_.reset(new base::MessageLoopForIO()); 75 message_loop_.reset(new base::MessageLoopForIO());
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
106 HidDeviceId device_id_; 123 HidDeviceId device_id_;
107 }; 124 };
108 125
109 TEST_F(HidConnectionTest, ReadWrite) { 126 TEST_F(HidConnectionTest, ReadWrite) {
110 if (!UsbTestGadget::IsTestEnabled()) return; 127 if (!UsbTestGadget::IsTestEnabled()) return;
111 128
112 scoped_refptr<HidConnection> conn = service_->Connect(device_id_); 129 scoped_refptr<HidConnection> conn = service_->Connect(device_id_);
113 ASSERT_TRUE(conn); 130 ASSERT_TRUE(conn);
114 131
115 for (int i = 0; i < 8; ++i) { 132 for (int i = 0; i < 8; ++i) {
116 scoped_refptr<IOBufferWithSize> write_buffer(new IOBufferWithSize(8)); 133 scoped_refptr<IOBufferWithSize> buffer(new IOBufferWithSize(9));
117 *(int64_t*)write_buffer->data() = i; 134 buffer->data()[0] = 0;
135 for (int j = 1; j < buffer->size(); ++j) {
136 buffer->data()[j] = i + j - 1;
137 }
118 138
119 TestCompletionCallback write_callback; 139 TestCompletionCallback write_callback;
120 conn->Write(0, write_buffer, write_callback.callback()); 140 conn->Write(buffer, buffer->size(), write_callback.write_callback());
121 ASSERT_TRUE(write_callback.WaitForResult()); 141 ASSERT_TRUE(write_callback.WaitForResult());
122 ASSERT_EQ(8UL, write_callback.transferred());
123 142
124 scoped_refptr<IOBufferWithSize> read_buffer(new IOBufferWithSize(8));
125 TestCompletionCallback read_callback; 143 TestCompletionCallback read_callback;
126 conn->Read(read_buffer, read_callback.callback()); 144 conn->Read(read_callback.read_callback());
127 ASSERT_TRUE(read_callback.WaitForResult()); 145 ASSERT_TRUE(read_callback.WaitForResult());
128 ASSERT_EQ(8UL, read_callback.transferred()); 146 ASSERT_EQ(9UL, read_callback.size());
129 ASSERT_EQ(i, *(int64_t*)read_buffer->data()); 147 ASSERT_EQ(0, read_callback.buffer()->data()[0]);
148 for (int j = 1; j < buffer->size(); ++j) {
149 ASSERT_EQ(i + j - 1, read_callback.buffer()->data()[j]);
150 }
130 } 151 }
131 } 152 }
132 153
133 } // namespace device 154 } // namespace device
OLDNEW
« no previous file with comments | « device/hid/hid_connection_mac.cc ('k') | device/hid/hid_connection_win.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698