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_factory.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/search_engines/template_url_service_factory.h"
16 #include "chrome/browser/ui/browser.h"
17 #include "chrome/browser/ui/browser_commands.h"
18 #include "chrome/browser/ui/browser_tabstrip.h"
19 #include "chrome/browser/ui/browser_window.h"
20 #include "chrome/browser/ui/location_bar/location_bar.h"
21 #include "chrome/browser/ui/omnibox/omnibox_popup_model.h"
22 #include "chrome/browser/ui/omnibox/omnibox_view.h"
23 #include "chrome/browser/ui/tabs/tab_strip_model.h"
24 #include "chrome/common/chrome_paths.h"
25 #include "chrome/common/url_constants.h"
26 #include "chrome/test/base/in_process_browser_test.h"
27 #include "chrome/test/base/test_switches.h"
28 #include "chrome/test/base/ui_test_utils.h"
29 #include "components/history/core/browser/history_service.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 (base::CommandLine::ForCurrentProcess()->HasSwitch(
74 switches::kAshBrowserTests
))
78 WaitForTemplateURLServiceToLoad();
79 LocationBar
* location_bar
= GetLocationBar();
80 OmniboxView
* omnibox_view
= location_bar
->GetOmniboxView();
82 EXPECT_FALSE(location_bar
->GetDestinationURL().is_valid());
83 EXPECT_EQ(base::UTF8ToUTF16(url::kAboutBlankURL
), omnibox_view
->GetText());
84 // TODO(phajdan.jr): check state of IsSelectAll when it's consistent across
87 location_bar
->FocusLocation(true);
89 EXPECT_FALSE(location_bar
->GetDestinationURL().is_valid());
90 EXPECT_EQ(base::UTF8ToUTF16(url::kAboutBlankURL
), 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(url::kAboutBlankURL
), omnibox_view
->GetText());
103 EXPECT_FALSE(omnibox_view
->IsSelectAll());
105 omnibox_view
->SetUserText(base::ASCIIToUTF16("chrome"));
106 location_bar
->Revert();
108 EXPECT_FALSE(location_bar
->GetDestinationURL().is_valid());
109 EXPECT_EQ(base::UTF8ToUTF16(url::kAboutBlankURL
), omnibox_view
->GetText());
110 EXPECT_FALSE(omnibox_view
->IsSelectAll());
113 // Autocomplete test is flaky on ChromeOS.
114 // http://crbug.com/52928
115 #if defined(OS_CHROMEOS)
116 #define MAYBE_Autocomplete DISABLED_Autocomplete
118 #define MAYBE_Autocomplete Autocomplete
121 IN_PROC_BROWSER_TEST_F(AutocompleteBrowserTest
, MAYBE_Autocomplete
) {
122 #if defined(OS_WIN) && defined(USE_ASH)
123 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
124 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
125 switches::kAshBrowserTests
))
129 WaitForTemplateURLServiceToLoad();
130 // The results depend on the history backend being loaded. Make sure it is
131 // loaded so that the autocomplete results are consistent.
132 ui_test_utils::WaitForHistoryToLoad(HistoryServiceFactory::GetForProfile(
133 browser()->profile(), ServiceAccessType::EXPLICIT_ACCESS
));
135 LocationBar
* location_bar
= GetLocationBar();
136 OmniboxView
* omnibox_view
= location_bar
->GetOmniboxView();
137 AutocompleteController
* autocomplete_controller
= GetAutocompleteController();
140 omnibox_view
->model()->SetInputInProgress(true);
141 autocomplete_controller
->Start(AutocompleteInput(
142 base::ASCIIToUTF16("chrome"), base::string16::npos
, std::string(),
143 GURL(), metrics::OmniboxEventProto::NTP
, true, false, true, false,
144 ChromeAutocompleteSchemeClassifier(browser()->profile())));
146 EXPECT_TRUE(autocomplete_controller
->done());
147 EXPECT_FALSE(location_bar
->GetDestinationURL().is_valid());
148 EXPECT_TRUE(omnibox_view
->GetText().empty());
149 EXPECT_TRUE(omnibox_view
->IsSelectAll());
150 const AutocompleteResult
& result
= autocomplete_controller
->result();
151 ASSERT_GE(result
.size(), 1U) << AutocompleteResultAsString(result
);
152 AutocompleteMatch match
= result
.match_at(0);
153 EXPECT_EQ(AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED
, match
.type
);
154 EXPECT_FALSE(match
.deletable
);
158 location_bar
->Revert();
159 EXPECT_FALSE(location_bar
->GetDestinationURL().is_valid());
160 EXPECT_EQ(base::UTF8ToUTF16(url::kAboutBlankURL
), omnibox_view
->GetText());
161 EXPECT_FALSE(omnibox_view
->IsSelectAll());
162 const AutocompleteResult
& result
= autocomplete_controller
->result();
163 EXPECT_TRUE(result
.empty()) << AutocompleteResultAsString(result
);
167 IN_PROC_BROWSER_TEST_F(AutocompleteBrowserTest
, TabAwayRevertSelect
) {
168 #if defined(OS_WIN) && defined(USE_ASH)
169 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
170 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
171 switches::kAshBrowserTests
))
175 WaitForTemplateURLServiceToLoad();
176 // http://code.google.com/p/chromium/issues/detail?id=38385
177 // Make sure that tabbing away from an empty omnibox causes a revert
179 LocationBar
* location_bar
= GetLocationBar();
180 OmniboxView
* omnibox_view
= location_bar
->GetOmniboxView();
181 EXPECT_EQ(base::UTF8ToUTF16(url::kAboutBlankURL
), omnibox_view
->GetText());
182 omnibox_view
->SetUserText(base::string16());
183 content::WindowedNotificationObserver
observer(
184 content::NOTIFICATION_LOAD_STOP
,
185 content::NotificationService::AllSources());
186 chrome::AddSelectedTabWithURL(browser(),
187 GURL(url::kAboutBlankURL
),
188 ui::PAGE_TRANSITION_AUTO_TOPLEVEL
);
190 EXPECT_EQ(base::UTF8ToUTF16(url::kAboutBlankURL
), omnibox_view
->GetText());
191 chrome::CloseTab(browser());
192 EXPECT_EQ(base::UTF8ToUTF16(url::kAboutBlankURL
), omnibox_view
->GetText());
193 EXPECT_TRUE(omnibox_view
->IsSelectAll());
196 IN_PROC_BROWSER_TEST_F(AutocompleteBrowserTest
, FocusSearch
) {
197 #if defined(OS_WIN) && defined(USE_ASH)
198 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
199 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
200 switches::kAshBrowserTests
))
204 WaitForTemplateURLServiceToLoad();
205 LocationBar
* location_bar
= GetLocationBar();
206 OmniboxView
* omnibox_view
= location_bar
->GetOmniboxView();
208 // Focus search when omnibox is blank
210 EXPECT_FALSE(location_bar
->GetDestinationURL().is_valid());
211 EXPECT_EQ(base::UTF8ToUTF16(url::kAboutBlankURL
), omnibox_view
->GetText());
213 location_bar
->FocusSearch();
214 EXPECT_FALSE(location_bar
->GetDestinationURL().is_valid());
215 EXPECT_EQ(base::ASCIIToUTF16("?"), omnibox_view
->GetText());
217 size_t selection_start
, selection_end
;
218 omnibox_view
->GetSelectionBounds(&selection_start
, &selection_end
);
219 EXPECT_EQ(1U, selection_start
);
220 EXPECT_EQ(1U, selection_end
);
223 // Focus search when omnibox is _not_ alread in forced query mode.
225 omnibox_view
->SetUserText(base::ASCIIToUTF16("foo"));
226 EXPECT_FALSE(location_bar
->GetDestinationURL().is_valid());
227 EXPECT_EQ(base::ASCIIToUTF16("foo"), omnibox_view
->GetText());
229 location_bar
->FocusSearch();
230 EXPECT_FALSE(location_bar
->GetDestinationURL().is_valid());
231 EXPECT_EQ(base::ASCIIToUTF16("?"), omnibox_view
->GetText());
233 size_t selection_start
, selection_end
;
234 omnibox_view
->GetSelectionBounds(&selection_start
, &selection_end
);
235 EXPECT_EQ(1U, selection_start
);
236 EXPECT_EQ(1U, selection_end
);
239 // Focus search when omnibox _is_ already in forced query mode, but no query
242 omnibox_view
->SetUserText(base::ASCIIToUTF16("?"));
243 EXPECT_FALSE(location_bar
->GetDestinationURL().is_valid());
244 EXPECT_EQ(base::ASCIIToUTF16("?"), omnibox_view
->GetText());
246 location_bar
->FocusSearch();
247 EXPECT_FALSE(location_bar
->GetDestinationURL().is_valid());
248 EXPECT_EQ(base::ASCIIToUTF16("?"), omnibox_view
->GetText());
250 size_t selection_start
, selection_end
;
251 omnibox_view
->GetSelectionBounds(&selection_start
, &selection_end
);
252 EXPECT_EQ(1U, selection_start
);
253 EXPECT_EQ(1U, selection_end
);
256 // Focus search when omnibox _is_ already in forced query mode, and some query
259 omnibox_view
->SetUserText(base::ASCIIToUTF16("?foo"));
260 EXPECT_FALSE(location_bar
->GetDestinationURL().is_valid());
261 EXPECT_EQ(base::ASCIIToUTF16("?foo"), omnibox_view
->GetText());
263 location_bar
->FocusSearch();
264 EXPECT_FALSE(location_bar
->GetDestinationURL().is_valid());
265 EXPECT_EQ(base::ASCIIToUTF16("?foo"), omnibox_view
->GetText());
267 size_t selection_start
, selection_end
;
268 omnibox_view
->GetSelectionBounds(&selection_start
, &selection_end
);
269 EXPECT_EQ(1U, std::min(selection_start
, selection_end
));
270 EXPECT_EQ(4U, std::max(selection_start
, selection_end
));
273 // Focus search when omnibox is in forced query mode with leading whitespace.
275 omnibox_view
->SetUserText(base::ASCIIToUTF16(" ?foo"));
276 EXPECT_FALSE(location_bar
->GetDestinationURL().is_valid());
277 EXPECT_EQ(base::ASCIIToUTF16(" ?foo"), omnibox_view
->GetText());
279 location_bar
->FocusSearch();
280 EXPECT_FALSE(location_bar
->GetDestinationURL().is_valid());
281 EXPECT_EQ(base::ASCIIToUTF16(" ?foo"), omnibox_view
->GetText());
283 size_t selection_start
, selection_end
;
284 omnibox_view
->GetSelectionBounds(&selection_start
, &selection_end
);
285 EXPECT_EQ(4U, std::min(selection_start
, selection_end
));
286 EXPECT_EQ(7U, std::max(selection_start
, selection_end
));