Upload upstream chromium 76.0.3809.146
[platform/framework/web/chromium-efl.git] / base / fuchsia / service_directory_unittest.cc
1 // Copyright 2018 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 "base/fuchsia/service_directory.h"
6
7 #include <lib/fdio/fdio.h>
8 #include <lib/zx/channel.h>
9 #include <utility>
10
11 #include "base/bind.h"
12 #include "base/fuchsia/service_directory_test_base.h"
13 #include "base/location.h"
14 #include "base/run_loop.h"
15 #include "base/task_runner.h"
16 #include "base/test/test_timeouts.h"
17 #include "base/threading/thread_task_runner_handle.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19
20 namespace base {
21 namespace fuchsia {
22
23 class ServiceDirectoryTest : public ServiceDirectoryTestBase {};
24
25 // Verifies that ServiceDirectoryClient can consume a public service in
26 // ServiceDirectory and that connection is disconnected when the client stub is
27 // destroyed.
28 TEST_F(ServiceDirectoryTest, ConnectDisconnect) {
29   auto stub = public_service_directory_client_
30                   ->ConnectToService<testfidl::TestInterface>();
31   VerifyTestInterface(&stub, ZX_OK);
32
33   base::RunLoop run_loop;
34   service_binding_->SetOnLastClientCallback(run_loop.QuitClosure());
35
36   base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
37       FROM_HERE,
38       base::BindOnce(
39           [](base::RunLoop* run_loop) {
40             ADD_FAILURE();
41             run_loop->Quit();
42           },
43           &run_loop),
44       TestTimeouts::action_timeout());
45
46   stub.Unbind();
47   run_loop.Run();
48 }
49
50 // Verify that we can connect to a service through both "public" and "svc".
51 TEST_F(ServiceDirectoryTest, ConnectNewAndLegacyServices) {
52   auto stub = public_service_directory_client_
53                   ->ConnectToService<testfidl::TestInterface>();
54   auto stub2 = legacy_public_service_directory_client_
55                    ->ConnectToService<testfidl::TestInterface>();
56   VerifyTestInterface(&stub, ZX_OK);
57   VerifyTestInterface(&stub2, ZX_OK);
58 }
59
60 // Verify that we can connect to the same service more than once.
61 TEST_F(ServiceDirectoryTest, ConnectMulti) {
62   auto stub = public_service_directory_client_
63                   ->ConnectToService<testfidl::TestInterface>();
64   auto stub2 = public_service_directory_client_
65                    ->ConnectToService<testfidl::TestInterface>();
66   VerifyTestInterface(&stub, ZX_OK);
67   VerifyTestInterface(&stub2, ZX_OK);
68 }
69
70 // Verify that services are also exported to the legacy flat service namespace.
71 TEST_F(ServiceDirectoryTest, ConnectLegacy) {
72   auto stub = root_service_directory_client_
73                   ->ConnectToService<testfidl::TestInterface>();
74   VerifyTestInterface(&stub, ZX_OK);
75 }
76
77 // Verify that ServiceDirectoryClient can handle the case when the service
78 // directory connection is disconnected.
79 TEST_F(ServiceDirectoryTest, DirectoryGone) {
80   service_binding_.reset();
81   service_directory_.reset();
82
83   fidl::InterfacePtr<testfidl::TestInterface> stub;
84   zx_status_t status =
85       public_service_directory_client_->ConnectToService(stub.NewRequest());
86   EXPECT_EQ(status, ZX_ERR_PEER_CLOSED);
87
88   VerifyTestInterface(&stub, ZX_ERR_PEER_CLOSED);
89 }
90
91 // Verify that the case when the service doesn't exist is handled properly.
92 TEST_F(ServiceDirectoryTest, NoService) {
93   service_binding_.reset();
94   auto stub = public_service_directory_client_
95                   ->ConnectToService<testfidl::TestInterface>();
96   VerifyTestInterface(&stub, ZX_ERR_PEER_CLOSED);
97 }
98
99 // Verify that we can connect to a debug service.
100 TEST_F(ServiceDirectoryTest, ConnectDebugService) {
101   // Remove the public service binding.
102   service_binding_.reset();
103
104   // Publish the test service to the "debug" directory.
105   ScopedServiceBinding<testfidl::TestInterface> debug_service_binding(
106       service_directory_->debug(), &test_service_);
107
108   auto debug_stub = debug_service_directory_client_
109                         ->ConnectToService<testfidl::TestInterface>();
110   VerifyTestInterface(&debug_stub, ZX_OK);
111
112   auto release_stub = public_service_directory_client_
113                           ->ConnectToService<testfidl::TestInterface>();
114   VerifyTestInterface(&release_stub, ZX_ERR_PEER_CLOSED);
115 }
116
117 }  // namespace fuchsia
118 }  // namespace base