[email protected] | 3ff2a10 | 2011-01-20 23:50:27 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | a16ed65e | 2009-02-14 01:35:27 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 3ff2a10 | 2011-01-20 23:50:27 | [diff] [blame] | 5 | #ifndef IPC_IPC_TEST_SINK_H_ |
| 6 | #define IPC_IPC_TEST_SINK_H_ |
[email protected] | 32b76ef | 2010-07-26 23:08:24 | [diff] [blame] | 7 | #pragma once |
[email protected] | a16ed65e | 2009-02-14 01:35:27 | [diff] [blame] | 8 | |
| 9 | #include <utility> |
| 10 | #include <vector> |
| 11 | |
| 12 | #include "base/basictypes.h" |
[email protected] | 9f9db89 | 2011-01-31 21:43:31 | [diff] [blame^] | 13 | #include "base/observer_list.h" |
[email protected] | 90b721e6 | 2010-04-05 17:35:01 | [diff] [blame] | 14 | #include "ipc/ipc_channel.h" |
[email protected] | a16ed65e | 2009-02-14 01:35:27 | [diff] [blame] | 15 | |
| 16 | namespace IPC { |
| 17 | |
[email protected] | a83d4229 | 2010-08-17 22:51:10 | [diff] [blame] | 18 | class Message; |
| 19 | |
[email protected] | a16ed65e | 2009-02-14 01:35:27 | [diff] [blame] | 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. |
| 23 | // |
| 24 | // Typical usage: |
| 25 | // |
| 26 | // test_sink.ClearMessages(); |
| 27 | // do_something(); |
| 28 | // |
| 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)); |
| 33 | // |
| 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); |
| 41 | // |
| 42 | // // Go on to the next phase of the test. |
| 43 | // test_sink.ClearMessages(); |
| 44 | // |
[email protected] | 9f9db89 | 2011-01-31 21:43:31 | [diff] [blame^] | 45 | // You can also register to be notified when messages are posted to the sink. |
| 46 | // This can be useful if you need to wait for a particular message that will |
| 47 | // be posted asynchronously. Example usage: |
| 48 | // |
| 49 | // class MyListener : public IPC::Channel::Listener { |
| 50 | // public: |
| 51 | // virtual bool OnMessageReceived(const IPC::Message& msg) { |
| 52 | // <do something with the message> |
| 53 | // MessageLoop::current()->Quit(); |
| 54 | // return false; // to store the message in the sink, or true to drop it |
| 55 | // } |
| 56 | // }; |
| 57 | // |
| 58 | // MyListener listener; |
| 59 | // test_sink.AddFilter(&listener); |
| 60 | // StartSomeAsynchronousProcess(&test_sink); |
| 61 | // MessageLoop::current()->Run(); |
| 62 | // <inspect the results> |
| 63 | // ... |
| 64 | // |
[email protected] | a16ed65e | 2009-02-14 01:35:27 | [diff] [blame] | 65 | // To hook up the sink, all you need to do is call OnMessageReceived when a |
[email protected] | ad76da7 | 2010-03-24 00:27:18 | [diff] [blame] | 66 | // message is received. |
[email protected] | 3ff2a10 | 2011-01-20 23:50:27 | [diff] [blame] | 67 | class TestSink : public Channel { |
[email protected] | a16ed65e | 2009-02-14 01:35:27 | [diff] [blame] | 68 | public: |
| 69 | TestSink(); |
| 70 | ~TestSink(); |
| 71 | |
[email protected] | 90b721e6 | 2010-04-05 17:35:01 | [diff] [blame] | 72 | // Interface in IPC::Channel. This copies the message to the sink and then |
| 73 | // deletes it. |
| 74 | virtual bool Send(IPC::Message* message); |
| 75 | |
[email protected] | a16ed65e | 2009-02-14 01:35:27 | [diff] [blame] | 76 | // Used by the source of the messages to send the message to the sink. This |
| 77 | // will make a copy of the message and store it in the list. |
[email protected] | 6db8d990 | 2010-12-24 08:36:25 | [diff] [blame] | 78 | bool OnMessageReceived(const Message& msg); |
[email protected] | a16ed65e | 2009-02-14 01:35:27 | [diff] [blame] | 79 | |
| 80 | // Returns the number of messages in the queue. |
| 81 | size_t message_count() const { return messages_.size(); } |
| 82 | |
| 83 | // Clears the message queue of saved messages. |
| 84 | void ClearMessages(); |
| 85 | |
| 86 | // Returns the message at the given index in the queue. The index may be out |
| 87 | // of range, in which case the return value is NULL. The returned pointer will |
| 88 | // only be valid until another message is received or the list is cleared. |
| 89 | const Message* GetMessageAt(size_t index) const; |
| 90 | |
| 91 | // Returns the first message with the given ID in the queue. If there is no |
| 92 | // message with the given ID, returns NULL. The returned pointer will only be |
| 93 | // valid until another message is received or the list is cleared. |
[email protected] | 168ae92 | 2009-12-04 18:08:45 | [diff] [blame] | 94 | const Message* GetFirstMessageMatching(uint32 id) const; |
[email protected] | a16ed65e | 2009-02-14 01:35:27 | [diff] [blame] | 95 | |
| 96 | // Returns the message with the given ID in the queue. If there is no such |
| 97 | // message or there is more than one of that message, this will return NULL |
| 98 | // (with the expectation that you'll do an ASSERT_TRUE() on the result). |
| 99 | // The returned pointer will only be valid until another message is received |
| 100 | // or the list is cleared. |
[email protected] | 168ae92 | 2009-12-04 18:08:45 | [diff] [blame] | 101 | const Message* GetUniqueMessageMatching(uint32 id) const; |
[email protected] | a16ed65e | 2009-02-14 01:35:27 | [diff] [blame] | 102 | |
[email protected] | 9f9db89 | 2011-01-31 21:43:31 | [diff] [blame^] | 103 | // Adds the given listener as a filter to the TestSink. |
| 104 | // When a message is received by the TestSink, it will be dispatched to |
| 105 | // the filters, in the order they were added. If a filter returns true |
| 106 | // from OnMessageReceived, subsequent filters will not receive the message |
| 107 | // and the TestSink will not store it. |
| 108 | void AddFilter(Channel::Listener* filter); |
| 109 | |
| 110 | // Removes the given filter from the TestSink. |
| 111 | void RemoveFilter(Channel::Listener* filter); |
| 112 | |
[email protected] | a16ed65e | 2009-02-14 01:35:27 | [diff] [blame] | 113 | private: |
| 114 | // The actual list of received messages. |
| 115 | std::vector<Message> messages_; |
[email protected] | 9f9db89 | 2011-01-31 21:43:31 | [diff] [blame^] | 116 | ObserverList<Channel::Listener> filter_list_; |
[email protected] | a16ed65e | 2009-02-14 01:35:27 | [diff] [blame] | 117 | |
| 118 | DISALLOW_COPY_AND_ASSIGN(TestSink); |
| 119 | }; |
| 120 | |
| 121 | } // namespace IPC |
| 122 | |
[email protected] | 3ff2a10 | 2011-01-20 23:50:27 | [diff] [blame] | 123 | #endif // IPC_IPC_TEST_SINK_H_ |