| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 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 "content/public/test/mock_render_thread.h" | |
| 6 | |
| 7 #include "base/process_util.h" | |
| 8 #include "base/message_loop_proxy.h" | |
| 9 #include "content/common/view_messages.h" | |
| 10 #include "ipc/ipc_message_utils.h" | |
| 11 #include "ipc/ipc_sync_message.h" | |
| 12 #include "testing/gtest/include/gtest/gtest.h" | |
| 13 | |
| 14 namespace content { | |
| 15 | |
| 16 MockRenderThread::MockRenderThread() | |
| 17 : routing_id_(0), surface_id_(0), opener_id_(0), new_window_routing_id_(0) { | |
| 18 } | |
| 19 | |
| 20 MockRenderThread::~MockRenderThread() { | |
| 21 } | |
| 22 | |
| 23 void MockRenderThread::VerifyRunJavaScriptMessageSend( | |
| 24 const string16& expected_alert_message) { | |
| 25 const IPC::Message* alert_msg = | |
| 26 sink_.GetUniqueMessageMatching(ViewHostMsg_RunJavaScriptMessage::ID); | |
| 27 ASSERT_TRUE(alert_msg); | |
| 28 PickleIterator iter = IPC::SyncMessage::GetDataIterator(alert_msg); | |
| 29 ViewHostMsg_RunJavaScriptMessage::SendParam alert_param; | |
| 30 ASSERT_TRUE(IPC::ReadParam(alert_msg, &iter, &alert_param)); | |
| 31 EXPECT_EQ(expected_alert_message, alert_param.a); | |
| 32 } | |
| 33 | |
| 34 // Called by the Widget. Used to send messages to the browser. | |
| 35 // We short-circuit the mechanism and handle the messages right here on this | |
| 36 // class. | |
| 37 bool MockRenderThread::Send(IPC::Message* msg) { | |
| 38 // We need to simulate a synchronous channel, thus we are going to receive | |
| 39 // through this function messages, messages with reply and reply messages. | |
| 40 // We can only handle one synchronous message at a time. | |
| 41 if (msg->is_reply()) { | |
| 42 if (reply_deserializer_.get()) { | |
| 43 reply_deserializer_->SerializeOutputParameters(*msg); | |
| 44 reply_deserializer_.reset(); | |
| 45 } | |
| 46 } else { | |
| 47 if (msg->is_sync()) { | |
| 48 // We actually need to handle deleting the reply deserializer for sync | |
| 49 // messages. | |
| 50 reply_deserializer_.reset( | |
| 51 static_cast<IPC::SyncMessage*>(msg)->GetReplyDeserializer()); | |
| 52 } | |
| 53 OnMessageReceived(*msg); | |
| 54 } | |
| 55 delete msg; | |
| 56 return true; | |
| 57 } | |
| 58 | |
| 59 MessageLoop* MockRenderThread::GetMessageLoop() { | |
| 60 return NULL; | |
| 61 } | |
| 62 | |
| 63 IPC::SyncChannel* MockRenderThread::GetChannel() { | |
| 64 return NULL; | |
| 65 } | |
| 66 | |
| 67 std::string MockRenderThread::GetLocale() { | |
| 68 return std::string(); | |
| 69 } | |
| 70 | |
| 71 IPC::SyncMessageFilter* MockRenderThread::GetSyncMessageFilter() { | |
| 72 return NULL; | |
| 73 } | |
| 74 | |
| 75 scoped_refptr<base::MessageLoopProxy> | |
| 76 MockRenderThread::GetIOMessageLoopProxy() { | |
| 77 return scoped_refptr<base::MessageLoopProxy>(); | |
| 78 } | |
| 79 | |
| 80 void MockRenderThread::AddRoute(int32 routing_id, IPC::Listener* listener) { | |
| 81 // We may hear this for views created from OnMsgCreateWindow as well, | |
| 82 // in which case we don't want to track the new widget. | |
| 83 if (routing_id_ == routing_id) | |
| 84 widget_ = listener; | |
| 85 } | |
| 86 | |
| 87 void MockRenderThread::RemoveRoute(int32 routing_id) { | |
| 88 // We may hear this for views created from OnMsgCreateWindow as well, | |
| 89 // in which case we don't want to track the new widget. | |
| 90 if (routing_id_ == routing_id) | |
| 91 widget_ = NULL; | |
| 92 } | |
| 93 | |
| 94 int MockRenderThread::GenerateRoutingID() { | |
| 95 NOTREACHED(); | |
| 96 return MSG_ROUTING_NONE; | |
| 97 } | |
| 98 | |
| 99 void MockRenderThread::AddFilter(IPC::ChannelProxy::MessageFilter* filter) { | |
| 100 filter->OnFilterAdded(&sink()); | |
| 101 } | |
| 102 | |
| 103 void MockRenderThread::RemoveFilter(IPC::ChannelProxy::MessageFilter* filter) { | |
| 104 filter->OnFilterRemoved(); | |
| 105 } | |
| 106 | |
| 107 void MockRenderThread::SetOutgoingMessageFilter( | |
| 108 IPC::ChannelProxy::OutgoingMessageFilter* filter) { | |
| 109 } | |
| 110 | |
| 111 void MockRenderThread::AddObserver(content::RenderProcessObserver* observer) { | |
| 112 } | |
| 113 | |
| 114 void MockRenderThread::RemoveObserver( | |
| 115 content::RenderProcessObserver* observer) { | |
| 116 } | |
| 117 | |
| 118 void MockRenderThread::SetResourceDispatcherDelegate( | |
| 119 content::ResourceDispatcherDelegate* delegate) { | |
| 120 } | |
| 121 | |
| 122 void MockRenderThread::WidgetHidden() { | |
| 123 } | |
| 124 | |
| 125 void MockRenderThread::WidgetRestored() { | |
| 126 } | |
| 127 | |
| 128 void MockRenderThread::EnsureWebKitInitialized() { | |
| 129 } | |
| 130 | |
| 131 void MockRenderThread::RecordUserMetrics(const std::string& action) { | |
| 132 } | |
| 133 | |
| 134 base::SharedMemoryHandle MockRenderThread::HostAllocateSharedMemoryBuffer( | |
| 135 uint32 buffer_size) { | |
| 136 base::SharedMemory shared_buf; | |
| 137 if (!shared_buf.CreateAndMapAnonymous(buffer_size)) { | |
| 138 NOTREACHED() << "Cannot map shared memory buffer"; | |
| 139 return base::SharedMemory::NULLHandle(); | |
| 140 } | |
| 141 base::SharedMemoryHandle handle; | |
| 142 shared_buf.GiveToProcess(base::GetCurrentProcessHandle(), &handle); | |
| 143 return handle; | |
| 144 } | |
| 145 | |
| 146 void MockRenderThread::RegisterExtension(v8::Extension* extension) { | |
| 147 } | |
| 148 | |
| 149 void MockRenderThread::ScheduleIdleHandler(int64 initial_delay_ms) { | |
| 150 } | |
| 151 | |
| 152 void MockRenderThread::IdleHandler() { | |
| 153 } | |
| 154 | |
| 155 int64 MockRenderThread::GetIdleNotificationDelayInMs() const { | |
| 156 return 0; | |
| 157 } | |
| 158 | |
| 159 void MockRenderThread::SetIdleNotificationDelayInMs( | |
| 160 int64 idle_notification_delay_in_ms) { | |
| 161 } | |
| 162 | |
| 163 void MockRenderThread::ToggleWebKitSharedTimer(bool suspend) { | |
| 164 } | |
| 165 | |
| 166 void MockRenderThread::UpdateHistograms(int sequence_number) { | |
| 167 } | |
| 168 | |
| 169 #if defined(OS_WIN) | |
| 170 void MockRenderThread::PreCacheFont(const LOGFONT& log_font) { | |
| 171 } | |
| 172 | |
| 173 void MockRenderThread::ReleaseCachedFonts() { | |
| 174 } | |
| 175 | |
| 176 #endif // OS_WIN | |
| 177 | |
| 178 void MockRenderThread::SendCloseMessage() { | |
| 179 ViewMsg_Close msg(routing_id_); | |
| 180 widget_->OnMessageReceived(msg); | |
| 181 } | |
| 182 | |
| 183 // The Widget expects to be returned valid route_id. | |
| 184 void MockRenderThread::OnMsgCreateWidget(int opener_id, | |
| 185 WebKit::WebPopupType popup_type, | |
| 186 int* route_id, | |
| 187 int* surface_id) { | |
| 188 opener_id_ = opener_id; | |
| 189 *route_id = routing_id_; | |
| 190 *surface_id = surface_id_; | |
| 191 } | |
| 192 | |
| 193 // The View expects to be returned a valid route_id different from its own. | |
| 194 void MockRenderThread::OnMsgCreateWindow( | |
| 195 const ViewHostMsg_CreateWindow_Params& params, | |
| 196 int* route_id, | |
| 197 int* surface_id, | |
| 198 int64* cloned_session_storage_namespace_id) { | |
| 199 *route_id = new_window_routing_id_; | |
| 200 *surface_id = surface_id_; | |
| 201 *cloned_session_storage_namespace_id = 0; | |
| 202 } | |
| 203 | |
| 204 bool MockRenderThread::OnMessageReceived(const IPC::Message& msg) { | |
| 205 // Save the message in the sink. | |
| 206 sink_.OnMessageReceived(msg); | |
| 207 | |
| 208 bool handled = true; | |
| 209 bool msg_is_ok = true; | |
| 210 IPC_BEGIN_MESSAGE_MAP_EX(MockRenderThread, msg, msg_is_ok) | |
| 211 IPC_MESSAGE_HANDLER(ViewHostMsg_CreateWidget, OnMsgCreateWidget) | |
| 212 IPC_MESSAGE_HANDLER(ViewHostMsg_CreateWindow, OnMsgCreateWindow) | |
| 213 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 214 IPC_END_MESSAGE_MAP_EX() | |
| 215 return handled; | |
| 216 } | |
| 217 | |
| 218 #if defined(OS_WIN) | |
| 219 void MockRenderThread::OnDuplicateSection( | |
| 220 base::SharedMemoryHandle renderer_handle, | |
| 221 base::SharedMemoryHandle* browser_handle) { | |
| 222 // We don't have to duplicate the input handles since RenderViewTest does not | |
| 223 // separate a browser process from a renderer process. | |
| 224 *browser_handle = renderer_handle; | |
| 225 } | |
| 226 #endif // defined(OS_WIN) | |
| 227 | |
| 228 } // namespace content | |
| OLD | NEW |