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.
6 #include "base/message_loop/message_loop.h"
7 #include "base/run_loop.h"
8 #include "device/serial/serial.mojom.h"
9 #include "device/serial/serial_service_impl.h"
10 #include "device/serial/test_serial_io_handler.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "third_party/mojo/src/mojo/public/cpp/bindings/interface_ptr.h"
13 #include "third_party/mojo/src/mojo/public/cpp/bindings/interface_request.h"
18 class FakeSerialDeviceEnumerator
: public SerialDeviceEnumerator
{
19 mojo::Array
<serial::DeviceInfoPtr
> GetDevices() override
{
20 mojo::Array
<serial::DeviceInfoPtr
> devices(1);
21 devices
[0] = serial::DeviceInfo::New();
22 devices
[0]->path
= "device";
23 return devices
.Pass();
27 class FailToOpenIoHandler
: public TestSerialIoHandler
{
29 void Open(const std::string
& port
,
30 const serial::ConnectionOptions
& options
,
31 const OpenCompleteCallback
& callback
) override
{
36 ~FailToOpenIoHandler() override
{}
41 class SerialServiceTest
: public testing::Test
{
43 SerialServiceTest() : connected_(false), expecting_error_(false) {}
45 void StoreDevices(mojo::Array
<serial::DeviceInfoPtr
> devices
) {
46 devices_
= devices
.Pass();
50 void OnConnectionError() {
52 EXPECT_TRUE(expecting_error_
);
55 void RunMessageLoop() {
56 run_loop_
.reset(new base::RunLoop
);
60 void StopMessageLoop() {
61 ASSERT_TRUE(run_loop_
);
62 message_loop_
.PostTask(FROM_HERE
, run_loop_
->QuitClosure());
65 void OnGotInfo(serial::ConnectionInfoPtr options
) {
70 scoped_refptr
<SerialIoHandler
> ReturnIoHandler() { return io_handler_
; }
72 void RunConnectTest(const std::string
& path
, bool expecting_success
) {
73 if (!io_handler_
.get())
74 io_handler_
= new TestSerialIoHandler
;
75 mojo::InterfacePtr
<serial::SerialService
> service
;
76 new SerialServiceImpl(
77 new SerialConnectionFactory(
78 base::Bind(&SerialServiceTest::ReturnIoHandler
,
79 base::Unretained(this)),
80 base::ThreadTaskRunnerHandle::Get()),
81 scoped_ptr
<SerialDeviceEnumerator
>(new FakeSerialDeviceEnumerator
),
82 mojo::GetProxy(&service
));
83 mojo::InterfacePtr
<serial::Connection
> connection
;
84 mojo::InterfacePtr
<serial::DataSink
> sink
;
85 mojo::InterfacePtr
<serial::DataSource
> source
;
86 mojo::InterfacePtr
<serial::DataSourceClient
> source_client
;
87 mojo::GetProxy(&source_client
);
88 service
->Connect(path
, serial::ConnectionOptions::New(),
89 mojo::GetProxy(&connection
), mojo::GetProxy(&sink
),
90 mojo::GetProxy(&source
), source_client
.Pass());
91 connection
.set_connection_error_handler(base::Bind(
92 &SerialServiceTest::OnConnectionError
, base::Unretained(this)));
93 expecting_error_
= !expecting_success
;
95 base::Bind(&SerialServiceTest::OnGotInfo
, base::Unretained(this)));
97 EXPECT_EQ(!expecting_success
, connection
.encountered_error());
98 EXPECT_EQ(expecting_success
, connected_
);
102 base::MessageLoop message_loop_
;
103 scoped_ptr
<base::RunLoop
> run_loop_
;
104 mojo::Array
<serial::DeviceInfoPtr
> devices_
;
105 scoped_refptr
<TestSerialIoHandler
> io_handler_
;
107 bool expecting_error_
;
108 serial::ConnectionInfoPtr info_
;
111 DISALLOW_COPY_AND_ASSIGN(SerialServiceTest
);
114 TEST_F(SerialServiceTest
, GetDevices
) {
115 mojo::InterfacePtr
<serial::SerialService
> service
;
116 SerialServiceImpl::Create(NULL
, NULL
, mojo::GetProxy(&service
));
117 service
.set_connection_error_handler(base::Bind(
118 &SerialServiceTest::OnConnectionError
, base::Unretained(this)));
119 mojo::Array
<serial::DeviceInfoPtr
> result
;
121 base::Bind(&SerialServiceTest::StoreDevices
, base::Unretained(this)));
124 // Because we're running on unknown hardware, only check that we received a
126 EXPECT_TRUE(devices_
);
129 TEST_F(SerialServiceTest
, Connect
) {
130 RunConnectTest("device", true);
133 TEST_F(SerialServiceTest
, ConnectInvalidPath
) {
134 RunConnectTest("invalid_path", false);
137 TEST_F(SerialServiceTest
, ConnectOpenFailed
) {
138 io_handler_
= new FailToOpenIoHandler
;
139 RunConnectTest("device", false);
142 } // namespace device