blob: 5539c98d7cbd1574bade41f752cb5d02daa733dc [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
tfarina10a5c062015-09-04 18:47:578#include <stdint.h>
9
[email protected]a16ed65e2009-02-14 01:35:2710#include <utility>
11#include <vector>
12
tfarina4da8275d2015-09-16 09:56:2113#include "base/compiler_specific.h"
14#include "base/macros.h"
[email protected]9f9db892011-01-31 21:43:3115#include "base/observer_list.h"
[email protected]90b721e62010-04-05 17:35:0116#include "ipc/ipc_channel.h"
[email protected]a16ed65e2009-02-14 01:35:2717
18namespace IPC {
19
[email protected]a83d42292010-08-17 22:51:1020class Message;
21
[email protected]a16ed65e2009-02-14 01:35:2722// This test sink provides a "sink" for IPC messages that are sent. It allows
23// the caller to query messages received in various different ways. It is
24// designed for tests for objects that use the IPC system.
25//
26// Typical usage:
27//
28// test_sink.ClearMessages();
29// do_something();
30//
31// // We should have gotten exactly one update state message.
32// EXPECT_TRUE(test_sink.GetUniqeMessageMatching(ViewHostMsg_Update::ID));
33// // ...and no start load messages.
34// EXPECT_FALSE(test_sink.GetFirstMessageMatching(ViewHostMsg_Start::ID));
35//
36// // Now inspect a message. This assumes a message that was declared like
37// // this: IPC_MESSAGE_ROUTED2(ViewMsg_Foo, bool, int)
38// IPC::Message* msg = test_sink.GetFirstMessageMatching(ViewMsg_Foo::ID));
39// ASSERT_TRUE(msg);
40// bool first_param;
41// int second_param;
42// ViewMsg_Foo::Read(msg, &first_param, &second_param);
43//
44// // Go on to the next phase of the test.
45// test_sink.ClearMessages();
46//
[email protected]465faa22011-02-08 16:31:4647// To read a sync reply, do this:
48//
49// IPC::Message* msg = test_sink.GetUniqueMessageMatching(IPC_REPLY_ID);
50// ASSERT_TRUE(msg);
brettwd5ca2bc2015-05-29 22:15:4751// base::TupleTypes<ViewHostMsg_Foo::ReplyParam>::ValueTuple reply_data;
[email protected]465faa22011-02-08 16:31:4652// EXPECT_TRUE(ViewHostMsg_Foo::ReadReplyParam(msg, &reply_data));
53//
[email protected]9f9db892011-01-31 21:43:3154// You can also register to be notified when messages are posted to the sink.
55// This can be useful if you need to wait for a particular message that will
56// be posted asynchronously. Example usage:
57//
[email protected]b7f59e822012-06-29 22:05:2658// class MyListener : public IPC::Listener {
[email protected]9f9db892011-01-31 21:43:3159// public:
60// virtual bool OnMessageReceived(const IPC::Message& msg) {
61// <do something with the message>
62// MessageLoop::current()->Quit();
63// return false; // to store the message in the sink, or true to drop it
64// }
65// };
66//
67// MyListener listener;
68// test_sink.AddFilter(&listener);
69// StartSomeAsynchronousProcess(&test_sink);
70// MessageLoop::current()->Run();
71// <inspect the results>
72// ...
73//
[email protected]a16ed65e2009-02-14 01:35:2774// To hook up the sink, all you need to do is call OnMessageReceived when a
[email protected]ad76da72010-03-24 00:27:1875// message is received.
[email protected]a5768512013-04-12 19:35:3576class TestSink : public Channel {
[email protected]a16ed65e2009-02-14 01:35:2777 public:
78 TestSink();
dchengfe61fca2014-10-22 02:29:5279 ~TestSink() override;
[email protected]a16ed65e2009-02-14 01:35:2780
[email protected]90b721e62010-04-05 17:35:0181 // Interface in IPC::Channel. This copies the message to the sink and then
82 // deletes it.
dchengfe61fca2014-10-22 02:29:5283 bool Send(IPC::Message* message) override;
84 bool Connect() override WARN_UNUSED_RESULT;
85 void Close() override;
86 base::ProcessId GetPeerPID() const override;
87 base::ProcessId GetSelfPID() const override;
[email protected]2f60c9b2014-06-06 20:13:5188
89#if defined(OS_POSIX) && !defined(OS_NACL)
dchengfe61fca2014-10-22 02:29:5290 int GetClientFileDescriptor() const override;
91 base::ScopedFD TakeClientFileDescriptor() override;
[email protected]2f60c9b2014-06-06 20:13:5192#endif // defined(OS_POSIX) && !defined(OS_NACL)
[email protected]90b721e62010-04-05 17:35:0193
[email protected]a16ed65e2009-02-14 01:35:2794 // Used by the source of the messages to send the message to the sink. This
95 // will make a copy of the message and store it in the list.
[email protected]a5768512013-04-12 19:35:3596 bool OnMessageReceived(const Message& msg);
[email protected]a16ed65e2009-02-14 01:35:2797
98 // Returns the number of messages in the queue.
99 size_t message_count() const { return messages_.size(); }
100
101 // Clears the message queue of saved messages.
102 void ClearMessages();
103
104 // Returns the message at the given index in the queue. The index may be out
105 // of range, in which case the return value is NULL. The returned pointer will
106 // only be valid until another message is received or the list is cleared.
107 const Message* GetMessageAt(size_t index) const;
108
109 // Returns the first message with the given ID in the queue. If there is no
110 // message with the given ID, returns NULL. The returned pointer will only be
111 // valid until another message is received or the list is cleared.
tfarina10a5c062015-09-04 18:47:57112 const Message* GetFirstMessageMatching(uint32_t id) const;
[email protected]a16ed65e2009-02-14 01:35:27113
114 // Returns the message with the given ID in the queue. If there is no such
115 // message or there is more than one of that message, this will return NULL
116 // (with the expectation that you'll do an ASSERT_TRUE() on the result).
117 // The returned pointer will only be valid until another message is received
118 // or the list is cleared.
tfarina10a5c062015-09-04 18:47:57119 const Message* GetUniqueMessageMatching(uint32_t id) const;
[email protected]a16ed65e2009-02-14 01:35:27120
[email protected]9f9db892011-01-31 21:43:31121 // Adds the given listener as a filter to the TestSink.
122 // When a message is received by the TestSink, it will be dispatched to
123 // the filters, in the order they were added. If a filter returns true
124 // from OnMessageReceived, subsequent filters will not receive the message
125 // and the TestSink will not store it.
[email protected]b7f59e822012-06-29 22:05:26126 void AddFilter(Listener* filter);
[email protected]9f9db892011-01-31 21:43:31127
128 // Removes the given filter from the TestSink.
[email protected]b7f59e822012-06-29 22:05:26129 void RemoveFilter(Listener* filter);
[email protected]9f9db892011-01-31 21:43:31130
[email protected]a16ed65e2009-02-14 01:35:27131 private:
132 // The actual list of received messages.
133 std::vector<Message> messages_;
brettw236d3172015-06-03 16:31:43134 base::ObserverList<Listener> filter_list_;
[email protected]a16ed65e2009-02-14 01:35:27135
136 DISALLOW_COPY_AND_ASSIGN(TestSink);
137};
138
139} // namespace IPC
140
[email protected]3ff2a1032011-01-20 23:50:27141#endif // IPC_IPC_TEST_SINK_H_