1 // Copyright (c) 2011 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/history/history.h" 14 #include "chrome/browser/tabs/tab_strip_model.h" 15 #include "chrome/browser/ui/browser.h" 16 #include "chrome/browser/ui/browser_window.h" 17 #include "chrome/browser/ui/omnibox/location_bar.h" 18 #include "chrome/common/url_constants.h" 19 #include "chrome/test/in_process_browser_test.h" 20 #include "chrome/test/ui_test_utils.h" 21 #include "content/browser/tab_contents/tab_contents.h" 22 #include "content/common/notification_type.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 34 namespace { 35 36 string16 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 50 class 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 62 IN_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 100 IN_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( 110 ASCIIToUTF16("chrome"), string16(), true, false, true, 111 AutocompleteInput::SYNCHRONOUS_MATCHES); 112 113 EXPECT_TRUE(autocomplete_controller->done()); 114 EXPECT_TRUE(location_bar->GetInputString().empty()); 115 EXPECT_TRUE(location_bar->location_entry()->GetText().empty()); 116 EXPECT_TRUE(location_bar->location_entry()->IsSelectAll()); 117 const AutocompleteResult& result = autocomplete_controller->result(); 118 // We get two matches because we have a provider for extension apps and the 119 // Chrome Web Store is a built-in Extension app. For this test, we only care 120 // about the other match existing. 121 ASSERT_GE(result.size(), 1U) << AutocompleteResultAsString(result); 122 AutocompleteMatch match = result.match_at(0); 123 EXPECT_EQ(AutocompleteMatch::SEARCH_WHAT_YOU_TYPED, match.type); 124 EXPECT_FALSE(match.deletable); 125 } 126 127 { 128 location_bar->Revert(); 129 130 EXPECT_TRUE(location_bar->GetInputString().empty()); 131 EXPECT_EQ(UTF8ToUTF16(chrome::kAboutBlankURL), 132 location_bar->location_entry()->GetText()); 133 EXPECT_FALSE(location_bar->location_entry()->IsSelectAll()); 134 const AutocompleteResult& result = autocomplete_controller->result(); 135 EXPECT_TRUE(result.empty()) << AutocompleteResultAsString(result); 136 } 137 } 138 139 IN_PROC_BROWSER_TEST_F(AutocompleteBrowserTest, TabAwayRevertSelect) { 140 // http://code.google.com/p/chromium/issues/detail?id=38385 141 // Make sure that tabbing away from an empty omnibar causes a revert 142 // and select all. 143 LocationBar* location_bar = GetLocationBar(); 144 EXPECT_EQ(UTF8ToUTF16(chrome::kAboutBlankURL), 145 location_bar->location_entry()->GetText()); 146 location_bar->location_entry()->SetUserText(string16()); 147 browser()->AddSelectedTabWithURL(GURL(chrome::kAboutBlankURL), 148 PageTransition::START_PAGE); 149 ui_test_utils::WaitForNavigation( 150 &browser()->GetSelectedTabContents()->controller()); 151 EXPECT_EQ(UTF8ToUTF16(chrome::kAboutBlankURL), 152 location_bar->location_entry()->GetText()); 153 browser()->CloseTab(); 154 EXPECT_EQ(UTF8ToUTF16(chrome::kAboutBlankURL), 155 location_bar->location_entry()->GetText()); 156 EXPECT_TRUE(location_bar->location_entry()->IsSelectAll()); 157 } 158 159 IN_PROC_BROWSER_TEST_F(AutocompleteBrowserTest, FocusSearch) { 160 LocationBar* location_bar = GetLocationBar(); 161 162 // Focus search when omnibox is blank 163 { 164 EXPECT_TRUE(location_bar->GetInputString().empty()); 165 EXPECT_EQ(UTF8ToUTF16(chrome::kAboutBlankURL), 166 location_bar->location_entry()->GetText()); 167 168 location_bar->FocusSearch(); 169 EXPECT_TRUE(location_bar->GetInputString().empty()); 170 EXPECT_EQ(ASCIIToUTF16("?"), location_bar->location_entry()->GetText()); 171 172 size_t selection_start, selection_end; 173 location_bar->location_entry()->GetSelectionBounds(&selection_start, 174 &selection_end); 175 EXPECT_EQ(1U, selection_start); 176 EXPECT_EQ(1U, selection_end); 177 } 178 179 // Focus search when omnibox is _not_ alread in forced query mode. 180 { 181 location_bar->location_entry()->SetUserText(ASCIIToUTF16("foo")); 182 EXPECT_TRUE(location_bar->GetInputString().empty()); 183 EXPECT_EQ(ASCIIToUTF16("foo"), location_bar->location_entry()->GetText()); 184 185 location_bar->FocusSearch(); 186 EXPECT_TRUE(location_bar->GetInputString().empty()); 187 EXPECT_EQ(ASCIIToUTF16("?"), location_bar->location_entry()->GetText()); 188 189 size_t selection_start, selection_end; 190 location_bar->location_entry()->GetSelectionBounds(&selection_start, 191 &selection_end); 192 EXPECT_EQ(1U, selection_start); 193 EXPECT_EQ(1U, selection_end); 194 } 195 196 // Focus search when omnibox _is_ already in forced query mode, but no query 197 // has been typed. 198 { 199 location_bar->location_entry()->SetUserText(ASCIIToUTF16("?")); 200 EXPECT_TRUE(location_bar->GetInputString().empty()); 201 EXPECT_EQ(ASCIIToUTF16("?"), location_bar->location_entry()->GetText()); 202 203 location_bar->FocusSearch(); 204 EXPECT_TRUE(location_bar->GetInputString().empty()); 205 EXPECT_EQ(ASCIIToUTF16("?"), location_bar->location_entry()->GetText()); 206 207 size_t selection_start, selection_end; 208 location_bar->location_entry()->GetSelectionBounds(&selection_start, 209 &selection_end); 210 EXPECT_EQ(1U, selection_start); 211 EXPECT_EQ(1U, selection_end); 212 } 213 214 // Focus search when omnibox _is_ already in forced query mode, and some query 215 // has been typed. 216 { 217 location_bar->location_entry()->SetUserText(ASCIIToUTF16("?foo")); 218 EXPECT_TRUE(location_bar->GetInputString().empty()); 219 EXPECT_EQ(ASCIIToUTF16("?foo"), location_bar->location_entry()->GetText()); 220 221 location_bar->FocusSearch(); 222 EXPECT_TRUE(location_bar->GetInputString().empty()); 223 EXPECT_EQ(ASCIIToUTF16("?foo"), location_bar->location_entry()->GetText()); 224 225 size_t selection_start, selection_end; 226 location_bar->location_entry()->GetSelectionBounds(&selection_start, 227 &selection_end); 228 EXPECT_EQ(1U, std::min(selection_start, selection_end)); 229 EXPECT_EQ(4U, std::max(selection_start, selection_end)); 230 } 231 232 // Focus search when omnibox is in forced query mode with leading whitespace. 233 { 234 location_bar->location_entry()->SetUserText(ASCIIToUTF16(" ?foo")); 235 EXPECT_TRUE(location_bar->GetInputString().empty()); 236 EXPECT_EQ(ASCIIToUTF16(" ?foo"), 237 location_bar->location_entry()->GetText()); 238 239 location_bar->FocusSearch(); 240 EXPECT_TRUE(location_bar->GetInputString().empty()); 241 EXPECT_EQ(ASCIIToUTF16(" ?foo"), 242 location_bar->location_entry()->GetText()); 243 244 size_t selection_start, selection_end; 245 location_bar->location_entry()->GetSelectionBounds(&selection_start, 246 &selection_end); 247 EXPECT_EQ(4U, std::min(selection_start, selection_end)); 248 EXPECT_EQ(7U, std::max(selection_start, selection_end)); 249 } 250 } 251