summaryrefslogtreecommitdiffstats
path: root/mojo/public/tests
diff options
context:
space:
mode:
authordarin@chromium.org <darin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-12-04 05:58:58 +0000
committerdarin@chromium.org <darin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-12-04 05:58:58 +0000
commit82f8b0f47d58226fe5dc8951637c9f614c2d6545 (patch)
treeaff46e1aebb7288a8feb975890fa21bca5d2e312 /mojo/public/tests
parente9476cd6510e31d114e4de13e01bb54e317d2168 (diff)
downloadchromium_src-82f8b0f47d58226fe5dc8951637c9f614c2d6545.zip
chromium_src-82f8b0f47d58226fe5dc8951637c9f614c2d6545.tar.gz
chromium_src-82f8b0f47d58226fe5dc8951637c9f614c2d6545.tar.bz2
Mojo: Generate bindings that make use of ScopedMessagePipeHandle.
R=abarth@chromium.org, davemoore@google.com, viettrungluu@chromium.org Review URL: https://codereview.chromium.org/97713002 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@238592 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'mojo/public/tests')
-rw-r--r--mojo/public/tests/bindings_connector_unittest.cc2
-rw-r--r--mojo/public/tests/bindings_handle_passing_unittest.cc128
-rw-r--r--mojo/public/tests/sample_factory.mojom30
-rw-r--r--mojo/public/tests/test_support.cc47
-rw-r--r--mojo/public/tests/test_support.h6
5 files changed, 213 insertions, 0 deletions
diff --git a/mojo/public/tests/bindings_connector_unittest.cc b/mojo/public/tests/bindings_connector_unittest.cc
index 10de52c..47052ab 100644
--- a/mojo/public/tests/bindings_connector_unittest.cc
+++ b/mojo/public/tests/bindings_connector_unittest.cc
@@ -210,6 +210,8 @@ TEST_F(BindingsConnectorTest, MessageWithHandles) {
// TODO(vtl): Do we need a better way of "downcasting" the handle types?
ScopedMessagePipeHandle smph;
smph.reset(MessagePipeHandle(message_received.handles[0].value()));
+ message_received.handles[0] = Handle(); // |smph| now owns this handle.
+
Connector connector_received(smph.Pass());
Connector connector_original(handles[1].Pass());
diff --git a/mojo/public/tests/bindings_handle_passing_unittest.cc b/mojo/public/tests/bindings_handle_passing_unittest.cc
new file mode 100644
index 0000000..6ebc1bd
--- /dev/null
+++ b/mojo/public/tests/bindings_handle_passing_unittest.cc
@@ -0,0 +1,128 @@
+// Copyright 2013 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/bindings/lib/remote_ptr.h"
+#include "mojo/public/tests/simple_bindings_support.h"
+#include "mojo/public/tests/test_support.h"
+#include "mojom/sample_factory.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace mojo {
+namespace test {
+namespace {
+
+const char kText1[] = "hello";
+const char kText2[] = "world";
+
+class SampleFactoryImpl : public sample::FactoryStub {
+ public:
+ explicit SampleFactoryImpl(ScopedMessagePipeHandle pipe)
+ : client_(pipe.Pass()) {
+ client_.SetPeer(this);
+ }
+
+ virtual void DoStuff(const sample::Request& request,
+ ScopedMessagePipeHandle pipe) MOJO_OVERRIDE {
+ std::string text1;
+ EXPECT_TRUE(ReadTextMessage(pipe.get(), &text1));
+
+ std::string text2;
+ EXPECT_TRUE(ReadTextMessage(request.pipe().get(), &text2));
+
+ ScopedMessagePipeHandle pipe0;
+ CreateMessagePipe(&pipe0, &pipe1_);
+
+ EXPECT_TRUE(WriteTextMessage(pipe1_.get(), text2));
+
+ AllocationScope scope;
+ sample::Response::Builder response;
+ response.set_x(2);
+ response.set_pipe(pipe0.Pass());
+ client_->DidStuff(response.Finish(), text1);
+ }
+
+ private:
+ RemotePtr<sample::FactoryClient> client_;
+ ScopedMessagePipeHandle pipe1_;
+};
+
+class SampleFactoryClientImpl : public sample::FactoryClientStub {
+ public:
+ explicit SampleFactoryClientImpl(ScopedMessagePipeHandle pipe)
+ : factory_(pipe.Pass()),
+ got_response_(false) {
+ factory_.SetPeer(this);
+ }
+
+ void Start() {
+ ScopedMessagePipeHandle pipe0;
+ CreateMessagePipe(&pipe0, &pipe1_);
+
+ EXPECT_TRUE(WriteTextMessage(pipe1_.get(), kText1));
+
+ ScopedMessagePipeHandle pipe2;
+ CreateMessagePipe(&pipe2, &pipe3_);
+
+ EXPECT_TRUE(WriteTextMessage(pipe3_.get(), kText2));
+
+ AllocationScope scope;
+ sample::Request::Builder request;
+ request.set_x(1);
+ request.set_pipe(pipe2.Pass());
+ factory_->DoStuff(request.Finish(), pipe0.Pass());
+ }
+
+ bool got_response() const {
+ return got_response_;
+ }
+
+ virtual void DidStuff(const sample::Response& response,
+ const String& text1) MOJO_OVERRIDE {
+ EXPECT_EQ(std::string(kText1), text1.To<std::string>());
+
+ std::string text2;
+ EXPECT_TRUE(ReadTextMessage(response.pipe().get(), &text2));
+ EXPECT_EQ(std::string(kText2), text2);
+
+ got_response_ = true;
+ }
+
+ private:
+ RemotePtr<sample::Factory> factory_;
+ ScopedMessagePipeHandle pipe1_;
+ ScopedMessagePipeHandle pipe3_;
+ bool got_response_;
+};
+
+} // namespace
+
+class BindingsHandlePassingTest : public testing::Test {
+ public:
+ void PumpMessages() {
+ bindings_support_.Process();
+ }
+
+ private:
+ SimpleBindingsSupport bindings_support_;
+};
+
+TEST_F(BindingsHandlePassingTest, Basic) {
+ ScopedMessagePipeHandle pipe0;
+ ScopedMessagePipeHandle pipe1;
+ CreateMessagePipe(&pipe0, &pipe1);
+
+ SampleFactoryImpl factory(pipe0.Pass());
+ SampleFactoryClientImpl factory_client(pipe1.Pass());
+
+ factory_client.Start();
+
+ EXPECT_FALSE(factory_client.got_response());
+
+ PumpMessages();
+
+ EXPECT_TRUE(factory_client.got_response());
+}
+
+} // namespace test
+} // namespace mojo
diff --git a/mojo/public/tests/sample_factory.mojom b/mojo/public/tests/sample_factory.mojom
new file mode 100644
index 0000000..7ae6b34
--- /dev/null
+++ b/mojo/public/tests/sample_factory.mojom
@@ -0,0 +1,30 @@
+// Copyright 2013 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.
+
+module sample {
+
+// This sample shows how handles to MessagePipes can be sent as both parameters
+// to methods as well as fields on structs.
+
+struct Request {
+ int32 x;
+ handle<message_pipe> pipe;
+};
+
+struct Response {
+ int32 x;
+ handle<message_pipe> pipe;
+};
+
+[Peer=FactoryClient]
+interface Factory {
+ void DoStuff(Request request, handle<message_pipe> pipe);
+};
+
+[Peer=Factory]
+interface FactoryClient {
+ void DidStuff(Response response, string text);
+};
+
+} // module sample
diff --git a/mojo/public/tests/test_support.cc b/mojo/public/tests/test_support.cc
index 151fae3..81a0c3a2 100644
--- a/mojo/public/tests/test_support.cc
+++ b/mojo/public/tests/test_support.cc
@@ -10,6 +10,53 @@
namespace mojo {
namespace test {
+bool WriteTextMessage(MessagePipeHandle handle, const std::string& text) {
+ MojoResult rv = WriteMessageRaw(handle,
+ text.data(),
+ static_cast<uint32_t>(text.size()),
+ NULL,
+ 0,
+ MOJO_WRITE_MESSAGE_FLAG_NONE);
+ return rv == MOJO_RESULT_OK;
+}
+
+bool ReadTextMessage(MessagePipeHandle handle, std::string* text) {
+ MojoResult rv;
+ bool did_wait = false;
+
+ uint32_t num_bytes = 0, num_handles = 0;
+ for (;;) {
+ rv = ReadMessageRaw(handle,
+ NULL,
+ &num_bytes,
+ NULL,
+ &num_handles,
+ MOJO_READ_MESSAGE_FLAG_NONE);
+ if (rv == MOJO_RESULT_NOT_FOUND) {
+ if (did_wait) {
+ assert(false); // Looping endlessly!?
+ return false;
+ }
+ rv = Wait(handle, MOJO_WAIT_FLAG_READABLE, MOJO_DEADLINE_INDEFINITE);
+ if (rv != MOJO_RESULT_OK)
+ return false;
+ did_wait = true;
+ } else {
+ assert(!num_handles);
+ break;
+ }
+ }
+
+ text->resize(num_bytes);
+ rv = ReadMessageRaw(handle,
+ &text->at(0),
+ &num_bytes,
+ NULL,
+ &num_handles,
+ MOJO_READ_MESSAGE_FLAG_NONE);
+ return rv == MOJO_RESULT_OK;
+}
+
void IterateAndReportPerf(const char* test_name,
base::Callback<void()> single_iteration) {
// TODO(vtl): These should be specifiable using command-line flags.
diff --git a/mojo/public/tests/test_support.h b/mojo/public/tests/test_support.h
index bddd7a0..9c144a3 100644
--- a/mojo/public/tests/test_support.h
+++ b/mojo/public/tests/test_support.h
@@ -5,11 +5,17 @@
#ifndef MOJO_PUBLIC_TESTS_TEST_SUPPORT_H_
#define MOJO_PUBLIC_TESTS_TEST_SUPPORT_H_
+#include <string>
+
#include "base/callback.h"
+#include "mojo/public/system/core_cpp.h"
namespace mojo {
namespace test {
+bool WriteTextMessage(MessagePipeHandle handle, const std::string& text);
+bool ReadTextMessage(MessagePipeHandle handle, std::string* text);
+
// Run |single_iteration| an appropriate number of times and report its
// performance appropriately. (This actually runs |single_iteration| for a fixed
// amount of time and reports the number of iterations per unit time.)