Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(293)

Side by Side Diff: components/ntp_tiles/most_visited_sites_unittest.cc

Issue 2866033002: [NTP Tiles] Fetch missing MostLikely tiles from a Google server (Closed)
Patch Set: Comments #3 Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 "components/ntp_tiles/most_visited_sites.h" 5 #include "components/ntp_tiles/most_visited_sites.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <memory> 9 #include <memory>
10 #include <ostream> 10 #include <ostream>
11 #include <string> 11 #include <string>
12 #include <utility> 12 #include <utility>
13 #include <vector> 13 #include <vector>
14 14
15 #include "base/callback_list.h" 15 #include "base/callback_list.h"
16 #include "base/command_line.h" 16 #include "base/command_line.h"
17 #include "base/macros.h" 17 #include "base/macros.h"
18 #include "base/memory/ptr_util.h" 18 #include "base/memory/ptr_util.h"
19 #include "base/message_loop/message_loop.h" 19 #include "base/message_loop/message_loop.h"
20 #include "base/run_loop.h" 20 #include "base/run_loop.h"
21 #include "base/strings/utf_string_conversions.h" 21 #include "base/strings/utf_string_conversions.h"
22 #include "base/task/cancelable_task_tracker.h" 22 #include "base/task/cancelable_task_tracker.h"
23 #include "base/test/scoped_feature_list.h"
23 #include "base/test/sequenced_worker_pool_owner.h" 24 #include "base/test/sequenced_worker_pool_owner.h"
24 #include "base/threading/thread_task_runner_handle.h" 25 #include "base/threading/thread_task_runner_handle.h"
25 #include "components/history/core/browser/top_sites.h" 26 #include "components/history/core/browser/top_sites.h"
26 #include "components/history/core/browser/top_sites_observer.h" 27 #include "components/history/core/browser/top_sites_observer.h"
28 #include "components/ntp_tiles/constants.h"
27 #include "components/ntp_tiles/icon_cacher.h" 29 #include "components/ntp_tiles/icon_cacher.h"
28 #include "components/ntp_tiles/json_unsafe_parser.h" 30 #include "components/ntp_tiles/json_unsafe_parser.h"
29 #include "components/ntp_tiles/popular_sites_impl.h" 31 #include "components/ntp_tiles/popular_sites_impl.h"
30 #include "components/ntp_tiles/pref_names.h" 32 #include "components/ntp_tiles/pref_names.h"
31 #include "components/ntp_tiles/switches.h" 33 #include "components/ntp_tiles/switches.h"
32 #include "components/sync_preferences/testing_pref_service_syncable.h" 34 #include "components/sync_preferences/testing_pref_service_syncable.h"
33 #include "net/url_request/test_url_fetcher_factory.h" 35 #include "net/url_request/test_url_fetcher_factory.h"
34 #include "net/url_request/url_request_test_util.h" 36 #include "net/url_request/url_request_test_util.h"
35 #include "testing/gmock/include/gmock/gmock.h" 37 #include "testing/gmock/include/gmock/gmock.h"
36 #include "testing/gtest/include/gtest/gtest.h" 38 #include "testing/gtest/include/gtest/gtest.h"
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after
187 }; 189 };
188 190
189 class MockMostVisitedSitesObserver : public MostVisitedSites::Observer { 191 class MockMostVisitedSitesObserver : public MostVisitedSites::Observer {
190 public: 192 public:
191 MOCK_METHOD1(OnMostVisitedURLsAvailable, void(const NTPTilesVector& tiles)); 193 MOCK_METHOD1(OnMostVisitedURLsAvailable, void(const NTPTilesVector& tiles));
192 MOCK_METHOD1(OnIconMadeAvailable, void(const GURL& site_url)); 194 MOCK_METHOD1(OnIconMadeAvailable, void(const GURL& site_url));
193 }; 195 };
194 196
195 class MockIconCacher : public IconCacher { 197 class MockIconCacher : public IconCacher {
196 public: 198 public:
197 MOCK_METHOD3(StartFetch, 199 MOCK_METHOD3(StartFetchPopularSites,
198 void(PopularSites::Site site, 200 void(PopularSites::Site site,
199 const base::Closure& icon_available, 201 const base::Closure& icon_available,
200 const base::Closure& preliminary_icon_available)); 202 const base::Closure& preliminary_icon_available));
203 MOCK_METHOD2(StartFetchMostLikely,
204 void(const GURL& page_url, const base::Closure& icon_available));
201 }; 205 };
202 206
203 class PopularSitesFactoryForTest { 207 class PopularSitesFactoryForTest {
204 public: 208 public:
205 PopularSitesFactoryForTest( 209 PopularSitesFactoryForTest(
206 bool enabled, 210 bool enabled,
207 sync_preferences::TestingPrefServiceSyncable* pref_service) 211 sync_preferences::TestingPrefServiceSyncable* pref_service)
208 : prefs_(pref_service), 212 : prefs_(pref_service),
209 url_fetcher_factory_(/*default_factory=*/nullptr), 213 url_fetcher_factory_(/*default_factory=*/nullptr),
210 url_request_context_(new net::TestURLRequestContextGetter( 214 url_request_context_(new net::TestURLRequestContextGetter(
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
280 MostVisitedSites::RegisterProfilePrefs(pref_service_.registry()); 284 MostVisitedSites::RegisterProfilePrefs(pref_service_.registry());
281 285
282 if (IsPopularSitesEnabledViaVariations()) { 286 if (IsPopularSitesEnabledViaVariations()) {
283 base::CommandLine::ForCurrentProcess()->AppendSwitch( 287 base::CommandLine::ForCurrentProcess()->AppendSwitch(
284 switches::kEnableNTPPopularSites); 288 switches::kEnableNTPPopularSites);
285 } else { 289 } else {
286 base::CommandLine::ForCurrentProcess()->AppendSwitch( 290 base::CommandLine::ForCurrentProcess()->AppendSwitch(
287 switches::kDisableNTPPopularSites); 291 switches::kDisableNTPPopularSites);
288 } 292 }
289 293
294 // Disable in most tests, this is overriden in a specific test.
295 feature_list_.InitAndDisableFeature(
296 kNtpMostLikelyFaviconsFromServerFeature);
297
290 // We use StrictMock to make sure the object is not used unless Popular 298 // We use StrictMock to make sure the object is not used unless Popular
291 // Sites is enabled. 299 // Sites is enabled.
292 auto icon_cacher = base::MakeUnique<StrictMock<MockIconCacher>>(); 300 auto icon_cacher = base::MakeUnique<StrictMock<MockIconCacher>>();
301 icon_cacher_ = icon_cacher.get();
293 302
294 if (IsPopularSitesEnabledViaVariations()) { 303 if (IsPopularSitesEnabledViaVariations()) {
295 // Populate Popular Sites' internal cache by mimicking a past usage of 304 // Populate Popular Sites' internal cache by mimicking a past usage of
296 // PopularSitesImpl. 305 // PopularSitesImpl.
297 auto tmp_popular_sites = popular_sites_factory_.New(); 306 auto tmp_popular_sites = popular_sites_factory_.New();
298 base::RunLoop loop; 307 base::RunLoop loop;
299 bool save_success = false; 308 bool save_success = false;
300 tmp_popular_sites->MaybeStartFetch( 309 tmp_popular_sites->MaybeStartFetch(
301 /*force_download=*/true, 310 /*force_download=*/true,
302 base::Bind( 311 base::Bind(
303 [](bool* save_success, base::RunLoop* loop, bool success) { 312 [](bool* save_success, base::RunLoop* loop, bool success) {
304 *save_success = success; 313 *save_success = success;
305 loop->Quit(); 314 loop->Quit();
306 }, 315 },
307 &save_success, &loop)); 316 &save_success, &loop));
308 loop.Run(); 317 loop.Run();
309 EXPECT_TRUE(save_success); 318 EXPECT_TRUE(save_success);
310 319
311 // With PopularSites enabled, blacklist is exercised. 320 // With PopularSites enabled, blacklist is exercised.
312 EXPECT_CALL(*mock_top_sites_, IsBlacklisted(_)) 321 EXPECT_CALL(*mock_top_sites_, IsBlacklisted(_))
313 .WillRepeatedly(Return(false)); 322 .WillRepeatedly(Return(false));
314 // Mock icon cacher never replies, and we also don't verify whether the 323 // Mock icon cacher never replies, and we also don't verify whether the
315 // code uses it correctly. 324 // code uses it correctly.
316 EXPECT_CALL(*icon_cacher, StartFetch(_, _, _)).Times(AtLeast(0)); 325 EXPECT_CALL(*icon_cacher, StartFetchPopularSites(_, _, _))
326 .Times(AtLeast(0));
317 } 327 }
318 328
319 most_visited_sites_ = base::MakeUnique<MostVisitedSites>( 329 most_visited_sites_ = base::MakeUnique<MostVisitedSites>(
320 &pref_service_, mock_top_sites_, &mock_suggestions_service_, 330 &pref_service_, mock_top_sites_, &mock_suggestions_service_,
321 popular_sites_factory_.New(), std::move(icon_cacher), 331 popular_sites_factory_.New(), std::move(icon_cacher),
322 /*supervisor=*/nullptr); 332 /*supervisor=*/nullptr);
323 } 333 }
324 334
325 bool IsPopularSitesEnabledViaVariations() const { return GetParam(); } 335 bool IsPopularSitesEnabledViaVariations() const { return GetParam(); }
326 336
(...skipping 15 matching lines...) Expand all
342 suggestions_service_callbacks_; 352 suggestions_service_callbacks_;
343 TopSitesCallbackList top_sites_callbacks_; 353 TopSitesCallbackList top_sites_callbacks_;
344 354
345 base::MessageLoop message_loop_; 355 base::MessageLoop message_loop_;
346 sync_preferences::TestingPrefServiceSyncable pref_service_; 356 sync_preferences::TestingPrefServiceSyncable pref_service_;
347 PopularSitesFactoryForTest popular_sites_factory_; 357 PopularSitesFactoryForTest popular_sites_factory_;
348 scoped_refptr<StrictMock<MockTopSites>> mock_top_sites_; 358 scoped_refptr<StrictMock<MockTopSites>> mock_top_sites_;
349 StrictMock<MockSuggestionsService> mock_suggestions_service_; 359 StrictMock<MockSuggestionsService> mock_suggestions_service_;
350 StrictMock<MockMostVisitedSitesObserver> mock_observer_; 360 StrictMock<MockMostVisitedSitesObserver> mock_observer_;
351 std::unique_ptr<MostVisitedSites> most_visited_sites_; 361 std::unique_ptr<MostVisitedSites> most_visited_sites_;
362 base::test::ScopedFeatureList feature_list_;
363 MockIconCacher* icon_cacher_;
352 }; 364 };
353 365
354 TEST_P(MostVisitedSitesTest, ShouldStartNoCallInConstructor) { 366 TEST_P(MostVisitedSitesTest, ShouldStartNoCallInConstructor) {
355 // No call to mocks expected by the mere fact of instantiating 367 // No call to mocks expected by the mere fact of instantiating
356 // MostVisitedSites. 368 // MostVisitedSites.
357 base::RunLoop().RunUntilIdle(); 369 base::RunLoop().RunUntilIdle();
358 } 370 }
359 371
360 TEST_P(MostVisitedSitesTest, ShouldHandleTopSitesCacheHit) { 372 TEST_P(MostVisitedSitesTest, ShouldHandleTopSitesCacheHit) {
361 // If cached, TopSites returns the tiles synchronously, running the callback 373 // If cached, TopSites returns the tiles synchronously, running the callback
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after
533 MatchesTile("Site 6", "http://site6/", TileSource::TOP_SITES), 545 MatchesTile("Site 6", "http://site6/", TileSource::TOP_SITES),
534 MatchesTile("Site 7", "http://site7/", TileSource::TOP_SITES)))); 546 MatchesTile("Site 7", "http://site7/", TileSource::TOP_SITES))));
535 top_sites_callbacks_.ClearAndNotify( 547 top_sites_callbacks_.ClearAndNotify(
536 {MakeMostVisitedURL("Site 4", "http://site4/"), 548 {MakeMostVisitedURL("Site 4", "http://site4/"),
537 MakeMostVisitedURL("Site 5", "http://site5/"), 549 MakeMostVisitedURL("Site 5", "http://site5/"),
538 MakeMostVisitedURL("Site 6", "http://site6/"), 550 MakeMostVisitedURL("Site 6", "http://site6/"),
539 MakeMostVisitedURL("Site 7", "http://site7/")}); 551 MakeMostVisitedURL("Site 7", "http://site7/")});
540 base::RunLoop().RunUntilIdle(); 552 base::RunLoop().RunUntilIdle();
541 } 553 }
542 554
555 TEST_P(MostVisitedSitesWithCacheHitTest, ShouldFetchFaviconsIfEnabled) {
556 base::test::ScopedFeatureList feature_list;
557 feature_list.InitAndEnableFeature(kNtpMostLikelyFaviconsFromServerFeature);
558
559 EXPECT_CALL(mock_observer_, OnMostVisitedURLsAvailable(_));
560 EXPECT_CALL(*icon_cacher_, StartFetchMostLikely(GURL("http://site4/"), _));
561
562 suggestions_service_callbacks_.Notify(
563 MakeProfile({MakeSuggestion("Site 4", "http://site4/")}));
564 base::RunLoop().RunUntilIdle();
565 }
566
543 INSTANTIATE_TEST_CASE_P(MostVisitedSitesWithCacheHitTest, 567 INSTANTIATE_TEST_CASE_P(MostVisitedSitesWithCacheHitTest,
544 MostVisitedSitesWithCacheHitTest, 568 MostVisitedSitesWithCacheHitTest,
545 ::testing::Bool()); 569 ::testing::Bool());
546 570
547 class MostVisitedSitesWithEmptyCacheTest : public MostVisitedSitesTest { 571 class MostVisitedSitesWithEmptyCacheTest : public MostVisitedSitesTest {
548 public: 572 public:
549 // Constructor sets the common expectations for the case where suggestions 573 // Constructor sets the common expectations for the case where suggestions
550 // service doesn't have cached results when the observer is registered. 574 // service doesn't have cached results when the observer is registered.
551 MostVisitedSitesWithEmptyCacheTest() { 575 MostVisitedSitesWithEmptyCacheTest() {
552 InSequence seq; 576 InSequence seq;
(...skipping 322 matching lines...) Expand 10 before | Expand all | Expand 10 after
875 TileSource::TOP_SITES), 899 TileSource::TOP_SITES),
876 MatchesTile("Site 4", "https://www.site4.com/", 900 MatchesTile("Site 4", "https://www.site4.com/",
877 TileSource::TOP_SITES), 901 TileSource::TOP_SITES),
878 MatchesTile("Site 1", "https://www.site1.com/", TileSource::POPULAR), 902 MatchesTile("Site 1", "https://www.site1.com/", TileSource::POPULAR),
879 MatchesTile("Site 2", "https://www.site2.com/", 903 MatchesTile("Site 2", "https://www.site2.com/",
880 TileSource::POPULAR))); 904 TileSource::POPULAR)));
881 } 905 }
882 906
883 } // namespace 907 } // namespace
884 } // namespace ntp_tiles 908 } // namespace ntp_tiles
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698