OLD | NEW |
| (Empty) |
1 // Copyright 2016 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 "mojo/public/cpp/application/service_provider_impl.h" | |
6 | |
7 #include <utility> | |
8 | |
9 #include "gtest/gtest.h" | |
10 #include "mojo/public/cpp/application/connect.h" | |
11 #include "mojo/public/cpp/bindings/strong_binding.h" | |
12 #include "mojo/public/cpp/system/macros.h" | |
13 #include "mojo/public/cpp/utility/run_loop.h" | |
14 #include "mojo/public/interfaces/application/service_provider.mojom.h" | |
15 #include "mojo/public/interfaces/bindings/tests/ping_service.mojom.h" | |
16 | |
17 namespace mojo { | |
18 namespace { | |
19 | |
20 class ServiceProviderImplTest : public testing::Test { | |
21 public: | |
22 ServiceProviderImplTest() {} | |
23 ~ServiceProviderImplTest() override { loop_.RunUntilIdle(); } | |
24 | |
25 RunLoop& loop() { return loop_; } | |
26 | |
27 protected: | |
28 void QuitLoop(bool ok) { | |
29 EXPECT_TRUE(ok); | |
30 loop_.Quit(); | |
31 } | |
32 | |
33 private: | |
34 RunLoop loop_; | |
35 | |
36 MOJO_DISALLOW_COPY_AND_ASSIGN(ServiceProviderImplTest); | |
37 }; | |
38 | |
39 class PingServiceImpl : public test::PingService { | |
40 public: | |
41 PingServiceImpl(InterfaceRequest<test::PingService> ping_service_request) | |
42 : strong_binding_(this, std::move(ping_service_request)) {} | |
43 ~PingServiceImpl() override {} | |
44 | |
45 // |test::PingService|: | |
46 void Ping(const PingCallback& callback) override { callback.Run(); } | |
47 | |
48 private: | |
49 StrongBinding<test::PingService> strong_binding_; | |
50 | |
51 MOJO_DISALLOW_COPY_AND_ASSIGN(PingServiceImpl); | |
52 }; | |
53 | |
54 TEST_F(ServiceProviderImplTest, Basic) { | |
55 const char kRemoteUrl[] = "https://example.com/remote.mojo"; | |
56 const char kConnectionUrl[] = "https://example.com/me.mojo"; | |
57 | |
58 const char kPing1[] = "Ping1"; | |
59 const char kPing2[] = "Ping2"; | |
60 const char kPing3[] = "Ping3"; | |
61 | |
62 ServiceProviderPtr sp; | |
63 ServiceProviderImpl impl(ConnectionContext(ConnectionContext::Type::INCOMING, | |
64 kRemoteUrl, kConnectionUrl), | |
65 GetProxy(&sp)); | |
66 EXPECT_EQ(ConnectionContext::Type::INCOMING, impl.connection_context().type); | |
67 EXPECT_EQ(kRemoteUrl, impl.connection_context().remote_url); | |
68 EXPECT_EQ(kConnectionUrl, impl.connection_context().connection_url); | |
69 | |
70 impl.AddService<test::PingService>( | |
71 [&kRemoteUrl, &kConnectionUrl]( | |
72 const ConnectionContext& connection_context, | |
73 InterfaceRequest<test::PingService> ping_service_request) { | |
74 EXPECT_EQ(ConnectionContext::Type::INCOMING, connection_context.type); | |
75 EXPECT_EQ(kRemoteUrl, connection_context.remote_url); | |
76 EXPECT_EQ(kConnectionUrl, connection_context.connection_url); | |
77 new PingServiceImpl(std::move(ping_service_request)); | |
78 }, | |
79 kPing1); | |
80 | |
81 impl.AddService<test::PingService>( | |
82 [&kRemoteUrl, &kConnectionUrl]( | |
83 const ConnectionContext& connection_context, | |
84 InterfaceRequest<test::PingService> ping_service_request) { | |
85 EXPECT_EQ(ConnectionContext::Type::INCOMING, connection_context.type); | |
86 EXPECT_EQ(kRemoteUrl, connection_context.remote_url); | |
87 EXPECT_EQ(kConnectionUrl, connection_context.connection_url); | |
88 new PingServiceImpl(std::move(ping_service_request)); | |
89 }, | |
90 kPing2); | |
91 | |
92 { | |
93 test::PingServicePtr ping1; | |
94 ConnectToService(sp.get(), GetProxy(&ping1), kPing1); | |
95 ping1.set_connection_error_handler([this] { QuitLoop(false); }); | |
96 ping1->Ping([this] { QuitLoop(true); }); | |
97 loop().Run(); | |
98 } | |
99 loop().RunUntilIdle(); // Run stuff caused by destructors. | |
100 | |
101 { | |
102 test::PingServicePtr ping2; | |
103 ConnectToService(sp.get(), GetProxy(&ping2), kPing2); | |
104 ping2.set_connection_error_handler([this] { QuitLoop(false); }); | |
105 ping2->Ping([this] { QuitLoop(true); }); | |
106 loop().Run(); | |
107 } | |
108 loop().RunUntilIdle(); // Run stuff caused by destructors. | |
109 | |
110 // "Ping3" isn't actually registered! | |
111 { | |
112 test::PingServicePtr ping3; | |
113 ConnectToService(sp.get(), GetProxy(&ping3), kPing3); | |
114 ping3.set_connection_error_handler([this] { QuitLoop(true); }); | |
115 ping3->Ping([this] { QuitLoop(false); }); | |
116 loop().Run(); | |
117 } | |
118 loop().RunUntilIdle(); // Run stuff caused by destructors. | |
119 | |
120 impl.RemoveService<test::PingService>(kPing2); | |
121 | |
122 // "Ping2" should no longer work. | |
123 { | |
124 test::PingServicePtr ping2; | |
125 ConnectToService(sp.get(), GetProxy(&ping2), kPing2); | |
126 ping2.set_connection_error_handler([this] { QuitLoop(true); }); | |
127 ping2->Ping([this] { QuitLoop(false); }); | |
128 loop().Run(); | |
129 } | |
130 loop().RunUntilIdle(); // Run stuff caused by destructors. | |
131 | |
132 // But "Ping1" should still work. | |
133 { | |
134 test::PingServicePtr ping1; | |
135 ConnectToService(sp.get(), GetProxy(&ping1), kPing1); | |
136 ping1.set_connection_error_handler([this] { QuitLoop(false); }); | |
137 ping1->Ping([this] { QuitLoop(true); }); | |
138 loop().Run(); | |
139 } | |
140 loop().RunUntilIdle(); // Run stuff caused by destructors. | |
141 | |
142 impl.RemoveServiceForName(kPing1); | |
143 | |
144 // "Ping1" should no longer work. | |
145 { | |
146 test::PingServicePtr ping1; | |
147 ConnectToService(sp.get(), GetProxy(&ping1), kPing1); | |
148 ping1.set_connection_error_handler([this] { QuitLoop(true); }); | |
149 ping1->Ping([this] { QuitLoop(false); }); | |
150 loop().Run(); | |
151 } | |
152 loop().RunUntilIdle(); // Run stuff caused by destructors. | |
153 | |
154 sp.reset(); | |
155 loop().RunUntilIdle(); | |
156 } | |
157 | |
158 TEST_F(ServiceProviderImplTest, CloseAndRebind) { | |
159 const char kRemoteUrl1[] = "https://example.com/remote1.mojo"; | |
160 const char kRemoteUrl2[] = "https://example.com/remote2.mojo"; | |
161 const char kConnectionUrl[] = "https://example.com/me.mojo"; | |
162 const char kPing[] = "Ping"; | |
163 | |
164 ServiceProviderPtr sp1; | |
165 ServiceProviderImpl impl(ConnectionContext(ConnectionContext::Type::INCOMING, | |
166 kRemoteUrl1, kConnectionUrl), | |
167 GetProxy(&sp1)); | |
168 EXPECT_EQ(ConnectionContext::Type::INCOMING, impl.connection_context().type); | |
169 EXPECT_EQ(kRemoteUrl1, impl.connection_context().remote_url); | |
170 EXPECT_EQ(kConnectionUrl, impl.connection_context().connection_url); | |
171 | |
172 impl.AddService<test::PingService>( | |
173 [&kRemoteUrl1, &kRemoteUrl2, &kConnectionUrl]( | |
174 const ConnectionContext& connection_context, | |
175 InterfaceRequest<test::PingService> ping_service_request) { | |
176 EXPECT_EQ(ConnectionContext::Type::INCOMING, connection_context.type); | |
177 EXPECT_TRUE(connection_context.remote_url == kRemoteUrl1 || | |
178 connection_context.remote_url == kRemoteUrl2); | |
179 EXPECT_EQ(kConnectionUrl, connection_context.connection_url); | |
180 new PingServiceImpl(std::move(ping_service_request)); | |
181 }, | |
182 kPing); | |
183 | |
184 { | |
185 test::PingServicePtr ping; | |
186 ConnectToService(sp1.get(), GetProxy(&ping), kPing); | |
187 ping.set_connection_error_handler([this] { QuitLoop(false); }); | |
188 ping->Ping([this] { QuitLoop(true); }); | |
189 loop().Run(); | |
190 } | |
191 loop().RunUntilIdle(); // Run stuff caused by destructors. | |
192 | |
193 impl.Close(); | |
194 EXPECT_EQ(ConnectionContext::Type::UNKNOWN, impl.connection_context().type); | |
195 EXPECT_EQ(std::string(), impl.connection_context().remote_url); | |
196 EXPECT_EQ(std::string(), impl.connection_context().connection_url); | |
197 sp1.reset(); | |
198 loop().RunUntilIdle(); | |
199 | |
200 ServiceProviderPtr sp2; | |
201 impl.Bind(ConnectionContext(ConnectionContext::Type::INCOMING, kRemoteUrl2, | |
202 kConnectionUrl), | |
203 GetProxy(&sp2)); | |
204 EXPECT_EQ(ConnectionContext::Type::INCOMING, impl.connection_context().type); | |
205 EXPECT_EQ(kRemoteUrl2, impl.connection_context().remote_url); | |
206 EXPECT_EQ(kConnectionUrl, impl.connection_context().connection_url); | |
207 | |
208 { | |
209 test::PingServicePtr ping; | |
210 ConnectToService(sp2.get(), GetProxy(&ping), kPing); | |
211 ping.set_connection_error_handler([this] { QuitLoop(false); }); | |
212 ping->Ping([this] { QuitLoop(true); }); | |
213 loop().Run(); | |
214 } | |
215 loop().RunUntilIdle(); // Run stuff caused by destructors. | |
216 | |
217 // Can close multiple times. | |
218 impl.Close(); | |
219 impl.Close(); | |
220 sp2.reset(); | |
221 loop().RunUntilIdle(); | |
222 } | |
223 | |
224 TEST_F(ServiceProviderImplTest, Bind) { | |
225 const char kRemoteUrl[] = "https://example.com/remote.mojo"; | |
226 const char kConnectionUrl[] = "https://example.com/me.mojo"; | |
227 const char kPing[] = "Ping"; | |
228 | |
229 ServiceProviderPtr sp; | |
230 ServiceProviderImpl impl; | |
231 EXPECT_EQ(ConnectionContext::Type::UNKNOWN, impl.connection_context().type); | |
232 EXPECT_EQ(std::string(), impl.connection_context().remote_url); | |
233 EXPECT_EQ(std::string(), impl.connection_context().connection_url); | |
234 | |
235 impl.Bind(ConnectionContext(ConnectionContext::Type::INCOMING, kRemoteUrl, | |
236 kConnectionUrl), | |
237 GetProxy(&sp)); | |
238 | |
239 impl.AddService<test::PingService>( | |
240 [&kRemoteUrl, &kConnectionUrl]( | |
241 const ConnectionContext& connection_context, | |
242 InterfaceRequest<test::PingService> request) { | |
243 EXPECT_EQ(ConnectionContext::Type::INCOMING, connection_context.type); | |
244 EXPECT_EQ(kRemoteUrl, connection_context.remote_url); | |
245 EXPECT_EQ(kConnectionUrl, connection_context.connection_url); | |
246 new PingServiceImpl(std::move(request)); | |
247 }, | |
248 kPing); | |
249 | |
250 { | |
251 test::PingServicePtr ping; | |
252 ConnectToService(sp.get(), GetProxy(&ping), kPing); | |
253 ping.set_connection_error_handler([this] { QuitLoop(false); }); | |
254 ping->Ping([this] { QuitLoop(true); }); | |
255 loop().Run(); | |
256 } | |
257 loop().RunUntilIdle(); // Run stuff caused by destructors. | |
258 | |
259 sp.reset(); | |
260 loop().RunUntilIdle(); | |
261 } | |
262 | |
263 class FauxServiceProvider : public ServiceProvider { | |
264 public: | |
265 explicit FauxServiceProvider( | |
266 std::function<void(const std::string& service_name)> | |
267 on_connect_to_service) | |
268 : on_connect_to_service_(on_connect_to_service) {} | |
269 ~FauxServiceProvider() override {} | |
270 | |
271 // |ServiceProvider|: | |
272 void ConnectToService(const String& service_name, | |
273 ScopedMessagePipeHandle client_handle) override { | |
274 on_connect_to_service_(service_name.get()); | |
275 } | |
276 | |
277 private: | |
278 std::function<void(const std::string& service_name)> on_connect_to_service_; | |
279 | |
280 MOJO_DISALLOW_COPY_AND_ASSIGN(FauxServiceProvider); | |
281 }; | |
282 | |
283 TEST_F(ServiceProviderImplTest, FallbackServiceProvider) { | |
284 const char kWhatever[] = "Whatever"; | |
285 | |
286 ServiceProviderPtr sp; | |
287 ServiceProviderImpl impl(ConnectionContext(ConnectionContext::Type::INCOMING, | |
288 "https://example.com/remote.mojo", | |
289 "https://example.com/me.mojo"), | |
290 GetProxy(&sp)); | |
291 | |
292 { | |
293 test::PingServicePtr ping; | |
294 ConnectToService(sp.get(), GetProxy(&ping), kWhatever); | |
295 ping.set_connection_error_handler([this] { QuitLoop(true); }); | |
296 ping->Ping([this] { QuitLoop(false); }); | |
297 loop().Run(); | |
298 } | |
299 loop().RunUntilIdle(); // Run stuff caused by destructors. | |
300 | |
301 bool was_run = false; | |
302 FauxServiceProvider fallback_sp( | |
303 [this, &kWhatever, &was_run](const std::string& service_name) { | |
304 EXPECT_EQ(kWhatever, service_name); | |
305 was_run = true; | |
306 }); | |
307 impl.set_fallback_service_provider(&fallback_sp); | |
308 | |
309 { | |
310 test::PingServicePtr ping; | |
311 ConnectToService(sp.get(), GetProxy(&ping), kWhatever); | |
312 ping.set_connection_error_handler([this] { QuitLoop(true); }); | |
313 EXPECT_FALSE(was_run); | |
314 ping->Ping([this] { QuitLoop(false); }); | |
315 loop().Run(); | |
316 EXPECT_TRUE(was_run); | |
317 } | |
318 loop().RunUntilIdle(); // Run stuff caused by destructors. | |
319 | |
320 // Clear the fallback. | |
321 impl.set_fallback_service_provider(nullptr); | |
322 was_run = false; | |
323 | |
324 { | |
325 test::PingServicePtr ping; | |
326 ConnectToService(sp.get(), GetProxy(&ping), kWhatever); | |
327 ping.set_connection_error_handler([this] { QuitLoop(true); }); | |
328 ping->Ping([this] { QuitLoop(false); }); | |
329 loop().Run(); | |
330 } | |
331 loop().RunUntilIdle(); // Run stuff caused by destructors. | |
332 | |
333 sp.reset(); | |
334 loop().RunUntilIdle(); | |
335 | |
336 EXPECT_FALSE(was_run); | |
337 } | |
338 | |
339 TEST_F(ServiceProviderImplTest, ConstructRequestNotPending) { | |
340 ServiceProviderImpl impl(ConnectionContext(ConnectionContext::Type::INCOMING, | |
341 "https://example.com/remote.mojo", | |
342 "https://example.com/me.mojo"), | |
343 InterfaceRequest<ServiceProvider>()); | |
344 EXPECT_EQ(ConnectionContext::Type::UNKNOWN, impl.connection_context().type); | |
345 EXPECT_EQ(std::string(), impl.connection_context().remote_url); | |
346 EXPECT_EQ(std::string(), impl.connection_context().connection_url); | |
347 } | |
348 | |
349 // TODO(vtl): Explicitly test |AddServiceForName()|? | |
350 | |
351 } // namespace | |
352 } // namespace mojo | |
OLD | NEW |