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_RENDER_VIEW_TEST_H_ 6 #define CONTENT_PUBLIC_TEST_RENDER_VIEW_TEST_H_ 7 8 #include <string> 9 10 #include "base/command_line.h" 11 #include "base/memory/scoped_ptr.h" 12 #include "base/message_loop/message_loop.h" 13 #include "base/strings/string16.h" 14 #include "content/public/browser/native_web_keyboard_event.h" 15 #include "content/public/common/main_function_params.h" 16 #include "content/public/common/page_state.h" 17 #include "content/public/test/mock_render_thread.h" 18 #include "testing/gtest/include/gtest/gtest.h" 19 #include "third_party/WebKit/public/platform/Platform.h" 20 #include "third_party/WebKit/public/web/WebFrame.h" 21 22 namespace blink { 23 class WebWidget; 24 } 25 26 namespace gfx { 27 class Rect; 28 } 29 30 namespace content { 31 class ContentBrowserClient; 32 class ContentClient; 33 class ContentRendererClient; 34 class MockRenderProcess; 35 class PageState; 36 class RendererMainPlatformDelegate; 37 class RendererWebKitPlatformSupportImplNoSandboxImpl; 38 class RenderView; 39 40 class RenderViewTest : public testing::Test { 41 public: 42 // A special WebKitPlatformSupportImpl class for getting rid off the 43 // dependency to the sandbox, which is not available in RenderViewTest. 44 class RendererWebKitPlatformSupportImplNoSandbox { 45 public: 46 RendererWebKitPlatformSupportImplNoSandbox(); 47 ~RendererWebKitPlatformSupportImplNoSandbox(); 48 blink::Platform* Get(); 49 50 private: 51 scoped_ptr<RendererWebKitPlatformSupportImplNoSandboxImpl> 52 webkit_platform_support_; 53 }; 54 55 RenderViewTest(); 56 virtual ~RenderViewTest(); 57 58 protected: 59 // Spins the message loop to process all messages that are currently pending. 60 void ProcessPendingMessages(); 61 62 // Returns a pointer to the main frame. 63 blink::WebLocalFrame* GetMainFrame(); 64 65 // Executes the given JavaScript in the context of the main frame. The input 66 // is a NULL-terminated UTF-8 string. 67 void ExecuteJavaScript(const char* js); 68 69 // Executes the given JavaScript and sets the int value it evaluates to in 70 // |result|. 71 // Returns true if the JavaScript was evaluated correctly to an int value, 72 // false otherwise. 73 bool ExecuteJavaScriptAndReturnIntValue(const base::string16& script, 74 int* result); 75 76 // Loads the given HTML into the main frame as a data: URL and blocks until 77 // the navigation is committed. 78 void LoadHTML(const char* html); 79 80 // Returns the current PageState. 81 PageState GetCurrentPageState(); 82 83 // Navigates the main frame back or forward in session history and commits. 84 // The caller must capture a PageState for the target page. 85 void GoBack(const PageState& state); 86 void GoForward(const PageState& state); 87 88 // Sends one native key event over IPC. 89 void SendNativeKeyEvent(const NativeWebKeyboardEvent& key_event); 90 91 // Send a raw keyboard event to the renderer. 92 void SendWebKeyboardEvent(const blink::WebKeyboardEvent& key_event); 93 94 // Send a raw mouse event to the renderer. 95 void SendWebMouseEvent(const blink::WebMouseEvent& key_event); 96 97 // Returns the bounds (coordinates and size) of the element with id 98 // |element_id|. Returns an empty rect if such an element was not found. 99 gfx::Rect GetElementBounds(const std::string& element_id); 100 101 // Sends a left mouse click in the middle of the element with id |element_id|. 102 // Returns true if the event was sent, false otherwise (typically because 103 // the element was not found). 104 bool SimulateElementClick(const std::string& element_id); 105 106 // Simulates |node| being focused. 107 void SetFocused(const blink::WebNode& node); 108 109 // Clears anything associated with the browsing history. 110 void ClearHistory(); 111 112 // Simulates a navigation with a type of reload to the given url. 113 void Reload(const GURL& url); 114 115 // Returns the IPC message ID of the navigation message. 116 uint32 GetNavigationIPCType(); 117 118 // Resize the view. 119 void Resize(gfx::Size new_size, 120 gfx::Rect resizer_rect, 121 bool is_fullscreen); 122 123 // These are all methods from RenderViewImpl that we expose to testing code. 124 bool OnMessageReceived(const IPC::Message& msg); 125 void DidNavigateWithinPage(blink::WebLocalFrame* frame, 126 bool is_new_navigation); 127 void SendContentStateImmediately(); 128 blink::WebWidget* GetWebWidget(); 129 130 // Allows a subclass to override the various content client implementations. 131 virtual ContentClient* CreateContentClient(); 132 virtual ContentBrowserClient* CreateContentBrowserClient(); 133 virtual ContentRendererClient* CreateContentRendererClient(); 134 135 // testing::Test 136 virtual void SetUp() OVERRIDE; 137 138 virtual void TearDown() OVERRIDE; 139 140 base::MessageLoop msg_loop_; 141 scoped_ptr<MockRenderProcess> mock_process_; 142 // We use a naked pointer because we don't want to expose RenderViewImpl in 143 // the embedder's namespace. 144 RenderView* view_; 145 RendererWebKitPlatformSupportImplNoSandbox webkit_platform_support_; 146 scoped_ptr<ContentClient> content_client_; 147 scoped_ptr<ContentBrowserClient> content_browser_client_; 148 scoped_ptr<ContentRendererClient> content_renderer_client_; 149 scoped_ptr<MockRenderThread> render_thread_; 150 151 // Used to setup the process so renderers can run. 152 scoped_ptr<RendererMainPlatformDelegate> platform_; 153 scoped_ptr<MainFunctionParams> params_; 154 scoped_ptr<base::CommandLine> command_line_; 155 156 #if defined(OS_MACOSX) 157 scoped_ptr<base::mac::ScopedNSAutoreleasePool> autorelease_pool_; 158 #endif 159 160 private: 161 void GoToOffset(int offset, const PageState& state); 162 }; 163 164 } // namespace content 165 166 #endif // CONTENT_PUBLIC_TEST_RENDER_VIEW_TEST_H_ 167