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/metrics/field_trial.h" | |
| 7 #include "base/strings/string_number_conversions.h" | 8 #include "base/strings/string_number_conversions.h" |
| 8 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
| 9 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| 10 #include "chrome/browser/autocomplete/autocomplete_input.h" | 11 #include "chrome/browser/autocomplete/autocomplete_input.h" |
| 11 #include "chrome/browser/autocomplete/autocomplete_match.h" | 12 #include "chrome/browser/autocomplete/autocomplete_match.h" |
| 12 #include "chrome/browser/autocomplete/autocomplete_provider.h" | 13 #include "chrome/browser/autocomplete/autocomplete_provider.h" |
| 13 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" | 14 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" |
| 14 #include "chrome/browser/search_engines/template_url_service.h" | 15 #include "chrome/browser/search_engines/template_url_service.h" |
| 15 #include "chrome/browser/search_engines/template_url_service_test_util.h" | 16 #include "chrome/browser/search_engines/template_url_service_test_util.h" |
| 17 #include "chrome/common/metrics/entropy_provider.h" | |
| 18 #include "chrome/common/metrics/variations/variations_util.h" | |
| 16 #include "chrome/test/base/testing_profile.h" | 19 #include "chrome/test/base/testing_profile.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 21 |
| 19 class AutocompleteResultTest : public testing::Test { | 22 class AutocompleteResultTest : public testing::Test { |
| 20 public: | 23 public: |
| 21 struct TestData { | 24 struct TestData { |
| 22 // Used to build a url for the AutocompleteMatch. The URL becomes | 25 // Used to build a url for the AutocompleteMatch. The URL becomes |
| 23 // "http://" + ('a' + |url_id|) (e.g. an ID of 2 yields "http://b"). | 26 // "http://" + ('a' + |url_id|) (e.g. an ID of 2 yields "http://b"). |
| 24 int url_id; | 27 int url_id; |
| 25 | 28 |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 38 std::string() /* unknown country code */); | 41 std::string() /* unknown country code */); |
| 39 #endif | 42 #endif |
| 40 test_util_.SetUp(); | 43 test_util_.SetUp(); |
| 41 test_util_.VerifyLoad(); | 44 test_util_.VerifyLoad(); |
| 42 } | 45 } |
| 43 | 46 |
| 44 virtual void TearDown() OVERRIDE { | 47 virtual void TearDown() OVERRIDE { |
| 45 test_util_.TearDown(); | 48 test_util_.TearDown(); |
| 46 } | 49 } |
| 47 | 50 |
| 51 static void SetUpTestCase() { | |
| 52 ResetFieldTrialList(); | |
| 53 } | |
| 54 | |
| 55 static void TearDownTestCase() { | |
| 56 delete field_trial_list_; | |
| 57 field_trial_list_ = NULL; | |
| 58 } | |
| 59 | |
| 60 static void ResetFieldTrialList(); | |
| 61 | |
| 48 // Configures |match| from |data|. | 62 // Configures |match| from |data|. |
| 49 static void PopulateAutocompleteMatch(const TestData& data, | 63 static void PopulateAutocompleteMatch(const TestData& data, |
| 50 AutocompleteMatch* match); | 64 AutocompleteMatch* match); |
| 51 | 65 |
| 52 // Adds |count| AutocompleteMatches to |matches|. | 66 // Adds |count| AutocompleteMatches to |matches|. |
| 53 static void PopulateAutocompleteMatches(const TestData* data, | 67 static void PopulateAutocompleteMatches(const TestData* data, |
| 54 size_t count, | 68 size_t count, |
| 55 ACMatches* matches); | 69 ACMatches* matches); |
| 56 | 70 |
| 57 // Asserts that |result| has |expected_count| matches matching |expected|. | 71 // Asserts that |result| has |expected_count| matches matching |expected|. |
| 58 void AssertResultMatches(const AutocompleteResult& result, | 72 void AssertResultMatches(const AutocompleteResult& result, |
| 59 const TestData* expected, | 73 const TestData* expected, |
| 60 size_t expected_count); | 74 size_t expected_count); |
| 61 | 75 |
| 62 // Creates an AutocompleteResult from |last| and |current|. The two are | 76 // Creates an AutocompleteResult from |last| and |current|. The two are |
| 63 // merged by |CopyOldMatches| and compared by |AssertResultMatches|. | 77 // merged by |CopyOldMatches| and compared by |AssertResultMatches|. |
| 64 void RunCopyOldMatchesTest(const TestData* last, size_t last_size, | 78 void RunCopyOldMatchesTest(const TestData* last, size_t last_size, |
| 65 const TestData* current, size_t current_size, | 79 const TestData* current, size_t current_size, |
| 66 const TestData* expected, size_t expected_size); | 80 const TestData* expected, size_t expected_size); |
| 67 | 81 |
| 68 protected: | 82 protected: |
| 69 TemplateURLServiceTestUtil test_util_; | 83 TemplateURLServiceTestUtil test_util_; |
| 70 | 84 |
| 71 private: | 85 private: |
| 86 static base::FieldTrialList* field_trial_list_; | |
| 87 | |
| 72 DISALLOW_COPY_AND_ASSIGN(AutocompleteResultTest); | 88 DISALLOW_COPY_AND_ASSIGN(AutocompleteResultTest); |
| 73 }; | 89 }; |
| 74 | 90 |
| 75 // static | 91 // static |
| 92 base::FieldTrialList* AutocompleteResultTest::field_trial_list_ = NULL; | |
| 93 | |
| 94 // static | |
| 95 void AutocompleteResultTest::ResetFieldTrialList() { | |
| 96 // It's important to delete the old pointer first which sets | |
| 97 // FieldTrialList::global_ to NULL. | |
| 98 if (field_trial_list_) | |
|
H Fung
2013/08/06 22:03:17
Nit: the if is not needed?
Mark P
2013/08/06 22:18:06
I will ignore this comment because
https://coderev
| |
| 99 delete field_trial_list_; | |
| 100 field_trial_list_ = new base::FieldTrialList( | |
| 101 new metrics::SHA1EntropyProvider("foo")); | |
| 102 } | |
| 103 | |
| 104 // static | |
| 76 void AutocompleteResultTest::PopulateAutocompleteMatch( | 105 void AutocompleteResultTest::PopulateAutocompleteMatch( |
| 77 const TestData& data, | 106 const TestData& data, |
| 78 AutocompleteMatch* match) { | 107 AutocompleteMatch* match) { |
| 79 match->provider = reinterpret_cast<AutocompleteProvider*>(data.provider_id); | 108 match->provider = reinterpret_cast<AutocompleteProvider*>(data.provider_id); |
| 80 match->fill_into_edit = base::IntToString16(data.url_id); | 109 match->fill_into_edit = base::IntToString16(data.url_id); |
| 81 std::string url_id(1, data.url_id + 'a'); | 110 std::string url_id(1, data.url_id + 'a'); |
| 82 match->destination_url = GURL("http://" + url_id); | 111 match->destination_url = GURL("http://" + url_id); |
| 83 match->relevance = data.relevance; | 112 match->relevance = data.relevance; |
| 113 match->allowed_to_be_default_match = true; | |
| 84 } | 114 } |
| 85 | 115 |
| 86 // static | 116 // static |
| 87 void AutocompleteResultTest::PopulateAutocompleteMatches( | 117 void AutocompleteResultTest::PopulateAutocompleteMatches( |
| 88 const TestData* data, | 118 const TestData* data, |
| 89 size_t count, | 119 size_t count, |
| 90 ACMatches* matches) { | 120 ACMatches* matches) { |
| 91 for (size_t i = 0; i < count; ++i) { | 121 for (size_t i = 0; i < count; ++i) { |
| 92 AutocompleteMatch match; | 122 AutocompleteMatch match; |
| 93 PopulateAutocompleteMatch(data[i], &match); | 123 PopulateAutocompleteMatch(data[i], &match); |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 141 AutocompleteResult r2; | 171 AutocompleteResult r2; |
| 142 | 172 |
| 143 // Swap with empty shouldn't do anything interesting. | 173 // Swap with empty shouldn't do anything interesting. |
| 144 r1.Swap(&r2); | 174 r1.Swap(&r2); |
| 145 EXPECT_EQ(r1.end(), r1.default_match()); | 175 EXPECT_EQ(r1.end(), r1.default_match()); |
| 146 EXPECT_EQ(r2.end(), r2.default_match()); | 176 EXPECT_EQ(r2.end(), r2.default_match()); |
| 147 | 177 |
| 148 // Swap with a single match. | 178 // Swap with a single match. |
| 149 ACMatches matches; | 179 ACMatches matches; |
| 150 AutocompleteMatch match; | 180 AutocompleteMatch match; |
| 181 match.allowed_to_be_default_match = true; | |
| 151 AutocompleteInput input(ASCIIToUTF16("a"), string16::npos, string16(), GURL(), | 182 AutocompleteInput input(ASCIIToUTF16("a"), string16::npos, string16(), GURL(), |
| 152 AutocompleteInput::INVALID_SPEC, false, false, false, | 183 AutocompleteInput::INVALID_SPEC, false, false, false, |
| 153 AutocompleteInput::ALL_MATCHES); | 184 AutocompleteInput::ALL_MATCHES); |
| 154 matches.push_back(match); | 185 matches.push_back(match); |
| 155 r1.AppendMatches(matches); | 186 r1.AppendMatches(matches); |
| 156 r1.SortAndCull(input, test_util_.profile()); | 187 r1.SortAndCull(input, test_util_.profile()); |
| 157 EXPECT_EQ(r1.begin(), r1.default_match()); | 188 EXPECT_EQ(r1.begin(), r1.default_match()); |
| 158 EXPECT_EQ("http://a/", r1.alternate_nav_url().spec()); | 189 EXPECT_EQ("http://a/", r1.alternate_nav_url().spec()); |
| 159 r1.Swap(&r2); | 190 r1.Swap(&r2); |
| 160 EXPECT_TRUE(r1.empty()); | 191 EXPECT_TRUE(r1.empty()); |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 285 EXPECT_EQ("http://www.foo.com/s?q=foo", | 316 EXPECT_EQ("http://www.foo.com/s?q=foo", |
| 286 result.match_at(0)->destination_url.spec()); | 317 result.match_at(0)->destination_url.spec()); |
| 287 EXPECT_EQ(1300, result.match_at(0)->relevance); | 318 EXPECT_EQ(1300, result.match_at(0)->relevance); |
| 288 EXPECT_EQ("http://www.foo.com/s?q=foo2", | 319 EXPECT_EQ("http://www.foo.com/s?q=foo2", |
| 289 result.match_at(1)->destination_url.spec()); | 320 result.match_at(1)->destination_url.spec()); |
| 290 EXPECT_EQ(1200, result.match_at(1)->relevance); | 321 EXPECT_EQ(1200, result.match_at(1)->relevance); |
| 291 EXPECT_EQ("http://www.foo.com/", | 322 EXPECT_EQ("http://www.foo.com/", |
| 292 result.match_at(2)->destination_url.spec()); | 323 result.match_at(2)->destination_url.spec()); |
| 293 EXPECT_EQ(900, result.match_at(2)->relevance); | 324 EXPECT_EQ(900, result.match_at(2)->relevance); |
| 294 } | 325 } |
| 326 | |
| 327 TEST_F(AutocompleteResultTest, SortAndCullReorderForDefaultMatch) { | |
| 328 TestData data[] = { | |
| 329 { 0, 0, 1300 }, | |
| 330 { 1, 0, 1200 }, | |
| 331 { 2, 0, 1100 }, | |
| 332 { 3, 0, 1000 } | |
| 333 }; | |
| 334 | |
| 335 // Must be the same as kBundledExperimentFieldTrialName | |
| 336 // defined in omnibox_field_trial.cc. | |
| 337 const std::string kTrialName = "OmniboxBundledExperimentV1"; | |
| 338 // Must be the same as kReorderForLegalDefaultMatchRule defined in | |
| 339 // omnibox_field_trial.cc. | |
| 340 const std::string kRuleName = "ReorderForLegalDefaultMatch"; | |
| 341 // Must be the same as the constant used in | |
| 342 // OmniboxFieldTrial::ReorderForLegalDefaultMatch(). | |
| 343 const std::string kReorderEnabled = "ReorderForLegalDefaultMatch"; | |
| 344 std::map<std::string, std::string> params; | |
| 345 // Enable reorder for omnibox inputs on the user's homepage. | |
| 346 params[kRuleName + ":3"] = kReorderEnabled; | |
| 347 ASSERT_TRUE(chrome_variations::AssociateVariationParams( | |
| 348 kTrialName, "A", params)); | |
| 349 base::FieldTrialList::CreateFieldTrial(kTrialName, "A"); | |
| 350 | |
| 351 { | |
| 352 // Check that reorder doesn't do anything if the top result | |
| 353 // is already a legal default match (which is the default from | |
| 354 // PopulateAutocompleteMatches()). | |
| 355 ACMatches matches; | |
| 356 PopulateAutocompleteMatches(data, arraysize(data), &matches); | |
| 357 AutocompleteResult result; | |
| 358 result.AppendMatches(matches); | |
| 359 AutocompleteInput input(string16(), string16::npos, string16(), GURL(), | |
| 360 AutocompleteInput::HOMEPAGE, false, false, false, | |
| 361 AutocompleteInput::ALL_MATCHES); | |
| 362 result.SortAndCull(input, test_util_.profile()); | |
| 363 AssertResultMatches(result, data, 4); | |
| 364 } | |
| 365 | |
| 366 { | |
| 367 // Check that reorder swaps up a result appropriately. | |
| 368 ACMatches matches; | |
| 369 PopulateAutocompleteMatches(data, arraysize(data), &matches); | |
| 370 matches[0].allowed_to_be_default_match = false; | |
| 371 matches[1].allowed_to_be_default_match = false; | |
| 372 AutocompleteResult result; | |
| 373 result.AppendMatches(matches); | |
| 374 AutocompleteInput input(string16(), string16::npos, string16(), GURL(), | |
| 375 AutocompleteInput::HOMEPAGE, false, false, false, | |
| 376 AutocompleteInput::ALL_MATCHES); | |
| 377 result.SortAndCull(input, test_util_.profile()); | |
| 378 ASSERT_EQ(4U, result.size()); | |
| 379 EXPECT_EQ("http://c/", result.match_at(0)->destination_url.spec()); | |
| 380 EXPECT_EQ("http://a/", result.match_at(1)->destination_url.spec()); | |
| 381 EXPECT_EQ("http://b/", result.match_at(2)->destination_url.spec()); | |
| 382 EXPECT_EQ("http://d/", result.match_at(3)->destination_url.spec()); | |
| 383 } | |
| 384 } | |
| OLD | NEW |