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 #include "chrome/renderer/mock_render_thread.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "chrome/common/ipc_message_utils.h" |
| 9 #include "chrome/common/render_messages.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 |
| 12 MockRenderThread::MockRenderThread() |
| 13 : routing_id_(0), |
| 14 opener_id_(0), |
| 15 widget_(NULL), |
| 16 reply_deserializer_(NULL) { |
| 17 } |
| 18 |
| 19 MockRenderThread::~MockRenderThread() { |
| 20 // Don't leak the allocated message bodies. |
| 21 ClearMessages(); |
| 22 } |
| 23 |
| 24 // Called by the Widget. The routing_id must match the routing id assigned |
| 25 // to the Widget in reply to ViewHostMsg_CreateWidget message. |
| 26 void MockRenderThread::AddRoute(int32 routing_id, |
| 27 IPC::Channel::Listener* listener) { |
| 28 EXPECT_EQ(routing_id_, routing_id); |
| 29 widget_ = listener; |
| 30 } |
| 31 |
| 32 // Called by the Widget. The routing id must match the routing id of AddRoute. |
| 33 void MockRenderThread::RemoveRoute(int32 routing_id) { |
| 34 EXPECT_EQ(routing_id_, routing_id); |
| 35 widget_ = NULL; |
| 36 } |
| 37 |
| 38 // Called by the Widget. Used to send messages to the browser. |
| 39 // We short-circuit the mechanim and handle the messages right here on this |
| 40 // class. |
| 41 bool MockRenderThread::Send(IPC::Message* msg) { |
| 42 // We need to simulate a synchronous channel, thus we are going to receive |
| 43 // through this function messages, messages with reply and reply messages. |
| 44 // We can only handle one synchronous message at a time. |
| 45 if (msg->is_reply()) { |
| 46 if (reply_deserializer_) { |
| 47 reply_deserializer_->SerializeOutputParameters(*msg); |
| 48 delete reply_deserializer_; |
| 49 reply_deserializer_ = NULL; |
| 50 } |
| 51 } else { |
| 52 if (msg->is_sync()) { |
| 53 reply_deserializer_ = |
| 54 static_cast<IPC::SyncMessage*>(msg)->GetReplyDeserializer(); |
| 55 } |
| 56 OnMessageReceived(*msg); |
| 57 } |
| 58 delete msg; |
| 59 return true; |
| 60 } |
| 61 |
| 62 void MockRenderThread::ClearMessages() { |
| 63 for (size_t i = 0; i < messages_.size(); i++) |
| 64 delete[] messages_[i].second; |
| 65 messages_.clear(); |
| 66 } |
| 67 |
| 68 const IPC::Message* MockRenderThread::GetMessageAt(size_t index) const { |
| 69 if (index >= messages_.size()) |
| 70 return NULL; |
| 71 return &messages_[index].first; |
| 72 } |
| 73 |
| 74 const IPC::Message* MockRenderThread::GetFirstMessageMatching(uint16 id) const { |
| 75 for (size_t i = 0; i < messages_.size(); i++) { |
| 76 if (messages_[i].first.type() == id) |
| 77 return &messages_[i].first; |
| 78 } |
| 79 return NULL; |
| 80 } |
| 81 |
| 82 const IPC::Message* MockRenderThread::GetUniqueMessageMatching( |
| 83 uint16 id) const { |
| 84 size_t found_index = 0; |
| 85 size_t found_count = 0; |
| 86 for (size_t i = 0; i < messages_.size(); i++) { |
| 87 if (messages_[i].first.type() == id) { |
| 88 found_count++; |
| 89 found_index = i; |
| 90 } |
| 91 } |
| 92 if (found_count != 1) |
| 93 return NULL; // Didn't find a unique one. |
| 94 return &messages_[found_index].first; |
| 95 } |
| 96 |
| 97 void MockRenderThread::SendCloseMessage() { |
| 98 ViewMsg_Close msg(routing_id_); |
| 99 widget_->OnMessageReceived(msg); |
| 100 } |
| 101 |
| 102 void MockRenderThread::OnMessageReceived(const IPC::Message& msg) { |
| 103 // Copy the message into a pair. This is tricky since the message doesn't |
| 104 // manage its data itself. |
| 105 char* data_copy; |
| 106 if (msg.size()) { |
| 107 data_copy = new char[msg.size()]; |
| 108 memcpy(data_copy, msg.data(), msg.size()); |
| 109 } else { |
| 110 // Dummy data so we can treat everything the same. |
| 111 data_copy = new char[1]; |
| 112 data_copy[0] = 0; |
| 113 } |
| 114 |
| 115 // Save the message. |
| 116 messages_.push_back( |
| 117 std::make_pair(IPC::Message(data_copy, msg.size()), data_copy)); |
| 118 |
| 119 // Some messages we do special handling. |
| 120 bool handled = true; |
| 121 bool msg_is_ok = true; |
| 122 IPC_BEGIN_MESSAGE_MAP_EX(MockRenderThread, msg, msg_is_ok) |
| 123 IPC_MESSAGE_HANDLER(ViewHostMsg_CreateWidget, OnMsgCreateWidget); |
| 124 IPC_MESSAGE_UNHANDLED(handled = false) |
| 125 IPC_END_MESSAGE_MAP_EX() |
| 126 } |
| 127 |
| 128 // The Widget expects to be returned valid route_id. |
| 129 void MockRenderThread::OnMsgCreateWidget(int opener_id, |
| 130 bool focus_on_show, |
| 131 int* route_id) { |
| 132 opener_id_ = opener_id; |
| 133 *route_id = routing_id_; |
| 134 } |
| 135 |
OLD | NEW |