Commit 5cc4cc8d authored by Russell Davis's avatar Russell Davis Committed by Commit Bot

Fix Find behaving like Find Next

This bug happened in two cases when using Find:
1) On macOS, always
2) On all platforms, when text is selected

This change fixes both cases, while also unifying macOS behavior with
other platforms. (I think the previous exclusion of macOS from the
find-from-selection behavior may have been due to a misunderstanding of
some mac-specific code/comments.)

Now, when doing a find-from-selection, the initial result is the same
selected text, rather than the next occurrence. So it's a no-op
(as it should be) in terms of the selection and the document scroll
position.

Bug: 1043550
Change-Id: I78a27b74324f74b1e3c7528fc91badf85f65d22f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2145247
Auto-Submit: Russell Davis <russell.davis@gmail.com>
Reviewed-by: default avatarScott Violet <sky@chromium.org>
Reviewed-by: default avatarJeff Fisher <jeffish@microsoft.com>
Commit-Queue: Jeff Fisher <jeffish@microsoft.com>
Cr-Commit-Position: refs/heads/master@{#759048}
parent 709fad70
......@@ -827,6 +827,7 @@ Ruben Bridgewater <ruben@bridgewater.de>
Ruben Terrazas <rubentopo@gmail.com>
Rufus Hamade <rufus.hamade@imgtec.com>
Ruiyi Luo <luoruiyi2008@gmail.com>
Russell Davis <russell.davis@gmail.com>
Ryan Ackley <ryanackley@gmail.com>
Ryan Norton <rnorton10@gmail.com>
Ryan Sleevi <ryan-chromium-dev@sleevi.com>
......
......@@ -51,27 +51,37 @@ void FindBarController::Show(bool find_next, bool forward_direction) {
find_bar_->SetFocusAndSelection();
base::string16 find_text;
if (!find_next && !has_user_modified_text_) {
base::string16 selected_text = GetSelectedText();
if (selected_text.length() <= 250)
find_text = selected_text;
}
#if defined(OS_MACOSX)
// We always want to search for the current contents of the find bar on
// OS X. For regular profile it's always the current find pboard. For
// Incognito window it's the newest value of the find pboard content and
// user-typed text.
find_text = find_bar_->GetFindText();
if (find_next) {
// For macOS, we always want to search for the current contents of the find
// bar on OS X, rather than the behavior we'd get with empty find_text
// (see FindBarState::GetSearchPrepopulateText).
find_text = find_bar_->GetFindText();
}
#endif
if (!find_text.empty() || find_next) {
// Don't update the local input if we're using the global pasteboard.
if (!find_bar_->HasGlobalFindPasteboard())
if (!find_next && !has_user_modified_text_) {
base::string16 selected_text = GetSelectedText();
auto selected_length = selected_text.length();
if (selected_length > 0 && selected_length <= 250) {
find_text = selected_text;
find_bar_->SetFindTextAndSelectedRange(find_text,
gfx::Range(0, find_text.length()));
find_tab_helper->StartFinding(find_text, forward_direction, false);
if (web_contents_) {
// Collapse the selection to its start, so we can run a find_next and
// make it find the selection. This is a no-op in terms of what ends
// up selected, but initializes the rest of the find machinery (like
// showing how many matches there are in the document).
web_contents_->AdjustSelectionByCharacterOffset(0, -selected_length,
false);
find_next = true;
}
}
}
if (find_next)
find_tab_helper->StartFinding(find_text, forward_direction, false);
}
void FindBarController::EndFindSession(
......
......@@ -619,8 +619,6 @@ IN_PROC_BROWSER_TEST_F(FindInPageTest, MAYBE_CtrlEnter) {
observer.Wait();
}
// FindInPage on Mac doesn't use prepopulated values. Search there is global.
#if !defined(OS_MACOSX)
IN_PROC_BROWSER_TEST_F(FindInPageTest, SelectionDuringFind) {
ASSERT_TRUE(embedded_test_server()->Start());
// Make sure Chrome is in the foreground, otherwise sending input
......@@ -645,12 +643,14 @@ IN_PROC_BROWSER_TEST_F(FindInPageTest, SelectionDuringFind) {
browser()->GetFindBarController()->Show();
EXPECT_TRUE(IsViewFocused(browser(), VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
// verify the text matches the selection
// Verify the text matches the selection
EXPECT_EQ(ASCIIToUTF16("text"), GetFindBarText());
find_in_page::FindNotificationDetails details = WaitForFindResult();
EXPECT_TRUE(details.number_of_matches() > 0);
// Verify the correct match is highlighted (the one corresponding to the
// text that was selected). See http://crbug.com/1043550
EXPECT_EQ(2, details.active_match_ordinal());
EXPECT_EQ(5, details.number_of_matches());
}
#endif
IN_PROC_BROWSER_TEST_F(FindInPageTest, GlobalEscapeClosesFind) {
ASSERT_TRUE(embedded_test_server()->Start());
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment