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

Side by Side Diff: components/suggestions/suggestions_service_unittest.cc

Issue 527943002: [Most Visited] Check for Sync state when using SuggestionsService. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@2125
Patch Set: Created 6 years, 3 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 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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/suggestions/suggestions_service.h" 5 #include "components/suggestions/suggestions_service.h"
6 6
7 #include <map> 7 #include <map>
8 #include <sstream> 8 #include <sstream>
9 #include <string> 9 #include <string>
10 10
11 #include "base/bind.h" 11 #include "base/bind.h"
12 #include "base/memory/scoped_ptr.h" 12 #include "base/memory/scoped_ptr.h"
13 #include "base/message_loop/message_loop.h" 13 #include "base/message_loop/message_loop.h"
14 #include "base/metrics/field_trial.h" 14 #include "base/metrics/field_trial.h"
15 #include "base/prefs/pref_service.h" 15 #include "base/prefs/pref_service.h"
16 #include "base/strings/utf_string_conversions.h" 16 #include "base/strings/utf_string_conversions.h"
17 #include "components/suggestions/blacklist_store.h" 17 #include "components/suggestions/blacklist_store.h"
18 #include "components/suggestions/image_manager.h" 18 #include "components/suggestions/image_manager.h"
19 #include "components/suggestions/proto/suggestions.pb.h" 19 #include "components/suggestions/proto/suggestions.pb.h"
20 #include "components/suggestions/suggestions_store.h" 20 #include "components/suggestions/suggestions_store.h"
21 #include "components/suggestions/suggestions_utils.h"
21 #include "components/variations/entropy_provider.h" 22 #include "components/variations/entropy_provider.h"
22 #include "components/variations/variations_associated_data.h" 23 #include "components/variations/variations_associated_data.h"
23 #include "net/http/http_response_headers.h" 24 #include "net/http/http_response_headers.h"
24 #include "net/http/http_status_code.h" 25 #include "net/http/http_status_code.h"
25 #include "net/url_request/test_url_fetcher_factory.h" 26 #include "net/url_request/test_url_fetcher_factory.h"
26 #include "net/url_request/url_request_status.h" 27 #include "net/url_request/url_request_status.h"
27 #include "net/url_request/url_request_test_util.h" 28 #include "net/url_request/url_request_test_util.h"
28 #include "testing/gmock/include/gmock/gmock.h" 29 #include "testing/gmock/include/gmock/gmock.h"
29 #include "testing/gtest/include/gtest/gtest.h" 30 #include "testing/gtest/include/gtest/gtest.h"
30 31
(...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after
319 EXPECT_CALL(*mock_thumbnail_manager_, Initialize(_)); 320 EXPECT_CALL(*mock_thumbnail_manager_, Initialize(_));
320 321
321 // Expect a call to the blacklist store. Return that there's nothing to 322 // Expect a call to the blacklist store. Return that there's nothing to
322 // blacklist. 323 // blacklist.
323 EXPECT_CALL(*mock_blacklist_store_, FilterSuggestions(_)); 324 EXPECT_CALL(*mock_blacklist_store_, FilterSuggestions(_));
324 EXPECT_CALL(*mock_blacklist_store_, GetFirstUrlFromBlacklist(_)) 325 EXPECT_CALL(*mock_blacklist_store_, GetFirstUrlFromBlacklist(_))
325 .WillOnce(Return(false)); 326 .WillOnce(Return(false));
326 327
327 // Send the request. Empty data will be returned to the callback. 328 // Send the request. Empty data will be returned to the callback.
328 suggestions_service->FetchSuggestionsData( 329 suggestions_service->FetchSuggestionsData(
330 INITIALIZED_ENABLED_HISTORY, // Normal mode.
329 base::Bind(&SuggestionsServiceTest::ExpectEmptySuggestionsProfile, 331 base::Bind(&SuggestionsServiceTest::ExpectEmptySuggestionsProfile,
330 base::Unretained(this))); 332 base::Unretained(this)));
331 333
332 // (Testing only) wait until suggestion fetch is complete. 334 // (Testing only) wait until suggestion fetch is complete.
333 io_message_loop_.RunUntilIdle(); 335 io_message_loop_.RunUntilIdle();
334 336
335 // Ensure that ExpectEmptySuggestionsProfile ran once. 337 // Ensure that ExpectEmptySuggestionsProfile ran once.
336 EXPECT_EQ(1, suggestions_empty_data_count_); 338 EXPECT_EQ(1, suggestions_empty_data_count_);
337 } 339 }
338 340
(...skipping 15 matching lines...) Expand all
354 // Set up expectations on the SuggestionsStore. 356 // Set up expectations on the SuggestionsStore.
355 EXPECT_CALL(*mock_suggestions_store_, ClearSuggestions()); 357 EXPECT_CALL(*mock_suggestions_store_, ClearSuggestions());
356 358
357 // Expect a call to the blacklist store. Return that there's nothing to 359 // Expect a call to the blacklist store. Return that there's nothing to
358 // blacklist. 360 // blacklist.
359 EXPECT_CALL(*mock_blacklist_store_, GetFirstUrlFromBlacklist(_)) 361 EXPECT_CALL(*mock_blacklist_store_, GetFirstUrlFromBlacklist(_))
360 .WillOnce(Return(false)); 362 .WillOnce(Return(false));
361 363
362 // Send the request. Empty data will be returned to the callback. 364 // Send the request. Empty data will be returned to the callback.
363 suggestions_service->FetchSuggestionsData( 365 suggestions_service->FetchSuggestionsData(
366 INITIALIZED_ENABLED_HISTORY, // Normal mode.
364 base::Bind(&SuggestionsServiceTest::ExpectEmptySuggestionsProfile, 367 base::Bind(&SuggestionsServiceTest::ExpectEmptySuggestionsProfile,
365 base::Unretained(this))); 368 base::Unretained(this)));
366 369
367 // (Testing only) wait until suggestion fetch is complete. 370 // (Testing only) wait until suggestion fetch is complete.
368 io_message_loop_.RunUntilIdle(); 371 io_message_loop_.RunUntilIdle();
369 372
370 // Ensure that ExpectEmptySuggestionsProfile ran once. 373 // Ensure that ExpectEmptySuggestionsProfile ran once.
371 EXPECT_EQ(1, suggestions_empty_data_count_); 374 EXPECT_EQ(1, suggestions_empty_data_count_);
372 } 375 }
373 376
377 TEST_F(SuggestionsServiceTest, FetchSuggestionsDataSyncDisabled) {
378 // Field trial enabled with a specific suggestions URL.
379 EnableFieldTrial(kFakeSuggestionsURL, kFakeSuggestionsCommonParams,
380 kFakeBlacklistPath, kFakeBlacklistUrlParam, false);
381 scoped_ptr<SuggestionsService> suggestions_service(
382 CreateSuggestionsServiceWithMocks());
383 EXPECT_TRUE(suggestions_service != NULL);
384
385 // Set up expectations on the SuggestionsStore.
386 EXPECT_CALL(*mock_suggestions_store_, ClearSuggestions());
387
388 // Send the request. Cache is cleared and empty data will be returned to the
389 // callback.
390 suggestions_service->FetchSuggestionsData(
391 SYNC_OR_HISTORY_SYNC_DISABLED,
392 base::Bind(&SuggestionsServiceTest::ExpectEmptySuggestionsProfile,
393 base::Unretained(this)));
394
395 // Wait for posted task to complete.
396 base::MessageLoop::current()->RunUntilIdle();
397
398 // Ensure that ExpectEmptySuggestionsProfile ran once.
399 EXPECT_EQ(1, suggestions_empty_data_count_);
400 }
401
402 TEST_F(SuggestionsServiceTest, FetchSuggestionsDataSyncNotInitializedEnabled) {
403 // Field trial enabled with a specific suggestions URL.
404 EnableFieldTrial(kFakeSuggestionsURL, kFakeSuggestionsCommonParams,
405 kFakeBlacklistPath, kFakeBlacklistUrlParam, false);
406 scoped_ptr<SuggestionsService> suggestions_service(
407 CreateSuggestionsServiceWithMocks());
408 EXPECT_TRUE(suggestions_service != NULL);
409 scoped_ptr<SuggestionsProfile> suggestions_profile(
410 CreateSuggestionsProfile());
411
412 // Expectations.
413 EXPECT_CALL(*mock_suggestions_store_, LoadSuggestions(_))
414 .WillOnce(DoAll(SetArgPointee<0>(*suggestions_profile), Return(true)));
415 EXPECT_CALL(*mock_thumbnail_manager_,
416 Initialize(EqualsProto(*suggestions_profile)));
417 EXPECT_CALL(*mock_blacklist_store_, FilterSuggestions(_));
418
419 // Send the request. In this state, cached data will be returned to the
420 // caller.
421 suggestions_service->FetchSuggestionsData(
422 NOT_INITIALIZED_ENABLED,
423 base::Bind(&SuggestionsServiceTest::CheckSuggestionsData,
424 base::Unretained(this)));
425
426 // Wait for posted task to complete.
427 base::MessageLoop::current()->RunUntilIdle();
428
429 // Ensure that CheckSuggestionsData ran once.
430 EXPECT_EQ(1, suggestions_data_check_count_);
431 }
432
374 TEST_F(SuggestionsServiceTest, BlacklistURL) { 433 TEST_F(SuggestionsServiceTest, BlacklistURL) {
375 EnableFieldTrial(kFakeSuggestionsURL, kFakeSuggestionsCommonParams, 434 EnableFieldTrial(kFakeSuggestionsURL, kFakeSuggestionsCommonParams,
376 kFakeBlacklistPath, kFakeBlacklistUrlParam, false); 435 kFakeBlacklistPath, kFakeBlacklistUrlParam, false);
377 scoped_ptr<SuggestionsService> suggestions_service( 436 scoped_ptr<SuggestionsService> suggestions_service(
378 CreateSuggestionsServiceWithMocks()); 437 CreateSuggestionsServiceWithMocks());
379 EXPECT_TRUE(suggestions_service != NULL); 438 EXPECT_TRUE(suggestions_service != NULL);
380 439
381 GURL blacklist_url(kBlacklistUrl); 440 GURL blacklist_url(kBlacklistUrl);
382 std::string request_url = GetExpectedBlacklistRequestUrl(blacklist_url); 441 std::string request_url = GetExpectedBlacklistRequestUrl(blacklist_url);
383 scoped_ptr<SuggestionsProfile> suggestions_profile( 442 scoped_ptr<SuggestionsProfile> suggestions_profile(
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
529 scoped_ptr<SuggestionsService> suggestions_service( 588 scoped_ptr<SuggestionsService> suggestions_service(
530 CreateSuggestionsServiceWithMocks()); 589 CreateSuggestionsServiceWithMocks());
531 SuggestionsProfile suggestions = 590 SuggestionsProfile suggestions =
532 CreateSuggestionsProfileWithExpiryTimestamps(); 591 CreateSuggestionsProfileWithExpiryTimestamps();
533 suggestions_service->SetDefaultExpiryTimestamp(&suggestions, 592 suggestions_service->SetDefaultExpiryTimestamp(&suggestions,
534 kTestDefaultExpiry); 593 kTestDefaultExpiry);
535 EXPECT_EQ(kTestSetExpiry, suggestions.suggestions(0).expiry_ts()); 594 EXPECT_EQ(kTestSetExpiry, suggestions.suggestions(0).expiry_ts());
536 EXPECT_EQ(kTestDefaultExpiry, suggestions.suggestions(1).expiry_ts()); 595 EXPECT_EQ(kTestDefaultExpiry, suggestions.suggestions(1).expiry_ts());
537 } 596 }
538 } // namespace suggestions 597 } // namespace suggestions
OLDNEW
« no previous file with comments | « components/suggestions/suggestions_service.cc ('k') | components/suggestions/suggestions_utils.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698