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 619 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
630 "VALUES(" | 630 "VALUES(" |
631 " 1, '435A5C7A-F6B7-4DF2-8696-22E4FCBA3EB2', 'foo.txt', 'foo.txt'," | 631 " 1, '435A5C7A-F6B7-4DF2-8696-22E4FCBA3EB2', 'foo.txt', 'foo.txt'," |
632 " 13104873187307670, 11, 11, 1, 0, 0, X'', 13104873187521021, 0," | 632 " 13104873187307670, 11, 11, 1, 0, 0, X'', 13104873187521021, 0," |
633 " 'http://example.com/dl/', '', '', '', '', '', 'text/plain'," | 633 " 'http://example.com/dl/', '', '', '', '', '', 'text/plain'," |
634 " 'text/plain')")); | 634 " 'text/plain')")); |
635 ASSERT_TRUE(s.Run()); | 635 ASSERT_TRUE(s.Run()); |
636 } | 636 } |
637 { | 637 { |
638 sql::Statement s(db.GetUniqueStatement( | 638 sql::Statement s(db.GetUniqueStatement( |
639 "INSERT INTO downloads_url_chains (id, chain_index, url) VALUES " | 639 "INSERT INTO downloads_url_chains (id, chain_index, url) VALUES " |
640 "(4, 0, 'url')")); | 640 "(1, 0, 'url')")); |
641 ASSERT_TRUE(s.Run()); | 641 ASSERT_TRUE(s.Run()); |
642 } | 642 } |
643 } | 643 } |
644 | 644 |
645 // Re-open the db using the HistoryDatabase, which should migrate to the | 645 // Re-open the db using the HistoryDatabase, which should migrate to the |
646 // current version, creating the tab_url and tab_referrer_url columns. | 646 // current version, creating the tab_url and tab_referrer_url columns. |
647 CreateBackendAndDatabase(); | 647 CreateBackendAndDatabase(); |
648 DeleteBackend(); | 648 DeleteBackend(); |
649 { | 649 { |
650 // Re-open the db for manual manipulation. | 650 // Re-open the db for manual manipulation. |
(...skipping 11 matching lines...) Expand all Loading... |
662 { | 662 { |
663 sql::Statement s(db.GetUniqueStatement( | 663 sql::Statement s(db.GetUniqueStatement( |
664 "SELECT tab_url, tab_referrer_url from downloads")); | 664 "SELECT tab_url, tab_referrer_url from downloads")); |
665 EXPECT_TRUE(s.Step()); | 665 EXPECT_TRUE(s.Step()); |
666 EXPECT_EQ(std::string(), s.ColumnString(0)); | 666 EXPECT_EQ(std::string(), s.ColumnString(0)); |
667 EXPECT_EQ(std::string(), s.ColumnString(1)); | 667 EXPECT_EQ(std::string(), s.ColumnString(1)); |
668 } | 668 } |
669 } | 669 } |
670 } | 670 } |
671 | 671 |
| 672 TEST_F(HistoryBackendDBTest, MigrateDownloadSiteInstanceUrl) { |
| 673 ASSERT_NO_FATAL_FAILURE(CreateDBVersion(31)); |
| 674 { |
| 675 sql::Connection db; |
| 676 ASSERT_TRUE(db.Open(history_dir_.Append(kHistoryFilename))); |
| 677 { |
| 678 sql::Statement s(db.GetUniqueStatement( |
| 679 "INSERT INTO downloads (" |
| 680 " id, guid, current_path, target_path, start_time, received_bytes," |
| 681 " total_bytes, state, danger_type, interrupt_reason, hash," |
| 682 " end_time, opened, referrer, tab_url, tab_referrer_url," |
| 683 " http_method, by_ext_id, by_ext_name, etag, last_modified," |
| 684 " mime_type, original_mime_type)" |
| 685 "VALUES(" |
| 686 " 1, '435A5C7A-F6B7-4DF2-8696-22E4FCBA3EB2', 'foo.txt', 'foo.txt'," |
| 687 " 13104873187307670, 11, 11, 1, 0, 0, X'', 13104873187521021, 0," |
| 688 " 'http://example.com/dl/', '', '', '', '', '', '', ''," |
| 689 " 'text/plain', 'text/plain')")); |
| 690 ASSERT_TRUE(s.Run()); |
| 691 } |
| 692 { |
| 693 sql::Statement s(db.GetUniqueStatement( |
| 694 "INSERT INTO downloads_url_chains (id, chain_index, url) VALUES " |
| 695 "(1, 0, 'url')")); |
| 696 ASSERT_TRUE(s.Run()); |
| 697 } |
| 698 } |
| 699 |
| 700 // Re-open the db using the HistoryDatabase, which should migrate to the |
| 701 // current version, creating the site_url column. |
| 702 CreateBackendAndDatabase(); |
| 703 DeleteBackend(); |
| 704 { |
| 705 // Re-open the db for manual manipulation. |
| 706 sql::Connection db; |
| 707 ASSERT_TRUE(db.Open(history_dir_.Append(kHistoryFilename))); |
| 708 // The version should have been updated. |
| 709 int cur_version = HistoryDatabase::GetCurrentVersion(); |
| 710 ASSERT_LE(31, cur_version); |
| 711 { |
| 712 sql::Statement s(db.GetUniqueStatement( |
| 713 "SELECT value FROM meta WHERE key = 'version'")); |
| 714 EXPECT_TRUE(s.Step()); |
| 715 EXPECT_EQ(cur_version, s.ColumnInt(0)); |
| 716 } |
| 717 { |
| 718 sql::Statement s(db.GetUniqueStatement("SELECT site_url from downloads")); |
| 719 EXPECT_TRUE(s.Step()); |
| 720 EXPECT_EQ(std::string(), s.ColumnString(0)); |
| 721 } |
| 722 } |
| 723 } |
| 724 |
672 TEST_F(HistoryBackendDBTest, DownloadCreateAndQuery) { | 725 TEST_F(HistoryBackendDBTest, DownloadCreateAndQuery) { |
673 CreateBackendAndDatabase(); | 726 CreateBackendAndDatabase(); |
674 | 727 |
675 ASSERT_EQ(0u, db_->CountDownloads()); | 728 ASSERT_EQ(0u, db_->CountDownloads()); |
676 | 729 |
677 std::vector<GURL> url_chain; | 730 std::vector<GURL> url_chain; |
678 url_chain.push_back(GURL("http://example.com/a")); | 731 url_chain.push_back(GURL("http://example.com/a")); |
679 url_chain.push_back(GURL("http://example.com/b")); | 732 url_chain.push_back(GURL("http://example.com/b")); |
680 url_chain.push_back(GURL("http://example.com/c")); | 733 url_chain.push_back(GURL("http://example.com/c")); |
681 | 734 |
682 base::Time start_time(base::Time::Now()); | 735 base::Time start_time(base::Time::Now()); |
683 base::Time end_time(start_time + base::TimeDelta::FromHours(1)); | 736 base::Time end_time(start_time + base::TimeDelta::FromHours(1)); |
684 | 737 |
685 DownloadRow download_A( | 738 DownloadRow download_A( |
686 base::FilePath(FILE_PATH_LITERAL("/path/1")), | 739 base::FilePath(FILE_PATH_LITERAL("/path/1")), |
687 base::FilePath(FILE_PATH_LITERAL("/path/2")), url_chain, | 740 base::FilePath(FILE_PATH_LITERAL("/path/2")), url_chain, |
688 GURL("http://example.com/referrer"), GURL("http://example.com/tab-url"), | 741 GURL("http://example.com/referrer"), GURL("http://example.com"), |
| 742 GURL("http://example.com/tab-url"), |
689 GURL("http://example.com/tab-referrer"), "GET", "mime/type", | 743 GURL("http://example.com/tab-referrer"), "GET", "mime/type", |
690 "original/mime-type", start_time, end_time, "etag1", "last_modified_1", | 744 "original/mime-type", start_time, end_time, "etag1", "last_modified_1", |
691 100, 1000, DownloadState::INTERRUPTED, DownloadDangerType::NOT_DANGEROUS, | 745 100, 1000, DownloadState::INTERRUPTED, DownloadDangerType::NOT_DANGEROUS, |
692 kTestDownloadInterruptReasonCrash, "hash-value1", 1, | 746 kTestDownloadInterruptReasonCrash, "hash-value1", 1, |
693 "FE672168-26EF-4275-A149-FEC25F6A75F9", false, "extension-id", | 747 "FE672168-26EF-4275-A149-FEC25F6A75F9", false, "extension-id", |
694 "extension-name"); | 748 "extension-name"); |
695 ASSERT_TRUE(db_->CreateDownload(download_A)); | 749 ASSERT_TRUE(db_->CreateDownload(download_A)); |
696 | 750 |
697 url_chain.push_back(GURL("http://example.com/d")); | 751 url_chain.push_back(GURL("http://example.com/d")); |
698 | 752 |
699 base::Time start_time2(start_time + base::TimeDelta::FromHours(10)); | 753 base::Time start_time2(start_time + base::TimeDelta::FromHours(10)); |
700 base::Time end_time2(end_time + base::TimeDelta::FromHours(10)); | 754 base::Time end_time2(end_time + base::TimeDelta::FromHours(10)); |
701 | 755 |
702 DownloadRow download_B( | 756 DownloadRow download_B( |
703 base::FilePath(FILE_PATH_LITERAL("/path/3")), | 757 base::FilePath(FILE_PATH_LITERAL("/path/3")), |
704 base::FilePath(FILE_PATH_LITERAL("/path/4")), url_chain, | 758 base::FilePath(FILE_PATH_LITERAL("/path/4")), url_chain, |
705 GURL("http://example.com/referrer2"), GURL("http://example.com/tab-url2"), | 759 GURL("http://example.com/referrer2"), GURL("http://2.example.com"), |
| 760 GURL("http://example.com/tab-url2"), |
706 GURL("http://example.com/tab-referrer2"), "POST", "mime/type2", | 761 GURL("http://example.com/tab-referrer2"), "POST", "mime/type2", |
707 "original/mime-type2", start_time2, end_time2, "etag2", "last_modified_2", | 762 "original/mime-type2", start_time2, end_time2, "etag2", "last_modified_2", |
708 1001, 1001, DownloadState::COMPLETE, DownloadDangerType::DANGEROUS_FILE, | 763 1001, 1001, DownloadState::COMPLETE, DownloadDangerType::DANGEROUS_FILE, |
709 kTestDownloadInterruptReasonNone, std::string(), 2, | 764 kTestDownloadInterruptReasonNone, std::string(), 2, |
710 "b70f3869-7d75-4878-acb4-4caf7026d12b", false, "extension-id", | 765 "b70f3869-7d75-4878-acb4-4caf7026d12b", false, "extension-id", |
711 "extension-name"); | 766 "extension-name"); |
712 ASSERT_TRUE(db_->CreateDownload(download_B)); | 767 ASSERT_TRUE(db_->CreateDownload(download_B)); |
713 | 768 |
714 EXPECT_EQ(2u, db_->CountDownloads()); | 769 EXPECT_EQ(2u, db_->CountDownloads()); |
715 | 770 |
(...skipping 18 matching lines...) Expand all Loading... |
734 url_chain.push_back(GURL("http://example.com/a")); | 789 url_chain.push_back(GURL("http://example.com/a")); |
735 url_chain.push_back(GURL("http://example.com/b")); | 790 url_chain.push_back(GURL("http://example.com/b")); |
736 url_chain.push_back(GURL("http://example.com/c")); | 791 url_chain.push_back(GURL("http://example.com/c")); |
737 | 792 |
738 base::Time start_time(base::Time::Now()); | 793 base::Time start_time(base::Time::Now()); |
739 base::Time end_time(start_time + base::TimeDelta::FromHours(1)); | 794 base::Time end_time(start_time + base::TimeDelta::FromHours(1)); |
740 | 795 |
741 DownloadRow download( | 796 DownloadRow download( |
742 base::FilePath(FILE_PATH_LITERAL("/path/1")), | 797 base::FilePath(FILE_PATH_LITERAL("/path/1")), |
743 base::FilePath(FILE_PATH_LITERAL("/path/2")), url_chain, | 798 base::FilePath(FILE_PATH_LITERAL("/path/2")), url_chain, |
744 GURL("http://example.com/referrer"), GURL("http://example.com/tab-url"), | 799 GURL("http://example.com/referrer"), GURL("http://example.com"), |
| 800 GURL("http://example.com/tab-url"), |
745 GURL("http://example.com/tab-referrer"), "GET", "mime/type", | 801 GURL("http://example.com/tab-referrer"), "GET", "mime/type", |
746 "original/mime-type", start_time, end_time, "etag1", "last_modified_1", | 802 "original/mime-type", start_time, end_time, "etag1", "last_modified_1", |
747 100, 1000, DownloadState::INTERRUPTED, DownloadDangerType::NOT_DANGEROUS, | 803 100, 1000, DownloadState::INTERRUPTED, DownloadDangerType::NOT_DANGEROUS, |
748 3, "some-hash-value", 1, "FE672168-26EF-4275-A149-FEC25F6A75F9", false, | 804 3, "some-hash-value", 1, "FE672168-26EF-4275-A149-FEC25F6A75F9", false, |
749 "extension-id", "extension-name"); | 805 "extension-id", "extension-name"); |
750 db_->CreateDownload(download); | 806 db_->CreateDownload(download); |
751 | 807 |
752 download.current_path = | 808 download.current_path = |
753 base::FilePath(FILE_PATH_LITERAL("/new/current_path")); | 809 base::FilePath(FILE_PATH_LITERAL("/new/current_path")); |
754 download.target_path = base::FilePath(FILE_PATH_LITERAL("/new/target_path")); | 810 download.target_path = base::FilePath(FILE_PATH_LITERAL("/new/target_path")); |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
833 } | 889 } |
834 | 890 |
835 TEST_F(HistoryBackendDBTest, DownloadNukeRecordsMissingURLs) { | 891 TEST_F(HistoryBackendDBTest, DownloadNukeRecordsMissingURLs) { |
836 CreateBackendAndDatabase(); | 892 CreateBackendAndDatabase(); |
837 base::Time now(base::Time::Now()); | 893 base::Time now(base::Time::Now()); |
838 std::vector<GURL> url_chain; | 894 std::vector<GURL> url_chain; |
839 DownloadRow download( | 895 DownloadRow download( |
840 base::FilePath(FILE_PATH_LITERAL("foo-path")), | 896 base::FilePath(FILE_PATH_LITERAL("foo-path")), |
841 base::FilePath(FILE_PATH_LITERAL("foo-path")), url_chain, | 897 base::FilePath(FILE_PATH_LITERAL("foo-path")), url_chain, |
842 GURL(std::string()), GURL(std::string()), GURL(std::string()), | 898 GURL(std::string()), GURL(std::string()), GURL(std::string()), |
843 std::string(), "application/octet-stream", "application/octet-stream", | 899 GURL(std::string()), std::string(), "application/octet-stream", |
844 now, now, std::string(), std::string(), 0, 512, DownloadState::COMPLETE, | 900 "application/octet-stream", now, now, std::string(), std::string(), 0, |
845 DownloadDangerType::NOT_DANGEROUS, kTestDownloadInterruptReasonNone, | 901 512, DownloadState::COMPLETE, DownloadDangerType::NOT_DANGEROUS, |
846 std::string(), 1, "05AF6C8E-E4E0-45D7-B5CE-BC99F7019918", 0, "by_ext_id", | 902 kTestDownloadInterruptReasonNone, std::string(), 1, |
847 "by_ext_name"); | 903 "05AF6C8E-E4E0-45D7-B5CE-BC99F7019918", 0, "by_ext_id", "by_ext_name"); |
848 | 904 |
849 // Creating records without any urls should fail. | 905 // Creating records without any urls should fail. |
850 EXPECT_FALSE(db_->CreateDownload(download)); | 906 EXPECT_FALSE(db_->CreateDownload(download)); |
851 | 907 |
852 download.url_chain.push_back(GURL("foo-url")); | 908 download.url_chain.push_back(GURL("foo-url")); |
853 EXPECT_TRUE(db_->CreateDownload(download)); | 909 EXPECT_TRUE(db_->CreateDownload(download)); |
854 | 910 |
855 // Pretend that the URLs were dropped. | 911 // Pretend that the URLs were dropped. |
856 DeleteBackend(); | 912 DeleteBackend(); |
857 { | 913 { |
(...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1088 // URL should win instead. | 1144 // URL should win instead. |
1089 std::vector<std::unique_ptr<PageUsageData>> results2 = | 1145 std::vector<std::unique_ptr<PageUsageData>> results2 = |
1090 db_->QuerySegmentUsage(time, 1, base::Bind(&FilterURL)); | 1146 db_->QuerySegmentUsage(time, 1, base::Bind(&FilterURL)); |
1091 ASSERT_EQ(1u, results2.size()); | 1147 ASSERT_EQ(1u, results2.size()); |
1092 EXPECT_EQ(url2, results2[0]->GetURL()); | 1148 EXPECT_EQ(url2, results2[0]->GetURL()); |
1093 EXPECT_EQ(segment_id2, results2[0]->GetID()); | 1149 EXPECT_EQ(segment_id2, results2[0]->GetID()); |
1094 } | 1150 } |
1095 | 1151 |
1096 } // namespace | 1152 } // namespace |
1097 } // namespace history | 1153 } // namespace history |
OLD | NEW |