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 "mojo/public/bindings/lib/remote_ptr.h" | |
6 #include "mojo/public/tests/simple_bindings_support.h" | |
7 #include "mojom/math_calculator.h" | |
8 #include "testing/gtest/include/gtest/gtest.h" | |
9 | |
10 namespace mojo { | |
11 namespace test { | |
12 | |
13 class MathCalculatorImpl : public math::Calculator { | |
14 public: | |
15 virtual ~MathCalculatorImpl() {} | |
16 | |
17 explicit MathCalculatorImpl(ScopedMessagePipeHandle pipe) | |
18 : ui_(pipe.Pass(), this), | |
19 total_(0.0) { | |
20 } | |
21 | |
22 virtual void Clear() MOJO_OVERRIDE { | |
23 ui_->Output(total_); | |
24 } | |
25 | |
26 virtual void Add(double value) MOJO_OVERRIDE { | |
27 total_ += value; | |
28 ui_->Output(total_); | |
29 } | |
30 | |
31 virtual void Multiply(double value) MOJO_OVERRIDE { | |
32 total_ *= value; | |
33 ui_->Output(total_); | |
34 } | |
35 | |
36 private: | |
37 RemotePtr<math::CalculatorUI> ui_; | |
38 double total_; | |
39 }; | |
40 | |
41 class MathCalculatorUIImpl : public math::CalculatorUI { | |
42 public: | |
43 explicit MathCalculatorUIImpl(ScopedMessagePipeHandle pipe) | |
44 : calculator_(pipe.Pass(), this), | |
45 output_(0.0) { | |
46 } | |
47 | |
48 bool encountered_error() const { | |
49 return calculator_.encountered_error(); | |
50 } | |
51 | |
52 void Add(double value) { | |
53 calculator_->Add(value); | |
54 } | |
55 | |
56 void Subtract(double value) { | |
57 calculator_->Add(-value); | |
58 } | |
59 | |
60 void Multiply(double value) { | |
61 calculator_->Multiply(value); | |
62 } | |
63 | |
64 void Divide(double value) { | |
65 calculator_->Multiply(1.0 / value); | |
66 } | |
67 | |
68 double GetOutput() const { | |
69 return output_; | |
70 } | |
71 | |
72 private: | |
73 // math::CalculatorUI implementation: | |
74 virtual void Output(double value) MOJO_OVERRIDE { | |
75 output_ = value; | |
76 } | |
77 | |
78 RemotePtr<math::Calculator> calculator_; | |
79 double output_; | |
80 }; | |
81 | |
82 class BindingsRemotePtrTest : public testing::Test { | |
83 public: | |
84 BindingsRemotePtrTest() { | |
85 CreateMessagePipe(&pipe0_, &pipe1_); | |
86 } | |
87 | |
88 virtual ~BindingsRemotePtrTest() { | |
89 } | |
90 | |
91 void PumpMessages() { | |
92 bindings_support_.Process(); | |
93 } | |
94 | |
95 protected: | |
96 ScopedMessagePipeHandle pipe0_; | |
97 ScopedMessagePipeHandle 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_.Pass()); | |
106 | |
107 // Suppose this is instantiated in a process that has pipe1_. | |
108 MathCalculatorUIImpl calculator_ui(pipe1_.Pass()); | |
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 TEST_F(BindingsRemotePtrTest, Movable) { | |
119 RemotePtr<math::Calculator> a; | |
120 RemotePtr<math::Calculator> b(pipe0_.Pass(), NULL); | |
121 | |
122 EXPECT_TRUE(a.is_null()); | |
123 EXPECT_FALSE(b.is_null()); | |
124 | |
125 a = b.Pass(); | |
126 | |
127 EXPECT_FALSE(a.is_null()); | |
128 EXPECT_TRUE(b.is_null()); | |
129 } | |
130 | |
131 TEST_F(BindingsRemotePtrTest, Resettable) { | |
132 RemotePtr<math::Calculator> a; | |
133 | |
134 EXPECT_TRUE(a.is_null()); | |
135 | |
136 MessagePipeHandle handle = pipe0_.get(); | |
137 | |
138 a.reset(pipe0_.Pass(), NULL); | |
139 | |
140 EXPECT_FALSE(a.is_null()); | |
141 | |
142 a.reset(); | |
143 | |
144 EXPECT_TRUE(a.is_null()); | |
145 | |
146 // Test that handle was closed. | |
147 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, CloseRaw(handle)); | |
148 } | |
149 | |
150 TEST_F(BindingsRemotePtrTest, EncounteredError) { | |
151 MathCalculatorImpl* calculator = new MathCalculatorImpl(pipe0_.Pass()); | |
152 | |
153 MathCalculatorUIImpl calculator_ui(pipe1_.Pass()); | |
154 | |
155 calculator_ui.Add(2.0); | |
156 PumpMessages(); | |
157 EXPECT_EQ(2.0, calculator_ui.GetOutput()); | |
158 EXPECT_FALSE(calculator_ui.encountered_error()); | |
159 | |
160 calculator_ui.Multiply(5.0); | |
161 EXPECT_FALSE(calculator_ui.encountered_error()); | |
162 | |
163 // Close the other side of the pipe. | |
164 delete calculator; | |
165 | |
166 // The state change isn't picked up locally yet. | |
167 EXPECT_FALSE(calculator_ui.encountered_error()); | |
168 | |
169 PumpMessages(); | |
170 | |
171 // OK, now we see the error. | |
172 EXPECT_TRUE(calculator_ui.encountered_error()); | |
173 } | |
174 | |
175 } // namespace test | |
176 } // namespace mojo | |
OLD | NEW |