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/scored_history_match.h" | |
| 6 | |
| 5 #include <algorithm> | 7 #include <algorithm> |
| 6 | 8 |
| 7 #include "base/auto_reset.h" | 9 #include "base/auto_reset.h" |
| 8 #include "base/bind.h" | 10 #include "base/bind.h" |
| 9 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/strings/string16.h" | 12 #include "base/strings/string16.h" |
| 11 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 12 #include "chrome/browser/autocomplete/scored_history_match_builder_impl.h" | |
| 13 #include "testing/gmock/include/gmock/gmock.h" | 14 #include "testing/gmock/include/gmock/gmock.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 16 |
| 16 using base::ASCIIToUTF16; | 17 using base::ASCIIToUTF16; |
| 17 using testing::ElementsAre; | 18 using testing::ElementsAre; |
| 18 using testing::Pair; | 19 using testing::Pair; |
| 19 | 20 |
| 20 namespace { | 21 namespace { |
| 21 | 22 |
| 22 // Returns a VisitInfoVector that includes |num_visits| spread over the | 23 // Returns a VisitInfoVector that includes |num_visits| spread over the |
| 23 // last |frequency|*|num_visits| days (relative to |now|). A frequency of | 24 // last |frequency|*|num_visits| days (relative to |now|). A frequency of |
| 24 // one means one visit each day, two means every other day, etc. | 25 // one means one visit each day, two means every other day, etc. |
| 25 VisitInfoVector CreateVisitInfoVector(int num_visits, | 26 VisitInfoVector CreateVisitInfoVector(int num_visits, |
| 26 int frequency, | 27 int frequency, |
| 27 base::Time now) { | 28 base::Time now) { |
| 28 VisitInfoVector visits; | 29 VisitInfoVector visits; |
| 29 for (int i = 0; i < num_visits; ++i) { | 30 for (int i = 0; i < num_visits; ++i) { |
| 30 visits.push_back( | 31 visits.push_back( |
| 31 std::make_pair(now - base::TimeDelta::FromDays(i * frequency), | 32 std::make_pair(now - base::TimeDelta::FromDays(i * frequency), |
| 32 ui::PAGE_TRANSITION_LINK)); | 33 ui::PAGE_TRANSITION_LINK)); |
| 33 } | 34 } |
| 34 return visits; | 35 return visits; |
| 35 } | 36 } |
| 36 | 37 |
| 37 } // namespace | 38 } // namespace |
| 38 | 39 |
| 39 class ScoredHistoryMatchBuilderImplTest : public testing::Test { | 40 class ScoredHistoryMatchTest : public testing::Test { |
| 40 protected: | 41 protected: |
| 41 // Convenience function to create a history::URLRow with basic data for |url|, | 42 // Convenience function to create a history::URLRow with basic data for |url|, |
| 42 // |title|, |visit_count|, and |typed_count|. |days_since_last_visit| gives | 43 // |title|, |visit_count|, and |typed_count|. |days_since_last_visit| gives |
| 43 // the number of days ago to which to set the URL's last_visit. | 44 // the number of days ago to which to set the URL's last_visit. |
| 44 history::URLRow MakeURLRow(const char* url, | 45 history::URLRow MakeURLRow(const char* url, |
| 45 const char* title, | 46 const char* title, |
| 46 int visit_count, | 47 int visit_count, |
| 47 int days_since_last_visit, | 48 int days_since_last_visit, |
| 48 int typed_count); | 49 int typed_count); |
| 49 | 50 |
| 50 // Convenience function to set the word starts information from a | 51 // Convenience function to set the word starts information from a |
| 51 // history::URLRow's URL and title. | 52 // history::URLRow's URL and title. |
| 52 void PopulateWordStarts(const history::URLRow& url_row, | 53 void PopulateWordStarts(const history::URLRow& url_row, |
| 53 RowWordStarts* word_starts); | 54 RowWordStarts* word_starts); |
| 54 | 55 |
| 55 // Convenience functions for easily creating vectors of search terms. | 56 // Convenience functions for easily creating vectors of search terms. |
| 56 String16Vector Make1Term(const char* term) const; | 57 String16Vector Make1Term(const char* term) const; |
| 57 String16Vector Make2Terms(const char* term_1, const char* term_2) const; | 58 String16Vector Make2Terms(const char* term_1, const char* term_2) const; |
| 58 | 59 |
| 59 // Convenience function for GetTopicalityScore() that builds the term match | 60 // Convenience function for GetTopicalityScore() that builds the term match |
| 60 // and word break information automatically that are needed to call | 61 // and word break information automatically that are needed to call |
| 61 // GetTopicalityScore(). It only works for scoring a single term, not | 62 // GetTopicalityScore(). It only works for scoring a single term, not |
| 62 // multiple terms. | 63 // multiple terms. |
| 63 float GetTopicalityScoreOfTermAgainstURLAndTitle(const base::string16& term, | 64 float GetTopicalityScoreOfTermAgainstURLAndTitle(const base::string16& term, |
| 64 const base::string16& url, | 65 const base::string16& url, |
| 65 const base::string16& title); | 66 const base::string16& title); |
| 66 | 67 |
| 67 // Registers an URL as bookmarked. | |
| 68 void AddBookmark(const GURL& url); | |
| 69 | |
| 70 // Returns the ScoredHistoryMatch::Builder to use. | |
| 71 ScoredHistoryMatch::Builder* builder() { return builder_.get(); } | |
| 72 | |
| 73 private: | 68 private: |
| 74 // Returns whether an URL is bookmarked, used by | |
| 75 // ScoredHistoryMatchBuilderImpl. | |
| 76 bool IsBookmarked(const GURL& url); | |
| 77 | |
| 78 // testing::Test implementation. | 69 // testing::Test implementation. |
| 79 void SetUp() override; | |
| 80 void TearDown() override; | 70 void TearDown() override; |
| 81 | 71 |
| 82 std::set<GURL> bookmarked_urls_; | 72 std::set<GURL> bookmarked_urls_; |
|
Mark P
2015/03/10 00:28:12
Do you need this and TearDown anymore given your n
sdefresne
2015/03/10 10:43:28
No. Removed.
| |
| 83 scoped_ptr<ScoredHistoryMatch::Builder> builder_; | |
| 84 }; | 73 }; |
| 85 | 74 |
| 86 history::URLRow ScoredHistoryMatchBuilderImplTest::MakeURLRow( | 75 history::URLRow ScoredHistoryMatchTest::MakeURLRow(const char* url, |
| 87 const char* url, | 76 const char* title, |
| 88 const char* title, | 77 int visit_count, |
| 89 int visit_count, | 78 int days_since_last_visit, |
| 90 int days_since_last_visit, | 79 int typed_count) { |
| 91 int typed_count) { | |
| 92 history::URLRow row(GURL(url), 0); | 80 history::URLRow row(GURL(url), 0); |
| 93 row.set_title(ASCIIToUTF16(title)); | 81 row.set_title(ASCIIToUTF16(title)); |
| 94 row.set_visit_count(visit_count); | 82 row.set_visit_count(visit_count); |
| 95 row.set_typed_count(typed_count); | 83 row.set_typed_count(typed_count); |
| 96 row.set_last_visit(base::Time::NowFromSystemTime() - | 84 row.set_last_visit(base::Time::NowFromSystemTime() - |
| 97 base::TimeDelta::FromDays(days_since_last_visit)); | 85 base::TimeDelta::FromDays(days_since_last_visit)); |
| 98 return row; | 86 return row; |
| 99 } | 87 } |
| 100 | 88 |
| 101 void ScoredHistoryMatchBuilderImplTest::PopulateWordStarts( | 89 void ScoredHistoryMatchTest::PopulateWordStarts(const history::URLRow& url_row, |
| 102 const history::URLRow& url_row, | 90 RowWordStarts* word_starts) { |
| 103 RowWordStarts* word_starts) { | |
| 104 String16SetFromString16(ASCIIToUTF16(url_row.url().spec()), | 91 String16SetFromString16(ASCIIToUTF16(url_row.url().spec()), |
| 105 &word_starts->url_word_starts_); | 92 &word_starts->url_word_starts_); |
| 106 String16SetFromString16(url_row.title(), &word_starts->title_word_starts_); | 93 String16SetFromString16(url_row.title(), &word_starts->title_word_starts_); |
| 107 } | 94 } |
| 108 | 95 |
| 109 String16Vector ScoredHistoryMatchBuilderImplTest::Make1Term( | 96 String16Vector ScoredHistoryMatchTest::Make1Term(const char* term) const { |
| 110 const char* term) const { | |
| 111 String16Vector original_terms; | 97 String16Vector original_terms; |
| 112 original_terms.push_back(ASCIIToUTF16(term)); | 98 original_terms.push_back(ASCIIToUTF16(term)); |
| 113 return original_terms; | 99 return original_terms; |
| 114 } | 100 } |
| 115 | 101 |
| 116 String16Vector ScoredHistoryMatchBuilderImplTest::Make2Terms( | 102 String16Vector ScoredHistoryMatchTest::Make2Terms(const char* term_1, |
| 117 const char* term_1, | 103 const char* term_2) const { |
| 118 const char* term_2) const { | |
| 119 String16Vector original_terms; | 104 String16Vector original_terms; |
| 120 original_terms.push_back(ASCIIToUTF16(term_1)); | 105 original_terms.push_back(ASCIIToUTF16(term_1)); |
| 121 original_terms.push_back(ASCIIToUTF16(term_2)); | 106 original_terms.push_back(ASCIIToUTF16(term_2)); |
| 122 return original_terms; | 107 return original_terms; |
| 123 } | 108 } |
| 124 | 109 |
| 125 float ScoredHistoryMatchBuilderImplTest:: | 110 float ScoredHistoryMatchTest::GetTopicalityScoreOfTermAgainstURLAndTitle( |
| 126 GetTopicalityScoreOfTermAgainstURLAndTitle(const base::string16& term, | 111 const base::string16& term, |
| 127 const base::string16& url, | 112 const base::string16& url, |
| 128 const base::string16& title) { | 113 const base::string16& title) { |
| 129 // Make an empty match and simply populate the fields we need in order | 114 // Make an empty match and simply populate the fields we need in order |
| 130 // to call GetTopicalityScore(). | 115 // to call GetTopicalityScore(). |
| 131 ScoredHistoryMatch scored_match; | 116 ScoredHistoryMatch scored_match; |
| 132 scored_match.url_matches = MatchTermInString(term, url, 0); | 117 scored_match.url_matches = MatchTermInString(term, url, 0); |
| 133 scored_match.title_matches = MatchTermInString(term, title, 0); | 118 scored_match.title_matches = MatchTermInString(term, title, 0); |
| 134 RowWordStarts word_starts; | 119 RowWordStarts word_starts; |
| 135 String16SetFromString16(url, &word_starts.url_word_starts_); | 120 String16SetFromString16(url, &word_starts.url_word_starts_); |
| 136 String16SetFromString16(title, &word_starts.title_word_starts_); | 121 String16SetFromString16(title, &word_starts.title_word_starts_); |
| 137 WordStarts one_word_no_offset(1, 0u); | 122 WordStarts one_word_no_offset(1, 0u); |
| 138 return ScoredHistoryMatchBuilderImpl::GetTopicalityScore( | 123 return scored_match.GetTopicalityScore(1, url, one_word_no_offset, |
| 139 1, url, one_word_no_offset, word_starts, &scored_match); | 124 word_starts); |
| 140 } | 125 } |
| 141 | 126 |
| 142 void ScoredHistoryMatchBuilderImplTest::AddBookmark(const GURL& url) { | 127 void ScoredHistoryMatchTest::TearDown() { |
| 143 bookmarked_urls_.insert(url); | 128 bookmarked_urls_.clear(); |
| 144 } | 129 } |
| 145 | 130 |
| 146 bool ScoredHistoryMatchBuilderImplTest::IsBookmarked(const GURL& url) { | 131 TEST_F(ScoredHistoryMatchTest, Scoring) { |
| 147 return bookmarked_urls_.find(url) != bookmarked_urls_.end(); | |
| 148 } | |
| 149 | |
| 150 void ScoredHistoryMatchBuilderImplTest::SetUp() { | |
| 151 builder_.reset(new ScoredHistoryMatchBuilderImpl( | |
| 152 base::Bind(&ScoredHistoryMatchBuilderImplTest::IsBookmarked, | |
| 153 base::Unretained(this)))); | |
| 154 } | |
| 155 | |
| 156 void ScoredHistoryMatchBuilderImplTest::TearDown() { | |
| 157 bookmarked_urls_.clear(); | |
| 158 builder_.reset(); | |
| 159 } | |
| 160 | |
| 161 TEST_F(ScoredHistoryMatchBuilderImplTest, Scoring) { | |
| 162 // We use NowFromSystemTime() because MakeURLRow uses the same function | 132 // We use NowFromSystemTime() because MakeURLRow uses the same function |
| 163 // to calculate last visit time when building a row. | 133 // to calculate last visit time when building a row. |
| 164 base::Time now = base::Time::NowFromSystemTime(); | 134 base::Time now = base::Time::NowFromSystemTime(); |
| 165 | 135 |
| 166 history::URLRow row_a(MakeURLRow("http://fedcba", "abcd bcd", 3, 30, 1)); | 136 history::URLRow row_a(MakeURLRow("http://fedcba", "abcd bcd", 3, 30, 1)); |
| 167 RowWordStarts word_starts_a; | 137 RowWordStarts word_starts_a; |
| 168 PopulateWordStarts(row_a, &word_starts_a); | 138 PopulateWordStarts(row_a, &word_starts_a); |
| 169 WordStarts one_word_no_offset(1, 0u); | 139 WordStarts one_word_no_offset(1, 0u); |
| 170 VisitInfoVector visits_a = CreateVisitInfoVector(3, 30, now); | 140 VisitInfoVector visits_a = CreateVisitInfoVector(3, 30, now); |
| 171 // Mark one visit as typed. | 141 // Mark one visit as typed. |
| 172 visits_a[0].second = ui::PAGE_TRANSITION_TYPED; | 142 visits_a[0].second = ui::PAGE_TRANSITION_TYPED; |
| 173 ScoredHistoryMatch scored_a = builder()->Build( | 143 ScoredHistoryMatch scored_a(row_a, visits_a, std::string(), |
| 174 row_a, visits_a, std::string(), ASCIIToUTF16("abc"), Make1Term("abc"), | 144 ASCIIToUTF16("abc"), Make1Term("abc"), |
| 175 one_word_no_offset, word_starts_a, now); | 145 one_word_no_offset, word_starts_a, false, now); |
| 176 | 146 |
| 177 // Test scores based on visit_count. | 147 // Test scores based on visit_count. |
| 178 history::URLRow row_b(MakeURLRow("http://abcdef", "abcd bcd", 10, 30, 1)); | 148 history::URLRow row_b(MakeURLRow("http://abcdef", "abcd bcd", 10, 30, 1)); |
| 179 RowWordStarts word_starts_b; | 149 RowWordStarts word_starts_b; |
| 180 PopulateWordStarts(row_b, &word_starts_b); | 150 PopulateWordStarts(row_b, &word_starts_b); |
| 181 VisitInfoVector visits_b = CreateVisitInfoVector(10, 30, now); | 151 VisitInfoVector visits_b = CreateVisitInfoVector(10, 30, now); |
| 182 visits_b[0].second = ui::PAGE_TRANSITION_TYPED; | 152 visits_b[0].second = ui::PAGE_TRANSITION_TYPED; |
| 183 ScoredHistoryMatch scored_b = builder()->Build( | 153 ScoredHistoryMatch scored_b(row_b, visits_b, std::string(), |
| 184 row_b, visits_b, std::string(), ASCIIToUTF16("abc"), Make1Term("abc"), | 154 ASCIIToUTF16("abc"), Make1Term("abc"), |
| 185 one_word_no_offset, word_starts_b, now); | 155 one_word_no_offset, word_starts_b, false, now); |
| 186 EXPECT_GT(scored_b.raw_score, scored_a.raw_score); | 156 EXPECT_GT(scored_b.raw_score, scored_a.raw_score); |
| 187 | 157 |
| 188 // Test scores based on last_visit. | 158 // Test scores based on last_visit. |
| 189 history::URLRow row_c(MakeURLRow("http://abcdef", "abcd bcd", 3, 10, 1)); | 159 history::URLRow row_c(MakeURLRow("http://abcdef", "abcd bcd", 3, 10, 1)); |
| 190 RowWordStarts word_starts_c; | 160 RowWordStarts word_starts_c; |
| 191 PopulateWordStarts(row_c, &word_starts_c); | 161 PopulateWordStarts(row_c, &word_starts_c); |
| 192 VisitInfoVector visits_c = CreateVisitInfoVector(3, 10, now); | 162 VisitInfoVector visits_c = CreateVisitInfoVector(3, 10, now); |
| 193 visits_c[0].second = ui::PAGE_TRANSITION_TYPED; | 163 visits_c[0].second = ui::PAGE_TRANSITION_TYPED; |
| 194 ScoredHistoryMatch scored_c = builder()->Build( | 164 ScoredHistoryMatch scored_c(row_c, visits_c, std::string(), |
| 195 row_c, visits_c, std::string(), ASCIIToUTF16("abc"), Make1Term("abc"), | 165 ASCIIToUTF16("abc"), Make1Term("abc"), |
| 196 one_word_no_offset, word_starts_c, now); | 166 one_word_no_offset, word_starts_c, false, now); |
| 197 EXPECT_GT(scored_c.raw_score, scored_a.raw_score); | 167 EXPECT_GT(scored_c.raw_score, scored_a.raw_score); |
| 198 | 168 |
| 199 // Test scores based on typed_count. | 169 // Test scores based on typed_count. |
| 200 history::URLRow row_d(MakeURLRow("http://abcdef", "abcd bcd", 3, 30, 3)); | 170 history::URLRow row_d(MakeURLRow("http://abcdef", "abcd bcd", 3, 30, 3)); |
| 201 RowWordStarts word_starts_d; | 171 RowWordStarts word_starts_d; |
| 202 PopulateWordStarts(row_d, &word_starts_d); | 172 PopulateWordStarts(row_d, &word_starts_d); |
| 203 VisitInfoVector visits_d = CreateVisitInfoVector(3, 30, now); | 173 VisitInfoVector visits_d = CreateVisitInfoVector(3, 30, now); |
| 204 visits_d[0].second = ui::PAGE_TRANSITION_TYPED; | 174 visits_d[0].second = ui::PAGE_TRANSITION_TYPED; |
| 205 visits_d[1].second = ui::PAGE_TRANSITION_TYPED; | 175 visits_d[1].second = ui::PAGE_TRANSITION_TYPED; |
| 206 visits_d[2].second = ui::PAGE_TRANSITION_TYPED; | 176 visits_d[2].second = ui::PAGE_TRANSITION_TYPED; |
| 207 ScoredHistoryMatch scored_d = builder()->Build( | 177 ScoredHistoryMatch scored_d(row_d, visits_d, std::string(), |
| 208 row_d, visits_d, std::string(), ASCIIToUTF16("abc"), Make1Term("abc"), | 178 ASCIIToUTF16("abc"), Make1Term("abc"), |
| 209 one_word_no_offset, word_starts_d, now); | 179 one_word_no_offset, word_starts_d, false, now); |
| 210 EXPECT_GT(scored_d.raw_score, scored_a.raw_score); | 180 EXPECT_GT(scored_d.raw_score, scored_a.raw_score); |
| 211 | 181 |
| 212 // Test scores based on a terms appearing multiple times. | 182 // Test scores based on a terms appearing multiple times. |
| 213 history::URLRow row_e(MakeURLRow( | 183 history::URLRow row_e(MakeURLRow( |
| 214 "http://csi.csi.csi/csi_csi", | 184 "http://csi.csi.csi/csi_csi", |
| 215 "CSI Guide to CSI Las Vegas, CSI New York, CSI Provo", 3, 30, 3)); | 185 "CSI Guide to CSI Las Vegas, CSI New York, CSI Provo", 3, 30, 3)); |
| 216 RowWordStarts word_starts_e; | 186 RowWordStarts word_starts_e; |
| 217 PopulateWordStarts(row_e, &word_starts_e); | 187 PopulateWordStarts(row_e, &word_starts_e); |
| 218 const VisitInfoVector visits_e = visits_d; | 188 const VisitInfoVector visits_e = visits_d; |
| 219 ScoredHistoryMatch scored_e = builder()->Build( | 189 ScoredHistoryMatch scored_e(row_e, visits_e, std::string(), |
| 220 row_e, visits_e, std::string(), ASCIIToUTF16("csi"), Make1Term("csi"), | 190 ASCIIToUTF16("csi"), Make1Term("csi"), |
| 221 one_word_no_offset, word_starts_e, now); | 191 one_word_no_offset, word_starts_e, false, now); |
| 222 EXPECT_LT(scored_e.raw_score, 1400); | 192 EXPECT_LT(scored_e.raw_score, 1400); |
| 223 | 193 |
| 224 // Test that a result with only a mid-term match (i.e., not at a word | 194 // Test that a result with only a mid-term match (i.e., not at a word |
| 225 // boundary) scores 0. | 195 // boundary) scores 0. |
| 226 ScoredHistoryMatch scored_f = | 196 ScoredHistoryMatch scored_f(row_a, visits_a, std::string(), |
| 227 builder()->Build(row_a, visits_a, std::string(), ASCIIToUTF16("cd"), | 197 ASCIIToUTF16("cd"), Make1Term("cd"), |
| 228 Make1Term("cd"), one_word_no_offset, word_starts_a, now); | 198 one_word_no_offset, word_starts_a, false, now); |
| 229 EXPECT_EQ(scored_f.raw_score, 0); | 199 EXPECT_EQ(scored_f.raw_score, 0); |
| 230 } | 200 } |
| 231 | 201 |
| 232 TEST_F(ScoredHistoryMatchBuilderImplTest, ScoringBookmarks) { | 202 TEST_F(ScoredHistoryMatchTest, ScoringBookmarks) { |
| 233 // We use NowFromSystemTime() because MakeURLRow uses the same function | 203 // We use NowFromSystemTime() because MakeURLRow uses the same function |
| 234 // to calculate last visit time when building a row. | 204 // to calculate last visit time when building a row. |
| 235 base::Time now = base::Time::NowFromSystemTime(); | 205 base::Time now = base::Time::NowFromSystemTime(); |
| 236 | 206 |
| 237 std::string url_string("http://fedcba"); | 207 std::string url_string("http://fedcba"); |
| 238 const GURL url(url_string); | 208 const GURL url(url_string); |
| 239 history::URLRow row(MakeURLRow(url_string.c_str(), "abcd bcd", 8, 3, 1)); | 209 history::URLRow row(MakeURLRow(url_string.c_str(), "abcd bcd", 8, 3, 1)); |
| 240 RowWordStarts word_starts; | 210 RowWordStarts word_starts; |
| 241 PopulateWordStarts(row, &word_starts); | 211 PopulateWordStarts(row, &word_starts); |
| 242 WordStarts one_word_no_offset(1, 0u); | 212 WordStarts one_word_no_offset(1, 0u); |
| 243 VisitInfoVector visits = CreateVisitInfoVector(8, 3, now); | 213 VisitInfoVector visits = CreateVisitInfoVector(8, 3, now); |
| 244 ScoredHistoryMatch scored = | 214 ScoredHistoryMatch scored(row, visits, std::string(), ASCIIToUTF16("abc"), |
| 245 builder()->Build(row, visits, std::string(), ASCIIToUTF16("abc"), | 215 Make1Term("abc"), one_word_no_offset, word_starts, |
| 246 Make1Term("abc"), one_word_no_offset, word_starts, now); | 216 false, now); |
| 247 // Now bookmark that URL and make sure its score increases. | 217 // Now check that if URL is bookmarked then its score increases. |
| 248 base::AutoReset<int> reset(&ScoredHistoryMatchBuilderImpl::bookmark_value_, | 218 base::AutoReset<int> reset(&ScoredHistoryMatch::bookmark_value_, 5); |
| 249 5); | 219 ScoredHistoryMatch scored_with_bookmark( |
| 250 AddBookmark(url); | 220 row, visits, std::string(), ASCIIToUTF16("abc"), Make1Term("abc"), |
| 251 ScoredHistoryMatch scored_with_bookmark = | 221 one_word_no_offset, word_starts, true, now); |
| 252 builder()->Build(row, visits, std::string(), ASCIIToUTF16("abc"), | |
| 253 Make1Term("abc"), one_word_no_offset, word_starts, now); | |
| 254 EXPECT_GT(scored_with_bookmark.raw_score, scored.raw_score); | 222 EXPECT_GT(scored_with_bookmark.raw_score, scored.raw_score); |
| 255 } | 223 } |
| 256 | 224 |
| 257 TEST_F(ScoredHistoryMatchBuilderImplTest, ScoringTLD) { | 225 TEST_F(ScoredHistoryMatchTest, ScoringTLD) { |
| 258 // We use NowFromSystemTime() because MakeURLRow uses the same function | 226 // We use NowFromSystemTime() because MakeURLRow uses the same function |
| 259 // to calculate last visit time when building a row. | 227 // to calculate last visit time when building a row. |
| 260 base::Time now = base::Time::NowFromSystemTime(); | 228 base::Time now = base::Time::NowFromSystemTime(); |
| 261 | 229 |
| 262 // By default the URL should not be returned for a query that includes "com". | 230 // By default the URL should not be returned for a query that includes "com". |
| 263 std::string url_string("http://fedcba.com/"); | 231 std::string url_string("http://fedcba.com/"); |
| 264 const GURL url(url_string); | 232 const GURL url(url_string); |
| 265 history::URLRow row(MakeURLRow(url_string.c_str(), "", 8, 3, 1)); | 233 history::URLRow row(MakeURLRow(url_string.c_str(), "", 8, 3, 1)); |
| 266 RowWordStarts word_starts; | 234 RowWordStarts word_starts; |
| 267 PopulateWordStarts(row, &word_starts); | 235 PopulateWordStarts(row, &word_starts); |
| 268 WordStarts two_words_no_offsets(2, 0u); | 236 WordStarts two_words_no_offsets(2, 0u); |
| 269 VisitInfoVector visits = CreateVisitInfoVector(8, 3, now); | 237 VisitInfoVector visits = CreateVisitInfoVector(8, 3, now); |
| 270 ScoredHistoryMatch scored = builder()->Build( | 238 ScoredHistoryMatch scored(row, visits, std::string(), ASCIIToUTF16("fed com"), |
| 271 row, visits, std::string(), ASCIIToUTF16("fed com"), | 239 Make2Terms("fed", "com"), two_words_no_offsets, |
| 272 Make2Terms("fed", "com"), two_words_no_offsets, word_starts, now); | 240 word_starts, false, now); |
| 273 EXPECT_EQ(0, scored.raw_score); | 241 EXPECT_EQ(0, scored.raw_score); |
| 274 | 242 |
| 275 // Now allow credit for the match in the TLD. | 243 // Now allow credit for the match in the TLD. |
| 276 base::AutoReset<bool> reset( | 244 base::AutoReset<bool> reset(&ScoredHistoryMatch::allow_tld_matches_, true); |
| 277 &ScoredHistoryMatchBuilderImpl::allow_tld_matches_, true); | 245 ScoredHistoryMatch scored_with_tld( |
| 278 ScoredHistoryMatch scored_with_tld = builder()->Build( | |
| 279 row, visits, std::string(), ASCIIToUTF16("fed com"), | 246 row, visits, std::string(), ASCIIToUTF16("fed com"), |
| 280 Make2Terms("fed", "com"), two_words_no_offsets, word_starts, now); | 247 Make2Terms("fed", "com"), two_words_no_offsets, word_starts, false, now); |
| 281 EXPECT_GT(scored_with_tld.raw_score, 0); | 248 EXPECT_GT(scored_with_tld.raw_score, 0); |
| 282 } | 249 } |
| 283 | 250 |
| 284 TEST_F(ScoredHistoryMatchBuilderImplTest, ScoringScheme) { | 251 TEST_F(ScoredHistoryMatchTest, ScoringScheme) { |
| 285 // We use NowFromSystemTime() because MakeURLRow uses the same function | 252 // We use NowFromSystemTime() because MakeURLRow uses the same function |
| 286 // to calculate last visit time when building a row. | 253 // to calculate last visit time when building a row. |
| 287 base::Time now = base::Time::NowFromSystemTime(); | 254 base::Time now = base::Time::NowFromSystemTime(); |
| 288 | 255 |
| 289 // By default the URL should not be returned for a query that includes "http". | 256 // By default the URL should not be returned for a query that includes "http". |
| 290 std::string url_string("http://fedcba/"); | 257 std::string url_string("http://fedcba/"); |
| 291 const GURL url(url_string); | 258 const GURL url(url_string); |
| 292 history::URLRow row(MakeURLRow(url_string.c_str(), "", 8, 3, 1)); | 259 history::URLRow row(MakeURLRow(url_string.c_str(), "", 8, 3, 1)); |
| 293 RowWordStarts word_starts; | 260 RowWordStarts word_starts; |
| 294 PopulateWordStarts(row, &word_starts); | 261 PopulateWordStarts(row, &word_starts); |
| 295 WordStarts two_words_no_offsets(2, 0u); | 262 WordStarts two_words_no_offsets(2, 0u); |
| 296 VisitInfoVector visits = CreateVisitInfoVector(8, 3, now); | 263 VisitInfoVector visits = CreateVisitInfoVector(8, 3, now); |
| 297 ScoredHistoryMatch scored = builder()->Build( | 264 ScoredHistoryMatch scored(row, visits, std::string(), |
| 298 row, visits, std::string(), ASCIIToUTF16("fed http"), | 265 ASCIIToUTF16("fed http"), Make2Terms("fed", "http"), |
| 299 Make2Terms("fed", "http"), two_words_no_offsets, word_starts, now); | 266 two_words_no_offsets, word_starts, false, now); |
| 300 EXPECT_EQ(0, scored.raw_score); | 267 EXPECT_EQ(0, scored.raw_score); |
| 301 | 268 |
| 302 // Now allow credit for the match in the scheme. | 269 // Now allow credit for the match in the scheme. |
| 303 base::AutoReset<bool> reset( | 270 base::AutoReset<bool> reset(&ScoredHistoryMatch::allow_scheme_matches_, true); |
| 304 &ScoredHistoryMatchBuilderImpl::allow_scheme_matches_, true); | 271 ScoredHistoryMatch scored_with_scheme( |
| 305 ScoredHistoryMatch scored_with_scheme = builder()->Build( | |
| 306 row, visits, std::string(), ASCIIToUTF16("fed http"), | 272 row, visits, std::string(), ASCIIToUTF16("fed http"), |
| 307 Make2Terms("fed", "http"), two_words_no_offsets, word_starts, now); | 273 Make2Terms("fed", "http"), two_words_no_offsets, word_starts, false, now); |
| 308 EXPECT_GT(scored_with_scheme.raw_score, 0); | 274 EXPECT_GT(scored_with_scheme.raw_score, 0); |
| 309 } | 275 } |
| 310 | 276 |
| 311 TEST_F(ScoredHistoryMatchBuilderImplTest, Inlining) { | 277 TEST_F(ScoredHistoryMatchTest, Inlining) { |
| 312 // We use NowFromSystemTime() because MakeURLRow uses the same function | 278 // We use NowFromSystemTime() because MakeURLRow uses the same function |
| 313 // to calculate last visit time when building a row. | 279 // to calculate last visit time when building a row. |
| 314 base::Time now = base::Time::NowFromSystemTime(); | 280 base::Time now = base::Time::NowFromSystemTime(); |
| 315 RowWordStarts word_starts; | 281 RowWordStarts word_starts; |
| 316 WordStarts one_word_no_offset(1, 0u); | 282 WordStarts one_word_no_offset(1, 0u); |
| 317 VisitInfoVector visits; | 283 VisitInfoVector visits; |
| 318 | 284 |
| 319 { | 285 { |
| 320 history::URLRow row( | 286 history::URLRow row( |
| 321 MakeURLRow("http://www.google.com", "abcdef", 3, 30, 1)); | 287 MakeURLRow("http://www.google.com", "abcdef", 3, 30, 1)); |
| 322 PopulateWordStarts(row, &word_starts); | 288 PopulateWordStarts(row, &word_starts); |
| 323 ScoredHistoryMatch scored_a = | 289 ScoredHistoryMatch scored_a(row, visits, std::string(), ASCIIToUTF16("g"), |
| 324 builder()->Build(row, visits, std::string(), ASCIIToUTF16("g"), | 290 Make1Term("g"), one_word_no_offset, word_starts, |
| 325 Make1Term("g"), one_word_no_offset, word_starts, now); | 291 false, now); |
| 326 EXPECT_TRUE(scored_a.can_inline); | 292 EXPECT_TRUE(scored_a.can_inline); |
| 327 EXPECT_FALSE(scored_a.match_in_scheme); | 293 EXPECT_FALSE(scored_a.match_in_scheme); |
| 328 ScoredHistoryMatch scored_b = | 294 ScoredHistoryMatch scored_b(row, visits, std::string(), ASCIIToUTF16("w"), |
| 329 builder()->Build(row, visits, std::string(), ASCIIToUTF16("w"), | 295 Make1Term("w"), one_word_no_offset, word_starts, |
| 330 Make1Term("w"), one_word_no_offset, word_starts, now); | 296 false, now); |
| 331 EXPECT_TRUE(scored_b.can_inline); | 297 EXPECT_TRUE(scored_b.can_inline); |
| 332 EXPECT_FALSE(scored_b.match_in_scheme); | 298 EXPECT_FALSE(scored_b.match_in_scheme); |
| 333 ScoredHistoryMatch scored_c = | 299 ScoredHistoryMatch scored_c(row, visits, std::string(), ASCIIToUTF16("h"), |
| 334 builder()->Build(row, visits, std::string(), ASCIIToUTF16("h"), | 300 Make1Term("h"), one_word_no_offset, word_starts, |
| 335 Make1Term("h"), one_word_no_offset, word_starts, now); | 301 false, now); |
| 336 EXPECT_TRUE(scored_c.can_inline); | 302 EXPECT_TRUE(scored_c.can_inline); |
| 337 EXPECT_TRUE(scored_c.match_in_scheme); | 303 EXPECT_TRUE(scored_c.match_in_scheme); |
| 338 ScoredHistoryMatch scored_d = | 304 ScoredHistoryMatch scored_d(row, visits, std::string(), ASCIIToUTF16("o"), |
| 339 builder()->Build(row, visits, std::string(), ASCIIToUTF16("o"), | 305 Make1Term("o"), one_word_no_offset, word_starts, |
| 340 Make1Term("o"), one_word_no_offset, word_starts, now); | 306 false, now); |
| 341 EXPECT_FALSE(scored_d.can_inline); | 307 EXPECT_FALSE(scored_d.can_inline); |
| 342 EXPECT_FALSE(scored_d.match_in_scheme); | 308 EXPECT_FALSE(scored_d.match_in_scheme); |
| 343 } | 309 } |
| 344 | 310 |
| 345 { | 311 { |
| 346 history::URLRow row(MakeURLRow("http://teams.foo.com", "abcdef", 3, 30, 1)); | 312 history::URLRow row(MakeURLRow("http://teams.foo.com", "abcdef", 3, 30, 1)); |
| 347 PopulateWordStarts(row, &word_starts); | 313 PopulateWordStarts(row, &word_starts); |
| 348 ScoredHistoryMatch scored_a = | 314 ScoredHistoryMatch scored_a(row, visits, std::string(), ASCIIToUTF16("t"), |
| 349 builder()->Build(row, visits, std::string(), ASCIIToUTF16("t"), | 315 Make1Term("t"), one_word_no_offset, word_starts, |
| 350 Make1Term("t"), one_word_no_offset, word_starts, now); | 316 false, now); |
| 351 EXPECT_TRUE(scored_a.can_inline); | 317 EXPECT_TRUE(scored_a.can_inline); |
| 352 EXPECT_FALSE(scored_a.match_in_scheme); | 318 EXPECT_FALSE(scored_a.match_in_scheme); |
| 353 ScoredHistoryMatch scored_b = | 319 ScoredHistoryMatch scored_b(row, visits, std::string(), ASCIIToUTF16("f"), |
| 354 builder()->Build(row, visits, std::string(), ASCIIToUTF16("f"), | 320 Make1Term("f"), one_word_no_offset, word_starts, |
| 355 Make1Term("f"), one_word_no_offset, word_starts, now); | 321 false, now); |
| 356 EXPECT_FALSE(scored_b.can_inline); | 322 EXPECT_FALSE(scored_b.can_inline); |
| 357 EXPECT_FALSE(scored_b.match_in_scheme); | 323 EXPECT_FALSE(scored_b.match_in_scheme); |
| 358 ScoredHistoryMatch scored_c = | 324 ScoredHistoryMatch scored_c(row, visits, std::string(), ASCIIToUTF16("o"), |
| 359 builder()->Build(row, visits, std::string(), ASCIIToUTF16("o"), | 325 Make1Term("o"), one_word_no_offset, word_starts, |
| 360 Make1Term("o"), one_word_no_offset, word_starts, now); | 326 false, now); |
| 361 EXPECT_FALSE(scored_c.can_inline); | 327 EXPECT_FALSE(scored_c.can_inline); |
| 362 EXPECT_FALSE(scored_c.match_in_scheme); | 328 EXPECT_FALSE(scored_c.match_in_scheme); |
| 363 } | 329 } |
| 364 | 330 |
| 365 { | 331 { |
| 366 history::URLRow row( | 332 history::URLRow row( |
| 367 MakeURLRow("https://www.testing.com", "abcdef", 3, 30, 1)); | 333 MakeURLRow("https://www.testing.com", "abcdef", 3, 30, 1)); |
| 368 PopulateWordStarts(row, &word_starts); | 334 PopulateWordStarts(row, &word_starts); |
| 369 ScoredHistoryMatch scored_a = | 335 ScoredHistoryMatch scored_a(row, visits, std::string(), ASCIIToUTF16("t"), |
| 370 builder()->Build(row, visits, std::string(), ASCIIToUTF16("t"), | 336 Make1Term("t"), one_word_no_offset, word_starts, |
| 371 Make1Term("t"), one_word_no_offset, word_starts, now); | 337 false, now); |
| 372 EXPECT_TRUE(scored_a.can_inline); | 338 EXPECT_TRUE(scored_a.can_inline); |
| 373 EXPECT_FALSE(scored_a.match_in_scheme); | 339 EXPECT_FALSE(scored_a.match_in_scheme); |
| 374 ScoredHistoryMatch scored_b = | 340 ScoredHistoryMatch scored_b(row, visits, std::string(), ASCIIToUTF16("h"), |
| 375 builder()->Build(row, visits, std::string(), ASCIIToUTF16("h"), | 341 Make1Term("h"), one_word_no_offset, word_starts, |
| 376 Make1Term("h"), one_word_no_offset, word_starts, now); | 342 false, now); |
| 377 EXPECT_TRUE(scored_b.can_inline); | 343 EXPECT_TRUE(scored_b.can_inline); |
| 378 EXPECT_TRUE(scored_b.match_in_scheme); | 344 EXPECT_TRUE(scored_b.match_in_scheme); |
| 379 ScoredHistoryMatch scored_c = | 345 ScoredHistoryMatch scored_c(row, visits, std::string(), ASCIIToUTF16("w"), |
| 380 builder()->Build(row, visits, std::string(), ASCIIToUTF16("w"), | 346 Make1Term("w"), one_word_no_offset, word_starts, |
| 381 Make1Term("w"), one_word_no_offset, word_starts, now); | 347 false, now); |
| 382 EXPECT_TRUE(scored_c.can_inline); | 348 EXPECT_TRUE(scored_c.can_inline); |
| 383 EXPECT_FALSE(scored_c.match_in_scheme); | 349 EXPECT_FALSE(scored_c.match_in_scheme); |
| 384 } | 350 } |
| 385 } | 351 } |
| 386 | 352 |
| 387 TEST_F(ScoredHistoryMatchBuilderImplTest, GetTopicalityScoreTrailingSlash) { | 353 TEST_F(ScoredHistoryMatchTest, GetTopicalityScoreTrailingSlash) { |
| 388 const float hostname = GetTopicalityScoreOfTermAgainstURLAndTitle( | 354 const float hostname = GetTopicalityScoreOfTermAgainstURLAndTitle( |
| 389 ASCIIToUTF16("def"), ASCIIToUTF16("http://abc.def.com/"), | 355 ASCIIToUTF16("def"), ASCIIToUTF16("http://abc.def.com/"), |
| 390 ASCIIToUTF16("Non-Matching Title")); | 356 ASCIIToUTF16("Non-Matching Title")); |
| 391 const float hostname_no_slash = GetTopicalityScoreOfTermAgainstURLAndTitle( | 357 const float hostname_no_slash = GetTopicalityScoreOfTermAgainstURLAndTitle( |
| 392 ASCIIToUTF16("def"), ASCIIToUTF16("http://abc.def.com"), | 358 ASCIIToUTF16("def"), ASCIIToUTF16("http://abc.def.com"), |
| 393 ASCIIToUTF16("Non-Matching Title")); | 359 ASCIIToUTF16("Non-Matching Title")); |
| 394 EXPECT_EQ(hostname_no_slash, hostname); | 360 EXPECT_EQ(hostname_no_slash, hostname); |
| 395 } | 361 } |
| 396 | 362 |
| 397 // This function only tests scoring of single terms that match exactly | 363 // This function only tests scoring of single terms that match exactly |
| 398 // once somewhere in the URL or title. | 364 // once somewhere in the URL or title. |
| 399 TEST_F(ScoredHistoryMatchBuilderImplTest, GetTopicalityScore) { | 365 TEST_F(ScoredHistoryMatchTest, GetTopicalityScore) { |
| 400 base::string16 url = ASCIIToUTF16( | 366 base::string16 url = ASCIIToUTF16( |
| 401 "http://abc.def.com/path1/path2?" | 367 "http://abc.def.com/path1/path2?" |
| 402 "arg1=val1&arg2=val2#hash_component"); | 368 "arg1=val1&arg2=val2#hash_component"); |
| 403 base::string16 title = ASCIIToUTF16("here is a title"); | 369 base::string16 title = ASCIIToUTF16("here is a title"); |
| 404 const float hostname_score = | 370 const float hostname_score = GetTopicalityScoreOfTermAgainstURLAndTitle( |
| 405 GetTopicalityScoreOfTermAgainstURLAndTitle( | 371 ASCIIToUTF16("abc"), url, title); |
| 406 ASCIIToUTF16("abc"), url, title); | |
| 407 const float hostname_mid_word_score = | 372 const float hostname_mid_word_score = |
| 408 GetTopicalityScoreOfTermAgainstURLAndTitle( | 373 GetTopicalityScoreOfTermAgainstURLAndTitle(ASCIIToUTF16("bc"), url, |
| 409 ASCIIToUTF16("bc"), url, title); | 374 title); |
| 410 const float domain_name_score = | 375 const float domain_name_score = GetTopicalityScoreOfTermAgainstURLAndTitle( |
| 411 GetTopicalityScoreOfTermAgainstURLAndTitle( | 376 ASCIIToUTF16("def"), url, title); |
| 412 ASCIIToUTF16("def"), url, title); | |
| 413 const float domain_name_mid_word_score = | 377 const float domain_name_mid_word_score = |
| 414 GetTopicalityScoreOfTermAgainstURLAndTitle( | 378 GetTopicalityScoreOfTermAgainstURLAndTitle(ASCIIToUTF16("ef"), url, |
| 415 ASCIIToUTF16("ef"), url, title); | 379 title); |
| 416 const float tld_score = | 380 const float tld_score = GetTopicalityScoreOfTermAgainstURLAndTitle( |
| 417 GetTopicalityScoreOfTermAgainstURLAndTitle( | 381 ASCIIToUTF16("com"), url, title); |
| 418 ASCIIToUTF16("com"), url, title); | 382 const float tld_mid_word_score = GetTopicalityScoreOfTermAgainstURLAndTitle( |
| 419 const float tld_mid_word_score = | 383 ASCIIToUTF16("om"), url, title); |
| 420 GetTopicalityScoreOfTermAgainstURLAndTitle( | 384 const float path_score = GetTopicalityScoreOfTermAgainstURLAndTitle( |
| 421 ASCIIToUTF16("om"), url, title); | 385 ASCIIToUTF16("path1"), url, title); |
| 422 const float path_score = | 386 const float path_mid_word_score = GetTopicalityScoreOfTermAgainstURLAndTitle( |
| 423 GetTopicalityScoreOfTermAgainstURLAndTitle( | 387 ASCIIToUTF16("ath1"), url, title); |
| 424 ASCIIToUTF16("path1"), url, title); | 388 const float arg_score = GetTopicalityScoreOfTermAgainstURLAndTitle( |
| 425 const float path_mid_word_score = | 389 ASCIIToUTF16("arg2"), url, title); |
| 426 GetTopicalityScoreOfTermAgainstURLAndTitle( | 390 const float arg_mid_word_score = GetTopicalityScoreOfTermAgainstURLAndTitle( |
| 427 ASCIIToUTF16("ath1"), url, title); | 391 ASCIIToUTF16("rg2"), url, title); |
| 428 const float arg_score = | 392 const float protocol_score = GetTopicalityScoreOfTermAgainstURLAndTitle( |
| 429 GetTopicalityScoreOfTermAgainstURLAndTitle( | 393 ASCIIToUTF16("htt"), url, title); |
| 430 ASCIIToUTF16("arg2"), url, title); | |
| 431 const float arg_mid_word_score = | |
| 432 GetTopicalityScoreOfTermAgainstURLAndTitle( | |
| 433 ASCIIToUTF16("rg2"), url, title); | |
| 434 const float protocol_score = | |
| 435 GetTopicalityScoreOfTermAgainstURLAndTitle( | |
| 436 ASCIIToUTF16("htt"), url, title); | |
| 437 const float protocol_mid_word_score = | 394 const float protocol_mid_word_score = |
| 438 GetTopicalityScoreOfTermAgainstURLAndTitle( | 395 GetTopicalityScoreOfTermAgainstURLAndTitle(ASCIIToUTF16("tt"), url, |
| 439 ASCIIToUTF16("tt"), url, title); | 396 title); |
| 440 const float title_score = | 397 const float title_score = GetTopicalityScoreOfTermAgainstURLAndTitle( |
| 441 GetTopicalityScoreOfTermAgainstURLAndTitle( | 398 ASCIIToUTF16("her"), url, title); |
| 442 ASCIIToUTF16("her"), url, title); | 399 const float title_mid_word_score = GetTopicalityScoreOfTermAgainstURLAndTitle( |
| 443 const float title_mid_word_score = | 400 ASCIIToUTF16("er"), url, title); |
| 444 GetTopicalityScoreOfTermAgainstURLAndTitle( | |
| 445 ASCIIToUTF16("er"), url, title); | |
| 446 // Verify hostname and domain name > path > arg. | 401 // Verify hostname and domain name > path > arg. |
| 447 EXPECT_GT(hostname_score, path_score); | 402 EXPECT_GT(hostname_score, path_score); |
| 448 EXPECT_GT(domain_name_score, path_score); | 403 EXPECT_GT(domain_name_score, path_score); |
| 449 EXPECT_GT(path_score, arg_score); | 404 EXPECT_GT(path_score, arg_score); |
| 450 // Verify that domain name > path and domain name > arg for non-word | 405 // Verify that domain name > path and domain name > arg for non-word |
| 451 // boundaries. | 406 // boundaries. |
| 452 EXPECT_GT(hostname_mid_word_score, path_mid_word_score); | 407 EXPECT_GT(hostname_mid_word_score, path_mid_word_score); |
| 453 EXPECT_GT(domain_name_mid_word_score, path_mid_word_score); | 408 EXPECT_GT(domain_name_mid_word_score, path_mid_word_score); |
| 454 EXPECT_GT(domain_name_mid_word_score, arg_mid_word_score); | 409 EXPECT_GT(domain_name_mid_word_score, arg_mid_word_score); |
| 455 EXPECT_GT(hostname_mid_word_score, arg_mid_word_score); | 410 EXPECT_GT(hostname_mid_word_score, arg_mid_word_score); |
| 456 // Also verify that the matches at non-word-boundaries all score | 411 // Also verify that the matches at non-word-boundaries all score |
| 457 // worse than the matches at word boundaries. These three sets suffice. | 412 // worse than the matches at word boundaries. These three sets suffice. |
| 458 EXPECT_GT(arg_score, hostname_mid_word_score); | 413 EXPECT_GT(arg_score, hostname_mid_word_score); |
| 459 EXPECT_GT(arg_score, domain_name_mid_word_score); | 414 EXPECT_GT(arg_score, domain_name_mid_word_score); |
| 460 EXPECT_GT(title_score, title_mid_word_score); | 415 EXPECT_GT(title_score, title_mid_word_score); |
| 461 // Check that title matches fit somewhere reasonable compared to the | 416 // Check that title matches fit somewhere reasonable compared to the |
| 462 // various types of URL matches. | 417 // various types of URL matches. |
| 463 EXPECT_GT(title_score, arg_score); | 418 EXPECT_GT(title_score, arg_score); |
| 464 EXPECT_GT(arg_score, title_mid_word_score); | 419 EXPECT_GT(arg_score, title_mid_word_score); |
| 465 // Finally, verify that protocol matches and top level domain name | 420 // Finally, verify that protocol matches and top level domain name |
| 466 // matches (.com, .net, etc.) score worse than some of the mid-word | 421 // matches (.com, .net, etc.) score worse than some of the mid-word |
| 467 // matches that actually count. | 422 // matches that actually count. |
| 468 EXPECT_GT(hostname_mid_word_score, protocol_score); | 423 EXPECT_GT(hostname_mid_word_score, protocol_score); |
| 469 EXPECT_GT(hostname_mid_word_score, protocol_mid_word_score); | 424 EXPECT_GT(hostname_mid_word_score, protocol_mid_word_score); |
| 470 EXPECT_GT(hostname_mid_word_score, tld_score); | 425 EXPECT_GT(hostname_mid_word_score, tld_score); |
| 471 EXPECT_GT(hostname_mid_word_score, tld_mid_word_score); | 426 EXPECT_GT(hostname_mid_word_score, tld_mid_word_score); |
| 472 } | 427 } |
| 473 | 428 |
| 474 // Test the function GetFinalRelevancyScore(). | 429 // Test the function GetFinalRelevancyScore(). |
| 475 TEST_F(ScoredHistoryMatchBuilderImplTest, GetFinalRelevancyScore) { | 430 TEST_F(ScoredHistoryMatchTest, GetFinalRelevancyScore) { |
| 476 // hqp_relevance_buckets = "0.0:100,1.0:200,4.0:500,8.0:900,10.0:1000"; | 431 // hqp_relevance_buckets = "0.0:100,1.0:200,4.0:500,8.0:900,10.0:1000"; |
| 477 std::vector<ScoredHistoryMatchBuilderImpl::ScoreMaxRelevance> hqp_buckets; | 432 std::vector<ScoredHistoryMatch::ScoreMaxRelevance> hqp_buckets; |
| 478 hqp_buckets.push_back(std::make_pair(0.0, 100)); | 433 hqp_buckets.push_back(std::make_pair(0.0, 100)); |
| 479 hqp_buckets.push_back(std::make_pair(1.0, 200)); | 434 hqp_buckets.push_back(std::make_pair(1.0, 200)); |
| 480 hqp_buckets.push_back(std::make_pair(4.0, 500)); | 435 hqp_buckets.push_back(std::make_pair(4.0, 500)); |
| 481 hqp_buckets.push_back(std::make_pair(8.0, 900)); | 436 hqp_buckets.push_back(std::make_pair(8.0, 900)); |
| 482 hqp_buckets.push_back(std::make_pair(10.0, 1000)); | 437 hqp_buckets.push_back(std::make_pair(10.0, 1000)); |
| 483 // Check when topicality score is zero. | 438 // Check when topicality score is zero. |
| 484 float topicality_score = 0.0; | 439 float topicality_score = 0.0; |
| 485 float frequency_score = 10.0; | 440 float frequency_score = 10.0; |
| 486 // intermediate_score = 0.0 * 10.0 = 0.0. | 441 // intermediate_score = 0.0 * 10.0 = 0.0. |
| 487 EXPECT_EQ(0, | 442 EXPECT_EQ(0, ScoredHistoryMatch::GetFinalRelevancyScore( |
| 488 ScoredHistoryMatchBuilderImpl::GetFinalRelevancyScore( | 443 topicality_score, frequency_score, hqp_buckets)); |
| 489 topicality_score, frequency_score, hqp_buckets)); | |
| 490 | 444 |
| 491 // Check when intermediate score falls at the border range. | 445 // Check when intermediate score falls at the border range. |
| 492 topicality_score = 0.4f; | 446 topicality_score = 0.4f; |
| 493 frequency_score = 10.0f; | 447 frequency_score = 10.0f; |
| 494 // intermediate_score = 0.5 * 10.0 = 4.0. | 448 // intermediate_score = 0.5 * 10.0 = 4.0. |
| 495 EXPECT_EQ(500, | 449 EXPECT_EQ(500, ScoredHistoryMatch::GetFinalRelevancyScore( |
| 496 ScoredHistoryMatchBuilderImpl::GetFinalRelevancyScore( | 450 topicality_score, frequency_score, hqp_buckets)); |
| 497 topicality_score, frequency_score, hqp_buckets)); | |
| 498 | 451 |
| 499 // Checking the score that falls into one of the buckets. | 452 // Checking the score that falls into one of the buckets. |
| 500 topicality_score = 0.5f; | 453 topicality_score = 0.5f; |
| 501 frequency_score = 10.0f; | 454 frequency_score = 10.0f; |
| 502 // intermediate_score = 0.5 * 10.0 = 5.0. | 455 // intermediate_score = 0.5 * 10.0 = 5.0. |
| 503 EXPECT_EQ(600, // 500 + (((900 - 500)/(8 -4)) * 1) = 600. | 456 EXPECT_EQ(600, // 500 + (((900 - 500)/(8 -4)) * 1) = 600. |
| 504 ScoredHistoryMatchBuilderImpl::GetFinalRelevancyScore( | 457 ScoredHistoryMatch::GetFinalRelevancyScore( |
| 505 topicality_score, frequency_score, hqp_buckets)); | 458 topicality_score, frequency_score, hqp_buckets)); |
| 506 | 459 |
| 507 // Never give the score greater than maximum specified. | 460 // Never give the score greater than maximum specified. |
| 508 topicality_score = 0.5f; | 461 topicality_score = 0.5f; |
| 509 frequency_score = 22.0f; | 462 frequency_score = 22.0f; |
| 510 // intermediate_score = 0.5 * 22.0 = 11.0 | 463 // intermediate_score = 0.5 * 22.0 = 11.0 |
| 511 EXPECT_EQ(1000, | 464 EXPECT_EQ(1000, ScoredHistoryMatch::GetFinalRelevancyScore( |
| 512 ScoredHistoryMatchBuilderImpl::GetFinalRelevancyScore( | 465 topicality_score, frequency_score, hqp_buckets)); |
| 513 topicality_score, frequency_score, hqp_buckets)); | |
| 514 } | 466 } |
| 515 | 467 |
| 516 // Test the function GetHQPBucketsFromString(). | 468 // Test the function GetHQPBucketsFromString(). |
| 517 TEST_F(ScoredHistoryMatchBuilderImplTest, GetHQPBucketsFromString) { | 469 TEST_F(ScoredHistoryMatchTest, GetHQPBucketsFromString) { |
| 518 std::string buckets_str = "0.0:400,1.5:600,12.0:1300,20.0:1399"; | 470 std::string buckets_str = "0.0:400,1.5:600,12.0:1300,20.0:1399"; |
| 519 std::vector<ScoredHistoryMatchBuilderImpl::ScoreMaxRelevance> hqp_buckets; | 471 std::vector<ScoredHistoryMatch::ScoreMaxRelevance> hqp_buckets; |
| 520 | 472 |
| 521 EXPECT_TRUE(ScoredHistoryMatchBuilderImpl::GetHQPBucketsFromString( | 473 EXPECT_TRUE( |
| 522 buckets_str, &hqp_buckets)); | 474 ScoredHistoryMatch::GetHQPBucketsFromString(buckets_str, &hqp_buckets)); |
| 523 EXPECT_THAT(hqp_buckets, ElementsAre(Pair(0.0, 400), | 475 EXPECT_THAT(hqp_buckets, ElementsAre(Pair(0.0, 400), Pair(1.5, 600), |
| 524 Pair(1.5, 600), | 476 Pair(12.0, 1300), Pair(20.0, 1399))); |
| 525 Pair(12.0, 1300), | |
| 526 Pair(20.0, 1399))); | |
| 527 // invalid string. | 477 // invalid string. |
| 528 buckets_str = "0.0,400,1.5,600"; | 478 buckets_str = "0.0,400,1.5,600"; |
| 529 EXPECT_FALSE(ScoredHistoryMatchBuilderImpl::GetHQPBucketsFromString( | 479 EXPECT_FALSE( |
| 530 buckets_str, &hqp_buckets)); | 480 ScoredHistoryMatch::GetHQPBucketsFromString(buckets_str, &hqp_buckets)); |
| 531 } | 481 } |
| OLD | NEW |