blob: 3e70704348e25fd9df1b28cd329a144d3fd23a94 [file] [log] [blame]
[email protected]3ff2a1032011-01-20 23:50:271// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]a16ed65e2009-02-14 01:35:272// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]3ff2a1032011-01-20 23:50:275#ifndef IPC_IPC_TEST_SINK_H_
6#define IPC_IPC_TEST_SINK_H_
[email protected]a16ed65e2009-02-14 01:35:277
avi246998d82015-12-22 02:39:048#include <stddef.h>
tfarina10a5c062015-09-04 18:47:579#include <stdint.h>
10
[email protected]a16ed65e2009-02-14 01:35:2711#include <utility>
12#include <vector>
13
tfarina4da8275d2015-09-16 09:56:2114#include "base/compiler_specific.h"
15#include "base/macros.h"
[email protected]9f9db892011-01-31 21:43:3116#include "base/observer_list.h"
avi246998d82015-12-22 02:39:0417#include "build/build_config.h"
[email protected]90b721e62010-04-05 17:35:0118#include "ipc/ipc_channel.h"
[email protected]a16ed65e2009-02-14 01:35:2719
20namespace IPC {
21
[email protected]a83d42292010-08-17 22:51:1022class Message;
23
[email protected]a16ed65e2009-02-14 01:35:2724// This test sink provides a "sink" for IPC messages that are sent. It allows
25// the caller to query messages received in various different ways. It is
26// designed for tests for objects that use the IPC system.
27//
28// Typical usage:
29//
30// test_sink.ClearMessages();
31// do_something();
32//
33// // We should have gotten exactly one update state message.
34// EXPECT_TRUE(test_sink.GetUniqeMessageMatching(ViewHostMsg_Update::ID));
35// // ...and no start load messages.
36// EXPECT_FALSE(test_sink.GetFirstMessageMatching(ViewHostMsg_Start::ID));
37//
38// // Now inspect a message. This assumes a message that was declared like
39// // this: IPC_MESSAGE_ROUTED2(ViewMsg_Foo, bool, int)
40// IPC::Message* msg = test_sink.GetFirstMessageMatching(ViewMsg_Foo::ID));
41// ASSERT_TRUE(msg);
42// bool first_param;
43// int second_param;
44// ViewMsg_Foo::Read(msg, &first_param, &second_param);
45//
46// // Go on to the next phase of the test.
47// test_sink.ClearMessages();
48//
[email protected]465faa22011-02-08 16:31:4649// To read a sync reply, do this:
50//
51// IPC::Message* msg = test_sink.GetUniqueMessageMatching(IPC_REPLY_ID);
52// ASSERT_TRUE(msg);
brettwd5ca2bc2015-05-29 22:15:4753// base::TupleTypes<ViewHostMsg_Foo::ReplyParam>::ValueTuple reply_data;
[email protected]465faa22011-02-08 16:31:4654// EXPECT_TRUE(ViewHostMsg_Foo::ReadReplyParam(msg, &reply_data));
55//
[email protected]9f9db892011-01-31 21:43:3156// You can also register to be notified when messages are posted to the sink.
57// This can be useful if you need to wait for a particular message that will
58// be posted asynchronously. Example usage:
59//
[email protected]b7f59e822012-06-29 22:05:2660// class MyListener : public IPC::Listener {
[email protected]9f9db892011-01-31 21:43:3161// public:
kotenkovb2bb5e982016-03-16 20:50:0562// MyListener(const base::Closure& closure)
63// : message_received_closure_(closure) {}
[email protected]9f9db892011-01-31 21:43:3164// virtual bool OnMessageReceived(const IPC::Message& msg) {
65// <do something with the message>
kotenkovb2bb5e982016-03-16 20:50:0566// message_received_closure_.Run();
[email protected]9f9db892011-01-31 21:43:3167// return false; // to store the message in the sink, or true to drop it
68// }
kotenkovb2bb5e982016-03-16 20:50:0569// private:
70// base::Closure message_received_closure_;
[email protected]9f9db892011-01-31 21:43:3171// };
72//
kotenkovb2bb5e982016-03-16 20:50:0573// base::RunLoop run_loop;
74// MyListener listener(run_loop.QuitClosure());
[email protected]9f9db892011-01-31 21:43:3175// test_sink.AddFilter(&listener);
76// StartSomeAsynchronousProcess(&test_sink);
kotenkovb2bb5e982016-03-16 20:50:0577// run_loop.Run();
[email protected]9f9db892011-01-31 21:43:3178// <inspect the results>
79// ...
80//
[email protected]a16ed65e2009-02-14 01:35:2781// To hook up the sink, all you need to do is call OnMessageReceived when a
[email protected]ad76da72010-03-24 00:27:1882// message is received.
[email protected]a5768512013-04-12 19:35:3583class TestSink : public Channel {
[email protected]a16ed65e2009-02-14 01:35:2784 public:
85 TestSink();
dchengfe61fca2014-10-22 02:29:5286 ~TestSink() override;
[email protected]a16ed65e2009-02-14 01:35:2787
[email protected]90b721e62010-04-05 17:35:0188 // Interface in IPC::Channel. This copies the message to the sink and then
89 // deletes it.
dchengfe61fca2014-10-22 02:29:5290 bool Send(IPC::Message* message) override;
91 bool Connect() override WARN_UNUSED_RESULT;
92 void Close() override;
93 base::ProcessId GetPeerPID() const override;
94 base::ProcessId GetSelfPID() const override;
[email protected]2f60c9b2014-06-06 20:13:5195
96#if defined(OS_POSIX) && !defined(OS_NACL)
dchengfe61fca2014-10-22 02:29:5297 int GetClientFileDescriptor() const override;
98 base::ScopedFD TakeClientFileDescriptor() override;
[email protected]2f60c9b2014-06-06 20:13:5199#endif // defined(OS_POSIX) && !defined(OS_NACL)
[email protected]90b721e62010-04-05 17:35:01100
[email protected]a16ed65e2009-02-14 01:35:27101 // Used by the source of the messages to send the message to the sink. This
102 // will make a copy of the message and store it in the list.
[email protected]a5768512013-04-12 19:35:35103 bool OnMessageReceived(const Message& msg);
[email protected]a16ed65e2009-02-14 01:35:27104
105 // Returns the number of messages in the queue.
106 size_t message_count() const { return messages_.size(); }
107
108 // Clears the message queue of saved messages.
109 void ClearMessages();
110
111 // Returns the message at the given index in the queue. The index may be out
112 // of range, in which case the return value is NULL. The returned pointer will
113 // only be valid until another message is received or the list is cleared.
114 const Message* GetMessageAt(size_t index) const;
115
116 // Returns the first message with the given ID in the queue. If there is no
117 // message with the given ID, returns NULL. The returned pointer will only be
118 // valid until another message is received or the list is cleared.
tfarina10a5c062015-09-04 18:47:57119 const Message* GetFirstMessageMatching(uint32_t id) const;
[email protected]a16ed65e2009-02-14 01:35:27120
121 // Returns the message with the given ID in the queue. If there is no such
122 // message or there is more than one of that message, this will return NULL
123 // (with the expectation that you'll do an ASSERT_TRUE() on the result).
124 // The returned pointer will only be valid until another message is received
125 // or the list is cleared.
tfarina10a5c062015-09-04 18:47:57126 const Message* GetUniqueMessageMatching(uint32_t id) const;
[email protected]a16ed65e2009-02-14 01:35:27127
[email protected]9f9db892011-01-31 21:43:31128 // Adds the given listener as a filter to the TestSink.
129 // When a message is received by the TestSink, it will be dispatched to
130 // the filters, in the order they were added. If a filter returns true
131 // from OnMessageReceived, subsequent filters will not receive the message
132 // and the TestSink will not store it.
[email protected]b7f59e822012-06-29 22:05:26133 void AddFilter(Listener* filter);
[email protected]9f9db892011-01-31 21:43:31134
135 // Removes the given filter from the TestSink.
[email protected]b7f59e822012-06-29 22:05:26136 void RemoveFilter(Listener* filter);
[email protected]9f9db892011-01-31 21:43:31137
[email protected]a16ed65e2009-02-14 01:35:27138 private:
139 // The actual list of received messages.
140 std::vector<Message> messages_;
brettw236d3172015-06-03 16:31:43141 base::ObserverList<Listener> filter_list_;
[email protected]a16ed65e2009-02-14 01:35:27142
143 DISALLOW_COPY_AND_ASSIGN(TestSink);
144};
145
146} // namespace IPC
147
[email protected]3ff2a1032011-01-20 23:50:27148#endif // IPC_IPC_TEST_SINK_H_