| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 <stdint.h> | 5 #include <stdint.h> |
| 6 #include <utility> | 6 #include <utility> |
| 7 | 7 |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| 11 #include "mojo/public/cpp/bindings/binding.h" | 11 #include "mojo/public/cpp/bindings/binding.h" |
| 12 #include "mojo/public/cpp/bindings/strong_binding.h" | 12 #include "mojo/public/cpp/bindings/strong_binding.h" |
| 13 #include "mojo/public/interfaces/bindings/tests/math_calculator.mojom.h" | 13 #include "mojo/public/interfaces/bindings/tests/math_calculator.mojom.h" |
| 14 #include "mojo/public/interfaces/bindings/tests/sample_interfaces.mojom.h" | 14 #include "mojo/public/interfaces/bindings/tests/sample_interfaces.mojom.h" |
| 15 #include "mojo/public/interfaces/bindings/tests/sample_service.mojom.h" | 15 #include "mojo/public/interfaces/bindings/tests/sample_service.mojom.h" |
| 16 #include "mojo/public/interfaces/bindings/tests/scoping.mojom.h" | 16 #include "mojo/public/interfaces/bindings/tests/scoping.mojom.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 18 |
| 19 namespace mojo { | 19 namespace mojo { |
| 20 namespace test { | 20 namespace test { |
| 21 namespace { | 21 namespace { |
| 22 | 22 |
| 23 template <typename Method, typename Class> | |
| 24 class RunnableImpl { | |
| 25 public: | |
| 26 RunnableImpl(Method method, Class instance) | |
| 27 : method_(method), instance_(instance) {} | |
| 28 template <typename... Args> | |
| 29 void Run(Args... args) const { | |
| 30 (instance_->*method_)(args...); | |
| 31 } | |
| 32 | |
| 33 private: | |
| 34 Method method_; | |
| 35 Class instance_; | |
| 36 }; | |
| 37 | |
| 38 template <typename Method, typename Class> | |
| 39 RunnableImpl<Method, Class> MakeRunnable(Method method, Class object) { | |
| 40 return RunnableImpl<Method, Class>(method, object); | |
| 41 } | |
| 42 | |
| 43 typedef mojo::Callback<void(double)> CalcCallback; | 23 typedef mojo::Callback<void(double)> CalcCallback; |
| 44 | 24 |
| 45 class MathCalculatorImpl : public math::Calculator { | 25 class MathCalculatorImpl : public math::Calculator { |
| 46 public: | 26 public: |
| 47 explicit MathCalculatorImpl(InterfaceRequest<math::Calculator> request) | 27 explicit MathCalculatorImpl(InterfaceRequest<math::Calculator> request) |
| 48 : total_(0.0), binding_(this, std::move(request)) {} | 28 : total_(0.0), binding_(this, std::move(request)) {} |
| 49 ~MathCalculatorImpl() override {} | 29 ~MathCalculatorImpl() override {} |
| 50 | 30 |
| 51 void CloseMessagePipe() { binding_.Close(); } | 31 void CloseMessagePipe() { binding_.Close(); } |
| 52 | 32 |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 215 public: | 195 public: |
| 216 InterfacePtrTest() {} | 196 InterfacePtrTest() {} |
| 217 ~InterfacePtrTest() override { loop_.RunUntilIdle(); } | 197 ~InterfacePtrTest() override { loop_.RunUntilIdle(); } |
| 218 | 198 |
| 219 void PumpMessages() { loop_.RunUntilIdle(); } | 199 void PumpMessages() { loop_.RunUntilIdle(); } |
| 220 | 200 |
| 221 private: | 201 private: |
| 222 base::MessageLoop loop_; | 202 base::MessageLoop loop_; |
| 223 }; | 203 }; |
| 224 | 204 |
| 205 void SetFlagAndRunClosure(bool* flag, const base::Closure& closure) { |
| 206 *flag = true; |
| 207 closure.Run(); |
| 208 } |
| 209 |
| 210 void IgnoreValueAndRunClosure(const base::Closure& closure, int32_t value) { |
| 211 closure.Run(); |
| 212 } |
| 213 |
| 214 void ExpectValueAndRunClosure(uint32_t expected_value, |
| 215 const base::Closure& closure, |
| 216 uint32_t value) { |
| 217 EXPECT_EQ(expected_value, value); |
| 218 closure.Run(); |
| 219 } |
| 220 |
| 225 TEST_F(InterfacePtrTest, IsBound) { | 221 TEST_F(InterfacePtrTest, IsBound) { |
| 226 math::CalculatorPtr calc; | 222 math::CalculatorPtr calc; |
| 227 EXPECT_FALSE(calc.is_bound()); | 223 EXPECT_FALSE(calc.is_bound()); |
| 228 MathCalculatorImpl calc_impl(GetProxy(&calc)); | 224 MathCalculatorImpl calc_impl(GetProxy(&calc)); |
| 229 EXPECT_TRUE(calc.is_bound()); | 225 EXPECT_TRUE(calc.is_bound()); |
| 230 } | 226 } |
| 231 | 227 |
| 232 TEST_F(InterfacePtrTest, EndToEnd) { | 228 TEST_F(InterfacePtrTest, EndToEnd) { |
| 233 math::CalculatorPtr calc; | 229 math::CalculatorPtr calc; |
| 234 MathCalculatorImpl calc_impl(GetProxy(&calc)); | 230 MathCalculatorImpl calc_impl(GetProxy(&calc)); |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 344 EXPECT_TRUE(calculator_ui.encountered_error()); | 340 EXPECT_TRUE(calculator_ui.encountered_error()); |
| 345 } | 341 } |
| 346 | 342 |
| 347 TEST_F(InterfacePtrTest, EncounteredErrorCallback) { | 343 TEST_F(InterfacePtrTest, EncounteredErrorCallback) { |
| 348 math::CalculatorPtr proxy; | 344 math::CalculatorPtr proxy; |
| 349 MathCalculatorImpl calc_impl(GetProxy(&proxy)); | 345 MathCalculatorImpl calc_impl(GetProxy(&proxy)); |
| 350 | 346 |
| 351 bool encountered_error = false; | 347 bool encountered_error = false; |
| 352 base::RunLoop run_loop; | 348 base::RunLoop run_loop; |
| 353 proxy.set_connection_error_handler( | 349 proxy.set_connection_error_handler( |
| 354 [&encountered_error, &run_loop]() { | 350 base::Bind(&SetFlagAndRunClosure, &encountered_error, |
| 355 encountered_error = true; | 351 run_loop.QuitClosure())); |
| 356 run_loop.Quit(); | |
| 357 }); | |
| 358 | 352 |
| 359 MathCalculatorUI calculator_ui(std::move(proxy)); | 353 MathCalculatorUI calculator_ui(std::move(proxy)); |
| 360 | 354 |
| 361 base::RunLoop run_loop2; | 355 base::RunLoop run_loop2; |
| 362 calculator_ui.Add(2.0, run_loop2.QuitClosure()); | 356 calculator_ui.Add(2.0, run_loop2.QuitClosure()); |
| 363 run_loop2.Run(); | 357 run_loop2.Run(); |
| 364 EXPECT_EQ(2.0, calculator_ui.GetOutput()); | 358 EXPECT_EQ(2.0, calculator_ui.GetOutput()); |
| 365 EXPECT_FALSE(calculator_ui.encountered_error()); | 359 EXPECT_FALSE(calculator_ui.encountered_error()); |
| 366 | 360 |
| 367 calculator_ui.Multiply(5.0, base::Closure()); | 361 calculator_ui.Multiply(5.0, base::Closure()); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 411 run_loop.Run(); | 405 run_loop.Run(); |
| 412 | 406 |
| 413 EXPECT_EQ(0, SelfDestructingMathCalculatorUI::num_instances()); | 407 EXPECT_EQ(0, SelfDestructingMathCalculatorUI::num_instances()); |
| 414 } | 408 } |
| 415 | 409 |
| 416 TEST_F(InterfacePtrTest, ReentrantWaitForIncomingMethodCall) { | 410 TEST_F(InterfacePtrTest, ReentrantWaitForIncomingMethodCall) { |
| 417 sample::ServicePtr proxy; | 411 sample::ServicePtr proxy; |
| 418 ReentrantServiceImpl impl(GetProxy(&proxy)); | 412 ReentrantServiceImpl impl(GetProxy(&proxy)); |
| 419 | 413 |
| 420 base::RunLoop run_loop, run_loop2; | 414 base::RunLoop run_loop, run_loop2; |
| 421 auto called_cb = [&run_loop](int32_t result) { run_loop.Quit(); }; | |
| 422 auto called_cb2 = [&run_loop2](int32_t result) { run_loop2.Quit(); }; | |
| 423 proxy->Frobinate(nullptr, sample::Service::BazOptions::REGULAR, nullptr, | 415 proxy->Frobinate(nullptr, sample::Service::BazOptions::REGULAR, nullptr, |
| 424 called_cb); | 416 base::Bind(&IgnoreValueAndRunClosure, |
| 417 run_loop.QuitClosure())); |
| 425 proxy->Frobinate(nullptr, sample::Service::BazOptions::REGULAR, nullptr, | 418 proxy->Frobinate(nullptr, sample::Service::BazOptions::REGULAR, nullptr, |
| 426 called_cb2); | 419 base::Bind(&IgnoreValueAndRunClosure, |
| 420 run_loop2.QuitClosure())); |
| 427 | 421 |
| 428 run_loop.Run(); | 422 run_loop.Run(); |
| 429 run_loop2.Run(); | 423 run_loop2.Run(); |
| 430 | 424 |
| 431 EXPECT_EQ(2, impl.max_call_depth()); | 425 EXPECT_EQ(2, impl.max_call_depth()); |
| 432 } | 426 } |
| 433 | 427 |
| 434 TEST_F(InterfacePtrTest, QueryVersion) { | 428 TEST_F(InterfacePtrTest, QueryVersion) { |
| 435 IntegerAccessorImpl impl; | 429 IntegerAccessorImpl impl; |
| 436 sample::IntegerAccessorPtr ptr; | 430 sample::IntegerAccessorPtr ptr; |
| 437 Binding<sample::IntegerAccessor> binding(&impl, GetProxy(&ptr)); | 431 Binding<sample::IntegerAccessor> binding(&impl, GetProxy(&ptr)); |
| 438 | 432 |
| 439 EXPECT_EQ(0u, ptr.version()); | 433 EXPECT_EQ(0u, ptr.version()); |
| 440 | 434 |
| 441 base::RunLoop run_loop; | 435 base::RunLoop run_loop; |
| 442 auto callback = [&run_loop](uint32_t version) { | 436 ptr.QueryVersion(base::Bind(&ExpectValueAndRunClosure, 3u, |
| 443 EXPECT_EQ(3u, version); | 437 run_loop.QuitClosure())); |
| 444 run_loop.Quit(); | |
| 445 }; | |
| 446 ptr.QueryVersion(callback); | |
| 447 | |
| 448 run_loop.Run(); | 438 run_loop.Run(); |
| 449 | 439 |
| 450 EXPECT_EQ(3u, ptr.version()); | 440 EXPECT_EQ(3u, ptr.version()); |
| 451 } | 441 } |
| 452 | 442 |
| 453 TEST_F(InterfacePtrTest, RequireVersion) { | 443 TEST_F(InterfacePtrTest, RequireVersion) { |
| 454 IntegerAccessorImpl impl; | 444 IntegerAccessorImpl impl; |
| 455 sample::IntegerAccessorPtr ptr; | 445 sample::IntegerAccessorPtr ptr; |
| 456 Binding<sample::IntegerAccessor> binding(&impl, GetProxy(&ptr)); | 446 Binding<sample::IntegerAccessor> binding(&impl, GetProxy(&ptr)); |
| 457 | 447 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 492 public: | 482 public: |
| 493 StrongMathCalculatorImpl(ScopedMessagePipeHandle handle, | 483 StrongMathCalculatorImpl(ScopedMessagePipeHandle handle, |
| 494 bool* error_received, | 484 bool* error_received, |
| 495 bool* destroyed, | 485 bool* destroyed, |
| 496 const base::Closure& closure) | 486 const base::Closure& closure) |
| 497 : error_received_(error_received), | 487 : error_received_(error_received), |
| 498 destroyed_(destroyed), | 488 destroyed_(destroyed), |
| 499 closure_(closure), | 489 closure_(closure), |
| 500 binding_(this, std::move(handle)) { | 490 binding_(this, std::move(handle)) { |
| 501 binding_.set_connection_error_handler( | 491 binding_.set_connection_error_handler( |
| 502 [this]() { *error_received_ = true; closure_.Run(); }); | 492 base::Bind(&SetFlagAndRunClosure, error_received_, closure_)); |
| 503 } | 493 } |
| 504 ~StrongMathCalculatorImpl() override { *destroyed_ = true; } | 494 ~StrongMathCalculatorImpl() override { *destroyed_ = true; } |
| 505 | 495 |
| 506 // math::Calculator implementation. | 496 // math::Calculator implementation. |
| 507 void Clear(const CalcCallback& callback) override { callback.Run(total_); } | 497 void Clear(const CalcCallback& callback) override { callback.Run(total_); } |
| 508 | 498 |
| 509 void Add(double value, const CalcCallback& callback) override { | 499 void Add(double value, const CalcCallback& callback) override { |
| 510 total_ += value; | 500 total_ += value; |
| 511 callback.Run(total_); | 501 callback.Run(total_); |
| 512 } | 502 } |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 566 public: | 556 public: |
| 567 WeakMathCalculatorImpl(ScopedMessagePipeHandle handle, | 557 WeakMathCalculatorImpl(ScopedMessagePipeHandle handle, |
| 568 bool* error_received, | 558 bool* error_received, |
| 569 bool* destroyed, | 559 bool* destroyed, |
| 570 const base::Closure& closure) | 560 const base::Closure& closure) |
| 571 : error_received_(error_received), | 561 : error_received_(error_received), |
| 572 destroyed_(destroyed), | 562 destroyed_(destroyed), |
| 573 closure_(closure), | 563 closure_(closure), |
| 574 binding_(this, std::move(handle)) { | 564 binding_(this, std::move(handle)) { |
| 575 binding_.set_connection_error_handler( | 565 binding_.set_connection_error_handler( |
| 576 [this]() { *error_received_ = true; closure_.Run(); }); | 566 base::Bind(&SetFlagAndRunClosure, error_received_, closure_)); |
| 577 } | 567 } |
| 578 ~WeakMathCalculatorImpl() override { *destroyed_ = true; } | 568 ~WeakMathCalculatorImpl() override { *destroyed_ = true; } |
| 579 | 569 |
| 580 void Clear(const CalcCallback& callback) override { callback.Run(total_); } | 570 void Clear(const CalcCallback& callback) override { callback.Run(total_); } |
| 581 | 571 |
| 582 void Add(double value, const CalcCallback& callback) override { | 572 void Add(double value, const CalcCallback& callback) override { |
| 583 total_ += value; | 573 total_ += value; |
| 584 callback.Run(total_); | 574 callback.Run(total_); |
| 585 } | 575 } |
| 586 | 576 |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 734 // Create another PingTest pipe. | 724 // Create another PingTest pipe. |
| 735 sample::PingTestPtr ptr; | 725 sample::PingTestPtr ptr; |
| 736 sample::PingTestRequest request = GetProxy(&ptr); | 726 sample::PingTestRequest request = GetProxy(&ptr); |
| 737 | 727 |
| 738 // Fuse the new pipe to the one hanging off |impl|. | 728 // Fuse the new pipe to the one hanging off |impl|. |
| 739 EXPECT_TRUE(FuseInterface(std::move(request), proxy.PassInterface())); | 729 EXPECT_TRUE(FuseInterface(std::move(request), proxy.PassInterface())); |
| 740 | 730 |
| 741 // Ping! | 731 // Ping! |
| 742 bool called = false; | 732 bool called = false; |
| 743 base::RunLoop loop; | 733 base::RunLoop loop; |
| 744 ptr->Ping([&called, &loop] { | 734 ptr->Ping(base::Bind(&SetFlagAndRunClosure, &called, loop.QuitClosure())); |
| 745 called = true; | |
| 746 loop.Quit(); | |
| 747 }); | |
| 748 loop.Run(); | 735 loop.Run(); |
| 749 EXPECT_TRUE(called); | 736 EXPECT_TRUE(called); |
| 750 } | 737 } |
| 751 | 738 |
| 752 } // namespace | 739 } // namespace |
| 753 } // namespace test | 740 } // namespace test |
| 754 } // namespace mojo | 741 } // namespace mojo |
| OLD | NEW |