Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open foo 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 "gmock/gmock.h" |
| 18 | #include "gtest/gtest.h" |
| 19 | #include "perfetto/ipc/client.h" |
| 20 | #include "perfetto/ipc/host.h" |
| 21 | #include "src/base/test/test_task_runner.h" |
Primiano Tucci | b03ba36 | 2017-12-06 09:47:41 +0000 | [diff] [blame] | 22 | #include "src/ipc/test/test_socket.h" |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 23 | |
| 24 | #include "src/ipc/test/greeter_service.ipc.h" |
| 25 | #include "src/ipc/test/greeter_service.pb.h" |
| 26 | |
| 27 | namespace ipc_test { |
| 28 | namespace { |
| 29 | |
| 30 | using ::testing::_; |
| 31 | using ::testing::Invoke; |
| 32 | using ::perfetto::ipc::AsyncResult; |
| 33 | using ::perfetto::ipc::Client; |
| 34 | using ::perfetto::ipc::Deferred; |
| 35 | using ::perfetto::ipc::Host; |
| 36 | using ::perfetto::ipc::Service; |
| 37 | using ::perfetto::ipc::ServiceProxy; |
| 38 | |
Primiano Tucci | b03ba36 | 2017-12-06 09:47:41 +0000 | [diff] [blame] | 39 | constexpr char kSockName[] = TEST_SOCK_NAME("ipc_integrationtest"); |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 40 | |
| 41 | class MockEventListener : public ServiceProxy::EventListener { |
| 42 | public: |
| 43 | MOCK_METHOD0(OnConnect, void()); |
| 44 | MOCK_METHOD0(OnDisconnect, void()); |
| 45 | }; |
| 46 | |
| 47 | class MockGreeterService : public ipc_test::Greeter { |
| 48 | public: |
| 49 | MOCK_METHOD2(OnSayHello, |
| 50 | void(const GreeterRequestMsg&, DeferredGreeterReplyMsg*)); |
| 51 | void SayHello(const GreeterRequestMsg& request, |
| 52 | DeferredGreeterReplyMsg reply) override { |
| 53 | OnSayHello(request, &reply); |
| 54 | } |
| 55 | |
| 56 | MOCK_METHOD2(OnWaveGoodbye, |
| 57 | void(const GreeterRequestMsg&, DeferredGreeterReplyMsg*)); |
| 58 | void WaveGoodbye(const GreeterRequestMsg& request, |
| 59 | DeferredGreeterReplyMsg reply) override { |
| 60 | OnWaveGoodbye(request, &reply); |
| 61 | } |
| 62 | }; |
| 63 | |
| 64 | class IPCIntegrationTest : public ::testing::Test { |
| 65 | protected: |
Primiano Tucci | b03ba36 | 2017-12-06 09:47:41 +0000 | [diff] [blame] | 66 | void SetUp() override { DESTROY_TEST_SOCK(kSockName); } |
| 67 | void TearDown() override { DESTROY_TEST_SOCK(kSockName); } |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 68 | |
| 69 | perfetto::base::TestTaskRunner task_runner_; |
| 70 | MockEventListener svc_proxy_events_; |
| 71 | }; |
| 72 | |
| 73 | TEST_F(IPCIntegrationTest, SayHelloWaveGoodbye) { |
| 74 | std::unique_ptr<Host> host = Host::CreateInstance(kSockName, &task_runner_); |
| 75 | ASSERT_TRUE(host); |
| 76 | |
| 77 | MockGreeterService* svc = new MockGreeterService(); |
| 78 | ASSERT_TRUE(host->ExposeService(std::unique_ptr<Service>(svc))); |
| 79 | |
| 80 | auto on_connect = task_runner_.CreateCheckpoint("on_connect"); |
| 81 | EXPECT_CALL(svc_proxy_events_, OnConnect()).WillOnce(Invoke(on_connect)); |
| 82 | std::unique_ptr<Client> cli = |
| 83 | Client::CreateInstance(kSockName, &task_runner_); |
| 84 | std::unique_ptr<GreeterProxy> svc_proxy(new GreeterProxy(&svc_proxy_events_)); |
| 85 | cli->BindService(svc_proxy->GetWeakPtr()); |
| 86 | task_runner_.RunUntilCheckpoint("on_connect"); |
| 87 | |
| 88 | { |
| 89 | GreeterRequestMsg req; |
| 90 | req.set_name("Mr Bojangles"); |
| 91 | auto on_reply = task_runner_.CreateCheckpoint("on_hello_reply"); |
| 92 | Deferred<GreeterReplyMsg> deferred_reply( |
| 93 | [on_reply](AsyncResult<GreeterReplyMsg> reply) { |
| 94 | ASSERT_TRUE(reply.success()); |
| 95 | ASSERT_FALSE(reply.has_more()); |
| 96 | ASSERT_EQ("Hello Mr Bojangles", reply->message()); |
| 97 | on_reply(); |
| 98 | }); |
| 99 | |
| 100 | EXPECT_CALL(*svc, OnSayHello(_, _)) |
| 101 | .WillOnce(Invoke([](const GreeterRequestMsg& host_req, |
| 102 | Deferred<GreeterReplyMsg>* host_reply) { |
| 103 | auto reply = AsyncResult<GreeterReplyMsg>::Create(); |
| 104 | reply->set_message("Hello " + host_req.name()); |
| 105 | host_reply->Resolve(std::move(reply)); |
| 106 | })); |
| 107 | svc_proxy->SayHello(req, std::move(deferred_reply)); |
| 108 | task_runner_.RunUntilCheckpoint("on_hello_reply"); |
| 109 | } |
| 110 | |
| 111 | { |
| 112 | GreeterRequestMsg req; |
| 113 | req.set_name("Mrs Bojangles"); |
| 114 | auto on_reply = task_runner_.CreateCheckpoint("on_goodbye_reply"); |
| 115 | Deferred<GreeterReplyMsg> deferred_reply( |
| 116 | [on_reply](AsyncResult<GreeterReplyMsg> reply) { |
| 117 | ASSERT_TRUE(reply.success()); |
| 118 | ASSERT_FALSE(reply.has_more()); |
| 119 | ASSERT_EQ("Goodbye Mrs Bojangles", reply->message()); |
| 120 | on_reply(); |
| 121 | }); |
| 122 | |
| 123 | EXPECT_CALL(*svc, OnWaveGoodbye(_, _)) |
| 124 | .WillOnce(Invoke([](const GreeterRequestMsg& host_req, |
| 125 | Deferred<GreeterReplyMsg>* host_reply) { |
| 126 | auto reply = AsyncResult<GreeterReplyMsg>::Create(); |
| 127 | reply->set_message("Goodbye " + host_req.name()); |
| 128 | host_reply->Resolve(std::move(reply)); |
| 129 | })); |
| 130 | svc_proxy->WaveGoodbye(req, std::move(deferred_reply)); |
| 131 | task_runner_.RunUntilCheckpoint("on_goodbye_reply"); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | } // namespace |
| 136 | } // namespace ipc_test |