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