OLD | NEW |
(Empty) | |
| 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 #include "content/test/test_renderer_host.h" |
| 6 |
| 7 #include "content/browser/renderer_host/mock_render_process_host.h" |
| 8 #include "content/browser/renderer_host/render_view_host_factory.h" |
| 9 #include "content/browser/renderer_host/test_render_view_host.h" |
| 10 #include "content/browser/site_instance_impl.h" |
| 11 #include "content/browser/tab_contents/navigation_entry_impl.h" |
| 12 #include "content/browser/tab_contents/test_tab_contents.h" |
| 13 #include "content/test/test_browser_context.h" |
| 14 |
| 15 #if defined(USE_AURA) |
| 16 #include "ui/aura/root_window.h" |
| 17 #include "ui/aura/test/test_stacking_client.h" |
| 18 #endif |
| 19 |
| 20 namespace content { |
| 21 |
| 22 // Manages creation of the RenderViewHosts using our special subclass. This |
| 23 // automatically registers itself when it goes in scope, and unregisters itself |
| 24 // when it goes out of scope. Since you can't have more than one factory |
| 25 // registered at a time, you can only have one of these objects at a time. |
| 26 // |
| 27 // This is an implementation detail of this file and used only via |
| 28 // RenderViewHostTestEnabler. |
| 29 class TestRenderViewHostFactory : public RenderViewHostFactory { |
| 30 public: |
| 31 explicit TestRenderViewHostFactory( |
| 32 content::RenderProcessHostFactory* rph_factory); |
| 33 virtual ~TestRenderViewHostFactory(); |
| 34 |
| 35 virtual void set_render_process_host_factory( |
| 36 content::RenderProcessHostFactory* rph_factory); |
| 37 virtual content::RenderViewHost* CreateRenderViewHost( |
| 38 content::SiteInstance* instance, |
| 39 content::RenderViewHostDelegate* delegate, |
| 40 int routing_id, |
| 41 content::SessionStorageNamespace* session_storage) OVERRIDE; |
| 42 |
| 43 private: |
| 44 // This is a bit of a hack. With the current design of the site instances / |
| 45 // browsing instances, it's difficult to pass a RenderProcessHostFactory |
| 46 // around properly. |
| 47 // |
| 48 // Instead, we set it right before we create a new RenderViewHost, which |
| 49 // happens before the RenderProcessHost is created. This way, the instance |
| 50 // has the correct factory and creates our special RenderProcessHosts. |
| 51 content::RenderProcessHostFactory* render_process_host_factory_; |
| 52 |
| 53 DISALLOW_COPY_AND_ASSIGN(TestRenderViewHostFactory); |
| 54 }; |
| 55 |
| 56 TestRenderViewHostFactory::TestRenderViewHostFactory( |
| 57 content::RenderProcessHostFactory* rph_factory) |
| 58 : render_process_host_factory_(rph_factory) { |
| 59 RenderViewHostFactory::RegisterFactory(this); |
| 60 } |
| 61 |
| 62 TestRenderViewHostFactory::~TestRenderViewHostFactory() { |
| 63 RenderViewHostFactory::UnregisterFactory(); |
| 64 } |
| 65 |
| 66 void TestRenderViewHostFactory::set_render_process_host_factory( |
| 67 content::RenderProcessHostFactory* rph_factory) { |
| 68 render_process_host_factory_ = rph_factory; |
| 69 } |
| 70 |
| 71 content::RenderViewHost* TestRenderViewHostFactory::CreateRenderViewHost( |
| 72 SiteInstance* instance, |
| 73 RenderViewHostDelegate* delegate, |
| 74 int routing_id, |
| 75 SessionStorageNamespace* session_storage) { |
| 76 // See declaration of render_process_host_factory_ below. |
| 77 static_cast<SiteInstanceImpl*>(instance)-> |
| 78 set_render_process_host_factory(render_process_host_factory_); |
| 79 return new TestRenderViewHost(instance, delegate, routing_id); |
| 80 } |
| 81 |
| 82 // static |
| 83 RenderViewHostTester* RenderViewHostTester::For(RenderViewHost* host) { |
| 84 return static_cast<TestRenderViewHost*>(host); |
| 85 } |
| 86 |
| 87 // static |
| 88 void RenderViewHostTester::EnableAccessibilityUpdatedNotifications( |
| 89 RenderViewHost* host) { |
| 90 static_cast<RenderViewHostImpl*>( |
| 91 host)->set_send_accessibility_updated_notifications(true); |
| 92 } |
| 93 |
| 94 // static |
| 95 RenderViewHost* RenderViewHostTester::GetPendingForController( |
| 96 NavigationController* controller) { |
| 97 TabContents* tab_contents = static_cast<TabContents*>( |
| 98 controller->GetWebContents()); |
| 99 return tab_contents->GetRenderManagerForTesting()->pending_render_view_host(); |
| 100 } |
| 101 |
| 102 // static |
| 103 bool RenderViewHostTester::IsRenderViewHostSwappedOut(RenderViewHost* rvh) { |
| 104 return static_cast<RenderViewHostImpl*>(rvh)->is_swapped_out(); |
| 105 } |
| 106 |
| 107 RenderViewHostTestEnabler::RenderViewHostTestEnabler() |
| 108 : rph_factory_(new MockRenderProcessHostFactory()), |
| 109 rvh_factory_(new TestRenderViewHostFactory(rph_factory_.get())) { |
| 110 } |
| 111 |
| 112 RenderViewHostTestEnabler::~RenderViewHostTestEnabler() { |
| 113 } |
| 114 |
| 115 RenderViewHostTestHarness::RenderViewHostTestHarness() |
| 116 : contents_(NULL) { |
| 117 } |
| 118 |
| 119 RenderViewHostTestHarness::~RenderViewHostTestHarness() { |
| 120 } |
| 121 |
| 122 NavigationController& RenderViewHostTestHarness::controller() { |
| 123 return contents()->GetController(); |
| 124 } |
| 125 |
| 126 TestTabContents* RenderViewHostTestHarness::contents() { |
| 127 return contents_.get(); |
| 128 } |
| 129 |
| 130 RenderViewHost* RenderViewHostTestHarness::rvh() { |
| 131 return contents()->GetRenderViewHost(); |
| 132 } |
| 133 |
| 134 RenderViewHost* RenderViewHostTestHarness::pending_rvh() { |
| 135 return contents()->GetRenderManagerForTesting()->pending_render_view_host(); |
| 136 } |
| 137 |
| 138 RenderViewHost* RenderViewHostTestHarness::active_rvh() { |
| 139 return pending_rvh() ? pending_rvh() : rvh(); |
| 140 } |
| 141 |
| 142 BrowserContext* RenderViewHostTestHarness::browser_context() { |
| 143 return browser_context_.get(); |
| 144 } |
| 145 |
| 146 MockRenderProcessHost* RenderViewHostTestHarness::process() { |
| 147 return static_cast<MockRenderProcessHost*>(active_rvh()->GetProcess()); |
| 148 } |
| 149 |
| 150 void RenderViewHostTestHarness::DeleteContents() { |
| 151 SetContents(NULL); |
| 152 } |
| 153 |
| 154 void RenderViewHostTestHarness::SetContents(TestTabContents* contents) { |
| 155 contents_.reset(contents); |
| 156 } |
| 157 |
| 158 TestTabContents* RenderViewHostTestHarness::CreateTestTabContents() { |
| 159 // See comment above browser_context_ decl for why we check for NULL here. |
| 160 if (!browser_context_.get()) |
| 161 browser_context_.reset(new TestBrowserContext()); |
| 162 |
| 163 // This will be deleted when the TabContents goes away. |
| 164 SiteInstance* instance = SiteInstance::Create(browser_context_.get()); |
| 165 |
| 166 return new TestTabContents(browser_context_.get(), instance); |
| 167 } |
| 168 |
| 169 void RenderViewHostTestHarness::NavigateAndCommit(const GURL& url) { |
| 170 contents()->NavigateAndCommit(url); |
| 171 } |
| 172 |
| 173 void RenderViewHostTestHarness::Reload() { |
| 174 NavigationEntry* entry = controller().GetLastCommittedEntry(); |
| 175 DCHECK(entry); |
| 176 controller().Reload(false); |
| 177 static_cast<TestRenderViewHost*>( |
| 178 rvh())->SendNavigate(entry->GetPageID(), entry->GetURL()); |
| 179 } |
| 180 |
| 181 void RenderViewHostTestHarness::SetUp() { |
| 182 #if defined(USE_AURA) |
| 183 root_window_.reset(new aura::RootWindow); |
| 184 test_stacking_client_.reset( |
| 185 new aura::test::TestStackingClient(root_window_.get())); |
| 186 #endif |
| 187 SetContents(CreateTestTabContents()); |
| 188 } |
| 189 |
| 190 void RenderViewHostTestHarness::TearDown() { |
| 191 SetContents(NULL); |
| 192 #if defined(USE_AURA) |
| 193 test_stacking_client_.reset(); |
| 194 root_window_.reset(); |
| 195 #endif |
| 196 |
| 197 // Make sure that we flush any messages related to TabContents destruction |
| 198 // before we destroy the browser context. |
| 199 MessageLoop::current()->RunAllPending(); |
| 200 |
| 201 // Release the browser context on the UI thread. |
| 202 message_loop_.DeleteSoon(FROM_HERE, browser_context_.release()); |
| 203 message_loop_.RunAllPending(); |
| 204 } |
| 205 |
| 206 void RenderViewHostTestHarness::SetRenderProcessHostFactory( |
| 207 RenderProcessHostFactory* factory) { |
| 208 rvh_test_enabler_.rvh_factory_->set_render_process_host_factory(factory); |
| 209 } |
| 210 |
| 211 } // namespace content |
OLD | NEW |