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/auto_reset.h" | 7 #include "base/auto_reset.h" |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 base::JSONReader reader(base::JSON_ALLOW_TRAILING_COMMAS); | 154 base::JSONReader reader(base::JSON_ALLOW_TRAILING_COMMAS); |
155 *result = reader.ReadToValue(json); | 155 *result = reader.ReadToValue(json); |
156 if (!*result) { | 156 if (!*result) { |
157 DLOG(ERROR) << reader.GetErrorMessage(); | 157 DLOG(ERROR) << reader.GetErrorMessage(); |
158 return false; | 158 return false; |
159 } | 159 } |
160 | 160 |
161 return true; | 161 return true; |
162 } | 162 } |
163 | 163 |
| 164 // Specifying a prototype so that we can add the WARN_UNUSED_RESULT attribute. |
| 165 bool ExecuteScriptInIsolatedWorldHelper(RenderFrameHost* render_frame_host, |
| 166 const int world_id, |
| 167 const std::string& original_script, |
| 168 scoped_ptr<base::Value>* result) |
| 169 WARN_UNUSED_RESULT; |
| 170 |
| 171 bool ExecuteScriptInIsolatedWorldHelper(RenderFrameHost* render_frame_host, |
| 172 const int world_id, |
| 173 const std::string& original_script, |
| 174 scoped_ptr<base::Value>* result) { |
| 175 std::string script = |
| 176 "window.domAutomationController.setAutomationId(0);" + original_script; |
| 177 DOMOperationObserver dom_op_observer(render_frame_host->GetRenderViewHost()); |
| 178 render_frame_host->ExecuteJavaScriptInIsolatedWorld( |
| 179 base::UTF8ToUTF16(script), |
| 180 content::RenderFrameHost::JavaScriptResultCallback(), world_id); |
| 181 std::string json; |
| 182 if (!dom_op_observer.WaitAndGetResponse(&json)) { |
| 183 DLOG(ERROR) << "Cannot communicate with DOMOperationObserver."; |
| 184 return false; |
| 185 } |
| 186 |
| 187 // Nothing more to do for callers that ignore the returned JS value. |
| 188 if (!result) |
| 189 return true; |
| 190 |
| 191 base::JSONReader reader(base::JSON_ALLOW_TRAILING_COMMAS); |
| 192 *result = reader.ReadToValue(json); |
| 193 if (!*result) { |
| 194 DLOG(ERROR) << reader.GetErrorMessage(); |
| 195 return false; |
| 196 } |
| 197 |
| 198 return true; |
| 199 } |
| 200 |
164 void BuildSimpleWebKeyEvent(blink::WebInputEvent::Type type, | 201 void BuildSimpleWebKeyEvent(blink::WebInputEvent::Type type, |
165 ui::KeyboardCode key_code, | 202 ui::KeyboardCode key_code, |
166 int native_key_code, | 203 int native_key_code, |
167 int modifiers, | 204 int modifiers, |
168 NativeWebKeyboardEvent* event) { | 205 NativeWebKeyboardEvent* event) { |
169 event->nativeKeyCode = native_key_code; | 206 event->nativeKeyCode = native_key_code; |
170 event->windowsKeyCode = key_code; | 207 event->windowsKeyCode = key_code; |
171 event->setKeyIdentifierFromWindowsKeyCode(); | 208 event->setKeyIdentifierFromWindowsKeyCode(); |
172 event->type = type; | 209 event->type = type; |
173 event->modifiers = modifiers; | 210 event->modifiers = modifiers; |
(...skipping 402 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
576 DCHECK(result); | 613 DCHECK(result); |
577 scoped_ptr<base::Value> value; | 614 scoped_ptr<base::Value> value; |
578 if (!ExecuteScriptHelper(adapter.render_frame_host(), script, &value) || | 615 if (!ExecuteScriptHelper(adapter.render_frame_host(), script, &value) || |
579 !value.get()) { | 616 !value.get()) { |
580 return false; | 617 return false; |
581 } | 618 } |
582 | 619 |
583 return value->GetAsBoolean(result); | 620 return value->GetAsBoolean(result); |
584 } | 621 } |
585 | 622 |
| 623 bool ExecuteScriptInIsolatedWorldAndExtractBool( |
| 624 const ToRenderFrameHost& adapter, |
| 625 const int world_id, |
| 626 const std::string& script, |
| 627 bool* result) { |
| 628 DCHECK(result); |
| 629 scoped_ptr<base::Value> value; |
| 630 if (!ExecuteScriptInIsolatedWorldHelper(adapter.render_frame_host(), world_id, |
| 631 script, &value) || |
| 632 !value.get()) { |
| 633 return false; |
| 634 } |
| 635 |
| 636 return value->GetAsBoolean(result); |
| 637 } |
| 638 |
586 bool ExecuteScriptAndExtractString(const ToRenderFrameHost& adapter, | 639 bool ExecuteScriptAndExtractString(const ToRenderFrameHost& adapter, |
587 const std::string& script, | 640 const std::string& script, |
588 std::string* result) { | 641 std::string* result) { |
589 DCHECK(result); | 642 DCHECK(result); |
590 scoped_ptr<base::Value> value; | 643 scoped_ptr<base::Value> value; |
591 if (!ExecuteScriptHelper(adapter.render_frame_host(), script, &value) || | 644 if (!ExecuteScriptHelper(adapter.render_frame_host(), script, &value) || |
592 !value.get()) { | 645 !value.get()) { |
593 return false; | 646 return false; |
594 } | 647 } |
595 | 648 |
(...skipping 389 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
985 void FrameWatcher::WaitFrames(int frames_to_wait) { | 1038 void FrameWatcher::WaitFrames(int frames_to_wait) { |
986 if (frames_to_wait <= 0) | 1039 if (frames_to_wait <= 0) |
987 return; | 1040 return; |
988 base::RunLoop run_loop; | 1041 base::RunLoop run_loop; |
989 base::AutoReset<base::Closure> reset_quit(&quit_, run_loop.QuitClosure()); | 1042 base::AutoReset<base::Closure> reset_quit(&quit_, run_loop.QuitClosure()); |
990 base::AutoReset<int> reset_frames_to_wait(&frames_to_wait_, frames_to_wait); | 1043 base::AutoReset<int> reset_frames_to_wait(&frames_to_wait_, frames_to_wait); |
991 run_loop.Run(); | 1044 run_loop.Run(); |
992 } | 1045 } |
993 | 1046 |
994 } // namespace content | 1047 } // namespace content |
OLD | NEW |