mock_render_thread.h revision c2e0dbddbe15c98d52c4786dac06cb8952a8ae6d
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#ifndef CONTENT_PUBLIC_TEST_MOCK_RENDER_THREAD_H_
6#define CONTENT_PUBLIC_TEST_MOCK_RENDER_THREAD_H_
7
8#include "base/shared_memory.h"
9#include "base/string16.h"
10#include "content/public/renderer/render_thread.h"
11#include "ipc/ipc_test_sink.h"
12#include "third_party/WebKit/Source/WebKit/chromium/public/WebPopupType.h"
13
14struct ViewHostMsg_CreateWindow_Params;
15
16namespace IPC {
17class MessageReplyDeserializer;
18}
19
20namespace content {
21
22// This class is a very simple mock of RenderThread. It simulates an IPC channel
23// which supports only three messages:
24// ViewHostMsg_CreateWidget : sync message sent by the Widget.
25// ViewHostMsg_CreateWindow : sync message sent by the Widget.
26// ViewMsg_Close : async, send to the Widget.
27class MockRenderThread : public RenderThread {
28 public:
29  MockRenderThread();
30  virtual ~MockRenderThread();
31
32  // Provides access to the messages that have been received by this thread.
33  IPC::TestSink& sink() { return sink_; }
34
35  // Helpers for embedders to know when content IPC messages are received, since
36  // they don't have access to content IPC files.
37  void VerifyRunJavaScriptMessageSend(const string16& expected_alert_message);
38
39  // RenderThread implementation:
40  virtual bool Send(IPC::Message* msg) OVERRIDE;
41  virtual base::MessageLoop* GetMessageLoop() OVERRIDE;
42  virtual IPC::SyncChannel* GetChannel() OVERRIDE;
43  virtual std::string GetLocale() OVERRIDE;
44  virtual IPC::SyncMessageFilter* GetSyncMessageFilter() OVERRIDE;
45  virtual scoped_refptr<base::MessageLoopProxy> GetIOMessageLoopProxy()
46      OVERRIDE;
47  virtual void AddRoute(int32 routing_id, IPC::Listener* listener) OVERRIDE;
48  virtual void RemoveRoute(int32 routing_id) OVERRIDE;
49  virtual int GenerateRoutingID() OVERRIDE;
50  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(RenderProcessObserver* observer) OVERRIDE;
55  virtual void RemoveObserver(RenderProcessObserver* observer) OVERRIDE;
56  virtual void SetResourceDispatcherDelegate(
57      ResourceDispatcherDelegate* delegate) OVERRIDE;
58  virtual void WidgetHidden() OVERRIDE;
59  virtual void WidgetRestored() OVERRIDE;
60  virtual void EnsureWebKitInitialized() OVERRIDE;
61  virtual void RecordUserMetrics(const std::string& action) OVERRIDE;
62  virtual scoped_ptr<base::SharedMemory> HostAllocateSharedMemoryBuffer(
63      size_t buffer_size) OVERRIDE;
64  virtual void RegisterExtension(v8::Extension* extension) OVERRIDE;
65  virtual void ScheduleIdleHandler(int64 initial_delay_ms) OVERRIDE;
66  virtual void IdleHandler() OVERRIDE;
67  virtual int64 GetIdleNotificationDelayInMs() const OVERRIDE;
68  virtual void SetIdleNotificationDelayInMs(
69      int64 idle_notification_delay_in_ms) OVERRIDE;
70  virtual void ToggleWebKitSharedTimer(bool suspend) OVERRIDE;
71  virtual void UpdateHistograms(int sequence_number) OVERRIDE;
72  virtual bool ResolveProxy(const GURL& url, std::string* proxy_list) OVERRIDE;
73#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
85  void set_surface_id(int32 id) {
86    surface_id_ = id;
87  }
88
89  int32 opener_id() const {
90    return opener_id_;
91  }
92
93  bool has_widget() const {
94    return (widget_ != NULL);
95  }
96
97  void set_new_window_routing_id(int32 id) {
98    new_window_routing_id_ = id;
99  }
100
101  // 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 OnCreateWidget(int opener_id,
113                      WebKit::WebPopupType popup_type,
114                      int* route_id,
115                      int* surface_id);
116
117  // 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 OnCreateWindow(
121    const ViewHostMsg_CreateWindow_Params& params,
122    int* route_id,
123    int* surface_id,
124    int64* cloned_session_storage_namespace_id);
125
126#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
136  // Surface id what will be assigned to the Widget.
137  int32 surface_id_;
138
139  // 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
143  // adds a new route.  We do not keep track of Widgets created with
144  // OnCreateWindow.
145  IPC::Listener* widget_;
146
147  // Routing id that will be assigned to a CreateWindow Widget.
148  int32 new_window_routing_id_;
149
150  // The last known good deserializer for sync messages.
151  scoped_ptr<IPC::MessageReplyDeserializer> reply_deserializer_;
152
153  // A list of message filters added to this thread.
154  std::vector<scoped_refptr<IPC::ChannelProxy::MessageFilter> > filters_;
155};
156
157}  // namespace content
158
159#endif  // CONTENT_PUBLIC_TEST_MOCK_RENDER_THREAD_H_
160