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 download_created_index_(0) {} | 205 download_created_index_(0) {} |
202 virtual ~DownloadHistoryTest() { | 206 virtual ~DownloadHistoryTest() { |
203 STLDeleteElements(&items_); | 207 STLDeleteElements(&items_); |
204 } | 208 } |
205 | 209 |
206 protected: | 210 protected: |
207 virtual void TearDown() OVERRIDE { | 211 virtual void TearDown() OVERRIDE { |
208 download_history_.reset(); | 212 download_history_.reset(); |
209 } | 213 } |
210 | 214 |
211 content::MockDownloadManager& manager() { return *manager_.get(); } | 215 content::MockDownloadManager& manager() { return *manager_.get(); } |
212 content::MockDownloadItem& item(size_t index) { return *items_[index]; } | 216 content::MockDownloadItem& item(size_t index) { return *items_[index]; } |
| 217 DownloadHistory* download_history() { return download_history_.get(); } |
213 | 218 |
214 void SetManagerObserver( | 219 void SetManagerObserver( |
215 content::DownloadManager::Observer* manager_observer) { | 220 content::DownloadManager::Observer* manager_observer) { |
216 manager_observer_ = manager_observer; | 221 manager_observer_ = manager_observer; |
217 } | 222 } |
218 content::DownloadManager::Observer* manager_observer() { | 223 content::DownloadManager::Observer* manager_observer() { |
219 return manager_observer_; | 224 return manager_observer_; |
220 } | 225 } |
221 | 226 |
222 void ExpectWillQueryDownloads(scoped_ptr<InfoVector> infos) { | 227 void CreateDownloadHistory(scoped_ptr<InfoVector> infos) { |
223 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 228 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
224 CHECK(infos.get()); | 229 CHECK(infos.get()); |
225 EXPECT_CALL(manager(), AddObserver(_)).WillOnce(WithArg<0>(Invoke( | 230 EXPECT_CALL(manager(), AddObserver(_)).WillOnce(WithArg<0>(Invoke( |
226 this, &DownloadHistoryTest::SetManagerObserver))); | 231 this, &DownloadHistoryTest::SetManagerObserver))); |
227 EXPECT_CALL(manager(), RemoveObserver(_)); | 232 EXPECT_CALL(manager(), RemoveObserver(_)); |
228 download_created_index_ = 0; | 233 download_created_index_ = 0; |
229 for (size_t index = 0; index < infos->size(); ++index) { | 234 for (size_t index = 0; index < infos->size(); ++index) { |
230 content::MockDownloadManager::CreateDownloadItemAdapter adapter( | 235 content::MockDownloadManager::CreateDownloadItemAdapter adapter( |
231 infos->at(index).id, | 236 infos->at(index).id, |
232 infos->at(index).current_path, | 237 infos->at(index).current_path, |
(...skipping 21 matching lines...) Expand all Loading... |
254 history_->ExpectWillQueryDownloads(infos.Pass()); | 259 history_->ExpectWillQueryDownloads(infos.Pass()); |
255 EXPECT_CALL(*manager_.get(), GetAllDownloads(_)).WillRepeatedly(Return()); | 260 EXPECT_CALL(*manager_.get(), GetAllDownloads(_)).WillRepeatedly(Return()); |
256 download_history_.reset(new DownloadHistory( | 261 download_history_.reset(new DownloadHistory( |
257 &manager(), scoped_ptr<DownloadHistory::HistoryAdapter>(history_))); | 262 &manager(), scoped_ptr<DownloadHistory::HistoryAdapter>(history_))); |
258 content::RunAllPendingInMessageLoop(content::BrowserThread::UI); | 263 content::RunAllPendingInMessageLoop(content::BrowserThread::UI); |
259 history_->ExpectQueryDownloadsDone(); | 264 history_->ExpectQueryDownloadsDone(); |
260 } | 265 } |
261 | 266 |
262 void CallOnDownloadCreated(size_t index) { | 267 void CallOnDownloadCreated(size_t index) { |
263 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 268 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 269 if (!pre_on_create_handler_.is_null()) |
| 270 pre_on_create_handler_.Run(&item(index)); |
264 manager_observer()->OnDownloadCreated(&manager(), &item(index)); | 271 manager_observer()->OnDownloadCreated(&manager(), &item(index)); |
| 272 if (!post_on_create_handler_.is_null()) |
| 273 post_on_create_handler_.Run(&item(index)); |
265 } | 274 } |
266 | 275 |
267 void CallOnDownloadCreatedInOrder() { | 276 void CallOnDownloadCreatedInOrder() { |
268 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 277 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
269 // Gmock doesn't appear to support something like InvokeWithTheseArgs. Maybe | 278 // Gmock doesn't appear to support something like InvokeWithTheseArgs. Maybe |
270 // gmock needs to learn about base::Callback. | 279 // gmock needs to learn about base::Callback. |
271 CallOnDownloadCreated(download_created_index_++); | 280 CallOnDownloadCreated(download_created_index_++); |
272 } | 281 } |
273 | 282 |
274 void set_slow_create_download(bool slow) { | 283 void set_slow_create_download(bool slow) { |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
310 void ExpectNoDownloadsRemoved() { | 319 void ExpectNoDownloadsRemoved() { |
311 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 320 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
312 history_->ExpectNoDownloadsRemoved(); | 321 history_->ExpectNoDownloadsRemoved(); |
313 } | 322 } |
314 | 323 |
315 void ExpectDownloadsRemoved(const IdSet& ids) { | 324 void ExpectDownloadsRemoved(const IdSet& ids) { |
316 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 325 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
317 history_->ExpectDownloadsRemoved(ids); | 326 history_->ExpectDownloadsRemoved(ids); |
318 } | 327 } |
319 | 328 |
| 329 void ExpectDownloadsRestoredFromHistory(bool expected_value) { |
| 330 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 331 pre_on_create_handler_ = |
| 332 base::Bind(&DownloadHistoryTest::CheckDownloadWasRestoredFromHistory, |
| 333 base::Unretained(this), |
| 334 expected_value); |
| 335 post_on_create_handler_ = |
| 336 base::Bind(&DownloadHistoryTest::CheckDownloadWasRestoredFromHistory, |
| 337 base::Unretained(this), |
| 338 expected_value); |
| 339 } |
| 340 |
320 void InitBasicItem(const base::FilePath::CharType* path, | 341 void InitBasicItem(const base::FilePath::CharType* path, |
321 const char* url_string, | 342 const char* url_string, |
322 const char* referrer_string, | 343 const char* referrer_string, |
323 history::DownloadRow* info) { | 344 history::DownloadRow* info) { |
324 GURL url(url_string); | 345 GURL url(url_string); |
325 GURL referrer(referrer_string); | 346 GURL referrer(referrer_string); |
326 std::vector<GURL> url_chain; | 347 std::vector<GURL> url_chain; |
327 url_chain.push_back(url); | 348 url_chain.push_back(url); |
328 InitItem(static_cast<uint32>(items_.size() + 1), | 349 InitItem(static_cast<uint32>(items_.size() + 1), |
329 base::FilePath(path), | 350 base::FilePath(path), |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
430 | 451 |
431 std::vector<content::DownloadItem*> items; | 452 std::vector<content::DownloadItem*> items; |
432 for (size_t i = 0; i < items_.size(); ++i) { | 453 for (size_t i = 0; i < items_.size(); ++i) { |
433 items.push_back(&item(i)); | 454 items.push_back(&item(i)); |
434 } | 455 } |
435 EXPECT_CALL(*manager_.get(), GetAllDownloads(_)) | 456 EXPECT_CALL(*manager_.get(), GetAllDownloads(_)) |
436 .WillRepeatedly(SetArgPointee<0>(items)); | 457 .WillRepeatedly(SetArgPointee<0>(items)); |
437 } | 458 } |
438 | 459 |
439 private: | 460 private: |
| 461 void CheckDownloadWasRestoredFromHistory(bool expected_value, |
| 462 content::MockDownloadItem* item) { |
| 463 ASSERT_TRUE(download_history_.get()); |
| 464 EXPECT_EQ(expected_value, download_history_->WasRestoredFromHistory(item)); |
| 465 } |
| 466 |
440 base::MessageLoopForUI loop_; | 467 base::MessageLoopForUI loop_; |
441 content::TestBrowserThread ui_thread_; | 468 content::TestBrowserThread ui_thread_; |
442 std::vector<StrictMockDownloadItem*> items_; | 469 std::vector<StrictMockDownloadItem*> items_; |
443 scoped_ptr<content::MockDownloadManager> manager_; | 470 scoped_ptr<content::MockDownloadManager> manager_; |
444 FakeHistoryAdapter* history_; | 471 FakeHistoryAdapter* history_; |
445 scoped_ptr<DownloadHistory> download_history_; | 472 scoped_ptr<DownloadHistory> download_history_; |
446 content::DownloadManager::Observer* manager_observer_; | 473 content::DownloadManager::Observer* manager_observer_; |
447 size_t download_created_index_; | 474 size_t download_created_index_; |
| 475 DownloadItemCallback pre_on_create_handler_; |
| 476 DownloadItemCallback post_on_create_handler_; |
448 | 477 |
449 DISALLOW_COPY_AND_ASSIGN(DownloadHistoryTest); | 478 DISALLOW_COPY_AND_ASSIGN(DownloadHistoryTest); |
450 }; | 479 }; |
451 | 480 |
452 } // namespace | |
453 | |
454 // Test loading an item from the database, changing it, saving it back, removing | 481 // Test loading an item from the database, changing it, saving it back, removing |
455 // it. | 482 // it. |
456 TEST_F(DownloadHistoryTest, DownloadHistoryTest_Load) { | 483 TEST_F(DownloadHistoryTest, DownloadHistoryTest_Load) { |
457 // Load a download from history, create the item, OnDownloadCreated, | 484 // Load a download from history, create the item, OnDownloadCreated, |
458 // OnDownloadUpdated, OnDownloadRemoved. | 485 // OnDownloadUpdated, OnDownloadRemoved. |
459 history::DownloadRow info; | 486 history::DownloadRow info; |
460 InitBasicItem(FILE_PATH_LITERAL("/foo/bar.pdf"), | 487 InitBasicItem(FILE_PATH_LITERAL("/foo/bar.pdf"), |
461 "http://example.com/bar.pdf", | 488 "http://example.com/bar.pdf", |
462 "http://example.com/referrer.html", | 489 "http://example.com/referrer.html", |
463 &info); | 490 &info); |
464 { | 491 { |
465 scoped_ptr<InfoVector> infos(new InfoVector()); | 492 scoped_ptr<InfoVector> infos(new InfoVector()); |
466 infos->push_back(info); | 493 infos->push_back(info); |
467 ExpectWillQueryDownloads(infos.Pass()); | 494 CreateDownloadHistory(infos.Pass()); |
468 ExpectNoDownloadCreated(); | 495 ExpectNoDownloadCreated(); |
469 } | 496 } |
470 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); | 497 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); |
471 | 498 |
472 // Pretend that something changed on the item. | 499 // Pretend that something changed on the item. |
473 EXPECT_CALL(item(0), GetOpened()).WillRepeatedly(Return(true)); | 500 EXPECT_CALL(item(0), GetOpened()).WillRepeatedly(Return(true)); |
474 item(0).NotifyObserversDownloadUpdated(); | 501 item(0).NotifyObserversDownloadUpdated(); |
475 info.opened = true; | 502 info.opened = true; |
476 ExpectDownloadUpdated(info); | 503 ExpectDownloadUpdated(info); |
477 | 504 |
478 // Pretend that the user removed the item. | 505 // Pretend that the user removed the item. |
479 IdSet ids; | 506 IdSet ids; |
480 ids.insert(info.id); | 507 ids.insert(info.id); |
481 item(0).NotifyObserversDownloadRemoved(); | 508 item(0).NotifyObserversDownloadRemoved(); |
482 ExpectDownloadsRemoved(ids); | 509 ExpectDownloadsRemoved(ids); |
483 } | 510 } |
484 | 511 |
| 512 // Test that WasRestoredFromHistory accurately identifies downloads that were |
| 513 // created from history, even during an OnDownloadCreated() handler. |
| 514 TEST_F(DownloadHistoryTest, DownloadHistoryTest_WasRestoredFromHistory_True) { |
| 515 // This sets DownloadHistoryTest to call DH::WasRestoredFromHistory() both |
| 516 // before and after DH::OnDownloadCreated() is called. At each call, the |
| 517 // expected return value is |true| since the download was restored from |
| 518 // history. |
| 519 ExpectDownloadsRestoredFromHistory(true); |
| 520 |
| 521 // Construct a DownloadHistory with a single history download. This results in |
| 522 // DownloadManager::CreateDownload() being called for the restored download. |
| 523 // The above test expectation should verify that the value of |
| 524 // WasRestoredFromHistory is correct for this download. |
| 525 history::DownloadRow info; |
| 526 InitBasicItem(FILE_PATH_LITERAL("/foo/bar.pdf"), |
| 527 "http://example.com/bar.pdf", |
| 528 "http://example.com/referrer.html", |
| 529 &info); |
| 530 scoped_ptr<InfoVector> infos(new InfoVector()); |
| 531 infos->push_back(info); |
| 532 CreateDownloadHistory(infos.Pass()); |
| 533 |
| 534 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); |
| 535 } |
| 536 |
| 537 // Test that WasRestoredFromHistory accurately identifies downloads that were |
| 538 // not created from history. |
| 539 TEST_F(DownloadHistoryTest, DownloadHistoryTest_WasRestoredFromHistory_False) { |
| 540 // This sets DownloadHistoryTest to call DH::WasRestoredFromHistory() both |
| 541 // before and after DH::OnDownloadCreated() is called. At each call, the |
| 542 // expected return value is |true| since the download was restored from |
| 543 // history. |
| 544 ExpectDownloadsRestoredFromHistory(false); |
| 545 |
| 546 // Create a DownloadHistory with no history downloads. No |
| 547 // DownloadManager::CreateDownload() calls are expected. |
| 548 CreateDownloadHistory(scoped_ptr<InfoVector>(new InfoVector())); |
| 549 |
| 550 // Notify DownloadHistory that a new download was created. The above test |
| 551 // expecation should verify that WasRestoredFromHistory is correct for this |
| 552 // download. |
| 553 history::DownloadRow info; |
| 554 InitBasicItem(FILE_PATH_LITERAL("/foo/bar.pdf"), |
| 555 "http://example.com/bar.pdf", |
| 556 "http://example.com/referrer.html", |
| 557 &info); |
| 558 CallOnDownloadCreated(0); |
| 559 ExpectDownloadCreated(info); |
| 560 } |
| 561 |
485 // Test creating an item, saving it to the database, changing it, saving it | 562 // Test creating an item, saving it to the database, changing it, saving it |
486 // back, removing it. | 563 // back, removing it. |
487 TEST_F(DownloadHistoryTest, DownloadHistoryTest_Create) { | 564 TEST_F(DownloadHistoryTest, DownloadHistoryTest_Create) { |
488 // Create a fresh item not from history, OnDownloadCreated, OnDownloadUpdated, | 565 // Create a fresh item not from history, OnDownloadCreated, OnDownloadUpdated, |
489 // OnDownloadRemoved. | 566 // OnDownloadRemoved. |
490 ExpectWillQueryDownloads(scoped_ptr<InfoVector>(new InfoVector())); | 567 CreateDownloadHistory(scoped_ptr<InfoVector>(new InfoVector())); |
491 | 568 |
492 history::DownloadRow info; | 569 history::DownloadRow info; |
493 InitBasicItem(FILE_PATH_LITERAL("/foo/bar.pdf"), | 570 InitBasicItem(FILE_PATH_LITERAL("/foo/bar.pdf"), |
494 "http://example.com/bar.pdf", | 571 "http://example.com/bar.pdf", |
495 "http://example.com/referrer.html", | 572 "http://example.com/referrer.html", |
496 &info); | 573 &info); |
497 | 574 |
498 // Pretend the manager just created |item|. | 575 // Pretend the manager just created |item|. |
499 CallOnDownloadCreated(0); | 576 CallOnDownloadCreated(0); |
500 ExpectDownloadCreated(info); | 577 ExpectDownloadCreated(info); |
501 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); | 578 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); |
502 | 579 |
503 // Pretend that something changed on the item. | 580 // Pretend that something changed on the item. |
504 EXPECT_CALL(item(0), GetOpened()).WillRepeatedly(Return(true)); | 581 EXPECT_CALL(item(0), GetOpened()).WillRepeatedly(Return(true)); |
505 item(0).NotifyObserversDownloadUpdated(); | 582 item(0).NotifyObserversDownloadUpdated(); |
506 info.opened = true; | 583 info.opened = true; |
507 ExpectDownloadUpdated(info); | 584 ExpectDownloadUpdated(info); |
508 | 585 |
509 // Pretend that the user removed the item. | 586 // Pretend that the user removed the item. |
510 IdSet ids; | 587 IdSet ids; |
511 ids.insert(info.id); | 588 ids.insert(info.id); |
512 item(0).NotifyObserversDownloadRemoved(); | 589 item(0).NotifyObserversDownloadRemoved(); |
513 ExpectDownloadsRemoved(ids); | 590 ExpectDownloadsRemoved(ids); |
514 } | 591 } |
515 | 592 |
516 // Test that changes to persisted fields in a DownloadItem triggers database | 593 // Test that changes to persisted fields in a DownloadItem triggers database |
517 // updates. | 594 // updates. |
518 TEST_F(DownloadHistoryTest, DownloadHistoryTest_Update) { | 595 TEST_F(DownloadHistoryTest, DownloadHistoryTest_Update) { |
519 ExpectWillQueryDownloads(scoped_ptr<InfoVector>(new InfoVector())); | 596 CreateDownloadHistory(scoped_ptr<InfoVector>(new InfoVector())); |
520 | 597 |
521 history::DownloadRow info; | 598 history::DownloadRow info; |
522 InitBasicItem(FILE_PATH_LITERAL("/foo/bar.pdf"), | 599 InitBasicItem(FILE_PATH_LITERAL("/foo/bar.pdf"), |
523 "http://example.com/bar.pdf", | 600 "http://example.com/bar.pdf", |
524 "http://example.com/referrer.html", | 601 "http://example.com/referrer.html", |
525 &info); | 602 &info); |
526 CallOnDownloadCreated(0); | 603 CallOnDownloadCreated(0); |
527 ExpectDownloadCreated(info); | 604 ExpectDownloadCreated(info); |
528 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); | 605 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); |
529 | 606 |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
604 ExpectDownloadUpdated(info); | 681 ExpectDownloadUpdated(info); |
605 } | 682 } |
606 | 683 |
607 // Test creating a new item, saving it, removing it by setting it Temporary, | 684 // Test creating a new item, saving it, removing it by setting it Temporary, |
608 // changing it without saving it back because it's Temporary, clearing | 685 // changing it without saving it back because it's Temporary, clearing |
609 // IsTemporary, saving it back, changing it, saving it back because it isn't | 686 // IsTemporary, saving it back, changing it, saving it back because it isn't |
610 // Temporary anymore. | 687 // Temporary anymore. |
611 TEST_F(DownloadHistoryTest, DownloadHistoryTest_Temporary) { | 688 TEST_F(DownloadHistoryTest, DownloadHistoryTest_Temporary) { |
612 // Create a fresh item not from history, OnDownloadCreated, OnDownloadUpdated, | 689 // Create a fresh item not from history, OnDownloadCreated, OnDownloadUpdated, |
613 // OnDownloadRemoved. | 690 // OnDownloadRemoved. |
614 ExpectWillQueryDownloads(scoped_ptr<InfoVector>(new InfoVector())); | 691 CreateDownloadHistory(scoped_ptr<InfoVector>(new InfoVector())); |
615 | 692 |
616 history::DownloadRow info; | 693 history::DownloadRow info; |
617 InitBasicItem(FILE_PATH_LITERAL("/foo/bar.pdf"), | 694 InitBasicItem(FILE_PATH_LITERAL("/foo/bar.pdf"), |
618 "http://example.com/bar.pdf", | 695 "http://example.com/bar.pdf", |
619 "http://example.com/referrer.html", | 696 "http://example.com/referrer.html", |
620 &info); | 697 &info); |
621 | 698 |
622 // Pretend the manager just created |item|. | 699 // Pretend the manager just created |item|. |
623 CallOnDownloadCreated(0); | 700 CallOnDownloadCreated(0); |
624 ExpectDownloadCreated(info); | 701 ExpectDownloadCreated(info); |
(...skipping 22 matching lines...) Expand all Loading... |
647 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); | 724 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); |
648 | 725 |
649 EXPECT_CALL(item(0), GetReceivedBytes()).WillRepeatedly(Return(100)); | 726 EXPECT_CALL(item(0), GetReceivedBytes()).WillRepeatedly(Return(100)); |
650 item(0).NotifyObserversDownloadUpdated(); | 727 item(0).NotifyObserversDownloadUpdated(); |
651 info.received_bytes = 100; | 728 info.received_bytes = 100; |
652 ExpectDownloadUpdated(info); | 729 ExpectDownloadUpdated(info); |
653 } | 730 } |
654 | 731 |
655 // Test removing downloads while they're still being added. | 732 // Test removing downloads while they're still being added. |
656 TEST_F(DownloadHistoryTest, DownloadHistoryTest_RemoveWhileAdding) { | 733 TEST_F(DownloadHistoryTest, DownloadHistoryTest_RemoveWhileAdding) { |
657 ExpectWillQueryDownloads(scoped_ptr<InfoVector>(new InfoVector())); | 734 CreateDownloadHistory(scoped_ptr<InfoVector>(new InfoVector())); |
658 | 735 |
659 history::DownloadRow info; | 736 history::DownloadRow info; |
660 InitBasicItem(FILE_PATH_LITERAL("/foo/bar.pdf"), | 737 InitBasicItem(FILE_PATH_LITERAL("/foo/bar.pdf"), |
661 "http://example.com/bar.pdf", | 738 "http://example.com/bar.pdf", |
662 "http://example.com/referrer.html", | 739 "http://example.com/referrer.html", |
663 &info); | 740 &info); |
664 | 741 |
665 // Instruct CreateDownload() to not callback to DownloadHistory immediately, | 742 // Instruct CreateDownload() to not callback to DownloadHistory immediately, |
666 // but to wait for FinishCreateDownload(). | 743 // but to wait for FinishCreateDownload(). |
667 set_slow_create_download(true); | 744 set_slow_create_download(true); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
700 "http://example.com/referrer.html", | 777 "http://example.com/referrer.html", |
701 &info0); | 778 &info0); |
702 InitBasicItem(FILE_PATH_LITERAL("/foo/qux.pdf"), | 779 InitBasicItem(FILE_PATH_LITERAL("/foo/qux.pdf"), |
703 "http://example.com/qux.pdf", | 780 "http://example.com/qux.pdf", |
704 "http://example.com/referrer1.html", | 781 "http://example.com/referrer1.html", |
705 &info1); | 782 &info1); |
706 { | 783 { |
707 scoped_ptr<InfoVector> infos(new InfoVector()); | 784 scoped_ptr<InfoVector> infos(new InfoVector()); |
708 infos->push_back(info0); | 785 infos->push_back(info0); |
709 infos->push_back(info1); | 786 infos->push_back(info1); |
710 ExpectWillQueryDownloads(infos.Pass()); | 787 CreateDownloadHistory(infos.Pass()); |
711 ExpectNoDownloadCreated(); | 788 ExpectNoDownloadCreated(); |
712 } | 789 } |
713 | 790 |
714 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); | 791 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); |
715 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(1))); | 792 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(1))); |
716 | 793 |
717 // Pretend that the user removed both items. | 794 // Pretend that the user removed both items. |
718 IdSet ids; | 795 IdSet ids; |
719 ids.insert(info0.id); | 796 ids.insert(info0.id); |
720 ids.insert(info1.id); | 797 ids.insert(info1.id); |
721 item(0).NotifyObserversDownloadRemoved(); | 798 item(0).NotifyObserversDownloadRemoved(); |
722 item(1).NotifyObserversDownloadRemoved(); | 799 item(1).NotifyObserversDownloadRemoved(); |
723 ExpectDownloadsRemoved(ids); | 800 ExpectDownloadsRemoved(ids); |
724 } | 801 } |
725 | 802 |
726 // Test what happens when HistoryService/CreateDownload::CreateDownload() fails. | 803 // Test what happens when HistoryService/CreateDownload::CreateDownload() fails. |
727 TEST_F(DownloadHistoryTest, DownloadHistoryTest_CreateFailed) { | 804 TEST_F(DownloadHistoryTest, DownloadHistoryTest_CreateFailed) { |
728 // Create a fresh item not from history, OnDownloadCreated, OnDownloadUpdated, | 805 // Create a fresh item not from history, OnDownloadCreated, OnDownloadUpdated, |
729 // OnDownloadRemoved. | 806 // OnDownloadRemoved. |
730 ExpectWillQueryDownloads(scoped_ptr<InfoVector>(new InfoVector())); | 807 CreateDownloadHistory(scoped_ptr<InfoVector>(new InfoVector())); |
731 | 808 |
732 history::DownloadRow info; | 809 history::DownloadRow info; |
733 InitBasicItem(FILE_PATH_LITERAL("/foo/bar.pdf"), | 810 InitBasicItem(FILE_PATH_LITERAL("/foo/bar.pdf"), |
734 "http://example.com/bar.pdf", | 811 "http://example.com/bar.pdf", |
735 "http://example.com/referrer.html", | 812 "http://example.com/referrer.html", |
736 &info); | 813 &info); |
737 | 814 |
738 FailCreateDownload(); | 815 FailCreateDownload(); |
739 // Pretend the manager just created |item|. | 816 // Pretend the manager just created |item|. |
740 CallOnDownloadCreated(0); | 817 CallOnDownloadCreated(0); |
741 ExpectDownloadCreated(info); | 818 ExpectDownloadCreated(info); |
742 EXPECT_FALSE(DownloadHistory::IsPersisted(&item(0))); | 819 EXPECT_FALSE(DownloadHistory::IsPersisted(&item(0))); |
743 | 820 |
744 EXPECT_CALL(item(0), GetReceivedBytes()).WillRepeatedly(Return(100)); | 821 EXPECT_CALL(item(0), GetReceivedBytes()).WillRepeatedly(Return(100)); |
745 item(0).NotifyObserversDownloadUpdated(); | 822 item(0).NotifyObserversDownloadUpdated(); |
746 info.received_bytes = 100; | 823 info.received_bytes = 100; |
747 ExpectDownloadCreated(info); | 824 ExpectDownloadCreated(info); |
748 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); | 825 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); |
749 } | 826 } |
750 | 827 |
751 TEST_F(DownloadHistoryTest, DownloadHistoryTest_UpdateWhileAdding) { | 828 TEST_F(DownloadHistoryTest, DownloadHistoryTest_UpdateWhileAdding) { |
752 // Create a fresh item not from history, OnDownloadCreated, OnDownloadUpdated, | 829 // Create a fresh item not from history, OnDownloadCreated, OnDownloadUpdated, |
753 // OnDownloadRemoved. | 830 // OnDownloadRemoved. |
754 ExpectWillQueryDownloads(scoped_ptr<InfoVector>(new InfoVector())); | 831 CreateDownloadHistory(scoped_ptr<InfoVector>(new InfoVector())); |
755 | 832 |
756 history::DownloadRow info; | 833 history::DownloadRow info; |
757 InitBasicItem(FILE_PATH_LITERAL("/foo/bar.pdf"), | 834 InitBasicItem(FILE_PATH_LITERAL("/foo/bar.pdf"), |
758 "http://example.com/bar.pdf", | 835 "http://example.com/bar.pdf", |
759 "http://example.com/referrer.html", | 836 "http://example.com/referrer.html", |
760 &info); | 837 &info); |
761 | 838 |
762 // Instruct CreateDownload() to not callback to DownloadHistory immediately, | 839 // Instruct CreateDownload() to not callback to DownloadHistory immediately, |
763 // but to wait for FinishCreateDownload(). | 840 // but to wait for FinishCreateDownload(). |
764 set_slow_create_download(true); | 841 set_slow_create_download(true); |
765 | 842 |
766 // Pretend the manager just created |item|. | 843 // Pretend the manager just created |item|. |
767 CallOnDownloadCreated(0); | 844 CallOnDownloadCreated(0); |
768 ExpectDownloadCreated(info); | 845 ExpectDownloadCreated(info); |
769 EXPECT_FALSE(DownloadHistory::IsPersisted(&item(0))); | 846 EXPECT_FALSE(DownloadHistory::IsPersisted(&item(0))); |
770 | 847 |
771 // Pretend that something changed on the item. | 848 // Pretend that something changed on the item. |
772 EXPECT_CALL(item(0), GetOpened()).WillRepeatedly(Return(true)); | 849 EXPECT_CALL(item(0), GetOpened()).WillRepeatedly(Return(true)); |
773 item(0).NotifyObserversDownloadUpdated(); | 850 item(0).NotifyObserversDownloadUpdated(); |
774 | 851 |
775 FinishCreateDownload(); | 852 FinishCreateDownload(); |
776 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); | 853 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); |
777 | 854 |
778 // ItemAdded should call OnDownloadUpdated, which should detect that the item | 855 // ItemAdded should call OnDownloadUpdated, which should detect that the item |
779 // changed while it was being added and call UpdateDownload immediately. | 856 // changed while it was being added and call UpdateDownload immediately. |
780 info.opened = true; | 857 info.opened = true; |
781 ExpectDownloadUpdated(info); | 858 ExpectDownloadUpdated(info); |
782 } | 859 } |
| 860 |
| 861 } // anonymous namespace |
OLD | NEW |