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

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

Issue 1937063002: [NTP Snippets] Add metric for fetch error cases (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@record_article_fetch_errors_0
Patch Set: Adopted FetchHttpResponseOrErrorCode. Created 4 years, 7 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 "base/memory/ptr_util.h" 7 #include "base/memory/ptr_util.h"
8 #include "base/message_loop/message_loop.h" 8 #include "base/message_loop/message_loop.h"
9 #include "base/run_loop.h" 9 #include "base/run_loop.h"
10 #include "base/strings/stringprintf.h" 10 #include "base/strings/stringprintf.h"
11 #include "base/test/histogram_tester.h"
11 #include "base/thread_task_runner_handle.h" 12 #include "base/thread_task_runner_handle.h"
12 #include "google_apis/google_api_keys.h" 13 #include "google_apis/google_api_keys.h"
13 #include "net/url_request/test_url_fetcher_factory.h" 14 #include "net/url_request/test_url_fetcher_factory.h"
14 #include "net/url_request/url_request_test_util.h" 15 #include "net/url_request/url_request_test_util.h"
15 #include "testing/gmock/include/gmock/gmock.h" 16 #include "testing/gmock/include/gmock/gmock.h"
16 #include "testing/gtest/include/gtest/gtest.h" 17 #include "testing/gtest/include/gtest/gtest.h"
17 18
18 namespace ntp_snippets { 19 namespace ntp_snippets {
19 namespace { 20 namespace {
20 21
22 using testing::ElementsAre;
21 using testing::IsEmpty; 23 using testing::IsEmpty;
22 using testing::IsNull; 24 using testing::IsNull;
23 using testing::Not; 25 using testing::Not;
24 using testing::NotNull; 26 using testing::NotNull;
25 27
26 const char kTestContentSnippetsServerFormat[] = 28 const char kTestContentSnippetsServerFormat[] =
27 "https://chromereader-pa.googleapis.com/v1/fetch?key=%s"; 29 "https://chromereader-pa.googleapis.com/v1/fetch?key=%s";
28 30
29 class MockSnippetsAvailableCallback { 31 class MockSnippetsAvailableCallback {
30 public: 32 public:
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
63 65
64 net::FakeURLFetcherFactory& fake_url_fetcher_factory() { 66 net::FakeURLFetcherFactory& fake_url_fetcher_factory() {
65 return fake_url_fetcher_factory_; 67 return fake_url_fetcher_factory_;
66 } 68 }
67 69
68 NTPSnippetsFetcher& snippets_fetcher() { return snippets_fetcher_; } 70 NTPSnippetsFetcher& snippets_fetcher() { return snippets_fetcher_; }
69 MockSnippetsAvailableCallback& mock_callback() { return mock_callback_; } 71 MockSnippetsAvailableCallback& mock_callback() { return mock_callback_; }
70 void RunUntilIdle() { message_loop_.RunUntilIdle(); } 72 void RunUntilIdle() { message_loop_.RunUntilIdle(); }
71 const GURL& test_url() { return test_url_; } 73 const GURL& test_url() { return test_url_; }
72 74
75 base::HistogramTester& histogram_tester() { return histogram_tester_; }
76
73 private: 77 private:
74 FailingFakeURLFetcherFactory failing_url_fetcher_factory_; 78 FailingFakeURLFetcherFactory failing_url_fetcher_factory_;
75 // Instantiation of factory automatically sets itself as URLFetcher's factory. 79 // Instantiation of factory automatically sets itself as URLFetcher's factory.
76 net::FakeURLFetcherFactory fake_url_fetcher_factory_; 80 net::FakeURLFetcherFactory fake_url_fetcher_factory_;
77 // Needed to use ThreadTaskRunnerHandle. 81 // Needed to use ThreadTaskRunnerHandle.
78 base::MessageLoop message_loop_; 82 base::MessageLoop message_loop_;
79 NTPSnippetsFetcher snippets_fetcher_; 83 NTPSnippetsFetcher snippets_fetcher_;
80 MockSnippetsAvailableCallback mock_callback_; 84 MockSnippetsAvailableCallback mock_callback_;
81 std::unique_ptr< 85 std::unique_ptr<
82 NTPSnippetsFetcher::SnippetsAvailableCallbackList::Subscription> 86 NTPSnippetsFetcher::SnippetsAvailableCallbackList::Subscription>
83 snippets_fetcher_subscription_; 87 snippets_fetcher_subscription_;
84 const GURL test_url_; 88 const GURL test_url_;
89 base::HistogramTester histogram_tester_;
85 90
86 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsFetcherTest); 91 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsFetcherTest);
87 }; 92 };
88 93
89 TEST_F(NTPSnippetsFetcherTest, ShouldNotFetchOnCreation) { 94 TEST_F(NTPSnippetsFetcherTest, ShouldNotFetchOnCreation) {
90 // The lack of registered baked in responses would cause any fetch to fail. 95 // The lack of registered baked in responses would cause any fetch to fail.
91 RunUntilIdle(); 96 RunUntilIdle();
97 EXPECT_THAT(histogram_tester().GetAllSamples(
98 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
99 IsEmpty());
92 } 100 }
93 101
94 TEST_F(NTPSnippetsFetcherTest, ShouldFetchSuccessfully) { 102 TEST_F(NTPSnippetsFetcherTest, ShouldFetchSuccessfully) {
95 const std::string json_str = "{ \"recos\": [] }"; 103 const std::string json_str = "{ \"recos\": [] }";
96 fake_url_fetcher_factory().SetFakeResponse(test_url(), 104 fake_url_fetcher_factory().SetFakeResponse(test_url(),
97 /*data=*/json_str, net::HTTP_OK, 105 /*data=*/json_str, net::HTTP_OK,
98 net::URLRequestStatus::SUCCESS); 106 net::URLRequestStatus::SUCCESS);
99 EXPECT_CALL(mock_callback(), Run(/*snippets_json=*/json_str, 107 EXPECT_CALL(mock_callback(), Run(/*snippets_json=*/json_str,
100 /*status_message=*/std::string())) 108 /*status_message=*/std::string()))
101 .Times(1); 109 .Times(1);
102 snippets_fetcher().FetchSnippets(/*hosts=*/std::set<std::string>(), 110 snippets_fetcher().FetchSnippets(/*hosts=*/std::set<std::string>(),
103 /*count=*/1); 111 /*count=*/1);
104 RunUntilIdle(); 112 RunUntilIdle();
113 EXPECT_THAT(histogram_tester().GetAllSamples(
114 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
115 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1)));
105 } 116 }
106 117
107 TEST_F(NTPSnippetsFetcherTest, ShouldReportUrlStatusError) { 118 TEST_F(NTPSnippetsFetcherTest, ShouldReportUrlStatusError) {
108 fake_url_fetcher_factory().SetFakeResponse(test_url(), 119 fake_url_fetcher_factory().SetFakeResponse(test_url(),
109 /*data=*/std::string(), 120 /*data=*/std::string(),
110 net::HTTP_NOT_FOUND, 121 net::HTTP_NOT_FOUND,
111 net::URLRequestStatus::FAILED); 122 net::URLRequestStatus::FAILED);
112 EXPECT_CALL(mock_callback(), 123 EXPECT_CALL(mock_callback(),
113 Run(/*snippets_json=*/std::string(), 124 Run(/*snippets_json=*/std::string(),
114 /*status_message=*/"URLRequestStatus error -2")) 125 /*status_message=*/"URLRequestStatus error -2"))
115 .Times(1); 126 .Times(1);
116 snippets_fetcher().FetchSnippets(/*hosts=*/std::set<std::string>(), 127 snippets_fetcher().FetchSnippets(/*hosts=*/std::set<std::string>(),
117 /*count=*/1); 128 /*count=*/1);
118 RunUntilIdle(); 129 RunUntilIdle();
130 EXPECT_THAT(histogram_tester().GetAllSamples(
131 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
132 ElementsAre(base::Bucket(/*min=*/-2, /*count=*/1)));
119 } 133 }
120 134
121 TEST_F(NTPSnippetsFetcherTest, ShouldReportHttpError) { 135 TEST_F(NTPSnippetsFetcherTest, ShouldReportHttpError) {
122 fake_url_fetcher_factory().SetFakeResponse(test_url(), 136 fake_url_fetcher_factory().SetFakeResponse(test_url(),
123 /*data=*/std::string(), 137 /*data=*/std::string(),
124 net::HTTP_NOT_FOUND, 138 net::HTTP_NOT_FOUND,
125 net::URLRequestStatus::SUCCESS); 139 net::URLRequestStatus::SUCCESS);
126 EXPECT_CALL(mock_callback(), Run(/*snippets_json=*/std::string(), 140 EXPECT_CALL(mock_callback(), Run(/*snippets_json=*/std::string(),
127 /*status_message=*/"HTTP error 404")) 141 /*status_message=*/"HTTP error 404"))
128 .Times(1); 142 .Times(1);
129 snippets_fetcher().FetchSnippets(/*hosts=*/std::set<std::string>(), 143 snippets_fetcher().FetchSnippets(/*hosts=*/std::set<std::string>(),
130 /*count=*/1); 144 /*count=*/1);
131 RunUntilIdle(); 145 RunUntilIdle();
146 EXPECT_THAT(histogram_tester().GetAllSamples(
147 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
148 ElementsAre(base::Bucket(/*min=*/404, /*count=*/1)));
132 } 149 }
133 150
134 // This test actually verifies that the test setup itself is sane, to prevent 151 // This test actually verifies that the test setup itself is sane, to prevent
135 // hard-to-reproduce test failures. 152 // hard-to-reproduce test failures.
136 TEST_F(NTPSnippetsFetcherTest, ShouldReportHttpErrorForMissingBakedResponse) { 153 TEST_F(NTPSnippetsFetcherTest, ShouldReportHttpErrorForMissingBakedResponse) {
137 EXPECT_CALL(mock_callback(), Run(/*snippets_json=*/std::string(), 154 EXPECT_CALL(mock_callback(), Run(/*snippets_json=*/std::string(),
138 /*status_message=*/Not(IsEmpty()))) 155 /*status_message=*/Not(IsEmpty())))
139 .Times(1); 156 .Times(1);
140 snippets_fetcher().FetchSnippets(/*hosts=*/std::set<std::string>(), 157 snippets_fetcher().FetchSnippets(/*hosts=*/std::set<std::string>(),
141 /*count=*/1); 158 /*count=*/1);
142 RunUntilIdle(); 159 RunUntilIdle();
143 } 160 }
144 161
145 TEST_F(NTPSnippetsFetcherTest, ShouldCancelOngoingFetch) { 162 TEST_F(NTPSnippetsFetcherTest, ShouldCancelOngoingFetch) {
146 const std::string json_str = "{ \"recos\": [] }"; 163 const std::string json_str = "{ \"recos\": [] }";
147 fake_url_fetcher_factory().SetFakeResponse(test_url(), 164 fake_url_fetcher_factory().SetFakeResponse(test_url(),
148 /*data=*/json_str, net::HTTP_OK, 165 /*data=*/json_str, net::HTTP_OK,
149 net::URLRequestStatus::SUCCESS); 166 net::URLRequestStatus::SUCCESS);
150 EXPECT_CALL(mock_callback(), Run(/*snippets_json=*/json_str, 167 EXPECT_CALL(mock_callback(), Run(/*snippets_json=*/json_str,
151 /*status_message=*/std::string())) 168 /*status_message=*/std::string()))
152 .Times(1); 169 .Times(1);
153 snippets_fetcher().FetchSnippets(/*hosts=*/std::set<std::string>(), 170 snippets_fetcher().FetchSnippets(/*hosts=*/std::set<std::string>(),
154 /*count=*/1); 171 /*count=*/1);
155 // Second call to FetchSnippets() overrides/cancels the previous. Callback is 172 // Second call to FetchSnippets() overrides/cancels the previous. Callback is
156 // expected to be called once. 173 // expected to be called once.
157 snippets_fetcher().FetchSnippets(/*hosts=*/std::set<std::string>(), 174 snippets_fetcher().FetchSnippets(/*hosts=*/std::set<std::string>(),
158 /*count=*/1); 175 /*count=*/1);
159 RunUntilIdle(); 176 RunUntilIdle();
177 EXPECT_THAT(histogram_tester().GetAllSamples(
178 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
179 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1)));
160 } 180 }
161 181
162 } // namespace 182 } // namespace
163 } // namespace ntp_snippets 183 } // namespace ntp_snippets
OLDNEW
« no previous file with comments | « components/ntp_snippets/ntp_snippets_fetcher.cc ('k') | tools/metrics/histograms/histograms.xml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698