Upload upstream chromium 76.0.3809.146
[platform/framework/web/chromium-efl.git] / services / device / serial / serial_port_impl_unittest.cc
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.
4
5 #include "services/device/serial/serial_port_impl.h"
6
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"
12
13 namespace device {
14
15 namespace {
16
17 class SerialPortImplTest : public DeviceServiceTestBase {
18  public:
19   SerialPortImplTest() = default;
20   ~SerialPortImplTest() override = default;
21
22  protected:
23   DISALLOW_COPY_AND_ASSIGN(SerialPortImplTest);
24 };
25
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());
35
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);
42
43   // When the serial port connection is closed the watcher connection should be
44   // closed.
45   serial_port.reset();
46   watcher_binding->FlushForTesting();
47   EXPECT_FALSE(watcher_binding);
48 }
49
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());
59
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);
66
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());
72 }
73
74 }  // namespace
75
76 }  // namespace device