OLD | NEW |
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 Loading... |
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 459 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
546 .WillOnce(WithArg<0>(MovePointeeTo(&snippets))); | 547 .WillOnce(WithArg<0>(MovePointeeTo(&snippets))); |
547 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), | 548 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), |
548 test_excluded(), | 549 test_excluded(), |
549 /*count=*/1, | 550 /*count=*/1, |
550 /*force_request=*/true); | 551 /*force_request=*/true); |
551 FastForwardUntilNoTasksRemain(); | 552 FastForwardUntilNoTasksRemain(); |
552 | 553 |
553 ASSERT_TRUE(snippets); | 554 ASSERT_TRUE(snippets); |
554 ASSERT_THAT(snippets->size(), Eq(2u)); | 555 ASSERT_THAT(snippets->size(), Eq(2u)); |
555 for (const auto& category : *snippets) { | 556 for (const auto& category : *snippets) { |
556 const auto& articles = category.second; | 557 const auto& articles = category.snippets; |
557 switch (category.first.id()) { | 558 switch (category.category.id()) { |
558 case static_cast<int>(KnownCategories::ARTICLES): | 559 case static_cast<int>(KnownCategories::ARTICLES): |
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/foobar")); | 562 Eq("http://localhost/foobar")); |
562 break; | 563 break; |
563 case static_cast<int>(KnownCategories::ARTICLES) + 1: | 564 case static_cast<int>(KnownCategories::ARTICLES) + 1: |
564 ASSERT_THAT(articles.size(), Eq(1u)); | 565 ASSERT_THAT(articles.size(), Eq(1u)); |
565 EXPECT_THAT(articles[0]->best_source().url.spec(), | 566 EXPECT_THAT(articles[0]->best_source().url.spec(), |
566 Eq("http://localhost/foo2")); | 567 Eq("http://localhost/foo2")); |
567 break; | 568 break; |
568 default: | 569 default: |
569 FAIL() << "unknown category ID " << category.first.id(); | 570 FAIL() << "unknown category ID " << category.category.id(); |
570 } | 571 } |
571 } | 572 } |
572 | 573 |
573 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK")); | 574 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK")); |
574 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); | 575 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); |
575 EXPECT_THAT(histogram_tester().GetAllSamples( | 576 EXPECT_THAT(histogram_tester().GetAllSamples( |
576 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), | 577 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), |
577 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); | 578 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); |
578 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), | 579 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), |
579 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, | 580 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, |
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
805 if (snippets) { | 806 if (snippets) { |
806 // Matchers above aren't any more precise than this, so this is sufficient | 807 // Matchers above aren't any more precise than this, so this is sufficient |
807 // for test-failure diagnostics. | 808 // for test-failure diagnostics. |
808 return os << "list with " << snippets->size() << " elements"; | 809 return os << "list with " << snippets->size() << " elements"; |
809 } else { | 810 } else { |
810 return os << "null"; | 811 return os << "null"; |
811 } | 812 } |
812 } | 813 } |
813 | 814 |
814 } // namespace ntp_snippets | 815 } // namespace ntp_snippets |
OLD | NEW |