Upload upstream chromium 76.0.3809.146
[platform/framework/web/chromium-efl.git] / base / fuchsia / service_provider_impl_unittest.cc
1 // Copyright 2019 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_provider_impl.h"
6
7 #include <lib/zx/channel.h>
8 #include <utility>
9
10 #include "base/fuchsia/scoped_service_binding.h"
11 #include "base/fuchsia/service_directory.h"
12 #include "base/fuchsia/test_interface_impl.h"
13 #include "base/fuchsia/testfidl/cpp/fidl.h"
14 #include "base/message_loop/message_loop.h"
15 #include "base/run_loop.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17
18 namespace base {
19 namespace fuchsia {
20
21 class ServiceProviderImplTest : public testing::Test {
22  public:
23   ServiceProviderImplTest() = default;
24   ~ServiceProviderImplTest() override = default;
25
26   void VerifyTestInterface(fidl::InterfacePtr<testfidl::TestInterface>* stub,
27                            zx_status_t expected_error) {
28     // Call the service and wait for response.
29     base::RunLoop run_loop;
30     zx_status_t actual_error = ZX_OK;
31
32     stub->set_error_handler([&run_loop, &actual_error](zx_status_t status) {
33       actual_error = status;
34       run_loop.Quit();
35     });
36
37     (*stub)->Add(2, 2, [&run_loop](int32_t result) {
38       EXPECT_EQ(result, 4);
39       run_loop.Quit();
40     });
41
42     run_loop.Run();
43
44     EXPECT_EQ(expected_error, actual_error);
45
46     // Reset error handler because the current one captures |run_loop| and
47     // |error| references which are about to be destroyed.
48     stub->set_error_handler(nullptr);
49   }
50
51  protected:
52   MessageLoopForIO message_loop_;
53   TestInterfaceImpl test_service_;
54
55   DISALLOW_COPY_AND_ASSIGN(ServiceProviderImplTest);
56 };
57
58 // Verifies that we can connect to the service service more than once.
59 TEST_F(ServiceProviderImplTest, ConnectMulti) {
60   fidl::InterfaceHandle<::fuchsia::io::Directory> directory_channel;
61   ServiceDirectory service_directory(directory_channel.NewRequest());
62   ServiceProviderImpl provider_impl(std::move(directory_channel));
63   ScopedServiceBinding<testfidl::TestInterface> service_binding(
64       &service_directory, &test_service_);
65
66   ::fuchsia::sys::ServiceProviderPtr provider_client;
67   provider_impl.AddBinding(provider_client.NewRequest());
68
69   testfidl::TestInterfacePtr stub;
70   provider_client->ConnectToService(testfidl::TestInterface::Name_,
71                                     stub.NewRequest().TakeChannel());
72
73   testfidl::TestInterfacePtr stub2;
74   provider_client->ConnectToService(testfidl::TestInterface::Name_,
75                                     stub2.NewRequest().TakeChannel());
76
77   VerifyTestInterface(&stub, ZX_OK);
78   VerifyTestInterface(&stub2, ZX_OK);
79 }
80
81 // Verify that the case when the service doesn't exist is handled properly.
82 TEST_F(ServiceProviderImplTest, NoService) {
83   fidl::InterfaceHandle<::fuchsia::io::Directory> directory_channel;
84   ServiceDirectory service_directory(directory_channel.NewRequest());
85   ServiceProviderImpl provider_impl(std::move(directory_channel));
86
87   ::fuchsia::sys::ServiceProviderPtr provider_client;
88   provider_impl.AddBinding(provider_client.NewRequest());
89
90   testfidl::TestInterfacePtr stub;
91   provider_client->ConnectToService(testfidl::TestInterface::Name_,
92                                     stub.NewRequest().TakeChannel());
93
94   VerifyTestInterface(&stub, ZX_ERR_PEER_CLOSED);
95 }
96
97 }  // namespace fuchsia
98 }  // namespace base