OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef IPC_IPC_TEST_SINK_H_ | 5 #ifndef IPC_IPC_TEST_SINK_H_ |
6 #define IPC_IPC_TEST_SINK_H_ | 6 #define IPC_IPC_TEST_SINK_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <utility> | 9 #include <utility> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
| 13 #include "base/observer_list.h" |
13 #include "ipc/ipc_channel.h" | 14 #include "ipc/ipc_channel.h" |
14 | 15 |
15 namespace IPC { | 16 namespace IPC { |
16 | 17 |
17 class Message; | 18 class Message; |
18 | 19 |
19 // This test sink provides a "sink" for IPC messages that are sent. It allows | 20 // 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 // the caller to query messages received in various different ways. It is |
21 // designed for tests for objects that use the IPC system. | 22 // designed for tests for objects that use the IPC system. |
22 // | 23 // |
(...skipping 11 matching lines...) Expand all Loading... |
34 // // this: IPC_MESSAGE_ROUTED2(ViewMsg_Foo, bool, int) | 35 // // this: IPC_MESSAGE_ROUTED2(ViewMsg_Foo, bool, int) |
35 // IPC::Message* msg = test_sink.GetFirstMessageMatching(ViewMsg_Foo::ID)); | 36 // IPC::Message* msg = test_sink.GetFirstMessageMatching(ViewMsg_Foo::ID)); |
36 // ASSERT_TRUE(msg); | 37 // ASSERT_TRUE(msg); |
37 // bool first_param; | 38 // bool first_param; |
38 // int second_param; | 39 // int second_param; |
39 // ViewMsg_Foo::Read(msg, &first_param, &second_param); | 40 // ViewMsg_Foo::Read(msg, &first_param, &second_param); |
40 // | 41 // |
41 // // Go on to the next phase of the test. | 42 // // Go on to the next phase of the test. |
42 // test_sink.ClearMessages(); | 43 // test_sink.ClearMessages(); |
43 // | 44 // |
| 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 // |
44 // To hook up the sink, all you need to do is call OnMessageReceived when a | 65 // To hook up the sink, all you need to do is call OnMessageReceived when a |
45 // message is received. | 66 // message is received. |
46 class TestSink : public Channel { | 67 class TestSink : public Channel { |
47 public: | 68 public: |
48 TestSink(); | 69 TestSink(); |
49 ~TestSink(); | 70 ~TestSink(); |
50 | 71 |
51 // Interface in IPC::Channel. This copies the message to the sink and then | 72 // Interface in IPC::Channel. This copies the message to the sink and then |
52 // deletes it. | 73 // deletes it. |
53 virtual bool Send(IPC::Message* message); | 74 virtual bool Send(IPC::Message* message); |
(...skipping 18 matching lines...) Expand all Loading... |
72 // valid until another message is received or the list is cleared. | 93 // valid until another message is received or the list is cleared. |
73 const Message* GetFirstMessageMatching(uint32 id) const; | 94 const Message* GetFirstMessageMatching(uint32 id) const; |
74 | 95 |
75 // Returns the message with the given ID in the queue. If there is no such | 96 // 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 | 97 // 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). | 98 // (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 | 99 // The returned pointer will only be valid until another message is received |
79 // or the list is cleared. | 100 // or the list is cleared. |
80 const Message* GetUniqueMessageMatching(uint32 id) const; | 101 const Message* GetUniqueMessageMatching(uint32 id) const; |
81 | 102 |
| 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 |
82 private: | 113 private: |
83 // The actual list of received messages. | 114 // The actual list of received messages. |
84 std::vector<Message> messages_; | 115 std::vector<Message> messages_; |
| 116 ObserverList<Channel::Listener> filter_list_; |
85 | 117 |
86 DISALLOW_COPY_AND_ASSIGN(TestSink); | 118 DISALLOW_COPY_AND_ASSIGN(TestSink); |
87 }; | 119 }; |
88 | 120 |
89 } // namespace IPC | 121 } // namespace IPC |
90 | 122 |
91 #endif // IPC_IPC_TEST_SINK_H_ | 123 #endif // IPC_IPC_TEST_SINK_H_ |
OLD | NEW |