OLD | NEW |
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 15 matching lines...) Expand all Loading... |
26 #include "content/public/test/test_utils.h" | 26 #include "content/public/test/test_utils.h" |
27 #include "net/cookies/cookie_store.h" | 27 #include "net/cookies/cookie_store.h" |
28 #include "net/test/python_utils.h" | 28 #include "net/test/python_utils.h" |
29 #include "net/url_request/url_request_context.h" | 29 #include "net/url_request/url_request_context.h" |
30 #include "net/url_request/url_request_context_getter.h" | 30 #include "net/url_request/url_request_context_getter.h" |
31 #include "testing/gtest/include/gtest/gtest.h" | 31 #include "testing/gtest/include/gtest/gtest.h" |
32 | 32 |
33 static const int kDefaultWsPort = 8880; | 33 static const int kDefaultWsPort = 8880; |
34 | 34 |
35 namespace content { | 35 namespace content { |
36 | |
37 namespace { | 36 namespace { |
38 | 37 |
39 class DOMOperationObserver : public NotificationObserver, | 38 class DOMOperationObserver : public NotificationObserver, |
40 public WebContentsObserver { | 39 public WebContentsObserver { |
41 public: | 40 public: |
42 explicit DOMOperationObserver(RenderViewHost* render_view_host) | 41 explicit DOMOperationObserver(RenderViewHost* render_view_host) |
43 : WebContentsObserver(WebContents::FromRenderViewHost(render_view_host)), | 42 : WebContentsObserver(WebContents::FromRenderViewHost(render_view_host)), |
44 did_respond_(false) { | 43 did_respond_(false) { |
45 registrar_.Add(this, NOTIFICATION_DOM_OPERATION_RESPONSE, | 44 registrar_.Add(this, NOTIFICATION_DOM_OPERATION_RESPONSE, |
46 Source<RenderViewHost>(render_view_host)); | 45 Source<RenderViewHost>(render_view_host)); |
(...skipping 25 matching lines...) Expand all Loading... |
72 NotificationRegistrar registrar_; | 71 NotificationRegistrar registrar_; |
73 std::string response_; | 72 std::string response_; |
74 bool did_respond_; | 73 bool did_respond_; |
75 scoped_refptr<MessageLoopRunner> message_loop_runner_; | 74 scoped_refptr<MessageLoopRunner> message_loop_runner_; |
76 | 75 |
77 DISALLOW_COPY_AND_ASSIGN(DOMOperationObserver); | 76 DISALLOW_COPY_AND_ASSIGN(DOMOperationObserver); |
78 }; | 77 }; |
79 | 78 |
80 // Specifying a prototype so that we can add the WARN_UNUSED_RESULT attribute. | 79 // Specifying a prototype so that we can add the WARN_UNUSED_RESULT attribute. |
81 bool ExecuteJavaScriptHelper(RenderViewHost* render_view_host, | 80 bool ExecuteJavaScriptHelper(RenderViewHost* render_view_host, |
82 const std::wstring& frame_xpath, | 81 const std::string& frame_xpath, |
83 const std::wstring& original_script, | 82 const std::string& original_script, |
84 scoped_ptr<Value>* result) WARN_UNUSED_RESULT; | 83 scoped_ptr<Value>* result) WARN_UNUSED_RESULT; |
85 | 84 |
86 // Executes the passed |original_script| in the frame pointed to by | 85 // 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 | 86 // |frame_xpath|. If |result| is not NULL, stores the value that the evaluation |
88 // of the script in |result|. Returns true on success. | 87 // of the script in |result|. Returns true on success. |
89 bool ExecuteJavaScriptHelper(RenderViewHost* render_view_host, | 88 bool ExecuteJavaScriptHelper(RenderViewHost* render_view_host, |
90 const std::wstring& frame_xpath, | 89 const std::string& frame_xpath, |
91 const std::wstring& original_script, | 90 const std::string& original_script, |
92 scoped_ptr<Value>* result) { | 91 scoped_ptr<Value>* result) { |
93 // TODO(jcampan): we should make the domAutomationController not require an | 92 // TODO(jcampan): we should make the domAutomationController not require an |
94 // automation id. | 93 // automation id. |
95 std::wstring script = L"window.domAutomationController.setAutomationId(0);" + | 94 std::string script = |
96 original_script; | 95 "window.domAutomationController.setAutomationId(0);" + original_script; |
97 DOMOperationObserver dom_op_observer(render_view_host); | 96 DOMOperationObserver dom_op_observer(render_view_host); |
98 render_view_host->ExecuteJavascriptInWebFrame(WideToUTF16Hack(frame_xpath), | 97 render_view_host->ExecuteJavascriptInWebFrame(UTF8ToUTF16(frame_xpath), |
99 WideToUTF16Hack(script)); | 98 UTF8ToUTF16(script)); |
100 std::string json; | 99 std::string json; |
101 if (!dom_op_observer.WaitAndGetResponse(&json)) { | 100 if (!dom_op_observer.WaitAndGetResponse(&json)) { |
102 DLOG(ERROR) << "Cannot communicate with DOMOperationObserver."; | 101 DLOG(ERROR) << "Cannot communicate with DOMOperationObserver."; |
103 return false; | 102 return false; |
104 } | 103 } |
105 | 104 |
106 // Nothing more to do for callers that ignore the returned JS value. | 105 // Nothing more to do for callers that ignore the returned JS value. |
107 if (!result) | 106 if (!result) |
108 return true; | 107 return true; |
109 | 108 |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
274 web_contents->GetRenderViewHost()->ForwardKeyboardEvent(char_event); | 273 web_contents->GetRenderViewHost()->ForwardKeyboardEvent(char_event); |
275 | 274 |
276 NativeWebKeyboardEvent event_up; | 275 NativeWebKeyboardEvent event_up; |
277 BuildSimpleWebKeyEvent( | 276 BuildSimpleWebKeyEvent( |
278 WebKit::WebInputEvent::KeyUp, key, control, shift, alt, command, | 277 WebKit::WebInputEvent::KeyUp, key, control, shift, alt, command, |
279 &event_up); | 278 &event_up); |
280 web_contents->GetRenderViewHost()->ForwardKeyboardEvent(event_up); | 279 web_contents->GetRenderViewHost()->ForwardKeyboardEvent(event_up); |
281 } | 280 } |
282 | 281 |
283 bool ExecuteJavaScript(RenderViewHost* render_view_host, | 282 bool ExecuteJavaScript(RenderViewHost* render_view_host, |
284 const std::wstring& frame_xpath, | 283 const std::string& frame_xpath, |
285 const std::wstring& original_script) { | 284 const std::string& original_script) { |
286 std::wstring script = | 285 std::string script = |
287 original_script + L";window.domAutomationController.send(0);"; | 286 original_script + ";window.domAutomationController.send(0);"; |
288 return ExecuteJavaScriptHelper(render_view_host, frame_xpath, script, NULL); | 287 return ExecuteJavaScriptHelper(render_view_host, frame_xpath, script, NULL); |
289 } | 288 } |
290 | 289 |
291 bool ExecuteJavaScriptAndExtractInt(RenderViewHost* render_view_host, | 290 bool ExecuteJavaScriptAndExtractInt(RenderViewHost* render_view_host, |
292 const std::wstring& frame_xpath, | 291 const std::string& frame_xpath, |
293 const std::wstring& script, | 292 const std::string& script, |
294 int* result) { | 293 int* result) { |
295 DCHECK(result); | 294 DCHECK(result); |
296 scoped_ptr<Value> value; | 295 scoped_ptr<Value> value; |
297 if (!ExecuteJavaScriptHelper(render_view_host, frame_xpath, script, &value) || | 296 if (!ExecuteJavaScriptHelper(render_view_host, frame_xpath, script, &value) || |
298 !value.get()) | 297 !value.get()) |
299 return false; | 298 return false; |
300 | 299 |
301 return value->GetAsInteger(result); | 300 return value->GetAsInteger(result); |
302 } | 301 } |
303 | 302 |
304 bool ExecuteJavaScriptAndExtractBool(RenderViewHost* render_view_host, | 303 bool ExecuteJavaScriptAndExtractBool(RenderViewHost* render_view_host, |
305 const std::wstring& frame_xpath, | 304 const std::string& frame_xpath, |
306 const std::wstring& script, | 305 const std::string& script, |
307 bool* result) { | 306 bool* result) { |
308 DCHECK(result); | 307 DCHECK(result); |
309 scoped_ptr<Value> value; | 308 scoped_ptr<Value> value; |
310 if (!ExecuteJavaScriptHelper(render_view_host, frame_xpath, script, &value) || | 309 if (!ExecuteJavaScriptHelper(render_view_host, frame_xpath, script, &value) || |
311 !value.get()) | 310 !value.get()) |
312 return false; | 311 return false; |
313 | 312 |
314 return value->GetAsBoolean(result); | 313 return value->GetAsBoolean(result); |
315 } | 314 } |
316 | 315 |
317 bool ExecuteJavaScriptAndExtractString(RenderViewHost* render_view_host, | 316 bool ExecuteJavaScriptAndExtractString(RenderViewHost* render_view_host, |
318 const std::wstring& frame_xpath, | 317 const std::string& frame_xpath, |
319 const std::wstring& script, | 318 const std::string& script, |
320 std::string* result) { | 319 std::string* result) { |
321 DCHECK(result); | 320 DCHECK(result); |
322 scoped_ptr<Value> value; | 321 scoped_ptr<Value> value; |
323 if (!ExecuteJavaScriptHelper(render_view_host, frame_xpath, script, &value) || | 322 if (!ExecuteJavaScriptHelper(render_view_host, frame_xpath, script, &value) || |
324 !value.get()) | 323 !value.get()) |
325 return false; | 324 return false; |
326 | 325 |
327 return value->GetAsString(result); | 326 return value->GetAsString(result); |
328 } | 327 } |
329 | 328 |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
456 } | 455 } |
457 // The queue should not be empty, unless we were quit because of a timeout. | 456 // The queue should not be empty, unless we were quit because of a timeout. |
458 if (message_queue_.empty()) | 457 if (message_queue_.empty()) |
459 return false; | 458 return false; |
460 if (message) | 459 if (message) |
461 *message = message_queue_.front(); | 460 *message = message_queue_.front(); |
462 return true; | 461 return true; |
463 } | 462 } |
464 | 463 |
465 } // namespace content | 464 } // namespace content |
OLD | NEW |