| OLD | NEW |
| 1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. | 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 | 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 CHROME_RENDERER_MOCK_RENDER_THREAD_H_ | 5 #ifndef CHROME_RENDERER_MOCK_RENDER_THREAD_H_ |
| 6 #define CHROME_RENDERER_MOCK_RENDER_THREAD_H_ | 6 #define CHROME_RENDERER_MOCK_RENDER_THREAD_H_ |
| 7 | 7 |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "chrome/common/ipc_test_sink.h" |
| 10 #include "chrome/renderer/render_thread.h" | 11 #include "chrome/renderer/render_thread.h" |
| 11 | 12 |
| 12 // This class is very simple mock of RenderThread. It simulates an IPC channel | 13 // This class is very simple mock of RenderThread. It simulates an IPC channel |
| 13 // which supports only two messages: | 14 // which supports only two messages: |
| 14 // ViewHostMsg_CreateWidget : sync message sent by the Widget. | 15 // ViewHostMsg_CreateWidget : sync message sent by the Widget. |
| 15 // ViewMsg_Close : async, send to the Widget. | 16 // ViewMsg_Close : async, send to the Widget. |
| 16 class MockRenderThread : public RenderThreadBase { | 17 class MockRenderThread : public RenderThreadBase { |
| 17 public: | 18 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(); | 19 MockRenderThread(); |
| 23 virtual ~MockRenderThread(); | 20 virtual ~MockRenderThread(); |
| 24 | 21 |
| 22 // Provides access to the messages that have been received by this thread. |
| 23 IPC::TestSink& sink() { return sink_; } |
| 24 |
| 25 // Called by the Widget. Not used in the test. | 25 // Called by the Widget. Not used in the test. |
| 26 virtual bool InSend() const { | 26 virtual bool InSend() const { |
| 27 return false; | 27 return false; |
| 28 } | 28 } |
| 29 | 29 |
| 30 // Called by the Widget. The routing_id must match the routing id assigned | 30 // Called by the Widget. The routing_id must match the routing id assigned |
| 31 // to the Widget in reply to ViewHostMsg_CreateWidget message. | 31 // to the Widget in reply to ViewHostMsg_CreateWidget message. |
| 32 virtual void AddRoute(int32 routing_id, IPC::Channel::Listener* listener); | 32 virtual void AddRoute(int32 routing_id, IPC::Channel::Listener* listener); |
| 33 | 33 |
| 34 // Called by the Widget. The routing id must match the routing id of AddRoute. | 34 // Called by the Widget. The routing id must match the routing id of AddRoute. |
| (...skipping 18 matching lines...) Expand all Loading... |
| 53 } | 53 } |
| 54 | 54 |
| 55 int32 opener_id() const { | 55 int32 opener_id() const { |
| 56 return opener_id_; | 56 return opener_id_; |
| 57 } | 57 } |
| 58 | 58 |
| 59 bool has_widget() const { | 59 bool has_widget() const { |
| 60 return widget_ ? true : false; | 60 return widget_ ? true : false; |
| 61 } | 61 } |
| 62 | 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 | 63 // Simulates the Widget receiving a close message. This should result |
| 87 // on releasing the internal reference counts and destroying the internal | 64 // on releasing the internal reference counts and destroying the internal |
| 88 // state. | 65 // state. |
| 89 void SendCloseMessage(); | 66 void SendCloseMessage(); |
| 90 | 67 |
| 91 private: | 68 private: |
| 92 // This function operates as a regular IPC listener. | 69 // This function operates as a regular IPC listener. |
| 93 void OnMessageReceived(const IPC::Message& msg); | 70 void OnMessageReceived(const IPC::Message& msg); |
| 94 | 71 |
| 95 // The Widget expects to be returned valid route_id. | 72 // The Widget expects to be returned valid route_id. |
| 96 void OnMsgCreateWidget(int opener_id, | 73 void OnMsgCreateWidget(int opener_id, |
| 97 bool activatable, | 74 bool activatable, |
| 98 int* route_id); | 75 int* route_id); |
| 99 | 76 |
| 77 IPC::TestSink sink_; |
| 78 |
| 100 // Routing id what will be assigned to the Widget. | 79 // Routing id what will be assigned to the Widget. |
| 101 int32 routing_id_; | 80 int32 routing_id_; |
| 102 | 81 |
| 103 // Opener id reported by the Widget. | 82 // Opener id reported by the Widget. |
| 104 int32 opener_id_; | 83 int32 opener_id_; |
| 105 | 84 |
| 106 // We only keep track of one Widget, we learn its pointer when it | 85 // We only keep track of one Widget, we learn its pointer when it |
| 107 // adds a new route. | 86 // adds a new route. |
| 108 IPC::Channel::Listener* widget_; | 87 IPC::Channel::Listener* widget_; |
| 109 | 88 |
| 110 // The last known good deserializer for sync messages. | 89 // The last known good deserializer for sync messages. |
| 111 scoped_ptr<IPC::MessageReplyDeserializer> reply_deserializer_; | 90 scoped_ptr<IPC::MessageReplyDeserializer> reply_deserializer_; |
| 112 | |
| 113 std::vector<MessagePair> messages_; | |
| 114 }; | 91 }; |
| 115 | 92 |
| 116 #endif // CHROME_RENDERER_MOCK_RENDER_THREAD_H_ | 93 #endif // CHROME_RENDERER_MOCK_RENDER_THREAD_H_ |
| OLD | NEW |