| 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 #ifndef CONTENT_TEST_TEST_RENDERER_HOST_H_ | |
| 6 #define CONTENT_TEST_TEST_RENDERER_HOST_H_ | |
| 7 #pragma once | |
| 8 | |
| 9 #include "base/memory/scoped_ptr.h" | |
| 10 #include "base/message_loop.h" | |
| 11 #include "content/public/browser/render_view_host.h" | |
| 12 #include "content/public/common/page_transition_types.h" | |
| 13 #include "testing/gtest/include/gtest/gtest.h" | |
| 14 | |
| 15 #if defined(USE_AURA) | |
| 16 #include "ui/aura/test/aura_test_helper.h" | |
| 17 #endif | |
| 18 | |
| 19 namespace aura { | |
| 20 namespace test { | |
| 21 class AuraTestHelper; | |
| 22 } | |
| 23 } | |
| 24 | |
| 25 namespace content { | |
| 26 | |
| 27 class BrowserContext; | |
| 28 class MockRenderProcessHost; | |
| 29 class MockRenderProcessHostFactory; | |
| 30 class NavigationController; | |
| 31 class RenderProcessHostFactory; | |
| 32 class RenderViewHostDelegate; | |
| 33 class TestRenderViewHostFactory; | |
| 34 class WebContents; | |
| 35 | |
| 36 // An interface and utility for driving tests of RenderViewHost. | |
| 37 class RenderViewHostTester { | |
| 38 public: | |
| 39 // Retrieves the RenderViewHostTester that drives the specified | |
| 40 // RenderViewHost. The RenderViewHost must have been created while | |
| 41 // RenderViewHost testing was enabled; use a | |
| 42 // RenderViewHostTestEnabler instance (see below) to do this. | |
| 43 static RenderViewHostTester* For(RenderViewHost* host); | |
| 44 | |
| 45 // This removes the need to expose | |
| 46 // RenderViewHostImpl::set_send_accessibility_updated_notifications() | |
| 47 // outside of content. | |
| 48 static void EnableAccessibilityUpdatedNotifications(RenderViewHost* host); | |
| 49 | |
| 50 // If the given WebContentsImpl has a pending RVH, returns it, otherwise NULL. | |
| 51 static RenderViewHost* GetPendingForController( | |
| 52 NavigationController* controller); | |
| 53 | |
| 54 // This removes the need to expose | |
| 55 // RenderViewHostImpl::is_swapped_out() outside of content. | |
| 56 // | |
| 57 // This is safe to call on any RenderViewHost, not just ones | |
| 58 // constructed while a RenderViewHostTestEnabler is in play. | |
| 59 static bool IsRenderViewHostSwappedOut(RenderViewHost* rvh); | |
| 60 | |
| 61 // Calls the RenderViewHosts' private OnMessageReceived function with the | |
| 62 // given message. | |
| 63 static bool TestOnMessageReceived(RenderViewHost* rvh, | |
| 64 const IPC::Message& msg); | |
| 65 | |
| 66 virtual ~RenderViewHostTester() {} | |
| 67 | |
| 68 // Gives tests access to RenderViewHostImpl::CreateRenderView. | |
| 69 virtual bool CreateRenderView(const string16& frame_name, | |
| 70 int opener_route_id, | |
| 71 int32 max_page_id, | |
| 72 const std::string& embedder_channel_name, | |
| 73 int embedder_container_id) = 0; | |
| 74 | |
| 75 // Calls OnMsgNavigate on the RenderViewHost with the given information, | |
| 76 // setting the rest of the parameters in the message to the "typical" values. | |
| 77 // This is a helper function for simulating the most common types of loads. | |
| 78 virtual void SendNavigate(int page_id, const GURL& url) = 0; | |
| 79 | |
| 80 // Calls OnMsgNavigate on the RenderViewHost with the given information, | |
| 81 // including a custom PageTransition. Sets the rest of the | |
| 82 // parameters in the message to the "typical" values. This is a helper | |
| 83 // function for simulating the most common types of loads. | |
| 84 virtual void SendNavigateWithTransition(int page_id, const GURL& url, | |
| 85 PageTransition transition) = 0; | |
| 86 | |
| 87 // Calls OnMsgShouldCloseACK on the RenderViewHost with the given parameter. | |
| 88 virtual void SendShouldCloseACK(bool proceed) = 0; | |
| 89 | |
| 90 // If set, future loads will have |mime_type| set as the mime type. | |
| 91 // If not set, the mime type will default to "text/html". | |
| 92 virtual void SetContentsMimeType(const std::string& mime_type) = 0; | |
| 93 | |
| 94 // Simulates the SwapOut_ACK that fires if you commit a cross-site | |
| 95 // navigation without making any network requests. | |
| 96 virtual void SimulateSwapOutACK() = 0; | |
| 97 | |
| 98 // Makes the WasHidden/WasRestored calls to the RenderWidget that | |
| 99 // tell it it has been hidden or restored from having been hidden. | |
| 100 virtual void SimulateWasHidden() = 0; | |
| 101 virtual void SimulateWasRestored() = 0; | |
| 102 }; | |
| 103 | |
| 104 // You can instantiate only one class like this at a time. During its | |
| 105 // lifetime, RenderViewHost objects created may be used via | |
| 106 // RenderViewHostTester. | |
| 107 class RenderViewHostTestEnabler { | |
| 108 public: | |
| 109 RenderViewHostTestEnabler(); | |
| 110 ~RenderViewHostTestEnabler(); | |
| 111 | |
| 112 private: | |
| 113 DISALLOW_COPY_AND_ASSIGN(RenderViewHostTestEnabler); | |
| 114 friend class RenderViewHostTestHarness; | |
| 115 | |
| 116 scoped_ptr<MockRenderProcessHostFactory> rph_factory_; | |
| 117 scoped_ptr<TestRenderViewHostFactory> rvh_factory_; | |
| 118 }; | |
| 119 | |
| 120 // RenderViewHostTestHarness --------------------------------------------------- | |
| 121 class RenderViewHostTestHarness : public testing::Test { | |
| 122 public: | |
| 123 RenderViewHostTestHarness(); | |
| 124 virtual ~RenderViewHostTestHarness(); | |
| 125 | |
| 126 NavigationController& controller(); | |
| 127 virtual WebContents* web_contents(); | |
| 128 RenderViewHost* rvh(); | |
| 129 RenderViewHost* pending_rvh(); | |
| 130 RenderViewHost* active_rvh(); | |
| 131 BrowserContext* browser_context(); | |
| 132 MockRenderProcessHost* process(); | |
| 133 | |
| 134 // Frees the current WebContents for tests that want to test destruction. | |
| 135 void DeleteContents(); | |
| 136 | |
| 137 // Sets the current WebContents for tests that want to alter it. Takes | |
| 138 // ownership of the WebContents passed. | |
| 139 virtual void SetContents(WebContents* contents); | |
| 140 | |
| 141 // Creates a new test-enabled WebContents. Ownership passes to the | |
| 142 // caller. | |
| 143 WebContents* CreateTestWebContents(); | |
| 144 | |
| 145 // Cover for |contents()->NavigateAndCommit(url)|. See | |
| 146 // WebContentsTester::NavigateAndCommit for details. | |
| 147 void NavigateAndCommit(const GURL& url); | |
| 148 | |
| 149 // Simulates a reload of the current page. | |
| 150 void Reload(); | |
| 151 | |
| 152 protected: | |
| 153 // testing::Test | |
| 154 virtual void SetUp() OVERRIDE; | |
| 155 virtual void TearDown() OVERRIDE; | |
| 156 | |
| 157 #if defined(USE_AURA) | |
| 158 aura::RootWindow* root_window() { return aura_test_helper_->root_window(); } | |
| 159 #endif | |
| 160 | |
| 161 // Replaces the RPH being used. | |
| 162 void SetRenderProcessHostFactory(RenderProcessHostFactory* factory); | |
| 163 | |
| 164 // This browser context will be created in SetUp if it has not already been | |
| 165 // created. This allows tests to override the browser context if they so | |
| 166 // choose in their own SetUp function before calling the base class's (us) | |
| 167 // SetUp(). | |
| 168 scoped_ptr<BrowserContext> browser_context_; | |
| 169 | |
| 170 MessageLoopForUI message_loop_; | |
| 171 | |
| 172 private: | |
| 173 // It is important not to use this directly in the implementation as | |
| 174 // web_contents() and SetContents() are virtual and may be | |
| 175 // overridden by subclasses. | |
| 176 scoped_ptr<WebContents> contents_; | |
| 177 #if defined(USE_AURA) | |
| 178 scoped_ptr<aura::test::AuraTestHelper> aura_test_helper_; | |
| 179 #endif | |
| 180 RenderViewHostTestEnabler rvh_test_enabler_; | |
| 181 | |
| 182 DISALLOW_COPY_AND_ASSIGN(RenderViewHostTestHarness); | |
| 183 }; | |
| 184 | |
| 185 } // namespace content | |
| 186 | |
| 187 #endif // CONTENT_TEST_TEST_RENDERER_HOST_H_ | |
| OLD | NEW |