GPU workaround to simulate Out of Memory errors with large textures
[chromium-blink-merge.git] / device / serial / serial_service_unittest.cc
blob265b0b5aa47d34a92cff093537c4c1d151fdf2c0
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 "testing/gtest/include/gtest/gtest.h"
12 #include "third_party/mojo/src/mojo/public/cpp/bindings/error_handler.h"
13 #include "third_party/mojo/src/mojo/public/cpp/bindings/interface_ptr.h"
14 #include "third_party/mojo/src/mojo/public/cpp/bindings/interface_request.h"
16 namespace device {
17 namespace {
19 class FakeSerialDeviceEnumerator : public SerialDeviceEnumerator {
20 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 void Open(const std::string& port,
31 const serial::ConnectionOptions& options,
32 const OpenCompleteCallback& callback) override {
33 callback.Run(false);
36 protected:
37 ~FailToOpenIoHandler() override {}
40 } // namespace
42 class SerialServiceTest : public testing::Test, public mojo::ErrorHandler {
43 public:
44 SerialServiceTest() : connected_(false), expecting_error_(false) {}
46 void StoreDevices(mojo::Array<serial::DeviceInfoPtr> devices) {
47 devices_ = devices.Pass();
48 StopMessageLoop();
51 void OnConnectionError() override {
52 StopMessageLoop();
53 EXPECT_TRUE(expecting_error_);
56 void RunMessageLoop() {
57 run_loop_.reset(new base::RunLoop);
58 run_loop_->Run();
61 void StopMessageLoop() {
62 ASSERT_TRUE(run_loop_);
63 message_loop_.PostTask(FROM_HERE, run_loop_->QuitClosure());
66 void OnGotInfo(serial::ConnectionInfoPtr options) {
67 connected_ = true;
68 StopMessageLoop();
71 scoped_refptr<SerialIoHandler> ReturnIoHandler() { return io_handler_; }
73 void RunConnectTest(const std::string& path, bool expecting_success) {
74 if (!io_handler_.get())
75 io_handler_ = new TestSerialIoHandler;
76 mojo::InterfacePtr<serial::SerialService> service;
77 mojo::BindToProxy(
78 new SerialServiceImpl(
79 new SerialConnectionFactory(
80 base::Bind(&SerialServiceTest::ReturnIoHandler,
81 base::Unretained(this)),
82 base::MessageLoopProxy::current()),
83 scoped_ptr<SerialDeviceEnumerator>(new FakeSerialDeviceEnumerator)),
84 &service);
85 mojo::InterfacePtr<serial::Connection> connection;
86 mojo::InterfacePtr<serial::DataSink> sink;
87 mojo::InterfacePtr<serial::DataSource> source;
88 mojo::InterfacePtr<serial::DataSourceClient> source_client;
89 mojo::GetProxy(&source_client);
90 service->Connect(path, serial::ConnectionOptions::New(),
91 mojo::GetProxy(&connection), mojo::GetProxy(&sink),
92 mojo::GetProxy(&source), source_client.Pass());
93 connection.set_error_handler(this);
94 expecting_error_ = !expecting_success;
95 connection->GetInfo(
96 base::Bind(&SerialServiceTest::OnGotInfo, base::Unretained(this)));
97 RunMessageLoop();
98 EXPECT_EQ(!expecting_success, connection.encountered_error());
99 EXPECT_EQ(expecting_success, connected_);
100 connection.reset();
103 base::MessageLoop message_loop_;
104 scoped_ptr<base::RunLoop> run_loop_;
105 mojo::Array<serial::DeviceInfoPtr> devices_;
106 scoped_refptr<TestSerialIoHandler> io_handler_;
107 bool connected_;
108 bool expecting_error_;
109 serial::ConnectionInfoPtr info_;
111 private:
112 DISALLOW_COPY_AND_ASSIGN(SerialServiceTest);
115 TEST_F(SerialServiceTest, GetDevices) {
116 mojo::InterfacePtr<serial::SerialService> service;
117 SerialServiceImpl::Create(NULL, NULL, mojo::GetProxy(&service));
118 service.set_error_handler(this);
119 mojo::Array<serial::DeviceInfoPtr> result;
120 service->GetDevices(
121 base::Bind(&SerialServiceTest::StoreDevices, base::Unretained(this)));
122 RunMessageLoop();
124 // Because we're running on unknown hardware, only check that we received a
125 // non-null result.
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