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/strings/string16.h"
6 #include "base/strings/utf_string_conversions.h"
7 #include "chrome/browser/autocomplete/autocomplete_controller.h"
8 #include "chrome/browser/autocomplete/chrome_autocomplete_scheme_classifier.h"
9 #include "chrome/browser/extensions/api/omnibox/omnibox_api_testbase.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/search_engines/template_url_service_factory.h"
12 #include "chrome/browser/ui/browser.h"
13 #include "chrome/browser/ui/location_bar/location_bar.h"
14 #include "chrome/browser/ui/omnibox/omnibox_view.h"
15 #include "chrome/test/base/ui_test_utils.h"
16 #include "components/metrics/proto/omnibox_event.pb.h"
17 #include "components/omnibox/autocomplete_input.h"
18 #include "components/omnibox/autocomplete_match.h"
19 #include "components/omnibox/autocomplete_result.h"
20 #include "ui/base/window_open_disposition.h"
22 using base::ASCIIToUTF16;
23 using metrics::OmniboxEventProto;
25 // http://crbug.com/167158
26 IN_PROC_BROWSER_TEST_F(OmniboxApiTest, DISABLED_Basic) {
27 ASSERT_TRUE(RunExtensionTest("omnibox")) << message_;
29 // The results depend on the TemplateURLService being loaded. Make sure it is
30 // loaded so that the autocomplete results are consistent.
31 Profile* profile = browser()->profile();
32 ui_test_utils::WaitForTemplateURLServiceToLoad(
33 TemplateURLServiceFactory::GetForProfile(profile));
35 AutocompleteController* autocomplete_controller =
36 GetAutocompleteController(browser());
38 // Test that our extension's keyword is suggested to us when we partially type
41 autocomplete_controller->Start(AutocompleteInput(
42 ASCIIToUTF16("keywor"), base::string16::npos, base::string16(), GURL(),
43 OmniboxEventProto::NTP, true, false, true, true,
44 ChromeAutocompleteSchemeClassifier(profile)));
45 WaitForAutocompleteDone(autocomplete_controller);
46 EXPECT_TRUE(autocomplete_controller->done());
48 // Now, peek into the controller to see if it has the results we expect.
49 // First result should be to search for what was typed, second should be to
50 // enter "extension keyword" mode.
51 const AutocompleteResult& result = autocomplete_controller->result();
52 ASSERT_EQ(2U, result.size()) << AutocompleteResultAsString(result);
53 AutocompleteMatch match = result.match_at(0);
54 EXPECT_EQ(AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED, match.type);
55 EXPECT_FALSE(match.deletable);
57 match = result.match_at(1);
58 EXPECT_EQ(ASCIIToUTF16("keyword"), match.keyword);
61 // Test that our extension can send suggestions back to us.
63 autocomplete_controller->Start(AutocompleteInput(
64 ASCIIToUTF16("keyword suggestio"), base::string16::npos,
65 base::string16(), GURL(), OmniboxEventProto::NTP, true, false, true,
66 true, ChromeAutocompleteSchemeClassifier(profile)));
67 WaitForAutocompleteDone(autocomplete_controller);
68 EXPECT_TRUE(autocomplete_controller->done());
70 // Now, peek into the controller to see if it has the results we expect.
71 // First result should be to invoke the keyword with what we typed, 2-4
72 // should be to invoke with suggestions from the extension, and the last
73 // should be to search for what we typed.
74 const AutocompleteResult& result = autocomplete_controller->result();
75 ASSERT_EQ(5U, result.size()) << AutocompleteResultAsString(result);
77 EXPECT_EQ(ASCIIToUTF16("keyword"), result.match_at(0).keyword);
78 EXPECT_EQ(ASCIIToUTF16("keyword suggestio"),
79 result.match_at(0).fill_into_edit);
80 EXPECT_EQ(AutocompleteMatchType::SEARCH_OTHER_ENGINE,
81 result.match_at(0).type);
82 EXPECT_EQ(AutocompleteProvider::TYPE_KEYWORD,
83 result.match_at(0).provider->type());
84 EXPECT_EQ(ASCIIToUTF16("keyword"), result.match_at(1).keyword);
85 EXPECT_EQ(ASCIIToUTF16("keyword suggestion1"),
86 result.match_at(1).fill_into_edit);
87 EXPECT_EQ(AutocompleteProvider::TYPE_KEYWORD,
88 result.match_at(1).provider->type());
89 EXPECT_EQ(ASCIIToUTF16("keyword"), result.match_at(2).keyword);
90 EXPECT_EQ(ASCIIToUTF16("keyword suggestion2"),
91 result.match_at(2).fill_into_edit);
92 EXPECT_EQ(AutocompleteProvider::TYPE_KEYWORD,
93 result.match_at(2).provider->type());
94 EXPECT_EQ(ASCIIToUTF16("keyword"), result.match_at(3).keyword);
95 EXPECT_EQ(ASCIIToUTF16("keyword suggestion3"),
96 result.match_at(3).fill_into_edit);
97 EXPECT_EQ(AutocompleteProvider::TYPE_KEYWORD,
98 result.match_at(3).provider->type());
100 base::string16 description =
101 ASCIIToUTF16("Description with style: <match>, [dim], (url till end)");
102 EXPECT_EQ(description, result.match_at(1).contents);
103 ASSERT_EQ(6u, result.match_at(1).contents_class.size());
106 result.match_at(1).contents_class[0].offset);
107 EXPECT_EQ(ACMatchClassification::NONE,
108 result.match_at(1).contents_class[0].style);
110 EXPECT_EQ(description.find('<'),
111 result.match_at(1).contents_class[1].offset);
112 EXPECT_EQ(ACMatchClassification::MATCH,
113 result.match_at(1).contents_class[1].style);
115 EXPECT_EQ(description.find('>') + 1u,
116 result.match_at(1).contents_class[2].offset);
117 EXPECT_EQ(ACMatchClassification::NONE,
118 result.match_at(1).contents_class[2].style);
120 EXPECT_EQ(description.find('['),
121 result.match_at(1).contents_class[3].offset);
122 EXPECT_EQ(ACMatchClassification::DIM,
123 result.match_at(1).contents_class[3].style);
125 EXPECT_EQ(description.find(']') + 1u,
126 result.match_at(1).contents_class[4].offset);
127 EXPECT_EQ(ACMatchClassification::NONE,
128 result.match_at(1).contents_class[4].style);
130 EXPECT_EQ(description.find('('),
131 result.match_at(1).contents_class[5].offset);
132 EXPECT_EQ(ACMatchClassification::URL,
133 result.match_at(1).contents_class[5].style);
135 AutocompleteMatch match = result.match_at(4);
136 EXPECT_EQ(AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED, match.type);
137 EXPECT_EQ(AutocompleteProvider::TYPE_SEARCH,
138 result.match_at(4).provider->type());
139 EXPECT_FALSE(match.deletable);
142 // Flaky, see http://crbug.com/167158
145 LocationBar* location_bar = GetLocationBar(browser());
146 ResultCatcher catcher;
147 OmniboxView* omnibox_view = location_bar->GetOmniboxView();
148 omnibox_view->OnBeforePossibleChange();
149 omnibox_view->SetUserText(ASCIIToUTF16("keyword command"));
150 omnibox_view->OnAfterPossibleChange();
151 location_bar->AcceptInput();
152 // This checks that the keyword provider (via javascript)
153 // gets told to navigate to the string "command".
154 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
159 IN_PROC_BROWSER_TEST_F(OmniboxApiTest, OnInputEntered) {
160 ASSERT_TRUE(RunExtensionTest("omnibox")) << message_;
161 Profile* profile = browser()->profile();
162 ui_test_utils::WaitForTemplateURLServiceToLoad(
163 TemplateURLServiceFactory::GetForProfile(profile));
165 LocationBar* location_bar = GetLocationBar(browser());
166 OmniboxView* omnibox_view = location_bar->GetOmniboxView();
167 ResultCatcher catcher;
168 AutocompleteController* autocomplete_controller =
169 GetAutocompleteController(browser());
170 omnibox_view->OnBeforePossibleChange();
171 omnibox_view->SetUserText(ASCIIToUTF16("keyword command"));
172 omnibox_view->OnAfterPossibleChange();
174 autocomplete_controller->Start(AutocompleteInput(
175 ASCIIToUTF16("keyword command"), base::string16::npos, base::string16(),
176 GURL(), OmniboxEventProto::NTP, true, false, true, true,
177 ChromeAutocompleteSchemeClassifier(profile)));
178 omnibox_view->model()->AcceptInput(CURRENT_TAB, false);
179 WaitForAutocompleteDone(autocomplete_controller);
180 EXPECT_TRUE(autocomplete_controller->done());
181 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
183 omnibox_view->OnBeforePossibleChange();
184 omnibox_view->SetUserText(ASCIIToUTF16("keyword newtab"));
185 omnibox_view->OnAfterPossibleChange();
186 WaitForAutocompleteDone(autocomplete_controller);
187 EXPECT_TRUE(autocomplete_controller->done());
189 autocomplete_controller->Start(AutocompleteInput(
190 ASCIIToUTF16("keyword newtab"), base::string16::npos, base::string16(),
191 GURL(), OmniboxEventProto::NTP, true, false, true, true,
192 ChromeAutocompleteSchemeClassifier(profile)));
193 omnibox_view->model()->AcceptInput(NEW_FOREGROUND_TAB, false);
194 WaitForAutocompleteDone(autocomplete_controller);
195 EXPECT_TRUE(autocomplete_controller->done());
196 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
199 // Tests that we get suggestions from and send input to the incognito context
200 // of an incognito split mode extension.
201 // http://crbug.com/100927
202 // Test is flaky: http://crbug.com/101219
203 IN_PROC_BROWSER_TEST_F(OmniboxApiTest, DISABLED_IncognitoSplitMode) {
204 Profile* profile = browser()->profile();
205 ResultCatcher catcher_incognito;
206 catcher_incognito.RestrictToProfile(profile->GetOffTheRecordProfile());
208 ASSERT_TRUE(RunExtensionTestIncognito("omnibox")) << message_;
210 // Open an incognito window and wait for the incognito extension process to
212 Browser* incognito_browser = CreateIncognitoBrowser();
213 ASSERT_TRUE(catcher_incognito.GetNextResult()) << catcher_incognito.message();
215 // The results depend on the TemplateURLService being loaded. Make sure it is
216 // loaded so that the autocomplete results are consistent.
217 ui_test_utils::WaitForTemplateURLServiceToLoad(
218 TemplateURLServiceFactory::GetForProfile(browser()->profile()));
220 LocationBar* location_bar = GetLocationBar(incognito_browser);
221 AutocompleteController* autocomplete_controller =
222 GetAutocompleteController(incognito_browser);
224 // Test that we get the incognito-specific suggestions.
226 autocomplete_controller->Start(AutocompleteInput(
227 ASCIIToUTF16("keyword suggestio"), base::string16::npos,
228 base::string16(), GURL(), OmniboxEventProto::NTP, true, false, true,
229 true, ChromeAutocompleteSchemeClassifier(profile)));
230 WaitForAutocompleteDone(autocomplete_controller);
231 EXPECT_TRUE(autocomplete_controller->done());
233 // First result should be to invoke the keyword with what we typed, 2-4
234 // should be to invoke with suggestions from the extension, and the last
235 // should be to search for what we typed.
236 const AutocompleteResult& result = autocomplete_controller->result();
237 ASSERT_EQ(5U, result.size()) << AutocompleteResultAsString(result);
238 ASSERT_FALSE(result.match_at(0).keyword.empty());
239 EXPECT_EQ(ASCIIToUTF16("keyword suggestion3 incognito"),
240 result.match_at(3).fill_into_edit);
243 // Test that our input is sent to the incognito context. The test will do a
244 // text comparison and succeed only if "command incognito" is sent to the
245 // incognito context.
247 ResultCatcher catcher;
248 autocomplete_controller->Start(AutocompleteInput(
249 ASCIIToUTF16("keyword command incognito"), base::string16::npos,
250 base::string16(), GURL(), OmniboxEventProto::NTP, true, false, true,
251 true, ChromeAutocompleteSchemeClassifier(profile)));
252 location_bar->AcceptInput();
253 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();