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

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: Created 6 years, 4 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
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() : result_(false) {}
27 : callback_(base::Bind(&TestCompletionCallback::SetResult, 27 ~TestCompletionCallback() {}
28 base::Unretained(this))) {}
29 28
30 void SetResult(bool success, size_t size) { 29 void SetReadResult(bool success,
30 scoped_refptr<net::IOBuffer> buffer,
31 size_t size) {
31 result_ = success; 32 result_ = success;
32 transferred_ = size; 33 buffer_ = buffer;
34 size_ = size;
33 run_loop_.Quit(); 35 run_loop_.Quit();
34 } 36 }
35 37
38 void SetWriteResult(bool success) {
39 result_ = success;
40 run_loop_.Quit();
41 }
42
36 bool WaitForResult() { 43 bool WaitForResult() {
37 run_loop_.Run(); 44 run_loop_.Run();
38 return result_; 45 return result_;
39 } 46 }
40 47
41 const HidConnection::IOCallback& callback() const { return callback_; } 48 HidConnection::ReadCallback read_callback() {
Ken Rockot(use gerrit already) 2014/08/26 16:32:04 nit: It's not strictly an accessor so this naming
Reilly Grant (use Gerrit) 2014/08/26 17:17:22 Done.
42 size_t transferred() const { return transferred_; } 49 return base::Bind(&TestCompletionCallback::SetReadResult,
50 base::Unretained(this));
51 }
52
53 HidConnection::WriteCallback write_callback() {
54 return base::Bind(&TestCompletionCallback::SetWriteResult,
55 base::Unretained(this));
56 }
57
58 scoped_refptr<net::IOBuffer> buffer() const { return buffer_; }
59 size_t size() const { return size_; }
43 60
44 private: 61 private:
45 const HidConnection::IOCallback callback_;
46 base::RunLoop run_loop_; 62 base::RunLoop run_loop_;
47 bool result_; 63 bool result_;
48 size_t transferred_; 64 size_t size_;
65 scoped_refptr<net::IOBuffer> buffer_;
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

Powered by Google App Engine
This is Rietveld 408576698