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