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

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

Issue 1922083004: Allow fetching personalized snippets from ChromeReader. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Yet another unittest fix 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/json/json_reader.h" 7 #include "base/json/json_reader.h"
8 #include "base/memory/ptr_util.h" 8 #include "base/memory/ptr_util.h"
9 #include "base/strings/stringprintf.h" 9 #include "base/strings/stringprintf.h"
10 #include "base/test/histogram_tester.h" 10 #include "base/test/histogram_tester.h"
11 #include "base/test/test_mock_time_task_runner.h" 11 #include "base/test/test_mock_time_task_runner.h"
12 #include "base/thread_task_runner_handle.h" 12 #include "base/thread_task_runner_handle.h"
13 #include "base/time/time.h" 13 #include "base/time/time.h"
14 #include "base/values.h" 14 #include "base/values.h"
15 #include "components/ntp_snippets/ntp_snippet.h" 15 #include "components/ntp_snippets/ntp_snippet.h"
16 #include "components/signin/core/browser/account_tracker_service.h"
17 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h"
18 #include "components/signin/core/browser/fake_signin_manager.h"
19 #include "components/signin/core/browser/test_signin_client.h"
16 #include "google_apis/google_api_keys.h" 20 #include "google_apis/google_api_keys.h"
17 #include "net/url_request/test_url_fetcher_factory.h" 21 #include "net/url_request/test_url_fetcher_factory.h"
18 #include "net/url_request/url_request_test_util.h" 22 #include "net/url_request/url_request_test_util.h"
19 #include "testing/gmock/include/gmock/gmock.h" 23 #include "testing/gmock/include/gmock/gmock.h"
20 #include "testing/gtest/include/gtest/gtest.h" 24 #include "testing/gtest/include/gtest/gtest.h"
21 25
22 namespace ntp_snippets { 26 namespace ntp_snippets {
23 namespace { 27 namespace {
24 28
25 using testing::ElementsAre; 29 using testing::ElementsAre;
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
88 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( 92 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
89 FROM_HERE, base::Bind(&ParseJson, json, success_callback, error_callback), 93 FROM_HERE, base::Bind(&ParseJson, json, success_callback, error_callback),
90 base::TimeDelta::FromMilliseconds(kTestJsonParsingLatencyMs)); 94 base::TimeDelta::FromMilliseconds(kTestJsonParsingLatencyMs));
91 } 95 }
92 96
93 class NTPSnippetsFetcherTest : public testing::Test { 97 class NTPSnippetsFetcherTest : public testing::Test {
94 public: 98 public:
95 NTPSnippetsFetcherTest() 99 NTPSnippetsFetcherTest()
96 : mock_task_runner_(new base::TestMockTimeTaskRunner()), 100 : mock_task_runner_(new base::TestMockTimeTaskRunner()),
97 mock_task_runner_handle_(mock_task_runner_), 101 mock_task_runner_handle_(mock_task_runner_),
102 signin_client_(new TestSigninClient(nullptr)),
103 account_tracker_(new AccountTrackerService()),
104 fake_signin_manager_(new FakeSigninManagerBase(signin_client_.get(),
105 account_tracker_.get())),
106 fake_token_service_(new FakeProfileOAuth2TokenService()),
98 snippets_fetcher_( 107 snippets_fetcher_(
108 fake_signin_manager_.get(),
109 fake_token_service_.get(),
99 scoped_refptr<net::TestURLRequestContextGetter>( 110 scoped_refptr<net::TestURLRequestContextGetter>(
100 new net::TestURLRequestContextGetter(mock_task_runner_.get())), 111 new net::TestURLRequestContextGetter(mock_task_runner_.get())),
101 base::Bind(&ParseJsonDelayed), 112 base::Bind(&ParseJsonDelayed),
102 /*is_stable_channel=*/true), 113 /*is_stable_channel=*/true),
114 test_lang_("en-US"),
103 test_url_(base::StringPrintf(kTestContentSnippetsServerFormat, 115 test_url_(base::StringPrintf(kTestContentSnippetsServerFormat,
104 google_apis::GetAPIKey().c_str())) { 116 google_apis::GetAPIKey().c_str())) {
105 snippets_fetcher_.SetCallback( 117 snippets_fetcher_.SetCallback(
106 base::Bind(&MockSnippetsAvailableCallback::WrappedRun, 118 base::Bind(&MockSnippetsAvailableCallback::WrappedRun,
107 base::Unretained(&mock_callback_))); 119 base::Unretained(&mock_callback_)));
108 snippets_fetcher_.SetTickClockForTesting( 120 snippets_fetcher_.SetTickClockForTesting(
109 mock_task_runner_->GetMockTickClock()); 121 mock_task_runner_->GetMockTickClock());
110 test_hosts_.insert("www.somehost.com"); 122 test_hosts_.insert("www.somehost.com");
111 // Increase initial time such that ticks are non-zero. 123 // Increase initial time such that ticks are non-zero.
112 mock_task_runner_->FastForwardBy(base::TimeDelta::FromMilliseconds(1234)); 124 mock_task_runner_->FastForwardBy(base::TimeDelta::FromMilliseconds(1234));
113 } 125 }
114 126
115 NTPSnippetsFetcher& snippets_fetcher() { return snippets_fetcher_; } 127 NTPSnippetsFetcher& snippets_fetcher() { return snippets_fetcher_; }
116 MockSnippetsAvailableCallback& mock_callback() { return mock_callback_; } 128 MockSnippetsAvailableCallback& mock_callback() { return mock_callback_; }
117 void FastForwardUntilNoTasksRemain() { 129 void FastForwardUntilNoTasksRemain() {
118 mock_task_runner_->FastForwardUntilNoTasksRemain(); 130 mock_task_runner_->FastForwardUntilNoTasksRemain();
119 } 131 }
132 const std::string& test_lang() const { return test_lang_; }
120 const GURL& test_url() { return test_url_; } 133 const GURL& test_url() { return test_url_; }
121 const std::set<std::string>& test_hosts() const { return test_hosts_; } 134 const std::set<std::string>& test_hosts() const { return test_hosts_; }
122 base::HistogramTester& histogram_tester() { return histogram_tester_; } 135 base::HistogramTester& histogram_tester() { return histogram_tester_; }
123 136
124 void InitFakeURLFetcherFactory() { 137 void InitFakeURLFetcherFactory() {
125 if (fake_url_fetcher_factory_) 138 if (fake_url_fetcher_factory_)
126 return; 139 return;
127 // Instantiation of factory automatically sets itself as URLFetcher's 140 // Instantiation of factory automatically sets itself as URLFetcher's
128 // factory. 141 // factory.
129 fake_url_fetcher_factory_.reset(new net::FakeURLFetcherFactory( 142 fake_url_fetcher_factory_.reset(new net::FakeURLFetcherFactory(
130 /*default_factory=*/&failing_url_fetcher_factory_)); 143 /*default_factory=*/&failing_url_fetcher_factory_));
131 } 144 }
132 145
133 void SetFakeResponse(const std::string& response_data, 146 void SetFakeResponse(const std::string& response_data,
134 net::HttpStatusCode response_code, 147 net::HttpStatusCode response_code,
135 net::URLRequestStatus::Status status) { 148 net::URLRequestStatus::Status status) {
136 InitFakeURLFetcherFactory(); 149 InitFakeURLFetcherFactory();
137 fake_url_fetcher_factory_->SetFakeResponse(test_url_, response_data, 150 fake_url_fetcher_factory_->SetFakeResponse(test_url_, response_data,
138 response_code, status); 151 response_code, status);
139 } 152 }
140 153
141 private: 154 private:
142 scoped_refptr<base::TestMockTimeTaskRunner> mock_task_runner_; 155 scoped_refptr<base::TestMockTimeTaskRunner> mock_task_runner_;
143 base::ThreadTaskRunnerHandle mock_task_runner_handle_; 156 base::ThreadTaskRunnerHandle mock_task_runner_handle_;
144 FailingFakeURLFetcherFactory failing_url_fetcher_factory_; 157 FailingFakeURLFetcherFactory failing_url_fetcher_factory_;
145 // Initialized lazily in SetFakeResponse(). 158 // Initialized lazily in SetFakeResponse().
146 std::unique_ptr<net::FakeURLFetcherFactory> fake_url_fetcher_factory_; 159 std::unique_ptr<net::FakeURLFetcherFactory> fake_url_fetcher_factory_;
160 std::unique_ptr<TestSigninClient> signin_client_;
161 std::unique_ptr<AccountTrackerService> account_tracker_;
162 std::unique_ptr<SigninManagerBase> fake_signin_manager_;
163 std::unique_ptr<OAuth2TokenService> fake_token_service_;
147 NTPSnippetsFetcher snippets_fetcher_; 164 NTPSnippetsFetcher snippets_fetcher_;
148 MockSnippetsAvailableCallback mock_callback_; 165 MockSnippetsAvailableCallback mock_callback_;
166 const std::string test_lang_;
149 const GURL test_url_; 167 const GURL test_url_;
150 std::set<std::string> test_hosts_; 168 std::set<std::string> test_hosts_;
151 base::HistogramTester histogram_tester_; 169 base::HistogramTester histogram_tester_;
152 170
153 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsFetcherTest); 171 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsFetcherTest);
154 }; 172 };
155 173
156 TEST_F(NTPSnippetsFetcherTest, ShouldNotFetchOnCreation) { 174 TEST_F(NTPSnippetsFetcherTest, ShouldNotFetchOnCreation) {
157 // The lack of registered baked in responses would cause any fetch to fail. 175 // The lack of registered baked in responses would cause any fetch to fail.
158 FastForwardUntilNoTasksRemain(); 176 FastForwardUntilNoTasksRemain();
(...skipping 13 matching lines...) Expand all
172 " \"sourceCorpusInfo\" : [{" 190 " \"sourceCorpusInfo\" : [{"
173 " \"ampUrl\" : \"http://localhost/amp\"," 191 " \"ampUrl\" : \"http://localhost/amp\","
174 " \"corpusId\" : \"http://localhost/foobar\"," 192 " \"corpusId\" : \"http://localhost/foobar\","
175 " \"publisherData\": { \"sourceName\" : \"Foo News\" }" 193 " \"publisherData\": { \"sourceName\" : \"Foo News\" }"
176 " }]" 194 " }]"
177 " }" 195 " }"
178 "}]}"; 196 "}]}";
179 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK, 197 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK,
180 net::URLRequestStatus::SUCCESS); 198 net::URLRequestStatus::SUCCESS);
181 EXPECT_CALL(mock_callback(), Run(/*snippets=*/PointeeSizeIs(1))).Times(1); 199 EXPECT_CALL(mock_callback(), Run(/*snippets=*/PointeeSizeIs(1))).Times(1);
182 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), /*count=*/1); 200 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(),
201 /*count=*/1);
183 FastForwardUntilNoTasksRemain(); 202 FastForwardUntilNoTasksRemain();
184 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK")); 203 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK"));
185 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); 204 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr));
186 EXPECT_THAT(histogram_tester().GetAllSamples( 205 EXPECT_THAT(histogram_tester().GetAllSamples(
187 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), 206 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
188 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); 207 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1)));
189 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), 208 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"),
190 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, 209 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs,
191 /*count=*/1))); 210 /*count=*/1)));
192 } 211 }
193 212
194 TEST_F(NTPSnippetsFetcherTest, ShouldFetchSuccessfullyEmptyList) { 213 TEST_F(NTPSnippetsFetcherTest, ShouldFetchSuccessfullyEmptyList) {
195 const std::string kJsonStr = "{\"recos\": []}"; 214 const std::string kJsonStr = "{\"recos\": []}";
196 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK, 215 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK,
197 net::URLRequestStatus::SUCCESS); 216 net::URLRequestStatus::SUCCESS);
198 EXPECT_CALL(mock_callback(), Run(/*snippets=*/PointeeSizeIs(0))).Times(1); 217 EXPECT_CALL(mock_callback(), Run(/*snippets=*/PointeeSizeIs(0))).Times(1);
199 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), /*count=*/1); 218 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(),
219 /*count=*/1);
200 FastForwardUntilNoTasksRemain(); 220 FastForwardUntilNoTasksRemain();
201 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK")); 221 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK"));
202 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); 222 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr));
203 EXPECT_THAT( 223 EXPECT_THAT(
204 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), 224 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"),
205 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1))); 225 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1)));
206 EXPECT_THAT(histogram_tester().GetAllSamples( 226 EXPECT_THAT(histogram_tester().GetAllSamples(
207 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), 227 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
208 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); 228 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1)));
209 } 229 }
210 230
211 TEST_F(NTPSnippetsFetcherTest, ShouldReportEmptyHostsError) { 231 TEST_F(NTPSnippetsFetcherTest, ShouldReportEmptyHostsError) {
212 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); 232 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1);
213 snippets_fetcher().FetchSnippetsFromHosts(/*hosts=*/std::set<std::string>(), 233 snippets_fetcher().FetchSnippetsFromHosts(/*hosts=*/std::set<std::string>(),
234 /*language_code=*/"en-US",
214 /*count=*/1); 235 /*count=*/1);
215 FastForwardUntilNoTasksRemain(); 236 FastForwardUntilNoTasksRemain();
216 EXPECT_THAT(snippets_fetcher().last_status(), 237 EXPECT_THAT(snippets_fetcher().last_status(),
217 Eq("Cannot fetch for empty hosts list.")); 238 Eq("Cannot fetch for empty hosts list."));
218 EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty()); 239 EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty());
219 EXPECT_THAT( 240 EXPECT_THAT(
220 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), 241 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"),
221 ElementsAre(base::Bucket(/*min=*/1, /*count=*/1))); 242 ElementsAre(base::Bucket(/*min=*/1, /*count=*/1)));
222 EXPECT_THAT(histogram_tester().GetAllSamples( 243 EXPECT_THAT(histogram_tester().GetAllSamples(
223 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), 244 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
224 IsEmpty()); 245 IsEmpty());
225 // This particular error gets triggered prior to JSON parsing and hence tests 246 // This particular error gets triggered prior to JSON parsing and hence tests
226 // observe no fetch latency. 247 // observe no fetch latency.
227 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), 248 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"),
228 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1))); 249 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1)));
229 } 250 }
230 251
231 TEST_F(NTPSnippetsFetcherTest, ShouldRestrictToHosts) { 252 TEST_F(NTPSnippetsFetcherTest, ShouldRestrictToHosts) {
232 net::TestURLFetcherFactory test_url_fetcher_factory; 253 net::TestURLFetcherFactory test_url_fetcher_factory;
233 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), /*count=*/17); 254 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(),
255 /*count=*/17);
234 net::TestURLFetcher* fetcher = test_url_fetcher_factory.GetFetcherByID(0); 256 net::TestURLFetcher* fetcher = test_url_fetcher_factory.GetFetcherByID(0);
235 ASSERT_THAT(fetcher, NotNull()); 257 ASSERT_THAT(fetcher, NotNull());
236 std::unique_ptr<base::Value> value = 258 std::unique_ptr<base::Value> value =
237 base::JSONReader::Read(fetcher->upload_data()); 259 base::JSONReader::Read(fetcher->upload_data());
238 ASSERT_TRUE(value); 260 ASSERT_TRUE(value);
239 const base::DictionaryValue* dict; 261 const base::DictionaryValue* dict;
240 ASSERT_TRUE(value->GetAsDictionary(&dict)); 262 ASSERT_TRUE(value->GetAsDictionary(&dict));
241 const base::DictionaryValue* local_scoring_params; 263 const base::DictionaryValue* local_scoring_params;
242 ASSERT_TRUE(dict->GetDictionary("advanced_options.local_scoring_params", 264 ASSERT_TRUE(dict->GetDictionary("advanced_options.local_scoring_params",
243 &local_scoring_params)); 265 &local_scoring_params));
244 const base::DictionaryValue* content_selectors; 266 const base::DictionaryValue* content_selectors;
245 ASSERT_TRUE(local_scoring_params->GetDictionary("content_selectors", 267 ASSERT_TRUE(local_scoring_params->GetDictionary("content_selectors",
246 &content_selectors)); 268 &content_selectors));
247 std::string content_selector_value; 269 std::string content_selector_value;
248 EXPECT_TRUE(content_selectors->GetString("value", &content_selector_value)); 270 EXPECT_TRUE(content_selectors->GetString("value", &content_selector_value));
249 EXPECT_THAT(content_selector_value, Eq("www.somehost.com")); 271 EXPECT_THAT(content_selector_value, Eq("www.somehost.com"));
250 } 272 }
251 273
252 TEST_F(NTPSnippetsFetcherTest, ShouldReportUrlStatusError) { 274 TEST_F(NTPSnippetsFetcherTest, ShouldReportUrlStatusError) {
253 SetFakeResponse(/*data=*/std::string(), net::HTTP_NOT_FOUND, 275 SetFakeResponse(/*data=*/std::string(), net::HTTP_NOT_FOUND,
254 net::URLRequestStatus::FAILED); 276 net::URLRequestStatus::FAILED);
255 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); 277 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1);
256 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), /*count=*/1); 278 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(),
279 /*count=*/1);
257 FastForwardUntilNoTasksRemain(); 280 FastForwardUntilNoTasksRemain();
258 EXPECT_THAT(snippets_fetcher().last_status(), 281 EXPECT_THAT(snippets_fetcher().last_status(),
259 Eq("URLRequestStatus error -2")); 282 Eq("URLRequestStatus error -2"));
260 EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty()); 283 EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty());
261 EXPECT_THAT( 284 EXPECT_THAT(
262 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), 285 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"),
263 ElementsAre(base::Bucket(/*min=*/2, /*count=*/1))); 286 ElementsAre(base::Bucket(/*min=*/2, /*count=*/1)));
264 EXPECT_THAT(histogram_tester().GetAllSamples( 287 EXPECT_THAT(histogram_tester().GetAllSamples(
265 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), 288 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
266 ElementsAre(base::Bucket(/*min=*/-2, /*count=*/1))); 289 ElementsAre(base::Bucket(/*min=*/-2, /*count=*/1)));
267 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), 290 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"),
268 Not(IsEmpty())); 291 Not(IsEmpty()));
269 } 292 }
270 293
271 TEST_F(NTPSnippetsFetcherTest, ShouldReportHttpError) { 294 TEST_F(NTPSnippetsFetcherTest, ShouldReportHttpError) {
272 SetFakeResponse(/*data=*/std::string(), net::HTTP_NOT_FOUND, 295 SetFakeResponse(/*data=*/std::string(), net::HTTP_NOT_FOUND,
273 net::URLRequestStatus::SUCCESS); 296 net::URLRequestStatus::SUCCESS);
274 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); 297 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1);
275 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), /*count=*/1); 298 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(),
299 /*count=*/1);
276 FastForwardUntilNoTasksRemain(); 300 FastForwardUntilNoTasksRemain();
277 EXPECT_THAT(snippets_fetcher().last_status(), Eq("HTTP error 404"));
278 EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty()); 301 EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty());
279 EXPECT_THAT( 302 EXPECT_THAT(
280 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), 303 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"),
281 ElementsAre(base::Bucket(/*min=*/3, /*count=*/1))); 304 ElementsAre(base::Bucket(/*min=*/3, /*count=*/1)));
282 EXPECT_THAT(histogram_tester().GetAllSamples( 305 EXPECT_THAT(histogram_tester().GetAllSamples(
283 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), 306 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
284 ElementsAre(base::Bucket(/*min=*/404, /*count=*/1))); 307 ElementsAre(base::Bucket(/*min=*/404, /*count=*/1)));
285 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), 308 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"),
286 Not(IsEmpty())); 309 Not(IsEmpty()));
287 } 310 }
288 311
289 TEST_F(NTPSnippetsFetcherTest, ShouldReportJsonError) { 312 TEST_F(NTPSnippetsFetcherTest, ShouldReportJsonError) {
290 const std::string kInvalidJsonStr = "{ \"recos\": []"; 313 const std::string kInvalidJsonStr = "{ \"recos\": []";
291 SetFakeResponse(/*data=*/kInvalidJsonStr, net::HTTP_OK, 314 SetFakeResponse(/*data=*/kInvalidJsonStr, net::HTTP_OK,
292 net::URLRequestStatus::SUCCESS); 315 net::URLRequestStatus::SUCCESS);
293 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); 316 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1);
294 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), /*count=*/1); 317 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(),
318 /*count=*/1);
295 FastForwardUntilNoTasksRemain(); 319 FastForwardUntilNoTasksRemain();
296 EXPECT_THAT(snippets_fetcher().last_status(), 320 EXPECT_THAT(snippets_fetcher().last_status(),
297 StartsWith("Received invalid JSON (error ")); 321 StartsWith("Received invalid JSON (error "));
298 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kInvalidJsonStr)); 322 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kInvalidJsonStr));
299 EXPECT_THAT( 323 EXPECT_THAT(
300 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), 324 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"),
301 ElementsAre(base::Bucket(/*min=*/4, /*count=*/1))); 325 ElementsAre(base::Bucket(/*min=*/4, /*count=*/1)));
302 EXPECT_THAT(histogram_tester().GetAllSamples( 326 EXPECT_THAT(histogram_tester().GetAllSamples(
303 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), 327 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
304 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); 328 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1)));
305 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), 329 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"),
306 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, 330 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs,
307 /*count=*/1))); 331 /*count=*/1)));
308 } 332 }
309 333
310 TEST_F(NTPSnippetsFetcherTest, ShouldReportJsonErrorForEmptyResponse) { 334 TEST_F(NTPSnippetsFetcherTest, ShouldReportJsonErrorForEmptyResponse) {
311 SetFakeResponse(/*data=*/std::string(), net::HTTP_OK, 335 SetFakeResponse(/*data=*/std::string(), net::HTTP_OK,
312 net::URLRequestStatus::SUCCESS); 336 net::URLRequestStatus::SUCCESS);
313 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); 337 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1);
314 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), /*count=*/1); 338 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(),
339 /*count=*/1);
315 FastForwardUntilNoTasksRemain(); 340 FastForwardUntilNoTasksRemain();
316 EXPECT_THAT(snippets_fetcher().last_status(),
317 StartsWith("Received invalid JSON (error "));
318 EXPECT_THAT(snippets_fetcher().last_json(), std::string()); 341 EXPECT_THAT(snippets_fetcher().last_json(), std::string());
319 EXPECT_THAT( 342 EXPECT_THAT(
320 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), 343 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"),
321 ElementsAre(base::Bucket(/*min=*/4, /*count=*/1))); 344 ElementsAre(base::Bucket(/*min=*/4, /*count=*/1)));
322 EXPECT_THAT(histogram_tester().GetAllSamples( 345 EXPECT_THAT(histogram_tester().GetAllSamples(
323 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), 346 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
324 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); 347 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1)));
325 } 348 }
326 349
327 TEST_F(NTPSnippetsFetcherTest, ShouldReportInvalidListError) { 350 TEST_F(NTPSnippetsFetcherTest, ShouldReportInvalidListError) {
328 const std::string kJsonStr = 351 const std::string kJsonStr =
329 "{\"recos\": [{ \"contentInfo\": { \"foo\" : \"bar\" }}]}"; 352 "{\"recos\": [{ \"contentInfo\": { \"foo\" : \"bar\" }}]}";
330 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK, 353 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK,
331 net::URLRequestStatus::SUCCESS); 354 net::URLRequestStatus::SUCCESS);
332 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); 355 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1);
333 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), /*count=*/1); 356 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(),
357 /*count=*/1);
334 FastForwardUntilNoTasksRemain(); 358 FastForwardUntilNoTasksRemain();
335 EXPECT_THAT(snippets_fetcher().last_status(), Eq("Invalid / empty list."));
336 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); 359 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr));
337 EXPECT_THAT( 360 EXPECT_THAT(
338 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), 361 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"),
339 ElementsAre(base::Bucket(/*min=*/5, /*count=*/1))); 362 ElementsAre(base::Bucket(/*min=*/5, /*count=*/1)));
340 EXPECT_THAT(histogram_tester().GetAllSamples( 363 EXPECT_THAT(histogram_tester().GetAllSamples(
341 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), 364 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
342 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); 365 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1)));
343 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), 366 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"),
344 Not(IsEmpty())); 367 Not(IsEmpty()));
345 } 368 }
346 369
347 // This test actually verifies that the test setup itself is sane, to prevent 370 // This test actually verifies that the test setup itself is sane, to prevent
348 // hard-to-reproduce test failures. 371 // hard-to-reproduce test failures.
349 TEST_F(NTPSnippetsFetcherTest, ShouldReportHttpErrorForMissingBakedResponse) { 372 TEST_F(NTPSnippetsFetcherTest, ShouldReportHttpErrorForMissingBakedResponse) {
350 InitFakeURLFetcherFactory(); 373 InitFakeURLFetcherFactory();
351 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); 374 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1);
352 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), /*count=*/1); 375 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(),
376 /*count=*/1);
353 FastForwardUntilNoTasksRemain(); 377 FastForwardUntilNoTasksRemain();
354 } 378 }
355 379
356 TEST_F(NTPSnippetsFetcherTest, ShouldCancelOngoingFetch) { 380 TEST_F(NTPSnippetsFetcherTest, ShouldCancelOngoingFetch) {
357 const std::string kJsonStr = "{ \"recos\": [] }"; 381 const std::string kJsonStr = "{ \"recos\": [] }";
358 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK, 382 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK,
359 net::URLRequestStatus::SUCCESS); 383 net::URLRequestStatus::SUCCESS);
360 EXPECT_CALL(mock_callback(), Run(/*snippets=*/PointeeSizeIs(0))).Times(1); 384 EXPECT_CALL(mock_callback(), Run(/*snippets=*/PointeeSizeIs(0))).Times(1);
361 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), /*count=*/1); 385 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(),
386 /*count=*/1);
362 // Second call to FetchSnippetsFromHosts() overrides/cancels the previous. 387 // Second call to FetchSnippetsFromHosts() overrides/cancels the previous.
363 // Callback is expected to be called once. 388 // Callback is expected to be called once.
364 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), /*count=*/1); 389 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(),
390 /*count=*/1);
365 FastForwardUntilNoTasksRemain(); 391 FastForwardUntilNoTasksRemain();
366 EXPECT_THAT( 392 EXPECT_THAT(
367 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), 393 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"),
368 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1))); 394 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1)));
369 EXPECT_THAT(histogram_tester().GetAllSamples( 395 EXPECT_THAT(histogram_tester().GetAllSamples(
370 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), 396 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
371 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); 397 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1)));
372 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), 398 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"),
373 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, 399 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs,
374 /*count=*/1))); 400 /*count=*/1)));
375 } 401 }
376 402
377 } // namespace 403 } // namespace
378 } // namespace ntp_snippets 404 } // namespace ntp_snippets
OLDNEW
« no previous file with comments | « components/ntp_snippets/ntp_snippets_fetcher.cc ('k') | components/ntp_snippets/ntp_snippets_service.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698