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 "components/omnibox/autocomplete_provider.h"
8 #include "base/command_line.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/strings/string16.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/string_util.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "chrome/browser/autocomplete/autocomplete_controller.h"
16 #include "chrome/browser/autocomplete/chrome_autocomplete_scheme_classifier.h"
17 #include "chrome/browser/chrome_notification_types.h"
18 #include "chrome/browser/search_engines/template_url_service_factory.h"
19 #include "chrome/test/base/testing_browser_process.h"
20 #include "chrome/test/base/testing_profile.h"
21 #include "components/metrics/proto/omnibox_event.pb.h"
22 #include "components/omnibox/autocomplete_input.h"
23 #include "components/omnibox/autocomplete_match.h"
24 #include "components/omnibox/autocomplete_provider_listener.h"
25 #include "components/omnibox/keyword_provider.h"
26 #include "components/omnibox/search_provider.h"
27 #include "components/search_engines/search_engines_switches.h"
28 #include "components/search_engines/template_url.h"
29 #include "components/search_engines/template_url_service.h"
30 #include "content/public/browser/notification_observer.h"
31 #include "content/public/browser/notification_registrar.h"
32 #include "content/public/browser/notification_source.h"
33 #include "testing/gtest/include/gtest/gtest.h"
35 static std::ostream
& operator<<(std::ostream
& os
,
36 const AutocompleteResult::const_iterator
& it
) {
37 return os
<< static_cast<const AutocompleteMatch
*>(&(*it
));
41 const size_t kResultsPerProvider
= 3;
42 const char kTestTemplateURLKeyword
[] = "t";
45 // Autocomplete provider that provides known results. Note that this is
46 // refcounted so that it can also be a task on the message loop.
47 class TestProvider
: public AutocompleteProvider
{
49 TestProvider(int relevance
, const base::string16
& prefix
,
51 const base::string16 match_keyword
)
52 : AutocompleteProvider(AutocompleteProvider::TYPE_SEARCH
),
55 relevance_(relevance
),
57 match_keyword_(match_keyword
) {
60 void Start(const AutocompleteInput
& input
,
62 bool called_due_to_focus
) override
;
64 void set_listener(AutocompleteProviderListener
* listener
) {
69 ~TestProvider() override
{}
73 void AddResults(int start_at
, int num
);
74 void AddResultsWithSearchTermsArgs(
77 AutocompleteMatch::Type type
,
78 const TemplateURLRef::SearchTermsArgs
& search_terms_args
);
80 AutocompleteProviderListener
* listener_
;
83 const base::string16 prefix_
;
84 const base::string16 match_keyword_
;
87 void TestProvider::Start(const AutocompleteInput
& input
,
89 bool called_due_to_focus
) {
95 if (called_due_to_focus
)
98 // Generate 4 results synchronously, the rest later.
100 AddResultsWithSearchTermsArgs(
101 1, 1, AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED
,
102 TemplateURLRef::SearchTermsArgs(base::ASCIIToUTF16("echo")));
103 AddResultsWithSearchTermsArgs(
104 2, 1, AutocompleteMatchType::NAVSUGGEST
,
105 TemplateURLRef::SearchTermsArgs(base::ASCIIToUTF16("nav")));
106 AddResultsWithSearchTermsArgs(
107 3, 1, AutocompleteMatchType::SEARCH_SUGGEST
,
108 TemplateURLRef::SearchTermsArgs(base::ASCIIToUTF16("query")));
110 if (input
.want_asynchronous_matches()) {
112 base::MessageLoop::current()->PostTask(
113 FROM_HERE
, base::Bind(&TestProvider::Run
, this));
117 void TestProvider::Run() {
118 DCHECK_GT(kResultsPerProvider
, 0U);
119 AddResults(1, kResultsPerProvider
);
122 listener_
->OnProviderUpdate(true);
125 void TestProvider::AddResults(int start_at
, int num
) {
126 AddResultsWithSearchTermsArgs(start_at
,
128 AutocompleteMatchType::URL_WHAT_YOU_TYPED
,
129 TemplateURLRef::SearchTermsArgs(
133 void TestProvider::AddResultsWithSearchTermsArgs(
136 AutocompleteMatch::Type type
,
137 const TemplateURLRef::SearchTermsArgs
& search_terms_args
) {
138 for (int i
= start_at
; i
< num
; i
++) {
139 AutocompleteMatch
match(this, relevance_
- i
, false, type
);
141 match
.fill_into_edit
= prefix_
+ base::UTF8ToUTF16(base::IntToString(i
));
142 match
.destination_url
= GURL(base::UTF16ToUTF8(match
.fill_into_edit
));
143 match
.allowed_to_be_default_match
= true;
145 match
.contents
= match
.fill_into_edit
;
146 match
.contents_class
.push_back(
147 ACMatchClassification(0, ACMatchClassification::NONE
));
148 match
.description
= match
.fill_into_edit
;
149 match
.description_class
.push_back(
150 ACMatchClassification(0, ACMatchClassification::NONE
));
151 match
.search_terms_args
.reset(
152 new TemplateURLRef::SearchTermsArgs(search_terms_args
));
153 if (!match_keyword_
.empty()) {
154 match
.keyword
= match_keyword_
;
155 TemplateURLService
* service
=
156 TemplateURLServiceFactory::GetForProfile(profile_
);
157 ASSERT_TRUE(match
.GetTemplateURL(service
, false) != NULL
);
160 matches_
.push_back(match
);
164 class AutocompleteProviderTest
: public testing::Test
,
165 public content::NotificationObserver
{
167 struct KeywordTestData
{
168 const base::string16 fill_into_edit
;
169 const base::string16 keyword
;
170 const base::string16 expected_associated_keyword
;
173 struct AssistedQueryStatsTestData
{
174 const AutocompleteMatch::Type match_type
;
175 const std::string expected_aqs
;
179 // Registers a test TemplateURL under the given keyword.
180 void RegisterTemplateURL(const base::string16 keyword
,
181 const std::string
& template_url
);
183 // Resets |controller_| with two TestProviders. |provider1_ptr| and
184 // |provider2_ptr| are updated to point to the new providers if non-NULL.
185 void ResetControllerWithTestProviders(bool same_destinations
,
186 TestProvider
** provider1_ptr
,
187 TestProvider
** provider2_ptr
);
189 // Runs a query on the input "a", and makes sure both providers' input is
190 // properly collected.
193 // Constructs an AutocompleteResult from |match_data|, sets the |controller_|
194 // to pretend it was running against input |input|, calls the |controller_|'s
195 // UpdateAssociatedKeywords, and checks that the matches have associated
196 // keywords as expected.
197 void RunKeywordTest(const base::string16
& input
,
198 const KeywordTestData
* match_data
,
201 void RunAssistedQueryStatsTest(
202 const AssistedQueryStatsTestData
* aqs_test_data
,
205 void RunQuery(const base::string16 query
);
207 void ResetControllerWithKeywordAndSearchProviders();
208 void ResetControllerWithKeywordProvider();
209 void RunExactKeymatchTest(bool allow_exact_keyword_match
);
213 // Returns match.destination_url as it would be set by
214 // AutocompleteController::UpdateMatchDestinationURL().
215 GURL
GetDestinationURL(AutocompleteMatch match
,
216 base::TimeDelta query_formulation_time
) const;
218 void set_search_provider_field_trial_triggered_in_session(bool val
) {
219 controller_
->search_provider_
->field_trial_triggered_in_session_
= val
;
221 bool search_provider_field_trial_triggered_in_session() {
222 return controller_
->search_provider_
->field_trial_triggered_in_session();
224 void set_current_page_classification(
225 metrics::OmniboxEventProto::PageClassification classification
) {
226 controller_
->input_
.current_page_classification_
= classification
;
229 AutocompleteResult result_
;
232 // content::NotificationObserver:
233 void Observe(int type
,
234 const content::NotificationSource
& source
,
235 const content::NotificationDetails
& details
) override
;
237 base::MessageLoopForUI message_loop_
;
238 content::NotificationRegistrar registrar_
;
239 TestingProfile profile_
;
240 scoped_ptr
<AutocompleteController
> controller_
;
243 void AutocompleteProviderTest::RegisterTemplateURL(
244 const base::string16 keyword
,
245 const std::string
& template_url
) {
246 if (TemplateURLServiceFactory::GetForProfile(&profile_
) == NULL
) {
247 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
248 &profile_
, &TemplateURLServiceFactory::BuildInstanceFor
);
250 TemplateURLData data
;
251 data
.SetURL(template_url
);
252 data
.SetKeyword(keyword
);
253 TemplateURL
* default_t_url
= new TemplateURL(data
);
254 TemplateURLService
* turl_model
=
255 TemplateURLServiceFactory::GetForProfile(&profile_
);
256 turl_model
->Add(default_t_url
);
257 turl_model
->SetUserSelectedDefaultSearchProvider(default_t_url
);
259 TemplateURLID default_provider_id
= default_t_url
->id();
260 ASSERT_NE(0, default_provider_id
);
263 void AutocompleteProviderTest::ResetControllerWithTestProviders(
264 bool same_destinations
,
265 TestProvider
** provider1_ptr
,
266 TestProvider
** provider2_ptr
) {
267 // TODO: Move it outside this method, after refactoring the existing
268 // unit tests. Specifically:
269 // (1) Make sure that AutocompleteMatch.keyword is set iff there is
270 // a corresponding call to RegisterTemplateURL; otherwise the
271 // controller flow will crash; this practically means that
272 // RunTests/ResetControllerXXX/RegisterTemplateURL should
273 // be coordinated with each other.
274 // (2) Inject test arguments rather than rely on the hardcoded values, e.g.
275 // don't rely on kResultsPerProvided and default relevance ordering
277 RegisterTemplateURL(base::ASCIIToUTF16(kTestTemplateURLKeyword
),
278 "http://aqs/{searchTerms}/{google:assistedQueryStats}");
280 AutocompleteController::Providers providers
;
282 // Construct two new providers, with either the same or different prefixes.
283 TestProvider
* provider1
= new TestProvider(
285 base::ASCIIToUTF16("http://a"),
287 base::ASCIIToUTF16(kTestTemplateURLKeyword
));
288 providers
.push_back(provider1
);
290 TestProvider
* provider2
= new TestProvider(
291 kResultsPerProvider
* 2,
292 same_destinations
? base::ASCIIToUTF16("http://a")
293 : base::ASCIIToUTF16("http://b"),
296 providers
.push_back(provider2
);
298 // Reset the controller to contain our new providers.
299 controller_
.reset(new AutocompleteController(
300 &profile_
, TemplateURLServiceFactory::GetForProfile(&profile_
), NULL
, 0));
301 // We're going to swap the providers vector, but the old vector should be
302 // empty so no elements need to be freed at this point.
303 EXPECT_TRUE(controller_
->providers_
.empty());
304 controller_
->providers_
.swap(providers
);
305 provider1
->set_listener(controller_
.get());
306 provider2
->set_listener(controller_
.get());
308 // The providers don't complete synchronously, so listen for "result updated"
311 chrome::NOTIFICATION_AUTOCOMPLETE_CONTROLLER_RESULT_READY
,
312 content::Source
<AutocompleteController
>(controller_
.get()));
315 *provider1_ptr
= provider1
;
317 *provider2_ptr
= provider2
;
320 void AutocompleteProviderTest::
321 ResetControllerWithKeywordAndSearchProviders() {
322 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
323 &profile_
, &TemplateURLServiceFactory::BuildInstanceFor
);
325 // Reset the default TemplateURL.
326 TemplateURLData data
;
327 data
.SetURL("http://defaultturl/{searchTerms}");
328 TemplateURL
* default_t_url
= new TemplateURL(data
);
329 TemplateURLService
* turl_model
=
330 TemplateURLServiceFactory::GetForProfile(&profile_
);
331 turl_model
->Add(default_t_url
);
332 turl_model
->SetUserSelectedDefaultSearchProvider(default_t_url
);
333 TemplateURLID default_provider_id
= default_t_url
->id();
334 ASSERT_NE(0, default_provider_id
);
336 // Create another TemplateURL for KeywordProvider.
337 TemplateURLData data2
;
338 data2
.short_name
= base::ASCIIToUTF16("k");
339 data2
.SetKeyword(base::ASCIIToUTF16("k"));
340 data2
.SetURL("http://keyword/{searchTerms}");
341 TemplateURL
* keyword_t_url
= new TemplateURL(data2
);
342 turl_model
->Add(keyword_t_url
);
343 ASSERT_NE(0, keyword_t_url
->id());
345 controller_
.reset(new AutocompleteController(
346 &profile_
, TemplateURLServiceFactory::GetForProfile(&profile_
), NULL
,
347 AutocompleteProvider::TYPE_KEYWORD
| AutocompleteProvider::TYPE_SEARCH
));
350 void AutocompleteProviderTest::ResetControllerWithKeywordProvider() {
351 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
352 &profile_
, &TemplateURLServiceFactory::BuildInstanceFor
);
354 TemplateURLService
* turl_model
=
355 TemplateURLServiceFactory::GetForProfile(&profile_
);
357 // Create a TemplateURL for KeywordProvider.
358 TemplateURLData data
;
359 data
.short_name
= base::ASCIIToUTF16("foo.com");
360 data
.SetKeyword(base::ASCIIToUTF16("foo.com"));
361 data
.SetURL("http://foo.com/{searchTerms}");
362 TemplateURL
* keyword_t_url
= new TemplateURL(data
);
363 turl_model
->Add(keyword_t_url
);
364 ASSERT_NE(0, keyword_t_url
->id());
366 // Make a TemplateURL for KeywordProvider that a shorter version of the
368 data
.short_name
= base::ASCIIToUTF16("f");
369 data
.SetKeyword(base::ASCIIToUTF16("f"));
370 data
.SetURL("http://f.com/{searchTerms}");
371 keyword_t_url
= new TemplateURL(data
);
372 turl_model
->Add(keyword_t_url
);
373 ASSERT_NE(0, keyword_t_url
->id());
375 // Create another TemplateURL for KeywordProvider.
376 data
.short_name
= base::ASCIIToUTF16("bar.com");
377 data
.SetKeyword(base::ASCIIToUTF16("bar.com"));
378 data
.SetURL("http://bar.com/{searchTerms}");
379 keyword_t_url
= new TemplateURL(data
);
380 turl_model
->Add(keyword_t_url
);
381 ASSERT_NE(0, keyword_t_url
->id());
383 controller_
.reset(new AutocompleteController(
384 &profile_
, TemplateURLServiceFactory::GetForProfile(&profile_
), NULL
,
385 AutocompleteProvider::TYPE_KEYWORD
));
388 void AutocompleteProviderTest::RunTest() {
389 RunQuery(base::ASCIIToUTF16("a"));
392 void AutocompleteProviderTest::RunKeywordTest(const base::string16
& input
,
393 const KeywordTestData
* match_data
,
396 for (size_t i
= 0; i
< size
; ++i
) {
397 AutocompleteMatch match
;
398 match
.relevance
= 1000; // Arbitrary non-zero value.
399 match
.allowed_to_be_default_match
= true;
400 match
.fill_into_edit
= match_data
[i
].fill_into_edit
;
401 match
.transition
= ui::PAGE_TRANSITION_KEYWORD
;
402 match
.keyword
= match_data
[i
].keyword
;
403 matches
.push_back(match
);
406 AutocompleteResult result
;
407 result
.AppendMatches(matches
);
408 controller_
->input_
= AutocompleteInput(
409 input
, base::string16::npos
, std::string(), GURL(),
410 metrics::OmniboxEventProto::INSTANT_NTP_WITH_OMNIBOX_AS_STARTING_FOCUS
,
411 false, true, true, true, ChromeAutocompleteSchemeClassifier(&profile_
));
412 controller_
->UpdateAssociatedKeywords(&result
);
414 for (size_t j
= 0; j
< result
.size(); ++j
) {
415 EXPECT_EQ(match_data
[j
].expected_associated_keyword
,
416 result
.match_at(j
)->associated_keyword
.get() ?
417 result
.match_at(j
)->associated_keyword
->keyword
:
422 void AutocompleteProviderTest::RunAssistedQueryStatsTest(
423 const AssistedQueryStatsTestData
* aqs_test_data
,
426 const size_t kMaxRelevance
= 1000;
428 for (size_t i
= 0; i
< size
; ++i
) {
429 AutocompleteMatch
match(NULL
, kMaxRelevance
- i
, false,
430 aqs_test_data
[i
].match_type
);
431 match
.allowed_to_be_default_match
= true;
432 match
.keyword
= base::ASCIIToUTF16(kTestTemplateURLKeyword
);
433 match
.search_terms_args
.reset(
434 new TemplateURLRef::SearchTermsArgs(base::string16()));
435 matches
.push_back(match
);
438 result_
.AppendMatches(matches
);
441 controller_
->UpdateAssistedQueryStats(&result_
);
444 for (size_t i
= 0; i
< size
; ++i
) {
445 EXPECT_EQ(aqs_test_data
[i
].expected_aqs
,
446 result_
.match_at(i
)->search_terms_args
->assisted_query_stats
);
450 void AutocompleteProviderTest::RunQuery(const base::string16 query
) {
452 controller_
->Start(AutocompleteInput(
453 query
, base::string16::npos
, std::string(), GURL(),
454 metrics::OmniboxEventProto::INVALID_SPEC
, true, false, true, true,
455 ChromeAutocompleteSchemeClassifier(&profile_
)));
457 if (!controller_
->done())
458 // The message loop will terminate when all autocomplete input has been
460 base::MessageLoop::current()->Run();
463 void AutocompleteProviderTest::RunExactKeymatchTest(
464 bool allow_exact_keyword_match
) {
465 // Send the controller input which exactly matches the keyword provider we
466 // created in ResetControllerWithKeywordAndSearchProviders(). The default
467 // match should thus be a search-other-engine match iff
468 // |allow_exact_keyword_match| is true. Regardless, the match should
469 // be from SearchProvider. (It provides all verbatim search matches,
471 controller_
->Start(AutocompleteInput(
472 base::ASCIIToUTF16("k test"), base::string16::npos
, std::string(), GURL(),
473 metrics::OmniboxEventProto::INVALID_SPEC
, true, false,
474 allow_exact_keyword_match
, false,
475 ChromeAutocompleteSchemeClassifier(&profile_
)));
476 EXPECT_TRUE(controller_
->done());
477 EXPECT_EQ(AutocompleteProvider::TYPE_SEARCH
,
478 controller_
->result().default_match()->provider
->type());
479 EXPECT_EQ(allow_exact_keyword_match
?
480 AutocompleteMatchType::SEARCH_OTHER_ENGINE
:
481 AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED
,
482 controller_
->result().default_match()->type
);
485 void AutocompleteProviderTest::CopyResults() {
486 result_
.CopyFrom(controller_
->result());
489 GURL
AutocompleteProviderTest::GetDestinationURL(
490 AutocompleteMatch match
,
491 base::TimeDelta query_formulation_time
) const {
492 controller_
->UpdateMatchDestinationURLWithQueryFormulationTime(
493 query_formulation_time
, &match
);
494 return match
.destination_url
;
497 void AutocompleteProviderTest::Observe(
499 const content::NotificationSource
& source
,
500 const content::NotificationDetails
& details
) {
501 if (controller_
->done()) {
503 base::MessageLoop::current()->Quit();
507 // Tests that the default selection is set properly when updating results.
508 TEST_F(AutocompleteProviderTest
, Query
) {
509 TestProvider
* provider1
= NULL
;
510 TestProvider
* provider2
= NULL
;
511 ResetControllerWithTestProviders(false, &provider1
, &provider2
);
514 // Make sure the default match gets set to the highest relevance match. The
515 // highest relevance matches should come from the second provider.
516 EXPECT_EQ(kResultsPerProvider
* 2, result_
.size());
517 ASSERT_NE(result_
.end(), result_
.default_match());
518 EXPECT_EQ(provider2
, result_
.default_match()->provider
);
521 // Tests assisted query stats.
522 TEST_F(AutocompleteProviderTest
, AssistedQueryStats
) {
523 ResetControllerWithTestProviders(false, NULL
, NULL
);
526 ASSERT_EQ(kResultsPerProvider
* 2, result_
.size());
528 // Now, check the results from the second provider, as they should not have
529 // assisted query stats set.
530 for (size_t i
= 0; i
< kResultsPerProvider
; ++i
) {
532 result_
.match_at(i
)->search_terms_args
->assisted_query_stats
.empty());
534 // The first provider has a test keyword, so AQS should be non-empty.
535 for (size_t i
= kResultsPerProvider
; i
< kResultsPerProvider
* 2; ++i
) {
537 result_
.match_at(i
)->search_terms_args
->assisted_query_stats
.empty());
541 TEST_F(AutocompleteProviderTest
, RemoveDuplicates
) {
542 TestProvider
* provider1
= NULL
;
543 TestProvider
* provider2
= NULL
;
544 ResetControllerWithTestProviders(true, &provider1
, &provider2
);
547 // Make sure all the first provider's results were eliminated by the second
549 EXPECT_EQ(kResultsPerProvider
, result_
.size());
550 for (AutocompleteResult::const_iterator
i(result_
.begin());
551 i
!= result_
.end(); ++i
)
552 EXPECT_EQ(provider2
, i
->provider
);
555 TEST_F(AutocompleteProviderTest
, AllowExactKeywordMatch
) {
556 ResetControllerWithKeywordAndSearchProviders();
557 RunExactKeymatchTest(true);
558 RunExactKeymatchTest(false);
561 // Ensures matches from (only) the default search provider respect any extra
562 // query params set on the command line.
563 TEST_F(AutocompleteProviderTest
, ExtraQueryParams
) {
564 ResetControllerWithKeywordAndSearchProviders();
565 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
566 switches::kExtraSearchQueryParams
, "a=b");
567 RunExactKeymatchTest(true);
569 ASSERT_EQ(2U, result_
.size());
570 EXPECT_EQ("http://keyword/test",
571 result_
.match_at(0)->destination_url
.possibly_invalid_spec());
572 EXPECT_EQ("http://defaultturl/k%20test?a=b",
573 result_
.match_at(1)->destination_url
.possibly_invalid_spec());
576 // Test that redundant associated keywords are removed.
577 TEST_F(AutocompleteProviderTest
, RedundantKeywordsIgnoredInResult
) {
578 ResetControllerWithKeywordProvider();
581 KeywordTestData duplicate_url
[] = {
582 { base::ASCIIToUTF16("fo"), base::string16(), base::string16() },
583 { base::ASCIIToUTF16("foo.com"), base::string16(),
584 base::ASCIIToUTF16("foo.com") },
585 { base::ASCIIToUTF16("foo.com"), base::string16(), base::string16() }
588 SCOPED_TRACE("Duplicate url");
589 RunKeywordTest(base::ASCIIToUTF16("fo"), duplicate_url
,
590 arraysize(duplicate_url
));
594 KeywordTestData keyword_match
[] = {
595 { base::ASCIIToUTF16("foo.com"), base::ASCIIToUTF16("foo.com"),
597 { base::ASCIIToUTF16("foo.com"), base::string16(), base::string16() }
600 SCOPED_TRACE("Duplicate url with keyword match");
601 RunKeywordTest(base::ASCIIToUTF16("fo"), keyword_match
,
602 arraysize(keyword_match
));
606 KeywordTestData multiple_keyword
[] = {
607 { base::ASCIIToUTF16("fo"), base::string16(), base::string16() },
608 { base::ASCIIToUTF16("foo.com"), base::string16(),
609 base::ASCIIToUTF16("foo.com") },
610 { base::ASCIIToUTF16("foo.com"), base::string16(), base::string16() },
611 { base::ASCIIToUTF16("bar.com"), base::string16(),
612 base::ASCIIToUTF16("bar.com") },
615 SCOPED_TRACE("Duplicate url with multiple keywords");
616 RunKeywordTest(base::ASCIIToUTF16("fo"), multiple_keyword
,
617 arraysize(multiple_keyword
));
621 // Test that exact match keywords trump keywords associated with
623 TEST_F(AutocompleteProviderTest
, ExactMatchKeywords
) {
624 ResetControllerWithKeywordProvider();
627 KeywordTestData keyword_match
[] = {
628 { base::ASCIIToUTF16("foo.com"), base::string16(),
629 base::ASCIIToUTF16("foo.com") }
632 SCOPED_TRACE("keyword match as usual");
633 RunKeywordTest(base::ASCIIToUTF16("fo"), keyword_match
,
634 arraysize(keyword_match
));
637 // The same result set with an input of "f" (versus "fo") should get
638 // a different associated keyword because "f" is an exact match for
639 // a keyword and that should trump the keyword normally associated with
642 KeywordTestData keyword_match
[] = {
643 { base::ASCIIToUTF16("foo.com"), base::string16(),
644 base::ASCIIToUTF16("f") }
647 SCOPED_TRACE("keyword exact match");
648 RunKeywordTest(base::ASCIIToUTF16("f"), keyword_match
,
649 arraysize(keyword_match
));
653 TEST_F(AutocompleteProviderTest
, UpdateAssistedQueryStats
) {
654 ResetControllerWithTestProviders(false, NULL
, NULL
);
657 AssistedQueryStatsTestData test_data
[] = {
658 // MSVC doesn't support zero-length arrays, so supply some dummy data.
659 { AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED
, "" }
661 SCOPED_TRACE("No matches");
662 // Note: We pass 0 here to ignore the dummy data above.
663 RunAssistedQueryStatsTest(test_data
, 0);
667 AssistedQueryStatsTestData test_data
[] = {
668 { AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED
, "chrome..69i57" }
670 SCOPED_TRACE("One match");
671 RunAssistedQueryStatsTest(test_data
, arraysize(test_data
));
675 AssistedQueryStatsTestData test_data
[] = {
676 { AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED
,
677 "chrome..69i57j69i58j5l2j0l3j69i59" },
678 { AutocompleteMatchType::URL_WHAT_YOU_TYPED
,
679 "chrome..69i57j69i58j5l2j0l3j69i59" },
680 { AutocompleteMatchType::NAVSUGGEST
,
681 "chrome.2.69i57j69i58j5l2j0l3j69i59" },
682 { AutocompleteMatchType::NAVSUGGEST
,
683 "chrome.3.69i57j69i58j5l2j0l3j69i59" },
684 { AutocompleteMatchType::SEARCH_SUGGEST
,
685 "chrome.4.69i57j69i58j5l2j0l3j69i59" },
686 { AutocompleteMatchType::SEARCH_SUGGEST
,
687 "chrome.5.69i57j69i58j5l2j0l3j69i59" },
688 { AutocompleteMatchType::SEARCH_SUGGEST
,
689 "chrome.6.69i57j69i58j5l2j0l3j69i59" },
690 { AutocompleteMatchType::SEARCH_HISTORY
,
691 "chrome.7.69i57j69i58j5l2j0l3j69i59" },
693 SCOPED_TRACE("Multiple matches");
694 RunAssistedQueryStatsTest(test_data
, arraysize(test_data
));
698 TEST_F(AutocompleteProviderTest
, GetDestinationURL
) {
699 ResetControllerWithKeywordAndSearchProviders();
701 // For the destination URL to have aqs parameters for query formulation time
702 // and the field trial triggered bit, many conditions need to be satisfied.
703 AutocompleteMatch
match(NULL
, 1100, false,
704 AutocompleteMatchType::SEARCH_SUGGEST
);
705 GURL
url(GetDestinationURL(match
, base::TimeDelta::FromMilliseconds(2456)));
706 EXPECT_TRUE(url
.path().empty());
708 // The protocol needs to be https.
709 RegisterTemplateURL(base::ASCIIToUTF16(kTestTemplateURLKeyword
),
710 "https://aqs/{searchTerms}/{google:assistedQueryStats}");
711 url
= GetDestinationURL(match
, base::TimeDelta::FromMilliseconds(2456));
712 EXPECT_TRUE(url
.path().empty());
714 // There needs to be a keyword provider.
715 match
.keyword
= base::ASCIIToUTF16(kTestTemplateURLKeyword
);
716 url
= GetDestinationURL(match
, base::TimeDelta::FromMilliseconds(2456));
717 EXPECT_TRUE(url
.path().empty());
719 // search_terms_args needs to be set.
720 match
.search_terms_args
.reset(
721 new TemplateURLRef::SearchTermsArgs(base::string16()));
722 url
= GetDestinationURL(match
, base::TimeDelta::FromMilliseconds(2456));
723 EXPECT_TRUE(url
.path().empty());
725 // assisted_query_stats needs to have been previously set.
726 match
.search_terms_args
->assisted_query_stats
=
727 "chrome.0.69i57j69i58j5l2j0l3j69i59";
728 url
= GetDestinationURL(match
, base::TimeDelta::FromMilliseconds(2456));
729 EXPECT_EQ("//aqs=chrome.0.69i57j69i58j5l2j0l3j69i59.2456j0j0&", url
.path());
731 // Test field trial triggered bit set.
732 set_search_provider_field_trial_triggered_in_session(true);
733 EXPECT_TRUE(search_provider_field_trial_triggered_in_session());
734 url
= GetDestinationURL(match
, base::TimeDelta::FromMilliseconds(2456));
735 EXPECT_EQ("//aqs=chrome.0.69i57j69i58j5l2j0l3j69i59.2456j1j0&", url
.path());
737 // Test page classification set.
738 set_current_page_classification(metrics::OmniboxEventProto::OTHER
);
739 set_search_provider_field_trial_triggered_in_session(false);
740 EXPECT_FALSE(search_provider_field_trial_triggered_in_session());
741 url
= GetDestinationURL(match
, base::TimeDelta::FromMilliseconds(2456));
742 EXPECT_EQ("//aqs=chrome.0.69i57j69i58j5l2j0l3j69i59.2456j0j4&", url
.path());
744 // Test page classification and field trial triggered set.
745 set_search_provider_field_trial_triggered_in_session(true);
746 EXPECT_TRUE(search_provider_field_trial_triggered_in_session());
747 url
= GetDestinationURL(match
, base::TimeDelta::FromMilliseconds(2456));
748 EXPECT_EQ("//aqs=chrome.0.69i57j69i58j5l2j0l3j69i59.2456j1j4&", url
.path());