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