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

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

Issue 2619993002: ntp_tiles: Migrate to multi-observer model
Patch Set: Fixed build. Created 3 years, 11 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>
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after
200 bool empty() const { return callbacks_.empty(); } 200 bool empty() const { return callbacks_.empty(); }
201 201
202 private: 202 private:
203 std::vector<TopSites::GetMostVisitedURLsCallback> callbacks_; 203 std::vector<TopSites::GetMostVisitedURLsCallback> callbacks_;
204 }; 204 };
205 205
206 class MostVisitedSitesTest : public ::testing::Test { 206 class MostVisitedSitesTest : public ::testing::Test {
207 protected: 207 protected:
208 MostVisitedSitesTest() 208 MostVisitedSitesTest()
209 : mock_top_sites_(new StrictMock<MockTopSites>()), 209 : mock_top_sites_(new StrictMock<MockTopSites>()),
210 mock_popular_sites_(new StrictMock<MockPopularSites>()), 210 mock_popular_sites_(new StrictMock<MockPopularSites>()) {
211 most_visited_sites_(&pref_service_,
212 mock_top_sites_,
213 &mock_suggestions_service_,
214 base::WrapUnique(mock_popular_sites_),
215 /*icon_cacher=*/nullptr,
216 /*supervisor=*/nullptr) {
217 MostVisitedSites::RegisterProfilePrefs(pref_service_.registry()); 211 MostVisitedSites::RegisterProfilePrefs(pref_service_.registry());
218 // TODO(mastiz): Add test coverage including Popular Sites. 212 // TODO(mastiz): Add test coverage including Popular Sites.
219 base::CommandLine::ForCurrentProcess()->AppendSwitch( 213 base::CommandLine::ForCurrentProcess()->AppendSwitch(
220 switches::kDisableNTPPopularSites); 214 switches::kDisableNTPPopularSites);
221 // PopularSites::sites() might be called even if the feature is disabled. 215 // PopularSites::sites() might be called even if the feature is disabled.
222 // An empty vector is returned because there was no actual fetch. 216 // An empty vector is returned because there was no actual fetch.
223 EXPECT_CALL(*mock_popular_sites_, sites()) 217 EXPECT_CALL(*mock_popular_sites_, sites())
224 .Times(AtLeast(0)) 218 .Times(AtLeast(0))
225 .WillRepeatedly(ReturnRef(empty_popular_sites_vector_)); 219 .WillRepeatedly(ReturnRef(empty_popular_sites_vector_));
220 EXPECT_CALL(mock_suggestions_service_, AddCallback(_))
221 .WillOnce(Invoke(&suggestions_service_callbacks_,
222 &SuggestionsService::ResponseCallbackList::Add));
223
224 most_visited_sites_ =
225 base::WrapUnique<MostVisitedSites>(new MostVisitedSites(
226 &pref_service_, mock_top_sites_, &mock_suggestions_service_,
227 base::WrapUnique(mock_popular_sites_),
228 /*icon_cacher=*/nullptr,
229 /*supervisor=*/nullptr));
230 most_visited_sites_->AddObserver(&mock_observer_);
231 VerifyAndClearExpectations();
232
233 EXPECT_FALSE(suggestions_service_callbacks_.empty());
226 } 234 }
227 235
228 bool VerifyAndClearExpectations() { 236 bool VerifyAndClearExpectations() {
229 base::RunLoop().RunUntilIdle(); 237 base::RunLoop().RunUntilIdle();
230 // Note that we don't verify or clear mock_popular_sites_, since there's 238 // Note that we don't verify or clear mock_popular_sites_, since there's
231 // no interaction expected except sites() possibly being called, which is 239 // no interaction expected except sites() possibly being called, which is
232 // verified during teardown. 240 // verified during teardown.
233 return Mock::VerifyAndClearExpectations(mock_top_sites_.get()) && 241 return Mock::VerifyAndClearExpectations(mock_top_sites_.get()) &&
234 Mock::VerifyAndClearExpectations(&mock_suggestions_service_) && 242 Mock::VerifyAndClearExpectations(&mock_suggestions_service_) &&
235 Mock::VerifyAndClearExpectations(&mock_observer_); 243 Mock::VerifyAndClearExpectations(&mock_observer_);
236 } 244 }
237 245
238 base::CallbackList<SuggestionsService::ResponseCallback::RunType> 246 base::CallbackList<SuggestionsService::ResponseCallback::RunType>
239 suggestions_service_callbacks_; 247 suggestions_service_callbacks_;
240 TopSitesCallbackList top_sites_callbacks_; 248 TopSitesCallbackList top_sites_callbacks_;
241 249
242 base::MessageLoop message_loop_; 250 base::MessageLoop message_loop_;
243 sync_preferences::TestingPrefServiceSyncable pref_service_; 251 sync_preferences::TestingPrefServiceSyncable pref_service_;
244 scoped_refptr<StrictMock<MockTopSites>> mock_top_sites_; 252 scoped_refptr<StrictMock<MockTopSites>> mock_top_sites_;
245 StrictMock<MockSuggestionsService> mock_suggestions_service_; 253 StrictMock<MockSuggestionsService> mock_suggestions_service_;
246 StrictMock<MockPopularSites>* const mock_popular_sites_; 254 StrictMock<MockPopularSites>* const mock_popular_sites_;
247 StrictMock<MockMostVisitedSitesObserver> mock_observer_; 255 StrictMock<MockMostVisitedSitesObserver> mock_observer_;
248 MostVisitedSites most_visited_sites_; 256 std::unique_ptr<MostVisitedSites> most_visited_sites_;
249 const PopularSites::SitesVector empty_popular_sites_vector_; 257 const PopularSites::SitesVector empty_popular_sites_vector_;
250 }; 258 };
251 259
252 TEST_F(MostVisitedSitesTest, ShouldStartNoCallInConstructor) { 260 TEST_F(MostVisitedSitesTest, ShouldRegisterCallbacksInConstructor) {
253 // No call to mocks expected by the mere fact of instantiating 261 EXPECT_CALL(mock_observer_, OnMostVisitedURLsAvailable(_));
254 // MostVisitedSites. 262 suggestions_service_callbacks_.Notify(
263 MakeProfile({MakeSuggestion("Site 1", "http://site1/")}));
255 base::RunLoop().RunUntilIdle(); 264 base::RunLoop().RunUntilIdle();
256 } 265 }
257 266
258 class MostVisitedSitesWithCacheHitTest : public MostVisitedSitesTest { 267 class MostVisitedSitesWithCacheHitTest : public MostVisitedSitesTest {
259 public: 268 public:
260 // Constructor sets the common expectations for the case where suggestions 269 // Constructor sets the common expectations for the case where suggestions
261 // service has cached results when the observer is registered. 270 // service has cached results when the observer is registered.
262 MostVisitedSitesWithCacheHitTest() { 271 MostVisitedSitesWithCacheHitTest() {
263 InSequence seq; 272 InSequence seq;
264 EXPECT_CALL(*mock_top_sites_, SyncWithHistory()); 273 EXPECT_CALL(*mock_top_sites_, SyncWithHistory());
265 EXPECT_CALL(mock_suggestions_service_, AddCallback(_))
266 .WillOnce(Invoke(&suggestions_service_callbacks_,
267 &SuggestionsService::ResponseCallbackList::Add));
268 EXPECT_CALL(mock_suggestions_service_, GetSuggestionsDataFromCache()) 274 EXPECT_CALL(mock_suggestions_service_, GetSuggestionsDataFromCache())
269 .WillOnce(Return(MakeProfile({ 275 .WillOnce(Return(MakeProfile({
270 MakeSuggestion("Site 1", "http://site1/"), 276 MakeSuggestion("Site 1", "http://site1/"),
271 MakeSuggestion("Site 2", "http://site2/"), 277 MakeSuggestion("Site 2", "http://site2/"),
272 MakeSuggestion("Site 3", "http://site3/"), 278 MakeSuggestion("Site 3", "http://site3/"),
273 MakeSuggestion("Site 4", "http://site4/"), 279 MakeSuggestion("Site 4", "http://site4/"),
274 }))); 280 })));
275 EXPECT_CALL(mock_observer_, 281 EXPECT_CALL(mock_observer_,
276 OnMostVisitedURLsAvailable(ElementsAre( 282 OnMostVisitedURLsAvailable(ElementsAre(
277 MatchesTile("Site 1", "http://site1/", 283 MatchesTile("Site 1", "http://site1/",
278 NTPTileSource::SUGGESTIONS_SERVICE), 284 NTPTileSource::SUGGESTIONS_SERVICE),
279 MatchesTile("Site 2", "http://site2/", 285 MatchesTile("Site 2", "http://site2/",
280 NTPTileSource::SUGGESTIONS_SERVICE), 286 NTPTileSource::SUGGESTIONS_SERVICE),
281 MatchesTile("Site 3", "http://site3/", 287 MatchesTile("Site 3", "http://site3/",
288 NTPTileSource::SUGGESTIONS_SERVICE),
289 MatchesTile("Site 4", "http://site4/",
282 NTPTileSource::SUGGESTIONS_SERVICE)))); 290 NTPTileSource::SUGGESTIONS_SERVICE))));
283 EXPECT_CALL(mock_suggestions_service_, FetchSuggestionsData()) 291 EXPECT_CALL(mock_suggestions_service_, FetchSuggestionsData())
284 .WillOnce(Return(true)); 292 .WillOnce(Return(true));
285 most_visited_sites_.SetMostVisitedURLsObserver(&mock_observer_, 293 most_visited_sites_->Refresh();
286 /*num_sites=*/3);
287 VerifyAndClearExpectations(); 294 VerifyAndClearExpectations();
288 295
289 EXPECT_FALSE(suggestions_service_callbacks_.empty()); 296 EXPECT_FALSE(suggestions_service_callbacks_.empty());
290 EXPECT_TRUE(top_sites_callbacks_.empty()); 297 EXPECT_TRUE(top_sites_callbacks_.empty());
291 } 298 }
292 }; 299 };
293 300
294 TEST_F(MostVisitedSitesWithCacheHitTest, ShouldFavorSuggestionsServiceCache) { 301 TEST_F(MostVisitedSitesWithCacheHitTest, ShouldFavorSuggestionsServiceCache) {
295 // Constructor sets basic expectations for a suggestions service cache hit. 302 // Constructor sets basic expectations for a suggestions service cache hit.
296 } 303 }
(...skipping 24 matching lines...) Expand all
321 base::RunLoop().RunUntilIdle(); 328 base::RunLoop().RunUntilIdle();
322 } 329 }
323 330
324 class MostVisitedSitesWithEmptyCacheTest : public MostVisitedSitesTest { 331 class MostVisitedSitesWithEmptyCacheTest : public MostVisitedSitesTest {
325 public: 332 public:
326 // Constructor sets the common expectations for the case where suggestions 333 // Constructor sets the common expectations for the case where suggestions
327 // service doesn't have cached results when the observer is registered. 334 // service doesn't have cached results when the observer is registered.
328 MostVisitedSitesWithEmptyCacheTest() { 335 MostVisitedSitesWithEmptyCacheTest() {
329 InSequence seq; 336 InSequence seq;
330 EXPECT_CALL(*mock_top_sites_, SyncWithHistory()); 337 EXPECT_CALL(*mock_top_sites_, SyncWithHistory());
331 EXPECT_CALL(mock_suggestions_service_, AddCallback(_))
332 .WillOnce(Invoke(&suggestions_service_callbacks_,
333 &SuggestionsService::ResponseCallbackList::Add));
334 EXPECT_CALL(mock_suggestions_service_, GetSuggestionsDataFromCache()) 338 EXPECT_CALL(mock_suggestions_service_, GetSuggestionsDataFromCache())
335 .WillOnce(Return(SuggestionsProfile())); // Empty cache. 339 .WillOnce(Return(SuggestionsProfile())); // Empty cache.
336 EXPECT_CALL(*mock_top_sites_, GetMostVisitedURLs(_, false)) 340 EXPECT_CALL(*mock_top_sites_, GetMostVisitedURLs(_, false))
337 .WillOnce(Invoke(&top_sites_callbacks_, &TopSitesCallbackList::Add)); 341 .WillOnce(Invoke(&top_sites_callbacks_, &TopSitesCallbackList::Add));
338 EXPECT_CALL(mock_suggestions_service_, FetchSuggestionsData()) 342 EXPECT_CALL(mock_suggestions_service_, FetchSuggestionsData())
339 .WillOnce(Return(true)); 343 .WillOnce(Return(true));
340 most_visited_sites_.SetMostVisitedURLsObserver(&mock_observer_, 344 most_visited_sites_->Refresh();
341 /*num_sites=*/3);
342 VerifyAndClearExpectations(); 345 VerifyAndClearExpectations();
343 346
344 EXPECT_FALSE(suggestions_service_callbacks_.empty());
345 EXPECT_FALSE(top_sites_callbacks_.empty()); 347 EXPECT_FALSE(top_sites_callbacks_.empty());
346 } 348 }
347 }; 349 };
348 350
349 TEST_F(MostVisitedSitesWithEmptyCacheTest, 351 TEST_F(MostVisitedSitesWithEmptyCacheTest,
350 ShouldQueryTopSitesAndSuggestionsService) { 352 ShouldQueryTopSitesAndSuggestionsService) {
351 // Constructor sets basic expectations for a suggestions service cache miss. 353 // Constructor sets basic expectations for a suggestions service cache miss.
352 } 354 }
353 355
354 TEST_F(MostVisitedSitesWithEmptyCacheTest, 356 TEST_F(MostVisitedSitesWithEmptyCacheTest,
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after
520 MatchesTile("Site 4", "https://www.site4.com/", 522 MatchesTile("Site 4", "https://www.site4.com/",
521 NTPTileSource::TOP_SITES), 523 NTPTileSource::TOP_SITES),
522 MatchesTile("Site 1", "https://www.site1.com/", 524 MatchesTile("Site 1", "https://www.site1.com/",
523 NTPTileSource::POPULAR), 525 NTPTileSource::POPULAR),
524 MatchesTile("Site 2", "https://www.site2.com/", 526 MatchesTile("Site 2", "https://www.site2.com/",
525 NTPTileSource::POPULAR))); 527 NTPTileSource::POPULAR)));
526 } 528 }
527 529
528 } // namespace 530 } // namespace
529 } // namespace ntp_tiles 531 } // namespace ntp_tiles
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698