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.
5 #include "mojo/public/bindings/lib/remote_ptr.h"
6 #include "mojo/public/environment/environment.h"
7 #include "mojo/public/utility/run_loop.h"
8 #include "mojom/math_calculator.h"
9 #include "testing/gtest/include/gtest/gtest.h"
14 class MathCalculatorImpl : public math::Calculator {
16 virtual ~MathCalculatorImpl() {}
18 explicit MathCalculatorImpl(ScopedMessagePipeHandle pipe)
19 : ui_(pipe.Pass(), this),
23 virtual void Clear() MOJO_OVERRIDE {
27 virtual void Add(double value) MOJO_OVERRIDE {
32 virtual void Multiply(double value) MOJO_OVERRIDE {
38 RemotePtr<math::CalculatorUI> ui_;
42 class MathCalculatorUIImpl : public math::CalculatorUI {
44 explicit MathCalculatorUIImpl(ScopedMessagePipeHandle pipe)
45 : calculator_(pipe.Pass(), this),
49 bool encountered_error() const {
50 return calculator_.encountered_error();
53 void Add(double value) {
54 calculator_->Add(value);
57 void Subtract(double value) {
58 calculator_->Add(-value);
61 void Multiply(double value) {
62 calculator_->Multiply(value);
65 void Divide(double value) {
66 calculator_->Multiply(1.0 / value);
69 double GetOutput() const {
74 // math::CalculatorUI implementation:
75 virtual void Output(double value) MOJO_OVERRIDE {
79 RemotePtr<math::Calculator> calculator_;
83 class RemotePtrTest : public testing::Test {
86 CreateMessagePipe(&pipe0_, &pipe1_);
89 virtual ~RemotePtrTest() {
97 ScopedMessagePipeHandle pipe0_;
98 ScopedMessagePipeHandle pipe1_;
105 TEST_F(RemotePtrTest, EndToEnd) {
106 // Suppose this is instantiated in a process that has pipe0_.
107 MathCalculatorImpl calculator(pipe0_.Pass());
109 // Suppose this is instantiated in a process that has pipe1_.
110 MathCalculatorUIImpl calculator_ui(pipe1_.Pass());
112 calculator_ui.Add(2.0);
113 calculator_ui.Multiply(5.0);
117 EXPECT_EQ(10.0, calculator_ui.GetOutput());
120 TEST_F(RemotePtrTest, Movable) {
121 RemotePtr<math::Calculator> a;
122 RemotePtr<math::Calculator> b(pipe0_.Pass(), NULL);
124 EXPECT_TRUE(a.is_null());
125 EXPECT_FALSE(b.is_null());
129 EXPECT_FALSE(a.is_null());
130 EXPECT_TRUE(b.is_null());
133 TEST_F(RemotePtrTest, Resettable) {
134 RemotePtr<math::Calculator> a;
136 EXPECT_TRUE(a.is_null());
138 MessagePipeHandle handle = pipe0_.get();
140 a.reset(pipe0_.Pass(), NULL);
142 EXPECT_FALSE(a.is_null());
146 EXPECT_TRUE(a.is_null());
148 // Test that handle was closed.
149 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, CloseRaw(handle));
152 TEST_F(RemotePtrTest, EncounteredError) {
153 MathCalculatorImpl* calculator = new MathCalculatorImpl(pipe0_.Pass());
155 MathCalculatorUIImpl calculator_ui(pipe1_.Pass());
157 calculator_ui.Add(2.0);
159 EXPECT_EQ(2.0, calculator_ui.GetOutput());
160 EXPECT_FALSE(calculator_ui.encountered_error());
162 calculator_ui.Multiply(5.0);
163 EXPECT_FALSE(calculator_ui.encountered_error());
165 // Close the other side of the pipe.
168 // The state change isn't picked up locally yet.
169 EXPECT_FALSE(calculator_ui.encountered_error());
173 // OK, now we see the error.
174 EXPECT_TRUE(calculator_ui.encountered_error());