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

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

Powered by Google App Engine
This is Rietveld 408576698