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

Side by Side Diff: mojo/public/cpp/bindings/tests/router_unittest.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 unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <stdlib.h>
6 #include <string.h>
7
8 #include "base/message_loop/message_loop.h" 5 #include "base/message_loop/message_loop.h"
9 #include "mojo/message_pump/message_pump_mojo.h" 6 #include "mojo/message_pump/message_pump_mojo.h"
10 #include "mojo/public/cpp/bindings/lib/message_builder.h"
11 #include "mojo/public/cpp/bindings/lib/router.h" 7 #include "mojo/public/cpp/bindings/lib/router.h"
12 #include "mojo/public/cpp/bindings/tests/message_queue.h" 8 #include "mojo/public/cpp/bindings/tests/message_queue.h"
9 #include "mojo/public/cpp/bindings/tests/router_test_util.h"
13 #include "mojo/public/cpp/system/macros.h" 10 #include "mojo/public/cpp/system/macros.h"
14 #include "testing/gtest/include/gtest/gtest.h" 11 #include "testing/gtest/include/gtest/gtest.h"
15 12
16 namespace mojo { 13 namespace mojo {
17 namespace test { 14 namespace test {
18 namespace { 15 namespace {
19 16
20 void AllocRequestMessage(uint32_t name, const char* text, Message* message) {
21 size_t payload_size = strlen(text) + 1; // Plus null terminator.
22 internal::RequestMessageBuilder builder(name, payload_size);
23 memcpy(builder.buffer()->Allocate(payload_size), text, payload_size);
24
25 builder.message()->MoveTo(message);
26 }
27
28 void AllocResponseMessage(uint32_t name,
29 const char* text,
30 uint64_t request_id,
31 Message* message) {
32 size_t payload_size = strlen(text) + 1; // Plus null terminator.
33 internal::ResponseMessageBuilder builder(name, payload_size, request_id);
34 memcpy(builder.buffer()->Allocate(payload_size), text, payload_size);
35
36 builder.message()->MoveTo(message);
37 }
38
39 class MessageAccumulator : public MessageReceiver {
40 public:
41 explicit MessageAccumulator(MessageQueue* queue) : queue_(queue) {}
42
43 bool Accept(Message* message) override {
44 queue_->Push(message);
45 return true;
46 }
47
48 private:
49 MessageQueue* queue_;
50 };
51
52 class ResponseGenerator : public MessageReceiverWithResponderStatus {
53 public:
54 ResponseGenerator() {}
55
56 bool Accept(Message* message) override { return false; }
57
58 bool AcceptWithResponder(Message* message,
59 MessageReceiverWithStatus* responder) override {
60 EXPECT_TRUE(message->has_flag(internal::kMessageExpectsResponse));
61
62 bool result = SendResponse(
63 message->name(), message->request_id(),
64 reinterpret_cast<const char*>(message->payload()), responder);
65 EXPECT_TRUE(responder->IsValid());
66 delete responder;
67 return result;
68 }
69
70 bool SendResponse(uint32_t name,
71 uint64_t request_id,
72 const char* request_string,
73 MessageReceiver* responder) {
74 Message response;
75 std::string response_string(request_string);
76 response_string += " world!";
77 AllocResponseMessage(name, response_string.c_str(), request_id, &response);
78
79 return responder->Accept(&response);
80 }
81 };
82
83 class LazyResponseGenerator : public ResponseGenerator {
84 public:
85 LazyResponseGenerator() : responder_(nullptr), name_(0), request_id_(0) {}
86
87 ~LazyResponseGenerator() override { delete responder_; }
88
89 bool AcceptWithResponder(Message* message,
90 MessageReceiverWithStatus* responder) override {
91 name_ = message->name();
92 request_id_ = message->request_id();
93 request_string_ =
94 std::string(reinterpret_cast<const char*>(message->payload()));
95 responder_ = responder;
96 return true;
97 }
98
99 bool has_responder() const { return !!responder_; }
100
101 bool responder_is_valid() const { return responder_->IsValid(); }
102
103 // Send the response and delete the responder.
104 void CompleteWithResponse() { Complete(true); }
105
106 // Delete the responder without sending a response.
107 void CompleteWithoutResponse() { Complete(false); }
108
109 private:
110 // Completes the request handling by deleting responder_. Optionally
111 // also sends a response.
112 void Complete(bool send_response) {
113 if (send_response) {
114 SendResponse(name_, request_id_, request_string_.c_str(), responder_);
115 }
116 delete responder_;
117 responder_ = nullptr;
118 }
119
120 MessageReceiverWithStatus* responder_;
121 uint32_t name_;
122 uint64_t request_id_;
123 std::string request_string_;
124 };
125
126 class RouterTest : public testing::Test { 17 class RouterTest : public testing::Test {
127 public: 18 public:
128 RouterTest() : loop_(common::MessagePumpMojo::Create()) {} 19 RouterTest() : loop_(common::MessagePumpMojo::Create()) {}
129 20
130 void SetUp() override { 21 void SetUp() override {
131 CreateMessagePipe(nullptr, &handle0_, &handle1_); 22 CreateMessagePipe(nullptr, &handle0_, &handle1_);
132 } 23 }
133 24
134 void TearDown() override {} 25 void TearDown() override {}
135 26
(...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after
370 EXPECT_TRUE(generator.has_responder()); 261 EXPECT_TRUE(generator.has_responder());
371 } 262 }
372 263
373 EXPECT_FALSE(generator.responder_is_valid()); 264 EXPECT_FALSE(generator.responder_is_valid());
374 generator.CompleteWithResponse(); // This should end up doing nothing. 265 generator.CompleteWithResponse(); // This should end up doing nothing.
375 } 266 }
376 267
377 } // namespace 268 } // namespace
378 } // namespace test 269 } // namespace test
379 } // namespace mojo 270 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/tests/router_test_util.cc ('k') | mojo/public/cpp/bindings/tests/validation_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698