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.
5 #ifndef CONTENT_PUBLIC_TEST_MOCK_RENDER_THREAD_H_
6 #define CONTENT_PUBLIC_TEST_MOCK_RENDER_THREAD_H_
8 #include "base/memory/shared_memory.h"
9 #include "base/observer_list.h"
10 #include "base/strings/string16.h"
11 #include "content/public/renderer/render_thread.h"
12 #include "ipc/ipc_test_sink.h"
13 #include "ipc/message_filter.h"
14 #include "third_party/WebKit/public/web/WebPopupType.h"
16 struct ViewHostMsg_CreateWindow_Params
;
20 class MessageReplyDeserializer
;
25 // This class is a very simple mock of RenderThread. It simulates an IPC channel
26 // which supports only three messages:
27 // ViewHostMsg_CreateWidget : sync message sent by the Widget.
28 // ViewHostMsg_CreateWindow : sync message sent by the Widget.
29 // ViewMsg_Close : async, send to the Widget.
30 class MockRenderThread
: public RenderThread
{
33 ~MockRenderThread() override
;
35 // Provides access to the messages that have been received by this thread.
36 IPC::TestSink
& sink() { return sink_
; }
38 // RenderThread implementation:
39 bool Send(IPC::Message
* msg
) override
;
40 base::MessageLoop
* GetMessageLoop() override
;
41 IPC::SyncChannel
* GetChannel() override
;
42 std::string
GetLocale() override
;
43 IPC::SyncMessageFilter
* GetSyncMessageFilter() override
;
44 scoped_refptr
<base::MessageLoopProxy
> GetIOMessageLoopProxy() override
;
45 void AddRoute(int32 routing_id
, IPC::Listener
* listener
) override
;
46 void RemoveRoute(int32 routing_id
) override
;
47 int GenerateRoutingID() override
;
48 void AddFilter(IPC::MessageFilter
* filter
) override
;
49 void RemoveFilter(IPC::MessageFilter
* filter
) override
;
50 void AddObserver(RenderProcessObserver
* observer
) override
;
51 void RemoveObserver(RenderProcessObserver
* observer
) override
;
52 void SetResourceDispatcherDelegate(
53 ResourceDispatcherDelegate
* delegate
) override
;
54 void EnsureWebKitInitialized() override
;
55 void RecordAction(const base::UserMetricsAction
& action
) override
;
56 void RecordComputedAction(const std::string
& action
) override
;
57 scoped_ptr
<base::SharedMemory
> HostAllocateSharedMemoryBuffer(
58 size_t buffer_size
) override
;
59 void RegisterExtension(v8::Extension
* extension
) override
;
60 void ScheduleIdleHandler(int64 initial_delay_ms
) override
;
61 void IdleHandler() override
;
62 int64
GetIdleNotificationDelayInMs() const override
;
63 void SetIdleNotificationDelayInMs(
64 int64 idle_notification_delay_in_ms
) override
;
65 void UpdateHistograms(int sequence_number
) override
;
66 int PostTaskToAllWebWorkers(const base::Closure
& closure
) override
;
67 bool ResolveProxy(const GURL
& url
, std::string
* proxy_list
) override
;
68 base::WaitableEvent
* GetShutdownEvent() override
;
70 virtual void PreCacheFont(const LOGFONT
& log_font
) override
;
71 virtual void ReleaseCachedFonts() override
;
73 ServiceRegistry
* GetServiceRegistry() override
;
75 //////////////////////////////////////////////////////////////////////////
76 // The following functions are called by the test itself.
78 void set_routing_id(int32 id
) {
82 void set_surface_id(int32 id
) {
86 int32
opener_id() const {
90 void set_new_window_routing_id(int32 id
) {
91 new_window_routing_id_
= id
;
94 void set_new_frame_routing_id(int32 id
) {
95 new_frame_routing_id_
= id
;
98 // Simulates the Widget receiving a close message. This should result
99 // on releasing the internal reference counts and destroying the internal
101 void SendCloseMessage();
103 // Dispatches control messages to observers.
104 bool OnControlMessageReceived(const IPC::Message
& msg
);
106 ObserverList
<RenderProcessObserver
>& observers() {
111 // This function operates as a regular IPC listener. Subclasses
112 // overriding this should first delegate to this implementation.
113 virtual bool OnMessageReceived(const IPC::Message
& msg
);
115 // The Widget expects to be returned valid route_id.
116 void OnCreateWidget(int opener_id
,
117 blink::WebPopupType popup_type
,
121 // The View expects to be returned a valid route_id different from its own.
122 // We do not keep track of the newly created widget in MockRenderThread,
123 // so it must be cleaned up on its own.
125 const ViewHostMsg_CreateWindow_Params
& params
,
127 int* main_frame_route_id
,
129 int64
* cloned_session_storage_namespace_id
);
131 // The Frame expects to be returned a valid route_id different from its own.
132 void OnCreateChildFrame(int new_frame_routing_id
,
133 const std::string
& frame_name
,
134 int* new_render_frame_id
);
137 void OnDuplicateSection(base::SharedMemoryHandle renderer_handle
,
138 base::SharedMemoryHandle
* browser_handle
);
143 // Routing id what will be assigned to the Widget.
146 // Surface id what will be assigned to the Widget.
149 // Opener id reported by the Widget.
152 // Routing id that will be assigned to a CreateWindow Widget.
153 int32 new_window_routing_id_
;
154 int32 new_window_main_frame_routing_id_
;
155 int32 new_frame_routing_id_
;
157 // The last known good deserializer for sync messages.
158 scoped_ptr
<IPC::MessageReplyDeserializer
> reply_deserializer_
;
160 // A list of message filters added to this thread.
161 std::vector
<scoped_refptr
<IPC::MessageFilter
> > filters_
;
163 // Observers to notify.
164 ObserverList
<RenderProcessObserver
> observers_
;
167 } // namespace content
169 #endif // CONTENT_PUBLIC_TEST_MOCK_RENDER_THREAD_H_