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