| 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 <algorithm> | 5 #include <algorithm> |
| 6 #include <set> | 6 #include <set> |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
| 12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
| 13 #include "base/file_util.h" | 13 #include "base/file_util.h" |
| 14 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
| 15 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
| 16 #include "base/memory/scoped_ptr.h" | 16 #include "base/memory/scoped_ptr.h" |
| 17 #include "base/path_service.h" | 17 #include "base/path_service.h" |
| 18 #include "base/platform_file.h" |
| 18 #include "base/strings/string16.h" | 19 #include "base/strings/string16.h" |
| 19 #include "base/strings/string_number_conversions.h" | 20 #include "base/strings/string_number_conversions.h" |
| 20 #include "base/strings/utf_string_conversions.h" | 21 #include "base/strings/utf_string_conversions.h" |
| 21 #include "chrome/browser/bookmarks/bookmark_model.h" | 22 #include "chrome/browser/bookmarks/bookmark_model.h" |
| 22 #include "chrome/browser/bookmarks/bookmark_model_factory.h" | 23 #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
| 23 #include "chrome/browser/bookmarks/bookmark_utils.h" | 24 #include "chrome/browser/bookmarks/bookmark_utils.h" |
| 24 #include "chrome/browser/history/history_backend.h" | 25 #include "chrome/browser/history/history_backend.h" |
| 25 #include "chrome/browser/history/history_notifications.h" | 26 #include "chrome/browser/history/history_notifications.h" |
| 26 #include "chrome/browser/history/history_service.h" | 27 #include "chrome/browser/history/history_service.h" |
| 27 #include "chrome/browser/history/history_service_factory.h" | 28 #include "chrome/browser/history/history_service_factory.h" |
| (...skipping 469 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 497 rows.push_back(row1); | 498 rows.push_back(row1); |
| 498 backend_->AddPagesWithDetails(rows, history::SOURCE_BROWSED); | 499 backend_->AddPagesWithDetails(rows, history::SOURCE_BROWSED); |
| 499 | 500 |
| 500 URLID row1_id = backend_->db_->GetRowForURL(row1.url(), NULL); | 501 URLID row1_id = backend_->db_->GetRowForURL(row1.url(), NULL); |
| 501 URLID row2_id = backend_->db_->GetRowForURL(row2.url(), NULL); | 502 URLID row2_id = backend_->db_->GetRowForURL(row2.url(), NULL); |
| 502 | 503 |
| 503 // Get the two visits for the URLs we just added. | 504 // Get the two visits for the URLs we just added. |
| 504 VisitVector visits; | 505 VisitVector visits; |
| 505 backend_->db_->GetVisitsForURL(row1_id, &visits); | 506 backend_->db_->GetVisitsForURL(row1_id, &visits); |
| 506 ASSERT_EQ(1U, visits.size()); | 507 ASSERT_EQ(1U, visits.size()); |
| 507 VisitID visit1_id = visits[0].visit_id; | |
| 508 | 508 |
| 509 visits.clear(); | 509 visits.clear(); |
| 510 backend_->db_->GetVisitsForURL(row2_id, &visits); | 510 backend_->db_->GetVisitsForURL(row2_id, &visits); |
| 511 ASSERT_EQ(1U, visits.size()); | 511 ASSERT_EQ(1U, visits.size()); |
| 512 VisitID visit2_id = visits[0].visit_id; | |
| 513 | 512 |
| 514 // The in-memory backend should have been set and it should have gotten the | 513 // The in-memory backend should have been set and it should have gotten the |
| 515 // typed URL. | 514 // typed URL. |
| 516 ASSERT_TRUE(mem_backend_.get()); | 515 ASSERT_TRUE(mem_backend_.get()); |
| 517 URLRow outrow1; | 516 URLRow outrow1; |
| 518 EXPECT_TRUE(mem_backend_->db_->GetRowForURL(row1.url(), NULL)); | 517 EXPECT_TRUE(mem_backend_->db_->GetRowForURL(row1.url(), NULL)); |
| 519 | 518 |
| 520 // Add thumbnails for each page. The |Images| take ownership of SkBitmap | 519 // Add thumbnails for each page. The |Images| take ownership of SkBitmap |
| 521 // created from decoding the images. | 520 // created from decoding the images. |
| 522 ThumbnailScore score(0.25, true, true); | 521 ThumbnailScore score(0.25, true, true); |
| 523 scoped_ptr<SkBitmap> google_bitmap( | 522 scoped_ptr<SkBitmap> google_bitmap( |
| 524 gfx::JPEGCodec::Decode(kGoogleThumbnail, sizeof(kGoogleThumbnail))); | 523 gfx::JPEGCodec::Decode(kGoogleThumbnail, sizeof(kGoogleThumbnail))); |
| 525 | 524 |
| 526 gfx::Image google_image = gfx::Image::CreateFrom1xBitmap(*google_bitmap); | 525 gfx::Image google_image = gfx::Image::CreateFrom1xBitmap(*google_bitmap); |
| 527 | 526 |
| 528 Time time; | 527 Time time; |
| 529 GURL gurl; | 528 GURL gurl; |
| 530 backend_->thumbnail_db_->SetPageThumbnail(gurl, row1_id, &google_image, | 529 backend_->thumbnail_db_->SetPageThumbnail(gurl, row1_id, &google_image, |
| 531 score, time); | 530 score, time); |
| 532 scoped_ptr<SkBitmap> weewar_bitmap( | 531 scoped_ptr<SkBitmap> weewar_bitmap( |
| 533 gfx::JPEGCodec::Decode(kWeewarThumbnail, sizeof(kWeewarThumbnail))); | 532 gfx::JPEGCodec::Decode(kWeewarThumbnail, sizeof(kWeewarThumbnail))); |
| 534 gfx::Image weewar_image = gfx::Image::CreateFrom1xBitmap(*weewar_bitmap); | 533 gfx::Image weewar_image = gfx::Image::CreateFrom1xBitmap(*weewar_bitmap); |
| 535 backend_->thumbnail_db_->SetPageThumbnail(gurl, row2_id, &weewar_image, | 534 backend_->thumbnail_db_->SetPageThumbnail(gurl, row2_id, &weewar_image, |
| 536 score, time); | 535 score, time); |
| 537 | 536 |
| 538 // Star row1. | 537 // Star row1. |
| 539 bookmark_model_.AddURL( | 538 bookmark_model_.AddURL( |
| 540 bookmark_model_.bookmark_bar_node(), 0, string16(), row1.url()); | 539 bookmark_model_.bookmark_bar_node(), 0, string16(), row1.url()); |
| 541 | 540 |
| 542 // Set full text index for each one. | |
| 543 backend_->text_database_->AddPageData(row1.url(), row1_id, visit1_id, | |
| 544 row1.last_visit(), | |
| 545 UTF8ToUTF16("Title 1"), | |
| 546 UTF8ToUTF16("Body 1")); | |
| 547 backend_->text_database_->AddPageData(row2.url(), row2_id, visit2_id, | |
| 548 row2.last_visit(), | |
| 549 UTF8ToUTF16("Title 2"), | |
| 550 UTF8ToUTF16("Body 2")); | |
| 551 | |
| 552 // Now finally clear all history. | 541 // Now finally clear all history. |
| 553 backend_->DeleteAllHistory(); | 542 backend_->DeleteAllHistory(); |
| 554 | 543 |
| 555 // The first URL should be preserved but the time should be cleared. | 544 // The first URL should be preserved but the time should be cleared. |
| 556 EXPECT_TRUE(backend_->db_->GetRowForURL(row1.url(), &outrow1)); | 545 EXPECT_TRUE(backend_->db_->GetRowForURL(row1.url(), &outrow1)); |
| 557 EXPECT_EQ(row1.url(), outrow1.url()); | 546 EXPECT_EQ(row1.url(), outrow1.url()); |
| 558 EXPECT_EQ(0, outrow1.visit_count()); | 547 EXPECT_EQ(0, outrow1.visit_count()); |
| 559 EXPECT_EQ(0, outrow1.typed_count()); | 548 EXPECT_EQ(0, outrow1.typed_count()); |
| 560 EXPECT_TRUE(Time() == outrow1.last_visit()); | 549 EXPECT_TRUE(Time() == outrow1.last_visit()); |
| 561 | 550 |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 608 // The remaining URL should still reference the same favicon, even if its | 597 // The remaining URL should still reference the same favicon, even if its |
| 609 // ID has changed. | 598 // ID has changed. |
| 610 std::vector<IconMapping> mappings; | 599 std::vector<IconMapping> mappings; |
| 611 EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL( | 600 EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL( |
| 612 outrow1.url(), chrome::FAVICON, &mappings)); | 601 outrow1.url(), chrome::FAVICON, &mappings)); |
| 613 EXPECT_EQ(1u, mappings.size()); | 602 EXPECT_EQ(1u, mappings.size()); |
| 614 EXPECT_EQ(out_favicon1, mappings[0].icon_id); | 603 EXPECT_EQ(out_favicon1, mappings[0].icon_id); |
| 615 | 604 |
| 616 // The first URL should still be bookmarked. | 605 // The first URL should still be bookmarked. |
| 617 EXPECT_TRUE(bookmark_model_.IsBookmarked(row1.url())); | 606 EXPECT_TRUE(bookmark_model_.IsBookmarked(row1.url())); |
| 618 | |
| 619 // The full text database should have no data. | |
| 620 std::vector<TextDatabase::Match> text_matches; | |
| 621 Time first_time_searched; | |
| 622 backend_->text_database_->GetTextMatches(UTF8ToUTF16("Body"), | |
| 623 QueryOptions(), | |
| 624 &text_matches, | |
| 625 &first_time_searched); | |
| 626 EXPECT_EQ(0U, text_matches.size()); | |
| 627 } | 607 } |
| 628 | 608 |
| 629 // Checks that adding a visit, then calling DeleteAll, and then trying to add | 609 // Checks that adding a visit, then calling DeleteAll, and then trying to add |
| 630 // data for the visited page works. This can happen when clearing the history | 610 // data for the visited page works. This can happen when clearing the history |
| 631 // immediately after visiting a page. | 611 // immediately after visiting a page. |
| 632 TEST_F(HistoryBackendTest, DeleteAllThenAddData) { | 612 TEST_F(HistoryBackendTest, DeleteAllThenAddData) { |
| 633 ASSERT_TRUE(backend_.get()); | 613 ASSERT_TRUE(backend_.get()); |
| 634 | 614 |
| 635 Time visit_time = Time::Now(); | 615 Time visit_time = Time::Now(); |
| 636 GURL url("http://www.google.com/"); | 616 GURL url("http://www.google.com/"); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 652 // Clear all history. | 632 // Clear all history. |
| 653 backend_->DeleteAllHistory(); | 633 backend_->DeleteAllHistory(); |
| 654 | 634 |
| 655 // The row should be deleted. | 635 // The row should be deleted. |
| 656 EXPECT_FALSE(backend_->db_->GetRowForURL(url, &outrow)); | 636 EXPECT_FALSE(backend_->db_->GetRowForURL(url, &outrow)); |
| 657 | 637 |
| 658 // The visit should be deleted. | 638 // The visit should be deleted. |
| 659 backend_->db_->GetAllVisitsInRange(Time(), Time(), 0, &all_visits); | 639 backend_->db_->GetAllVisitsInRange(Time(), Time(), 0, &all_visits); |
| 660 ASSERT_EQ(0U, all_visits.size()); | 640 ASSERT_EQ(0U, all_visits.size()); |
| 661 | 641 |
| 662 // Try and set the full text index. | 642 // Try and set the title. |
| 663 backend_->SetPageTitle(url, UTF8ToUTF16("Title")); | 643 backend_->SetPageTitle(url, UTF8ToUTF16("Title")); |
| 664 backend_->SetPageContents(url, UTF8ToUTF16("Body")); | |
| 665 | 644 |
| 666 // The row should still be deleted. | 645 // The row should still be deleted. |
| 667 EXPECT_FALSE(backend_->db_->GetRowForURL(url, &outrow)); | 646 EXPECT_FALSE(backend_->db_->GetRowForURL(url, &outrow)); |
| 668 | 647 |
| 669 // The visit should still be deleted. | 648 // The visit should still be deleted. |
| 670 backend_->db_->GetAllVisitsInRange(Time(), Time(), 0, &all_visits); | 649 backend_->db_->GetAllVisitsInRange(Time(), Time(), 0, &all_visits); |
| 671 ASSERT_EQ(0U, all_visits.size()); | 650 ASSERT_EQ(0U, all_visits.size()); |
| 672 | |
| 673 // The full text database should have no data. | |
| 674 std::vector<TextDatabase::Match> text_matches; | |
| 675 Time first_time_searched; | |
| 676 backend_->text_database_->GetTextMatches(UTF8ToUTF16("Body"), | |
| 677 QueryOptions(), | |
| 678 &text_matches, | |
| 679 &first_time_searched); | |
| 680 EXPECT_EQ(0U, text_matches.size()); | |
| 681 } | 651 } |
| 682 | 652 |
| 683 TEST_F(HistoryBackendTest, URLsNoLongerBookmarked) { | 653 TEST_F(HistoryBackendTest, URLsNoLongerBookmarked) { |
| 684 GURL favicon_url1("http://www.google.com/favicon.ico"); | 654 GURL favicon_url1("http://www.google.com/favicon.ico"); |
| 685 GURL favicon_url2("http://news.google.com/favicon.ico"); | 655 GURL favicon_url2("http://news.google.com/favicon.ico"); |
| 686 | 656 |
| 687 std::vector<unsigned char> data; | 657 std::vector<unsigned char> data; |
| 688 data.push_back('1'); | 658 data.push_back('1'); |
| 689 chrome::FaviconID favicon1 = backend_->thumbnail_db_->AddFavicon( | 659 chrome::FaviconID favicon1 = backend_->thumbnail_db_->AddFavicon( |
| 690 favicon_url1, | 660 favicon_url1, |
| (...skipping 2114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2805 | 2775 |
| 2806 service->AddPage( | 2776 service->AddPage( |
| 2807 url, base::Time::Now(), NULL, 1, GURL(), RedirectList(), | 2777 url, base::Time::Now(), NULL, 1, GURL(), RedirectList(), |
| 2808 content::PAGE_TRANSITION_TYPED, SOURCE_BROWSED, false); | 2778 content::PAGE_TRANSITION_TYPED, SOURCE_BROWSED, false); |
| 2809 | 2779 |
| 2810 // This won't actually delete the URL, rather it'll empty out the visits. | 2780 // This won't actually delete the URL, rather it'll empty out the visits. |
| 2811 // This triggers blocking on the BookmarkModel. | 2781 // This triggers blocking on the BookmarkModel. |
| 2812 service->DeleteURL(url); | 2782 service->DeleteURL(url); |
| 2813 } | 2783 } |
| 2814 | 2784 |
| 2785 // Test DeleteFTSIndexDatabases deletes expected files. |
| 2786 TEST_F(HistoryBackendTest, DeleteFTSIndexDatabases) { |
| 2787 ASSERT_TRUE(backend_.get()); |
| 2788 |
| 2789 base::FilePath history_path(getTestDir()); |
| 2790 base::FilePath db1(history_path.AppendASCII("History Index 2013-05")); |
| 2791 base::FilePath db1_journal(db1.InsertBeforeExtensionASCII("-journal")); |
| 2792 base::FilePath db1_wal(db1.InsertBeforeExtensionASCII("-wal")); |
| 2793 base::FilePath db2_symlink(history_path.AppendASCII("History Index 2013-06")); |
| 2794 base::FilePath db2_actual(history_path.AppendASCII("Underlying DB")); |
| 2795 |
| 2796 // Setup dummy index database files. |
| 2797 int flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_READ; |
| 2798 EXPECT_NE(base::kInvalidPlatformFileValue, |
| 2799 base::CreatePlatformFile(db1, flags, NULL, NULL)); |
| 2800 EXPECT_NE(base::kInvalidPlatformFileValue, |
| 2801 base::CreatePlatformFile(db1_journal, flags, NULL, NULL)); |
| 2802 EXPECT_NE(base::kInvalidPlatformFileValue, |
| 2803 base::CreatePlatformFile(db1_wal, flags, NULL, NULL)); |
| 2804 EXPECT_NE(base::kInvalidPlatformFileValue, |
| 2805 base::CreatePlatformFile(db2_actual, flags, NULL, NULL)); |
| 2806 #if defined(OS_POSIX) |
| 2807 EXPECT_TRUE(file_util::CreateSymbolicLink(db2_actual, db2_symlink)); |
| 2808 #endif |
| 2809 |
| 2810 // Delete all DTS index databases. |
| 2811 backend_->DeleteFTSIndexDatabases(); |
| 2812 EXPECT_FALSE(base::PathExists(db1)); |
| 2813 EXPECT_FALSE(base::PathExists(db1_wal)); |
| 2814 EXPECT_FALSE(base::PathExists(db1_journal)); |
| 2815 EXPECT_FALSE(base::PathExists(db2_symlink)); |
| 2816 EXPECT_TRUE(base::PathExists(db2_actual)); // Symlinks shouldn't be followed. |
| 2817 } |
| 2818 |
| 2815 } // namespace history | 2819 } // namespace history |
| OLD | NEW |