OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <stdio.h> |
| 6 |
| 7 #include "mojo/public/bindings/lib/remote_ptr.h" |
| 8 #include "mojo/public/tests/generated/math_calculator.h" |
| 9 #include "mojo/public/tests/simple_bindings_support.h" |
| 10 #include "mojo/public/tests/test_support.h" |
| 11 |
| 12 namespace mojo { |
| 13 namespace test { |
| 14 |
| 15 class MathCalculatorImpl : public math::CalculatorStub { |
| 16 public: |
| 17 explicit MathCalculatorImpl(Handle pipe) |
| 18 : ui_(pipe), |
| 19 total_(0.0) { |
| 20 ui_.SetPeer(this); |
| 21 } |
| 22 |
| 23 virtual void Clear() MOJO_OVERRIDE { |
| 24 ui_->Output(total_); |
| 25 } |
| 26 |
| 27 virtual void Add(double value) MOJO_OVERRIDE { |
| 28 total_ += value; |
| 29 ui_->Output(total_); |
| 30 } |
| 31 |
| 32 virtual void Multiply(double value) MOJO_OVERRIDE { |
| 33 total_ *= value; |
| 34 ui_->Output(total_); |
| 35 } |
| 36 |
| 37 private: |
| 38 RemotePtr<math::CalculatorUI> ui_; |
| 39 double total_; |
| 40 }; |
| 41 |
| 42 class MathCalculatorUIImpl : public math::CalculatorUIStub { |
| 43 public: |
| 44 explicit MathCalculatorUIImpl(Handle pipe) |
| 45 : calculator_(pipe), |
| 46 output_(0.0) { |
| 47 calculator_.SetPeer(this); |
| 48 } |
| 49 |
| 50 void Add(double value) { |
| 51 calculator_->Add(value); |
| 52 } |
| 53 |
| 54 void Subtract(double value) { |
| 55 calculator_->Add(-value); |
| 56 } |
| 57 |
| 58 void Multiply(double value) { |
| 59 calculator_->Multiply(value); |
| 60 } |
| 61 |
| 62 void Divide(double value) { |
| 63 calculator_->Multiply(1.0 / value); |
| 64 } |
| 65 |
| 66 double GetOutput() const { |
| 67 return output_; |
| 68 } |
| 69 |
| 70 private: |
| 71 // math::CalculatorUI implementation: |
| 72 virtual void Output(double value) MOJO_OVERRIDE { |
| 73 output_ = value; |
| 74 } |
| 75 |
| 76 RemotePtr<math::Calculator> calculator_; |
| 77 double output_; |
| 78 }; |
| 79 |
| 80 class BindingsRemotePtrTest : public TestBase { |
| 81 public: |
| 82 BindingsRemotePtrTest() { |
| 83 CreateMessagePipe(&pipe0_, &pipe1_); |
| 84 } |
| 85 |
| 86 virtual ~BindingsRemotePtrTest() { |
| 87 Close(pipe0_); |
| 88 Close(pipe1_); |
| 89 } |
| 90 |
| 91 void PumpMessages() { |
| 92 bindings_support_.Process(); |
| 93 } |
| 94 |
| 95 protected: |
| 96 Handle pipe0_; |
| 97 Handle pipe1_; |
| 98 |
| 99 private: |
| 100 SimpleBindingsSupport bindings_support_; |
| 101 }; |
| 102 |
| 103 TEST_F(BindingsRemotePtrTest, EndToEnd) { |
| 104 // Suppose this is instantiated in a process that has pipe0_. |
| 105 MathCalculatorImpl calculator(pipe0_); |
| 106 |
| 107 // Suppose this is instantiated in a process that has pipe1_. |
| 108 MathCalculatorUIImpl calculator_ui(pipe1_); |
| 109 |
| 110 calculator_ui.Add(2.0); |
| 111 calculator_ui.Multiply(5.0); |
| 112 |
| 113 PumpMessages(); |
| 114 |
| 115 EXPECT_EQ(10.0, calculator_ui.GetOutput()); |
| 116 } |
| 117 |
| 118 } // namespace test |
| 119 } // namespace mojo |
OLD | NEW |