1 // Copyright (c) 2017 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 "services/device/serial/serial_port_impl.h"
7 #include "base/macros.h"
8 #include "mojo/public/cpp/bindings/interface_request.h"
9 #include "mojo/public/cpp/bindings/strong_binding.h"
10 #include "services/device/device_service_test_base.h"
11 #include "services/device/public/mojom/serial.mojom.h"
17 class SerialPortImplTest : public DeviceServiceTestBase {
19 SerialPortImplTest() = default;
20 ~SerialPortImplTest() override = default;
23 DISALLOW_COPY_AND_ASSIGN(SerialPortImplTest);
26 TEST_F(SerialPortImplTest, WatcherClosedWhenPortClosed) {
27 mojom::SerialPortPtr serial_port;
28 mojom::SerialPortConnectionWatcherPtrInfo watcher_ptr;
29 auto watcher_binding = mojo::MakeStrongBinding(
30 std::make_unique<mojom::SerialPortConnectionWatcher>(),
31 mojo::MakeRequest(&watcher_ptr));
32 SerialPortImpl::Create(base::FilePath(), mojo::MakeRequest(&serial_port),
33 std::move(watcher_ptr),
34 base::ThreadTaskRunnerHandle::Get());
36 // To start with both the serial port connection and the connection watcher
37 // connection should remain open.
38 serial_port.FlushForTesting();
39 EXPECT_FALSE(serial_port.encountered_error());
40 watcher_binding->FlushForTesting();
41 EXPECT_TRUE(watcher_binding);
43 // When the serial port connection is closed the watcher connection should be
46 watcher_binding->FlushForTesting();
47 EXPECT_FALSE(watcher_binding);
50 TEST_F(SerialPortImplTest, PortClosedWhenWatcherClosed) {
51 mojom::SerialPortPtr serial_port;
52 mojom::SerialPortConnectionWatcherPtrInfo watcher_ptr;
53 auto watcher_binding = mojo::MakeStrongBinding(
54 std::make_unique<mojom::SerialPortConnectionWatcher>(),
55 mojo::MakeRequest(&watcher_ptr));
56 SerialPortImpl::Create(base::FilePath(), mojo::MakeRequest(&serial_port),
57 std::move(watcher_ptr),
58 base::ThreadTaskRunnerHandle::Get());
60 // To start with both the serial port connection and the connection watcher
61 // connection should remain open.
62 serial_port.FlushForTesting();
63 EXPECT_FALSE(serial_port.encountered_error());
64 watcher_binding->FlushForTesting();
65 EXPECT_TRUE(watcher_binding);
67 // When the watcher connection is closed, for safety, the serial port
68 // connection should also be closed.
69 watcher_binding->Close();
70 serial_port.FlushForTesting();
71 EXPECT_TRUE(serial_port.encountered_error());