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
3 // found in the LICENSE file.
5 #include "base/command_line.h"
6 #include "base/format_macros.h"
7 #include "base/strings/stringprintf.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/autocomplete/chrome_autocomplete_scheme_classifier.h"
10 #include "chrome/browser/extensions/extension_browsertest.h"
11 #include "chrome/browser/extensions/extension_service.h"
12 #include "chrome/browser/extensions/unpacked_installer.h"
13 #include "chrome/browser/history/history_service.h"
14 #include "chrome/browser/history/history_service_factory.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/search_engines/template_url_service_factory.h"
17 #include "chrome/browser/ui/browser.h"
18 #include "chrome/browser/ui/browser_commands.h"
19 #include "chrome/browser/ui/browser_tabstrip.h"
20 #include "chrome/browser/ui/browser_window.h"
21 #include "chrome/browser/ui/location_bar/location_bar.h"
22 #include "chrome/browser/ui/omnibox/omnibox_popup_model.h"
23 #include "chrome/browser/ui/omnibox/omnibox_view.h"
24 #include "chrome/browser/ui/tabs/tab_strip_model.h"
25 #include "chrome/common/chrome_paths.h"
26 #include "chrome/common/url_constants.h"
27 #include "chrome/test/base/in_process_browser_test.h"
28 #include "chrome/test/base/test_switches.h"
29 #include "chrome/test/base/ui_test_utils.h"
30 #include "components/metrics/proto/omnibox_event.pb.h"
31 #include "components/omnibox/autocomplete_input.h"
32 #include "components/omnibox/autocomplete_match.h"
33 #include "components/omnibox/autocomplete_provider.h"
34 #include "content/public/browser/notification_service.h"
35 #include "content/public/browser/notification_types.h"
36 #include "testing/gtest/include/gtest/gtest.h"
40 base::string16 AutocompleteResultAsString(const AutocompleteResult& result) {
41 std::string output(base::StringPrintf("{%" PRIuS "} ", result.size()));
42 for (size_t i = 0; i < result.size(); ++i) {
43 AutocompleteMatch match = result.match_at(i);
44 output.append(base::StringPrintf("[\"%s\" by \"%s\"] ",
45 base::UTF16ToUTF8(match.contents).c_str(),
46 match.provider->GetName()));
48 return base::UTF8ToUTF16(output);
53 class AutocompleteBrowserTest : public ExtensionBrowserTest {
55 void WaitForTemplateURLServiceToLoad() {
56 ui_test_utils::WaitForTemplateURLServiceToLoad(
57 TemplateURLServiceFactory::GetForProfile(browser()->profile()));
60 LocationBar* GetLocationBar() const {
61 return browser()->window()->GetLocationBar();
64 AutocompleteController* GetAutocompleteController() const {
65 return GetLocationBar()->GetOmniboxView()->model()->popup_model()->
66 autocomplete_controller();
70 IN_PROC_BROWSER_TEST_F(AutocompleteBrowserTest, Basic) {
71 #if defined(OS_WIN) && defined(USE_ASH)
72 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
73 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
77 WaitForTemplateURLServiceToLoad();
78 LocationBar* location_bar = GetLocationBar();
79 OmniboxView* omnibox_view = location_bar->GetOmniboxView();
81 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid());
82 EXPECT_EQ(base::UTF8ToUTF16(url::kAboutBlankURL), omnibox_view->GetText());
83 // TODO(phajdan.jr): check state of IsSelectAll when it's consistent across
86 location_bar->FocusLocation(true);
88 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid());
89 EXPECT_EQ(base::UTF8ToUTF16(url::kAboutBlankURL), omnibox_view->GetText());
90 EXPECT_TRUE(omnibox_view->IsSelectAll());
92 omnibox_view->SetUserText(base::ASCIIToUTF16("chrome"));
94 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid());
95 EXPECT_EQ(base::ASCIIToUTF16("chrome"), omnibox_view->GetText());
96 EXPECT_FALSE(omnibox_view->IsSelectAll());
98 omnibox_view->RevertAll();
100 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid());
101 EXPECT_EQ(base::UTF8ToUTF16(url::kAboutBlankURL), omnibox_view->GetText());
102 EXPECT_FALSE(omnibox_view->IsSelectAll());
104 omnibox_view->SetUserText(base::ASCIIToUTF16("chrome"));
105 location_bar->Revert();
107 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid());
108 EXPECT_EQ(base::UTF8ToUTF16(url::kAboutBlankURL), omnibox_view->GetText());
109 EXPECT_FALSE(omnibox_view->IsSelectAll());
112 // Autocomplete test is flaky on ChromeOS.
113 // http://crbug.com/52928
114 #if defined(OS_CHROMEOS)
115 #define MAYBE_Autocomplete DISABLED_Autocomplete
117 #define MAYBE_Autocomplete Autocomplete
120 IN_PROC_BROWSER_TEST_F(AutocompleteBrowserTest, MAYBE_Autocomplete) {
121 #if defined(OS_WIN) && defined(USE_ASH)
122 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
123 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
127 WaitForTemplateURLServiceToLoad();
128 // The results depend on the history backend being loaded. Make sure it is
129 // loaded so that the autocomplete results are consistent.
130 ui_test_utils::WaitForHistoryToLoad(
131 HistoryServiceFactory::GetForProfile(browser()->profile(),
132 Profile::EXPLICIT_ACCESS));
134 LocationBar* location_bar = GetLocationBar();
135 OmniboxView* omnibox_view = location_bar->GetOmniboxView();
136 AutocompleteController* autocomplete_controller = GetAutocompleteController();
139 omnibox_view->model()->SetInputInProgress(true);
140 autocomplete_controller->Start(AutocompleteInput(
141 base::ASCIIToUTF16("chrome"), base::string16::npos, std::string(),
142 GURL(), metrics::OmniboxEventProto::NTP, true, false, true, false,
143 ChromeAutocompleteSchemeClassifier(browser()->profile())));
145 EXPECT_TRUE(autocomplete_controller->done());
146 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid());
147 EXPECT_TRUE(omnibox_view->GetText().empty());
148 EXPECT_TRUE(omnibox_view->IsSelectAll());
149 const AutocompleteResult& result = autocomplete_controller->result();
150 ASSERT_GE(result.size(), 1U) << AutocompleteResultAsString(result);
151 AutocompleteMatch match = result.match_at(0);
152 EXPECT_EQ(AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED, match.type);
153 EXPECT_FALSE(match.deletable);
157 location_bar->Revert();
158 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid());
159 EXPECT_EQ(base::UTF8ToUTF16(url::kAboutBlankURL), omnibox_view->GetText());
160 EXPECT_FALSE(omnibox_view->IsSelectAll());
161 const AutocompleteResult& result = autocomplete_controller->result();
162 EXPECT_TRUE(result.empty()) << AutocompleteResultAsString(result);
166 IN_PROC_BROWSER_TEST_F(AutocompleteBrowserTest, TabAwayRevertSelect) {
167 #if defined(OS_WIN) && defined(USE_ASH)
168 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
169 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
173 WaitForTemplateURLServiceToLoad();
174 // http://code.google.com/p/chromium/issues/detail?id=38385
175 // Make sure that tabbing away from an empty omnibox causes a revert
177 LocationBar* location_bar = GetLocationBar();
178 OmniboxView* omnibox_view = location_bar->GetOmniboxView();
179 EXPECT_EQ(base::UTF8ToUTF16(url::kAboutBlankURL), omnibox_view->GetText());
180 omnibox_view->SetUserText(base::string16());
181 content::WindowedNotificationObserver observer(
182 content::NOTIFICATION_LOAD_STOP,
183 content::NotificationService::AllSources());
184 chrome::AddSelectedTabWithURL(browser(),
185 GURL(url::kAboutBlankURL),
186 ui::PAGE_TRANSITION_AUTO_TOPLEVEL);
188 EXPECT_EQ(base::UTF8ToUTF16(url::kAboutBlankURL), omnibox_view->GetText());
189 chrome::CloseTab(browser());
190 EXPECT_EQ(base::UTF8ToUTF16(url::kAboutBlankURL), omnibox_view->GetText());
191 EXPECT_TRUE(omnibox_view->IsSelectAll());
194 IN_PROC_BROWSER_TEST_F(AutocompleteBrowserTest, FocusSearch) {
195 #if defined(OS_WIN) && defined(USE_ASH)
196 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
197 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
201 WaitForTemplateURLServiceToLoad();
202 LocationBar* location_bar = GetLocationBar();
203 OmniboxView* omnibox_view = location_bar->GetOmniboxView();
205 // Focus search when omnibox is blank
207 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid());
208 EXPECT_EQ(base::UTF8ToUTF16(url::kAboutBlankURL), omnibox_view->GetText());
210 location_bar->FocusSearch();
211 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid());
212 EXPECT_EQ(base::ASCIIToUTF16("?"), omnibox_view->GetText());
214 size_t selection_start, selection_end;
215 omnibox_view->GetSelectionBounds(&selection_start, &selection_end);
216 EXPECT_EQ(1U, selection_start);
217 EXPECT_EQ(1U, selection_end);
220 // Focus search when omnibox is _not_ alread in forced query mode.
222 omnibox_view->SetUserText(base::ASCIIToUTF16("foo"));
223 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid());
224 EXPECT_EQ(base::ASCIIToUTF16("foo"), omnibox_view->GetText());
226 location_bar->FocusSearch();
227 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid());
228 EXPECT_EQ(base::ASCIIToUTF16("?"), omnibox_view->GetText());
230 size_t selection_start, selection_end;
231 omnibox_view->GetSelectionBounds(&selection_start, &selection_end);
232 EXPECT_EQ(1U, selection_start);
233 EXPECT_EQ(1U, selection_end);
236 // Focus search when omnibox _is_ already in forced query mode, but no query
239 omnibox_view->SetUserText(base::ASCIIToUTF16("?"));
240 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid());
241 EXPECT_EQ(base::ASCIIToUTF16("?"), omnibox_view->GetText());
243 location_bar->FocusSearch();
244 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid());
245 EXPECT_EQ(base::ASCIIToUTF16("?"), omnibox_view->GetText());
247 size_t selection_start, selection_end;
248 omnibox_view->GetSelectionBounds(&selection_start, &selection_end);
249 EXPECT_EQ(1U, selection_start);
250 EXPECT_EQ(1U, selection_end);
253 // Focus search when omnibox _is_ already in forced query mode, and some query
256 omnibox_view->SetUserText(base::ASCIIToUTF16("?foo"));
257 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid());
258 EXPECT_EQ(base::ASCIIToUTF16("?foo"), omnibox_view->GetText());
260 location_bar->FocusSearch();
261 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid());
262 EXPECT_EQ(base::ASCIIToUTF16("?foo"), omnibox_view->GetText());
264 size_t selection_start, selection_end;
265 omnibox_view->GetSelectionBounds(&selection_start, &selection_end);
266 EXPECT_EQ(1U, std::min(selection_start, selection_end));
267 EXPECT_EQ(4U, std::max(selection_start, selection_end));
270 // Focus search when omnibox is in forced query mode with leading whitespace.
272 omnibox_view->SetUserText(base::ASCIIToUTF16(" ?foo"));
273 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid());
274 EXPECT_EQ(base::ASCIIToUTF16(" ?foo"), omnibox_view->GetText());
276 location_bar->FocusSearch();
277 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid());
278 EXPECT_EQ(base::ASCIIToUTF16(" ?foo"), omnibox_view->GetText());
280 size_t selection_start, selection_end;
281 omnibox_view->GetSelectionBounds(&selection_start, &selection_end);
282 EXPECT_EQ(4U, std::min(selection_start, selection_end));
283 EXPECT_EQ(7U, std::max(selection_start, selection_end));