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

Side by Side Diff: chrome/browser/history/in_memory_url_index_unittest.cc

Issue 10541045: Move ScoredHistoryMatch into Its Own Set of Files (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <algorithm> 5 #include <algorithm>
6 #include <fstream> 6 #include <fstream>
7 7
8 #include "base/file_path.h" 8 #include "base/file_path.h"
9 #include "base/file_util.h" 9 #include "base/file_util.h"
10 #include "base/message_loop.h" 10 #include "base/message_loop.h"
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
98 InMemoryURLIndexTest(); 98 InMemoryURLIndexTest();
99 99
100 protected: 100 protected:
101 // Test setup. 101 // Test setup.
102 virtual void SetUp(); 102 virtual void SetUp();
103 103
104 // Allows the database containing the test data to be customized by 104 // Allows the database containing the test data to be customized by
105 // subclasses. 105 // subclasses.
106 virtual FilePath::StringType TestDBName() const; 106 virtual FilePath::StringType TestDBName() const;
107 107
108 #if 0
108 // Convenience function to create a URLRow with basic data for |url|, |title|, 109 // Convenience function to create a URLRow with basic data for |url|, |title|,
109 // |visit_count|, and |typed_count|. |last_visit_ago| gives the number of 110 // |visit_count|, and |typed_count|. |last_visit_ago| gives the number of
110 // days from now to set the URL's last_visit. 111 // days from now to set the URL's last_visit.
111 URLRow MakeURLRow(const char* url, 112 URLRow MakeURLRow(const char* url,
112 const char* title, 113 const char* title,
113 int visit_count, 114 int visit_count,
114 int last_visit_ago, 115 int last_visit_ago,
115 int typed_count); 116 int typed_count);
116 117
117 // Convenience functions for easily creating vectors of search terms. 118 // Convenience functions for easily creating vectors of search terms.
118 String16Vector Make1Term(const char* term) const; 119 String16Vector Make1Term(const char* term) const;
119 String16Vector Make2Terms(const char* term_1, const char* term_2) const; 120 String16Vector Make2Terms(const char* term_1, const char* term_2) const;
120 121
121 // Convenience function for GetTopicalityScore() that builds the 122 // Convenience function for GetTopicalityScore() that builds the
122 // term match and word break information automatically that are needed 123 // term match and word break information automatically that are needed
123 // to call GetTopicalityScore(). It only works for scoring a single term, 124 // to call GetTopicalityScore(). It only works for scoring a single term,
124 // not multiple terms. 125 // not multiple terms.
125 float GetTopicalityScoreOfTermAgainstURLAndTitle(const string16& term, 126 float GetTopicalityScoreOfTermAgainstURLAndTitle(const string16& term,
126 const string16& url, 127 const string16& url,
127 const string16& title); 128 const string16& title);
129 #endif
128 130
129 // Validates that the given |term| is contained in |cache| and that it is 131 // Validates that the given |term| is contained in |cache| and that it is
130 // marked as in-use. 132 // marked as in-use.
131 void CheckTerm(const URLIndexPrivateData::SearchTermCacheMap& cache, 133 void CheckTerm(const URLIndexPrivateData::SearchTermCacheMap& cache,
132 string16 term) const; 134 string16 term) const;
133 135
134 // Pass-through function to simplify our friendship with HistoryService. 136 // Pass-through function to simplify our friendship with HistoryService.
135 sql::Connection& GetDB(); 137 sql::Connection& GetDB();
136 138
137 // Pass-through functions to simplify our friendship with InMemoryURLIndex. 139 // Pass-through functions to simplify our friendship with InMemoryURLIndex.
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after
291 url_index_.reset( 293 url_index_.reset(
292 new InMemoryURLIndex(&profile_, FilePath(), "en,ja,hi,zh")); 294 new InMemoryURLIndex(&profile_, FilePath(), "en,ja,hi,zh"));
293 url_index_->Init(); 295 url_index_->Init();
294 url_index_->RebuildFromHistory(history_database_); 296 url_index_->RebuildFromHistory(history_database_);
295 } 297 }
296 298
297 FilePath::StringType InMemoryURLIndexTest::TestDBName() const { 299 FilePath::StringType InMemoryURLIndexTest::TestDBName() const {
298 return FILE_PATH_LITERAL("url_history_provider_test.db.txt"); 300 return FILE_PATH_LITERAL("url_history_provider_test.db.txt");
299 } 301 }
300 302
303 #if 0
301 URLRow InMemoryURLIndexTest::MakeURLRow(const char* url, 304 URLRow InMemoryURLIndexTest::MakeURLRow(const char* url,
302 const char* title, 305 const char* title,
303 int visit_count, 306 int visit_count,
304 int last_visit_ago, 307 int last_visit_ago,
305 int typed_count) { 308 int typed_count) {
306 URLRow row(GURL(url), 0); 309 URLRow row(GURL(url), 0);
307 row.set_title(UTF8ToUTF16(title)); 310 row.set_title(UTF8ToUTF16(title));
308 row.set_visit_count(visit_count); 311 row.set_visit_count(visit_count);
309 row.set_typed_count(typed_count); 312 row.set_typed_count(typed_count);
310 row.set_last_visit(base::Time::NowFromSystemTime() - 313 row.set_last_visit(base::Time::NowFromSystemTime() -
(...skipping 17 matching lines...) Expand all
328 331
329 float InMemoryURLIndexTest::GetTopicalityScoreOfTermAgainstURLAndTitle( 332 float InMemoryURLIndexTest::GetTopicalityScoreOfTermAgainstURLAndTitle(
330 const string16& term, 333 const string16& term,
331 const string16& url, 334 const string16& url,
332 const string16& title) { 335 const string16& title) {
333 TermMatches url_matches = MatchTermInString(term, url, 0); 336 TermMatches url_matches = MatchTermInString(term, url, 0);
334 TermMatches title_matches = MatchTermInString(term, title, 0); 337 TermMatches title_matches = MatchTermInString(term, title, 0);
335 RowWordStarts word_starts; 338 RowWordStarts word_starts;
336 String16SetFromString16(url, &word_starts.url_word_starts_); 339 String16SetFromString16(url, &word_starts.url_word_starts_);
337 String16SetFromString16(title, &word_starts.title_word_starts_); 340 String16SetFromString16(title, &word_starts.title_word_starts_);
338 return GetPrivateData()->GetTopicalityScore( 341 return ScoredHistoryMatch::GetTopicalityScore(
339 1, url, url_matches, title_matches, word_starts); 342 1, url, url_matches, title_matches, word_starts);
340 } 343 }
344 #endif
341 345
342 void InMemoryURLIndexTest::CheckTerm( 346 void InMemoryURLIndexTest::CheckTerm(
343 const URLIndexPrivateData::SearchTermCacheMap& cache, 347 const URLIndexPrivateData::SearchTermCacheMap& cache,
344 string16 term) const { 348 string16 term) const {
345 URLIndexPrivateData::SearchTermCacheMap::const_iterator cache_iter( 349 URLIndexPrivateData::SearchTermCacheMap::const_iterator cache_iter(
346 cache.find(term)); 350 cache.find(term));
347 ASSERT_TRUE(cache.end() != cache_iter) 351 ASSERT_TRUE(cache.end() != cache_iter)
348 << "Cache does not contain '" << term << "' but should."; 352 << "Cache does not contain '" << term << "' but should.";
349 URLIndexPrivateData::SearchTermCacheItem cache_item = cache_iter->second; 353 URLIndexPrivateData::SearchTermCacheItem cache_item = cache_iter->second;
350 EXPECT_TRUE(cache_item.used_) 354 EXPECT_TRUE(cache_item.used_)
(...skipping 431 matching lines...) Expand 10 before | Expand all | Expand 10 after
782 CheckTerm(cache, ASCIIToUTF16("mort")); 786 CheckTerm(cache, ASCIIToUTF16("mort"));
783 CheckTerm(cache, ASCIIToUTF16("reco")); 787 CheckTerm(cache, ASCIIToUTF16("reco"));
784 788
785 // Simulate a <DELETE> by removing the 'reco' and adding back the 'rec'. 789 // Simulate a <DELETE> by removing the 'reco' and adding back the 'rec'.
786 url_index_->HistoryItemsForTerms(ASCIIToUTF16("mort rec")); 790 url_index_->HistoryItemsForTerms(ASCIIToUTF16("mort rec"));
787 ASSERT_EQ(2U, cache.size()); 791 ASSERT_EQ(2U, cache.size());
788 CheckTerm(cache, ASCIIToUTF16("mort")); 792 CheckTerm(cache, ASCIIToUTF16("mort"));
789 CheckTerm(cache, ASCIIToUTF16("rec")); 793 CheckTerm(cache, ASCIIToUTF16("rec"));
790 } 794 }
791 795
796 #if 0
792 TEST_F(InMemoryURLIndexTest, Scoring) { 797 TEST_F(InMemoryURLIndexTest, Scoring) {
793 URLRow row_a(MakeURLRow("http://abcdef", "fedcba", 3, 30, 1)); 798 URLRow row_a(MakeURLRow("http://abcdef", "fedcba", 3, 30, 1));
794 URLIndexPrivateData& private_data(*GetPrivateData());
795 // We use NowFromSystemTime() because MakeURLRow uses the same function 799 // We use NowFromSystemTime() because MakeURLRow uses the same function
796 // to calculate last visit time when building a row. 800 // to calculate last visit time when building a row.
797 base::Time now = base::Time::NowFromSystemTime(); 801 base::Time now = base::Time::NowFromSystemTime();
798 RowWordStarts word_starts; 802 RowWordStarts word_starts;
799 // Test scores based on position. 803 // Test scores based on position.
800 // TODO(mpearson): Test new_scoring if we're actually going to turn it 804 // TODO(mpearson): Test new_scoring if we're actually going to turn it
801 // on by default. This requires setting word_starts, which isn't done 805 // on by default. This requires setting word_starts, which isn't done
802 // right now. 806 // right now.
803 ScoredHistoryMatch scored_a(private_data.ScoredMatchForURL( 807 ScoredHistoryMatch scored_a(row_a, ASCIIToUTF16("abc"), Make1Term("abc"),
804 row_a, ASCIIToUTF16("abc"), Make1Term("abc"), word_starts, now)); 808 word_starts, now);
805 ScoredHistoryMatch scored_b(private_data.ScoredMatchForURL( 809 ScoredHistoryMatch scored_b(row_a, ASCIIToUTF16("bcd"), Make1Term("bcd"),
806 row_a, ASCIIToUTF16("bcd"), Make1Term("bcd"), word_starts, now)); 810 word_starts, now);
807 EXPECT_GT(scored_a.raw_score, scored_b.raw_score); 811 EXPECT_GT(scored_a.raw_score, scored_b.raw_score);
808 // Test scores based on length. 812 // Test scores based on length.
809 ScoredHistoryMatch scored_c(private_data.ScoredMatchForURL( 813 ScoredHistoryMatch scored_c(row_a, ASCIIToUTF16("abcd"), Make1Term("abcd"),
810 row_a, ASCIIToUTF16("abcd"), Make1Term("abcd"), word_starts, now)); 814 word_starts, now);
811 EXPECT_LT(scored_a.raw_score, scored_c.raw_score); 815 EXPECT_LT(scored_a.raw_score, scored_c.raw_score);
812 // Test scores based on order. 816 // Test scores based on order.
813 ScoredHistoryMatch scored_d(private_data.ScoredMatchForURL( 817 ScoredHistoryMatch scored_d(row_a, ASCIIToUTF16("abcdef"),
814 row_a, ASCIIToUTF16("abcdef"), Make2Terms("abc", "def"), word_starts, 818 Make2Terms("abc", "def"), word_starts, now);
815 now)); 819 ScoredHistoryMatch scored_e(row_a, ASCIIToUTF16("def abc"),
816 ScoredHistoryMatch scored_e(private_data.ScoredMatchForURL( 820 Make2Terms("def", "abc"), word_starts, now);
817 row_a, ASCIIToUTF16("def abc"), Make2Terms("def", "abc"), word_starts,
818 now));
819 EXPECT_GT(scored_d.raw_score, scored_e.raw_score); 821 EXPECT_GT(scored_d.raw_score, scored_e.raw_score);
820 // Test scores based on visit_count. 822 // Test scores based on visit_count.
821 URLRow row_b(MakeURLRow("http://abcdef", "fedcba", 10, 30, 1)); 823 URLRow row_b(MakeURLRow("http://abcdef", "fedcba", 10, 30, 1));
822 ScoredHistoryMatch scored_f(private_data.ScoredMatchForURL( 824 ScoredHistoryMatch scored_f(row_b, ASCIIToUTF16("abc"), Make1Term("abc"),
823 row_b, ASCIIToUTF16("abc"), Make1Term("abc"), word_starts, now)); 825 word_starts, now);
824 EXPECT_GT(scored_f.raw_score, scored_a.raw_score); 826 EXPECT_GT(scored_f.raw_score, scored_a.raw_score);
825 // Test scores based on last_visit. 827 // Test scores based on last_visit.
826 URLRow row_c(MakeURLRow("http://abcdef", "fedcba", 3, 10, 1)); 828 URLRow row_c(MakeURLRow("http://abcdef", "fedcba", 3, 10, 1));
827 ScoredHistoryMatch scored_g(private_data.ScoredMatchForURL( 829 ScoredHistoryMatch scored_g(row_c, ASCIIToUTF16("abc"), Make1Term("abc"),
828 row_c, ASCIIToUTF16("abc"), Make1Term("abc"), word_starts, now)); 830 word_starts, now);
829 EXPECT_GT(scored_g.raw_score, scored_a.raw_score); 831 EXPECT_GT(scored_g.raw_score, scored_a.raw_score);
830 // Test scores based on typed_count. 832 // Test scores based on typed_count.
831 URLRow row_d(MakeURLRow("http://abcdef", "fedcba", 3, 30, 10)); 833 URLRow row_d(MakeURLRow("http://abcdef", "fedcba", 3, 30, 10));
832 ScoredHistoryMatch scored_h(private_data.ScoredMatchForURL( 834 ScoredHistoryMatch scored_h(row_d, ASCIIToUTF16("abc"), Make1Term("abc"),
833 row_d, ASCIIToUTF16("abc"), Make1Term("abc"), word_starts, now)); 835 word_starts, now);
834 EXPECT_GT(scored_h.raw_score, scored_a.raw_score); 836 EXPECT_GT(scored_h.raw_score, scored_a.raw_score);
835 // Test scores based on a terms appearing multiple times. 837 // Test scores based on a terms appearing multiple times.
836 URLRow row_i(MakeURLRow("http://csi.csi.csi/csi_csi", 838 URLRow row_i(MakeURLRow("http://csi.csi.csi/csi_csi",
837 "CSI Guide to CSI Las Vegas, CSI New York, CSI Provo", 3, 30, 10)); 839 "CSI Guide to CSI Las Vegas, CSI New York, CSI Provo", 3, 30, 10));
838 ScoredHistoryMatch scored_i(private_data.ScoredMatchForURL( 840 ScoredHistoryMatch scored_i(row_i, ASCIIToUTF16("csi"), Make1Term("csi"),
839 row_i, ASCIIToUTF16("csi"), Make1Term("csi"), word_starts, now)); 841 word_starts, now);
840 EXPECT_LT(scored_i.raw_score, 1400); 842 EXPECT_LT(scored_i.raw_score, 1400);
841 } 843 }
842 844
843 TEST_F(InMemoryURLIndexTest, GetTopicalityScoreTrailingSlash) { 845 TEST_F(InMemoryURLIndexTest, GetTopicalityScoreTrailingSlash) {
844 const float hostname = GetTopicalityScoreOfTermAgainstURLAndTitle( 846 const float hostname = GetTopicalityScoreOfTermAgainstURLAndTitle(
845 ASCIIToUTF16("def"), 847 ASCIIToUTF16("def"),
846 ASCIIToUTF16("http://abc.def.com/"), 848 ASCIIToUTF16("http://abc.def.com/"),
847 ASCIIToUTF16("Non-Matching Title")); 849 ASCIIToUTF16("Non-Matching Title"));
848 const float hostname_no_slash = GetTopicalityScoreOfTermAgainstURLAndTitle( 850 const float hostname_no_slash = GetTopicalityScoreOfTermAgainstURLAndTitle(
849 ASCIIToUTF16("def"), 851 ASCIIToUTF16("def"),
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
904 EXPECT_GT(arg_score, hostname_mid_word_score); 906 EXPECT_GT(arg_score, hostname_mid_word_score);
905 EXPECT_GT(title_mid_word_score, arg_mid_word_score); 907 EXPECT_GT(title_mid_word_score, arg_mid_word_score);
906 // Finally, verify that protocol matches score worse than everything 908 // Finally, verify that protocol matches score worse than everything
907 // (except possibly mid-word matches in the ?arg section of the URL--I 909 // (except possibly mid-word matches in the ?arg section of the URL--I
908 // can imagine scoring those pretty harshly as well). 910 // can imagine scoring those pretty harshly as well).
909 EXPECT_GT(path_mid_word_score, protocol_score); 911 EXPECT_GT(path_mid_word_score, protocol_score);
910 EXPECT_GT(path_mid_word_score, protocol_mid_word_score); 912 EXPECT_GT(path_mid_word_score, protocol_mid_word_score);
911 EXPECT_GT(title_mid_word_score, protocol_score); 913 EXPECT_GT(title_mid_word_score, protocol_score);
912 EXPECT_GT(title_mid_word_score, protocol_mid_word_score); 914 EXPECT_GT(title_mid_word_score, protocol_mid_word_score);
913 } 915 }
916 #endif
914 917
915 TEST_F(InMemoryURLIndexTest, AddNewRows) { 918 TEST_F(InMemoryURLIndexTest, AddNewRows) {
916 // Verify that the row we're going to add does not already exist. 919 // Verify that the row we're going to add does not already exist.
917 URLID new_row_id = 87654321; 920 URLID new_row_id = 87654321;
918 // Newly created URLRows get a last_visit time of 'right now' so it should 921 // Newly created URLRows get a last_visit time of 'right now' so it should
919 // qualify as a quick result candidate. 922 // qualify as a quick result candidate.
920 EXPECT_TRUE(url_index_->HistoryItemsForTerms( 923 EXPECT_TRUE(url_index_->HistoryItemsForTerms(
921 ASCIIToUTF16("brokeandalone")).empty()); 924 ASCIIToUTF16("brokeandalone")).empty());
922 925
923 // Add a new row. 926 // Add a new row.
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after
1150 full_file_path.GetComponents(&actual_parts); 1153 full_file_path.GetComponents(&actual_parts);
1151 ASSERT_EQ(expected_parts.size(), actual_parts.size()); 1154 ASSERT_EQ(expected_parts.size(), actual_parts.size());
1152 size_t count = expected_parts.size(); 1155 size_t count = expected_parts.size();
1153 for (size_t i = 0; i < count; ++i) 1156 for (size_t i = 0; i < count; ++i)
1154 EXPECT_EQ(expected_parts[i], actual_parts[i]); 1157 EXPECT_EQ(expected_parts[i], actual_parts[i]);
1155 // Must clear the history_dir_ to satisfy the dtor's DCHECK. 1158 // Must clear the history_dir_ to satisfy the dtor's DCHECK.
1156 set_history_dir(FilePath()); 1159 set_history_dir(FilePath());
1157 } 1160 }
1158 1161
1159 } // namespace history 1162 } // namespace history
OLDNEW
« no previous file with comments | « chrome/browser/history/in_memory_url_index_types.cc ('k') | chrome/browser/history/scored_history_match.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698