Chromium Code Reviews| 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 MyObserver : public IPC::TestSink::Observer { | |
| 50 // public: | |
|
noelutz
2011/01/28 02:13:06
nit: indentation is wrong I think.
Brian Ryner
2011/01/28 20:43:07
Done.
| |
| 51 // virtual void OnMessageReceived(const IPC::Message& msg) { | |
| 52 // <do something with the message> | |
| 53 // MessageLoop::current()->Quit(); | |
| 54 // } | |
| 55 // }; | |
| 56 // | |
| 57 // MyObserver observer; | |
| 58 // test_sink.AddObserver(&observer); | |
| 59 // StartSomeAsynchronousProcess(&test_sink); | |
| 60 // MessageLoop::current()->Run(); | |
| 61 // <inspect the results> | |
| 62 // ... | |
| 63 // | |
| 44 // To hook up the sink, all you need to do is call OnMessageReceived when a | 64 // To hook up the sink, all you need to do is call OnMessageReceived when a |
| 45 // message is received. | 65 // message is received. |
| 46 class TestSink : public Channel { | 66 class TestSink : public Channel { |
| 47 public: | 67 public: |
| 68 // Callers can register as an observer to be notified for each message | |
| 69 // that is sent to the TestSink. | |
| 70 class Observer { | |
| 71 public: | |
| 72 virtual ~Observer() {} | |
| 73 | |
| 74 // OnMessageReceived is called after each message is added to the TestSink. | |
| 75 virtual void OnMessageReceived(const Message& msg) = 0; | |
| 76 }; | |
| 77 | |
| 48 TestSink(); | 78 TestSink(); |
| 49 ~TestSink(); | 79 ~TestSink(); |
| 50 | 80 |
| 51 // Interface in IPC::Channel. This copies the message to the sink and then | 81 // Interface in IPC::Channel. This copies the message to the sink and then |
| 52 // deletes it. | 82 // deletes it. |
| 53 virtual bool Send(IPC::Message* message); | 83 virtual bool Send(IPC::Message* message); |
| 54 | 84 |
| 55 // Used by the source of the messages to send the message to the sink. This | 85 // Used by the source of the messages to send the message to the sink. This |
| 56 // will make a copy of the message and store it in the list. | 86 // will make a copy of the message and store it in the list. |
| 57 bool OnMessageReceived(const Message& msg); | 87 bool OnMessageReceived(const Message& msg); |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 72 // valid until another message is received or the list is cleared. | 102 // valid until another message is received or the list is cleared. |
| 73 const Message* GetFirstMessageMatching(uint32 id) const; | 103 const Message* GetFirstMessageMatching(uint32 id) const; |
| 74 | 104 |
| 75 // Returns the message with the given ID in the queue. If there is no such | 105 // 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 | 106 // 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). | 107 // (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 | 108 // The returned pointer will only be valid until another message is received |
| 79 // or the list is cleared. | 109 // or the list is cleared. |
| 80 const Message* GetUniqueMessageMatching(uint32 id) const; | 110 const Message* GetUniqueMessageMatching(uint32 id) const; |
| 81 | 111 |
| 112 // Adds the given observer to the TestSink. | |
| 113 void AddObserver(Observer* obs); | |
| 114 | |
| 115 // Removes the given observer from the TestSink. | |
| 116 void RemoveObserver(Observer* obs); | |
| 117 | |
| 82 private: | 118 private: |
| 83 // The actual list of received messages. | 119 // The actual list of received messages. |
| 84 std::vector<Message> messages_; | 120 std::vector<Message> messages_; |
| 121 ObserverList<Observer> observer_list_; | |
| 85 | 122 |
| 86 DISALLOW_COPY_AND_ASSIGN(TestSink); | 123 DISALLOW_COPY_AND_ASSIGN(TestSink); |
| 87 }; | 124 }; |
| 88 | 125 |
| 89 } // namespace IPC | 126 } // namespace IPC |
| 90 | 127 |
| 91 #endif // IPC_IPC_TEST_SINK_H_ | 128 #endif // IPC_IPC_TEST_SINK_H_ |
| OLD | NEW |