| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 27 matching lines...) Expand all Loading... |
| 38 #include "base/utf_string_conversions.h" | 38 #include "base/utf_string_conversions.h" |
| 39 #include "chrome/browser/download/download_item.h" | 39 #include "chrome/browser/download/download_item.h" |
| 40 #include "chrome/browser/history/download_create_info.h" | 40 #include "chrome/browser/history/download_create_info.h" |
| 41 #include "chrome/browser/history/history.h" | 41 #include "chrome/browser/history/history.h" |
| 42 #include "chrome/browser/history/history_backend.h" | 42 #include "chrome/browser/history/history_backend.h" |
| 43 #include "chrome/browser/history/history_database.h" | 43 #include "chrome/browser/history/history_database.h" |
| 44 #include "chrome/browser/history/history_notifications.h" | 44 #include "chrome/browser/history/history_notifications.h" |
| 45 #include "chrome/browser/history/in_memory_database.h" | 45 #include "chrome/browser/history/in_memory_database.h" |
| 46 #include "chrome/browser/history/in_memory_history_backend.h" | 46 #include "chrome/browser/history/in_memory_history_backend.h" |
| 47 #include "chrome/browser/history/page_usage_data.h" | 47 #include "chrome/browser/history/page_usage_data.h" |
| 48 #include "chrome/browser/history/top_sites.h" | |
| 49 #include "chrome/common/chrome_paths.h" | 48 #include "chrome/common/chrome_paths.h" |
| 50 #include "chrome/common/notification_details.h" | 49 #include "chrome/common/notification_details.h" |
| 51 #include "chrome/common/notification_source.h" | 50 #include "chrome/common/notification_source.h" |
| 52 #include "chrome/common/thumbnail_score.h" | 51 #include "chrome/common/thumbnail_score.h" |
| 53 #include "chrome/tools/profiles/thumbnail-inl.h" | 52 #include "chrome/tools/profiles/thumbnail-inl.h" |
| 54 #include "gfx/codec/jpeg_codec.h" | 53 #include "gfx/codec/jpeg_codec.h" |
| 55 #include "testing/gtest/include/gtest/gtest.h" | 54 #include "testing/gtest/include/gtest/gtest.h" |
| 56 #include "third_party/skia/include/core/SkBitmap.h" | 55 #include "third_party/skia/include/core/SkBitmap.h" |
| 57 | 56 |
| 58 using base::Time; | 57 using base::Time; |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 HistoryTest() | 105 HistoryTest() |
| 107 : history_service_(NULL), | 106 : history_service_(NULL), |
| 108 got_thumbnail_callback_(false), | 107 got_thumbnail_callback_(false), |
| 109 redirect_query_success_(false), | 108 redirect_query_success_(false), |
| 110 query_url_success_(false), | 109 query_url_success_(false), |
| 111 db_(NULL) { | 110 db_(NULL) { |
| 112 } | 111 } |
| 113 ~HistoryTest() { | 112 ~HistoryTest() { |
| 114 } | 113 } |
| 115 | 114 |
| 116 // Thumbnail callback: we save the data and exit the message loop so the | |
| 117 // unit test can read the data | |
| 118 void OnThumbnailDataAvailable( | |
| 119 HistoryService::Handle request_handle, | |
| 120 scoped_refptr<RefCountedBytes> jpeg_data) { | |
| 121 got_thumbnail_callback_ = true; | |
| 122 if (jpeg_data.get()) { | |
| 123 std::copy(jpeg_data->data.begin(), jpeg_data->data.end(), | |
| 124 std::back_inserter(thumbnail_data_)); | |
| 125 } | |
| 126 MessageLoop::current()->Quit(); | |
| 127 } | |
| 128 | |
| 129 // Creates the HistoryBackend and HistoryDatabase on the current thread, | 115 // Creates the HistoryBackend and HistoryDatabase on the current thread, |
| 130 // assigning the values to backend_ and db_. | 116 // assigning the values to backend_ and db_. |
| 131 void CreateBackendAndDatabase() { | 117 void CreateBackendAndDatabase() { |
| 132 backend_ = | 118 backend_ = |
| 133 new HistoryBackend(history_dir_, new BackendDelegate(this), NULL); | 119 new HistoryBackend(history_dir_, new BackendDelegate(this), NULL); |
| 134 backend_->Init(std::string(), false); | 120 backend_->Init(std::string(), false); |
| 135 db_ = backend_->db_.get(); | 121 db_ = backend_->db_.get(); |
| 136 DCHECK(in_mem_backend_.get()) << "Mem backend should have been set by " | 122 DCHECK(in_mem_backend_.get()) << "Mem backend should have been set by " |
| 137 "HistoryBackend::Init"; | 123 "HistoryBackend::Init"; |
| 138 } | 124 } |
| (...skipping 536 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 675 MessageLoop::current()->Run(); | 661 MessageLoop::current()->Run(); |
| 676 | 662 |
| 677 // Make sure we still have one segment. | 663 // Make sure we still have one segment. |
| 678 ASSERT_EQ(1U, page_usage_data_->size()); | 664 ASSERT_EQ(1U, page_usage_data_->size()); |
| 679 EXPECT_TRUE(page_usage_data_[0]->GetURL() == existing_url); | 665 EXPECT_TRUE(page_usage_data_[0]->GetURL() == existing_url); |
| 680 | 666 |
| 681 // However, the score should have increased. | 667 // However, the score should have increased. |
| 682 EXPECT_GT(page_usage_data_[0]->GetScore(), 5.0); | 668 EXPECT_GT(page_usage_data_[0]->GetScore(), 5.0); |
| 683 } | 669 } |
| 684 | 670 |
| 685 // This just tests history system -> thumbnail database integration, the actual | |
| 686 // thumbnail tests are in its own file. | |
| 687 TEST_F(HistoryTest, Thumbnails) { | |
| 688 if (history::TopSites::IsEnabled()) | |
| 689 return; // TopSitesTest replaces this. | |
| 690 | |
| 691 scoped_refptr<HistoryService> history(new HistoryService); | |
| 692 history_service_ = history; | |
| 693 ASSERT_TRUE(history->Init(history_dir_, NULL)); | |
| 694 | |
| 695 scoped_ptr<SkBitmap> thumbnail( | |
| 696 gfx::JPEGCodec::Decode(kGoogleThumbnail, sizeof(kGoogleThumbnail))); | |
| 697 static const double boringness = 0.25; | |
| 698 | |
| 699 const GURL url("http://www.google.com/thumbnail_test/"); | |
| 700 // Must be visited before adding a thumbnail. | |
| 701 history->AddPage(url, history::SOURCE_BROWSED); | |
| 702 history->SetPageThumbnail(url, *thumbnail, | |
| 703 ThumbnailScore(boringness, true, true)); | |
| 704 | |
| 705 // Make sure we get the correct thumbnail data. | |
| 706 EXPECT_TRUE(history->GetPageThumbnail(url, &consumer_, | |
| 707 NewCallback(static_cast<HistoryTest*>(this), | |
| 708 &HistoryTest::OnThumbnailDataAvailable))); | |
| 709 thumbnail_data_.clear(); | |
| 710 MessageLoop::current()->Run(); | |
| 711 // Make sure we got a valid JPEG back. This isn't equivalent to | |
| 712 // being correct, but when we're roundtripping through JPEG | |
| 713 // compression and we don't have a similarity measure. | |
| 714 EXPECT_TRUE(thumbnail_data_.size()); | |
| 715 scoped_ptr<SkBitmap> decoded_thumbnail( | |
| 716 gfx::JPEGCodec::Decode(&thumbnail_data_[0], thumbnail_data_.size())); | |
| 717 EXPECT_TRUE(decoded_thumbnail.get()); | |
| 718 | |
| 719 // Request a nonexistent thumbnail and make sure we get | |
| 720 // a callback and no data. | |
| 721 EXPECT_TRUE(history->GetPageThumbnail(GURL("http://asdfasdf.com/"), | |
| 722 &consumer_, | |
| 723 NewCallback(static_cast<HistoryTest*>(this), | |
| 724 &HistoryTest::OnThumbnailDataAvailable))); | |
| 725 thumbnail_data_.clear(); | |
| 726 MessageLoop::current()->Run(); | |
| 727 EXPECT_EQ(0U, thumbnail_data_.size()); | |
| 728 | |
| 729 // Request the thumbnail and cancel the request.. | |
| 730 got_thumbnail_callback_ = false; | |
| 731 thumbnail_data_.clear(); | |
| 732 HistoryService::Handle handle = history->GetPageThumbnail(url, &consumer_, | |
| 733 NewCallback(static_cast<HistoryTest*>(this), | |
| 734 &HistoryTest::OnThumbnailDataAvailable)); | |
| 735 EXPECT_TRUE(handle); | |
| 736 | |
| 737 history->CancelRequest(handle); | |
| 738 | |
| 739 // We create a task with a timeout so we can make sure we don't get and | |
| 740 // data in that time. | |
| 741 class QuitMessageLoop : public Task { | |
| 742 public: | |
| 743 virtual void Run() { | |
| 744 MessageLoop::current()->Quit(); | |
| 745 } | |
| 746 }; | |
| 747 MessageLoop::current()->PostDelayedTask(FROM_HERE, new QuitMessageLoop, 2000); | |
| 748 MessageLoop::current()->Run(); | |
| 749 EXPECT_FALSE(got_thumbnail_callback_); | |
| 750 } | |
| 751 | |
| 752 TEST_F(HistoryTest, MostVisitedURLs) { | 671 TEST_F(HistoryTest, MostVisitedURLs) { |
| 753 scoped_refptr<HistoryService> history(new HistoryService); | 672 scoped_refptr<HistoryService> history(new HistoryService); |
| 754 history_service_ = history; | 673 history_service_ = history; |
| 755 ASSERT_TRUE(history->Init(history_dir_, NULL)); | 674 ASSERT_TRUE(history->Init(history_dir_, NULL)); |
| 756 | 675 |
| 757 const GURL url0("http://www.google.com/url0/"); | 676 const GURL url0("http://www.google.com/url0/"); |
| 758 const GURL url1("http://www.google.com/url1/"); | 677 const GURL url1("http://www.google.com/url1/"); |
| 759 const GURL url2("http://www.google.com/url2/"); | 678 const GURL url2("http://www.google.com/url2/"); |
| 760 const GURL url3("http://www.google.com/url3/"); | 679 const GURL url3("http://www.google.com/url3/"); |
| 761 const GURL url4("http://www.google.com/url4/"); | 680 const GURL url4("http://www.google.com/url4/"); |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 932 history_service_ = history; | 851 history_service_ = history; |
| 933 history->ScheduleDBTask(task.get(), &request_consumer); | 852 history->ScheduleDBTask(task.get(), &request_consumer); |
| 934 request_consumer.CancelAllRequests(); | 853 request_consumer.CancelAllRequests(); |
| 935 CleanupHistoryService(); | 854 CleanupHistoryService(); |
| 936 // WARNING: history has now been deleted. | 855 // WARNING: history has now been deleted. |
| 937 history = NULL; | 856 history = NULL; |
| 938 ASSERT_FALSE(task->done_invoked); | 857 ASSERT_FALSE(task->done_invoked); |
| 939 } | 858 } |
| 940 | 859 |
| 941 } // namespace history | 860 } // namespace history |
| OLD | NEW |