autocomplete_browsertest.cc revision dc0f95d653279beabeb9817299e2902918ba123e
1// Copyright (c) 2010 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5#include "base/format_macros.h" 6#include "base/string_util.h" 7#include "base/utf_string_conversions.h" 8#include "chrome/browser/autocomplete/autocomplete.h" 9#include "chrome/browser/autocomplete/autocomplete_edit.h" 10#include "chrome/browser/autocomplete/autocomplete_edit_view.h" 11#include "chrome/browser/autocomplete/autocomplete_match.h" 12#include "chrome/browser/autocomplete/autocomplete_popup_model.h" 13#include "chrome/browser/browser_window.h" 14#include "chrome/browser/history/history.h" 15#include "chrome/browser/tabs/tab_strip_model.h" 16#include "chrome/browser/ui/browser.h" 17#include "chrome/browser/ui/omnibox/location_bar.h" 18#include "chrome/common/notification_type.h" 19#include "chrome/common/url_constants.h" 20#include "chrome/test/in_process_browser_test.h" 21#include "chrome/test/ui_test_utils.h" 22#include "content/browser/tab_contents/tab_contents.h" 23#include "testing/gtest/include/gtest/gtest.h" 24 25// Autocomplete test is flaky on ChromeOS. 26// http://crbug.com/52928 27#if defined(OS_CHROMEOS) 28#define MAYBE_Autocomplete FLAKY_Autocomplete 29#else 30#define MAYBE_Autocomplete Autocomplete 31#endif 32 33 34namespace { 35 36string16 AutocompleteResultAsString(const AutocompleteResult& result) { 37 std::string output(base::StringPrintf("{%" PRIuS "} ", result.size())); 38 for (size_t i = 0; i < result.size(); ++i) { 39 AutocompleteMatch match = result.match_at(i); 40 std::string provider_name = match.provider->name(); 41 output.append(base::StringPrintf("[\"%s\" by \"%s\"] ", 42 UTF16ToUTF8(match.contents).c_str(), 43 provider_name.c_str())); 44 } 45 return UTF8ToUTF16(output); 46} 47 48} // namespace 49 50class AutocompleteBrowserTest : public InProcessBrowserTest { 51 protected: 52 LocationBar* GetLocationBar() const { 53 return browser()->window()->GetLocationBar(); 54 } 55 56 AutocompleteController* GetAutocompleteController() const { 57 return GetLocationBar()->location_entry()->model()->popup_model()-> 58 autocomplete_controller(); 59 } 60}; 61 62IN_PROC_BROWSER_TEST_F(AutocompleteBrowserTest, Basic) { 63 LocationBar* location_bar = GetLocationBar(); 64 65 EXPECT_TRUE(location_bar->GetInputString().empty()); 66 EXPECT_EQ(UTF8ToUTF16(chrome::kAboutBlankURL), 67 location_bar->location_entry()->GetText()); 68 // TODO(phajdan.jr): check state of IsSelectAll when it's consistent across 69 // platforms. 70 71 location_bar->FocusLocation(true); 72 73 EXPECT_TRUE(location_bar->GetInputString().empty()); 74 EXPECT_EQ(UTF8ToUTF16(chrome::kAboutBlankURL), 75 location_bar->location_entry()->GetText()); 76 EXPECT_TRUE(location_bar->location_entry()->IsSelectAll()); 77 78 location_bar->location_entry()->SetUserText(ASCIIToUTF16("chrome")); 79 80 EXPECT_TRUE(location_bar->GetInputString().empty()); 81 EXPECT_EQ(ASCIIToUTF16("chrome"), location_bar->location_entry()->GetText()); 82 EXPECT_FALSE(location_bar->location_entry()->IsSelectAll()); 83 84 location_bar->location_entry()->RevertAll(); 85 86 EXPECT_TRUE(location_bar->GetInputString().empty()); 87 EXPECT_EQ(UTF8ToUTF16(chrome::kAboutBlankURL), 88 location_bar->location_entry()->GetText()); 89 EXPECT_FALSE(location_bar->location_entry()->IsSelectAll()); 90 91 location_bar->location_entry()->SetUserText(ASCIIToUTF16("chrome")); 92 location_bar->Revert(); 93 94 EXPECT_TRUE(location_bar->GetInputString().empty()); 95 EXPECT_EQ(UTF8ToUTF16(chrome::kAboutBlankURL), 96 location_bar->location_entry()->GetText()); 97 EXPECT_FALSE(location_bar->location_entry()->IsSelectAll()); 98} 99 100IN_PROC_BROWSER_TEST_F(AutocompleteBrowserTest, MAYBE_Autocomplete) { 101 // The results depend on the history backend being loaded. Make sure it is 102 // loaded so that the autocomplete results are consistent. 103 ui_test_utils::WaitForHistoryToLoad(browser()); 104 105 LocationBar* location_bar = GetLocationBar(); 106 AutocompleteController* autocomplete_controller = GetAutocompleteController(); 107 108 { 109 autocomplete_controller->Start(ASCIIToUTF16("chrome"), string16(), 110 true, false, true, true); 111 112 EXPECT_TRUE(autocomplete_controller->done()); 113 EXPECT_TRUE(location_bar->GetInputString().empty()); 114 EXPECT_TRUE(location_bar->location_entry()->GetText().empty()); 115 EXPECT_TRUE(location_bar->location_entry()->IsSelectAll()); 116 const AutocompleteResult& result = autocomplete_controller->result(); 117 ASSERT_EQ(1U, result.size()) << AutocompleteResultAsString(result); 118 AutocompleteMatch match = result.match_at(0); 119 EXPECT_EQ(AutocompleteMatch::SEARCH_WHAT_YOU_TYPED, match.type); 120 EXPECT_FALSE(match.deletable); 121 } 122 123 { 124 location_bar->Revert(); 125 126 EXPECT_TRUE(location_bar->GetInputString().empty()); 127 EXPECT_EQ(UTF8ToUTF16(chrome::kAboutBlankURL), 128 location_bar->location_entry()->GetText()); 129 EXPECT_FALSE(location_bar->location_entry()->IsSelectAll()); 130 const AutocompleteResult& result = autocomplete_controller->result(); 131 EXPECT_TRUE(result.empty()) << AutocompleteResultAsString(result); 132 } 133} 134 135IN_PROC_BROWSER_TEST_F(AutocompleteBrowserTest, TabAwayRevertSelect) { 136 // http://code.google.com/p/chromium/issues/detail?id=38385 137 // Make sure that tabbing away from an empty omnibar causes a revert 138 // and select all. 139 LocationBar* location_bar = GetLocationBar(); 140 EXPECT_EQ(UTF8ToUTF16(chrome::kAboutBlankURL), 141 location_bar->location_entry()->GetText()); 142 location_bar->location_entry()->SetUserText(string16()); 143 browser()->AddSelectedTabWithURL(GURL(chrome::kAboutBlankURL), 144 PageTransition::START_PAGE); 145 ui_test_utils::WaitForNavigation( 146 &browser()->GetSelectedTabContents()->controller()); 147 EXPECT_EQ(UTF8ToUTF16(chrome::kAboutBlankURL), 148 location_bar->location_entry()->GetText()); 149 browser()->CloseTab(); 150 EXPECT_EQ(UTF8ToUTF16(chrome::kAboutBlankURL), 151 location_bar->location_entry()->GetText()); 152 EXPECT_TRUE(location_bar->location_entry()->IsSelectAll()); 153} 154 155IN_PROC_BROWSER_TEST_F(AutocompleteBrowserTest, FocusSearch) { 156 LocationBar* location_bar = GetLocationBar(); 157 158 // Focus search when omnibox is blank 159 { 160 EXPECT_TRUE(location_bar->GetInputString().empty()); 161 EXPECT_EQ(UTF8ToUTF16(chrome::kAboutBlankURL), 162 location_bar->location_entry()->GetText()); 163 164 location_bar->FocusSearch(); 165 EXPECT_TRUE(location_bar->GetInputString().empty()); 166 EXPECT_EQ(ASCIIToUTF16("?"), location_bar->location_entry()->GetText()); 167 168 size_t selection_start, selection_end; 169 location_bar->location_entry()->GetSelectionBounds(&selection_start, 170 &selection_end); 171 EXPECT_EQ(1U, selection_start); 172 EXPECT_EQ(1U, selection_end); 173 } 174 175 // Focus search when omnibox is _not_ alread in forced query mode. 176 { 177 location_bar->location_entry()->SetUserText(ASCIIToUTF16("foo")); 178 EXPECT_TRUE(location_bar->GetInputString().empty()); 179 EXPECT_EQ(ASCIIToUTF16("foo"), location_bar->location_entry()->GetText()); 180 181 location_bar->FocusSearch(); 182 EXPECT_TRUE(location_bar->GetInputString().empty()); 183 EXPECT_EQ(ASCIIToUTF16("?"), location_bar->location_entry()->GetText()); 184 185 size_t selection_start, selection_end; 186 location_bar->location_entry()->GetSelectionBounds(&selection_start, 187 &selection_end); 188 EXPECT_EQ(1U, selection_start); 189 EXPECT_EQ(1U, selection_end); 190 } 191 192 // Focus search when omnibox _is_ already in forced query mode, but no query 193 // has been typed. 194 { 195 location_bar->location_entry()->SetUserText(ASCIIToUTF16("?")); 196 EXPECT_TRUE(location_bar->GetInputString().empty()); 197 EXPECT_EQ(ASCIIToUTF16("?"), location_bar->location_entry()->GetText()); 198 199 location_bar->FocusSearch(); 200 EXPECT_TRUE(location_bar->GetInputString().empty()); 201 EXPECT_EQ(ASCIIToUTF16("?"), location_bar->location_entry()->GetText()); 202 203 size_t selection_start, selection_end; 204 location_bar->location_entry()->GetSelectionBounds(&selection_start, 205 &selection_end); 206 EXPECT_EQ(1U, selection_start); 207 EXPECT_EQ(1U, selection_end); 208 } 209 210 // Focus search when omnibox _is_ already in forced query mode, and some query 211 // has been typed. 212 { 213 location_bar->location_entry()->SetUserText(ASCIIToUTF16("?foo")); 214 EXPECT_TRUE(location_bar->GetInputString().empty()); 215 EXPECT_EQ(ASCIIToUTF16("?foo"), location_bar->location_entry()->GetText()); 216 217 location_bar->FocusSearch(); 218 EXPECT_TRUE(location_bar->GetInputString().empty()); 219 EXPECT_EQ(ASCIIToUTF16("?foo"), location_bar->location_entry()->GetText()); 220 221 size_t selection_start, selection_end; 222 location_bar->location_entry()->GetSelectionBounds(&selection_start, 223 &selection_end); 224 EXPECT_EQ(1U, std::min(selection_start, selection_end)); 225 EXPECT_EQ(4U, std::max(selection_start, selection_end)); 226 } 227 228 // Focus search when omnibox is in forced query mode with leading whitespace. 229 { 230 location_bar->location_entry()->SetUserText(ASCIIToUTF16(" ?foo")); 231 EXPECT_TRUE(location_bar->GetInputString().empty()); 232 EXPECT_EQ(ASCIIToUTF16(" ?foo"), 233 location_bar->location_entry()->GetText()); 234 235 location_bar->FocusSearch(); 236 EXPECT_TRUE(location_bar->GetInputString().empty()); 237 EXPECT_EQ(ASCIIToUTF16(" ?foo"), 238 location_bar->location_entry()->GetText()); 239 240 size_t selection_start, selection_end; 241 location_bar->location_entry()->GetSelectionBounds(&selection_start, 242 &selection_end); 243 EXPECT_EQ(4U, std::min(selection_start, selection_end)); 244 EXPECT_EQ(7U, std::max(selection_start, selection_end)); 245 } 246} 247