Removes some dead accessibility code.
[chromium-blink-merge.git] / ipc / ipc_test_sink.h
blobad1125a3fc571d61db71ae87c399ca8e68df938d
1 // 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.
5 #ifndef IPC_IPC_TEST_SINK_H_
6 #define IPC_IPC_TEST_SINK_H_
7 #pragma once
9 #include <utility>
10 #include <vector>
12 #include "base/basictypes.h"
13 #include "base/observer_list.h"
14 #include "ipc/ipc_channel.h"
16 namespace IPC {
18 class Message;
20 // This test sink provides a "sink" for IPC messages that are sent. It allows
21 // the caller to query messages received in various different ways. It is
22 // designed for tests for objects that use the IPC system.
24 // Typical usage:
26 // test_sink.ClearMessages();
27 // do_something();
29 // // We should have gotten exactly one update state message.
30 // EXPECT_TRUE(test_sink.GetUniqeMessageMatching(ViewHostMsg_Update::ID));
31 // // ...and no start load messages.
32 // EXPECT_FALSE(test_sink.GetFirstMessageMatching(ViewHostMsg_Start::ID));
34 // // Now inspect a message. This assumes a message that was declared like
35 // // this: IPC_MESSAGE_ROUTED2(ViewMsg_Foo, bool, int)
36 // IPC::Message* msg = test_sink.GetFirstMessageMatching(ViewMsg_Foo::ID));
37 // ASSERT_TRUE(msg);
38 // bool first_param;
39 // int second_param;
40 // ViewMsg_Foo::Read(msg, &first_param, &second_param);
42 // // Go on to the next phase of the test.
43 // test_sink.ClearMessages();
45 // To read a sync reply, do this:
47 // IPC::Message* msg = test_sink.GetUniqueMessageMatching(IPC_REPLY_ID);
48 // ASSERT_TRUE(msg);
49 // TupleTypes<ViewHostMsg_Foo::ReplyParam>::ValueTuple reply_data;
50 // EXPECT_TRUE(ViewHostMsg_Foo::ReadReplyParam(msg, &reply_data));
52 // You can also register to be notified when messages are posted to the sink.
53 // This can be useful if you need to wait for a particular message that will
54 // be posted asynchronously. Example usage:
56 // class MyListener : public IPC::Channel::Listener {
57 // public:
58 // virtual bool OnMessageReceived(const IPC::Message& msg) {
59 // <do something with the message>
60 // MessageLoop::current()->Quit();
61 // return false; // to store the message in the sink, or true to drop it
62 // }
63 // };
65 // MyListener listener;
66 // test_sink.AddFilter(&listener);
67 // StartSomeAsynchronousProcess(&test_sink);
68 // MessageLoop::current()->Run();
69 // <inspect the results>
70 // ...
72 // To hook up the sink, all you need to do is call OnMessageReceived when a
73 // message is received.
74 class TestSink : public Channel {
75 public:
76 TestSink();
77 virtual ~TestSink();
79 // Interface in IPC::Channel. This copies the message to the sink and then
80 // deletes it.
81 virtual bool Send(IPC::Message* message);
83 // Used by the source of the messages to send the message to the sink. This
84 // will make a copy of the message and store it in the list.
85 bool OnMessageReceived(const Message& msg);
87 // Returns the number of messages in the queue.
88 size_t message_count() const { return messages_.size(); }
90 // Clears the message queue of saved messages.
91 void ClearMessages();
93 // Returns the message at the given index in the queue. The index may be out
94 // of range, in which case the return value is NULL. The returned pointer will
95 // only be valid until another message is received or the list is cleared.
96 const Message* GetMessageAt(size_t index) const;
98 // Returns the first message with the given ID in the queue. If there is no
99 // message with the given ID, returns NULL. The returned pointer will only be
100 // valid until another message is received or the list is cleared.
101 const Message* GetFirstMessageMatching(uint32 id) const;
103 // Returns the message with the given ID in the queue. If there is no such
104 // message or there is more than one of that message, this will return NULL
105 // (with the expectation that you'll do an ASSERT_TRUE() on the result).
106 // The returned pointer will only be valid until another message is received
107 // or the list is cleared.
108 const Message* GetUniqueMessageMatching(uint32 id) const;
110 // Adds the given listener as a filter to the TestSink.
111 // When a message is received by the TestSink, it will be dispatched to
112 // the filters, in the order they were added. If a filter returns true
113 // from OnMessageReceived, subsequent filters will not receive the message
114 // and the TestSink will not store it.
115 void AddFilter(Channel::Listener* filter);
117 // Removes the given filter from the TestSink.
118 void RemoveFilter(Channel::Listener* filter);
120 private:
121 // The actual list of received messages.
122 std::vector<Message> messages_;
123 ObserverList<Channel::Listener> filter_list_;
125 DISALLOW_COPY_AND_ASSIGN(TestSink);
128 } // namespace IPC
130 #endif // IPC_IPC_TEST_SINK_H_