1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CONTENT_PUBLIC_TEST_BROWSER_TEST_UTILS_H_
#define CONTENT_PUBLIC_TEST_BROWSER_TEST_UTILS_H_
#include <queue>
#include <string>
#include <vector>
#include "base/callback_forward.h"
#include "base/compiler_specific.h"
#include "base/files/scoped_temp_dir.h"
#include "base/memory/ref_counted.h"
#include "base/process.h"
#include "base/string16.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h"
#include "googleurl/src/gurl.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebInputEvent.h"
#include "ui/base/keycodes/keyboard_codes.h"
#if defined(OS_WIN)
#include "base/win/scoped_handle.h"
#endif
class CommandLine;
namespace base {
class RunLoop;
}
namespace gfx {
class Point;
}
// A collections of functions designed for use with content_browsertests and
// browser_tests.
// TO BE CLEAR: any function here must work against both binaries. If it only
// works with browser_tests, it should be in chrome\test\base\ui_test_utils.h.
// If it only works with content_browsertests, it should be in
// content\test\content_browser_test_utils.h.
namespace content {
class BrowserContext;
class MessageLoopRunner;
class RenderViewHost;
class WebContents;
// Generate a URL for a file path including a query string.
GURL GetFileUrlWithQuery(const FilePath& path, const std::string& query_string);
// Waits for a load stop for the specified |web_contents|'s controller, if the
// tab is currently web_contents. Otherwise returns immediately.
void WaitForLoadStop(WebContents* web_contents);
// Causes the specified web_contents to crash. Blocks until it is crashed.
void CrashTab(WebContents* web_contents);
// Simulates clicking at the center of the given tab asynchronously; modifiers
// may contain bits from WebInputEvent::Modifiers.
void SimulateMouseClick(WebContents* web_contents,
int modifiers,
WebKit::WebMouseEvent::Button button);
// Simulates asynchronously a mouse enter/move/leave event.
void SimulateMouseEvent(WebContents* web_contents,
WebKit::WebInputEvent::Type type,
const gfx::Point& point);
// Sends a key press asynchronously.
void SimulateKeyPress(WebContents* web_contents,
ui::KeyboardCode key,
bool control,
bool shift,
bool alt,
bool command);
// Executes the passed |script| in the frame pointed to by |frame_xpath| (use
// empty string for main frame). The |script| should not invoke
// domAutomationController.send(); otherwise, your test will hang or be flaky.
// If you want to extract a result, use one of the below functions.
// Returns true on success.
bool ExecuteJavaScript(RenderViewHost* render_view_host,
const std::wstring& frame_xpath,
const std::wstring& script) WARN_UNUSED_RESULT;
// The following methods executes the passed |script| in the frame pointed to by
// |frame_xpath| (use empty string for main frame) and sets |result| to the
// value returned by the script evaluation.
// They return true on success, false if the script evaluation failed or did not
// evaluate to the expected type.
bool ExecuteJavaScriptAndExtractInt(RenderViewHost* render_view_host,
const std::wstring& frame_xpath,
const std::wstring& script,
int* result) WARN_UNUSED_RESULT;
bool ExecuteJavaScriptAndExtractBool(RenderViewHost* render_view_host,
const std::wstring& frame_xpath,
const std::wstring& script,
bool* result) WARN_UNUSED_RESULT;
bool ExecuteJavaScriptAndExtractString(
RenderViewHost* render_view_host,
const std::wstring& frame_xpath,
const std::wstring& script,
std::string* result) WARN_UNUSED_RESULT;
// Returns the cookies for the given url.
std::string GetCookies(BrowserContext* browser_context, const GURL& url);
// Sets a cookie for the given url. Returns true on success.
bool SetCookie(BrowserContext* browser_context,
const GURL& url,
const std::string& value);
// Watches title changes on a tab, blocking until an expected title is set.
class TitleWatcher : public NotificationObserver {
public:
// |web_contents| must be non-NULL and needs to stay alive for the
// entire lifetime of |this|. |expected_title| is the title that |this|
// will wait for.
TitleWatcher(WebContents* web_contents,
const string16& expected_title);
virtual ~TitleWatcher();
// Adds another title to watch for.
void AlsoWaitForTitle(const string16& expected_title);
// Waits until the title matches either expected_title or one of the titles
// added with AlsoWaitForTitle. Returns the value of the most recently
// observed matching title.
const string16& WaitAndGetTitle() WARN_UNUSED_RESULT;
private:
// NotificationObserver
virtual void Observe(int type,
const NotificationSource& source,
const NotificationDetails& details) OVERRIDE;
WebContents* web_contents_;
std::vector<string16> expected_titles_;
NotificationRegistrar notification_registrar_;
scoped_refptr<MessageLoopRunner> message_loop_runner_;
// The most recently observed expected title, if any.
string16 observed_title_;
bool expected_title_observed_;
bool quit_loop_on_observation_;
DISALLOW_COPY_AND_ASSIGN(TitleWatcher);
};
// Watches for responses from the DOMAutomationController and keeps them in a
// queue. Useful for waiting for a message to be received.
class DOMMessageQueue : public NotificationObserver {
public:
// Constructs a DOMMessageQueue and begins listening for messages from the
// DOMAutomationController. Do not construct this until the browser has
// started.
DOMMessageQueue();
virtual ~DOMMessageQueue();
// Removes all messages in the message queue.
void ClearQueue();
// Wait for the next message to arrive. |message| will be set to the next
// message, if not null. Returns true on success.
bool WaitForMessage(std::string* message) WARN_UNUSED_RESULT;
// Overridden NotificationObserver methods.
virtual void Observe(int type,
const NotificationSource& source,
const NotificationDetails& details) OVERRIDE;
private:
NotificationRegistrar registrar_;
std::queue<std::string> message_queue_;
bool waiting_for_message_;
scoped_refptr<MessageLoopRunner> message_loop_runner_;
DISALLOW_COPY_AND_ASSIGN(DOMMessageQueue);
};
} // namespace content
#endif // CONTENT_PUBLIC_TEST_BROWSER_TEST_UTILS_H_
|