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

Side by Side Diff: chrome/browser/renderer_host/site_per_process_text_input_browsertest.cc

Issue 2641403002: [refactor] - Move IsFocusedElementEditable() and ClearFocusedElement() from RenderViewHost to WebCo… (Closed)
Patch Set: Addressed sky@'s comment Created 3 years, 11 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
« no previous file with comments | « no previous file | chrome/browser/ui/views/find_bar_host.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 <vector> 5 #include <vector>
6 6
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "base/strings/utf_string_conversions.h" 8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/chrome_content_browser_client.h" 9 #include "chrome/browser/chrome_content_browser_client.h"
10 #include "chrome/browser/ui/browser.h" 10 #include "chrome/browser/ui/browser.h"
11 #include "chrome/browser/ui/tabs/tab_strip_model.h" 11 #include "chrome/browser/ui/tabs/tab_strip_model.h"
12 #include "chrome/test/base/in_process_browser_test.h" 12 #include "chrome/test/base/in_process_browser_test.h"
13 #include "chrome/test/base/interactive_test_utils.h" 13 #include "chrome/test/base/interactive_test_utils.h"
14 #include "chrome/test/base/ui_test_utils.h" 14 #include "chrome/test/base/ui_test_utils.h"
15 #include "content/public/browser/content_browser_client.h" 15 #include "content/public/browser/content_browser_client.h"
16 #include "content/public/browser/render_frame_host.h" 16 #include "content/public/browser/render_frame_host.h"
17 #include "content/public/browser/render_process_host.h" 17 #include "content/public/browser/render_process_host.h"
18 #include "content/public/browser/render_view_host.h"
19 #include "content/public/browser/render_widget_host_view.h" 18 #include "content/public/browser/render_widget_host_view.h"
20 #include "content/public/browser/web_contents.h" 19 #include "content/public/browser/web_contents.h"
21 #include "content/public/common/content_client.h" 20 #include "content/public/common/content_client.h"
22 #include "content/public/common/form_field_data.h" 21 #include "content/public/common/form_field_data.h"
23 #include "content/public/test/browser_test_utils.h" 22 #include "content/public/test/browser_test_utils.h"
24 #include "content/public/test/content_browser_test_utils.h" 23 #include "content/public/test/content_browser_test_utils.h"
25 #include "content/public/test/test_utils.h" 24 #include "content/public/test/test_utils.h"
26 #include "content/public/test/text_input_test_utils.h" 25 #include "content/public/test/text_input_test_utils.h"
27 #include "net/dns/mock_host_resolver.h" 26 #include "net/dns/mock_host_resolver.h"
28 #include "net/test/embedded_test_server/embedded_test_server.h" 27 #include "net/test/embedded_test_server/embedded_test_server.h"
(...skipping 327 matching lines...) Expand 10 before | Expand all | Expand 10 after
356 embedded_test_server()->ServeFilesFromSourceDirectory("content/test/data"); 355 embedded_test_server()->ServeFilesFromSourceDirectory("content/test/data");
357 356
358 ASSERT_TRUE(embedded_test_server()->Start()); 357 ASSERT_TRUE(embedded_test_server()->Start());
359 } 358 }
360 359
361 protected: 360 protected:
362 content::WebContents* active_contents() { 361 content::WebContents* active_contents() {
363 return browser()->tab_strip_model()->GetActiveWebContents(); 362 return browser()->tab_strip_model()->GetActiveWebContents();
364 } 363 }
365 364
366 content::RenderViewHost* render_view_host() {
367 return active_contents()->GetRenderViewHost();
368 }
369
370 // static 365 // static
371 // Adds an <input> field to a given frame by executing javascript code. 366 // Adds an <input> field to a given frame by executing javascript code.
372 // The input can be added as the first element or the last element of 367 // The input can be added as the first element or the last element of
373 // |document.body|. The text range defined by |selection_range| will be 368 // |document.body|. The text range defined by |selection_range| will be
374 // marked. 369 // marked.
375 static void AddInputFieldToFrame(content::RenderFrameHost* rfh, 370 static void AddInputFieldToFrame(content::RenderFrameHost* rfh,
376 const std::string& type, 371 const std::string& type,
377 const std::string& value, 372 const std::string& value,
378 bool append_as_first_child) { 373 bool append_as_first_child) {
379 std::string script = base::StringPrintf( 374 std::string script = base::StringPrintf(
(...skipping 524 matching lines...) Expand 10 before | Expand all | Expand 10 after
904 ExecuteScript(frame, "document.querySelector('input').blur();")); 899 ExecuteScript(frame, "document.querySelector('input').blur();"));
905 return content::DoesFrameHaveFocusedEditableElement(frame); 900 return content::DoesFrameHaveFocusedEditableElement(frame);
906 }; 901 };
907 902
908 // Similarly, we should receive updates when losing focus. 903 // Similarly, we should receive updates when losing focus.
909 for (auto* frame : frames) 904 for (auto* frame : frames)
910 EXPECT_FALSE(blur_input_and_return_editable_element_state(frame)); 905 EXPECT_FALSE(blur_input_and_return_editable_element_state(frame));
911 } 906 }
912 907
913 // This test tracks page level focused editable element tracking using 908 // This test tracks page level focused editable element tracking using
914 // RenderViewHost. In a page with multiple frames, a frame is selected and 909 // WebContents. In a page with multiple frames, a frame is selected and
915 // focused. Then the <input> inside frame is both focused and blurred and and 910 // focused. Then the <input> inside frame is both focused and blurred and and
916 // in both cases the test verifies that RendeViewHost is aware whether or not a 911 // in both cases the test verifies that WebContents is aware whether or not a
917 // focused editable element exists on the page. 912 // focused editable element exists on the page.
918 IN_PROC_BROWSER_TEST_F(SitePerProcessTextInputManagerTest, 913 IN_PROC_BROWSER_TEST_F(SitePerProcessTextInputManagerTest,
919 TrackPageFocusEditableElement) { 914 TrackPageFocusEditableElement) {
920 CreateIframePage("a(a, b(a))"); 915 CreateIframePage("a(a, b(a))");
921 std::vector<content::RenderFrameHost*> frames{ 916 std::vector<content::RenderFrameHost*> frames{
922 GetFrame(IndexVector{}), GetFrame(IndexVector{0}), 917 GetFrame(IndexVector{}), GetFrame(IndexVector{0}),
923 GetFrame(IndexVector{1}), GetFrame(IndexVector{1, 0})}; 918 GetFrame(IndexVector{1}), GetFrame(IndexVector{1, 0})};
924 for (size_t i = 0; i < frames.size(); ++i) 919 for (size_t i = 0; i < frames.size(); ++i)
925 AddInputFieldToFrame(frames[i], "text", "some text", true); 920 AddInputFieldToFrame(frames[i], "text", "some text", true);
926 921
927 auto focus_frame = [](content::RenderFrameHost* frame) { 922 auto focus_frame = [](content::RenderFrameHost* frame) {
928 EXPECT_TRUE(ExecuteScript(frame, "window.focus();")); 923 EXPECT_TRUE(ExecuteScript(frame, "window.focus();"));
929 }; 924 };
930 925
931 auto set_input_focus = [](content::RenderFrameHost* frame, bool focus) { 926 auto set_input_focus = [](content::RenderFrameHost* frame, bool focus) {
932 EXPECT_TRUE(ExecuteScript( 927 EXPECT_TRUE(ExecuteScript(
933 frame, base::StringPrintf("document.querySelector('input').%s();", 928 frame, base::StringPrintf("document.querySelector('input').%s();",
934 (focus ? "focus" : "blur")))); 929 (focus ? "focus" : "blur"))));
935 }; 930 };
936 931
937 for (auto* frame : frames) { 932 for (auto* frame : frames) {
938 focus_frame(frame); 933 focus_frame(frame);
939 // Focus the <input>. 934 // Focus the <input>.
940 set_input_focus(frame, true); 935 set_input_focus(frame, true);
941 EXPECT_TRUE(render_view_host()->IsFocusedElementEditable()); 936 EXPECT_TRUE(active_contents()->IsFocusedElementEditable());
942 // No blur <input>. 937 // No blur <input>.
943 set_input_focus(frame, false); 938 set_input_focus(frame, false);
944 EXPECT_FALSE(render_view_host()->IsFocusedElementEditable()); 939 EXPECT_FALSE(active_contents()->IsFocusedElementEditable());
945 } 940 }
946 } 941 }
947 942
948 // TODO(ekaramad): Could this become a unit test instead? 943 // TODO(ekaramad): Could this become a unit test instead?
949 // This test focuses <input> elements on the page and verifies that 944 // This test focuses <input> elements on the page and verifies that
950 // RenderViewHost knows about the focused editable element. Then it asks the 945 // WebContents knows about the focused editable element. Then it asks the
951 // RenderViewHost to clear focused element and verifies that there is no longer 946 // WebContents to clear focused element and verifies that there is no longer
952 // a focused editable element on the page. 947 // a focused editable element on the page.
953 IN_PROC_BROWSER_TEST_F(SitePerProcessTextInputManagerTest, 948 IN_PROC_BROWSER_TEST_F(SitePerProcessTextInputManagerTest,
954 ClearFocusedElementOnPage) { 949 ClearFocusedElementOnPage) {
955 CreateIframePage("a(a, b(a))"); 950 CreateIframePage("a(a, b(a))");
956 std::vector<content::RenderFrameHost*> frames{ 951 std::vector<content::RenderFrameHost*> frames{
957 GetFrame(IndexVector{}), GetFrame(IndexVector{0}), 952 GetFrame(IndexVector{}), GetFrame(IndexVector{0}),
958 GetFrame(IndexVector{1}), GetFrame(IndexVector{1, 0})}; 953 GetFrame(IndexVector{1}), GetFrame(IndexVector{1, 0})};
959 for (size_t i = 0; i < frames.size(); ++i) 954 for (size_t i = 0; i < frames.size(); ++i)
960 AddInputFieldToFrame(frames[i], "text", "some text", true); 955 AddInputFieldToFrame(frames[i], "text", "some text", true);
961 956
962 auto focus_frame_and_input = [](content::RenderFrameHost* frame) { 957 auto focus_frame_and_input = [](content::RenderFrameHost* frame) {
963 EXPECT_TRUE(ExecuteScript(frame, 958 EXPECT_TRUE(ExecuteScript(frame,
964 "window.focus();" 959 "window.focus();"
965 "document.querySelector('input').focus();")); 960 "document.querySelector('input').focus();"));
966 }; 961 };
967 962
968 for (auto* frame : frames) { 963 for (auto* frame : frames) {
969 focus_frame_and_input(frame); 964 focus_frame_and_input(frame);
970 EXPECT_TRUE(render_view_host()->IsFocusedElementEditable()); 965 EXPECT_TRUE(active_contents()->IsFocusedElementEditable());
971 render_view_host()->ClearFocusedElement(); 966 active_contents()->ClearFocusedElement();
972 EXPECT_FALSE(render_view_host()->IsFocusedElementEditable()); 967 EXPECT_FALSE(active_contents()->IsFocusedElementEditable());
973 } 968 }
974 } 969 }
975 970
976 // TODO(ekaramad): The following tests are specifically written for Aura and are 971 // TODO(ekaramad): The following tests are specifically written for Aura and are
977 // based on InputMethodObserver. Write similar tests for Mac/Android/Mus 972 // based on InputMethodObserver. Write similar tests for Mac/Android/Mus
978 // (crbug.com/602723). 973 // (crbug.com/602723).
979 #if defined(USE_AURA) 974 #if defined(USE_AURA)
980 // ----------------------------------------------------------------------------- 975 // -----------------------------------------------------------------------------
981 // Input Method Observer Tests 976 // Input Method Observer Tests
982 // 977 //
(...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after
1347 1342
1348 // Closing this WebContents while we still hold on to our TestBrowserClient. 1343 // Closing this WebContents while we still hold on to our TestBrowserClient.
1349 EXPECT_TRUE(browser()->tab_strip_model()->CloseWebContentsAt( 1344 EXPECT_TRUE(browser()->tab_strip_model()->CloseWebContentsAt(
1350 1, TabStripModel::CLOSE_USER_GESTURE)); 1345 1, TabStripModel::CLOSE_USER_GESTURE));
1351 1346
1352 // For the cleanup of the original WebContents in tab index 0. 1347 // For the cleanup of the original WebContents in tab index 0.
1353 content::SetBrowserClientForTesting(old_browser_client); 1348 content::SetBrowserClientForTesting(old_browser_client);
1354 } 1349 }
1355 #endif // defined(MAC_OSX) 1350 #endif // defined(MAC_OSX)
1356 #endif // !defined(OS_ANDROID) 1351 #endif // !defined(OS_ANDROID)
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/ui/views/find_bar_host.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698