Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(343)

Side by Side Diff: content/public/test/browser_test_utils.cc

Issue 11753009: Simplify ExecuteJavaScript* functions. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Update prerender_browsertest.cc. Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « content/public/test/browser_test_utils.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/public/test/browser_test_utils.h" 5 #include "content/public/test/browser_test_utils.h"
6 6
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "base/json/json_reader.h" 8 #include "base/json/json_reader.h"
9 #include "base/path_service.h" 9 #include "base/path_service.h"
10 #include "base/process_util.h" 10 #include "base/process_util.h"
(...skipping 21 matching lines...) Expand all
32 #include "testing/gtest/include/gtest/gtest.h" 32 #include "testing/gtest/include/gtest/gtest.h"
33 33
34 static const int kDefaultWsPort = 8880; 34 static const int kDefaultWsPort = 8880;
35 35
36 namespace content { 36 namespace content {
37 namespace { 37 namespace {
38 38
39 class DOMOperationObserver : public NotificationObserver, 39 class DOMOperationObserver : public NotificationObserver,
40 public WebContentsObserver { 40 public WebContentsObserver {
41 public: 41 public:
42 explicit DOMOperationObserver(RenderViewHost* render_view_host) 42 explicit DOMOperationObserver(RenderViewHost* rvh)
43 : WebContentsObserver(WebContents::FromRenderViewHost(render_view_host)), 43 : WebContentsObserver(WebContents::FromRenderViewHost(rvh)),
44 did_respond_(false) { 44 did_respond_(false) {
45 registrar_.Add(this, NOTIFICATION_DOM_OPERATION_RESPONSE, 45 registrar_.Add(this, NOTIFICATION_DOM_OPERATION_RESPONSE,
46 Source<RenderViewHost>(render_view_host)); 46 Source<RenderViewHost>(rvh));
47 message_loop_runner_ = new MessageLoopRunner; 47 message_loop_runner_ = new MessageLoopRunner;
48 } 48 }
49 49
50 virtual void Observe(int type, 50 virtual void Observe(int type,
51 const NotificationSource& source, 51 const NotificationSource& source,
52 const NotificationDetails& details) OVERRIDE { 52 const NotificationDetails& details) OVERRIDE {
53 DCHECK(type == NOTIFICATION_DOM_OPERATION_RESPONSE); 53 DCHECK(type == NOTIFICATION_DOM_OPERATION_RESPONSE);
54 Details<DomOperationNotificationDetails> dom_op_details(details); 54 Details<DomOperationNotificationDetails> dom_op_details(details);
55 response_ = dom_op_details->json; 55 response_ = dom_op_details->json;
56 did_respond_ = true; 56 did_respond_ = true;
(...skipping 14 matching lines...) Expand all
71 private: 71 private:
72 NotificationRegistrar registrar_; 72 NotificationRegistrar registrar_;
73 std::string response_; 73 std::string response_;
74 bool did_respond_; 74 bool did_respond_;
75 scoped_refptr<MessageLoopRunner> message_loop_runner_; 75 scoped_refptr<MessageLoopRunner> message_loop_runner_;
76 76
77 DISALLOW_COPY_AND_ASSIGN(DOMOperationObserver); 77 DISALLOW_COPY_AND_ASSIGN(DOMOperationObserver);
78 }; 78 };
79 79
80 // Specifying a prototype so that we can add the WARN_UNUSED_RESULT attribute. 80 // Specifying a prototype so that we can add the WARN_UNUSED_RESULT attribute.
81 bool ExecuteJavaScriptHelper(RenderViewHost* render_view_host, 81 bool ExecuteScriptHelper(RenderViewHost* render_view_host,
82 const std::string& frame_xpath, 82 const std::string& frame_xpath,
83 const std::string& original_script, 83 const std::string& original_script,
84 scoped_ptr<Value>* result) WARN_UNUSED_RESULT; 84 scoped_ptr<Value>* result) WARN_UNUSED_RESULT;
85 85
86 // Executes the passed |original_script| in the frame pointed to by 86 // Executes the passed |original_script| in the frame pointed to by
87 // |frame_xpath|. If |result| is not NULL, stores the value that the evaluation 87 // |frame_xpath|. If |result| is not NULL, stores the value that the evaluation
88 // of the script in |result|. Returns true on success. 88 // of the script in |result|. Returns true on success.
89 bool ExecuteJavaScriptHelper(RenderViewHost* render_view_host, 89 bool ExecuteScriptHelper(RenderViewHost* render_view_host,
90 const std::string& frame_xpath, 90 const std::string& frame_xpath,
91 const std::string& original_script, 91 const std::string& original_script,
92 scoped_ptr<Value>* result) { 92 scoped_ptr<Value>* result) {
93 // TODO(jcampan): we should make the domAutomationController not require an 93 // TODO(jcampan): we should make the domAutomationController not require an
94 // automation id. 94 // automation id.
95 std::string script = 95 std::string script =
96 "window.domAutomationController.setAutomationId(0);" + original_script; 96 "window.domAutomationController.setAutomationId(0);" + original_script;
97 DOMOperationObserver dom_op_observer(render_view_host); 97 DOMOperationObserver dom_op_observer(render_view_host);
98 render_view_host->ExecuteJavascriptInWebFrame(UTF8ToUTF16(frame_xpath), 98 render_view_host->ExecuteJavascriptInWebFrame(UTF8ToUTF16(frame_xpath),
99 UTF8ToUTF16(script)); 99 UTF8ToUTF16(script));
100 std::string json; 100 std::string json;
101 if (!dom_op_observer.WaitAndGetResponse(&json)) { 101 if (!dom_op_observer.WaitAndGetResponse(&json)) {
102 DLOG(ERROR) << "Cannot communicate with DOMOperationObserver."; 102 DLOG(ERROR) << "Cannot communicate with DOMOperationObserver.";
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after
273 &char_event); 273 &char_event);
274 web_contents->GetRenderViewHost()->ForwardKeyboardEvent(char_event); 274 web_contents->GetRenderViewHost()->ForwardKeyboardEvent(char_event);
275 275
276 NativeWebKeyboardEvent event_up; 276 NativeWebKeyboardEvent event_up;
277 BuildSimpleWebKeyEvent( 277 BuildSimpleWebKeyEvent(
278 WebKit::WebInputEvent::KeyUp, key, control, shift, alt, command, 278 WebKit::WebInputEvent::KeyUp, key, control, shift, alt, command,
279 &event_up); 279 &event_up);
280 web_contents->GetRenderViewHost()->ForwardKeyboardEvent(event_up); 280 web_contents->GetRenderViewHost()->ForwardKeyboardEvent(event_up);
281 } 281 }
282 282
283 bool ExecuteJavaScript(RenderViewHost* render_view_host, 283 namespace internal {
284 const std::string& frame_xpath, 284
285 const std::string& original_script) { 285 ToRenderViewHost::ToRenderViewHost(WebContents* web_contents)
286 : render_view_host_(web_contents->GetRenderViewHost()) {
287 }
288
289 ToRenderViewHost::ToRenderViewHost(RenderViewHost* render_view_host)
290 : render_view_host_(render_view_host) {
291 }
292
293 } // namespace internal
294
295 bool ExecuteScriptInFrame(const internal::ToRenderViewHost& adapter,
296 const std::string& frame_xpath,
297 const std::string& original_script) {
286 std::string script = 298 std::string script =
287 original_script + ";window.domAutomationController.send(0);"; 299 original_script + ";window.domAutomationController.send(0);";
288 return ExecuteJavaScriptHelper(render_view_host, frame_xpath, script, NULL); 300 return ExecuteScriptHelper(adapter.render_view_host(), frame_xpath, script,
301 NULL);
289 } 302 }
290 303
291 bool ExecuteJavaScriptAndExtractInt(RenderViewHost* render_view_host, 304 bool ExecuteScriptInFrameAndExtractInt(
292 const std::string& frame_xpath, 305 const internal::ToRenderViewHost& adapter,
293 const std::string& script, 306 const std::string& frame_xpath,
294 int* result) { 307 const std::string& script,
308 int* result) {
295 DCHECK(result); 309 DCHECK(result);
296 scoped_ptr<Value> value; 310 scoped_ptr<Value> value;
297 if (!ExecuteJavaScriptHelper(render_view_host, frame_xpath, script, &value) || 311 if (!ExecuteScriptHelper(adapter.render_view_host(), frame_xpath, script,
298 !value.get()) 312 &value) || !value.get())
299 return false; 313 return false;
300 314
301 return value->GetAsInteger(result); 315 return value->GetAsInteger(result);
302 } 316 }
303 317
304 bool ExecuteJavaScriptAndExtractBool(RenderViewHost* render_view_host, 318 bool ExecuteScriptInFrameAndExtractBool(
305 const std::string& frame_xpath, 319 const internal::ToRenderViewHost& adapter,
306 const std::string& script, 320 const std::string& frame_xpath,
307 bool* result) { 321 const std::string& script,
322 bool* result) {
308 DCHECK(result); 323 DCHECK(result);
309 scoped_ptr<Value> value; 324 scoped_ptr<Value> value;
310 if (!ExecuteJavaScriptHelper(render_view_host, frame_xpath, script, &value) || 325 if (!ExecuteScriptHelper(adapter.render_view_host(), frame_xpath, script,
311 !value.get()) 326 &value) || !value.get())
312 return false; 327 return false;
313 328
314 return value->GetAsBoolean(result); 329 return value->GetAsBoolean(result);
315 } 330 }
316 331
317 bool ExecuteJavaScriptAndExtractString(RenderViewHost* render_view_host, 332 bool ExecuteScriptInFrameAndExtractString(
318 const std::string& frame_xpath, 333 const internal::ToRenderViewHost& adapter,
319 const std::string& script, 334 const std::string& frame_xpath,
320 std::string* result) { 335 const std::string& script,
336 std::string* result) {
321 DCHECK(result); 337 DCHECK(result);
322 scoped_ptr<Value> value; 338 scoped_ptr<Value> value;
323 if (!ExecuteJavaScriptHelper(render_view_host, frame_xpath, script, &value) || 339 if (!ExecuteScriptHelper(adapter.render_view_host(), frame_xpath, script,
324 !value.get()) 340 &value) || !value.get())
325 return false; 341 return false;
326 342
327 return value->GetAsString(result); 343 return value->GetAsString(result);
328 } 344 }
329 345
346 bool ExecuteScript(const internal::ToRenderViewHost& adapter,
347 const std::string& script) {
348 return ExecuteScriptInFrame(adapter, std::string(), script);
349 }
350
351 bool ExecuteScriptAndExtractInt(const internal::ToRenderViewHost& adapter,
352 const std::string& script, int* result) {
353 return ExecuteScriptInFrameAndExtractInt(adapter, std::string(), script,
354 result);
355 }
356
357 bool ExecuteScriptAndExtractBool(const internal::ToRenderViewHost& adapter,
358 const std::string& script, bool* result) {
359 return ExecuteScriptInFrameAndExtractBool(adapter, std::string(), script,
360 result);
361 }
362
363 bool ExecuteScriptAndExtractString(const internal::ToRenderViewHost& adapter,
364 const std::string& script,
365 std::string* result) {
366 return ExecuteScriptInFrameAndExtractString(adapter, std::string(), script,
367 result);
368 }
369
330 std::string GetCookies(BrowserContext* browser_context, const GURL& url) { 370 std::string GetCookies(BrowserContext* browser_context, const GURL& url) {
331 std::string cookies; 371 std::string cookies;
332 base::WaitableEvent event(true, false); 372 base::WaitableEvent event(true, false);
333 net::URLRequestContextGetter* context_getter = 373 net::URLRequestContextGetter* context_getter =
334 browser_context->GetRequestContext(); 374 browser_context->GetRequestContext();
335 375
336 BrowserThread::PostTask( 376 BrowserThread::PostTask(
337 BrowserThread::IO, FROM_HERE, 377 BrowserThread::IO, FROM_HERE,
338 base::Bind(&GetCookiesOnIOThread, url, 378 base::Bind(&GetCookiesOnIOThread, url,
339 make_scoped_refptr(context_getter), &event, &cookies)); 379 make_scoped_refptr(context_getter), &event, &cookies));
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
456 } 496 }
457 // The queue should not be empty, unless we were quit because of a timeout. 497 // The queue should not be empty, unless we were quit because of a timeout.
458 if (message_queue_.empty()) 498 if (message_queue_.empty())
459 return false; 499 return false;
460 if (message) 500 if (message)
461 *message = message_queue_.front(); 501 *message = message_queue_.front();
462 return true; 502 return true;
463 } 503 }
464 504
465 } // namespace content 505 } // namespace content
OLDNEW
« no previous file with comments | « content/public/test/browser_test_utils.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698