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 #include "ipc/ipc_test_sink.h"
7 #include "ipc/ipc_listener.h"
8 #include "ipc/ipc_message.h"
12 TestSink::TestSink() {
15 TestSink::~TestSink() {
18 bool TestSink::Send(Message
* message
) {
19 OnMessageReceived(*message
);
24 bool TestSink::OnMessageReceived(const Message
& msg
) {
25 ObserverListBase
<Listener
>::Iterator
it(filter_list_
);
27 while ((observer
= it
.GetNext()) != NULL
) {
28 if (observer
->OnMessageReceived(msg
))
32 // No filter handled the message, so store it.
33 messages_
.push_back(Message(msg
));
37 void TestSink::ClearMessages() {
41 const Message
* TestSink::GetMessageAt(size_t index
) const {
42 if (index
>= messages_
.size())
44 return &messages_
[index
];
47 const Message
* TestSink::GetFirstMessageMatching(uint32 id
) const {
48 for (size_t i
= 0; i
< messages_
.size(); i
++) {
49 if (messages_
[i
].type() == id
)
55 const Message
* TestSink::GetUniqueMessageMatching(uint32 id
) const {
56 size_t found_index
= 0;
57 size_t found_count
= 0;
58 for (size_t i
= 0; i
< messages_
.size(); i
++) {
59 if (messages_
[i
].type() == id
) {
65 return NULL
; // Didn't find a unique one.
66 return &messages_
[found_index
];
69 void TestSink::AddFilter(Listener
* filter
) {
70 filter_list_
.AddObserver(filter
);
73 void TestSink::RemoveFilter(Listener
* filter
) {
74 filter_list_
.RemoveObserver(filter
);