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

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

Issue 2771073002: Add missing files in //device to appropriate BUILD.gn files. (Closed)
Patch Set: Rebased. Created 3 years, 9 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/power_save_blocker/BUILD.gn ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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.
4
5 #include <memory>
6 #include <utility>
7
8 #include "base/bind.h"
9 #include "base/macros.h"
10 #include "base/memory/ptr_util.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/run_loop.h"
13 #include "base/single_thread_task_runner.h"
14 #include "base/threading/thread_task_runner_handle.h"
15 #include "device/serial/serial.mojom.h"
16 #include "device/serial/serial_service_impl.h"
17 #include "device/serial/test_serial_io_handler.h"
18 #include "mojo/public/cpp/bindings/interface_ptr.h"
19 #include "mojo/public/cpp/bindings/interface_request.h"
20 #include "mojo/public/cpp/bindings/strong_binding.h"
21 #include "testing/gtest/include/gtest/gtest.h"
22
23 namespace device {
24 namespace {
25
26 class FakeSerialDeviceEnumerator : public SerialDeviceEnumerator {
27 std::vector<serial::DeviceInfoPtr> GetDevices() override {
28 std::vector<serial::DeviceInfoPtr> devices(1);
29 devices[0] = serial::DeviceInfo::New();
30 devices[0]->path = "device";
31 return devices;
32 }
33 };
34
35 class FailToOpenIoHandler : public TestSerialIoHandler {
36 public:
37 void Open(const std::string& port,
38 const serial::ConnectionOptions& options,
39 const OpenCompleteCallback& callback) override {
40 callback.Run(false);
41 }
42
43 protected:
44 ~FailToOpenIoHandler() override {}
45 };
46
47 } // namespace
48
49 class SerialServiceTest : public testing::Test {
50 public:
51 SerialServiceTest() : connected_(false), expecting_error_(false) {}
52
53 void StoreDevices(std::vector<serial::DeviceInfoPtr> devices) {
54 devices_ = std::move(devices);
55 StopMessageLoop();
56 }
57
58 void OnConnectionError() {
59 StopMessageLoop();
60 EXPECT_TRUE(expecting_error_);
61 }
62
63 void RunMessageLoop() {
64 run_loop_.reset(new base::RunLoop);
65 run_loop_->Run();
66 }
67
68 void StopMessageLoop() {
69 ASSERT_TRUE(run_loop_);
70 message_loop_.task_runner()->PostTask(FROM_HERE, run_loop_->QuitClosure());
71 }
72
73 void OnGotInfo(serial::ConnectionInfoPtr options) {
74 connected_ = true;
75 StopMessageLoop();
76 }
77
78 scoped_refptr<SerialIoHandler> ReturnIoHandler() { return io_handler_; }
79
80 void RunConnectTest(const std::string& path, bool expecting_success) {
81 if (!io_handler_.get())
82 io_handler_ = new TestSerialIoHandler;
83 mojo::InterfacePtr<serial::SerialService> service;
84 mojo::MakeStrongBinding(
85 base::MakeUnique<SerialServiceImpl>(
86 new SerialConnectionFactory(
87 base::Bind(&SerialServiceTest::ReturnIoHandler,
88 base::Unretained(this)),
89 base::ThreadTaskRunnerHandle::Get()),
90 base::MakeUnique<FakeSerialDeviceEnumerator>()),
91 mojo::MakeRequest(&service));
92 mojo::InterfacePtr<serial::Connection> connection;
93 mojo::InterfacePtr<serial::DataSink> sink;
94 mojo::InterfacePtr<serial::DataSource> source;
95 mojo::InterfacePtr<serial::DataSourceClient> source_client;
96 mojo::MakeRequest(&source_client);
97 service->Connect(path, serial::ConnectionOptions::New(),
98 mojo::MakeRequest(&connection), mojo::MakeRequest(&sink),
99 mojo::MakeRequest(&source), std::move(source_client));
100 connection.set_connection_error_handler(base::Bind(
101 &SerialServiceTest::OnConnectionError, base::Unretained(this)));
102 expecting_error_ = !expecting_success;
103 connection->GetInfo(
104 base::Bind(&SerialServiceTest::OnGotInfo, base::Unretained(this)));
105 RunMessageLoop();
106 EXPECT_EQ(!expecting_success, connection.encountered_error());
107 EXPECT_EQ(expecting_success, connected_);
108 connection.reset();
109 }
110
111 base::MessageLoop message_loop_;
112 std::unique_ptr<base::RunLoop> run_loop_;
113 std::vector<serial::DeviceInfoPtr> devices_;
114 scoped_refptr<TestSerialIoHandler> io_handler_;
115 bool connected_;
116 bool expecting_error_;
117 serial::ConnectionInfoPtr info_;
118
119 private:
120 DISALLOW_COPY_AND_ASSIGN(SerialServiceTest);
121 };
122
123 TEST_F(SerialServiceTest, GetDevices) {
124 mojo::InterfacePtr<serial::SerialService> service;
125 SerialServiceImpl::Create(NULL, NULL, mojo::MakeRequest(&service));
126 service.set_connection_error_handler(base::Bind(
127 &SerialServiceTest::OnConnectionError, base::Unretained(this)));
128 std::vector<serial::DeviceInfoPtr> result;
129 service->GetDevices(
130 base::Bind(&SerialServiceTest::StoreDevices, base::Unretained(this)));
131 RunMessageLoop();
132
133 // Because we're running on unknown hardware, only check that we received a
134 // non-null result.
135 EXPECT_TRUE(devices_);
136 }
137
138 TEST_F(SerialServiceTest, Connect) {
139 RunConnectTest("device", true);
140 }
141
142 TEST_F(SerialServiceTest, ConnectInvalidPath) {
143 RunConnectTest("invalid_path", false);
144 }
145
146 TEST_F(SerialServiceTest, ConnectOpenFailed) {
147 io_handler_ = new FailToOpenIoHandler;
148 RunConnectTest("device", false);
149 }
150
151 } // namespace device
OLDNEW
« no previous file with comments | « device/power_save_blocker/BUILD.gn ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698