| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "mojo/public/cpp/application/service_provider_impl.h" | 5 #include "mojo/public/cpp/application/service_provider_impl.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "mojo/public/cpp/application/connect.h" | 9 #include "mojo/public/cpp/application/connect.h" |
| 10 #include "mojo/public/cpp/bindings/strong_binding.h" | 10 #include "mojo/public/cpp/bindings/strong_binding.h" |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 47 // |test::PingService|: | 47 // |test::PingService|: |
| 48 void Ping(const PingCallback& callback) override { callback.Run(); } | 48 void Ping(const PingCallback& callback) override { callback.Run(); } |
| 49 | 49 |
| 50 private: | 50 private: |
| 51 StrongBinding<test::PingService> strong_binding_; | 51 StrongBinding<test::PingService> strong_binding_; |
| 52 | 52 |
| 53 MOJO_DISALLOW_COPY_AND_ASSIGN(PingServiceImpl); | 53 MOJO_DISALLOW_COPY_AND_ASSIGN(PingServiceImpl); |
| 54 }; | 54 }; |
| 55 | 55 |
| 56 TEST_F(ServiceProviderImplTest, Basic) { | 56 TEST_F(ServiceProviderImplTest, Basic) { |
| 57 const char kRemoteUrl[] = "https://example.com/remote.mojo"; |
| 58 const char kConnectionUrl[] = "https://example.com/me.mojo"; |
| 59 |
| 57 const char kPing1[] = "Ping1"; | 60 const char kPing1[] = "Ping1"; |
| 58 const char kPing2[] = "Ping2"; | 61 const char kPing2[] = "Ping2"; |
| 59 const char kPing3[] = "Ping3"; | 62 const char kPing3[] = "Ping3"; |
| 60 | 63 |
| 61 ServiceProviderPtr sp; | 64 ServiceProviderPtr sp; |
| 62 ServiceProviderImpl impl(GetProxy(&sp)); | 65 ServiceProviderImpl impl(ConnectionContext(ConnectionContext::Type::INCOMING, |
| 66 kRemoteUrl, kConnectionUrl), |
| 67 GetProxy(&sp)); |
| 63 | 68 |
| 64 impl.AddServiceNew<test::PingService>( | 69 impl.AddServiceNew<test::PingService>( |
| 65 [](const ConnectionContext& connection_context, | 70 [&kRemoteUrl, &kConnectionUrl]( |
| 66 InterfaceRequest<test::PingService> ping_service_request) { | 71 const ConnectionContext& connection_context, |
| 72 InterfaceRequest<test::PingService> ping_service_request) { |
| 73 EXPECT_EQ(ConnectionContext::Type::INCOMING, connection_context.type); |
| 74 EXPECT_EQ(kRemoteUrl, connection_context.remote_url); |
| 75 EXPECT_EQ(kConnectionUrl, connection_context.connection_url); |
| 67 new PingServiceImpl(std::move(ping_service_request)); | 76 new PingServiceImpl(std::move(ping_service_request)); |
| 68 }, | 77 }, |
| 69 kPing1); | 78 kPing1); |
| 70 | 79 |
| 71 impl.AddServiceNew<test::PingService>( | 80 impl.AddServiceNew<test::PingService>( |
| 72 [](const ConnectionContext& connection_context, | 81 [&kRemoteUrl, &kConnectionUrl]( |
| 73 InterfaceRequest<test::PingService> ping_service_request) { | 82 const ConnectionContext& connection_context, |
| 83 InterfaceRequest<test::PingService> ping_service_request) { |
| 84 EXPECT_EQ(ConnectionContext::Type::INCOMING, connection_context.type); |
| 85 EXPECT_EQ(kRemoteUrl, connection_context.remote_url); |
| 86 EXPECT_EQ(kConnectionUrl, connection_context.connection_url); |
| 74 new PingServiceImpl(std::move(ping_service_request)); | 87 new PingServiceImpl(std::move(ping_service_request)); |
| 75 }, | 88 }, |
| 76 kPing2); | 89 kPing2); |
| 77 | 90 |
| 78 { | 91 { |
| 79 test::PingServicePtr ping1; | 92 test::PingServicePtr ping1; |
| 80 ConnectToService(sp.get(), GetProxy(&ping1), kPing1); | 93 ConnectToService(sp.get(), GetProxy(&ping1), kPing1); |
| 81 ping1.set_connection_error_handler([this] { QuitLoop(false); }); | 94 ping1.set_connection_error_handler([this] { QuitLoop(false); }); |
| 82 ping1->Ping([this] { QuitLoop(true); }); | 95 ping1->Ping([this] { QuitLoop(true); }); |
| 83 loop().Run(); | 96 loop().Run(); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 101 ping3->Ping([this] { QuitLoop(false); }); | 114 ping3->Ping([this] { QuitLoop(false); }); |
| 102 loop().Run(); | 115 loop().Run(); |
| 103 } | 116 } |
| 104 loop().RunUntilIdle(); // Run stuff caused by destructors. | 117 loop().RunUntilIdle(); // Run stuff caused by destructors. |
| 105 | 118 |
| 106 sp.reset(); | 119 sp.reset(); |
| 107 loop().RunUntilIdle(); | 120 loop().RunUntilIdle(); |
| 108 } | 121 } |
| 109 | 122 |
| 110 TEST_F(ServiceProviderImplTest, CloseAndRebind) { | 123 TEST_F(ServiceProviderImplTest, CloseAndRebind) { |
| 124 const char kRemoteUrl1[] = "https://example.com/remote1.mojo"; |
| 125 const char kRemoteUrl2[] = "https://example.com/remote2.mojo"; |
| 126 const char kConnectionUrl[] = "https://example.com/me.mojo"; |
| 111 const char kPing[] = "Ping"; | 127 const char kPing[] = "Ping"; |
| 112 | 128 |
| 113 ServiceProviderPtr sp1; | 129 ServiceProviderPtr sp1; |
| 114 ServiceProviderImpl impl(GetProxy(&sp1)); | 130 ServiceProviderImpl impl(ConnectionContext(ConnectionContext::Type::INCOMING, |
| 131 kRemoteUrl1, kConnectionUrl), |
| 132 GetProxy(&sp1)); |
| 115 | 133 |
| 116 impl.AddServiceNew<test::PingService>( | 134 impl.AddServiceNew<test::PingService>( |
| 117 [](const ConnectionContext& connection_context, | 135 [&kRemoteUrl1, &kRemoteUrl2, &kConnectionUrl]( |
| 118 InterfaceRequest<test::PingService> ping_service_request) { | 136 const ConnectionContext& connection_context, |
| 137 InterfaceRequest<test::PingService> ping_service_request) { |
| 138 EXPECT_EQ(ConnectionContext::Type::INCOMING, connection_context.type); |
| 139 EXPECT_TRUE(connection_context.remote_url == kRemoteUrl1 || |
| 140 connection_context.remote_url == kRemoteUrl2); |
| 141 EXPECT_EQ(kConnectionUrl, connection_context.connection_url); |
| 119 new PingServiceImpl(std::move(ping_service_request)); | 142 new PingServiceImpl(std::move(ping_service_request)); |
| 120 }, | 143 }, |
| 121 kPing); | 144 kPing); |
| 122 | 145 |
| 123 { | 146 { |
| 124 test::PingServicePtr ping; | 147 test::PingServicePtr ping; |
| 125 ConnectToService(sp1.get(), GetProxy(&ping), kPing); | 148 ConnectToService(sp1.get(), GetProxy(&ping), kPing); |
| 126 ping.set_connection_error_handler([this] { QuitLoop(false); }); | 149 ping.set_connection_error_handler([this] { QuitLoop(false); }); |
| 127 ping->Ping([this] { QuitLoop(true); }); | 150 ping->Ping([this] { QuitLoop(true); }); |
| 128 loop().Run(); | 151 loop().Run(); |
| 129 } | 152 } |
| 130 loop().RunUntilIdle(); // Run stuff caused by destructors. | 153 loop().RunUntilIdle(); // Run stuff caused by destructors. |
| 131 | 154 |
| 132 impl.Close(); | 155 impl.Close(); |
| 133 sp1.reset(); | 156 sp1.reset(); |
| 134 loop().RunUntilIdle(); | 157 loop().RunUntilIdle(); |
| 135 | 158 |
| 136 ServiceProviderPtr sp2; | 159 ServiceProviderPtr sp2; |
| 137 impl.Bind(GetProxy(&sp2)); | 160 impl.Bind(ConnectionContext(ConnectionContext::Type::INCOMING, kRemoteUrl2, |
| 161 kConnectionUrl), |
| 162 GetProxy(&sp2)); |
| 138 | 163 |
| 139 { | 164 { |
| 140 test::PingServicePtr ping; | 165 test::PingServicePtr ping; |
| 141 ConnectToService(sp2.get(), GetProxy(&ping), kPing); | 166 ConnectToService(sp2.get(), GetProxy(&ping), kPing); |
| 142 ping.set_connection_error_handler([this] { QuitLoop(false); }); | 167 ping.set_connection_error_handler([this] { QuitLoop(false); }); |
| 143 ping->Ping([this] { QuitLoop(true); }); | 168 ping->Ping([this] { QuitLoop(true); }); |
| 144 loop().Run(); | 169 loop().Run(); |
| 145 } | 170 } |
| 146 loop().RunUntilIdle(); // Run stuff caused by destructors. | 171 loop().RunUntilIdle(); // Run stuff caused by destructors. |
| 147 | 172 |
| 148 // Can close multiple times. | 173 // Can close multiple times. |
| 149 impl.Close(); | 174 impl.Close(); |
| 150 impl.Close(); | 175 impl.Close(); |
| 151 sp2.reset(); | 176 sp2.reset(); |
| 152 loop().RunUntilIdle(); | 177 loop().RunUntilIdle(); |
| 153 } | 178 } |
| 154 | 179 |
| 155 TEST_F(ServiceProviderImplTest, Bind) { | 180 TEST_F(ServiceProviderImplTest, Bind) { |
| 181 const char kRemoteUrl[] = "https://example.com/remote.mojo"; |
| 182 const char kConnectionUrl[] = "https://example.com/me.mojo"; |
| 156 const char kPing[] = "Ping"; | 183 const char kPing[] = "Ping"; |
| 157 | 184 |
| 158 ServiceProviderPtr sp; | 185 ServiceProviderPtr sp; |
| 159 ServiceProviderImpl impl; | 186 ServiceProviderImpl impl; |
| 160 | 187 |
| 161 impl.Bind(GetProxy(&sp)); | 188 impl.Bind(ConnectionContext(ConnectionContext::Type::INCOMING, kRemoteUrl, |
| 189 kConnectionUrl), |
| 190 GetProxy(&sp)); |
| 162 | 191 |
| 163 impl.AddServiceNew<test::PingService>( | 192 impl.AddServiceNew<test::PingService>( |
| 164 [](const ConnectionContext& connection_context, | 193 [&kRemoteUrl, &kConnectionUrl]( |
| 165 InterfaceRequest<test::PingService> request) { | 194 const ConnectionContext& connection_context, |
| 195 InterfaceRequest<test::PingService> request) { |
| 196 EXPECT_EQ(ConnectionContext::Type::INCOMING, connection_context.type); |
| 197 EXPECT_EQ(kRemoteUrl, connection_context.remote_url); |
| 198 EXPECT_EQ(kConnectionUrl, connection_context.connection_url); |
| 166 new PingServiceImpl(std::move(request)); | 199 new PingServiceImpl(std::move(request)); |
| 167 }, | 200 }, |
| 168 kPing); | 201 kPing); |
| 169 | 202 |
| 170 { | 203 { |
| 171 test::PingServicePtr ping; | 204 test::PingServicePtr ping; |
| 172 ConnectToService(sp.get(), GetProxy(&ping), kPing); | 205 ConnectToService(sp.get(), GetProxy(&ping), kPing); |
| 173 ping.set_connection_error_handler([this] { QuitLoop(false); }); | 206 ping.set_connection_error_handler([this] { QuitLoop(false); }); |
| 174 ping->Ping([this] { QuitLoop(true); }); | 207 ping->Ping([this] { QuitLoop(true); }); |
| 175 loop().Run(); | 208 loop().Run(); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 197 private: | 230 private: |
| 198 std::function<void(const std::string& service_name)> on_connect_to_service_; | 231 std::function<void(const std::string& service_name)> on_connect_to_service_; |
| 199 | 232 |
| 200 MOJO_DISALLOW_COPY_AND_ASSIGN(FauxServiceProvider); | 233 MOJO_DISALLOW_COPY_AND_ASSIGN(FauxServiceProvider); |
| 201 }; | 234 }; |
| 202 | 235 |
| 203 TEST_F(ServiceProviderImplTest, FallbackServiceProvider) { | 236 TEST_F(ServiceProviderImplTest, FallbackServiceProvider) { |
| 204 const char kWhatever[] = "Whatever"; | 237 const char kWhatever[] = "Whatever"; |
| 205 | 238 |
| 206 ServiceProviderPtr sp; | 239 ServiceProviderPtr sp; |
| 207 ServiceProviderImpl impl(GetProxy(&sp)); | 240 ServiceProviderImpl impl(ConnectionContext(ConnectionContext::Type::INCOMING, |
| 241 "https://example.com/remote.mojo", |
| 242 "https://example.com/me.mojo"), |
| 243 GetProxy(&sp)); |
| 208 | 244 |
| 209 { | 245 { |
| 210 test::PingServicePtr ping; | 246 test::PingServicePtr ping; |
| 211 ConnectToService(sp.get(), GetProxy(&ping), kWhatever); | 247 ConnectToService(sp.get(), GetProxy(&ping), kWhatever); |
| 212 ping.set_connection_error_handler([this] { QuitLoop(true); }); | 248 ping.set_connection_error_handler([this] { QuitLoop(true); }); |
| 213 ping->Ping([this] { QuitLoop(false); }); | 249 ping->Ping([this] { QuitLoop(false); }); |
| 214 loop().Run(); | 250 loop().Run(); |
| 215 } | 251 } |
| 216 loop().RunUntilIdle(); // Run stuff caused by destructors. | 252 loop().RunUntilIdle(); // Run stuff caused by destructors. |
| 217 | 253 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 248 loop().RunUntilIdle(); // Run stuff caused by destructors. | 284 loop().RunUntilIdle(); // Run stuff caused by destructors. |
| 249 | 285 |
| 250 sp.reset(); | 286 sp.reset(); |
| 251 loop().RunUntilIdle(); | 287 loop().RunUntilIdle(); |
| 252 | 288 |
| 253 EXPECT_FALSE(was_run); | 289 EXPECT_FALSE(was_run); |
| 254 } | 290 } |
| 255 | 291 |
| 256 } // namespace | 292 } // namespace |
| 257 } // namespace mojo | 293 } // namespace mojo |
| OLD | NEW |