OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/autocomplete/autocomplete_provider.h" | 5 #include "components/autocomplete/autocomplete_provider.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 matches_.push_back(match); | 155 matches_.push_back(match); |
156 } | 156 } |
157 } | 157 } |
158 | 158 |
159 class AutocompleteProviderTest : public testing::Test, | 159 class AutocompleteProviderTest : public testing::Test, |
160 public content::NotificationObserver { | 160 public content::NotificationObserver { |
161 protected: | 161 protected: |
162 struct KeywordTestData { | 162 struct KeywordTestData { |
163 const base::string16 fill_into_edit; | 163 const base::string16 fill_into_edit; |
164 const base::string16 keyword; | 164 const base::string16 keyword; |
165 const bool expected_keyword_result; | 165 const base::string16 expected_associated_keyword; |
166 }; | 166 }; |
167 | 167 |
168 struct AssistedQueryStatsTestData { | 168 struct AssistedQueryStatsTestData { |
169 const AutocompleteMatch::Type match_type; | 169 const AutocompleteMatch::Type match_type; |
170 const std::string expected_aqs; | 170 const std::string expected_aqs; |
171 }; | 171 }; |
172 | 172 |
173 protected: | 173 protected: |
174 // Registers a test TemplateURL under the given keyword. | 174 // Registers a test TemplateURL under the given keyword. |
175 void RegisterTemplateURL(const base::string16 keyword, | 175 void RegisterTemplateURL(const base::string16 keyword, |
176 const std::string& template_url); | 176 const std::string& template_url); |
177 | 177 |
178 // Resets |controller_| with two TestProviders. |provider1_ptr| and | 178 // Resets |controller_| with two TestProviders. |provider1_ptr| and |
179 // |provider2_ptr| are updated to point to the new providers if non-NULL. | 179 // |provider2_ptr| are updated to point to the new providers if non-NULL. |
180 void ResetControllerWithTestProviders(bool same_destinations, | 180 void ResetControllerWithTestProviders(bool same_destinations, |
181 TestProvider** provider1_ptr, | 181 TestProvider** provider1_ptr, |
182 TestProvider** provider2_ptr); | 182 TestProvider** provider2_ptr); |
183 | 183 |
184 // Runs a query on the input "a", and makes sure both providers' input is | 184 // Runs a query on the input "a", and makes sure both providers' input is |
185 // properly collected. | 185 // properly collected. |
186 void RunTest(); | 186 void RunTest(); |
187 | 187 |
188 void RunRedundantKeywordTest(const KeywordTestData* match_data, size_t size); | 188 // Constructs an AutocompleteResult from |match_data|, sets the |controller_| |
| 189 // to pretend it was running against input |input|, calls the |controller_|'s |
| 190 // UpdateAssociatedKeywords, and checks that the matches have associated |
| 191 // keywords as expected. |
| 192 void RunKeywordTest(const base::string16& input, |
| 193 const KeywordTestData* match_data, |
| 194 size_t size); |
189 | 195 |
190 void RunAssistedQueryStatsTest( | 196 void RunAssistedQueryStatsTest( |
191 const AssistedQueryStatsTestData* aqs_test_data, | 197 const AssistedQueryStatsTestData* aqs_test_data, |
192 size_t size); | 198 size_t size); |
193 | 199 |
194 void RunQuery(const base::string16 query); | 200 void RunQuery(const base::string16 query); |
195 | 201 |
196 void ResetControllerWithKeywordAndSearchProviders(); | 202 void ResetControllerWithKeywordAndSearchProviders(); |
197 void ResetControllerWithKeywordProvider(); | 203 void ResetControllerWithKeywordProvider(); |
198 void RunExactKeymatchTest(bool allow_exact_keyword_match); | 204 void RunExactKeymatchTest(bool allow_exact_keyword_match); |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
334 | 340 |
335 // Create a TemplateURL for KeywordProvider. | 341 // Create a TemplateURL for KeywordProvider. |
336 TemplateURLData data; | 342 TemplateURLData data; |
337 data.short_name = base::ASCIIToUTF16("foo.com"); | 343 data.short_name = base::ASCIIToUTF16("foo.com"); |
338 data.SetKeyword(base::ASCIIToUTF16("foo.com")); | 344 data.SetKeyword(base::ASCIIToUTF16("foo.com")); |
339 data.SetURL("http://foo.com/{searchTerms}"); | 345 data.SetURL("http://foo.com/{searchTerms}"); |
340 TemplateURL* keyword_t_url = new TemplateURL(data); | 346 TemplateURL* keyword_t_url = new TemplateURL(data); |
341 turl_model->Add(keyword_t_url); | 347 turl_model->Add(keyword_t_url); |
342 ASSERT_NE(0, keyword_t_url->id()); | 348 ASSERT_NE(0, keyword_t_url->id()); |
343 | 349 |
| 350 // Make a TemplateURL for KeywordProvider that a shorter version of the |
| 351 // first. |
| 352 data.short_name = base::ASCIIToUTF16("f"); |
| 353 data.SetKeyword(base::ASCIIToUTF16("f")); |
| 354 data.SetURL("http://f.com/{searchTerms}"); |
| 355 keyword_t_url = new TemplateURL(data); |
| 356 turl_model->Add(keyword_t_url); |
| 357 ASSERT_NE(0, keyword_t_url->id()); |
| 358 |
344 // Create another TemplateURL for KeywordProvider. | 359 // Create another TemplateURL for KeywordProvider. |
345 data.short_name = base::ASCIIToUTF16("bar.com"); | 360 data.short_name = base::ASCIIToUTF16("bar.com"); |
346 data.SetKeyword(base::ASCIIToUTF16("bar.com")); | 361 data.SetKeyword(base::ASCIIToUTF16("bar.com")); |
347 data.SetURL("http://bar.com/{searchTerms}"); | 362 data.SetURL("http://bar.com/{searchTerms}"); |
348 keyword_t_url = new TemplateURL(data); | 363 keyword_t_url = new TemplateURL(data); |
349 turl_model->Add(keyword_t_url); | 364 turl_model->Add(keyword_t_url); |
350 ASSERT_NE(0, keyword_t_url->id()); | 365 ASSERT_NE(0, keyword_t_url->id()); |
351 | 366 |
352 controller_.reset(new AutocompleteController( | 367 controller_.reset(new AutocompleteController( |
353 &profile_, TemplateURLServiceFactory::GetForProfile(&profile_), NULL, | 368 &profile_, TemplateURLServiceFactory::GetForProfile(&profile_), NULL, |
354 AutocompleteProvider::TYPE_KEYWORD)); | 369 AutocompleteProvider::TYPE_KEYWORD)); |
355 } | 370 } |
356 | 371 |
357 void AutocompleteProviderTest::RunTest() { | 372 void AutocompleteProviderTest::RunTest() { |
358 RunQuery(base::ASCIIToUTF16("a")); | 373 RunQuery(base::ASCIIToUTF16("a")); |
359 } | 374 } |
360 | 375 |
361 void AutocompleteProviderTest::RunRedundantKeywordTest( | 376 void AutocompleteProviderTest::RunKeywordTest(const base::string16& input, |
362 const KeywordTestData* match_data, | 377 const KeywordTestData* match_data, |
363 size_t size) { | 378 size_t size) { |
364 ACMatches matches; | 379 ACMatches matches; |
365 for (size_t i = 0; i < size; ++i) { | 380 for (size_t i = 0; i < size; ++i) { |
366 AutocompleteMatch match; | 381 AutocompleteMatch match; |
367 match.relevance = 1000; // Arbitrary non-zero value. | 382 match.relevance = 1000; // Arbitrary non-zero value. |
368 match.allowed_to_be_default_match = true; | 383 match.allowed_to_be_default_match = true; |
369 match.fill_into_edit = match_data[i].fill_into_edit; | 384 match.fill_into_edit = match_data[i].fill_into_edit; |
370 match.transition = content::PAGE_TRANSITION_KEYWORD; | 385 match.transition = content::PAGE_TRANSITION_KEYWORD; |
371 match.keyword = match_data[i].keyword; | 386 match.keyword = match_data[i].keyword; |
372 matches.push_back(match); | 387 matches.push_back(match); |
373 } | 388 } |
374 | 389 |
375 AutocompleteResult result; | 390 AutocompleteResult result; |
376 result.AppendMatches(matches); | 391 result.AppendMatches(matches); |
| 392 controller_->input_ = AutocompleteInput( |
| 393 input, base::string16::npos, base::string16(), GURL(), |
| 394 metrics::OmniboxEventProto::INSTANT_NTP_WITH_OMNIBOX_AS_STARTING_FOCUS, |
| 395 false, true, true, true, ChromeAutocompleteSchemeClassifier(&profile_)); |
377 controller_->UpdateAssociatedKeywords(&result); | 396 controller_->UpdateAssociatedKeywords(&result); |
378 | 397 |
379 for (size_t j = 0; j < result.size(); ++j) { | 398 for (size_t j = 0; j < result.size(); ++j) { |
380 EXPECT_EQ(match_data[j].expected_keyword_result, | 399 EXPECT_EQ(match_data[j].expected_associated_keyword, |
381 result.match_at(j)->associated_keyword.get() != NULL); | 400 result.match_at(j)->associated_keyword.get() ? |
| 401 result.match_at(j)->associated_keyword->keyword : |
| 402 base::string16()); |
382 } | 403 } |
383 } | 404 } |
384 | 405 |
385 void AutocompleteProviderTest::RunAssistedQueryStatsTest( | 406 void AutocompleteProviderTest::RunAssistedQueryStatsTest( |
386 const AssistedQueryStatsTestData* aqs_test_data, | 407 const AssistedQueryStatsTestData* aqs_test_data, |
387 size_t size) { | 408 size_t size) { |
388 // Prepare input. | 409 // Prepare input. |
389 const size_t kMaxRelevance = 1000; | 410 const size_t kMaxRelevance = 1000; |
390 ACMatches matches; | 411 ACMatches matches; |
391 for (size_t i = 0; i < size; ++i) { | 412 for (size_t i = 0; i < size; ++i) { |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
535 EXPECT_EQ("http://defaultturl/k%20test?a=b", | 556 EXPECT_EQ("http://defaultturl/k%20test?a=b", |
536 result_.match_at(1)->destination_url.possibly_invalid_spec()); | 557 result_.match_at(1)->destination_url.possibly_invalid_spec()); |
537 } | 558 } |
538 | 559 |
539 // Test that redundant associated keywords are removed. | 560 // Test that redundant associated keywords are removed. |
540 TEST_F(AutocompleteProviderTest, RedundantKeywordsIgnoredInResult) { | 561 TEST_F(AutocompleteProviderTest, RedundantKeywordsIgnoredInResult) { |
541 ResetControllerWithKeywordProvider(); | 562 ResetControllerWithKeywordProvider(); |
542 | 563 |
543 { | 564 { |
544 KeywordTestData duplicate_url[] = { | 565 KeywordTestData duplicate_url[] = { |
545 { base::ASCIIToUTF16("fo"), base::string16(), false }, | 566 { base::ASCIIToUTF16("fo"), base::string16(), base::string16() }, |
546 { base::ASCIIToUTF16("foo.com"), base::string16(), true }, | 567 { base::ASCIIToUTF16("foo.com"), base::string16(), |
547 { base::ASCIIToUTF16("foo.com"), base::string16(), false } | 568 base::ASCIIToUTF16("foo.com") }, |
| 569 { base::ASCIIToUTF16("foo.com"), base::string16(), base::string16() } |
548 }; | 570 }; |
549 | 571 |
550 SCOPED_TRACE("Duplicate url"); | 572 SCOPED_TRACE("Duplicate url"); |
551 RunRedundantKeywordTest(duplicate_url, ARRAYSIZE_UNSAFE(duplicate_url)); | 573 RunKeywordTest(base::ASCIIToUTF16("fo"), duplicate_url, |
| 574 ARRAYSIZE_UNSAFE(duplicate_url)); |
552 } | 575 } |
553 | 576 |
554 { | 577 { |
555 KeywordTestData keyword_match[] = { | 578 KeywordTestData keyword_match[] = { |
556 { base::ASCIIToUTF16("foo.com"), base::ASCIIToUTF16("foo.com"), false }, | 579 { base::ASCIIToUTF16("foo.com"), base::ASCIIToUTF16("foo.com"), |
557 { base::ASCIIToUTF16("foo.com"), base::string16(), false } | 580 base::string16() }, |
| 581 { base::ASCIIToUTF16("foo.com"), base::string16(), base::string16() } |
558 }; | 582 }; |
559 | 583 |
560 SCOPED_TRACE("Duplicate url with keyword match"); | 584 SCOPED_TRACE("Duplicate url with keyword match"); |
561 RunRedundantKeywordTest(keyword_match, ARRAYSIZE_UNSAFE(keyword_match)); | 585 RunKeywordTest(base::ASCIIToUTF16("fo"), keyword_match, |
| 586 ARRAYSIZE_UNSAFE(keyword_match)); |
562 } | 587 } |
563 | 588 |
564 { | 589 { |
565 KeywordTestData multiple_keyword[] = { | 590 KeywordTestData multiple_keyword[] = { |
566 { base::ASCIIToUTF16("fo"), base::string16(), false }, | 591 { base::ASCIIToUTF16("fo"), base::string16(), base::string16() }, |
567 { base::ASCIIToUTF16("foo.com"), base::string16(), true }, | 592 { base::ASCIIToUTF16("foo.com"), base::string16(), |
568 { base::ASCIIToUTF16("foo.com"), base::string16(), false }, | 593 base::ASCIIToUTF16("foo.com") }, |
569 { base::ASCIIToUTF16("bar.com"), base::string16(), true }, | 594 { base::ASCIIToUTF16("foo.com"), base::string16(), base::string16() }, |
| 595 { base::ASCIIToUTF16("bar.com"), base::string16(), |
| 596 base::ASCIIToUTF16("bar.com") }, |
570 }; | 597 }; |
571 | 598 |
572 SCOPED_TRACE("Duplicate url with multiple keywords"); | 599 SCOPED_TRACE("Duplicate url with multiple keywords"); |
573 RunRedundantKeywordTest(multiple_keyword, | 600 RunKeywordTest(base::ASCIIToUTF16("fo"), multiple_keyword, |
574 ARRAYSIZE_UNSAFE(multiple_keyword)); | 601 ARRAYSIZE_UNSAFE(multiple_keyword)); |
575 } | 602 } |
576 } | 603 } |
577 | 604 |
| 605 // Test that exact match keywords trump keywords associated with |
| 606 // the match. |
| 607 TEST_F(AutocompleteProviderTest, ExactMatchKeywords) { |
| 608 ResetControllerWithKeywordProvider(); |
| 609 |
| 610 { |
| 611 KeywordTestData keyword_match[] = { |
| 612 { base::ASCIIToUTF16("foo.com"), base::string16(), |
| 613 base::ASCIIToUTF16("foo.com") } |
| 614 }; |
| 615 |
| 616 SCOPED_TRACE("keyword match as usual"); |
| 617 RunKeywordTest(base::ASCIIToUTF16("fo"), keyword_match, |
| 618 ARRAYSIZE_UNSAFE(keyword_match)); |
| 619 } |
| 620 |
| 621 // The same result set with an input of "f" (versus "fo") should get |
| 622 // a different associated keyword because "f" is an exact match for |
| 623 // a keyword and that should trump the keyword normally associated with |
| 624 // this match. |
| 625 { |
| 626 KeywordTestData keyword_match[] = { |
| 627 { base::ASCIIToUTF16("foo.com"), base::string16(), |
| 628 base::ASCIIToUTF16("f") } |
| 629 }; |
| 630 |
| 631 SCOPED_TRACE("keyword exact match"); |
| 632 RunKeywordTest(base::ASCIIToUTF16("f"), keyword_match, |
| 633 ARRAYSIZE_UNSAFE(keyword_match)); |
| 634 } |
| 635 } |
| 636 |
578 TEST_F(AutocompleteProviderTest, UpdateAssistedQueryStats) { | 637 TEST_F(AutocompleteProviderTest, UpdateAssistedQueryStats) { |
579 ResetControllerWithTestProviders(false, NULL, NULL); | 638 ResetControllerWithTestProviders(false, NULL, NULL); |
580 | 639 |
581 { | 640 { |
582 AssistedQueryStatsTestData test_data[] = { | 641 AssistedQueryStatsTestData test_data[] = { |
583 // MSVC doesn't support zero-length arrays, so supply some dummy data. | 642 // MSVC doesn't support zero-length arrays, so supply some dummy data. |
584 { AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED, "" } | 643 { AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED, "" } |
585 }; | 644 }; |
586 SCOPED_TRACE("No matches"); | 645 SCOPED_TRACE("No matches"); |
587 // Note: We pass 0 here to ignore the dummy data above. | 646 // Note: We pass 0 here to ignore the dummy data above. |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
669 url = GetDestinationURL(match, base::TimeDelta::FromMilliseconds(2456)); | 728 url = GetDestinationURL(match, base::TimeDelta::FromMilliseconds(2456)); |
670 EXPECT_EQ("//aqs=chrome.0.69i57j69i58j5l2j0l3j69i59.2456j0j4&", url.path()); | 729 EXPECT_EQ("//aqs=chrome.0.69i57j69i58j5l2j0l3j69i59.2456j0j4&", url.path()); |
671 | 730 |
672 // Test page classification and field trial triggered set. | 731 // Test page classification and field trial triggered set. |
673 controller_->search_provider_->field_trial_triggered_in_session_ = true; | 732 controller_->search_provider_->field_trial_triggered_in_session_ = true; |
674 EXPECT_TRUE( | 733 EXPECT_TRUE( |
675 controller_->search_provider_->field_trial_triggered_in_session()); | 734 controller_->search_provider_->field_trial_triggered_in_session()); |
676 url = GetDestinationURL(match, base::TimeDelta::FromMilliseconds(2456)); | 735 url = GetDestinationURL(match, base::TimeDelta::FromMilliseconds(2456)); |
677 EXPECT_EQ("//aqs=chrome.0.69i57j69i58j5l2j0l3j69i59.2456j1j4&", url.path()); | 736 EXPECT_EQ("//aqs=chrome.0.69i57j69i58j5l2j0l3j69i59.2456j1j4&", url.path()); |
678 } | 737 } |
OLD | NEW |