| 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 "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/memory/weak_ptr.h" | 6 #include "base/memory/weak_ptr.h" |
| 7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
| 8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "base/task/cancelable_task_tracker.h" | 9 #include "base/task/cancelable_task_tracker.h" |
| 10 #include "chrome/browser/chrome_notification_types.h" | |
| 11 #include "chrome/browser/history/history_notifications.h" | |
| 12 #include "chrome/browser/history/history_service_factory.h" | 10 #include "chrome/browser/history/history_service_factory.h" |
| 13 #include "chrome/browser/history/history_unittest_base.h" | 11 #include "chrome/browser/history/history_unittest_base.h" |
| 14 #include "chrome/browser/history/top_sites.h" | 12 #include "chrome/browser/history/top_sites.h" |
| 15 #include "chrome/browser/history/top_sites_impl.h" | 13 #include "chrome/browser/history/top_sites_impl.h" |
| 16 #include "chrome/common/chrome_constants.h" | 14 #include "chrome/common/chrome_constants.h" |
| 17 #include "chrome/common/chrome_paths.h" | 15 #include "chrome/common/chrome_paths.h" |
| 18 #include "chrome/test/base/testing_profile.h" | 16 #include "chrome/test/base/testing_profile.h" |
| 19 #include "components/history/core/browser/history_db_task.h" | 17 #include "components/history/core/browser/history_db_task.h" |
| 20 #include "components/history/core/browser/history_types.h" | 18 #include "components/history/core/browser/history_types.h" |
| 21 #include "components/history/core/browser/top_sites_cache.h" | 19 #include "components/history/core/browser/top_sites_cache.h" |
| 22 #include "content/public/browser/notification_service.h" | |
| 23 #include "content/public/test/test_browser_thread.h" | 20 #include "content/public/test/test_browser_thread.h" |
| 24 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 25 #include "third_party/skia/include/core/SkBitmap.h" | 22 #include "third_party/skia/include/core/SkBitmap.h" |
| 26 #include "ui/gfx/codec/jpeg_codec.h" | 23 #include "ui/gfx/codec/jpeg_codec.h" |
| 27 #include "url/gurl.h" | 24 #include "url/gurl.h" |
| 28 | 25 |
| 29 using content::BrowserThread; | 26 using content::BrowserThread; |
| 30 | 27 |
| 31 class TestTopSitesObserver : public history::TopSitesObserver { | |
| 32 public: | |
| 33 explicit TestTopSitesObserver(Profile* profile, history::TopSites* top_sites); | |
| 34 virtual ~TestTopSitesObserver(); | |
| 35 // TopSitesObserver: | |
| 36 void TopSitesLoaded(history::TopSites* top_sites) override; | |
| 37 void TopSitesChanged(history::TopSites* top_sites) override; | |
| 38 | |
| 39 private: | |
| 40 Profile* profile_; | |
| 41 history::TopSites* top_sites_; | |
| 42 }; | |
| 43 | |
| 44 TestTopSitesObserver::~TestTopSitesObserver() { | |
| 45 top_sites_->RemoveObserver(this); | |
| 46 } | |
| 47 | |
| 48 TestTopSitesObserver::TestTopSitesObserver(Profile* profile, | |
| 49 history::TopSites* top_sites) | |
| 50 : profile_(profile), top_sites_(top_sites) { | |
| 51 DCHECK(top_sites_); | |
| 52 top_sites_->AddObserver(this); | |
| 53 } | |
| 54 | |
| 55 void TestTopSitesObserver::TopSitesLoaded(history::TopSites* top_sites) { | |
| 56 content::NotificationService::current()->Notify( | |
| 57 chrome::NOTIFICATION_TOP_SITES_LOADED, | |
| 58 content::Source<Profile>(profile_), | |
| 59 content::Details<history::TopSites>(top_sites)); | |
| 60 } | |
| 61 | |
| 62 void TestTopSitesObserver::TopSitesChanged(history::TopSites* top_sites) { | |
| 63 content::NotificationService::current()->Notify( | |
| 64 chrome::NOTIFICATION_TOP_SITES_CHANGED, | |
| 65 content::Source<Profile>(profile_), | |
| 66 content::NotificationService::NoDetails()); | |
| 67 } | |
| 68 | |
| 69 namespace history { | 28 namespace history { |
| 70 | 29 |
| 71 namespace { | 30 namespace { |
| 72 | 31 |
| 73 // Used by WaitForHistory, see it for details. | 32 // Used by WaitForHistory, see it for details. |
| 74 class WaitForHistoryTask : public HistoryDBTask { | 33 class WaitForHistoryTask : public HistoryDBTask { |
| 75 public: | 34 public: |
| 76 WaitForHistoryTask() {} | 35 WaitForHistoryTask() {} |
| 77 | 36 |
| 78 bool RunOnDBThread(HistoryBackend* backend, HistoryDatabase* db) override { | 37 bool RunOnDBThread(HistoryBackend* backend, HistoryDatabase* db) override { |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 public: | 130 public: |
| 172 TopSitesImplTest() | 131 TopSitesImplTest() |
| 173 : ui_thread_(BrowserThread::UI, &message_loop_), | 132 : ui_thread_(BrowserThread::UI, &message_loop_), |
| 174 db_thread_(BrowserThread::DB, &message_loop_) { | 133 db_thread_(BrowserThread::DB, &message_loop_) { |
| 175 } | 134 } |
| 176 | 135 |
| 177 void SetUp() override { | 136 void SetUp() override { |
| 178 profile_.reset(new TestingProfile); | 137 profile_.reset(new TestingProfile); |
| 179 if (CreateHistoryAndTopSites()) { | 138 if (CreateHistoryAndTopSites()) { |
| 180 ASSERT_TRUE(profile_->CreateHistoryService(false, false)); | 139 ASSERT_TRUE(profile_->CreateHistoryService(false, false)); |
| 181 CreateTopSitesAndObserver(); | 140 CreateTopSites(); |
| 182 profile_->BlockUntilTopSitesLoaded(); | 141 profile_->BlockUntilTopSitesLoaded(); |
| 183 } | 142 } |
| 184 } | 143 } |
| 185 | 144 |
| 186 void TearDown() override { | 145 void TearDown() override { |
| 187 top_sites_observer_.reset(); | |
| 188 profile_.reset(); | 146 profile_.reset(); |
| 189 } | 147 } |
| 190 | 148 |
| 191 // Returns true if history and top sites should be created in SetUp. | 149 // Returns true if history and top sites should be created in SetUp. |
| 192 virtual bool CreateHistoryAndTopSites() { | 150 virtual bool CreateHistoryAndTopSites() { |
| 193 return true; | 151 return true; |
| 194 } | 152 } |
| 195 | 153 |
| 196 // Gets the thumbnail for |url| from TopSites. | 154 // Gets the thumbnail for |url| from TopSites. |
| 197 SkBitmap GetThumbnail(const GURL& url) { | 155 SkBitmap GetThumbnail(const GURL& url) { |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 313 bool ThumbnailEqualsBytes(const gfx::Image& image, | 271 bool ThumbnailEqualsBytes(const gfx::Image& image, |
| 314 base::RefCountedMemory* bytes) { | 272 base::RefCountedMemory* bytes) { |
| 315 scoped_refptr<base::RefCountedBytes> encoded_image; | 273 scoped_refptr<base::RefCountedBytes> encoded_image; |
| 316 TopSitesImpl::EncodeBitmap(image, &encoded_image); | 274 TopSitesImpl::EncodeBitmap(image, &encoded_image); |
| 317 return ThumbnailsAreEqual(encoded_image.get(), bytes); | 275 return ThumbnailsAreEqual(encoded_image.get(), bytes); |
| 318 } | 276 } |
| 319 | 277 |
| 320 // Recreates top sites. This forces top sites to reread from the db. | 278 // Recreates top sites. This forces top sites to reread from the db. |
| 321 void RecreateTopSitesAndBlock() { | 279 void RecreateTopSitesAndBlock() { |
| 322 // Recreate TopSites and wait for it to load. | 280 // Recreate TopSites and wait for it to load. |
| 323 CreateTopSitesAndObserver(); | 281 CreateTopSites(); |
| 324 // As history already loaded we have to fake this call. | 282 // As history already loaded we have to fake this call. |
| 325 profile()->BlockUntilTopSitesLoaded(); | 283 profile()->BlockUntilTopSitesLoaded(); |
| 326 } | 284 } |
| 327 | 285 |
| 328 // Wrappers that allow private TopSites functions to be called from the | 286 // Wrappers that allow private TopSites functions to be called from the |
| 329 // individual tests without making them all be friends. | 287 // individual tests without making them all be friends. |
| 330 GURL GetCanonicalURL(const GURL& url) { | 288 GURL GetCanonicalURL(const GURL& url) { |
| 331 return top_sites()->cache_->GetCanonicalURL(url); | 289 return top_sites()->cache_->GetCanonicalURL(url); |
| 332 } | 290 } |
| 333 | 291 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 358 bool AddPrepopulatedPages(MostVisitedURLList* urls) { | 316 bool AddPrepopulatedPages(MostVisitedURLList* urls) { |
| 359 return top_sites()->AddPrepopulatedPages(urls, 0u); | 317 return top_sites()->AddPrepopulatedPages(urls, 0u); |
| 360 } | 318 } |
| 361 | 319 |
| 362 void EmptyThreadSafeCache() { | 320 void EmptyThreadSafeCache() { |
| 363 base::AutoLock lock(top_sites()->lock_); | 321 base::AutoLock lock(top_sites()->lock_); |
| 364 MostVisitedURLList empty; | 322 MostVisitedURLList empty; |
| 365 top_sites()->thread_safe_cache_->SetTopSites(empty); | 323 top_sites()->thread_safe_cache_->SetTopSites(empty); |
| 366 } | 324 } |
| 367 | 325 |
| 368 void CreateTopSitesAndObserver() { | 326 void CreateTopSites() { profile_->CreateTopSites(); } |
| 369 if (top_sites_observer_) | |
| 370 top_sites_observer_.reset(); | |
| 371 | |
| 372 profile_->CreateTopSites(); | |
| 373 top_sites_observer_.reset( | |
| 374 new TestTopSitesObserver(profile_.get(), profile_->GetTopSites())); | |
| 375 } | |
| 376 | 327 |
| 377 private: | 328 private: |
| 378 base::MessageLoopForUI message_loop_; | 329 base::MessageLoopForUI message_loop_; |
| 379 content::TestBrowserThread ui_thread_; | 330 content::TestBrowserThread ui_thread_; |
| 380 content::TestBrowserThread db_thread_; | 331 content::TestBrowserThread db_thread_; |
| 381 scoped_ptr<TestingProfile> profile_; | 332 scoped_ptr<TestingProfile> profile_; |
| 382 scoped_ptr<TestTopSitesObserver> top_sites_observer_; | |
| 383 // To cancel HistoryService tasks. | 333 // To cancel HistoryService tasks. |
| 384 base::CancelableTaskTracker history_tracker_; | 334 base::CancelableTaskTracker history_tracker_; |
| 385 | 335 |
| 386 // To cancel TopSitesBackend tasks. | 336 // To cancel TopSitesBackend tasks. |
| 387 base::CancelableTaskTracker top_sites_tracker_; | 337 base::CancelableTaskTracker top_sites_tracker_; |
| 388 | 338 |
| 389 DISALLOW_COPY_AND_ASSIGN(TopSitesImplTest); | 339 DISALLOW_COPY_AND_ASSIGN(TopSitesImplTest); |
| 390 }; // Class TopSitesImplTest | 340 }; // Class TopSitesImplTest |
| 391 | 341 |
| 392 // Helper function for appending a URL to a vector of "most visited" URLs, | 342 // Helper function for appending a URL to a vector of "most visited" URLs, |
| (...skipping 630 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1023 EXPECT_EQ(52, GetUpdateDelay().InMinutes()); | 973 EXPECT_EQ(52, GetUpdateDelay().InMinutes()); |
| 1024 | 974 |
| 1025 SetLastNumUrlsChanged(20); | 975 SetLastNumUrlsChanged(20); |
| 1026 EXPECT_EQ(1, GetUpdateDelay().InMinutes()); | 976 EXPECT_EQ(1, GetUpdateDelay().InMinutes()); |
| 1027 } | 977 } |
| 1028 | 978 |
| 1029 // Verifies that callbacks are notified correctly if requested before top sites | 979 // Verifies that callbacks are notified correctly if requested before top sites |
| 1030 // has loaded. | 980 // has loaded. |
| 1031 TEST_F(TopSitesImplTest, NotifyCallbacksWhenLoaded) { | 981 TEST_F(TopSitesImplTest, NotifyCallbacksWhenLoaded) { |
| 1032 // Recreate top sites. It won't be loaded now. | 982 // Recreate top sites. It won't be loaded now. |
| 1033 CreateTopSitesAndObserver(); | 983 CreateTopSites(); |
| 1034 | 984 |
| 1035 EXPECT_FALSE(IsTopSitesLoaded()); | 985 EXPECT_FALSE(IsTopSitesLoaded()); |
| 1036 | 986 |
| 1037 TopSitesQuerier querier1; | 987 TopSitesQuerier querier1; |
| 1038 TopSitesQuerier querier2; | 988 TopSitesQuerier querier2; |
| 1039 TopSitesQuerier querier3; | 989 TopSitesQuerier querier3; |
| 1040 | 990 |
| 1041 // Starts the queries. | 991 // Starts the queries. |
| 1042 querier1.QueryTopSites(top_sites(), false); | 992 querier1.QueryTopSites(top_sites(), false); |
| 1043 querier2.QueryTopSites(top_sites(), false); | 993 querier2.QueryTopSites(top_sites(), false); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 1064 MostVisitedURL url; | 1014 MostVisitedURL url; |
| 1065 url.url = GURL("http://1.com/"); | 1015 url.url = GURL("http://1.com/"); |
| 1066 url.redirects.push_back(url.url); | 1016 url.redirects.push_back(url.url); |
| 1067 pages.push_back(url); | 1017 pages.push_back(url); |
| 1068 url.url = GURL("http://2.com/"); | 1018 url.url = GURL("http://2.com/"); |
| 1069 url.redirects.push_back(url.url); | 1019 url.redirects.push_back(url.url); |
| 1070 pages.push_back(url); | 1020 pages.push_back(url); |
| 1071 SetTopSites(pages); | 1021 SetTopSites(pages); |
| 1072 | 1022 |
| 1073 // Recreate top sites. It won't be loaded now. | 1023 // Recreate top sites. It won't be loaded now. |
| 1074 CreateTopSitesAndObserver(); | 1024 CreateTopSites(); |
| 1075 | 1025 |
| 1076 EXPECT_FALSE(IsTopSitesLoaded()); | 1026 EXPECT_FALSE(IsTopSitesLoaded()); |
| 1077 | 1027 |
| 1078 TopSitesQuerier querier4; | 1028 TopSitesQuerier querier4; |
| 1079 | 1029 |
| 1080 // Query again. | 1030 // Query again. |
| 1081 querier4.QueryTopSites(top_sites(), false); | 1031 querier4.QueryTopSites(top_sites(), false); |
| 1082 | 1032 |
| 1083 // We shouldn't have gotten a callback. | 1033 // We shouldn't have gotten a callback. |
| 1084 EXPECT_EQ(0, querier4.number_of_callbacks()); | 1034 EXPECT_EQ(0, querier4.number_of_callbacks()); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 1109 ASSERT_EQ(3u + GetPrepopulatePages().size(), querier5.urls().size()); | 1059 ASSERT_EQ(3u + GetPrepopulatePages().size(), querier5.urls().size()); |
| 1110 EXPECT_EQ("http://1.com/", querier5.urls()[0].url.spec()); | 1060 EXPECT_EQ("http://1.com/", querier5.urls()[0].url.spec()); |
| 1111 EXPECT_EQ("http://2.com/", querier5.urls()[1].url.spec()); | 1061 EXPECT_EQ("http://2.com/", querier5.urls()[1].url.spec()); |
| 1112 EXPECT_EQ("http://3.com/", querier5.urls()[2].url.spec()); | 1062 EXPECT_EQ("http://3.com/", querier5.urls()[2].url.spec()); |
| 1113 ASSERT_NO_FATAL_FAILURE(ContainsPrepopulatePages(querier5, 3)); | 1063 ASSERT_NO_FATAL_FAILURE(ContainsPrepopulatePages(querier5, 3)); |
| 1114 } | 1064 } |
| 1115 | 1065 |
| 1116 // Makes sure canceled requests are not notified. | 1066 // Makes sure canceled requests are not notified. |
| 1117 TEST_F(TopSitesImplTest, CancelingRequestsForTopSites) { | 1067 TEST_F(TopSitesImplTest, CancelingRequestsForTopSites) { |
| 1118 // Recreate top sites. It won't be loaded now. | 1068 // Recreate top sites. It won't be loaded now. |
| 1119 CreateTopSitesAndObserver(); | 1069 CreateTopSites(); |
| 1120 | 1070 |
| 1121 EXPECT_FALSE(IsTopSitesLoaded()); | 1071 EXPECT_FALSE(IsTopSitesLoaded()); |
| 1122 | 1072 |
| 1123 TopSitesQuerier querier1; | 1073 TopSitesQuerier querier1; |
| 1124 TopSitesQuerier querier2; | 1074 TopSitesQuerier querier2; |
| 1125 | 1075 |
| 1126 // Starts the queries. | 1076 // Starts the queries. |
| 1127 querier1.QueryTopSites(top_sites(), false); | 1077 querier1.QueryTopSites(top_sites(), false); |
| 1128 querier2.QueryTopSites(top_sites(), false); | 1078 querier2.QueryTopSites(top_sites(), false); |
| 1129 | 1079 |
| (...skipping 552 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1682 ASSERT_EQ(orig_thumbnail.getSize(), thumbnail.getSize()); | 1632 ASSERT_EQ(orig_thumbnail.getSize(), thumbnail.getSize()); |
| 1683 orig_thumbnail.lockPixels(); | 1633 orig_thumbnail.lockPixels(); |
| 1684 thumbnail.lockPixels(); | 1634 thumbnail.lockPixels(); |
| 1685 EXPECT_EQ(0, memcmp(orig_thumbnail.getPixels(), thumbnail.getPixels(), | 1635 EXPECT_EQ(0, memcmp(orig_thumbnail.getPixels(), thumbnail.getPixels(), |
| 1686 orig_thumbnail.getSize())); | 1636 orig_thumbnail.getSize())); |
| 1687 thumbnail.unlockPixels(); | 1637 thumbnail.unlockPixels(); |
| 1688 orig_thumbnail.unlockPixels(); | 1638 orig_thumbnail.unlockPixels(); |
| 1689 } | 1639 } |
| 1690 | 1640 |
| 1691 } // namespace history | 1641 } // namespace history |
| OLD | NEW |