1 /*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "perfetto/ext/ipc/client.h"
18 #include "perfetto/ext/ipc/host.h"
19 #include "src/base/test/test_task_runner.h"
20 #include "src/ipc/test/test_socket.h"
21 #include "test/gtest_and_gmock.h"
22
23 #include "src/ipc/test/greeter_service.gen.h"
24 #include "src/ipc/test/greeter_service.ipc.h"
25
26 namespace ipc_test {
27 namespace {
28
29 using ::testing::_;
30 using ::testing::Invoke;
31 using ::perfetto::ipc::AsyncResult;
32 using ::perfetto::ipc::Client;
33 using ::perfetto::ipc::Deferred;
34 using ::perfetto::ipc::Host;
35 using ::perfetto::ipc::Service;
36 using ::perfetto::ipc::ServiceProxy;
37
38 using namespace ::ipc_test::gen;
39
40 ::perfetto::ipc::TestSocket kTestSocket{"ipc_integrationtest"};
41
42 class MockEventListener : public ServiceProxy::EventListener {
43 public:
44 MOCK_METHOD0(OnConnect, void());
45 MOCK_METHOD0(OnDisconnect, void());
46 };
47
48 class MockGreeterService : public ::ipc_test::gen::Greeter {
49 public:
50 MOCK_METHOD2(OnSayHello,
51 void(const GreeterRequestMsg&, DeferredGreeterReplyMsg*));
SayHello(const GreeterRequestMsg & request,DeferredGreeterReplyMsg reply)52 void SayHello(const GreeterRequestMsg& request,
53 DeferredGreeterReplyMsg reply) override {
54 OnSayHello(request, &reply);
55 }
56
57 MOCK_METHOD2(OnWaveGoodbye,
58 void(const GreeterRequestMsg&, DeferredGreeterReplyMsg*));
WaveGoodbye(const GreeterRequestMsg & request,DeferredGreeterReplyMsg reply)59 void WaveGoodbye(const GreeterRequestMsg& request,
60 DeferredGreeterReplyMsg reply) override {
61 OnWaveGoodbye(request, &reply);
62 }
63 };
64
65 class IPCIntegrationTest : public ::testing::Test {
66 protected:
SetUp()67 void SetUp() override { kTestSocket.Destroy(); }
TearDown()68 void TearDown() override { kTestSocket.Destroy(); }
69
70 perfetto::base::TestTaskRunner task_runner_;
71 MockEventListener svc_proxy_events_;
72 };
73
TEST_F(IPCIntegrationTest,SayHelloWaveGoodbye)74 TEST_F(IPCIntegrationTest, SayHelloWaveGoodbye) {
75 std::unique_ptr<Host> host =
76 Host::CreateInstance(kTestSocket.name(), &task_runner_);
77 ASSERT_TRUE(host);
78
79 MockGreeterService* svc = new MockGreeterService();
80 ASSERT_TRUE(host->ExposeService(std::unique_ptr<Service>(svc)));
81
82 auto on_connect = task_runner_.CreateCheckpoint("on_connect");
83 EXPECT_CALL(svc_proxy_events_, OnConnect()).WillOnce(Invoke(on_connect));
84 std::unique_ptr<Client> cli = Client::CreateInstance(
85 {kTestSocket.name(), /*retry=*/false}, &task_runner_);
86 std::unique_ptr<GreeterProxy> svc_proxy(new GreeterProxy(&svc_proxy_events_));
87 cli->BindService(svc_proxy->GetWeakPtr());
88 task_runner_.RunUntilCheckpoint("on_connect");
89
90 {
91 GreeterRequestMsg req;
92 req.set_name("Mr Bojangles");
93 auto on_reply = task_runner_.CreateCheckpoint("on_hello_reply");
94 Deferred<GreeterReplyMsg> deferred_reply(
95 [on_reply](AsyncResult<GreeterReplyMsg> reply) {
96 ASSERT_TRUE(reply.success());
97 ASSERT_FALSE(reply.has_more());
98 ASSERT_EQ("Hello Mr Bojangles", reply->message());
99 on_reply();
100 });
101
102 EXPECT_CALL(*svc, OnSayHello(_, _))
103 .WillOnce(Invoke([](const GreeterRequestMsg& host_req,
104 Deferred<GreeterReplyMsg>* host_reply) {
105 auto reply = AsyncResult<GreeterReplyMsg>::Create();
106 reply->set_message("Hello " + host_req.name());
107 host_reply->Resolve(std::move(reply));
108 }));
109 svc_proxy->SayHello(req, std::move(deferred_reply));
110 task_runner_.RunUntilCheckpoint("on_hello_reply");
111 }
112
113 {
114 GreeterRequestMsg req;
115 req.set_name("Mrs Bojangles");
116 auto on_reply = task_runner_.CreateCheckpoint("on_goodbye_reply");
117 Deferred<GreeterReplyMsg> deferred_reply(
118 [on_reply](AsyncResult<GreeterReplyMsg> reply) {
119 ASSERT_TRUE(reply.success());
120 ASSERT_FALSE(reply.has_more());
121 ASSERT_EQ("Goodbye Mrs Bojangles", reply->message());
122 on_reply();
123 });
124
125 EXPECT_CALL(*svc, OnWaveGoodbye(_, _))
126 .WillOnce(Invoke([](const GreeterRequestMsg& host_req,
127 Deferred<GreeterReplyMsg>* host_reply) {
128 auto reply = AsyncResult<GreeterReplyMsg>::Create();
129 reply->set_message("Goodbye " + host_req.name());
130 host_reply->Resolve(std::move(reply));
131 }));
132 svc_proxy->WaveGoodbye(req, std::move(deferred_reply));
133 task_runner_.RunUntilCheckpoint("on_goodbye_reply");
134 }
135 }
136
137 } // namespace
138 } // namespace ipc_test
139