1 // Copyright 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 "base/macros.h"
6 #include "base/test/scoped_task_environment.h"
7 #include "mojo/public/cpp/bindings/binding.h"
8 #include "mojo/public/cpp/system/message_pipe.h"
9 #include "mojo/public/cpp/system/wait.h"
10 #include "mojo/public/interfaces/bindings/tests/ping_service.mojom.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 
13 namespace mojo {
14 namespace {
15 
16 class TestHelperTest : public testing::Test {
17  public:
18   TestHelperTest() = default;
19   ~TestHelperTest() override = default;
20 
21  private:
22   base::test::ScopedTaskEnvironment task_environment_;
23 
24   DISALLOW_COPY_AND_ASSIGN(TestHelperTest);
25 };
26 
27 class PingImpl : public test::PingService {
28  public:
PingImpl(test::PingServiceRequest request)29   explicit PingImpl(test::PingServiceRequest request)
30       : binding_(this, std::move(request)) {}
31   ~PingImpl() override = default;
32 
pinged() const33   bool pinged() const { return pinged_; }
34 
35   // test::PingService:
Ping(const PingCallback & callback)36   void Ping(const PingCallback& callback) override {
37     pinged_ = true;
38     callback.Run();
39   }
40 
41  private:
42   bool pinged_ = false;
43   Binding<test::PingService> binding_;
44 
45   DISALLOW_COPY_AND_ASSIGN(PingImpl);
46 };
47 
48 class EchoImpl : public test::EchoService {
49  public:
EchoImpl(test::EchoServiceRequest request)50   explicit EchoImpl(test::EchoServiceRequest request)
51       : binding_(this, std::move(request)) {}
52   ~EchoImpl() override = default;
53 
54   // test::EchoService:
Echo(const std::string & message,const EchoCallback & callback)55   void Echo(const std::string& message, const EchoCallback& callback) override {
56     callback.Run(message);
57   }
58 
59  private:
60   Binding<test::EchoService> binding_;
61 
62   DISALLOW_COPY_AND_ASSIGN(EchoImpl);
63 };
64 
65 class TrampolineImpl : public test::HandleTrampoline {
66  public:
TrampolineImpl(test::HandleTrampolineRequest request)67   explicit TrampolineImpl(test::HandleTrampolineRequest request)
68       : binding_(this, std::move(request)) {}
69   ~TrampolineImpl() override = default;
70 
71   // test::HandleTrampoline:
BounceOne(ScopedMessagePipeHandle one,const BounceOneCallback & callback)72   void BounceOne(ScopedMessagePipeHandle one,
73                  const BounceOneCallback& callback) override {
74     callback.Run(std::move(one));
75   }
76 
BounceTwo(ScopedMessagePipeHandle one,ScopedMessagePipeHandle two,const BounceTwoCallback & callback)77   void BounceTwo(ScopedMessagePipeHandle one,
78                  ScopedMessagePipeHandle two,
79                  const BounceTwoCallback& callback) override {
80     callback.Run(std::move(one), std::move(two));
81   }
82 
83  private:
84   Binding<test::HandleTrampoline> binding_;
85 
86   DISALLOW_COPY_AND_ASSIGN(TrampolineImpl);
87 };
88 
TEST_F(TestHelperTest,AsyncWaiter)89 TEST_F(TestHelperTest, AsyncWaiter) {
90   test::PingServicePtr ping;
91   PingImpl ping_impl(MakeRequest(&ping));
92 
93   test::PingServiceAsyncWaiter wait_for_ping(ping.get());
94   EXPECT_FALSE(ping_impl.pinged());
95   wait_for_ping.Ping();
96   EXPECT_TRUE(ping_impl.pinged());
97 
98   test::EchoServicePtr echo;
99   EchoImpl echo_impl(MakeRequest(&echo));
100 
101   test::EchoServiceAsyncWaiter wait_for_echo(echo.get());
102   const std::string kTestString = "a machine that goes 'ping'";
103   std::string response;
104   wait_for_echo.Echo(kTestString, &response);
105   EXPECT_EQ(kTestString, response);
106 
107   test::HandleTrampolinePtr trampoline;
108   TrampolineImpl trampoline_impl(MakeRequest(&trampoline));
109 
110   test::HandleTrampolineAsyncWaiter wait_for_trampoline(trampoline.get());
111   MessagePipe pipe;
112   ScopedMessagePipeHandle handle0, handle1;
113   WriteMessageRaw(pipe.handle0.get(), kTestString.data(), kTestString.size(),
114                   nullptr, 0, MOJO_WRITE_MESSAGE_FLAG_NONE);
115   wait_for_trampoline.BounceOne(std::move(pipe.handle0), &handle0);
116   wait_for_trampoline.BounceTwo(std::move(handle0), std::move(pipe.handle1),
117                                 &handle0, &handle1);
118 
119   // Verify that our pipe handles are the same as the original pipe.
120   Wait(handle1.get(), MOJO_HANDLE_SIGNAL_READABLE);
121   std::vector<uint8_t> payload;
122   ReadMessageRaw(handle1.get(), &payload, nullptr, MOJO_READ_MESSAGE_FLAG_NONE);
123   std::string original_message(payload.begin(), payload.end());
124   EXPECT_EQ(kTestString, original_message);
125 }
126 
127 }  // namespace
128 }  // namespace mojo
129