| 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 "mojo/public/cpp/bindings/binding.h" | 5 #include "mojo/public/cpp/bindings/binding.h" |
| 6 #include "mojo/public/cpp/bindings/error_handler.h" | |
| 7 #include "mojo/public/cpp/bindings/strong_binding.h" | 6 #include "mojo/public/cpp/bindings/strong_binding.h" |
| 8 #include "mojo/public/cpp/environment/environment.h" | 7 #include "mojo/public/cpp/environment/environment.h" |
| 9 #include "mojo/public/cpp/utility/run_loop.h" | 8 #include "mojo/public/cpp/utility/run_loop.h" |
| 10 #include "mojo/public/interfaces/bindings/tests/math_calculator.mojom.h" | 9 #include "mojo/public/interfaces/bindings/tests/math_calculator.mojom.h" |
| 11 #include "mojo/public/interfaces/bindings/tests/sample_interfaces.mojom.h" | 10 #include "mojo/public/interfaces/bindings/tests/sample_interfaces.mojom.h" |
| 12 #include "mojo/public/interfaces/bindings/tests/sample_service.mojom.h" | 11 #include "mojo/public/interfaces/bindings/tests/sample_service.mojom.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 13 |
| 15 namespace mojo { | 14 namespace mojo { |
| 16 namespace test { | 15 namespace test { |
| 17 namespace { | 16 namespace { |
| 18 | 17 |
| 19 class ErrorObserver : public ErrorHandler { | |
| 20 public: | |
| 21 ErrorObserver() : encountered_error_(false) {} | |
| 22 | |
| 23 bool encountered_error() const { return encountered_error_; } | |
| 24 | |
| 25 void OnConnectionError() override { encountered_error_ = true; } | |
| 26 | |
| 27 private: | |
| 28 bool encountered_error_; | |
| 29 }; | |
| 30 | |
| 31 template <typename Method, typename Class> | 18 template <typename Method, typename Class> |
| 32 class RunnableImpl { | 19 class RunnableImpl { |
| 33 public: | 20 public: |
| 34 RunnableImpl(Method method, Class instance) | 21 RunnableImpl(Method method, Class instance) |
| 35 : method_(method), instance_(instance) {} | 22 : method_(method), instance_(instance) {} |
| 36 template <typename... Args> | 23 template <typename... Args> |
| 37 void Run(Args... args) const { | 24 void Run(Args... args) const { |
| 38 (instance_->*method_)(args...); | 25 (instance_->*method_)(args...); |
| 39 } | 26 } |
| 40 | 27 |
| (...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 312 PumpMessages(); | 299 PumpMessages(); |
| 313 | 300 |
| 314 // OK, now we see the error. | 301 // OK, now we see the error. |
| 315 EXPECT_TRUE(calculator_ui.encountered_error()); | 302 EXPECT_TRUE(calculator_ui.encountered_error()); |
| 316 } | 303 } |
| 317 | 304 |
| 318 TEST_F(InterfacePtrTest, EncounteredErrorCallback) { | 305 TEST_F(InterfacePtrTest, EncounteredErrorCallback) { |
| 319 math::CalculatorPtr proxy; | 306 math::CalculatorPtr proxy; |
| 320 MathCalculatorImpl calc_impl(GetProxy(&proxy)); | 307 MathCalculatorImpl calc_impl(GetProxy(&proxy)); |
| 321 | 308 |
| 322 ErrorObserver error_observer; | 309 bool encountered_error = false; |
| 323 proxy.set_error_handler(&error_observer); | 310 proxy.set_connection_error_handler( |
| 311 [&encountered_error]() { encountered_error = true; }); |
| 324 | 312 |
| 325 MathCalculatorUI calculator_ui(proxy.Pass()); | 313 MathCalculatorUI calculator_ui(proxy.Pass()); |
| 326 | 314 |
| 327 calculator_ui.Add(2.0); | 315 calculator_ui.Add(2.0); |
| 328 PumpMessages(); | 316 PumpMessages(); |
| 329 EXPECT_EQ(2.0, calculator_ui.GetOutput()); | 317 EXPECT_EQ(2.0, calculator_ui.GetOutput()); |
| 330 EXPECT_FALSE(calculator_ui.encountered_error()); | 318 EXPECT_FALSE(calculator_ui.encountered_error()); |
| 331 | 319 |
| 332 calculator_ui.Multiply(5.0); | 320 calculator_ui.Multiply(5.0); |
| 333 EXPECT_FALSE(calculator_ui.encountered_error()); | 321 EXPECT_FALSE(calculator_ui.encountered_error()); |
| 334 | 322 |
| 335 // Close the server. | 323 // Close the server. |
| 336 calc_impl.CloseMessagePipe(); | 324 calc_impl.CloseMessagePipe(); |
| 337 | 325 |
| 338 // The state change isn't picked up locally yet. | 326 // The state change isn't picked up locally yet. |
| 339 EXPECT_FALSE(calculator_ui.encountered_error()); | 327 EXPECT_FALSE(calculator_ui.encountered_error()); |
| 340 | 328 |
| 341 PumpMessages(); | 329 PumpMessages(); |
| 342 | 330 |
| 343 // OK, now we see the error. | 331 // OK, now we see the error. |
| 344 EXPECT_TRUE(calculator_ui.encountered_error()); | 332 EXPECT_TRUE(calculator_ui.encountered_error()); |
| 345 | 333 |
| 346 // We should have also been able to observe the error through the | 334 // We should have also been able to observe the error through the error |
| 347 // ErrorHandler interface. | 335 // handler. |
| 348 EXPECT_TRUE(error_observer.encountered_error()); | 336 EXPECT_TRUE(encountered_error); |
| 349 } | 337 } |
| 350 | 338 |
| 351 TEST_F(InterfacePtrTest, DestroyInterfacePtrOnMethodResponse) { | 339 TEST_F(InterfacePtrTest, DestroyInterfacePtrOnMethodResponse) { |
| 352 math::CalculatorPtr proxy; | 340 math::CalculatorPtr proxy; |
| 353 MathCalculatorImpl calc_impl(GetProxy(&proxy)); | 341 MathCalculatorImpl calc_impl(GetProxy(&proxy)); |
| 354 | 342 |
| 355 EXPECT_EQ(0, SelfDestructingMathCalculatorUI::num_instances()); | 343 EXPECT_EQ(0, SelfDestructingMathCalculatorUI::num_instances()); |
| 356 | 344 |
| 357 SelfDestructingMathCalculatorUI* impl = | 345 SelfDestructingMathCalculatorUI* impl = |
| 358 new SelfDestructingMathCalculatorUI(proxy.Pass()); | 346 new SelfDestructingMathCalculatorUI(proxy.Pass()); |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 432 ptr.RequireVersion(4u); | 420 ptr.RequireVersion(4u); |
| 433 // This value is set to the input of RequireVersion() synchronously. | 421 // This value is set to the input of RequireVersion() synchronously. |
| 434 EXPECT_EQ(4u, ptr.version()); | 422 EXPECT_EQ(4u, ptr.version()); |
| 435 ptr->SetInteger(789, sample::ENUM_VALUE); | 423 ptr->SetInteger(789, sample::ENUM_VALUE); |
| 436 PumpMessages(); | 424 PumpMessages(); |
| 437 EXPECT_TRUE(ptr.encountered_error()); | 425 EXPECT_TRUE(ptr.encountered_error()); |
| 438 // The call to SetInteger() after RequireVersion(4u) is ignored. | 426 // The call to SetInteger() after RequireVersion(4u) is ignored. |
| 439 EXPECT_EQ(456, impl.integer()); | 427 EXPECT_EQ(456, impl.integer()); |
| 440 } | 428 } |
| 441 | 429 |
| 442 class StrongMathCalculatorImpl : public math::Calculator, public ErrorHandler { | 430 class StrongMathCalculatorImpl : public math::Calculator { |
| 443 public: | 431 public: |
| 444 StrongMathCalculatorImpl(ScopedMessagePipeHandle handle, | 432 StrongMathCalculatorImpl(ScopedMessagePipeHandle handle, |
| 445 bool* error_received, | 433 bool* error_received, |
| 446 bool* destroyed) | 434 bool* destroyed) |
| 447 : error_received_(error_received), | 435 : error_received_(error_received), |
| 448 destroyed_(destroyed), | 436 destroyed_(destroyed), |
| 449 binding_(this, handle.Pass()) { | 437 binding_(this, handle.Pass()) { |
| 450 binding_.set_error_handler(this); | 438 binding_.set_connection_error_handler( |
| 439 [this]() { *error_received_ = true; }); |
| 451 } | 440 } |
| 452 ~StrongMathCalculatorImpl() override { *destroyed_ = true; } | 441 ~StrongMathCalculatorImpl() override { *destroyed_ = true; } |
| 453 | 442 |
| 454 // math::Calculator implementation. | 443 // math::Calculator implementation. |
| 455 void Clear(const CalcCallback& callback) override { callback.Run(total_); } | 444 void Clear(const CalcCallback& callback) override { callback.Run(total_); } |
| 456 | 445 |
| 457 void Add(double value, const CalcCallback& callback) override { | 446 void Add(double value, const CalcCallback& callback) override { |
| 458 total_ += value; | 447 total_ += value; |
| 459 callback.Run(total_); | 448 callback.Run(total_); |
| 460 } | 449 } |
| 461 | 450 |
| 462 void Multiply(double value, const CalcCallback& callback) override { | 451 void Multiply(double value, const CalcCallback& callback) override { |
| 463 total_ *= value; | 452 total_ *= value; |
| 464 callback.Run(total_); | 453 callback.Run(total_); |
| 465 } | 454 } |
| 466 | 455 |
| 467 // ErrorHandler implementation. | |
| 468 void OnConnectionError() override { *error_received_ = true; } | |
| 469 | |
| 470 private: | 456 private: |
| 471 double total_ = 0.0; | 457 double total_ = 0.0; |
| 472 bool* error_received_; | 458 bool* error_received_; |
| 473 bool* destroyed_; | 459 bool* destroyed_; |
| 474 | 460 |
| 475 StrongBinding<math::Calculator> binding_; | 461 StrongBinding<math::Calculator> binding_; |
| 476 }; | 462 }; |
| 477 | 463 |
| 478 TEST(StrongConnectorTest, Math) { | 464 TEST(StrongConnectorTest, Math) { |
| 479 Environment env; | 465 Environment env; |
| (...skipping 24 matching lines...) Expand all Loading... |
| 504 } | 490 } |
| 505 // Destroying calculator_ui should close the pipe and generate an error on the | 491 // Destroying calculator_ui should close the pipe and generate an error on the |
| 506 // other | 492 // other |
| 507 // end which will destroy the instance since it is strongly bound. | 493 // end which will destroy the instance since it is strongly bound. |
| 508 | 494 |
| 509 loop.RunUntilIdle(); | 495 loop.RunUntilIdle(); |
| 510 EXPECT_TRUE(error_received); | 496 EXPECT_TRUE(error_received); |
| 511 EXPECT_TRUE(destroyed); | 497 EXPECT_TRUE(destroyed); |
| 512 } | 498 } |
| 513 | 499 |
| 514 class WeakMathCalculatorImpl : public math::Calculator, public ErrorHandler { | 500 class WeakMathCalculatorImpl : public math::Calculator { |
| 515 public: | 501 public: |
| 516 WeakMathCalculatorImpl(ScopedMessagePipeHandle handle, | 502 WeakMathCalculatorImpl(ScopedMessagePipeHandle handle, |
| 517 bool* error_received, | 503 bool* error_received, |
| 518 bool* destroyed) | 504 bool* destroyed) |
| 519 : error_received_(error_received), | 505 : error_received_(error_received), |
| 520 destroyed_(destroyed), | 506 destroyed_(destroyed), |
| 521 binding_(this, handle.Pass()) { | 507 binding_(this, handle.Pass()) { |
| 522 binding_.set_error_handler(this); | 508 binding_.set_connection_error_handler( |
| 509 [this]() { *error_received_ = true; }); |
| 523 } | 510 } |
| 524 ~WeakMathCalculatorImpl() override { *destroyed_ = true; } | 511 ~WeakMathCalculatorImpl() override { *destroyed_ = true; } |
| 525 | 512 |
| 526 void Clear(const CalcCallback& callback) override { callback.Run(total_); } | 513 void Clear(const CalcCallback& callback) override { callback.Run(total_); } |
| 527 | 514 |
| 528 void Add(double value, const CalcCallback& callback) override { | 515 void Add(double value, const CalcCallback& callback) override { |
| 529 total_ += value; | 516 total_ += value; |
| 530 callback.Run(total_); | 517 callback.Run(total_); |
| 531 } | 518 } |
| 532 | 519 |
| 533 void Multiply(double value, const CalcCallback& callback) override { | 520 void Multiply(double value, const CalcCallback& callback) override { |
| 534 total_ *= value; | 521 total_ *= value; |
| 535 callback.Run(total_); | 522 callback.Run(total_); |
| 536 } | 523 } |
| 537 | 524 |
| 538 // ErrorHandler implementation. | |
| 539 void OnConnectionError() override { *error_received_ = true; } | |
| 540 | |
| 541 private: | 525 private: |
| 542 double total_ = 0.0; | 526 double total_ = 0.0; |
| 543 bool* error_received_; | 527 bool* error_received_; |
| 544 bool* destroyed_; | 528 bool* destroyed_; |
| 545 | 529 |
| 546 Binding<math::Calculator> binding_; | 530 Binding<math::Calculator> binding_; |
| 547 }; | 531 }; |
| 548 | 532 |
| 549 TEST(WeakConnectorTest, Math) { | 533 TEST(WeakConnectorTest, Math) { |
| 550 Environment env; | 534 Environment env; |
| (...skipping 26 matching lines...) Expand all Loading... |
| 577 } | 561 } |
| 578 | 562 |
| 579 loop.RunUntilIdle(); | 563 loop.RunUntilIdle(); |
| 580 EXPECT_TRUE(error_received); | 564 EXPECT_TRUE(error_received); |
| 581 EXPECT_FALSE(destroyed); | 565 EXPECT_FALSE(destroyed); |
| 582 } | 566 } |
| 583 | 567 |
| 584 } // namespace | 568 } // namespace |
| 585 } // namespace test | 569 } // namespace test |
| 586 } // namespace mojo | 570 } // namespace mojo |
| OLD | NEW |