OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2008 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. |
| 4 |
| 5 #ifndef CHROME_RENDERER_MOCK_RENDER_THREAD_H_ |
| 6 #define CHROME_RENDERER_MOCK_RENDER_THREAD_H_ |
| 7 |
| 8 #include <vector> |
| 9 |
| 10 #include "chrome/renderer/render_thread.h" |
| 11 |
| 12 // This class is very simple mock of RenderThread. It simulates an IPC channel |
| 13 // which supports only two messages: |
| 14 // ViewHostMsg_CreateWidget : sync message sent by the Widget. |
| 15 // ViewMsg_Close : async, send to the Widget. |
| 16 class MockRenderThread : public RenderThreadBase { |
| 17 public: |
| 18 // Encapusulates an IPC message and its associated data (which is not |
| 19 // otherwise bound to the lifetime of the message). |
| 20 typedef std::pair<IPC::Message, char*> MessagePair; |
| 21 |
| 22 MockRenderThread(); |
| 23 virtual ~MockRenderThread(); |
| 24 |
| 25 // Called by the Widget. Not used in the test. |
| 26 virtual bool InSend() const { |
| 27 return false; |
| 28 } |
| 29 |
| 30 // Called by the Widget. The routing_id must match the routing id assigned |
| 31 // to the Widget in reply to ViewHostMsg_CreateWidget message. |
| 32 virtual void AddRoute(int32 routing_id, IPC::Channel::Listener* listener); |
| 33 |
| 34 // Called by the Widget. The routing id must match the routing id of AddRoute. |
| 35 virtual void RemoveRoute(int32 routing_id); |
| 36 |
| 37 // Called by the Widget. Used to send messages to the browser. |
| 38 // We short-circuit the mechanim and handle the messages right here on this |
| 39 // class. |
| 40 virtual bool Send(IPC::Message* msg); |
| 41 |
| 42 // Our mock thread doesn't do filtering. |
| 43 virtual void AddFilter(IPC::ChannelProxy::MessageFilter* filter) { |
| 44 } |
| 45 virtual void RemoveFilter(IPC::ChannelProxy::MessageFilter* filter) { |
| 46 } |
| 47 |
| 48 ////////////////////////////////////////////////////////////////////////// |
| 49 // The following functions are called by the test itself. |
| 50 |
| 51 void set_routing_id(int32 id) { |
| 52 routing_id_ = id; |
| 53 } |
| 54 |
| 55 int32 opener_id() const { |
| 56 return opener_id_; |
| 57 } |
| 58 |
| 59 bool has_widget() const { |
| 60 return widget_ ? true : false; |
| 61 } |
| 62 |
| 63 // Returns the number of messages in the queue. |
| 64 size_t message_count() const { return messages_.size(); } |
| 65 |
| 66 // Clears the message queue of saved messages. |
| 67 void ClearMessages(); |
| 68 |
| 69 // Returns the message at the given index in the queue. The index may be out |
| 70 // of range, in which case the return value is NULL. The returned pointer will |
| 71 // only be valid until another message is received or the list is cleared. |
| 72 const IPC::Message* GetMessageAt(size_t index) const; |
| 73 |
| 74 // Returns the first message with the given ID in the queue. If there is no |
| 75 // message with the given ID, returns NULL. The returned pointer will only be |
| 76 // valid until another message is received or the list is cleared. |
| 77 const IPC::Message* GetFirstMessageMatching(uint16 id) const; |
| 78 |
| 79 // Returns the message with the given ID in the queue. If there is no such |
| 80 // message or there is more than one of that message, this will return NULL |
| 81 // (with the expectation that you'll do an ASSERT_TRUE() on the result). |
| 82 // The returned pointer will only be valid until another message is received |
| 83 // or the list is cleared. |
| 84 const IPC::Message* GetUniqueMessageMatching(uint16 id) const; |
| 85 |
| 86 // Simulates the Widget receiving a close message. This should result |
| 87 // on releasing the internal reference counts and destroying the internal |
| 88 // state. |
| 89 void SendCloseMessage(); |
| 90 |
| 91 private: |
| 92 // This function operates as a regular IPC listener. |
| 93 void OnMessageReceived(const IPC::Message& msg); |
| 94 |
| 95 // The Widget expects to be returned valid route_id. |
| 96 void OnMsgCreateWidget(int opener_id, |
| 97 bool focus_on_show, |
| 98 int* route_id); |
| 99 |
| 100 // Routing id what will be assigned to the Widget. |
| 101 int32 routing_id_; |
| 102 |
| 103 // Opener id reported by the Widget. |
| 104 int32 opener_id_; |
| 105 |
| 106 // We only keep track of one Widget, we learn its pointer when it |
| 107 // adds a new route. |
| 108 IPC::Channel::Listener* widget_; |
| 109 |
| 110 // The last known good deserializer for sync messages. |
| 111 IPC::MessageReplyDeserializer* reply_deserializer_; |
| 112 |
| 113 std::vector<MessagePair> messages_; |
| 114 }; |
| 115 |
| 116 #endif // CHROME_RENDERER_MOCK_RENDER_THREAD_H_ |
OLD | NEW |