blob: 4864e35bc4b92f771f4e366cd4f84f5e919354e5 [file] [log] [blame]
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "mojo/public/cpp/environment/environment.h"
#include "mojo/public/cpp/test_support/test_utils.h"
#include "mojo/public/cpp/utility/run_loop.h"
#include "mojo/public/interfaces/bindings/tests/sample_import.mojom.h"
#include "mojo/public/interfaces/bindings/tests/sample_interfaces.mojom.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace mojo {
namespace test {
namespace {
class ProviderImpl : public InterfaceImpl<sample::Provider> {
public:
void EchoString(const String& a,
const Callback<void(String)>& callback) override {
Callback<void(String)> callback_copy;
// Make sure operator= is used.
callback_copy = callback;
callback_copy.Run(a);
}
void EchoStrings(const String& a,
const String& b,
const Callback<void(String, String)>& callback) override {
callback.Run(a, b);
}
void EchoMessagePipeHandle(
ScopedMessagePipeHandle a,
const Callback<void(ScopedMessagePipeHandle)>& callback) override {
callback.Run(a.Pass());
}
void EchoEnum(sample::Enum a,
const Callback<void(sample::Enum)>& callback) override {
callback.Run(a);
}
};
class StringRecorder {
public:
explicit StringRecorder(std::string* buf) : buf_(buf) {}
void Run(const String& a) const { *buf_ = a; }
void Run(const String& a, const String& b) const {
*buf_ = a.get() + b.get();
}
private:
std::string* buf_;
};
class EnumRecorder {
public:
explicit EnumRecorder(sample::Enum* value) : value_(value) {}
void Run(sample::Enum a) const { *value_ = a; }
private:
sample::Enum* value_;
};
class MessagePipeWriter {
public:
explicit MessagePipeWriter(const char* text) : text_(text) {}
void Run(ScopedMessagePipeHandle handle) const {
WriteTextMessage(handle.get(), text_);
}
private:
std::string text_;
};
class RequestResponseTest : public testing::Test {
public:
~RequestResponseTest() override { loop_.RunUntilIdle(); }
void PumpMessages() { loop_.RunUntilIdle(); }
private:
Environment env_;
RunLoop loop_;
};
TEST_F(RequestResponseTest, EchoString) {
sample::ProviderPtr provider;
BindToProxy(new ProviderImpl(), &provider);
std::string buf;
provider->EchoString(String::From("hello"), StringRecorder(&buf));
PumpMessages();
EXPECT_EQ(std::string("hello"), buf);
}
TEST_F(RequestResponseTest, EchoStrings) {
sample::ProviderPtr provider;
BindToProxy(new ProviderImpl(), &provider);
std::string buf;
provider->EchoStrings(
String::From("hello"), String::From(" world"), StringRecorder(&buf));
PumpMessages();
EXPECT_EQ(std::string("hello world"), buf);
}
TEST_F(RequestResponseTest, EchoMessagePipeHandle) {
sample::ProviderPtr provider;
BindToProxy(new ProviderImpl(), &provider);
MessagePipe pipe2;
provider->EchoMessagePipeHandle(pipe2.handle1.Pass(),
MessagePipeWriter("hello"));
PumpMessages();
std::string value;
ReadTextMessage(pipe2.handle0.get(), &value);
EXPECT_EQ(std::string("hello"), value);
}
TEST_F(RequestResponseTest, EchoEnum) {
sample::ProviderPtr provider;
BindToProxy(new ProviderImpl(), &provider);
sample::Enum value;
provider->EchoEnum(sample::ENUM_VALUE, EnumRecorder(&value));
PumpMessages();
EXPECT_EQ(sample::ENUM_VALUE, value);
}
} // namespace
} // namespace test
} // namespace mojo