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

Side by Side Diff: chrome/browser/extensions/api/serial/serial_apitest.cc

Issue 10827123: Add serial bulk reads. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Merge help. Created 8 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 <deque>
5 #include <string> 6 #include <string>
6 #include <vector> 7 #include <vector>
7 8
8 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
9 #include "chrome/browser/extensions/api/api_resource_event_notifier.h" 10 #include "chrome/browser/extensions/api/api_resource_event_notifier.h"
10 #include "chrome/browser/extensions/api/serial/serial_api.h" 11 #include "chrome/browser/extensions/api/serial/serial_api.h"
11 #include "chrome/browser/extensions/api/serial/serial_connection.h" 12 #include "chrome/browser/extensions/api/serial/serial_connection.h"
12 #include "chrome/browser/extensions/extension_apitest.h" 13 #include "chrome/browser/extensions/extension_apitest.h"
13 #include "chrome/browser/extensions/extension_function.h" 14 #include "chrome/browser/extensions/extension_function.h"
14 #include "chrome/browser/extensions/extension_function_test_utils.h" 15 #include "chrome/browser/extensions/extension_function_test_utils.h"
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
67 opened_ = true; 68 opened_ = true;
68 return true; 69 return true;
69 } 70 }
70 71
71 virtual void Close() { 72 virtual void Close() {
72 DCHECK(opened_); 73 DCHECK(opened_);
73 } 74 }
74 75
75 virtual void Flush() { 76 virtual void Flush() {
76 DCHECK(opened_); 77 DCHECK(opened_);
77 read_index_ = write_index_ = 0; 78 buffer_.clear();
78 } 79 }
79 80
80 virtual int Read(uint8* byte) { 81 virtual int Read(scoped_refptr<net::IOBufferWithSize> io_buffer) {
81 DCHECK(byte); 82 DCHECK(io_buffer->data());
82 83
83 if (read_index_ >= write_index_) { 84 if (buffer_.empty()) {
84 return 0; 85 return 0;
85 } 86 }
86 *byte = ring_buffer_[read_index_++]; 87 char *data = io_buffer->data();
87 if (read_index_ == BUFFER_SIZE) 88 int bytes_to_copy = io_buffer->size();
88 read_index_ = 0; 89 while (bytes_to_copy-- && !buffer_.empty()) {
89 return 1; 90 *data++ = buffer_.front();
91 buffer_.pop_front();
92 }
93 return io_buffer->size();
90 } 94 }
91 95
92 virtual int Write(scoped_refptr<net::IOBuffer> io_buffer, int byte_count) { 96 virtual int Write(scoped_refptr<net::IOBuffer> io_buffer, int byte_count) {
93 DCHECK(io_buffer.get()); 97 DCHECK(io_buffer.get());
94 DCHECK(byte_count >= 0); 98 DCHECK(byte_count >= 0);
95 99
96 char *data = io_buffer->data(); 100 char *data = io_buffer->data();
97 int count = byte_count; 101 int count = byte_count;
98 while (count--) { 102 while (count--)
99 ring_buffer_[write_index_++] = *data++; 103 buffer_.push_back(*data++);
100 if (write_index_ == BUFFER_SIZE)
101 write_index_ = 0;
102 }
103 return byte_count; 104 return byte_count;
104 } 105 }
105 106
106 MOCK_METHOD1(GetControlSignals, bool(ControlSignals &)); 107 MOCK_METHOD1(GetControlSignals, bool(ControlSignals &));
107 MOCK_METHOD1(SetControlSignals, bool(const ControlSignals &)); 108 MOCK_METHOD1(SetControlSignals, bool(const ControlSignals &));
108 109
109 private: 110 private:
110 enum { BUFFER_SIZE = 256 };
111 bool opened_; 111 bool opened_;
112 char ring_buffer_[BUFFER_SIZE]; 112 std::deque<char> buffer_;
113 int read_index_;
114 int write_index_;
115 113
116 DISALLOW_COPY_AND_ASSIGN(FakeEchoSerialConnection); 114 DISALLOW_COPY_AND_ASSIGN(FakeEchoSerialConnection);
117 }; 115 };
118 116
119 class FakeSerialOpenFunction : public SerialOpenFunction { 117 class FakeSerialOpenFunction : public SerialOpenFunction {
120 protected: 118 protected:
121 virtual SerialConnection* CreateSerialConnection( 119 virtual SerialConnection* CreateSerialConnection(
122 const std::string& port, 120 const std::string& port,
123 int bitrate, 121 int bitrate,
124 ApiResourceEventNotifier* event_notifier) OVERRIDE { 122 ApiResourceEventNotifier* event_notifier) OVERRIDE {
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
183 181
184 ASSERT_TRUE(RunExtensionTest("serial/api")) << message_; 182 ASSERT_TRUE(RunExtensionTest("serial/api")) << message_;
185 } 183 }
186 184
187 IN_PROC_BROWSER_TEST_F(SerialApiTest, SerialRealHardware) { 185 IN_PROC_BROWSER_TEST_F(SerialApiTest, SerialRealHardware) {
188 ResultCatcher catcher; 186 ResultCatcher catcher;
189 catcher.RestrictToProfile(browser()->profile()); 187 catcher.RestrictToProfile(browser()->profile());
190 188
191 ASSERT_TRUE(RunExtensionTest("serial/real_hardware")) << message_; 189 ASSERT_TRUE(RunExtensionTest("serial/real_hardware")) << message_;
192 } 190 }
OLDNEW
« no previous file with comments | « chrome/browser/extensions/api/serial/serial_api.cc ('k') | chrome/browser/extensions/api/serial/serial_connection.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698