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/remote/ntp_snippets_fetcher.h" | 5 #include "components/ntp_snippets/remote/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 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 | 56 |
57 ACTION_P(MovePointeeTo, ptr) { | 57 ACTION_P(MovePointeeTo, ptr) { |
58 *ptr = std::move(*arg0); | 58 *ptr = std::move(*arg0); |
59 } | 59 } |
60 | 60 |
61 MATCHER(HasValue, "") { | 61 MATCHER(HasValue, "") { |
62 return static_cast<bool>(*arg); | 62 return static_cast<bool>(*arg); |
63 } | 63 } |
64 | 64 |
65 MATCHER(IsEmptyArticleList, "is an empty list of articles") { | 65 MATCHER(IsEmptyArticleList, "is an empty list of articles") { |
66 NTPSnippetsFetcher::OptionalSnippets& snippets = *arg; | 66 NTPSnippetsFetcher::OptionalFetchedCategories& fetched_categories = *arg; |
67 return snippets && snippets->size() == 1 && | 67 return fetched_categories && fetched_categories->size() == 1 && |
68 snippets->begin()->snippets.empty(); | 68 fetched_categories->begin()->snippets.empty(); |
69 } | 69 } |
70 | 70 |
71 MATCHER_P(IsSingleArticle, url, "is a list with the single article %(url)s") { | 71 MATCHER_P(IsSingleArticle, url, "is a list with the single article %(url)s") { |
72 NTPSnippetsFetcher::OptionalSnippets& snippets = *arg; | 72 NTPSnippetsFetcher::OptionalFetchedCategories& fetched_categories = *arg; |
73 return snippets && snippets->size() == 1 && | 73 return fetched_categories && fetched_categories->size() == 1 && |
74 snippets->begin()->snippets.size() == 1 && | 74 fetched_categories->begin()->snippets.size() == 1 && |
75 snippets->begin()->snippets[0]->best_source().url.spec() == url; | 75 fetched_categories->begin()->snippets[0]->best_source().url.spec() == |
| 76 url; |
76 } | 77 } |
77 | 78 |
78 MATCHER_P(EqualsJSON, json, "equals JSON") { | 79 MATCHER_P(EqualsJSON, json, "equals JSON") { |
79 std::unique_ptr<base::Value> expected = base::JSONReader::Read(json); | 80 std::unique_ptr<base::Value> expected = base::JSONReader::Read(json); |
80 if (!expected) { | 81 if (!expected) { |
81 *result_listener << "INTERNAL ERROR: couldn't parse expected JSON"; | 82 *result_listener << "INTERNAL ERROR: couldn't parse expected JSON"; |
82 return false; | 83 return false; |
83 } | 84 } |
84 | 85 |
85 std::string err_msg; | 86 std::string err_msg; |
86 int err_line, err_col; | 87 int err_line, err_col; |
87 std::unique_ptr<base::Value> actual = base::JSONReader::ReadAndReturnError( | 88 std::unique_ptr<base::Value> actual = base::JSONReader::ReadAndReturnError( |
88 arg, base::JSON_PARSE_RFC, nullptr, &err_msg, &err_line, &err_col); | 89 arg, base::JSON_PARSE_RFC, nullptr, &err_msg, &err_line, &err_col); |
89 if (!actual) { | 90 if (!actual) { |
90 *result_listener << "input:" << err_line << ":" << err_col << ": " | 91 *result_listener << "input:" << err_line << ":" << err_col << ": " |
91 << "parse error: " << err_msg; | 92 << "parse error: " << err_msg; |
92 return false; | 93 return false; |
93 } | 94 } |
94 return base::Value::Equals(actual.get(), expected.get()); | 95 return base::Value::Equals(actual.get(), expected.get()); |
95 } | 96 } |
96 | 97 |
97 class MockSnippetsAvailableCallback { | 98 class MockSnippetsAvailableCallback { |
98 public: | 99 public: |
99 // Workaround for gMock's lack of support for movable arguments. | 100 // Workaround for gMock's lack of support for movable arguments. |
100 void WrappedRun(NTPSnippetsFetcher::OptionalSnippets snippets) { | 101 void WrappedRun( |
101 Run(&snippets); | 102 NTPSnippetsFetcher::OptionalFetchedCategories fetched_categories) { |
| 103 Run(&fetched_categories); |
102 } | 104 } |
103 | 105 |
104 MOCK_METHOD1(Run, void(NTPSnippetsFetcher::OptionalSnippets* snippets)); | 106 MOCK_METHOD1( |
| 107 Run, |
| 108 void(NTPSnippetsFetcher::OptionalFetchedCategories* fetched_categories)); |
105 }; | 109 }; |
106 | 110 |
107 // Factory for FakeURLFetcher objects that always generate errors. | 111 // Factory for FakeURLFetcher objects that always generate errors. |
108 class FailingFakeURLFetcherFactory : public net::URLFetcherFactory { | 112 class FailingFakeURLFetcherFactory : public net::URLFetcherFactory { |
109 public: | 113 public: |
110 std::unique_ptr<net::URLFetcher> CreateURLFetcher( | 114 std::unique_ptr<net::URLFetcher> CreateURLFetcher( |
111 int id, const GURL& url, net::URLFetcher::RequestType request_type, | 115 int id, const GURL& url, net::URLFetcher::RequestType request_type, |
112 net::URLFetcherDelegate* d) override { | 116 net::URLFetcherDelegate* d) override { |
113 return base::MakeUnique<net::FakeURLFetcher>( | 117 return base::MakeUnique<net::FakeURLFetcher>( |
114 url, d, /*response_data=*/std::string(), net::HTTP_NOT_FOUND, | 118 url, d, /*response_data=*/std::string(), net::HTTP_NOT_FOUND, |
(...skipping 414 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
529 " \"creationTime\" : \"2016-06-30T11:01:37.000Z\"," | 533 " \"creationTime\" : \"2016-06-30T11:01:37.000Z\"," |
530 " \"expirationTime\" : \"2016-07-01T11:01:37.000Z\"," | 534 " \"expirationTime\" : \"2016-07-01T11:01:37.000Z\"," |
531 " \"attribution\" : \"Foo News\"," | 535 " \"attribution\" : \"Foo News\"," |
532 " \"imageUrl\" : \"http://localhost/foo2.jpg\"," | 536 " \"imageUrl\" : \"http://localhost/foo2.jpg\"," |
533 " \"ampUrl\" : \"http://localhost/amp\"," | 537 " \"ampUrl\" : \"http://localhost/amp\"," |
534 " \"faviconUrl\" : \"http://localhost/favicon.ico\" " | 538 " \"faviconUrl\" : \"http://localhost/favicon.ico\" " |
535 " }]" | 539 " }]" |
536 "}]}"; | 540 "}]}"; |
537 SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK, | 541 SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK, |
538 net::URLRequestStatus::SUCCESS); | 542 net::URLRequestStatus::SUCCESS); |
539 NTPSnippetsFetcher::OptionalSnippets snippets; | 543 NTPSnippetsFetcher::OptionalFetchedCategories fetched_categories; |
540 EXPECT_CALL(mock_callback(), Run(_)) | 544 EXPECT_CALL(mock_callback(), Run(_)) |
541 .WillOnce(WithArg<0>(MovePointeeTo(&snippets))); | 545 .WillOnce(WithArg<0>(MovePointeeTo(&fetched_categories))); |
542 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), | 546 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), |
543 test_excluded(), | 547 test_excluded(), |
544 /*count=*/1, | 548 /*count=*/1, |
545 /*interactive_request=*/true); | 549 /*interactive_request=*/true); |
546 FastForwardUntilNoTasksRemain(); | 550 FastForwardUntilNoTasksRemain(); |
547 | 551 |
548 ASSERT_TRUE(snippets); | 552 ASSERT_TRUE(fetched_categories); |
549 ASSERT_THAT(snippets->size(), Eq(2u)); | 553 ASSERT_THAT(fetched_categories->size(), Eq(2u)); |
550 for (const auto& category : *snippets) { | 554 for (const auto& category : *fetched_categories) { |
551 const auto& articles = category.snippets; | 555 const auto& articles = category.snippets; |
552 switch (category.category.id()) { | 556 switch (category.category.id()) { |
553 case static_cast<int>(KnownCategories::ARTICLES): | 557 case static_cast<int>(KnownCategories::ARTICLES): |
554 ASSERT_THAT(articles.size(), Eq(1u)); | 558 ASSERT_THAT(articles.size(), Eq(1u)); |
555 EXPECT_THAT(articles[0]->best_source().url.spec(), | 559 EXPECT_THAT(articles[0]->best_source().url.spec(), |
556 Eq("http://localhost/foobar")); | 560 Eq("http://localhost/foobar")); |
557 break; | 561 break; |
558 case static_cast<int>(KnownCategories::ARTICLES) + 1: | 562 case static_cast<int>(KnownCategories::ARTICLES) + 1: |
559 ASSERT_THAT(articles.size(), Eq(1u)); | 563 ASSERT_THAT(articles.size(), Eq(1u)); |
560 EXPECT_THAT(articles[0]->best_source().url.spec(), | 564 EXPECT_THAT(articles[0]->best_source().url.spec(), |
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
789 EXPECT_THAT(histogram_tester().GetAllSamples( | 793 EXPECT_THAT(histogram_tester().GetAllSamples( |
790 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), | 794 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), |
791 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); | 795 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); |
792 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), | 796 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), |
793 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, | 797 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, |
794 /*count=*/1))); | 798 /*count=*/1))); |
795 } | 799 } |
796 | 800 |
797 ::std::ostream& operator<<( | 801 ::std::ostream& operator<<( |
798 ::std::ostream& os, | 802 ::std::ostream& os, |
799 const NTPSnippetsFetcher::OptionalSnippets& snippets) { | 803 const NTPSnippetsFetcher::OptionalFetchedCategories& fetched_categories) { |
800 if (snippets) { | 804 if (fetched_categories) { |
801 // Matchers above aren't any more precise than this, so this is sufficient | 805 // Matchers above aren't any more precise than this, so this is sufficient |
802 // for test-failure diagnostics. | 806 // for test-failure diagnostics. |
803 return os << "list with " << snippets->size() << " elements"; | 807 return os << "list with " << fetched_categories->size() << " elements"; |
804 } | 808 } |
805 return os << "null"; | 809 return os << "null"; |
806 } | 810 } |
807 | 811 |
808 } // namespace ntp_snippets | 812 } // namespace ntp_snippets |
OLD | NEW |