| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <stddef.h> | 5 #include <stddef.h> |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/format_macros.h" | 8 #include "base/format_macros.h" |
| 9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| 11 #include "build/build_config.h" | 11 #include "build/build_config.h" |
| 12 #include "chrome/browser/autocomplete/chrome_autocomplete_scheme_classifier.h" | 12 #include "chrome/browser/autocomplete/chrome_autocomplete_scheme_classifier.h" |
| 13 #include "chrome/browser/extensions/extension_browsertest.h" | 13 #include "chrome/browser/extensions/extension_browsertest.h" |
| 14 #include "chrome/browser/extensions/extension_service.h" | 14 #include "chrome/browser/extensions/extension_service.h" |
| 15 #include "chrome/browser/extensions/unpacked_installer.h" | 15 #include "chrome/browser/extensions/unpacked_installer.h" |
| 16 #include "chrome/browser/history/history_service_factory.h" | 16 #include "chrome/browser/history/history_service_factory.h" |
| 17 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
| 18 #include "chrome/browser/search_engines/template_url_service_factory.h" | 18 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 19 #include "chrome/browser/ui/browser.h" | 19 #include "chrome/browser/ui/browser.h" |
| 20 #include "chrome/browser/ui/browser_commands.h" | 20 #include "chrome/browser/ui/browser_commands.h" |
| 21 #include "chrome/browser/ui/browser_tabstrip.h" | 21 #include "chrome/browser/ui/browser_tabstrip.h" |
| 22 #include "chrome/browser/ui/browser_window.h" | 22 #include "chrome/browser/ui/browser_window.h" |
| 23 #include "chrome/browser/ui/location_bar/location_bar.h" | 23 #include "chrome/browser/ui/location_bar/location_bar.h" |
| 24 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 24 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 25 #include "chrome/common/chrome_paths.h" | 25 #include "chrome/common/chrome_paths.h" |
| 26 #include "chrome/common/url_constants.h" | 26 #include "chrome/common/url_constants.h" |
| 27 #include "chrome/test/base/in_process_browser_test.h" | 27 #include "chrome/test/base/in_process_browser_test.h" |
| 28 #include "chrome/test/base/interactive_test_utils.h" |
| 28 #include "chrome/test/base/search_test_utils.h" | 29 #include "chrome/test/base/search_test_utils.h" |
| 29 #include "chrome/test/base/ui_test_utils.h" | 30 #include "chrome/test/base/ui_test_utils.h" |
| 30 #include "components/history/core/browser/history_service.h" | 31 #include "components/history/core/browser/history_service.h" |
| 31 #include "components/metrics/proto/omnibox_event.pb.h" | 32 #include "components/metrics/proto/omnibox_event.pb.h" |
| 32 #include "components/omnibox/browser/autocomplete_input.h" | 33 #include "components/omnibox/browser/autocomplete_input.h" |
| 33 #include "components/omnibox/browser/autocomplete_match.h" | 34 #include "components/omnibox/browser/autocomplete_match.h" |
| 34 #include "components/omnibox/browser/autocomplete_provider.h" | 35 #include "components/omnibox/browser/autocomplete_provider.h" |
| 35 #include "components/omnibox/browser/omnibox_popup_model.h" | 36 #include "components/omnibox/browser/omnibox_popup_model.h" |
| 36 #include "components/omnibox/browser/omnibox_view.h" | 37 #include "components/omnibox/browser/omnibox_view.h" |
| 38 #include "components/search_engines/template_url_service.h" |
| 37 #include "content/public/browser/notification_service.h" | 39 #include "content/public/browser/notification_service.h" |
| 38 #include "content/public/browser/notification_types.h" | 40 #include "content/public/browser/notification_types.h" |
| 39 #include "testing/gtest/include/gtest/gtest.h" | 41 #include "testing/gtest/include/gtest/gtest.h" |
| 40 | 42 |
| 41 namespace { | 43 namespace { |
| 42 | 44 |
| 43 base::string16 AutocompleteResultAsString(const AutocompleteResult& result) { | 45 base::string16 AutocompleteResultAsString(const AutocompleteResult& result) { |
| 44 std::string output(base::StringPrintf("{%" PRIuS "} ", result.size())); | 46 std::string output(base::StringPrintf("{%" PRIuS "} ", result.size())); |
| 45 for (size_t i = 0; i < result.size(); ++i) { | 47 for (size_t i = 0; i < result.size(); ++i) { |
| 46 AutocompleteMatch match = result.match_at(i); | 48 AutocompleteMatch match = result.match_at(i); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 61 } | 63 } |
| 62 | 64 |
| 63 LocationBar* GetLocationBar() const { | 65 LocationBar* GetLocationBar() const { |
| 64 return browser()->window()->GetLocationBar(); | 66 return browser()->window()->GetLocationBar(); |
| 65 } | 67 } |
| 66 | 68 |
| 67 AutocompleteController* GetAutocompleteController() const { | 69 AutocompleteController* GetAutocompleteController() const { |
| 68 return GetLocationBar()->GetOmniboxView()->model()->popup_model()-> | 70 return GetLocationBar()->GetOmniboxView()->model()->popup_model()-> |
| 69 autocomplete_controller(); | 71 autocomplete_controller(); |
| 70 } | 72 } |
| 73 |
| 74 void FocusSearchCheckPreconditions() const { |
| 75 LocationBar* location_bar = GetLocationBar(); |
| 76 OmniboxView* omnibox_view = location_bar->GetOmniboxView(); |
| 77 OmniboxEditModel* omnibox_model = omnibox_view->model(); |
| 78 |
| 79 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid()); |
| 80 EXPECT_EQ(base::UTF8ToUTF16(url::kAboutBlankURL), |
| 81 omnibox_view->GetText()); |
| 82 EXPECT_EQ(base::string16(), omnibox_model->keyword()); |
| 83 EXPECT_FALSE(omnibox_model->is_keyword_hint()); |
| 84 EXPECT_FALSE(omnibox_model->is_keyword_selected()); |
| 85 } |
| 71 }; | 86 }; |
| 72 | 87 |
| 73 IN_PROC_BROWSER_TEST_F(AutocompleteBrowserTest, Basic) { | 88 IN_PROC_BROWSER_TEST_F(AutocompleteBrowserTest, Basic) { |
| 74 WaitForTemplateURLServiceToLoad(); | 89 WaitForTemplateURLServiceToLoad(); |
| 75 LocationBar* location_bar = GetLocationBar(); | 90 LocationBar* location_bar = GetLocationBar(); |
| 76 OmniboxView* omnibox_view = location_bar->GetOmniboxView(); | 91 OmniboxView* omnibox_view = location_bar->GetOmniboxView(); |
| 77 | 92 |
| 78 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid()); | 93 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid()); |
| 79 EXPECT_EQ(base::UTF8ToUTF16(url::kAboutBlankURL), omnibox_view->GetText()); | 94 EXPECT_EQ(base::UTF8ToUTF16(url::kAboutBlankURL), omnibox_view->GetText()); |
| 80 // TODO(phajdan.jr): check state of IsSelectAll when it's consistent across | 95 // TODO(phajdan.jr): check state of IsSelectAll when it's consistent across |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 172 EXPECT_EQ(base::UTF8ToUTF16(url::kAboutBlankURL), omnibox_view->GetText()); | 187 EXPECT_EQ(base::UTF8ToUTF16(url::kAboutBlankURL), omnibox_view->GetText()); |
| 173 chrome::CloseTab(browser()); | 188 chrome::CloseTab(browser()); |
| 174 EXPECT_EQ(base::UTF8ToUTF16(url::kAboutBlankURL), omnibox_view->GetText()); | 189 EXPECT_EQ(base::UTF8ToUTF16(url::kAboutBlankURL), omnibox_view->GetText()); |
| 175 EXPECT_TRUE(omnibox_view->IsSelectAll()); | 190 EXPECT_TRUE(omnibox_view->IsSelectAll()); |
| 176 } | 191 } |
| 177 | 192 |
| 178 IN_PROC_BROWSER_TEST_F(AutocompleteBrowserTest, FocusSearch) { | 193 IN_PROC_BROWSER_TEST_F(AutocompleteBrowserTest, FocusSearch) { |
| 179 WaitForTemplateURLServiceToLoad(); | 194 WaitForTemplateURLServiceToLoad(); |
| 180 LocationBar* location_bar = GetLocationBar(); | 195 LocationBar* location_bar = GetLocationBar(); |
| 181 OmniboxView* omnibox_view = location_bar->GetOmniboxView(); | 196 OmniboxView* omnibox_view = location_bar->GetOmniboxView(); |
| 197 OmniboxEditModel* omnibox_model = omnibox_view->model(); |
| 182 | 198 |
| 183 // Focus search when omnibox is blank | 199 TemplateURLService* template_url_service = |
| 200 TemplateURLServiceFactory::GetForProfile(browser()->profile()); |
| 201 base::string16 default_search_keyword = |
| 202 template_url_service->GetDefaultSearchProvider()->keyword(); |
| 203 |
| 204 base::string16 query_text = base::ASCIIToUTF16("foo"); |
| 205 |
| 206 size_t selection_start, selection_end; |
| 207 |
| 208 // Focus search when omnibox is blank. |
| 184 { | 209 { |
| 185 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid()); | 210 FocusSearchCheckPreconditions(); |
| 186 EXPECT_EQ(base::UTF8ToUTF16(url::kAboutBlankURL), omnibox_view->GetText()); | |
| 187 | 211 |
| 188 location_bar->FocusSearch(); | 212 location_bar->FocusSearch(); |
| 189 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid()); | 213 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid()); |
| 190 EXPECT_EQ(base::ASCIIToUTF16("?"), omnibox_view->GetText()); | 214 EXPECT_EQ(base::string16(), omnibox_view->GetText()); |
| 215 EXPECT_EQ(default_search_keyword, omnibox_model->keyword()); |
| 216 EXPECT_FALSE(omnibox_model->is_keyword_hint()); |
| 217 EXPECT_TRUE(omnibox_model->is_keyword_selected()); |
| 191 | 218 |
| 192 size_t selection_start, selection_end; | |
| 193 omnibox_view->GetSelectionBounds(&selection_start, &selection_end); | 219 omnibox_view->GetSelectionBounds(&selection_start, &selection_end); |
| 194 EXPECT_EQ(1U, selection_start); | 220 EXPECT_EQ(0U, selection_start); |
| 195 EXPECT_EQ(1U, selection_end); | 221 EXPECT_EQ(0U, selection_end); |
| 222 |
| 223 omnibox_view->RevertAll(); |
| 196 } | 224 } |
| 197 | 225 |
| 198 // Focus search when omnibox is _not_ alread in forced query mode. | 226 // Focus search when omnibox is _not_ already in keyword mode. |
| 199 { | 227 { |
| 200 omnibox_view->SetUserText(base::ASCIIToUTF16("foo")); | 228 FocusSearchCheckPreconditions(); |
| 229 |
| 230 omnibox_view->SetUserText(query_text); |
| 201 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid()); | 231 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid()); |
| 202 EXPECT_EQ(base::ASCIIToUTF16("foo"), omnibox_view->GetText()); | 232 EXPECT_EQ(query_text, omnibox_view->GetText()); |
| 233 EXPECT_EQ(base::string16(), omnibox_model->keyword()); |
| 234 EXPECT_FALSE(omnibox_model->is_keyword_hint()); |
| 235 EXPECT_FALSE(omnibox_model->is_keyword_selected()); |
| 203 | 236 |
| 204 location_bar->FocusSearch(); | 237 location_bar->FocusSearch(); |
| 205 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid()); | 238 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid()); |
| 206 EXPECT_EQ(base::ASCIIToUTF16("?"), omnibox_view->GetText()); | 239 EXPECT_EQ(query_text, omnibox_view->GetText()); |
| 240 EXPECT_EQ(default_search_keyword, omnibox_model->keyword()); |
| 241 EXPECT_FALSE(omnibox_model->is_keyword_hint()); |
| 242 EXPECT_TRUE(omnibox_model->is_keyword_selected()); |
| 207 | 243 |
| 208 size_t selection_start, selection_end; | |
| 209 omnibox_view->GetSelectionBounds(&selection_start, &selection_end); | 244 omnibox_view->GetSelectionBounds(&selection_start, &selection_end); |
| 210 EXPECT_EQ(1U, selection_start); | 245 EXPECT_EQ(0U, std::min(selection_start, selection_end)); |
| 211 EXPECT_EQ(1U, selection_end); | 246 EXPECT_EQ(query_text.length(), std::max(selection_start, selection_end)); |
| 247 |
| 248 omnibox_view->RevertAll(); |
| 212 } | 249 } |
| 213 | 250 |
| 214 // Focus search when omnibox _is_ already in forced query mode, but no query | 251 // Focus search when omnibox _is_ already in keyword mode, but no query |
| 215 // has been typed. | 252 // has been typed. |
| 216 { | 253 { |
| 217 omnibox_view->SetUserText(base::ASCIIToUTF16("?")); | 254 FocusSearchCheckPreconditions(); |
| 218 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid()); | |
| 219 EXPECT_EQ(base::ASCIIToUTF16("?"), omnibox_view->GetText()); | |
| 220 | 255 |
| 221 location_bar->FocusSearch(); | 256 location_bar->FocusSearch(); |
| 222 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid()); | 257 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid()); |
| 223 EXPECT_EQ(base::ASCIIToUTF16("?"), omnibox_view->GetText()); | 258 EXPECT_EQ(base::string16(), omnibox_view->GetText()); |
| 259 EXPECT_EQ(default_search_keyword, omnibox_model->keyword()); |
| 260 EXPECT_FALSE(omnibox_model->is_keyword_hint()); |
| 261 EXPECT_TRUE(omnibox_model->is_keyword_selected()); |
| 224 | 262 |
| 225 size_t selection_start, selection_end; | |
| 226 omnibox_view->GetSelectionBounds(&selection_start, &selection_end); | 263 omnibox_view->GetSelectionBounds(&selection_start, &selection_end); |
| 227 EXPECT_EQ(1U, selection_start); | 264 EXPECT_EQ(0U, selection_start); |
| 228 EXPECT_EQ(1U, selection_end); | 265 EXPECT_EQ(0U, selection_end); |
| 229 } | |
| 230 | |
| 231 // Focus search when omnibox _is_ already in forced query mode, and some query | |
| 232 // has been typed. | |
| 233 { | |
| 234 omnibox_view->SetUserText(base::ASCIIToUTF16("?foo")); | |
| 235 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid()); | |
| 236 EXPECT_EQ(base::ASCIIToUTF16("?foo"), omnibox_view->GetText()); | |
| 237 | 266 |
| 238 location_bar->FocusSearch(); | 267 location_bar->FocusSearch(); |
| 239 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid()); | 268 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid()); |
| 240 EXPECT_EQ(base::ASCIIToUTF16("?foo"), omnibox_view->GetText()); | 269 EXPECT_EQ(base::string16(), omnibox_view->GetText()); |
| 270 EXPECT_EQ(default_search_keyword, omnibox_model->keyword()); |
| 271 EXPECT_FALSE(omnibox_model->is_keyword_hint()); |
| 272 EXPECT_TRUE(omnibox_model->is_keyword_selected()); |
| 241 | 273 |
| 242 size_t selection_start, selection_end; | |
| 243 omnibox_view->GetSelectionBounds(&selection_start, &selection_end); | 274 omnibox_view->GetSelectionBounds(&selection_start, &selection_end); |
| 244 EXPECT_EQ(1U, std::min(selection_start, selection_end)); | 275 EXPECT_EQ(0U, selection_start); |
| 245 EXPECT_EQ(4U, std::max(selection_start, selection_end)); | 276 EXPECT_EQ(0U, selection_end); |
| 277 |
| 278 omnibox_view->RevertAll(); |
| 246 } | 279 } |
| 247 | 280 |
| 248 // Focus search when omnibox is in forced query mode with leading whitespace. | 281 // Focus search when omnibox _is_ already in keyword mode, and some query |
| 282 // has been typed. |
| 249 { | 283 { |
| 250 omnibox_view->SetUserText(base::ASCIIToUTF16(" ?foo")); | 284 FocusSearchCheckPreconditions(); |
| 285 |
| 286 omnibox_view->SetUserText(query_text); |
| 287 location_bar->FocusSearch(); |
| 251 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid()); | 288 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid()); |
| 252 EXPECT_EQ(base::ASCIIToUTF16(" ?foo"), omnibox_view->GetText()); | 289 EXPECT_EQ(query_text, omnibox_view->GetText()); |
| 290 EXPECT_EQ(default_search_keyword, omnibox_model->keyword()); |
| 291 EXPECT_FALSE(omnibox_model->is_keyword_hint()); |
| 292 EXPECT_TRUE(omnibox_model->is_keyword_selected()); |
| 293 |
| 294 omnibox_view->GetSelectionBounds(&selection_start, &selection_end); |
| 295 EXPECT_EQ(0U, std::min(selection_start, selection_end)); |
| 296 EXPECT_EQ(query_text.length(), std::max(selection_start, selection_end)); |
| 253 | 297 |
| 254 location_bar->FocusSearch(); | 298 location_bar->FocusSearch(); |
| 255 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid()); | 299 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid()); |
| 256 EXPECT_EQ(base::ASCIIToUTF16(" ?foo"), omnibox_view->GetText()); | 300 EXPECT_EQ(query_text, omnibox_view->GetText()); |
| 301 EXPECT_EQ(default_search_keyword, omnibox_model->keyword()); |
| 302 EXPECT_FALSE(omnibox_model->is_keyword_hint()); |
| 303 EXPECT_TRUE(omnibox_model->is_keyword_selected()); |
| 257 | 304 |
| 258 size_t selection_start, selection_end; | |
| 259 omnibox_view->GetSelectionBounds(&selection_start, &selection_end); | 305 omnibox_view->GetSelectionBounds(&selection_start, &selection_end); |
| 260 EXPECT_EQ(4U, std::min(selection_start, selection_end)); | 306 EXPECT_EQ(0U, std::min(selection_start, selection_end)); |
| 261 EXPECT_EQ(7U, std::max(selection_start, selection_end)); | 307 EXPECT_EQ(query_text.length(), std::max(selection_start, selection_end)); |
| 308 |
| 309 omnibox_view->RevertAll(); |
| 310 } |
| 311 |
| 312 // If the user gets into keyword mode using a keyboard shortcut, and presses |
| 313 // backspace, they should be left with their original query without their dsp |
| 314 // keyword. |
| 315 { |
| 316 FocusSearchCheckPreconditions(); |
| 317 |
| 318 omnibox_view->SetUserText(query_text); |
| 319 // The user presses Ctrl-K. |
| 320 location_bar->FocusSearch(); |
| 321 // The user presses backspace. |
| 322 omnibox_model->ClearKeyword(); |
| 323 |
| 324 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid()); |
| 325 EXPECT_EQ(query_text, omnibox_view->GetText()); |
| 326 EXPECT_EQ(base::string16(), omnibox_model->keyword()); |
| 327 EXPECT_FALSE(omnibox_model->is_keyword_hint()); |
| 328 EXPECT_FALSE(omnibox_model->is_keyword_selected()); |
| 329 |
| 330 omnibox_view->RevertAll(); |
| 262 } | 331 } |
| 263 } | 332 } |
| OLD | NEW |