blob: c6114f166821de69e8563125a5d76e083c62b49b [file] [log] [blame]
[email protected]3ff2a102011-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]3ff2a102011-01-20 23:50:275#ifndef IPC_IPC_TEST_SINK_H_
6#define IPC_IPC_TEST_SINK_H_
[email protected]32b76ef2010-07-26 23:08:247#pragma once
[email protected]a16ed65e2009-02-14 01:35:278
9#include <utility>
10#include <vector>
11
12#include "base/basictypes.h"
[email protected]90b721e62010-04-05 17:35:0113#include "ipc/ipc_channel.h"
[email protected]a16ed65e2009-02-14 01:35:2714
15namespace IPC {
16
[email protected]a83d42292010-08-17 22:51:1017class Message;
18
[email protected]a16ed65e2009-02-14 01:35:2719// 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//
44// To hook up the sink, all you need to do is call OnMessageReceived when a
[email protected]ad76da72010-03-24 00:27:1845// message is received.
[email protected]3ff2a102011-01-20 23:50:2746class TestSink : public Channel {
[email protected]a16ed65e2009-02-14 01:35:2747 public:
48 TestSink();
49 ~TestSink();
50
[email protected]90b721e62010-04-05 17:35:0151 // Interface in IPC::Channel. This copies the message to the sink and then
52 // deletes it.
53 virtual bool Send(IPC::Message* message);
54
[email protected]a16ed65e2009-02-14 01:35:2755 // Used by the source of the messages to send the message to the sink. This
56 // will make a copy of the message and store it in the list.
[email protected]6db8d9902010-12-24 08:36:2557 bool OnMessageReceived(const Message& msg);
[email protected]a16ed65e2009-02-14 01:35:2758
59 // Returns the number of messages in the queue.
60 size_t message_count() const { return messages_.size(); }
61
62 // Clears the message queue of saved messages.
63 void ClearMessages();
64
65 // Returns the message at the given index in the queue. The index may be out
66 // of range, in which case the return value is NULL. The returned pointer will
67 // only be valid until another message is received or the list is cleared.
68 const Message* GetMessageAt(size_t index) const;
69
70 // Returns the first message with the given ID in the queue. If there is no
71 // message with the given ID, returns NULL. The returned pointer will only be
72 // valid until another message is received or the list is cleared.
[email protected]168ae922009-12-04 18:08:4573 const Message* GetFirstMessageMatching(uint32 id) const;
[email protected]a16ed65e2009-02-14 01:35:2774
75 // Returns the message with the given ID in the queue. If there is no such
76 // message or there is more than one of that message, this will return NULL
77 // (with the expectation that you'll do an ASSERT_TRUE() on the result).
78 // The returned pointer will only be valid until another message is received
79 // or the list is cleared.
[email protected]168ae922009-12-04 18:08:4580 const Message* GetUniqueMessageMatching(uint32 id) const;
[email protected]a16ed65e2009-02-14 01:35:2781
82 private:
83 // The actual list of received messages.
84 std::vector<Message> messages_;
85
86 DISALLOW_COPY_AND_ASSIGN(TestSink);
87};
88
89} // namespace IPC
90
[email protected]3ff2a102011-01-20 23:50:2791#endif // IPC_IPC_TEST_SINK_H_