| 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/lib/remote_ptr.h" | 5 #include "mojo/public/bindings/lib/remote_ptr.h" |
| 6 #include "mojo/public/tests/simple_bindings_support.h" | 6 #include "mojo/public/tests/bindings/simple_bindings_support.h" |
| 7 #include "mojom/math_calculator.h" | 7 #include "mojom/math_calculator.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 | 9 |
| 10 namespace mojo { | 10 namespace mojo { |
| 11 namespace test { | 11 namespace test { |
| 12 | 12 |
| 13 class MathCalculatorImpl : public math::Calculator { | 13 class MathCalculatorImpl : public math::Calculator { |
| 14 public: | 14 public: |
| 15 virtual ~MathCalculatorImpl() {} | 15 virtual ~MathCalculatorImpl() {} |
| 16 | 16 |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 72 private: | 72 private: |
| 73 // math::CalculatorUI implementation: | 73 // math::CalculatorUI implementation: |
| 74 virtual void Output(double value) MOJO_OVERRIDE { | 74 virtual void Output(double value) MOJO_OVERRIDE { |
| 75 output_ = value; | 75 output_ = value; |
| 76 } | 76 } |
| 77 | 77 |
| 78 RemotePtr<math::Calculator> calculator_; | 78 RemotePtr<math::Calculator> calculator_; |
| 79 double output_; | 79 double output_; |
| 80 }; | 80 }; |
| 81 | 81 |
| 82 class BindingsRemotePtrTest : public testing::Test { | 82 class RemotePtrTest : public testing::Test { |
| 83 public: | 83 public: |
| 84 BindingsRemotePtrTest() { | 84 RemotePtrTest() { |
| 85 CreateMessagePipe(&pipe0_, &pipe1_); | 85 CreateMessagePipe(&pipe0_, &pipe1_); |
| 86 } | 86 } |
| 87 | 87 |
| 88 virtual ~BindingsRemotePtrTest() { | 88 virtual ~RemotePtrTest() { |
| 89 } | 89 } |
| 90 | 90 |
| 91 void PumpMessages() { | 91 void PumpMessages() { |
| 92 bindings_support_.Process(); | 92 bindings_support_.Process(); |
| 93 } | 93 } |
| 94 | 94 |
| 95 protected: | 95 protected: |
| 96 ScopedMessagePipeHandle pipe0_; | 96 ScopedMessagePipeHandle pipe0_; |
| 97 ScopedMessagePipeHandle pipe1_; | 97 ScopedMessagePipeHandle pipe1_; |
| 98 | 98 |
| 99 private: | 99 private: |
| 100 SimpleBindingsSupport bindings_support_; | 100 SimpleBindingsSupport bindings_support_; |
| 101 }; | 101 }; |
| 102 | 102 |
| 103 TEST_F(BindingsRemotePtrTest, EndToEnd) { | 103 TEST_F(RemotePtrTest, EndToEnd) { |
| 104 // Suppose this is instantiated in a process that has pipe0_. | 104 // Suppose this is instantiated in a process that has pipe0_. |
| 105 MathCalculatorImpl calculator(pipe0_.Pass()); | 105 MathCalculatorImpl calculator(pipe0_.Pass()); |
| 106 | 106 |
| 107 // Suppose this is instantiated in a process that has pipe1_. | 107 // Suppose this is instantiated in a process that has pipe1_. |
| 108 MathCalculatorUIImpl calculator_ui(pipe1_.Pass()); | 108 MathCalculatorUIImpl calculator_ui(pipe1_.Pass()); |
| 109 | 109 |
| 110 calculator_ui.Add(2.0); | 110 calculator_ui.Add(2.0); |
| 111 calculator_ui.Multiply(5.0); | 111 calculator_ui.Multiply(5.0); |
| 112 | 112 |
| 113 PumpMessages(); | 113 PumpMessages(); |
| 114 | 114 |
| 115 EXPECT_EQ(10.0, calculator_ui.GetOutput()); | 115 EXPECT_EQ(10.0, calculator_ui.GetOutput()); |
| 116 } | 116 } |
| 117 | 117 |
| 118 TEST_F(BindingsRemotePtrTest, Movable) { | 118 TEST_F(RemotePtrTest, Movable) { |
| 119 RemotePtr<math::Calculator> a; | 119 RemotePtr<math::Calculator> a; |
| 120 RemotePtr<math::Calculator> b(pipe0_.Pass(), NULL); | 120 RemotePtr<math::Calculator> b(pipe0_.Pass(), NULL); |
| 121 | 121 |
| 122 EXPECT_TRUE(a.is_null()); | 122 EXPECT_TRUE(a.is_null()); |
| 123 EXPECT_FALSE(b.is_null()); | 123 EXPECT_FALSE(b.is_null()); |
| 124 | 124 |
| 125 a = b.Pass(); | 125 a = b.Pass(); |
| 126 | 126 |
| 127 EXPECT_FALSE(a.is_null()); | 127 EXPECT_FALSE(a.is_null()); |
| 128 EXPECT_TRUE(b.is_null()); | 128 EXPECT_TRUE(b.is_null()); |
| 129 } | 129 } |
| 130 | 130 |
| 131 TEST_F(BindingsRemotePtrTest, Resettable) { | 131 TEST_F(RemotePtrTest, Resettable) { |
| 132 RemotePtr<math::Calculator> a; | 132 RemotePtr<math::Calculator> a; |
| 133 | 133 |
| 134 EXPECT_TRUE(a.is_null()); | 134 EXPECT_TRUE(a.is_null()); |
| 135 | 135 |
| 136 MessagePipeHandle handle = pipe0_.get(); | 136 MessagePipeHandle handle = pipe0_.get(); |
| 137 | 137 |
| 138 a.reset(pipe0_.Pass(), NULL); | 138 a.reset(pipe0_.Pass(), NULL); |
| 139 | 139 |
| 140 EXPECT_FALSE(a.is_null()); | 140 EXPECT_FALSE(a.is_null()); |
| 141 | 141 |
| 142 a.reset(); | 142 a.reset(); |
| 143 | 143 |
| 144 EXPECT_TRUE(a.is_null()); | 144 EXPECT_TRUE(a.is_null()); |
| 145 | 145 |
| 146 // Test that handle was closed. | 146 // Test that handle was closed. |
| 147 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, CloseRaw(handle)); | 147 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, CloseRaw(handle)); |
| 148 } | 148 } |
| 149 | 149 |
| 150 TEST_F(BindingsRemotePtrTest, EncounteredError) { | 150 TEST_F(RemotePtrTest, EncounteredError) { |
| 151 MathCalculatorImpl* calculator = new MathCalculatorImpl(pipe0_.Pass()); | 151 MathCalculatorImpl* calculator = new MathCalculatorImpl(pipe0_.Pass()); |
| 152 | 152 |
| 153 MathCalculatorUIImpl calculator_ui(pipe1_.Pass()); | 153 MathCalculatorUIImpl calculator_ui(pipe1_.Pass()); |
| 154 | 154 |
| 155 calculator_ui.Add(2.0); | 155 calculator_ui.Add(2.0); |
| 156 PumpMessages(); | 156 PumpMessages(); |
| 157 EXPECT_EQ(2.0, calculator_ui.GetOutput()); | 157 EXPECT_EQ(2.0, calculator_ui.GetOutput()); |
| 158 EXPECT_FALSE(calculator_ui.encountered_error()); | 158 EXPECT_FALSE(calculator_ui.encountered_error()); |
| 159 | 159 |
| 160 calculator_ui.Multiply(5.0); | 160 calculator_ui.Multiply(5.0); |
| 161 EXPECT_FALSE(calculator_ui.encountered_error()); | 161 EXPECT_FALSE(calculator_ui.encountered_error()); |
| 162 | 162 |
| 163 // Close the other side of the pipe. | 163 // Close the other side of the pipe. |
| 164 delete calculator; | 164 delete calculator; |
| 165 | 165 |
| 166 // The state change isn't picked up locally yet. | 166 // The state change isn't picked up locally yet. |
| 167 EXPECT_FALSE(calculator_ui.encountered_error()); | 167 EXPECT_FALSE(calculator_ui.encountered_error()); |
| 168 | 168 |
| 169 PumpMessages(); | 169 PumpMessages(); |
| 170 | 170 |
| 171 // OK, now we see the error. | 171 // OK, now we see the error. |
| 172 EXPECT_TRUE(calculator_ui.encountered_error()); | 172 EXPECT_TRUE(calculator_ui.encountered_error()); |
| 173 } | 173 } |
| 174 | 174 |
| 175 } // namespace test | 175 } // namespace test |
| 176 } // namespace mojo | 176 } // namespace mojo |
| OLD | NEW |