Upstream version 5.34.98.0
[platform/framework/web/crosswalk.git] / src / mojo / public / tests / bindings / remote_ptr_unittest.cc
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/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"
10
11 namespace mojo {
12 namespace test {
13
14 class MathCalculatorImpl : public math::Calculator {
15  public:
16   virtual ~MathCalculatorImpl() {}
17
18   explicit MathCalculatorImpl(ScopedMessagePipeHandle pipe)
19       : ui_(pipe.Pass(), this),
20         total_(0.0) {
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::CalculatorUI {
43  public:
44   explicit MathCalculatorUIImpl(ScopedMessagePipeHandle pipe)
45       : calculator_(pipe.Pass(), this),
46         output_(0.0) {
47   }
48
49   bool encountered_error() const {
50     return calculator_.encountered_error();
51   }
52
53   void Add(double value) {
54     calculator_->Add(value);
55   }
56
57   void Subtract(double value) {
58     calculator_->Add(-value);
59   }
60
61   void Multiply(double value) {
62     calculator_->Multiply(value);
63   }
64
65   void Divide(double value) {
66     calculator_->Multiply(1.0 / value);
67   }
68
69   double GetOutput() const {
70     return output_;
71   }
72
73  private:
74   // math::CalculatorUI implementation:
75   virtual void Output(double value) MOJO_OVERRIDE {
76     output_ = value;
77   }
78
79   RemotePtr<math::Calculator> calculator_;
80   double output_;
81 };
82
83 class RemotePtrTest : public testing::Test {
84  public:
85   RemotePtrTest() {
86     CreateMessagePipe(&pipe0_, &pipe1_);
87   }
88
89   virtual ~RemotePtrTest() {
90   }
91
92   void PumpMessages() {
93     loop_.RunUntilIdle();
94   }
95
96  protected:
97   ScopedMessagePipeHandle pipe0_;
98   ScopedMessagePipeHandle pipe1_;
99
100  private:
101   Environment env_;
102   RunLoop loop_;
103 };
104
105 TEST_F(RemotePtrTest, EndToEnd) {
106   // Suppose this is instantiated in a process that has pipe0_.
107   MathCalculatorImpl calculator(pipe0_.Pass());
108
109   // Suppose this is instantiated in a process that has pipe1_.
110   MathCalculatorUIImpl calculator_ui(pipe1_.Pass());
111
112   calculator_ui.Add(2.0);
113   calculator_ui.Multiply(5.0);
114
115   PumpMessages();
116
117   EXPECT_EQ(10.0, calculator_ui.GetOutput());
118 }
119
120 TEST_F(RemotePtrTest, Movable) {
121   RemotePtr<math::Calculator> a;
122   RemotePtr<math::Calculator> b(pipe0_.Pass(), NULL);
123
124   EXPECT_TRUE(a.is_null());
125   EXPECT_FALSE(b.is_null());
126
127   a = b.Pass();
128
129   EXPECT_FALSE(a.is_null());
130   EXPECT_TRUE(b.is_null());
131 }
132
133 TEST_F(RemotePtrTest, Resettable) {
134   RemotePtr<math::Calculator> a;
135
136   EXPECT_TRUE(a.is_null());
137
138   MessagePipeHandle handle = pipe0_.get();
139
140   a.reset(pipe0_.Pass(), NULL);
141
142   EXPECT_FALSE(a.is_null());
143
144   a.reset();
145
146   EXPECT_TRUE(a.is_null());
147
148   // Test that handle was closed.
149   EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, CloseRaw(handle));
150 }
151
152 TEST_F(RemotePtrTest, EncounteredError) {
153   MathCalculatorImpl* calculator = new MathCalculatorImpl(pipe0_.Pass());
154
155   MathCalculatorUIImpl calculator_ui(pipe1_.Pass());
156
157   calculator_ui.Add(2.0);
158   PumpMessages();
159   EXPECT_EQ(2.0, calculator_ui.GetOutput());
160   EXPECT_FALSE(calculator_ui.encountered_error());
161
162   calculator_ui.Multiply(5.0);
163   EXPECT_FALSE(calculator_ui.encountered_error());
164
165   // Close the other side of the pipe.
166   delete calculator;
167
168   // The state change isn't picked up locally yet.
169   EXPECT_FALSE(calculator_ui.encountered_error());
170
171   PumpMessages();
172
173   // OK, now we see the error.
174   EXPECT_TRUE(calculator_ui.encountered_error());
175 }
176
177 }  // namespace test
178 }  // namespace mojo