1 // Copyright 2015 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. 4 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_TESTS_ROUTER_TEST_UTIL_H_ 6 #define MOJO_PUBLIC_CPP_BINDINGS_TESTS_ROUTER_TEST_UTIL_H_ 7 8 #include <stdint.h> 9 10 #include <string> 11 12 #include "base/callback.h" 13 #include "mojo/public/cpp/bindings/message.h" 14 15 namespace mojo { 16 namespace test { 17 18 class MessageQueue; 19 20 void AllocRequestMessage(uint32_t name, const char* text, Message* message); 21 void AllocResponseMessage(uint32_t name, 22 const char* text, 23 uint64_t request_id, 24 Message* message); 25 26 class MessageAccumulator : public MessageReceiver { 27 public: 28 MessageAccumulator(MessageQueue* queue, 29 const base::Closure& closure = base::Closure()); 30 ~MessageAccumulator() override; 31 32 bool Accept(Message* message) override; 33 34 private: 35 MessageQueue* queue_; 36 base::Closure closure_; 37 }; 38 39 class ResponseGenerator : public MessageReceiverWithResponderStatus { 40 public: 41 ResponseGenerator(); 42 43 bool Accept(Message* message) override; 44 45 bool AcceptWithResponder( 46 Message* message, 47 std::unique_ptr<MessageReceiverWithStatus> responder) override; 48 bool SendResponse(uint32_t name, 49 uint64_t request_id, 50 const char* request_string, 51 MessageReceiver* responder); 52 }; 53 54 class LazyResponseGenerator : public ResponseGenerator { 55 public: 56 explicit LazyResponseGenerator( 57 const base::Closure& closure = base::Closure()); 58 59 ~LazyResponseGenerator() override; 60 61 bool AcceptWithResponder( 62 Message* message, 63 std::unique_ptr<MessageReceiverWithStatus> responder) override; 64 has_responder()65 bool has_responder() const { return !!responder_; } 66 responder_is_valid()67 bool responder_is_valid() const { return responder_->IsConnected(); } 68 set_closure(const base::Closure & closure)69 void set_closure(const base::Closure& closure) { closure_ = closure; } 70 71 // Sends the response and delete the responder. CompleteWithResponse()72 void CompleteWithResponse() { Complete(true); } 73 74 // Deletes the responder without sending a response. CompleteWithoutResponse()75 void CompleteWithoutResponse() { Complete(false); } 76 77 private: 78 // Completes the request handling by deleting responder_. Optionally 79 // also sends a response. 80 void Complete(bool send_response); 81 82 std::unique_ptr<MessageReceiverWithStatus> responder_; 83 uint32_t name_; 84 uint64_t request_id_; 85 std::string request_string_; 86 base::Closure closure_; 87 }; 88 89 } // namespace test 90 } // namespace mojo 91 92 #endif // MOJO_PUBLIC_CPP_BINDINGS_TESTS_ROUTER_TEST_UTIL_H_ 93