diff options
9 files changed, 146 insertions, 8 deletions
diff --git a/chrome/browser/autocomplete/autocomplete_browsertest.cc b/chrome/browser/autocomplete/autocomplete_browsertest.cc index 26e071e..1e8b681 100644 --- a/chrome/browser/autocomplete/autocomplete_browsertest.cc +++ b/chrome/browser/autocomplete/autocomplete_browsertest.cc @@ -167,3 +167,94 @@ IN_PROC_BROWSER_TEST_F(AutocompleteBrowserTest, TabAwayRevertSelect) { location_bar->location_entry()->GetText()); EXPECT_TRUE(location_bar->location_entry()->IsSelectAll()); } + +IN_PROC_BROWSER_TEST_F(AutocompleteBrowserTest, FocusSearch) { + LocationBar* location_bar = GetLocationBar(); + + // Focus search when omnibox is blank + { + EXPECT_EQ(std::wstring(), location_bar->GetInputString()); + EXPECT_EQ(UTF8ToWide(chrome::kAboutBlankURL), + location_bar->location_entry()->GetText()); + + location_bar->FocusSearch(); + EXPECT_EQ(std::wstring(), location_bar->GetInputString()); + EXPECT_EQ(L"?", location_bar->location_entry()->GetText()); + + size_t selection_start, selection_end; + location_bar->location_entry()->GetSelectionBounds(&selection_start, + &selection_end); + EXPECT_EQ(1U, selection_start); + EXPECT_EQ(1U, selection_end); + } + + // Focus search when omnibox is _not_ alread in forced query mode. + { + location_bar->location_entry()->SetUserText(L"foo"); + EXPECT_EQ(std::wstring(), location_bar->GetInputString()); + EXPECT_EQ(L"foo", location_bar->location_entry()->GetText()); + + location_bar->FocusSearch(); + EXPECT_EQ(std::wstring(), location_bar->GetInputString()); + EXPECT_EQ(L"?", location_bar->location_entry()->GetText()); + + size_t selection_start, selection_end; + location_bar->location_entry()->GetSelectionBounds(&selection_start, + &selection_end); + EXPECT_EQ(1U, selection_start); + EXPECT_EQ(1U, selection_end); + } + + // Focus search when omnibox _is_ already in forced query mode, but no query + // has been typed. + { + location_bar->location_entry()->SetUserText(L"?"); + EXPECT_EQ(std::wstring(), location_bar->GetInputString()); + EXPECT_EQ(L"?", location_bar->location_entry()->GetText()); + + location_bar->FocusSearch(); + EXPECT_EQ(std::wstring(), location_bar->GetInputString()); + EXPECT_EQ(L"?", location_bar->location_entry()->GetText()); + + size_t selection_start, selection_end; + location_bar->location_entry()->GetSelectionBounds(&selection_start, + &selection_end); + EXPECT_EQ(1U, selection_start); + EXPECT_EQ(1U, selection_end); + } + + // Focus search when omnibox _is_ already in forced query mode, and some query + // has been typed. + { + location_bar->location_entry()->SetUserText(L"?foo"); + EXPECT_EQ(std::wstring(), location_bar->GetInputString()); + EXPECT_EQ(L"?foo", location_bar->location_entry()->GetText()); + + location_bar->FocusSearch(); + EXPECT_EQ(std::wstring(), location_bar->GetInputString()); + EXPECT_EQ(L"?foo", location_bar->location_entry()->GetText()); + + size_t selection_start, selection_end; + location_bar->location_entry()->GetSelectionBounds(&selection_start, + &selection_end); + EXPECT_EQ(1U, std::min(selection_start, selection_end)); + EXPECT_EQ(4U, std::max(selection_start, selection_end)); + } + + // Focus search when omnibox is in forced query mode with leading whitespace. + { + location_bar->location_entry()->SetUserText(L" ?foo"); + EXPECT_EQ(std::wstring(), location_bar->GetInputString()); + EXPECT_EQ(L" ?foo", location_bar->location_entry()->GetText()); + + location_bar->FocusSearch(); + EXPECT_EQ(std::wstring(), location_bar->GetInputString()); + EXPECT_EQ(L" ?foo", location_bar->location_entry()->GetText()); + + size_t selection_start, selection_end; + location_bar->location_entry()->GetSelectionBounds(&selection_start, + &selection_end); + EXPECT_EQ(4U, std::min(selection_start, selection_end)); + EXPECT_EQ(7U, std::max(selection_start, selection_end)); + } +} diff --git a/chrome/browser/autocomplete/autocomplete_edit_unittest.cc b/chrome/browser/autocomplete/autocomplete_edit_unittest.cc index 0cbec9e..cc8c6049 100644 --- a/chrome/browser/autocomplete/autocomplete_edit_unittest.cc +++ b/chrome/browser/autocomplete/autocomplete_edit_unittest.cc @@ -35,6 +35,8 @@ class TestingAutocompleteEditView : public AutocompleteEditView { size_t caret_pos) {} virtual void SetForcedQuery() {} virtual bool IsSelectAll() { return false; } + virtual void GetSelectionBounds(std::wstring::size_type* start, + std::wstring::size_type* end) {} virtual void SelectAll(bool reversed) {} virtual void RevertAll() {} virtual void UpdatePopup() {} diff --git a/chrome/browser/autocomplete/autocomplete_edit_view.h b/chrome/browser/autocomplete/autocomplete_edit_view.h index 7f473b2..ce9a227 100644 --- a/chrome/browser/autocomplete/autocomplete_edit_view.h +++ b/chrome/browser/autocomplete/autocomplete_edit_view.h @@ -1,4 +1,4 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Copyright (c) 2010 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -82,7 +82,7 @@ class AutocompleteEditView { // Sets the edit to forced query mode. Practically speaking, this means that // if the edit is not in forced query mode, its text is set to "?" with the // cursor at the end, and if the edit is in forced query mode (its first - // character is '?'), the text after the '?' is selected. + // non-whitespace character is '?'), the text after the '?' is selected. // // In the future we should display the search engine UI for the default engine // rather than '?'. @@ -91,6 +91,13 @@ class AutocompleteEditView { // Returns true if all text is selected or there is no text at all. virtual bool IsSelectAll() = 0; + // Fills |start| and |end| with the indexes of the current selection's bounds. + // It is not guaranteed that |*start < *end|, as the selection can be + // directed. If there is no selection, |start| and |end| will both be equal + // to the current cursor position. + virtual void GetSelectionBounds(std::wstring::size_type* start, + std::wstring::size_type* end) = 0; + // Selects all the text in the edit. Use this in place of SetSelAll() to // avoid selecting the "phantom newline" at the end of the edit. virtual void SelectAll(bool reversed) = 0; diff --git a/chrome/browser/autocomplete/autocomplete_edit_view_gtk.cc b/chrome/browser/autocomplete/autocomplete_edit_view_gtk.cc index ec86e2e..b6e44cc 100644 --- a/chrome/browser/autocomplete/autocomplete_edit_view_gtk.cc +++ b/chrome/browser/autocomplete/autocomplete_edit_view_gtk.cc @@ -12,6 +12,7 @@ #include "app/l10n_util.h" #include "base/gtk_util.h" #include "base/logging.h" +#include "base/string_util.h" #include "base/utf_string_conversions.h" #include "chrome/app/chrome_dll_resource.h" #include "chrome/browser/autocomplete/autocomplete_edit.h" @@ -437,11 +438,12 @@ void AutocompleteEditViewGtk::SetWindowTextAndCaretPos(const std::wstring& text, void AutocompleteEditViewGtk::SetForcedQuery() { const std::wstring current_text(GetText()); - if (current_text.empty() || (current_text[0] != '?')) { + const size_t start = current_text.find_first_not_of(kWhitespaceWide); + if (start == std::wstring::npos || (current_text[start] != '?')) { SetUserText(L"?"); } else { StartUpdatingHighlightedText(); - SetSelectedRange(CharRange(current_text.size(), 1)); + SetSelectedRange(CharRange(current_text.size(), start + 1)); FinishUpdatingHighlightedText(); } } @@ -458,6 +460,13 @@ bool AutocompleteEditViewGtk::IsSelectAll() { gtk_text_iter_equal(&end, &sel_end); } +void AutocompleteEditViewGtk::GetSelectionBounds(std::wstring::size_type* start, + std::wstring::size_type* end) { + CharRange selection = GetSelection(); + *start = static_cast<size_t>(selection.cp_min); + *end = static_cast<size_t>(selection.cp_max); +} + void AutocompleteEditViewGtk::SelectAll(bool reversed) { // SelectAll() is invoked as a side effect of other actions (e.g. switching // tabs or hitting Escape) in autocomplete_edit.cc, so we don't update the diff --git a/chrome/browser/autocomplete/autocomplete_edit_view_gtk.h b/chrome/browser/autocomplete/autocomplete_edit_view_gtk.h index 80056bc..7e94ca2 100644 --- a/chrome/browser/autocomplete/autocomplete_edit_view_gtk.h +++ b/chrome/browser/autocomplete/autocomplete_edit_view_gtk.h @@ -107,6 +107,8 @@ class AutocompleteEditViewGtk : public AutocompleteEditView, virtual void SetForcedQuery(); virtual bool IsSelectAll(); + virtual void GetSelectionBounds(std::wstring::size_type* start, + std::wstring::size_type* end); virtual void SelectAll(bool reversed); virtual void RevertAll(); diff --git a/chrome/browser/autocomplete/autocomplete_edit_view_mac.h b/chrome/browser/autocomplete/autocomplete_edit_view_mac.h index 8d87098..d3c2570 100644 --- a/chrome/browser/autocomplete/autocomplete_edit_view_mac.h +++ b/chrome/browser/autocomplete/autocomplete_edit_view_mac.h @@ -62,6 +62,8 @@ class AutocompleteEditViewMac : public AutocompleteEditView, virtual void SetForcedQuery(); virtual bool IsSelectAll(); + virtual void GetSelectionBounds(std::wstring::size_type* start, + std::wstring::size_type* end); virtual void SelectAll(bool reversed); virtual void RevertAll(); diff --git a/chrome/browser/autocomplete/autocomplete_edit_view_mac.mm b/chrome/browser/autocomplete/autocomplete_edit_view_mac.mm index 916cefc..499b031 100644 --- a/chrome/browser/autocomplete/autocomplete_edit_view_mac.mm +++ b/chrome/browser/autocomplete/autocomplete_edit_view_mac.mm @@ -348,10 +348,11 @@ void AutocompleteEditViewMac::SetForcedQuery() { FocusLocation(true); const std::wstring current_text(GetText()); - if (current_text.empty() || (current_text[0] != '?')) { + const size_t start = current_text.find_first_not_of(kWhitespaceWide); + if (start == std::wstring::npos || (current_text[start] != '?')) { SetUserText(L"?"); } else { - NSRange range = NSMakeRange(1, current_text.size() - 1); + NSRange range = NSMakeRange(start + 1, current_text.size() - start - 1); [[field_ currentEditor] setSelectedRange:range]; } } @@ -363,6 +364,18 @@ bool AutocompleteEditViewMac::IsSelectAll() { return NSEqualRanges(all_range, GetSelectedRange()); } +void AutocompleteEditViewMac::GetSelectionBounds(std::wstring::size_type* start, + std::wstring::size_type* end) { + if (![field_ currentEditor]) { + *start = *end = 0; + return; + } + + const NSRange selected_range = GetSelectedRange(); + *start = static_cast<size_t>(selected_range.location); + *end = static_cast<size_t>(NSMaxRange(selected_range)); +} + void AutocompleteEditViewMac::SelectAll(bool reversed) { // TODO(shess): Figure out what |reversed| implies. The gtk version // has it imply inverting the selection front to back, but I don't diff --git a/chrome/browser/autocomplete/autocomplete_edit_view_win.cc b/chrome/browser/autocomplete/autocomplete_edit_view_win.cc index e75ce26..a00526b 100644 --- a/chrome/browser/autocomplete/autocomplete_edit_view_win.cc +++ b/chrome/browser/autocomplete/autocomplete_edit_view_win.cc @@ -27,6 +27,7 @@ #include "base/iat_patch.h" #include "base/lazy_instance.h" #include "base/ref_counted.h" +#include "base/string_util.h" #include "base/utf_string_conversions.h" #include "chrome/app/chrome_dll_resource.h" #include "chrome/browser/autocomplete/autocomplete_accessibility.h" @@ -633,10 +634,11 @@ void AutocompleteEditViewWin::SetWindowTextAndCaretPos(const std::wstring& text, void AutocompleteEditViewWin::SetForcedQuery() { const std::wstring current_text(GetText()); - if (current_text.empty() || (current_text[0] != '?')) + const size_t start = current_text.find_first_not_of(kWhitespaceWide); + if (start == std::wstring::npos || (current_text[start] != '?')) SetUserText(L"?"); else - SetSelection(current_text.length(), 1); + SetSelection(current_text.length(), start + 1); } bool AutocompleteEditViewWin::IsSelectAll() { @@ -645,6 +647,14 @@ bool AutocompleteEditViewWin::IsSelectAll() { return IsSelectAllForRange(selection); } +void AutocompleteEditViewWin::GetSelectionBounds(std::wstring::size_type* start, + std::wstring::size_type* end) { + CHARRANGE selection; + GetSel(selection); + *start = static_cast<size_t>(selection.cpMin); + *end = static_cast<size_t>(selection.cpMax); +} + void AutocompleteEditViewWin::SelectAll(bool reversed) { if (reversed) SetSelection(GetTextLength(), 0); diff --git a/chrome/browser/autocomplete/autocomplete_edit_view_win.h b/chrome/browser/autocomplete/autocomplete_edit_view_win.h index 24d0321..25fb45f 100644 --- a/chrome/browser/autocomplete/autocomplete_edit_view_win.h +++ b/chrome/browser/autocomplete/autocomplete_edit_view_win.h @@ -107,6 +107,8 @@ class AutocompleteEditViewWin virtual void SetForcedQuery(); virtual bool IsSelectAll(); + virtual void GetSelectionBounds(std::wstring::size_type* start, + std::wstring::size_type* end); virtual void SelectAll(bool reversed); virtual void RevertAll(); |