Chromium Code Reviews| 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 f90dc3de1ea13e82398f0df4283aecab9834efff..2a0f66406f04f564292e80195e76c192bb6f26e7 100644 |
| --- a/mojo/public/cpp/bindings/tests/interface_ptr_unittest.cc |
| +++ b/mojo/public/cpp/bindings/tests/interface_ptr_unittest.cc |
| @@ -2,6 +2,8 @@ |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| +#include "base/bind.h" |
| +#include "base/callback.h" |
| #include "mojo/public/cpp/bindings/binding.h" |
| #include "mojo/public/cpp/bindings/error_handler.h" |
| #include "mojo/public/cpp/bindings/strong_binding.h" |
| @@ -27,34 +29,40 @@ class ErrorObserver : public ErrorHandler { |
| bool encountered_error_; |
| }; |
| +typedef mojo::Callback<void(double)> CalcCallback; |
| + |
| class MathCalculatorImpl : public InterfaceImpl<math::Calculator> { |
|
jamesr
2015/01/22 00:30:04
if you're rewriting these anyway might as well swi
Aaron Boodman
2015/01/22 22:27:29
It seems to work fine as InterfaceImpl. I'd rather
|
| public: |
| ~MathCalculatorImpl() override {} |
| MathCalculatorImpl() : total_(0.0) {} |
| - void Clear() override { client()->Output(total_); } |
| + void Clear(const CalcCallback& callback) override { |
| + // TODO(aa): Shouldn't it actually reset total_? |
|
jamesr
2015/01/22 00:30:04
instead of a TODO can you just do it? do the tests
Aaron Boodman
2015/01/22 22:27:29
Done.
|
| + callback.Run(total_); |
| + } |
| - void Add(double value) override { |
| + void Add(double value, const CalcCallback& callback) override { |
| total_ += value; |
| - client()->Output(total_); |
| + callback.Run(total_); |
| } |
| - void Multiply(double value) override { |
| + void Multiply(double value, const CalcCallback& callback) override { |
| total_ *= value; |
| - client()->Output(total_); |
| + callback.Run(total_); |
| } |
| private: |
| double total_; |
| }; |
| -class MathCalculatorUIImpl : public math::CalculatorUI { |
| +class MathCalculatorUI { |
| public: |
| - explicit MathCalculatorUIImpl(math::CalculatorPtr calculator) |
| - : calculator_(calculator.Pass()), output_(0.0) { |
| - calculator_.set_client(this); |
| - } |
| + explicit MathCalculatorUI(math::CalculatorPtr calculator) |
| + : calculator_(calculator.Pass()), |
| + callback_( |
| + base::Bind(&MathCalculatorUI::Output, base::Unretained(this))), |
|
jamesr
2015/01/22 00:30:04
since there's only one type of Output it should be
Aaron Boodman
2015/01/22 22:27:29
You keep using that word 'functor'. I don't think
|
| + output_(0.0) {} |
| bool WaitForIncomingMethodCall() { |
| return calculator_.WaitForIncomingMethodCall(); |
| @@ -62,46 +70,48 @@ class MathCalculatorUIImpl : public math::CalculatorUI { |
| bool encountered_error() const { return calculator_.encountered_error(); } |
| - void Add(double value) { calculator_->Add(value); } |
| + void Add(double value) { calculator_->Add(value, callback_); } |
| - void Subtract(double value) { calculator_->Add(-value); } |
| + void Subtract(double value) { calculator_->Add(-value, callback_); } |
| - void Multiply(double value) { calculator_->Multiply(value); } |
| + void Multiply(double value) { calculator_->Multiply(value, callback_); } |
| - void Divide(double value) { calculator_->Multiply(1.0 / value); } |
| + void Divide(double value) { calculator_->Multiply(1.0 / value, callback_); } |
| double GetOutput() const { return output_; } |
| private: |
| - // math::CalculatorUI implementation: |
| - void Output(double value) override { output_ = value; } |
| + void Output(double value) { output_ = value; } |
| math::CalculatorPtr calculator_; |
| + mojo::Callback<void(double)> callback_; |
| double output_; |
| }; |
| -class SelfDestructingMathCalculatorUIImpl : public math::CalculatorUI { |
| +class SelfDestructingMathCalculatorUI { |
| public: |
| - explicit SelfDestructingMathCalculatorUIImpl(math::CalculatorPtr calculator) |
| - : calculator_(calculator.Pass()), nesting_level_(0) { |
| + explicit SelfDestructingMathCalculatorUI(math::CalculatorPtr calculator) |
| + : calculator_(calculator.Pass()), |
| + callback_(base::Bind(&SelfDestructingMathCalculatorUI::Output, |
| + base::Unretained(this))), |
| + nesting_level_(0) { |
| ++num_instances_; |
| - calculator_.set_client(this); |
| } |
| void BeginTest(bool nested) { |
| nesting_level_ = nested ? 2 : 1; |
| - calculator_->Add(1.0); |
| + calculator_->Add(1.0, callback_); |
| } |
| static int num_instances() { return num_instances_; } |
| private: |
| - ~SelfDestructingMathCalculatorUIImpl() override { --num_instances_; } |
| + ~SelfDestructingMathCalculatorUI() { --num_instances_; } |
| - void Output(double value) override { |
| + void Output(double value) { |
| if (--nesting_level_ > 0) { |
| // Add some more and wait for re-entrant call to Output! |
| - calculator_->Add(1.0); |
| + calculator_->Add(1.0, callback_); |
| RunLoop::current()->RunUntilIdle(); |
| } else { |
| delete this; |
| @@ -109,12 +119,13 @@ class SelfDestructingMathCalculatorUIImpl : public math::CalculatorUI { |
| } |
| math::CalculatorPtr calculator_; |
| + mojo::Callback<void(double)> callback_; |
| int nesting_level_; |
| static int num_instances_; |
| }; |
| // static |
| -int SelfDestructingMathCalculatorUIImpl::num_instances_ = 0; |
| +int SelfDestructingMathCalculatorUI::num_instances_ = 0; |
| class ReentrantServiceImpl : public InterfaceImpl<sample::Service> { |
| public: |
| @@ -157,7 +168,7 @@ TEST_F(InterfacePtrTest, EndToEnd) { |
| BindToProxy(new MathCalculatorImpl(), &calc); |
| // Suppose this is instantiated in a process that has pipe1_. |
| - MathCalculatorUIImpl calculator_ui(calc.Pass()); |
| + MathCalculatorUI calculator_ui(calc.Pass()); |
| calculator_ui.Add(2.0); |
| calculator_ui.Multiply(5.0); |
| @@ -172,7 +183,7 @@ TEST_F(InterfacePtrTest, EndToEnd_Synchronous) { |
| MathCalculatorImpl* impl = BindToProxy(new MathCalculatorImpl(), &calc); |
| // Suppose this is instantiated in a process that has pipe1_. |
| - MathCalculatorUIImpl calculator_ui(calc.Pass()); |
| + MathCalculatorUI calculator_ui(calc.Pass()); |
| EXPECT_EQ(0.0, calculator_ui.GetOutput()); |
| @@ -230,7 +241,7 @@ TEST_F(InterfacePtrTest, EncounteredError) { |
| math::CalculatorPtr proxy; |
| MathCalculatorImpl* server = BindToProxy(new MathCalculatorImpl(), &proxy); |
| - MathCalculatorUIImpl calculator_ui(proxy.Pass()); |
| + MathCalculatorUI calculator_ui(proxy.Pass()); |
| calculator_ui.Add(2.0); |
| PumpMessages(); |
| @@ -259,7 +270,7 @@ TEST_F(InterfacePtrTest, EncounteredErrorCallback) { |
| ErrorObserver error_observer; |
| proxy.set_error_handler(&error_observer); |
| - MathCalculatorUIImpl calculator_ui(proxy.Pass()); |
| + MathCalculatorUI calculator_ui(proxy.Pass()); |
| calculator_ui.Add(2.0); |
| PumpMessages(); |
| @@ -297,30 +308,30 @@ TEST_F(InterfacePtrTest, DestroyInterfacePtrOnClientMethod) { |
| math::CalculatorPtr proxy; |
| BindToProxy(new MathCalculatorImpl(), &proxy); |
| - EXPECT_EQ(0, SelfDestructingMathCalculatorUIImpl::num_instances()); |
| + EXPECT_EQ(0, SelfDestructingMathCalculatorUI::num_instances()); |
| - SelfDestructingMathCalculatorUIImpl* impl = |
| - new SelfDestructingMathCalculatorUIImpl(proxy.Pass()); |
| + SelfDestructingMathCalculatorUI* impl = |
| + new SelfDestructingMathCalculatorUI(proxy.Pass()); |
| impl->BeginTest(false); |
| PumpMessages(); |
| - EXPECT_EQ(0, SelfDestructingMathCalculatorUIImpl::num_instances()); |
| + EXPECT_EQ(0, SelfDestructingMathCalculatorUI::num_instances()); |
| } |
| TEST_F(InterfacePtrTest, NestedDestroyInterfacePtrOnClientMethod) { |
| math::CalculatorPtr proxy; |
| BindToProxy(new MathCalculatorImpl(), &proxy); |
| - EXPECT_EQ(0, SelfDestructingMathCalculatorUIImpl::num_instances()); |
| + EXPECT_EQ(0, SelfDestructingMathCalculatorUI::num_instances()); |
| - SelfDestructingMathCalculatorUIImpl* impl = |
| - new SelfDestructingMathCalculatorUIImpl(proxy.Pass()); |
| + SelfDestructingMathCalculatorUI* impl = |
| + new SelfDestructingMathCalculatorUI(proxy.Pass()); |
| impl->BeginTest(true); |
| PumpMessages(); |
| - EXPECT_EQ(0, SelfDestructingMathCalculatorUIImpl::num_instances()); |
| + EXPECT_EQ(0, SelfDestructingMathCalculatorUI::num_instances()); |
| } |
| TEST_F(InterfacePtrTest, ReentrantWaitForIncomingMethodCall) { |
| @@ -337,6 +348,8 @@ TEST_F(InterfacePtrTest, ReentrantWaitForIncomingMethodCall) { |
| class StrongMathCalculatorImpl : public math::Calculator, public ErrorHandler { |
| public: |
| + typedef mojo::Callback<void(double)> Callback; |
|
jamesr
2015/01/22 00:30:04
I think we already have this typedef
Aaron Boodman
2015/01/22 22:27:29
Done.
|
| + |
| StrongMathCalculatorImpl(ScopedMessagePipeHandle handle, |
| bool* error_received, |
| bool* destroyed) |
| @@ -348,16 +361,16 @@ class StrongMathCalculatorImpl : public math::Calculator, public ErrorHandler { |
| ~StrongMathCalculatorImpl() override { *destroyed_ = true; } |
| // math::Calculator implementation. |
| - void Clear() override { binding_.client()->Output(total_); } |
| + void Clear(const CalcCallback& callback) override { callback.Run(total_); } |
| - void Add(double value) override { |
| + void Add(double value, const CalcCallback& callback) override { |
| total_ += value; |
| - binding_.client()->Output(total_); |
| + callback.Run(total_); |
| } |
| - void Multiply(double value) override { |
| + void Multiply(double value, const CalcCallback& callback) override { |
| total_ *= value; |
| - binding_.client()->Output(total_); |
| + callback.Run(total_); |
| } |
| // ErrorHandler implementation. |
| @@ -387,7 +400,7 @@ TEST(StrongConnectorTest, Math) { |
| { |
| // Suppose this is instantiated in a process that has the other end of the |
| // message pipe. |
| - MathCalculatorUIImpl calculator_ui(calc.Pass()); |
| + MathCalculatorUI calculator_ui(calc.Pass()); |
| calculator_ui.Add(2.0); |
| calculator_ui.Multiply(5.0); |
| @@ -419,16 +432,16 @@ class WeakMathCalculatorImpl : public math::Calculator, public ErrorHandler { |
| } |
| ~WeakMathCalculatorImpl() override { *destroyed_ = true; } |
| - void Clear() override { binding_.client()->Output(total_); } |
| + void Clear(const CalcCallback& callback) override { callback.Run(total_); } |
| - void Add(double value) override { |
| + void Add(double value, const CalcCallback& callback) override { |
| total_ += value; |
| - binding_.client()->Output(total_); |
| + callback.Run(total_); |
| } |
| - void Multiply(double value) override { |
| + void Multiply(double value, const CalcCallback& callback) override { |
| total_ *= value; |
| - binding_.client()->Output(total_); |
| + callback.Run(total_); |
| } |
| // ErrorHandler implementation. |
| @@ -457,7 +470,7 @@ TEST(WeakConnectorTest, Math) { |
| { |
| // Suppose this is instantiated in a process that has the other end of the |
| // message pipe. |
| - MathCalculatorUIImpl calculator_ui(calc.Pass()); |
| + MathCalculatorUI calculator_ui(calc.Pass()); |
| calculator_ui.Add(2.0); |
| calculator_ui.Multiply(5.0); |