OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include <utility> | |
6 | |
7 #include "gtest/gtest.h" | |
8 #include "mojo/public/cpp/bindings/binding.h" | |
9 #include "mojo/public/cpp/bindings/strong_binding.h" | |
10 #include "mojo/public/cpp/test_support/test_utils.h" | |
11 #include "mojo/public/cpp/utility/run_loop.h" | |
12 #include "mojo/public/interfaces/bindings/tests/sample_factory.mojom.h" | |
13 | |
14 namespace mojo { | |
15 namespace test { | |
16 namespace { | |
17 | |
18 const char kText1[] = "hello"; | |
19 const char kText2[] = "world"; | |
20 | |
21 class StringRecorder { | |
22 public: | |
23 explicit StringRecorder(std::string* buf) : buf_(buf) {} | |
24 void Run(const String& a) const { *buf_ = a.To<std::string>(); } | |
25 | |
26 private: | |
27 std::string* buf_; | |
28 }; | |
29 | |
30 class ImportedInterfaceImpl : public imported::ImportedInterface { | |
31 public: | |
32 explicit ImportedInterfaceImpl( | |
33 InterfaceRequest<imported::ImportedInterface> request) | |
34 : binding_(this, request.Pass()) {} | |
35 | |
36 void DoSomething() override { do_something_count_++; } | |
37 | |
38 static int do_something_count() { return do_something_count_; } | |
39 | |
40 private: | |
41 static int do_something_count_; | |
42 Binding<ImportedInterface> binding_; | |
43 }; | |
44 int ImportedInterfaceImpl::do_something_count_ = 0; | |
45 | |
46 class SampleNamedObjectImpl : public sample::NamedObject { | |
47 public: | |
48 explicit SampleNamedObjectImpl(InterfaceRequest<sample::NamedObject> request) | |
49 : binding_(this, request.Pass()) {} | |
50 void SetName(const mojo::String& name) override { name_ = name; } | |
51 | |
52 void GetName(const mojo::Callback<void(mojo::String)>& callback) override { | |
53 callback.Run(name_); | |
54 } | |
55 | |
56 private: | |
57 std::string name_; | |
58 StrongBinding<sample::NamedObject> binding_; | |
59 }; | |
60 | |
61 class SampleFactoryImpl : public sample::Factory { | |
62 public: | |
63 explicit SampleFactoryImpl(InterfaceRequest<sample::Factory> request) | |
64 : binding_(this, request.Pass()) {} | |
65 | |
66 void DoStuff(sample::RequestPtr request, | |
67 ScopedMessagePipeHandle pipe, | |
68 const DoStuffCallback& callback) override { | |
69 std::string text1; | |
70 if (pipe.is_valid()) | |
71 EXPECT_TRUE(ReadTextMessage(pipe.get(), &text1)); | |
72 | |
73 std::string text2; | |
74 if (request->pipe.is_valid()) { | |
75 EXPECT_TRUE(ReadTextMessage(request->pipe.get(), &text2)); | |
76 | |
77 // Ensure that simply accessing request->pipe does not close it. | |
78 EXPECT_TRUE(request->pipe.is_valid()); | |
79 } | |
80 | |
81 ScopedMessagePipeHandle pipe0; | |
82 if (!text2.empty()) { | |
83 CreateMessagePipe(nullptr, &pipe0, &pipe1_); | |
84 EXPECT_TRUE(WriteTextMessage(pipe1_.get(), text2)); | |
85 } | |
86 | |
87 sample::ResponsePtr response(sample::Response::New()); | |
88 response->x = 2; | |
89 response->pipe = pipe0.Pass(); | |
90 callback.Run(response.Pass(), text1); | |
91 | |
92 if (request->obj) | |
93 imported::ImportedInterfacePtr::Create(std::move(request->obj)) | |
94 ->DoSomething(); | |
95 } | |
96 | |
97 void DoStuff2(ScopedDataPipeConsumerHandle pipe, | |
98 const DoStuff2Callback& callback) override { | |
99 // Read the data from the pipe, writing the response (as a string) to | |
100 // DidStuff2(). | |
101 ASSERT_TRUE(pipe.is_valid()); | |
102 uint32_t data_size = 0; | |
103 ASSERT_EQ(MOJO_RESULT_OK, | |
104 ReadDataRaw( | |
105 pipe.get(), nullptr, &data_size, MOJO_READ_DATA_FLAG_QUERY)); | |
106 ASSERT_NE(0, static_cast<int>(data_size)); | |
107 char data[64]; | |
108 ASSERT_LT(static_cast<int>(data_size), 64); | |
109 ASSERT_EQ( | |
110 MOJO_RESULT_OK, | |
111 ReadDataRaw( | |
112 pipe.get(), data, &data_size, MOJO_READ_DATA_FLAG_ALL_OR_NONE)); | |
113 | |
114 callback.Run(data); | |
115 } | |
116 | |
117 void CreateNamedObject( | |
118 InterfaceRequest<sample::NamedObject> object_request) override { | |
119 EXPECT_TRUE(object_request.is_pending()); | |
120 new SampleNamedObjectImpl(object_request.Pass()); | |
121 } | |
122 | |
123 // These aren't called or implemented, but exist here to test that the | |
124 // methods are generated with the correct argument types for imported | |
125 // interfaces. | |
126 void RequestImportedInterface( | |
127 InterfaceRequest<imported::ImportedInterface> imported, | |
128 const mojo::Callback<void(InterfaceRequest<imported::ImportedInterface>)>& | |
129 callback) override {} | |
130 void TakeImportedInterface( | |
131 InterfaceHandle<imported::ImportedInterface> imported, | |
132 const mojo::Callback<void(InterfaceHandle<imported::ImportedInterface>)>& | |
133 callback) override {} | |
134 | |
135 private: | |
136 ScopedMessagePipeHandle pipe1_; | |
137 Binding<sample::Factory> binding_; | |
138 }; | |
139 | |
140 class HandlePassingTest : public testing::Test { | |
141 public: | |
142 void TearDown() override { PumpMessages(); } | |
143 | |
144 void PumpMessages() { loop_.RunUntilIdle(); } | |
145 | |
146 private: | |
147 RunLoop loop_; | |
148 }; | |
149 | |
150 struct DoStuffCallback { | |
151 DoStuffCallback(bool* got_response, std::string* got_text_reply) | |
152 : got_response(got_response), got_text_reply(got_text_reply) {} | |
153 | |
154 void Run(sample::ResponsePtr response, const String& text_reply) const { | |
155 *got_text_reply = text_reply; | |
156 | |
157 if (response->pipe.is_valid()) { | |
158 std::string text2; | |
159 EXPECT_TRUE(ReadTextMessage(response->pipe.get(), &text2)); | |
160 | |
161 // Ensure that simply accessing response.pipe does not close it. | |
162 EXPECT_TRUE(response->pipe.is_valid()); | |
163 | |
164 EXPECT_EQ(std::string(kText2), text2); | |
165 | |
166 // Do some more tests of handle passing: | |
167 ScopedMessagePipeHandle p = response->pipe.Pass(); | |
168 EXPECT_TRUE(p.is_valid()); | |
169 EXPECT_FALSE(response->pipe.is_valid()); | |
170 } | |
171 | |
172 *got_response = true; | |
173 } | |
174 | |
175 bool* got_response; | |
176 std::string* got_text_reply; | |
177 }; | |
178 | |
179 TEST_F(HandlePassingTest, Basic) { | |
180 sample::FactoryPtr factory; | |
181 SampleFactoryImpl factory_impl(GetProxy(&factory)); | |
182 | |
183 MessagePipe pipe0; | |
184 EXPECT_TRUE(WriteTextMessage(pipe0.handle1.get(), kText1)); | |
185 | |
186 MessagePipe pipe1; | |
187 EXPECT_TRUE(WriteTextMessage(pipe1.handle1.get(), kText2)); | |
188 | |
189 imported::ImportedInterfacePtr imported; | |
190 ImportedInterfaceImpl imported_impl(GetProxy(&imported)); | |
191 | |
192 sample::RequestPtr request(sample::Request::New()); | |
193 request->x = 1; | |
194 request->pipe = pipe1.handle0.Pass(); | |
195 request->obj = imported.Pass(); | |
196 bool got_response = false; | |
197 std::string got_text_reply; | |
198 DoStuffCallback cb(&got_response, &got_text_reply); | |
199 factory->DoStuff(request.Pass(), pipe0.handle0.Pass(), cb); | |
200 | |
201 EXPECT_FALSE(*cb.got_response); | |
202 int count_before = ImportedInterfaceImpl::do_something_count(); | |
203 | |
204 PumpMessages(); | |
205 | |
206 EXPECT_TRUE(*cb.got_response); | |
207 EXPECT_EQ(kText1, *cb.got_text_reply); | |
208 EXPECT_EQ(1, ImportedInterfaceImpl::do_something_count() - count_before); | |
209 } | |
210 | |
211 TEST_F(HandlePassingTest, PassInvalid) { | |
212 sample::FactoryPtr factory; | |
213 SampleFactoryImpl factory_impl(GetProxy(&factory)); | |
214 | |
215 sample::RequestPtr request(sample::Request::New()); | |
216 request->x = 1; | |
217 bool got_response = false; | |
218 std::string got_text_reply; | |
219 DoStuffCallback cb(&got_response, &got_text_reply); | |
220 factory->DoStuff(request.Pass(), ScopedMessagePipeHandle().Pass(), cb); | |
221 | |
222 EXPECT_FALSE(*cb.got_response); | |
223 | |
224 PumpMessages(); | |
225 | |
226 EXPECT_TRUE(*cb.got_response); | |
227 } | |
228 | |
229 struct DoStuff2Callback { | |
230 DoStuff2Callback(bool* got_response, std::string* got_text_reply) | |
231 : got_response(got_response), got_text_reply(got_text_reply) {} | |
232 | |
233 void Run(const String& text_reply) const { | |
234 *got_response = true; | |
235 *got_text_reply = text_reply; | |
236 } | |
237 | |
238 bool* got_response; | |
239 std::string* got_text_reply; | |
240 }; | |
241 | |
242 // Verifies DataPipeConsumer can be passed and read from. | |
243 TEST_F(HandlePassingTest, DataPipe) { | |
244 sample::FactoryPtr factory; | |
245 SampleFactoryImpl factory_impl(GetProxy(&factory)); | |
246 | |
247 // Writes a string to a data pipe and passes the data pipe (consumer) to the | |
248 // factory. | |
249 ScopedDataPipeProducerHandle producer_handle; | |
250 ScopedDataPipeConsumerHandle consumer_handle; | |
251 MojoCreateDataPipeOptions options = {sizeof(MojoCreateDataPipeOptions), | |
252 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, | |
253 1, | |
254 1024}; | |
255 ASSERT_EQ(MOJO_RESULT_OK, | |
256 CreateDataPipe(&options, &producer_handle, &consumer_handle)); | |
257 std::string expected_text_reply = "got it"; | |
258 // +1 for \0. | |
259 uint32_t data_size = static_cast<uint32_t>(expected_text_reply.size() + 1); | |
260 ASSERT_EQ(MOJO_RESULT_OK, | |
261 WriteDataRaw(producer_handle.get(), | |
262 expected_text_reply.c_str(), | |
263 &data_size, | |
264 MOJO_WRITE_DATA_FLAG_ALL_OR_NONE)); | |
265 | |
266 bool got_response = false; | |
267 std::string got_text_reply; | |
268 DoStuff2Callback cb(&got_response, &got_text_reply); | |
269 factory->DoStuff2(consumer_handle.Pass(), cb); | |
270 | |
271 EXPECT_FALSE(*cb.got_response); | |
272 | |
273 PumpMessages(); | |
274 | |
275 EXPECT_TRUE(*cb.got_response); | |
276 EXPECT_EQ(expected_text_reply, *cb.got_text_reply); | |
277 } | |
278 | |
279 TEST_F(HandlePassingTest, PipesAreClosed) { | |
280 sample::FactoryPtr factory; | |
281 SampleFactoryImpl factory_impl(GetProxy(&factory)); | |
282 | |
283 MessagePipe extra_pipe; | |
284 | |
285 MojoHandle handle0_value = extra_pipe.handle0.get().value(); | |
286 MojoHandle handle1_value = extra_pipe.handle1.get().value(); | |
287 | |
288 { | |
289 auto pipes = Array<ScopedMessagePipeHandle>::New(2); | |
290 pipes[0] = extra_pipe.handle0.Pass(); | |
291 pipes[1] = extra_pipe.handle1.Pass(); | |
292 | |
293 sample::RequestPtr request(sample::Request::New()); | |
294 request->more_pipes = pipes.Pass(); | |
295 | |
296 factory->DoStuff(request.Pass(), ScopedMessagePipeHandle(), | |
297 sample::Factory::DoStuffCallback()); | |
298 } | |
299 | |
300 // We expect the pipes to have been closed. | |
301 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoClose(handle0_value)); | |
302 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoClose(handle1_value)); | |
303 } | |
304 | |
305 TEST_F(HandlePassingTest, IsHandle) { | |
306 // Validate that mojo::internal::IsHandle<> works as expected since this. | |
307 // template is key to ensuring that we don't leak handles. | |
308 EXPECT_TRUE(internal::IsHandle<Handle>::value); | |
309 EXPECT_TRUE(internal::IsHandle<MessagePipeHandle>::value); | |
310 EXPECT_TRUE(internal::IsHandle<DataPipeConsumerHandle>::value); | |
311 EXPECT_TRUE(internal::IsHandle<DataPipeProducerHandle>::value); | |
312 EXPECT_TRUE(internal::IsHandle<SharedBufferHandle>::value); | |
313 | |
314 // Basic sanity checks... | |
315 EXPECT_FALSE(internal::IsHandle<int>::value); | |
316 EXPECT_FALSE(internal::IsHandle<sample::FactoryPtr>::value); | |
317 EXPECT_FALSE(internal::IsHandle<String>::value); | |
318 } | |
319 | |
320 TEST_F(HandlePassingTest, CreateNamedObject) { | |
321 sample::FactoryPtr factory; | |
322 SampleFactoryImpl factory_impl(GetProxy(&factory)); | |
323 | |
324 sample::NamedObjectPtr object1; | |
325 EXPECT_FALSE(object1); | |
326 | |
327 InterfaceRequest<sample::NamedObject> object1_request = GetProxy(&object1); | |
328 EXPECT_TRUE(object1_request.is_pending()); | |
329 factory->CreateNamedObject(object1_request.Pass()); | |
330 EXPECT_FALSE(object1_request.is_pending()); // We've passed the request. | |
331 | |
332 ASSERT_TRUE(object1); | |
333 object1->SetName("object1"); | |
334 | |
335 sample::NamedObjectPtr object2; | |
336 factory->CreateNamedObject(GetProxy(&object2)); | |
337 object2->SetName("object2"); | |
338 | |
339 std::string name1; | |
340 object1->GetName(StringRecorder(&name1)); | |
341 | |
342 std::string name2; | |
343 object2->GetName(StringRecorder(&name2)); | |
344 | |
345 PumpMessages(); // Yield for results. | |
346 | |
347 EXPECT_EQ(std::string("object1"), name1); | |
348 EXPECT_EQ(std::string("object2"), name2); | |
349 } | |
350 | |
351 } // namespace | |
352 } // namespace test | |
353 } // namespace mojo | |
OLD | NEW |