| 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 "base/bind.h" |
| 5 #include "base/message_loop/message_loop.h" | 6 #include "base/message_loop/message_loop.h" |
| 6 #include "base/single_thread_task_runner.h" | 7 #include "base/single_thread_task_runner.h" |
| 7 #include "base/synchronization/lock.h" | 8 #include "base/synchronization/lock.h" |
| 8 #include "base/synchronization/waitable_event.h" | 9 #include "base/synchronization/waitable_event.h" |
| 9 #include "base/threading/platform_thread.h" | 10 #include "base/threading/platform_thread.h" |
| 10 #include "mojo/public/cpp/bindings/associated_binding.h" | 11 #include "mojo/public/cpp/bindings/associated_binding.h" |
| 11 #include "mojo/public/cpp/bindings/associated_group.h" | 12 #include "mojo/public/cpp/bindings/associated_group.h" |
| 12 #include "mojo/public/cpp/bindings/associated_interface_ptr.h" | 13 #include "mojo/public/cpp/bindings/associated_interface_ptr.h" |
| 13 #include "mojo/public/cpp/bindings/associated_interface_ptr_info.h" | 14 #include "mojo/public/cpp/bindings/associated_interface_ptr_info.h" |
| 14 #include "mojo/public/cpp/bindings/associated_interface_request.h" | 15 #include "mojo/public/cpp/bindings/associated_interface_request.h" |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 template <typename BindingType, typename RequestType> | 118 template <typename BindingType, typename RequestType> |
| 118 class IntegerSenderImpl : public IntegerSender { | 119 class IntegerSenderImpl : public IntegerSender { |
| 119 public: | 120 public: |
| 120 IntegerSenderImpl(RequestType request, | 121 IntegerSenderImpl(RequestType request, |
| 121 scoped_refptr<base::SingleThreadTaskRunner> runner) | 122 scoped_refptr<base::SingleThreadTaskRunner> runner) |
| 122 : binding_(this, std::move(request), std::move(runner)) {} | 123 : binding_(this, std::move(request), std::move(runner)) {} |
| 123 | 124 |
| 124 ~IntegerSenderImpl() override {} | 125 ~IntegerSenderImpl() override {} |
| 125 | 126 |
| 126 using EchoHandler = Callback<void(int32_t, const EchoCallback&)>; | 127 using EchoHandler = Callback<void(int32_t, const EchoCallback&)>; |
| 128 |
| 127 void set_echo_handler(const EchoHandler& handler) { echo_handler_ = handler; } | 129 void set_echo_handler(const EchoHandler& handler) { echo_handler_ = handler; } |
| 128 | 130 |
| 129 void Echo(int32_t value, const EchoCallback& callback) override { | 131 void Echo(int32_t value, const EchoCallback& callback) override { |
| 130 if (echo_handler_.is_null()) | 132 if (echo_handler_.is_null()) |
| 131 callback.Run(value); | 133 callback.Run(value); |
| 132 else | 134 else |
| 133 echo_handler_.Run(value, callback); | 135 echo_handler_.Run(value, callback); |
| 134 } | 136 } |
| 135 void Send(int32_t value) override { NOTREACHED(); } | 137 void Send(int32_t value) override { NOTREACHED(); } |
| 136 | 138 |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 scoped_refptr<TestTaskRunner>(new TestTaskRunner); | 211 scoped_refptr<TestTaskRunner>(new TestTaskRunner); |
| 210 sender_ptr_task_runner_ = scoped_refptr<TestTaskRunner>(new TestTaskRunner); | 212 sender_ptr_task_runner_ = scoped_refptr<TestTaskRunner>(new TestTaskRunner); |
| 211 | 213 |
| 212 auto connection_request = | 214 auto connection_request = |
| 213 GetProxy(&connection_ptr_, connection_ptr_task_runner_); | 215 GetProxy(&connection_ptr_, connection_ptr_task_runner_); |
| 214 connection_impl_.reset(new IntegerSenderConnectionImpl( | 216 connection_impl_.reset(new IntegerSenderConnectionImpl( |
| 215 std::move(connection_request), connection_binding_task_runner_, | 217 std::move(connection_request), connection_binding_task_runner_, |
| 216 sender_binding_task_runner_)); | 218 sender_binding_task_runner_)); |
| 217 | 219 |
| 218 connection_impl_->set_get_sender_notification( | 220 connection_impl_->set_get_sender_notification( |
| 219 [this]() { connection_binding_task_runner_->Quit(); }); | 221 base::Bind(&AssociatedBindTaskRunnerTest::QuitTaskRunner, |
| 222 base::Unretained(this))); |
| 220 | 223 |
| 221 connection_ptr_->GetSender(GetProxy(&sender_ptr_, | 224 connection_ptr_->GetSender(GetProxy(&sender_ptr_, |
| 222 connection_ptr_.associated_group(), | 225 connection_ptr_.associated_group(), |
| 223 sender_ptr_task_runner_)); | 226 sender_ptr_task_runner_)); |
| 224 connection_binding_task_runner_->Run(); | 227 connection_binding_task_runner_->Run(); |
| 225 } | 228 } |
| 226 | 229 |
| 230 void QuitTaskRunner() { |
| 231 connection_binding_task_runner_->Quit(); |
| 232 } |
| 233 |
| 227 base::MessageLoop loop_; | 234 base::MessageLoop loop_; |
| 228 scoped_refptr<TestTaskRunner> connection_binding_task_runner_; | 235 scoped_refptr<TestTaskRunner> connection_binding_task_runner_; |
| 229 scoped_refptr<TestTaskRunner> connection_ptr_task_runner_; | 236 scoped_refptr<TestTaskRunner> connection_ptr_task_runner_; |
| 230 scoped_refptr<TestTaskRunner> sender_binding_task_runner_; | 237 scoped_refptr<TestTaskRunner> sender_binding_task_runner_; |
| 231 scoped_refptr<TestTaskRunner> sender_ptr_task_runner_; | 238 scoped_refptr<TestTaskRunner> sender_ptr_task_runner_; |
| 232 | 239 |
| 233 IntegerSenderConnectionPtr connection_ptr_; | 240 IntegerSenderConnectionPtr connection_ptr_; |
| 234 std::unique_ptr<IntegerSenderConnectionImpl> connection_impl_; | 241 std::unique_ptr<IntegerSenderConnectionImpl> connection_impl_; |
| 235 IntegerSenderAssociatedPtr sender_ptr_; | 242 IntegerSenderAssociatedPtr sender_ptr_; |
| 236 }; | 243 }; |
| 237 | 244 |
| 245 void DoSetFlagAndQuitTaskRunner(bool* flag, |
| 246 scoped_refptr<TestTaskRunner> task_runner) { |
| 247 *flag = true; |
| 248 if (task_runner) |
| 249 task_runner->Quit(); |
| 250 } |
| 251 |
| 252 void DoExpectValueSetFlagAndQuitTaskRunner( |
| 253 int32_t expected_value, |
| 254 bool* flag, |
| 255 scoped_refptr<TestTaskRunner> task_runner, |
| 256 int32_t value) { |
| 257 EXPECT_EQ(expected_value, value); |
| 258 DoSetFlagAndQuitTaskRunner(flag, task_runner); |
| 259 } |
| 260 |
| 261 void DoExpectValueSetFlagForwardValueAndQuitTaskRunner( |
| 262 int32_t expected_value, |
| 263 bool* flag, |
| 264 scoped_refptr<TestTaskRunner> task_runner, |
| 265 int32_t value, |
| 266 const IntegerSender::EchoCallback& callback) { |
| 267 EXPECT_EQ(expected_value, value); |
| 268 *flag = true; |
| 269 callback.Run(value); |
| 270 task_runner->Quit(); |
| 271 } |
| 272 |
| 273 base::Closure SetFlagAndQuitTaskRunner( |
| 274 bool* flag, |
| 275 scoped_refptr<TestTaskRunner> task_runner) { |
| 276 return base::Bind(&DoSetFlagAndQuitTaskRunner, flag, task_runner); |
| 277 } |
| 278 |
| 279 base::Callback<void(int32_t)> ExpectValueSetFlagAndQuitTaskRunner( |
| 280 int32_t expected_value, |
| 281 bool* flag, |
| 282 scoped_refptr<TestTaskRunner> task_runner) { |
| 283 return base::Bind(&DoExpectValueSetFlagAndQuitTaskRunner, expected_value, |
| 284 flag, task_runner); |
| 285 } |
| 286 |
| 238 TEST_F(BindTaskRunnerTest, MethodCall) { | 287 TEST_F(BindTaskRunnerTest, MethodCall) { |
| 239 bool echo_called = false; | 288 bool echo_called = false; |
| 240 impl_->set_echo_handler( | 289 impl_->set_echo_handler( |
| 241 [&, this](int32_t value, const IntegerSender::EchoCallback& callback) { | 290 base::Bind(&DoExpectValueSetFlagForwardValueAndQuitTaskRunner, |
| 242 EXPECT_EQ(1024, value); | 291 1024, &echo_called, binding_task_runner_)); |
| 243 echo_called = true; | |
| 244 callback.Run(value); | |
| 245 binding_task_runner_->Quit(); | |
| 246 }); | |
| 247 | |
| 248 bool echo_replied = false; | 292 bool echo_replied = false; |
| 249 ptr_->Echo(1024, [&, this](int32_t value) { | 293 ptr_->Echo(1024, ExpectValueSetFlagAndQuitTaskRunner(1024, &echo_replied, |
| 250 EXPECT_EQ(1024, value); | 294 ptr_task_runner_)); |
| 251 echo_replied = true; | |
| 252 ptr_task_runner_->Quit(); | |
| 253 }); | |
| 254 binding_task_runner_->Run(); | 295 binding_task_runner_->Run(); |
| 255 EXPECT_TRUE(echo_called); | 296 EXPECT_TRUE(echo_called); |
| 256 ptr_task_runner_->Run(); | 297 ptr_task_runner_->Run(); |
| 257 EXPECT_TRUE(echo_replied); | 298 EXPECT_TRUE(echo_replied); |
| 258 } | 299 } |
| 259 | 300 |
| 260 TEST_F(BindTaskRunnerTest, BindingConnectionError) { | 301 TEST_F(BindTaskRunnerTest, BindingConnectionError) { |
| 261 bool connection_error_called = false; | 302 bool connection_error_called = false; |
| 262 impl_->binding()->set_connection_error_handler([&, this]() { | 303 impl_->binding()->set_connection_error_handler( |
| 263 connection_error_called = true; | 304 SetFlagAndQuitTaskRunner(&connection_error_called, binding_task_runner_)); |
| 264 binding_task_runner_->Quit(); | |
| 265 }); | |
| 266 | |
| 267 ptr_.reset(); | 305 ptr_.reset(); |
| 268 binding_task_runner_->Run(); | 306 binding_task_runner_->Run(); |
| 269 EXPECT_TRUE(connection_error_called); | 307 EXPECT_TRUE(connection_error_called); |
| 270 } | 308 } |
| 271 | 309 |
| 272 TEST_F(BindTaskRunnerTest, PtrConnectionError) { | 310 TEST_F(BindTaskRunnerTest, PtrConnectionError) { |
| 273 bool connection_error_called = false; | 311 bool connection_error_called = false; |
| 274 ptr_.set_connection_error_handler([&, this]() { | 312 ptr_.set_connection_error_handler( |
| 275 connection_error_called = true; | 313 SetFlagAndQuitTaskRunner(&connection_error_called, ptr_task_runner_)); |
| 276 ptr_task_runner_->Quit(); | |
| 277 }); | |
| 278 | |
| 279 impl_->binding()->Close(); | 314 impl_->binding()->Close(); |
| 280 ptr_task_runner_->Run(); | 315 ptr_task_runner_->Run(); |
| 281 EXPECT_TRUE(connection_error_called); | 316 EXPECT_TRUE(connection_error_called); |
| 282 } | 317 } |
| 283 | 318 |
| 319 void ExpectValueSetFlagAndForward(int32_t expected_value, |
| 320 bool* flag, |
| 321 int32_t value, |
| 322 const IntegerSender::EchoCallback& callback) { |
| 323 EXPECT_EQ(expected_value, value); |
| 324 *flag = true; |
| 325 callback.Run(value); |
| 326 } |
| 327 |
| 284 TEST_F(AssociatedBindTaskRunnerTest, MethodCall) { | 328 TEST_F(AssociatedBindTaskRunnerTest, MethodCall) { |
| 285 bool echo_called = false; | 329 bool echo_called = false; |
| 286 connection_impl_->sender_impl()->set_echo_handler( | 330 connection_impl_->sender_impl()->set_echo_handler( |
| 287 [&](int32_t value, const IntegerSender::EchoCallback& callback) { | 331 base::Bind(&ExpectValueSetFlagAndForward, 1024, &echo_called)); |
| 288 EXPECT_EQ(1024, value); | |
| 289 echo_called = true; | |
| 290 callback.Run(value); | |
| 291 }); | |
| 292 | 332 |
| 293 bool echo_replied = false; | 333 bool echo_replied = false; |
| 294 sender_ptr_->Echo(1024, [&](int32_t value) { | 334 sender_ptr_->Echo( |
| 295 EXPECT_EQ(1024, value); | 335 1024, ExpectValueSetFlagAndQuitTaskRunner(1024, &echo_replied, nullptr)); |
| 296 echo_replied = true; | |
| 297 }); | |
| 298 | 336 |
| 299 // The Echo request first arrives at the master endpoint's task runner, and | 337 // The Echo request first arrives at the master endpoint's task runner, and |
| 300 // then is forwarded to the associated endpoint's task runner. | 338 // then is forwarded to the associated endpoint's task runner. |
| 301 connection_binding_task_runner_->RunOneTask(); | 339 connection_binding_task_runner_->RunOneTask(); |
| 302 sender_binding_task_runner_->RunOneTask(); | 340 sender_binding_task_runner_->RunOneTask(); |
| 303 EXPECT_TRUE(echo_called); | 341 EXPECT_TRUE(echo_called); |
| 304 | 342 |
| 305 // Similarly, the Echo response arrives at the master endpoint's task runner | 343 // Similarly, the Echo response arrives at the master endpoint's task runner |
| 306 // and then is forwarded to the associated endpoint's task runner. | 344 // and then is forwarded to the associated endpoint's task runner. |
| 307 connection_ptr_task_runner_->RunOneTask(); | 345 connection_ptr_task_runner_->RunOneTask(); |
| 308 sender_ptr_task_runner_->RunOneTask(); | 346 sender_ptr_task_runner_->RunOneTask(); |
| 309 EXPECT_TRUE(echo_replied); | 347 EXPECT_TRUE(echo_replied); |
| 310 } | 348 } |
| 311 | 349 |
| 312 TEST_F(AssociatedBindTaskRunnerTest, BindingConnectionError) { | 350 TEST_F(AssociatedBindTaskRunnerTest, BindingConnectionError) { |
| 313 bool sender_impl_error = false; | 351 bool sender_impl_error = false; |
| 314 connection_impl_->sender_impl()->binding()->set_connection_error_handler( | 352 connection_impl_->sender_impl()->binding()->set_connection_error_handler( |
| 315 [&, this]() { | 353 SetFlagAndQuitTaskRunner(&sender_impl_error, |
| 316 sender_impl_error = true; | 354 sender_binding_task_runner_)); |
| 317 sender_binding_task_runner_->Quit(); | |
| 318 }); | |
| 319 | |
| 320 bool connection_impl_error = false; | 355 bool connection_impl_error = false; |
| 321 connection_impl_->binding()->set_connection_error_handler([&, this]() { | 356 connection_impl_->binding()->set_connection_error_handler( |
| 322 connection_impl_error = true; | 357 SetFlagAndQuitTaskRunner(&connection_impl_error, |
| 323 connection_binding_task_runner_->Quit(); | 358 connection_binding_task_runner_)); |
| 324 }); | |
| 325 | |
| 326 bool sender_ptr_error = false; | 359 bool sender_ptr_error = false; |
| 327 sender_ptr_.set_connection_error_handler([&, this]() { | 360 sender_ptr_.set_connection_error_handler( |
| 328 sender_ptr_error = true; | 361 SetFlagAndQuitTaskRunner(&sender_ptr_error, sender_ptr_task_runner_)); |
| 329 sender_ptr_task_runner_->Quit(); | |
| 330 }); | |
| 331 connection_ptr_.reset(); | 362 connection_ptr_.reset(); |
| 332 sender_ptr_task_runner_->Run(); | 363 sender_ptr_task_runner_->Run(); |
| 333 EXPECT_TRUE(sender_ptr_error); | 364 EXPECT_TRUE(sender_ptr_error); |
| 334 connection_binding_task_runner_->Run(); | 365 connection_binding_task_runner_->Run(); |
| 335 EXPECT_TRUE(connection_impl_error); | 366 EXPECT_TRUE(connection_impl_error); |
| 336 sender_binding_task_runner_->Run(); | 367 sender_binding_task_runner_->Run(); |
| 337 EXPECT_TRUE(sender_impl_error); | 368 EXPECT_TRUE(sender_impl_error); |
| 338 } | 369 } |
| 339 | 370 |
| 340 TEST_F(AssociatedBindTaskRunnerTest, PtrConnectionError) { | 371 TEST_F(AssociatedBindTaskRunnerTest, PtrConnectionError) { |
| 341 bool sender_impl_error = false; | 372 bool sender_impl_error = false; |
| 342 connection_impl_->sender_impl()->binding()->set_connection_error_handler( | 373 connection_impl_->sender_impl()->binding()->set_connection_error_handler( |
| 343 [&, this]() { | 374 SetFlagAndQuitTaskRunner(&sender_impl_error, |
| 344 sender_impl_error = true; | 375 sender_binding_task_runner_)); |
| 345 sender_binding_task_runner_->Quit(); | |
| 346 }); | |
| 347 | |
| 348 bool connection_ptr_error = false; | 376 bool connection_ptr_error = false; |
| 349 connection_ptr_.set_connection_error_handler([&, this]() { | 377 connection_ptr_.set_connection_error_handler( |
| 350 connection_ptr_error = true; | 378 SetFlagAndQuitTaskRunner(&connection_ptr_error, |
| 351 connection_ptr_task_runner_->Quit(); | 379 connection_ptr_task_runner_)); |
| 352 }); | |
| 353 | |
| 354 bool sender_ptr_error = false; | 380 bool sender_ptr_error = false; |
| 355 sender_ptr_.set_connection_error_handler([&, this]() { | 381 sender_ptr_.set_connection_error_handler( |
| 356 sender_ptr_error = true; | 382 SetFlagAndQuitTaskRunner(&sender_ptr_error, sender_ptr_task_runner_)); |
| 357 sender_ptr_task_runner_->Quit(); | |
| 358 }); | |
| 359 connection_impl_->binding()->Close(); | 383 connection_impl_->binding()->Close(); |
| 360 sender_binding_task_runner_->Run(); | 384 sender_binding_task_runner_->Run(); |
| 361 EXPECT_TRUE(sender_impl_error); | 385 EXPECT_TRUE(sender_impl_error); |
| 362 connection_ptr_task_runner_->Run(); | 386 connection_ptr_task_runner_->Run(); |
| 363 EXPECT_TRUE(connection_ptr_error); | 387 EXPECT_TRUE(connection_ptr_error); |
| 364 sender_ptr_task_runner_->Run(); | 388 sender_ptr_task_runner_->Run(); |
| 365 EXPECT_TRUE(sender_ptr_error); | 389 EXPECT_TRUE(sender_ptr_error); |
| 366 } | 390 } |
| 367 | 391 |
| 368 } // namespace | 392 } // namespace |
| 369 } // namespace test | 393 } // namespace test |
| 370 } // namespace mojo | 394 } // namespace mojo |
| OLD | NEW |