| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "components/ntp_snippets/remote/remote_suggestions_provider.h" | |
| 6 | |
| 7 #include <memory> | |
| 8 #include <utility> | |
| 9 #include <vector> | |
| 10 | |
| 11 #include "base/command_line.h" | |
| 12 #include "base/files/file_path.h" | |
| 13 #include "base/files/scoped_temp_dir.h" | |
| 14 #include "base/json/json_reader.h" | |
| 15 #include "base/macros.h" | |
| 16 #include "base/memory/ptr_util.h" | |
| 17 #include "base/message_loop/message_loop.h" | |
| 18 #include "base/run_loop.h" | |
| 19 #include "base/strings/string_number_conversions.h" | |
| 20 #include "base/strings/string_util.h" | |
| 21 #include "base/strings/stringprintf.h" | |
| 22 #include "base/test/histogram_tester.h" | |
| 23 #include "base/test/simple_test_clock.h" | |
| 24 #include "base/threading/thread_task_runner_handle.h" | |
| 25 #include "base/time/time.h" | |
| 26 #include "components/image_fetcher/image_decoder.h" | |
| 27 #include "components/image_fetcher/image_fetcher.h" | |
| 28 #include "components/image_fetcher/image_fetcher_delegate.h" | |
| 29 #include "components/ntp_snippets/category.h" | |
| 30 #include "components/ntp_snippets/category_info.h" | |
| 31 #include "components/ntp_snippets/category_rankers/category_ranker.h" | |
| 32 #include "components/ntp_snippets/category_rankers/constant_category_ranker.h" | |
| 33 #include "components/ntp_snippets/category_rankers/mock_category_ranker.h" | |
| 34 #include "components/ntp_snippets/ntp_snippets_constants.h" | |
| 35 #include "components/ntp_snippets/pref_names.h" | |
| 36 #include "components/ntp_snippets/remote/ntp_snippet.h" | |
| 37 #include "components/ntp_snippets/remote/ntp_snippets_fetcher.h" | |
| 38 #include "components/ntp_snippets/remote/ntp_snippets_scheduler.h" | |
| 39 #include "components/ntp_snippets/remote/remote_suggestions_database.h" | |
| 40 #include "components/ntp_snippets/remote/test_utils.h" | |
| 41 #include "components/ntp_snippets/user_classifier.h" | |
| 42 #include "components/prefs/testing_pref_service.h" | |
| 43 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h" | |
| 44 #include "components/signin/core/browser/fake_signin_manager.h" | |
| 45 #include "components/variations/variations_params_manager.h" | |
| 46 #include "net/url_request/test_url_fetcher_factory.h" | |
| 47 #include "net/url_request/url_request_test_util.h" | |
| 48 #include "testing/gmock/include/gmock/gmock.h" | |
| 49 #include "testing/gtest/include/gtest/gtest.h" | |
| 50 #include "ui/gfx/image/image.h" | |
| 51 #include "ui/gfx/image/image_unittest_util.h" | |
| 52 | |
| 53 using image_fetcher::ImageFetcher; | |
| 54 using image_fetcher::ImageFetcherDelegate; | |
| 55 using testing::_; | |
| 56 using testing::ElementsAre; | |
| 57 using testing::Eq; | |
| 58 using testing::InSequence; | |
| 59 using testing::Invoke; | |
| 60 using testing::IsEmpty; | |
| 61 using testing::Mock; | |
| 62 using testing::MockFunction; | |
| 63 using testing::NiceMock; | |
| 64 using testing::Not; | |
| 65 using testing::SaveArg; | |
| 66 using testing::SizeIs; | |
| 67 using testing::StartsWith; | |
| 68 using testing::WithArgs; | |
| 69 | |
| 70 namespace ntp_snippets { | |
| 71 | |
| 72 namespace { | |
| 73 | |
| 74 MATCHER_P(IdEq, value, "") { | |
| 75 return arg->id() == value; | |
| 76 } | |
| 77 | |
| 78 MATCHER_P(IdWithinCategoryEq, expected_id, "") { | |
| 79 return arg.id().id_within_category() == expected_id; | |
| 80 } | |
| 81 | |
| 82 MATCHER_P(IsCategory, id, "") { | |
| 83 return arg.id() == static_cast<int>(id); | |
| 84 } | |
| 85 | |
| 86 MATCHER_P(HasCode, code, "") { | |
| 87 return arg.code == code; | |
| 88 } | |
| 89 | |
| 90 const base::Time::Exploded kDefaultCreationTime = {2015, 11, 4, 25, 13, 46, 45}; | |
| 91 const char kTestContentSuggestionsServerEndpoint[] = | |
| 92 "https://localunittest-chromecontentsuggestions-pa.googleapis.com/v1/" | |
| 93 "suggestions/fetch"; | |
| 94 const char kAPIKey[] = "fakeAPIkey"; | |
| 95 const char kTestContentSuggestionsServerWithAPIKey[] = | |
| 96 "https://localunittest-chromecontentsuggestions-pa.googleapis.com/v1/" | |
| 97 "suggestions/fetch?key=fakeAPIkey"; | |
| 98 | |
| 99 const char kSnippetUrl[] = "http://localhost/foobar"; | |
| 100 const char kSnippetTitle[] = "Title"; | |
| 101 const char kSnippetText[] = "Snippet"; | |
| 102 const char kSnippetSalientImage[] = "http://localhost/salient_image"; | |
| 103 const char kSnippetPublisherName[] = "Foo News"; | |
| 104 const char kSnippetAmpUrl[] = "http://localhost/amp"; | |
| 105 | |
| 106 const char kSnippetUrl2[] = "http://foo.com/bar"; | |
| 107 | |
| 108 const char kTestJsonDefaultCategoryTitle[] = "Some title"; | |
| 109 | |
| 110 const int kUnknownRemoteCategoryId = 1234; | |
| 111 | |
| 112 base::Time GetDefaultCreationTime() { | |
| 113 base::Time out_time; | |
| 114 EXPECT_TRUE(base::Time::FromUTCExploded(kDefaultCreationTime, &out_time)); | |
| 115 return out_time; | |
| 116 } | |
| 117 | |
| 118 base::Time GetDefaultExpirationTime() { | |
| 119 return base::Time::Now() + base::TimeDelta::FromHours(1); | |
| 120 } | |
| 121 | |
| 122 std::string GetCategoryJson(const std::vector<std::string>& snippets, | |
| 123 int remote_category_id, | |
| 124 const std::string& category_title) { | |
| 125 return base::StringPrintf( | |
| 126 " {\n" | |
| 127 " \"id\": %d,\n" | |
| 128 " \"localizedTitle\": \"%s\",\n" | |
| 129 " \"suggestions\": [%s]\n" | |
| 130 " }\n", | |
| 131 remote_category_id, category_title.c_str(), | |
| 132 base::JoinString(snippets, ", ").c_str()); | |
| 133 } | |
| 134 | |
| 135 class MultiCategoryJsonBuilder { | |
| 136 public: | |
| 137 MultiCategoryJsonBuilder() {} | |
| 138 | |
| 139 MultiCategoryJsonBuilder& AddCategoryWithCustomTitle( | |
| 140 const std::vector<std::string>& snippets, | |
| 141 int remote_category_id, | |
| 142 const std::string& category_title) { | |
| 143 category_json_.push_back( | |
| 144 GetCategoryJson(snippets, remote_category_id, category_title)); | |
| 145 return *this; | |
| 146 } | |
| 147 | |
| 148 MultiCategoryJsonBuilder& AddCategory( | |
| 149 const std::vector<std::string>& snippets, | |
| 150 int remote_category_id) { | |
| 151 return AddCategoryWithCustomTitle( | |
| 152 snippets, remote_category_id, | |
| 153 "Title" + base::IntToString(remote_category_id)); | |
| 154 } | |
| 155 | |
| 156 std::string Build() { | |
| 157 return base::StringPrintf( | |
| 158 "{\n" | |
| 159 " \"categories\": [\n" | |
| 160 "%s\n" | |
| 161 " ]\n" | |
| 162 "}\n", | |
| 163 base::JoinString(category_json_, " ,\n").c_str()); | |
| 164 } | |
| 165 | |
| 166 private: | |
| 167 std::vector<std::string> category_json_; | |
| 168 }; | |
| 169 | |
| 170 // TODO(vitaliii): Remove these convenience functions as they do not provide | |
| 171 // that much value and add additional redirections obscuring the code. | |
| 172 std::string GetTestJson(const std::vector<std::string>& snippets, | |
| 173 const std::string& category_title) { | |
| 174 return MultiCategoryJsonBuilder() | |
| 175 .AddCategoryWithCustomTitle(snippets, /*remote_category_id=*/1, | |
| 176 category_title) | |
| 177 .Build(); | |
| 178 } | |
| 179 | |
| 180 std::string GetTestJson(const std::vector<std::string>& snippets) { | |
| 181 return GetTestJson(snippets, kTestJsonDefaultCategoryTitle); | |
| 182 } | |
| 183 | |
| 184 std::string FormatTime(const base::Time& t) { | |
| 185 base::Time::Exploded x; | |
| 186 t.UTCExplode(&x); | |
| 187 return base::StringPrintf("%04d-%02d-%02dT%02d:%02d:%02dZ", x.year, x.month, | |
| 188 x.day_of_month, x.hour, x.minute, x.second); | |
| 189 } | |
| 190 | |
| 191 std::string GetSnippetWithUrlAndTimesAndSource( | |
| 192 const std::vector<std::string>& ids, | |
| 193 const std::string& url, | |
| 194 const base::Time& creation_time, | |
| 195 const base::Time& expiry_time, | |
| 196 const std::string& publisher, | |
| 197 const std::string& amp_url) { | |
| 198 const std::string ids_string = base::JoinString(ids, "\",\n \""); | |
| 199 return base::StringPrintf( | |
| 200 "{\n" | |
| 201 " \"ids\": [\n" | |
| 202 " \"%s\"\n" | |
| 203 " ],\n" | |
| 204 " \"title\": \"%s\",\n" | |
| 205 " \"snippet\": \"%s\",\n" | |
| 206 " \"fullPageUrl\": \"%s\",\n" | |
| 207 " \"creationTime\": \"%s\",\n" | |
| 208 " \"expirationTime\": \"%s\",\n" | |
| 209 " \"attribution\": \"%s\",\n" | |
| 210 " \"imageUrl\": \"%s\",\n" | |
| 211 " \"ampUrl\": \"%s\"\n" | |
| 212 " }", | |
| 213 ids_string.c_str(), kSnippetTitle, kSnippetText, url.c_str(), | |
| 214 FormatTime(creation_time).c_str(), FormatTime(expiry_time).c_str(), | |
| 215 publisher.c_str(), kSnippetSalientImage, amp_url.c_str()); | |
| 216 } | |
| 217 | |
| 218 std::string GetSnippetWithSources(const std::string& source_url, | |
| 219 const std::string& publisher, | |
| 220 const std::string& amp_url) { | |
| 221 return GetSnippetWithUrlAndTimesAndSource( | |
| 222 {kSnippetUrl}, source_url, GetDefaultCreationTime(), | |
| 223 GetDefaultExpirationTime(), publisher, amp_url); | |
| 224 } | |
| 225 | |
| 226 std::string GetSnippetWithUrlAndTimes(const std::string& url, | |
| 227 const base::Time& content_creation_time, | |
| 228 const base::Time& expiry_time) { | |
| 229 return GetSnippetWithUrlAndTimesAndSource({url}, url, content_creation_time, | |
| 230 expiry_time, kSnippetPublisherName, | |
| 231 kSnippetAmpUrl); | |
| 232 } | |
| 233 | |
| 234 std::string GetSnippetWithTimes(const base::Time& content_creation_time, | |
| 235 const base::Time& expiry_time) { | |
| 236 return GetSnippetWithUrlAndTimes(kSnippetUrl, content_creation_time, | |
| 237 expiry_time); | |
| 238 } | |
| 239 | |
| 240 std::string GetSnippetWithUrl(const std::string& url) { | |
| 241 return GetSnippetWithUrlAndTimes(url, GetDefaultCreationTime(), | |
| 242 GetDefaultExpirationTime()); | |
| 243 } | |
| 244 | |
| 245 std::string GetSnippet() { | |
| 246 return GetSnippetWithUrlAndTimes(kSnippetUrl, GetDefaultCreationTime(), | |
| 247 GetDefaultExpirationTime()); | |
| 248 } | |
| 249 | |
| 250 std::string GetSnippetN(int n) { | |
| 251 return GetSnippetWithUrlAndTimes(base::StringPrintf("%s/%d", kSnippetUrl, n), | |
| 252 GetDefaultCreationTime(), | |
| 253 GetDefaultExpirationTime()); | |
| 254 } | |
| 255 | |
| 256 std::string GetExpiredSnippet() { | |
| 257 return GetSnippetWithTimes(GetDefaultCreationTime(), base::Time::Now()); | |
| 258 } | |
| 259 | |
| 260 std::string GetInvalidSnippet() { | |
| 261 std::string json_str = GetSnippet(); | |
| 262 // Make the json invalid by removing the final closing brace. | |
| 263 return json_str.substr(0, json_str.size() - 1); | |
| 264 } | |
| 265 | |
| 266 std::string GetIncompleteSnippet() { | |
| 267 std::string json_str = GetSnippet(); | |
| 268 // Rename the "url" entry. The result is syntactically valid json that will | |
| 269 // fail to parse as snippets. | |
| 270 size_t pos = json_str.find("\"fullPageUrl\""); | |
| 271 if (pos == std::string::npos) { | |
| 272 NOTREACHED(); | |
| 273 return std::string(); | |
| 274 } | |
| 275 json_str[pos + 1] = 'x'; | |
| 276 return json_str; | |
| 277 } | |
| 278 | |
| 279 using ServeImageCallback = base::Callback<void( | |
| 280 const std::string&, | |
| 281 base::Callback<void(const std::string&, const gfx::Image&)>)>; | |
| 282 | |
| 283 void ServeOneByOneImage( | |
| 284 image_fetcher::ImageFetcherDelegate* notify, | |
| 285 const std::string& id, | |
| 286 base::Callback<void(const std::string&, const gfx::Image&)> callback) { | |
| 287 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 288 FROM_HERE, base::Bind(callback, id, gfx::test::CreateImage(1, 1))); | |
| 289 notify->OnImageDataFetched(id, "1-by-1-image-data"); | |
| 290 } | |
| 291 | |
| 292 gfx::Image FetchImage(RemoteSuggestionsProvider* service, | |
| 293 const ContentSuggestion::ID& suggestion_id) { | |
| 294 gfx::Image result; | |
| 295 base::RunLoop run_loop; | |
| 296 service->FetchSuggestionImage(suggestion_id, | |
| 297 base::Bind( | |
| 298 [](base::Closure signal, gfx::Image* output, | |
| 299 const gfx::Image& loaded) { | |
| 300 *output = loaded; | |
| 301 signal.Run(); | |
| 302 }, | |
| 303 run_loop.QuitClosure(), &result)); | |
| 304 run_loop.Run(); | |
| 305 return result; | |
| 306 } | |
| 307 | |
| 308 void ParseJson( | |
| 309 const std::string& json, | |
| 310 const ntp_snippets::NTPSnippetsFetcher::SuccessCallback& success_callback, | |
| 311 const ntp_snippets::NTPSnippetsFetcher::ErrorCallback& error_callback) { | |
| 312 base::JSONReader json_reader; | |
| 313 std::unique_ptr<base::Value> value = json_reader.ReadToValue(json); | |
| 314 if (value) { | |
| 315 success_callback.Run(std::move(value)); | |
| 316 } else { | |
| 317 error_callback.Run(json_reader.GetErrorMessage()); | |
| 318 } | |
| 319 } | |
| 320 | |
| 321 // Factory for FakeURLFetcher objects that always generate errors. | |
| 322 class FailingFakeURLFetcherFactory : public net::URLFetcherFactory { | |
| 323 public: | |
| 324 std::unique_ptr<net::URLFetcher> CreateURLFetcher( | |
| 325 int id, | |
| 326 const GURL& url, | |
| 327 net::URLFetcher::RequestType request_type, | |
| 328 net::URLFetcherDelegate* d) override { | |
| 329 return base::MakeUnique<net::FakeURLFetcher>( | |
| 330 url, d, /*response_data=*/std::string(), net::HTTP_NOT_FOUND, | |
| 331 net::URLRequestStatus::FAILED); | |
| 332 } | |
| 333 }; | |
| 334 | |
| 335 class MockScheduler : public NTPSnippetsScheduler { | |
| 336 public: | |
| 337 MOCK_METHOD2(Schedule, | |
| 338 bool(base::TimeDelta period_wifi, | |
| 339 base::TimeDelta period_fallback)); | |
| 340 MOCK_METHOD0(Unschedule, bool()); | |
| 341 }; | |
| 342 | |
| 343 class MockImageFetcher : public ImageFetcher { | |
| 344 public: | |
| 345 MOCK_METHOD1(SetImageFetcherDelegate, void(ImageFetcherDelegate*)); | |
| 346 MOCK_METHOD1(SetDataUseServiceName, void(DataUseServiceName)); | |
| 347 MOCK_METHOD3( | |
| 348 StartOrQueueNetworkRequest, | |
| 349 void(const std::string&, | |
| 350 const GURL&, | |
| 351 base::Callback<void(const std::string&, const gfx::Image&)>)); | |
| 352 }; | |
| 353 | |
| 354 class FakeContentSuggestionsProviderObserver | |
| 355 : public ContentSuggestionsProvider::Observer { | |
| 356 public: | |
| 357 FakeContentSuggestionsProviderObserver() = default; | |
| 358 | |
| 359 void OnNewSuggestions(ContentSuggestionsProvider* provider, | |
| 360 Category category, | |
| 361 std::vector<ContentSuggestion> suggestions) override { | |
| 362 suggestions_[category] = std::move(suggestions); | |
| 363 } | |
| 364 | |
| 365 void OnCategoryStatusChanged(ContentSuggestionsProvider* provider, | |
| 366 Category category, | |
| 367 CategoryStatus new_status) override { | |
| 368 statuses_[category] = new_status; | |
| 369 } | |
| 370 | |
| 371 void OnSuggestionInvalidated( | |
| 372 ContentSuggestionsProvider* provider, | |
| 373 const ContentSuggestion::ID& suggestion_id) override {} | |
| 374 | |
| 375 const std::map<Category, CategoryStatus, Category::CompareByID>& statuses() | |
| 376 const { | |
| 377 return statuses_; | |
| 378 } | |
| 379 | |
| 380 CategoryStatus StatusForCategory(Category category) const { | |
| 381 auto it = statuses_.find(category); | |
| 382 if (it == statuses_.end()) { | |
| 383 return CategoryStatus::NOT_PROVIDED; | |
| 384 } | |
| 385 return it->second; | |
| 386 } | |
| 387 | |
| 388 const std::vector<ContentSuggestion>& SuggestionsForCategory( | |
| 389 Category category) { | |
| 390 return suggestions_[category]; | |
| 391 } | |
| 392 | |
| 393 private: | |
| 394 std::map<Category, CategoryStatus, Category::CompareByID> statuses_; | |
| 395 std::map<Category, std::vector<ContentSuggestion>, Category::CompareByID> | |
| 396 suggestions_; | |
| 397 | |
| 398 DISALLOW_COPY_AND_ASSIGN(FakeContentSuggestionsProviderObserver); | |
| 399 }; | |
| 400 | |
| 401 class FakeImageDecoder : public image_fetcher::ImageDecoder { | |
| 402 public: | |
| 403 FakeImageDecoder() {} | |
| 404 ~FakeImageDecoder() override = default; | |
| 405 void DecodeImage( | |
| 406 const std::string& image_data, | |
| 407 const image_fetcher::ImageDecodedCallback& callback) override { | |
| 408 callback.Run(decoded_image_); | |
| 409 } | |
| 410 | |
| 411 void SetDecodedImage(const gfx::Image& image) { decoded_image_ = image; } | |
| 412 | |
| 413 private: | |
| 414 gfx::Image decoded_image_; | |
| 415 }; | |
| 416 | |
| 417 } // namespace | |
| 418 | |
| 419 class RemoteSuggestionsProviderTest : public ::testing::Test { | |
| 420 public: | |
| 421 RemoteSuggestionsProviderTest() | |
| 422 : params_manager_(ntp_snippets::kStudyName, | |
| 423 {{"content_suggestions_backend", | |
| 424 kTestContentSuggestionsServerEndpoint}, | |
| 425 {"fetching_personalization", "non_personal"}}), | |
| 426 fake_url_fetcher_factory_( | |
| 427 /*default_factory=*/&failing_url_fetcher_factory_), | |
| 428 test_url_(kTestContentSuggestionsServerWithAPIKey), | |
| 429 category_ranker_(base::MakeUnique<ConstantCategoryRanker>()), | |
| 430 user_classifier_(/*pref_service=*/nullptr), | |
| 431 image_fetcher_(nullptr), | |
| 432 image_decoder_(nullptr), | |
| 433 database_(nullptr) { | |
| 434 RemoteSuggestionsProvider::RegisterProfilePrefs( | |
| 435 utils_.pref_service()->registry()); | |
| 436 RequestThrottler::RegisterProfilePrefs(utils_.pref_service()->registry()); | |
| 437 | |
| 438 EXPECT_TRUE(database_dir_.CreateUniqueTempDir()); | |
| 439 } | |
| 440 | |
| 441 ~RemoteSuggestionsProviderTest() override { | |
| 442 // We need to run the message loop after deleting the database, because | |
| 443 // ProtoDatabaseImpl deletes the actual LevelDB asynchronously on the task | |
| 444 // runner. Without this, we'd get reports of memory leaks. | |
| 445 base::RunLoop().RunUntilIdle(); | |
| 446 } | |
| 447 | |
| 448 // TODO(vitaliii): Rewrite this function to initialize a test class member | |
| 449 // instead of creating a new service. | |
| 450 std::unique_ptr<RemoteSuggestionsProvider> MakeSnippetsService( | |
| 451 bool set_empty_response = true) { | |
| 452 auto service = MakeSnippetsServiceWithoutInitialization(); | |
| 453 WaitForSnippetsServiceInitialization(service.get(), set_empty_response); | |
| 454 return service; | |
| 455 } | |
| 456 | |
| 457 std::unique_ptr<RemoteSuggestionsProvider> | |
| 458 MakeSnippetsServiceWithoutInitialization() { | |
| 459 scoped_refptr<base::SingleThreadTaskRunner> task_runner( | |
| 460 base::ThreadTaskRunnerHandle::Get()); | |
| 461 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter = | |
| 462 new net::TestURLRequestContextGetter(task_runner.get()); | |
| 463 | |
| 464 utils_.ResetSigninManager(); | |
| 465 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher = | |
| 466 base::MakeUnique<NTPSnippetsFetcher>( | |
| 467 utils_.fake_signin_manager(), fake_token_service_.get(), | |
| 468 std::move(request_context_getter), utils_.pref_service(), nullptr, | |
| 469 base::Bind(&ParseJson), kAPIKey, &user_classifier_); | |
| 470 | |
| 471 utils_.fake_signin_manager()->SignIn("foo@bar.com"); | |
| 472 | |
| 473 auto image_fetcher = base::MakeUnique<NiceMock<MockImageFetcher>>(); | |
| 474 | |
| 475 image_fetcher_ = image_fetcher.get(); | |
| 476 EXPECT_CALL(*image_fetcher, SetImageFetcherDelegate(_)); | |
| 477 auto image_decoder = base::MakeUnique<FakeImageDecoder>(); | |
| 478 image_decoder_ = image_decoder.get(); | |
| 479 EXPECT_FALSE(observer_); | |
| 480 observer_ = base::MakeUnique<FakeContentSuggestionsProviderObserver>(); | |
| 481 auto database = base::MakeUnique<RemoteSuggestionsDatabase>( | |
| 482 database_dir_.GetPath(), task_runner); | |
| 483 database_ = database.get(); | |
| 484 return base::MakeUnique<RemoteSuggestionsProvider>( | |
| 485 observer_.get(), utils_.pref_service(), "fr", category_ranker_.get(), | |
| 486 &user_classifier_, &scheduler_, std::move(snippets_fetcher), | |
| 487 std::move(image_fetcher), std::move(image_decoder), std::move(database), | |
| 488 base::MakeUnique<RemoteSuggestionsStatusService>( | |
| 489 utils_.fake_signin_manager(), utils_.pref_service())); | |
| 490 } | |
| 491 | |
| 492 void WaitForSnippetsServiceInitialization(RemoteSuggestionsProvider* service, | |
| 493 bool set_empty_response) { | |
| 494 EXPECT_EQ(RemoteSuggestionsProvider::State::NOT_INITED, service->state_); | |
| 495 | |
| 496 // Add an initial fetch response, as the service tries to fetch when there | |
| 497 // is nothing in the DB. | |
| 498 if (set_empty_response) { | |
| 499 SetUpFetchResponse(GetTestJson(std::vector<std::string>())); | |
| 500 } | |
| 501 | |
| 502 // TODO(treib): Find a better way to wait for initialization to finish. | |
| 503 base::RunLoop().RunUntilIdle(); | |
| 504 EXPECT_NE(RemoteSuggestionsProvider::State::NOT_INITED, service->state_); | |
| 505 } | |
| 506 | |
| 507 void ResetSnippetsService(std::unique_ptr<RemoteSuggestionsProvider>* service, | |
| 508 bool set_empty_response) { | |
| 509 service->reset(); | |
| 510 observer_.reset(); | |
| 511 *service = MakeSnippetsService(set_empty_response); | |
| 512 } | |
| 513 | |
| 514 void SetCategoryRanker(std::unique_ptr<CategoryRanker> category_ranker) { | |
| 515 category_ranker_ = std::move(category_ranker); | |
| 516 } | |
| 517 | |
| 518 ContentSuggestion::ID MakeArticleID(const std::string& id_within_category) { | |
| 519 return ContentSuggestion::ID(articles_category(), id_within_category); | |
| 520 } | |
| 521 | |
| 522 Category articles_category() { | |
| 523 return Category::FromKnownCategory(KnownCategories::ARTICLES); | |
| 524 } | |
| 525 | |
| 526 ContentSuggestion::ID MakeOtherID(const std::string& id_within_category) { | |
| 527 return ContentSuggestion::ID(other_category(), id_within_category); | |
| 528 } | |
| 529 | |
| 530 // TODO(tschumann): Get rid of the convenience other_category() and | |
| 531 // unknown_category() helpers -- tests can just define their own. | |
| 532 Category other_category() { return Category::FromRemoteCategory(2); } | |
| 533 | |
| 534 Category unknown_category() { | |
| 535 return Category::FromRemoteCategory(kUnknownRemoteCategoryId); | |
| 536 } | |
| 537 | |
| 538 protected: | |
| 539 const GURL& test_url() { return test_url_; } | |
| 540 FakeContentSuggestionsProviderObserver& observer() { return *observer_; } | |
| 541 MockScheduler& mock_scheduler() { return scheduler_; } | |
| 542 // TODO(tschumann): Make this a strict-mock. We want to avoid unneccesary | |
| 543 // network requests. | |
| 544 NiceMock<MockImageFetcher>* image_fetcher() { return image_fetcher_; } | |
| 545 FakeImageDecoder* image_decoder() { return image_decoder_; } | |
| 546 PrefService* pref_service() { return utils_.pref_service(); } | |
| 547 RemoteSuggestionsDatabase* database() { return database_; } | |
| 548 | |
| 549 // Provide the json to be returned by the fake fetcher. | |
| 550 void SetUpFetchResponse(const std::string& json) { | |
| 551 fake_url_fetcher_factory_.SetFakeResponse(test_url_, json, net::HTTP_OK, | |
| 552 net::URLRequestStatus::SUCCESS); | |
| 553 } | |
| 554 | |
| 555 // Have the fake fetcher fail due to a HTTP error like a 404. | |
| 556 void SetUpHttpError() { | |
| 557 fake_url_fetcher_factory_.SetFakeResponse(test_url_, /*json=*/std::string(), | |
| 558 net::HTTP_NOT_FOUND, | |
| 559 net::URLRequestStatus::SUCCESS); | |
| 560 } | |
| 561 | |
| 562 void LoadFromJSONString(RemoteSuggestionsProvider* service, | |
| 563 const std::string& json) { | |
| 564 SetUpFetchResponse(json); | |
| 565 service->FetchSnippets(true); | |
| 566 base::RunLoop().RunUntilIdle(); | |
| 567 } | |
| 568 | |
| 569 void LoadMoreFromJSONString(RemoteSuggestionsProvider* service, | |
| 570 const Category& category, | |
| 571 const std::string& json, | |
| 572 const std::set<std::string>& known_ids, | |
| 573 FetchDoneCallback callback) { | |
| 574 SetUpFetchResponse(json); | |
| 575 service->Fetch(category, known_ids, callback); | |
| 576 base::RunLoop().RunUntilIdle(); | |
| 577 } | |
| 578 | |
| 579 private: | |
| 580 variations::testing::VariationParamsManager params_manager_; | |
| 581 test::RemoteSuggestionsTestUtils utils_; | |
| 582 base::MessageLoop message_loop_; | |
| 583 FailingFakeURLFetcherFactory failing_url_fetcher_factory_; | |
| 584 // Instantiation of factory automatically sets itself as URLFetcher's factory. | |
| 585 net::FakeURLFetcherFactory fake_url_fetcher_factory_; | |
| 586 const GURL test_url_; | |
| 587 std::unique_ptr<OAuth2TokenService> fake_token_service_; | |
| 588 std::unique_ptr<CategoryRanker> category_ranker_; | |
| 589 UserClassifier user_classifier_; | |
| 590 NiceMock<MockScheduler> scheduler_; | |
| 591 std::unique_ptr<FakeContentSuggestionsProviderObserver> observer_; | |
| 592 NiceMock<MockImageFetcher>* image_fetcher_; | |
| 593 FakeImageDecoder* image_decoder_; | |
| 594 | |
| 595 base::ScopedTempDir database_dir_; | |
| 596 RemoteSuggestionsDatabase* database_; | |
| 597 | |
| 598 DISALLOW_COPY_AND_ASSIGN(RemoteSuggestionsProviderTest); | |
| 599 }; | |
| 600 | |
| 601 TEST_F(RemoteSuggestionsProviderTest, ScheduleOnStart) { | |
| 602 // We should get two |Schedule| calls: The first when initialization | |
| 603 // completes, the second one after the automatic (since the service doesn't | |
| 604 // have any data yet) fetch finishes. | |
| 605 EXPECT_CALL(mock_scheduler(), Schedule(_, _)).Times(2); | |
| 606 EXPECT_CALL(mock_scheduler(), Unschedule()).Times(0); | |
| 607 auto service = MakeSnippetsService(); | |
| 608 | |
| 609 // When we have no snippets are all, loading the service initiates a fetch. | |
| 610 EXPECT_EQ("OK", service->snippets_fetcher()->last_status()); | |
| 611 } | |
| 612 | |
| 613 TEST_F(RemoteSuggestionsProviderTest, DontRescheduleOnStart) { | |
| 614 EXPECT_CALL(mock_scheduler(), Schedule(_, _)).Times(2); | |
| 615 EXPECT_CALL(mock_scheduler(), Unschedule()).Times(0); | |
| 616 SetUpFetchResponse(GetTestJson({GetSnippet()})); | |
| 617 auto service = MakeSnippetsService(/*set_empty_response=*/false); | |
| 618 | |
| 619 // When recreating the service, we should not get any |Schedule| calls: | |
| 620 // The tasks are already scheduled with the correct intervals, so nothing on | |
| 621 // initialization, and the service has data from the DB, so no automatic fetch | |
| 622 // should happen. | |
| 623 Mock::VerifyAndClearExpectations(&mock_scheduler()); | |
| 624 EXPECT_CALL(mock_scheduler(), Schedule(_, _)).Times(0); | |
| 625 EXPECT_CALL(mock_scheduler(), Unschedule()).Times(0); | |
| 626 ResetSnippetsService(&service, /*set_empty_response=*/true); | |
| 627 } | |
| 628 | |
| 629 TEST_F(RemoteSuggestionsProviderTest, RescheduleAfterSuccessfulFetch) { | |
| 630 // We should get two |Schedule| calls: The first when initialization | |
| 631 // completes, the second one after the automatic (since the service doesn't | |
| 632 // have any data yet) fetch finishes. | |
| 633 EXPECT_CALL(mock_scheduler(), Schedule(_, _)).Times(2); | |
| 634 auto service = MakeSnippetsService(); | |
| 635 | |
| 636 // A successful fetch should trigger another |Schedule|. | |
| 637 EXPECT_CALL(mock_scheduler(), Schedule(_, _)); | |
| 638 LoadFromJSONString(service.get(), GetTestJson({GetSnippet()})); | |
| 639 } | |
| 640 | |
| 641 TEST_F(RemoteSuggestionsProviderTest, DontRescheduleAfterFailedFetch) { | |
| 642 // We should get two |Schedule| calls: The first when initialization | |
| 643 // completes, the second one after the automatic (since the service doesn't | |
| 644 // have any data yet) fetch finishes. | |
| 645 EXPECT_CALL(mock_scheduler(), Schedule(_, _)).Times(2); | |
| 646 auto service = MakeSnippetsService(); | |
| 647 | |
| 648 // A failed fetch should NOT trigger another |Schedule|. | |
| 649 EXPECT_CALL(mock_scheduler(), Schedule(_, _)).Times(0); | |
| 650 LoadFromJSONString(service.get(), GetTestJson({GetInvalidSnippet()})); | |
| 651 } | |
| 652 | |
| 653 TEST_F(RemoteSuggestionsProviderTest, IgnoreRescheduleBeforeInit) { | |
| 654 // We should get two |Schedule| calls: The first when initialization | |
| 655 // completes, the second one after the automatic (since the service doesn't | |
| 656 // have any data yet) fetch finishes. | |
| 657 EXPECT_CALL(mock_scheduler(), Schedule(_, _)).Times(2); | |
| 658 // The |RescheduleFetching| call shouldn't do anything (in particular not | |
| 659 // result in an |Unschedule|), since the service isn't initialized yet. | |
| 660 EXPECT_CALL(mock_scheduler(), Unschedule()).Times(0); | |
| 661 auto service = MakeSnippetsServiceWithoutInitialization(); | |
| 662 service->RescheduleFetching(false); | |
| 663 WaitForSnippetsServiceInitialization(service.get(), | |
| 664 /*set_empty_response=*/true); | |
| 665 } | |
| 666 | |
| 667 TEST_F(RemoteSuggestionsProviderTest, HandleForcedRescheduleBeforeInit) { | |
| 668 { | |
| 669 InSequence s; | |
| 670 // The |RescheduleFetching| call with force=true should result in an | |
| 671 // |Unschedule|, since the service isn't initialized yet. | |
| 672 EXPECT_CALL(mock_scheduler(), Unschedule()).Times(1); | |
| 673 // We should get two |Schedule| calls: The first when initialization | |
| 674 // completes, the second one after the automatic (since the service doesn't | |
| 675 // have any data yet) fetch finishes. | |
| 676 EXPECT_CALL(mock_scheduler(), Schedule(_, _)).Times(2); | |
| 677 } | |
| 678 auto service = MakeSnippetsServiceWithoutInitialization(); | |
| 679 service->RescheduleFetching(true); | |
| 680 WaitForSnippetsServiceInitialization(service.get(), | |
| 681 /*set_empty_response=*/true); | |
| 682 } | |
| 683 | |
| 684 TEST_F(RemoteSuggestionsProviderTest, RescheduleOnStateChange) { | |
| 685 { | |
| 686 InSequence s; | |
| 687 // Initial startup. | |
| 688 EXPECT_CALL(mock_scheduler(), Schedule(_, _)).Times(2); | |
| 689 // Service gets disabled. | |
| 690 EXPECT_CALL(mock_scheduler(), Unschedule()); | |
| 691 // Service gets enabled again. | |
| 692 EXPECT_CALL(mock_scheduler(), Schedule(_, _)).Times(2); | |
| 693 } | |
| 694 auto service = MakeSnippetsService(); | |
| 695 ASSERT_TRUE(service->ready()); | |
| 696 | |
| 697 service->OnStatusChanged(RemoteSuggestionsStatus::ENABLED_AND_SIGNED_IN, | |
| 698 RemoteSuggestionsStatus::EXPLICITLY_DISABLED); | |
| 699 ASSERT_FALSE(service->ready()); | |
| 700 base::RunLoop().RunUntilIdle(); | |
| 701 | |
| 702 service->OnStatusChanged(RemoteSuggestionsStatus::EXPLICITLY_DISABLED, | |
| 703 RemoteSuggestionsStatus::ENABLED_AND_SIGNED_OUT); | |
| 704 ASSERT_TRUE(service->ready()); | |
| 705 base::RunLoop().RunUntilIdle(); | |
| 706 } | |
| 707 | |
| 708 TEST_F(RemoteSuggestionsProviderTest, DontUnscheduleOnShutdown) { | |
| 709 EXPECT_CALL(mock_scheduler(), Schedule(_, _)).Times(2); | |
| 710 EXPECT_CALL(mock_scheduler(), Unschedule()).Times(0); | |
| 711 | |
| 712 auto service = MakeSnippetsService(); | |
| 713 | |
| 714 service.reset(); | |
| 715 base::RunLoop().RunUntilIdle(); | |
| 716 } | |
| 717 | |
| 718 TEST_F(RemoteSuggestionsProviderTest, Full) { | |
| 719 std::string json_str(GetTestJson({GetSnippet()})); | |
| 720 | |
| 721 auto service = MakeSnippetsService(); | |
| 722 | |
| 723 LoadFromJSONString(service.get(), json_str); | |
| 724 | |
| 725 ASSERT_THAT(observer().SuggestionsForCategory(articles_category()), | |
| 726 SizeIs(1)); | |
| 727 ASSERT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(1)); | |
| 728 | |
| 729 const ContentSuggestion& suggestion = | |
| 730 observer().SuggestionsForCategory(articles_category()).front(); | |
| 731 | |
| 732 EXPECT_EQ(MakeArticleID(kSnippetUrl), suggestion.id()); | |
| 733 EXPECT_EQ(kSnippetTitle, base::UTF16ToUTF8(suggestion.title())); | |
| 734 EXPECT_EQ(kSnippetText, base::UTF16ToUTF8(suggestion.snippet_text())); | |
| 735 EXPECT_EQ(GetDefaultCreationTime(), suggestion.publish_date()); | |
| 736 EXPECT_EQ(kSnippetPublisherName, | |
| 737 base::UTF16ToUTF8(suggestion.publisher_name())); | |
| 738 EXPECT_EQ(GURL(kSnippetAmpUrl), suggestion.amp_url()); | |
| 739 } | |
| 740 | |
| 741 TEST_F(RemoteSuggestionsProviderTest, CategoryTitle) { | |
| 742 const base::string16 test_default_title = | |
| 743 base::UTF8ToUTF16(kTestJsonDefaultCategoryTitle); | |
| 744 | |
| 745 // Don't send an initial response -- we want to test what happens without any | |
| 746 // server status. | |
| 747 auto service = MakeSnippetsService(/*set_empty_response=*/false); | |
| 748 | |
| 749 // The articles category should be there by default, and have a title. | |
| 750 CategoryInfo info_before = service->GetCategoryInfo(articles_category()); | |
| 751 ASSERT_THAT(info_before.title(), Not(IsEmpty())); | |
| 752 ASSERT_THAT(info_before.title(), Not(Eq(test_default_title))); | |
| 753 EXPECT_THAT(info_before.has_more_action(), Eq(true)); | |
| 754 EXPECT_THAT(info_before.has_reload_action(), Eq(true)); | |
| 755 EXPECT_THAT(info_before.has_view_all_action(), Eq(false)); | |
| 756 EXPECT_THAT(info_before.show_if_empty(), Eq(true)); | |
| 757 | |
| 758 std::string json_str_with_title(GetTestJson({GetSnippet()})); | |
| 759 LoadFromJSONString(service.get(), json_str_with_title); | |
| 760 | |
| 761 ASSERT_THAT(observer().SuggestionsForCategory(articles_category()), | |
| 762 SizeIs(1)); | |
| 763 ASSERT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(1)); | |
| 764 | |
| 765 // The response contained a title, |kTestJsonDefaultCategoryTitle|. | |
| 766 // Make sure we updated the title in the CategoryInfo. | |
| 767 CategoryInfo info_with_title = service->GetCategoryInfo(articles_category()); | |
| 768 EXPECT_THAT(info_before.title(), Not(Eq(info_with_title.title()))); | |
| 769 EXPECT_THAT(test_default_title, Eq(info_with_title.title())); | |
| 770 EXPECT_THAT(info_before.has_more_action(), Eq(true)); | |
| 771 EXPECT_THAT(info_before.has_reload_action(), Eq(true)); | |
| 772 EXPECT_THAT(info_before.has_view_all_action(), Eq(false)); | |
| 773 EXPECT_THAT(info_before.show_if_empty(), Eq(true)); | |
| 774 } | |
| 775 | |
| 776 TEST_F(RemoteSuggestionsProviderTest, MultipleCategories) { | |
| 777 auto service = MakeSnippetsService(); | |
| 778 std::string json_str = | |
| 779 MultiCategoryJsonBuilder() | |
| 780 .AddCategory({GetSnippetN(0)}, /*remote_category_id=*/1) | |
| 781 .AddCategory({GetSnippetN(1)}, /*remote_category_id=*/2) | |
| 782 .Build(); | |
| 783 LoadFromJSONString(service.get(), json_str); | |
| 784 | |
| 785 ASSERT_THAT(observer().statuses(), | |
| 786 Eq(std::map<Category, CategoryStatus, Category::CompareByID>{ | |
| 787 {articles_category(), CategoryStatus::AVAILABLE}, | |
| 788 {other_category(), CategoryStatus::AVAILABLE}, | |
| 789 })); | |
| 790 | |
| 791 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(1)); | |
| 792 EXPECT_THAT(service->GetSnippetsForTesting(other_category()), SizeIs(1)); | |
| 793 | |
| 794 ASSERT_THAT(observer().SuggestionsForCategory(articles_category()), | |
| 795 SizeIs(1)); | |
| 796 | |
| 797 ASSERT_THAT(observer().SuggestionsForCategory(other_category()), SizeIs(1)); | |
| 798 | |
| 799 { | |
| 800 const ContentSuggestion& suggestion = | |
| 801 observer().SuggestionsForCategory(articles_category()).front(); | |
| 802 EXPECT_EQ(MakeArticleID(std::string(kSnippetUrl) + "/0"), suggestion.id()); | |
| 803 EXPECT_EQ(kSnippetTitle, base::UTF16ToUTF8(suggestion.title())); | |
| 804 EXPECT_EQ(kSnippetText, base::UTF16ToUTF8(suggestion.snippet_text())); | |
| 805 EXPECT_EQ(GetDefaultCreationTime(), suggestion.publish_date()); | |
| 806 EXPECT_EQ(kSnippetPublisherName, | |
| 807 base::UTF16ToUTF8(suggestion.publisher_name())); | |
| 808 EXPECT_EQ(GURL(kSnippetAmpUrl), suggestion.amp_url()); | |
| 809 } | |
| 810 | |
| 811 { | |
| 812 const ContentSuggestion& suggestion = | |
| 813 observer().SuggestionsForCategory(other_category()).front(); | |
| 814 EXPECT_EQ(MakeOtherID(std::string(kSnippetUrl) + "/1"), suggestion.id()); | |
| 815 EXPECT_EQ(kSnippetTitle, base::UTF16ToUTF8(suggestion.title())); | |
| 816 EXPECT_EQ(kSnippetText, base::UTF16ToUTF8(suggestion.snippet_text())); | |
| 817 EXPECT_EQ(GetDefaultCreationTime(), suggestion.publish_date()); | |
| 818 EXPECT_EQ(kSnippetPublisherName, | |
| 819 base::UTF16ToUTF8(suggestion.publisher_name())); | |
| 820 EXPECT_EQ(GURL(kSnippetAmpUrl), suggestion.amp_url()); | |
| 821 } | |
| 822 } | |
| 823 | |
| 824 TEST_F(RemoteSuggestionsProviderTest, ArticleCategoryInfo) { | |
| 825 auto service = MakeSnippetsService(); | |
| 826 CategoryInfo article_info = service->GetCategoryInfo(articles_category()); | |
| 827 EXPECT_THAT(article_info.has_more_action(), Eq(true)); | |
| 828 EXPECT_THAT(article_info.has_reload_action(), Eq(true)); | |
| 829 EXPECT_THAT(article_info.has_view_all_action(), Eq(false)); | |
| 830 EXPECT_THAT(article_info.show_if_empty(), Eq(true)); | |
| 831 } | |
| 832 | |
| 833 TEST_F(RemoteSuggestionsProviderTest, ExperimentalCategoryInfo) { | |
| 834 auto service = MakeSnippetsService(); | |
| 835 std::string json_str = | |
| 836 MultiCategoryJsonBuilder() | |
| 837 .AddCategory({GetSnippetN(0)}, /*remote_category_id=*/1) | |
| 838 .AddCategory({GetSnippetN(1)}, kUnknownRemoteCategoryId) | |
| 839 .Build(); | |
| 840 // Load data with multiple categories so that a new experimental category gets | |
| 841 // registered. | |
| 842 LoadFromJSONString(service.get(), json_str); | |
| 843 | |
| 844 CategoryInfo info = service->GetCategoryInfo(unknown_category()); | |
| 845 EXPECT_THAT(info.has_more_action(), Eq(false)); | |
| 846 EXPECT_THAT(info.has_reload_action(), Eq(false)); | |
| 847 EXPECT_THAT(info.has_view_all_action(), Eq(false)); | |
| 848 EXPECT_THAT(info.show_if_empty(), Eq(false)); | |
| 849 } | |
| 850 | |
| 851 TEST_F(RemoteSuggestionsProviderTest, AddRemoteCategoriesToCategoryRanker) { | |
| 852 auto mock_ranker = base::MakeUnique<MockCategoryRanker>(); | |
| 853 MockCategoryRanker* raw_mock_ranker = mock_ranker.get(); | |
| 854 SetCategoryRanker(std::move(mock_ranker)); | |
| 855 std::string json_str = | |
| 856 MultiCategoryJsonBuilder() | |
| 857 .AddCategory({GetSnippetN(0)}, /*remote_category_id=*/11) | |
| 858 .AddCategory({GetSnippetN(1)}, /*remote_category_id=*/13) | |
| 859 .AddCategory({GetSnippetN(2)}, /*remote_category_id=*/12) | |
| 860 .Build(); | |
| 861 SetUpFetchResponse(json_str); | |
| 862 { | |
| 863 // The order of categories is determined by the order in which they are | |
| 864 // added. Thus, the latter is tested here. | |
| 865 InSequence s; | |
| 866 EXPECT_CALL(*raw_mock_ranker, | |
| 867 AppendCategoryIfNecessary(Category::FromRemoteCategory(11))); | |
| 868 EXPECT_CALL(*raw_mock_ranker, | |
| 869 AppendCategoryIfNecessary(Category::FromRemoteCategory(13))); | |
| 870 EXPECT_CALL(*raw_mock_ranker, | |
| 871 AppendCategoryIfNecessary(Category::FromRemoteCategory(12))); | |
| 872 } | |
| 873 auto service = MakeSnippetsService(/*set_empty_response=*/false); | |
| 874 } | |
| 875 | |
| 876 TEST_F(RemoteSuggestionsProviderTest, PersistCategoryInfos) { | |
| 877 auto service = MakeSnippetsService(); | |
| 878 // TODO(vitaliii): Use |articles_category()| instead of constant ID below. | |
| 879 std::string json_str = | |
| 880 MultiCategoryJsonBuilder() | |
| 881 .AddCategoryWithCustomTitle( | |
| 882 {GetSnippetN(0)}, /*remote_category_id=*/1, "Articles for You") | |
| 883 .AddCategoryWithCustomTitle({GetSnippetN(1)}, | |
| 884 kUnknownRemoteCategoryId, "Other Things") | |
| 885 .Build(); | |
| 886 LoadFromJSONString(service.get(), json_str); | |
| 887 | |
| 888 ASSERT_EQ(observer().StatusForCategory(articles_category()), | |
| 889 CategoryStatus::AVAILABLE); | |
| 890 ASSERT_EQ(observer().StatusForCategory(unknown_category()), | |
| 891 CategoryStatus::AVAILABLE); | |
| 892 | |
| 893 CategoryInfo info_articles_before = | |
| 894 service->GetCategoryInfo(articles_category()); | |
| 895 CategoryInfo info_unknown_before = | |
| 896 service->GetCategoryInfo(unknown_category()); | |
| 897 | |
| 898 // Recreate the service to simulate a Chrome restart. | |
| 899 ResetSnippetsService(&service, /*set_empty_response=*/true); | |
| 900 | |
| 901 // The categories should have been restored. | |
| 902 ASSERT_NE(observer().StatusForCategory(articles_category()), | |
| 903 CategoryStatus::NOT_PROVIDED); | |
| 904 ASSERT_NE(observer().StatusForCategory(unknown_category()), | |
| 905 CategoryStatus::NOT_PROVIDED); | |
| 906 | |
| 907 EXPECT_EQ(observer().StatusForCategory(articles_category()), | |
| 908 CategoryStatus::AVAILABLE); | |
| 909 EXPECT_EQ(observer().StatusForCategory(unknown_category()), | |
| 910 CategoryStatus::AVAILABLE); | |
| 911 | |
| 912 CategoryInfo info_articles_after = | |
| 913 service->GetCategoryInfo(articles_category()); | |
| 914 CategoryInfo info_unknown_after = | |
| 915 service->GetCategoryInfo(unknown_category()); | |
| 916 | |
| 917 EXPECT_EQ(info_articles_before.title(), info_articles_after.title()); | |
| 918 EXPECT_EQ(info_unknown_before.title(), info_unknown_after.title()); | |
| 919 } | |
| 920 | |
| 921 TEST_F(RemoteSuggestionsProviderTest, PersistRemoteCategoryOrder) { | |
| 922 // We create a service with a normal ranker to store the order. | |
| 923 std::string json_str = | |
| 924 MultiCategoryJsonBuilder() | |
| 925 .AddCategory({GetSnippetN(0)}, /*remote_category_id=*/11) | |
| 926 .AddCategory({GetSnippetN(1)}, /*remote_category_id=*/13) | |
| 927 .AddCategory({GetSnippetN(2)}, /*remote_category_id=*/12) | |
| 928 .Build(); | |
| 929 SetUpFetchResponse(json_str); | |
| 930 auto service = MakeSnippetsService(/*set_empty_response=*/false); | |
| 931 | |
| 932 // We manually recreate the service to simulate Chrome restart and enforce a | |
| 933 // mock ranker. The response is cleared to ensure that the order is not | |
| 934 // fetched. | |
| 935 SetUpFetchResponse(""); | |
| 936 auto mock_ranker = base::MakeUnique<MockCategoryRanker>(); | |
| 937 MockCategoryRanker* raw_mock_ranker = mock_ranker.get(); | |
| 938 SetCategoryRanker(std::move(mock_ranker)); | |
| 939 { | |
| 940 // The order of categories is determined by the order in which they are | |
| 941 // added. Thus, the latter is tested here. | |
| 942 InSequence s; | |
| 943 // Article category always exists and, therefore, it is stored in prefs too. | |
| 944 EXPECT_CALL(*raw_mock_ranker, | |
| 945 AppendCategoryIfNecessary(articles_category())); | |
| 946 | |
| 947 EXPECT_CALL(*raw_mock_ranker, | |
| 948 AppendCategoryIfNecessary(Category::FromRemoteCategory(11))); | |
| 949 EXPECT_CALL(*raw_mock_ranker, | |
| 950 AppendCategoryIfNecessary(Category::FromRemoteCategory(13))); | |
| 951 EXPECT_CALL(*raw_mock_ranker, | |
| 952 AppendCategoryIfNecessary(Category::FromRemoteCategory(12))); | |
| 953 } | |
| 954 ResetSnippetsService(&service, /*set_empty_response=*/false); | |
| 955 } | |
| 956 | |
| 957 TEST_F(RemoteSuggestionsProviderTest, PersistSuggestions) { | |
| 958 auto service = MakeSnippetsService(); | |
| 959 std::string json_str = | |
| 960 MultiCategoryJsonBuilder() | |
| 961 .AddCategory({GetSnippetN(0)}, /*remote_category_id=*/1) | |
| 962 .AddCategory({GetSnippetN(2)}, /*remote_category_id=*/2) | |
| 963 .Build(); | |
| 964 LoadFromJSONString(service.get(), json_str); | |
| 965 | |
| 966 ASSERT_THAT(observer().SuggestionsForCategory(articles_category()), | |
| 967 SizeIs(1)); | |
| 968 ASSERT_THAT(observer().SuggestionsForCategory(other_category()), SizeIs(1)); | |
| 969 | |
| 970 // Recreate the service to simulate a Chrome restart. | |
| 971 ResetSnippetsService(&service, /*set_empty_response=*/true); | |
| 972 | |
| 973 // The suggestions in both categories should have been restored. | |
| 974 EXPECT_THAT(observer().SuggestionsForCategory(articles_category()), | |
| 975 SizeIs(1)); | |
| 976 EXPECT_THAT(observer().SuggestionsForCategory(other_category()), SizeIs(1)); | |
| 977 } | |
| 978 | |
| 979 TEST_F(RemoteSuggestionsProviderTest, DontNotifyIfNotAvailable) { | |
| 980 // Get some suggestions into the database. | |
| 981 auto service = MakeSnippetsService(); | |
| 982 std::string json_str = | |
| 983 MultiCategoryJsonBuilder() | |
| 984 .AddCategory({GetSnippetN(0)}, | |
| 985 /*remote_category_id=*/1) | |
| 986 .AddCategory({GetSnippetN(1)}, /*remote_category_id=*/2) | |
| 987 .Build(); | |
| 988 LoadFromJSONString(service.get(), json_str); | |
| 989 | |
| 990 ASSERT_THAT(observer().SuggestionsForCategory(articles_category()), | |
| 991 SizeIs(1)); | |
| 992 ASSERT_THAT(observer().SuggestionsForCategory(other_category()), SizeIs(1)); | |
| 993 | |
| 994 service.reset(); | |
| 995 | |
| 996 // Set the pref that disables remote suggestions. | |
| 997 pref_service()->SetBoolean(prefs::kEnableSnippets, false); | |
| 998 | |
| 999 // Recreate the service to simulate a Chrome start. | |
| 1000 ResetSnippetsService(&service, /*set_empty_response=*/true); | |
| 1001 | |
| 1002 ASSERT_THAT(RemoteSuggestionsProvider::State::DISABLED, Eq(service->state_)); | |
| 1003 | |
| 1004 // Now the observer should not have received any suggestions. | |
| 1005 EXPECT_THAT(observer().SuggestionsForCategory(articles_category()), | |
| 1006 IsEmpty()); | |
| 1007 EXPECT_THAT(observer().SuggestionsForCategory(other_category()), IsEmpty()); | |
| 1008 } | |
| 1009 | |
| 1010 TEST_F(RemoteSuggestionsProviderTest, Clear) { | |
| 1011 auto service = MakeSnippetsService(); | |
| 1012 | |
| 1013 std::string json_str(GetTestJson({GetSnippet()})); | |
| 1014 | |
| 1015 LoadFromJSONString(service.get(), json_str); | |
| 1016 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(1)); | |
| 1017 | |
| 1018 service->ClearCachedSuggestions(articles_category()); | |
| 1019 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty()); | |
| 1020 } | |
| 1021 | |
| 1022 TEST_F(RemoteSuggestionsProviderTest, ReplaceSnippets) { | |
| 1023 auto service = MakeSnippetsService(); | |
| 1024 | |
| 1025 std::string first("http://first"); | |
| 1026 LoadFromJSONString(service.get(), GetTestJson({GetSnippetWithUrl(first)})); | |
| 1027 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), | |
| 1028 ElementsAre(IdEq(first))); | |
| 1029 | |
| 1030 std::string second("http://second"); | |
| 1031 LoadFromJSONString(service.get(), GetTestJson({GetSnippetWithUrl(second)})); | |
| 1032 // The snippets loaded last replace all that was loaded previously. | |
| 1033 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), | |
| 1034 ElementsAre(IdEq(second))); | |
| 1035 } | |
| 1036 | |
| 1037 TEST_F(RemoteSuggestionsProviderTest, LoadsAdditionalSnippets) { | |
| 1038 auto service = MakeSnippetsService(); | |
| 1039 | |
| 1040 LoadFromJSONString(service.get(), | |
| 1041 GetTestJson({GetSnippetWithUrl("http://first")})); | |
| 1042 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), | |
| 1043 ElementsAre(IdEq("http://first"))); | |
| 1044 | |
| 1045 auto expect_only_second_suggestion_received = base::Bind([]( | |
| 1046 Status status, std::vector<ContentSuggestion> suggestions) { | |
| 1047 EXPECT_THAT(suggestions, SizeIs(1)); | |
| 1048 EXPECT_THAT(suggestions[0].id().id_within_category(), Eq("http://second")); | |
| 1049 }); | |
| 1050 LoadMoreFromJSONString(service.get(), articles_category(), | |
| 1051 GetTestJson({GetSnippetWithUrl("http://second")}), | |
| 1052 /*known_ids=*/std::set<std::string>(), | |
| 1053 expect_only_second_suggestion_received); | |
| 1054 | |
| 1055 // Verify we can resolve the image of the new snippets. | |
| 1056 ServeImageCallback cb = | |
| 1057 base::Bind(&ServeOneByOneImage, &service->GetImageFetcherForTesting()); | |
| 1058 EXPECT_CALL(*image_fetcher(), StartOrQueueNetworkRequest(_, _, _)) | |
| 1059 .Times(2) | |
| 1060 .WillRepeatedly(WithArgs<0, 2>(Invoke(&cb, &ServeImageCallback::Run))); | |
| 1061 image_decoder()->SetDecodedImage(gfx::test::CreateImage(1, 1)); | |
| 1062 gfx::Image image = FetchImage(service.get(), MakeArticleID("http://first")); | |
| 1063 EXPECT_FALSE(image.IsEmpty()); | |
| 1064 EXPECT_EQ(1, image.Width()); | |
| 1065 | |
| 1066 image = FetchImage(service.get(), MakeArticleID("http://second")); | |
| 1067 EXPECT_FALSE(image.IsEmpty()); | |
| 1068 EXPECT_EQ(1, image.Width()); | |
| 1069 | |
| 1070 // Verify that the observer received the update as well. We should see the | |
| 1071 // newly-fetched items filled up with existing ones. | |
| 1072 EXPECT_THAT(observer().SuggestionsForCategory(articles_category()), | |
| 1073 ElementsAre(IdWithinCategoryEq("http://first"), | |
| 1074 IdWithinCategoryEq("http://second"))); | |
| 1075 } | |
| 1076 | |
| 1077 // The tests TestMergingFetchedMoreSnippetsFillup and | |
| 1078 // TestMergingFetchedMoreSnippetsReplaceAll simulate the following user story: | |
| 1079 // 1) fetch suggestions in NTP A | |
| 1080 // 2) fetch more suggestions in NTP A. | |
| 1081 // 3) open new NTP B: See the last 10 results visible in step 2). | |
| 1082 // 4) fetch more suggestions in NTP B. Make sure no results from step 1) which | |
| 1083 // were superseded in step 2) get merged back in again. | |
| 1084 // TODO(tschumann): Test step 4) on a higher level instead of peeking into the | |
| 1085 // internal 'dismissed' data. The proper check is to make sure we tell the | |
| 1086 // backend to exclude these snippets. | |
| 1087 TEST_F(RemoteSuggestionsProviderTest, TestMergingFetchedMoreSnippetsFillup) { | |
| 1088 auto service = MakeSnippetsService(/*set_empty_response=*/false); | |
| 1089 LoadFromJSONString( | |
| 1090 service.get(), | |
| 1091 GetTestJson( | |
| 1092 {GetSnippetWithUrl("http://id-1"), GetSnippetWithUrl("http://id-2"), | |
| 1093 GetSnippetWithUrl("http://id-3"), GetSnippetWithUrl("http://id-4"), | |
| 1094 GetSnippetWithUrl("http://id-5"), GetSnippetWithUrl("http://id-6"), | |
| 1095 GetSnippetWithUrl("http://id-7"), GetSnippetWithUrl("http://id-8"), | |
| 1096 GetSnippetWithUrl("http://id-9"), | |
| 1097 GetSnippetWithUrl("http://id-10")})); | |
| 1098 EXPECT_THAT( | |
| 1099 observer().SuggestionsForCategory(articles_category()), | |
| 1100 ElementsAre( | |
| 1101 IdWithinCategoryEq("http://id-1"), IdWithinCategoryEq("http://id-2"), | |
| 1102 IdWithinCategoryEq("http://id-3"), IdWithinCategoryEq("http://id-4"), | |
| 1103 IdWithinCategoryEq("http://id-5"), IdWithinCategoryEq("http://id-6"), | |
| 1104 IdWithinCategoryEq("http://id-7"), IdWithinCategoryEq("http://id-8"), | |
| 1105 IdWithinCategoryEq("http://id-9"), | |
| 1106 IdWithinCategoryEq("http://id-10"))); | |
| 1107 | |
| 1108 auto expect_receiving_two_new_snippets = | |
| 1109 base::Bind([](Status status, std::vector<ContentSuggestion> suggestions) { | |
| 1110 ASSERT_THAT(suggestions, SizeIs(2)); | |
| 1111 EXPECT_THAT(suggestions[0], IdWithinCategoryEq("http://more-id-1")); | |
| 1112 EXPECT_THAT(suggestions[1], IdWithinCategoryEq("http://more-id-2")); | |
| 1113 }); | |
| 1114 LoadMoreFromJSONString( | |
| 1115 service.get(), articles_category(), | |
| 1116 GetTestJson({GetSnippetWithUrl("http://more-id-1"), | |
| 1117 GetSnippetWithUrl("http://more-id-2")}), | |
| 1118 /*known_ids=*/{"http://id-1", "http://id-2", "http://id-3", "http://id-4", | |
| 1119 "http://id-5", "http://id-6", "http://id-7", "http://id-8", | |
| 1120 "http://id-9", "http://id-10"}, | |
| 1121 expect_receiving_two_new_snippets); | |
| 1122 | |
| 1123 // Verify that the observer received the update as well. We should see the | |
| 1124 // newly-fetched items filled up with existing ones. The merging is done | |
| 1125 // mimicking a scrolling behavior. | |
| 1126 EXPECT_THAT( | |
| 1127 observer().SuggestionsForCategory(articles_category()), | |
| 1128 ElementsAre( | |
| 1129 IdWithinCategoryEq("http://id-3"), IdWithinCategoryEq("http://id-4"), | |
| 1130 IdWithinCategoryEq("http://id-5"), IdWithinCategoryEq("http://id-6"), | |
| 1131 IdWithinCategoryEq("http://id-7"), IdWithinCategoryEq("http://id-8"), | |
| 1132 IdWithinCategoryEq("http://id-9"), IdWithinCategoryEq("http://id-10"), | |
| 1133 IdWithinCategoryEq("http://more-id-1"), | |
| 1134 IdWithinCategoryEq("http://more-id-2"))); | |
| 1135 // Verify the superseded suggestions got marked as dismissed. | |
| 1136 EXPECT_THAT(service->GetDismissedSnippetsForTesting(articles_category()), | |
| 1137 ElementsAre(IdEq("http://id-1"), IdEq("http://id-2"))); | |
| 1138 } | |
| 1139 | |
| 1140 TEST_F(RemoteSuggestionsProviderTest, | |
| 1141 TestMergingFetchedMoreSnippetsReplaceAll) { | |
| 1142 auto service = MakeSnippetsService(/*set_empty_response=*/false); | |
| 1143 LoadFromJSONString( | |
| 1144 service.get(), | |
| 1145 GetTestJson( | |
| 1146 {GetSnippetWithUrl("http://id-1"), GetSnippetWithUrl("http://id-2"), | |
| 1147 GetSnippetWithUrl("http://id-3"), GetSnippetWithUrl("http://id-4"), | |
| 1148 GetSnippetWithUrl("http://id-5"), GetSnippetWithUrl("http://id-6"), | |
| 1149 GetSnippetWithUrl("http://id-7"), GetSnippetWithUrl("http://id-8"), | |
| 1150 GetSnippetWithUrl("http://id-9"), | |
| 1151 GetSnippetWithUrl("http://id-10")})); | |
| 1152 EXPECT_THAT( | |
| 1153 observer().SuggestionsForCategory(articles_category()), | |
| 1154 ElementsAre( | |
| 1155 IdWithinCategoryEq("http://id-1"), IdWithinCategoryEq("http://id-2"), | |
| 1156 IdWithinCategoryEq("http://id-3"), IdWithinCategoryEq("http://id-4"), | |
| 1157 IdWithinCategoryEq("http://id-5"), IdWithinCategoryEq("http://id-6"), | |
| 1158 IdWithinCategoryEq("http://id-7"), IdWithinCategoryEq("http://id-8"), | |
| 1159 IdWithinCategoryEq("http://id-9"), | |
| 1160 IdWithinCategoryEq("http://id-10"))); | |
| 1161 | |
| 1162 auto expect_receiving_ten_new_snippets = | |
| 1163 base::Bind([](Status status, std::vector<ContentSuggestion> suggestions) { | |
| 1164 EXPECT_THAT(suggestions, ElementsAre( | |
| 1165 IdWithinCategoryEq("http://more-id-1"), | |
| 1166 IdWithinCategoryEq("http://more-id-2"), | |
| 1167 IdWithinCategoryEq("http://more-id-3"), | |
| 1168 IdWithinCategoryEq("http://more-id-4"), | |
| 1169 IdWithinCategoryEq("http://more-id-5"), | |
| 1170 IdWithinCategoryEq("http://more-id-6"), | |
| 1171 IdWithinCategoryEq("http://more-id-7"), | |
| 1172 IdWithinCategoryEq("http://more-id-8"), | |
| 1173 IdWithinCategoryEq("http://more-id-9"), | |
| 1174 IdWithinCategoryEq("http://more-id-10"))); | |
| 1175 }); | |
| 1176 LoadMoreFromJSONString( | |
| 1177 service.get(), articles_category(), | |
| 1178 GetTestJson({GetSnippetWithUrl("http://more-id-1"), | |
| 1179 GetSnippetWithUrl("http://more-id-2"), | |
| 1180 GetSnippetWithUrl("http://more-id-3"), | |
| 1181 GetSnippetWithUrl("http://more-id-4"), | |
| 1182 GetSnippetWithUrl("http://more-id-5"), | |
| 1183 GetSnippetWithUrl("http://more-id-6"), | |
| 1184 GetSnippetWithUrl("http://more-id-7"), | |
| 1185 GetSnippetWithUrl("http://more-id-8"), | |
| 1186 GetSnippetWithUrl("http://more-id-9"), | |
| 1187 GetSnippetWithUrl("http://more-id-10")}), | |
| 1188 /*known_ids=*/{"http://id-1", "http://id-2", "http://id-3", "http://id-4", | |
| 1189 "http://id-5", "http://id-6", "http://id-7", "http://id-8", | |
| 1190 "http://id-9", "http://id-10"}, | |
| 1191 expect_receiving_ten_new_snippets); | |
| 1192 EXPECT_THAT(observer().SuggestionsForCategory(articles_category()), | |
| 1193 ElementsAre(IdWithinCategoryEq("http://more-id-1"), | |
| 1194 IdWithinCategoryEq("http://more-id-2"), | |
| 1195 IdWithinCategoryEq("http://more-id-3"), | |
| 1196 IdWithinCategoryEq("http://more-id-4"), | |
| 1197 IdWithinCategoryEq("http://more-id-5"), | |
| 1198 IdWithinCategoryEq("http://more-id-6"), | |
| 1199 IdWithinCategoryEq("http://more-id-7"), | |
| 1200 IdWithinCategoryEq("http://more-id-8"), | |
| 1201 IdWithinCategoryEq("http://more-id-9"), | |
| 1202 IdWithinCategoryEq("http://more-id-10"))); | |
| 1203 // Verify the superseded suggestions got marked as dismissed. | |
| 1204 EXPECT_THAT( | |
| 1205 service->GetDismissedSnippetsForTesting(articles_category()), | |
| 1206 ElementsAre(IdEq("http://id-1"), IdEq("http://id-2"), IdEq("http://id-3"), | |
| 1207 IdEq("http://id-4"), IdEq("http://id-5"), IdEq("http://id-6"), | |
| 1208 IdEq("http://id-7"), IdEq("http://id-8"), IdEq("http://id-9"), | |
| 1209 IdEq("http://id-10"))); | |
| 1210 } | |
| 1211 | |
| 1212 // TODO(tschumann): We don't have test making sure the NTPSnippetsFetcher | |
| 1213 // actually gets the proper parameters. Add tests with an injected | |
| 1214 // NTPSnippetsFetcher to verify the parameters, including proper handling of | |
| 1215 // dismissed and known_ids. | |
| 1216 | |
| 1217 namespace { | |
| 1218 | |
| 1219 // Workaround for gMock's lack of support for movable types. | |
| 1220 void SuggestionsLoaded( | |
| 1221 MockFunction<void(Status, const std::vector<ContentSuggestion>&)>* loaded, | |
| 1222 Status status, | |
| 1223 std::vector<ContentSuggestion> suggestions) { | |
| 1224 loaded->Call(status, suggestions); | |
| 1225 } | |
| 1226 | |
| 1227 } // namespace | |
| 1228 | |
| 1229 TEST_F(RemoteSuggestionsProviderTest, ReturnFetchRequestEmptyBeforeInit) { | |
| 1230 auto service = MakeSnippetsServiceWithoutInitialization(); | |
| 1231 MockFunction<void(Status, const std::vector<ContentSuggestion>&)> loaded; | |
| 1232 EXPECT_CALL(loaded, Call(HasCode(StatusCode::TEMPORARY_ERROR), IsEmpty())); | |
| 1233 service->Fetch(articles_category(), std::set<std::string>(), | |
| 1234 base::Bind(&SuggestionsLoaded, &loaded)); | |
| 1235 base::RunLoop().RunUntilIdle(); | |
| 1236 } | |
| 1237 | |
| 1238 TEST_F(RemoteSuggestionsProviderTest, ReturnTemporaryErrorForInvalidJson) { | |
| 1239 auto service = MakeSnippetsService(); | |
| 1240 | |
| 1241 MockFunction<void(Status, const std::vector<ContentSuggestion>&)> loaded; | |
| 1242 EXPECT_CALL(loaded, Call(HasCode(StatusCode::TEMPORARY_ERROR), IsEmpty())); | |
| 1243 LoadMoreFromJSONString(service.get(), articles_category(), | |
| 1244 "invalid json string}]}", | |
| 1245 /*known_ids=*/std::set<std::string>(), | |
| 1246 base::Bind(&SuggestionsLoaded, &loaded)); | |
| 1247 EXPECT_THAT(service->snippets_fetcher()->last_status(), | |
| 1248 StartsWith("Received invalid JSON")); | |
| 1249 } | |
| 1250 | |
| 1251 TEST_F(RemoteSuggestionsProviderTest, ReturnTemporaryErrorForInvalidSnippet) { | |
| 1252 auto service = MakeSnippetsService(); | |
| 1253 | |
| 1254 MockFunction<void(Status, const std::vector<ContentSuggestion>&)> loaded; | |
| 1255 EXPECT_CALL(loaded, Call(HasCode(StatusCode::TEMPORARY_ERROR), IsEmpty())); | |
| 1256 LoadMoreFromJSONString(service.get(), articles_category(), | |
| 1257 GetTestJson({GetIncompleteSnippet()}), | |
| 1258 /*known_ids=*/std::set<std::string>(), | |
| 1259 base::Bind(&SuggestionsLoaded, &loaded)); | |
| 1260 EXPECT_THAT(service->snippets_fetcher()->last_status(), | |
| 1261 StartsWith("Invalid / empty list")); | |
| 1262 } | |
| 1263 | |
| 1264 TEST_F(RemoteSuggestionsProviderTest, ReturnTemporaryErrorForRequestFailure) { | |
| 1265 // Created SnippetsService will fail by default with unsuccessful request. | |
| 1266 auto service = MakeSnippetsService(/*set_empty_response=*/false); | |
| 1267 | |
| 1268 MockFunction<void(Status, const std::vector<ContentSuggestion>&)> loaded; | |
| 1269 EXPECT_CALL(loaded, Call(HasCode(StatusCode::TEMPORARY_ERROR), IsEmpty())); | |
| 1270 service->Fetch(articles_category(), | |
| 1271 /*known_ids=*/std::set<std::string>(), | |
| 1272 base::Bind(&SuggestionsLoaded, &loaded)); | |
| 1273 base::RunLoop().RunUntilIdle(); | |
| 1274 } | |
| 1275 | |
| 1276 TEST_F(RemoteSuggestionsProviderTest, ReturnTemporaryErrorForHttpFailure) { | |
| 1277 auto service = MakeSnippetsService(); | |
| 1278 SetUpHttpError(); | |
| 1279 | |
| 1280 MockFunction<void(Status, const std::vector<ContentSuggestion>&)> loaded; | |
| 1281 EXPECT_CALL(loaded, Call(HasCode(StatusCode::TEMPORARY_ERROR), IsEmpty())); | |
| 1282 service->Fetch(articles_category(), | |
| 1283 /*known_ids=*/std::set<std::string>(), | |
| 1284 base::Bind(&SuggestionsLoaded, &loaded)); | |
| 1285 base::RunLoop().RunUntilIdle(); | |
| 1286 } | |
| 1287 | |
| 1288 TEST_F(RemoteSuggestionsProviderTest, LoadInvalidJson) { | |
| 1289 auto service = MakeSnippetsService(); | |
| 1290 | |
| 1291 LoadFromJSONString(service.get(), GetTestJson({GetInvalidSnippet()})); | |
| 1292 EXPECT_THAT(service->snippets_fetcher()->last_status(), | |
| 1293 StartsWith("Received invalid JSON")); | |
| 1294 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty()); | |
| 1295 } | |
| 1296 | |
| 1297 TEST_F(RemoteSuggestionsProviderTest, LoadInvalidJsonWithExistingSnippets) { | |
| 1298 auto service = MakeSnippetsService(); | |
| 1299 | |
| 1300 LoadFromJSONString(service.get(), GetTestJson({GetSnippet()})); | |
| 1301 ASSERT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(1)); | |
| 1302 ASSERT_EQ("OK", service->snippets_fetcher()->last_status()); | |
| 1303 | |
| 1304 LoadFromJSONString(service.get(), GetTestJson({GetInvalidSnippet()})); | |
| 1305 EXPECT_THAT(service->snippets_fetcher()->last_status(), | |
| 1306 StartsWith("Received invalid JSON")); | |
| 1307 // This should not have changed the existing snippets. | |
| 1308 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(1)); | |
| 1309 } | |
| 1310 | |
| 1311 TEST_F(RemoteSuggestionsProviderTest, LoadIncompleteJson) { | |
| 1312 auto service = MakeSnippetsService(); | |
| 1313 | |
| 1314 LoadFromJSONString(service.get(), GetTestJson({GetIncompleteSnippet()})); | |
| 1315 EXPECT_EQ("Invalid / empty list.", | |
| 1316 service->snippets_fetcher()->last_status()); | |
| 1317 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty()); | |
| 1318 } | |
| 1319 | |
| 1320 TEST_F(RemoteSuggestionsProviderTest, LoadIncompleteJsonWithExistingSnippets) { | |
| 1321 auto service = MakeSnippetsService(); | |
| 1322 | |
| 1323 LoadFromJSONString(service.get(), GetTestJson({GetSnippet()})); | |
| 1324 ASSERT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(1)); | |
| 1325 | |
| 1326 LoadFromJSONString(service.get(), GetTestJson({GetIncompleteSnippet()})); | |
| 1327 EXPECT_EQ("Invalid / empty list.", | |
| 1328 service->snippets_fetcher()->last_status()); | |
| 1329 // This should not have changed the existing snippets. | |
| 1330 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(1)); | |
| 1331 } | |
| 1332 | |
| 1333 TEST_F(RemoteSuggestionsProviderTest, Dismiss) { | |
| 1334 auto service = MakeSnippetsService(); | |
| 1335 | |
| 1336 std::string json_str( | |
| 1337 GetTestJson({GetSnippetWithSources("http://site.com", "Source 1", "")})); | |
| 1338 | |
| 1339 LoadFromJSONString(service.get(), json_str); | |
| 1340 | |
| 1341 ASSERT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(1)); | |
| 1342 // Load the image to store it in the database. | |
| 1343 ServeImageCallback cb = | |
| 1344 base::Bind(&ServeOneByOneImage, &service->GetImageFetcherForTesting()); | |
| 1345 EXPECT_CALL(*image_fetcher(), StartOrQueueNetworkRequest(_, _, _)) | |
| 1346 .WillOnce(WithArgs<0, 2>(Invoke(&cb, &ServeImageCallback::Run))); | |
| 1347 image_decoder()->SetDecodedImage(gfx::test::CreateImage(1, 1)); | |
| 1348 gfx::Image image = FetchImage(service.get(), MakeArticleID(kSnippetUrl)); | |
| 1349 EXPECT_FALSE(image.IsEmpty()); | |
| 1350 EXPECT_EQ(1, image.Width()); | |
| 1351 | |
| 1352 // Dismissing a non-existent snippet shouldn't do anything. | |
| 1353 service->DismissSuggestion(MakeArticleID("http://othersite.com")); | |
| 1354 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(1)); | |
| 1355 | |
| 1356 // Dismiss the snippet. | |
| 1357 service->DismissSuggestion(MakeArticleID(kSnippetUrl)); | |
| 1358 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty()); | |
| 1359 | |
| 1360 // Verify we can still load the image of the discarded snippet (other NTPs | |
| 1361 // might still reference it). This should come from the database -- no network | |
| 1362 // fetch necessary. | |
| 1363 image_decoder()->SetDecodedImage(gfx::test::CreateImage(1, 1)); | |
| 1364 image = FetchImage(service.get(), MakeArticleID(kSnippetUrl)); | |
| 1365 EXPECT_FALSE(image.IsEmpty()); | |
| 1366 EXPECT_EQ(1, image.Width()); | |
| 1367 | |
| 1368 // Make sure that fetching the same snippet again does not re-add it. | |
| 1369 LoadFromJSONString(service.get(), json_str); | |
| 1370 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty()); | |
| 1371 | |
| 1372 // The snippet should stay dismissed even after re-creating the service. | |
| 1373 ResetSnippetsService(&service, /*set_empty_response=*/true); | |
| 1374 LoadFromJSONString(service.get(), json_str); | |
| 1375 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty()); | |
| 1376 | |
| 1377 // The snippet can be added again after clearing dismissed snippets. | |
| 1378 service->ClearDismissedSuggestionsForDebugging(articles_category()); | |
| 1379 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty()); | |
| 1380 LoadFromJSONString(service.get(), json_str); | |
| 1381 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(1)); | |
| 1382 } | |
| 1383 | |
| 1384 TEST_F(RemoteSuggestionsProviderTest, GetDismissed) { | |
| 1385 auto service = MakeSnippetsService(); | |
| 1386 | |
| 1387 LoadFromJSONString(service.get(), GetTestJson({GetSnippet()})); | |
| 1388 | |
| 1389 service->DismissSuggestion(MakeArticleID(kSnippetUrl)); | |
| 1390 | |
| 1391 service->GetDismissedSuggestionsForDebugging( | |
| 1392 articles_category(), | |
| 1393 base::Bind( | |
| 1394 [](RemoteSuggestionsProvider* service, | |
| 1395 RemoteSuggestionsProviderTest* test, | |
| 1396 std::vector<ContentSuggestion> dismissed_suggestions) { | |
| 1397 EXPECT_EQ(1u, dismissed_suggestions.size()); | |
| 1398 for (auto& suggestion : dismissed_suggestions) { | |
| 1399 EXPECT_EQ(test->MakeArticleID(kSnippetUrl), suggestion.id()); | |
| 1400 } | |
| 1401 }, | |
| 1402 service.get(), this)); | |
| 1403 base::RunLoop().RunUntilIdle(); | |
| 1404 | |
| 1405 // There should be no dismissed snippet after clearing the list. | |
| 1406 service->ClearDismissedSuggestionsForDebugging(articles_category()); | |
| 1407 service->GetDismissedSuggestionsForDebugging( | |
| 1408 articles_category(), | |
| 1409 base::Bind( | |
| 1410 [](RemoteSuggestionsProvider* service, | |
| 1411 RemoteSuggestionsProviderTest* test, | |
| 1412 std::vector<ContentSuggestion> dismissed_suggestions) { | |
| 1413 EXPECT_EQ(0u, dismissed_suggestions.size()); | |
| 1414 }, | |
| 1415 service.get(), this)); | |
| 1416 base::RunLoop().RunUntilIdle(); | |
| 1417 } | |
| 1418 | |
| 1419 TEST_F(RemoteSuggestionsProviderTest, CreationTimestampParseFail) { | |
| 1420 auto service = MakeSnippetsService(); | |
| 1421 | |
| 1422 std::string json = | |
| 1423 GetSnippetWithTimes(GetDefaultCreationTime(), GetDefaultExpirationTime()); | |
| 1424 base::ReplaceFirstSubstringAfterOffset( | |
| 1425 &json, 0, FormatTime(GetDefaultCreationTime()), "aaa1448459205"); | |
| 1426 std::string json_str(GetTestJson({json})); | |
| 1427 | |
| 1428 LoadFromJSONString(service.get(), json_str); | |
| 1429 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty()); | |
| 1430 } | |
| 1431 | |
| 1432 TEST_F(RemoteSuggestionsProviderTest, RemoveExpiredDismissedContent) { | |
| 1433 auto service = MakeSnippetsService(); | |
| 1434 | |
| 1435 std::string json_str1(GetTestJson({GetExpiredSnippet()})); | |
| 1436 // Load it. | |
| 1437 LoadFromJSONString(service.get(), json_str1); | |
| 1438 // Load the image to store it in the database. | |
| 1439 // TODO(tschumann): Introduce some abstraction to nicely work with image | |
| 1440 // fetching expectations. | |
| 1441 ServeImageCallback cb = | |
| 1442 base::Bind(&ServeOneByOneImage, &service->GetImageFetcherForTesting()); | |
| 1443 EXPECT_CALL(*image_fetcher(), StartOrQueueNetworkRequest(_, _, _)) | |
| 1444 .WillOnce(WithArgs<0, 2>(Invoke(&cb, &ServeImageCallback::Run))); | |
| 1445 image_decoder()->SetDecodedImage(gfx::test::CreateImage(1, 1)); | |
| 1446 gfx::Image image = FetchImage(service.get(), MakeArticleID(kSnippetUrl)); | |
| 1447 EXPECT_FALSE(image.IsEmpty()); | |
| 1448 EXPECT_EQ(1, image.Width()); | |
| 1449 | |
| 1450 // Dismiss the suggestion | |
| 1451 service->DismissSuggestion( | |
| 1452 ContentSuggestion::ID(articles_category(), kSnippetUrl)); | |
| 1453 | |
| 1454 // Load a different snippet - this will clear the expired dismissed ones. | |
| 1455 std::string json_str2(GetTestJson({GetSnippetWithUrl(kSnippetUrl2)})); | |
| 1456 LoadFromJSONString(service.get(), json_str2); | |
| 1457 | |
| 1458 EXPECT_THAT(service->GetDismissedSnippetsForTesting(articles_category()), | |
| 1459 IsEmpty()); | |
| 1460 | |
| 1461 // Verify the image got removed, too. | |
| 1462 EXPECT_TRUE(FetchImage(service.get(), MakeArticleID(kSnippetUrl)).IsEmpty()); | |
| 1463 } | |
| 1464 | |
| 1465 TEST_F(RemoteSuggestionsProviderTest, ExpiredContentNotRemoved) { | |
| 1466 auto service = MakeSnippetsService(); | |
| 1467 | |
| 1468 std::string json_str(GetTestJson({GetExpiredSnippet()})); | |
| 1469 | |
| 1470 LoadFromJSONString(service.get(), json_str); | |
| 1471 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(1)); | |
| 1472 } | |
| 1473 | |
| 1474 TEST_F(RemoteSuggestionsProviderTest, TestSingleSource) { | |
| 1475 auto service = MakeSnippetsService(); | |
| 1476 | |
| 1477 std::string json_str(GetTestJson({GetSnippetWithSources( | |
| 1478 "http://source1.com", "Source 1", "http://source1.amp.com")})); | |
| 1479 | |
| 1480 LoadFromJSONString(service.get(), json_str); | |
| 1481 ASSERT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(1)); | |
| 1482 const NTPSnippet& snippet = | |
| 1483 *service->GetSnippetsForTesting(articles_category()).front(); | |
| 1484 EXPECT_EQ(snippet.id(), kSnippetUrl); | |
| 1485 EXPECT_EQ(snippet.url(), GURL("http://source1.com")); | |
| 1486 EXPECT_EQ(snippet.publisher_name(), std::string("Source 1")); | |
| 1487 EXPECT_EQ(snippet.amp_url(), GURL("http://source1.amp.com")); | |
| 1488 } | |
| 1489 | |
| 1490 TEST_F(RemoteSuggestionsProviderTest, TestSingleSourceWithMalformedUrl) { | |
| 1491 auto service = MakeSnippetsService(); | |
| 1492 | |
| 1493 std::string json_str(GetTestJson({GetSnippetWithSources( | |
| 1494 "ceci n'est pas un url", "Source 1", "http://source1.amp.com")})); | |
| 1495 | |
| 1496 LoadFromJSONString(service.get(), json_str); | |
| 1497 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty()); | |
| 1498 } | |
| 1499 | |
| 1500 TEST_F(RemoteSuggestionsProviderTest, TestSingleSourceWithMissingData) { | |
| 1501 auto service = MakeSnippetsService(); | |
| 1502 | |
| 1503 std::string json_str( | |
| 1504 GetTestJson({GetSnippetWithSources("http://source1.com", "", "")})); | |
| 1505 | |
| 1506 LoadFromJSONString(service.get(), json_str); | |
| 1507 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty()); | |
| 1508 } | |
| 1509 | |
| 1510 TEST_F(RemoteSuggestionsProviderTest, LogNumArticlesHistogram) { | |
| 1511 auto service = MakeSnippetsService(); | |
| 1512 | |
| 1513 base::HistogramTester tester; | |
| 1514 LoadFromJSONString(service.get(), GetTestJson({GetInvalidSnippet()})); | |
| 1515 | |
| 1516 EXPECT_THAT(tester.GetAllSamples("NewTabPage.Snippets.NumArticles"), | |
| 1517 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1))); | |
| 1518 | |
| 1519 // Invalid JSON shouldn't contribute to NumArticlesFetched. | |
| 1520 EXPECT_THAT(tester.GetAllSamples("NewTabPage.Snippets.NumArticlesFetched"), | |
| 1521 IsEmpty()); | |
| 1522 | |
| 1523 // Valid JSON with empty list. | |
| 1524 LoadFromJSONString(service.get(), GetTestJson(std::vector<std::string>())); | |
| 1525 EXPECT_THAT(tester.GetAllSamples("NewTabPage.Snippets.NumArticles"), | |
| 1526 ElementsAre(base::Bucket(/*min=*/0, /*count=*/2))); | |
| 1527 EXPECT_THAT(tester.GetAllSamples("NewTabPage.Snippets.NumArticlesFetched"), | |
| 1528 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1))); | |
| 1529 | |
| 1530 // Snippet list should be populated with size 1. | |
| 1531 LoadFromJSONString(service.get(), GetTestJson({GetSnippet()})); | |
| 1532 EXPECT_THAT(tester.GetAllSamples("NewTabPage.Snippets.NumArticles"), | |
| 1533 ElementsAre(base::Bucket(/*min=*/0, /*count=*/2), | |
| 1534 base::Bucket(/*min=*/1, /*count=*/1))); | |
| 1535 EXPECT_THAT(tester.GetAllSamples("NewTabPage.Snippets.NumArticlesFetched"), | |
| 1536 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1), | |
| 1537 base::Bucket(/*min=*/1, /*count=*/1))); | |
| 1538 | |
| 1539 // Duplicate snippet shouldn't increase the list size. | |
| 1540 LoadFromJSONString(service.get(), GetTestJson({GetSnippet()})); | |
| 1541 EXPECT_THAT(tester.GetAllSamples("NewTabPage.Snippets.NumArticles"), | |
| 1542 ElementsAre(base::Bucket(/*min=*/0, /*count=*/2), | |
| 1543 base::Bucket(/*min=*/1, /*count=*/2))); | |
| 1544 EXPECT_THAT(tester.GetAllSamples("NewTabPage.Snippets.NumArticlesFetched"), | |
| 1545 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1), | |
| 1546 base::Bucket(/*min=*/1, /*count=*/2))); | |
| 1547 EXPECT_THAT( | |
| 1548 tester.GetAllSamples("NewTabPage.Snippets.NumArticlesZeroDueToDiscarded"), | |
| 1549 IsEmpty()); | |
| 1550 | |
| 1551 // Dismissing a snippet should decrease the list size. This will only be | |
| 1552 // logged after the next fetch. | |
| 1553 service->DismissSuggestion(MakeArticleID(kSnippetUrl)); | |
| 1554 LoadFromJSONString(service.get(), GetTestJson({GetSnippet()})); | |
| 1555 EXPECT_THAT(tester.GetAllSamples("NewTabPage.Snippets.NumArticles"), | |
| 1556 ElementsAre(base::Bucket(/*min=*/0, /*count=*/3), | |
| 1557 base::Bucket(/*min=*/1, /*count=*/2))); | |
| 1558 // Dismissed snippets shouldn't influence NumArticlesFetched. | |
| 1559 EXPECT_THAT(tester.GetAllSamples("NewTabPage.Snippets.NumArticlesFetched"), | |
| 1560 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1), | |
| 1561 base::Bucket(/*min=*/1, /*count=*/3))); | |
| 1562 EXPECT_THAT( | |
| 1563 tester.GetAllSamples("NewTabPage.Snippets.NumArticlesZeroDueToDiscarded"), | |
| 1564 ElementsAre(base::Bucket(/*min=*/1, /*count=*/1))); | |
| 1565 | |
| 1566 // There is only a single, dismissed snippet in the database, so recreating | |
| 1567 // the service will require us to re-fetch. | |
| 1568 tester.ExpectTotalCount("NewTabPage.Snippets.NumArticlesFetched", 4); | |
| 1569 ResetSnippetsService(&service, /*set_empty_response=*/true); | |
| 1570 EXPECT_EQ(observer().StatusForCategory(articles_category()), | |
| 1571 CategoryStatus::AVAILABLE); | |
| 1572 tester.ExpectTotalCount("NewTabPage.Snippets.NumArticlesFetched", 5); | |
| 1573 EXPECT_THAT( | |
| 1574 tester.GetAllSamples("NewTabPage.Snippets.NumArticlesZeroDueToDiscarded"), | |
| 1575 ElementsAre(base::Bucket(/*min=*/1, /*count=*/2))); | |
| 1576 | |
| 1577 // But if there's a non-dismissed snippet in the database, recreating it | |
| 1578 // shouldn't trigger a fetch. | |
| 1579 LoadFromJSONString( | |
| 1580 service.get(), | |
| 1581 GetTestJson({GetSnippetWithUrl("http://not-dismissed.com")})); | |
| 1582 tester.ExpectTotalCount("NewTabPage.Snippets.NumArticlesFetched", 6); | |
| 1583 ResetSnippetsService(&service, /*set_empty_response=*/true); | |
| 1584 tester.ExpectTotalCount("NewTabPage.Snippets.NumArticlesFetched", 6); | |
| 1585 } | |
| 1586 | |
| 1587 TEST_F(RemoteSuggestionsProviderTest, DismissShouldRespectAllKnownUrls) { | |
| 1588 auto service = MakeSnippetsService(); | |
| 1589 | |
| 1590 const base::Time creation = GetDefaultCreationTime(); | |
| 1591 const base::Time expiry = GetDefaultExpirationTime(); | |
| 1592 const std::vector<std::string> source_urls = { | |
| 1593 "http://mashable.com/2016/05/11/stolen", | |
| 1594 "http://www.aol.com/article/2016/05/stolen-doggie"}; | |
| 1595 const std::vector<std::string> publishers = {"Mashable", "AOL"}; | |
| 1596 const std::vector<std::string> amp_urls = { | |
| 1597 "http://mashable-amphtml.googleusercontent.com/1", | |
| 1598 "http://t2.gstatic.com/images?q=tbn:3"}; | |
| 1599 | |
| 1600 // Add the snippet from the mashable domain. | |
| 1601 LoadFromJSONString(service.get(), | |
| 1602 GetTestJson({GetSnippetWithUrlAndTimesAndSource( | |
| 1603 source_urls, source_urls[0], creation, expiry, | |
| 1604 publishers[0], amp_urls[0])})); | |
| 1605 ASSERT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(1)); | |
| 1606 // Dismiss the snippet via the mashable source corpus ID. | |
| 1607 service->DismissSuggestion(MakeArticleID(source_urls[0])); | |
| 1608 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty()); | |
| 1609 | |
| 1610 // The same article from the AOL domain should now be detected as dismissed. | |
| 1611 LoadFromJSONString(service.get(), | |
| 1612 GetTestJson({GetSnippetWithUrlAndTimesAndSource( | |
| 1613 source_urls, source_urls[1], creation, expiry, | |
| 1614 publishers[1], amp_urls[1])})); | |
| 1615 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty()); | |
| 1616 } | |
| 1617 | |
| 1618 TEST_F(RemoteSuggestionsProviderTest, StatusChanges) { | |
| 1619 auto service = MakeSnippetsService(); | |
| 1620 | |
| 1621 // Simulate user signed out | |
| 1622 SetUpFetchResponse(GetTestJson({GetSnippet()})); | |
| 1623 service->OnStatusChanged(RemoteSuggestionsStatus::ENABLED_AND_SIGNED_IN, | |
| 1624 RemoteSuggestionsStatus::SIGNED_OUT_AND_DISABLED); | |
| 1625 | |
| 1626 base::RunLoop().RunUntilIdle(); | |
| 1627 EXPECT_THAT(observer().StatusForCategory(articles_category()), | |
| 1628 Eq(CategoryStatus::SIGNED_OUT)); | |
| 1629 EXPECT_THAT(RemoteSuggestionsProvider::State::DISABLED, Eq(service->state_)); | |
| 1630 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), | |
| 1631 IsEmpty()); // No fetch should be made. | |
| 1632 | |
| 1633 // Simulate user sign in. The service should be ready again and load snippets. | |
| 1634 SetUpFetchResponse(GetTestJson({GetSnippet()})); | |
| 1635 service->OnStatusChanged(RemoteSuggestionsStatus::SIGNED_OUT_AND_DISABLED, | |
| 1636 RemoteSuggestionsStatus::ENABLED_AND_SIGNED_IN); | |
| 1637 EXPECT_THAT(observer().StatusForCategory(articles_category()), | |
| 1638 Eq(CategoryStatus::AVAILABLE_LOADING)); | |
| 1639 | |
| 1640 base::RunLoop().RunUntilIdle(); | |
| 1641 EXPECT_THAT(observer().StatusForCategory(articles_category()), | |
| 1642 Eq(CategoryStatus::AVAILABLE)); | |
| 1643 EXPECT_THAT(RemoteSuggestionsProvider::State::READY, Eq(service->state_)); | |
| 1644 EXPECT_FALSE(service->GetSnippetsForTesting(articles_category()).empty()); | |
| 1645 } | |
| 1646 | |
| 1647 TEST_F(RemoteSuggestionsProviderTest, ImageReturnedWithTheSameId) { | |
| 1648 auto service = MakeSnippetsService(); | |
| 1649 | |
| 1650 LoadFromJSONString(service.get(), GetTestJson({GetSnippet()})); | |
| 1651 | |
| 1652 gfx::Image image; | |
| 1653 MockFunction<void(const gfx::Image&)> image_fetched; | |
| 1654 ServeImageCallback cb = | |
| 1655 base::Bind(&ServeOneByOneImage, &service->GetImageFetcherForTesting()); | |
| 1656 { | |
| 1657 InSequence s; | |
| 1658 EXPECT_CALL(*image_fetcher(), StartOrQueueNetworkRequest(_, _, _)) | |
| 1659 .WillOnce(WithArgs<0, 2>(Invoke(&cb, &ServeImageCallback::Run))); | |
| 1660 EXPECT_CALL(image_fetched, Call(_)).WillOnce(SaveArg<0>(&image)); | |
| 1661 } | |
| 1662 | |
| 1663 service->FetchSuggestionImage( | |
| 1664 MakeArticleID(kSnippetUrl), | |
| 1665 base::Bind(&MockFunction<void(const gfx::Image&)>::Call, | |
| 1666 base::Unretained(&image_fetched))); | |
| 1667 base::RunLoop().RunUntilIdle(); | |
| 1668 // Check that the image by ServeOneByOneImage is really served. | |
| 1669 EXPECT_EQ(1, image.Width()); | |
| 1670 } | |
| 1671 | |
| 1672 TEST_F(RemoteSuggestionsProviderTest, EmptyImageReturnedForNonExistentId) { | |
| 1673 auto service = MakeSnippetsService(); | |
| 1674 | |
| 1675 // Create a non-empty image so that we can test the image gets updated. | |
| 1676 gfx::Image image = gfx::test::CreateImage(1, 1); | |
| 1677 MockFunction<void(const gfx::Image&)> image_fetched; | |
| 1678 EXPECT_CALL(image_fetched, Call(_)).WillOnce(SaveArg<0>(&image)); | |
| 1679 | |
| 1680 service->FetchSuggestionImage( | |
| 1681 MakeArticleID(kSnippetUrl2), | |
| 1682 base::Bind(&MockFunction<void(const gfx::Image&)>::Call, | |
| 1683 base::Unretained(&image_fetched))); | |
| 1684 | |
| 1685 base::RunLoop().RunUntilIdle(); | |
| 1686 EXPECT_TRUE(image.IsEmpty()); | |
| 1687 } | |
| 1688 | |
| 1689 TEST_F(RemoteSuggestionsProviderTest, | |
| 1690 FetchingUnknownImageIdShouldNotHitDatabase) { | |
| 1691 // Testing that the provider is not accessing the database is tricky. | |
| 1692 // Therefore, we simply put in some data making sure that if the provider asks | |
| 1693 // the database, it will get a wrong answer. | |
| 1694 auto service = MakeSnippetsService(); | |
| 1695 | |
| 1696 ContentSuggestion::ID unknown_id = MakeArticleID(kSnippetUrl2); | |
| 1697 database()->SaveImage(unknown_id.id_within_category(), "some image blob"); | |
| 1698 // Set up the image decoder to always return the 1x1 test image. | |
| 1699 image_decoder()->SetDecodedImage(gfx::test::CreateImage(1, 1)); | |
| 1700 | |
| 1701 // Create a non-empty image so that we can test the image gets updated. | |
| 1702 gfx::Image image = gfx::test::CreateImage(2, 2); | |
| 1703 MockFunction<void(const gfx::Image&)> image_fetched; | |
| 1704 EXPECT_CALL(image_fetched, Call(_)).WillOnce(SaveArg<0>(&image)); | |
| 1705 | |
| 1706 service->FetchSuggestionImage( | |
| 1707 MakeArticleID(kSnippetUrl2), | |
| 1708 base::Bind(&MockFunction<void(const gfx::Image&)>::Call, | |
| 1709 base::Unretained(&image_fetched))); | |
| 1710 | |
| 1711 base::RunLoop().RunUntilIdle(); | |
| 1712 EXPECT_TRUE(image.IsEmpty()) << "got image with width: " << image.Width(); | |
| 1713 } | |
| 1714 | |
| 1715 TEST_F(RemoteSuggestionsProviderTest, ClearHistoryRemovesAllSuggestions) { | |
| 1716 auto service = MakeSnippetsService(); | |
| 1717 | |
| 1718 std::string first_snippet = GetSnippetWithUrl("http://url1.com"); | |
| 1719 std::string second_snippet = GetSnippetWithUrl("http://url2.com"); | |
| 1720 std::string json_str = GetTestJson({first_snippet, second_snippet}); | |
| 1721 LoadFromJSONString(service.get(), json_str); | |
| 1722 ASSERT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(2)); | |
| 1723 | |
| 1724 service->DismissSuggestion(MakeArticleID("http://url1.com")); | |
| 1725 ASSERT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(1)); | |
| 1726 ASSERT_THAT(service->GetDismissedSnippetsForTesting(articles_category()), | |
| 1727 SizeIs(1)); | |
| 1728 | |
| 1729 base::Time begin = base::Time::FromTimeT(123), | |
| 1730 end = base::Time::FromTimeT(456); | |
| 1731 base::Callback<bool(const GURL& url)> filter; | |
| 1732 service->ClearHistory(begin, end, filter); | |
| 1733 | |
| 1734 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty()); | |
| 1735 EXPECT_THAT(service->GetDismissedSnippetsForTesting(articles_category()), | |
| 1736 IsEmpty()); | |
| 1737 } | |
| 1738 | |
| 1739 TEST_F(RemoteSuggestionsProviderTest, SuggestionsFetchedOnSignInAndSignOut) { | |
| 1740 auto service = MakeSnippetsService(); | |
| 1741 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty()); | |
| 1742 | |
| 1743 // |MakeSnippetsService()| creates a service where user is signed in already, | |
| 1744 // so we start by signing out. | |
| 1745 SetUpFetchResponse(GetTestJson({GetSnippetN(1)})); | |
| 1746 service->OnStatusChanged(RemoteSuggestionsStatus::ENABLED_AND_SIGNED_IN, | |
| 1747 RemoteSuggestionsStatus::ENABLED_AND_SIGNED_OUT); | |
| 1748 base::RunLoop().RunUntilIdle(); | |
| 1749 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(1)); | |
| 1750 | |
| 1751 // Sign in to check a transition from signed out to signed in. | |
| 1752 SetUpFetchResponse(GetTestJson({GetSnippetN(1), GetSnippetN(2)})); | |
| 1753 service->OnStatusChanged(RemoteSuggestionsStatus::ENABLED_AND_SIGNED_OUT, | |
| 1754 RemoteSuggestionsStatus::ENABLED_AND_SIGNED_IN); | |
| 1755 base::RunLoop().RunUntilIdle(); | |
| 1756 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(2)); | |
| 1757 } | |
| 1758 | |
| 1759 TEST_F(RemoteSuggestionsProviderTest, ShouldClearOrphanedImagesOnRestart) { | |
| 1760 auto service = MakeSnippetsService(); | |
| 1761 | |
| 1762 LoadFromJSONString(service.get(), GetTestJson({GetSnippet()})); | |
| 1763 ServeImageCallback cb = | |
| 1764 base::Bind(&ServeOneByOneImage, &service->GetImageFetcherForTesting()); | |
| 1765 | |
| 1766 EXPECT_CALL(*image_fetcher(), StartOrQueueNetworkRequest(_, _, _)) | |
| 1767 .WillOnce(WithArgs<0, 2>(Invoke(&cb, &ServeImageCallback::Run))); | |
| 1768 image_decoder()->SetDecodedImage(gfx::test::CreateImage(1, 1)); | |
| 1769 | |
| 1770 gfx::Image image = FetchImage(service.get(), MakeArticleID(kSnippetUrl)); | |
| 1771 EXPECT_EQ(1, image.Width()); | |
| 1772 EXPECT_FALSE(image.IsEmpty()); | |
| 1773 | |
| 1774 // Send new suggestion which don't include the snippet referencing the image. | |
| 1775 LoadFromJSONString(service.get(), | |
| 1776 GetTestJson({GetSnippetWithUrl( | |
| 1777 "http://something.com/pletely/unrelated")})); | |
| 1778 // The image should still be available until a restart happens. | |
| 1779 EXPECT_FALSE(FetchImage(service.get(), MakeArticleID(kSnippetUrl)).IsEmpty()); | |
| 1780 ResetSnippetsService(&service, /*set_empty_response=*/true); | |
| 1781 // After the restart, the image should be garbage collected. | |
| 1782 EXPECT_TRUE(FetchImage(service.get(), MakeArticleID(kSnippetUrl)).IsEmpty()); | |
| 1783 } | |
| 1784 | |
| 1785 TEST_F(RemoteSuggestionsProviderTest, | |
| 1786 ShouldHandleMoreThanMaxSnippetsInResponse) { | |
| 1787 auto service = MakeSnippetsService(); | |
| 1788 | |
| 1789 std::vector<std::string> suggestions; | |
| 1790 for (int i = 0; i < service->GetMaxSnippetCountForTesting() + 1; ++i) { | |
| 1791 suggestions.push_back(GetSnippetWithUrl( | |
| 1792 base::StringPrintf("http://localhost/snippet-id-%d", i))); | |
| 1793 } | |
| 1794 LoadFromJSONString(service.get(), GetTestJson(suggestions)); | |
| 1795 // TODO(tschumann): We should probably trim out any additional results and | |
| 1796 // only serve the MaxSnippetCount items. | |
| 1797 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), | |
| 1798 SizeIs(service->GetMaxSnippetCountForTesting() + 1)); | |
| 1799 } | |
| 1800 | |
| 1801 TEST_F(RemoteSuggestionsProviderTest, StoreLastSuccessfullBackgroundFetchTime) { | |
| 1802 // On initialization of the RemoteSuggestionsProvider a background fetch is | |
| 1803 // triggered since the snippets DB is empty. Therefore the service must not be | |
| 1804 // initialized until the test clock is set. | |
| 1805 auto service = MakeSnippetsServiceWithoutInitialization(); | |
| 1806 | |
| 1807 auto simple_test_clock = base::MakeUnique<base::SimpleTestClock>(); | |
| 1808 base::SimpleTestClock* simple_test_clock_ptr = simple_test_clock.get(); | |
| 1809 service->SetClockForTesting(std::move(simple_test_clock)); | |
| 1810 | |
| 1811 // Test that the preference is correctly initialized with the default value 0. | |
| 1812 EXPECT_EQ( | |
| 1813 0, pref_service()->GetInt64(prefs::kLastSuccessfulBackgroundFetchTime)); | |
| 1814 | |
| 1815 WaitForSnippetsServiceInitialization(service.get(), | |
| 1816 /*set_empty_response=*/true); | |
| 1817 EXPECT_EQ( | |
| 1818 simple_test_clock_ptr->Now().ToInternalValue(), | |
| 1819 pref_service()->GetInt64(prefs::kLastSuccessfulBackgroundFetchTime)); | |
| 1820 | |
| 1821 // Advance the time and check whether the time was updated correctly after the | |
| 1822 // background fetch. | |
| 1823 simple_test_clock_ptr->Advance(TimeDelta::FromHours(1)); | |
| 1824 service->FetchSnippetsInTheBackground(); | |
| 1825 base::RunLoop().RunUntilIdle(); | |
| 1826 EXPECT_EQ( | |
| 1827 simple_test_clock_ptr->Now().ToInternalValue(), | |
| 1828 pref_service()->GetInt64(prefs::kLastSuccessfulBackgroundFetchTime)); | |
| 1829 // TODO(markusheintz): Add a test that simulates a browser restart once the | |
| 1830 // scheduler refactoring is done (crbug.com/672434). | |
| 1831 } | |
| 1832 | |
| 1833 } // namespace ntp_snippets | |
| OLD | NEW |