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 // History unit tests come in two flavors: | 5 // History unit tests come in two flavors: |
6 // | 6 // |
7 // 1. The more complicated style is that the unit test creates a full history | 7 // 1. The more complicated style is that the unit test creates a full history |
8 // service. This spawns a background thread for the history backend, and | 8 // service. This spawns a background thread for the history backend, and |
9 // all communication is asynchronous. This is useful for testing more | 9 // all communication is asynchronous. This is useful for testing more |
10 // complicated things or end-to-end behavior. | 10 // complicated things or end-to-end behavior. |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 virtual void TearDown() { | 130 virtual void TearDown() { |
131 DeleteBackend(); | 131 DeleteBackend(); |
132 | 132 |
133 // Make sure we don't have any event pending that could disrupt the next | 133 // Make sure we don't have any event pending that could disrupt the next |
134 // test. | 134 // test. |
135 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); | 135 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
136 MessageLoop::current()->Run(); | 136 MessageLoop::current()->Run(); |
137 } | 137 } |
138 | 138 |
139 int64 AddDownload(DownloadItem::DownloadState state, const Time& time) { | 139 int64 AddDownload(DownloadItem::DownloadState state, const Time& time) { |
| 140 std::vector<GURL> url_chain; |
| 141 url_chain.push_back(GURL("foo-url")); |
| 142 |
140 DownloadPersistentStoreInfo download( | 143 DownloadPersistentStoreInfo download( |
141 FilePath(FILE_PATH_LITERAL("foo-path")), | 144 FilePath(FILE_PATH_LITERAL("foo-path")), |
142 GURL("foo-url"), | 145 FilePath(FILE_PATH_LITERAL("foo-path")), |
| 146 url_chain, |
143 GURL(""), | 147 GURL(""), |
144 time, | 148 time, |
145 time, | 149 time, |
146 0, | 150 0, |
147 512, | 151 512, |
148 state, | 152 state, |
| 153 content::DOWNLOAD_INTERRUPT_REASON_NONE, |
149 0, | 154 0, |
150 0); | 155 0); |
151 return db_->CreateDownload(download); | 156 return db_->CreateDownload(download); |
152 } | 157 } |
153 | 158 |
154 ScopedTempDir temp_dir_; | 159 ScopedTempDir temp_dir_; |
155 | 160 |
156 MessageLoopForUI message_loop_; | 161 MessageLoopForUI message_loop_; |
157 | 162 |
158 // names of the database files | 163 // names of the database files |
(...skipping 19 matching lines...) Expand all Loading... |
178 content::Details<HistoryDetails> det(details); | 183 content::Details<HistoryDetails> det(details); |
179 history_test_->in_mem_backend_->Observe(type, | 184 history_test_->in_mem_backend_->Observe(type, |
180 content::Source<HistoryBackendDBTest>(NULL), det); | 185 content::Source<HistoryBackendDBTest>(NULL), det); |
181 | 186 |
182 // The backend passes ownership of the details pointer to us. | 187 // The backend passes ownership of the details pointer to us. |
183 delete details; | 188 delete details; |
184 } | 189 } |
185 | 190 |
186 namespace { | 191 namespace { |
187 | 192 |
| 193 // Schema for the downloads database for verion 23 and earlier. |
| 194 const char* kVersion23DownloadsSchema = |
| 195 "CREATE TABLE downloads (" |
| 196 "id INTEGER PRIMARY KEY," |
| 197 "full_path LONGVARCHAR NOT NULL," |
| 198 "url LONGVARCHAR NOT NULL," |
| 199 "start_time INTEGER NOT NULL," |
| 200 "received_bytes INTEGER NOT NULL," |
| 201 "total_bytes INTEGER NOT NULL," |
| 202 "state INTEGER NOT NULL," |
| 203 "end_time INTEGER NOT NULL," |
| 204 "opened INTEGER NOT NULL)"; |
| 205 |
188 TEST_F(HistoryBackendDBTest, ClearBrowsingData_Downloads) { | 206 TEST_F(HistoryBackendDBTest, ClearBrowsingData_Downloads) { |
189 CreateBackendAndDatabase(); | 207 CreateBackendAndDatabase(); |
190 | 208 |
191 Time now = Time::Now(); | 209 Time now = Time::Now(); |
192 TimeDelta one_day = TimeDelta::FromDays(1); | 210 TimeDelta one_day = TimeDelta::FromDays(1); |
193 Time month_ago = now - TimeDelta::FromDays(30); | 211 Time month_ago = now - TimeDelta::FromDays(30); |
194 | 212 |
195 // Initially there should be nothing in the downloads database. | 213 // Initially there should be nothing in the downloads database. |
196 std::vector<DownloadPersistentStoreInfo> downloads; | 214 std::vector<DownloadPersistentStoreInfo> downloads; |
197 db_->QueryDownloads(&downloads); | 215 db_->QueryDownloads(&downloads); |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
279 { | 297 { |
280 // Re-open the db for manual manipulation. | 298 // Re-open the db for manual manipulation. |
281 sql::Connection db; | 299 sql::Connection db; |
282 ASSERT_TRUE(db.Open(history_dir_.Append(chrome::kHistoryFilename))); | 300 ASSERT_TRUE(db.Open(history_dir_.Append(chrome::kHistoryFilename))); |
283 { | 301 { |
284 // Manually force the version to 22. | 302 // Manually force the version to 22. |
285 sql::Statement version22(db.GetUniqueStatement( | 303 sql::Statement version22(db.GetUniqueStatement( |
286 "UPDATE meta SET value=22 WHERE key='version'")); | 304 "UPDATE meta SET value=22 WHERE key='version'")); |
287 ASSERT_TRUE(version22.Run()); | 305 ASSERT_TRUE(version22.Run()); |
288 } | 306 } |
| 307 // Nuke the new tables and create an old one. |
| 308 ASSERT_TRUE(db.Execute("DROP TABLE downloads")); |
| 309 ASSERT_TRUE(db.Execute("DROP TABLE downloads_url_chains")); |
| 310 ASSERT_TRUE(db.Execute(kVersion23DownloadsSchema)); |
| 311 |
289 // Manually insert corrupted rows; there's infrastructure in place now to | 312 // Manually insert corrupted rows; there's infrastructure in place now to |
290 // make this impossible, at least according to the test above. | 313 // make this impossible, at least according to the test above. |
291 for (int state = 0; state < 5; ++state) { | 314 for (int state = 0; state < 5; ++state) { |
292 sql::Statement s(db.GetUniqueStatement( | 315 sql::Statement s(db.GetUniqueStatement( |
293 "INSERT INTO downloads (id, full_path, url, start_time, " | 316 "INSERT INTO downloads (id, full_path, url, start_time, " |
294 "received_bytes, total_bytes, state, end_time, opened) VALUES " | 317 "received_bytes, total_bytes, state, end_time, opened) VALUES " |
295 "(?, ?, ?, ?, ?, ?, ?, ?, ?)")); | 318 "(?, ?, ?, ?, ?, ?, ?, ?, ?)")); |
296 s.BindInt64(0, 1 + state); | 319 s.BindInt64(0, 1 + state); |
297 s.BindString(1, "path"); | 320 s.BindString(1, "path"); |
298 s.BindString(2, "url"); | 321 s.BindString(2, "url"); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
336 // 4. | 359 // 4. |
337 EXPECT_EQ(((counter == 3) ? 4 : counter), statement.ColumnInt(1)); | 360 EXPECT_EQ(((counter == 3) ? 4 : counter), statement.ColumnInt(1)); |
338 EXPECT_EQ(counter % 2, statement.ColumnInt(2)); | 361 EXPECT_EQ(counter % 2, statement.ColumnInt(2)); |
339 ++counter; | 362 ++counter; |
340 } | 363 } |
341 EXPECT_EQ(5, counter); | 364 EXPECT_EQ(5, counter); |
342 } | 365 } |
343 } | 366 } |
344 } | 367 } |
345 | 368 |
| 369 TEST_F(HistoryBackendDBTest, MigrateDownloadsReasonAndPaths) { |
| 370 Time now(base::Time::Now()); |
| 371 |
| 372 // Create the db and close it so that we can reopen it directly. |
| 373 CreateBackendAndDatabase(); |
| 374 DeleteBackend(); |
| 375 { |
| 376 // Re-open the db for manual manipulation. |
| 377 sql::Connection db; |
| 378 ASSERT_TRUE(db.Open(history_dir_.Append(chrome::kHistoryFilename))); |
| 379 { |
| 380 // Manually force the version to 23. |
| 381 sql::Statement version23(db.GetUniqueStatement( |
| 382 "UPDATE meta SET value=23 WHERE key='version'")); |
| 383 ASSERT_TRUE(version23.Run()); |
| 384 } |
| 385 |
| 386 // Nuke the new tables and create an old one with some hand crafted |
| 387 // values in it. |
| 388 ASSERT_TRUE(db.Execute("DROP TABLE downloads")); |
| 389 ASSERT_TRUE(db.Execute("DROP TABLE downloads_url_chains")); |
| 390 ASSERT_TRUE(db.Execute(kVersion23DownloadsSchema)); |
| 391 |
| 392 // Manually insert some rows. |
| 393 sql::Statement s(db.GetUniqueStatement( |
| 394 "INSERT INTO downloads (id, full_path, url, start_time, " |
| 395 "received_bytes, total_bytes, state, end_time, opened) VALUES " |
| 396 "(?, ?, ?, ?, ?, ?, ?, ?, ?)")); |
| 397 |
| 398 int64 db_handle = 0; |
| 399 // Null path. |
| 400 s.BindInt64(0, ++db_handle); |
| 401 s.BindString(1, ""); |
| 402 s.BindString(2, "http://whatever.com/index.html"); |
| 403 s.BindInt64(3, now.ToTimeT()); |
| 404 s.BindInt64(4, 100); |
| 405 s.BindInt64(5, 100); |
| 406 s.BindInt(6, 1); |
| 407 s.BindInt64(7, now.ToTimeT()); |
| 408 s.BindInt(8, 1); |
| 409 ASSERT_TRUE(s.Run()); |
| 410 s.Reset(true); |
| 411 |
| 412 // Non-null path. |
| 413 s.BindInt64(0, ++db_handle); |
| 414 s.BindString(1, "/path/to/some/file"); |
| 415 s.BindString(2, "http://whatever.com/index1.html"); |
| 416 s.BindInt64(3, now.ToTimeT()); |
| 417 s.BindInt64(4, 100); |
| 418 s.BindInt64(5, 100); |
| 419 s.BindInt(6, 1); |
| 420 s.BindInt64(7, now.ToTimeT()); |
| 421 s.BindInt(8, 1); |
| 422 ASSERT_TRUE(s.Run()); |
| 423 } |
| 424 |
| 425 // Re-open the db using the HistoryDatabase, which should migrate from version |
| 426 // 23 to 24, creating the new tables and creating the new path and reason |
| 427 // columns. |
| 428 CreateBackendAndDatabase(); |
| 429 DeleteBackend(); |
| 430 { |
| 431 // Re-open the db for manual manipulation. |
| 432 sql::Connection db; |
| 433 ASSERT_TRUE(db.Open(history_dir_.Append(chrome::kHistoryFilename))); |
| 434 { |
| 435 // The version should have been updated. |
| 436 int cur_version = HistoryDatabase::GetCurrentVersion(); |
| 437 ASSERT_LT(23, cur_version); |
| 438 sql::Statement s(db.GetUniqueStatement( |
| 439 "SELECT value FROM meta WHERE key = 'version'")); |
| 440 EXPECT_TRUE(s.Step()); |
| 441 EXPECT_EQ(cur_version, s.ColumnInt(0)); |
| 442 } |
| 443 { |
| 444 base::Time nowish(base::Time::FromTimeT(now.ToTimeT())); |
| 445 |
| 446 // Confirm downloads table is valid. |
| 447 sql::Statement statement(db.GetUniqueStatement( |
| 448 "SELECT id, interrupt_reason, target_path, current_path, " |
| 449 " start_time, end_time " |
| 450 "FROM downloads ORDER BY id")); |
| 451 EXPECT_TRUE(statement.Step()); |
| 452 EXPECT_EQ(1, statement.ColumnInt64(0)); |
| 453 EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_NONE, |
| 454 statement.ColumnInt(1)); |
| 455 EXPECT_EQ("", statement.ColumnString(2)); |
| 456 EXPECT_EQ("", statement.ColumnString(3)); |
| 457 EXPECT_EQ(nowish.ToInternalValue(), statement.ColumnInt64(4)); |
| 458 EXPECT_EQ(nowish.ToInternalValue(), statement.ColumnInt64(5)); |
| 459 |
| 460 EXPECT_TRUE(statement.Step()); |
| 461 EXPECT_EQ(2, statement.ColumnInt64(0)); |
| 462 EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_NONE, |
| 463 statement.ColumnInt(1)); |
| 464 EXPECT_EQ("/path/to/some/file", statement.ColumnString(2)); |
| 465 EXPECT_EQ("/path/to/some/file", statement.ColumnString(3)); |
| 466 EXPECT_EQ(nowish.ToInternalValue(), statement.ColumnInt64(4)); |
| 467 EXPECT_EQ(nowish.ToInternalValue(), statement.ColumnInt64(5)); |
| 468 |
| 469 EXPECT_FALSE(statement.Step()); |
| 470 } |
| 471 { |
| 472 // Confirm donwloads_url_chains table is valid. |
| 473 sql::Statement statement(db.GetUniqueStatement( |
| 474 "SELECT id, chain_index, url FROM downloads_url_chains " |
| 475 " ORDER BY id, chain_index")); |
| 476 EXPECT_TRUE(statement.Step()); |
| 477 EXPECT_EQ(1, statement.ColumnInt64(0)); |
| 478 EXPECT_EQ(0, statement.ColumnInt(1)); |
| 479 EXPECT_EQ("http://whatever.com/index.html", statement.ColumnString(2)); |
| 480 |
| 481 EXPECT_TRUE(statement.Step()); |
| 482 EXPECT_EQ(2, statement.ColumnInt64(0)); |
| 483 EXPECT_EQ(0, statement.ColumnInt(1)); |
| 484 EXPECT_EQ("http://whatever.com/index1.html", statement.ColumnString(2)); |
| 485 |
| 486 EXPECT_FALSE(statement.Step()); |
| 487 } |
| 488 } |
| 489 } |
| 490 |
| 491 TEST_F(HistoryBackendDBTest, ConfirmDownloadRowCreateAndDelete) { |
| 492 // Create the DB. |
| 493 CreateBackendAndDatabase(); |
| 494 |
| 495 base::Time now(base::Time::Now()); |
| 496 |
| 497 // Add some downloads. |
| 498 AddDownload(DownloadItem::COMPLETE, now); |
| 499 int64 did2 = AddDownload(DownloadItem::COMPLETE, now + |
| 500 base::TimeDelta::FromDays(2)); |
| 501 int64 did3 = AddDownload(DownloadItem::COMPLETE, now - |
| 502 base::TimeDelta::FromDays(2)); |
| 503 |
| 504 // Confirm that resulted in the correct number of rows in the DB. |
| 505 DeleteBackend(); |
| 506 { |
| 507 sql::Connection db; |
| 508 ASSERT_TRUE(db.Open(history_dir_.Append(chrome::kHistoryFilename))); |
| 509 sql::Statement statement(db.GetUniqueStatement( |
| 510 "Select Count(*) from downloads")); |
| 511 EXPECT_TRUE(statement.Step()); |
| 512 EXPECT_EQ(3, statement.ColumnInt(0)); |
| 513 |
| 514 sql::Statement statement1(db.GetUniqueStatement( |
| 515 "Select Count(*) from downloads_url_chains")); |
| 516 EXPECT_TRUE(statement1.Step()); |
| 517 EXPECT_EQ(3, statement1.ColumnInt(0)); |
| 518 } |
| 519 |
| 520 // Delete some rows and make sure the results are still correct. |
| 521 CreateBackendAndDatabase(); |
| 522 db_->RemoveDownload(did2); |
| 523 db_->RemoveDownload(did3); |
| 524 DeleteBackend(); |
| 525 { |
| 526 sql::Connection db; |
| 527 ASSERT_TRUE(db.Open(history_dir_.Append(chrome::kHistoryFilename))); |
| 528 sql::Statement statement(db.GetUniqueStatement( |
| 529 "Select Count(*) from downloads")); |
| 530 EXPECT_TRUE(statement.Step()); |
| 531 EXPECT_EQ(1, statement.ColumnInt(0)); |
| 532 |
| 533 sql::Statement statement1(db.GetUniqueStatement( |
| 534 "Select Count(*) from downloads_url_chains")); |
| 535 EXPECT_TRUE(statement1.Step()); |
| 536 EXPECT_EQ(1, statement1.ColumnInt(0)); |
| 537 } |
| 538 } |
| 539 |
346 // The tracker uses RenderProcessHost pointers for scoping but never | 540 // The tracker uses RenderProcessHost pointers for scoping but never |
347 // dereferences them. We use ints because it's easier. This function converts | 541 // dereferences them. We use ints because it's easier. This function converts |
348 // between the two. | 542 // between the two. |
349 static void* MakeFakeHost(int id) { | 543 static void* MakeFakeHost(int id) { |
350 void* host = 0; | 544 void* host = 0; |
351 memcpy(&host, &id, sizeof(id)); | 545 memcpy(&host, &id, sizeof(id)); |
352 return host; | 546 return host; |
353 } | 547 } |
354 | 548 |
355 class HistoryTest : public testing::Test { | 549 class HistoryTest : public testing::Test { |
(...skipping 676 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1032 request_consumer.CancelAllRequests(); | 1226 request_consumer.CancelAllRequests(); |
1033 CleanupHistoryService(); | 1227 CleanupHistoryService(); |
1034 // WARNING: history has now been deleted. | 1228 // WARNING: history has now been deleted. |
1035 history_service_.reset(); | 1229 history_service_.reset(); |
1036 ASSERT_FALSE(task->done_invoked); | 1230 ASSERT_FALSE(task->done_invoked); |
1037 } | 1231 } |
1038 | 1232 |
1039 } // namespace | 1233 } // namespace |
1040 | 1234 |
1041 } // namespace history | 1235 } // namespace history |
OLD | NEW |