| 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/bindings/error_handler.h" |
| 5 #include "mojo/public/bindings/remote_ptr.h" | 6 #include "mojo/public/bindings/remote_ptr.h" |
| 6 #include "mojo/public/environment/environment.h" | 7 #include "mojo/public/environment/environment.h" |
| 7 #include "mojo/public/utility/run_loop.h" | 8 #include "mojo/public/utility/run_loop.h" |
| 8 #include "mojom/math_calculator.h" | 9 #include "mojom/math_calculator.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 10 | 11 |
| 11 namespace mojo { | 12 namespace mojo { |
| 12 namespace test { | 13 namespace test { |
| 14 namespace { |
| 15 |
| 16 class ErrorObserver : public ErrorHandler { |
| 17 public: |
| 18 ErrorObserver() : encountered_error_(false) { |
| 19 } |
| 20 |
| 21 bool encountered_error() const { return encountered_error_; } |
| 22 |
| 23 virtual void OnError() MOJO_OVERRIDE { |
| 24 encountered_error_ = true; |
| 25 } |
| 26 |
| 27 private: |
| 28 bool encountered_error_; |
| 29 }; |
| 13 | 30 |
| 14 class MathCalculatorImpl : public math::Calculator { | 31 class MathCalculatorImpl : public math::Calculator { |
| 15 public: | 32 public: |
| 16 virtual ~MathCalculatorImpl() {} | 33 virtual ~MathCalculatorImpl() {} |
| 17 | 34 |
| 18 explicit MathCalculatorImpl(ScopedMessagePipeHandle pipe) | 35 explicit MathCalculatorImpl(ScopedMessagePipeHandle pipe) |
| 19 : ui_(pipe.Pass(), this), | 36 : ui_(pipe.Pass(), this), |
| 20 total_(0.0) { | 37 total_(0.0) { |
| 21 } | 38 } |
| 22 | 39 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 34 ui_->Output(total_); | 51 ui_->Output(total_); |
| 35 } | 52 } |
| 36 | 53 |
| 37 private: | 54 private: |
| 38 RemotePtr<math::CalculatorUI> ui_; | 55 RemotePtr<math::CalculatorUI> ui_; |
| 39 double total_; | 56 double total_; |
| 40 }; | 57 }; |
| 41 | 58 |
| 42 class MathCalculatorUIImpl : public math::CalculatorUI { | 59 class MathCalculatorUIImpl : public math::CalculatorUI { |
| 43 public: | 60 public: |
| 44 explicit MathCalculatorUIImpl(ScopedMessagePipeHandle pipe) | 61 explicit MathCalculatorUIImpl(ScopedMessagePipeHandle pipe, |
| 45 : calculator_(pipe.Pass(), this), | 62 ErrorHandler* error_handler = NULL) |
| 63 : calculator_(pipe.Pass(), this, error_handler), |
| 46 output_(0.0) { | 64 output_(0.0) { |
| 47 } | 65 } |
| 48 | 66 |
| 49 bool encountered_error() const { | 67 bool encountered_error() const { |
| 50 return calculator_.encountered_error(); | 68 return calculator_.encountered_error(); |
| 51 } | 69 } |
| 52 | 70 |
| 53 void Add(double value) { | 71 void Add(double value) { |
| 54 calculator_->Add(value); | 72 calculator_->Add(value); |
| 55 } | 73 } |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 | 113 |
| 96 protected: | 114 protected: |
| 97 ScopedMessagePipeHandle pipe0_; | 115 ScopedMessagePipeHandle pipe0_; |
| 98 ScopedMessagePipeHandle pipe1_; | 116 ScopedMessagePipeHandle pipe1_; |
| 99 | 117 |
| 100 private: | 118 private: |
| 101 Environment env_; | 119 Environment env_; |
| 102 RunLoop loop_; | 120 RunLoop loop_; |
| 103 }; | 121 }; |
| 104 | 122 |
| 123 } // namespace |
| 124 |
| 105 TEST_F(RemotePtrTest, EndToEnd) { | 125 TEST_F(RemotePtrTest, EndToEnd) { |
| 106 // Suppose this is instantiated in a process that has pipe0_. | 126 // Suppose this is instantiated in a process that has pipe0_. |
| 107 MathCalculatorImpl calculator(pipe0_.Pass()); | 127 MathCalculatorImpl calculator(pipe0_.Pass()); |
| 108 | 128 |
| 109 // Suppose this is instantiated in a process that has pipe1_. | 129 // Suppose this is instantiated in a process that has pipe1_. |
| 110 MathCalculatorUIImpl calculator_ui(pipe1_.Pass()); | 130 MathCalculatorUIImpl calculator_ui(pipe1_.Pass()); |
| 111 | 131 |
| 112 calculator_ui.Add(2.0); | 132 calculator_ui.Add(2.0); |
| 113 calculator_ui.Multiply(5.0); | 133 calculator_ui.Multiply(5.0); |
| 114 | 134 |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 167 | 187 |
| 168 // The state change isn't picked up locally yet. | 188 // The state change isn't picked up locally yet. |
| 169 EXPECT_FALSE(calculator_ui.encountered_error()); | 189 EXPECT_FALSE(calculator_ui.encountered_error()); |
| 170 | 190 |
| 171 PumpMessages(); | 191 PumpMessages(); |
| 172 | 192 |
| 173 // OK, now we see the error. | 193 // OK, now we see the error. |
| 174 EXPECT_TRUE(calculator_ui.encountered_error()); | 194 EXPECT_TRUE(calculator_ui.encountered_error()); |
| 175 } | 195 } |
| 176 | 196 |
| 197 TEST_F(RemotePtrTest, EncounteredErrorCallback) { |
| 198 MathCalculatorImpl* calculator = new MathCalculatorImpl(pipe0_.Pass()); |
| 199 |
| 200 ErrorObserver error_observer; |
| 201 MathCalculatorUIImpl calculator_ui(pipe1_.Pass(), &error_observer); |
| 202 |
| 203 calculator_ui.Add(2.0); |
| 204 PumpMessages(); |
| 205 EXPECT_EQ(2.0, calculator_ui.GetOutput()); |
| 206 EXPECT_FALSE(calculator_ui.encountered_error()); |
| 207 |
| 208 calculator_ui.Multiply(5.0); |
| 209 EXPECT_FALSE(calculator_ui.encountered_error()); |
| 210 |
| 211 // Close the other side of the pipe. |
| 212 delete calculator; |
| 213 |
| 214 // The state change isn't picked up locally yet. |
| 215 EXPECT_FALSE(calculator_ui.encountered_error()); |
| 216 |
| 217 PumpMessages(); |
| 218 |
| 219 // OK, now we see the error. |
| 220 EXPECT_TRUE(calculator_ui.encountered_error()); |
| 221 |
| 222 // We should have also been able to observe the error through the |
| 223 // ErrorHandler interface. |
| 224 EXPECT_TRUE(error_observer.encountered_error()); |
| 225 } |
| 226 |
| 177 } // namespace test | 227 } // namespace test |
| 178 } // namespace mojo | 228 } // namespace mojo |
| OLD | NEW |