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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
|
// Copyright (c) 2009 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.
#include "chrome/test/ui_test_utils.h"
#include "base/json_reader.h"
#include "base/message_loop.h"
#include "base/path_service.h"
#include "base/process_util.h"
#include "base/values.h"
#include "chrome/browser/browser.h"
#include "chrome/browser/dom_operation_notification_details.h"
#include "chrome/browser/download/download_manager.h"
#include "chrome/browser/tab_contents/navigation_controller.h"
#include "chrome/browser/tab_contents/navigation_entry.h"
#include "chrome/browser/tab_contents/tab_contents.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/notification_registrar.h"
#include "chrome/common/notification_service.h"
#if defined(TOOLKIT_VIEWS)
#include "views/focus/accelerator_handler.h"
#endif
#include "googleurl/src/gurl.h"
#include "net/base/net_util.h"
namespace ui_test_utils {
namespace {
// Used to block until a navigation completes.
class NavigationNotificationObserver : public NotificationObserver {
public:
NavigationNotificationObserver(NavigationController* controller,
int number_of_navigations)
: navigation_started_(false),
navigations_completed_(0),
number_of_navigations_(number_of_navigations) {
registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED,
Source<NavigationController>(controller));
registrar_.Add(this, NotificationType::LOAD_START,
Source<NavigationController>(controller));
registrar_.Add(this, NotificationType::LOAD_STOP,
Source<NavigationController>(controller));
RunMessageLoop();
}
virtual void Observe(NotificationType type,
const NotificationSource& source,
const NotificationDetails& details) {
if (type == NotificationType::NAV_ENTRY_COMMITTED ||
type == NotificationType::LOAD_START) {
navigation_started_ = true;
} else if (type == NotificationType::LOAD_STOP) {
if (navigation_started_ &&
++navigations_completed_ == number_of_navigations_) {
navigation_started_ = false;
MessageLoopForUI::current()->Quit();
}
}
}
private:
NotificationRegistrar registrar_;
// If true the navigation has started.
bool navigation_started_;
// The number of navigations that have been completed.
int navigations_completed_;
// The number of navigations to wait for.
int number_of_navigations_;
DISALLOW_COPY_AND_ASSIGN(NavigationNotificationObserver);
};
class DOMOperationObserver : public NotificationObserver {
public:
explicit DOMOperationObserver(RenderViewHost* render_view_host) {
registrar_.Add(this, NotificationType::DOM_OPERATION_RESPONSE,
Source<RenderViewHost>(render_view_host));
RunMessageLoop();
}
virtual void Observe(NotificationType type,
const NotificationSource& source,
const NotificationDetails& details) {
DCHECK(type == NotificationType::DOM_OPERATION_RESPONSE);
Details<DomOperationNotificationDetails> dom_op_details(details);
response_ = dom_op_details->json();
MessageLoopForUI::current()->Quit();
}
std::string response() const { return response_; }
private:
NotificationRegistrar registrar_;
std::string response_;
DISALLOW_COPY_AND_ASSIGN(DOMOperationObserver);
};
// DownloadsCompleteObserver waits for a given number of downloads to complete.
// Example usage:
//
// ui_test_utils::NavigateToURL(browser(), zip_url);
// DownloadsCompleteObserver wait_on_download(
// browser()->profile()->GetDownloadManager(), 1);
// /* |zip_url| download will be complete by this line. */
//
class DownloadsCompleteObserver : public DownloadManager::Observer,
public DownloadItem::Observer {
public:
explicit DownloadsCompleteObserver(DownloadManager* download_manager,
size_t wait_count)
: download_manager_(download_manager),
wait_count_(wait_count),
waiting_(false) {
download_manager_->AddObserver(this);
}
// CheckAllDownloadsComplete will be called when the DownloadManager
// fires it's ModelChanged() call, and also when incomplete downloads
// fire their OnDownloadUpdated().
bool CheckAllDownloadsComplete() {
if (downloads_.size() < wait_count_)
return false;
bool still_waiting = false;
std::vector<DownloadItem*>::iterator it = downloads_.begin();
for (; it != downloads_.end(); ++it) {
// We always remove ourselves as an observer, then re-add if the download
// isn't complete. This is to avoid having to track which downloads we
// are currently observing. Removing has no effect if we are not currently
// an observer.
(*it)->RemoveObserver(this);
if ((*it)->state() != DownloadItem::COMPLETE) {
(*it)->AddObserver(this);
still_waiting = true;
}
}
if (still_waiting)
return false;
download_manager_->RemoveObserver(this);
// waiting_ will have been set if not all downloads were complete on first
// pass below in SetDownloads().
if (waiting_)
MessageLoopForUI::current()->Quit();
return true;
}
// DownloadItem::Observer
virtual void OnDownloadUpdated(DownloadItem* download) {
if (download->state() == DownloadItem::COMPLETE) {
CheckAllDownloadsComplete();
}
}
virtual void OnDownloadOpened(DownloadItem* download) {}
// DownloadManager::Observer
virtual void ModelChanged() {
download_manager_->GetDownloads(this, L"");
}
virtual void SetDownloads(std::vector<DownloadItem*>& downloads) {
downloads_ = downloads;
if (CheckAllDownloadsComplete())
return;
if (!waiting_) {
waiting_ = true;
ui_test_utils::RunMessageLoop();
}
}
private:
// The observed download manager.
DownloadManager* download_manager_;
// The current downloads being tracked.
std::vector<DownloadItem*> downloads_;
// The number of downloads to wait on completing.
size_t wait_count_;
// Whether an internal message loop has been started and must be quit upon
// all downloads completing.
bool waiting_;
DISALLOW_COPY_AND_ASSIGN(DownloadsCompleteObserver);
};
// Used to block until an application modal dialog is shown.
class AppModalDialogObserver : public NotificationObserver {
public:
AppModalDialogObserver() {}
AppModalDialog* WaitForAppModalDialog() {
registrar_.Add(this, NotificationType::APP_MODAL_DIALOG_SHOWN,
NotificationService::AllSources());
dialog_ = NULL;
ui_test_utils::RunMessageLoop();
DCHECK(dialog_);
return dialog_;
}
virtual void Observe(NotificationType type,
const NotificationSource& source,
const NotificationDetails& details) {
if (type == NotificationType::APP_MODAL_DIALOG_SHOWN) {
registrar_.Remove(this, NotificationType::APP_MODAL_DIALOG_SHOWN,
NotificationService::AllSources());
dialog_ = Source<AppModalDialog>(source).ptr();
MessageLoopForUI::current()->Quit();
} else {
NOTREACHED();
}
}
private:
NotificationRegistrar registrar_;
AppModalDialog* dialog_;
DISALLOW_COPY_AND_ASSIGN(AppModalDialogObserver);
};
class CrashedRenderProcessObserver : public NotificationObserver {
public:
explicit CrashedRenderProcessObserver(RenderProcessHost* rph) {
registrar_.Add(this, NotificationType::RENDERER_PROCESS_CLOSED,
Source<RenderProcessHost>(rph));
ui_test_utils::RunMessageLoop();
}
virtual void Observe(NotificationType type,
const NotificationSource& source,
const NotificationDetails& details) {
if (type == NotificationType::RENDERER_PROCESS_CLOSED) {
MessageLoopForUI::current()->Quit();
} else {
NOTREACHED();
}
}
private:
NotificationRegistrar registrar_;
DISALLOW_COPY_AND_ASSIGN(CrashedRenderProcessObserver);
};
} // namespace
void RunMessageLoop() {
MessageLoopForUI* loop = MessageLoopForUI::current();
bool did_allow_task_nesting = loop->NestableTasksAllowed();
loop->SetNestableTasksAllowed(true);
#if defined(TOOLKIT_VIEWS)
views::AcceleratorHandler handler;
loop->Run(&handler);
#elif defined(OS_LINUX)
loop->Run(NULL);
#else
loop->Run();
#endif
loop->SetNestableTasksAllowed(did_allow_task_nesting);
}
bool GetCurrentTabTitle(const Browser* browser, string16* title) {
TabContents* tab_contents = browser->GetSelectedTabContents();
if (!tab_contents)
return false;
NavigationEntry* last_entry = tab_contents->controller().GetActiveEntry();
if (!last_entry)
return false;
title->assign(last_entry->title());
return true;
}
bool WaitForNavigationInCurrentTab(Browser* browser) {
TabContents* tab_contents = browser->GetSelectedTabContents();
if (!tab_contents)
return false;
WaitForNavigation(&tab_contents->controller());
return true;
}
bool WaitForNavigationsInCurrentTab(Browser* browser,
int number_of_navigations) {
TabContents* tab_contents = browser->GetSelectedTabContents();
if (!tab_contents)
return false;
WaitForNavigations(&tab_contents->controller(), number_of_navigations);
return true;
}
void WaitForNavigation(NavigationController* controller) {
WaitForNavigations(controller, 1);
}
void WaitForNavigations(NavigationController* controller,
int number_of_navigations) {
NavigationNotificationObserver observer(controller, number_of_navigations);
}
void NavigateToURL(Browser* browser, const GURL& url) {
NavigateToURLBlockUntilNavigationsComplete(browser, url, 1);
}
void NavigateToURLBlockUntilNavigationsComplete(Browser* browser,
const GURL& url,
int number_of_navigations) {
NavigationController* controller =
&browser->GetSelectedTabContents()->controller();
browser->OpenURL(url, GURL(), CURRENT_TAB, PageTransition::TYPED);
WaitForNavigations(controller, number_of_navigations);
}
Value* ExecuteJavaScript(RenderViewHost* render_view_host,
const std::wstring& frame_xpath,
const std::wstring& original_script) {
// TODO(jcampan): we should make the domAutomationController not require an
// automation id.
std::wstring script = L"window.domAutomationController.setAutomationId(0);" +
original_script;
render_view_host->ExecuteJavascriptInWebFrame(frame_xpath, script);
DOMOperationObserver dom_op_observer(render_view_host);
std::string json = dom_op_observer.response();
// Wrap |json| in an array before deserializing because valid JSON has an
// array or an object as the root.
json.insert(0, "[");
json.append("]");
scoped_ptr<Value> root_val(JSONReader::Read(json, true));
if (!root_val->IsType(Value::TYPE_LIST))
return NULL;
ListValue* list = static_cast<ListValue*>(root_val.get());
Value* result;
if (!list || !list->GetSize() ||
!list->Remove(0, &result)) // Remove gives us ownership of the value.
return NULL;
return result;
}
bool ExecuteJavaScriptAndExtractInt(RenderViewHost* render_view_host,
const std::wstring& frame_xpath,
const std::wstring& script,
int* result) {
DCHECK(result);
scoped_ptr<Value> value(ExecuteJavaScript(render_view_host, frame_xpath,
script));
if (!value.get())
return false;
return value->GetAsInteger(result);
}
bool ExecuteJavaScriptAndExtractBool(RenderViewHost* render_view_host,
const std::wstring& frame_xpath,
const std::wstring& script,
bool* result) {
DCHECK(result);
scoped_ptr<Value> value(ExecuteJavaScript(render_view_host, frame_xpath,
script));
if (!value.get())
return false;
return value->GetAsBoolean(result);
}
bool ExecuteJavaScriptAndExtractString(RenderViewHost* render_view_host,
const std::wstring& frame_xpath,
const std::wstring& script,
std::string* result) {
DCHECK(result);
scoped_ptr<Value> value(ExecuteJavaScript(render_view_host, frame_xpath,
script));
if (!value.get())
return false;
return value->GetAsString(result);
}
GURL GetTestUrl(const std::wstring& dir, const std::wstring file) {
FilePath path;
PathService::Get(chrome::DIR_TEST_DATA, &path);
path = path.Append(FilePath::FromWStringHack(dir));
path = path.Append(FilePath::FromWStringHack(file));
return net::FilePathToFileURL(path);
}
void WaitForDownloadCount(DownloadManager* download_manager, size_t count) {
DownloadsCompleteObserver download_observer(download_manager, count);
}
AppModalDialog* WaitForAppModalDialog() {
AppModalDialogObserver observer;
return observer.WaitForAppModalDialog();
}
void CrashTab(TabContents* tab) {
RenderProcessHost* rph = tab->render_view_host()->process();
base::KillProcess(rph->process().handle(), 0, false);
CrashedRenderProcessObserver crash_observer(rph);
}
} // namespace ui_test_utils
|