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/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::CalculatorStub { | 13 class MathCalculatorImpl : public math::Calculator { |
14 public: | 14 public: |
15 virtual ~MathCalculatorImpl() {} | 15 virtual ~MathCalculatorImpl() {} |
16 | 16 |
17 explicit MathCalculatorImpl(ScopedMessagePipeHandle pipe) | 17 explicit MathCalculatorImpl(ScopedMessagePipeHandle pipe) |
18 : ui_(pipe.Pass()), | 18 : ui_(pipe.Pass(), this), |
19 total_(0.0) { | 19 total_(0.0) { |
20 ui_.SetPeer(this); | |
21 } | 20 } |
22 | 21 |
23 virtual void Clear() MOJO_OVERRIDE { | 22 virtual void Clear() MOJO_OVERRIDE { |
24 ui_->Output(total_); | 23 ui_->Output(total_); |
25 } | 24 } |
26 | 25 |
27 virtual void Add(double value) MOJO_OVERRIDE { | 26 virtual void Add(double value) MOJO_OVERRIDE { |
28 total_ += value; | 27 total_ += value; |
29 ui_->Output(total_); | 28 ui_->Output(total_); |
30 } | 29 } |
31 | 30 |
32 virtual void Multiply(double value) MOJO_OVERRIDE { | 31 virtual void Multiply(double value) MOJO_OVERRIDE { |
33 total_ *= value; | 32 total_ *= value; |
34 ui_->Output(total_); | 33 ui_->Output(total_); |
35 } | 34 } |
36 | 35 |
37 private: | 36 private: |
38 RemotePtr<math::CalculatorUI> ui_; | 37 RemotePtr<math::CalculatorUI> ui_; |
39 double total_; | 38 double total_; |
40 }; | 39 }; |
41 | 40 |
42 class MathCalculatorUIImpl : public math::CalculatorUIStub { | 41 class MathCalculatorUIImpl : public math::CalculatorUI { |
43 public: | 42 public: |
44 explicit MathCalculatorUIImpl(ScopedMessagePipeHandle pipe) | 43 explicit MathCalculatorUIImpl(ScopedMessagePipeHandle pipe) |
45 : calculator_(pipe.Pass()), | 44 : calculator_(pipe.Pass(), this), |
46 output_(0.0) { | 45 output_(0.0) { |
47 calculator_.SetPeer(this); | |
48 } | 46 } |
49 | 47 |
50 bool encountered_error() const { | 48 bool encountered_error() const { |
51 return calculator_.encountered_error(); | 49 return calculator_.encountered_error(); |
52 } | 50 } |
53 | 51 |
54 void Add(double value) { | 52 void Add(double value) { |
55 calculator_->Add(value); | 53 calculator_->Add(value); |
56 } | 54 } |
57 | 55 |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 calculator_ui.Add(2.0); | 110 calculator_ui.Add(2.0); |
113 calculator_ui.Multiply(5.0); | 111 calculator_ui.Multiply(5.0); |
114 | 112 |
115 PumpMessages(); | 113 PumpMessages(); |
116 | 114 |
117 EXPECT_EQ(10.0, calculator_ui.GetOutput()); | 115 EXPECT_EQ(10.0, calculator_ui.GetOutput()); |
118 } | 116 } |
119 | 117 |
120 TEST_F(BindingsRemotePtrTest, Movable) { | 118 TEST_F(BindingsRemotePtrTest, Movable) { |
121 RemotePtr<math::Calculator> a; | 119 RemotePtr<math::Calculator> a; |
122 RemotePtr<math::Calculator> b(pipe0_.Pass()); | 120 RemotePtr<math::Calculator> b(pipe0_.Pass(), NULL); |
123 | 121 |
124 EXPECT_TRUE(a.is_null()); | 122 EXPECT_TRUE(a.is_null()); |
125 EXPECT_FALSE(b.is_null()); | 123 EXPECT_FALSE(b.is_null()); |
126 | 124 |
127 a = b.Pass(); | 125 a = b.Pass(); |
128 | 126 |
129 EXPECT_FALSE(a.is_null()); | 127 EXPECT_FALSE(a.is_null()); |
130 EXPECT_TRUE(b.is_null()); | 128 EXPECT_TRUE(b.is_null()); |
131 } | 129 } |
132 | 130 |
133 TEST_F(BindingsRemotePtrTest, Resettable) { | 131 TEST_F(BindingsRemotePtrTest, Resettable) { |
134 RemotePtr<math::Calculator> a; | 132 RemotePtr<math::Calculator> a; |
135 | 133 |
136 EXPECT_TRUE(a.is_null()); | 134 EXPECT_TRUE(a.is_null()); |
137 | 135 |
138 MessagePipeHandle handle = pipe0_.get(); | 136 MessagePipeHandle handle = pipe0_.get(); |
139 | 137 |
140 a.reset(pipe0_.Pass()); | 138 a.reset(pipe0_.Pass(), NULL); |
141 | 139 |
142 EXPECT_FALSE(a.is_null()); | 140 EXPECT_FALSE(a.is_null()); |
143 | 141 |
144 a.reset(); | 142 a.reset(); |
145 | 143 |
146 EXPECT_TRUE(a.is_null()); | 144 EXPECT_TRUE(a.is_null()); |
147 | 145 |
148 // Test that handle was closed. | 146 // Test that handle was closed. |
149 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, CloseRaw(handle)); | 147 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, CloseRaw(handle)); |
150 } | 148 } |
(...skipping 18 matching lines...) Expand all Loading... |
169 EXPECT_FALSE(calculator_ui.encountered_error()); | 167 EXPECT_FALSE(calculator_ui.encountered_error()); |
170 | 168 |
171 PumpMessages(); | 169 PumpMessages(); |
172 | 170 |
173 // OK, now we see the error. | 171 // OK, now we see the error. |
174 EXPECT_TRUE(calculator_ui.encountered_error()); | 172 EXPECT_TRUE(calculator_ui.encountered_error()); |
175 } | 173 } |
176 | 174 |
177 } // namespace test | 175 } // namespace test |
178 } // namespace mojo | 176 } // namespace mojo |
OLD | NEW |