Index: chrome/browser/site_per_process_interactive_browsertest.cc |
diff --git a/chrome/browser/site_per_process_interactive_browsertest.cc b/chrome/browser/site_per_process_interactive_browsertest.cc |
index 7481c0d8042ad5bce4c71e8a289d8025ffa5efc7..99832b8ac817d10f38270cd3005020521757063e 100644 |
--- a/chrome/browser/site_per_process_interactive_browsertest.cc |
+++ b/chrome/browser/site_per_process_interactive_browsertest.cc |
@@ -27,6 +27,7 @@ |
#include "extensions/common/constants.h" |
#include "net/dns/mock_host_resolver.h" |
#include "net/test/embedded_test_server/embedded_test_server.h" |
+#include "ui/base/test/ui_controls.h" |
#include "ui/display/display.h" |
#include "ui/display/screen.h" |
#include "url/gurl.h" |
@@ -280,6 +281,175 @@ IN_PROC_BROWSER_TEST_F(SitePerProcessInteractiveBrowserTest, |
EXPECT_EQ(main_frame, web_contents->GetFocusedFrame()); |
} |
+// TODO(http://crbug.com/702330): Enable this test. |
alexmos
2017/03/24 23:55:39
nit: https
avallee
2017/03/27 19:54:24
Done.
|
+// Ensures that renderers know to advance focus to sibling frames and parent |
+// frames in the presence of mouse click initiated focus changes. |
+IN_PROC_BROWSER_TEST_F(SitePerProcessInteractiveBrowserTest, |
+ DISABLED_TabAndMouseFocusNavigation) { |
+ GURL main_url(embedded_test_server()->GetURL( |
+ "a.com", "/cross_site_iframe_factory.html?a(b,c)")); |
+ ui_test_utils::NavigateToURL(browser(), main_url); |
+ |
+ content::WebContents* web_contents = |
+ browser()->tab_strip_model()->GetActiveWebContents(); |
+ |
+ content::RenderFrameHost* main_frame = web_contents->GetMainFrame(); |
+ content::RenderFrameHost* child1 = ChildFrameAt(main_frame, 0); |
+ ASSERT_NE(nullptr, child1); |
+ content::RenderFrameHost* child2 = ChildFrameAt(main_frame, 1); |
+ ASSERT_NE(nullptr, child2); |
+ |
+ // Assign a name to each frame. This will be sent along in test messages |
+ // from focus events. |
+ EXPECT_TRUE(ExecuteScript(main_frame, "window.name = 'root';")); |
+ EXPECT_TRUE(ExecuteScript(child1, "window.name = 'child1';")); |
+ EXPECT_TRUE(ExecuteScript(child2, "window.name = 'child2';")); |
+ |
+ // This script will insert two <input> fields in the document, one at the |
+ // beginning and one at the end. For root frame, this means that we will |
+ // have an <input>, then two <iframe> elements, then another <input>. |
alexmos
2017/03/24 23:55:39
Perhaps also mention what it sends back. It's kin
avallee
2017/03/27 19:54:24
Done.
|
+ std::string script = |
+ "function onFocus(e) {" |
+ " console.log(window.name+'-focused-'+ e.target.id);" |
+ " domAutomationController.setAutomationId(0);" |
+ " domAutomationController.send(window.name + '-focused-' + e.target.id);" |
+ "}" |
+ "" |
+ "function getElementCoords(element) {" |
+ " var rect = element.getBoundingClientRect();" |
+ " return Math.floor(rect.left + 0.5 * rect.width) +','+" |
+ " Math.floor(rect.top + 0.5 * rect.height);" |
+ "}" |
+ "function getIframeCoords(element) {" |
+ " var rect = element.getBoundingClientRect();" |
+ " return Math.floor(rect.left) +','+" |
+ " Math.floor(rect.top);" |
+ "}" |
+ "" |
+ "document.styleSheets[0].insertRule('input {width:100%;margin:0;}', 1);" |
+ "document.styleSheets[0].insertRule('h2 {margin:0;}', 1);" |
+ "var input1 = document.createElement('input');" |
+ "input1.id = 'input1';" |
+ "input1.addEventListener('focus', onFocus, false);" |
+ "var input2 = document.createElement('input');" |
+ "input2.id = 'input2';" |
+ "input2.addEventListener('focus', onFocus, false);" |
+ "document.body.insertBefore(input1, document.body.firstChild);" |
+ "document.body.appendChild(input2);" |
+ "" |
+ "var frames = document.querySelectorAll('iframe');" |
+ "frames = Array.prototype.map.call(frames, getIframeCoords).join(';');" |
+ "var inputCoords = [input1, input2].map(getElementCoords).join(';');" |
+ "if (frames) {" |
+ " inputCoords = inputCoords + ':' + frames;" |
+ "}" |
+ "domAutomationController.send(inputCoords);"; |
+ |
+ auto parse_points = [](const std::string& input, const gfx::Point& offset) { |
+ base::StringPairs pieces; |
+ base::SplitStringIntoKeyValuePairs(input, ',', ';', &pieces); |
+ std::vector<gfx::Point> points; |
+ for (const auto& piece : pieces) { |
+ int x, y; |
+ EXPECT_TRUE(base::StringToInt(piece.first, &x)); |
+ EXPECT_TRUE(base::StringToInt(piece.second, &y)); |
+ points.push_back(gfx::Point(x + offset.x(), y + offset.y())); |
+ } |
+ return points; |
+ }; |
+ auto parse_points_and_offsets = [parse_points](const std::string& input) { |
+ auto pieces = base::SplitString(input, ":", base::TRIM_WHITESPACE, |
+ base::SPLIT_WANT_NONEMPTY); |
+ gfx::Point origin; |
alexmos
2017/03/24 23:55:39
is there a better name? I'm thinking of the docum
avallee
2017/03/27 19:54:24
Well both the input points and the offset points f
alexmos
2017/03/27 23:09:28
Yes, either one of those sounds fine.
|
+ return make_pair(parse_points(pieces[0], origin), |
+ parse_points(pieces[1], origin)); |
+ }; |
+ |
+ // Add two input fields to each of the three frames. |
+ std::string result; |
+ EXPECT_TRUE(ExecuteScriptAndExtractString(main_frame, script, &result)); |
+ auto parsed = parse_points_and_offsets(result); |
+ auto main_points = parsed.first; |
alexmos
2017/03/24 23:55:39
nit: I'd rename to something like main_frame_input
avallee
2017/03/27 19:54:24
Done.
|
+ |
+ EXPECT_TRUE(ExecuteScriptAndExtractString(child1, script, &result)); |
+ auto child1_points = parse_points(result, parsed.second[0]); |
+ EXPECT_TRUE(ExecuteScriptAndExtractString(child2, script, &result)); |
+ auto child2_points = parse_points(result, parsed.second[1]); |
+ |
+ // Helper to simulate a tab press and wait for a focus message. |
+ auto press_tab_and_wait_for_message = [web_contents](bool reverse) { |
+ content::DOMMessageQueue msg_queue; |
+ std::string reply; |
+ LOG(ERROR) << "SimulateKeyPress"; |
alexmos
2017/03/24 23:55:39
Did you want to leave these and the console.log()
avallee
2017/03/27 19:54:25
nope, missed a couple.
|
+ SimulateKeyPress(web_contents, ui::DomKey::TAB, ui::DomCode::TAB, |
+ ui::VKEY_TAB, false, reverse /* shift */, false, false); |
+ EXPECT_TRUE(msg_queue.WaitForMessage(&reply)); |
+ LOG(ERROR) << "the reply is: " << reply; |
+ return reply; |
+ }; |
+ |
+ auto click_element_and_wait_for_message = |
+ [web_contents](const gfx::Point& point) { |
+ content::DOMMessageQueue msg_queue{web_contents}; |
alexmos
2017/03/24 23:55:39
Why the {} and not just ()? And do you need it at
avallee
2017/03/27 19:54:24
Gone, this was trying to fix the failure that I di
|
+ |
+ auto content_bounds = web_contents->GetContainerBounds(); |
+ ui_controls::SendMouseMove(point.x() + content_bounds.x(), |
+ point.y() + content_bounds.y()); |
+ ui_controls::SendMouseClick(ui_controls::LEFT); |
+ |
+ std::string reply; |
+ EXPECT_TRUE(msg_queue.WaitForMessage(&reply)); |
+ return reply; |
+ }; |
+ |
+ // Tab from child1 back to root. |
+ EXPECT_EQ("\"root-focused-input1\"", |
+ click_element_and_wait_for_message(main_points[0])); |
+ EXPECT_EQ(main_frame, web_contents->GetFocusedFrame()); |
+ EXPECT_EQ("\"child1-focused-input1\"", |
+ click_element_and_wait_for_message(child1_points[0])); |
+ EXPECT_EQ(child1, web_contents->GetFocusedFrame()); |
+ EXPECT_EQ("\"root-focused-input1\"", press_tab_and_wait_for_message(true)); |
+ EXPECT_EQ(main_frame, web_contents->GetFocusedFrame()); |
+ |
+ // Tab from child2 forward to root. |
+ EXPECT_EQ("\"root-focused-input2\"", |
+ click_element_and_wait_for_message(main_points[1])); |
+ EXPECT_EQ(main_frame, web_contents->GetFocusedFrame()); |
+ EXPECT_EQ("\"child2-focused-input2\"", |
+ click_element_and_wait_for_message(child2_points[1])); |
+ EXPECT_EQ(child2, web_contents->GetFocusedFrame()); |
+ EXPECT_EQ("\"root-focused-input2\"", press_tab_and_wait_for_message(false)); |
+ EXPECT_EQ(main_frame, web_contents->GetFocusedFrame()); |
+ |
+ // Tab forward from child1 to child2. |
+ EXPECT_EQ("\"child2-focused-input1\"", |
+ click_element_and_wait_for_message(child2_points[0])); |
+ EXPECT_EQ(child2, web_contents->GetFocusedFrame()); |
+ EXPECT_EQ("\"child1-focused-input2\"", |
+ click_element_and_wait_for_message(child1_points[1])); |
+ EXPECT_EQ(child1, web_contents->GetFocusedFrame()); |
+ LOG(ERROR) << "before fail"; |
alexmos
2017/03/24 23:55:39
remove? (also below)
avallee
2017/03/27 19:54:24
Done.
|
+ EXPECT_EQ("\"child2-focused-input1\"", press_tab_and_wait_for_message(false)); |
+ LOG(ERROR) << "after fail"; |
+ EXPECT_EQ(child2, web_contents->GetFocusedFrame()); |
+ |
+ // Tab backward from child2 to child1. |
+ EXPECT_EQ("\"child1-focused-input2\"", |
+ click_element_and_wait_for_message(child1_points[1])); |
+ EXPECT_EQ(child1, web_contents->GetFocusedFrame()); |
+ EXPECT_EQ("\"child2-focused-input1\"", |
+ click_element_and_wait_for_message(child2_points[0])); |
+ EXPECT_EQ(child2, web_contents->GetFocusedFrame()); |
+ EXPECT_EQ("\"child1-focused-input2\"", press_tab_and_wait_for_message(true)); |
+ EXPECT_EQ(child1, web_contents->GetFocusedFrame()); |
+ |
+ // Ensure there are no pending focus events after tabbing. |
+ EXPECT_EQ("\"root-focused-input1\"", |
+ click_element_and_wait_for_message(main_points[0])) |
+ << "Unexpected extra focus events."; |
+} |
+ |
namespace { |
// Helper to retrieve the frame's (window.innerWidth, window.innerHeight). |