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

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

Issue 2478803003: Remove DOMAutomationController::automation_id_ (Closed)
Patch Set: Added |actual_response| checks to ExecuteScriptWithoutUserGesture. s/ExecuteUnmodifiedScript/Execu… Created 3 years, 5 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
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 <stddef.h> 7 #include <stddef.h>
8 #include <tuple> 8 #include <tuple>
9 #include <utility> 9 #include <utility>
10 10
11 #include "base/auto_reset.h" 11 #include "base/auto_reset.h"
12 #include "base/bind.h" 12 #include "base/bind.h"
13 #include "base/bind_helpers.h" 13 #include "base/bind_helpers.h"
14 #include "base/command_line.h" 14 #include "base/command_line.h"
15 #include "base/guid.h"
15 #include "base/json/json_reader.h" 16 #include "base/json/json_reader.h"
16 #include "base/macros.h" 17 #include "base/macros.h"
17 #include "base/process/kill.h" 18 #include "base/process/kill.h"
18 #include "base/rand_util.h" 19 #include "base/rand_util.h"
19 #include "base/stl_util.h" 20 #include "base/stl_util.h"
20 #include "base/strings/pattern.h" 21 #include "base/strings/pattern.h"
21 #include "base/strings/string_number_conversions.h" 22 #include "base/strings/string_number_conversions.h"
22 #include "base/strings/string_piece.h" 23 #include "base/strings/string_piece.h"
24 #include "base/strings/stringprintf.h"
23 #include "base/strings/utf_string_conversions.h" 25 #include "base/strings/utf_string_conversions.h"
24 #include "base/synchronization/waitable_event.h" 26 #include "base/synchronization/waitable_event.h"
25 #include "base/test/test_timeouts.h" 27 #include "base/test/test_timeouts.h"
26 #include "base/threading/thread_task_runner_handle.h" 28 #include "base/threading/thread_task_runner_handle.h"
27 #include "base/values.h" 29 #include "base/values.h"
28 #include "build/build_config.h" 30 #include "build/build_config.h"
29 #include "cc/surfaces/surface.h" 31 #include "cc/surfaces/surface.h"
30 #include "cc/surfaces/surface_manager.h" 32 #include "cc/surfaces/surface_manager.h"
31 #include "content/browser/accessibility/browser_accessibility.h" 33 #include "content/browser/accessibility/browser_accessibility.h"
32 #include "content/browser/accessibility/browser_accessibility_manager.h" 34 #include "content/browser/accessibility/browser_accessibility_manager.h"
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
167 base::JSONReader reader(base::JSON_ALLOW_TRAILING_COMMAS); 169 base::JSONReader reader(base::JSON_ALLOW_TRAILING_COMMAS);
168 *result = reader.ReadToValue(json); 170 *result = reader.ReadToValue(json);
169 if (!*result) { 171 if (!*result) {
170 DLOG(ERROR) << reader.GetErrorMessage(); 172 DLOG(ERROR) << reader.GetErrorMessage();
171 return false; 173 return false;
172 } 174 }
173 175
174 return true; 176 return true;
175 } 177 }
176 178
179 bool ExecuteScriptWithUserGestureControl(RenderFrameHost* frame,
180 const std::string& script,
181 bool user_gesture) {
182 // TODO(lukasza): ExecuteScript should just call
183 // ExecuteJavaScriptWithUserGestureForTests and avoid modifying the original
184 // script (and at that point we should merge it with and remove
185 // ExecuteScriptAsync). This is difficult to change, because many tests
186 // depend on the message loop pumping done by ExecuteScriptHelper below (this
187 // is fragile - these tests should wait on a more specific thing instead).
188
189 std::string expected_response = "ExecuteScript-" + base::GenerateGUID();
190 std::string new_script = base::StringPrintf(
191 R"( %s; // Original script.
192 window.domAutomationController.send('%s'); )",
193 script.c_str(), expected_response.c_str());
194
195 std::unique_ptr<base::Value> value;
196 if (!ExecuteScriptHelper(frame, new_script, user_gesture, &value) ||
197 !value.get()) {
198 return false;
199 }
200
201 DCHECK_EQ(base::Value::Type::STRING, value->GetType());
202 std::string actual_response;
203 if (value->GetAsString(&actual_response))
204 DCHECK_EQ(expected_response, actual_response);
205
206 return true;
207 }
208
177 // Specifying a prototype so that we can add the WARN_UNUSED_RESULT attribute. 209 // Specifying a prototype so that we can add the WARN_UNUSED_RESULT attribute.
178 bool ExecuteScriptInIsolatedWorldHelper(RenderFrameHost* render_frame_host, 210 bool ExecuteScriptInIsolatedWorldHelper(RenderFrameHost* render_frame_host,
179 const int world_id, 211 const int world_id,
180 const std::string& original_script, 212 const std::string& original_script,
181 std::unique_ptr<base::Value>* result) 213 std::unique_ptr<base::Value>* result)
182 WARN_UNUSED_RESULT; 214 WARN_UNUSED_RESULT;
183 215
184 bool ExecuteScriptInIsolatedWorldHelper(RenderFrameHost* render_frame_host, 216 bool ExecuteScriptInIsolatedWorldHelper(RenderFrameHost* render_frame_host,
185 const int world_id, 217 const int world_id,
186 const std::string& original_script, 218 const std::string& original_script,
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after
406 } 438 }
407 439
408 // Queries for video input devices on the current system using the getSources 440 // Queries for video input devices on the current system using the getSources
409 // API. 441 // API.
410 // 442 //
411 // This does not guarantee that a getUserMedia with video will succeed, as the 443 // This does not guarantee that a getUserMedia with video will succeed, as the
412 // camera could be busy for instance. 444 // camera could be busy for instance.
413 // 445 //
414 // Returns has-video-input-device to the test if there is a webcam available, 446 // Returns has-video-input-device to the test if there is a webcam available,
415 // no-video-input-devices otherwise. 447 // no-video-input-devices otherwise.
416 const char kHasVideoInputDeviceOnSystem[] = 448 const char kHasVideoInputDeviceOnSystem[] = R"(
417 "(function() {" 449 (function() {
418 "navigator.mediaDevices.enumerateDevices()" 450 navigator.mediaDevices.enumerateDevices()
419 ".then(function(devices) {" 451 .then(function(devices) {
420 "devices.forEach(function(device) {" 452 if (devices.some((device) => device.kind == 'videoinput')) {
421 "if (device.kind == 'videoinput') {" 453 window.domAutomationController.send('has-video-input-device');
422 "window.domAutomationController.send('has-video-input-device');" 454 } else {
423 "return;" 455 window.domAutomationController.send('no-video-input-devices');
424 "}" 456 }
425 "});" 457 });
426 "window.domAutomationController.send('no-video-input-devices');" 458 })()
427 "});" 459 )";
428 "})()";
429 460
430 const char kHasVideoInputDevice[] = "has-video-input-device"; 461 const char kHasVideoInputDevice[] = "has-video-input-device";
431 462
432 } // namespace 463 } // namespace
433 464
434 bool NavigateIframeToURL(WebContents* web_contents, 465 bool NavigateIframeToURL(WebContents* web_contents,
435 std::string iframe_id, 466 std::string iframe_id,
436 const GURL& url) { 467 const GURL& url) {
437 std::string script = base::StringPrintf( 468 std::string script = base::StringPrintf(
438 "setTimeout(\"" 469 "setTimeout(\""
(...skipping 370 matching lines...) Expand 10 before | Expand all | Expand 10 after
809 RenderFrameHost* ConvertToRenderFrameHost(RenderViewHost* render_view_host) { 840 RenderFrameHost* ConvertToRenderFrameHost(RenderViewHost* render_view_host) {
810 return render_view_host->GetMainFrame(); 841 return render_view_host->GetMainFrame();
811 } 842 }
812 843
813 RenderFrameHost* ConvertToRenderFrameHost(RenderFrameHost* render_frame_host) { 844 RenderFrameHost* ConvertToRenderFrameHost(RenderFrameHost* render_frame_host) {
814 return render_frame_host; 845 return render_frame_host;
815 } 846 }
816 847
817 bool ExecuteScript(const ToRenderFrameHost& adapter, 848 bool ExecuteScript(const ToRenderFrameHost& adapter,
818 const std::string& script) { 849 const std::string& script) {
819 std::string new_script = 850 return ExecuteScriptWithUserGestureControl(adapter.render_frame_host(),
820 script + ";window.domAutomationController.send(0);"; 851 script, true);
821 return ExecuteScriptHelper(adapter.render_frame_host(), new_script, true,
822 nullptr);
823 } 852 }
824 853
825 bool ExecuteScriptWithoutUserGesture(const ToRenderFrameHost& adapter, 854 bool ExecuteScriptWithoutUserGesture(const ToRenderFrameHost& adapter,
826 const std::string& script) { 855 const std::string& script) {
827 std::string new_script = script + ";window.domAutomationController.send(0);"; 856 return ExecuteScriptWithUserGestureControl(adapter.render_frame_host(),
828 return ExecuteScriptHelper(adapter.render_frame_host(), new_script, false, 857 script, false);
829 nullptr); 858 }
859
860 void ExecuteScriptAsync(const ToRenderFrameHost& adapter,
861 const std::string& script) {
862 adapter.render_frame_host()->ExecuteJavaScriptWithUserGestureForTests(
863 base::UTF8ToUTF16(script));
830 } 864 }
831 865
832 bool ExecuteScriptAndExtractDouble(const ToRenderFrameHost& adapter, 866 bool ExecuteScriptAndExtractDouble(const ToRenderFrameHost& adapter,
833 const std::string& script, double* result) { 867 const std::string& script, double* result) {
834 DCHECK(result); 868 DCHECK(result);
835 std::unique_ptr<base::Value> value; 869 std::unique_ptr<base::Value> value;
836 if (!ExecuteScriptHelper(adapter.render_frame_host(), script, true, &value) || 870 if (!ExecuteScriptHelper(adapter.render_frame_host(), script, true, &value) ||
837 !value.get()) { 871 !value.get()) {
838 return false; 872 return false;
839 } 873 }
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after
992 scoped_refptr<base::RefCountedMemory> bytes = 1026 scoped_refptr<base::RefCountedMemory> bytes =
993 ResourceBundle::GetSharedInstance().LoadDataResourceBytes(*iter); 1027 ResourceBundle::GetSharedInstance().LoadDataResourceBytes(*iter);
994 1028
995 if (HasGzipHeader(*bytes)) 1029 if (HasGzipHeader(*bytes))
996 AppendGzippedResource(*bytes, &script); 1030 AppendGzippedResource(*bytes, &script);
997 else 1031 else
998 script.append(bytes->front_as<char>(), bytes->size()); 1032 script.append(bytes->front_as<char>(), bytes->size());
999 1033
1000 script.append("\n"); 1034 script.append("\n");
1001 } 1035 }
1002 if (!ExecuteScript(web_contents, script)) 1036 ExecuteScriptAsync(web_contents, script);
1003 return false;
1004 1037
1005 DOMMessageQueue message_queue; 1038 DOMMessageQueue message_queue;
1006 if (!ExecuteScript(web_contents, "runTests()")) 1039 ExecuteScriptAsync(web_contents, "runTests()");
1007 return false;
1008 1040
1009 std::string message; 1041 std::string message;
1010 do { 1042 do {
1011 if (!message_queue.WaitForMessage(&message)) 1043 if (!message_queue.WaitForMessage(&message))
1012 return false; 1044 return false;
1013 } while (message.compare("\"PENDING\"") == 0); 1045 } while (message.compare("\"PENDING\"") == 0);
1014 1046
1015 return message.compare("\"SUCCESS\"") == 0; 1047 return message.compare("\"SUCCESS\"") == 0;
1016 } 1048 }
1017 1049
(...skipping 467 matching lines...) Expand 10 before | Expand all | Expand 10 after
1485 Source<WebContents>(web_contents)); 1517 Source<WebContents>(web_contents));
1486 } 1518 }
1487 1519
1488 DOMMessageQueue::~DOMMessageQueue() {} 1520 DOMMessageQueue::~DOMMessageQueue() {}
1489 1521
1490 void DOMMessageQueue::Observe(int type, 1522 void DOMMessageQueue::Observe(int type,
1491 const NotificationSource& source, 1523 const NotificationSource& source,
1492 const NotificationDetails& details) { 1524 const NotificationDetails& details) {
1493 Details<std::string> dom_op_result(details); 1525 Details<std::string> dom_op_result(details);
1494 message_queue_.push(*dom_op_result.ptr()); 1526 message_queue_.push(*dom_op_result.ptr());
1495 if (message_loop_runner_.get()) 1527 if (message_loop_runner_)
1496 message_loop_runner_->Quit(); 1528 message_loop_runner_->Quit();
1497 } 1529 }
1498 1530
1499 void DOMMessageQueue::RenderProcessGone(base::TerminationStatus status) { 1531 void DOMMessageQueue::RenderProcessGone(base::TerminationStatus status) {
1500 VLOG(0) << "DOMMessageQueue::RenderProcessGone " << status; 1532 VLOG(0) << "DOMMessageQueue::RenderProcessGone " << status;
1501 switch (status) { 1533 switch (status) {
1502 case base::TERMINATION_STATUS_NORMAL_TERMINATION: 1534 case base::TERMINATION_STATUS_NORMAL_TERMINATION:
1503 case base::TERMINATION_STATUS_STILL_RUNNING: 1535 case base::TERMINATION_STATUS_STILL_RUNNING:
1504 break; 1536 break;
1505 default: 1537 default:
1538 renderer_crashed_ = true;
1506 if (message_loop_runner_.get()) 1539 if (message_loop_runner_.get())
1507 message_loop_runner_->Quit(); 1540 message_loop_runner_->Quit();
1508 break; 1541 break;
1509 } 1542 }
1510 } 1543 }
1511 1544
1512 void DOMMessageQueue::ClearQueue() { 1545 void DOMMessageQueue::ClearQueue() {
1513 message_queue_ = std::queue<std::string>(); 1546 message_queue_ = std::queue<std::string>();
1514 } 1547 }
1515 1548
1516 bool DOMMessageQueue::WaitForMessage(std::string* message) { 1549 bool DOMMessageQueue::WaitForMessage(std::string* message) {
1517 DCHECK(message); 1550 DCHECK(message);
1518 if (message_queue_.empty()) { 1551 if (!renderer_crashed_ && message_queue_.empty()) {
1519 // This will be quit when a new message comes in. 1552 // This will be quit when a new message comes in.
1520 message_loop_runner_ = 1553 message_loop_runner_ =
1521 new MessageLoopRunner(MessageLoopRunner::QuitMode::IMMEDIATE); 1554 new MessageLoopRunner(MessageLoopRunner::QuitMode::IMMEDIATE);
1522 message_loop_runner_->Run(); 1555 message_loop_runner_->Run();
1523 } 1556 }
1524 return PopMessage(message); 1557 return PopMessage(message);
1525 } 1558 }
1526 1559
1527 bool DOMMessageQueue::PopMessage(std::string* message) { 1560 bool DOMMessageQueue::PopMessage(std::string* message) {
1528 DCHECK(message); 1561 DCHECK(message);
1529 if (message_queue_.empty()) 1562 if (renderer_crashed_ || message_queue_.empty())
1530 return false; 1563 return false;
1531 *message = message_queue_.front(); 1564 *message = message_queue_.front();
1532 message_queue_.pop(); 1565 message_queue_.pop();
1533 return true; 1566 return true;
1534 } 1567 }
1535 1568
1536 class WebContentsAddedObserver::RenderViewCreatedObserver 1569 class WebContentsAddedObserver::RenderViewCreatedObserver
1537 : public WebContentsObserver { 1570 : public WebContentsObserver {
1538 public: 1571 public:
1539 explicit RenderViewCreatedObserver(WebContents* web_contents) 1572 explicit RenderViewCreatedObserver(WebContents* web_contents)
(...skipping 564 matching lines...) Expand 10 before | Expand all | Expand 10 after
2104 RenderWidgetHostViewAura* rwhva = 2137 RenderWidgetHostViewAura* rwhva =
2105 static_cast<RenderWidgetHostViewAura*>(rwhv); 2138 static_cast<RenderWidgetHostViewAura*>(rwhv);
2106 rwhva->SetOverscrollControllerForTesting(std::move(mock)); 2139 rwhva->SetOverscrollControllerForTesting(std::move(mock));
2107 2140
2108 return raw_mock; 2141 return raw_mock;
2109 } 2142 }
2110 2143
2111 #endif // defined(USE_AURA) 2144 #endif // defined(USE_AURA)
2112 2145
2113 } // namespace content 2146 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698