- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / serial / serial_apitest.cc
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
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.h"
14 #include "chrome/browser/extensions/extension_function_test_utils.h"
15 #include "chrome/browser/extensions/extension_test_message_listener.h"
16 #include "chrome/browser/ui/browser.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "testing/gmock/include/gmock/gmock.h"
19
20 using testing::_;
21 using testing::Return;
22
23 using content::BrowserThread;
24
25 namespace serial = extensions::api::serial;
26
27 namespace {
28
29 class SerialApiTest : public ExtensionApiTest {
30  public:
31   SerialApiTest() {}
32 };
33
34 }  // namespace
35
36 namespace extensions {
37
38 class FakeSerialGetPortsFunction : public AsyncExtensionFunction {
39  public:
40   virtual bool RunImpl() OVERRIDE {
41     base::ListValue* ports = new base::ListValue();
42     ports->Append(Value::CreateStringValue("/dev/fakeserial"));
43     ports->Append(Value::CreateStringValue("\\\\COM800\\"));
44     SetResult(ports);
45     SendResponse(true);
46     return true;
47   }
48  protected:
49   virtual ~FakeSerialGetPortsFunction() {}
50 };
51
52 class FakeEchoSerialConnection : public SerialConnection {
53  public:
54   explicit FakeEchoSerialConnection(
55       const std::string& port,
56       int bitrate,
57       serial::DataBit databit,
58       serial::ParityBit parity,
59       serial::StopBit stopbit,
60       const std::string& owner_extension_id)
61       : SerialConnection(port, bitrate, databit, parity, stopbit,
62                         owner_extension_id),
63         opened_(true) {
64     Flush();
65     opened_ = false;
66   }
67
68   virtual ~FakeEchoSerialConnection() {
69   }
70
71   virtual bool Open() {
72     DCHECK(!opened_);
73     opened_ = true;
74     return true;
75   }
76
77   virtual void Close() {
78     DCHECK(opened_);
79   }
80
81   virtual void Flush() {
82     DCHECK(opened_);
83     buffer_.clear();
84   }
85
86   virtual int Read(scoped_refptr<net::IOBufferWithSize> io_buffer) {
87     DCHECK(io_buffer->data());
88
89     if (buffer_.empty()) {
90       return 0;
91     }
92     char *data = io_buffer->data();
93     int bytes_to_copy = io_buffer->size();
94     while (bytes_to_copy-- && !buffer_.empty()) {
95       *data++ = buffer_.front();
96       buffer_.pop_front();
97     }
98     return io_buffer->size();
99   }
100
101   virtual int Write(scoped_refptr<net::IOBuffer> io_buffer, int byte_count) {
102     DCHECK(io_buffer.get());
103     DCHECK_GE(byte_count, 0);
104
105     char *data = io_buffer->data();
106     int count = byte_count;
107     while (count--)
108       buffer_.push_back(*data++);
109     return byte_count;
110   }
111
112   MOCK_METHOD1(GetControlSignals, bool(ControlSignals &));
113   MOCK_METHOD1(SetControlSignals, bool(const ControlSignals &));
114
115  private:
116   bool opened_;
117   std::deque<char> buffer_;
118
119   DISALLOW_COPY_AND_ASSIGN(FakeEchoSerialConnection);
120 };
121
122 class FakeSerialOpenFunction : public SerialOpenFunction {
123  protected:
124   virtual SerialConnection* CreateSerialConnection(
125       const std::string& port,
126       int bitrate,
127       serial::DataBit databit,
128       serial::ParityBit parity,
129       serial::StopBit stopbit,
130       const std::string& owner_extension_id) OVERRIDE {
131     FakeEchoSerialConnection* serial_connection =
132         new FakeEchoSerialConnection(port, bitrate, databit, parity, stopbit,
133                                      owner_extension_id);
134     EXPECT_CALL(*serial_connection, GetControlSignals(_)).
135         Times(1).WillOnce(Return(true));
136     EXPECT_CALL(*serial_connection, SetControlSignals(_)).
137         Times(1).WillOnce(Return(true));
138     return serial_connection;
139   }
140   virtual bool DoesPortExist(const std::string& port) OVERRIDE {
141     return true;
142   }
143
144  protected:
145   virtual ~FakeSerialOpenFunction() {}
146 };
147
148 }  // namespace extensions
149
150 ExtensionFunction* FakeSerialGetPortsFunctionFactory() {
151   return new extensions::FakeSerialGetPortsFunction();
152 }
153
154 ExtensionFunction* FakeSerialOpenFunctionFactory() {
155   return new extensions::FakeSerialOpenFunction();
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(ExtensionFunctionDispatcher::OverrideFunction(
184       "serial.getPorts",
185       FakeSerialGetPortsFunctionFactory));
186   ASSERT_TRUE(ExtensionFunctionDispatcher::OverrideFunction(
187       "serial.open",
188       FakeSerialOpenFunctionFactory));
189 #endif
190
191   ASSERT_TRUE(RunExtensionTest("serial/api")) << message_;
192 }
193
194 IN_PROC_BROWSER_TEST_F(SerialApiTest, SerialRealHardware) {
195   ResultCatcher catcher;
196   catcher.RestrictToProfile(browser()->profile());
197
198   ASSERT_TRUE(RunExtensionTest("serial/real_hardware")) << message_;
199 }