Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / extensions / browser / api / serial / serial_apitest.cc
1 // Copyright 2014 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 <string>
6
7 #include "chrome/browser/extensions/extension_apitest.h"
8 #include "device/serial/test_serial_io_handler.h"
9 #include "extensions/browser/api/serial/serial_api.h"
10 #include "extensions/browser/api/serial/serial_connection.h"
11 #include "extensions/browser/extension_function.h"
12 #include "extensions/common/api/serial.h"
13 #include "testing/gmock/include/gmock/gmock.h"
14
15 using testing::_;
16 using testing::Return;
17
18 namespace {
19
20 class SerialApiTest : public ExtensionApiTest {
21  public:
22   SerialApiTest() {}
23 };
24
25 }  // namespace
26
27 namespace extensions {
28
29 class FakeSerialGetDevicesFunction : public AsyncExtensionFunction {
30  public:
31   virtual bool RunAsync() OVERRIDE {
32     base::ListValue* devices = new base::ListValue();
33     base::DictionaryValue* device0 = new base::DictionaryValue();
34     device0->SetString("path", "/dev/fakeserial");
35     base::DictionaryValue* device1 = new base::DictionaryValue();
36     device1->SetString("path", "\\\\COM800\\");
37     devices->Append(device0);
38     devices->Append(device1);
39     SetResult(devices);
40     SendResponse(true);
41     return true;
42   }
43
44  protected:
45   virtual ~FakeSerialGetDevicesFunction() {}
46 };
47
48 class FakeEchoSerialIoHandler : public device::TestSerialIoHandler {
49  public:
50   explicit FakeEchoSerialIoHandler() {
51     device_control_signals()->dcd = true;
52     device_control_signals()->cts = true;
53     device_control_signals()->ri = true;
54     device_control_signals()->dsr = true;
55   }
56
57   MOCK_METHOD1(SetControlSignals,
58                bool(const device::serial::HostControlSignals&));
59
60  protected:
61   virtual ~FakeEchoSerialIoHandler() {}
62
63  private:
64   DISALLOW_COPY_AND_ASSIGN(FakeEchoSerialIoHandler);
65 };
66
67 class FakeSerialConnectFunction : public core_api::SerialConnectFunction {
68  protected:
69   virtual SerialConnection* CreateSerialConnection(
70       const std::string& port,
71       const std::string& owner_extension_id) const OVERRIDE {
72     scoped_refptr<FakeEchoSerialIoHandler> io_handler =
73         new FakeEchoSerialIoHandler;
74     EXPECT_CALL(*io_handler, SetControlSignals(_)).Times(1).WillOnce(
75         Return(true));
76     SerialConnection* serial_connection =
77         new SerialConnection(port, owner_extension_id);
78     serial_connection->SetIoHandlerForTest(io_handler);
79     return serial_connection;
80   }
81
82  protected:
83   virtual ~FakeSerialConnectFunction() {}
84 };
85
86 }  // namespace extensions
87
88 ExtensionFunction* FakeSerialGetDevicesFunctionFactory() {
89   return new extensions::FakeSerialGetDevicesFunction();
90 }
91
92 ExtensionFunction* FakeSerialConnectFunctionFactory() {
93   return new extensions::FakeSerialConnectFunction();
94 }
95
96 // Disable SIMULATE_SERIAL_PORTS only if all the following are true:
97 //
98 // 1. You have an Arduino or compatible board attached to your machine and
99 // properly appearing as the first virtual serial port ("first" is very loosely
100 // defined as whichever port shows up in serial.getPorts). We've tested only
101 // the Atmega32u4 Breakout Board and Arduino Leonardo; note that both these
102 // boards are based on the Atmel ATmega32u4, rather than the more common
103 // Arduino '328p with either FTDI or '8/16u2 USB interfaces. TODO: test more
104 // widely.
105 //
106 // 2. Your user has permission to read/write the port. For example, this might
107 // mean that your user is in the "tty" or "uucp" group on Ubuntu flavors of
108 // Linux, or else that the port's path (e.g., /dev/ttyACM0) has global
109 // read/write permissions.
110 //
111 // 3. You have uploaded a program to the board that does a byte-for-byte echo
112 // on the virtual serial port at 57600 bps. An example is at
113 // chrome/test/data/extensions/api_test/serial/api/serial_arduino_test.ino.
114 //
115 #define SIMULATE_SERIAL_PORTS (1)
116 IN_PROC_BROWSER_TEST_F(SerialApiTest, SerialFakeHardware) {
117   ResultCatcher catcher;
118   catcher.RestrictToProfile(browser()->profile());
119
120 #if SIMULATE_SERIAL_PORTS
121   ASSERT_TRUE(extensions::ExtensionFunctionDispatcher::OverrideFunction(
122       "serial.getDevices", FakeSerialGetDevicesFunctionFactory));
123   ASSERT_TRUE(extensions::ExtensionFunctionDispatcher::OverrideFunction(
124       "serial.connect", FakeSerialConnectFunctionFactory));
125 #endif
126
127   ASSERT_TRUE(RunExtensionTest("serial/api")) << message_;
128 }
129
130 IN_PROC_BROWSER_TEST_F(SerialApiTest, SerialRealHardware) {
131   ResultCatcher catcher;
132   catcher.RestrictToProfile(browser()->profile());
133
134   ASSERT_TRUE(RunExtensionTest("serial/real_hardware")) << message_;
135 }