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 "base/observer_list.h" 11 #include "content/public/browser/render_process_host.h" 12 #include "content/public/browser/render_process_host_factory.h" 13 #include "ipc/ipc_test_sink.h" 14 15 class StoragePartition; 16 class TransportDIB; 17 18 namespace content { 19 20 class MockRenderProcessHostFactory; 21 22 // A mock render process host that has no corresponding renderer process. All 23 // IPC messages are sent into the message sink for inspection by tests. 24 class MockRenderProcessHost : public RenderProcessHost { 25 public: 26 explicit MockRenderProcessHost(BrowserContext* browser_context); 27 virtual ~MockRenderProcessHost(); 28 29 // Provides access to all IPC messages that would have been sent to the 30 // renderer via this RenderProcessHost. 31 IPC::TestSink& sink() { return sink_; } 32 33 // Provides test access to how many times a bad message has been received. 34 int bad_msg_count() const { return bad_msg_count_; } 35 36 // RenderProcessHost implementation (public portion). 37 virtual void EnableSendQueue() OVERRIDE; 38 virtual bool Init() OVERRIDE; 39 virtual int GetNextRoutingID() OVERRIDE; 40 virtual void AddRoute(int32 routing_id, IPC::Listener* listener) OVERRIDE; 41 virtual void RemoveRoute(int32 routing_id) OVERRIDE; 42 virtual void AddObserver(RenderProcessHostObserver* observer) OVERRIDE; 43 virtual void RemoveObserver(RenderProcessHostObserver* observer) OVERRIDE; 44 virtual bool WaitForBackingStoreMsg(int render_widget_id, 45 const base::TimeDelta& max_delay, 46 IPC::Message* msg) OVERRIDE; 47 virtual void ReceivedBadMessage() OVERRIDE; 48 virtual void WidgetRestored() OVERRIDE; 49 virtual void WidgetHidden() OVERRIDE; 50 virtual int VisibleWidgetCount() const OVERRIDE; 51 virtual bool IsGuest() const OVERRIDE; 52 virtual StoragePartition* GetStoragePartition() const OVERRIDE; 53 virtual void AddWord(const base::string16& word); 54 virtual bool FastShutdownIfPossible() OVERRIDE; 55 virtual bool FastShutdownStarted() const OVERRIDE; 56 virtual void DumpHandles() OVERRIDE; 57 virtual base::ProcessHandle GetHandle() const OVERRIDE; 58 virtual TransportDIB* MapTransportDIB(TransportDIB::Id dib_id) OVERRIDE; 59 virtual TransportDIB* GetTransportDIB(TransportDIB::Id dib_id) OVERRIDE; 60 virtual int GetID() const OVERRIDE; 61 virtual bool HasConnection() const OVERRIDE; 62 virtual void SetIgnoreInputEvents(bool ignore_input_events) OVERRIDE; 63 virtual bool IgnoreInputEvents() const OVERRIDE; 64 virtual void Cleanup() OVERRIDE; 65 virtual void AddPendingView() OVERRIDE; 66 virtual void RemovePendingView() OVERRIDE; 67 virtual void SetSuddenTerminationAllowed(bool allowed) OVERRIDE; 68 virtual bool SuddenTerminationAllowed() const OVERRIDE; 69 virtual BrowserContext* GetBrowserContext() const OVERRIDE; 70 virtual bool InSameStoragePartition( 71 StoragePartition* partition) const OVERRIDE; 72 virtual IPC::ChannelProxy* GetChannel() OVERRIDE; 73 virtual void AddFilter(BrowserMessageFilter* filter) 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 int GetActiveViewCount(); 93 94 void SetIsGuest(bool is_guest) { 95 is_guest_ = is_guest; 96 } 97 98 private: 99 // Stores IPC messages that would have been sent to the renderer. 100 IPC::TestSink sink_; 101 TransportDIB* transport_dib_; 102 int bad_msg_count_; 103 const MockRenderProcessHostFactory* factory_; 104 int id_; 105 BrowserContext* browser_context_; 106 ObserverList<RenderProcessHostObserver> observers_; 107 108 IDMap<RenderWidgetHost> render_widget_hosts_; 109 int prev_routing_id_; 110 IDMap<IPC::Listener> listeners_; 111 bool fast_shutdown_started_; 112 bool deletion_callback_called_; 113 bool is_guest_; 114 115 DISALLOW_COPY_AND_ASSIGN(MockRenderProcessHost); 116 }; 117 118 class MockRenderProcessHostFactory : public RenderProcessHostFactory { 119 public: 120 MockRenderProcessHostFactory(); 121 virtual ~MockRenderProcessHostFactory(); 122 123 virtual RenderProcessHost* CreateRenderProcessHost( 124 BrowserContext* browser_context, 125 SiteInstance* site_instance) const OVERRIDE; 126 127 // Removes the given MockRenderProcessHost from the MockRenderProcessHost list 128 // without deleting it. When a test deletes a MockRenderProcessHost, we need 129 // to remove it from |processes_| to prevent it from being deleted twice. 130 void Remove(MockRenderProcessHost* host) const; 131 132 private: 133 // A list of MockRenderProcessHosts created by this object. This list is used 134 // for deleting all MockRenderProcessHosts that have not deleted by a test in 135 // the destructor and prevent them from being leaked. 136 mutable ScopedVector<MockRenderProcessHost> processes_; 137 138 DISALLOW_COPY_AND_ASSIGN(MockRenderProcessHostFactory); 139 }; 140 141 } // namespace content 142 143 #endif // CONTENT_PUBLIC_TEST_MOCK_RENDER_PROCESS_HOST_H_ 144