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

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

Issue 401563002: Add a partial Mojo serial connection interface and implementation. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: address comments 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
« 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 "mojo/public/cpp/bindings/error_handler.h" 11 #include "mojo/public/cpp/bindings/error_handler.h"
11 #include "mojo/public/cpp/bindings/interface_ptr.h" 12 #include "mojo/public/cpp/bindings/interface_ptr.h"
12 #include "mojo/public/cpp/bindings/interface_request.h" 13 #include "mojo/public/cpp/bindings/interface_request.h"
13 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/gtest/include/gtest/gtest.h"
14 15
15 namespace device { 16 namespace device {
17 namespace {
18
19 class FakeSerialDeviceEnumerator : public SerialDeviceEnumerator {
20 virtual mojo::Array<serial::DeviceInfoPtr> GetDevices() OVERRIDE {
21 mojo::Array<serial::DeviceInfoPtr> devices(1);
22 devices[0] = serial::DeviceInfo::New();
23 devices[0]->path = "device";
24 return devices.Pass();
25 }
26 };
27
28 class FailToOpenIoHandler : public TestSerialIoHandler {
29 public:
30 static scoped_refptr<SerialIoHandler> Create() {
31 return new FailToOpenIoHandler;
32 }
33
34 virtual void Open(const std::string& port,
35 const OpenCompleteCallback& callback) OVERRIDE {
36 callback.Run(false);
37 }
38
39 protected:
40 virtual ~FailToOpenIoHandler() {}
41 };
42
43 } // namespace
16 44
17 class SerialServiceTest : public testing::Test, public mojo::ErrorHandler { 45 class SerialServiceTest : public testing::Test, public mojo::ErrorHandler {
18 public: 46 public:
19 SerialServiceTest() {} 47 SerialServiceTest() {}
20 48
21 void StoreDevices(mojo::Array<serial::DeviceInfoPtr> devices) { 49 void StoreDevices(mojo::Array<serial::DeviceInfoPtr> devices) {
22 devices_ = devices.Pass(); 50 devices_ = devices.Pass();
23 message_loop_.PostTask(FROM_HERE, run_loop_.QuitClosure()); 51 StopMessageLoop();
24 } 52 }
25 53
26 virtual void OnConnectionError() OVERRIDE { 54 virtual void OnConnectionError() OVERRIDE {
27 message_loop_.PostTask(FROM_HERE, run_loop_.QuitClosure()); 55 StopMessageLoop();
28 FAIL() << "Connection error"; 56 EXPECT_TRUE(expecting_error_);
29 } 57 }
30 58
59 void RunMessageLoop() {
60 run_loop_.reset(new base::RunLoop);
61 run_loop_->Run();
62 }
63
64 void StopMessageLoop() {
65 ASSERT_TRUE(run_loop_);
66 message_loop_.PostTask(FROM_HERE, run_loop_->QuitClosure());
67 }
68
69 void OnGotInfo(serial::ConnectionInfoPtr options) { StopMessageLoop(); }
70
31 base::MessageLoop message_loop_; 71 base::MessageLoop message_loop_;
32 base::RunLoop run_loop_; 72 scoped_ptr<base::RunLoop> run_loop_;
33 mojo::Array<serial::DeviceInfoPtr> devices_; 73 mojo::Array<serial::DeviceInfoPtr> devices_;
74 scoped_refptr<TestSerialIoHandler> io_handler_;
75 bool expecting_error_;
76 serial::ConnectionInfoPtr info_;
34 77
35 private: 78 private:
36 DISALLOW_COPY_AND_ASSIGN(SerialServiceTest); 79 DISALLOW_COPY_AND_ASSIGN(SerialServiceTest);
37 }; 80 };
38 81
39 TEST_F(SerialServiceTest, GetDevices) { 82 TEST_F(SerialServiceTest, GetDevices) {
40 mojo::InterfacePtr<serial::SerialService> service; 83 mojo::InterfacePtr<serial::SerialService> service;
41 SerialServiceImpl::Create(mojo::Get(&service)); 84 SerialServiceImpl::Create(NULL, mojo::Get(&service));
42 service.set_error_handler(this); 85 service.set_error_handler(this);
43 mojo::Array<serial::DeviceInfoPtr> result; 86 mojo::Array<serial::DeviceInfoPtr> result;
44 service->GetDevices( 87 service->GetDevices(
45 base::Bind(&SerialServiceTest::StoreDevices, base::Unretained(this))); 88 base::Bind(&SerialServiceTest::StoreDevices, base::Unretained(this)));
46 run_loop_.Run(); 89 RunMessageLoop();
47 90
48 // Because we're running on unknown hardware, only check that we received a 91 // Because we're running on unknown hardware, only check that we received a
49 // non-null result. 92 // non-null result.
50 EXPECT_TRUE(devices_); 93 EXPECT_TRUE(devices_);
51 } 94 }
52 95
96 TEST_F(SerialServiceTest, Connect) {
97 mojo::InterfacePtr<serial::SerialService> service;
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 }
114
115 TEST_F(SerialServiceTest, ConnectInvalidPath) {
116 mojo::InterfacePtr<serial::SerialService> service;
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 }
131
132 TEST_F(SerialServiceTest, ConnectOpenFailed) {
133 mojo::InterfacePtr<serial::SerialService> service;
134 mojo::BindToProxy(
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 }
148
53 } // namespace device 149 } // 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