OLD | NEW |
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 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 18 matching lines...) Expand all Loading... |
29 using ::testing::Return; | 29 using ::testing::Return; |
30 using testing::SetArgPointee; | 30 using testing::SetArgPointee; |
31 using ::testing::NiceMock; | 31 using ::testing::NiceMock; |
32 using ::testing::StrictMock; | 32 using ::testing::StrictMock; |
33 using ::testing::_; | 33 using ::testing::_; |
34 | 34 |
35 namespace { | 35 namespace { |
36 | 36 |
37 const char kTestTitle[] = "a title"; | 37 const char kTestTitle[] = "a title"; |
38 const char kTestUrl[] = "http://go.com"; | 38 const char kTestUrl[] = "http://go.com"; |
| 39 const char kTestFaviconUrl[] = |
| 40 "https://s2.googleusercontent.com/s2/favicons?domain_url=" |
| 41 "http://go.com&alt=s&sz=32"; |
39 const char kBlacklistUrl[] = "http://blacklist.com"; | 42 const char kBlacklistUrl[] = "http://blacklist.com"; |
40 const char kBlacklistUrlAlt[] = "http://blacklist-atl.com"; | 43 const char kBlacklistUrlAlt[] = "http://blacklist-atl.com"; |
41 const int64 kTestDefaultExpiry = 1402200000000000; | 44 const int64 kTestDefaultExpiry = 1402200000000000; |
42 const int64 kTestSetExpiry = 1404792000000000; | 45 const int64 kTestSetExpiry = 1404792000000000; |
43 | 46 |
44 scoped_ptr<net::FakeURLFetcher> CreateURLFetcher( | 47 scoped_ptr<net::FakeURLFetcher> CreateURLFetcher( |
45 const GURL& url, net::URLFetcherDelegate* delegate, | 48 const GURL& url, net::URLFetcherDelegate* delegate, |
46 const std::string& response_data, net::HttpStatusCode response_code, | 49 const std::string& response_data, net::HttpStatusCode response_code, |
47 net::URLRequestStatus::Status status) { | 50 net::URLRequestStatus::Status status) { |
48 scoped_ptr<net::FakeURLFetcher> fetcher(new net::FakeURLFetcher( | 51 scoped_ptr<net::FakeURLFetcher> fetcher(new net::FakeURLFetcher( |
49 url, delegate, response_data, response_code, status)); | 52 url, delegate, response_data, response_code, status)); |
50 | 53 |
51 if (response_code == net::HTTP_OK) { | 54 if (response_code == net::HTTP_OK) { |
52 scoped_refptr<net::HttpResponseHeaders> download_headers( | 55 scoped_refptr<net::HttpResponseHeaders> download_headers( |
53 new net::HttpResponseHeaders("")); | 56 new net::HttpResponseHeaders("")); |
54 download_headers->AddHeader("Content-Type: text/html"); | 57 download_headers->AddHeader("Content-Type: text/html"); |
55 fetcher->set_response_headers(download_headers); | 58 fetcher->set_response_headers(download_headers); |
56 } | 59 } |
57 return fetcher.Pass(); | 60 return fetcher.Pass(); |
58 } | 61 } |
59 | 62 |
60 std::string GetExpectedBlacklistRequestUrl(const GURL& blacklist_url) { | 63 std::string GetExpectedBlacklistRequestUrl(const GURL& blacklist_url) { |
61 std::stringstream request_url; | 64 std::stringstream request_url; |
62 request_url << "https://www.google.com/chromesuggestions/blacklist?t=2&url=" | 65 request_url << suggestions::kSuggestionsBlacklistURLPrefix |
63 << net::EscapeQueryParamValue(blacklist_url.spec(), true); | 66 << net::EscapeQueryParamValue(blacklist_url.spec(), true); |
64 return request_url.str(); | 67 return request_url.str(); |
65 } | 68 } |
66 | 69 |
67 // GMock matcher for protobuf equality. | 70 // GMock matcher for protobuf equality. |
68 MATCHER_P(EqualsProto, message, "") { | 71 MATCHER_P(EqualsProto, message, "") { |
69 // This implementation assumes protobuf serialization is deterministic, which | 72 // This implementation assumes protobuf serialization is deterministic, which |
70 // is true in practice but technically not something that code is supposed | 73 // is true in practice but technically not something that code is supposed |
71 // to rely on. However, it vastly simplifies the implementation. | 74 // to rely on. However, it vastly simplifies the implementation. |
72 std::string expected_serialized, actual_serialized; | 75 std::string expected_serialized, actual_serialized; |
(...skipping 29 matching lines...) Expand all Loading... |
102 | 105 |
103 return profile; | 106 return profile; |
104 } | 107 } |
105 | 108 |
106 class TestSuggestionsStore : public suggestions::SuggestionsStore { | 109 class TestSuggestionsStore : public suggestions::SuggestionsStore { |
107 public: | 110 public: |
108 TestSuggestionsStore() { | 111 TestSuggestionsStore() { |
109 cached_suggestions = CreateSuggestionsProfile(); | 112 cached_suggestions = CreateSuggestionsProfile(); |
110 } | 113 } |
111 bool LoadSuggestions(SuggestionsProfile* suggestions) override { | 114 bool LoadSuggestions(SuggestionsProfile* suggestions) override { |
112 if (cached_suggestions.suggestions_size()) { | 115 suggestions->CopyFrom(cached_suggestions); |
113 *suggestions = cached_suggestions; | 116 return cached_suggestions.suggestions_size(); |
114 return true; | |
115 } | |
116 return false; | |
117 } | 117 } |
118 bool StoreSuggestions(const SuggestionsProfile& suggestions) | 118 bool StoreSuggestions(const SuggestionsProfile& suggestions) |
119 override { | 119 override { |
120 cached_suggestions = suggestions; | 120 cached_suggestions.CopyFrom(suggestions); |
121 return true; | 121 return true; |
122 } | 122 } |
123 void ClearSuggestions() override { | 123 void ClearSuggestions() override { |
124 cached_suggestions = SuggestionsProfile(); | 124 cached_suggestions = SuggestionsProfile(); |
125 } | 125 } |
126 | 126 |
127 SuggestionsProfile cached_suggestions; | 127 SuggestionsProfile cached_suggestions; |
128 }; | 128 }; |
129 | 129 |
130 class MockImageManager : public suggestions::ImageManager { | 130 class MockImageManager : public suggestions::ImageManager { |
131 public: | 131 public: |
132 MockImageManager() {} | 132 MockImageManager() {} |
133 virtual ~MockImageManager() {} | 133 virtual ~MockImageManager() {} |
134 MOCK_METHOD1(Initialize, void(const SuggestionsProfile&)); | 134 MOCK_METHOD1(Initialize, void(const SuggestionsProfile&)); |
135 MOCK_METHOD2(GetImageForURL, | 135 MOCK_METHOD2(GetImageForURL, |
136 void(const GURL&, | 136 void(const GURL&, |
137 base::Callback<void(const GURL&, const SkBitmap*)>)); | 137 base::Callback<void(const GURL&, const SkBitmap*)>)); |
138 }; | 138 }; |
139 | 139 |
140 class MockBlacklistStore : public suggestions::BlacklistStore { | 140 class MockBlacklistStore : public suggestions::BlacklistStore { |
141 public: | 141 public: |
142 MOCK_METHOD1(BlacklistUrl, bool(const GURL&)); | 142 MOCK_METHOD1(BlacklistUrl, bool(const GURL&)); |
143 MOCK_METHOD0(IsEmpty, bool()); | 143 MOCK_METHOD0(IsEmpty, bool()); |
144 MOCK_METHOD1(GetTimeUntilReadyForUpload, bool(base::TimeDelta*)); | 144 MOCK_METHOD1(GetTimeUntilReadyForUpload, bool(base::TimeDelta*)); |
145 MOCK_METHOD2(GetTimeUntilURLReadyForUpload, | 145 MOCK_METHOD2(GetTimeUntilURLReadyForUpload, |
146 bool(const GURL&, base::TimeDelta*)); | 146 bool(const GURL&, base::TimeDelta*)); |
147 MOCK_METHOD1(GetCandidateForUpload, bool(GURL*)); | 147 MOCK_METHOD1(GetCandidateForUpload, bool(GURL*)); |
148 MOCK_METHOD1(RemoveUrl, bool(const GURL&)); | 148 MOCK_METHOD1(RemoveUrl, bool(const GURL&)); |
149 MOCK_METHOD1(FilterSuggestions, void(SuggestionsProfile*)); | 149 MOCK_METHOD1(FilterSuggestions, void(SuggestionsProfile*)); |
| 150 MOCK_METHOD0(ClearBlacklist, void()); |
150 }; | 151 }; |
151 | 152 |
152 class SuggestionsServiceTest : public testing::Test { | 153 class SuggestionsServiceTest : public testing::Test { |
153 public: | 154 public: |
154 void CheckSuggestionsData(const SuggestionsProfile& suggestions_profile) { | 155 void CheckCallback(const SuggestionsProfile& suggestions_profile) { |
| 156 ++suggestions_data_callback_count_; |
| 157 } |
| 158 |
| 159 void CheckSuggestionsData() { |
| 160 SuggestionsProfile suggestions_profile; |
| 161 test_suggestions_store_->LoadSuggestions(&suggestions_profile); |
155 EXPECT_EQ(1, suggestions_profile.suggestions_size()); | 162 EXPECT_EQ(1, suggestions_profile.suggestions_size()); |
156 EXPECT_EQ(kTestTitle, suggestions_profile.suggestions(0).title()); | 163 EXPECT_EQ(kTestTitle, suggestions_profile.suggestions(0).title()); |
157 EXPECT_EQ(kTestUrl, suggestions_profile.suggestions(0).url()); | 164 EXPECT_EQ(kTestUrl, suggestions_profile.suggestions(0).url()); |
158 ++suggestions_data_check_count_; | 165 EXPECT_EQ(kTestFaviconUrl, |
| 166 suggestions_profile.suggestions(0).favicon_url()); |
159 } | 167 } |
160 | 168 |
161 void SetBlacklistFailure() { | 169 void SetBlacklistFailure() { |
162 blacklisting_failed_ = true; | 170 blacklisting_failed_ = true; |
163 } | 171 } |
164 | 172 |
165 void SetUndoBlacklistFailure() { | 173 void SetUndoBlacklistFailure() { |
166 undo_blacklisting_failed_ = true; | 174 undo_blacklisting_failed_ = true; |
167 } | 175 } |
168 | 176 |
169 void ExpectEmptySuggestionsProfile(const SuggestionsProfile& profile) { | 177 void ExpectEmptySuggestionsProfile(const SuggestionsProfile& profile) { |
170 EXPECT_EQ(0, profile.suggestions_size()); | 178 EXPECT_EQ(0, profile.suggestions_size()); |
171 ++suggestions_empty_data_count_; | 179 ++suggestions_empty_data_count_; |
172 } | 180 } |
173 | 181 |
174 int suggestions_data_check_count_; | 182 int suggestions_data_callback_count_; |
175 int suggestions_empty_data_count_; | 183 int suggestions_empty_data_count_; |
176 bool blacklisting_failed_; | 184 bool blacklisting_failed_; |
177 bool undo_blacklisting_failed_; | 185 bool undo_blacklisting_failed_; |
178 | 186 |
179 protected: | 187 protected: |
180 SuggestionsServiceTest() | 188 SuggestionsServiceTest() |
181 : suggestions_data_check_count_(0), | 189 : suggestions_data_callback_count_(0), |
182 suggestions_empty_data_count_(0), | 190 suggestions_empty_data_count_(0), |
183 blacklisting_failed_(false), | 191 blacklisting_failed_(false), |
184 undo_blacklisting_failed_(false), | 192 undo_blacklisting_failed_(false), |
185 factory_(NULL, base::Bind(&CreateURLFetcher)), | 193 factory_(NULL, base::Bind(&CreateURLFetcher)), |
186 mock_thumbnail_manager_(NULL), | 194 mock_thumbnail_manager_(NULL), |
187 mock_blacklist_store_(NULL), | 195 mock_blacklist_store_(NULL), |
188 test_suggestions_store_(NULL) {} | 196 test_suggestions_store_(NULL) {} |
189 | 197 |
190 ~SuggestionsServiceTest() override {} | 198 ~SuggestionsServiceTest() override {} |
191 | 199 |
192 void SetUp() override { | 200 void SetUp() override { |
193 request_context_ = | 201 request_context_ = |
194 new net::TestURLRequestContextGetter(io_message_loop_.task_runner()); | 202 new net::TestURLRequestContextGetter(io_message_loop_.task_runner()); |
195 } | 203 } |
196 | 204 |
197 void FetchSuggestionsDataHelper(SyncState sync_state) { | 205 void FetchSuggestionsDataHelper(SyncState sync_state) { |
198 scoped_ptr<SuggestionsService> suggestions_service( | 206 scoped_ptr<SuggestionsService> suggestions_service( |
199 CreateSuggestionsServiceWithMocks()); | 207 CreateSuggestionsServiceWithMocks()); |
200 EXPECT_TRUE(suggestions_service != NULL); | 208 EXPECT_TRUE(suggestions_service != NULL); |
201 | 209 |
202 // Add some suggestions in the cache. | 210 SuggestionsProfile suggestions_profile = CreateSuggestionsProfile(); |
203 FillSuggestionsStore(); | |
204 SuggestionsProfile suggestions_profile; | |
205 test_suggestions_store_->LoadSuggestions(&suggestions_profile); | |
206 | 211 |
207 // Set up net::FakeURLFetcherFactory. | 212 // Set up net::FakeURLFetcherFactory. |
208 factory_.SetFakeResponse(GURL(kSuggestionsURL), | 213 factory_.SetFakeResponse(GURL(kSuggestionsURL), |
209 suggestions_profile.SerializeAsString(), | 214 suggestions_profile.SerializeAsString(), |
210 net::HTTP_OK, net::URLRequestStatus::SUCCESS); | 215 net::HTTP_OK, net::URLRequestStatus::SUCCESS); |
211 | 216 |
212 // Expectations. | 217 // Expectations. |
213 EXPECT_CALL(*mock_thumbnail_manager_, | 218 EXPECT_CALL(*mock_thumbnail_manager_, |
214 Initialize(EqualsProto(suggestions_profile))); | 219 Initialize(EqualsProto(suggestions_profile))); |
215 EXPECT_CALL(*mock_blacklist_store_, FilterSuggestions(_)); | 220 EXPECT_CALL(*mock_blacklist_store_, FilterSuggestions(_)); |
216 EXPECT_CALL(*mock_blacklist_store_, GetTimeUntilReadyForUpload(_)) | 221 EXPECT_CALL(*mock_blacklist_store_, GetTimeUntilReadyForUpload(_)) |
217 .WillOnce(Return(false)); | 222 .WillOnce(Return(false)); |
218 | 223 |
219 // Send the request. The data will be returned to the callback. | 224 // Send the request. The data will be returned to the callback. |
220 suggestions_service->FetchSuggestionsData( | 225 suggestions_service->FetchSuggestionsData( |
221 sync_state, | 226 sync_state, base::Bind(&SuggestionsServiceTest::CheckCallback, |
222 base::Bind(&SuggestionsServiceTest::CheckSuggestionsData, | 227 base::Unretained(this))); |
223 base::Unretained(this))); | |
224 | 228 |
225 // Ensure that CheckSuggestionsData() ran once. | 229 // Ensure that CheckSuggestionsData() ran once. |
226 EXPECT_EQ(1, suggestions_data_check_count_); | 230 EXPECT_EQ(1, suggestions_data_callback_count_); |
227 | 231 |
228 // Let the network request run. | 232 // Let the network request run. |
229 io_message_loop_.RunUntilIdle(); | 233 io_message_loop_.RunUntilIdle(); |
| 234 |
| 235 CheckSuggestionsData(); |
230 } | 236 } |
231 | 237 |
232 SuggestionsService* CreateSuggestionsServiceWithMocks() { | 238 SuggestionsService* CreateSuggestionsServiceWithMocks() { |
233 // These objects are owned by the returned SuggestionsService, but we keep | 239 // These objects are owned by the returned SuggestionsService, but we keep |
234 // the pointer around for testing. | 240 // the pointer around for testing. |
235 test_suggestions_store_ = new TestSuggestionsStore(); | 241 test_suggestions_store_ = new TestSuggestionsStore(); |
236 mock_thumbnail_manager_ = new StrictMock<MockImageManager>(); | 242 mock_thumbnail_manager_ = new StrictMock<MockImageManager>(); |
237 mock_blacklist_store_ = new StrictMock<MockBlacklistStore>(); | 243 mock_blacklist_store_ = new StrictMock<MockBlacklistStore>(); |
238 return new SuggestionsService( | 244 return new SuggestionsService( |
239 request_context_.get(), | 245 request_context_.get(), |
240 scoped_ptr<SuggestionsStore>(test_suggestions_store_), | 246 scoped_ptr<SuggestionsStore>(test_suggestions_store_), |
241 scoped_ptr<ImageManager>(mock_thumbnail_manager_), | 247 scoped_ptr<ImageManager>(mock_thumbnail_manager_), |
242 scoped_ptr<BlacklistStore>(mock_blacklist_store_)); | 248 scoped_ptr<BlacklistStore>(mock_blacklist_store_)); |
243 } | 249 } |
244 | 250 |
245 void FillSuggestionsStore() { | |
246 test_suggestions_store_->StoreSuggestions(CreateSuggestionsProfile()); | |
247 } | |
248 | |
249 void Blacklist(SuggestionsService* suggestions_service, GURL url) { | 251 void Blacklist(SuggestionsService* suggestions_service, GURL url) { |
250 suggestions_service->BlacklistURL( | 252 suggestions_service->BlacklistURL( |
251 url, | 253 url, base::Bind(&SuggestionsServiceTest::CheckCallback, |
252 base::Bind(&SuggestionsServiceTest::CheckSuggestionsData, | 254 base::Unretained(this)), |
253 base::Unretained(this)), | |
254 base::Bind(&SuggestionsServiceTest::SetBlacklistFailure, | 255 base::Bind(&SuggestionsServiceTest::SetBlacklistFailure, |
255 base::Unretained(this))); | 256 base::Unretained(this))); |
256 } | 257 } |
257 | 258 |
258 void UndoBlacklist(SuggestionsService* suggestions_service, GURL url) { | 259 void UndoBlacklist(SuggestionsService* suggestions_service, GURL url) { |
259 suggestions_service->UndoBlacklistURL( | 260 suggestions_service->UndoBlacklistURL( |
260 url, | 261 url, base::Bind(&SuggestionsServiceTest::CheckCallback, |
261 base::Bind(&SuggestionsServiceTest::CheckSuggestionsData, | 262 base::Unretained(this)), |
262 base::Unretained(this)), | |
263 base::Bind(&SuggestionsServiceTest::SetUndoBlacklistFailure, | 263 base::Bind(&SuggestionsServiceTest::SetUndoBlacklistFailure, |
264 base::Unretained(this))); | 264 base::Unretained(this))); |
265 } | 265 } |
266 | 266 |
267 // Helper for Undo failure tests. Depending on |is_uploaded|, tests either | 267 // Helper for Undo failure tests. Depending on |is_uploaded|, tests either |
268 // the case where the URL is no longer in the local blacklist or the case | 268 // the case where the URL is no longer in the local blacklist or the case |
269 // in which it's not yet candidate for upload. | 269 // in which it's not yet candidate for upload. |
270 void UndoBlacklistURLFailsHelper(bool is_uploaded) { | 270 void UndoBlacklistURLFailsHelper(bool is_uploaded) { |
271 scoped_ptr<SuggestionsService> suggestions_service( | 271 scoped_ptr<SuggestionsService> suggestions_service( |
272 CreateSuggestionsServiceWithMocks()); | 272 CreateSuggestionsServiceWithMocks()); |
(...skipping 22 matching lines...) Expand all Loading... |
295 // URL is not yet candidate for upload. | 295 // URL is not yet candidate for upload. |
296 base::TimeDelta negative_delay = base::TimeDelta::FromHours(-1); | 296 base::TimeDelta negative_delay = base::TimeDelta::FromHours(-1); |
297 EXPECT_CALL(*mock_blacklist_store_, | 297 EXPECT_CALL(*mock_blacklist_store_, |
298 GetTimeUntilURLReadyForUpload(Eq(blacklist_url), _)) | 298 GetTimeUntilURLReadyForUpload(Eq(blacklist_url), _)) |
299 .WillOnce(DoAll(SetArgPointee<1>(negative_delay), Return(true))); | 299 .WillOnce(DoAll(SetArgPointee<1>(negative_delay), Return(true))); |
300 } | 300 } |
301 | 301 |
302 Blacklist(suggestions_service.get(), blacklist_url); | 302 Blacklist(suggestions_service.get(), blacklist_url); |
303 UndoBlacklist(suggestions_service.get(), blacklist_url); | 303 UndoBlacklist(suggestions_service.get(), blacklist_url); |
304 | 304 |
305 EXPECT_EQ(1, suggestions_data_check_count_); | 305 EXPECT_EQ(1, suggestions_data_callback_count_); |
306 EXPECT_FALSE(blacklisting_failed_); | 306 EXPECT_FALSE(blacklisting_failed_); |
307 EXPECT_TRUE(undo_blacklisting_failed_); | 307 EXPECT_TRUE(undo_blacklisting_failed_); |
308 } | 308 } |
309 | 309 |
310 protected: | 310 protected: |
311 base::MessageLoopForIO io_message_loop_; | 311 base::MessageLoopForIO io_message_loop_; |
312 net::FakeURLFetcherFactory factory_; | 312 net::FakeURLFetcherFactory factory_; |
313 // Only used if the SuggestionsService is built with mocks. Not owned. | 313 // Only used if the SuggestionsService is built with mocks. Not owned. |
314 MockImageManager* mock_thumbnail_manager_; | 314 MockImageManager* mock_thumbnail_manager_; |
315 MockBlacklistStore* mock_blacklist_store_; | 315 MockBlacklistStore* mock_blacklist_store_; |
(...skipping 10 matching lines...) Expand all Loading... |
326 | 326 |
327 TEST_F(SuggestionsServiceTest, FetchSuggestionsDataSyncNotInitializedEnabled) { | 327 TEST_F(SuggestionsServiceTest, FetchSuggestionsDataSyncNotInitializedEnabled) { |
328 FetchSuggestionsDataHelper(NOT_INITIALIZED_ENABLED); | 328 FetchSuggestionsDataHelper(NOT_INITIALIZED_ENABLED); |
329 } | 329 } |
330 | 330 |
331 TEST_F(SuggestionsServiceTest, FetchSuggestionsDataSyncDisabled) { | 331 TEST_F(SuggestionsServiceTest, FetchSuggestionsDataSyncDisabled) { |
332 scoped_ptr<SuggestionsService> suggestions_service( | 332 scoped_ptr<SuggestionsService> suggestions_service( |
333 CreateSuggestionsServiceWithMocks()); | 333 CreateSuggestionsServiceWithMocks()); |
334 EXPECT_TRUE(suggestions_service != NULL); | 334 EXPECT_TRUE(suggestions_service != NULL); |
335 | 335 |
336 FillSuggestionsStore(); | |
337 | |
338 // Send the request. Cache is cleared and empty data will be returned to the | 336 // Send the request. Cache is cleared and empty data will be returned to the |
339 // callback. | 337 // callback. |
340 suggestions_service->FetchSuggestionsData( | 338 suggestions_service->FetchSuggestionsData( |
341 SYNC_OR_HISTORY_SYNC_DISABLED, | 339 SYNC_OR_HISTORY_SYNC_DISABLED, |
342 base::Bind(&SuggestionsServiceTest::ExpectEmptySuggestionsProfile, | 340 base::Bind(&SuggestionsServiceTest::ExpectEmptySuggestionsProfile, |
343 base::Unretained(this))); | 341 base::Unretained(this))); |
344 | 342 |
345 // Ensure that ExpectEmptySuggestionsProfile ran once. | 343 // Ensure that ExpectEmptySuggestionsProfile ran once. |
346 EXPECT_EQ(1, suggestions_empty_data_count_); | 344 EXPECT_EQ(1, suggestions_empty_data_count_); |
347 } | 345 } |
(...skipping 15 matching lines...) Expand all Loading... |
363 | 361 |
364 // (Testing only) wait until suggestion fetch is complete. | 362 // (Testing only) wait until suggestion fetch is complete. |
365 io_message_loop_.RunUntilIdle(); | 363 io_message_loop_.RunUntilIdle(); |
366 } | 364 } |
367 | 365 |
368 TEST_F(SuggestionsServiceTest, IssueRequestIfNoneOngoingResponseNotOK) { | 366 TEST_F(SuggestionsServiceTest, IssueRequestIfNoneOngoingResponseNotOK) { |
369 scoped_ptr<SuggestionsService> suggestions_service( | 367 scoped_ptr<SuggestionsService> suggestions_service( |
370 CreateSuggestionsServiceWithMocks()); | 368 CreateSuggestionsServiceWithMocks()); |
371 EXPECT_TRUE(suggestions_service != NULL); | 369 EXPECT_TRUE(suggestions_service != NULL); |
372 | 370 |
373 // Add some suggestions in the cache. | |
374 FillSuggestionsStore(); | |
375 | |
376 // Fake a non-200 response code. | 371 // Fake a non-200 response code. |
377 factory_.SetFakeResponse(GURL(kSuggestionsURL), "irrelevant", | 372 factory_.SetFakeResponse(GURL(kSuggestionsURL), "irrelevant", |
378 net::HTTP_BAD_REQUEST, | 373 net::HTTP_BAD_REQUEST, |
379 net::URLRequestStatus::SUCCESS); | 374 net::URLRequestStatus::SUCCESS); |
380 | 375 |
381 // Expect that an upload to the blacklist is scheduled. | 376 // Expect that an upload to the blacklist is scheduled. |
382 EXPECT_CALL(*mock_blacklist_store_, GetTimeUntilReadyForUpload(_)) | 377 EXPECT_CALL(*mock_blacklist_store_, GetTimeUntilReadyForUpload(_)) |
383 .WillOnce(Return(false)); | 378 .WillOnce(Return(false)); |
384 | 379 |
385 // Send the request. Empty data will be returned to the callback. | 380 // Send the request. Empty data will be returned to the callback. |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
424 Blacklist(suggestions_service.get(), blacklist_url); | 419 Blacklist(suggestions_service.get(), blacklist_url); |
425 | 420 |
426 // Wait on the upload task. This only works when the scheduling task is not | 421 // Wait on the upload task. This only works when the scheduling task is not |
427 // for future execution (note how both the SuggestionsService's scheduling | 422 // for future execution (note how both the SuggestionsService's scheduling |
428 // delay and the BlacklistStore's candidacy delay are zero). Then wait on | 423 // delay and the BlacklistStore's candidacy delay are zero). Then wait on |
429 // the blacklist request, then again on the next blacklist scheduling task. | 424 // the blacklist request, then again on the next blacklist scheduling task. |
430 base::MessageLoop::current()->RunUntilIdle(); | 425 base::MessageLoop::current()->RunUntilIdle(); |
431 io_message_loop_.RunUntilIdle(); | 426 io_message_loop_.RunUntilIdle(); |
432 base::MessageLoop::current()->RunUntilIdle(); | 427 base::MessageLoop::current()->RunUntilIdle(); |
433 | 428 |
434 // Ensure that CheckSuggestionsData() ran once. | 429 EXPECT_EQ(1, suggestions_data_callback_count_); |
435 EXPECT_EQ(1, suggestions_data_check_count_); | |
436 EXPECT_FALSE(blacklisting_failed_); | 430 EXPECT_FALSE(blacklisting_failed_); |
| 431 CheckSuggestionsData(); |
437 } | 432 } |
438 | 433 |
439 TEST_F(SuggestionsServiceTest, BlacklistURLFails) { | 434 TEST_F(SuggestionsServiceTest, BlacklistURLFails) { |
440 scoped_ptr<SuggestionsService> suggestions_service( | 435 scoped_ptr<SuggestionsService> suggestions_service( |
441 CreateSuggestionsServiceWithMocks()); | 436 CreateSuggestionsServiceWithMocks()); |
442 EXPECT_TRUE(suggestions_service != NULL); | 437 EXPECT_TRUE(suggestions_service != NULL); |
443 GURL blacklist_url(kBlacklistUrl); | 438 GURL blacklist_url(kBlacklistUrl); |
444 EXPECT_CALL(*mock_blacklist_store_, BlacklistUrl(Eq(blacklist_url))) | 439 EXPECT_CALL(*mock_blacklist_store_, BlacklistUrl(Eq(blacklist_url))) |
445 .WillOnce(Return(false)); | 440 .WillOnce(Return(false)); |
446 | 441 |
447 Blacklist(suggestions_service.get(), blacklist_url); | 442 Blacklist(suggestions_service.get(), blacklist_url); |
448 | 443 |
449 EXPECT_TRUE(blacklisting_failed_); | 444 EXPECT_TRUE(blacklisting_failed_); |
450 EXPECT_EQ(0, suggestions_data_check_count_); | 445 EXPECT_EQ(0, suggestions_data_callback_count_); |
451 } | 446 } |
452 | 447 |
453 // Initial blacklist request fails, triggering a second which succeeds. | 448 // Initial blacklist request fails, triggering a second which succeeds. |
454 TEST_F(SuggestionsServiceTest, BlacklistURLRequestFails) { | 449 TEST_F(SuggestionsServiceTest, BlacklistURLRequestFails) { |
455 scoped_ptr<SuggestionsService> suggestions_service( | 450 scoped_ptr<SuggestionsService> suggestions_service( |
456 CreateSuggestionsServiceWithMocks()); | 451 CreateSuggestionsServiceWithMocks()); |
457 EXPECT_TRUE(suggestions_service != NULL); | 452 EXPECT_TRUE(suggestions_service != NULL); |
458 base::TimeDelta no_delay = base::TimeDelta::FromSeconds(0); | 453 base::TimeDelta no_delay = base::TimeDelta::FromSeconds(0); |
459 suggestions_service->set_blacklist_delay(no_delay); | 454 suggestions_service->set_blacklist_delay(no_delay); |
460 | 455 |
(...skipping 25 matching lines...) Expand all Loading... |
486 .WillOnce(DoAll(SetArgPointee<0>(no_delay), Return(true))) | 481 .WillOnce(DoAll(SetArgPointee<0>(no_delay), Return(true))) |
487 .WillOnce(Return(false)); | 482 .WillOnce(Return(false)); |
488 EXPECT_CALL(*mock_blacklist_store_, GetCandidateForUpload(_)) | 483 EXPECT_CALL(*mock_blacklist_store_, GetCandidateForUpload(_)) |
489 .WillOnce(DoAll(SetArgPointee<0>(blacklist_url), Return(true))) | 484 .WillOnce(DoAll(SetArgPointee<0>(blacklist_url), Return(true))) |
490 .WillOnce(DoAll(SetArgPointee<0>(blacklist_url_alt), Return(true))); | 485 .WillOnce(DoAll(SetArgPointee<0>(blacklist_url_alt), Return(true))); |
491 EXPECT_CALL(*mock_blacklist_store_, RemoveUrl(Eq(blacklist_url_alt))) | 486 EXPECT_CALL(*mock_blacklist_store_, RemoveUrl(Eq(blacklist_url_alt))) |
492 .WillOnce(Return(true)); | 487 .WillOnce(Return(true)); |
493 | 488 |
494 // Blacklist call, first request attempt. | 489 // Blacklist call, first request attempt. |
495 Blacklist(suggestions_service.get(), blacklist_url); | 490 Blacklist(suggestions_service.get(), blacklist_url); |
496 EXPECT_EQ(1, suggestions_data_check_count_); | 491 EXPECT_EQ(1, suggestions_data_callback_count_); |
497 EXPECT_FALSE(blacklisting_failed_); | 492 EXPECT_FALSE(blacklisting_failed_); |
498 | 493 |
499 // Wait for the first scheduling, the first request, the second scheduling, | 494 // Wait for the first scheduling, the first request, the second scheduling, |
500 // second request and the third scheduling. Again, note that calling | 495 // second request and the third scheduling. Again, note that calling |
501 // RunUntilIdle on the MessageLoop only works when the task is not posted for | 496 // RunUntilIdle on the MessageLoop only works when the task is not posted for |
502 // the future. | 497 // the future. |
503 base::MessageLoop::current()->RunUntilIdle(); | 498 base::MessageLoop::current()->RunUntilIdle(); |
504 io_message_loop_.RunUntilIdle(); | 499 io_message_loop_.RunUntilIdle(); |
505 base::MessageLoop::current()->RunUntilIdle(); | 500 base::MessageLoop::current()->RunUntilIdle(); |
506 io_message_loop_.RunUntilIdle(); | 501 io_message_loop_.RunUntilIdle(); |
507 base::MessageLoop::current()->RunUntilIdle(); | 502 base::MessageLoop::current()->RunUntilIdle(); |
| 503 CheckSuggestionsData(); |
508 } | 504 } |
509 | 505 |
510 TEST_F(SuggestionsServiceTest, UndoBlacklistURL) { | 506 TEST_F(SuggestionsServiceTest, UndoBlacklistURL) { |
511 scoped_ptr<SuggestionsService> suggestions_service( | 507 scoped_ptr<SuggestionsService> suggestions_service( |
512 CreateSuggestionsServiceWithMocks()); | 508 CreateSuggestionsServiceWithMocks()); |
513 EXPECT_TRUE(suggestions_service != NULL); | 509 EXPECT_TRUE(suggestions_service != NULL); |
514 // Ensure scheduling the request doesn't happen before undo. | 510 // Ensure scheduling the request doesn't happen before undo. |
515 base::TimeDelta delay = base::TimeDelta::FromHours(1); | 511 base::TimeDelta delay = base::TimeDelta::FromHours(1); |
516 suggestions_service->set_blacklist_delay(delay); | 512 suggestions_service->set_blacklist_delay(delay); |
517 SuggestionsProfile suggestions_profile = CreateSuggestionsProfile(); | 513 SuggestionsProfile suggestions_profile = CreateSuggestionsProfile(); |
(...skipping 12 matching lines...) Expand all Loading... |
530 // Undo expectations. | 526 // Undo expectations. |
531 EXPECT_CALL(*mock_blacklist_store_, | 527 EXPECT_CALL(*mock_blacklist_store_, |
532 GetTimeUntilURLReadyForUpload(Eq(blacklist_url), _)) | 528 GetTimeUntilURLReadyForUpload(Eq(blacklist_url), _)) |
533 .WillOnce(DoAll(SetArgPointee<1>(delay), Return(true))); | 529 .WillOnce(DoAll(SetArgPointee<1>(delay), Return(true))); |
534 EXPECT_CALL(*mock_blacklist_store_, RemoveUrl(Eq(blacklist_url))) | 530 EXPECT_CALL(*mock_blacklist_store_, RemoveUrl(Eq(blacklist_url))) |
535 .WillOnce(Return(true)); | 531 .WillOnce(Return(true)); |
536 | 532 |
537 Blacklist(suggestions_service.get(), blacklist_url); | 533 Blacklist(suggestions_service.get(), blacklist_url); |
538 UndoBlacklist(suggestions_service.get(), blacklist_url); | 534 UndoBlacklist(suggestions_service.get(), blacklist_url); |
539 | 535 |
540 EXPECT_EQ(2, suggestions_data_check_count_); | 536 EXPECT_EQ(2, suggestions_data_callback_count_); |
541 EXPECT_FALSE(blacklisting_failed_); | 537 EXPECT_FALSE(blacklisting_failed_); |
542 EXPECT_FALSE(undo_blacklisting_failed_); | 538 EXPECT_FALSE(undo_blacklisting_failed_); |
543 } | 539 } |
544 | 540 |
| 541 TEST_F(SuggestionsServiceTest, ClearBlacklist) { |
| 542 scoped_ptr<SuggestionsService> suggestions_service( |
| 543 CreateSuggestionsServiceWithMocks()); |
| 544 EXPECT_TRUE(suggestions_service != NULL); |
| 545 // Ensure scheduling the request doesn't happen before undo. |
| 546 base::TimeDelta delay = base::TimeDelta::FromHours(1); |
| 547 suggestions_service->set_blacklist_delay(delay); |
| 548 SuggestionsProfile suggestions_profile = CreateSuggestionsProfile(); |
| 549 GURL blacklist_url(kBlacklistUrl); |
| 550 |
| 551 factory_.SetFakeResponse(GURL(suggestions::kSuggestionsBlacklistClearURL), |
| 552 suggestions_profile.SerializeAsString(), |
| 553 net::HTTP_OK, net::URLRequestStatus::SUCCESS); |
| 554 |
| 555 // Blacklist expectations. |
| 556 EXPECT_CALL(*mock_blacklist_store_, BlacklistUrl(Eq(blacklist_url))) |
| 557 .WillOnce(Return(true)); |
| 558 EXPECT_CALL(*mock_thumbnail_manager_, |
| 559 Initialize(EqualsProto(suggestions_profile))) |
| 560 .Times(AnyNumber()); |
| 561 EXPECT_CALL(*mock_blacklist_store_, FilterSuggestions(_)).Times(AnyNumber()); |
| 562 EXPECT_CALL(*mock_blacklist_store_, GetTimeUntilReadyForUpload(_)) |
| 563 .WillOnce(DoAll(SetArgPointee<0>(delay), Return(true))); |
| 564 EXPECT_CALL(*mock_blacklist_store_, ClearBlacklist()); |
| 565 |
| 566 Blacklist(suggestions_service.get(), blacklist_url); |
| 567 suggestions_service->ClearBlacklist(base::Bind( |
| 568 &SuggestionsServiceTest::CheckCallback, base::Unretained(this))); |
| 569 |
| 570 EXPECT_EQ(2, suggestions_data_callback_count_); |
| 571 EXPECT_FALSE(blacklisting_failed_); |
| 572 } |
545 | 573 |
546 TEST_F(SuggestionsServiceTest, UndoBlacklistURLFailsIfNotInBlacklist) { | 574 TEST_F(SuggestionsServiceTest, UndoBlacklistURLFailsIfNotInBlacklist) { |
547 UndoBlacklistURLFailsHelper(true); | 575 UndoBlacklistURLFailsHelper(true); |
548 } | 576 } |
549 | 577 |
550 TEST_F(SuggestionsServiceTest, UndoBlacklistURLFailsIfAlreadyCandidate) { | 578 TEST_F(SuggestionsServiceTest, UndoBlacklistURLFailsIfAlreadyCandidate) { |
551 UndoBlacklistURLFailsHelper(false); | 579 UndoBlacklistURLFailsHelper(false); |
552 } | 580 } |
553 | 581 |
554 TEST_F(SuggestionsServiceTest, GetBlacklistedUrl) { | 582 TEST_F(SuggestionsServiceTest, GetBlacklistedUrl) { |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
598 scoped_ptr<SuggestionsService> suggestions_service( | 626 scoped_ptr<SuggestionsService> suggestions_service( |
599 CreateSuggestionsServiceWithMocks()); | 627 CreateSuggestionsServiceWithMocks()); |
600 SuggestionsProfile suggestions = | 628 SuggestionsProfile suggestions = |
601 CreateSuggestionsProfileWithExpiryTimestamps(); | 629 CreateSuggestionsProfileWithExpiryTimestamps(); |
602 suggestions_service->SetDefaultExpiryTimestamp(&suggestions, | 630 suggestions_service->SetDefaultExpiryTimestamp(&suggestions, |
603 kTestDefaultExpiry); | 631 kTestDefaultExpiry); |
604 EXPECT_EQ(kTestSetExpiry, suggestions.suggestions(0).expiry_ts()); | 632 EXPECT_EQ(kTestSetExpiry, suggestions.suggestions(0).expiry_ts()); |
605 EXPECT_EQ(kTestDefaultExpiry, suggestions.suggestions(1).expiry_ts()); | 633 EXPECT_EQ(kTestDefaultExpiry, suggestions.suggestions(1).expiry_ts()); |
606 } | 634 } |
607 } // namespace suggestions | 635 } // namespace suggestions |
OLD | NEW |