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