Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(470)

Side by Side Diff: chrome/browser/autocomplete/autocomplete_result_unittest.cc

Issue 69703002: Modified GetMatchToPrefetch() to return a valid match for prefetching even when hide_verbatim flag i (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressed comments Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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"
11 #include "base/strings/utf_string_conversions.h" 11 #include "base/strings/utf_string_conversions.h"
12 #include "chrome/browser/autocomplete/autocomplete_input.h" 12 #include "chrome/browser/autocomplete/autocomplete_input.h"
13 #include "chrome/browser/autocomplete/autocomplete_match.h" 13 #include "chrome/browser/autocomplete/autocomplete_match.h"
14 #include "chrome/browser/autocomplete/autocomplete_provider.h" 14 #include "chrome/browser/autocomplete/autocomplete_provider.h"
15 #include "chrome/browser/omnibox/omnibox_field_trial.h" 15 #include "chrome/browser/omnibox/omnibox_field_trial.h"
16 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" 16 #include "chrome/browser/search_engines/template_url_prepopulate_data.h"
17 #include "chrome/browser/search_engines/template_url_service.h" 17 #include "chrome/browser/search_engines/template_url_service.h"
18 #include "chrome/browser/search_engines/template_url_service_test_util.h" 18 #include "chrome/browser/search_engines/template_url_service_test_util.h"
19 #include "chrome/common/autocomplete_match_type.h" 19 #include "chrome/common/autocomplete_match_type.h"
20 #include "chrome/common/metrics/variations/variations_util.h" 20 #include "chrome/common/metrics/variations/variations_util.h"
21 #include "chrome/test/base/testing_profile.h" 21 #include "chrome/test/base/testing_profile.h"
22 #include "components/variations/entropy_provider.h" 22 #include "components/variations/entropy_provider.h"
23 #include "testing/gtest/include/gtest/gtest.h" 23 #include "testing/gtest/include/gtest/gtest.h"
24 24
25 namespace {
26
27 // Creates an AutocompleteMatch using |destination_url| and |type| and appends
28 // it to |matches|.
29 void AddMatch(const std::string& destination_url, AutocompleteMatch::Type type,
30 ACMatches* matches) {
Peter Kasting 2013/12/17 23:47:34 Nit: One arg per line
kmadhusu 2013/12/18 23:26:08 Done.
31 ASSERT_TRUE(matches != NULL);
32 AutocompleteMatch* last_match =
33 !matches->empty() ? &((*matches)[matches->size() - 1]) : NULL;
Peter Kasting 2013/12/17 23:47:34 Nit: Needlessly verbose. ELiminate this line and
kmadhusu 2013/12/18 23:26:08 Done.
34 AutocompleteMatch match;
35 match.destination_url = GURL(destination_url);
36 match.relevance = last_match ? last_match->relevance - 100 : 1300;
37 match.allowed_to_be_default_match = true;
38 match.type = type;
39 matches->push_back(match);
40 }
41
42 } // namespace
43
25 class AutocompleteResultTest : public testing::Test { 44 class AutocompleteResultTest : public testing::Test {
26 public: 45 public:
27 struct TestData { 46 struct TestData {
28 // Used to build a url for the AutocompleteMatch. The URL becomes 47 // Used to build a url for the AutocompleteMatch. The URL becomes
29 // "http://" + ('a' + |url_id|) (e.g. an ID of 2 yields "http://b"). 48 // "http://" + ('a' + |url_id|) (e.g. an ID of 2 yields "http://b").
30 int url_id; 49 int url_id;
31 50
32 // ID of the provider. 51 // ID of the provider.
33 int provider_id; 52 int provider_id;
34 53
(...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after
311 result.match_at(1)->destination_url.spec()); 330 result.match_at(1)->destination_url.spec());
312 EXPECT_EQ(1200, result.match_at(1)->relevance); 331 EXPECT_EQ(1200, result.match_at(1)->relevance);
313 EXPECT_EQ("http://www.foo.com/", 332 EXPECT_EQ("http://www.foo.com/",
314 result.match_at(2)->destination_url.spec()); 333 result.match_at(2)->destination_url.spec());
315 EXPECT_EQ(900, result.match_at(2)->relevance); 334 EXPECT_EQ(900, result.match_at(2)->relevance);
316 } 335 }
317 336
318 TEST_F(AutocompleteResultTest, SortAndCullWithDemotionsByType) { 337 TEST_F(AutocompleteResultTest, SortAndCullWithDemotionsByType) {
319 // Add some matches. 338 // Add some matches.
320 ACMatches matches; 339 ACMatches matches;
321 { 340 AddMatch("http://history-url/", AutocompleteMatchType::HISTORY_URL, &matches);
322 AutocompleteMatch match; 341 AddMatch("http://search-what-you-typed/",
323 match.destination_url = GURL("http://history-url/"); 342 AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED, &matches);
324 match.relevance = 1400; 343 AddMatch("http://history-title/", AutocompleteMatchType::HISTORY_TITLE,
325 match.allowed_to_be_default_match = true; 344 &matches);
326 match.type = AutocompleteMatchType::HISTORY_URL; 345
327 matches.push_back(match); 346 // Add a search history type match and demote its relevance score.
328 } 347 AddMatch("http://search-history/", AutocompleteMatchType::SEARCH_HISTORY,
329 { 348 &matches);
330 AutocompleteMatch match; 349 matches[matches.size() - 1].relevance = 500;
Peter Kasting 2013/12/17 23:47:34 Nit: Use matches.back()
kmadhusu 2013/12/18 23:26:08 Done.
331 match.destination_url = GURL("http://search-what-you-typed/");
332 match.relevance = 1300;
333 match.allowed_to_be_default_match = true;
334 match.type = AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED;
335 matches.push_back(match);
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 350
354 // Add a rule demoting history-url and killing history-title. 351 // Add a rule demoting history-url and killing history-title.
355 { 352 {
356 std::map<std::string, std::string> params; 353 std::map<std::string, std::string> params;
357 params[std::string(OmniboxFieldTrial::kDemoteByTypeRule) + ":3:*"] = 354 params[std::string(OmniboxFieldTrial::kDemoteByTypeRule) + ":3:*"] =
358 "1:50,7:100,2:0"; // 3 == HOME_PAGE 355 "1:50,7:100,2:0"; // 3 == HOME_PAGE
359 ASSERT_TRUE(chrome_variations::AssociateVariationParams( 356 ASSERT_TRUE(chrome_variations::AssociateVariationParams(
360 OmniboxFieldTrial::kBundledExperimentFieldTrialName, "A", params)); 357 OmniboxFieldTrial::kBundledExperimentFieldTrialName, "A", params));
361 } 358 }
362 base::FieldTrialList::CreateFieldTrial( 359 base::FieldTrialList::CreateFieldTrial(
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
479 AutocompleteInput::HOME_PAGE, false, false, false, 476 AutocompleteInput::HOME_PAGE, false, false, false,
480 AutocompleteInput::ALL_MATCHES); 477 AutocompleteInput::ALL_MATCHES);
481 result.SortAndCull(input, test_util_.profile()); 478 result.SortAndCull(input, test_util_.profile());
482 ASSERT_EQ(4U, result.size()); 479 ASSERT_EQ(4U, result.size());
483 EXPECT_EQ("http://c/", result.match_at(0)->destination_url.spec()); 480 EXPECT_EQ("http://c/", result.match_at(0)->destination_url.spec());
484 EXPECT_EQ("http://a/", result.match_at(1)->destination_url.spec()); 481 EXPECT_EQ("http://a/", result.match_at(1)->destination_url.spec());
485 EXPECT_EQ("http://b/", result.match_at(2)->destination_url.spec()); 482 EXPECT_EQ("http://b/", result.match_at(2)->destination_url.spec());
486 EXPECT_EQ("http://d/", result.match_at(3)->destination_url.spec()); 483 EXPECT_EQ("http://d/", result.match_at(3)->destination_url.spec());
487 } 484 }
488 } 485 }
486
487 TEST_F(AutocompleteResultTest, ShouldHideTopMatch) {
Peter Kasting 2013/12/17 23:47:34 There are a ton of tests here, most of which start
kmadhusu 2013/12/18 23:26:08 Done.
488 // Add some matches.
489 ACMatches matches;
490 AddMatch("http://search-what-you-typed/",
491 AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED, &matches);
492 AddMatch("http://history-title/", AutocompleteMatchType::HISTORY_TITLE,
493 &matches);
494 AddMatch("http://search-history/", AutocompleteMatchType::SEARCH_HISTORY,
495 &matches);
496
497 base::FieldTrialList::CreateFieldTrial("InstantExtended",
498 "Group1 hide_verbatim:1");
499 AutocompleteResult result;
500 result.AppendMatches(matches);
501 EXPECT_TRUE(result.ShouldHideTopMatch());
502 }
503
504 TEST_F(AutocompleteResultTest, DoNotHideTopMatch) {
505 ACMatches matches;
506 AddMatch("http://search-what-you-typed/",
507 AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED, &matches);
508 AddMatch("http://url-what-you-typed/",
509 AutocompleteMatchType::URL_WHAT_YOU_TYPED, &matches);
510 AddMatch("http://history-title/", AutocompleteMatchType::HISTORY_TITLE,
511 &matches);
512 AddMatch("http://search-history/", AutocompleteMatchType::SEARCH_HISTORY,
513 &matches);
514
515 base::FieldTrialList::CreateFieldTrial("InstantExtended",
516 "Group1 hide_verbatim:1");
517 AutocompleteResult result;
518 result.AppendMatches(matches);
519 // If the verbatim first match is followed by another verbatim match, don't
520 // hide the top verbatim match.
521 EXPECT_FALSE(result.ShouldHideTopMatch());
522 }
523
524 TEST_F(AutocompleteResultTest, DoNotHideTopMatch_TopMatchIsNotVerbatim) {
525 ACMatches matches;
526 AddMatch("http://search-history/", AutocompleteMatchType::SEARCH_HISTORY,
527 &matches);
528 AddMatch("http://search-what-you-typed/",
529 AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED, &matches);
530 AddMatch("http://history-title/", AutocompleteMatchType::HISTORY_TITLE,
531 &matches);
532
533 base::FieldTrialList::CreateFieldTrial("InstantExtended",
534 "Group1 hide_verbatim:1");
535 AutocompleteResult result;
536 result.AppendMatches(matches);
537 // Top match is not a verbatim type match. Do not hide the top match.
538 EXPECT_FALSE(result.ShouldHideTopMatch());
539 }
540
541 TEST_F(AutocompleteResultTest, DoNotHideTopMatch_FieldTrialFlagDisabled) {
542 // Add some matches. This test config is identical to ShouldHideTopMatch test
543 // except that the "hide_verbatim" flag is disabled in the field trials.
544 ACMatches matches;
545 AddMatch("http://search-what-you-typed/",
546 AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED, &matches);
547 AddMatch("http://history-title/", AutocompleteMatchType::HISTORY_TITLE,
548 &matches);
549 AddMatch("http://search-history/", AutocompleteMatchType::SEARCH_HISTORY,
550 &matches);
551
552 base::FieldTrialList::CreateFieldTrial("InstantExtended",
553 "Group1 hide_verbatim:0");
554 AutocompleteResult result;
555 result.AppendMatches(matches);
556 // Field trial flag "hide_verbatim" is disabled. Do not hide top match.
557 EXPECT_FALSE(result.ShouldHideTopMatch());
558 }
559
560 TEST_F(AutocompleteResultTest,
561 TopMatchIsVerbatimAndHasNoConsecutiveVerbatimMatches) {
562 ACMatches matches;
563 AddMatch("http://url-what-you-typed/",
564 AutocompleteMatchType::URL_WHAT_YOU_TYPED, &matches);
565 AddMatch("http://history-title/", AutocompleteMatchType::HISTORY_TITLE,
566 &matches);
567
568 AutocompleteResult result;
569 result.AppendMatches(matches);
570 EXPECT_TRUE(result.TopMatchIsVerbatimAndHasNoConsecutiveVerbatimMatches());
571 }
572
573 TEST_F(AutocompleteResultTest,
574 TopMatchIsVerbatimAndHasConsecutiveVerbatimMatches) {
575 ACMatches matches;
576 AddMatch("http://search-what-you-typed/",
577 AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED, &matches);
578 AddMatch("http://url-what-you-typed/",
579 AutocompleteMatchType::URL_WHAT_YOU_TYPED, &matches);
580 AddMatch("http://history-title/", AutocompleteMatchType::HISTORY_TITLE,
581 &matches);
582
583 AutocompleteResult result;
584 result.AppendMatches(matches);
585 EXPECT_FALSE(result.TopMatchIsVerbatimAndHasNoConsecutiveVerbatimMatches());
586 }
587
588 TEST_F(AutocompleteResultTest, TopMatchIsNotVerbatim) {
589 ACMatches matches;
590 AutocompleteResult result;
591 result.AppendMatches(matches);
592
593 // Result set is empty.
594 EXPECT_FALSE(result.TopMatchIsVerbatimAndHasNoConsecutiveVerbatimMatches());
595
596 // Add a non-verbatim match to the result.
597 AddMatch("http://history-title/", AutocompleteMatchType::HISTORY_TITLE,
598 &matches);
599
600 result.AppendMatches(matches);
601 EXPECT_FALSE(result.TopMatchIsVerbatimAndHasNoConsecutiveVerbatimMatches());
602 }
603
604 TEST_F(AutocompleteResultTest,
605 TopMatchIsVerbatimAndHasNoConsecutiveVerbatimMatches_SingleMatchFound) {
606 ACMatches matches;
607 AddMatch("http://url-what-you-typed/",
608 AutocompleteMatchType::URL_WHAT_YOU_TYPED, &matches);
609
610 AutocompleteResult result;
611 result.AppendMatches(matches);
612 EXPECT_TRUE(result.TopMatchIsVerbatimAndHasNoConsecutiveVerbatimMatches());
613 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698