| 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/error_handler.h" | 5 #include "mojo/public/cpp/bindings/error_handler.h" |
| 6 #include "mojo/public/cpp/environment/environment.h" | 6 #include "mojo/public/cpp/environment/environment.h" |
| 7 #include "mojo/public/cpp/utility/run_loop.h" | 7 #include "mojo/public/cpp/utility/run_loop.h" |
| 8 #include "mojo/public/interfaces/bindings/tests/math_calculator.mojom.h" | 8 #include "mojo/public/interfaces/bindings/tests/math_calculator.mojom.h" |
| 9 #include "mojo/public/interfaces/bindings/tests/sample_service.mojom.h" | 9 #include "mojo/public/interfaces/bindings/tests/sample_service.mojom.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 | 11 |
| 12 namespace mojo { | 12 namespace mojo { |
| 13 namespace test { | 13 namespace test { |
| 14 namespace { | 14 namespace { |
| 15 | 15 |
| 16 class ErrorObserver : public ErrorHandler { | 16 class ErrorObserver : public ErrorHandler { |
| 17 public: | 17 public: |
| 18 ErrorObserver() : encountered_error_(false) {} | 18 ErrorObserver() : encountered_error_(false) {} |
| 19 | 19 |
| 20 bool encountered_error() const { return encountered_error_; } | 20 bool encountered_error() const { return encountered_error_; } |
| 21 | 21 |
| 22 virtual void OnConnectionError() override { encountered_error_ = true; } | 22 void OnConnectionError() override { encountered_error_ = true; } |
| 23 | 23 |
| 24 private: | 24 private: |
| 25 bool encountered_error_; | 25 bool encountered_error_; |
| 26 }; | 26 }; |
| 27 | 27 |
| 28 class MathCalculatorImpl : public InterfaceImpl<math::Calculator> { | 28 class MathCalculatorImpl : public InterfaceImpl<math::Calculator> { |
| 29 public: | 29 public: |
| 30 virtual ~MathCalculatorImpl() {} | 30 ~MathCalculatorImpl() override {} |
| 31 | 31 |
| 32 MathCalculatorImpl() : total_(0.0), got_connection_(false) {} | 32 MathCalculatorImpl() : total_(0.0), got_connection_(false) {} |
| 33 | 33 |
| 34 virtual void OnConnectionEstablished() override { got_connection_ = true; } | 34 void OnConnectionEstablished() override { got_connection_ = true; } |
| 35 | 35 |
| 36 virtual void Clear() override { client()->Output(total_); } | 36 void Clear() override { client()->Output(total_); } |
| 37 | 37 |
| 38 virtual void Add(double value) override { | 38 void Add(double value) override { |
| 39 total_ += value; | 39 total_ += value; |
| 40 client()->Output(total_); | 40 client()->Output(total_); |
| 41 } | 41 } |
| 42 | 42 |
| 43 virtual void Multiply(double value) override { | 43 void Multiply(double value) override { |
| 44 total_ *= value; | 44 total_ *= value; |
| 45 client()->Output(total_); | 45 client()->Output(total_); |
| 46 } | 46 } |
| 47 | 47 |
| 48 bool got_connection() const { return got_connection_; } | 48 bool got_connection() const { return got_connection_; } |
| 49 | 49 |
| 50 private: | 50 private: |
| 51 double total_; | 51 double total_; |
| 52 bool got_connection_; | 52 bool got_connection_; |
| 53 }; | 53 }; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 70 void Subtract(double value) { calculator_->Add(-value); } | 70 void Subtract(double value) { calculator_->Add(-value); } |
| 71 | 71 |
| 72 void Multiply(double value) { calculator_->Multiply(value); } | 72 void Multiply(double value) { calculator_->Multiply(value); } |
| 73 | 73 |
| 74 void Divide(double value) { calculator_->Multiply(1.0 / value); } | 74 void Divide(double value) { calculator_->Multiply(1.0 / value); } |
| 75 | 75 |
| 76 double GetOutput() const { return output_; } | 76 double GetOutput() const { return output_; } |
| 77 | 77 |
| 78 private: | 78 private: |
| 79 // math::CalculatorUI implementation: | 79 // math::CalculatorUI implementation: |
| 80 virtual void Output(double value) override { output_ = value; } | 80 void Output(double value) override { output_ = value; } |
| 81 | 81 |
| 82 math::CalculatorPtr calculator_; | 82 math::CalculatorPtr calculator_; |
| 83 double output_; | 83 double output_; |
| 84 }; | 84 }; |
| 85 | 85 |
| 86 class SelfDestructingMathCalculatorUIImpl : public math::CalculatorUI { | 86 class SelfDestructingMathCalculatorUIImpl : public math::CalculatorUI { |
| 87 public: | 87 public: |
| 88 explicit SelfDestructingMathCalculatorUIImpl(math::CalculatorPtr calculator) | 88 explicit SelfDestructingMathCalculatorUIImpl(math::CalculatorPtr calculator) |
| 89 : calculator_(calculator.Pass()), nesting_level_(0) { | 89 : calculator_(calculator.Pass()), nesting_level_(0) { |
| 90 ++num_instances_; | 90 ++num_instances_; |
| 91 calculator_.set_client(this); | 91 calculator_.set_client(this); |
| 92 } | 92 } |
| 93 | 93 |
| 94 void BeginTest(bool nested) { | 94 void BeginTest(bool nested) { |
| 95 nesting_level_ = nested ? 2 : 1; | 95 nesting_level_ = nested ? 2 : 1; |
| 96 calculator_->Add(1.0); | 96 calculator_->Add(1.0); |
| 97 } | 97 } |
| 98 | 98 |
| 99 static int num_instances() { return num_instances_; } | 99 static int num_instances() { return num_instances_; } |
| 100 | 100 |
| 101 private: | 101 private: |
| 102 virtual ~SelfDestructingMathCalculatorUIImpl() { --num_instances_; } | 102 ~SelfDestructingMathCalculatorUIImpl() override { --num_instances_; } |
| 103 | 103 |
| 104 virtual void Output(double value) override { | 104 void Output(double value) override { |
| 105 if (--nesting_level_ > 0) { | 105 if (--nesting_level_ > 0) { |
| 106 // Add some more and wait for re-entrant call to Output! | 106 // Add some more and wait for re-entrant call to Output! |
| 107 calculator_->Add(1.0); | 107 calculator_->Add(1.0); |
| 108 RunLoop::current()->RunUntilIdle(); | 108 RunLoop::current()->RunUntilIdle(); |
| 109 } else { | 109 } else { |
| 110 delete this; | 110 delete this; |
| 111 } | 111 } |
| 112 } | 112 } |
| 113 | 113 |
| 114 math::CalculatorPtr calculator_; | 114 math::CalculatorPtr calculator_; |
| 115 int nesting_level_; | 115 int nesting_level_; |
| 116 static int num_instances_; | 116 static int num_instances_; |
| 117 }; | 117 }; |
| 118 | 118 |
| 119 // static | 119 // static |
| 120 int SelfDestructingMathCalculatorUIImpl::num_instances_ = 0; | 120 int SelfDestructingMathCalculatorUIImpl::num_instances_ = 0; |
| 121 | 121 |
| 122 class ReentrantServiceImpl : public InterfaceImpl<sample::Service> { | 122 class ReentrantServiceImpl : public InterfaceImpl<sample::Service> { |
| 123 public: | 123 public: |
| 124 virtual ~ReentrantServiceImpl() {} | 124 ~ReentrantServiceImpl() override {} |
| 125 | 125 |
| 126 ReentrantServiceImpl() | 126 ReentrantServiceImpl() |
| 127 : got_connection_(false), call_depth_(0), max_call_depth_(0) {} | 127 : got_connection_(false), call_depth_(0), max_call_depth_(0) {} |
| 128 | 128 |
| 129 virtual void OnConnectionEstablished() override { got_connection_ = true; } | 129 void OnConnectionEstablished() override { got_connection_ = true; } |
| 130 | 130 |
| 131 bool got_connection() const { return got_connection_; } | 131 bool got_connection() const { return got_connection_; } |
| 132 | 132 |
| 133 int max_call_depth() { return max_call_depth_; } | 133 int max_call_depth() { return max_call_depth_; } |
| 134 | 134 |
| 135 virtual void Frobinate(sample::FooPtr foo, | 135 void Frobinate(sample::FooPtr foo, |
| 136 sample::Service::BazOptions baz, | 136 sample::Service::BazOptions baz, |
| 137 sample::PortPtr port) override { | 137 sample::PortPtr port) override { |
| 138 max_call_depth_ = std::max(++call_depth_, max_call_depth_); | 138 max_call_depth_ = std::max(++call_depth_, max_call_depth_); |
| 139 if (call_depth_ == 1) { | 139 if (call_depth_ == 1) { |
| 140 EXPECT_TRUE(WaitForIncomingMethodCall()); | 140 EXPECT_TRUE(WaitForIncomingMethodCall()); |
| 141 } | 141 } |
| 142 call_depth_--; | 142 call_depth_--; |
| 143 } | 143 } |
| 144 | 144 |
| 145 virtual void GetPort(mojo::InterfaceRequest<sample::Port> port) override {} | 145 void GetPort(mojo::InterfaceRequest<sample::Port> port) override {} |
| 146 | 146 |
| 147 private: | 147 private: |
| 148 bool got_connection_; | 148 bool got_connection_; |
| 149 int call_depth_; | 149 int call_depth_; |
| 150 int max_call_depth_; | 150 int max_call_depth_; |
| 151 }; | 151 }; |
| 152 | 152 |
| 153 class InterfacePtrTest : public testing::Test { | 153 class InterfacePtrTest : public testing::Test { |
| 154 public: | 154 public: |
| 155 virtual ~InterfacePtrTest() { loop_.RunUntilIdle(); } | 155 virtual ~InterfacePtrTest() { loop_.RunUntilIdle(); } |
| (...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 347 sample::PortPtr()); | 347 sample::PortPtr()); |
| 348 | 348 |
| 349 PumpMessages(); | 349 PumpMessages(); |
| 350 | 350 |
| 351 EXPECT_EQ(2, impl->max_call_depth()); | 351 EXPECT_EQ(2, impl->max_call_depth()); |
| 352 } | 352 } |
| 353 | 353 |
| 354 } // namespace | 354 } // namespace |
| 355 } // namespace test | 355 } // namespace test |
| 356 } // namespace mojo | 356 } // namespace mojo |
| OLD | NEW |