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/path_service.h"
8 #include "base/strings/stringprintf.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/autocomplete/autocomplete_input.h"
11 #include "chrome/browser/autocomplete/autocomplete_match.h"
12 #include "chrome/browser/autocomplete/autocomplete_provider.h"
13 #include "chrome/browser/extensions/extension_browsertest.h"
14 #include "chrome/browser/extensions/extension_service.h"
15 #include "chrome/browser/extensions/unpacked_installer.h"
16 #include "chrome/browser/history/history_service.h"
17 #include "chrome/browser/history/history_service_factory.h"
18 #include "chrome/browser/profiles/profile.h"
19 #include "chrome/browser/search_engines/template_url_service_factory.h"
20 #include "chrome/browser/ui/browser.h"
21 #include "chrome/browser/ui/browser_commands.h"
22 #include "chrome/browser/ui/browser_tabstrip.h"
23 #include "chrome/browser/ui/browser_window.h"
24 #include "chrome/browser/ui/omnibox/location_bar.h"
25 #include "chrome/browser/ui/omnibox/omnibox_popup_model.h"
26 #include "chrome/browser/ui/omnibox/omnibox_view.h"
27 #include "chrome/browser/ui/tabs/tab_strip_model.h"
28 #include "chrome/common/chrome_paths.h"
29 #include "chrome/common/url_constants.h"
30 #include "chrome/test/base/in_process_browser_test.h"
31 #include "chrome/test/base/test_switches.h"
32 #include "chrome/test/base/ui_test_utils.h"
33 #include "content/public/browser/notification_service.h"
34 #include "content/public/browser/notification_types.h"
35 #include "testing/gtest/include/gtest/gtest.h"
39 base::string16 AutocompleteResultAsString(const AutocompleteResult& result) {
40 std::string output(base::StringPrintf("{%" PRIuS "} ", result.size()));
41 for (size_t i = 0; i < result.size(); ++i) {
42 AutocompleteMatch match = result.match_at(i);
43 output.append(base::StringPrintf("[\"%s\" by \"%s\"] ",
44 base::UTF16ToUTF8(match.contents).c_str(),
45 match.provider->GetName()));
47 return base::UTF8ToUTF16(output);
52 class AutocompleteBrowserTest : public ExtensionBrowserTest {
54 void WaitForTemplateURLServiceToLoad() {
55 ui_test_utils::WaitForTemplateURLServiceToLoad(
56 TemplateURLServiceFactory::GetForProfile(browser()->profile()));
59 LocationBar* GetLocationBar() const {
60 return browser()->window()->GetLocationBar();
63 AutocompleteController* GetAutocompleteController() const {
64 return GetLocationBar()->GetOmniboxView()->model()->popup_model()->
65 autocomplete_controller();
69 IN_PROC_BROWSER_TEST_F(AutocompleteBrowserTest, Basic) {
70 #if defined(OS_WIN) && defined(USE_ASH)
71 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
72 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
76 WaitForTemplateURLServiceToLoad();
77 LocationBar* location_bar = GetLocationBar();
78 OmniboxView* omnibox_view = location_bar->GetOmniboxView();
80 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid());
81 EXPECT_EQ(base::UTF8ToUTF16(content::kAboutBlankURL),
82 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(content::kAboutBlankURL),
90 omnibox_view->GetText());
91 EXPECT_TRUE(omnibox_view->IsSelectAll());
93 omnibox_view->SetUserText(base::ASCIIToUTF16("chrome"));
95 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid());
96 EXPECT_EQ(base::ASCIIToUTF16("chrome"), omnibox_view->GetText());
97 EXPECT_FALSE(omnibox_view->IsSelectAll());
99 omnibox_view->RevertAll();
101 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid());
102 EXPECT_EQ(base::UTF8ToUTF16(content::kAboutBlankURL),
103 omnibox_view->GetText());
104 EXPECT_FALSE(omnibox_view->IsSelectAll());
106 omnibox_view->SetUserText(base::ASCIIToUTF16("chrome"));
107 location_bar->Revert();
109 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid());
110 EXPECT_EQ(base::UTF8ToUTF16(content::kAboutBlankURL),
111 omnibox_view->GetText());
112 EXPECT_FALSE(omnibox_view->IsSelectAll());
115 // Autocomplete test is flaky on ChromeOS.
116 // http://crbug.com/52928
117 #if defined(OS_CHROMEOS)
118 #define MAYBE_Autocomplete DISABLED_Autocomplete
120 #define MAYBE_Autocomplete Autocomplete
123 IN_PROC_BROWSER_TEST_F(AutocompleteBrowserTest, MAYBE_Autocomplete) {
124 #if defined(OS_WIN) && defined(USE_ASH)
125 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
126 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
130 WaitForTemplateURLServiceToLoad();
131 // The results depend on the history backend being loaded. Make sure it is
132 // loaded so that the autocomplete results are consistent.
133 ui_test_utils::WaitForHistoryToLoad(
134 HistoryServiceFactory::GetForProfile(browser()->profile(),
135 Profile::EXPLICIT_ACCESS));
137 LocationBar* location_bar = GetLocationBar();
138 OmniboxView* omnibox_view = location_bar->GetOmniboxView();
139 AutocompleteController* autocomplete_controller = GetAutocompleteController();
142 omnibox_view->model()->SetInputInProgress(true);
143 autocomplete_controller->Start(AutocompleteInput(
144 base::ASCIIToUTF16("chrome"), base::string16::npos, base::string16(),
145 GURL(), AutocompleteInput::NTP, true, false, true,
146 AutocompleteInput::SYNCHRONOUS_MATCHES));
148 EXPECT_TRUE(autocomplete_controller->done());
149 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid());
150 EXPECT_TRUE(omnibox_view->GetText().empty());
151 EXPECT_TRUE(omnibox_view->IsSelectAll());
152 const AutocompleteResult& result = autocomplete_controller->result();
153 // We get two matches because we have a provider for extension apps and the
154 // Chrome Web Store is a built-in Extension app. For this test, we only care
155 // about the other match existing.
156 ASSERT_GE(result.size(), 1U) << AutocompleteResultAsString(result);
157 AutocompleteMatch match = result.match_at(0);
158 EXPECT_EQ(AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED, match.type);
159 EXPECT_FALSE(match.deletable);
163 location_bar->Revert();
164 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid());
165 EXPECT_EQ(base::UTF8ToUTF16(content::kAboutBlankURL),
166 omnibox_view->GetText());
167 EXPECT_FALSE(omnibox_view->IsSelectAll());
168 const AutocompleteResult& result = autocomplete_controller->result();
169 EXPECT_TRUE(result.empty()) << AutocompleteResultAsString(result);
173 IN_PROC_BROWSER_TEST_F(AutocompleteBrowserTest, TabAwayRevertSelect) {
174 #if defined(OS_WIN) && defined(USE_ASH)
175 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
176 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
180 WaitForTemplateURLServiceToLoad();
181 // http://code.google.com/p/chromium/issues/detail?id=38385
182 // Make sure that tabbing away from an empty omnibox causes a revert
184 LocationBar* location_bar = GetLocationBar();
185 OmniboxView* omnibox_view = location_bar->GetOmniboxView();
186 EXPECT_EQ(base::UTF8ToUTF16(content::kAboutBlankURL),
187 omnibox_view->GetText());
188 omnibox_view->SetUserText(base::string16());
189 content::WindowedNotificationObserver observer(
190 content::NOTIFICATION_LOAD_STOP,
191 content::NotificationService::AllSources());
192 chrome::AddSelectedTabWithURL(browser(), GURL(content::kAboutBlankURL),
193 content::PAGE_TRANSITION_AUTO_TOPLEVEL);
195 EXPECT_EQ(base::UTF8ToUTF16(content::kAboutBlankURL),
196 omnibox_view->GetText());
197 chrome::CloseTab(browser());
198 EXPECT_EQ(base::UTF8ToUTF16(content::kAboutBlankURL),
199 omnibox_view->GetText());
200 EXPECT_TRUE(omnibox_view->IsSelectAll());
203 IN_PROC_BROWSER_TEST_F(AutocompleteBrowserTest, FocusSearch) {
204 #if defined(OS_WIN) && defined(USE_ASH)
205 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
206 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
210 WaitForTemplateURLServiceToLoad();
211 LocationBar* location_bar = GetLocationBar();
212 OmniboxView* omnibox_view = location_bar->GetOmniboxView();
214 // Focus search when omnibox is blank
216 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid());
217 EXPECT_EQ(base::UTF8ToUTF16(content::kAboutBlankURL),
218 omnibox_view->GetText());
220 location_bar->FocusSearch();
221 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid());
222 EXPECT_EQ(base::ASCIIToUTF16("?"), omnibox_view->GetText());
224 size_t selection_start, selection_end;
225 omnibox_view->GetSelectionBounds(&selection_start, &selection_end);
226 EXPECT_EQ(1U, selection_start);
227 EXPECT_EQ(1U, selection_end);
230 // Focus search when omnibox is _not_ alread in forced query mode.
232 omnibox_view->SetUserText(base::ASCIIToUTF16("foo"));
233 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid());
234 EXPECT_EQ(base::ASCIIToUTF16("foo"), omnibox_view->GetText());
236 location_bar->FocusSearch();
237 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid());
238 EXPECT_EQ(base::ASCIIToUTF16("?"), omnibox_view->GetText());
240 size_t selection_start, selection_end;
241 omnibox_view->GetSelectionBounds(&selection_start, &selection_end);
242 EXPECT_EQ(1U, selection_start);
243 EXPECT_EQ(1U, selection_end);
246 // Focus search when omnibox _is_ already in forced query mode, but no query
249 omnibox_view->SetUserText(base::ASCIIToUTF16("?"));
250 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid());
251 EXPECT_EQ(base::ASCIIToUTF16("?"), omnibox_view->GetText());
253 location_bar->FocusSearch();
254 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid());
255 EXPECT_EQ(base::ASCIIToUTF16("?"), omnibox_view->GetText());
257 size_t selection_start, selection_end;
258 omnibox_view->GetSelectionBounds(&selection_start, &selection_end);
259 EXPECT_EQ(1U, selection_start);
260 EXPECT_EQ(1U, selection_end);
263 // Focus search when omnibox _is_ already in forced query mode, and some query
266 omnibox_view->SetUserText(base::ASCIIToUTF16("?foo"));
267 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid());
268 EXPECT_EQ(base::ASCIIToUTF16("?foo"), omnibox_view->GetText());
270 location_bar->FocusSearch();
271 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid());
272 EXPECT_EQ(base::ASCIIToUTF16("?foo"), omnibox_view->GetText());
274 size_t selection_start, selection_end;
275 omnibox_view->GetSelectionBounds(&selection_start, &selection_end);
276 EXPECT_EQ(1U, std::min(selection_start, selection_end));
277 EXPECT_EQ(4U, std::max(selection_start, selection_end));
280 // Focus search when omnibox is in forced query mode with leading whitespace.
282 omnibox_view->SetUserText(base::ASCIIToUTF16(" ?foo"));
283 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid());
284 EXPECT_EQ(base::ASCIIToUTF16(" ?foo"), omnibox_view->GetText());
286 location_bar->FocusSearch();
287 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid());
288 EXPECT_EQ(base::ASCIIToUTF16(" ?foo"), omnibox_view->GetText());
290 size_t selection_start, selection_end;
291 omnibox_view->GetSelectionBounds(&selection_start, &selection_end);
292 EXPECT_EQ(4U, std::min(selection_start, selection_end));
293 EXPECT_EQ(7U, std::max(selection_start, selection_end));