mock_render_process_host.h revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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_PROCESS_HOST_H_
6#define CONTENT_PUBLIC_TEST_MOCK_RENDER_PROCESS_HOST_H_
7
8#include "base/basictypes.h"
9#include "base/memory/scoped_vector.h"
10#include "content/public/browser/render_process_host.h"
11#include "content/public/browser/render_process_host_factory.h"
12#include "ipc/ipc_test_sink.h"
13
14class StoragePartition;
15class TransportDIB;
16
17namespace content {
18
19class MockRenderProcessHostFactory;
20
21// A mock render process host that has no corresponding renderer process.  All
22// IPC messages are sent into the message sink for inspection by tests.
23class MockRenderProcessHost : public RenderProcessHost {
24 public:
25  explicit MockRenderProcessHost(BrowserContext* browser_context);
26  virtual ~MockRenderProcessHost();
27
28  // Provides access to all IPC messages that would have been sent to the
29  // renderer via this RenderProcessHost.
30  IPC::TestSink& sink() { return sink_; }
31
32  // Provides test access to how many times a bad message has been received.
33  int bad_msg_count() const { return bad_msg_count_; }
34
35  // RenderProcessHost implementation (public portion).
36  virtual void EnableSendQueue() OVERRIDE;
37  virtual bool Init() OVERRIDE;
38  virtual int GetNextRoutingID() OVERRIDE;
39  virtual void SimulateSwapOutACK(
40      const ViewMsg_SwapOut_Params& params) OVERRIDE;
41  virtual bool WaitForBackingStoreMsg(int render_widget_id,
42                                      const base::TimeDelta& max_delay,
43                                      IPC::Message* msg) OVERRIDE;
44  virtual void ReceivedBadMessage() OVERRIDE;
45  virtual void WidgetRestored() OVERRIDE;
46  virtual void WidgetHidden() OVERRIDE;
47  virtual int VisibleWidgetCount() const OVERRIDE;
48  virtual bool IsGuest() const OVERRIDE;
49  virtual StoragePartition* GetStoragePartition() const OVERRIDE;
50  virtual void AddWord(const string16& word);
51  virtual bool FastShutdownIfPossible() OVERRIDE;
52  virtual bool FastShutdownStarted() const OVERRIDE;
53  virtual void DumpHandles() OVERRIDE;
54  virtual base::ProcessHandle GetHandle() const OVERRIDE;
55  virtual TransportDIB* GetTransportDIB(TransportDIB::Id dib_id) OVERRIDE;
56  virtual int GetID() const OVERRIDE;
57  virtual bool HasConnection() const OVERRIDE;
58  virtual void SetIgnoreInputEvents(bool ignore_input_events) OVERRIDE;
59  virtual bool IgnoreInputEvents() const OVERRIDE;
60  virtual void Attach(RenderWidgetHost* host, int routing_id) OVERRIDE;
61  virtual void Release(int routing_id) OVERRIDE;
62  virtual void Cleanup() OVERRIDE;
63  virtual void AddPendingView() OVERRIDE;
64  virtual void RemovePendingView() OVERRIDE;
65  virtual void SetSuddenTerminationAllowed(bool allowed) OVERRIDE;
66  virtual bool SuddenTerminationAllowed() const OVERRIDE;
67  virtual RenderWidgetHost* GetRenderWidgetHostByID(int routing_id)
68        OVERRIDE;
69  virtual BrowserContext* GetBrowserContext() const OVERRIDE;
70  virtual bool InSameStoragePartition(
71      StoragePartition* partition) const OVERRIDE;
72  virtual IPC::ChannelProxy* GetChannel() OVERRIDE;
73  virtual RenderWidgetHostsIterator GetRenderWidgetHostsIterator() OVERRIDE;
74  virtual bool FastShutdownForPageCount(size_t count) OVERRIDE;
75  virtual base::TimeDelta GetChildProcessIdleTime() const OVERRIDE;
76  virtual void SurfaceUpdated(int32 surface_id) OVERRIDE;
77  virtual void ResumeRequestsForView(int route_id) OVERRIDE;
78
79  // IPC::Sender via RenderProcessHost.
80  virtual bool Send(IPC::Message* msg) OVERRIDE;
81
82  // IPC::Listener via RenderProcessHost.
83  virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE;
84  virtual void OnChannelConnected(int32 peer_pid) OVERRIDE;
85
86  // Attaches the factory object so we can remove this object in its destructor
87  // and prevent MockRenderProcessHostFacotry from deleting it.
88  void SetFactory(const MockRenderProcessHostFactory* factory) {
89    factory_ = factory;
90  }
91
92 private:
93  // Stores IPC messages that would have been sent to the renderer.
94  IPC::TestSink sink_;
95  TransportDIB* transport_dib_;
96  int bad_msg_count_;
97  const MockRenderProcessHostFactory* factory_;
98  int id_;
99  BrowserContext* browser_context_;
100
101  IDMap<RenderWidgetHost> render_widget_hosts_;
102  bool fast_shutdown_started_;
103
104  DISALLOW_COPY_AND_ASSIGN(MockRenderProcessHost);
105};
106
107class MockRenderProcessHostFactory : public RenderProcessHostFactory {
108 public:
109  MockRenderProcessHostFactory();
110  virtual ~MockRenderProcessHostFactory();
111
112  virtual RenderProcessHost* CreateRenderProcessHost(
113      BrowserContext* browser_context) const OVERRIDE;
114
115  // Removes the given MockRenderProcessHost from the MockRenderProcessHost list
116  // without deleting it. When a test deletes a MockRenderProcessHost, we need
117  // to remove it from |processes_| to prevent it from being deleted twice.
118  void Remove(MockRenderProcessHost* host) const;
119
120 private:
121  // A list of MockRenderProcessHosts created by this object. This list is used
122  // for deleting all MockRenderProcessHosts that have not deleted by a test in
123  // the destructor and prevent them from being leaked.
124  mutable ScopedVector<MockRenderProcessHost> processes_;
125
126  DISALLOW_COPY_AND_ASSIGN(MockRenderProcessHostFactory);
127};
128
129}  // namespace content
130
131#endif  // CONTENT_PUBLIC_TEST_MOCK_RENDER_PROCESS_HOST_H_
132