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

Unified Diff: components/ntp_snippets/remote/ntp_snippets_fetcher_unittest.cc

Issue 2421463002: FetchMore functionality backend (Closed)
Patch Set: NTBR. Rebasing a lot. Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: components/ntp_snippets/remote/ntp_snippets_fetcher_unittest.cc
diff --git a/components/ntp_snippets/remote/ntp_snippets_fetcher_unittest.cc b/components/ntp_snippets/remote/ntp_snippets_fetcher_unittest.cc
index c48f88fc60012248507091a2b3707a49448c0aa4..428d636c6d9886b20ddc1c5d49960211201e4e72 100644
--- a/components/ntp_snippets/remote/ntp_snippets_fetcher_unittest.cc
+++ b/components/ntp_snippets/remote/ntp_snippets_fetcher_unittest.cc
@@ -173,15 +173,26 @@ class NTPSnippetsFetcherTest : public testing::Test {
pref_service_.get(), &category_factory_, nullptr,
base::Bind(&ParseJsonDelayed), kAPIKey, user_classifier_.get());
- snippets_fetcher_->SetCallback(
- base::Bind(&MockSnippetsAvailableCallback::WrappedRun,
- base::Unretained(&mock_callback_)));
snippets_fetcher_->SetTickClockForTesting(
mock_task_runner_->GetMockTickClock());
// Increase initial time such that ticks are non-zero.
mock_task_runner_->FastForwardBy(base::TimeDelta::FromMilliseconds(1234));
}
+ NTPSnippetsFetcher::SnippetsAvailableCallback MakeMockCallback() {
+ return base::BindOnce(&MockSnippetsAvailableCallback::WrappedRun,
+ base::Unretained(&mock_callback_));
+ }
+
+ base::Optional<Category> NoExclusiveCategory() {
+ return base::Optional<Category>();
+ }
+
+ base::Optional<Category> OptionalArticlesCategory() {
+ return base::Optional<Category>(
+ CategoryFactory().FromKnownCategory(KnownCategories::ARTICLES));
+ }
+
NTPSnippetsFetcher& snippets_fetcher() { return *snippets_fetcher_; }
MockSnippetsAvailableCallback& mock_callback() { return mock_callback_; }
void FastForwardUntilNoTasksRemain() {
@@ -645,6 +656,97 @@ TEST_F(NTPSnippetsContentSuggestionsFetcherTest, ServerCategories) {
/*count=*/1)));
}
+TEST_F(NTPSnippetsContentSuggestionsFetcherTest, ExclusiveCategoryOnly) {
+ const std::string kJsonStr =
+ "{\"categories\" : [{"
+ " \"id\": 1,"
+ " \"localizedTitle\": \"Articles for You\","
+ " \"suggestions\" : [{"
+ " \"ids\" : [\"http://localhost/foobar\"],"
+ " \"title\" : \"Foo Barred from Baz\","
+ " \"snippet\" : \"...\","
+ " \"fullPageUrl\" : \"http://localhost/foobar\","
+ " \"creationTime\" : \"2016-06-30T11:01:37.000Z\","
+ " \"expirationTime\" : \"2016-07-01T11:01:37.000Z\","
+ " \"attribution\" : \"Foo News\","
+ " \"imageUrl\" : \"http://localhost/foobar.jpg\","
+ " \"ampUrl\" : \"http://localhost/amp\","
+ " \"faviconUrl\" : \"http://localhost/favicon.ico\" "
+ " }]"
+ "}, {"
+ " \"id\": 2,"
+ " \"localizedTitle\": \"Articles for Me\","
+ " \"suggestions\" : [{"
+ " \"ids\" : [\"http://localhost/foo2\"],"
+ " \"title\" : \"Foo Barred from Baz\","
+ " \"snippet\" : \"...\","
+ " \"fullPageUrl\" : \"http://localhost/foo2\","
+ " \"creationTime\" : \"2016-06-30T11:01:37.000Z\","
+ " \"expirationTime\" : \"2016-07-01T11:01:37.000Z\","
+ " \"attribution\" : \"Foo News\","
+ " \"imageUrl\" : \"http://localhost/foo2.jpg\","
+ " \"ampUrl\" : \"http://localhost/amp\","
+ " \"faviconUrl\" : \"http://localhost/favicon.ico\" "
+ " }]"
+ "}]}";
+ SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK,
+ net::URLRequestStatus::SUCCESS);
+ NTPSnippetsFetcher::OptionalFetchedCategories fetched_categories;
+ EXPECT_CALL(mock_callback(), Run(_))
+ .WillOnce(WithArg<0>(MovePointeeTo(&fetched_categories)));
+ auto params = test_params();
+ params.exclusive_category = OptionalArticlesCategory();
+ snippets_fetcher().FetchSnippets(params);
+ FastForwardUntilNoTasksRemain();
+
+ ASSERT_TRUE(fetched_categories);
+ ASSERT_THAT(fetched_categories->size(), Eq(1u));
+ for (const auto& category : *fetched_categories) {
vitaliii 2016/11/01 23:29:57 There is no need to have a loop if there is an ASS
fhorschig 2016/11/02 05:05:27 Correct. Dropped in CL 2446163005.
+ const auto& articles = category.snippets;
+ switch (category.category.id()) {
vitaliii 2016/11/01 23:29:57 Replace with an |if|.
fhorschig 2016/11/02 05:05:27 Also correct. Also dropped in CL 2446163005.
+ case static_cast<int>(KnownCategories::ARTICLES):
+ ASSERT_THAT(articles.size(), Eq(1u));
+ EXPECT_THAT(articles[0]->best_source().url.spec(),
+ Eq("http://localhost/foobar"));
+ break;
+ default:
+ FAIL() << "unexpected category with ID " << category.category.id();
+ }
+ }
+}
+
+TEST_F(NTPSnippetsContentSuggestionsFetcherTest, EmptyExclusiveCategory) {
+ const std::string kJsonStr =
+ "{\"categories\" : [{"
+ " \"id\": 2,"
+ " \"localizedTitle\": \"No Articles for You\","
+ " \"suggestions\" : [{"
+ " \"ids\" : [\"http://localhost/foobar\"],"
+ " \"title\" : \"Foo Barred from Baz\","
+ " \"snippet\" : \"...\","
+ " \"fullPageUrl\" : \"http://localhost/foobar\","
+ " \"creationTime\" : \"2016-06-30T11:01:37.000Z\","
+ " \"expirationTime\" : \"2016-07-01T11:01:37.000Z\","
+ " \"attribution\" : \"Foo News\","
+ " \"imageUrl\" : \"http://localhost/foobar.jpg\","
+ " \"ampUrl\" : \"http://localhost/amp\","
+ " \"faviconUrl\" : \"http://localhost/favicon.ico\" "
+ " }]"
+ "}]}";
+ SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK,
+ net::URLRequestStatus::SUCCESS);
+ NTPSnippetsFetcher::OptionalFetchedCategories fetched_categories;
+ EXPECT_CALL(mock_callback(), Run(_))
+ .WillOnce(WithArg<0>(MovePointeeTo(&fetched_categories)));
+ auto params = test_params();
+ params.exclusive_category = OptionalArticlesCategory();
+ snippets_fetcher().FetchSnippets(params);
+ FastForwardUntilNoTasksRemain();
+
+ ASSERT_TRUE(fetched_categories);
+ ASSERT_THAT(fetched_categories->size(), Eq(0u));
vitaliii 2016/11/01 23:29:57 nit: EXPECT_THAT
fhorschig 2016/11/02 05:05:27 Done in CL 2446163005.
+}
+
TEST_F(NTPSnippetsFetcherTest, ShouldFetchSuccessfullyEmptyList) {
const std::string kJsonStr = "{\"recos\": []}";
SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK,
@@ -664,7 +766,7 @@ TEST_F(NTPSnippetsFetcherTest, ShouldFetchSuccessfullyEmptyList) {
TEST_F(NTPSnippetsFetcherTest, ShouldRestrictToHosts) {
net::TestURLFetcherFactory test_url_fetcher_factory;
- NTPSnippetsFetcher::Params params = test_params();
+ auto params = test_params();
params.hosts = {"www.somehost1.com", "www.somehost2.com"};
params.count_to_fetch = 17;
snippets_fetcher().FetchSnippets(params);

Powered by Google App Engine
This is Rietveld 408576698