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

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

Powered by Google App Engine
This is Rietveld 408576698