| 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 | 20 |
| 21 bool encountered_error() const { return encountered_error_; } | 21 bool encountered_error() const { return encountered_error_; } |
| 22 | 22 |
| 23 virtual void OnConnectionError() MOJO_OVERRIDE { | 23 virtual void OnConnectionError() override { encountered_error_ = true; } |
| 24 encountered_error_ = true; | |
| 25 } | |
| 26 | 24 |
| 27 private: | 25 private: |
| 28 bool encountered_error_; | 26 bool encountered_error_; |
| 29 }; | 27 }; |
| 30 | 28 |
| 31 class MathCalculatorImpl : public InterfaceImpl<math::Calculator> { | 29 class MathCalculatorImpl : public InterfaceImpl<math::Calculator> { |
| 32 public: | 30 public: |
| 33 virtual ~MathCalculatorImpl() {} | 31 virtual ~MathCalculatorImpl() {} |
| 34 | 32 |
| 35 MathCalculatorImpl() | 33 MathCalculatorImpl() |
| 36 : total_(0.0), | 34 : total_(0.0), |
| 37 got_connection_(false) { | 35 got_connection_(false) { |
| 38 } | 36 } |
| 39 | 37 |
| 40 virtual void OnConnectionEstablished() MOJO_OVERRIDE { | 38 virtual void OnConnectionEstablished() override { got_connection_ = true; } |
| 41 got_connection_ = true; | |
| 42 } | |
| 43 | 39 |
| 44 virtual void Clear() MOJO_OVERRIDE { | 40 virtual void Clear() override { client()->Output(total_); } |
| 45 client()->Output(total_); | |
| 46 } | |
| 47 | 41 |
| 48 virtual void Add(double value) MOJO_OVERRIDE { | 42 virtual void Add(double value) override { |
| 49 total_ += value; | 43 total_ += value; |
| 50 client()->Output(total_); | 44 client()->Output(total_); |
| 51 } | 45 } |
| 52 | 46 |
| 53 virtual void Multiply(double value) MOJO_OVERRIDE { | 47 virtual void Multiply(double value) override { |
| 54 total_ *= value; | 48 total_ *= value; |
| 55 client()->Output(total_); | 49 client()->Output(total_); |
| 56 } | 50 } |
| 57 | 51 |
| 58 bool got_connection() const { | 52 bool got_connection() const { |
| 59 return got_connection_; | 53 return got_connection_; |
| 60 } | 54 } |
| 61 | 55 |
| 62 private: | 56 private: |
| 63 double total_; | 57 double total_; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 void Divide(double value) { | 89 void Divide(double value) { |
| 96 calculator_->Multiply(1.0 / value); | 90 calculator_->Multiply(1.0 / value); |
| 97 } | 91 } |
| 98 | 92 |
| 99 double GetOutput() const { | 93 double GetOutput() const { |
| 100 return output_; | 94 return output_; |
| 101 } | 95 } |
| 102 | 96 |
| 103 private: | 97 private: |
| 104 // math::CalculatorUI implementation: | 98 // math::CalculatorUI implementation: |
| 105 virtual void Output(double value) MOJO_OVERRIDE { | 99 virtual void Output(double value) override { output_ = value; } |
| 106 output_ = value; | |
| 107 } | |
| 108 | 100 |
| 109 math::CalculatorPtr calculator_; | 101 math::CalculatorPtr calculator_; |
| 110 double output_; | 102 double output_; |
| 111 }; | 103 }; |
| 112 | 104 |
| 113 class SelfDestructingMathCalculatorUIImpl : public math::CalculatorUI { | 105 class SelfDestructingMathCalculatorUIImpl : public math::CalculatorUI { |
| 114 public: | 106 public: |
| 115 explicit SelfDestructingMathCalculatorUIImpl(math::CalculatorPtr calculator) | 107 explicit SelfDestructingMathCalculatorUIImpl(math::CalculatorPtr calculator) |
| 116 : calculator_(calculator.Pass()), | 108 : calculator_(calculator.Pass()), |
| 117 nesting_level_(0) { | 109 nesting_level_(0) { |
| 118 ++num_instances_; | 110 ++num_instances_; |
| 119 calculator_.set_client(this); | 111 calculator_.set_client(this); |
| 120 } | 112 } |
| 121 | 113 |
| 122 void BeginTest(bool nested) { | 114 void BeginTest(bool nested) { |
| 123 nesting_level_ = nested ? 2 : 1; | 115 nesting_level_ = nested ? 2 : 1; |
| 124 calculator_->Add(1.0); | 116 calculator_->Add(1.0); |
| 125 } | 117 } |
| 126 | 118 |
| 127 static int num_instances() { return num_instances_; } | 119 static int num_instances() { return num_instances_; } |
| 128 | 120 |
| 129 private: | 121 private: |
| 130 virtual ~SelfDestructingMathCalculatorUIImpl() { | 122 virtual ~SelfDestructingMathCalculatorUIImpl() { |
| 131 --num_instances_; | 123 --num_instances_; |
| 132 } | 124 } |
| 133 | 125 |
| 134 virtual void Output(double value) MOJO_OVERRIDE { | 126 virtual void Output(double value) override { |
| 135 if (--nesting_level_ > 0) { | 127 if (--nesting_level_ > 0) { |
| 136 // Add some more and wait for re-entrant call to Output! | 128 // Add some more and wait for re-entrant call to Output! |
| 137 calculator_->Add(1.0); | 129 calculator_->Add(1.0); |
| 138 RunLoop::current()->RunUntilIdle(); | 130 RunLoop::current()->RunUntilIdle(); |
| 139 } else { | 131 } else { |
| 140 delete this; | 132 delete this; |
| 141 } | 133 } |
| 142 } | 134 } |
| 143 | 135 |
| 144 math::CalculatorPtr calculator_; | 136 math::CalculatorPtr calculator_; |
| 145 int nesting_level_; | 137 int nesting_level_; |
| 146 static int num_instances_; | 138 static int num_instances_; |
| 147 }; | 139 }; |
| 148 | 140 |
| 149 // static | 141 // static |
| 150 int SelfDestructingMathCalculatorUIImpl::num_instances_ = 0; | 142 int SelfDestructingMathCalculatorUIImpl::num_instances_ = 0; |
| 151 | 143 |
| 152 class ReentrantServiceImpl : public InterfaceImpl<sample::Service> { | 144 class ReentrantServiceImpl : public InterfaceImpl<sample::Service> { |
| 153 public: | 145 public: |
| 154 virtual ~ReentrantServiceImpl() {} | 146 virtual ~ReentrantServiceImpl() {} |
| 155 | 147 |
| 156 ReentrantServiceImpl() | 148 ReentrantServiceImpl() |
| 157 : got_connection_(false), call_depth_(0), max_call_depth_(0) {} | 149 : got_connection_(false), call_depth_(0), max_call_depth_(0) {} |
| 158 | 150 |
| 159 virtual void OnConnectionEstablished() MOJO_OVERRIDE { | 151 virtual void OnConnectionEstablished() override { got_connection_ = true; } |
| 160 got_connection_ = true; | |
| 161 } | |
| 162 | 152 |
| 163 bool got_connection() const { | 153 bool got_connection() const { |
| 164 return got_connection_; | 154 return got_connection_; |
| 165 } | 155 } |
| 166 | 156 |
| 167 int max_call_depth() { return max_call_depth_; } | 157 int max_call_depth() { return max_call_depth_; } |
| 168 | 158 |
| 169 virtual void Frobinate(sample::FooPtr foo, | 159 virtual void Frobinate(sample::FooPtr foo, |
| 170 sample::Service::BazOptions baz, | 160 sample::Service::BazOptions baz, |
| 171 sample::PortPtr port) MOJO_OVERRIDE { | 161 sample::PortPtr port) override { |
| 172 max_call_depth_ = std::max(++call_depth_, max_call_depth_); | 162 max_call_depth_ = std::max(++call_depth_, max_call_depth_); |
| 173 if (call_depth_ == 1) { | 163 if (call_depth_ == 1) { |
| 174 EXPECT_TRUE(WaitForIncomingMethodCall()); | 164 EXPECT_TRUE(WaitForIncomingMethodCall()); |
| 175 } | 165 } |
| 176 call_depth_--; | 166 call_depth_--; |
| 177 } | 167 } |
| 178 | 168 |
| 179 virtual void GetPort(mojo::InterfaceRequest<sample::Port> port) | 169 virtual void GetPort(mojo::InterfaceRequest<sample::Port> port) override {} |
| 180 MOJO_OVERRIDE {} | |
| 181 | 170 |
| 182 private: | 171 private: |
| 183 bool got_connection_; | 172 bool got_connection_; |
| 184 int call_depth_; | 173 int call_depth_; |
| 185 int max_call_depth_; | 174 int max_call_depth_; |
| 186 }; | 175 }; |
| 187 | 176 |
| 188 class InterfacePtrTest : public testing::Test { | 177 class InterfacePtrTest : public testing::Test { |
| 189 public: | 178 public: |
| 190 virtual ~InterfacePtrTest() { | 179 virtual ~InterfacePtrTest() { |
| (...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 386 sample::PortPtr()); | 375 sample::PortPtr()); |
| 387 | 376 |
| 388 PumpMessages(); | 377 PumpMessages(); |
| 389 | 378 |
| 390 EXPECT_EQ(2, impl->max_call_depth()); | 379 EXPECT_EQ(2, impl->max_call_depth()); |
| 391 } | 380 } |
| 392 | 381 |
| 393 } // namespace | 382 } // namespace |
| 394 } // namespace test | 383 } // namespace test |
| 395 } // namespace mojo | 384 } // namespace mojo |
| OLD | NEW |