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.
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"
17 class ProviderImpl : public InterfaceImpl<sample::Provider> {
19 virtual void OnConnectionError() MOJO_OVERRIDE {
23 virtual void SetClient(sample::ProviderClient* client) MOJO_OVERRIDE {
24 // Ignored. TODO(darin): Eliminate ProviderClient.
27 virtual void EchoString(
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;
37 virtual void EchoStrings(
40 const Callback<void(String, String)>& callback) MOJO_OVERRIDE {
41 AllocationScope scope;
45 virtual void EchoMessagePipeHandle(
46 ScopedMessagePipeHandle a,
47 const Callback<void(ScopedMessagePipeHandle)>& callback) MOJO_OVERRIDE {
48 AllocationScope scope;
49 callback.Run(a.Pass());
52 virtual void EchoEnum(sample::Enum a,
53 const Callback<void(sample::Enum)>& callback)
59 class StringRecorder {
61 StringRecorder(std::string* buf) : buf_(buf) {
63 void Run(const String& a) const {
64 *buf_ = a.To<std::string>();
66 void Run(const String& a, const String& b) const {
67 *buf_ = a.To<std::string>() + b.To<std::string>();
75 EnumRecorder(sample::Enum* value) : value_(value) {
77 void Run(sample::Enum a) const {
84 class MessagePipeWriter {
86 explicit MessagePipeWriter(const char* text) : text_(text) {
88 void Run(ScopedMessagePipeHandle handle) const {
89 WriteTextMessage(handle.get(), text_);
95 class RequestResponseTest : public testing::Test {
97 virtual ~RequestResponseTest() {
101 void PumpMessages() {
102 loop_.RunUntilIdle();
110 TEST_F(RequestResponseTest, EchoString) {
111 sample::ProviderPtr provider;
112 BindToProxy(new ProviderImpl(), &provider);
116 AllocationScope scope;
117 provider->EchoString("hello", StringRecorder(&buf));
122 EXPECT_EQ(std::string("hello"), buf);
125 TEST_F(RequestResponseTest, EchoStrings) {
126 sample::ProviderPtr provider;
127 BindToProxy(new ProviderImpl(), &provider);
131 AllocationScope scope;
132 provider->EchoStrings("hello", " world", StringRecorder(&buf));
137 EXPECT_EQ(std::string("hello world"), buf);
140 TEST_F(RequestResponseTest, EchoMessagePipeHandle) {
141 sample::ProviderPtr provider;
142 BindToProxy(new ProviderImpl(), &provider);
146 AllocationScope scope;
147 provider->EchoMessagePipeHandle(pipe2.handle1.Pass(),
148 MessagePipeWriter("hello"));
154 ReadTextMessage(pipe2.handle0.get(), &value);
156 EXPECT_EQ(std::string("hello"), value);
159 TEST_F(RequestResponseTest, EchoEnum) {
160 sample::ProviderPtr provider;
161 BindToProxy(new ProviderImpl(), &provider);
165 AllocationScope scope;
166 provider->EchoEnum(sample::ENUM_VALUE, EnumRecorder(&value));
171 EXPECT_EQ(sample::ENUM_VALUE, value);