OLD | NEW |
(Empty) | |
| 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 // // Go on to the next phase of the test. |
| 31 // test_sink.ClearMessages(); |
| 32 // |
| 33 // To hook up the sink, all you need to do is call OnMessageReceived when a |
| 34 // message is recieved. |
| 35 class TestSink { |
| 36 public: |
| 37 TestSink(); |
| 38 ~TestSink(); |
| 39 |
| 40 // Used by the source of the messages to send the message to the sink. This |
| 41 // will make a copy of the message and store it in the list. |
| 42 void OnMessageReceived(const Message& msg); |
| 43 |
| 44 // Returns the number of messages in the queue. |
| 45 size_t message_count() const { return messages_.size(); } |
| 46 |
| 47 // Clears the message queue of saved messages. |
| 48 void ClearMessages(); |
| 49 |
| 50 // Returns the message at the given index in the queue. The index may be out |
| 51 // of range, in which case the return value is NULL. The returned pointer will |
| 52 // only be valid until another message is received or the list is cleared. |
| 53 const Message* GetMessageAt(size_t index) const; |
| 54 |
| 55 // Returns the first message with the given ID in the queue. If there is no |
| 56 // message with the given ID, returns NULL. The returned pointer will only be |
| 57 // valid until another message is received or the list is cleared. |
| 58 const Message* GetFirstMessageMatching(uint16 id) const; |
| 59 |
| 60 // Returns the message with the given ID in the queue. If there is no such |
| 61 // message or there is more than one of that message, this will return NULL |
| 62 // (with the expectation that you'll do an ASSERT_TRUE() on the result). |
| 63 // The returned pointer will only be valid until another message is received |
| 64 // or the list is cleared. |
| 65 const Message* GetUniqueMessageMatching(uint16 id) const; |
| 66 |
| 67 private: |
| 68 // The actual list of received messages. |
| 69 std::vector<Message> messages_; |
| 70 |
| 71 DISALLOW_COPY_AND_ASSIGN(TestSink); |
| 72 }; |
| 73 |
| 74 } // namespace IPC |
| 75 |
| 76 #endif // CHROME_COMMON_IPC_TEST_SINK_H_ |
OLD | NEW |