| 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 #include "app/l10n_util.h" | 5 #include "app/l10n_util.h" |
| 6 #include "base/file_util.h" | 6 #include "base/file_util.h" |
| 7 #include "base/scoped_temp_dir.h" | 7 #include "base/scoped_temp_dir.h" |
| 8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
| 9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
| 10 #include "base/values.h" | 10 #include "base/values.h" |
| 11 #include "chrome/browser/chrome_thread.h" | 11 #include "chrome/browser/chrome_thread.h" |
| 12 #include "chrome/browser/history/top_sites.h" | 12 #include "chrome/browser/history/top_sites.h" |
| 13 #include "chrome/browser/dom_ui/most_visited_handler.h" | 13 #include "chrome/browser/dom_ui/most_visited_handler.h" |
| 14 #include "chrome/browser/history/history_marshaling.h" | 14 #include "chrome/browser/history/history_marshaling.h" |
| 15 #include "chrome/browser/history/top_sites_database.h" | 15 #include "chrome/browser/history/top_sites_database.h" |
| 16 #include "chrome/browser/history/history_notifications.h" | 16 #include "chrome/browser/history/history_notifications.h" |
| 17 #include "chrome/common/chrome_paths.h" | 17 #include "chrome/common/chrome_paths.h" |
| 18 #include "chrome/test/testing_profile.h" | 18 #include "chrome/test/testing_profile.h" |
| 19 #include "chrome/tools/profiles/thumbnail-inl.h" | 19 #include "chrome/tools/profiles/thumbnail-inl.h" |
| 20 #include "gfx/codec/jpeg_codec.h" | 20 #include "gfx/codec/jpeg_codec.h" |
| 21 #include "googleurl/src/gurl.h" | 21 #include "googleurl/src/gurl.h" |
| 22 #include "grit/chromium_strings.h" | 22 #include "grit/chromium_strings.h" |
| 23 #include "grit/generated_resources.h" | 23 #include "grit/generated_resources.h" |
| 24 #include "grit/locale_settings.h" | 24 #include "grit/locale_settings.h" |
| 25 #include "testing/gmock/include/gmock/gmock.h" |
| 25 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
| 26 #include "third_party/skia/include/core/SkBitmap.h" | 27 #include "third_party/skia/include/core/SkBitmap.h" |
| 27 | 28 |
| 28 | 29 |
| 29 namespace history { | 30 namespace history { |
| 30 | 31 |
| 31 static const unsigned char kBlob[] = | 32 static const unsigned char kBlob[] = |
| 32 "12346102356120394751634516591348710478123649165419234519234512349134"; | 33 "12346102356120394751634516591348710478123649165419234519234512349134"; |
| 33 | 34 |
| 34 class TopSitesTest : public testing::Test { | 35 class TopSitesTest : public testing::Test { |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 scoped_refptr<RefCountedBytes> random_thumbnail_; | 123 scoped_refptr<RefCountedBytes> random_thumbnail_; |
| 123 scoped_refptr<RefCountedBytes> weewar_thumbnail_; | 124 scoped_refptr<RefCountedBytes> weewar_thumbnail_; |
| 124 MessageLoop message_loop_; | 125 MessageLoop message_loop_; |
| 125 CancelableRequestConsumer consumer_; | 126 CancelableRequestConsumer consumer_; |
| 126 | 127 |
| 127 DISALLOW_COPY_AND_ASSIGN(TopSitesTest); | 128 DISALLOW_COPY_AND_ASSIGN(TopSitesTest); |
| 128 }; | 129 }; |
| 129 | 130 |
| 130 | 131 |
| 131 // A mockup of a HistoryService used for testing TopSites. | 132 // A mockup of a HistoryService used for testing TopSites. |
| 132 class MockHistoryServiceImpl : public TopSites::MockHistoryService { | 133 class MockHistoryServiceImpl |
| 134 : public base::RefCountedThreadSafe<MockHistoryServiceImpl>, |
| 135 public TopSites::MockHistoryService { |
| 133 public: | 136 public: |
| 134 MockHistoryServiceImpl() : num_thumbnail_requests_(0) {} | 137 MockHistoryServiceImpl() : num_thumbnail_requests_(0), handle_num_(0) {} |
| 135 | 138 |
| 136 // Calls the callback directly with the results. | 139 // Calls the callback directly with the results. |
| 137 HistoryService::Handle QueryMostVisitedURLs( | 140 HistoryService::Handle QueryMostVisitedURLs( |
| 138 int result_count, int days_back, | 141 int result_count, int days_back, |
| 139 CancelableRequestConsumerBase* consumer, | 142 CancelableRequestConsumerBase* consumer, |
| 140 HistoryService::QueryMostVisitedURLsCallback* callback) { | 143 HistoryService::QueryMostVisitedURLsCallback* callback) { |
| 141 callback->Run(CancelableRequestProvider::Handle(0), // Handle is unused. | 144 CancelableRequestProvider::Handle handle(++handle_num_); |
| 142 most_visited_urls_); | 145 // Run the callback on another task in order that the handle can be |
| 146 // returned before the callback is run, which is necessary for the |
| 147 // refresh callback mechanism to work in |TopSites|. |
| 148 MessageLoop::current()->PostTask( |
| 149 FROM_HERE, |
| 150 NewRunnableMethod(this, |
| 151 &MockHistoryServiceImpl::RunCallback, |
| 152 callback, |
| 153 handle)); |
| 154 return handle; |
| 155 } |
| 156 |
| 157 void RunCallback(HistoryService::QueryMostVisitedURLsCallback* callback, |
| 158 CancelableRequestProvider::Handle handle) { |
| 159 callback->Run(handle, most_visited_urls_); |
| 143 delete callback; | 160 delete callback; |
| 144 return 0; | |
| 145 } | 161 } |
| 146 | 162 |
| 147 // Add a page to the end of the pages list. | 163 // Add a page to the end of the pages list. |
| 148 void AppendMockPage(const GURL& url, | 164 void AppendMockPage(const GURL& url, |
| 149 const string16& title) { | 165 const string16& title) { |
| 150 MostVisitedURL page; | 166 MostVisitedURL page; |
| 151 page.url = url; | 167 page.url = url; |
| 152 page.title = title; | 168 page.title = title; |
| 153 page.redirects = RedirectList(); | 169 page.redirects = RedirectList(); |
| 154 page.redirects.push_back(url); | 170 page.redirects.push_back(url); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 181 num_thumbnail_requests_ = 0; | 197 num_thumbnail_requests_ = 0; |
| 182 } | 198 } |
| 183 | 199 |
| 184 int GetNumberOfThumbnailRequests() { | 200 int GetNumberOfThumbnailRequests() { |
| 185 return num_thumbnail_requests_; | 201 return num_thumbnail_requests_; |
| 186 } | 202 } |
| 187 | 203 |
| 188 private: | 204 private: |
| 189 MostVisitedURLList most_visited_urls_; | 205 MostVisitedURLList most_visited_urls_; |
| 190 int num_thumbnail_requests_; // Number of calls to GetPageThumbnail. | 206 int num_thumbnail_requests_; // Number of calls to GetPageThumbnail. |
| 207 int handle_num_; |
| 191 }; | 208 }; |
| 192 | 209 |
| 193 | 210 |
| 194 // A mockup of a TopSitesDatabase used for testing TopSites. | 211 // A mockup of a TopSitesDatabase used for testing TopSites. |
| 195 class MockTopSitesDatabaseImpl : public TopSitesDatabase { | 212 class MockTopSitesDatabaseImpl : public TopSitesDatabase { |
| 196 public: | 213 public: |
| 197 virtual void GetPageThumbnails(MostVisitedURLList* urls, | 214 virtual void GetPageThumbnails(MostVisitedURLList* urls, |
| 198 std::map<GURL, Images>* thumbnails) { | 215 std::map<GURL, Images>* thumbnails) { |
| 199 // Return a copy of the vector. | 216 // Return a copy of the vector. |
| 200 *urls = top_sites_list_; | 217 *urls = top_sites_list_; |
| (...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 457 result->size())); | 474 result->size())); |
| 458 EXPECT_EQ(0, memcmp(thumbnail.getPixels(), out_bitmap->getPixels(), | 475 EXPECT_EQ(0, memcmp(thumbnail.getPixels(), out_bitmap->getPixels(), |
| 459 thumbnail.getSize())); | 476 thumbnail.getSize())); |
| 460 } | 477 } |
| 461 | 478 |
| 462 TEST_F(TopSitesTest, GetMostVisited) { | 479 TEST_F(TopSitesTest, GetMostVisited) { |
| 463 ChromeThread db_loop(ChromeThread::DB, MessageLoop::current()); | 480 ChromeThread db_loop(ChromeThread::DB, MessageLoop::current()); |
| 464 GURL news("http://news.google.com/"); | 481 GURL news("http://news.google.com/"); |
| 465 GURL google("http://google.com/"); | 482 GURL google("http://google.com/"); |
| 466 | 483 |
| 467 MockHistoryServiceImpl hs; | 484 scoped_refptr<MockHistoryServiceImpl> hs(new MockHistoryServiceImpl); |
| 468 hs.AppendMockPage(news, ASCIIToUTF16("Google News")); | 485 hs->AppendMockPage(news, ASCIIToUTF16("Google News")); |
| 469 hs.AppendMockPage(google, ASCIIToUTF16("Google")); | 486 hs->AppendMockPage(google, ASCIIToUTF16("Google")); |
| 470 top_sites().SetMockHistoryService(&hs); | 487 top_sites().SetMockHistoryService(hs.get()); |
| 471 | 488 |
| 472 top_sites().StartQueryForMostVisited(); | 489 top_sites().StartQueryForMostVisited(); |
| 473 MessageLoop::current()->RunAllPending(); | 490 MessageLoop::current()->RunAllPending(); |
| 474 top_sites().GetMostVisitedURLs( | 491 top_sites().GetMostVisitedURLs( |
| 475 consumer(), | 492 consumer(), |
| 476 NewCallback(static_cast<TopSitesTest*>(this), | 493 NewCallback(static_cast<TopSitesTest*>(this), |
| 477 &TopSitesTest::OnTopSitesAvailable)); | 494 &TopSitesTest::OnTopSitesAvailable)); |
| 478 // 2 extra prepopulated URLs. | 495 // 2 extra prepopulated URLs. |
| 479 ASSERT_EQ(4u, urls().size()); | 496 ASSERT_EQ(4u, urls().size()); |
| 480 EXPECT_EQ(news, urls()[0].url); | 497 EXPECT_EQ(news, urls()[0].url); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 529 NewCallback(static_cast<TopSitesTest*>(this), | 546 NewCallback(static_cast<TopSitesTest*>(this), |
| 530 &TopSitesTest::OnTopSitesAvailable)); | 547 &TopSitesTest::OnTopSitesAvailable)); |
| 531 ASSERT_EQ(4u, urls().size()); | 548 ASSERT_EQ(4u, urls().size()); |
| 532 EXPECT_EQ(google_url, urls()[0].url); | 549 EXPECT_EQ(google_url, urls()[0].url); |
| 533 EXPECT_EQ(google_title, urls()[0].title); | 550 EXPECT_EQ(google_title, urls()[0].title); |
| 534 EXPECT_EQ(asdf_url, urls()[1].url); | 551 EXPECT_EQ(asdf_url, urls()[1].url); |
| 535 EXPECT_EQ(asdf_title, urls()[1].title); | 552 EXPECT_EQ(asdf_title, urls()[1].title); |
| 536 EXPECT_EQ(welcome_url(), urls()[2].url); | 553 EXPECT_EQ(welcome_url(), urls()[2].url); |
| 537 EXPECT_EQ(themes_url(), urls()[3].url); | 554 EXPECT_EQ(themes_url(), urls()[3].url); |
| 538 | 555 |
| 539 MockHistoryServiceImpl hs; | 556 scoped_refptr<MockHistoryServiceImpl> hs(new MockHistoryServiceImpl); |
| 540 // Add one old, one new URL to the history. | 557 // Add one old, one new URL to the history. |
| 541 hs.AppendMockPage(google_url, google_title); | 558 hs->AppendMockPage(google_url, google_title); |
| 542 hs.AppendMockPage(news_url, news_title); | 559 hs->AppendMockPage(news_url, news_title); |
| 543 top_sites().SetMockHistoryService(&hs); | 560 top_sites().SetMockHistoryService(hs.get()); |
| 544 | 561 |
| 545 // This writes the new data to the DB. | 562 // This writes the new data to the DB. |
| 546 top_sites().StartQueryForMostVisited(); | 563 top_sites().StartQueryForMostVisited(); |
| 547 MessageLoop::current()->RunAllPending(); | 564 MessageLoop::current()->RunAllPending(); |
| 548 | 565 |
| 549 std::map<GURL, Images> thumbnails; | 566 std::map<GURL, Images> thumbnails; |
| 550 MostVisitedURLList result; | 567 MostVisitedURLList result; |
| 551 db->GetPageThumbnails(&result, &thumbnails); | 568 db->GetPageThumbnails(&result, &thumbnails); |
| 552 ASSERT_EQ(4u, result.size()); | 569 ASSERT_EQ(4u, result.size()); |
| 553 EXPECT_EQ(google_title, result[0].title); | 570 EXPECT_EQ(google_title, result[0].title); |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 710 ASSERT_EQ(3u, urls()[0].redirects.size()); | 727 ASSERT_EQ(3u, urls()[0].redirects.size()); |
| 711 EXPECT_EQ(google1_url, urls()[0].redirects[0]); | 728 EXPECT_EQ(google1_url, urls()[0].redirects[0]); |
| 712 EXPECT_EQ(google2_url, urls()[0].redirects[1]); | 729 EXPECT_EQ(google2_url, urls()[0].redirects[1]); |
| 713 EXPECT_EQ(google3_url, urls()[0].redirects[2]); | 730 EXPECT_EQ(google3_url, urls()[0].redirects[2]); |
| 714 | 731 |
| 715 EXPECT_EQ(asdf_url, urls()[1].url); | 732 EXPECT_EQ(asdf_url, urls()[1].url); |
| 716 EXPECT_EQ(asdf_title, urls()[1].title); | 733 EXPECT_EQ(asdf_title, urls()[1].title); |
| 717 EXPECT_EQ(welcome_url(), urls()[2].url); | 734 EXPECT_EQ(welcome_url(), urls()[2].url); |
| 718 EXPECT_EQ(themes_url(), urls()[3].url); | 735 EXPECT_EQ(themes_url(), urls()[3].url); |
| 719 | 736 |
| 720 MockHistoryServiceImpl hs; | 737 scoped_refptr<MockHistoryServiceImpl> hs(new MockHistoryServiceImpl); |
| 721 // Add one old, one new URL to the history. | 738 // Add one old, one new URL to the history. |
| 722 hs.AppendMockPage(google1_url, google_title); | 739 hs->AppendMockPage(google1_url, google_title); |
| 723 hs.AppendMockPage(news_url, news_title); | 740 hs->AppendMockPage(news_url, news_title); |
| 724 top_sites().SetMockHistoryService(&hs); | 741 top_sites().SetMockHistoryService(hs.get()); |
| 725 | 742 |
| 726 // This requests data from History Service and writes it to the DB. | 743 // This requests data from History Service and writes it to the DB. |
| 727 top_sites().StartQueryForMostVisited(); | 744 top_sites().StartQueryForMostVisited(); |
| 728 MessageLoop::current()->RunAllPending(); | 745 MessageLoop::current()->RunAllPending(); |
| 729 | 746 |
| 730 std::map<GURL, Images> thumbnails; | 747 std::map<GURL, Images> thumbnails; |
| 731 MostVisitedURLList results; | 748 MostVisitedURLList results; |
| 732 db->GetPageThumbnails(&results, &thumbnails); | 749 db->GetPageThumbnails(&results, &thumbnails); |
| 733 ASSERT_EQ(4u, results.size()); | 750 ASSERT_EQ(4u, results.size()); |
| 734 EXPECT_EQ(google_title, results[0].title); | 751 EXPECT_EQ(google_title, results[0].title); |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 782 | 799 |
| 783 TEST_F(TopSitesTest, DeleteNotifications) { | 800 TEST_F(TopSitesTest, DeleteNotifications) { |
| 784 ChromeThread db_loop(ChromeThread::DB, MessageLoop::current()); | 801 ChromeThread db_loop(ChromeThread::DB, MessageLoop::current()); |
| 785 GURL google1_url("http://google.com"); | 802 GURL google1_url("http://google.com"); |
| 786 GURL google2_url("http://google.com/redirect"); | 803 GURL google2_url("http://google.com/redirect"); |
| 787 GURL google3_url("http://www.google.com"); | 804 GURL google3_url("http://www.google.com"); |
| 788 string16 google_title(ASCIIToUTF16("Google")); | 805 string16 google_title(ASCIIToUTF16("Google")); |
| 789 GURL news_url("http://news.google.com"); | 806 GURL news_url("http://news.google.com"); |
| 790 string16 news_title(ASCIIToUTF16("Google News")); | 807 string16 news_title(ASCIIToUTF16("Google News")); |
| 791 | 808 |
| 792 MockHistoryServiceImpl hs; | 809 scoped_refptr<MockHistoryServiceImpl> hs(new MockHistoryServiceImpl); |
| 793 | 810 |
| 794 top_sites().Init(file_name()); | 811 top_sites().Init(file_name()); |
| 795 | 812 |
| 796 hs.AppendMockPage(google1_url, google_title); | 813 hs->AppendMockPage(google1_url, google_title); |
| 797 hs.AppendMockPage(news_url, news_title); | 814 hs->AppendMockPage(news_url, news_title); |
| 798 top_sites().SetMockHistoryService(&hs); | 815 top_sites().SetMockHistoryService(hs.get()); |
| 799 | 816 |
| 800 top_sites().StartQueryForMostVisited(); | 817 top_sites().StartQueryForMostVisited(); |
| 801 MessageLoop::current()->RunAllPending(); | 818 MessageLoop::current()->RunAllPending(); |
| 802 | 819 |
| 803 top_sites().GetMostVisitedURLs( | 820 top_sites().GetMostVisitedURLs( |
| 804 consumer(), | 821 consumer(), |
| 805 NewCallback(static_cast<TopSitesTest*>(this), | 822 NewCallback(static_cast<TopSitesTest*>(this), |
| 806 &TopSitesTest::OnTopSitesAvailable)); | 823 &TopSitesTest::OnTopSitesAvailable)); |
| 807 // 2 extra prepopulated URLs. | 824 // 2 extra prepopulated URLs. |
| 808 ASSERT_EQ(4u, urls().size()); | 825 ASSERT_EQ(4u, urls().size()); |
| 809 | 826 |
| 810 hs.RemoveMostVisitedURL(); | 827 hs->RemoveMostVisitedURL(); |
| 811 | 828 |
| 812 history::URLsDeletedDetails history_details; | 829 history::URLsDeletedDetails history_details; |
| 813 history_details.all_history = false; | 830 history_details.all_history = false; |
| 814 Details<URLsDeletedDetails> details(&history_details); | 831 Details<URLsDeletedDetails> details(&history_details); |
| 815 top_sites().Observe(NotificationType::HISTORY_URLS_DELETED, | 832 top_sites().Observe(NotificationType::HISTORY_URLS_DELETED, |
| 816 Source<Profile> (&profile()), | 833 Source<Profile> (&profile()), |
| 817 details); | 834 details); |
| 818 MessageLoop::current()->RunAllPending(); | 835 MessageLoop::current()->RunAllPending(); |
| 819 | 836 |
| 820 top_sites().GetMostVisitedURLs( | 837 top_sites().GetMostVisitedURLs( |
| 821 consumer(), | 838 consumer(), |
| 822 NewCallback(static_cast<TopSitesTest*>(this), | 839 NewCallback(static_cast<TopSitesTest*>(this), |
| 823 &TopSitesTest::OnTopSitesAvailable)); | 840 &TopSitesTest::OnTopSitesAvailable)); |
| 824 ASSERT_EQ(3u, urls().size()); | 841 ASSERT_EQ(3u, urls().size()); |
| 825 EXPECT_EQ(google_title, urls()[0].title); | 842 EXPECT_EQ(google_title, urls()[0].title); |
| 826 EXPECT_EQ(welcome_url(), urls()[1].url); | 843 EXPECT_EQ(welcome_url(), urls()[1].url); |
| 827 EXPECT_EQ(themes_url(), urls()[2].url); | 844 EXPECT_EQ(themes_url(), urls()[2].url); |
| 828 | 845 |
| 829 hs.RemoveMostVisitedURL(); | 846 hs->RemoveMostVisitedURL(); |
| 830 history_details.all_history = true; | 847 history_details.all_history = true; |
| 831 details = Details<HistoryDetails>(&history_details); | 848 details = Details<HistoryDetails>(&history_details); |
| 832 top_sites().Observe(NotificationType::HISTORY_URLS_DELETED, | 849 top_sites().Observe(NotificationType::HISTORY_URLS_DELETED, |
| 833 Source<Profile> (&profile()), | 850 Source<Profile> (&profile()), |
| 834 details); | 851 details); |
| 835 MessageLoop::current()->RunAllPending(); | 852 MessageLoop::current()->RunAllPending(); |
| 836 top_sites().GetMostVisitedURLs( | 853 top_sites().GetMostVisitedURLs( |
| 837 consumer(), | 854 consumer(), |
| 838 NewCallback(static_cast<TopSitesTest*>(this), | 855 NewCallback(static_cast<TopSitesTest*>(this), |
| 839 &TopSitesTest::OnTopSitesAvailable)); | 856 &TopSitesTest::OnTopSitesAvailable)); |
| 840 ASSERT_EQ(2u, urls().size()); | 857 ASSERT_EQ(2u, urls().size()); |
| 841 EXPECT_EQ(welcome_url(), urls()[0].url); | 858 EXPECT_EQ(welcome_url(), urls()[0].url); |
| 842 EXPECT_EQ(themes_url(), urls()[1].url); | 859 EXPECT_EQ(themes_url(), urls()[1].url); |
| 843 } | 860 } |
| 844 | 861 |
| 845 TEST_F(TopSitesTest, PinnedURLsDeleted) { | 862 TEST_F(TopSitesTest, PinnedURLsDeleted) { |
| 846 ChromeThread db_loop(ChromeThread::DB, MessageLoop::current()); | 863 ChromeThread db_loop(ChromeThread::DB, MessageLoop::current()); |
| 847 GURL google1_url("http://google.com"); | 864 GURL google1_url("http://google.com"); |
| 848 GURL google2_url("http://google.com/redirect"); | 865 GURL google2_url("http://google.com/redirect"); |
| 849 GURL google3_url("http://www.google.com"); | 866 GURL google3_url("http://www.google.com"); |
| 850 string16 google_title(ASCIIToUTF16("Google")); | 867 string16 google_title(ASCIIToUTF16("Google")); |
| 851 GURL news_url("http://news.google.com"); | 868 GURL news_url("http://news.google.com"); |
| 852 string16 news_title(ASCIIToUTF16("Google News")); | 869 string16 news_title(ASCIIToUTF16("Google News")); |
| 853 | 870 |
| 854 MockHistoryServiceImpl hs; | 871 scoped_refptr<MockHistoryServiceImpl> hs(new MockHistoryServiceImpl); |
| 855 | 872 |
| 856 top_sites().Init(file_name()); | 873 top_sites().Init(file_name()); |
| 857 | 874 |
| 858 hs.AppendMockPage(google1_url, google_title); | 875 hs->AppendMockPage(google1_url, google_title); |
| 859 hs.AppendMockPage(news_url, news_title); | 876 hs->AppendMockPage(news_url, news_title); |
| 860 top_sites().SetMockHistoryService(&hs); | 877 top_sites().SetMockHistoryService(hs.get()); |
| 861 | 878 |
| 862 top_sites().StartQueryForMostVisited(); | 879 top_sites().StartQueryForMostVisited(); |
| 863 MessageLoop::current()->RunAllPending(); | 880 MessageLoop::current()->RunAllPending(); |
| 864 top_sites().GetMostVisitedURLs( | 881 top_sites().GetMostVisitedURLs( |
| 865 consumer(), | 882 consumer(), |
| 866 NewCallback(static_cast<TopSitesTest*>(this), | 883 NewCallback(static_cast<TopSitesTest*>(this), |
| 867 &TopSitesTest::OnTopSitesAvailable)); | 884 &TopSitesTest::OnTopSitesAvailable)); |
| 868 MessageLoop::current()->RunAllPending(); | 885 MessageLoop::current()->RunAllPending(); |
| 869 EXPECT_EQ(1u, number_of_callbacks()); | 886 EXPECT_EQ(1u, number_of_callbacks()); |
| 870 // 2 extra prepopulated URLs. | 887 // 2 extra prepopulated URLs. |
| 871 ASSERT_EQ(4u, urls().size()); | 888 ASSERT_EQ(4u, urls().size()); |
| 872 | 889 |
| 873 top_sites().AddPinnedURL(news_url, 3); | 890 top_sites().AddPinnedURL(news_url, 3); |
| 874 EXPECT_TRUE(top_sites().IsURLPinned(news_url)); | 891 EXPECT_TRUE(top_sites().IsURLPinned(news_url)); |
| 875 | 892 |
| 876 hs.RemoveMostVisitedURL(); | 893 hs->RemoveMostVisitedURL(); |
| 877 history::URLsDeletedDetails history_details; | 894 history::URLsDeletedDetails history_details; |
| 878 history_details.all_history = false; | 895 history_details.all_history = false; |
| 879 history_details.urls.insert(news_url); | 896 history_details.urls.insert(news_url); |
| 880 Details<URLsDeletedDetails> details(&history_details); | 897 Details<URLsDeletedDetails> details(&history_details); |
| 881 top_sites().Observe(NotificationType::HISTORY_URLS_DELETED, | 898 top_sites().Observe(NotificationType::HISTORY_URLS_DELETED, |
| 882 Source<Profile> (&profile()), | 899 Source<Profile> (&profile()), |
| 883 details); | 900 details); |
| 884 MessageLoop::current()->RunAllPending(); | 901 MessageLoop::current()->RunAllPending(); |
| 885 top_sites().GetMostVisitedURLs( | 902 top_sites().GetMostVisitedURLs( |
| 886 consumer(), | 903 consumer(), |
| 887 NewCallback(static_cast<TopSitesTest*>(this), | 904 NewCallback(static_cast<TopSitesTest*>(this), |
| 888 &TopSitesTest::OnTopSitesAvailable)); | 905 &TopSitesTest::OnTopSitesAvailable)); |
| 889 MessageLoop::current()->RunAllPending(); | 906 MessageLoop::current()->RunAllPending(); |
| 890 EXPECT_EQ(2u, number_of_callbacks()); | 907 EXPECT_EQ(2u, number_of_callbacks()); |
| 891 ASSERT_EQ(3u, urls().size()); | 908 ASSERT_EQ(3u, urls().size()); |
| 892 EXPECT_FALSE(top_sites().IsURLPinned(news_url)); | 909 EXPECT_FALSE(top_sites().IsURLPinned(news_url)); |
| 893 | 910 |
| 894 hs.RemoveMostVisitedURL(); | 911 hs->RemoveMostVisitedURL(); |
| 895 history_details.all_history = true; | 912 history_details.all_history = true; |
| 896 details = Details<HistoryDetails>(&history_details); | 913 details = Details<HistoryDetails>(&history_details); |
| 897 top_sites().Observe(NotificationType::HISTORY_URLS_DELETED, | 914 top_sites().Observe(NotificationType::HISTORY_URLS_DELETED, |
| 898 Source<Profile> (&profile()), | 915 Source<Profile> (&profile()), |
| 899 details); | 916 details); |
| 900 MessageLoop::current()->RunAllPending(); | 917 MessageLoop::current()->RunAllPending(); |
| 901 top_sites().GetMostVisitedURLs( | 918 top_sites().GetMostVisitedURLs( |
| 902 consumer(), | 919 consumer(), |
| 903 NewCallback(static_cast<TopSitesTest*>(this), | 920 NewCallback(static_cast<TopSitesTest*>(this), |
| 904 &TopSitesTest::OnTopSitesAvailable)); | 921 &TopSitesTest::OnTopSitesAvailable)); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 931 EXPECT_EQ(1, top_sites().GetUpdateDelay().InMinutes()); | 948 EXPECT_EQ(1, top_sites().GetUpdateDelay().InMinutes()); |
| 932 } | 949 } |
| 933 | 950 |
| 934 TEST_F(TopSitesTest, Migration) { | 951 TEST_F(TopSitesTest, Migration) { |
| 935 ChromeThread db_loop(ChromeThread::DB, MessageLoop::current()); | 952 ChromeThread db_loop(ChromeThread::DB, MessageLoop::current()); |
| 936 GURL google1_url("http://google.com"); | 953 GURL google1_url("http://google.com"); |
| 937 string16 google_title(ASCIIToUTF16("Google")); | 954 string16 google_title(ASCIIToUTF16("Google")); |
| 938 GURL news_url("http://news.google.com"); | 955 GURL news_url("http://news.google.com"); |
| 939 string16 news_title(ASCIIToUTF16("Google News")); | 956 string16 news_title(ASCIIToUTF16("Google News")); |
| 940 | 957 |
| 941 MockHistoryServiceImpl hs; | 958 scoped_refptr<MockHistoryServiceImpl> hs(new MockHistoryServiceImpl); |
| 942 | 959 |
| 943 top_sites().Init(file_name()); | 960 top_sites().Init(file_name()); |
| 944 | 961 |
| 945 hs.AppendMockPage(google1_url, google_title); | 962 hs->AppendMockPage(google1_url, google_title); |
| 946 hs.AppendMockPage(news_url, news_title); | 963 hs->AppendMockPage(news_url, news_title); |
| 947 top_sites().SetMockHistoryService(&hs); | 964 top_sites().SetMockHistoryService(hs.get()); |
| 948 MessageLoop::current()->RunAllPending(); | 965 MessageLoop::current()->RunAllPending(); |
| 949 | 966 |
| 950 top_sites().StartMigration(); | 967 top_sites().StartMigration(); |
| 951 EXPECT_TRUE(top_sites().migration_in_progress_); | 968 EXPECT_TRUE(top_sites().migration_in_progress_); |
| 952 MessageLoop::current()->RunAllPending(); | 969 MessageLoop::current()->RunAllPending(); |
| 953 EXPECT_EQ(2, hs.GetNumberOfThumbnailRequests()); | 970 EXPECT_EQ(2, hs->GetNumberOfThumbnailRequests()); |
| 954 EXPECT_FALSE(top_sites().migration_in_progress_); | 971 EXPECT_FALSE(top_sites().migration_in_progress_); |
| 955 } | 972 } |
| 956 | 973 |
| 957 TEST_F(TopSitesTest, QueueingRequestsForTopSites) { | 974 TEST_F(TopSitesTest, QueueingRequestsForTopSites) { |
| 958 ChromeThread db_loop(ChromeThread::DB, MessageLoop::current()); | 975 ChromeThread db_loop(ChromeThread::DB, MessageLoop::current()); |
| 959 CancelableRequestConsumer c1; | 976 CancelableRequestConsumer c1; |
| 960 CancelableRequestConsumer c2; | 977 CancelableRequestConsumer c2; |
| 961 CancelableRequestConsumer c3; | 978 CancelableRequestConsumer c3; |
| 962 top_sites().GetMostVisitedURLs( | 979 top_sites().GetMostVisitedURLs( |
| 963 &c1, | 980 &c1, |
| (...skipping 14 matching lines...) Expand all Loading... |
| 978 EXPECT_EQ(0u, urls().size()); | 995 EXPECT_EQ(0u, urls().size()); |
| 979 | 996 |
| 980 MostVisitedURLList pages; | 997 MostVisitedURLList pages; |
| 981 MostVisitedURL url; | 998 MostVisitedURL url; |
| 982 url.url = GURL("http://1.com/"); | 999 url.url = GURL("http://1.com/"); |
| 983 url.redirects.push_back(url.url); | 1000 url.redirects.push_back(url.url); |
| 984 pages.push_back(url); | 1001 pages.push_back(url); |
| 985 url.url = GURL("http://2.com/"); | 1002 url.url = GURL("http://2.com/"); |
| 986 url.redirects.push_back(url.url); | 1003 url.redirects.push_back(url.url); |
| 987 pages.push_back(url); | 1004 pages.push_back(url); |
| 988 top_sites().OnTopSitesAvailable(0, pages); | 1005 top_sites().OnTopSitesAvailable(1, pages); |
| 989 MessageLoop::current()->RunAllPending(); | 1006 MessageLoop::current()->RunAllPending(); |
| 990 | 1007 |
| 991 EXPECT_EQ(3u, number_of_callbacks()); | 1008 EXPECT_EQ(3u, number_of_callbacks()); |
| 992 | 1009 |
| 993 ASSERT_EQ(4u, urls().size()); | 1010 ASSERT_EQ(4u, urls().size()); |
| 994 EXPECT_EQ("http://1.com/", urls()[0].url.spec()); | 1011 EXPECT_EQ("http://1.com/", urls()[0].url.spec()); |
| 995 EXPECT_EQ("http://2.com/", urls()[1].url.spec()); | 1012 EXPECT_EQ("http://2.com/", urls()[1].url.spec()); |
| 996 EXPECT_EQ(welcome_url(), urls()[2].url); | 1013 EXPECT_EQ(welcome_url(), urls()[2].url); |
| 997 EXPECT_EQ(themes_url(), urls()[3].url); | 1014 EXPECT_EQ(themes_url(), urls()[3].url); |
| 998 | 1015 |
| 999 | 1016 |
| 1000 url.url = GURL("http://3.com/"); | 1017 url.url = GURL("http://3.com/"); |
| 1001 url.redirects.push_back(url.url); | 1018 url.redirects.push_back(url.url); |
| 1002 pages.push_back(url); | 1019 pages.push_back(url); |
| 1003 top_sites().OnTopSitesAvailable(0, pages); | 1020 top_sites().OnTopSitesAvailable(1, pages); |
| 1004 MessageLoop::current()->RunAllPending(); | 1021 MessageLoop::current()->RunAllPending(); |
| 1005 | 1022 |
| 1006 top_sites().GetMostVisitedURLs( | 1023 top_sites().GetMostVisitedURLs( |
| 1007 &c3, | 1024 &c3, |
| 1008 NewCallback(static_cast<TopSitesTest*>(this), | 1025 NewCallback(static_cast<TopSitesTest*>(this), |
| 1009 &TopSitesTest::OnTopSitesAvailable)); | 1026 &TopSitesTest::OnTopSitesAvailable)); |
| 1010 | 1027 |
| 1011 EXPECT_EQ(4u, number_of_callbacks()); | 1028 EXPECT_EQ(4u, number_of_callbacks()); |
| 1012 | 1029 |
| 1013 ASSERT_EQ(5u, urls().size()); | 1030 ASSERT_EQ(5u, urls().size()); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1047 EXPECT_EQ(0u, urls().size()); | 1064 EXPECT_EQ(0u, urls().size()); |
| 1048 | 1065 |
| 1049 MostVisitedURLList pages; | 1066 MostVisitedURLList pages; |
| 1050 MostVisitedURL url; | 1067 MostVisitedURL url; |
| 1051 url.url = GURL("http://1.com/"); | 1068 url.url = GURL("http://1.com/"); |
| 1052 url.redirects.push_back(url.url); | 1069 url.redirects.push_back(url.url); |
| 1053 pages.push_back(url); | 1070 pages.push_back(url); |
| 1054 url.url = GURL("http://2.com/"); | 1071 url.url = GURL("http://2.com/"); |
| 1055 pages.push_back(url); | 1072 pages.push_back(url); |
| 1056 | 1073 |
| 1057 top_sites().OnTopSitesAvailable(0, pages); | 1074 top_sites().OnTopSitesAvailable(1, pages); |
| 1058 MessageLoop::current()->RunAllPending(); | 1075 MessageLoop::current()->RunAllPending(); |
| 1059 | 1076 |
| 1060 // 1 request was canceled. | 1077 // 1 request was canceled. |
| 1061 EXPECT_EQ(2u, number_of_callbacks()); | 1078 EXPECT_EQ(2u, number_of_callbacks()); |
| 1062 | 1079 |
| 1063 // 2 extra prepopulated URLs. | 1080 // 2 extra prepopulated URLs. |
| 1064 ASSERT_EQ(4u, urls().size()); | 1081 ASSERT_EQ(4u, urls().size()); |
| 1065 EXPECT_EQ("http://1.com/", urls()[0].url.spec()); | 1082 EXPECT_EQ("http://1.com/", urls()[0].url.spec()); |
| 1066 EXPECT_EQ("http://2.com/", urls()[1].url.spec()); | 1083 EXPECT_EQ("http://2.com/", urls()[1].url.spec()); |
| 1067 } | 1084 } |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1118 pages.push_back(url); | 1135 pages.push_back(url); |
| 1119 url1.url = GURL("http://google.com/"); | 1136 url1.url = GURL("http://google.com/"); |
| 1120 url1.redirects.push_back(url1.url); | 1137 url1.redirects.push_back(url1.url); |
| 1121 pages.push_back(url1); | 1138 pages.push_back(url1); |
| 1122 | 1139 |
| 1123 CancelableRequestConsumer c; | 1140 CancelableRequestConsumer c; |
| 1124 top_sites().GetMostVisitedURLs( | 1141 top_sites().GetMostVisitedURLs( |
| 1125 &c, | 1142 &c, |
| 1126 NewCallback(static_cast<TopSitesTest*>(this), | 1143 NewCallback(static_cast<TopSitesTest*>(this), |
| 1127 &TopSitesTest::OnTopSitesAvailable)); | 1144 &TopSitesTest::OnTopSitesAvailable)); |
| 1128 top_sites().OnTopSitesAvailable(0, pages); | 1145 top_sites().OnTopSitesAvailable(1, pages); |
| 1129 MessageLoop::current()->RunAllPending(); | 1146 MessageLoop::current()->RunAllPending(); |
| 1130 { | 1147 { |
| 1131 Lock& l = lock(); | 1148 Lock& l = lock(); |
| 1132 AutoLock lock(l); // IsBlacklisted must be in a lock. | 1149 AutoLock lock(l); // IsBlacklisted must be in a lock. |
| 1133 EXPECT_FALSE(top_sites().IsBlacklisted(GURL("http://bbc.com/"))); | 1150 EXPECT_FALSE(top_sites().IsBlacklisted(GURL("http://bbc.com/"))); |
| 1134 } | 1151 } |
| 1135 | 1152 |
| 1136 EXPECT_EQ(1u, number_of_callbacks()); | 1153 EXPECT_EQ(1u, number_of_callbacks()); |
| 1137 | 1154 |
| 1138 ASSERT_EQ(4u, urls().size()); | 1155 ASSERT_EQ(4u, urls().size()); |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1212 pages.push_back(url); | 1229 pages.push_back(url); |
| 1213 url1.url = GURL("http://google.com/"); | 1230 url1.url = GURL("http://google.com/"); |
| 1214 url1.redirects.push_back(url1.url); | 1231 url1.redirects.push_back(url1.url); |
| 1215 pages.push_back(url1); | 1232 pages.push_back(url1); |
| 1216 | 1233 |
| 1217 CancelableRequestConsumer c; | 1234 CancelableRequestConsumer c; |
| 1218 top_sites().GetMostVisitedURLs( | 1235 top_sites().GetMostVisitedURLs( |
| 1219 &c, | 1236 &c, |
| 1220 NewCallback(static_cast<TopSitesTest*>(this), | 1237 NewCallback(static_cast<TopSitesTest*>(this), |
| 1221 &TopSitesTest::OnTopSitesAvailable)); | 1238 &TopSitesTest::OnTopSitesAvailable)); |
| 1222 top_sites().OnTopSitesAvailable(0, pages); | 1239 top_sites().OnTopSitesAvailable(1, pages); |
| 1223 MessageLoop::current()->RunAllPending(); | 1240 MessageLoop::current()->RunAllPending(); |
| 1224 EXPECT_FALSE(top_sites().IsURLPinned(GURL("http://bbc.com/"))); | 1241 EXPECT_FALSE(top_sites().IsURLPinned(GURL("http://bbc.com/"))); |
| 1225 | 1242 |
| 1226 ASSERT_EQ(4u, urls().size()); | 1243 ASSERT_EQ(4u, urls().size()); |
| 1227 EXPECT_EQ("http://bbc.com/", urls()[0].url.spec()); | 1244 EXPECT_EQ("http://bbc.com/", urls()[0].url.spec()); |
| 1228 EXPECT_EQ("http://google.com/", urls()[1].url.spec()); | 1245 EXPECT_EQ("http://google.com/", urls()[1].url.spec()); |
| 1229 EXPECT_EQ(welcome_url(), urls()[2].url); | 1246 EXPECT_EQ(welcome_url(), urls()[2].url); |
| 1230 EXPECT_EQ(themes_url(), urls()[3].url); | 1247 EXPECT_EQ(themes_url(), urls()[3].url); |
| 1231 | 1248 |
| 1232 top_sites().AddPinnedURL(GURL("http://google.com/"), 3); | 1249 top_sites().AddPinnedURL(GURL("http://google.com/"), 3); |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1289 } | 1306 } |
| 1290 | 1307 |
| 1291 TEST_F(TopSitesTest, BlacklistingAndPinnedURLs) { | 1308 TEST_F(TopSitesTest, BlacklistingAndPinnedURLs) { |
| 1292 ChromeThread db_loop(ChromeThread::DB, MessageLoop::current()); | 1309 ChromeThread db_loop(ChromeThread::DB, MessageLoop::current()); |
| 1293 MostVisitedURLList pages; | 1310 MostVisitedURLList pages; |
| 1294 CancelableRequestConsumer c; | 1311 CancelableRequestConsumer c; |
| 1295 top_sites().GetMostVisitedURLs( | 1312 top_sites().GetMostVisitedURLs( |
| 1296 &c, | 1313 &c, |
| 1297 NewCallback(static_cast<TopSitesTest*>(this), | 1314 NewCallback(static_cast<TopSitesTest*>(this), |
| 1298 &TopSitesTest::OnTopSitesAvailable)); | 1315 &TopSitesTest::OnTopSitesAvailable)); |
| 1299 top_sites().OnTopSitesAvailable(0, pages); | 1316 top_sites().OnTopSitesAvailable(1, pages); |
| 1300 MessageLoop::current()->RunAllPending(); | 1317 MessageLoop::current()->RunAllPending(); |
| 1301 | 1318 |
| 1302 ASSERT_EQ(2u, urls().size()); | 1319 ASSERT_EQ(2u, urls().size()); |
| 1303 EXPECT_EQ(welcome_url(), urls()[0].url); | 1320 EXPECT_EQ(welcome_url(), urls()[0].url); |
| 1304 EXPECT_EQ(themes_url(), urls()[1].url); | 1321 EXPECT_EQ(themes_url(), urls()[1].url); |
| 1305 | 1322 |
| 1306 top_sites().AddPinnedURL(themes_url(), 1); | 1323 top_sites().AddPinnedURL(themes_url(), 1); |
| 1307 top_sites().AddBlacklistedURL(welcome_url()); | 1324 top_sites().AddBlacklistedURL(welcome_url()); |
| 1308 | 1325 |
| 1309 top_sites().GetMostVisitedURLs( | 1326 top_sites().GetMostVisitedURLs( |
| (...skipping 20 matching lines...) Expand all Loading... |
| 1330 pages.push_back(url); | 1347 pages.push_back(url); |
| 1331 | 1348 |
| 1332 top_sites().AddPrepopulatedPages(&pages); | 1349 top_sites().AddPrepopulatedPages(&pages); |
| 1333 | 1350 |
| 1334 // Themes URL is already in pages; should not be added twice. | 1351 // Themes URL is already in pages; should not be added twice. |
| 1335 ASSERT_EQ(2u, pages.size()); | 1352 ASSERT_EQ(2u, pages.size()); |
| 1336 EXPECT_EQ(themes_url(), pages[0].url); | 1353 EXPECT_EQ(themes_url(), pages[0].url); |
| 1337 EXPECT_EQ(welcome_url(), pages[1].url); | 1354 EXPECT_EQ(welcome_url(), pages[1].url); |
| 1338 } | 1355 } |
| 1339 | 1356 |
| 1357 class MockRefreshObserver { |
| 1358 public: |
| 1359 MOCK_METHOD0(OnRefresh, void ()); // NOLINT |
| 1360 }; |
| 1361 |
| 1362 ACTION_P3(GetMostVisitedURLs, top_sites, consumer, callback_obj) { |
| 1363 top_sites->GetMostVisitedURLs( |
| 1364 consumer, |
| 1365 NewCallback(static_cast<TopSitesTest*>(callback_obj), |
| 1366 &TopSitesTest::OnTopSitesAvailable)); |
| 1367 } |
| 1368 |
| 1369 TEST_F(TopSitesTest, RefreshCallback) { |
| 1370 ChromeThread db_loop(ChromeThread::DB, MessageLoop::current()); |
| 1371 |
| 1372 scoped_refptr<MockHistoryServiceImpl> hs(new MockHistoryServiceImpl); |
| 1373 hs->AppendMockPage(GURL("http://google.com"), ASCIIToUTF16("Google")); |
| 1374 top_sites().SetMockHistoryService(hs.get()); |
| 1375 |
| 1376 top_sites().StartQueryForMostVisited(); |
| 1377 MessageLoop::current()->RunAllPending(); |
| 1378 |
| 1379 // Test that the callback is called. |
| 1380 MockRefreshObserver refresh_mock; |
| 1381 EXPECT_CALL(refresh_mock, OnRefresh()) |
| 1382 .WillOnce(GetMostVisitedURLs(&top_sites(), consumer(), this)); |
| 1383 top_sites().RefreshAndCallback( |
| 1384 consumer(), |
| 1385 NewCallback(&refresh_mock, &MockRefreshObserver::OnRefresh)); |
| 1386 MessageLoop::current()->RunAllPending(); |
| 1387 MessageLoop::current()->RunAllPending(); |
| 1388 ASSERT_EQ(3u, urls().size()); |
| 1389 |
| 1390 // Test that the callback is not called again. |
| 1391 top_sites().GetMostVisitedURLs( |
| 1392 consumer(), |
| 1393 NewCallback(static_cast<TopSitesTest*>(this), |
| 1394 &TopSitesTest::OnTopSitesAvailable)); |
| 1395 MessageLoop::current()->RunAllPending(); |
| 1396 } |
| 1397 |
| 1340 } // namespace history | 1398 } // namespace history |
| OLD | NEW |