browser_test_utils.h revision 58537e28ecd584eab876aee8be7156509866d23a
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_BROWSER_TEST_UTILS_H_ 6#define CONTENT_PUBLIC_TEST_BROWSER_TEST_UTILS_H_ 7 8#include <queue> 9#include <string> 10#include <vector> 11 12#include "base/callback_forward.h" 13#include "base/compiler_specific.h" 14#include "base/files/scoped_temp_dir.h" 15#include "base/memory/ref_counted.h" 16#include "base/process/process.h" 17#include "base/strings/string16.h" 18#include "content/public/browser/notification_observer.h" 19#include "content/public/browser/notification_registrar.h" 20#include "third_party/WebKit/public/web/WebInputEvent.h" 21#include "ui/base/keycodes/keyboard_codes.h" 22#include "url/gurl.h" 23 24#if defined(OS_WIN) 25#include "base/win/scoped_handle.h" 26#endif 27 28class CommandLine; 29 30namespace base { 31class RunLoop; 32} 33 34namespace gfx { 35class Point; 36} 37 38// A collections of functions designed for use with content_browsertests and 39// browser_tests. 40// TO BE CLEAR: any function here must work against both binaries. If it only 41// works with browser_tests, it should be in chrome\test\base\ui_test_utils.h. 42// If it only works with content_browsertests, it should be in 43// content\test\content_browser_test_utils.h. 44 45namespace content { 46 47class BrowserContext; 48class MessageLoopRunner; 49class RenderViewHost; 50class WebContents; 51 52// Generate a URL for a file path including a query string. 53GURL GetFileUrlWithQuery(const base::FilePath& path, 54 const std::string& query_string); 55 56// Waits for a load stop for the specified |web_contents|'s controller, if the 57// tab is currently web_contents. Otherwise returns immediately. 58void WaitForLoadStop(WebContents* web_contents); 59 60// Causes the specified web_contents to crash. Blocks until it is crashed. 61void CrashTab(WebContents* web_contents); 62 63// Simulates clicking at the center of the given tab asynchronously; modifiers 64// may contain bits from WebInputEvent::Modifiers. 65void SimulateMouseClick(WebContents* web_contents, 66 int modifiers, 67 WebKit::WebMouseEvent::Button button); 68 69// Simulates clicking at the point |point| of the given tab asynchronously; 70// modifiers may contain bits from WebInputEvent::Modifiers. 71void SimulateMouseClickAt(WebContents* web_contents, 72 int modifiers, 73 WebKit::WebMouseEvent::Button button, 74 const gfx::Point& point); 75 76// Simulates asynchronously a mouse enter/move/leave event. 77void SimulateMouseEvent(WebContents* web_contents, 78 WebKit::WebInputEvent::Type type, 79 const gfx::Point& point); 80 81// Sends a key press asynchronously. 82// The native code of the key event will be set to InvalidNativeKeycode(). 83// |key_code| alone is good enough for scenarios that only need the char 84// value represented by a key event and not the physical key on the keyboard 85// or the keyboard layout. 86// For scenarios such as chromoting that need the native code, 87// SimulateKeyPressWithCode should be used. 88void SimulateKeyPress(WebContents* web_contents, 89 ui::KeyboardCode key_code, 90 bool control, 91 bool shift, 92 bool alt, 93 bool command); 94 95// Sends a key press asynchronously. 96// |code| specifies the UIEvents (aka: DOM4Events) value of the key: 97// https://dvcs.w3.org/hg/d4e/raw-file/tip/source_respec.htm 98// The native code of the key event will be set based on |code|. 99// See ui/base/keycodes/vi usb_keycode_map.h for mappings between |code| 100// and the native code. 101// Examples of the various codes: 102// key_code: VKEY_A 103// code: "KeyA" 104// native key code: 0x001e (for Windows). 105// native key code: 0x0026 (for Linux). 106void SimulateKeyPressWithCode(WebContents* web_contents, 107 ui::KeyboardCode key_code, 108 const char* code, 109 bool control, 110 bool shift, 111 bool alt, 112 bool command); 113 114// Allow ExecuteScript* methods to target either a WebContents or a 115// RenderViewHost. Targetting a WebContents means executing script in the 116// RenderViewHost returned by WebContents::GetRenderViewHost(), which is the 117// "current" RenderViewHost. Pass a specific RenderViewHost to target, for 118// example, a "swapped-out" RenderViewHost. 119namespace internal { 120class ToRenderViewHost { 121 public: 122 ToRenderViewHost(WebContents* web_contents); 123 ToRenderViewHost(RenderViewHost* render_view_host); 124 125 RenderViewHost* render_view_host() const { return render_view_host_; } 126 127 private: 128 RenderViewHost* render_view_host_; 129}; 130} // namespace internal 131 132// Executes the passed |script| in the frame pointed to by |frame_xpath| (use 133// empty string for main frame). The |script| should not invoke 134// domAutomationController.send(); otherwise, your test will hang or be flaky. 135// If you want to extract a result, use one of the below functions. 136// Returns true on success. 137bool ExecuteScriptInFrame(const internal::ToRenderViewHost& adapter, 138 const std::string& frame_xpath, 139 const std::string& script) WARN_UNUSED_RESULT; 140 141// The following methods executes the passed |script| in the frame pointed to by 142// |frame_xpath| (use empty string for main frame) and sets |result| to the 143// value passed to "window.domAutomationController.send" by the executed script. 144// They return true on success, false if the script execution failed or did not 145// evaluate to the expected type. 146bool ExecuteScriptInFrameAndExtractInt( 147 const internal::ToRenderViewHost& adapter, 148 const std::string& frame_xpath, 149 const std::string& script, 150 int* result) WARN_UNUSED_RESULT; 151bool ExecuteScriptInFrameAndExtractBool( 152 const internal::ToRenderViewHost& adapter, 153 const std::string& frame_xpath, 154 const std::string& script, 155 bool* result) WARN_UNUSED_RESULT; 156bool ExecuteScriptInFrameAndExtractString( 157 const internal::ToRenderViewHost& adapter, 158 const std::string& frame_xpath, 159 const std::string& script, 160 std::string* result) WARN_UNUSED_RESULT; 161 162// Top-frame script execution helpers (a.k.a., the common case): 163bool ExecuteScript(const internal::ToRenderViewHost& adapter, 164 const std::string& script) WARN_UNUSED_RESULT; 165bool ExecuteScriptAndExtractInt(const internal::ToRenderViewHost& adapter, 166 const std::string& script, 167 int* result) WARN_UNUSED_RESULT; 168bool ExecuteScriptAndExtractBool(const internal::ToRenderViewHost& adapter, 169 const std::string& script, 170 bool* result) WARN_UNUSED_RESULT; 171bool ExecuteScriptAndExtractString(const internal::ToRenderViewHost& adapter, 172 const std::string& script, 173 std::string* result) WARN_UNUSED_RESULT; 174 175// Executes the WebUI resource test runner injecting each resource ID in 176// |js_resource_ids| prior to executing the tests. 177// 178// Returns true if tests ran successfully, false otherwise. 179bool ExecuteWebUIResourceTest(const internal::ToRenderViewHost& adapter, 180 const std::vector<int>& js_resource_ids); 181 182// Returns the cookies for the given url. 183std::string GetCookies(BrowserContext* browser_context, const GURL& url); 184 185// Sets a cookie for the given url. Returns true on success. 186bool SetCookie(BrowserContext* browser_context, 187 const GURL& url, 188 const std::string& value); 189 190// Watches title changes on a tab, blocking until an expected title is set. 191class TitleWatcher : public NotificationObserver { 192 public: 193 // |web_contents| must be non-NULL and needs to stay alive for the 194 // entire lifetime of |this|. |expected_title| is the title that |this| 195 // will wait for. 196 TitleWatcher(WebContents* web_contents, 197 const string16& expected_title); 198 virtual ~TitleWatcher(); 199 200 // Adds another title to watch for. 201 void AlsoWaitForTitle(const string16& expected_title); 202 203 // Waits until the title matches either expected_title or one of the titles 204 // added with AlsoWaitForTitle. Returns the value of the most recently 205 // observed matching title. 206 const string16& WaitAndGetTitle() WARN_UNUSED_RESULT; 207 208 private: 209 // NotificationObserver 210 virtual void Observe(int type, 211 const NotificationSource& source, 212 const NotificationDetails& details) OVERRIDE; 213 214 WebContents* web_contents_; 215 std::vector<string16> expected_titles_; 216 NotificationRegistrar notification_registrar_; 217 scoped_refptr<MessageLoopRunner> message_loop_runner_; 218 219 // The most recently observed expected title, if any. 220 string16 observed_title_; 221 222 bool expected_title_observed_; 223 bool quit_loop_on_observation_; 224 225 DISALLOW_COPY_AND_ASSIGN(TitleWatcher); 226}; 227 228// Watches for responses from the DOMAutomationController and keeps them in a 229// queue. Useful for waiting for a message to be received. 230class DOMMessageQueue : public NotificationObserver { 231 public: 232 // Constructs a DOMMessageQueue and begins listening for messages from the 233 // DOMAutomationController. Do not construct this until the browser has 234 // started. 235 DOMMessageQueue(); 236 virtual ~DOMMessageQueue(); 237 238 // Removes all messages in the message queue. 239 void ClearQueue(); 240 241 // Wait for the next message to arrive. |message| will be set to the next 242 // message, if not null. Returns true on success. 243 bool WaitForMessage(std::string* message) WARN_UNUSED_RESULT; 244 245 // Overridden NotificationObserver methods. 246 virtual void Observe(int type, 247 const NotificationSource& source, 248 const NotificationDetails& details) OVERRIDE; 249 250 private: 251 NotificationRegistrar registrar_; 252 std::queue<std::string> message_queue_; 253 bool waiting_for_message_; 254 scoped_refptr<MessageLoopRunner> message_loop_runner_; 255 256 DISALLOW_COPY_AND_ASSIGN(DOMMessageQueue); 257}; 258 259} // namespace content 260 261#endif // CONTENT_PUBLIC_TEST_BROWSER_TEST_UTILS_H_ 262