| 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 24 matching lines...) Expand all Loading... |
| 35 #include "base/memory/scoped_ptr.h" | 35 #include "base/memory/scoped_ptr.h" |
| 36 #include "base/memory/scoped_vector.h" | 36 #include "base/memory/scoped_vector.h" |
| 37 #include "base/message_loop/message_loop.h" | 37 #include "base/message_loop/message_loop.h" |
| 38 #include "base/path_service.h" | 38 #include "base/path_service.h" |
| 39 #include "base/strings/string_util.h" | 39 #include "base/strings/string_util.h" |
| 40 #include "base/strings/stringprintf.h" | 40 #include "base/strings/stringprintf.h" |
| 41 #include "base/strings/utf_string_conversions.h" | 41 #include "base/strings/utf_string_conversions.h" |
| 42 #include "base/task/cancelable_task_tracker.h" | 42 #include "base/task/cancelable_task_tracker.h" |
| 43 #include "base/threading/platform_thread.h" | 43 #include "base/threading/platform_thread.h" |
| 44 #include "base/time/time.h" | 44 #include "base/time/time.h" |
| 45 #include "chrome/browser/history/download_row.h" | |
| 46 #include "chrome/browser/history/history_backend.h" | 45 #include "chrome/browser/history/history_backend.h" |
| 47 #include "chrome/browser/history/history_database.h" | 46 #include "chrome/browser/history/history_database.h" |
| 48 #include "chrome/browser/history/history_notifications.h" | 47 #include "chrome/browser/history/history_notifications.h" |
| 49 #include "chrome/browser/history/history_service.h" | 48 #include "chrome/browser/history/history_service.h" |
| 50 #include "chrome/browser/history/history_unittest_base.h" | 49 #include "chrome/browser/history/history_unittest_base.h" |
| 51 #include "chrome/browser/history/in_memory_history_backend.h" | 50 #include "chrome/browser/history/in_memory_history_backend.h" |
| 52 #include "chrome/common/chrome_constants.h" | 51 #include "chrome/common/chrome_constants.h" |
| 53 #include "chrome/common/chrome_paths.h" | 52 #include "chrome/common/chrome_paths.h" |
| 54 #include "chrome/tools/profiles/thumbnail-inl.h" | 53 #include "chrome/tools/profiles/thumbnail-inl.h" |
| 54 #include "components/history/content/browser/download_constants_utils.h" |
| 55 #include "components/history/core/browser/download_constants.h" |
| 56 #include "components/history/core/browser/download_row.h" |
| 55 #include "components/history/core/browser/history_constants.h" | 57 #include "components/history/core/browser/history_constants.h" |
| 56 #include "components/history/core/browser/history_db_task.h" | 58 #include "components/history/core/browser/history_db_task.h" |
| 57 #include "components/history/core/browser/in_memory_database.h" | 59 #include "components/history/core/browser/in_memory_database.h" |
| 58 #include "components/history/core/browser/page_usage_data.h" | 60 #include "components/history/core/browser/page_usage_data.h" |
| 59 #include "components/history/core/common/thumbnail_score.h" | 61 #include "components/history/core/common/thumbnail_score.h" |
| 60 #include "content/public/browser/download_item.h" | 62 #include "content/public/browser/download_item.h" |
| 61 #include "content/public/browser/notification_details.h" | 63 #include "content/public/browser/notification_details.h" |
| 62 #include "content/public/browser/notification_source.h" | 64 #include "content/public/browser/notification_source.h" |
| 63 #include "sql/connection.h" | 65 #include "sql/connection.h" |
| 64 #include "sql/statement.h" | 66 #include "sql/statement.h" |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 170 void TearDown() override { | 172 void TearDown() override { |
| 171 DeleteBackend(); | 173 DeleteBackend(); |
| 172 | 174 |
| 173 // Make sure we don't have any event pending that could disrupt the next | 175 // Make sure we don't have any event pending that could disrupt the next |
| 174 // test. | 176 // test. |
| 175 base::MessageLoop::current()->PostTask(FROM_HERE, | 177 base::MessageLoop::current()->PostTask(FROM_HERE, |
| 176 base::MessageLoop::QuitClosure()); | 178 base::MessageLoop::QuitClosure()); |
| 177 base::MessageLoop::current()->Run(); | 179 base::MessageLoop::current()->Run(); |
| 178 } | 180 } |
| 179 | 181 |
| 180 bool AddDownload(uint32 id, | 182 bool AddDownload(uint32 id, DownloadState state, const Time& time) { |
| 181 DownloadItem::DownloadState state, | |
| 182 const Time& time) { | |
| 183 std::vector<GURL> url_chain; | 183 std::vector<GURL> url_chain; |
| 184 url_chain.push_back(GURL("foo-url")); | 184 url_chain.push_back(GURL("foo-url")); |
| 185 | 185 |
| 186 DownloadRow download(base::FilePath(FILE_PATH_LITERAL("current-path")), | 186 DownloadRow download(base::FilePath(FILE_PATH_LITERAL("current-path")), |
| 187 base::FilePath(FILE_PATH_LITERAL("target-path")), | 187 base::FilePath(FILE_PATH_LITERAL("target-path")), |
| 188 url_chain, | 188 url_chain, |
| 189 GURL("http://referrer.com/"), | 189 GURL("http://referrer.com/"), |
| 190 "application/vnd.oasis.opendocument.text", | 190 "application/vnd.oasis.opendocument.text", |
| 191 "application/octet-stream", | 191 "application/octet-stream", |
| 192 time, | 192 time, |
| 193 time, | 193 time, |
| 194 std::string(), | 194 std::string(), |
| 195 std::string(), | 195 std::string(), |
| 196 0, | 196 0, |
| 197 512, | 197 512, |
| 198 state, | 198 state, |
| 199 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | 199 DownloadDangerType::NOT_DANGEROUS, |
| 200 content::DOWNLOAD_INTERRUPT_REASON_NONE, | 200 ToHistoryDownloadInterruptReason( |
| 201 content::DOWNLOAD_INTERRUPT_REASON_NONE), |
| 201 id, | 202 id, |
| 202 false, | 203 false, |
| 203 "by_ext_id", | 204 "by_ext_id", |
| 204 "by_ext_name"); | 205 "by_ext_name"); |
| 205 return db_->CreateDownload(download); | 206 return db_->CreateDownload(download); |
| 206 } | 207 } |
| 207 | 208 |
| 208 base::ScopedTempDir temp_dir_; | 209 base::ScopedTempDir temp_dir_; |
| 209 | 210 |
| 210 base::MessageLoopForUI message_loop_; | 211 base::MessageLoopForUI message_loop_; |
| (...skipping 29 matching lines...) Expand all Loading... |
| 240 | 241 |
| 241 // Initially there should be nothing in the downloads database. | 242 // Initially there should be nothing in the downloads database. |
| 242 std::vector<DownloadRow> downloads; | 243 std::vector<DownloadRow> downloads; |
| 243 db_->QueryDownloads(&downloads); | 244 db_->QueryDownloads(&downloads); |
| 244 EXPECT_EQ(0U, downloads.size()); | 245 EXPECT_EQ(0U, downloads.size()); |
| 245 | 246 |
| 246 // Add a download, test that it was added correctly, remove it, test that it | 247 // Add a download, test that it was added correctly, remove it, test that it |
| 247 // was removed. | 248 // was removed. |
| 248 Time now = Time(); | 249 Time now = Time(); |
| 249 uint32 id = 1; | 250 uint32 id = 1; |
| 250 EXPECT_TRUE(AddDownload(id, DownloadItem::COMPLETE, Time())); | 251 EXPECT_TRUE(AddDownload(id, DownloadState::COMPLETE, Time())); |
| 251 db_->QueryDownloads(&downloads); | 252 db_->QueryDownloads(&downloads); |
| 252 EXPECT_EQ(1U, downloads.size()); | 253 EXPECT_EQ(1U, downloads.size()); |
| 253 | 254 |
| 254 EXPECT_EQ(base::FilePath(FILE_PATH_LITERAL("current-path")), | 255 EXPECT_EQ(base::FilePath(FILE_PATH_LITERAL("current-path")), |
| 255 downloads[0].current_path); | 256 downloads[0].current_path); |
| 256 EXPECT_EQ(base::FilePath(FILE_PATH_LITERAL("target-path")), | 257 EXPECT_EQ(base::FilePath(FILE_PATH_LITERAL("target-path")), |
| 257 downloads[0].target_path); | 258 downloads[0].target_path); |
| 258 EXPECT_EQ(1UL, downloads[0].url_chain.size()); | 259 EXPECT_EQ(1UL, downloads[0].url_chain.size()); |
| 259 EXPECT_EQ(GURL("foo-url"), downloads[0].url_chain[0]); | 260 EXPECT_EQ(GURL("foo-url"), downloads[0].url_chain[0]); |
| 260 EXPECT_EQ(std::string("http://referrer.com/"), | 261 EXPECT_EQ(std::string("http://referrer.com/"), |
| 261 std::string(downloads[0].referrer_url.spec())); | 262 std::string(downloads[0].referrer_url.spec())); |
| 262 EXPECT_EQ(now, downloads[0].start_time); | 263 EXPECT_EQ(now, downloads[0].start_time); |
| 263 EXPECT_EQ(now, downloads[0].end_time); | 264 EXPECT_EQ(now, downloads[0].end_time); |
| 264 EXPECT_EQ(0, downloads[0].received_bytes); | 265 EXPECT_EQ(0, downloads[0].received_bytes); |
| 265 EXPECT_EQ(512, downloads[0].total_bytes); | 266 EXPECT_EQ(512, downloads[0].total_bytes); |
| 266 EXPECT_EQ(DownloadItem::COMPLETE, downloads[0].state); | 267 EXPECT_EQ(DownloadState::COMPLETE, downloads[0].state); |
| 267 EXPECT_EQ(content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | 268 EXPECT_EQ(DownloadDangerType::NOT_DANGEROUS, downloads[0].danger_type); |
| 268 downloads[0].danger_type); | |
| 269 EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_NONE, | 269 EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_NONE, |
| 270 downloads[0].interrupt_reason); | 270 downloads[0].interrupt_reason); |
| 271 EXPECT_FALSE(downloads[0].opened); | 271 EXPECT_FALSE(downloads[0].opened); |
| 272 EXPECT_EQ("by_ext_id", downloads[0].by_ext_id); | 272 EXPECT_EQ("by_ext_id", downloads[0].by_ext_id); |
| 273 EXPECT_EQ("by_ext_name", downloads[0].by_ext_name); | 273 EXPECT_EQ("by_ext_name", downloads[0].by_ext_name); |
| 274 EXPECT_EQ("application/vnd.oasis.opendocument.text", downloads[0].mime_type); | 274 EXPECT_EQ("application/vnd.oasis.opendocument.text", downloads[0].mime_type); |
| 275 EXPECT_EQ("application/octet-stream", downloads[0].original_mime_type); | 275 EXPECT_EQ("application/octet-stream", downloads[0].original_mime_type); |
| 276 | 276 |
| 277 db_->QueryDownloads(&downloads); | 277 db_->QueryDownloads(&downloads); |
| 278 EXPECT_EQ(1U, downloads.size()); | 278 EXPECT_EQ(1U, downloads.size()); |
| (...skipping 440 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 719 } | 719 } |
| 720 | 720 |
| 721 TEST_F(HistoryBackendDBTest, ConfirmDownloadRowCreateAndDelete) { | 721 TEST_F(HistoryBackendDBTest, ConfirmDownloadRowCreateAndDelete) { |
| 722 // Create the DB. | 722 // Create the DB. |
| 723 CreateBackendAndDatabase(); | 723 CreateBackendAndDatabase(); |
| 724 | 724 |
| 725 base::Time now(base::Time::Now()); | 725 base::Time now(base::Time::Now()); |
| 726 | 726 |
| 727 // Add some downloads. | 727 // Add some downloads. |
| 728 uint32 id1 = 1, id2 = 2, id3 = 3; | 728 uint32 id1 = 1, id2 = 2, id3 = 3; |
| 729 AddDownload(id1, DownloadItem::COMPLETE, now); | 729 AddDownload(id1, DownloadState::COMPLETE, now); |
| 730 AddDownload(id2, DownloadItem::COMPLETE, now + base::TimeDelta::FromDays(2)); | 730 AddDownload(id2, DownloadState::COMPLETE, now + base::TimeDelta::FromDays(2)); |
| 731 AddDownload(id3, DownloadItem::COMPLETE, now - base::TimeDelta::FromDays(2)); | 731 AddDownload(id3, DownloadState::COMPLETE, now - base::TimeDelta::FromDays(2)); |
| 732 | 732 |
| 733 // Confirm that resulted in the correct number of rows in the DB. | 733 // Confirm that resulted in the correct number of rows in the DB. |
| 734 DeleteBackend(); | 734 DeleteBackend(); |
| 735 { | 735 { |
| 736 sql::Connection db; | 736 sql::Connection db; |
| 737 ASSERT_TRUE(db.Open(history_dir_.Append(kHistoryFilename))); | 737 ASSERT_TRUE(db.Open(history_dir_.Append(kHistoryFilename))); |
| 738 sql::Statement statement(db.GetUniqueStatement( | 738 sql::Statement statement(db.GetUniqueStatement( |
| 739 "Select Count(*) from downloads")); | 739 "Select Count(*) from downloads")); |
| 740 EXPECT_TRUE(statement.Step()); | 740 EXPECT_TRUE(statement.Step()); |
| 741 EXPECT_EQ(3, statement.ColumnInt(0)); | 741 EXPECT_EQ(3, statement.ColumnInt(0)); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 775 url_chain, | 775 url_chain, |
| 776 GURL(std::string()), | 776 GURL(std::string()), |
| 777 "application/octet-stream", | 777 "application/octet-stream", |
| 778 "application/octet-stream", | 778 "application/octet-stream", |
| 779 now, | 779 now, |
| 780 now, | 780 now, |
| 781 std::string(), | 781 std::string(), |
| 782 std::string(), | 782 std::string(), |
| 783 0, | 783 0, |
| 784 512, | 784 512, |
| 785 DownloadItem::COMPLETE, | 785 DownloadState::COMPLETE, |
| 786 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | 786 DownloadDangerType::NOT_DANGEROUS, |
| 787 content::DOWNLOAD_INTERRUPT_REASON_NONE, | 787 ToHistoryDownloadInterruptReason( |
| 788 content::DOWNLOAD_INTERRUPT_REASON_NONE), |
| 788 1, | 789 1, |
| 789 0, | 790 0, |
| 790 "by_ext_id", | 791 "by_ext_id", |
| 791 "by_ext_name"); | 792 "by_ext_name"); |
| 792 | 793 |
| 793 // Creating records without any urls should fail. | 794 // Creating records without any urls should fail. |
| 794 EXPECT_FALSE(db_->CreateDownload(download)); | 795 EXPECT_FALSE(db_->CreateDownload(download)); |
| 795 | 796 |
| 796 download.url_chain.push_back(GURL("foo-url")); | 797 download.url_chain.push_back(GURL("foo-url")); |
| 797 EXPECT_TRUE(db_->CreateDownload(download)); | 798 EXPECT_TRUE(db_->CreateDownload(download)); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 824 } | 825 } |
| 825 } | 826 } |
| 826 | 827 |
| 827 TEST_F(HistoryBackendDBTest, ConfirmDownloadInProgressCleanup) { | 828 TEST_F(HistoryBackendDBTest, ConfirmDownloadInProgressCleanup) { |
| 828 // Create the DB. | 829 // Create the DB. |
| 829 CreateBackendAndDatabase(); | 830 CreateBackendAndDatabase(); |
| 830 | 831 |
| 831 base::Time now(base::Time::Now()); | 832 base::Time now(base::Time::Now()); |
| 832 | 833 |
| 833 // Put an IN_PROGRESS download in the DB. | 834 // Put an IN_PROGRESS download in the DB. |
| 834 AddDownload(1, DownloadItem::IN_PROGRESS, now); | 835 AddDownload(1, DownloadState::IN_PROGRESS, now); |
| 835 | 836 |
| 836 // Confirm that they made it into the DB unchanged. | 837 // Confirm that they made it into the DB unchanged. |
| 837 DeleteBackend(); | 838 DeleteBackend(); |
| 838 { | 839 { |
| 839 sql::Connection db; | 840 sql::Connection db; |
| 840 ASSERT_TRUE(db.Open(history_dir_.Append(kHistoryFilename))); | 841 ASSERT_TRUE(db.Open(history_dir_.Append(kHistoryFilename))); |
| 841 sql::Statement statement(db.GetUniqueStatement( | 842 sql::Statement statement(db.GetUniqueStatement( |
| 842 "Select Count(*) from downloads")); | 843 "Select Count(*) from downloads")); |
| 843 EXPECT_TRUE(statement.Step()); | 844 EXPECT_TRUE(statement.Step()); |
| 844 EXPECT_EQ(1, statement.ColumnInt(0)); | 845 EXPECT_EQ(1, statement.ColumnInt(0)); |
| 845 | 846 |
| 846 sql::Statement statement1(db.GetUniqueStatement( | 847 sql::Statement statement1(db.GetUniqueStatement( |
| 847 "Select state, interrupt_reason from downloads")); | 848 "Select state, interrupt_reason from downloads")); |
| 848 EXPECT_TRUE(statement1.Step()); | 849 EXPECT_TRUE(statement1.Step()); |
| 849 EXPECT_EQ(DownloadDatabase::kStateInProgress, statement1.ColumnInt(0)); | 850 EXPECT_EQ(DownloadStateToInt(DownloadState::IN_PROGRESS), |
| 851 statement1.ColumnInt(0)); |
| 850 EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_NONE, statement1.ColumnInt(1)); | 852 EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_NONE, statement1.ColumnInt(1)); |
| 851 EXPECT_FALSE(statement1.Step()); | 853 EXPECT_FALSE(statement1.Step()); |
| 852 } | 854 } |
| 853 | 855 |
| 854 // Read in the DB through query downloads, then test that the | 856 // Read in the DB through query downloads, then test that the |
| 855 // right transformation was returned. | 857 // right transformation was returned. |
| 856 CreateBackendAndDatabase(); | 858 CreateBackendAndDatabase(); |
| 857 std::vector<DownloadRow> results; | 859 std::vector<DownloadRow> results; |
| 858 db_->QueryDownloads(&results); | 860 db_->QueryDownloads(&results); |
| 859 ASSERT_EQ(1u, results.size()); | 861 ASSERT_EQ(1u, results.size()); |
| 860 EXPECT_EQ(content::DownloadItem::INTERRUPTED, results[0].state); | 862 EXPECT_EQ(DownloadState::INTERRUPTED, results[0].state); |
| 861 EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_CRASH, | 863 EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_CRASH, |
| 862 results[0].interrupt_reason); | 864 results[0].interrupt_reason); |
| 863 | 865 |
| 864 // Allow the update to propagate, shut down the DB, and confirm that | 866 // Allow the update to propagate, shut down the DB, and confirm that |
| 865 // the query updated the on disk database as well. | 867 // the query updated the on disk database as well. |
| 866 base::MessageLoop::current()->RunUntilIdle(); | 868 base::MessageLoop::current()->RunUntilIdle(); |
| 867 DeleteBackend(); | 869 DeleteBackend(); |
| 868 { | 870 { |
| 869 sql::Connection db; | 871 sql::Connection db; |
| 870 ASSERT_TRUE(db.Open(history_dir_.Append(kHistoryFilename))); | 872 ASSERT_TRUE(db.Open(history_dir_.Append(kHistoryFilename))); |
| 871 sql::Statement statement(db.GetUniqueStatement( | 873 sql::Statement statement(db.GetUniqueStatement( |
| 872 "Select Count(*) from downloads")); | 874 "Select Count(*) from downloads")); |
| 873 EXPECT_TRUE(statement.Step()); | 875 EXPECT_TRUE(statement.Step()); |
| 874 EXPECT_EQ(1, statement.ColumnInt(0)); | 876 EXPECT_EQ(1, statement.ColumnInt(0)); |
| 875 | 877 |
| 876 sql::Statement statement1(db.GetUniqueStatement( | 878 sql::Statement statement1(db.GetUniqueStatement( |
| 877 "Select state, interrupt_reason from downloads")); | 879 "Select state, interrupt_reason from downloads")); |
| 878 EXPECT_TRUE(statement1.Step()); | 880 EXPECT_TRUE(statement1.Step()); |
| 879 EXPECT_EQ(DownloadDatabase::kStateInterrupted, statement1.ColumnInt(0)); | 881 EXPECT_EQ(DownloadStateToInt(DownloadState::INTERRUPTED), |
| 882 statement1.ColumnInt(0)); |
| 880 EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_CRASH, | 883 EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_CRASH, |
| 881 statement1.ColumnInt(1)); | 884 statement1.ColumnInt(1)); |
| 882 EXPECT_FALSE(statement1.Step()); | 885 EXPECT_FALSE(statement1.Step()); |
| 883 } | 886 } |
| 884 } | 887 } |
| 885 | 888 |
| 886 struct InterruptReasonAssociation { | 889 struct InterruptReasonAssociation { |
| 887 std::string name; | 890 std::string name; |
| 888 int value; | 891 int value; |
| 889 }; | 892 }; |
| (...skipping 973 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1863 std::vector<PageUsageData*> results; | 1866 std::vector<PageUsageData*> results; |
| 1864 db_->QuerySegmentUsage(segment_time, 10, &results); | 1867 db_->QuerySegmentUsage(segment_time, 10, &results); |
| 1865 ASSERT_EQ(1u, results.size()); | 1868 ASSERT_EQ(1u, results.size()); |
| 1866 EXPECT_EQ(url, results[0]->GetURL()); | 1869 EXPECT_EQ(url, results[0]->GetURL()); |
| 1867 EXPECT_EQ(segment_id, results[0]->GetID()); | 1870 EXPECT_EQ(segment_id, results[0]->GetID()); |
| 1868 EXPECT_EQ(title, results[0]->GetTitle()); | 1871 EXPECT_EQ(title, results[0]->GetTitle()); |
| 1869 STLDeleteElements(&results); | 1872 STLDeleteElements(&results); |
| 1870 } | 1873 } |
| 1871 | 1874 |
| 1872 } // namespace history | 1875 } // namespace history |
| OLD | NEW |