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

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

Issue 766053010: SuggestionsService undo blacklist functionality. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address Mathieu's comments. Created 6 years 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
(...skipping 11 matching lines...) Expand all
22 #include "components/variations/variations_associated_data.h" 22 #include "components/variations/variations_associated_data.h"
23 #include "net/http/http_response_headers.h" 23 #include "net/http/http_response_headers.h"
24 #include "net/http/http_status_code.h" 24 #include "net/http/http_status_code.h"
25 #include "net/url_request/test_url_fetcher_factory.h" 25 #include "net/url_request/test_url_fetcher_factory.h"
26 #include "net/url_request/url_request_status.h" 26 #include "net/url_request/url_request_status.h"
27 #include "net/url_request/url_request_test_util.h" 27 #include "net/url_request/url_request_test_util.h"
28 #include "testing/gmock/include/gmock/gmock.h" 28 #include "testing/gmock/include/gmock/gmock.h"
29 #include "testing/gtest/include/gtest/gtest.h" 29 #include "testing/gtest/include/gtest/gtest.h"
30 30
31 using testing::DoAll; 31 using testing::DoAll;
32 using ::testing::AnyNumber;
32 using ::testing::Eq; 33 using ::testing::Eq;
33 using ::testing::Return; 34 using ::testing::Return;
34 using testing::SetArgPointee; 35 using testing::SetArgPointee;
35 using ::testing::NiceMock; 36 using ::testing::NiceMock;
36 using ::testing::StrictMock; 37 using ::testing::StrictMock;
37 using ::testing::_; 38 using ::testing::_;
38 39
39 namespace { 40 namespace {
40 41
41 const char kTestTitle[] = "a title"; 42 const char kTestTitle[] = "a title";
42 const char kTestUrl[] = "http://go.com"; 43 const char kTestUrl[] = "http://go.com";
43 const char kBlacklistUrl[] = "http://blacklist.com"; 44 const char kBlacklistUrl[] = "http://blacklist.com";
45 const char kBlacklistUrlAlt[] = "http://blacklist-atl.com";
44 const int64 kTestDefaultExpiry = 1402200000000000; 46 const int64 kTestDefaultExpiry = 1402200000000000;
45 const int64 kTestSetExpiry = 1404792000000000; 47 const int64 kTestSetExpiry = 1404792000000000;
46 48
47 scoped_ptr<net::FakeURLFetcher> CreateURLFetcher( 49 scoped_ptr<net::FakeURLFetcher> CreateURLFetcher(
48 const GURL& url, net::URLFetcherDelegate* delegate, 50 const GURL& url, net::URLFetcherDelegate* delegate,
49 const std::string& response_data, net::HttpStatusCode response_code, 51 const std::string& response_data, net::HttpStatusCode response_code,
50 net::URLRequestStatus::Status status) { 52 net::URLRequestStatus::Status status) {
51 scoped_ptr<net::FakeURLFetcher> fetcher(new net::FakeURLFetcher( 53 scoped_ptr<net::FakeURLFetcher> fetcher(new net::FakeURLFetcher(
52 url, delegate, response_data, response_code, status)); 54 url, delegate, response_data, response_code, status));
53 55
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
137 virtual ~MockImageManager() {} 139 virtual ~MockImageManager() {}
138 MOCK_METHOD1(Initialize, void(const SuggestionsProfile&)); 140 MOCK_METHOD1(Initialize, void(const SuggestionsProfile&));
139 MOCK_METHOD2(GetImageForURL, 141 MOCK_METHOD2(GetImageForURL,
140 void(const GURL&, 142 void(const GURL&,
141 base::Callback<void(const GURL&, const SkBitmap*)>)); 143 base::Callback<void(const GURL&, const SkBitmap*)>));
142 }; 144 };
143 145
144 class MockBlacklistStore : public suggestions::BlacklistStore { 146 class MockBlacklistStore : public suggestions::BlacklistStore {
145 public: 147 public:
146 MOCK_METHOD1(BlacklistUrl, bool(const GURL&)); 148 MOCK_METHOD1(BlacklistUrl, bool(const GURL&));
147 MOCK_METHOD1(GetFirstUrlFromBlacklist, bool(GURL*)); 149 MOCK_METHOD0(IsEmpty, bool());
150 MOCK_METHOD1(GetTimeUntilReadyForUpload, bool(base::TimeDelta*));
151 MOCK_METHOD2(GetTimeUntilURLReadyForUpload,
152 bool(const GURL&, base::TimeDelta*));
153 MOCK_METHOD1(GetCandidateForUpload, bool(GURL*));
148 MOCK_METHOD1(RemoveUrl, bool(const GURL&)); 154 MOCK_METHOD1(RemoveUrl, bool(const GURL&));
149 MOCK_METHOD1(FilterSuggestions, void(SuggestionsProfile*)); 155 MOCK_METHOD1(FilterSuggestions, void(SuggestionsProfile*));
150 }; 156 };
151 157
152 class SuggestionsServiceTest : public testing::Test { 158 class SuggestionsServiceTest : public testing::Test {
153 public: 159 public:
154 void CheckSuggestionsData(const SuggestionsProfile& suggestions_profile) { 160 void CheckSuggestionsData(const SuggestionsProfile& suggestions_profile) {
155 EXPECT_EQ(1, suggestions_profile.suggestions_size()); 161 EXPECT_EQ(1, suggestions_profile.suggestions_size());
156 EXPECT_EQ(kTestTitle, suggestions_profile.suggestions(0).title()); 162 EXPECT_EQ(kTestTitle, suggestions_profile.suggestions(0).title());
157 EXPECT_EQ(kTestUrl, suggestions_profile.suggestions(0).url()); 163 EXPECT_EQ(kTestUrl, suggestions_profile.suggestions(0).url());
158 ++suggestions_data_check_count_; 164 ++suggestions_data_check_count_;
159 } 165 }
160 166
167 void SetBlacklistFailure() {
168 blacklisting_failed_ = true;
169 }
170
171 void SetUndoBlacklistFailure() {
172 undo_blacklisting_failed_ = true;
173 }
174
161 void ExpectEmptySuggestionsProfile(const SuggestionsProfile& profile) { 175 void ExpectEmptySuggestionsProfile(const SuggestionsProfile& profile) {
162 EXPECT_EQ(0, profile.suggestions_size()); 176 EXPECT_EQ(0, profile.suggestions_size());
163 ++suggestions_empty_data_count_; 177 ++suggestions_empty_data_count_;
164 } 178 }
165 179
166 int suggestions_data_check_count_; 180 int suggestions_data_check_count_;
167 int suggestions_empty_data_count_; 181 int suggestions_empty_data_count_;
182 bool blacklisting_failed_;
183 bool undo_blacklisting_failed_;
168 184
169 protected: 185 protected:
170 SuggestionsServiceTest() 186 SuggestionsServiceTest()
171 : suggestions_data_check_count_(0), 187 : suggestions_data_check_count_(0),
172 suggestions_empty_data_count_(0), 188 suggestions_empty_data_count_(0),
189 blacklisting_failed_(false),
173 factory_(NULL, base::Bind(&CreateURLFetcher)), 190 factory_(NULL, base::Bind(&CreateURLFetcher)),
174 mock_thumbnail_manager_(NULL), 191 mock_thumbnail_manager_(NULL),
175 mock_blacklist_store_(NULL), 192 mock_blacklist_store_(NULL),
176 test_suggestions_store_(NULL) {} 193 test_suggestions_store_(NULL) {}
177 194
178 ~SuggestionsServiceTest() override {} 195 ~SuggestionsServiceTest() override {}
179 196
180 void SetUp() override { 197 void SetUp() override {
181 request_context_ = new net::TestURLRequestContextGetter( 198 request_context_ = new net::TestURLRequestContextGetter(
182 io_message_loop_.message_loop_proxy()); 199 io_message_loop_.message_loop_proxy());
(...skipping 29 matching lines...) Expand all
212 229
213 // Set up net::FakeURLFetcherFactory. 230 // Set up net::FakeURLFetcherFactory.
214 factory_.SetFakeResponse(GURL(kSuggestionsURL), 231 factory_.SetFakeResponse(GURL(kSuggestionsURL),
215 suggestions_profile.SerializeAsString(), 232 suggestions_profile.SerializeAsString(),
216 net::HTTP_OK, net::URLRequestStatus::SUCCESS); 233 net::HTTP_OK, net::URLRequestStatus::SUCCESS);
217 234
218 // Expectations. 235 // Expectations.
219 EXPECT_CALL(*mock_thumbnail_manager_, 236 EXPECT_CALL(*mock_thumbnail_manager_,
220 Initialize(EqualsProto(suggestions_profile))); 237 Initialize(EqualsProto(suggestions_profile)));
221 EXPECT_CALL(*mock_blacklist_store_, FilterSuggestions(_)); 238 EXPECT_CALL(*mock_blacklist_store_, FilterSuggestions(_));
222 EXPECT_CALL(*mock_blacklist_store_, GetFirstUrlFromBlacklist(_)) 239 EXPECT_CALL(*mock_blacklist_store_, GetTimeUntilReadyForUpload(_))
223 .WillOnce(Return(false)); 240 .WillOnce(Return(false));
224 241
225 // Send the request. The data will be returned to the callback. 242 // Send the request. The data will be returned to the callback.
226 suggestions_service->FetchSuggestionsData( 243 suggestions_service->FetchSuggestionsData(
227 sync_state, 244 sync_state,
228 base::Bind(&SuggestionsServiceTest::CheckSuggestionsData, 245 base::Bind(&SuggestionsServiceTest::CheckSuggestionsData,
229 base::Unretained(this))); 246 base::Unretained(this)));
230 247
231 // Ensure that CheckSuggestionsData() ran once. 248 // Ensure that CheckSuggestionsData() ran once.
232 EXPECT_EQ(1, suggestions_data_check_count_); 249 EXPECT_EQ(1, suggestions_data_check_count_);
(...skipping 12 matching lines...) Expand all
245 request_context_.get(), 262 request_context_.get(),
246 scoped_ptr<SuggestionsStore>(test_suggestions_store_), 263 scoped_ptr<SuggestionsStore>(test_suggestions_store_),
247 scoped_ptr<ImageManager>(mock_thumbnail_manager_), 264 scoped_ptr<ImageManager>(mock_thumbnail_manager_),
248 scoped_ptr<BlacklistStore>(mock_blacklist_store_)); 265 scoped_ptr<BlacklistStore>(mock_blacklist_store_));
249 } 266 }
250 267
251 void FillSuggestionsStore() { 268 void FillSuggestionsStore() {
252 test_suggestions_store_->StoreSuggestions(CreateSuggestionsProfile()); 269 test_suggestions_store_->StoreSuggestions(CreateSuggestionsProfile());
253 } 270 }
254 271
272 void Blacklist(SuggestionsService* suggestions_service, GURL url) {
273 suggestions_service->BlacklistURL(
274 url,
275 base::Bind(&SuggestionsServiceTest::CheckSuggestionsData,
276 base::Unretained(this)),
277 base::Bind(&SuggestionsServiceTest::SetBlacklistFailure,
278 base::Unretained(this)));
279 }
280
281 void UndoBlacklist(SuggestionsService* suggestions_service, GURL url) {
282 suggestions_service->UndoBlacklistURL(
283 url,
284 base::Bind(&SuggestionsServiceTest::CheckSuggestionsData,
285 base::Unretained(this)),
286 base::Bind(&SuggestionsServiceTest::SetUndoBlacklistFailure,
287 base::Unretained(this)));
288 }
289
290 // Helper for Undo failure tests. Depending on |is_uploaded|, tests either
291 // the case where the URL is no longer in the local blacklist or the case
292 // in which it's not yet candidate for upload.
293 void UndoBlacklistURLFailsHelper(bool is_uploaded) {
294 scoped_ptr<SuggestionsService> suggestions_service(
295 CreateSuggestionsServiceWithMocks());
296 EXPECT_TRUE(suggestions_service != NULL);
297 // Ensure scheduling the request doesn't happen before undo.
298 base::TimeDelta delay = base::TimeDelta::FromHours(1);
299 suggestions_service->set_blacklist_delay(delay);
300 SuggestionsProfile suggestions_profile = CreateSuggestionsProfile();
301 GURL blacklist_url(kBlacklistUrl);
302
303 // Blacklist expectations.
304 EXPECT_CALL(*mock_blacklist_store_, BlacklistUrl(Eq(blacklist_url)))
305 .WillOnce(Return(true));
306 EXPECT_CALL(*mock_thumbnail_manager_,
307 Initialize(EqualsProto(suggestions_profile)));
308 EXPECT_CALL(*mock_blacklist_store_, FilterSuggestions(_));
309 EXPECT_CALL(*mock_blacklist_store_, GetTimeUntilReadyForUpload(_))
310 .WillOnce(DoAll(SetArgPointee<0>(delay), Return(true)));
311 // Undo expectations.
312 if (is_uploaded) {
313 // URL is not in local blacklist.
314 EXPECT_CALL(*mock_blacklist_store_,
315 GetTimeUntilURLReadyForUpload(Eq(blacklist_url), _))
316 .WillOnce(Return(false));
317 } else {
318 // URL is not yet candidate for upload.
319 base::TimeDelta negative_delay = base::TimeDelta::FromHours(-1);
320 EXPECT_CALL(*mock_blacklist_store_,
321 GetTimeUntilURLReadyForUpload(Eq(blacklist_url), _))
322 .WillOnce(DoAll(SetArgPointee<1>(negative_delay), Return(true)));
323 }
324
325 Blacklist(suggestions_service.get(), blacklist_url);
326 UndoBlacklist(suggestions_service.get(), blacklist_url);
327
328 EXPECT_EQ(1, suggestions_data_check_count_);
329 EXPECT_FALSE(blacklisting_failed_);
330 EXPECT_TRUE(undo_blacklisting_failed_);
331 }
332
255 protected: 333 protected:
256 base::MessageLoopForIO io_message_loop_; 334 base::MessageLoopForIO io_message_loop_;
257 net::FakeURLFetcherFactory factory_; 335 net::FakeURLFetcherFactory factory_;
258 // Only used if the SuggestionsService is built with mocks. Not owned. 336 // Only used if the SuggestionsService is built with mocks. Not owned.
259 MockImageManager* mock_thumbnail_manager_; 337 MockImageManager* mock_thumbnail_manager_;
260 MockBlacklistStore* mock_blacklist_store_; 338 MockBlacklistStore* mock_blacklist_store_;
261 TestSuggestionsStore* test_suggestions_store_; 339 TestSuggestionsStore* test_suggestions_store_;
262 scoped_refptr<net::TestURLRequestContextGetter> request_context_; 340 scoped_refptr<net::TestURLRequestContextGetter> request_context_;
263 341
264 private: 342 private:
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
303 381
304 TEST_F(SuggestionsServiceTest, IssueRequestIfNoneOngoingError) { 382 TEST_F(SuggestionsServiceTest, IssueRequestIfNoneOngoingError) {
305 scoped_ptr<SuggestionsService> suggestions_service( 383 scoped_ptr<SuggestionsService> suggestions_service(
306 CreateSuggestionsServiceWithMocks()); 384 CreateSuggestionsServiceWithMocks());
307 EXPECT_TRUE(suggestions_service != NULL); 385 EXPECT_TRUE(suggestions_service != NULL);
308 386
309 // Fake a request error. 387 // Fake a request error.
310 factory_.SetFakeResponse(GURL(kSuggestionsURL), "irrelevant", net::HTTP_OK, 388 factory_.SetFakeResponse(GURL(kSuggestionsURL), "irrelevant", net::HTTP_OK,
311 net::URLRequestStatus::FAILED); 389 net::URLRequestStatus::FAILED);
312 390
313 EXPECT_CALL(*mock_blacklist_store_, GetFirstUrlFromBlacklist(_)) 391 EXPECT_CALL(*mock_blacklist_store_, GetTimeUntilReadyForUpload(_))
314 .WillOnce(Return(false)); 392 .WillOnce(Return(false));
315 393
316 // Send the request. Empty data will be returned to the callback. 394 // Send the request. Empty data will be returned to the callback.
317 suggestions_service->IssueRequestIfNoneOngoing(GURL(kSuggestionsURL)); 395 suggestions_service->IssueRequestIfNoneOngoing(GURL(kSuggestionsURL));
318 396
319 // (Testing only) wait until suggestion fetch is complete. 397 // (Testing only) wait until suggestion fetch is complete.
320 io_message_loop_.RunUntilIdle(); 398 io_message_loop_.RunUntilIdle();
321 } 399 }
322 400
323 TEST_F(SuggestionsServiceTest, IssueRequestIfNoneOngoingResponseNotOK) { 401 TEST_F(SuggestionsServiceTest, IssueRequestIfNoneOngoingResponseNotOK) {
324 scoped_ptr<SuggestionsService> suggestions_service( 402 scoped_ptr<SuggestionsService> suggestions_service(
325 CreateSuggestionsServiceWithMocks()); 403 CreateSuggestionsServiceWithMocks());
326 EXPECT_TRUE(suggestions_service != NULL); 404 EXPECT_TRUE(suggestions_service != NULL);
327 405
328 // Add some suggestions in the cache. 406 // Add some suggestions in the cache.
329 FillSuggestionsStore(); 407 FillSuggestionsStore();
330 408
331 // Fake a non-200 response code. 409 // Fake a non-200 response code.
332 factory_.SetFakeResponse(GURL(kSuggestionsURL), "irrelevant", 410 factory_.SetFakeResponse(GURL(kSuggestionsURL), "irrelevant",
333 net::HTTP_BAD_REQUEST, 411 net::HTTP_BAD_REQUEST,
334 net::URLRequestStatus::SUCCESS); 412 net::URLRequestStatus::SUCCESS);
335 413
336 // Expect that an upload to the blacklist is scheduled. 414 // Expect that an upload to the blacklist is scheduled.
337 EXPECT_CALL(*mock_blacklist_store_, GetFirstUrlFromBlacklist(_)) 415 EXPECT_CALL(*mock_blacklist_store_, GetTimeUntilReadyForUpload(_))
338 .WillOnce(Return(false)); 416 .WillOnce(Return(false));
339 417
340 // Send the request. Empty data will be returned to the callback. 418 // Send the request. Empty data will be returned to the callback.
341 suggestions_service->IssueRequestIfNoneOngoing(GURL(kSuggestionsURL)); 419 suggestions_service->IssueRequestIfNoneOngoing(GURL(kSuggestionsURL));
342 420
343 // (Testing only) wait until suggestion fetch is complete. 421 // (Testing only) wait until suggestion fetch is complete.
344 io_message_loop_.RunUntilIdle(); 422 io_message_loop_.RunUntilIdle();
345 423
346 // Expect no suggestions in the cache. 424 // Expect no suggestions in the cache.
347 SuggestionsProfile empty_suggestions; 425 SuggestionsProfile empty_suggestions;
348 EXPECT_FALSE(test_suggestions_store_->LoadSuggestions(&empty_suggestions)); 426 EXPECT_FALSE(test_suggestions_store_->LoadSuggestions(&empty_suggestions));
349 } 427 }
350 428
351 TEST_F(SuggestionsServiceTest, BlacklistURL) { 429 TEST_F(SuggestionsServiceTest, BlacklistURL) {
352 scoped_ptr<SuggestionsService> suggestions_service( 430 scoped_ptr<SuggestionsService> suggestions_service(
353 CreateSuggestionsServiceWithMocks()); 431 CreateSuggestionsServiceWithMocks());
354 EXPECT_TRUE(suggestions_service != NULL); 432 EXPECT_TRUE(suggestions_service != NULL);
433 base::TimeDelta no_delay = base::TimeDelta::FromSeconds(0);
434 suggestions_service->set_blacklist_delay(no_delay);
355 435
356 GURL blacklist_url(kBlacklistUrl); 436 GURL blacklist_url(kBlacklistUrl);
357 std::string request_url = GetExpectedBlacklistRequestUrl(blacklist_url); 437 std::string request_url = GetExpectedBlacklistRequestUrl(blacklist_url);
358 SuggestionsProfile suggestions_profile = CreateSuggestionsProfile(); 438 SuggestionsProfile suggestions_profile = CreateSuggestionsProfile();
359 factory_.SetFakeResponse(GURL(request_url), 439 factory_.SetFakeResponse(GURL(request_url),
360 suggestions_profile.SerializeAsString(), 440 suggestions_profile.SerializeAsString(),
361 net::HTTP_OK, net::URLRequestStatus::SUCCESS); 441 net::HTTP_OK, net::URLRequestStatus::SUCCESS);
362
363 EXPECT_CALL(*mock_thumbnail_manager_, 442 EXPECT_CALL(*mock_thumbnail_manager_,
364 Initialize(EqualsProto(suggestions_profile))); 443 Initialize(EqualsProto(suggestions_profile)));
365 444
366 // Expected calls to the blacklist store. 445 // Expected calls to the blacklist store.
367 EXPECT_CALL(*mock_blacklist_store_, BlacklistUrl(Eq(blacklist_url))) 446 EXPECT_CALL(*mock_blacklist_store_, BlacklistUrl(Eq(blacklist_url)))
368 .WillOnce(Return(true)); 447 .WillOnce(Return(true));
448 EXPECT_CALL(*mock_blacklist_store_, FilterSuggestions(_));
449 EXPECT_CALL(*mock_blacklist_store_, GetTimeUntilReadyForUpload(_))
450 .WillOnce(DoAll(SetArgPointee<0>(no_delay), Return(true)))
451 .WillOnce(Return(false));
452 EXPECT_CALL(*mock_blacklist_store_, GetCandidateForUpload(_))
453 .WillOnce(DoAll(SetArgPointee<0>(blacklist_url), Return(true)));
369 EXPECT_CALL(*mock_blacklist_store_, RemoveUrl(Eq(blacklist_url))) 454 EXPECT_CALL(*mock_blacklist_store_, RemoveUrl(Eq(blacklist_url)))
370 .WillOnce(Return(true)); 455 .WillOnce(Return(true));
371 EXPECT_CALL(*mock_blacklist_store_, FilterSuggestions(_));
372 EXPECT_CALL(*mock_blacklist_store_, GetFirstUrlFromBlacklist(_))
373 .WillOnce(Return(false));
374 456
375 // Send the request. The data will be returned to the callback. 457 Blacklist(suggestions_service.get(), blacklist_url);
376 suggestions_service->BlacklistURL( 458
377 blacklist_url, base::Bind(&SuggestionsServiceTest::CheckSuggestionsData, 459 // Wait on the upload task. This only works when the scheduling task is not
378 base::Unretained(this))); 460 // for future execution (note how both the SuggestionsService's scheduling
461 // delay and the BlacklistStore's candidacy delay are zero). Then wait on
462 // the blacklist request, then again on the next blacklist scheduling task.
463 base::MessageLoop::current()->RunUntilIdle();
464 io_message_loop_.RunUntilIdle();
465 base::MessageLoop::current()->RunUntilIdle();
379 466
380 // Ensure that CheckSuggestionsData() ran once. 467 // Ensure that CheckSuggestionsData() ran once.
381 EXPECT_EQ(1, suggestions_data_check_count_); 468 EXPECT_EQ(1, suggestions_data_check_count_);
382 469 EXPECT_FALSE(blacklisting_failed_);
383 // (Testing only) wait until blacklist request is complete.
384 io_message_loop_.RunUntilIdle();
385 } 470 }
386 471
387 // Initial blacklist request fails, triggering a scheduled upload which
388 // succeeds.
389 TEST_F(SuggestionsServiceTest, BlacklistURLFails) { 472 TEST_F(SuggestionsServiceTest, BlacklistURLFails) {
390 scoped_ptr<SuggestionsService> suggestions_service( 473 scoped_ptr<SuggestionsService> suggestions_service(
391 CreateSuggestionsServiceWithMocks()); 474 CreateSuggestionsServiceWithMocks());
392 EXPECT_TRUE(suggestions_service != NULL); 475 EXPECT_TRUE(suggestions_service != NULL);
393 suggestions_service->set_blacklist_delay(0); // Don't wait during a test! 476 GURL blacklist_url(kBlacklistUrl);
477 EXPECT_CALL(*mock_blacklist_store_, BlacklistUrl(Eq(blacklist_url)))
478 .WillOnce(Return(false));
479
480 Blacklist(suggestions_service.get(), blacklist_url);
481
482 EXPECT_TRUE(blacklisting_failed_);
483 EXPECT_EQ(0, suggestions_data_check_count_);
484 }
485
486 // Initial blacklist request fails, triggering a second which succeeds.
487 TEST_F(SuggestionsServiceTest, BlacklistURLRequestFails) {
488 scoped_ptr<SuggestionsService> suggestions_service(
489 CreateSuggestionsServiceWithMocks());
490 EXPECT_TRUE(suggestions_service != NULL);
491 base::TimeDelta no_delay = base::TimeDelta::FromSeconds(0);
492 suggestions_service->set_blacklist_delay(no_delay);
493
494 GURL blacklist_url(kBlacklistUrl);
495 std::string request_url = GetExpectedBlacklistRequestUrl(blacklist_url);
496 GURL blacklist_url_alt(kBlacklistUrlAlt);
497 std::string request_url_alt = GetExpectedBlacklistRequestUrl(
498 blacklist_url_alt);
499 SuggestionsProfile suggestions_profile = CreateSuggestionsProfile();
500
501 // Note: we want to set the response for the blacklist URL to first
502 // succeed, then fail. This doesn't seem possible. For simplicity of testing,
503 // we'll pretend the URL changed in the BlacklistStore between the first and
504 // the second request, and adjust expectations accordingly.
505 factory_.SetFakeResponse(GURL(request_url), "irrelevant", net::HTTP_OK,
506 net::URLRequestStatus::FAILED);
507 factory_.SetFakeResponse(GURL(request_url_alt),
508 suggestions_profile.SerializeAsString(),
509 net::HTTP_OK, net::URLRequestStatus::SUCCESS);
510
511 // Expectations.
512 EXPECT_CALL(*mock_thumbnail_manager_,
513 Initialize(EqualsProto(suggestions_profile)));
514 EXPECT_CALL(*mock_blacklist_store_, BlacklistUrl(Eq(blacklist_url)))
515 .WillOnce(Return(true));
516 EXPECT_CALL(*mock_blacklist_store_, FilterSuggestions(_));
517 EXPECT_CALL(*mock_blacklist_store_, GetTimeUntilReadyForUpload(_))
518 .WillOnce(DoAll(SetArgPointee<0>(no_delay), Return(true)))
519 .WillOnce(DoAll(SetArgPointee<0>(no_delay), Return(true)))
520 .WillOnce(Return(false));
521 EXPECT_CALL(*mock_blacklist_store_, GetCandidateForUpload(_))
522 .WillOnce(DoAll(SetArgPointee<0>(blacklist_url), Return(true)))
523 .WillOnce(DoAll(SetArgPointee<0>(blacklist_url_alt), Return(true)));
524 EXPECT_CALL(*mock_blacklist_store_, RemoveUrl(Eq(blacklist_url_alt)))
525 .WillOnce(Return(true));
526
527 // Blacklist call, first request attempt.
528 Blacklist(suggestions_service.get(), blacklist_url);
529 EXPECT_EQ(1, suggestions_data_check_count_);
530 EXPECT_FALSE(blacklisting_failed_);
531
532 // Wait for the first scheduling, the first request, the second scheduling,
533 // second request and the third scheduling. Again, note that calling
534 // RunUntilIdle on the MessageLoop only works when the task is not posted for
535 // the future.
536 base::MessageLoop::current()->RunUntilIdle();
537 io_message_loop_.RunUntilIdle();
538 base::MessageLoop::current()->RunUntilIdle();
539 io_message_loop_.RunUntilIdle();
540 base::MessageLoop::current()->RunUntilIdle();
541 }
542
543 TEST_F(SuggestionsServiceTest, UndoBlacklistURL) {
544 scoped_ptr<SuggestionsService> suggestions_service(
545 CreateSuggestionsServiceWithMocks());
546 EXPECT_TRUE(suggestions_service != NULL);
547 // Ensure scheduling the request doesn't happen before undo.
548 base::TimeDelta delay = base::TimeDelta::FromHours(1);
549 suggestions_service->set_blacklist_delay(delay);
394 SuggestionsProfile suggestions_profile = CreateSuggestionsProfile(); 550 SuggestionsProfile suggestions_profile = CreateSuggestionsProfile();
395 GURL blacklist_url(kBlacklistUrl); 551 GURL blacklist_url(kBlacklistUrl);
396 552
397 // Expectations specific to the synchronous pass. 553 // Blacklist expectations.
398 EXPECT_CALL(*mock_blacklist_store_, BlacklistUrl(Eq(blacklist_url))) 554 EXPECT_CALL(*mock_blacklist_store_, BlacklistUrl(Eq(blacklist_url)))
399 .WillOnce(Return(true)); 555 .WillOnce(Return(true));
400 EXPECT_CALL(*mock_thumbnail_manager_, 556 EXPECT_CALL(*mock_thumbnail_manager_,
401 Initialize(EqualsProto(suggestions_profile))); 557 Initialize(EqualsProto(suggestions_profile)))
402 EXPECT_CALL(*mock_blacklist_store_, FilterSuggestions(_)); 558 .Times(AnyNumber());
559 EXPECT_CALL(*mock_blacklist_store_, FilterSuggestions(_))
560 .Times(AnyNumber());
561 EXPECT_CALL(*mock_blacklist_store_, GetTimeUntilReadyForUpload(_))
562 .WillOnce(DoAll(SetArgPointee<0>(delay), Return(true)));
563 // Undo expectations.
564 EXPECT_CALL(*mock_blacklist_store_,
565 GetTimeUntilURLReadyForUpload(Eq(blacklist_url), _))
566 .WillOnce(DoAll(SetArgPointee<1>(delay), Return(true)));
567 EXPECT_CALL(*mock_blacklist_store_, RemoveUrl(Eq(blacklist_url)))
568 .WillOnce(Return(true));
403 569
404 // Expectations specific to the second request. 570 Blacklist(suggestions_service.get(), blacklist_url);
405 EXPECT_CALL(*mock_blacklist_store_, RemoveUrl(Eq(blacklist_url))) 571 UndoBlacklist(suggestions_service.get(), blacklist_url);
406 .WillOnce(Return(true));
407 572
408 // Expectations pertaining to both requests. 573 EXPECT_EQ(2, suggestions_data_check_count_);
409 EXPECT_CALL(*mock_blacklist_store_, GetFirstUrlFromBlacklist(_)) 574 EXPECT_FALSE(blacklisting_failed_);
410 .WillOnce(Return(true)) 575 EXPECT_FALSE(undo_blacklisting_failed_);
411 .WillOnce(DoAll(SetArgPointee<0>(blacklist_url), Return(true))) 576 }
412 .WillOnce(Return(false));
413 577
414 // Set up behavior for the first call to blacklist.
415 std::string request_url = GetExpectedBlacklistRequestUrl(blacklist_url);
416 factory_.SetFakeResponse(GURL(request_url), "irrelevant", net::HTTP_OK,
417 net::URLRequestStatus::FAILED);
418 578
419 // Send the request. The data will be returned to the callback immediately. 579 TEST_F(SuggestionsServiceTest, UndoBlacklistURLFailsIfNotInBlacklist) {
420 suggestions_service->BlacklistURL( 580 UndoBlacklistURLFailsHelper(true);
421 blacklist_url, base::Bind(&SuggestionsServiceTest::CheckSuggestionsData, 581 }
422 base::Unretained(this)));
423 582
424 // Ensure that CheckSuggestionsData() ran once. 583 TEST_F(SuggestionsServiceTest, UndoBlacklistURLFailsIfAlreadyCandidate) {
425 EXPECT_EQ(1, suggestions_data_check_count_); 584 UndoBlacklistURLFailsHelper(false);
426
427 // We can now set up behavior for the second call to blacklist.
428 factory_.SetFakeResponse(GURL(request_url),
429 suggestions_profile.SerializeAsString(),
430 net::HTTP_OK, net::URLRequestStatus::SUCCESS);
431
432 // Wait until first request is complete.
433 io_message_loop_.RunUntilIdle();
434 // ... Other task gets posted to the message loop.
435 base::MessageLoop::current()->RunUntilIdle();
436 // ... And completes.
437 io_message_loop_.RunUntilIdle();
438 } 585 }
439 586
440 TEST_F(SuggestionsServiceTest, GetBlacklistedUrl) { 587 TEST_F(SuggestionsServiceTest, GetBlacklistedUrl) {
441 scoped_ptr<GURL> request_url; 588 scoped_ptr<GURL> request_url;
442 scoped_ptr<net::FakeURLFetcher> fetcher; 589 scoped_ptr<net::FakeURLFetcher> fetcher;
443 GURL retrieved_url; 590 GURL retrieved_url;
444 591
445 // Not a blacklist request. 592 // Not a blacklist request.
446 request_url.reset(new GURL("http://not-blacklisting.com/a?b=c")); 593 request_url.reset(new GURL("http://not-blacklisting.com/a?b=c"));
447 fetcher = CreateURLFetcher(*request_url, NULL, "", net::HTTP_OK, 594 fetcher = CreateURLFetcher(*request_url, NULL, "", net::HTTP_OK,
(...skipping 10 matching lines...) Expand all
458 fetcher.reset(); 605 fetcher.reset();
459 fetcher = CreateURLFetcher(*request_url, NULL, "", net::HTTP_OK, 606 fetcher = CreateURLFetcher(*request_url, NULL, "", net::HTTP_OK,
460 net::URLRequestStatus::SUCCESS); 607 net::URLRequestStatus::SUCCESS);
461 EXPECT_TRUE(SuggestionsService::GetBlacklistedUrl(*fetcher, &retrieved_url)); 608 EXPECT_TRUE(SuggestionsService::GetBlacklistedUrl(*fetcher, &retrieved_url));
462 EXPECT_EQ(blacklisted_url, retrieved_url.spec()); 609 EXPECT_EQ(blacklisted_url, retrieved_url.spec());
463 } 610 }
464 611
465 TEST_F(SuggestionsServiceTest, UpdateBlacklistDelay) { 612 TEST_F(SuggestionsServiceTest, UpdateBlacklistDelay) {
466 scoped_ptr<SuggestionsService> suggestions_service( 613 scoped_ptr<SuggestionsService> suggestions_service(
467 CreateSuggestionsServiceWithMocks()); 614 CreateSuggestionsServiceWithMocks());
468 int initial_delay = suggestions_service->blacklist_delay(); 615 base::TimeDelta initial_delay = suggestions_service->blacklist_delay();
469 616
470 // Delay unchanged on success. 617 // Delay unchanged on success.
471 suggestions_service->UpdateBlacklistDelay(true); 618 suggestions_service->UpdateBlacklistDelay(true);
472 EXPECT_EQ(initial_delay, suggestions_service->blacklist_delay()); 619 EXPECT_EQ(initial_delay, suggestions_service->blacklist_delay());
473 620
474 // Delay increases on failure. 621 // Delay increases on failure.
475 suggestions_service->UpdateBlacklistDelay(false); 622 suggestions_service->UpdateBlacklistDelay(false);
476 EXPECT_GT(suggestions_service->blacklist_delay(), initial_delay); 623 EXPECT_GT(suggestions_service->blacklist_delay(), initial_delay);
477 624
478 // Delay resets on success. 625 // Delay resets on success.
479 suggestions_service->UpdateBlacklistDelay(true); 626 suggestions_service->UpdateBlacklistDelay(true);
480 EXPECT_EQ(initial_delay, suggestions_service->blacklist_delay()); 627 EXPECT_EQ(initial_delay, suggestions_service->blacklist_delay());
481 } 628 }
482 629
483 TEST_F(SuggestionsServiceTest, CheckDefaultTimeStamps) { 630 TEST_F(SuggestionsServiceTest, CheckDefaultTimeStamps) {
484 scoped_ptr<SuggestionsService> suggestions_service( 631 scoped_ptr<SuggestionsService> suggestions_service(
485 CreateSuggestionsServiceWithMocks()); 632 CreateSuggestionsServiceWithMocks());
486 SuggestionsProfile suggestions = 633 SuggestionsProfile suggestions =
487 CreateSuggestionsProfileWithExpiryTimestamps(); 634 CreateSuggestionsProfileWithExpiryTimestamps();
488 suggestions_service->SetDefaultExpiryTimestamp(&suggestions, 635 suggestions_service->SetDefaultExpiryTimestamp(&suggestions,
489 kTestDefaultExpiry); 636 kTestDefaultExpiry);
490 EXPECT_EQ(kTestSetExpiry, suggestions.suggestions(0).expiry_ts()); 637 EXPECT_EQ(kTestSetExpiry, suggestions.suggestions(0).expiry_ts());
491 EXPECT_EQ(kTestDefaultExpiry, suggestions.suggestions(1).expiry_ts()); 638 EXPECT_EQ(kTestDefaultExpiry, suggestions.suggestions(1).expiry_ts());
492 } 639 }
493 } // namespace suggestions 640 } // namespace suggestions
OLDNEW
« components/suggestions/blacklist_store.cc ('K') | « components/suggestions/suggestions_service.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698