Home | History | Annotate | Download | only in test
      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/public/test/test_renderer_host.h"
      6 
      7 #include "base/run_loop.h"
      8 #include "content/browser/renderer_host/render_view_host_factory.h"
      9 #include "content/browser/renderer_host/render_widget_host_impl.h"
     10 #include "content/browser/renderer_host/test_render_view_host.h"
     11 #include "content/browser/site_instance_impl.h"
     12 #include "content/browser/web_contents/navigation_entry_impl.h"
     13 #include "content/public/browser/browser_thread.h"
     14 #include "content/public/browser/web_contents.h"
     15 #include "content/public/test/mock_render_process_host.h"
     16 #include "content/public/test/test_browser_context.h"
     17 #include "content/test/test_render_view_host_factory.h"
     18 #include "content/test/test_web_contents.h"
     19 
     20 #if defined(OS_WIN)
     21 #include "ui/base/win/scoped_ole_initializer.h"
     22 #endif
     23 
     24 #if defined(USE_AURA)
     25 #include "ui/aura/test/aura_test_helper.h"
     26 #endif
     27 
     28 namespace content {
     29 
     30 // RenderViewHostTester -------------------------------------------------------
     31 
     32 // static
     33 RenderViewHostTester* RenderViewHostTester::For(RenderViewHost* host) {
     34   return static_cast<TestRenderViewHost*>(host);
     35 }
     36 
     37 // static
     38 RenderViewHost* RenderViewHostTester::GetPendingForController(
     39     NavigationController* controller) {
     40   WebContentsImpl* web_contents = static_cast<WebContentsImpl*>(
     41       controller->GetWebContents());
     42   return web_contents->GetRenderManagerForTesting()->pending_render_view_host();
     43 }
     44 
     45 // static
     46 bool RenderViewHostTester::IsRenderViewHostSwappedOut(RenderViewHost* rvh) {
     47   return static_cast<RenderViewHostImpl*>(rvh)->is_swapped_out();
     48 }
     49 
     50 // static
     51 bool RenderViewHostTester::TestOnMessageReceived(RenderViewHost* rvh,
     52                                                  const IPC::Message& msg) {
     53   return static_cast<RenderViewHostImpl*>(rvh)->OnMessageReceived(msg);
     54 }
     55 
     56 // static
     57 bool RenderViewHostTester::HasTouchEventHandler(RenderViewHost* rvh) {
     58   RenderWidgetHostImpl* host_impl = RenderWidgetHostImpl::From(rvh);
     59   return host_impl->has_touch_handler();
     60 }
     61 
     62 
     63 // RenderViewHostTestEnabler --------------------------------------------------
     64 
     65 RenderViewHostTestEnabler::RenderViewHostTestEnabler()
     66     : rph_factory_(new MockRenderProcessHostFactory()),
     67       rvh_factory_(new TestRenderViewHostFactory(rph_factory_.get())) {
     68 }
     69 
     70 RenderViewHostTestEnabler::~RenderViewHostTestEnabler() {
     71 }
     72 
     73 
     74 // RenderViewHostTestHarness --------------------------------------------------
     75 
     76 RenderViewHostTestHarness::RenderViewHostTestHarness()
     77     : thread_bundle_options_(TestBrowserThreadBundle::DEFAULT) {}
     78 
     79 RenderViewHostTestHarness::~RenderViewHostTestHarness() {
     80 }
     81 
     82 NavigationController& RenderViewHostTestHarness::controller() {
     83   return web_contents()->GetController();
     84 }
     85 
     86 WebContents* RenderViewHostTestHarness::web_contents() {
     87   return contents_.get();
     88 }
     89 
     90 RenderViewHost* RenderViewHostTestHarness::rvh() {
     91   return web_contents()->GetRenderViewHost();
     92 }
     93 
     94 RenderViewHost* RenderViewHostTestHarness::pending_rvh() {
     95   return static_cast<TestWebContents*>(web_contents())->
     96       GetRenderManagerForTesting()->pending_render_view_host();
     97 }
     98 
     99 RenderViewHost* RenderViewHostTestHarness::active_rvh() {
    100   return pending_rvh() ? pending_rvh() : rvh();
    101 }
    102 
    103 BrowserContext* RenderViewHostTestHarness::browser_context() {
    104   return browser_context_.get();
    105 }
    106 
    107 MockRenderProcessHost* RenderViewHostTestHarness::process() {
    108   return static_cast<MockRenderProcessHost*>(active_rvh()->GetProcess());
    109 }
    110 
    111 void RenderViewHostTestHarness::DeleteContents() {
    112   SetContents(NULL);
    113 }
    114 
    115 void RenderViewHostTestHarness::SetContents(WebContents* contents) {
    116   contents_.reset(contents);
    117 }
    118 
    119 WebContents* RenderViewHostTestHarness::CreateTestWebContents() {
    120   // Make sure we ran SetUp() already.
    121 #if defined(OS_WIN)
    122   DCHECK(ole_initializer_ != NULL);
    123 #endif
    124 #if defined(USE_AURA)
    125   DCHECK(aura_test_helper_ != NULL);
    126 #endif
    127 
    128   // This will be deleted when the WebContentsImpl goes away.
    129   SiteInstance* instance = SiteInstance::Create(browser_context_.get());
    130 
    131   return TestWebContents::Create(browser_context_.get(), instance);
    132 }
    133 
    134 void RenderViewHostTestHarness::NavigateAndCommit(const GURL& url) {
    135   static_cast<TestWebContents*>(web_contents())->NavigateAndCommit(url);
    136 }
    137 
    138 void RenderViewHostTestHarness::Reload() {
    139   NavigationEntry* entry = controller().GetLastCommittedEntry();
    140   DCHECK(entry);
    141   controller().Reload(false);
    142   static_cast<TestRenderViewHost*>(
    143       rvh())->SendNavigate(entry->GetPageID(), entry->GetURL());
    144 }
    145 
    146 void RenderViewHostTestHarness::FailedReload() {
    147   NavigationEntry* entry = controller().GetLastCommittedEntry();
    148   DCHECK(entry);
    149   controller().Reload(false);
    150   static_cast<TestRenderViewHost*>(
    151       rvh())->SendFailedNavigate(entry->GetPageID(), entry->GetURL());
    152 }
    153 
    154 void RenderViewHostTestHarness::SetUp() {
    155   thread_bundle_.reset(new TestBrowserThreadBundle(thread_bundle_options_));
    156 
    157 #if defined(OS_WIN)
    158   ole_initializer_.reset(new ui::ScopedOleInitializer());
    159 #endif
    160 #if defined(USE_AURA)
    161   aura_test_helper_.reset(
    162       new aura::test::AuraTestHelper(base::MessageLoopForUI::current()));
    163   aura_test_helper_->SetUp();
    164 #endif
    165 
    166   DCHECK(!browser_context_);
    167   browser_context_.reset(CreateBrowserContext());
    168 
    169   SetContents(CreateTestWebContents());
    170 }
    171 
    172 void RenderViewHostTestHarness::TearDown() {
    173   SetContents(NULL);
    174 #if defined(USE_AURA)
    175   aura_test_helper_->TearDown();
    176 #endif
    177   // Make sure that we flush any messages related to WebContentsImpl destruction
    178   // before we destroy the browser context.
    179   base::RunLoop().RunUntilIdle();
    180 
    181 #if defined(OS_WIN)
    182   ole_initializer_.reset();
    183 #endif
    184 
    185   // Delete any RenderProcessHosts before the BrowserContext goes away.
    186   if (rvh_test_enabler_.rph_factory_)
    187     rvh_test_enabler_.rph_factory_.reset();
    188 
    189   // Release the browser context by posting itself on the end of the task
    190   // queue. This is preferable to immediate deletion because it will behave
    191   // properly if the |rph_factory_| reset above enqueued any tasks which
    192   // depend on |browser_context_|.
    193   BrowserThread::DeleteSoon(content::BrowserThread::UI,
    194                             FROM_HERE,
    195                             browser_context_.release());
    196   thread_bundle_.reset();
    197 }
    198 
    199 BrowserContext* RenderViewHostTestHarness::CreateBrowserContext() {
    200   return new TestBrowserContext();
    201 }
    202 
    203 void RenderViewHostTestHarness::SetRenderProcessHostFactory(
    204     RenderProcessHostFactory* factory) {
    205     rvh_test_enabler_.rvh_factory_->set_render_process_host_factory(factory);
    206 }
    207 
    208 }  // namespace content
    209