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