Try backing SkPicture with SkRecord in Chromium.
[chromium-blink-merge.git] / device / serial / serial_service_unittest.cc
bloba02d075df7adae441ee1afbfd7b455197bd4e13f
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.
5 #include "base/bind.h"
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 "mojo/public/cpp/bindings/error_handler.h"
12 #include "mojo/public/cpp/bindings/interface_ptr.h"
13 #include "mojo/public/cpp/bindings/interface_request.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 namespace device {
17 namespace {
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();
28 class FailToOpenIoHandler : public TestSerialIoHandler {
29 public:
30 virtual void Open(const std::string& port,
31 const OpenCompleteCallback& callback) OVERRIDE {
32 callback.Run(false);
35 protected:
36 virtual ~FailToOpenIoHandler() {}
39 } // namespace
41 class SerialServiceTest : public testing::Test, public mojo::ErrorHandler {
42 public:
43 SerialServiceTest() : connected_(false), expecting_error_(false) {}
45 void StoreDevices(mojo::Array<serial::DeviceInfoPtr> devices) {
46 devices_ = devices.Pass();
47 StopMessageLoop();
50 virtual void OnConnectionError() OVERRIDE {
51 StopMessageLoop();
52 EXPECT_TRUE(expecting_error_);
55 void RunMessageLoop() {
56 run_loop_.reset(new base::RunLoop);
57 run_loop_->Run();
60 void StopMessageLoop() {
61 ASSERT_TRUE(run_loop_);
62 message_loop_.PostTask(FROM_HERE, run_loop_->QuitClosure());
65 void OnGotInfo(serial::ConnectionInfoPtr options) {
66 connected_ = true;
67 StopMessageLoop();
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 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();
102 base::MessageLoop message_loop_;
103 scoped_ptr<base::RunLoop> run_loop_;
104 mojo::Array<serial::DeviceInfoPtr> devices_;
105 scoped_refptr<TestSerialIoHandler> io_handler_;
106 bool connected_;
107 bool expecting_error_;
108 serial::ConnectionInfoPtr info_;
110 private:
111 DISALLOW_COPY_AND_ASSIGN(SerialServiceTest);
114 TEST_F(SerialServiceTest, GetDevices) {
115 mojo::InterfacePtr<serial::SerialService> service;
116 SerialServiceImpl::Create(NULL, mojo::Get(&service));
117 service.set_error_handler(this);
118 mojo::Array<serial::DeviceInfoPtr> result;
119 service->GetDevices(
120 base::Bind(&SerialServiceTest::StoreDevices, base::Unretained(this)));
121 RunMessageLoop();
123 // Because we're running on unknown hardware, only check that we received a
124 // non-null result.
125 EXPECT_TRUE(devices_);
128 TEST_F(SerialServiceTest, Connect) {
129 RunConnectTest("device", true);
132 TEST_F(SerialServiceTest, ConnectInvalidPath) {
133 RunConnectTest("invalid_path", false);
136 TEST_F(SerialServiceTest, ConnectOpenFailed) {
137 io_handler_ = new FailToOpenIoHandler;
138 RunConnectTest("device", false);
141 } // namespace device