Implement SimulateMatchingPhase by real calls to PasswordFormManager::FetchMatchingLo...
[chromium-blink-merge.git] / ipc / ipc_test_sink.h
blob1a11f40fb2d2e5aeb12f68a31a5fdb63b5b10bea
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
3 // found in the LICENSE file.
5 #ifndef IPC_IPC_TEST_SINK_H_
6 #define IPC_IPC_TEST_SINK_H_
8 #include <stdint.h>
10 #include <utility>
11 #include <vector>
13 #include "base/basictypes.h"
14 #include "base/observer_list.h"
15 #include "ipc/ipc_channel.h"
17 namespace IPC {
19 class Message;
21 // This test sink provides a "sink" for IPC messages that are sent. It allows
22 // the caller to query messages received in various different ways. It is
23 // designed for tests for objects that use the IPC system.
25 // Typical usage:
27 // test_sink.ClearMessages();
28 // do_something();
30 // // We should have gotten exactly one update state message.
31 // EXPECT_TRUE(test_sink.GetUniqeMessageMatching(ViewHostMsg_Update::ID));
32 // // ...and no start load messages.
33 // EXPECT_FALSE(test_sink.GetFirstMessageMatching(ViewHostMsg_Start::ID));
35 // // Now inspect a message. This assumes a message that was declared like
36 // // this: IPC_MESSAGE_ROUTED2(ViewMsg_Foo, bool, int)
37 // IPC::Message* msg = test_sink.GetFirstMessageMatching(ViewMsg_Foo::ID));
38 // ASSERT_TRUE(msg);
39 // bool first_param;
40 // int second_param;
41 // ViewMsg_Foo::Read(msg, &first_param, &second_param);
43 // // Go on to the next phase of the test.
44 // test_sink.ClearMessages();
46 // To read a sync reply, do this:
48 // IPC::Message* msg = test_sink.GetUniqueMessageMatching(IPC_REPLY_ID);
49 // ASSERT_TRUE(msg);
50 // base::TupleTypes<ViewHostMsg_Foo::ReplyParam>::ValueTuple reply_data;
51 // EXPECT_TRUE(ViewHostMsg_Foo::ReadReplyParam(msg, &reply_data));
53 // You can also register to be notified when messages are posted to the sink.
54 // This can be useful if you need to wait for a particular message that will
55 // be posted asynchronously. Example usage:
57 // class MyListener : public IPC::Listener {
58 // public:
59 // virtual bool OnMessageReceived(const IPC::Message& msg) {
60 // <do something with the message>
61 // MessageLoop::current()->Quit();
62 // return false; // to store the message in the sink, or true to drop it
63 // }
64 // };
66 // MyListener listener;
67 // test_sink.AddFilter(&listener);
68 // StartSomeAsynchronousProcess(&test_sink);
69 // MessageLoop::current()->Run();
70 // <inspect the results>
71 // ...
73 // To hook up the sink, all you need to do is call OnMessageReceived when a
74 // message is received.
75 class TestSink : public Channel {
76 public:
77 TestSink();
78 ~TestSink() override;
80 // Interface in IPC::Channel. This copies the message to the sink and then
81 // deletes it.
82 bool Send(IPC::Message* message) override;
83 bool Connect() override WARN_UNUSED_RESULT;
84 void Close() override;
85 base::ProcessId GetPeerPID() const override;
86 base::ProcessId GetSelfPID() const override;
88 #if defined(OS_POSIX) && !defined(OS_NACL)
89 int GetClientFileDescriptor() const override;
90 base::ScopedFD TakeClientFileDescriptor() override;
91 #endif // defined(OS_POSIX) && !defined(OS_NACL)
93 // Used by the source of the messages to send the message to the sink. This
94 // will make a copy of the message and store it in the list.
95 bool OnMessageReceived(const Message& msg);
97 // Returns the number of messages in the queue.
98 size_t message_count() const { return messages_.size(); }
100 // Clears the message queue of saved messages.
101 void ClearMessages();
103 // Returns the message at the given index in the queue. The index may be out
104 // of range, in which case the return value is NULL. The returned pointer will
105 // only be valid until another message is received or the list is cleared.
106 const Message* GetMessageAt(size_t index) const;
108 // Returns the first message with the given ID in the queue. If there is no
109 // message with the given ID, returns NULL. The returned pointer will only be
110 // valid until another message is received or the list is cleared.
111 const Message* GetFirstMessageMatching(uint32_t id) const;
113 // Returns the message with the given ID in the queue. If there is no such
114 // message or there is more than one of that message, this will return NULL
115 // (with the expectation that you'll do an ASSERT_TRUE() on the result).
116 // The returned pointer will only be valid until another message is received
117 // or the list is cleared.
118 const Message* GetUniqueMessageMatching(uint32_t id) const;
120 // Adds the given listener as a filter to the TestSink.
121 // When a message is received by the TestSink, it will be dispatched to
122 // the filters, in the order they were added. If a filter returns true
123 // from OnMessageReceived, subsequent filters will not receive the message
124 // and the TestSink will not store it.
125 void AddFilter(Listener* filter);
127 // Removes the given filter from the TestSink.
128 void RemoveFilter(Listener* filter);
130 private:
131 // The actual list of received messages.
132 std::vector<Message> messages_;
133 base::ObserverList<Listener> filter_list_;
135 DISALLOW_COPY_AND_ASSIGN(TestSink);
138 } // namespace IPC
140 #endif // IPC_IPC_TEST_SINK_H_