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, false));
147 EXPECT_TRUE(autocomplete_controller
->done());
148 EXPECT_FALSE(location_bar
->GetDestinationURL().is_valid());
149 EXPECT_TRUE(omnibox_view
->GetText().empty());
150 EXPECT_TRUE(omnibox_view
->IsSelectAll());
151 const AutocompleteResult
& result
= autocomplete_controller
->result();
152 // We get two matches because we have a provider for extension apps and the
153 // Chrome Web Store is a built-in Extension app. For this test, we only care
154 // about the other match existing.
155 ASSERT_GE(result
.size(), 1U) << AutocompleteResultAsString(result
);
156 AutocompleteMatch match
= result
.match_at(0);
157 EXPECT_EQ(AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED
, match
.type
);
158 EXPECT_FALSE(match
.deletable
);
162 location_bar
->Revert();
163 EXPECT_FALSE(location_bar
->GetDestinationURL().is_valid());
164 EXPECT_EQ(base::UTF8ToUTF16(content::kAboutBlankURL
),
165 omnibox_view
->GetText());
166 EXPECT_FALSE(omnibox_view
->IsSelectAll());
167 const AutocompleteResult
& result
= autocomplete_controller
->result();
168 EXPECT_TRUE(result
.empty()) << AutocompleteResultAsString(result
);
172 IN_PROC_BROWSER_TEST_F(AutocompleteBrowserTest
, TabAwayRevertSelect
) {
173 #if defined(OS_WIN) && defined(USE_ASH)
174 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
175 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests
))
179 WaitForTemplateURLServiceToLoad();
180 // http://code.google.com/p/chromium/issues/detail?id=38385
181 // Make sure that tabbing away from an empty omnibox causes a revert
183 LocationBar
* location_bar
= GetLocationBar();
184 OmniboxView
* omnibox_view
= location_bar
->GetOmniboxView();
185 EXPECT_EQ(base::UTF8ToUTF16(content::kAboutBlankURL
),
186 omnibox_view
->GetText());
187 omnibox_view
->SetUserText(base::string16());
188 content::WindowedNotificationObserver
observer(
189 content::NOTIFICATION_LOAD_STOP
,
190 content::NotificationService::AllSources());
191 chrome::AddSelectedTabWithURL(browser(), GURL(content::kAboutBlankURL
),
192 content::PAGE_TRANSITION_AUTO_TOPLEVEL
);
194 EXPECT_EQ(base::UTF8ToUTF16(content::kAboutBlankURL
),
195 omnibox_view
->GetText());
196 chrome::CloseTab(browser());
197 EXPECT_EQ(base::UTF8ToUTF16(content::kAboutBlankURL
),
198 omnibox_view
->GetText());
199 EXPECT_TRUE(omnibox_view
->IsSelectAll());
202 IN_PROC_BROWSER_TEST_F(AutocompleteBrowserTest
, FocusSearch
) {
203 #if defined(OS_WIN) && defined(USE_ASH)
204 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
205 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests
))
209 WaitForTemplateURLServiceToLoad();
210 LocationBar
* location_bar
= GetLocationBar();
211 OmniboxView
* omnibox_view
= location_bar
->GetOmniboxView();
213 // Focus search when omnibox is blank
215 EXPECT_FALSE(location_bar
->GetDestinationURL().is_valid());
216 EXPECT_EQ(base::UTF8ToUTF16(content::kAboutBlankURL
),
217 omnibox_view
->GetText());
219 location_bar
->FocusSearch();
220 EXPECT_FALSE(location_bar
->GetDestinationURL().is_valid());
221 EXPECT_EQ(base::ASCIIToUTF16("?"), omnibox_view
->GetText());
223 size_t selection_start
, selection_end
;
224 omnibox_view
->GetSelectionBounds(&selection_start
, &selection_end
);
225 EXPECT_EQ(1U, selection_start
);
226 EXPECT_EQ(1U, selection_end
);
229 // Focus search when omnibox is _not_ alread in forced query mode.
231 omnibox_view
->SetUserText(base::ASCIIToUTF16("foo"));
232 EXPECT_FALSE(location_bar
->GetDestinationURL().is_valid());
233 EXPECT_EQ(base::ASCIIToUTF16("foo"), omnibox_view
->GetText());
235 location_bar
->FocusSearch();
236 EXPECT_FALSE(location_bar
->GetDestinationURL().is_valid());
237 EXPECT_EQ(base::ASCIIToUTF16("?"), omnibox_view
->GetText());
239 size_t selection_start
, selection_end
;
240 omnibox_view
->GetSelectionBounds(&selection_start
, &selection_end
);
241 EXPECT_EQ(1U, selection_start
);
242 EXPECT_EQ(1U, selection_end
);
245 // Focus search when omnibox _is_ already in forced query mode, but no query
248 omnibox_view
->SetUserText(base::ASCIIToUTF16("?"));
249 EXPECT_FALSE(location_bar
->GetDestinationURL().is_valid());
250 EXPECT_EQ(base::ASCIIToUTF16("?"), omnibox_view
->GetText());
252 location_bar
->FocusSearch();
253 EXPECT_FALSE(location_bar
->GetDestinationURL().is_valid());
254 EXPECT_EQ(base::ASCIIToUTF16("?"), omnibox_view
->GetText());
256 size_t selection_start
, selection_end
;
257 omnibox_view
->GetSelectionBounds(&selection_start
, &selection_end
);
258 EXPECT_EQ(1U, selection_start
);
259 EXPECT_EQ(1U, selection_end
);
262 // Focus search when omnibox _is_ already in forced query mode, and some query
265 omnibox_view
->SetUserText(base::ASCIIToUTF16("?foo"));
266 EXPECT_FALSE(location_bar
->GetDestinationURL().is_valid());
267 EXPECT_EQ(base::ASCIIToUTF16("?foo"), omnibox_view
->GetText());
269 location_bar
->FocusSearch();
270 EXPECT_FALSE(location_bar
->GetDestinationURL().is_valid());
271 EXPECT_EQ(base::ASCIIToUTF16("?foo"), omnibox_view
->GetText());
273 size_t selection_start
, selection_end
;
274 omnibox_view
->GetSelectionBounds(&selection_start
, &selection_end
);
275 EXPECT_EQ(1U, std::min(selection_start
, selection_end
));
276 EXPECT_EQ(4U, std::max(selection_start
, selection_end
));
279 // Focus search when omnibox is in forced query mode with leading whitespace.
281 omnibox_view
->SetUserText(base::ASCIIToUTF16(" ?foo"));
282 EXPECT_FALSE(location_bar
->GetDestinationURL().is_valid());
283 EXPECT_EQ(base::ASCIIToUTF16(" ?foo"), omnibox_view
->GetText());
285 location_bar
->FocusSearch();
286 EXPECT_FALSE(location_bar
->GetDestinationURL().is_valid());
287 EXPECT_EQ(base::ASCIIToUTF16(" ?foo"), omnibox_view
->GetText());
289 size_t selection_start
, selection_end
;
290 omnibox_view
->GetSelectionBounds(&selection_start
, &selection_end
);
291 EXPECT_EQ(4U, std::min(selection_start
, selection_end
));
292 EXPECT_EQ(7U, std::max(selection_start
, selection_end
));