Enable Enterprise enrollment on desktop builds.
[chromium-blink-merge.git] / chrome / browser / extensions / api / omnibox / omnibox_api_browsertest.cc
blobd51d7a446eaaad3b35421c6529000f0ee72de648
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/autocomplete_input.h"
9 #include "chrome/browser/autocomplete/autocomplete_match.h"
10 #include "chrome/browser/autocomplete/autocomplete_result.h"
11 #include "chrome/browser/extensions/api/omnibox/omnibox_api_testbase.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/search_engines/template_url_service_factory.h"
14 #include "chrome/browser/ui/browser.h"
15 #include "chrome/browser/ui/omnibox/location_bar.h"
16 #include "chrome/browser/ui/omnibox/omnibox_view.h"
17 #include "chrome/test/base/ui_test_utils.h"
18 #include "ui/base/window_open_disposition.h"
20 using base::ASCIIToUTF16;
22 IN_PROC_BROWSER_TEST_F(OmniboxApiTest, Basic) {
23 ASSERT_TRUE(RunExtensionTest("omnibox")) << message_;
25 // The results depend on the TemplateURLService being loaded. Make sure it is
26 // loaded so that the autocomplete results are consistent.
27 ui_test_utils::WaitForTemplateURLServiceToLoad(
28 TemplateURLServiceFactory::GetForProfile(browser()->profile()));
30 AutocompleteController* autocomplete_controller =
31 GetAutocompleteController(browser());
33 // Test that our extension's keyword is suggested to us when we partially type
34 // it.
36 autocomplete_controller->Start(
37 AutocompleteInput(ASCIIToUTF16("keywor"), base::string16::npos,
38 base::string16(), GURL(), AutocompleteInput::NTP,
39 true, false, true, true));
40 WaitForAutocompleteDone(autocomplete_controller);
41 EXPECT_TRUE(autocomplete_controller->done());
43 // Now, peek into the controller to see if it has the results we expect.
44 // First result should be to search for what was typed, second should be to
45 // enter "extension keyword" mode.
46 const AutocompleteResult& result = autocomplete_controller->result();
47 ASSERT_EQ(2U, result.size()) << AutocompleteResultAsString(result);
48 AutocompleteMatch match = result.match_at(0);
49 EXPECT_EQ(AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED, match.type);
50 EXPECT_FALSE(match.deletable);
52 match = result.match_at(1);
53 EXPECT_EQ(ASCIIToUTF16("keyword"), match.keyword);
56 // Test that our extension can send suggestions back to us.
58 autocomplete_controller->Start(
59 AutocompleteInput(ASCIIToUTF16("keyword suggestio"),
60 base::string16::npos, base::string16(), GURL(),
61 AutocompleteInput::NTP, true, false, true, true));
62 WaitForAutocompleteDone(autocomplete_controller);
63 EXPECT_TRUE(autocomplete_controller->done());
65 // Now, peek into the controller to see if it has the results we expect.
66 // First result should be to invoke the keyword with what we typed, 2-4
67 // should be to invoke with suggestions from the extension, and the last
68 // should be to search for what we typed.
69 const AutocompleteResult& result = autocomplete_controller->result();
70 ASSERT_EQ(5U, result.size()) << AutocompleteResultAsString(result);
72 EXPECT_EQ(ASCIIToUTF16("keyword"), result.match_at(0).keyword);
73 EXPECT_EQ(ASCIIToUTF16("keyword suggestio"),
74 result.match_at(0).fill_into_edit);
75 EXPECT_EQ(AutocompleteMatchType::SEARCH_OTHER_ENGINE,
76 result.match_at(0).type);
77 EXPECT_EQ(AutocompleteProvider::TYPE_KEYWORD,
78 result.match_at(0).provider->type());
79 EXPECT_EQ(ASCIIToUTF16("keyword"), result.match_at(1).keyword);
80 EXPECT_EQ(ASCIIToUTF16("keyword suggestion1"),
81 result.match_at(1).fill_into_edit);
82 EXPECT_EQ(AutocompleteProvider::TYPE_KEYWORD,
83 result.match_at(1).provider->type());
84 EXPECT_EQ(ASCIIToUTF16("keyword"), result.match_at(2).keyword);
85 EXPECT_EQ(ASCIIToUTF16("keyword suggestion2"),
86 result.match_at(2).fill_into_edit);
87 EXPECT_EQ(AutocompleteProvider::TYPE_KEYWORD,
88 result.match_at(2).provider->type());
89 EXPECT_EQ(ASCIIToUTF16("keyword"), result.match_at(3).keyword);
90 EXPECT_EQ(ASCIIToUTF16("keyword suggestion3"),
91 result.match_at(3).fill_into_edit);
92 EXPECT_EQ(AutocompleteProvider::TYPE_KEYWORD,
93 result.match_at(3).provider->type());
95 base::string16 description =
96 ASCIIToUTF16("Description with style: <match>, [dim], (url till end)");
97 EXPECT_EQ(description, result.match_at(1).contents);
98 ASSERT_EQ(6u, result.match_at(1).contents_class.size());
100 EXPECT_EQ(0u,
101 result.match_at(1).contents_class[0].offset);
102 EXPECT_EQ(ACMatchClassification::NONE,
103 result.match_at(1).contents_class[0].style);
105 EXPECT_EQ(description.find('<'),
106 result.match_at(1).contents_class[1].offset);
107 EXPECT_EQ(ACMatchClassification::MATCH,
108 result.match_at(1).contents_class[1].style);
110 EXPECT_EQ(description.find('>') + 1u,
111 result.match_at(1).contents_class[2].offset);
112 EXPECT_EQ(ACMatchClassification::NONE,
113 result.match_at(1).contents_class[2].style);
115 EXPECT_EQ(description.find('['),
116 result.match_at(1).contents_class[3].offset);
117 EXPECT_EQ(ACMatchClassification::DIM,
118 result.match_at(1).contents_class[3].style);
120 EXPECT_EQ(description.find(']') + 1u,
121 result.match_at(1).contents_class[4].offset);
122 EXPECT_EQ(ACMatchClassification::NONE,
123 result.match_at(1).contents_class[4].style);
125 EXPECT_EQ(description.find('('),
126 result.match_at(1).contents_class[5].offset);
127 EXPECT_EQ(ACMatchClassification::URL,
128 result.match_at(1).contents_class[5].style);
130 AutocompleteMatch match = result.match_at(4);
131 EXPECT_EQ(AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED, match.type);
132 EXPECT_EQ(AutocompleteProvider::TYPE_SEARCH,
133 result.match_at(4).provider->type());
134 EXPECT_FALSE(match.deletable);
137 // Flaky, see http://crbug.com/167158
140 LocationBar* location_bar = GetLocationBar(browser());
141 ResultCatcher catcher;
142 OmniboxView* omnibox_view = location_bar->GetOmniboxView();
143 omnibox_view->OnBeforePossibleChange();
144 omnibox_view->SetUserText(ASCIIToUTF16("keyword command"));
145 omnibox_view->OnAfterPossibleChange();
146 location_bar->AcceptInput();
147 // This checks that the keyword provider (via javascript)
148 // gets told to navigate to the string "command".
149 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
154 IN_PROC_BROWSER_TEST_F(OmniboxApiTest, OnInputEntered) {
155 ASSERT_TRUE(RunExtensionTest("omnibox")) << message_;
156 ui_test_utils::WaitForTemplateURLServiceToLoad(
157 TemplateURLServiceFactory::GetForProfile(browser()->profile()));
159 LocationBar* location_bar = GetLocationBar(browser());
160 OmniboxView* omnibox_view = location_bar->GetOmniboxView();
161 ResultCatcher catcher;
162 AutocompleteController* autocomplete_controller =
163 GetAutocompleteController(browser());
164 omnibox_view->OnBeforePossibleChange();
165 omnibox_view->SetUserText(ASCIIToUTF16("keyword command"));
166 omnibox_view->OnAfterPossibleChange();
168 autocomplete_controller->Start(
169 AutocompleteInput(ASCIIToUTF16("keyword command"), base::string16::npos,
170 base::string16(), GURL(), AutocompleteInput::NTP,
171 true, false, true, true));
172 omnibox_view->model()->AcceptInput(CURRENT_TAB, false);
173 WaitForAutocompleteDone(autocomplete_controller);
174 EXPECT_TRUE(autocomplete_controller->done());
175 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
177 omnibox_view->OnBeforePossibleChange();
178 omnibox_view->SetUserText(ASCIIToUTF16("keyword newtab"));
179 omnibox_view->OnAfterPossibleChange();
180 WaitForAutocompleteDone(autocomplete_controller);
181 EXPECT_TRUE(autocomplete_controller->done());
183 autocomplete_controller->Start(
184 AutocompleteInput(ASCIIToUTF16("keyword newtab"), base::string16::npos,
185 base::string16(), GURL(), AutocompleteInput::NTP,
186 true, false, true, true));
187 omnibox_view->model()->AcceptInput(NEW_FOREGROUND_TAB, false);
188 WaitForAutocompleteDone(autocomplete_controller);
189 EXPECT_TRUE(autocomplete_controller->done());
190 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
193 // Tests that we get suggestions from and send input to the incognito context
194 // of an incognito split mode extension.
195 // http://crbug.com/100927
196 // Test is flaky: http://crbug.com/101219
197 IN_PROC_BROWSER_TEST_F(OmniboxApiTest, DISABLED_IncognitoSplitMode) {
198 ResultCatcher catcher_incognito;
199 catcher_incognito.RestrictToProfile(
200 browser()->profile()->GetOffTheRecordProfile());
202 ASSERT_TRUE(RunExtensionTestIncognito("omnibox")) << message_;
204 // Open an incognito window and wait for the incognito extension process to
205 // respond.
206 Browser* incognito_browser = CreateIncognitoBrowser();
207 ASSERT_TRUE(catcher_incognito.GetNextResult()) << catcher_incognito.message();
209 // The results depend on the TemplateURLService being loaded. Make sure it is
210 // loaded so that the autocomplete results are consistent.
211 ui_test_utils::WaitForTemplateURLServiceToLoad(
212 TemplateURLServiceFactory::GetForProfile(browser()->profile()));
214 LocationBar* location_bar = GetLocationBar(incognito_browser);
215 AutocompleteController* autocomplete_controller =
216 GetAutocompleteController(incognito_browser);
218 // Test that we get the incognito-specific suggestions.
220 autocomplete_controller->Start(
221 AutocompleteInput(ASCIIToUTF16("keyword suggestio"),
222 base::string16::npos, base::string16(), GURL(),
223 AutocompleteInput::NTP, true, false, true, true));
224 WaitForAutocompleteDone(autocomplete_controller);
225 EXPECT_TRUE(autocomplete_controller->done());
227 // First result should be to invoke the keyword with what we typed, 2-4
228 // should be to invoke with suggestions from the extension, and the last
229 // should be to search for what we typed.
230 const AutocompleteResult& result = autocomplete_controller->result();
231 ASSERT_EQ(5U, result.size()) << AutocompleteResultAsString(result);
232 ASSERT_FALSE(result.match_at(0).keyword.empty());
233 EXPECT_EQ(ASCIIToUTF16("keyword suggestion3 incognito"),
234 result.match_at(3).fill_into_edit);
237 // Test that our input is sent to the incognito context. The test will do a
238 // text comparison and succeed only if "command incognito" is sent to the
239 // incognito context.
241 ResultCatcher catcher;
242 autocomplete_controller->Start(
243 AutocompleteInput(ASCIIToUTF16("keyword command incognito"),
244 base::string16::npos, base::string16(), GURL(),
245 AutocompleteInput::NTP, true, false, true, true));
246 location_bar->AcceptInput();
247 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();