Chromium Code Reviews| 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 <set> | 5 #include <set> |
| 6 #include <vector> | 6 #include <vector> |
| 7 | 7 |
| 8 #include "base/memory/weak_ptr.h" | |
| 8 #include "base/rand_util.h" | 9 #include "base/rand_util.h" |
| 9 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
| 10 #include "chrome/browser/download/download_history.h" | 11 #include "chrome/browser/download/download_history.h" |
| 11 #include "chrome/browser/history/download_database.h" | 12 #include "chrome/browser/history/download_database.h" |
| 12 #include "chrome/browser/history/download_row.h" | 13 #include "chrome/browser/history/download_row.h" |
| 13 #include "chrome/browser/history/history_service.h" | 14 #include "chrome/browser/history/history_service.h" |
| 14 #include "content/public/test/mock_download_item.h" | 15 #include "content/public/test/mock_download_item.h" |
| 15 #include "content/public/test/mock_download_manager.h" | 16 #include "content/public/test/mock_download_manager.h" |
| 16 #include "content/public/test/test_browser_thread.h" | 17 #include "content/public/test/test_browser_thread.h" |
| 17 #include "content/public/test/test_utils.h" | 18 #include "content/public/test/test_utils.h" |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 186 history::DownloadRow update_download_; | 187 history::DownloadRow update_download_; |
| 187 scoped_ptr<InfoVector> expect_query_downloads_; | 188 scoped_ptr<InfoVector> expect_query_downloads_; |
| 188 IdSet remove_downloads_; | 189 IdSet remove_downloads_; |
| 189 history::DownloadRow create_download_info_; | 190 history::DownloadRow create_download_info_; |
| 190 | 191 |
| 191 DISALLOW_COPY_AND_ASSIGN(FakeHistoryAdapter); | 192 DISALLOW_COPY_AND_ASSIGN(FakeHistoryAdapter); |
| 192 }; | 193 }; |
| 193 | 194 |
| 194 class DownloadHistoryTest : public testing::Test { | 195 class DownloadHistoryTest : public testing::Test { |
| 195 public: | 196 public: |
| 197 // Generic callback that receives a pointer to a StrictMockDownloadItem. | |
| 198 typedef base::Callback<void(content::MockDownloadItem*)> DownloadItemCallback; | |
| 199 | |
| 196 DownloadHistoryTest() | 200 DownloadHistoryTest() |
| 197 : ui_thread_(content::BrowserThread::UI, &loop_), | 201 : ui_thread_(content::BrowserThread::UI, &loop_), |
| 198 manager_(new content::MockDownloadManager()), | 202 manager_(new content::MockDownloadManager()), |
| 199 history_(NULL), | 203 history_(NULL), |
| 200 manager_observer_(NULL), | 204 manager_observer_(NULL), |
| 201 item_observer_(NULL), | 205 item_observer_(NULL), |
| 202 download_created_index_(0) {} | 206 download_created_index_(0) {} |
| 203 virtual ~DownloadHistoryTest() { | 207 virtual ~DownloadHistoryTest() { |
| 204 STLDeleteElements(&items_); | 208 STLDeleteElements(&items_); |
| 205 } | 209 } |
| 206 | 210 |
| 207 protected: | 211 protected: |
| 208 virtual void TearDown() OVERRIDE { | 212 virtual void TearDown() OVERRIDE { |
| 209 download_history_.reset(); | 213 download_history_.reset(); |
| 210 } | 214 } |
| 211 | 215 |
| 212 content::MockDownloadManager& manager() { return *manager_.get(); } | 216 content::MockDownloadManager& manager() { return *manager_.get(); } |
| 213 content::MockDownloadItem& item(size_t index) { return *items_[index]; } | 217 content::MockDownloadItem& item(size_t index) { return *items_[index]; } |
| 218 DownloadHistory* download_history() { return download_history_.get(); } | |
| 214 | 219 |
| 215 void SetManagerObserver( | 220 void SetManagerObserver( |
| 216 content::DownloadManager::Observer* manager_observer) { | 221 content::DownloadManager::Observer* manager_observer) { |
| 217 manager_observer_ = manager_observer; | 222 manager_observer_ = manager_observer; |
| 218 } | 223 } |
| 219 content::DownloadManager::Observer* manager_observer() { | 224 content::DownloadManager::Observer* manager_observer() { |
| 220 return manager_observer_; | 225 return manager_observer_; |
| 221 } | 226 } |
| 222 | 227 |
| 223 // Relies on the same object observing all download items. | 228 // Relies on the same object observing all download items. |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 264 history_->ExpectWillQueryDownloads(infos.Pass()); | 269 history_->ExpectWillQueryDownloads(infos.Pass()); |
| 265 EXPECT_CALL(*manager_.get(), GetAllDownloads(_)).WillRepeatedly(Return()); | 270 EXPECT_CALL(*manager_.get(), GetAllDownloads(_)).WillRepeatedly(Return()); |
| 266 download_history_.reset(new DownloadHistory( | 271 download_history_.reset(new DownloadHistory( |
| 267 &manager(), scoped_ptr<DownloadHistory::HistoryAdapter>(history_))); | 272 &manager(), scoped_ptr<DownloadHistory::HistoryAdapter>(history_))); |
| 268 content::RunAllPendingInMessageLoop(content::BrowserThread::UI); | 273 content::RunAllPendingInMessageLoop(content::BrowserThread::UI); |
| 269 history_->ExpectQueryDownloadsDone(); | 274 history_->ExpectQueryDownloadsDone(); |
| 270 } | 275 } |
| 271 | 276 |
| 272 void CallOnDownloadCreated(size_t index) { | 277 void CallOnDownloadCreated(size_t index) { |
| 273 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 278 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 279 if (!pre_on_create_handler_.is_null()) | |
| 280 pre_on_create_handler_.Run(&item(index)); | |
| 274 manager_observer()->OnDownloadCreated(&manager(), &item(index)); | 281 manager_observer()->OnDownloadCreated(&manager(), &item(index)); |
| 282 if (!post_on_create_handler_.is_null()) | |
| 283 post_on_create_handler_.Run(&item(index)); | |
| 275 } | 284 } |
| 276 | 285 |
| 277 void CallOnDownloadCreatedInOrder() { | 286 void CallOnDownloadCreatedInOrder() { |
| 278 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 287 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 279 // Gmock doesn't appear to support something like InvokeWithTheseArgs. Maybe | 288 // Gmock doesn't appear to support something like InvokeWithTheseArgs. Maybe |
| 280 // gmock needs to learn about base::Callback. | 289 // gmock needs to learn about base::Callback. |
| 281 CallOnDownloadCreated(download_created_index_++); | 290 CallOnDownloadCreated(download_created_index_++); |
| 282 } | 291 } |
| 283 | 292 |
| 284 void set_slow_create_download(bool slow) { | 293 void set_slow_create_download(bool slow) { |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 320 void ExpectNoDownloadsRemoved() { | 329 void ExpectNoDownloadsRemoved() { |
| 321 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 330 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 322 history_->ExpectNoDownloadsRemoved(); | 331 history_->ExpectNoDownloadsRemoved(); |
| 323 } | 332 } |
| 324 | 333 |
| 325 void ExpectDownloadsRemoved(const IdSet& ids) { | 334 void ExpectDownloadsRemoved(const IdSet& ids) { |
| 326 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 335 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 327 history_->ExpectDownloadsRemoved(ids); | 336 history_->ExpectDownloadsRemoved(ids); |
| 328 } | 337 } |
| 329 | 338 |
| 339 void ExpectDownloadsRestoredFromHistory(bool expected_value) { | |
| 340 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 341 pre_on_create_handler_ = | |
| 342 base::Bind(&DownloadHistoryTest::CheckDownloadWasRestoredFromHistory, | |
| 343 base::Unretained(this), | |
| 344 expected_value); | |
| 345 post_on_create_handler_ = | |
| 346 base::Bind(&DownloadHistoryTest::CheckDownloadWasRestoredFromHistory, | |
| 347 base::Unretained(this), | |
| 348 expected_value); | |
| 349 } | |
| 350 | |
| 330 void InitBasicItem(const base::FilePath::CharType* path, | 351 void InitBasicItem(const base::FilePath::CharType* path, |
| 331 const char* url_string, | 352 const char* url_string, |
| 332 const char* referrer_string, | 353 const char* referrer_string, |
| 333 history::DownloadRow* info) { | 354 history::DownloadRow* info) { |
| 334 GURL url(url_string); | 355 GURL url(url_string); |
| 335 GURL referrer(referrer_string); | 356 GURL referrer(referrer_string); |
| 336 std::vector<GURL> url_chain; | 357 std::vector<GURL> url_chain; |
| 337 url_chain.push_back(url); | 358 url_chain.push_back(url); |
| 338 InitItem(static_cast<uint32>(items_.size() + 1), | 359 InitItem(static_cast<uint32>(items_.size() + 1), |
| 339 base::FilePath(path), | 360 base::FilePath(path), |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 443 | 464 |
| 444 std::vector<content::DownloadItem*> items; | 465 std::vector<content::DownloadItem*> items; |
| 445 for (size_t i = 0; i < items_.size(); ++i) { | 466 for (size_t i = 0; i < items_.size(); ++i) { |
| 446 items.push_back(&item(i)); | 467 items.push_back(&item(i)); |
| 447 } | 468 } |
| 448 EXPECT_CALL(*manager_.get(), GetAllDownloads(_)) | 469 EXPECT_CALL(*manager_.get(), GetAllDownloads(_)) |
| 449 .WillRepeatedly(SetArgPointee<0>(items)); | 470 .WillRepeatedly(SetArgPointee<0>(items)); |
| 450 } | 471 } |
| 451 | 472 |
| 452 private: | 473 private: |
| 474 void CheckDownloadWasRestoredFromHistory(bool expected_value, | |
| 475 content::MockDownloadItem* item) { | |
| 476 ASSERT_TRUE(download_history_.get()); | |
| 477 EXPECT_EQ(expected_value, download_history_->WasRestoredFromHistory(item)); | |
| 478 } | |
| 479 | |
| 453 base::MessageLoopForUI loop_; | 480 base::MessageLoopForUI loop_; |
| 454 content::TestBrowserThread ui_thread_; | 481 content::TestBrowserThread ui_thread_; |
| 455 std::vector<StrictMockDownloadItem*> items_; | 482 std::vector<StrictMockDownloadItem*> items_; |
| 456 scoped_ptr<content::MockDownloadManager> manager_; | 483 scoped_ptr<content::MockDownloadManager> manager_; |
| 457 FakeHistoryAdapter* history_; | 484 FakeHistoryAdapter* history_; |
| 458 scoped_ptr<DownloadHistory> download_history_; | 485 scoped_ptr<DownloadHistory> download_history_; |
| 459 content::DownloadManager::Observer* manager_observer_; | 486 content::DownloadManager::Observer* manager_observer_; |
| 460 content::DownloadItem::Observer* item_observer_; | 487 content::DownloadItem::Observer* item_observer_; |
| 461 size_t download_created_index_; | 488 size_t download_created_index_; |
| 489 DownloadItemCallback pre_on_create_handler_; | |
| 490 DownloadItemCallback post_on_create_handler_; | |
| 462 | 491 |
| 463 DISALLOW_COPY_AND_ASSIGN(DownloadHistoryTest); | 492 DISALLOW_COPY_AND_ASSIGN(DownloadHistoryTest); |
| 464 }; | 493 }; |
| 465 | 494 |
| 466 } // anonymous namespace | |
| 467 | |
| 468 // Test loading an item from the database, changing it, saving it back, removing | 495 // Test loading an item from the database, changing it, saving it back, removing |
| 469 // it. | 496 // it. |
| 470 TEST_F(DownloadHistoryTest, DownloadHistoryTest_Load) { | 497 TEST_F(DownloadHistoryTest, DownloadHistoryTest_Load) { |
| 471 // Load a download from history, create the item, OnDownloadCreated, | 498 // Load a download from history, create the item, OnDownloadCreated, |
| 472 // OnDownloadUpdated, OnDownloadRemoved. | 499 // OnDownloadUpdated, OnDownloadRemoved. |
| 473 history::DownloadRow info; | 500 history::DownloadRow info; |
| 474 InitBasicItem(FILE_PATH_LITERAL("/foo/bar.pdf"), | 501 InitBasicItem(FILE_PATH_LITERAL("/foo/bar.pdf"), |
| 475 "http://example.com/bar.pdf", | 502 "http://example.com/bar.pdf", |
| 476 "http://example.com/referrer.html", | 503 "http://example.com/referrer.html", |
| 477 &info); | 504 &info); |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 489 info.opened = true; | 516 info.opened = true; |
| 490 ExpectDownloadUpdated(info); | 517 ExpectDownloadUpdated(info); |
| 491 | 518 |
| 492 // Pretend that the user removed the item. | 519 // Pretend that the user removed the item. |
| 493 IdSet ids; | 520 IdSet ids; |
| 494 ids.insert(info.id); | 521 ids.insert(info.id); |
| 495 item_observer()->OnDownloadRemoved(&item(0)); | 522 item_observer()->OnDownloadRemoved(&item(0)); |
| 496 ExpectDownloadsRemoved(ids); | 523 ExpectDownloadsRemoved(ids); |
| 497 } | 524 } |
| 498 | 525 |
| 526 // Test that WasRestoredFromHistory accurately identifies downloads that were | |
| 527 // created from history, even during an OnDownloadCreated() handler. | |
| 528 TEST_F(DownloadHistoryTest, DownloadHistoryTest_WasRestoredFromHistory_True) { | |
| 529 ExpectDownloadsRestoredFromHistory(true); | |
| 530 history::DownloadRow info; | |
| 531 InitBasicItem(FILE_PATH_LITERAL("/foo/bar.pdf"), | |
| 532 "http://example.com/bar.pdf", | |
| 533 "http://example.com/referrer.html", | |
| 534 &info); | |
| 535 { | |
| 536 scoped_ptr<InfoVector> infos(new InfoVector()); | |
| 537 infos->push_back(info); | |
| 538 ExpectWillQueryDownloads(infos.Pass()); | |
|
Randy Smith (Not in Mondays)
2014/04/21 18:21:22
This may be due to my own cluelessness (I kept mis
asanka
2014/04/23 05:51:09
I changed the name.
| |
| 539 } | |
| 540 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); | |
| 541 } | |
| 542 | |
| 543 // Test that WasRestoredFromHistory accurately identifies downloads that were | |
| 544 // not created from history. | |
| 545 TEST_F(DownloadHistoryTest, DownloadHistoryTest_WasRestoredFromHistory_False) { | |
| 546 ExpectDownloadsRestoredFromHistory(false); | |
| 547 ExpectWillQueryDownloads(scoped_ptr<InfoVector>(new InfoVector())); | |
| 548 | |
| 549 history::DownloadRow info; | |
| 550 InitBasicItem(FILE_PATH_LITERAL("/foo/bar.pdf"), | |
| 551 "http://example.com/bar.pdf", | |
| 552 "http://example.com/referrer.html", | |
| 553 &info); | |
| 554 | |
| 555 CallOnDownloadCreated(0); | |
|
Randy Smith (Not in Mondays)
2014/04/21 18:21:22
It was harder to understand these tests because of
asanka
2014/04/23 05:51:09
Hmm. Yeah. I added some comments that should expla
| |
| 556 ExpectDownloadCreated(info); | |
| 557 } | |
| 558 | |
| 499 // Test creating an item, saving it to the database, changing it, saving it | 559 // Test creating an item, saving it to the database, changing it, saving it |
| 500 // back, removing it. | 560 // back, removing it. |
| 501 TEST_F(DownloadHistoryTest, DownloadHistoryTest_Create) { | 561 TEST_F(DownloadHistoryTest, DownloadHistoryTest_Create) { |
| 502 // Create a fresh item not from history, OnDownloadCreated, OnDownloadUpdated, | 562 // Create a fresh item not from history, OnDownloadCreated, OnDownloadUpdated, |
| 503 // OnDownloadRemoved. | 563 // OnDownloadRemoved. |
| 504 ExpectWillQueryDownloads(scoped_ptr<InfoVector>(new InfoVector())); | 564 ExpectWillQueryDownloads(scoped_ptr<InfoVector>(new InfoVector())); |
| 505 | 565 |
| 506 history::DownloadRow info; | 566 history::DownloadRow info; |
| 507 InitBasicItem(FILE_PATH_LITERAL("/foo/bar.pdf"), | 567 InitBasicItem(FILE_PATH_LITERAL("/foo/bar.pdf"), |
| 508 "http://example.com/bar.pdf", | 568 "http://example.com/bar.pdf", |
| (...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 787 item_observer()->OnDownloadUpdated(&item(0)); | 847 item_observer()->OnDownloadUpdated(&item(0)); |
| 788 | 848 |
| 789 FinishCreateDownload(); | 849 FinishCreateDownload(); |
| 790 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); | 850 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); |
| 791 | 851 |
| 792 // ItemAdded should call OnDownloadUpdated, which should detect that the item | 852 // ItemAdded should call OnDownloadUpdated, which should detect that the item |
| 793 // changed while it was being added and call UpdateDownload immediately. | 853 // changed while it was being added and call UpdateDownload immediately. |
| 794 info.opened = true; | 854 info.opened = true; |
| 795 ExpectDownloadUpdated(info); | 855 ExpectDownloadUpdated(info); |
| 796 } | 856 } |
| 857 | |
| 858 } // anonymous namespace | |
| OLD | NEW |