blob: 120fa16714b94e37be4a39c4f14addfa6eeba5e6 [file] [log] [blame]
[email protected]9f4f3322012-01-18 22:29:561// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]c6d068ff2011-10-14 17:28:232// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CONTENT_TEST_MOCK_RENDER_THREAD_H_
6#define CONTENT_TEST_MOCK_RENDER_THREAD_H_
7#pragma once
8
9#include "base/shared_memory.h"
[email protected]6f8f8b632011-12-05 16:43:5510#include "base/string16.h"
[email protected]c6d068ff2011-10-14 17:28:2311#include "content/public/renderer/render_thread.h"
12#include "ipc/ipc_test_sink.h"
13#include "third_party/WebKit/Source/WebKit/chromium/public/WebPopupType.h"
14
[email protected]744c2a22012-03-15 18:42:0415struct ViewHostMsg_CreateWindow_Params;
16
[email protected]c6d068ff2011-10-14 17:28:2317namespace IPC {
18class MessageReplyDeserializer;
19}
20
21namespace content {
22
23// This class is a very simple mock of RenderThread. It simulates an IPC channel
[email protected]744c2a22012-03-15 18:42:0424// which supports only three messages:
[email protected]c6d068ff2011-10-14 17:28:2325// ViewHostMsg_CreateWidget : sync message sent by the Widget.
[email protected]744c2a22012-03-15 18:42:0426// ViewHostMsg_CreateWindow : sync message sent by the Widget.
[email protected]c6d068ff2011-10-14 17:28:2327// ViewMsg_Close : async, send to the Widget.
28class MockRenderThread : public content::RenderThread {
29 public:
30 MockRenderThread();
31 virtual ~MockRenderThread();
32
33 // Provides access to the messages that have been received by this thread.
34 IPC::TestSink& sink() { return sink_; }
35
[email protected]6f8f8b632011-12-05 16:43:5536 // Helpers for embedders to know when content IPC messages are received, since
37 // they don't have access to content IPC files.
38 void VerifyRunJavaScriptMessageSend(const string16& expected_alert_message);
39
[email protected]c6d068ff2011-10-14 17:28:2340 // content::RenderThread implementation:
41 virtual bool Send(IPC::Message* msg) OVERRIDE;
42 virtual MessageLoop* GetMessageLoop() OVERRIDE;
43 virtual IPC::SyncChannel* GetChannel() OVERRIDE;
44 virtual std::string GetLocale() OVERRIDE;
[email protected]07bb6332012-01-21 01:07:5745 virtual IPC::SyncMessageFilter* GetSyncMessageFilter() OVERRIDE;
[email protected]c6d068ff2011-10-14 17:28:2346 virtual void AddRoute(int32 routing_id,
47 IPC::Channel::Listener* listener) OVERRIDE;
48 virtual void RemoveRoute(int32 routing_id) OVERRIDE;
[email protected]77fc9b92011-10-15 16:20:3749 virtual int GenerateRoutingID() OVERRIDE;
[email protected]c6d068ff2011-10-14 17:28:2350 virtual void AddFilter(IPC::ChannelProxy::MessageFilter* filter) OVERRIDE;
51 virtual void RemoveFilter(IPC::ChannelProxy::MessageFilter* filter) OVERRIDE;
52 virtual void SetOutgoingMessageFilter(
53 IPC::ChannelProxy::OutgoingMessageFilter* filter) OVERRIDE;
54 virtual void AddObserver(content::RenderProcessObserver* observer) OVERRIDE;
55 virtual void RemoveObserver(
56 content::RenderProcessObserver* observer) OVERRIDE;
57 virtual void SetResourceDispatcherDelegate(
58 content::ResourceDispatcherDelegate* delegate) OVERRIDE;
59 virtual void WidgetHidden() OVERRIDE;
60 virtual void WidgetRestored() OVERRIDE;
61 virtual void EnsureWebKitInitialized() OVERRIDE;
62 virtual void RecordUserMetrics(const std::string& action) OVERRIDE;
63 virtual base::SharedMemoryHandle HostAllocateSharedMemoryBuffer(
64 uint32 buffer_size) OVERRIDE;
65 virtual void RegisterExtension(v8::Extension* extension) OVERRIDE;
66 virtual bool IsRegisteredExtension(
67 const std::string& v8_extension_name) const OVERRIDE;
[email protected]6593ae12011-11-14 12:09:4468 virtual void ScheduleIdleHandler(int64 initial_delay_ms) OVERRIDE;
[email protected]c6d068ff2011-10-14 17:28:2369 virtual void IdleHandler() OVERRIDE;
[email protected]6593ae12011-11-14 12:09:4470 virtual int64 GetIdleNotificationDelayInMs() const OVERRIDE;
71 virtual void SetIdleNotificationDelayInMs(
72 int64 idle_notification_delay_in_ms) OVERRIDE;
[email protected]c6d068ff2011-10-14 17:28:2373#if defined(OS_WIN)
74 virtual void PreCacheFont(const LOGFONT& log_font) OVERRIDE;
75 virtual void ReleaseCachedFonts() OVERRIDE;
76#endif
77
78 //////////////////////////////////////////////////////////////////////////
79 // The following functions are called by the test itself.
80
81 void set_routing_id(int32 id) {
82 routing_id_ = id;
83 }
84
[email protected]9f4f3322012-01-18 22:29:5685 void set_surface_id(int32 id) {
86 surface_id_ = id;
87 }
88
[email protected]c6d068ff2011-10-14 17:28:2389 int32 opener_id() const {
90 return opener_id_;
91 }
92
93 bool has_widget() const {
94 return widget_ ? true : false;
95 }
96
[email protected]744c2a22012-03-15 18:42:0497 void set_new_window_routing_id(int32 id) {
98 new_window_routing_id_ = id;
99 }
100
[email protected]c6d068ff2011-10-14 17:28:23101 // Simulates the Widget receiving a close message. This should result
102 // on releasing the internal reference counts and destroying the internal
103 // state.
104 void SendCloseMessage();
105
106 protected:
107 // This function operates as a regular IPC listener. Subclasses
108 // overriding this should first delegate to this implementation.
109 virtual bool OnMessageReceived(const IPC::Message& msg);
110
111 // The Widget expects to be returned valid route_id.
112 void OnMsgCreateWidget(int opener_id,
113 WebKit::WebPopupType popup_type,
[email protected]9f4f3322012-01-18 22:29:56114 int* route_id,
115 int* surface_id);
[email protected]c6d068ff2011-10-14 17:28:23116
[email protected]744c2a22012-03-15 18:42:04117 // The View expects to be returned a valid route_id different from its own.
118 // We do not keep track of the newly created widget in MockRenderThread,
119 // so it must be cleaned up on its own.
120 void OnMsgCreateWindow(
121 const ViewHostMsg_CreateWindow_Params& params,
122 int* route_id,
123 int* surface_id,
124 int64* cloned_session_storage_namespace_id);
125
[email protected]c6d068ff2011-10-14 17:28:23126#if defined(OS_WIN)
127 void OnDuplicateSection(base::SharedMemoryHandle renderer_handle,
128 base::SharedMemoryHandle* browser_handle);
129#endif
130
131 IPC::TestSink sink_;
132
133 // Routing id what will be assigned to the Widget.
134 int32 routing_id_;
135
[email protected]9f4f3322012-01-18 22:29:56136 // Surface id what will be assigned to the Widget.
137 int32 surface_id_;
138
[email protected]c6d068ff2011-10-14 17:28:23139 // Opener id reported by the Widget.
140 int32 opener_id_;
141
142 // We only keep track of one Widget, we learn its pointer when it
[email protected]744c2a22012-03-15 18:42:04143 // adds a new route. We do not keep track of Widgets created with
144 // OnMsgCreateWindow.
[email protected]c6d068ff2011-10-14 17:28:23145 IPC::Channel::Listener* widget_;
146
[email protected]744c2a22012-03-15 18:42:04147 // Routing id that will be assigned to a CreateWindow Widget.
148 int32 new_window_routing_id_;
149
[email protected]c6d068ff2011-10-14 17:28:23150 // The last known good deserializer for sync messages.
151 scoped_ptr<IPC::MessageReplyDeserializer> reply_deserializer_;
152};
153
154} // namespace content
155
156#endif // CONTENT_TEST_MOCK_RENDER_THREAD_H_