Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / mojo / public / cpp / bindings / tests / request_response_unittest.cc
1 // Copyright 2014 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/cpp/bindings/allocation_scope.h"
6 #include "mojo/public/cpp/environment/environment.h"
7 #include "mojo/public/cpp/test_support/test_utils.h"
8 #include "mojo/public/cpp/utility/run_loop.h"
9 #include "mojo/public/interfaces/bindings/tests/sample_import.mojom.h"
10 #include "mojo/public/interfaces/bindings/tests/sample_interfaces.mojom.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12
13 namespace mojo {
14 namespace test {
15 namespace {
16
17 class ProviderImpl : public InterfaceImpl<sample::Provider> {
18  public:
19   virtual void OnConnectionError() MOJO_OVERRIDE {
20     delete this;
21   }
22
23   virtual void SetClient(sample::ProviderClient* client) MOJO_OVERRIDE {
24     // Ignored. TODO(darin): Eliminate ProviderClient.
25   }
26
27   virtual void EchoString(
28       const String& a,
29       const Callback<void(String)>& callback) MOJO_OVERRIDE {
30     AllocationScope scope;
31     Callback<void(String)> callback_copy;
32     // Make sure operator= is used.
33     callback_copy = callback;
34     callback_copy.Run(a);
35   }
36
37   virtual void EchoStrings(
38       const String& a,
39       const String& b,
40       const Callback<void(String, String)>& callback) MOJO_OVERRIDE {
41     AllocationScope scope;
42     callback.Run(a, b);
43   }
44
45   virtual void EchoMessagePipeHandle(
46       ScopedMessagePipeHandle a,
47       const Callback<void(ScopedMessagePipeHandle)>& callback) MOJO_OVERRIDE {
48     AllocationScope scope;
49     callback.Run(a.Pass());
50   }
51
52   virtual void EchoEnum(sample::Enum a,
53                         const Callback<void(sample::Enum)>& callback)
54       MOJO_OVERRIDE {
55     callback.Run(a);
56   }
57 };
58
59 class StringRecorder {
60  public:
61   StringRecorder(std::string* buf) : buf_(buf) {
62   }
63   void Run(const String& a) const {
64     *buf_ = a.To<std::string>();
65   }
66   void Run(const String& a, const String& b) const {
67     *buf_ = a.To<std::string>() + b.To<std::string>();
68   }
69  private:
70   std::string* buf_;
71 };
72
73 class EnumRecorder {
74  public:
75   EnumRecorder(sample::Enum* value) : value_(value) {
76   }
77   void Run(sample::Enum a) const {
78     *value_ = a;
79   }
80  private:
81   sample::Enum* value_;
82 };
83
84 class MessagePipeWriter {
85  public:
86   explicit MessagePipeWriter(const char* text) : text_(text) {
87   }
88   void Run(ScopedMessagePipeHandle handle) const {
89     WriteTextMessage(handle.get(), text_);
90   }
91  private:
92   std::string text_;
93 };
94
95 class RequestResponseTest : public testing::Test {
96  public:
97   virtual ~RequestResponseTest() {
98     loop_.RunUntilIdle();
99   }
100
101   void PumpMessages() {
102     loop_.RunUntilIdle();
103   }
104
105  private:
106   Environment env_;
107   RunLoop loop_;
108 };
109
110 TEST_F(RequestResponseTest, EchoString) {
111   sample::ProviderPtr provider;
112   BindToProxy(new ProviderImpl(), &provider);
113
114   std::string buf;
115   {
116     AllocationScope scope;
117     provider->EchoString("hello", StringRecorder(&buf));
118   }
119
120   PumpMessages();
121
122   EXPECT_EQ(std::string("hello"), buf);
123 }
124
125 TEST_F(RequestResponseTest, EchoStrings) {
126   sample::ProviderPtr provider;
127   BindToProxy(new ProviderImpl(), &provider);
128
129   std::string buf;
130   {
131     AllocationScope scope;
132     provider->EchoStrings("hello", " world", StringRecorder(&buf));
133   }
134
135   PumpMessages();
136
137   EXPECT_EQ(std::string("hello world"), buf);
138 }
139
140 TEST_F(RequestResponseTest, EchoMessagePipeHandle) {
141   sample::ProviderPtr provider;
142   BindToProxy(new ProviderImpl(), &provider);
143
144   MessagePipe pipe2;
145   {
146     AllocationScope scope;
147     provider->EchoMessagePipeHandle(pipe2.handle1.Pass(),
148                                     MessagePipeWriter("hello"));
149   }
150
151   PumpMessages();
152
153   std::string value;
154   ReadTextMessage(pipe2.handle0.get(), &value);
155
156   EXPECT_EQ(std::string("hello"), value);
157 }
158
159 TEST_F(RequestResponseTest, EchoEnum) {
160   sample::ProviderPtr provider;
161   BindToProxy(new ProviderImpl(), &provider);
162
163   sample::Enum value;
164   {
165     AllocationScope scope;
166     provider->EchoEnum(sample::ENUM_VALUE, EnumRecorder(&value));
167   }
168
169   PumpMessages();
170
171   EXPECT_EQ(sample::ENUM_VALUE, value);
172 }
173
174 }  // namespace
175 }  // namespace test
176 }  // namespace mojo