1 // Copyright 2013 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.
5 #include "mojo/public/bindings/allocation_scope.h"
6 #include "mojo/public/bindings/remote_ptr.h"
7 #include "mojo/public/environment/environment.h"
8 #include "mojo/public/tests/test_utils.h"
9 #include "mojo/public/utility/run_loop.h"
10 #include "mojom/sample_factory.h"
11 #include "testing/gtest/include/gtest/gtest.h"
17 const char kText1[] = "hello";
18 const char kText2[] = "world";
20 class SampleFactoryImpl : public sample::Factory {
22 explicit SampleFactoryImpl(sample::ScopedFactoryClientHandle handle)
23 : client_(handle.Pass(), this) {
26 virtual void DoStuff(const sample::Request& request,
27 ScopedMessagePipeHandle pipe) MOJO_OVERRIDE {
30 EXPECT_TRUE(ReadTextMessage(pipe.get(), &text1));
33 if (request.pipe().is_valid()) {
34 EXPECT_TRUE(ReadTextMessage(request.pipe().get(), &text2));
36 // Ensure that simply accessing request.pipe() does not close it.
37 EXPECT_TRUE(request.pipe().is_valid());
40 ScopedMessagePipeHandle pipe0;
42 CreateMessagePipe(&pipe0, &pipe1_);
43 EXPECT_TRUE(WriteTextMessage(pipe1_.get(), text2));
46 AllocationScope scope;
47 sample::Response::Builder response;
49 response.set_pipe(pipe0.Pass());
50 client_->DidStuff(response.Finish(), text1);
53 virtual void DoStuff2(ScopedDataPipeConsumerHandle pipe) MOJO_OVERRIDE {
54 // Read the data from the pipe, writing the response (as a string) to
56 ASSERT_TRUE(pipe.is_valid());
57 uint32_t data_size = 0;
58 ASSERT_EQ(MOJO_RESULT_OK,
59 ReadDataRaw(pipe.get(), NULL, &data_size,
60 MOJO_READ_DATA_FLAG_QUERY));
61 ASSERT_NE(0, static_cast<int>(data_size));
63 ASSERT_LT(static_cast<int>(data_size), 64);
64 ASSERT_EQ(MOJO_RESULT_OK,
65 ReadDataRaw(pipe.get(), data, &data_size,
66 MOJO_READ_DATA_FLAG_ALL_OR_NONE));
68 AllocationScope scope;
69 client_->DidStuff2(String(std::string(data)));
73 RemotePtr<sample::FactoryClient> client_;
74 ScopedMessagePipeHandle pipe1_;
77 class SampleFactoryClientImpl : public sample::FactoryClient {
79 explicit SampleFactoryClientImpl(sample::ScopedFactoryHandle handle)
80 : factory_(handle.Pass(), this),
81 got_response_(false) {
85 expected_text_reply_ = kText1;
87 ScopedMessagePipeHandle pipe0;
88 CreateMessagePipe(&pipe0, &pipe1_);
90 EXPECT_TRUE(WriteTextMessage(pipe1_.get(), kText1));
92 ScopedMessagePipeHandle pipe2;
93 CreateMessagePipe(&pipe2, &pipe3_);
95 EXPECT_TRUE(WriteTextMessage(pipe3_.get(), kText2));
97 AllocationScope scope;
98 sample::Request::Builder request;
100 request.set_pipe(pipe2.Pass());
101 factory_->DoStuff(request.Finish(), pipe0.Pass());
104 void StartNoPipes() {
105 expected_text_reply_.clear();
107 AllocationScope scope;
108 sample::Request::Builder request;
110 factory_->DoStuff(request.Finish(), ScopedMessagePipeHandle().Pass());
113 // Writes a string to a data pipe and passes the data pipe (consumer) to the
115 void StartDataPipe() {
116 expected_text_reply_.clear();
118 ScopedDataPipeProducerHandle producer_handle;
119 ScopedDataPipeConsumerHandle consumer_handle;
120 MojoCreateDataPipeOptions options = {
121 sizeof(MojoCreateDataPipeOptions),
122 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE,
125 ASSERT_EQ(MOJO_RESULT_OK,
126 CreateDataPipe(&options, &producer_handle, &consumer_handle));
127 expected_text_reply_ = "got it";
129 uint32_t data_size = static_cast<uint32_t>(expected_text_reply_.size() + 1);
130 ASSERT_EQ(MOJO_RESULT_OK,
131 WriteDataRaw(producer_handle.get(), expected_text_reply_.c_str(),
132 &data_size, MOJO_WRITE_DATA_FLAG_ALL_OR_NONE));
134 AllocationScope scope;
135 factory_->DoStuff2(consumer_handle.Pass());
138 bool got_response() const {
139 return got_response_;
142 virtual void DidStuff(const sample::Response& response,
143 const String& text_reply) MOJO_OVERRIDE {
144 EXPECT_EQ(expected_text_reply_, text_reply.To<std::string>());
146 if (response.pipe().is_valid()) {
148 EXPECT_TRUE(ReadTextMessage(response.pipe().get(), &text2));
150 // Ensure that simply accessing response.pipe() does not close it.
151 EXPECT_TRUE(response.pipe().is_valid());
153 EXPECT_EQ(std::string(kText2), text2);
155 // Do some more tests of handle passing:
156 ScopedMessagePipeHandle p = response.pipe().Pass();
157 EXPECT_TRUE(p.is_valid());
158 EXPECT_FALSE(response.pipe().is_valid());
161 got_response_ = true;
164 virtual void DidStuff2(const String& text_reply) MOJO_OVERRIDE {
165 got_response_ = true;
166 EXPECT_EQ(expected_text_reply_, text_reply.To<std::string>());
170 RemotePtr<sample::Factory> factory_;
171 ScopedMessagePipeHandle pipe1_;
172 ScopedMessagePipeHandle pipe3_;
173 std::string expected_text_reply_;
179 class HandlePassingTest : public testing::Test {
181 void PumpMessages() {
182 loop_.RunUntilIdle();
190 TEST_F(HandlePassingTest, Basic) {
191 InterfacePipe<sample::Factory> pipe;
193 SampleFactoryImpl factory(pipe.handle_to_peer.Pass());
194 SampleFactoryClientImpl factory_client(pipe.handle_to_self.Pass());
196 factory_client.Start();
198 EXPECT_FALSE(factory_client.got_response());
202 EXPECT_TRUE(factory_client.got_response());
205 TEST_F(HandlePassingTest, PassInvalid) {
206 InterfacePipe<sample::Factory> pipe;
208 SampleFactoryImpl factory(pipe.handle_to_peer.Pass());
209 SampleFactoryClientImpl factory_client(pipe.handle_to_self.Pass());
211 factory_client.StartNoPipes();
213 EXPECT_FALSE(factory_client.got_response());
217 EXPECT_TRUE(factory_client.got_response());
220 // Verifies DataPipeConsumer can be passed and read from.
221 TEST_F(HandlePassingTest, DataPipe) {
222 InterfacePipe<sample::Factory> pipe;
224 SampleFactoryImpl factory(pipe.handle_to_peer.Pass());
225 SampleFactoryClientImpl factory_client(pipe.handle_to_self.Pass());
227 ASSERT_NO_FATAL_FAILURE(factory_client.StartDataPipe());
229 EXPECT_FALSE(factory_client.got_response());
233 EXPECT_TRUE(factory_client.got_response());