Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(218)

Unified Diff: mojo/public/cpp/bindings/tests/router_test_util.cc

Issue 1455063004: Mojo C++ bindings: introduce MultiplexRouter and related classes. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: mojo/public/cpp/bindings/tests/router_test_util.cc
diff --git a/mojo/public/cpp/bindings/tests/router_test_util.cc b/mojo/public/cpp/bindings/tests/router_test_util.cc
new file mode 100644
index 0000000000000000000000000000000000000000..e10db24e5e2829319405a286b8544e5ebfc45aeb
--- /dev/null
+++ b/mojo/public/cpp/bindings/tests/router_test_util.cc
@@ -0,0 +1,100 @@
+// Copyright 2015 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/bindings/tests/router_test_util.h"
+
+#include <string.h>
+
+#include "mojo/public/cpp/bindings/lib/message_builder.h"
+#include "mojo/public/cpp/bindings/tests/message_queue.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace mojo {
+namespace test {
+
+void AllocRequestMessage(uint32_t name, const char* text, Message* message) {
+ size_t payload_size = strlen(text) + 1; // Plus null terminator.
+ internal::RequestMessageBuilder builder(name, payload_size);
+ memcpy(builder.buffer()->Allocate(payload_size), text, payload_size);
+
+ builder.message()->MoveTo(message);
+}
+
+void AllocResponseMessage(uint32_t name,
+ const char* text,
+ uint64_t request_id,
+ Message* message) {
+ size_t payload_size = strlen(text) + 1; // Plus null terminator.
+ internal::ResponseMessageBuilder builder(name, payload_size, request_id);
+ memcpy(builder.buffer()->Allocate(payload_size), text, payload_size);
+
+ builder.message()->MoveTo(message);
+}
+
+MessageAccumulator::MessageAccumulator(MessageQueue* queue) : queue_(queue) {}
+
+bool MessageAccumulator::Accept(Message* message) {
+ queue_->Push(message);
+ return true;
+}
+
+ResponseGenerator::ResponseGenerator() {}
+
+bool ResponseGenerator::Accept(Message* message) {
+ return false;
+}
+
+bool ResponseGenerator::AcceptWithResponder(
+ Message* message,
+ MessageReceiverWithStatus* responder) {
+ EXPECT_TRUE(message->has_flag(internal::kMessageExpectsResponse));
+
+ bool result = SendResponse(message->name(), message->request_id(),
+ reinterpret_cast<const char*>(message->payload()),
+ responder);
+ EXPECT_TRUE(responder->IsValid());
+ delete responder;
+ return result;
+}
+
+bool ResponseGenerator::SendResponse(uint32_t name,
+ uint64_t request_id,
+ const char* request_string,
+ MessageReceiver* responder) {
+ Message response;
+ std::string response_string(request_string);
+ response_string += " world!";
+ AllocResponseMessage(name, response_string.c_str(), request_id, &response);
+
+ return responder->Accept(&response);
+}
+
+LazyResponseGenerator::LazyResponseGenerator()
+ : responder_(nullptr), name_(0), request_id_(0) {}
+
+LazyResponseGenerator::~LazyResponseGenerator() {
+ delete responder_;
+}
+
+bool LazyResponseGenerator::AcceptWithResponder(
+ Message* message,
+ MessageReceiverWithStatus* responder) {
+ name_ = message->name();
+ request_id_ = message->request_id();
+ request_string_ =
+ std::string(reinterpret_cast<const char*>(message->payload()));
+ responder_ = responder;
+ return true;
+}
+
+void LazyResponseGenerator::Complete(bool send_response) {
+ if (send_response) {
+ SendResponse(name_, request_id_, request_string_.c_str(), responder_);
+ }
+ delete responder_;
+ responder_ = nullptr;
+}
+
+} // namespace test
+} // namespace mojo
« no previous file with comments | « mojo/public/cpp/bindings/tests/router_test_util.h ('k') | mojo/public/cpp/bindings/tests/router_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698