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
3 // found in the LICENSE file.
5 #include "components/suggestions/suggestions_service.h"
10 #include "base/bind.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "components/suggestions/blacklist_store.h"
13 #include "components/suggestions/image_manager.h"
14 #include "components/suggestions/proto/suggestions.pb.h"
15 #include "components/suggestions/suggestions_store.h"
16 #include "components/suggestions/suggestions_utils.h"
17 #include "net/http/http_response_headers.h"
18 #include "net/http/http_status_code.h"
19 #include "net/url_request/test_url_fetcher_factory.h"
20 #include "net/url_request/url_request_status.h"
21 #include "net/url_request/url_request_test_util.h"
22 #include "testing/gmock/include/gmock/gmock.h"
23 #include "testing/gtest/include/gtest/gtest.h"
27 using ::testing::AnyNumber
;
29 using ::testing::Return
;
30 using testing::SetArgPointee
;
31 using ::testing::NiceMock
;
32 using ::testing::StrictMock
;
37 const char kTestTitle
[] = "a title";
38 const char kTestUrl
[] = "http://go.com";
39 const char kBlacklistUrl
[] = "http://blacklist.com";
40 const char kBlacklistUrlAlt
[] = "http://blacklist-atl.com";
41 const int64 kTestDefaultExpiry
= 1402200000000000;
42 const int64 kTestSetExpiry
= 1404792000000000;
44 scoped_ptr
<net::FakeURLFetcher
> CreateURLFetcher(
45 const GURL
& url
, net::URLFetcherDelegate
* delegate
,
46 const std::string
& response_data
, net::HttpStatusCode response_code
,
47 net::URLRequestStatus::Status status
) {
48 scoped_ptr
<net::FakeURLFetcher
> fetcher(new net::FakeURLFetcher(
49 url
, delegate
, response_data
, response_code
, status
));
51 if (response_code
== net::HTTP_OK
) {
52 scoped_refptr
<net::HttpResponseHeaders
> download_headers(
53 new net::HttpResponseHeaders(""));
54 download_headers
->AddHeader("Content-Type: text/html");
55 fetcher
->set_response_headers(download_headers
);
57 return fetcher
.Pass();
60 std::string
GetExpectedBlacklistRequestUrl(const GURL
& blacklist_url
) {
61 std::stringstream request_url
;
62 request_url
<< "https://www.google.com/chromesuggestions/blacklist?t=2&url="
63 << net::EscapeQueryParamValue(blacklist_url
.spec(), true);
64 return request_url
.str();
67 // GMock matcher for protobuf equality.
68 MATCHER_P(EqualsProto
, message
, "") {
69 // This implementation assumes protobuf serialization is deterministic, which
70 // is true in practice but technically not something that code is supposed
71 // to rely on. However, it vastly simplifies the implementation.
72 std::string expected_serialized
, actual_serialized
;
73 message
.SerializeToString(&expected_serialized
);
74 arg
.SerializeToString(&actual_serialized
);
75 return expected_serialized
== actual_serialized
;
80 namespace suggestions
{
82 SuggestionsProfile
CreateSuggestionsProfile() {
83 SuggestionsProfile profile
;
84 ChromeSuggestion
* suggestion
= profile
.add_suggestions();
85 suggestion
->set_title(kTestTitle
);
86 suggestion
->set_url(kTestUrl
);
87 suggestion
->set_expiry_ts(kTestSetExpiry
);
91 // Creates one suggestion with expiry timestamp and one without.
92 SuggestionsProfile
CreateSuggestionsProfileWithExpiryTimestamps() {
93 SuggestionsProfile profile
;
94 ChromeSuggestion
* suggestion
= profile
.add_suggestions();
95 suggestion
->set_title(kTestTitle
);
96 suggestion
->set_url(kTestUrl
);
97 suggestion
->set_expiry_ts(kTestSetExpiry
);
99 suggestion
= profile
.add_suggestions();
100 suggestion
->set_title(kTestTitle
);
101 suggestion
->set_url(kTestUrl
);
106 class TestSuggestionsStore
: public suggestions::SuggestionsStore
{
108 TestSuggestionsStore() {
109 cached_suggestions
= CreateSuggestionsProfile();
111 bool LoadSuggestions(SuggestionsProfile
* suggestions
) override
{
112 if (cached_suggestions
.suggestions_size()) {
113 *suggestions
= cached_suggestions
;
118 bool StoreSuggestions(const SuggestionsProfile
& suggestions
)
120 cached_suggestions
= suggestions
;
123 void ClearSuggestions() override
{
124 cached_suggestions
= SuggestionsProfile();
127 SuggestionsProfile cached_suggestions
;
130 class MockImageManager
: public suggestions::ImageManager
{
132 MockImageManager() {}
133 virtual ~MockImageManager() {}
134 MOCK_METHOD1(Initialize
, void(const SuggestionsProfile
&));
135 MOCK_METHOD2(GetImageForURL
,
137 base::Callback
<void(const GURL
&, const SkBitmap
*)>));
140 class MockBlacklistStore
: public suggestions::BlacklistStore
{
142 MOCK_METHOD1(BlacklistUrl
, bool(const GURL
&));
143 MOCK_METHOD0(IsEmpty
, bool());
144 MOCK_METHOD1(GetTimeUntilReadyForUpload
, bool(base::TimeDelta
*));
145 MOCK_METHOD2(GetTimeUntilURLReadyForUpload
,
146 bool(const GURL
&, base::TimeDelta
*));
147 MOCK_METHOD1(GetCandidateForUpload
, bool(GURL
*));
148 MOCK_METHOD1(RemoveUrl
, bool(const GURL
&));
149 MOCK_METHOD1(FilterSuggestions
, void(SuggestionsProfile
*));
152 class SuggestionsServiceTest
: public testing::Test
{
154 void CheckSuggestionsData(const SuggestionsProfile
& suggestions_profile
) {
155 EXPECT_EQ(1, suggestions_profile
.suggestions_size());
156 EXPECT_EQ(kTestTitle
, suggestions_profile
.suggestions(0).title());
157 EXPECT_EQ(kTestUrl
, suggestions_profile
.suggestions(0).url());
158 ++suggestions_data_check_count_
;
161 void SetBlacklistFailure() {
162 blacklisting_failed_
= true;
165 void SetUndoBlacklistFailure() {
166 undo_blacklisting_failed_
= true;
169 void ExpectEmptySuggestionsProfile(const SuggestionsProfile
& profile
) {
170 EXPECT_EQ(0, profile
.suggestions_size());
171 ++suggestions_empty_data_count_
;
174 int suggestions_data_check_count_
;
175 int suggestions_empty_data_count_
;
176 bool blacklisting_failed_
;
177 bool undo_blacklisting_failed_
;
180 SuggestionsServiceTest()
181 : suggestions_data_check_count_(0),
182 suggestions_empty_data_count_(0),
183 blacklisting_failed_(false),
184 undo_blacklisting_failed_(false),
185 factory_(NULL
, base::Bind(&CreateURLFetcher
)),
186 mock_thumbnail_manager_(NULL
),
187 mock_blacklist_store_(NULL
),
188 test_suggestions_store_(NULL
) {}
190 ~SuggestionsServiceTest() override
{}
192 void SetUp() override
{
194 new net::TestURLRequestContextGetter(io_message_loop_
.task_runner());
197 void FetchSuggestionsDataHelper(SyncState sync_state
) {
198 scoped_ptr
<SuggestionsService
> suggestions_service(
199 CreateSuggestionsServiceWithMocks());
200 EXPECT_TRUE(suggestions_service
!= NULL
);
202 // Add some suggestions in the cache.
203 FillSuggestionsStore();
204 SuggestionsProfile suggestions_profile
;
205 test_suggestions_store_
->LoadSuggestions(&suggestions_profile
);
207 // Set up net::FakeURLFetcherFactory.
208 factory_
.SetFakeResponse(GURL(kSuggestionsURL
),
209 suggestions_profile
.SerializeAsString(),
210 net::HTTP_OK
, net::URLRequestStatus::SUCCESS
);
213 EXPECT_CALL(*mock_thumbnail_manager_
,
214 Initialize(EqualsProto(suggestions_profile
)));
215 EXPECT_CALL(*mock_blacklist_store_
, FilterSuggestions(_
));
216 EXPECT_CALL(*mock_blacklist_store_
, GetTimeUntilReadyForUpload(_
))
217 .WillOnce(Return(false));
219 // Send the request. The data will be returned to the callback.
220 suggestions_service
->FetchSuggestionsData(
222 base::Bind(&SuggestionsServiceTest::CheckSuggestionsData
,
223 base::Unretained(this)));
225 // Ensure that CheckSuggestionsData() ran once.
226 EXPECT_EQ(1, suggestions_data_check_count_
);
228 // Let the network request run.
229 io_message_loop_
.RunUntilIdle();
232 SuggestionsService
* CreateSuggestionsServiceWithMocks() {
233 // These objects are owned by the returned SuggestionsService, but we keep
234 // the pointer around for testing.
235 test_suggestions_store_
= new TestSuggestionsStore();
236 mock_thumbnail_manager_
= new StrictMock
<MockImageManager
>();
237 mock_blacklist_store_
= new StrictMock
<MockBlacklistStore
>();
238 return new SuggestionsService(
239 request_context_
.get(),
240 scoped_ptr
<SuggestionsStore
>(test_suggestions_store_
),
241 scoped_ptr
<ImageManager
>(mock_thumbnail_manager_
),
242 scoped_ptr
<BlacklistStore
>(mock_blacklist_store_
));
245 void FillSuggestionsStore() {
246 test_suggestions_store_
->StoreSuggestions(CreateSuggestionsProfile());
249 void Blacklist(SuggestionsService
* suggestions_service
, GURL url
) {
250 suggestions_service
->BlacklistURL(
252 base::Bind(&SuggestionsServiceTest::CheckSuggestionsData
,
253 base::Unretained(this)),
254 base::Bind(&SuggestionsServiceTest::SetBlacklistFailure
,
255 base::Unretained(this)));
258 void UndoBlacklist(SuggestionsService
* suggestions_service
, GURL url
) {
259 suggestions_service
->UndoBlacklistURL(
261 base::Bind(&SuggestionsServiceTest::CheckSuggestionsData
,
262 base::Unretained(this)),
263 base::Bind(&SuggestionsServiceTest::SetUndoBlacklistFailure
,
264 base::Unretained(this)));
267 // Helper for Undo failure tests. Depending on |is_uploaded|, tests either
268 // the case where the URL is no longer in the local blacklist or the case
269 // in which it's not yet candidate for upload.
270 void UndoBlacklistURLFailsHelper(bool is_uploaded
) {
271 scoped_ptr
<SuggestionsService
> suggestions_service(
272 CreateSuggestionsServiceWithMocks());
273 EXPECT_TRUE(suggestions_service
!= NULL
);
274 // Ensure scheduling the request doesn't happen before undo.
275 base::TimeDelta delay
= base::TimeDelta::FromHours(1);
276 suggestions_service
->set_blacklist_delay(delay
);
277 SuggestionsProfile suggestions_profile
= CreateSuggestionsProfile();
278 GURL
blacklist_url(kBlacklistUrl
);
280 // Blacklist expectations.
281 EXPECT_CALL(*mock_blacklist_store_
, BlacklistUrl(Eq(blacklist_url
)))
282 .WillOnce(Return(true));
283 EXPECT_CALL(*mock_thumbnail_manager_
,
284 Initialize(EqualsProto(suggestions_profile
)));
285 EXPECT_CALL(*mock_blacklist_store_
, FilterSuggestions(_
));
286 EXPECT_CALL(*mock_blacklist_store_
, GetTimeUntilReadyForUpload(_
))
287 .WillOnce(DoAll(SetArgPointee
<0>(delay
), Return(true)));
288 // Undo expectations.
290 // URL is not in local blacklist.
291 EXPECT_CALL(*mock_blacklist_store_
,
292 GetTimeUntilURLReadyForUpload(Eq(blacklist_url
), _
))
293 .WillOnce(Return(false));
295 // URL is not yet candidate for upload.
296 base::TimeDelta negative_delay
= base::TimeDelta::FromHours(-1);
297 EXPECT_CALL(*mock_blacklist_store_
,
298 GetTimeUntilURLReadyForUpload(Eq(blacklist_url
), _
))
299 .WillOnce(DoAll(SetArgPointee
<1>(negative_delay
), Return(true)));
302 Blacklist(suggestions_service
.get(), blacklist_url
);
303 UndoBlacklist(suggestions_service
.get(), blacklist_url
);
305 EXPECT_EQ(1, suggestions_data_check_count_
);
306 EXPECT_FALSE(blacklisting_failed_
);
307 EXPECT_TRUE(undo_blacklisting_failed_
);
311 base::MessageLoopForIO io_message_loop_
;
312 net::FakeURLFetcherFactory factory_
;
313 // Only used if the SuggestionsService is built with mocks. Not owned.
314 MockImageManager
* mock_thumbnail_manager_
;
315 MockBlacklistStore
* mock_blacklist_store_
;
316 TestSuggestionsStore
* test_suggestions_store_
;
317 scoped_refptr
<net::TestURLRequestContextGetter
> request_context_
;
320 DISALLOW_COPY_AND_ASSIGN(SuggestionsServiceTest
);
323 TEST_F(SuggestionsServiceTest
, FetchSuggestionsData
) {
324 FetchSuggestionsDataHelper(INITIALIZED_ENABLED_HISTORY
);
327 TEST_F(SuggestionsServiceTest
, FetchSuggestionsDataSyncNotInitializedEnabled
) {
328 FetchSuggestionsDataHelper(NOT_INITIALIZED_ENABLED
);
331 TEST_F(SuggestionsServiceTest
, FetchSuggestionsDataSyncDisabled
) {
332 scoped_ptr
<SuggestionsService
> suggestions_service(
333 CreateSuggestionsServiceWithMocks());
334 EXPECT_TRUE(suggestions_service
!= NULL
);
336 FillSuggestionsStore();
338 // Send the request. Cache is cleared and empty data will be returned to the
340 suggestions_service
->FetchSuggestionsData(
341 SYNC_OR_HISTORY_SYNC_DISABLED
,
342 base::Bind(&SuggestionsServiceTest::ExpectEmptySuggestionsProfile
,
343 base::Unretained(this)));
345 // Ensure that ExpectEmptySuggestionsProfile ran once.
346 EXPECT_EQ(1, suggestions_empty_data_count_
);
349 TEST_F(SuggestionsServiceTest
, IssueRequestIfNoneOngoingError
) {
350 scoped_ptr
<SuggestionsService
> suggestions_service(
351 CreateSuggestionsServiceWithMocks());
352 EXPECT_TRUE(suggestions_service
!= NULL
);
354 // Fake a request error.
355 factory_
.SetFakeResponse(GURL(kSuggestionsURL
), "irrelevant", net::HTTP_OK
,
356 net::URLRequestStatus::FAILED
);
358 EXPECT_CALL(*mock_blacklist_store_
, GetTimeUntilReadyForUpload(_
))
359 .WillOnce(Return(false));
361 // Send the request. Empty data will be returned to the callback.
362 suggestions_service
->IssueRequestIfNoneOngoing(GURL(kSuggestionsURL
));
364 // (Testing only) wait until suggestion fetch is complete.
365 io_message_loop_
.RunUntilIdle();
368 TEST_F(SuggestionsServiceTest
, IssueRequestIfNoneOngoingResponseNotOK
) {
369 scoped_ptr
<SuggestionsService
> suggestions_service(
370 CreateSuggestionsServiceWithMocks());
371 EXPECT_TRUE(suggestions_service
!= NULL
);
373 // Add some suggestions in the cache.
374 FillSuggestionsStore();
376 // Fake a non-200 response code.
377 factory_
.SetFakeResponse(GURL(kSuggestionsURL
), "irrelevant",
378 net::HTTP_BAD_REQUEST
,
379 net::URLRequestStatus::SUCCESS
);
381 // Expect that an upload to the blacklist is scheduled.
382 EXPECT_CALL(*mock_blacklist_store_
, GetTimeUntilReadyForUpload(_
))
383 .WillOnce(Return(false));
385 // Send the request. Empty data will be returned to the callback.
386 suggestions_service
->IssueRequestIfNoneOngoing(GURL(kSuggestionsURL
));
388 // (Testing only) wait until suggestion fetch is complete.
389 io_message_loop_
.RunUntilIdle();
391 // Expect no suggestions in the cache.
392 SuggestionsProfile empty_suggestions
;
393 EXPECT_FALSE(test_suggestions_store_
->LoadSuggestions(&empty_suggestions
));
396 TEST_F(SuggestionsServiceTest
, BlacklistURL
) {
397 scoped_ptr
<SuggestionsService
> suggestions_service(
398 CreateSuggestionsServiceWithMocks());
399 EXPECT_TRUE(suggestions_service
!= NULL
);
400 base::TimeDelta no_delay
= base::TimeDelta::FromSeconds(0);
401 suggestions_service
->set_blacklist_delay(no_delay
);
403 GURL
blacklist_url(kBlacklistUrl
);
404 std::string request_url
= GetExpectedBlacklistRequestUrl(blacklist_url
);
405 SuggestionsProfile suggestions_profile
= CreateSuggestionsProfile();
406 factory_
.SetFakeResponse(GURL(request_url
),
407 suggestions_profile
.SerializeAsString(),
408 net::HTTP_OK
, net::URLRequestStatus::SUCCESS
);
409 EXPECT_CALL(*mock_thumbnail_manager_
,
410 Initialize(EqualsProto(suggestions_profile
)));
412 // Expected calls to the blacklist store.
413 EXPECT_CALL(*mock_blacklist_store_
, BlacklistUrl(Eq(blacklist_url
)))
414 .WillOnce(Return(true));
415 EXPECT_CALL(*mock_blacklist_store_
, FilterSuggestions(_
));
416 EXPECT_CALL(*mock_blacklist_store_
, GetTimeUntilReadyForUpload(_
))
417 .WillOnce(DoAll(SetArgPointee
<0>(no_delay
), Return(true)))
418 .WillOnce(Return(false));
419 EXPECT_CALL(*mock_blacklist_store_
, GetCandidateForUpload(_
))
420 .WillOnce(DoAll(SetArgPointee
<0>(blacklist_url
), Return(true)));
421 EXPECT_CALL(*mock_blacklist_store_
, RemoveUrl(Eq(blacklist_url
)))
422 .WillOnce(Return(true));
424 Blacklist(suggestions_service
.get(), blacklist_url
);
426 // Wait on the upload task. This only works when the scheduling task is not
427 // for future execution (note how both the SuggestionsService's scheduling
428 // delay and the BlacklistStore's candidacy delay are zero). Then wait on
429 // the blacklist request, then again on the next blacklist scheduling task.
430 base::MessageLoop::current()->RunUntilIdle();
431 io_message_loop_
.RunUntilIdle();
432 base::MessageLoop::current()->RunUntilIdle();
434 // Ensure that CheckSuggestionsData() ran once.
435 EXPECT_EQ(1, suggestions_data_check_count_
);
436 EXPECT_FALSE(blacklisting_failed_
);
439 TEST_F(SuggestionsServiceTest
, BlacklistURLFails
) {
440 scoped_ptr
<SuggestionsService
> suggestions_service(
441 CreateSuggestionsServiceWithMocks());
442 EXPECT_TRUE(suggestions_service
!= NULL
);
443 GURL
blacklist_url(kBlacklistUrl
);
444 EXPECT_CALL(*mock_blacklist_store_
, BlacklistUrl(Eq(blacklist_url
)))
445 .WillOnce(Return(false));
447 Blacklist(suggestions_service
.get(), blacklist_url
);
449 EXPECT_TRUE(blacklisting_failed_
);
450 EXPECT_EQ(0, suggestions_data_check_count_
);
453 // Initial blacklist request fails, triggering a second which succeeds.
454 TEST_F(SuggestionsServiceTest
, BlacklistURLRequestFails
) {
455 scoped_ptr
<SuggestionsService
> suggestions_service(
456 CreateSuggestionsServiceWithMocks());
457 EXPECT_TRUE(suggestions_service
!= NULL
);
458 base::TimeDelta no_delay
= base::TimeDelta::FromSeconds(0);
459 suggestions_service
->set_blacklist_delay(no_delay
);
461 GURL
blacklist_url(kBlacklistUrl
);
462 std::string request_url
= GetExpectedBlacklistRequestUrl(blacklist_url
);
463 GURL
blacklist_url_alt(kBlacklistUrlAlt
);
464 std::string request_url_alt
= GetExpectedBlacklistRequestUrl(
466 SuggestionsProfile suggestions_profile
= CreateSuggestionsProfile();
468 // Note: we want to set the response for the blacklist URL to first
469 // succeed, then fail. This doesn't seem possible. For simplicity of testing,
470 // we'll pretend the URL changed in the BlacklistStore between the first and
471 // the second request, and adjust expectations accordingly.
472 factory_
.SetFakeResponse(GURL(request_url
), "irrelevant", net::HTTP_OK
,
473 net::URLRequestStatus::FAILED
);
474 factory_
.SetFakeResponse(GURL(request_url_alt
),
475 suggestions_profile
.SerializeAsString(),
476 net::HTTP_OK
, net::URLRequestStatus::SUCCESS
);
479 EXPECT_CALL(*mock_thumbnail_manager_
,
480 Initialize(EqualsProto(suggestions_profile
)));
481 EXPECT_CALL(*mock_blacklist_store_
, BlacklistUrl(Eq(blacklist_url
)))
482 .WillOnce(Return(true));
483 EXPECT_CALL(*mock_blacklist_store_
, FilterSuggestions(_
));
484 EXPECT_CALL(*mock_blacklist_store_
, GetTimeUntilReadyForUpload(_
))
485 .WillOnce(DoAll(SetArgPointee
<0>(no_delay
), Return(true)))
486 .WillOnce(DoAll(SetArgPointee
<0>(no_delay
), Return(true)))
487 .WillOnce(Return(false));
488 EXPECT_CALL(*mock_blacklist_store_
, GetCandidateForUpload(_
))
489 .WillOnce(DoAll(SetArgPointee
<0>(blacklist_url
), Return(true)))
490 .WillOnce(DoAll(SetArgPointee
<0>(blacklist_url_alt
), Return(true)));
491 EXPECT_CALL(*mock_blacklist_store_
, RemoveUrl(Eq(blacklist_url_alt
)))
492 .WillOnce(Return(true));
494 // Blacklist call, first request attempt.
495 Blacklist(suggestions_service
.get(), blacklist_url
);
496 EXPECT_EQ(1, suggestions_data_check_count_
);
497 EXPECT_FALSE(blacklisting_failed_
);
499 // Wait for the first scheduling, the first request, the second scheduling,
500 // second request and the third scheduling. Again, note that calling
501 // RunUntilIdle on the MessageLoop only works when the task is not posted for
503 base::MessageLoop::current()->RunUntilIdle();
504 io_message_loop_
.RunUntilIdle();
505 base::MessageLoop::current()->RunUntilIdle();
506 io_message_loop_
.RunUntilIdle();
507 base::MessageLoop::current()->RunUntilIdle();
510 TEST_F(SuggestionsServiceTest
, UndoBlacklistURL
) {
511 scoped_ptr
<SuggestionsService
> suggestions_service(
512 CreateSuggestionsServiceWithMocks());
513 EXPECT_TRUE(suggestions_service
!= NULL
);
514 // Ensure scheduling the request doesn't happen before undo.
515 base::TimeDelta delay
= base::TimeDelta::FromHours(1);
516 suggestions_service
->set_blacklist_delay(delay
);
517 SuggestionsProfile suggestions_profile
= CreateSuggestionsProfile();
518 GURL
blacklist_url(kBlacklistUrl
);
520 // Blacklist expectations.
521 EXPECT_CALL(*mock_blacklist_store_
, BlacklistUrl(Eq(blacklist_url
)))
522 .WillOnce(Return(true));
523 EXPECT_CALL(*mock_thumbnail_manager_
,
524 Initialize(EqualsProto(suggestions_profile
)))
526 EXPECT_CALL(*mock_blacklist_store_
, FilterSuggestions(_
))
528 EXPECT_CALL(*mock_blacklist_store_
, GetTimeUntilReadyForUpload(_
))
529 .WillOnce(DoAll(SetArgPointee
<0>(delay
), Return(true)));
530 // Undo expectations.
531 EXPECT_CALL(*mock_blacklist_store_
,
532 GetTimeUntilURLReadyForUpload(Eq(blacklist_url
), _
))
533 .WillOnce(DoAll(SetArgPointee
<1>(delay
), Return(true)));
534 EXPECT_CALL(*mock_blacklist_store_
, RemoveUrl(Eq(blacklist_url
)))
535 .WillOnce(Return(true));
537 Blacklist(suggestions_service
.get(), blacklist_url
);
538 UndoBlacklist(suggestions_service
.get(), blacklist_url
);
540 EXPECT_EQ(2, suggestions_data_check_count_
);
541 EXPECT_FALSE(blacklisting_failed_
);
542 EXPECT_FALSE(undo_blacklisting_failed_
);
546 TEST_F(SuggestionsServiceTest
, UndoBlacklistURLFailsIfNotInBlacklist
) {
547 UndoBlacklistURLFailsHelper(true);
550 TEST_F(SuggestionsServiceTest
, UndoBlacklistURLFailsIfAlreadyCandidate
) {
551 UndoBlacklistURLFailsHelper(false);
554 TEST_F(SuggestionsServiceTest
, GetBlacklistedUrl
) {
555 scoped_ptr
<GURL
> request_url
;
556 scoped_ptr
<net::FakeURLFetcher
> fetcher
;
559 // Not a blacklist request.
560 request_url
.reset(new GURL("http://not-blacklisting.com/a?b=c"));
561 fetcher
= CreateURLFetcher(*request_url
, NULL
, "", net::HTTP_OK
,
562 net::URLRequestStatus::SUCCESS
);
563 EXPECT_FALSE(SuggestionsService::GetBlacklistedUrl(*fetcher
, &retrieved_url
));
565 // An actual blacklist request.
566 string blacklisted_url
= "http://blacklisted.com/a?b=c&d=e";
567 string encoded_blacklisted_url
=
568 "http%3A%2F%2Fblacklisted.com%2Fa%3Fb%3Dc%26d%3De";
569 string
blacklist_request_prefix(kSuggestionsBlacklistURLPrefix
);
571 new GURL(blacklist_request_prefix
+ encoded_blacklisted_url
));
573 fetcher
= CreateURLFetcher(*request_url
, NULL
, "", net::HTTP_OK
,
574 net::URLRequestStatus::SUCCESS
);
575 EXPECT_TRUE(SuggestionsService::GetBlacklistedUrl(*fetcher
, &retrieved_url
));
576 EXPECT_EQ(blacklisted_url
, retrieved_url
.spec());
579 TEST_F(SuggestionsServiceTest
, UpdateBlacklistDelay
) {
580 scoped_ptr
<SuggestionsService
> suggestions_service(
581 CreateSuggestionsServiceWithMocks());
582 base::TimeDelta initial_delay
= suggestions_service
->blacklist_delay();
584 // Delay unchanged on success.
585 suggestions_service
->UpdateBlacklistDelay(true);
586 EXPECT_EQ(initial_delay
, suggestions_service
->blacklist_delay());
588 // Delay increases on failure.
589 suggestions_service
->UpdateBlacklistDelay(false);
590 EXPECT_GT(suggestions_service
->blacklist_delay(), initial_delay
);
592 // Delay resets on success.
593 suggestions_service
->UpdateBlacklistDelay(true);
594 EXPECT_EQ(initial_delay
, suggestions_service
->blacklist_delay());
597 TEST_F(SuggestionsServiceTest
, CheckDefaultTimeStamps
) {
598 scoped_ptr
<SuggestionsService
> suggestions_service(
599 CreateSuggestionsServiceWithMocks());
600 SuggestionsProfile suggestions
=
601 CreateSuggestionsProfileWithExpiryTimestamps();
602 suggestions_service
->SetDefaultExpiryTimestamp(&suggestions
,
604 EXPECT_EQ(kTestSetExpiry
, suggestions
.suggestions(0).expiry_ts());
605 EXPECT_EQ(kTestDefaultExpiry
, suggestions
.suggestions(1).expiry_ts());
607 } // namespace suggestions