browser_test_utils.h revision 5821806d5e7f356e8fa4b058a389a808ea183019
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/memory/ref_counted.h" 15#include "base/process.h" 16#include "base/scoped_temp_dir.h" 17#include "base/string16.h" 18#include "content/public/browser/notification_observer.h" 19#include "content/public/browser/notification_registrar.h" 20#include "googleurl/src/gurl.h" 21#include "third_party/WebKit/Source/WebKit/chromium/public/WebInputEvent.h" 22#include "ui/base/keycodes/keyboard_codes.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 FilePath& path, const std::string& query_string); 54 55// Waits for a load stop for the specified |web_contents|'s controller, if the 56// tab is currently web_contents. Otherwise returns immediately. 57void WaitForLoadStop(WebContents* web_contents); 58 59// Causes the specified web_contents to crash. Blocks until it is crashed. 60void CrashTab(WebContents* web_contents); 61 62// Simulates clicking at the center of the given tab asynchronously; modifiers 63// may contain bits from WebInputEvent::Modifiers. 64void SimulateMouseClick(WebContents* web_contents, 65 int modifiers, 66 WebKit::WebMouseEvent::Button button); 67 68// Simulates asynchronously a mouse enter/move/leave event. 69void SimulateMouseEvent(WebContents* web_contents, 70 WebKit::WebInputEvent::Type type, 71 const gfx::Point& point); 72 73// Sends a key press asynchronously. 74void SimulateKeyPress(WebContents* web_contents, 75 ui::KeyboardCode key, 76 bool control, 77 bool shift, 78 bool alt, 79 bool command); 80 81// Executes the passed |script| in the frame pointed to by |frame_xpath| (use 82// empty string for main frame). The |script| should not invoke 83// domAutomationController.send(); otherwise, your test will hang or be flaky. 84// If you want to extract a result, use one of the below functions. 85// Returns true on success. 86bool ExecuteJavaScript(RenderViewHost* render_view_host, 87 const std::wstring& frame_xpath, 88 const std::wstring& script) WARN_UNUSED_RESULT; 89 90// The following methods executes the passed |script| in the frame pointed to by 91// |frame_xpath| (use empty string for main frame) and sets |result| to the 92// value returned by the script evaluation. 93// They return true on success, false if the script evaluation failed or did not 94// evaluate to the expected type. 95bool ExecuteJavaScriptAndExtractInt(RenderViewHost* render_view_host, 96 const std::wstring& frame_xpath, 97 const std::wstring& script, 98 int* result) WARN_UNUSED_RESULT; 99bool ExecuteJavaScriptAndExtractBool(RenderViewHost* render_view_host, 100 const std::wstring& frame_xpath, 101 const std::wstring& script, 102 bool* result) WARN_UNUSED_RESULT; 103bool ExecuteJavaScriptAndExtractString( 104 RenderViewHost* render_view_host, 105 const std::wstring& frame_xpath, 106 const std::wstring& script, 107 std::string* result) WARN_UNUSED_RESULT; 108 109// Returns the cookies for the given url. 110std::string GetCookies(BrowserContext* browser_context, const GURL& url); 111 112// Sets a cookie for the given url. Returns true on success. 113bool SetCookie(BrowserContext* browser_context, 114 const GURL& url, 115 const std::string& value); 116 117// Watches title changes on a tab, blocking until an expected title is set. 118class TitleWatcher : public NotificationObserver { 119 public: 120 // |web_contents| must be non-NULL and needs to stay alive for the 121 // entire lifetime of |this|. |expected_title| is the title that |this| 122 // will wait for. 123 TitleWatcher(WebContents* web_contents, 124 const string16& expected_title); 125 virtual ~TitleWatcher(); 126 127 // Adds another title to watch for. 128 void AlsoWaitForTitle(const string16& expected_title); 129 130 // Waits until the title matches either expected_title or one of the titles 131 // added with AlsoWaitForTitle. Returns the value of the most recently 132 // observed matching title. 133 const string16& WaitAndGetTitle() WARN_UNUSED_RESULT; 134 135 private: 136 // NotificationObserver 137 virtual void Observe(int type, 138 const NotificationSource& source, 139 const NotificationDetails& details) OVERRIDE; 140 141 WebContents* web_contents_; 142 std::vector<string16> expected_titles_; 143 NotificationRegistrar notification_registrar_; 144 scoped_refptr<MessageLoopRunner> message_loop_runner_; 145 146 // The most recently observed expected title, if any. 147 string16 observed_title_; 148 149 bool expected_title_observed_; 150 bool quit_loop_on_observation_; 151 152 DISALLOW_COPY_AND_ASSIGN(TitleWatcher); 153}; 154 155// Watches for responses from the DOMAutomationController and keeps them in a 156// queue. Useful for waiting for a message to be received. 157class DOMMessageQueue : public NotificationObserver { 158 public: 159 // Constructs a DOMMessageQueue and begins listening for messages from the 160 // DOMAutomationController. Do not construct this until the browser has 161 // started. 162 DOMMessageQueue(); 163 virtual ~DOMMessageQueue(); 164 165 // Removes all messages in the message queue. 166 void ClearQueue(); 167 168 // Wait for the next message to arrive. |message| will be set to the next 169 // message, if not null. Returns true on success. 170 bool WaitForMessage(std::string* message) WARN_UNUSED_RESULT; 171 172 // Overridden NotificationObserver methods. 173 virtual void Observe(int type, 174 const NotificationSource& source, 175 const NotificationDetails& details) OVERRIDE; 176 177 private: 178 NotificationRegistrar registrar_; 179 std::queue<std::string> message_queue_; 180 bool waiting_for_message_; 181 scoped_refptr<MessageLoopRunner> message_loop_runner_; 182 183 DISALLOW_COPY_AND_ASSIGN(DOMMessageQueue); 184}; 185 186} // namespace content 187 188#endif // CONTENT_PUBLIC_TEST_BROWSER_TEST_UTILS_H_ 189