| Index: mojo/public/cpp/bindings/tests/interface_ptr_unittest.cc
|
| diff --git a/mojo/public/cpp/bindings/tests/interface_ptr_unittest.cc b/mojo/public/cpp/bindings/tests/interface_ptr_unittest.cc
|
| index 0cd56462555c046ab7ae28ce7fa16f1995524a4b..da0d681ef291890289a2fc384dcb27712d2226cf 100644
|
| --- a/mojo/public/cpp/bindings/tests/interface_ptr_unittest.cc
|
| +++ b/mojo/public/cpp/bindings/tests/interface_ptr_unittest.cc
|
| @@ -3,6 +3,8 @@
|
| // found in the LICENSE file.
|
|
|
| #include "mojo/public/cpp/bindings/error_handler.h"
|
| +#include "mojo/public/cpp/bindings/strong_connector.h"
|
| +#include "mojo/public/cpp/bindings/weak_connector.h"
|
| #include "mojo/public/cpp/environment/environment.h"
|
| #include "mojo/public/cpp/utility/run_loop.h"
|
| #include "mojo/public/interfaces/bindings/tests/math_calculator.mojom.h"
|
| @@ -351,6 +353,148 @@ TEST_F(InterfacePtrTest, ReentrantWaitForIncomingMethodCall) {
|
| EXPECT_EQ(2, impl->max_call_depth());
|
| }
|
|
|
| +class StrongMathCalculatorImpl : public math::Calculator, public ErrorHandler {
|
| + public:
|
| + StrongMathCalculatorImpl(ScopedMessagePipeHandle handle,
|
| + bool* error_received,
|
| + bool* destroyed)
|
| + : error_received_(error_received),
|
| + destroyed_(destroyed),
|
| + connector_(this, handle.Pass()) {
|
| + connector_.set_error_handler(this);
|
| + }
|
| + ~StrongMathCalculatorImpl() override { *destroyed_ = true; }
|
| +
|
| + // math::Calculator implementation.
|
| + void Clear() override { connector_.client()->Output(total_); }
|
| +
|
| + void Add(double value) override {
|
| + total_ += value;
|
| + connector_.client()->Output(total_);
|
| + }
|
| +
|
| + void Multiply(double value) override {
|
| + total_ *= value;
|
| + connector_.client()->Output(total_);
|
| + }
|
| +
|
| + // ErrorHandler implementation.
|
| + void OnConnectionError() override { *error_received_ = true; }
|
| +
|
| + private:
|
| + double total_ = 0.0;
|
| + bool* error_received_;
|
| + bool* destroyed_;
|
| +
|
| + StrongConnector<math::Calculator> connector_;
|
| +};
|
| +
|
| +TEST(StrongConnectorTest, Math) {
|
| + Environment env;
|
| + RunLoop loop;
|
| +
|
| + bool error_received = false;
|
| + bool destroyed = false;
|
| + MessagePipe pipe;
|
| + new StrongMathCalculatorImpl(
|
| + pipe.handle0.Pass(), &error_received, &destroyed);
|
| +
|
| + math::CalculatorPtr calc;
|
| + calc.Bind(pipe.handle1.Pass());
|
| +
|
| + {
|
| + // Suppose this is instantiated in a process that has the other end of the
|
| + // message pipe.
|
| + MathCalculatorUIImpl calculator_ui(calc.Pass());
|
| +
|
| + calculator_ui.Add(2.0);
|
| + calculator_ui.Multiply(5.0);
|
| +
|
| + loop.RunUntilIdle();
|
| +
|
| + EXPECT_EQ(10.0, calculator_ui.GetOutput());
|
| + EXPECT_FALSE(error_received);
|
| + EXPECT_FALSE(destroyed);
|
| + }
|
| + // Destroying calculator_ui should close the pipe and generate an error on the
|
| + // other
|
| + // end which will destroy the instance since it is strongly bound.
|
| +
|
| + loop.RunUntilIdle();
|
| + EXPECT_TRUE(error_received);
|
| + EXPECT_TRUE(destroyed);
|
| +}
|
| +
|
| +class WeakMathCalculatorImpl : public math::Calculator, public ErrorHandler {
|
| + public:
|
| + WeakMathCalculatorImpl(ScopedMessagePipeHandle handle,
|
| + bool* error_received,
|
| + bool* destroyed)
|
| + : error_received_(error_received),
|
| + destroyed_(destroyed),
|
| + connector_(this, handle.Pass()) {
|
| + connector_.set_error_handler(this);
|
| + }
|
| + ~WeakMathCalculatorImpl() override { *destroyed_ = true; }
|
| +
|
| + void Clear() override { connector_.client()->Output(total_); }
|
| +
|
| + void Add(double value) override {
|
| + total_ += value;
|
| + connector_.client()->Output(total_);
|
| + }
|
| +
|
| + void Multiply(double value) override {
|
| + total_ *= value;
|
| + connector_.client()->Output(total_);
|
| + }
|
| +
|
| + // ErrorHandler implementation.
|
| + void OnConnectionError() override { *error_received_ = true; }
|
| +
|
| + private:
|
| + double total_ = 0.0;
|
| + bool* error_received_;
|
| + bool* destroyed_;
|
| +
|
| + WeakConnector<math::Calculator> connector_;
|
| +};
|
| +
|
| +TEST(WeakConnectorTest, Math) {
|
| + Environment env;
|
| + RunLoop loop;
|
| +
|
| + bool error_received = false;
|
| + bool destroyed = false;
|
| + MessagePipe pipe;
|
| + WeakMathCalculatorImpl impl(pipe.handle0.Pass(), &error_received, &destroyed);
|
| +
|
| + math::CalculatorPtr calc;
|
| + calc.Bind(pipe.handle1.Pass());
|
| +
|
| + {
|
| + // Suppose this is instantiated in a process that has the other end of the
|
| + // message pipe.
|
| + MathCalculatorUIImpl calculator_ui(calc.Pass());
|
| +
|
| + calculator_ui.Add(2.0);
|
| + calculator_ui.Multiply(5.0);
|
| +
|
| + loop.RunUntilIdle();
|
| +
|
| + EXPECT_EQ(10.0, calculator_ui.GetOutput());
|
| + EXPECT_FALSE(error_received);
|
| + EXPECT_FALSE(destroyed);
|
| + // Destroying calculator_ui should close the pipe and generate an error on
|
| + // the other
|
| + // end which will destroy the instance since it is strongly bound.
|
| + }
|
| +
|
| + loop.RunUntilIdle();
|
| + EXPECT_TRUE(error_received);
|
| + EXPECT_FALSE(destroyed);
|
| +}
|
| +
|
| } // namespace
|
| } // namespace test
|
| } // namespace mojo
|
|
|