Upload upstream chromium 76.0.3809.146
[platform/framework/web/chromium-efl.git] / base / fuchsia / service_directory_test_base.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_test_base.h"
6
7 #include <lib/fdio/directory.h>
8 #include <utility>
9
10 namespace base {
11 namespace fuchsia {
12
13 ServiceDirectoryTestBase::ServiceDirectoryTestBase() {
14   // TODO(https://crbug.com/920920): Remove the ServiceDirectory's implicit
15   // "public" sub-directory and update this setup logic.
16
17   // Mount service dir and publish the service.
18   fidl::InterfaceHandle<::fuchsia::io::Directory> directory;
19   service_directory_ =
20       std::make_unique<ServiceDirectory>(directory.NewRequest());
21   service_binding_ =
22       std::make_unique<ScopedServiceBinding<testfidl::TestInterface>>(
23           service_directory_.get(), &test_service_);
24
25   // Create the ServiceDirectoryClient, connected to the "svc" sub-directory.
26   fidl::InterfaceHandle<::fuchsia::io::Directory> svc_directory;
27   CHECK_EQ(fdio_service_connect_at(
28                directory.channel().get(), "/svc/.",
29                svc_directory.NewRequest().TakeChannel().release()),
30            ZX_OK);
31   public_service_directory_client_ =
32       std::make_unique<ServiceDirectoryClient>(std::move(svc_directory));
33
34   // Create the ServiceDirectoryClient, connected to the "debug" sub-directory.
35   fidl::InterfaceHandle<::fuchsia::io::Directory> debug_directory;
36   CHECK_EQ(fdio_service_connect_at(
37                directory.channel().get(), "/debug/.",
38                debug_directory.NewRequest().TakeChannel().release()),
39            ZX_OK);
40   debug_service_directory_client_ =
41       std::make_unique<ServiceDirectoryClient>(std::move(debug_directory));
42
43   // Create the ServiceDirectoryClient, connected to the "public" sub-directory
44   // (same contents as "svc", provided for compatibility).
45   fidl::InterfaceHandle<::fuchsia::io::Directory> public_directory;
46   CHECK_EQ(fdio_service_connect_at(
47                directory.channel().get(), "/public/.",
48                public_directory.NewRequest().TakeChannel().release()),
49            ZX_OK);
50   legacy_public_service_directory_client_ =
51       std::make_unique<ServiceDirectoryClient>(std::move(public_directory));
52
53   // Create a ServiceDirectoryClient for the "private" part of the directory.
54   root_service_directory_client_ =
55       std::make_unique<ServiceDirectoryClient>(std::move(directory));
56 }
57
58 ServiceDirectoryTestBase::~ServiceDirectoryTestBase() = default;
59
60 void ServiceDirectoryTestBase::VerifyTestInterface(
61     fidl::InterfacePtr<testfidl::TestInterface>* stub,
62     zx_status_t expected_error) {
63   // Call the service and wait for response.
64   base::RunLoop run_loop;
65   zx_status_t actual_error = ZX_OK;
66
67   stub->set_error_handler([&run_loop, &actual_error](zx_status_t status) {
68     actual_error = status;
69     run_loop.Quit();
70   });
71
72   (*stub)->Add(2, 2, [&run_loop](int32_t result) {
73     EXPECT_EQ(result, 4);
74     run_loop.Quit();
75   });
76
77   run_loop.Run();
78
79   EXPECT_EQ(expected_error, actual_error);
80
81   // Reset error handler because the current one captures |run_loop| and
82   // |error| references which are about to be destroyed.
83   stub->set_error_handler(nullptr);
84 }
85
86 }  // namespace fuchsia
87 }  // namespace base