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/bindings/allocation_scope.h"
6 #include "mojo/public/bindings/remote_ptr.h"
7 #include "mojo/public/bindings/tests/sample_import.mojom.h"
8 #include "mojo/public/bindings/tests/sample_interfaces.mojom.h"
9 #include "mojo/public/environment/environment.h"
10 #include "mojo/public/tests/test_utils.h"
11 #include "mojo/public/utility/run_loop.h"
12 #include "testing/gtest/include/gtest/gtest.h"
18 class ProviderImpl : public sample::Provider {
20 explicit ProviderImpl(sample::ScopedProviderClientHandle handle)
21 : client_(handle.Pass(), this) {
24 virtual void EchoString(
26 const Callback<void(String)>& callback) MOJO_OVERRIDE {
27 AllocationScope scope;
31 virtual void EchoStrings(
34 const Callback<void(String, String)>& callback) MOJO_OVERRIDE {
35 AllocationScope scope;
39 virtual void EchoMessagePipeHandle(
40 ScopedMessagePipeHandle a,
41 const Callback<void(ScopedMessagePipeHandle)>& callback) MOJO_OVERRIDE {
42 AllocationScope scope;
43 callback.Run(a.Pass());
47 RemotePtr<sample::ProviderClient> client_;
50 class StringRecorder {
52 StringRecorder(std::string* buf) : buf_(buf) {
54 void Run(const String& a) const {
55 *buf_ = a.To<std::string>();
57 void Run(const String& a, const String& b) const {
58 *buf_ = a.To<std::string>() + b.To<std::string>();
64 class MessagePipeWriter {
66 explicit MessagePipeWriter(const char* text) : text_(text) {
68 void Run(ScopedMessagePipeHandle handle) const {
69 WriteTextMessage(handle.get(), text_);
75 class RequestResponseTest : public testing::Test {
86 TEST_F(RequestResponseTest, EchoString) {
87 InterfacePipe<sample::Provider> pipe;
88 ProviderImpl provider_impl(pipe.handle_to_peer.Pass());
89 RemotePtr<sample::Provider> provider(pipe.handle_to_self.Pass(), NULL);
93 AllocationScope scope;
94 provider->EchoString("hello", StringRecorder(&buf));
99 EXPECT_EQ(std::string("hello"), buf);
102 TEST_F(RequestResponseTest, EchoStrings) {
103 InterfacePipe<sample::Provider> pipe;
104 ProviderImpl provider_impl(pipe.handle_to_peer.Pass());
105 RemotePtr<sample::Provider> provider(pipe.handle_to_self.Pass(), NULL);
109 AllocationScope scope;
110 provider->EchoStrings("hello", " world", StringRecorder(&buf));
115 EXPECT_EQ(std::string("hello world"), buf);
118 TEST_F(RequestResponseTest, EchoMessagePipeHandle) {
119 InterfacePipe<sample::Provider> pipe;
120 ProviderImpl provider_impl(pipe.handle_to_peer.Pass());
121 RemotePtr<sample::Provider> provider(pipe.handle_to_self.Pass(), NULL);
125 AllocationScope scope;
126 provider->EchoMessagePipeHandle(pipe2.handle1.Pass(),
127 MessagePipeWriter("hello"));
133 ReadTextMessage(pipe2.handle0.get(), &value);
135 EXPECT_EQ(std::string("hello"), value);