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 "base/message_loop/message_loop.h"
13 #include "components/suggestions/blacklist_store.h"
14 #include "components/suggestions/image_manager.h"
15 #include "components/suggestions/proto/suggestions.pb.h"
16 #include "components/suggestions/suggestions_store.h"
17 #include "components/suggestions/suggestions_utils.h"
18 #include "net/http/http_response_headers.h"
19 #include "net/http/http_status_code.h"
20 #include "net/url_request/test_url_fetcher_factory.h"
21 #include "net/url_request/url_request_status.h"
22 #include "net/url_request/url_request_test_util.h"
23 #include "testing/gmock/include/gmock/gmock.h"
24 #include "testing/gtest/include/gtest/gtest.h"
28 using ::testing::AnyNumber
;
30 using ::testing::Return
;
31 using testing::SetArgPointee
;
32 using ::testing::NiceMock
;
33 using ::testing::StrictMock
;
38 const char kTestTitle
[] = "a title";
39 const char kTestUrl
[] = "http://go.com";
40 const char kBlacklistUrl
[] = "http://blacklist.com";
41 const char kBlacklistUrlAlt
[] = "http://blacklist-atl.com";
42 const int64 kTestDefaultExpiry
= 1402200000000000;
43 const int64 kTestSetExpiry
= 1404792000000000;
45 scoped_ptr
<net::FakeURLFetcher
> CreateURLFetcher(
46 const GURL
& url
, net::URLFetcherDelegate
* delegate
,
47 const std::string
& response_data
, net::HttpStatusCode response_code
,
48 net::URLRequestStatus::Status status
) {
49 scoped_ptr
<net::FakeURLFetcher
> fetcher(new net::FakeURLFetcher(
50 url
, delegate
, response_data
, response_code
, status
));
52 if (response_code
== net::HTTP_OK
) {
53 scoped_refptr
<net::HttpResponseHeaders
> download_headers(
54 new net::HttpResponseHeaders(""));
55 download_headers
->AddHeader("Content-Type: text/html");
56 fetcher
->set_response_headers(download_headers
);
58 return fetcher
.Pass();
61 std::string
GetExpectedBlacklistRequestUrl(const GURL
& blacklist_url
) {
62 std::stringstream request_url
;
63 request_url
<< "https://www.google.com/chromesuggestions/blacklist?t=2&url="
64 << net::EscapeQueryParamValue(blacklist_url
.spec(), true);
65 return request_url
.str();
68 // GMock matcher for protobuf equality.
69 MATCHER_P(EqualsProto
, message
, "") {
70 // This implementation assumes protobuf serialization is deterministic, which
71 // is true in practice but technically not something that code is supposed
72 // to rely on. However, it vastly simplifies the implementation.
73 std::string expected_serialized
, actual_serialized
;
74 message
.SerializeToString(&expected_serialized
);
75 arg
.SerializeToString(&actual_serialized
);
76 return expected_serialized
== actual_serialized
;
81 namespace suggestions
{
83 SuggestionsProfile
CreateSuggestionsProfile() {
84 SuggestionsProfile profile
;
85 ChromeSuggestion
* suggestion
= profile
.add_suggestions();
86 suggestion
->set_title(kTestTitle
);
87 suggestion
->set_url(kTestUrl
);
88 suggestion
->set_expiry_ts(kTestSetExpiry
);
92 // Creates one suggestion with expiry timestamp and one without.
93 SuggestionsProfile
CreateSuggestionsProfileWithExpiryTimestamps() {
94 SuggestionsProfile profile
;
95 ChromeSuggestion
* suggestion
= profile
.add_suggestions();
96 suggestion
->set_title(kTestTitle
);
97 suggestion
->set_url(kTestUrl
);
98 suggestion
->set_expiry_ts(kTestSetExpiry
);
100 suggestion
= profile
.add_suggestions();
101 suggestion
->set_title(kTestTitle
);
102 suggestion
->set_url(kTestUrl
);
107 class TestSuggestionsStore
: public suggestions::SuggestionsStore
{
109 TestSuggestionsStore() {
110 cached_suggestions
= CreateSuggestionsProfile();
112 bool LoadSuggestions(SuggestionsProfile
* suggestions
) override
{
113 if (cached_suggestions
.suggestions_size()) {
114 *suggestions
= cached_suggestions
;
119 bool StoreSuggestions(const SuggestionsProfile
& suggestions
)
121 cached_suggestions
= suggestions
;
124 void ClearSuggestions() override
{
125 cached_suggestions
= SuggestionsProfile();
128 SuggestionsProfile cached_suggestions
;
131 class MockImageManager
: public suggestions::ImageManager
{
133 MockImageManager() {}
134 virtual ~MockImageManager() {}
135 MOCK_METHOD1(Initialize
, void(const SuggestionsProfile
&));
136 MOCK_METHOD2(GetImageForURL
,
138 base::Callback
<void(const GURL
&, const SkBitmap
*)>));
141 class MockBlacklistStore
: public suggestions::BlacklistStore
{
143 MOCK_METHOD1(BlacklistUrl
, bool(const GURL
&));
144 MOCK_METHOD0(IsEmpty
, bool());
145 MOCK_METHOD1(GetTimeUntilReadyForUpload
, bool(base::TimeDelta
*));
146 MOCK_METHOD2(GetTimeUntilURLReadyForUpload
,
147 bool(const GURL
&, base::TimeDelta
*));
148 MOCK_METHOD1(GetCandidateForUpload
, bool(GURL
*));
149 MOCK_METHOD1(RemoveUrl
, bool(const GURL
&));
150 MOCK_METHOD1(FilterSuggestions
, void(SuggestionsProfile
*));
153 class SuggestionsServiceTest
: public testing::Test
{
155 void CheckSuggestionsData(const SuggestionsProfile
& suggestions_profile
) {
156 EXPECT_EQ(1, suggestions_profile
.suggestions_size());
157 EXPECT_EQ(kTestTitle
, suggestions_profile
.suggestions(0).title());
158 EXPECT_EQ(kTestUrl
, suggestions_profile
.suggestions(0).url());
159 ++suggestions_data_check_count_
;
162 void SetBlacklistFailure() {
163 blacklisting_failed_
= true;
166 void SetUndoBlacklistFailure() {
167 undo_blacklisting_failed_
= true;
170 void ExpectEmptySuggestionsProfile(const SuggestionsProfile
& profile
) {
171 EXPECT_EQ(0, profile
.suggestions_size());
172 ++suggestions_empty_data_count_
;
175 int suggestions_data_check_count_
;
176 int suggestions_empty_data_count_
;
177 bool blacklisting_failed_
;
178 bool undo_blacklisting_failed_
;
181 SuggestionsServiceTest()
182 : suggestions_data_check_count_(0),
183 suggestions_empty_data_count_(0),
184 blacklisting_failed_(false),
185 undo_blacklisting_failed_(false),
186 factory_(NULL
, base::Bind(&CreateURLFetcher
)),
187 mock_thumbnail_manager_(NULL
),
188 mock_blacklist_store_(NULL
),
189 test_suggestions_store_(NULL
) {}
191 ~SuggestionsServiceTest() override
{}
193 void SetUp() override
{
194 request_context_
= new net::TestURLRequestContextGetter(
195 io_message_loop_
.message_loop_proxy());
198 void FetchSuggestionsDataHelper(SyncState sync_state
) {
199 scoped_ptr
<SuggestionsService
> suggestions_service(
200 CreateSuggestionsServiceWithMocks());
201 EXPECT_TRUE(suggestions_service
!= NULL
);
203 // Add some suggestions in the cache.
204 FillSuggestionsStore();
205 SuggestionsProfile suggestions_profile
;
206 test_suggestions_store_
->LoadSuggestions(&suggestions_profile
);
208 // Set up net::FakeURLFetcherFactory.
209 factory_
.SetFakeResponse(GURL(kSuggestionsURL
),
210 suggestions_profile
.SerializeAsString(),
211 net::HTTP_OK
, net::URLRequestStatus::SUCCESS
);
214 EXPECT_CALL(*mock_thumbnail_manager_
,
215 Initialize(EqualsProto(suggestions_profile
)));
216 EXPECT_CALL(*mock_blacklist_store_
, FilterSuggestions(_
));
217 EXPECT_CALL(*mock_blacklist_store_
, GetTimeUntilReadyForUpload(_
))
218 .WillOnce(Return(false));
220 // Send the request. The data will be returned to the callback.
221 suggestions_service
->FetchSuggestionsData(
223 base::Bind(&SuggestionsServiceTest::CheckSuggestionsData
,
224 base::Unretained(this)));
226 // Ensure that CheckSuggestionsData() ran once.
227 EXPECT_EQ(1, suggestions_data_check_count_
);
229 // Let the network request run.
230 io_message_loop_
.RunUntilIdle();
233 SuggestionsService
* CreateSuggestionsServiceWithMocks() {
234 // These objects are owned by the returned SuggestionsService, but we keep
235 // the pointer around for testing.
236 test_suggestions_store_
= new TestSuggestionsStore();
237 mock_thumbnail_manager_
= new StrictMock
<MockImageManager
>();
238 mock_blacklist_store_
= new StrictMock
<MockBlacklistStore
>();
239 return new SuggestionsService(
240 request_context_
.get(),
241 scoped_ptr
<SuggestionsStore
>(test_suggestions_store_
),
242 scoped_ptr
<ImageManager
>(mock_thumbnail_manager_
),
243 scoped_ptr
<BlacklistStore
>(mock_blacklist_store_
));
246 void FillSuggestionsStore() {
247 test_suggestions_store_
->StoreSuggestions(CreateSuggestionsProfile());
250 void Blacklist(SuggestionsService
* suggestions_service
, GURL url
) {
251 suggestions_service
->BlacklistURL(
253 base::Bind(&SuggestionsServiceTest::CheckSuggestionsData
,
254 base::Unretained(this)),
255 base::Bind(&SuggestionsServiceTest::SetBlacklistFailure
,
256 base::Unretained(this)));
259 void UndoBlacklist(SuggestionsService
* suggestions_service
, GURL url
) {
260 suggestions_service
->UndoBlacklistURL(
262 base::Bind(&SuggestionsServiceTest::CheckSuggestionsData
,
263 base::Unretained(this)),
264 base::Bind(&SuggestionsServiceTest::SetUndoBlacklistFailure
,
265 base::Unretained(this)));
268 // Helper for Undo failure tests. Depending on |is_uploaded|, tests either
269 // the case where the URL is no longer in the local blacklist or the case
270 // in which it's not yet candidate for upload.
271 void UndoBlacklistURLFailsHelper(bool is_uploaded
) {
272 scoped_ptr
<SuggestionsService
> suggestions_service(
273 CreateSuggestionsServiceWithMocks());
274 EXPECT_TRUE(suggestions_service
!= NULL
);
275 // Ensure scheduling the request doesn't happen before undo.
276 base::TimeDelta delay
= base::TimeDelta::FromHours(1);
277 suggestions_service
->set_blacklist_delay(delay
);
278 SuggestionsProfile suggestions_profile
= CreateSuggestionsProfile();
279 GURL
blacklist_url(kBlacklistUrl
);
281 // Blacklist expectations.
282 EXPECT_CALL(*mock_blacklist_store_
, BlacklistUrl(Eq(blacklist_url
)))
283 .WillOnce(Return(true));
284 EXPECT_CALL(*mock_thumbnail_manager_
,
285 Initialize(EqualsProto(suggestions_profile
)));
286 EXPECT_CALL(*mock_blacklist_store_
, FilterSuggestions(_
));
287 EXPECT_CALL(*mock_blacklist_store_
, GetTimeUntilReadyForUpload(_
))
288 .WillOnce(DoAll(SetArgPointee
<0>(delay
), Return(true)));
289 // Undo expectations.
291 // URL is not in local blacklist.
292 EXPECT_CALL(*mock_blacklist_store_
,
293 GetTimeUntilURLReadyForUpload(Eq(blacklist_url
), _
))
294 .WillOnce(Return(false));
296 // URL is not yet candidate for upload.
297 base::TimeDelta negative_delay
= base::TimeDelta::FromHours(-1);
298 EXPECT_CALL(*mock_blacklist_store_
,
299 GetTimeUntilURLReadyForUpload(Eq(blacklist_url
), _
))
300 .WillOnce(DoAll(SetArgPointee
<1>(negative_delay
), Return(true)));
303 Blacklist(suggestions_service
.get(), blacklist_url
);
304 UndoBlacklist(suggestions_service
.get(), blacklist_url
);
306 EXPECT_EQ(1, suggestions_data_check_count_
);
307 EXPECT_FALSE(blacklisting_failed_
);
308 EXPECT_TRUE(undo_blacklisting_failed_
);
312 base::MessageLoopForIO io_message_loop_
;
313 net::FakeURLFetcherFactory factory_
;
314 // Only used if the SuggestionsService is built with mocks. Not owned.
315 MockImageManager
* mock_thumbnail_manager_
;
316 MockBlacklistStore
* mock_blacklist_store_
;
317 TestSuggestionsStore
* test_suggestions_store_
;
318 scoped_refptr
<net::TestURLRequestContextGetter
> request_context_
;
321 DISALLOW_COPY_AND_ASSIGN(SuggestionsServiceTest
);
324 TEST_F(SuggestionsServiceTest
, FetchSuggestionsData
) {
325 FetchSuggestionsDataHelper(INITIALIZED_ENABLED_HISTORY
);
328 TEST_F(SuggestionsServiceTest
, FetchSuggestionsDataSyncNotInitializedEnabled
) {
329 FetchSuggestionsDataHelper(NOT_INITIALIZED_ENABLED
);
332 TEST_F(SuggestionsServiceTest
, FetchSuggestionsDataSyncDisabled
) {
333 scoped_ptr
<SuggestionsService
> suggestions_service(
334 CreateSuggestionsServiceWithMocks());
335 EXPECT_TRUE(suggestions_service
!= NULL
);
337 FillSuggestionsStore();
339 // Send the request. Cache is cleared and empty data will be returned to the
341 suggestions_service
->FetchSuggestionsData(
342 SYNC_OR_HISTORY_SYNC_DISABLED
,
343 base::Bind(&SuggestionsServiceTest::ExpectEmptySuggestionsProfile
,
344 base::Unretained(this)));
346 // Ensure that ExpectEmptySuggestionsProfile ran once.
347 EXPECT_EQ(1, suggestions_empty_data_count_
);
350 TEST_F(SuggestionsServiceTest
, IssueRequestIfNoneOngoingError
) {
351 scoped_ptr
<SuggestionsService
> suggestions_service(
352 CreateSuggestionsServiceWithMocks());
353 EXPECT_TRUE(suggestions_service
!= NULL
);
355 // Fake a request error.
356 factory_
.SetFakeResponse(GURL(kSuggestionsURL
), "irrelevant", net::HTTP_OK
,
357 net::URLRequestStatus::FAILED
);
359 EXPECT_CALL(*mock_blacklist_store_
, GetTimeUntilReadyForUpload(_
))
360 .WillOnce(Return(false));
362 // Send the request. Empty data will be returned to the callback.
363 suggestions_service
->IssueRequestIfNoneOngoing(GURL(kSuggestionsURL
));
365 // (Testing only) wait until suggestion fetch is complete.
366 io_message_loop_
.RunUntilIdle();
369 TEST_F(SuggestionsServiceTest
, IssueRequestIfNoneOngoingResponseNotOK
) {
370 scoped_ptr
<SuggestionsService
> suggestions_service(
371 CreateSuggestionsServiceWithMocks());
372 EXPECT_TRUE(suggestions_service
!= NULL
);
374 // Add some suggestions in the cache.
375 FillSuggestionsStore();
377 // Fake a non-200 response code.
378 factory_
.SetFakeResponse(GURL(kSuggestionsURL
), "irrelevant",
379 net::HTTP_BAD_REQUEST
,
380 net::URLRequestStatus::SUCCESS
);
382 // Expect that an upload to the blacklist is scheduled.
383 EXPECT_CALL(*mock_blacklist_store_
, GetTimeUntilReadyForUpload(_
))
384 .WillOnce(Return(false));
386 // Send the request. Empty data will be returned to the callback.
387 suggestions_service
->IssueRequestIfNoneOngoing(GURL(kSuggestionsURL
));
389 // (Testing only) wait until suggestion fetch is complete.
390 io_message_loop_
.RunUntilIdle();
392 // Expect no suggestions in the cache.
393 SuggestionsProfile empty_suggestions
;
394 EXPECT_FALSE(test_suggestions_store_
->LoadSuggestions(&empty_suggestions
));
397 TEST_F(SuggestionsServiceTest
, BlacklistURL
) {
398 scoped_ptr
<SuggestionsService
> suggestions_service(
399 CreateSuggestionsServiceWithMocks());
400 EXPECT_TRUE(suggestions_service
!= NULL
);
401 base::TimeDelta no_delay
= base::TimeDelta::FromSeconds(0);
402 suggestions_service
->set_blacklist_delay(no_delay
);
404 GURL
blacklist_url(kBlacklistUrl
);
405 std::string request_url
= GetExpectedBlacklistRequestUrl(blacklist_url
);
406 SuggestionsProfile suggestions_profile
= CreateSuggestionsProfile();
407 factory_
.SetFakeResponse(GURL(request_url
),
408 suggestions_profile
.SerializeAsString(),
409 net::HTTP_OK
, net::URLRequestStatus::SUCCESS
);
410 EXPECT_CALL(*mock_thumbnail_manager_
,
411 Initialize(EqualsProto(suggestions_profile
)));
413 // Expected calls to the blacklist store.
414 EXPECT_CALL(*mock_blacklist_store_
, BlacklistUrl(Eq(blacklist_url
)))
415 .WillOnce(Return(true));
416 EXPECT_CALL(*mock_blacklist_store_
, FilterSuggestions(_
));
417 EXPECT_CALL(*mock_blacklist_store_
, GetTimeUntilReadyForUpload(_
))
418 .WillOnce(DoAll(SetArgPointee
<0>(no_delay
), Return(true)))
419 .WillOnce(Return(false));
420 EXPECT_CALL(*mock_blacklist_store_
, GetCandidateForUpload(_
))
421 .WillOnce(DoAll(SetArgPointee
<0>(blacklist_url
), Return(true)));
422 EXPECT_CALL(*mock_blacklist_store_
, RemoveUrl(Eq(blacklist_url
)))
423 .WillOnce(Return(true));
425 Blacklist(suggestions_service
.get(), blacklist_url
);
427 // Wait on the upload task. This only works when the scheduling task is not
428 // for future execution (note how both the SuggestionsService's scheduling
429 // delay and the BlacklistStore's candidacy delay are zero). Then wait on
430 // the blacklist request, then again on the next blacklist scheduling task.
431 base::MessageLoop::current()->RunUntilIdle();
432 io_message_loop_
.RunUntilIdle();
433 base::MessageLoop::current()->RunUntilIdle();
435 // Ensure that CheckSuggestionsData() ran once.
436 EXPECT_EQ(1, suggestions_data_check_count_
);
437 EXPECT_FALSE(blacklisting_failed_
);
440 TEST_F(SuggestionsServiceTest
, BlacklistURLFails
) {
441 scoped_ptr
<SuggestionsService
> suggestions_service(
442 CreateSuggestionsServiceWithMocks());
443 EXPECT_TRUE(suggestions_service
!= NULL
);
444 GURL
blacklist_url(kBlacklistUrl
);
445 EXPECT_CALL(*mock_blacklist_store_
, BlacklistUrl(Eq(blacklist_url
)))
446 .WillOnce(Return(false));
448 Blacklist(suggestions_service
.get(), blacklist_url
);
450 EXPECT_TRUE(blacklisting_failed_
);
451 EXPECT_EQ(0, suggestions_data_check_count_
);
454 // Initial blacklist request fails, triggering a second which succeeds.
455 TEST_F(SuggestionsServiceTest
, BlacklistURLRequestFails
) {
456 scoped_ptr
<SuggestionsService
> suggestions_service(
457 CreateSuggestionsServiceWithMocks());
458 EXPECT_TRUE(suggestions_service
!= NULL
);
459 base::TimeDelta no_delay
= base::TimeDelta::FromSeconds(0);
460 suggestions_service
->set_blacklist_delay(no_delay
);
462 GURL
blacklist_url(kBlacklistUrl
);
463 std::string request_url
= GetExpectedBlacklistRequestUrl(blacklist_url
);
464 GURL
blacklist_url_alt(kBlacklistUrlAlt
);
465 std::string request_url_alt
= GetExpectedBlacklistRequestUrl(
467 SuggestionsProfile suggestions_profile
= CreateSuggestionsProfile();
469 // Note: we want to set the response for the blacklist URL to first
470 // succeed, then fail. This doesn't seem possible. For simplicity of testing,
471 // we'll pretend the URL changed in the BlacklistStore between the first and
472 // the second request, and adjust expectations accordingly.
473 factory_
.SetFakeResponse(GURL(request_url
), "irrelevant", net::HTTP_OK
,
474 net::URLRequestStatus::FAILED
);
475 factory_
.SetFakeResponse(GURL(request_url_alt
),
476 suggestions_profile
.SerializeAsString(),
477 net::HTTP_OK
, net::URLRequestStatus::SUCCESS
);
480 EXPECT_CALL(*mock_thumbnail_manager_
,
481 Initialize(EqualsProto(suggestions_profile
)));
482 EXPECT_CALL(*mock_blacklist_store_
, BlacklistUrl(Eq(blacklist_url
)))
483 .WillOnce(Return(true));
484 EXPECT_CALL(*mock_blacklist_store_
, FilterSuggestions(_
));
485 EXPECT_CALL(*mock_blacklist_store_
, GetTimeUntilReadyForUpload(_
))
486 .WillOnce(DoAll(SetArgPointee
<0>(no_delay
), Return(true)))
487 .WillOnce(DoAll(SetArgPointee
<0>(no_delay
), Return(true)))
488 .WillOnce(Return(false));
489 EXPECT_CALL(*mock_blacklist_store_
, GetCandidateForUpload(_
))
490 .WillOnce(DoAll(SetArgPointee
<0>(blacklist_url
), Return(true)))
491 .WillOnce(DoAll(SetArgPointee
<0>(blacklist_url_alt
), Return(true)));
492 EXPECT_CALL(*mock_blacklist_store_
, RemoveUrl(Eq(blacklist_url_alt
)))
493 .WillOnce(Return(true));
495 // Blacklist call, first request attempt.
496 Blacklist(suggestions_service
.get(), blacklist_url
);
497 EXPECT_EQ(1, suggestions_data_check_count_
);
498 EXPECT_FALSE(blacklisting_failed_
);
500 // Wait for the first scheduling, the first request, the second scheduling,
501 // second request and the third scheduling. Again, note that calling
502 // RunUntilIdle on the MessageLoop only works when the task is not posted for
504 base::MessageLoop::current()->RunUntilIdle();
505 io_message_loop_
.RunUntilIdle();
506 base::MessageLoop::current()->RunUntilIdle();
507 io_message_loop_
.RunUntilIdle();
508 base::MessageLoop::current()->RunUntilIdle();
511 TEST_F(SuggestionsServiceTest
, UndoBlacklistURL
) {
512 scoped_ptr
<SuggestionsService
> suggestions_service(
513 CreateSuggestionsServiceWithMocks());
514 EXPECT_TRUE(suggestions_service
!= NULL
);
515 // Ensure scheduling the request doesn't happen before undo.
516 base::TimeDelta delay
= base::TimeDelta::FromHours(1);
517 suggestions_service
->set_blacklist_delay(delay
);
518 SuggestionsProfile suggestions_profile
= CreateSuggestionsProfile();
519 GURL
blacklist_url(kBlacklistUrl
);
521 // Blacklist expectations.
522 EXPECT_CALL(*mock_blacklist_store_
, BlacklistUrl(Eq(blacklist_url
)))
523 .WillOnce(Return(true));
524 EXPECT_CALL(*mock_thumbnail_manager_
,
525 Initialize(EqualsProto(suggestions_profile
)))
527 EXPECT_CALL(*mock_blacklist_store_
, FilterSuggestions(_
))
529 EXPECT_CALL(*mock_blacklist_store_
, GetTimeUntilReadyForUpload(_
))
530 .WillOnce(DoAll(SetArgPointee
<0>(delay
), Return(true)));
531 // Undo expectations.
532 EXPECT_CALL(*mock_blacklist_store_
,
533 GetTimeUntilURLReadyForUpload(Eq(blacklist_url
), _
))
534 .WillOnce(DoAll(SetArgPointee
<1>(delay
), Return(true)));
535 EXPECT_CALL(*mock_blacklist_store_
, RemoveUrl(Eq(blacklist_url
)))
536 .WillOnce(Return(true));
538 Blacklist(suggestions_service
.get(), blacklist_url
);
539 UndoBlacklist(suggestions_service
.get(), blacklist_url
);
541 EXPECT_EQ(2, suggestions_data_check_count_
);
542 EXPECT_FALSE(blacklisting_failed_
);
543 EXPECT_FALSE(undo_blacklisting_failed_
);
547 TEST_F(SuggestionsServiceTest
, UndoBlacklistURLFailsIfNotInBlacklist
) {
548 UndoBlacklistURLFailsHelper(true);
551 TEST_F(SuggestionsServiceTest
, UndoBlacklistURLFailsIfAlreadyCandidate
) {
552 UndoBlacklistURLFailsHelper(false);
555 TEST_F(SuggestionsServiceTest
, GetBlacklistedUrl
) {
556 scoped_ptr
<GURL
> request_url
;
557 scoped_ptr
<net::FakeURLFetcher
> fetcher
;
560 // Not a blacklist request.
561 request_url
.reset(new GURL("http://not-blacklisting.com/a?b=c"));
562 fetcher
= CreateURLFetcher(*request_url
, NULL
, "", net::HTTP_OK
,
563 net::URLRequestStatus::SUCCESS
);
564 EXPECT_FALSE(SuggestionsService::GetBlacklistedUrl(*fetcher
, &retrieved_url
));
566 // An actual blacklist request.
567 string blacklisted_url
= "http://blacklisted.com/a?b=c&d=e";
568 string encoded_blacklisted_url
=
569 "http%3A%2F%2Fblacklisted.com%2Fa%3Fb%3Dc%26d%3De";
570 string
blacklist_request_prefix(kSuggestionsBlacklistURLPrefix
);
572 new GURL(blacklist_request_prefix
+ encoded_blacklisted_url
));
574 fetcher
= CreateURLFetcher(*request_url
, NULL
, "", net::HTTP_OK
,
575 net::URLRequestStatus::SUCCESS
);
576 EXPECT_TRUE(SuggestionsService::GetBlacklistedUrl(*fetcher
, &retrieved_url
));
577 EXPECT_EQ(blacklisted_url
, retrieved_url
.spec());
580 TEST_F(SuggestionsServiceTest
, UpdateBlacklistDelay
) {
581 scoped_ptr
<SuggestionsService
> suggestions_service(
582 CreateSuggestionsServiceWithMocks());
583 base::TimeDelta initial_delay
= suggestions_service
->blacklist_delay();
585 // Delay unchanged on success.
586 suggestions_service
->UpdateBlacklistDelay(true);
587 EXPECT_EQ(initial_delay
, suggestions_service
->blacklist_delay());
589 // Delay increases on failure.
590 suggestions_service
->UpdateBlacklistDelay(false);
591 EXPECT_GT(suggestions_service
->blacklist_delay(), initial_delay
);
593 // Delay resets on success.
594 suggestions_service
->UpdateBlacklistDelay(true);
595 EXPECT_EQ(initial_delay
, suggestions_service
->blacklist_delay());
598 TEST_F(SuggestionsServiceTest
, CheckDefaultTimeStamps
) {
599 scoped_ptr
<SuggestionsService
> suggestions_service(
600 CreateSuggestionsServiceWithMocks());
601 SuggestionsProfile suggestions
=
602 CreateSuggestionsProfileWithExpiryTimestamps();
603 suggestions_service
->SetDefaultExpiryTimestamp(&suggestions
,
605 EXPECT_EQ(kTestSetExpiry
, suggestions
.suggestions(0).expiry_ts());
606 EXPECT_EQ(kTestDefaultExpiry
, suggestions
.suggestions(1).expiry_ts());
608 } // namespace suggestions