[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] | a16ed65e | 2009-02-14 01:35:27 | [diff] [blame] | 7 | |
avi | 246998d8 | 2015-12-22 02:39:04 | [diff] [blame] | 8 | #include <stddef.h> |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 9 | #include <stdint.h> |
| 10 | |
[email protected] | a16ed65e | 2009-02-14 01:35:27 | [diff] [blame] | 11 | #include <utility> |
| 12 | #include <vector> |
| 13 | |
[email protected] | 9f9db89 | 2011-01-31 21:43:31 | [diff] [blame] | 14 | #include "base/observer_list.h" |
avi | 246998d8 | 2015-12-22 02:39:04 | [diff] [blame] | 15 | #include "build/build_config.h" |
[email protected] | 90b721e6 | 2010-04-05 17:35:01 | [diff] [blame] | 16 | #include "ipc/ipc_channel.h" |
[email protected] | a16ed65e | 2009-02-14 01:35:27 | [diff] [blame] | 17 | |
| 18 | namespace IPC { |
| 19 | |
[email protected] | a83d4229 | 2010-08-17 22:51:10 | [diff] [blame] | 20 | class Message; |
| 21 | |
[email protected] | a16ed65e | 2009-02-14 01:35:27 | [diff] [blame] | 22 | // This test sink provides a "sink" for IPC messages that are sent. It allows |
| 23 | // the caller to query messages received in various different ways. It is |
| 24 | // designed for tests for objects that use the IPC system. |
| 25 | // |
| 26 | // Typical usage: |
| 27 | // |
| 28 | // test_sink.ClearMessages(); |
| 29 | // do_something(); |
| 30 | // |
| 31 | // // We should have gotten exactly one update state message. |
| 32 | // EXPECT_TRUE(test_sink.GetUniqeMessageMatching(ViewHostMsg_Update::ID)); |
| 33 | // // ...and no start load messages. |
| 34 | // EXPECT_FALSE(test_sink.GetFirstMessageMatching(ViewHostMsg_Start::ID)); |
| 35 | // |
| 36 | // // Now inspect a message. This assumes a message that was declared like |
| 37 | // // this: IPC_MESSAGE_ROUTED2(ViewMsg_Foo, bool, int) |
| 38 | // IPC::Message* msg = test_sink.GetFirstMessageMatching(ViewMsg_Foo::ID)); |
| 39 | // ASSERT_TRUE(msg); |
| 40 | // bool first_param; |
| 41 | // int second_param; |
| 42 | // ViewMsg_Foo::Read(msg, &first_param, &second_param); |
| 43 | // |
| 44 | // // Go on to the next phase of the test. |
| 45 | // test_sink.ClearMessages(); |
| 46 | // |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 47 | // To read a sync reply, do this: |
| 48 | // |
| 49 | // IPC::Message* msg = test_sink.GetUniqueMessageMatching(IPC_REPLY_ID); |
| 50 | // ASSERT_TRUE(msg); |
brettw | d5ca2bc | 2015-05-29 22:15:47 | [diff] [blame] | 51 | // base::TupleTypes<ViewHostMsg_Foo::ReplyParam>::ValueTuple reply_data; |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 52 | // EXPECT_TRUE(ViewHostMsg_Foo::ReadReplyParam(msg, &reply_data)); |
| 53 | // |
[email protected] | 9f9db89 | 2011-01-31 21:43:31 | [diff] [blame] | 54 | // You can also register to be notified when messages are posted to the sink. |
| 55 | // This can be useful if you need to wait for a particular message that will |
| 56 | // be posted asynchronously. Example usage: |
| 57 | // |
[email protected] | b7f59e82 | 2012-06-29 22:05:26 | [diff] [blame] | 58 | // class MyListener : public IPC::Listener { |
[email protected] | 9f9db89 | 2011-01-31 21:43:31 | [diff] [blame] | 59 | // public: |
Alex Turner | 02b697a | 2020-10-28 22:37:13 | [diff] [blame] | 60 | // MyListener(const base::RepeatingClosure& closure) |
kotenkov | b2bb5e98 | 2016-03-16 20:50:05 | [diff] [blame] | 61 | // : message_received_closure_(closure) {} |
[email protected] | 9f9db89 | 2011-01-31 21:43:31 | [diff] [blame] | 62 | // virtual bool OnMessageReceived(const IPC::Message& msg) { |
| 63 | // <do something with the message> |
kotenkov | b2bb5e98 | 2016-03-16 20:50:05 | [diff] [blame] | 64 | // message_received_closure_.Run(); |
[email protected] | 9f9db89 | 2011-01-31 21:43:31 | [diff] [blame] | 65 | // return false; // to store the message in the sink, or true to drop it |
| 66 | // } |
kotenkov | b2bb5e98 | 2016-03-16 20:50:05 | [diff] [blame] | 67 | // private: |
Alex Turner | 02b697a | 2020-10-28 22:37:13 | [diff] [blame] | 68 | // base::RepeatingClosure message_received_closure_; |
[email protected] | 9f9db89 | 2011-01-31 21:43:31 | [diff] [blame] | 69 | // }; |
| 70 | // |
kotenkov | b2bb5e98 | 2016-03-16 20:50:05 | [diff] [blame] | 71 | // base::RunLoop run_loop; |
| 72 | // MyListener listener(run_loop.QuitClosure()); |
[email protected] | 9f9db89 | 2011-01-31 21:43:31 | [diff] [blame] | 73 | // test_sink.AddFilter(&listener); |
| 74 | // StartSomeAsynchronousProcess(&test_sink); |
kotenkov | b2bb5e98 | 2016-03-16 20:50:05 | [diff] [blame] | 75 | // run_loop.Run(); |
[email protected] | 9f9db89 | 2011-01-31 21:43:31 | [diff] [blame] | 76 | // <inspect the results> |
| 77 | // ... |
| 78 | // |
[email protected] | a16ed65e | 2009-02-14 01:35:27 | [diff] [blame] | 79 | // 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] | 80 | // message is received. |
[email protected] | a576851 | 2013-04-12 19:35:35 | [diff] [blame] | 81 | class TestSink : public Channel { |
[email protected] | a16ed65e | 2009-02-14 01:35:27 | [diff] [blame] | 82 | public: |
| 83 | TestSink(); |
Peter Boström | c68c5aa | 2021-09-28 00:28:00 | [diff] [blame] | 84 | |
| 85 | TestSink(const TestSink&) = delete; |
| 86 | TestSink& operator=(const TestSink&) = delete; |
| 87 | |
dcheng | fe61fca | 2014-10-22 02:29:52 | [diff] [blame] | 88 | ~TestSink() override; |
[email protected] | a16ed65e | 2009-02-14 01:35:27 | [diff] [blame] | 89 | |
[email protected] | 90b721e6 | 2010-04-05 17:35:01 | [diff] [blame] | 90 | // Interface in IPC::Channel. This copies the message to the sink and then |
| 91 | // deletes it. |
dcheng | fe61fca | 2014-10-22 02:29:52 | [diff] [blame] | 92 | bool Send(IPC::Message* message) override; |
Daniel Cheng | e618e80 | 2022-01-14 18:56:52 | [diff] [blame] | 93 | [[nodiscard]] bool Connect() override; |
dcheng | fe61fca | 2014-10-22 02:29:52 | [diff] [blame] | 94 | void Close() override; |
[email protected] | 90b721e6 | 2010-04-05 17:35:01 | [diff] [blame] | 95 | |
[email protected] | a16ed65e | 2009-02-14 01:35:27 | [diff] [blame] | 96 | // Used by the source of the messages to send the message to the sink. This |
| 97 | // will make a copy of the message and store it in the list. |
[email protected] | a576851 | 2013-04-12 19:35:35 | [diff] [blame] | 98 | bool OnMessageReceived(const Message& msg); |
[email protected] | a16ed65e | 2009-02-14 01:35:27 | [diff] [blame] | 99 | |
| 100 | // Returns the number of messages in the queue. |
| 101 | size_t message_count() const { return messages_.size(); } |
| 102 | |
| 103 | // Clears the message queue of saved messages. |
| 104 | void ClearMessages(); |
| 105 | |
| 106 | // Returns the message at the given index in the queue. The index may be out |
| 107 | // of range, in which case the return value is NULL. The returned pointer will |
| 108 | // only be valid until another message is received or the list is cleared. |
| 109 | const Message* GetMessageAt(size_t index) const; |
| 110 | |
| 111 | // Returns the first message with the given ID in the queue. If there is no |
| 112 | // message with the given ID, returns NULL. The returned pointer will only be |
| 113 | // valid until another message is received or the list is cleared. |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 114 | const Message* GetFirstMessageMatching(uint32_t id) const; |
[email protected] | a16ed65e | 2009-02-14 01:35:27 | [diff] [blame] | 115 | |
| 116 | // Returns the message with the given ID in the queue. If there is no such |
| 117 | // message or there is more than one of that message, this will return NULL |
| 118 | // (with the expectation that you'll do an ASSERT_TRUE() on the result). |
| 119 | // The returned pointer will only be valid until another message is received |
| 120 | // or the list is cleared. |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 121 | const Message* GetUniqueMessageMatching(uint32_t id) const; |
[email protected] | a16ed65e | 2009-02-14 01:35:27 | [diff] [blame] | 122 | |
[email protected] | 9f9db89 | 2011-01-31 21:43:31 | [diff] [blame] | 123 | // Adds the given listener as a filter to the TestSink. |
| 124 | // When a message is received by the TestSink, it will be dispatched to |
| 125 | // the filters, in the order they were added. If a filter returns true |
| 126 | // from OnMessageReceived, subsequent filters will not receive the message |
| 127 | // and the TestSink will not store it. |
[email protected] | b7f59e82 | 2012-06-29 22:05:26 | [diff] [blame] | 128 | void AddFilter(Listener* filter); |
[email protected] | 9f9db89 | 2011-01-31 21:43:31 | [diff] [blame] | 129 | |
| 130 | // Removes the given filter from the TestSink. |
[email protected] | b7f59e82 | 2012-06-29 22:05:26 | [diff] [blame] | 131 | void RemoveFilter(Listener* filter); |
[email protected] | 9f9db89 | 2011-01-31 21:43:31 | [diff] [blame] | 132 | |
[email protected] | a16ed65e | 2009-02-14 01:35:27 | [diff] [blame] | 133 | private: |
| 134 | // The actual list of received messages. |
| 135 | std::vector<Message> messages_; |
Trent Apted | a250ec3ab | 2018-08-19 08:52:19 | [diff] [blame] | 136 | base::ObserverList<Listener>::Unchecked filter_list_; |
[email protected] | a16ed65e | 2009-02-14 01:35:27 | [diff] [blame] | 137 | }; |
| 138 | |
| 139 | } // namespace IPC |
| 140 | |
[email protected] | 3ff2a10 | 2011-01-20 23:50:27 | [diff] [blame] | 141 | #endif // IPC_IPC_TEST_SINK_H_ |