Chromium Code Reviews| 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 "chrome/browser/autocomplete/autocomplete_result.h" | 5 #include "chrome/browser/autocomplete/autocomplete_result.h" |
| 6 | 6 |
| 7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/metrics/field_trial.h" | 8 #include "base/metrics/field_trial.h" |
| 9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
| 10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 71 void AssertResultMatches(const AutocompleteResult& result, | 71 void AssertResultMatches(const AutocompleteResult& result, |
| 72 const TestData* expected, | 72 const TestData* expected, |
| 73 size_t expected_count); | 73 size_t expected_count); |
| 74 | 74 |
| 75 // Creates an AutocompleteResult from |last| and |current|. The two are | 75 // Creates an AutocompleteResult from |last| and |current|. The two are |
| 76 // merged by |CopyOldMatches| and compared by |AssertResultMatches|. | 76 // merged by |CopyOldMatches| and compared by |AssertResultMatches|. |
| 77 void RunCopyOldMatchesTest(const TestData* last, size_t last_size, | 77 void RunCopyOldMatchesTest(const TestData* last, size_t last_size, |
| 78 const TestData* current, size_t current_size, | 78 const TestData* current, size_t current_size, |
| 79 const TestData* expected, size_t expected_size); | 79 const TestData* expected, size_t expected_size); |
| 80 | 80 |
| 81 // Creates an AutocompleteMatch using |destination_url| and |type| and appends | |
|
Mark P
2013/12/13 01:10:55
There's no reason for this to be in the class. It
kmadhusu
2013/12/13 17:59:17
Done.
| |
| 82 // it to |matches|. | |
| 83 void AddMatch(const GURL& destination_url, AutocompleteMatch::Type type, | |
| 84 ACMatches* matches); | |
| 85 | |
| 81 protected: | 86 protected: |
| 82 TemplateURLServiceTestUtil test_util_; | 87 TemplateURLServiceTestUtil test_util_; |
| 83 | 88 |
| 84 private: | 89 private: |
| 85 scoped_ptr<base::FieldTrialList> field_trial_list_; | 90 scoped_ptr<base::FieldTrialList> field_trial_list_; |
| 86 | 91 |
| 87 DISALLOW_COPY_AND_ASSIGN(AutocompleteResultTest); | 92 DISALLOW_COPY_AND_ASSIGN(AutocompleteResultTest); |
| 88 }; | 93 }; |
| 89 | 94 |
| 90 // static | 95 // static |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 145 ACMatches current_matches; | 150 ACMatches current_matches; |
| 146 PopulateAutocompleteMatches(current, current_size, ¤t_matches); | 151 PopulateAutocompleteMatches(current, current_size, ¤t_matches); |
| 147 AutocompleteResult current_result; | 152 AutocompleteResult current_result; |
| 148 current_result.AppendMatches(current_matches); | 153 current_result.AppendMatches(current_matches); |
| 149 current_result.SortAndCull(input, test_util_.profile()); | 154 current_result.SortAndCull(input, test_util_.profile()); |
| 150 current_result.CopyOldMatches(input, last_result, test_util_.profile()); | 155 current_result.CopyOldMatches(input, last_result, test_util_.profile()); |
| 151 | 156 |
| 152 AssertResultMatches(current_result, expected, expected_size); | 157 AssertResultMatches(current_result, expected, expected_size); |
| 153 } | 158 } |
| 154 | 159 |
| 160 void AutocompleteResultTest::AddMatch(const GURL& destination_url, | |
| 161 AutocompleteMatch::Type type, | |
| 162 ACMatches* matches) { | |
| 163 ASSERT_TRUE(matches != NULL); | |
| 164 AutocompleteMatch* previous_match = | |
|
Mark P
2013/12/13 01:10:55
nit: last_match please.
kmadhusu
2013/12/13 17:59:17
Done.
| |
| 165 !matches->empty() ? &((*matches)[matches->size() -1]) : NULL; | |
|
Mark P
2013/12/13 01:10:55
nit: space before 1
kmadhusu
2013/12/13 17:59:17
Done.
| |
| 166 AutocompleteMatch match; | |
| 167 match.destination_url = destination_url; | |
| 168 match.relevance = | |
| 169 previous_match ? previous_match->relevance - 100 : 1300; | |
| 170 match.allowed_to_be_default_match = true; | |
| 171 match.type = type; | |
| 172 matches->push_back(match); | |
| 173 } | |
| 174 | |
| 155 // Assertion testing for AutocompleteResult::Swap. | 175 // Assertion testing for AutocompleteResult::Swap. |
| 156 TEST_F(AutocompleteResultTest, Swap) { | 176 TEST_F(AutocompleteResultTest, Swap) { |
| 157 AutocompleteResult r1; | 177 AutocompleteResult r1; |
| 158 AutocompleteResult r2; | 178 AutocompleteResult r2; |
| 159 | 179 |
| 160 // Swap with empty shouldn't do anything interesting. | 180 // Swap with empty shouldn't do anything interesting. |
| 161 r1.Swap(&r2); | 181 r1.Swap(&r2); |
| 162 EXPECT_EQ(r1.end(), r1.default_match()); | 182 EXPECT_EQ(r1.end(), r1.default_match()); |
| 163 EXPECT_EQ(r2.end(), r2.default_match()); | 183 EXPECT_EQ(r2.end(), r2.default_match()); |
| 164 | 184 |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 311 result.match_at(1)->destination_url.spec()); | 331 result.match_at(1)->destination_url.spec()); |
| 312 EXPECT_EQ(1200, result.match_at(1)->relevance); | 332 EXPECT_EQ(1200, result.match_at(1)->relevance); |
| 313 EXPECT_EQ("http://www.foo.com/", | 333 EXPECT_EQ("http://www.foo.com/", |
| 314 result.match_at(2)->destination_url.spec()); | 334 result.match_at(2)->destination_url.spec()); |
| 315 EXPECT_EQ(900, result.match_at(2)->relevance); | 335 EXPECT_EQ(900, result.match_at(2)->relevance); |
| 316 } | 336 } |
| 317 | 337 |
| 318 TEST_F(AutocompleteResultTest, SortAndCullWithDemotionsByType) { | 338 TEST_F(AutocompleteResultTest, SortAndCullWithDemotionsByType) { |
| 319 // Add some matches. | 339 // Add some matches. |
| 320 ACMatches matches; | 340 ACMatches matches; |
| 321 { | 341 AddMatch(GURL("http://history-url/"), |
| 322 AutocompleteMatch match; | 342 AutocompleteMatchType::HISTORY_URL, |
|
kmadhusu
2013/12/13 01:00:29
For better readability, I have added the arguments
Mark P
2013/12/13 01:10:55
Please consider putting them all one line if they
kmadhusu
2013/12/13 17:59:17
Done.
| |
| 323 match.destination_url = GURL("http://history-url/"); | 343 &matches); |
| 324 match.relevance = 1400; | 344 AddMatch(GURL("http://search-what-you-typed/"), |
| 325 match.allowed_to_be_default_match = true; | 345 AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED, |
| 326 match.type = AutocompleteMatchType::HISTORY_URL; | 346 &matches); |
| 327 matches.push_back(match); | 347 AddMatch(GURL("http://history-title/"), |
| 328 } | 348 AutocompleteMatchType::HISTORY_TITLE, |
| 329 { | 349 &matches); |
| 330 AutocompleteMatch match; | 350 |
| 331 match.destination_url = GURL("http://search-what-you-typed/"); | 351 // Add a search history type match and demote its relevance score. |
| 332 match.relevance = 1300; | 352 AddMatch(GURL("http://search-history/"), |
| 333 match.allowed_to_be_default_match = true; | 353 AutocompleteMatchType::SEARCH_HISTORY, |
| 334 match.type = AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED; | 354 &matches); |
| 335 matches.push_back(match); | 355 matches[matches.size()-1].relevance = 500; |
|
Mark P
2013/12/13 01:10:55
nit: spaces around binary operators (-)
kmadhusu
2013/12/13 17:59:17
Done.
| |
| 336 } | |
| 337 { | |
| 338 AutocompleteMatch match; | |
| 339 match.destination_url = GURL("http://history-title/"); | |
| 340 match.relevance = 1200; | |
| 341 match.allowed_to_be_default_match = true; | |
| 342 match.type = AutocompleteMatchType::HISTORY_TITLE; | |
| 343 matches.push_back(match); | |
| 344 } | |
| 345 { | |
| 346 AutocompleteMatch match; | |
| 347 match.destination_url = GURL("http://search-history/"); | |
| 348 match.relevance = 500; | |
| 349 match.allowed_to_be_default_match = true; | |
| 350 match.type = AutocompleteMatchType::SEARCH_HISTORY; | |
| 351 matches.push_back(match); | |
| 352 } | |
| 353 | 356 |
| 354 // Add a rule demoting history-url and killing history-title. | 357 // Add a rule demoting history-url and killing history-title. |
| 355 { | 358 { |
| 356 std::map<std::string, std::string> params; | 359 std::map<std::string, std::string> params; |
| 357 params[std::string(OmniboxFieldTrial::kDemoteByTypeRule) + ":3:*"] = | 360 params[std::string(OmniboxFieldTrial::kDemoteByTypeRule) + ":3:*"] = |
| 358 "1:50,7:100,2:0"; // 3 == HOME_PAGE | 361 "1:50,7:100,2:0"; // 3 == HOME_PAGE |
| 359 ASSERT_TRUE(chrome_variations::AssociateVariationParams( | 362 ASSERT_TRUE(chrome_variations::AssociateVariationParams( |
| 360 OmniboxFieldTrial::kBundledExperimentFieldTrialName, "A", params)); | 363 OmniboxFieldTrial::kBundledExperimentFieldTrialName, "A", params)); |
| 361 } | 364 } |
| 362 base::FieldTrialList::CreateFieldTrial( | 365 base::FieldTrialList::CreateFieldTrial( |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 479 AutocompleteInput::HOME_PAGE, false, false, false, | 482 AutocompleteInput::HOME_PAGE, false, false, false, |
| 480 AutocompleteInput::ALL_MATCHES); | 483 AutocompleteInput::ALL_MATCHES); |
| 481 result.SortAndCull(input, test_util_.profile()); | 484 result.SortAndCull(input, test_util_.profile()); |
| 482 ASSERT_EQ(4U, result.size()); | 485 ASSERT_EQ(4U, result.size()); |
| 483 EXPECT_EQ("http://c/", result.match_at(0)->destination_url.spec()); | 486 EXPECT_EQ("http://c/", result.match_at(0)->destination_url.spec()); |
| 484 EXPECT_EQ("http://a/", result.match_at(1)->destination_url.spec()); | 487 EXPECT_EQ("http://a/", result.match_at(1)->destination_url.spec()); |
| 485 EXPECT_EQ("http://b/", result.match_at(2)->destination_url.spec()); | 488 EXPECT_EQ("http://b/", result.match_at(2)->destination_url.spec()); |
| 486 EXPECT_EQ("http://d/", result.match_at(3)->destination_url.spec()); | 489 EXPECT_EQ("http://d/", result.match_at(3)->destination_url.spec()); |
| 487 } | 490 } |
| 488 } | 491 } |
| 492 | |
| 493 TEST_F(AutocompleteResultTest, ShouldHideTopMatch) { | |
| 494 // Add some matches. | |
| 495 ACMatches matches; | |
| 496 AddMatch(GURL("http://search-what-you-typed/"), | |
| 497 AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED, | |
| 498 &matches); | |
| 499 AddMatch(GURL("http://history-title/"), | |
| 500 AutocompleteMatchType::HISTORY_TITLE, | |
| 501 &matches); | |
| 502 AddMatch(GURL("http://search-history/"), | |
| 503 AutocompleteMatchType::SEARCH_HISTORY, | |
| 504 &matches); | |
| 505 | |
| 506 base::FieldTrialList::CreateFieldTrial("InstantExtended", | |
| 507 "Group1 hide_verbatim:1"); | |
| 508 AutocompleteResult result; | |
| 509 result.AppendMatches(matches); | |
| 510 EXPECT_TRUE(result.ShouldHideTopMatch()); | |
| 511 } | |
| 512 | |
| 513 TEST_F(AutocompleteResultTest, DoNotHideTopMatch) { | |
| 514 ACMatches matches; | |
| 515 AddMatch(GURL("http://search-what-you-typed/"), | |
| 516 AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED, | |
| 517 &matches); | |
| 518 AddMatch(GURL("http://url-what-you-typed/"), | |
| 519 AutocompleteMatchType::URL_WHAT_YOU_TYPED, | |
| 520 &matches); | |
| 521 AddMatch(GURL("http://history-title/"), | |
| 522 AutocompleteMatchType::HISTORY_TITLE, | |
| 523 &matches); | |
| 524 AddMatch(GURL("http://search-history/"), | |
| 525 AutocompleteMatchType::SEARCH_HISTORY, | |
| 526 &matches); | |
| 527 | |
| 528 base::FieldTrialList::CreateFieldTrial("InstantExtended", | |
| 529 "Group1 hide_verbatim:1"); | |
| 530 AutocompleteResult result; | |
| 531 result.AppendMatches(matches); | |
| 532 // If the verbatim first match is followed by another verbatim match, don't | |
| 533 // hide the top verbatim match. | |
| 534 EXPECT_FALSE(result.ShouldHideTopMatch()); | |
| 535 } | |
| 536 | |
| 537 TEST_F(AutocompleteResultTest, DoNotHideTopMatch_TopMatchIsNotVerbatim) { | |
| 538 ACMatches matches; | |
| 539 AddMatch(GURL("http://search-history/"), | |
| 540 AutocompleteMatchType::SEARCH_HISTORY, | |
| 541 &matches); | |
| 542 AddMatch(GURL("http://search-what-you-typed/"), | |
| 543 AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED, | |
| 544 &matches); | |
| 545 AddMatch(GURL("http://history-title/"), | |
| 546 AutocompleteMatchType::HISTORY_TITLE, | |
| 547 &matches); | |
| 548 | |
| 549 base::FieldTrialList::CreateFieldTrial("InstantExtended", | |
| 550 "Group1 hide_verbatim:1"); | |
| 551 AutocompleteResult result; | |
| 552 result.AppendMatches(matches); | |
| 553 // Top match is not a verbatim type match. Do not hide the top match. | |
| 554 EXPECT_FALSE(result.ShouldHideTopMatch()); | |
| 555 } | |
| 556 | |
| 557 TEST_F(AutocompleteResultTest, DoNotHideTopMatch_FieldTrialFlagDisabled) { | |
|
Mark P
2013/12/13 01:10:55
Please comment here that this is an identical conf
kmadhusu
2013/12/13 17:59:17
Done.
| |
| 558 // Add some matches. | |
| 559 ACMatches matches; | |
| 560 AddMatch(GURL("http://search-what-you-typed/"), | |
| 561 AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED, | |
| 562 &matches); | |
| 563 AddMatch(GURL("http://history-title/"), | |
| 564 AutocompleteMatchType::HISTORY_TITLE, | |
| 565 &matches); | |
| 566 AddMatch(GURL("http://search-history/"), | |
| 567 AutocompleteMatchType::SEARCH_HISTORY, | |
| 568 &matches); | |
| 569 | |
| 570 base::FieldTrialList::CreateFieldTrial("InstantExtended", | |
| 571 "Group1 hide_verbatim:0"); | |
| 572 AutocompleteResult result; | |
| 573 result.AppendMatches(matches); | |
| 574 // Field trial flag "hide_verbatim" is disabled. Do not hide top match. | |
| 575 EXPECT_FALSE(result.ShouldHideTopMatch()); | |
| 576 } | |
| 577 | |
| 578 TEST_F(AutocompleteResultTest, | |
| 579 TopMatchIsVerbatimAndHasNoConsecutiveVerbatimMatches) { | |
| 580 ACMatches matches; | |
| 581 AddMatch(GURL("http://url-what-you-typed/"), | |
| 582 AutocompleteMatchType::URL_WHAT_YOU_TYPED, | |
| 583 &matches); | |
| 584 AddMatch(GURL("http://history-title/"), | |
| 585 AutocompleteMatchType::HISTORY_TITLE, | |
| 586 &matches); | |
| 587 | |
| 588 AutocompleteResult result; | |
| 589 result.AppendMatches(matches); | |
| 590 EXPECT_TRUE(result.TopMatchIsVerbatimAndHasNoConsecutiveVerbatimMatches()); | |
| 591 } | |
| 592 | |
| 593 TEST_F(AutocompleteResultTest, | |
| 594 TopMatchIsVerbatimAndHasConsecutiveVerbatimMatches) { | |
| 595 ACMatches matches; | |
| 596 AddMatch(GURL("http://url-what-you-typed/"), | |
| 597 AutocompleteMatchType::URL_WHAT_YOU_TYPED, | |
| 598 &matches); | |
| 599 AddMatch(GURL("http://url-what-you-typed-1/"), | |
| 600 AutocompleteMatchType::URL_WHAT_YOU_TYPED, | |
|
Mark P
2013/12/13 01:10:55
There can be only one URL_WHAT_YOU_TYPED. This se
kmadhusu
2013/12/13 17:59:17
Done.
| |
| 601 &matches); | |
| 602 AddMatch(GURL("http://history-title/"), | |
| 603 AutocompleteMatchType::HISTORY_TITLE, | |
| 604 &matches); | |
| 605 | |
| 606 AutocompleteResult result; | |
| 607 result.AppendMatches(matches); | |
| 608 EXPECT_FALSE(result.TopMatchIsVerbatimAndHasNoConsecutiveVerbatimMatches()); | |
| 609 } | |
| 610 | |
| 611 TEST_F(AutocompleteResultTest, TopMatchIsNotVerbatim) { | |
| 612 ACMatches matches; | |
| 613 AutocompleteResult result; | |
| 614 result.AppendMatches(matches); | |
| 615 | |
| 616 // Result set is empty. | |
| 617 EXPECT_FALSE(result.TopMatchIsVerbatimAndHasNoConsecutiveVerbatimMatches()); | |
| 618 | |
| 619 // Add a non-verbatim match to the result. | |
| 620 AddMatch(GURL("http://history-title/"), | |
| 621 AutocompleteMatchType::HISTORY_TITLE, | |
| 622 &matches); | |
| 623 | |
| 624 result.AppendMatches(matches); | |
| 625 EXPECT_FALSE(result.TopMatchIsVerbatimAndHasNoConsecutiveVerbatimMatches()); | |
| 626 } | |
| 627 | |
| 628 TEST_F(AutocompleteResultTest, | |
| 629 TopMatchIsVerbatimAndHasNoConsecutiveVerbatimMatches_SingleMatchFound) { | |
| 630 ACMatches matches; | |
| 631 AddMatch(GURL("http://url-what-you-typed/"), | |
| 632 AutocompleteMatchType::URL_WHAT_YOU_TYPED, | |
| 633 &matches); | |
| 634 | |
| 635 AutocompleteResult result; | |
| 636 result.AppendMatches(matches); | |
| 637 EXPECT_TRUE(result.TopMatchIsVerbatimAndHasNoConsecutiveVerbatimMatches()); | |
| 638 } | |
| OLD | NEW |