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

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

Issue 2478803003: Remove DOMAutomationController::automation_id_ (Closed)
Patch Set: More of: Fixing issues caught by extra |expected_response| checks in ExecuteScript function. 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 373 matching lines...) Expand 10 before | Expand all | Expand 10 after
406 } 408 }
407 409
408 // Queries for video input devices on the current system using the getSources 410 // Queries for video input devices on the current system using the getSources
409 // API. 411 // API.
410 // 412 //
411 // This does not guarantee that a getUserMedia with video will succeed, as the 413 // This does not guarantee that a getUserMedia with video will succeed, as the
412 // camera could be busy for instance. 414 // camera could be busy for instance.
413 // 415 //
414 // Returns has-video-input-device to the test if there is a webcam available, 416 // Returns has-video-input-device to the test if there is a webcam available,
415 // no-video-input-devices otherwise. 417 // no-video-input-devices otherwise.
416 const char kHasVideoInputDeviceOnSystem[] = 418 const char kHasVideoInputDeviceOnSystem[] = R"(
417 "(function() {" 419 (function() {
418 "navigator.mediaDevices.enumerateDevices()" 420 navigator.mediaDevices.enumerateDevices()
419 ".then(function(devices) {" 421 .then(function(devices) {
420 "devices.forEach(function(device) {" 422 if (devices.some((device) => device.kind == 'videoinput')) {
421 "if (device.kind == 'videoinput') {" 423 window.domAutomationController.send('has-video-input-device');
422 "window.domAutomationController.send('has-video-input-device');" 424 } else {
423 "return;" 425 window.domAutomationController.send('no-video-input-devices');
424 "}" 426 }
425 "});" 427 });
426 "window.domAutomationController.send('no-video-input-devices');" 428 })()
427 "});" 429 )";
428 "})()";
429 430
430 const char kHasVideoInputDevice[] = "has-video-input-device"; 431 const char kHasVideoInputDevice[] = "has-video-input-device";
431 432
432 } // namespace 433 } // namespace
433 434
434 bool NavigateIframeToURL(WebContents* web_contents, 435 bool NavigateIframeToURL(WebContents* web_contents,
435 std::string iframe_id, 436 std::string iframe_id,
436 const GURL& url) { 437 const GURL& url) {
437 std::string script = base::StringPrintf( 438 std::string script = base::StringPrintf(
438 "setTimeout(\"" 439 "setTimeout(\""
(...skipping 370 matching lines...) Expand 10 before | Expand all | Expand 10 after
809 RenderFrameHost* ConvertToRenderFrameHost(RenderViewHost* render_view_host) { 810 RenderFrameHost* ConvertToRenderFrameHost(RenderViewHost* render_view_host) {
810 return render_view_host->GetMainFrame(); 811 return render_view_host->GetMainFrame();
811 } 812 }
812 813
813 RenderFrameHost* ConvertToRenderFrameHost(RenderFrameHost* render_frame_host) { 814 RenderFrameHost* ConvertToRenderFrameHost(RenderFrameHost* render_frame_host) {
814 return render_frame_host; 815 return render_frame_host;
815 } 816 }
816 817
817 bool ExecuteScript(const ToRenderFrameHost& adapter, 818 bool ExecuteScript(const ToRenderFrameHost& adapter,
818 const std::string& script) { 819 const std::string& script) {
819 std::string new_script = 820 // TODO(lukasza): ExecuteScript should just call
820 script + ";window.domAutomationController.send(0);"; 821 // ExecuteJavaScriptWithUserGestureForTests and avoid modifying the original
821 return ExecuteScriptHelper(adapter.render_frame_host(), new_script, true, 822 // script (and at that point we should merge it with and remove
822 nullptr); 823 // ExecuteUnmodifiedScript). This is difficult to change, because many tests
824 // depend on the message loop pumping done by ExecuteScriptHelper below (this
825 // is fragile - these tests should wait on a more specific thing instead).
826
827 std::string expected_response = "ExecuteScript-" + base::GenerateGUID();
828 std::string new_script = base::StringPrintf(
829 R"( %s; // Original script.
830 window.domAutomationController.send('%s'); )",
831 script.c_str(), expected_response.c_str());
832
833 std::string actual_response;
834 if (!ExecuteScriptAndExtractString(adapter, new_script, &actual_response))
835 return false;
836 DCHECK_EQ(expected_response, actual_response);
837
838 return true;
823 } 839 }
824 840
825 bool ExecuteScriptWithoutUserGesture(const ToRenderFrameHost& adapter, 841 bool ExecuteScriptWithoutUserGesture(const ToRenderFrameHost& adapter,
826 const std::string& script) { 842 const std::string& script) {
827 std::string new_script = script + ";window.domAutomationController.send(0);"; 843 std::string new_script = script + ";window.domAutomationController.send(0);";
828 return ExecuteScriptHelper(adapter.render_frame_host(), new_script, false, 844 return ExecuteScriptHelper(adapter.render_frame_host(), new_script, false,
829 nullptr); 845 nullptr);
830 } 846 }
831 847
848 void ExecuteUnmodifiedScript(const ToRenderFrameHost& adapter,
849 const std::string& script) {
850 adapter.render_frame_host()->ExecuteJavaScriptWithUserGestureForTests(
851 base::UTF8ToUTF16(script));
852 }
853
832 bool ExecuteScriptAndExtractDouble(const ToRenderFrameHost& adapter, 854 bool ExecuteScriptAndExtractDouble(const ToRenderFrameHost& adapter,
833 const std::string& script, double* result) { 855 const std::string& script, double* result) {
834 DCHECK(result); 856 DCHECK(result);
835 std::unique_ptr<base::Value> value; 857 std::unique_ptr<base::Value> value;
836 if (!ExecuteScriptHelper(adapter.render_frame_host(), script, true, &value) || 858 if (!ExecuteScriptHelper(adapter.render_frame_host(), script, true, &value) ||
837 !value.get()) { 859 !value.get()) {
838 return false; 860 return false;
839 } 861 }
840 862
841 return value->GetAsDouble(result); 863 return value->GetAsDouble(result);
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after
992 scoped_refptr<base::RefCountedMemory> bytes = 1014 scoped_refptr<base::RefCountedMemory> bytes =
993 ResourceBundle::GetSharedInstance().LoadDataResourceBytes(*iter); 1015 ResourceBundle::GetSharedInstance().LoadDataResourceBytes(*iter);
994 1016
995 if (HasGzipHeader(*bytes)) 1017 if (HasGzipHeader(*bytes))
996 AppendGzippedResource(*bytes, &script); 1018 AppendGzippedResource(*bytes, &script);
997 else 1019 else
998 script.append(bytes->front_as<char>(), bytes->size()); 1020 script.append(bytes->front_as<char>(), bytes->size());
999 1021
1000 script.append("\n"); 1022 script.append("\n");
1001 } 1023 }
1002 if (!ExecuteScript(web_contents, script)) 1024 ExecuteUnmodifiedScript(web_contents, script);
1003 return false;
1004 1025
1005 DOMMessageQueue message_queue; 1026 DOMMessageQueue message_queue;
1006 if (!ExecuteScript(web_contents, "runTests()")) 1027 ExecuteUnmodifiedScript(web_contents, "runTests()");
1007 return false;
1008 1028
1009 std::string message; 1029 std::string message;
1010 do { 1030 do {
1011 if (!message_queue.WaitForMessage(&message)) 1031 if (!message_queue.WaitForMessage(&message))
1012 return false; 1032 return false;
1013 } while (message.compare("\"PENDING\"") == 0); 1033 } while (message.compare("\"PENDING\"") == 0);
1014 1034
1015 return message.compare("\"SUCCESS\"") == 0; 1035 return message.compare("\"SUCCESS\"") == 0;
1016 } 1036 }
1017 1037
(...skipping 467 matching lines...) Expand 10 before | Expand all | Expand 10 after
1485 Source<WebContents>(web_contents)); 1505 Source<WebContents>(web_contents));
1486 } 1506 }
1487 1507
1488 DOMMessageQueue::~DOMMessageQueue() {} 1508 DOMMessageQueue::~DOMMessageQueue() {}
1489 1509
1490 void DOMMessageQueue::Observe(int type, 1510 void DOMMessageQueue::Observe(int type,
1491 const NotificationSource& source, 1511 const NotificationSource& source,
1492 const NotificationDetails& details) { 1512 const NotificationDetails& details) {
1493 Details<std::string> dom_op_result(details); 1513 Details<std::string> dom_op_result(details);
1494 message_queue_.push(*dom_op_result.ptr()); 1514 message_queue_.push(*dom_op_result.ptr());
1495 if (message_loop_runner_.get()) 1515 if (message_loop_runner_)
1496 message_loop_runner_->Quit(); 1516 message_loop_runner_->Quit();
1497 } 1517 }
1498 1518
1499 void DOMMessageQueue::RenderProcessGone(base::TerminationStatus status) { 1519 void DOMMessageQueue::RenderProcessGone(base::TerminationStatus status) {
1500 VLOG(0) << "DOMMessageQueue::RenderProcessGone " << status; 1520 VLOG(0) << "DOMMessageQueue::RenderProcessGone " << status;
1501 switch (status) { 1521 switch (status) {
1502 case base::TERMINATION_STATUS_NORMAL_TERMINATION: 1522 case base::TERMINATION_STATUS_NORMAL_TERMINATION:
1503 case base::TERMINATION_STATUS_STILL_RUNNING: 1523 case base::TERMINATION_STATUS_STILL_RUNNING:
1504 break; 1524 break;
1505 default: 1525 default:
1526 renderer_crashed_ = true;
1506 if (message_loop_runner_.get()) 1527 if (message_loop_runner_.get())
1507 message_loop_runner_->Quit(); 1528 message_loop_runner_->Quit();
1508 break; 1529 break;
1509 } 1530 }
1510 } 1531 }
1511 1532
1512 void DOMMessageQueue::ClearQueue() { 1533 void DOMMessageQueue::ClearQueue() {
1513 message_queue_ = std::queue<std::string>(); 1534 message_queue_ = std::queue<std::string>();
1514 } 1535 }
1515 1536
1516 bool DOMMessageQueue::WaitForMessage(std::string* message) { 1537 bool DOMMessageQueue::WaitForMessage(std::string* message) {
1517 DCHECK(message); 1538 DCHECK(message);
1518 if (message_queue_.empty()) { 1539 if (!renderer_crashed_ && message_queue_.empty()) {
1519 // This will be quit when a new message comes in. 1540 // This will be quit when a new message comes in.
1520 message_loop_runner_ = 1541 message_loop_runner_ =
1521 new MessageLoopRunner(MessageLoopRunner::QuitMode::IMMEDIATE); 1542 new MessageLoopRunner(MessageLoopRunner::QuitMode::IMMEDIATE);
1522 message_loop_runner_->Run(); 1543 message_loop_runner_->Run();
1523 } 1544 }
1524 return PopMessage(message); 1545 return PopMessage(message);
1525 } 1546 }
1526 1547
1527 bool DOMMessageQueue::PopMessage(std::string* message) { 1548 bool DOMMessageQueue::PopMessage(std::string* message) {
1528 DCHECK(message); 1549 DCHECK(message);
1529 if (message_queue_.empty()) 1550 if (renderer_crashed_ || message_queue_.empty())
1530 return false; 1551 return false;
1531 *message = message_queue_.front(); 1552 *message = message_queue_.front();
1532 message_queue_.pop(); 1553 message_queue_.pop();
1533 return true; 1554 return true;
1534 } 1555 }
1535 1556
1536 class WebContentsAddedObserver::RenderViewCreatedObserver 1557 class WebContentsAddedObserver::RenderViewCreatedObserver
1537 : public WebContentsObserver { 1558 : public WebContentsObserver {
1538 public: 1559 public:
1539 explicit RenderViewCreatedObserver(WebContents* web_contents) 1560 explicit RenderViewCreatedObserver(WebContents* web_contents)
(...skipping 564 matching lines...) Expand 10 before | Expand all | Expand 10 after
2104 RenderWidgetHostViewAura* rwhva = 2125 RenderWidgetHostViewAura* rwhva =
2105 static_cast<RenderWidgetHostViewAura*>(rwhv); 2126 static_cast<RenderWidgetHostViewAura*>(rwhv);
2106 rwhva->SetOverscrollControllerForTesting(std::move(mock)); 2127 rwhva->SetOverscrollControllerForTesting(std::move(mock));
2107 2128
2108 return raw_mock; 2129 return raw_mock;
2109 } 2130 }
2110 2131
2111 #endif // defined(USE_AURA) 2132 #endif // defined(USE_AURA)
2112 2133
2113 } // namespace content 2134 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698