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

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

Issue 369893008: Move the serial API to extensions/. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: add missing dependency from usb_service to chromeos Created 6 years, 5 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
(Empty)
1 // Copyright 2012 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 <deque>
6 #include <string>
7 #include <vector>
8
9 #include "base/memory/scoped_ptr.h"
10 #include "chrome/browser/extensions/api/serial/serial_api.h"
11 #include "chrome/browser/extensions/api/serial/serial_connection.h"
12 #include "chrome/browser/extensions/extension_apitest.h"
13 #include "chrome/browser/extensions/extension_function_test_utils.h"
14 #include "chrome/browser/extensions/extension_test_message_listener.h"
15 #include "chrome/browser/ui/browser.h"
16 #include "chrome/common/extensions/api/serial.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "extensions/browser/extension_function.h"
19 #include "testing/gmock/include/gmock/gmock.h"
20
21 using testing::_;
22 using testing::Return;
23
24 using content::BrowserThread;
25
26 namespace {
27
28 class SerialApiTest : public ExtensionApiTest {
29 public:
30 SerialApiTest() {}
31 };
32
33 } // namespace
34
35 namespace extensions {
36
37 class FakeSerialGetDevicesFunction : public AsyncExtensionFunction {
38 public:
39 virtual bool RunAsync() OVERRIDE {
40 base::ListValue* devices = new base::ListValue();
41 base::DictionaryValue* device0 = new base::DictionaryValue();
42 device0->SetString("path", "/dev/fakeserial");
43 base::DictionaryValue* device1 = new base::DictionaryValue();
44 device1->SetString("path", "\\\\COM800\\");
45 devices->Append(device0);
46 devices->Append(device1);
47 SetResult(devices);
48 SendResponse(true);
49 return true;
50 }
51 protected:
52 virtual ~FakeSerialGetDevicesFunction() {}
53 };
54
55 class FakeEchoSerialIoHandler : public device::SerialIoHandler {
56 public:
57 explicit FakeEchoSerialIoHandler() : opened_(false) {}
58
59 virtual void Open(const std::string& port,
60 const OpenCompleteCallback& callback) OVERRIDE {
61 DCHECK(!opened_);
62 opened_ = true;
63 callback.Run(true);
64 }
65
66 virtual bool ConfigurePort(
67 const device::serial::ConnectionOptions& options) OVERRIDE {
68 return true;
69 }
70
71 virtual void ReadImpl() OVERRIDE {}
72
73 virtual void CancelReadImpl() OVERRIDE {
74 QueueReadCompleted(0, read_cancel_reason());
75 }
76
77 virtual void WriteImpl() OVERRIDE {
78 DCHECK(pending_read_buffer());
79 DCHECK_LE(pending_write_buffer_len(), pending_read_buffer_len());
80 memcpy(pending_read_buffer()->data(),
81 pending_write_buffer()->data(),
82 pending_write_buffer_len());
83 QueueReadCompleted(pending_write_buffer_len(),
84 device::serial::RECEIVE_ERROR_NONE);
85 QueueWriteCompleted(pending_write_buffer_len(),
86 device::serial::SEND_ERROR_NONE);
87 }
88
89 virtual void CancelWriteImpl() OVERRIDE {
90 QueueWriteCompleted(0, write_cancel_reason());
91 }
92
93 virtual device::serial::DeviceControlSignalsPtr GetControlSignals()
94 const OVERRIDE {
95 device::serial::DeviceControlSignalsPtr signals(
96 device::serial::DeviceControlSignals::New());
97 signals->dcd = true;
98 signals->cts = true;
99 signals->ri = true;
100 signals->dsr = true;
101 return signals.Pass();
102 }
103
104 virtual device::serial::ConnectionInfoPtr GetPortInfo() const OVERRIDE {
105 device::serial::ConnectionInfoPtr info(
106 device::serial::ConnectionInfo::New());
107 info->bitrate = 9600;
108 info->data_bits = device::serial::DATA_BITS_EIGHT;
109 info->parity_bit = device::serial::PARITY_BIT_NO;
110 info->stop_bits = device::serial::STOP_BITS_ONE;
111 info->cts_flow_control = false;
112 return info.Pass();
113 }
114
115 virtual bool Flush() const OVERRIDE { return true; }
116
117 MOCK_METHOD1(SetControlSignals,
118 bool(const device::serial::HostControlSignals&));
119
120 protected:
121 virtual ~FakeEchoSerialIoHandler() {}
122
123 private:
124 bool opened_;
125
126 DISALLOW_COPY_AND_ASSIGN(FakeEchoSerialIoHandler);
127 };
128
129 class FakeSerialConnectFunction : public api::SerialConnectFunction {
130 protected:
131 virtual SerialConnection* CreateSerialConnection(
132 const std::string& port,
133 const std::string& owner_extension_id) const OVERRIDE {
134 scoped_refptr<FakeEchoSerialIoHandler> io_handler =
135 new FakeEchoSerialIoHandler;
136 EXPECT_CALL(*io_handler, SetControlSignals(_)).Times(1).WillOnce(
137 Return(true));
138 SerialConnection* serial_connection =
139 new SerialConnection(port, owner_extension_id);
140 serial_connection->SetIoHandlerForTest(io_handler);
141 return serial_connection;
142 }
143
144 protected:
145 virtual ~FakeSerialConnectFunction() {}
146 };
147
148 } // namespace extensions
149
150 ExtensionFunction* FakeSerialGetDevicesFunctionFactory() {
151 return new extensions::FakeSerialGetDevicesFunction();
152 }
153
154 ExtensionFunction* FakeSerialConnectFunctionFactory() {
155 return new extensions::FakeSerialConnectFunction();
156 }
157
158 // Disable SIMULATE_SERIAL_PORTS only if all the following are true:
159 //
160 // 1. You have an Arduino or compatible board attached to your machine and
161 // properly appearing as the first virtual serial port ("first" is very loosely
162 // defined as whichever port shows up in serial.getPorts). We've tested only
163 // the Atmega32u4 Breakout Board and Arduino Leonardo; note that both these
164 // boards are based on the Atmel ATmega32u4, rather than the more common
165 // Arduino '328p with either FTDI or '8/16u2 USB interfaces. TODO: test more
166 // widely.
167 //
168 // 2. Your user has permission to read/write the port. For example, this might
169 // mean that your user is in the "tty" or "uucp" group on Ubuntu flavors of
170 // Linux, or else that the port's path (e.g., /dev/ttyACM0) has global
171 // read/write permissions.
172 //
173 // 3. You have uploaded a program to the board that does a byte-for-byte echo
174 // on the virtual serial port at 57600 bps. An example is at
175 // chrome/test/data/extensions/api_test/serial/api/serial_arduino_test.ino.
176 //
177 #define SIMULATE_SERIAL_PORTS (1)
178 IN_PROC_BROWSER_TEST_F(SerialApiTest, SerialFakeHardware) {
179 ResultCatcher catcher;
180 catcher.RestrictToProfile(browser()->profile());
181
182 #if SIMULATE_SERIAL_PORTS
183 ASSERT_TRUE(extensions::ExtensionFunctionDispatcher::OverrideFunction(
184 "serial.getDevices", FakeSerialGetDevicesFunctionFactory));
185 ASSERT_TRUE(extensions::ExtensionFunctionDispatcher::OverrideFunction(
186 "serial.connect", FakeSerialConnectFunctionFactory));
187 #endif
188
189 ASSERT_TRUE(RunExtensionTest("serial/api")) << message_;
190 }
191
192 IN_PROC_BROWSER_TEST_F(SerialApiTest, SerialRealHardware) {
193 ResultCatcher catcher;
194 catcher.RestrictToProfile(browser()->profile());
195
196 ASSERT_TRUE(RunExtensionTest("serial/real_hardware")) << message_;
197 }
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