[email protected] | a16ed65e | 2009-02-14 01:35:27 | [diff] [blame^] | 1 | // Copyright (c) 2009 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 CHROME_COMMON_IPC_TEST_SINK_H_ |
| 6 | #define CHROME_COMMON_IPC_TEST_SINK_H_ |
| 7 | |
| 8 | #include <utility> |
| 9 | #include <vector> |
| 10 | |
| 11 | #include "base/basictypes.h" |
| 12 | #include "chrome/common/ipc_message.h" |
| 13 | |
| 14 | namespace IPC { |
| 15 | |
| 16 | // This test sink provides a "sink" for IPC messages that are sent. It allows |
| 17 | // the caller to query messages received in various different ways. It is |
| 18 | // designed for tests for objects that use the IPC system. |
| 19 | // |
| 20 | // Typical usage: |
| 21 | // |
| 22 | // test_sink.ClearMessages(); |
| 23 | // do_something(); |
| 24 | // |
| 25 | // // We should have gotten exactly one update state message. |
| 26 | // EXPECT_TRUE(test_sink.GetUniqeMessageMatching(ViewHostMsg_Update::ID)); |
| 27 | // // ...and no start load messages. |
| 28 | // EXPECT_FALSE(test_sink.GetFirstMessageMatching(ViewHostMsg_Start::ID)); |
| 29 | // |
| 30 | // // Now inspect a message. This assumes a message that was declared like |
| 31 | // // this: IPC_MESSAGE_ROUTED2(ViewMsg_Foo, bool, int) |
| 32 | // IPC::Message* msg = test_sink.GetFirstMessageMatching(ViewMsg_Foo::ID)); |
| 33 | // ASSERT_TRUE(msg); |
| 34 | // bool first_param; |
| 35 | // int second_param; |
| 36 | // ViewMsg_Foo::Read(msg, &first_param, &second_param); |
| 37 | // |
| 38 | // // Go on to the next phase of the test. |
| 39 | // test_sink.ClearMessages(); |
| 40 | // |
| 41 | // To hook up the sink, all you need to do is call OnMessageReceived when a |
| 42 | // message is recieved. |
| 43 | class TestSink { |
| 44 | public: |
| 45 | TestSink(); |
| 46 | ~TestSink(); |
| 47 | |
| 48 | // Used by the source of the messages to send the message to the sink. This |
| 49 | // will make a copy of the message and store it in the list. |
| 50 | void OnMessageReceived(const Message& msg); |
| 51 | |
| 52 | // Returns the number of messages in the queue. |
| 53 | size_t message_count() const { return messages_.size(); } |
| 54 | |
| 55 | // Clears the message queue of saved messages. |
| 56 | void ClearMessages(); |
| 57 | |
| 58 | // Returns the message at the given index in the queue. The index may be out |
| 59 | // of range, in which case the return value is NULL. The returned pointer will |
| 60 | // only be valid until another message is received or the list is cleared. |
| 61 | const Message* GetMessageAt(size_t index) const; |
| 62 | |
| 63 | // Returns the first message with the given ID in the queue. If there is no |
| 64 | // message with the given ID, returns NULL. The returned pointer will only be |
| 65 | // valid until another message is received or the list is cleared. |
| 66 | const Message* GetFirstMessageMatching(uint16 id) const; |
| 67 | |
| 68 | // Returns the message with the given ID in the queue. If there is no such |
| 69 | // message or there is more than one of that message, this will return NULL |
| 70 | // (with the expectation that you'll do an ASSERT_TRUE() on the result). |
| 71 | // The returned pointer will only be valid until another message is received |
| 72 | // or the list is cleared. |
| 73 | const Message* GetUniqueMessageMatching(uint16 id) const; |
| 74 | |
| 75 | private: |
| 76 | // The actual list of received messages. |
| 77 | std::vector<Message> messages_; |
| 78 | |
| 79 | DISALLOW_COPY_AND_ASSIGN(TestSink); |
| 80 | }; |
| 81 | |
| 82 | } // namespace IPC |
| 83 | |
| 84 | #endif // CHROME_COMMON_IPC_TEST_SINK_H_ |