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

Side by Side Diff: components/ntp_snippets/ntp_snippets_fetcher_unittest.cc

Issue 2276383002: Support server-provided category names. (Closed) Base URL: https://chromium.googlesource.com/chromium/src@master
Patch Set: Move "ignored" comment. Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/ntp_snippets/ntp_snippets_fetcher.h" 5 #include "components/ntp_snippets/ntp_snippets_fetcher.h"
6 6
7 #include <map> 7 #include <map>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/json/json_reader.h" 10 #include "base/json/json_reader.h"
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
59 ACTION_P(MovePointeeTo, ptr) { 59 ACTION_P(MovePointeeTo, ptr) {
60 *ptr = std::move(*arg0); 60 *ptr = std::move(*arg0);
61 } 61 }
62 62
63 MATCHER(HasValue, "") { 63 MATCHER(HasValue, "") {
64 return static_cast<bool>(*arg); 64 return static_cast<bool>(*arg);
65 } 65 }
66 66
67 MATCHER(IsEmptyArticleList, "is an empty list of articles") { 67 MATCHER(IsEmptyArticleList, "is an empty list of articles") {
68 NTPSnippetsFetcher::OptionalSnippets& snippets = *arg; 68 NTPSnippetsFetcher::OptionalSnippets& snippets = *arg;
69 return snippets && snippets->size() == 1 && snippets->begin()->second.empty(); 69 return snippets && snippets->size() == 1 &&
70 snippets->begin()->snippets.empty();
70 } 71 }
71 72
72 MATCHER_P(IsSingleArticle, url, "is a list with the single article %(url)s") { 73 MATCHER_P(IsSingleArticle, url, "is a list with the single article %(url)s") {
73 NTPSnippetsFetcher::OptionalSnippets& snippets = *arg; 74 NTPSnippetsFetcher::OptionalSnippets& snippets = *arg;
74 return snippets && snippets->size() == 1 && 75 return snippets && snippets->size() == 1 &&
75 snippets->begin()->second.size() == 1 && 76 snippets->begin()->snippets.size() == 1 &&
76 snippets->begin()->second[0]->best_source().url.spec() == url; 77 snippets->begin()->snippets[0]->best_source().url.spec() == url;
77 } 78 }
78 79
79 MATCHER_P(EqualsJSON, json, "equals JSON") { 80 MATCHER_P(EqualsJSON, json, "equals JSON") {
80 std::unique_ptr<base::Value> expected = base::JSONReader::Read(json); 81 std::unique_ptr<base::Value> expected = base::JSONReader::Read(json);
81 if (!expected) { 82 if (!expected) {
82 *result_listener << "INTERNAL ERROR: couldn't parse expected JSON"; 83 *result_listener << "INTERNAL ERROR: couldn't parse expected JSON";
83 return false; 84 return false;
84 } 85 }
85 86
86 std::string err_msg; 87 std::string err_msg;
(...skipping 454 matching lines...) Expand 10 before | Expand all | Expand 10 after
541 .WillOnce(WithArg<0>(MovePointeeTo(&snippets))); 542 .WillOnce(WithArg<0>(MovePointeeTo(&snippets)));
542 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), 543 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(),
543 test_excluded(), 544 test_excluded(),
544 /*count=*/1, 545 /*count=*/1,
545 /*force_request=*/true); 546 /*force_request=*/true);
546 FastForwardUntilNoTasksRemain(); 547 FastForwardUntilNoTasksRemain();
547 548
548 ASSERT_TRUE(snippets); 549 ASSERT_TRUE(snippets);
549 ASSERT_THAT(snippets->size(), Eq(2u)); 550 ASSERT_THAT(snippets->size(), Eq(2u));
550 for (const auto& category : *snippets) { 551 for (const auto& category : *snippets) {
551 const auto& articles = category.second; 552 const auto& articles = category.snippets;
552 switch (category.first.id()) { 553 switch (category.category.id()) {
553 case static_cast<int>(KnownCategories::ARTICLES): 554 case static_cast<int>(KnownCategories::ARTICLES):
554 ASSERT_THAT(articles.size(), Eq(1u)); 555 ASSERT_THAT(articles.size(), Eq(1u));
555 EXPECT_THAT(articles[0]->best_source().url.spec(), 556 EXPECT_THAT(articles[0]->best_source().url.spec(),
556 Eq("http://localhost/foobar")); 557 Eq("http://localhost/foobar"));
557 break; 558 break;
558 case static_cast<int>(KnownCategories::ARTICLES) + 1: 559 case static_cast<int>(KnownCategories::ARTICLES) + 1:
559 ASSERT_THAT(articles.size(), Eq(1u)); 560 ASSERT_THAT(articles.size(), Eq(1u));
560 EXPECT_THAT(articles[0]->best_source().url.spec(), 561 EXPECT_THAT(articles[0]->best_source().url.spec(),
561 Eq("http://localhost/foo2")); 562 Eq("http://localhost/foo2"));
562 break; 563 break;
563 default: 564 default:
564 FAIL() << "unknown category ID " << category.first.id(); 565 FAIL() << "unknown category ID " << category.category.id();
565 } 566 }
566 } 567 }
567 568
568 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK")); 569 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK"));
569 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); 570 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr));
570 EXPECT_THAT(histogram_tester().GetAllSamples( 571 EXPECT_THAT(histogram_tester().GetAllSamples(
571 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), 572 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
572 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); 573 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1)));
573 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), 574 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"),
574 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, 575 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs,
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after
800 if (snippets) { 801 if (snippets) {
801 // Matchers above aren't any more precise than this, so this is sufficient 802 // Matchers above aren't any more precise than this, so this is sufficient
802 // for test-failure diagnostics. 803 // for test-failure diagnostics.
803 return os << "list with " << snippets->size() << " elements"; 804 return os << "list with " << snippets->size() << " elements";
804 } else { 805 } else {
805 return os << "null"; 806 return os << "null";
806 } 807 }
807 } 808 }
808 809
809 } // namespace ntp_snippets 810 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698