| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <set> | 5 #include <set> |
| 6 | 6 |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 | 27 |
| 28 const char* kURL2 = "http://www.google.com/qwer"; | 28 const char* kURL2 = "http://www.google.com/qwer"; |
| 29 const char* kTitle2 = "Google B"; | 29 const char* kTitle2 = "Google B"; |
| 30 const char* kBody2 = "FOO two."; | 30 const char* kBody2 = "FOO two."; |
| 31 | 31 |
| 32 const char* kURL3 = "http://www.google.com/zxcv"; | 32 const char* kURL3 = "http://www.google.com/zxcv"; |
| 33 const char* kTitle3 = "Google C"; | 33 const char* kTitle3 = "Google C"; |
| 34 const char* kBody3 = "FOO drei"; | 34 const char* kBody3 = "FOO drei"; |
| 35 | 35 |
| 36 const char* kURL4 = "http://www.google.com/hjkl"; | 36 const char* kURL4 = "http://www.google.com/hjkl"; |
| 37 const char* kTitle4 = "Google D"; | 37 const char* kTitle4 = "Google de"; |
| 38 const char* kBody4 = "FOO lalala four."; | 38 const char* kBody4 = "FOO lalala four."; |
| 39 | 39 |
| 40 const char* kURL5 = "http://www.google.com/uiop"; | 40 const char* kURL5 = "http://www.google.de/s?q=mar%10&num=66"; |
| 41 const char* kTitle5 = "Google cinq"; | 41 const char* kTitle5 = "Google cinq"; |
| 42 const char* kBody5 = "FOO page one."; | 42 const char* kBody5 = "FOO page one. Including: punctuation."; |
| 43 | 43 |
| 44 // This provides a simple implementation of a URL+VisitDatabase using an | 44 // This provides a simple implementation of a URL+VisitDatabase using an |
| 45 // in-memory sqlite connection. The text database manager expects to be able to | 45 // in-memory sqlite connection. The text database manager expects to be able to |
| 46 // update the visit database to keep in sync. | 46 // update the visit database to keep in sync. |
| 47 class InMemDB : public URLDatabase, public VisitDatabase { | 47 class InMemDB : public URLDatabase, public VisitDatabase { |
| 48 public: | 48 public: |
| 49 InMemDB() { | 49 InMemDB() { |
| 50 EXPECT_TRUE(db_.OpenInMemory()); | 50 EXPECT_TRUE(db_.OpenInMemory()); |
| 51 CreateURLTable(false); | 51 CreateURLTable(false); |
| 52 InitVisitTable(); | 52 InitVisitTable(); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 78 visit_row.url_id = 1; | 78 visit_row.url_id = 1; |
| 79 visit_row.visit_time = Time::FromUTCExploded(exploded); | 79 visit_row.visit_time = Time::FromUTCExploded(exploded); |
| 80 visit_row.referring_visit = 0; | 80 visit_row.referring_visit = 0; |
| 81 visit_row.transition = content::PageTransitionFromInt(0); | 81 visit_row.transition = content::PageTransitionFromInt(0); |
| 82 visit_row.segment_id = 0; | 82 visit_row.segment_id = 0; |
| 83 visit_row.is_indexed = false; | 83 visit_row.is_indexed = false; |
| 84 visit_db->AddVisit(&visit_row, SOURCE_BROWSED); | 84 visit_db->AddVisit(&visit_row, SOURCE_BROWSED); |
| 85 | 85 |
| 86 times->push_back(visit_row.visit_time); | 86 times->push_back(visit_row.visit_time); |
| 87 manager.AddPageData(GURL(kURL1), visit_row.url_id, visit_row.visit_id, | 87 manager.AddPageData(GURL(kURL1), visit_row.url_id, visit_row.visit_id, |
| 88 visit_row.visit_time, UTF8ToUTF16(kTitle1), | 88 visit_row.visit_time, ASCIIToUTF16(kTitle1), |
| 89 UTF8ToUTF16(kBody1)); | 89 ASCIIToUTF16(kBody1)); |
| 90 | 90 |
| 91 exploded.day_of_month++; | 91 exploded.day_of_month++; |
| 92 visit_row.url_id = 2; | 92 visit_row.url_id = 2; |
| 93 visit_row.visit_time = Time::FromUTCExploded(exploded); | 93 visit_row.visit_time = Time::FromUTCExploded(exploded); |
| 94 visit_db->AddVisit(&visit_row, SOURCE_BROWSED); | 94 visit_db->AddVisit(&visit_row, SOURCE_BROWSED); |
| 95 times->push_back(visit_row.visit_time); | 95 times->push_back(visit_row.visit_time); |
| 96 manager.AddPageData(GURL(kURL2), visit_row.url_id, visit_row.visit_id, | 96 manager.AddPageData(GURL(kURL2), visit_row.url_id, visit_row.visit_id, |
| 97 visit_row.visit_time, UTF8ToUTF16(kTitle2), | 97 visit_row.visit_time, ASCIIToUTF16(kTitle2), |
| 98 UTF8ToUTF16(kBody2)); | 98 ASCIIToUTF16(kBody2)); |
| 99 | 99 |
| 100 exploded.day_of_month++; | 100 exploded.day_of_month++; |
| 101 visit_row.url_id = 2; | 101 visit_row.url_id = 2; |
| 102 visit_row.visit_time = Time::FromUTCExploded(exploded); | 102 visit_row.visit_time = Time::FromUTCExploded(exploded); |
| 103 visit_db->AddVisit(&visit_row, SOURCE_BROWSED); | 103 visit_db->AddVisit(&visit_row, SOURCE_BROWSED); |
| 104 times->push_back(visit_row.visit_time); | 104 times->push_back(visit_row.visit_time); |
| 105 manager.AddPageData(GURL(kURL3), visit_row.url_id, visit_row.visit_id, | 105 manager.AddPageData(GURL(kURL3), visit_row.url_id, visit_row.visit_id, |
| 106 visit_row.visit_time, UTF8ToUTF16(kTitle3), | 106 visit_row.visit_time, ASCIIToUTF16(kTitle3), |
| 107 UTF8ToUTF16(kBody3)); | 107 ASCIIToUTF16(kBody3)); |
| 108 | 108 |
| 109 // Put the next ones in the next month. | 109 // Put the next ones in the next month. |
| 110 exploded.month++; | 110 exploded.month++; |
| 111 visit_row.url_id = 2; | 111 visit_row.url_id = 2; |
| 112 visit_row.visit_time = Time::FromUTCExploded(exploded); | 112 visit_row.visit_time = Time::FromUTCExploded(exploded); |
| 113 visit_db->AddVisit(&visit_row, SOURCE_BROWSED); | 113 visit_db->AddVisit(&visit_row, SOURCE_BROWSED); |
| 114 times->push_back(visit_row.visit_time); | 114 times->push_back(visit_row.visit_time); |
| 115 manager.AddPageData(GURL(kURL4), visit_row.url_id, visit_row.visit_id, | 115 manager.AddPageData(GURL(kURL4), visit_row.url_id, visit_row.visit_id, |
| 116 visit_row.visit_time, UTF8ToUTF16(kTitle4), | 116 visit_row.visit_time, ASCIIToUTF16(kTitle4), |
| 117 UTF8ToUTF16(kBody4)); | 117 ASCIIToUTF16(kBody4)); |
| 118 | 118 |
| 119 exploded.day_of_month++; | 119 exploded.day_of_month++; |
| 120 visit_row.url_id = 2; | 120 visit_row.url_id = 2; |
| 121 visit_row.visit_time = Time::FromUTCExploded(exploded); | 121 visit_row.visit_time = Time::FromUTCExploded(exploded); |
| 122 visit_db->AddVisit(&visit_row, SOURCE_BROWSED); | 122 visit_db->AddVisit(&visit_row, SOURCE_BROWSED); |
| 123 times->push_back(visit_row.visit_time); | 123 times->push_back(visit_row.visit_time); |
| 124 manager.AddPageData(GURL(kURL5), visit_row.url_id, visit_row.visit_id, | 124 manager.AddPageData(GURL(kURL5), visit_row.url_id, visit_row.visit_id, |
| 125 visit_row.visit_time, UTF8ToUTF16(kTitle5), | 125 visit_row.visit_time, ASCIIToUTF16(kTitle5), |
| 126 UTF8ToUTF16(kBody5)); | 126 ASCIIToUTF16(kBody5)); |
| 127 | 127 |
| 128 // Put the first one in again in the second month. | 128 // Put the first one in again in the second month. |
| 129 exploded.day_of_month++; | 129 exploded.day_of_month++; |
| 130 visit_row.url_id = 2; | 130 visit_row.url_id = 2; |
| 131 visit_row.visit_time = Time::FromUTCExploded(exploded); | 131 visit_row.visit_time = Time::FromUTCExploded(exploded); |
| 132 visit_db->AddVisit(&visit_row, SOURCE_BROWSED); | 132 visit_db->AddVisit(&visit_row, SOURCE_BROWSED); |
| 133 times->push_back(visit_row.visit_time); | 133 times->push_back(visit_row.visit_time); |
| 134 manager.AddPageData(GURL(kURL1), visit_row.url_id, visit_row.visit_id, | 134 manager.AddPageData(GURL(kURL1), visit_row.url_id, visit_row.visit_id, |
| 135 visit_row.visit_time, UTF8ToUTF16(kTitle1), | 135 visit_row.visit_time, ASCIIToUTF16(kTitle1), |
| 136 UTF8ToUTF16(kBody1)); | 136 ASCIIToUTF16(kBody1)); |
| 137 } | 137 } |
| 138 | 138 |
| 139 bool ResultsHaveURL(const std::vector<TextDatabase::Match>& results, | 139 bool ResultsHaveURL(const std::vector<TextDatabase::Match>& results, |
| 140 const char* url) { | 140 const char* url) { |
| 141 GURL gurl(url); | 141 GURL gurl(url); |
| 142 for (size_t i = 0; i < results.size(); i++) { | 142 for (size_t i = 0; i < results.size(); i++) { |
| 143 if (results[i].url == gurl) | 143 if (results[i].url == gurl) |
| 144 return true; | 144 return true; |
| 145 } | 145 } |
| 146 return false; | 146 return false; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 178 ASSERT_TRUE(manager.Init(NULL)); | 178 ASSERT_TRUE(manager.Init(NULL)); |
| 179 | 179 |
| 180 std::vector<Time> times; | 180 std::vector<Time> times; |
| 181 AddAllPages(manager, &visit_db, ×); | 181 AddAllPages(manager, &visit_db, ×); |
| 182 | 182 |
| 183 QueryOptions options; | 183 QueryOptions options; |
| 184 options.begin_time = times[0] - TimeDelta::FromDays(100); | 184 options.begin_time = times[0] - TimeDelta::FromDays(100); |
| 185 options.end_time = times[times.size() - 1] + TimeDelta::FromDays(100); | 185 options.end_time = times[times.size() - 1] + TimeDelta::FromDays(100); |
| 186 std::vector<TextDatabase::Match> results; | 186 std::vector<TextDatabase::Match> results; |
| 187 Time first_time_searched; | 187 Time first_time_searched; |
| 188 manager.GetTextMatches(UTF8ToUTF16("FOO"), options, | 188 manager.GetTextMatches(ASCIIToUTF16("FOO"), options, |
| 189 &results, &first_time_searched); | 189 &results, &first_time_searched); |
| 190 | 190 |
| 191 // We should have matched every page. | 191 // We should have matched every page. |
| 192 EXPECT_EQ(6U, results.size()); | 192 EXPECT_EQ(6U, results.size()); |
| 193 EXPECT_TRUE(ResultsHaveURL(results, kURL1)); | 193 EXPECT_TRUE(ResultsHaveURL(results, kURL1)); |
| 194 EXPECT_TRUE(ResultsHaveURL(results, kURL2)); | 194 EXPECT_TRUE(ResultsHaveURL(results, kURL2)); |
| 195 EXPECT_TRUE(ResultsHaveURL(results, kURL3)); | 195 EXPECT_TRUE(ResultsHaveURL(results, kURL3)); |
| 196 EXPECT_TRUE(ResultsHaveURL(results, kURL4)); | 196 EXPECT_TRUE(ResultsHaveURL(results, kURL4)); |
| 197 EXPECT_TRUE(ResultsHaveURL(results, kURL5)); | 197 EXPECT_TRUE(ResultsHaveURL(results, kURL5)); |
| 198 | 198 |
| 199 // The first time searched should have been the first page's time or before | 199 // The first time searched should have been the first page's time or before |
| 200 // (it could have eliminated some time for us). | 200 // (it could have eliminated some time for us). |
| 201 EXPECT_TRUE(first_time_searched <= times[0]); | 201 EXPECT_TRUE(first_time_searched <= times[0]); |
| 202 } | 202 } |
| 203 | 203 |
| 204 // Tests that adding page components piecemeal will get them added properly. | 204 // Tests that adding page components piecemeal will get them added properly. |
| 205 // This does not supply a visit to update, this mode is used only by the unit | 205 // This does not supply a visit to update, this mode is used only by the unit |
| 206 // tests right now, but we test it anyway. | 206 // tests right now, but we test it anyway. |
| 207 TEST_F(TextDatabaseManagerTest, InsertCompleteNoVisit) { | 207 TEST_F(TextDatabaseManagerTest, InsertCompleteNoVisit) { |
| 208 ASSERT_TRUE(Init()); | 208 ASSERT_TRUE(Init()); |
| 209 InMemDB visit_db; | 209 InMemDB visit_db; |
| 210 TextDatabaseManager manager(dir_, &visit_db, &visit_db); | 210 TextDatabaseManager manager(dir_, &visit_db, &visit_db); |
| 211 ASSERT_TRUE(manager.Init(NULL)); | 211 ASSERT_TRUE(manager.Init(NULL)); |
| 212 | 212 |
| 213 // First add one without a visit. | 213 // First add one without a visit. |
| 214 const GURL url(kURL1); | 214 const GURL url(kURL1); |
| 215 manager.AddPageURL(url, 0, 0, Time::Now()); | 215 manager.AddPageURL(url, 0, 0, Time::Now()); |
| 216 manager.AddPageTitle(url, UTF8ToUTF16(kTitle1)); | 216 manager.AddPageTitle(url, ASCIIToUTF16(kTitle1)); |
| 217 manager.AddPageContents(url, UTF8ToUTF16(kBody1)); | 217 manager.AddPageContents(url, ASCIIToUTF16(kBody1)); |
| 218 | 218 |
| 219 // Check that the page got added. | 219 // Check that the page got added. |
| 220 QueryOptions options; | 220 QueryOptions options; |
| 221 std::vector<TextDatabase::Match> results; | 221 std::vector<TextDatabase::Match> results; |
| 222 Time first_time_searched; | 222 Time first_time_searched; |
| 223 | 223 |
| 224 manager.GetTextMatches(UTF8ToUTF16("FOO"), options, | 224 manager.GetTextMatches(ASCIIToUTF16("FOO"), options, |
| 225 &results, &first_time_searched); | 225 &results, &first_time_searched); |
| 226 ASSERT_EQ(1U, results.size()); | 226 ASSERT_EQ(1U, results.size()); |
| 227 EXPECT_EQ(kTitle1, UTF16ToUTF8(results[0].title)); | 227 EXPECT_EQ(kTitle1, UTF16ToUTF8(results[0].title)); |
| 228 } | 228 } |
| 229 | 229 |
| 230 // Like InsertCompleteNoVisit but specifies a visit to update. We check that the | 230 // Like InsertCompleteNoVisit but specifies a visit to update. We check that the |
| 231 // visit was updated properly. | 231 // visit was updated properly. |
| 232 TEST_F(TextDatabaseManagerTest, InsertCompleteVisit) { | 232 TEST_F(TextDatabaseManagerTest, InsertCompleteVisit) { |
| 233 ASSERT_TRUE(Init()); | 233 ASSERT_TRUE(Init()); |
| 234 InMemDB visit_db; | 234 InMemDB visit_db; |
| 235 TextDatabaseManager manager(dir_, &visit_db, &visit_db); | 235 TextDatabaseManager manager(dir_, &visit_db, &visit_db); |
| 236 ASSERT_TRUE(manager.Init(NULL)); | 236 ASSERT_TRUE(manager.Init(NULL)); |
| 237 | 237 |
| 238 // First add a visit to a page. We can just make up a URL ID since there is | 238 // First add a visit to a page. We can just make up a URL ID since there is |
| 239 // not actually any URL database around. | 239 // not actually any URL database around. |
| 240 VisitRow visit; | 240 VisitRow visit; |
| 241 visit.url_id = 1; | 241 visit.url_id = 1; |
| 242 visit.visit_time = Time::Now(); | 242 visit.visit_time = Time::Now(); |
| 243 visit.referring_visit = 0; | 243 visit.referring_visit = 0; |
| 244 visit.transition = content::PAGE_TRANSITION_LINK; | 244 visit.transition = content::PAGE_TRANSITION_LINK; |
| 245 visit.segment_id = 0; | 245 visit.segment_id = 0; |
| 246 visit.is_indexed = false; | 246 visit.is_indexed = false; |
| 247 visit_db.AddVisit(&visit, SOURCE_BROWSED); | 247 visit_db.AddVisit(&visit, SOURCE_BROWSED); |
| 248 | 248 |
| 249 // Add a full text indexed entry for that visit. | 249 // Add a full text indexed entry for that visit. |
| 250 const GURL url(kURL2); | 250 const GURL url(kURL2); |
| 251 manager.AddPageURL(url, visit.url_id, visit.visit_id, visit.visit_time); | 251 manager.AddPageURL(url, visit.url_id, visit.visit_id, visit.visit_time); |
| 252 manager.AddPageContents(url, UTF8ToUTF16(kBody2)); | 252 manager.AddPageContents(url, ASCIIToUTF16(kBody2)); |
| 253 manager.AddPageTitle(url, UTF8ToUTF16(kTitle2)); | 253 manager.AddPageTitle(url, ASCIIToUTF16(kTitle2)); |
| 254 | 254 |
| 255 // Check that the page got added. | 255 // Check that the page got added. |
| 256 QueryOptions options; | 256 QueryOptions options; |
| 257 std::vector<TextDatabase::Match> results; | 257 std::vector<TextDatabase::Match> results; |
| 258 Time first_time_searched; | 258 Time first_time_searched; |
| 259 | 259 |
| 260 manager.GetTextMatches(UTF8ToUTF16("FOO"), options, | 260 manager.GetTextMatches(ASCIIToUTF16("FOO"), options, |
| 261 &results, &first_time_searched); | 261 &results, &first_time_searched); |
| 262 ASSERT_EQ(1U, results.size()); | 262 ASSERT_EQ(1U, results.size()); |
| 263 EXPECT_EQ(kTitle2, UTF16ToUTF8(results[0].title)); | 263 EXPECT_EQ(kTitle2, UTF16ToUTF8(results[0].title)); |
| 264 | 264 |
| 265 // Check that the visit got updated for its new indexed state. | 265 // Check that the visit got updated for its new indexed state. |
| 266 VisitRow out_visit; | 266 VisitRow out_visit; |
| 267 ASSERT_TRUE(visit_db.GetRowForVisit(visit.visit_id, &out_visit)); | 267 ASSERT_TRUE(visit_db.GetRowForVisit(visit.visit_id, &out_visit)); |
| 268 EXPECT_TRUE(out_visit.is_indexed); | 268 EXPECT_TRUE(out_visit.is_indexed); |
| 269 } | 269 } |
| 270 | 270 |
| 271 // Tests that partial inserts that expire are added to the database. | 271 // Tests that partial inserts that expire are added to the database. |
| 272 TEST_F(TextDatabaseManagerTest, InsertPartial) { | 272 TEST_F(TextDatabaseManagerTest, InsertPartial) { |
| 273 ASSERT_TRUE(Init()); | 273 ASSERT_TRUE(Init()); |
| 274 InMemDB visit_db; | 274 InMemDB visit_db; |
| 275 TextDatabaseManager manager(dir_, &visit_db, &visit_db); | 275 TextDatabaseManager manager(dir_, &visit_db, &visit_db); |
| 276 ASSERT_TRUE(manager.Init(NULL)); | 276 ASSERT_TRUE(manager.Init(NULL)); |
| 277 | 277 |
| 278 // Add the first one with just a URL. | 278 // Add the first one with just a URL. |
| 279 GURL url1(kURL1); | 279 GURL url1(kURL1); |
| 280 manager.AddPageURL(url1, 0, 0, Time::Now()); | 280 manager.AddPageURL(url1, 0, 0, Time::Now()); |
| 281 | 281 |
| 282 // Now add a second one with a URL and title. | 282 // Now add a second one with a URL and title. |
| 283 GURL url2(kURL2); | 283 GURL url2(kURL2); |
| 284 manager.AddPageURL(url2, 0, 0, Time::Now()); | 284 manager.AddPageURL(url2, 0, 0, Time::Now()); |
| 285 manager.AddPageTitle(url2, UTF8ToUTF16(kTitle2)); | 285 manager.AddPageTitle(url2, ASCIIToUTF16(kTitle2)); |
| 286 | 286 |
| 287 // The third one has a URL and body. | 287 // The third one has a URL and body. |
| 288 GURL url3(kURL3); | 288 GURL url3(kURL3); |
| 289 manager.AddPageURL(url3, 0, 0, Time::Now()); | 289 manager.AddPageURL(url3, 0, 0, Time::Now()); |
| 290 manager.AddPageContents(url3, UTF8ToUTF16(kBody3)); | 290 manager.AddPageContents(url3, ASCIIToUTF16(kBody3)); |
| 291 | 291 |
| 292 // Expire stuff very fast. This assumes that the time between the first | 292 // Expire stuff very fast. This assumes that the time between the first |
| 293 // AddPageURL and this line is less than the expiration time (20 seconds). | 293 // AddPageURL and this line is less than the expiration time (20 seconds). |
| 294 TimeTicks added_time = TimeTicks::Now(); | 294 TimeTicks added_time = TimeTicks::Now(); |
| 295 TimeTicks expire_time = added_time + TimeDelta::FromSeconds(5); | 295 TimeTicks expire_time = added_time + TimeDelta::FromSeconds(5); |
| 296 manager.FlushOldChangesForTime(expire_time); | 296 manager.FlushOldChangesForTime(expire_time); |
| 297 | 297 |
| 298 // Do a query, nothing should be added yet. | 298 // Do a query, nothing should be added yet. |
| 299 QueryOptions options; | 299 QueryOptions options; |
| 300 std::vector<TextDatabase::Match> results; | 300 std::vector<TextDatabase::Match> results; |
| 301 Time first_time_searched; | 301 Time first_time_searched; |
| 302 manager.GetTextMatches(UTF8ToUTF16("google"), options, | 302 manager.GetTextMatches(ASCIIToUTF16("google"), options, |
| 303 &results, &first_time_searched); | 303 &results, &first_time_searched); |
| 304 ASSERT_EQ(0U, results.size()); | 304 ASSERT_EQ(0U, results.size()); |
| 305 | 305 |
| 306 // Compute a time threshold that will cause everything to be flushed, and | 306 // Compute a time threshold that will cause everything to be flushed, and |
| 307 // poke at the manager's internals to cause this to happen. | 307 // poke at the manager's internals to cause this to happen. |
| 308 expire_time = added_time + TimeDelta::FromDays(1); | 308 expire_time = added_time + TimeDelta::FromDays(1); |
| 309 manager.FlushOldChangesForTime(expire_time); | 309 manager.FlushOldChangesForTime(expire_time); |
| 310 | 310 |
| 311 // Now we should have all 3 URLs added. | 311 // Now we should have all 3 URLs added. |
| 312 manager.GetTextMatches(UTF8ToUTF16("google"), options, | 312 manager.GetTextMatches(ASCIIToUTF16("google"), options, |
| 313 &results, &first_time_searched); | 313 &results, &first_time_searched); |
| 314 ASSERT_EQ(3U, results.size()); | 314 ASSERT_EQ(3U, results.size()); |
| 315 EXPECT_TRUE(ResultsHaveURL(results, kURL1)); | 315 EXPECT_TRUE(ResultsHaveURL(results, kURL1)); |
| 316 EXPECT_TRUE(ResultsHaveURL(results, kURL2)); | 316 EXPECT_TRUE(ResultsHaveURL(results, kURL2)); |
| 317 EXPECT_TRUE(ResultsHaveURL(results, kURL3)); | 317 EXPECT_TRUE(ResultsHaveURL(results, kURL3)); |
| 318 } | 318 } |
| 319 | 319 |
| 320 // Tests that partial inserts (due to timeouts) will still get updated if the | 320 // Tests that partial inserts (due to timeouts) will still get updated if the |
| 321 // data comes in later. | 321 // data comes in later. |
| 322 TEST_F(TextDatabaseManagerTest, PartialComplete) { | 322 TEST_F(TextDatabaseManagerTest, PartialComplete) { |
| 323 ASSERT_TRUE(Init()); | 323 ASSERT_TRUE(Init()); |
| 324 InMemDB visit_db; | 324 InMemDB visit_db; |
| 325 TextDatabaseManager manager(dir_, &visit_db, &visit_db); | 325 TextDatabaseManager manager(dir_, &visit_db, &visit_db); |
| 326 ASSERT_TRUE(manager.Init(NULL)); | 326 ASSERT_TRUE(manager.Init(NULL)); |
| 327 | 327 |
| 328 Time added_time = Time::Now(); | 328 Time added_time = Time::Now(); |
| 329 GURL url(kURL1); | 329 GURL url(kURL1); |
| 330 | 330 |
| 331 // We have to have the URL in the URL and visit databases for this test to | 331 // We have to have the URL in the URL and visit databases for this test to |
| 332 // work. | 332 // work. |
| 333 URLRow url_row(url); | 333 URLRow url_row(url); |
| 334 url_row.set_title(UTF8ToUTF16("chocolate")); | 334 url_row.set_title(ASCIIToUTF16("chocolate")); |
| 335 URLID url_id = visit_db.AddURL(url_row); | 335 URLID url_id = visit_db.AddURL(url_row); |
| 336 ASSERT_TRUE(url_id); | 336 ASSERT_TRUE(url_id); |
| 337 VisitRow visit_row; | 337 VisitRow visit_row; |
| 338 visit_row.url_id = url_id; | 338 visit_row.url_id = url_id; |
| 339 visit_row.visit_time = added_time; | 339 visit_row.visit_time = added_time; |
| 340 visit_db.AddVisit(&visit_row, SOURCE_BROWSED); | 340 visit_db.AddVisit(&visit_row, SOURCE_BROWSED); |
| 341 | 341 |
| 342 // Add a URL with no title or body, and say that it expired. | 342 // Add a URL with no title or body, and say that it expired. |
| 343 manager.AddPageURL(url, 0, 0, added_time); | 343 manager.AddPageURL(url, 0, 0, added_time); |
| 344 TimeTicks expire_time = TimeTicks::Now() + TimeDelta::FromDays(1); | 344 TimeTicks expire_time = TimeTicks::Now() + TimeDelta::FromDays(1); |
| 345 manager.FlushOldChangesForTime(expire_time); | 345 manager.FlushOldChangesForTime(expire_time); |
| 346 | 346 |
| 347 // Add the title. We should be able to query based on that. The title in the | 347 // Add the title. We should be able to query based on that. The title in the |
| 348 // URL row we set above should not come into the picture. | 348 // URL row we set above should not come into the picture. |
| 349 manager.AddPageTitle(url, UTF8ToUTF16("Some unique title")); | 349 manager.AddPageTitle(url, ASCIIToUTF16("Some unique title")); |
| 350 Time first_time_searched; | 350 Time first_time_searched; |
| 351 QueryOptions options; | 351 QueryOptions options; |
| 352 std::vector<TextDatabase::Match> results; | 352 std::vector<TextDatabase::Match> results; |
| 353 manager.GetTextMatches(UTF8ToUTF16("unique"), options, | 353 manager.GetTextMatches(ASCIIToUTF16("unique"), options, |
| 354 &results, &first_time_searched); | 354 &results, &first_time_searched); |
| 355 EXPECT_EQ(1U, results.size()); | 355 EXPECT_EQ(1U, results.size()); |
| 356 manager.GetTextMatches(UTF8ToUTF16("chocolate"), options, | 356 manager.GetTextMatches(ASCIIToUTF16("chocolate"), options, |
| 357 &results, &first_time_searched); | 357 &results, &first_time_searched); |
| 358 EXPECT_EQ(0U, results.size()); | 358 EXPECT_EQ(0U, results.size()); |
| 359 | 359 |
| 360 // Now add the body, which should be queryable. | 360 // Now add the body, which should be queryable. |
| 361 manager.AddPageContents(url, UTF8ToUTF16("Very awesome body")); | 361 manager.AddPageContents(url, ASCIIToUTF16("Very awesome body")); |
| 362 manager.GetTextMatches(UTF8ToUTF16("awesome"), options, &results, &first_time_
searched); | 362 manager.GetTextMatches( |
| 363 ASCIIToUTF16("awesome"), options, &results, &first_time_searched); |
| 363 EXPECT_EQ(1U, results.size()); | 364 EXPECT_EQ(1U, results.size()); |
| 364 | 365 |
| 365 // Adding the body will actually copy the title from the URL table rather | 366 // Adding the body will actually copy the title from the URL table rather |
| 366 // than the previously indexed row (we made them not match above). This isn't | 367 // than the previously indexed row (we made them not match above). This isn't |
| 367 // necessarily what we want, but it's how it's implemented, and we don't want | 368 // necessarily what we want, but it's how it's implemented, and we don't want |
| 368 // to regress it. | 369 // to regress it. |
| 369 manager.GetTextMatches(UTF8ToUTF16("chocolate"), options, &results, &first_tim
e_searched); | 370 manager.GetTextMatches( |
| 371 ASCIIToUTF16("chocolate"), options, &results, &first_time_searched); |
| 370 EXPECT_EQ(1U, results.size()); | 372 EXPECT_EQ(1U, results.size()); |
| 371 } | 373 } |
| 372 | 374 |
| 373 // Tests that changes get properly committed to disk. | 375 // Tests that changes get properly committed to disk. |
| 374 TEST_F(TextDatabaseManagerTest, Writing) { | 376 TEST_F(TextDatabaseManagerTest, Writing) { |
| 375 ASSERT_TRUE(Init()); | 377 ASSERT_TRUE(Init()); |
| 376 | 378 |
| 377 QueryOptions options; | 379 QueryOptions options; |
| 378 std::vector<TextDatabase::Match> results; | 380 std::vector<TextDatabase::Match> results; |
| 379 Time first_time_searched; | 381 Time first_time_searched; |
| 380 | 382 |
| 381 InMemDB visit_db; | 383 InMemDB visit_db; |
| 382 | 384 |
| 383 // Create the manager and write some stuff to it. | 385 // Create the manager and write some stuff to it. |
| 384 { | 386 { |
| 385 TextDatabaseManager manager(dir_, &visit_db, &visit_db); | 387 TextDatabaseManager manager(dir_, &visit_db, &visit_db); |
| 386 ASSERT_TRUE(manager.Init(NULL)); | 388 ASSERT_TRUE(manager.Init(NULL)); |
| 387 | 389 |
| 388 std::vector<Time> times; | 390 std::vector<Time> times; |
| 389 AddAllPages(manager, &visit_db, ×); | 391 AddAllPages(manager, &visit_db, ×); |
| 390 | 392 |
| 391 // We should have matched every page. | 393 // We should have matched every page. |
| 392 manager.GetTextMatches(UTF8ToUTF16("FOO"), options, &results, &first_time_se
arched); | 394 manager.GetTextMatches( |
| 395 ASCIIToUTF16("FOO"), options, &results, &first_time_searched); |
| 393 EXPECT_EQ(6U, results.size()); | 396 EXPECT_EQ(6U, results.size()); |
| 394 } | 397 } |
| 395 results.clear(); | 398 results.clear(); |
| 396 | 399 |
| 397 // Recreate the manager and make sure it finds the written stuff. | 400 // Recreate the manager and make sure it finds the written stuff. |
| 398 { | 401 { |
| 399 TextDatabaseManager manager(dir_, &visit_db, &visit_db); | 402 TextDatabaseManager manager(dir_, &visit_db, &visit_db); |
| 400 ASSERT_TRUE(manager.Init(NULL)); | 403 ASSERT_TRUE(manager.Init(NULL)); |
| 401 | 404 |
| 402 // We should have matched every page again. | 405 // We should have matched every page again. |
| 403 manager.GetTextMatches(UTF8ToUTF16("FOO"), options, | 406 manager.GetTextMatches(ASCIIToUTF16("FOO"), options, |
| 404 &results, &first_time_searched); | 407 &results, &first_time_searched); |
| 405 EXPECT_EQ(6U, results.size()); | 408 EXPECT_EQ(6U, results.size()); |
| 406 } | 409 } |
| 407 } | 410 } |
| 408 | 411 |
| 409 // Tests that changes get properly committed to disk, as in the Writing test | 412 // Tests that changes get properly committed to disk, as in the Writing test |
| 410 // above, but when there is a transaction around the adds. | 413 // above, but when there is a transaction around the adds. |
| 411 TEST_F(TextDatabaseManagerTest, WritingTransaction) { | 414 TEST_F(TextDatabaseManagerTest, WritingTransaction) { |
| 412 ASSERT_TRUE(Init()); | 415 ASSERT_TRUE(Init()); |
| 413 | 416 |
| 414 QueryOptions options; | 417 QueryOptions options; |
| 415 std::vector<TextDatabase::Match> results; | 418 std::vector<TextDatabase::Match> results; |
| 416 Time first_time_searched; | 419 Time first_time_searched; |
| 417 | 420 |
| 418 InMemDB visit_db; | 421 InMemDB visit_db; |
| 419 | 422 |
| 420 // Create the manager and write some stuff to it. | 423 // Create the manager and write some stuff to it. |
| 421 { | 424 { |
| 422 TextDatabaseManager manager(dir_, &visit_db, &visit_db); | 425 TextDatabaseManager manager(dir_, &visit_db, &visit_db); |
| 423 ASSERT_TRUE(manager.Init(NULL)); | 426 ASSERT_TRUE(manager.Init(NULL)); |
| 424 | 427 |
| 425 std::vector<Time> times; | 428 std::vector<Time> times; |
| 426 manager.BeginTransaction(); | 429 manager.BeginTransaction(); |
| 427 AddAllPages(manager, &visit_db, ×); | 430 AddAllPages(manager, &visit_db, ×); |
| 428 // "Forget" to commit, it should be autocommittedd for us. | 431 // "Forget" to commit, it should be autocommittedd for us. |
| 429 | 432 |
| 430 // We should have matched every page. | 433 // We should have matched every page. |
| 431 manager.GetTextMatches(UTF8ToUTF16("FOO"), options, | 434 manager.GetTextMatches(ASCIIToUTF16("FOO"), options, |
| 432 &results, &first_time_searched); | 435 &results, &first_time_searched); |
| 433 EXPECT_EQ(6U, results.size()); | 436 EXPECT_EQ(6U, results.size()); |
| 434 } | 437 } |
| 435 results.clear(); | 438 results.clear(); |
| 436 | 439 |
| 437 // Recreate the manager and make sure it finds the written stuff. | 440 // Recreate the manager and make sure it finds the written stuff. |
| 438 { | 441 { |
| 439 TextDatabaseManager manager(dir_, &visit_db, &visit_db); | 442 TextDatabaseManager manager(dir_, &visit_db, &visit_db); |
| 440 ASSERT_TRUE(manager.Init(NULL)); | 443 ASSERT_TRUE(manager.Init(NULL)); |
| 441 | 444 |
| 442 // We should have matched every page again. | 445 // We should have matched every page again. |
| 443 manager.GetTextMatches(UTF8ToUTF16("FOO"), options, | 446 manager.GetTextMatches(ASCIIToUTF16("FOO"), options, |
| 444 &results, &first_time_searched); | 447 &results, &first_time_searched); |
| 445 EXPECT_EQ(6U, results.size()); | 448 EXPECT_EQ(6U, results.size()); |
| 446 } | 449 } |
| 447 } | 450 } |
| 448 | 451 |
| 449 // Tests querying where the maximum number of items is met. | 452 // Tests querying where the maximum number of items is met. |
| 450 TEST_F(TextDatabaseManagerTest, QueryMax) { | 453 TEST_F(TextDatabaseManagerTest, QueryMax) { |
| 451 ASSERT_TRUE(Init()); | 454 ASSERT_TRUE(Init()); |
| 452 InMemDB visit_db; | 455 InMemDB visit_db; |
| 453 TextDatabaseManager manager(dir_, &visit_db, &visit_db); | 456 TextDatabaseManager manager(dir_, &visit_db, &visit_db); |
| 454 ASSERT_TRUE(manager.Init(NULL)); | 457 ASSERT_TRUE(manager.Init(NULL)); |
| 455 | 458 |
| 456 std::vector<Time> times; | 459 std::vector<Time> times; |
| 457 AddAllPages(manager, &visit_db, ×); | 460 AddAllPages(manager, &visit_db, ×); |
| 458 | 461 |
| 459 string16 foo = UTF8ToUTF16("FOO"); | 462 string16 foo = ASCIIToUTF16("FOO"); |
| 460 | 463 |
| 461 QueryOptions options; | 464 QueryOptions options; |
| 462 options.begin_time = times[0] - TimeDelta::FromDays(100); | 465 options.begin_time = times[0] - TimeDelta::FromDays(100); |
| 463 options.end_time = times[times.size() - 1] + TimeDelta::FromDays(100); | 466 options.end_time = times[times.size() - 1] + TimeDelta::FromDays(100); |
| 464 options.max_count = 2; | 467 options.max_count = 2; |
| 465 std::vector<TextDatabase::Match> results; | 468 std::vector<TextDatabase::Match> results; |
| 466 Time first_time_searched; | 469 Time first_time_searched; |
| 467 manager.GetTextMatches(foo, options, &results, &first_time_searched); | 470 manager.GetTextMatches(foo, options, &results, &first_time_searched); |
| 468 | 471 |
| 469 // We should have gotten the last two pages as results (the first page is | 472 // We should have gotten the last two pages as results (the first page is |
| (...skipping 18 matching lines...) Expand all Loading... |
| 488 // Tests querying backwards in time in chunks. | 491 // Tests querying backwards in time in chunks. |
| 489 TEST_F(TextDatabaseManagerTest, QueryBackwards) { | 492 TEST_F(TextDatabaseManagerTest, QueryBackwards) { |
| 490 ASSERT_TRUE(Init()); | 493 ASSERT_TRUE(Init()); |
| 491 InMemDB visit_db; | 494 InMemDB visit_db; |
| 492 TextDatabaseManager manager(dir_, &visit_db, &visit_db); | 495 TextDatabaseManager manager(dir_, &visit_db, &visit_db); |
| 493 ASSERT_TRUE(manager.Init(NULL)); | 496 ASSERT_TRUE(manager.Init(NULL)); |
| 494 | 497 |
| 495 std::vector<Time> times; | 498 std::vector<Time> times; |
| 496 AddAllPages(manager, &visit_db, ×); | 499 AddAllPages(manager, &visit_db, ×); |
| 497 | 500 |
| 498 string16 foo = UTF8ToUTF16("FOO"); | 501 string16 foo = ASCIIToUTF16("FOO"); |
| 499 | 502 |
| 500 // First do a query for all time, but with a max of 2. This will give us the | 503 // First do a query for all time, but with a max of 2. This will give us the |
| 501 // last two results and will tell us where to start searching when we want | 504 // last two results and will tell us where to start searching when we want |
| 502 // to go back in time. | 505 // to go back in time. |
| 503 QueryOptions options; | 506 QueryOptions options; |
| 504 options.begin_time = times[0] - TimeDelta::FromDays(100); | 507 options.begin_time = times[0] - TimeDelta::FromDays(100); |
| 505 options.end_time = times[times.size() - 1] + TimeDelta::FromDays(100); | 508 options.end_time = times[times.size() - 1] + TimeDelta::FromDays(100); |
| 506 options.max_count = 2; | 509 options.max_count = 2; |
| 507 std::vector<TextDatabase::Match> results; | 510 std::vector<TextDatabase::Match> results; |
| 508 Time first_time_searched; | 511 Time first_time_searched; |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 588 times.push_back(Time::FromInternalValue(7)); | 591 times.push_back(Time::FromInternalValue(7)); |
| 589 times.push_back(Time::FromInternalValue(5)); | 592 times.push_back(Time::FromInternalValue(5)); |
| 590 times.push_back(Time::FromInternalValue(5)); | 593 times.push_back(Time::FromInternalValue(5)); |
| 591 times.push_back(Time::FromInternalValue(3)); | 594 times.push_back(Time::FromInternalValue(3)); |
| 592 times.push_back(Time::FromInternalValue(1)); | 595 times.push_back(Time::FromInternalValue(1)); |
| 593 manager.DeleteFromUncommittedForTimes(times); | 596 manager.DeleteFromUncommittedForTimes(times); |
| 594 | 597 |
| 595 EXPECT_EQ(3u, manager.GetUncommittedEntryCountForTest()); | 598 EXPECT_EQ(3u, manager.GetUncommittedEntryCountForTest()); |
| 596 } | 599 } |
| 597 | 600 |
| 601 TEST_F(TextDatabaseManagerTest, Query) { |
| 602 ASSERT_TRUE(Init()); |
| 603 |
| 604 QueryOptions options; |
| 605 std::vector<TextDatabase::Match> results; |
| 606 Time first_time_searched; |
| 607 |
| 608 InMemDB visit_db; |
| 609 |
| 610 TextDatabaseManager manager(dir_, &visit_db, &visit_db); |
| 611 ASSERT_TRUE(manager.Init(NULL)); |
| 612 |
| 613 std::vector<Time> times; |
| 614 AddAllPages(manager, &visit_db, ×); |
| 615 |
| 616 // Try a multi-word query. |
| 617 manager.GetTextMatches(ASCIIToUTF16("FOO drei"), options, |
| 618 &results, &first_time_searched); |
| 619 EXPECT_EQ(1U, results.size()); |
| 620 results.clear(); |
| 621 |
| 622 // Try a quoted query that should match only a single URL. |
| 623 manager.GetTextMatches(ASCIIToUTF16("\"google.de\""), |
| 624 options, &results, &first_time_searched); |
| 625 EXPECT_EQ(1U, results.size()); |
| 626 results.clear(); |
| 627 |
| 628 // Same, but with some special characters. |
| 629 manager.GetTextMatches(ASCIIToUTF16("\"google.de/s?q=mar%10&num=66\""), |
| 630 options, &results, &first_time_searched); |
| 631 EXPECT_EQ(1U, results.size()); |
| 632 results.clear(); |
| 633 |
| 634 // Using separate tokens should match in URL or in body. |
| 635 manager.GetTextMatches(ASCIIToUTF16("google de"), options, |
| 636 &results, &first_time_searched); |
| 637 EXPECT_EQ(2U, results.size()); |
| 638 EXPECT_TRUE(ResultsHaveURL(results, kURL4)); |
| 639 EXPECT_TRUE(ResultsHaveURL(results, kURL5)); |
| 640 results.clear(); |
| 641 |
| 642 // Try a quoted query that should match the body. |
| 643 manager.GetTextMatches(ASCIIToUTF16("\"Including: punctuation.\""), options, |
| 644 &results, &first_time_searched); |
| 645 EXPECT_EQ(1U, results.size()); |
| 646 results.clear(); |
| 647 |
| 648 // Ensure that punctuation is not ignored in quoted strings. |
| 649 manager.GetTextMatches(ASCIIToUTF16("\"Including punctuation\""), options, |
| 650 &results, &first_time_searched); |
| 651 EXPECT_EQ(0U, results.size()); |
| 652 |
| 653 // Ensure that regex characters don't have special meaning inside quotes. |
| 654 manager.GetTextMatches(ASCIIToUTF16("\"Including* punctuation\""), options, |
| 655 &results, &first_time_searched); |
| 656 EXPECT_EQ(0U, results.size()); |
| 657 manager.GetTextMatches(ASCIIToUTF16("\"Including. punctuation\""), options, |
| 658 &results, &first_time_searched); |
| 659 EXPECT_EQ(0U, results.size()); |
| 660 manager.GetTextMatches(ASCIIToUTF16("\"Including_ punctuation\""), options, |
| 661 &results, &first_time_searched); |
| 662 EXPECT_EQ(0U, results.size()); |
| 663 } |
| 664 |
| 598 } // namespace history | 665 } // namespace history |
| OLD | NEW |