blob: 54bab34e01f7a06bd5c40a2fc13a72cce15376c7 [file] [log] [blame]
brettw@google.comc0f85582011-01-21 08:50:27 +09001// Copyright (c) 2011 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 IPC_IPC_TEST_SINK_H_
6#define IPC_IPC_TEST_SINK_H_
brettw@google.comc0f85582011-01-21 08:50:27 +09007
8#include <utility>
9#include <vector>
10
11#include "base/basictypes.h"
bryner@chromium.org44ae58f2011-02-01 06:43:31 +090012#include "base/observer_list.h"
brettw@google.comc0f85582011-01-21 08:50:27 +090013#include "ipc/ipc_channel.h"
14
15namespace IPC {
16
17class Message;
18
19// This test sink provides a "sink" for IPC messages that are sent. It allows
20// the caller to query messages received in various different ways. It is
21// designed for tests for objects that use the IPC system.
22//
23// Typical usage:
24//
25// test_sink.ClearMessages();
26// do_something();
27//
28// // We should have gotten exactly one update state message.
29// EXPECT_TRUE(test_sink.GetUniqeMessageMatching(ViewHostMsg_Update::ID));
30// // ...and no start load messages.
31// EXPECT_FALSE(test_sink.GetFirstMessageMatching(ViewHostMsg_Start::ID));
32//
33// // Now inspect a message. This assumes a message that was declared like
34// // this: IPC_MESSAGE_ROUTED2(ViewMsg_Foo, bool, int)
35// IPC::Message* msg = test_sink.GetFirstMessageMatching(ViewMsg_Foo::ID));
36// ASSERT_TRUE(msg);
37// bool first_param;
38// int second_param;
39// ViewMsg_Foo::Read(msg, &first_param, &second_param);
40//
41// // Go on to the next phase of the test.
42// test_sink.ClearMessages();
43//
brettw@chromium.orgccb3fef2011-02-09 01:31:46 +090044// To read a sync reply, do this:
45//
46// IPC::Message* msg = test_sink.GetUniqueMessageMatching(IPC_REPLY_ID);
47// ASSERT_TRUE(msg);
jam@chromium.orgcbc95ec2011-03-16 02:16:55 +090048// TupleTypes<ViewHostMsg_Foo::ReplyParam>::ValueTuple reply_data;
brettw@chromium.orgccb3fef2011-02-09 01:31:46 +090049// EXPECT_TRUE(ViewHostMsg_Foo::ReadReplyParam(msg, &reply_data));
50//
bryner@chromium.org44ae58f2011-02-01 06:43:31 +090051// You can also register to be notified when messages are posted to the sink.
52// This can be useful if you need to wait for a particular message that will
53// be posted asynchronously. Example usage:
54//
brettw@chromium.orgdb1259e2012-06-30 07:05:26 +090055// class MyListener : public IPC::Listener {
bryner@chromium.org44ae58f2011-02-01 06:43:31 +090056// public:
57// virtual bool OnMessageReceived(const IPC::Message& msg) {
58// <do something with the message>
59// MessageLoop::current()->Quit();
60// return false; // to store the message in the sink, or true to drop it
61// }
62// };
63//
64// MyListener listener;
65// test_sink.AddFilter(&listener);
66// StartSomeAsynchronousProcess(&test_sink);
67// MessageLoop::current()->Run();
68// <inspect the results>
69// ...
70//
brettw@google.comc0f85582011-01-21 08:50:27 +090071// To hook up the sink, all you need to do is call OnMessageReceived when a
72// message is received.
dmazzoni@chromium.org0dc2c0c2013-04-13 04:35:35 +090073class TestSink : public Channel {
brettw@google.comc0f85582011-01-21 08:50:27 +090074 public:
75 TestSink();
hans@chromium.org78b75932011-05-25 18:08:19 +090076 virtual ~TestSink();
brettw@google.comc0f85582011-01-21 08:50:27 +090077
78 // Interface in IPC::Channel. This copies the message to the sink and then
79 // deletes it.
avi@chromium.org362c8a82011-11-18 01:09:44 +090080 virtual bool Send(IPC::Message* message) OVERRIDE;
ch.dumez@samsung.combbcb44c2014-06-10 23:38:39 +090081 virtual bool Connect() OVERRIDE WARN_UNUSED_RESULT;
morrita@chromium.orgfde2b6b2014-06-07 05:13:51 +090082 virtual void Close() OVERRIDE;
83 virtual base::ProcessId GetPeerPID() const OVERRIDE;
morrita@chromium.org15996aa2014-08-05 08:44:17 +090084 virtual base::ProcessId GetSelfPID() const OVERRIDE;
85 virtual ChannelHandle TakePipeHandle() OVERRIDE;
morrita@chromium.orgfde2b6b2014-06-07 05:13:51 +090086
87#if defined(OS_POSIX) && !defined(OS_NACL)
88 virtual int GetClientFileDescriptor() const OVERRIDE;
89 virtual int TakeClientFileDescriptor() OVERRIDE;
morrita@chromium.orgfde2b6b2014-06-07 05:13:51 +090090#endif // defined(OS_POSIX) && !defined(OS_NACL)
brettw@google.comc0f85582011-01-21 08:50:27 +090091
92 // Used by the source of the messages to send the message to the sink. This
93 // will make a copy of the message and store it in the list.
dmazzoni@chromium.org0dc2c0c2013-04-13 04:35:35 +090094 bool OnMessageReceived(const Message& msg);
brettw@google.comc0f85582011-01-21 08:50:27 +090095
96 // Returns the number of messages in the queue.
97 size_t message_count() const { return messages_.size(); }
98
99 // Clears the message queue of saved messages.
100 void ClearMessages();
101
102 // Returns the message at the given index in the queue. The index may be out
103 // of range, in which case the return value is NULL. The returned pointer will
104 // only be valid until another message is received or the list is cleared.
105 const Message* GetMessageAt(size_t index) const;
106
107 // Returns the first message with the given ID in the queue. If there is no
108 // message with the given ID, returns NULL. The returned pointer will only be
109 // valid until another message is received or the list is cleared.
110 const Message* GetFirstMessageMatching(uint32 id) const;
111
112 // Returns the message with the given ID in the queue. If there is no such
113 // message or there is more than one of that message, this will return NULL
114 // (with the expectation that you'll do an ASSERT_TRUE() on the result).
115 // The returned pointer will only be valid until another message is received
116 // or the list is cleared.
117 const Message* GetUniqueMessageMatching(uint32 id) const;
118
bryner@chromium.org44ae58f2011-02-01 06:43:31 +0900119 // Adds the given listener as a filter to the TestSink.
120 // When a message is received by the TestSink, it will be dispatched to
121 // the filters, in the order they were added. If a filter returns true
122 // from OnMessageReceived, subsequent filters will not receive the message
123 // and the TestSink will not store it.
brettw@chromium.orgdb1259e2012-06-30 07:05:26 +0900124 void AddFilter(Listener* filter);
bryner@chromium.org44ae58f2011-02-01 06:43:31 +0900125
126 // Removes the given filter from the TestSink.
brettw@chromium.orgdb1259e2012-06-30 07:05:26 +0900127 void RemoveFilter(Listener* filter);
bryner@chromium.org44ae58f2011-02-01 06:43:31 +0900128
brettw@google.comc0f85582011-01-21 08:50:27 +0900129 private:
130 // The actual list of received messages.
131 std::vector<Message> messages_;
brettw@chromium.orgdb1259e2012-06-30 07:05:26 +0900132 ObserverList<Listener> filter_list_;
brettw@google.comc0f85582011-01-21 08:50:27 +0900133
134 DISALLOW_COPY_AND_ASSIGN(TestSink);
135};
136
137} // namespace IPC
138
139#endif // IPC_IPC_TEST_SINK_H_