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