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/browser/site_per_process_browsertest.h" | 5 #include "content/browser/site_per_process_browsertest.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
90 const std::string& name) { | 90 const std::string& name) { |
91 bool success = false; | 91 bool success = false; |
92 EXPECT_TRUE(ExecuteScriptAndExtractBool( | 92 EXPECT_TRUE(ExecuteScriptAndExtractBool( |
93 caller_frame, | 93 caller_frame, |
94 "window.domAutomationController.send(" | 94 "window.domAutomationController.send(" |
95 " !!window.open('" + url.spec() + "', '" + name + "'));", | 95 " !!window.open('" + url.spec() + "', '" + name + "'));", |
96 &success)); | 96 &success)); |
97 EXPECT_TRUE(success); | 97 EXPECT_TRUE(success); |
98 } | 98 } |
99 | 99 |
100 // Helper function to simulate pressing and releasing the provided key. Unlike | |
101 // SimulateKeyPress in browser_test_utils, which always routes the event | |
102 // through the top frame's RenderWidgetHost, this function routes the keyboard | |
103 // event to the focused frame's RenderWidgetHost. | |
104 void RouteKeyboardEvent(WebContentsImpl* web_contents, base::char16 key) { | |
alexmos
2015/10/23 23:40:16
I wanted to just change SimulateKeyPress in browse
kenrb
2015/10/28 21:07:05
I don't think you want it in the content API, so i
alexmos
2015/10/29 05:26:29
You're right, I got confused and thought that it d
| |
105 RenderWidgetHostImpl* target_rwh = web_contents->GetFocusedRenderWidgetHost(); | |
106 | |
107 NativeWebKeyboardEvent event; | |
108 event.text[0] = key; | |
109 event.unmodifiedText[0] = key; | |
110 | |
111 event.type = blink::WebKeyboardEvent::RawKeyDown; | |
112 target_rwh->ForwardKeyboardEvent(event); | |
113 event.type = blink::WebKeyboardEvent::Char; | |
114 target_rwh->ForwardKeyboardEvent(event); | |
115 event.type = blink::WebKeyboardEvent::KeyUp; | |
116 target_rwh->ForwardKeyboardEvent(event); | |
117 } | |
118 | |
119 // Helper function to generate a click on the given RenderWidgetHost. The | |
120 // mouse event is forwarded directly to the RenderWidgetHost without any | |
121 // hit-testing. | |
122 void SimulateMouseClick(RenderWidgetHost* rwh, int x, int y) { | |
123 blink::WebMouseEvent mouse_event; | |
124 mouse_event.type = blink::WebInputEvent::MouseDown; | |
125 mouse_event.button = blink::WebPointerProperties::ButtonLeft; | |
126 mouse_event.x = x; | |
127 mouse_event.y = y; | |
128 rwh->ForwardMouseEvent(mouse_event); | |
129 } | |
130 | |
100 class RedirectNotificationObserver : public NotificationObserver { | 131 class RedirectNotificationObserver : public NotificationObserver { |
101 public: | 132 public: |
102 // Register to listen for notifications of the given type from either a | 133 // Register to listen for notifications of the given type from either a |
103 // specific source, or from all sources if |source| is | 134 // specific source, or from all sources if |source| is |
104 // NotificationService::AllSources(). | 135 // NotificationService::AllSources(). |
105 RedirectNotificationObserver(int notification_type, | 136 RedirectNotificationObserver(int notification_type, |
106 const NotificationSource& source); | 137 const NotificationSource& source); |
107 ~RedirectNotificationObserver() override; | 138 ~RedirectNotificationObserver() override; |
108 | 139 |
109 // Wait until the specified notification occurs. If the notification was | 140 // Wait until the specified notification occurs. If the notification was |
(...skipping 3371 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
3481 EXPECT_TRUE(ExecuteScriptAndExtractString(shell()->web_contents(), | 3512 EXPECT_TRUE(ExecuteScriptAndExtractString(shell()->web_contents(), |
3482 "focusInputField()", &result)); | 3513 "focusInputField()", &result)); |
3483 EXPECT_EQ(result, "input-focus"); | 3514 EXPECT_EQ(result, "input-focus"); |
3484 | 3515 |
3485 // The main frame should be focused. | 3516 // The main frame should be focused. |
3486 EXPECT_EQ(root, root->frame_tree()->GetFocusedFrame()); | 3517 EXPECT_EQ(root, root->frame_tree()->GetFocusedFrame()); |
3487 | 3518 |
3488 DOMMessageQueue msg_queue; | 3519 DOMMessageQueue msg_queue; |
3489 | 3520 |
3490 // Click on the cross-process subframe. | 3521 // Click on the cross-process subframe. |
3491 blink::WebMouseEvent mouse_event; | 3522 SimulateMouseClick( |
3492 mouse_event.type = blink::WebInputEvent::MouseDown; | 3523 root->child_at(0)->current_frame_host()->GetRenderWidgetHost(), 1, 1); |
3493 mouse_event.button = blink::WebPointerProperties::ButtonLeft; | |
3494 mouse_event.x = 1; | |
3495 mouse_event.y = 1; | |
3496 RenderWidgetHost* rwh_child = | |
3497 root->child_at(0)->current_frame_host()->GetRenderWidgetHost(); | |
3498 rwh_child->ForwardMouseEvent(mouse_event); | |
3499 | 3524 |
3500 // Check that the main frame lost focus and fired blur event on the input | 3525 // Check that the main frame lost focus and fired blur event on the input |
3501 // text field. | 3526 // text field. |
3502 std::string status; | 3527 std::string status; |
3503 while (msg_queue.WaitForMessage(&status)) { | 3528 while (msg_queue.WaitForMessage(&status)) { |
3504 if (status == "\"input-blur\"") | 3529 if (status == "\"input-blur\"") |
3505 break; | 3530 break; |
3506 } | 3531 } |
3507 | 3532 |
3508 // The subframe should now be focused. | 3533 // The subframe should now be focused. |
3509 EXPECT_EQ(root->child_at(0), root->frame_tree()->GetFocusedFrame()); | 3534 EXPECT_EQ(root->child_at(0), root->frame_tree()->GetFocusedFrame()); |
3510 | 3535 |
3511 // Click on the root frame. | 3536 // Click on the root frame. |
3512 shell()->web_contents()->GetRenderViewHost()->GetWidget()->ForwardMouseEvent( | 3537 SimulateMouseClick( |
3513 mouse_event); | 3538 shell()->web_contents()->GetRenderViewHost()->GetWidget(), 1, 1); |
3514 | 3539 |
3515 // Check that the subframe lost focus and fired blur event on its | 3540 // Check that the subframe lost focus and fired blur event on its |
3516 // document's body. | 3541 // document's body. |
3517 while (msg_queue.WaitForMessage(&status)) { | 3542 while (msg_queue.WaitForMessage(&status)) { |
3518 if (status == "\"document-blur\"") | 3543 if (status == "\"document-blur\"") |
3519 break; | 3544 break; |
3520 } | 3545 } |
3521 | 3546 |
3522 // The root frame should be focused again. | 3547 // The root frame should be focused again. |
3523 EXPECT_EQ(root, root->frame_tree()->GetFocusedFrame()); | 3548 EXPECT_EQ(root, root->frame_tree()->GetFocusedFrame()); |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
3619 EXPECT_EQ(popup->web_contents()->GetLastCommittedURL(), cross_url); | 3644 EXPECT_EQ(popup->web_contents()->GetLastCommittedURL(), cross_url); |
3620 | 3645 |
3621 // Use new window to navigate main window. | 3646 // Use new window to navigate main window. |
3622 std::string script = | 3647 std::string script = |
3623 "window.opener.location.href = '" + cross_url.spec() + "'"; | 3648 "window.opener.location.href = '" + cross_url.spec() + "'"; |
3624 EXPECT_TRUE(ExecuteScript(popup->web_contents(), script)); | 3649 EXPECT_TRUE(ExecuteScript(popup->web_contents(), script)); |
3625 EXPECT_TRUE(WaitForLoadStop(shell()->web_contents())); | 3650 EXPECT_TRUE(WaitForLoadStop(shell()->web_contents())); |
3626 EXPECT_EQ(shell()->web_contents()->GetLastCommittedURL(), cross_url); | 3651 EXPECT_EQ(shell()->web_contents()->GetLastCommittedURL(), cross_url); |
3627 } | 3652 } |
3628 | 3653 |
3654 // Ensure that a cross-process subframe can receive keyboard events when in | |
3655 // focus. | |
3656 IN_PROC_BROWSER_TEST_F(SitePerProcessBrowserTest, | |
3657 SubframeKeyboardEventRouting) { | |
3658 GURL main_url(embedded_test_server()->GetURL( | |
3659 "a.com", "/frame_tree/page_with_one_frame.html")); | |
3660 EXPECT_TRUE(NavigateToURL(shell(), main_url)); | |
3661 | |
3662 WebContentsImpl* web_contents = | |
3663 static_cast<WebContentsImpl*>(shell()->web_contents()); | |
3664 FrameTreeNode* root = web_contents->GetFrameTree()->root(); | |
3665 | |
3666 GURL frame_url( | |
3667 embedded_test_server()->GetURL("b.com", "/page_with_input_field.html")); | |
3668 NavigateFrameToURL(root->child_at(0), frame_url); | |
3669 EXPECT_TRUE(WaitForRenderFrameReady(root->child_at(0)->current_frame_host())); | |
3670 | |
3671 // Click on the subframe to focus it. | |
3672 SimulateMouseClick( | |
3673 root->child_at(0)->current_frame_host()->GetRenderWidgetHost(), 1, 1); | |
3674 | |
3675 // Focus the input field in the subframe. The return value "input-focus" | |
3676 // will be sent once the input field's focus event fires. | |
3677 std::string result; | |
3678 EXPECT_TRUE(ExecuteScriptAndExtractString( | |
3679 root->child_at(0)->current_frame_host(), "focusInputField()", &result)); | |
3680 EXPECT_EQ(result, "input-focus"); | |
3681 | |
3682 // The subframe should now be focused. | |
3683 EXPECT_EQ(root->child_at(0), root->frame_tree()->GetFocusedFrame()); | |
3684 | |
3685 // Generate a few keyboard events and route them to currently focused frame. | |
3686 RouteKeyboardEvent(web_contents, 'f'); | |
3687 RouteKeyboardEvent(web_contents, 'o'); | |
3688 RouteKeyboardEvent(web_contents, 'o'); | |
3689 | |
3690 // Verify that the input field in the subframe received the keystrokes. | |
3691 EXPECT_TRUE(ExecuteScriptAndExtractString( | |
3692 root->child_at(0)->current_frame_host(), | |
3693 "window.domAutomationController.send(getInputFieldText());", &result)); | |
3694 EXPECT_EQ("foo", result); | |
3695 } | |
3696 | |
3629 } // namespace content | 3697 } // namespace content |
OLD | NEW |