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

Side by Side Diff: device/serial/serial_service_unittest.cc

Issue 488363002: Implement the host side of serial connection I/O on data pipe. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix win x64 build Created 6 years, 3 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
« no previous file with comments | « device/serial/serial_service_impl.cc ('k') | device/serial/test_serial_io_handler.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 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 "base/bind.h" 5 #include "base/bind.h"
6 #include "base/message_loop/message_loop.h" 6 #include "base/message_loop/message_loop.h"
7 #include "base/run_loop.h" 7 #include "base/run_loop.h"
8 #include "device/serial/serial.mojom.h" 8 #include "device/serial/serial.mojom.h"
9 #include "device/serial/serial_service_impl.h" 9 #include "device/serial/serial_service_impl.h"
10 #include "device/serial/test_serial_io_handler.h" 10 #include "device/serial/test_serial_io_handler.h"
11 #include "mojo/public/cpp/bindings/error_handler.h" 11 #include "mojo/public/cpp/bindings/error_handler.h"
12 #include "mojo/public/cpp/bindings/interface_ptr.h" 12 #include "mojo/public/cpp/bindings/interface_ptr.h"
13 #include "mojo/public/cpp/bindings/interface_request.h" 13 #include "mojo/public/cpp/bindings/interface_request.h"
14 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/gtest/include/gtest/gtest.h"
15 15
16 namespace device { 16 namespace device {
17 namespace { 17 namespace {
18 18
19 class FakeSerialDeviceEnumerator : public SerialDeviceEnumerator { 19 class FakeSerialDeviceEnumerator : public SerialDeviceEnumerator {
20 virtual mojo::Array<serial::DeviceInfoPtr> GetDevices() OVERRIDE { 20 virtual mojo::Array<serial::DeviceInfoPtr> GetDevices() OVERRIDE {
21 mojo::Array<serial::DeviceInfoPtr> devices(1); 21 mojo::Array<serial::DeviceInfoPtr> devices(1);
22 devices[0] = serial::DeviceInfo::New(); 22 devices[0] = serial::DeviceInfo::New();
23 devices[0]->path = "device"; 23 devices[0]->path = "device";
24 return devices.Pass(); 24 return devices.Pass();
25 } 25 }
26 }; 26 };
27 27
28 class FailToOpenIoHandler : public TestSerialIoHandler { 28 class FailToOpenIoHandler : public TestSerialIoHandler {
29 public: 29 public:
30 static scoped_refptr<SerialIoHandler> Create() {
31 return new FailToOpenIoHandler;
32 }
33
34 virtual void Open(const std::string& port, 30 virtual void Open(const std::string& port,
35 const OpenCompleteCallback& callback) OVERRIDE { 31 const OpenCompleteCallback& callback) OVERRIDE {
36 callback.Run(false); 32 callback.Run(false);
37 } 33 }
38 34
39 protected: 35 protected:
40 virtual ~FailToOpenIoHandler() {} 36 virtual ~FailToOpenIoHandler() {}
41 }; 37 };
42 38
43 } // namespace 39 } // namespace
44 40
45 class SerialServiceTest : public testing::Test, public mojo::ErrorHandler { 41 class SerialServiceTest : public testing::Test, public mojo::ErrorHandler {
46 public: 42 public:
47 SerialServiceTest() {} 43 SerialServiceTest() : connected_(false), expecting_error_(false) {}
48 44
49 void StoreDevices(mojo::Array<serial::DeviceInfoPtr> devices) { 45 void StoreDevices(mojo::Array<serial::DeviceInfoPtr> devices) {
50 devices_ = devices.Pass(); 46 devices_ = devices.Pass();
51 StopMessageLoop(); 47 StopMessageLoop();
52 } 48 }
53 49
54 virtual void OnConnectionError() OVERRIDE { 50 virtual void OnConnectionError() OVERRIDE {
55 StopMessageLoop(); 51 StopMessageLoop();
56 EXPECT_TRUE(expecting_error_); 52 EXPECT_TRUE(expecting_error_);
57 } 53 }
58 54
59 void RunMessageLoop() { 55 void RunMessageLoop() {
60 run_loop_.reset(new base::RunLoop); 56 run_loop_.reset(new base::RunLoop);
61 run_loop_->Run(); 57 run_loop_->Run();
62 } 58 }
63 59
64 void StopMessageLoop() { 60 void StopMessageLoop() {
65 ASSERT_TRUE(run_loop_); 61 ASSERT_TRUE(run_loop_);
66 message_loop_.PostTask(FROM_HERE, run_loop_->QuitClosure()); 62 message_loop_.PostTask(FROM_HERE, run_loop_->QuitClosure());
67 } 63 }
68 64
69 void OnGotInfo(serial::ConnectionInfoPtr options) { StopMessageLoop(); } 65 void OnGotInfo(serial::ConnectionInfoPtr options) {
66 connected_ = true;
67 StopMessageLoop();
68 }
69
70 scoped_refptr<SerialIoHandler> ReturnIoHandler() { return io_handler_; }
71
72 void RunConnectTest(const std::string& path, bool expecting_success) {
73 if (!io_handler_)
74 io_handler_ = new TestSerialIoHandler;
75 mojo::InterfacePtr<serial::SerialService> service;
76 mojo::BindToProxy(
77 new SerialServiceImpl(
78 new SerialConnectionFactory(
79 base::Bind(&SerialServiceTest::ReturnIoHandler,
80 base::Unretained(this)),
81 base::MessageLoopProxy::current()),
82 scoped_ptr<SerialDeviceEnumerator>(new FakeSerialDeviceEnumerator)),
83 &service);
84 mojo::InterfacePtr<serial::Connection> connection;
85 mojo::InterfacePtr<serial::DataSink> sink;
86 mojo::InterfacePtr<serial::DataSource> source;
87 service->Connect(path,
88 serial::ConnectionOptions::New(),
89 mojo::Get(&connection),
90 mojo::Get(&sink),
91 mojo::Get(&source));
92 connection.set_error_handler(this);
93 expecting_error_ = !expecting_success;
94 connection->GetInfo(
95 base::Bind(&SerialServiceTest::OnGotInfo, base::Unretained(this)));
96 RunMessageLoop();
97 EXPECT_EQ(!expecting_success, connection.encountered_error());
98 EXPECT_EQ(expecting_success, connected_);
99 connection.reset();
100 }
70 101
71 base::MessageLoop message_loop_; 102 base::MessageLoop message_loop_;
72 scoped_ptr<base::RunLoop> run_loop_; 103 scoped_ptr<base::RunLoop> run_loop_;
73 mojo::Array<serial::DeviceInfoPtr> devices_; 104 mojo::Array<serial::DeviceInfoPtr> devices_;
74 scoped_refptr<TestSerialIoHandler> io_handler_; 105 scoped_refptr<TestSerialIoHandler> io_handler_;
106 bool connected_;
75 bool expecting_error_; 107 bool expecting_error_;
76 serial::ConnectionInfoPtr info_; 108 serial::ConnectionInfoPtr info_;
77 109
78 private: 110 private:
79 DISALLOW_COPY_AND_ASSIGN(SerialServiceTest); 111 DISALLOW_COPY_AND_ASSIGN(SerialServiceTest);
80 }; 112 };
81 113
82 TEST_F(SerialServiceTest, GetDevices) { 114 TEST_F(SerialServiceTest, GetDevices) {
83 mojo::InterfacePtr<serial::SerialService> service; 115 mojo::InterfacePtr<serial::SerialService> service;
84 SerialServiceImpl::Create(NULL, mojo::Get(&service)); 116 SerialServiceImpl::Create(NULL, mojo::Get(&service));
85 service.set_error_handler(this); 117 service.set_error_handler(this);
86 mojo::Array<serial::DeviceInfoPtr> result; 118 mojo::Array<serial::DeviceInfoPtr> result;
87 service->GetDevices( 119 service->GetDevices(
88 base::Bind(&SerialServiceTest::StoreDevices, base::Unretained(this))); 120 base::Bind(&SerialServiceTest::StoreDevices, base::Unretained(this)));
89 RunMessageLoop(); 121 RunMessageLoop();
90 122
91 // Because we're running on unknown hardware, only check that we received a 123 // Because we're running on unknown hardware, only check that we received a
92 // non-null result. 124 // non-null result.
93 EXPECT_TRUE(devices_); 125 EXPECT_TRUE(devices_);
94 } 126 }
95 127
96 TEST_F(SerialServiceTest, Connect) { 128 TEST_F(SerialServiceTest, Connect) {
97 mojo::InterfacePtr<serial::SerialService> service; 129 RunConnectTest("device", true);
98 mojo::BindToProxy(
99 new SerialServiceImpl(
100 new SerialConnectionFactory(base::Bind(&TestSerialIoHandler::Create),
101 base::MessageLoopProxy::current()),
102 scoped_ptr<SerialDeviceEnumerator>(new FakeSerialDeviceEnumerator)),
103 &service);
104 service.set_error_handler(this);
105 mojo::InterfacePtr<serial::Connection> connection;
106 service->Connect(
107 "device", serial::ConnectionOptions::New(), mojo::Get(&connection));
108 connection.set_error_handler(this);
109 connection->GetInfo(
110 base::Bind(&SerialServiceTest::OnGotInfo, base::Unretained(this)));
111 RunMessageLoop();
112 connection.reset();
113 } 130 }
114 131
115 TEST_F(SerialServiceTest, ConnectInvalidPath) { 132 TEST_F(SerialServiceTest, ConnectInvalidPath) {
116 mojo::InterfacePtr<serial::SerialService> service; 133 RunConnectTest("invalid_path", false);
117 mojo::BindToProxy(
118 new SerialServiceImpl(
119 new SerialConnectionFactory(base::Bind(&TestSerialIoHandler::Create),
120 base::MessageLoopProxy::current()),
121 scoped_ptr<SerialDeviceEnumerator>(new FakeSerialDeviceEnumerator)),
122 &service);
123 mojo::InterfacePtr<serial::Connection> connection;
124 service->Connect(
125 "invalid_path", serial::ConnectionOptions::New(), mojo::Get(&connection));
126 connection.set_error_handler(this);
127 expecting_error_ = true;
128 RunMessageLoop();
129 EXPECT_TRUE(connection.encountered_error());
130 } 134 }
131 135
132 TEST_F(SerialServiceTest, ConnectOpenFailed) { 136 TEST_F(SerialServiceTest, ConnectOpenFailed) {
133 mojo::InterfacePtr<serial::SerialService> service; 137 io_handler_ = new FailToOpenIoHandler;
134 mojo::BindToProxy( 138 RunConnectTest("device", false);
135 new SerialServiceImpl(
136 new SerialConnectionFactory(base::Bind(&FailToOpenIoHandler::Create),
137 base::MessageLoopProxy::current()),
138 scoped_ptr<SerialDeviceEnumerator>(new FakeSerialDeviceEnumerator)),
139 &service);
140 mojo::InterfacePtr<serial::Connection> connection;
141 service->Connect(
142 "device", serial::ConnectionOptions::New(), mojo::Get(&connection));
143 expecting_error_ = true;
144 connection.set_error_handler(this);
145 RunMessageLoop();
146 EXPECT_TRUE(connection.encountered_error());
147 } 139 }
148 140
149 } // namespace device 141 } // namespace device
OLDNEW
« no previous file with comments | « device/serial/serial_service_impl.cc ('k') | device/serial/test_serial_io_handler.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698