| 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 "chrome/browser/download/download_history.h" |
| 6 |
| 5 #include <stddef.h> | 7 #include <stddef.h> |
| 6 #include <stdint.h> | 8 #include <stdint.h> |
| 7 | 9 #include <utility> |
| 8 #include <vector> | 10 #include <vector> |
| 9 | 11 |
| 10 #include "base/macros.h" | 12 #include "base/macros.h" |
| 11 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
| 12 #include "base/rand_util.h" | 14 #include "base/rand_util.h" |
| 13 #include "base/stl_util.h" | 15 #include "base/stl_util.h" |
| 14 #include "chrome/browser/download/download_history.h" | |
| 15 #include "components/history/content/browser/download_constants_utils.h" | 16 #include "components/history/content/browser/download_constants_utils.h" |
| 16 #include "components/history/core/browser/download_constants.h" | 17 #include "components/history/core/browser/download_constants.h" |
| 17 #include "components/history/core/browser/download_row.h" | 18 #include "components/history/core/browser/download_row.h" |
| 18 #include "components/history/core/browser/history_service.h" | 19 #include "components/history/core/browser/history_service.h" |
| 19 #include "content/public/test/mock_download_item.h" | 20 #include "content/public/test/mock_download_item.h" |
| 20 #include "content/public/test/mock_download_manager.h" | 21 #include "content/public/test/mock_download_manager.h" |
| 21 #include "content/public/test/test_browser_thread.h" | 22 #include "content/public/test/test_browser_thread.h" |
| 22 #include "content/public/test/test_utils.h" | 23 #include "content/public/test/test_utils.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
| 24 | 25 |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 83 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 83 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | 84 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
| 84 base::Bind(&FakeHistoryAdapter::QueryDownloadsDone, | 85 base::Bind(&FakeHistoryAdapter::QueryDownloadsDone, |
| 85 base::Unretained(this), callback)); | 86 base::Unretained(this), callback)); |
| 86 } | 87 } |
| 87 | 88 |
| 88 void QueryDownloadsDone( | 89 void QueryDownloadsDone( |
| 89 const history::HistoryService::DownloadQueryCallback& callback) { | 90 const history::HistoryService::DownloadQueryCallback& callback) { |
| 90 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 91 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 91 CHECK(expect_query_downloads_.get()); | 92 CHECK(expect_query_downloads_.get()); |
| 92 callback.Run(expect_query_downloads_.Pass()); | 93 callback.Run(std::move(expect_query_downloads_)); |
| 93 } | 94 } |
| 94 | 95 |
| 95 void set_slow_create_download(bool slow) { slow_create_download_ = slow; } | 96 void set_slow_create_download(bool slow) { slow_create_download_ = slow; } |
| 96 | 97 |
| 97 void CreateDownload(const history::DownloadRow& info, | 98 void CreateDownload(const history::DownloadRow& info, |
| 98 const history::HistoryService::DownloadCreateCallback& | 99 const history::HistoryService::DownloadCreateCallback& |
| 99 callback) override { | 100 callback) override { |
| 100 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 101 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 101 create_download_info_ = info; | 102 create_download_info_ = info; |
| 102 // Must not call CreateDownload() again before FinishCreateDownload()! | 103 // Must not call CreateDownload() again before FinishCreateDownload()! |
| (...skipping 18 matching lines...) Expand all Loading... |
| 121 void RemoveDownloads(const IdSet& ids) override { | 122 void RemoveDownloads(const IdSet& ids) override { |
| 122 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 123 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 123 for (IdSet::const_iterator it = ids.begin(); | 124 for (IdSet::const_iterator it = ids.begin(); |
| 124 it != ids.end(); ++it) { | 125 it != ids.end(); ++it) { |
| 125 remove_downloads_.insert(*it); | 126 remove_downloads_.insert(*it); |
| 126 } | 127 } |
| 127 } | 128 } |
| 128 | 129 |
| 129 void ExpectWillQueryDownloads(scoped_ptr<InfoVector> infos) { | 130 void ExpectWillQueryDownloads(scoped_ptr<InfoVector> infos) { |
| 130 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 131 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 131 expect_query_downloads_ = infos.Pass(); | 132 expect_query_downloads_ = std::move(infos); |
| 132 } | 133 } |
| 133 | 134 |
| 134 void ExpectQueryDownloadsDone() { | 135 void ExpectQueryDownloadsDone() { |
| 135 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 136 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 136 EXPECT_TRUE(NULL == expect_query_downloads_.get()); | 137 EXPECT_TRUE(NULL == expect_query_downloads_.get()); |
| 137 } | 138 } |
| 138 | 139 |
| 139 void FailCreateDownload() { | 140 void FailCreateDownload() { |
| 140 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 141 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 141 fail_create_download_ = true; | 142 fail_create_download_ = true; |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 253 history::ToContentDownloadInterruptReason(row.interrupt_reason), | 254 history::ToContentDownloadInterruptReason(row.interrupt_reason), |
| 254 row.opened); | 255 row.opened); |
| 255 EXPECT_CALL(manager(), MockCreateDownloadItem(adapter)) | 256 EXPECT_CALL(manager(), MockCreateDownloadItem(adapter)) |
| 256 .WillOnce(DoAll( | 257 .WillOnce(DoAll( |
| 257 InvokeWithoutArgs( | 258 InvokeWithoutArgs( |
| 258 this, &DownloadHistoryTest::CallOnDownloadCreatedInOrder), | 259 this, &DownloadHistoryTest::CallOnDownloadCreatedInOrder), |
| 259 Return(&item(index)))); | 260 Return(&item(index)))); |
| 260 } | 261 } |
| 261 EXPECT_CALL(manager(), CheckForHistoryFilesRemoval()); | 262 EXPECT_CALL(manager(), CheckForHistoryFilesRemoval()); |
| 262 history_ = new FakeHistoryAdapter(); | 263 history_ = new FakeHistoryAdapter(); |
| 263 history_->ExpectWillQueryDownloads(infos.Pass()); | 264 history_->ExpectWillQueryDownloads(std::move(infos)); |
| 264 EXPECT_CALL(*manager_.get(), GetAllDownloads(_)).WillRepeatedly(Return()); | 265 EXPECT_CALL(*manager_.get(), GetAllDownloads(_)).WillRepeatedly(Return()); |
| 265 download_history_.reset(new DownloadHistory( | 266 download_history_.reset(new DownloadHistory( |
| 266 &manager(), scoped_ptr<DownloadHistory::HistoryAdapter>(history_))); | 267 &manager(), scoped_ptr<DownloadHistory::HistoryAdapter>(history_))); |
| 267 content::RunAllPendingInMessageLoop(content::BrowserThread::UI); | 268 content::RunAllPendingInMessageLoop(content::BrowserThread::UI); |
| 268 history_->ExpectQueryDownloadsDone(); | 269 history_->ExpectQueryDownloadsDone(); |
| 269 } | 270 } |
| 270 | 271 |
| 271 void CallOnDownloadCreated(size_t index) { | 272 void CallOnDownloadCreated(size_t index) { |
| 272 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 273 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 273 if (!pre_on_create_handler_.is_null()) | 274 if (!pre_on_create_handler_.is_null()) |
| (...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 484 // Load a download from history, create the item, OnDownloadCreated, | 485 // Load a download from history, create the item, OnDownloadCreated, |
| 485 // OnDownloadUpdated, OnDownloadRemoved. | 486 // OnDownloadUpdated, OnDownloadRemoved. |
| 486 history::DownloadRow info; | 487 history::DownloadRow info; |
| 487 InitBasicItem(FILE_PATH_LITERAL("/foo/bar.pdf"), | 488 InitBasicItem(FILE_PATH_LITERAL("/foo/bar.pdf"), |
| 488 "http://example.com/bar.pdf", | 489 "http://example.com/bar.pdf", |
| 489 "http://example.com/referrer.html", | 490 "http://example.com/referrer.html", |
| 490 &info); | 491 &info); |
| 491 { | 492 { |
| 492 scoped_ptr<InfoVector> infos(new InfoVector()); | 493 scoped_ptr<InfoVector> infos(new InfoVector()); |
| 493 infos->push_back(info); | 494 infos->push_back(info); |
| 494 CreateDownloadHistory(infos.Pass()); | 495 CreateDownloadHistory(std::move(infos)); |
| 495 ExpectNoDownloadCreated(); | 496 ExpectNoDownloadCreated(); |
| 496 } | 497 } |
| 497 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); | 498 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); |
| 498 | 499 |
| 499 // Pretend that something changed on the item. | 500 // Pretend that something changed on the item. |
| 500 EXPECT_CALL(item(0), GetOpened()).WillRepeatedly(Return(true)); | 501 EXPECT_CALL(item(0), GetOpened()).WillRepeatedly(Return(true)); |
| 501 item(0).NotifyObserversDownloadUpdated(); | 502 item(0).NotifyObserversDownloadUpdated(); |
| 502 info.opened = true; | 503 info.opened = true; |
| 503 ExpectDownloadUpdated(info); | 504 ExpectDownloadUpdated(info); |
| 504 | 505 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 522 // DownloadManager::CreateDownload() being called for the restored download. | 523 // DownloadManager::CreateDownload() being called for the restored download. |
| 523 // The above test expectation should verify that the value of | 524 // The above test expectation should verify that the value of |
| 524 // WasRestoredFromHistory is correct for this download. | 525 // WasRestoredFromHistory is correct for this download. |
| 525 history::DownloadRow info; | 526 history::DownloadRow info; |
| 526 InitBasicItem(FILE_PATH_LITERAL("/foo/bar.pdf"), | 527 InitBasicItem(FILE_PATH_LITERAL("/foo/bar.pdf"), |
| 527 "http://example.com/bar.pdf", | 528 "http://example.com/bar.pdf", |
| 528 "http://example.com/referrer.html", | 529 "http://example.com/referrer.html", |
| 529 &info); | 530 &info); |
| 530 scoped_ptr<InfoVector> infos(new InfoVector()); | 531 scoped_ptr<InfoVector> infos(new InfoVector()); |
| 531 infos->push_back(info); | 532 infos->push_back(info); |
| 532 CreateDownloadHistory(infos.Pass()); | 533 CreateDownloadHistory(std::move(infos)); |
| 533 | 534 |
| 534 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); | 535 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); |
| 535 } | 536 } |
| 536 | 537 |
| 537 // Test that WasRestoredFromHistory accurately identifies downloads that were | 538 // Test that WasRestoredFromHistory accurately identifies downloads that were |
| 538 // not created from history. | 539 // not created from history. |
| 539 TEST_F(DownloadHistoryTest, DownloadHistoryTest_WasRestoredFromHistory_False) { | 540 TEST_F(DownloadHistoryTest, DownloadHistoryTest_WasRestoredFromHistory_False) { |
| 540 // This sets DownloadHistoryTest to call DH::WasRestoredFromHistory() both | 541 // This sets DownloadHistoryTest to call DH::WasRestoredFromHistory() both |
| 541 // before and after DH::OnDownloadCreated() is called. At each call, the | 542 // before and after DH::OnDownloadCreated() is called. At each call, the |
| 542 // expected return value is |true| since the download was restored from | 543 // expected return value is |true| since the download was restored from |
| (...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 778 "http://example.com/referrer.html", | 779 "http://example.com/referrer.html", |
| 779 &info0); | 780 &info0); |
| 780 InitBasicItem(FILE_PATH_LITERAL("/foo/qux.pdf"), | 781 InitBasicItem(FILE_PATH_LITERAL("/foo/qux.pdf"), |
| 781 "http://example.com/qux.pdf", | 782 "http://example.com/qux.pdf", |
| 782 "http://example.com/referrer1.html", | 783 "http://example.com/referrer1.html", |
| 783 &info1); | 784 &info1); |
| 784 { | 785 { |
| 785 scoped_ptr<InfoVector> infos(new InfoVector()); | 786 scoped_ptr<InfoVector> infos(new InfoVector()); |
| 786 infos->push_back(info0); | 787 infos->push_back(info0); |
| 787 infos->push_back(info1); | 788 infos->push_back(info1); |
| 788 CreateDownloadHistory(infos.Pass()); | 789 CreateDownloadHistory(std::move(infos)); |
| 789 ExpectNoDownloadCreated(); | 790 ExpectNoDownloadCreated(); |
| 790 } | 791 } |
| 791 | 792 |
| 792 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); | 793 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); |
| 793 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(1))); | 794 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(1))); |
| 794 | 795 |
| 795 // Pretend that the user removed both items. | 796 // Pretend that the user removed both items. |
| 796 IdSet ids; | 797 IdSet ids; |
| 797 ids.insert(info0.id); | 798 ids.insert(info0.id); |
| 798 ids.insert(info1.id); | 799 ids.insert(info1.id); |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 853 FinishCreateDownload(); | 854 FinishCreateDownload(); |
| 854 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); | 855 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); |
| 855 | 856 |
| 856 // ItemAdded should call OnDownloadUpdated, which should detect that the item | 857 // ItemAdded should call OnDownloadUpdated, which should detect that the item |
| 857 // changed while it was being added and call UpdateDownload immediately. | 858 // changed while it was being added and call UpdateDownload immediately. |
| 858 info.opened = true; | 859 info.opened = true; |
| 859 ExpectDownloadUpdated(info); | 860 ExpectDownloadUpdated(info); |
| 860 } | 861 } |
| 861 | 862 |
| 862 } // anonymous namespace | 863 } // anonymous namespace |
| OLD | NEW |