| 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 "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/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| 11 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
| 12 #include "base/test/histogram_tester.h" | 12 #include "base/test/histogram_tester.h" |
| 13 #include "base/thread_task_runner_handle.h" | 13 #include "base/thread_task_runner_handle.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 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 66 std::unique_ptr<base::Value> value = json_reader.ReadToValue(json); | 70 std::unique_ptr<base::Value> value = json_reader.ReadToValue(json); |
| 67 if (value) | 71 if (value) |
| 68 success_callback.Run(std::move(value)); | 72 success_callback.Run(std::move(value)); |
| 69 else | 73 else |
| 70 error_callback.Run(json_reader.GetErrorMessage()); | 74 error_callback.Run(json_reader.GetErrorMessage()); |
| 71 } | 75 } |
| 72 | 76 |
| 73 class NTPSnippetsFetcherTest : public testing::Test { | 77 class NTPSnippetsFetcherTest : public testing::Test { |
| 74 public: | 78 public: |
| 75 NTPSnippetsFetcherTest() | 79 NTPSnippetsFetcherTest() |
| 76 : snippets_fetcher_(scoped_refptr<net::TestURLRequestContextGetter>( | 80 : signin_client_(new TestSigninClient(nullptr)), |
| 81 account_tracker_(new AccountTrackerService()), |
| 82 snippets_fetcher_(new FakeSigninManagerBase(signin_client_.get(), |
| 83 account_tracker_.get()), |
| 84 new FakeProfileOAuth2TokenService(), |
| 85 scoped_refptr<net::TestURLRequestContextGetter>( |
| 77 new net::TestURLRequestContextGetter( | 86 new net::TestURLRequestContextGetter( |
| 78 base::ThreadTaskRunnerHandle::Get())), | 87 base::ThreadTaskRunnerHandle::Get())), |
| 79 base::Bind(&ParseJson), | 88 base::Bind(&ParseJson), |
| 80 /*is_stable_channel=*/true), | 89 /*is_stable_channel=*/true), |
| 81 test_url_(base::StringPrintf(kTestContentSnippetsServerFormat, | 90 test_url_(base::StringPrintf(kTestContentSnippetsServerFormat, |
| 82 google_apis::GetAPIKey().c_str())) { | 91 google_apis::GetAPIKey().c_str())) { |
| 83 snippets_fetcher_.SetCallback( | 92 snippets_fetcher_.SetCallback( |
| 84 base::Bind(&MockSnippetsAvailableCallback::WrappedRun, | 93 base::Bind(&MockSnippetsAvailableCallback::WrappedRun, |
| 85 base::Unretained(&mock_callback_))); | 94 base::Unretained(&mock_callback_))); |
| 86 test_hosts_.insert("www.somehost.com"); | 95 test_hosts_.insert("www.somehost.com"); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 107 net::URLRequestStatus::Status status) { | 116 net::URLRequestStatus::Status status) { |
| 108 InitFakeURLFetcherFactory(); | 117 InitFakeURLFetcherFactory(); |
| 109 fake_url_fetcher_factory_->SetFakeResponse(test_url_, response_data, | 118 fake_url_fetcher_factory_->SetFakeResponse(test_url_, response_data, |
| 110 response_code, status); | 119 response_code, status); |
| 111 } | 120 } |
| 112 | 121 |
| 113 private: | 122 private: |
| 114 FailingFakeURLFetcherFactory failing_url_fetcher_factory_; | 123 FailingFakeURLFetcherFactory failing_url_fetcher_factory_; |
| 115 // Initialized lazily in SetFakeResponse(). | 124 // Initialized lazily in SetFakeResponse(). |
| 116 std::unique_ptr<net::FakeURLFetcherFactory> fake_url_fetcher_factory_; | 125 std::unique_ptr<net::FakeURLFetcherFactory> fake_url_fetcher_factory_; |
| 126 std::unique_ptr<TestSigninClient> signin_client_; |
| 127 std::unique_ptr<AccountTrackerService> account_tracker_; |
| 117 // Needed to use ThreadTaskRunnerHandle. | 128 // Needed to use ThreadTaskRunnerHandle. |
| 118 base::MessageLoop message_loop_; | 129 base::MessageLoop message_loop_; |
| 119 NTPSnippetsFetcher snippets_fetcher_; | 130 NTPSnippetsFetcher snippets_fetcher_; |
| 120 MockSnippetsAvailableCallback mock_callback_; | 131 MockSnippetsAvailableCallback mock_callback_; |
| 121 const GURL test_url_; | 132 const GURL test_url_; |
| 122 std::set<std::string> test_hosts_; | 133 std::set<std::string> test_hosts_; |
| 123 base::HistogramTester histogram_tester_; | 134 base::HistogramTester histogram_tester_; |
| 124 | 135 |
| 125 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsFetcherTest); | 136 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsFetcherTest); |
| 126 }; | 137 }; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 143 " \"corpusId\" : \"http://localhost/foobar\"," | 154 " \"corpusId\" : \"http://localhost/foobar\"," |
| 144 " \"publisherData\": { \"sourceName\" : \"Foo News\" }" | 155 " \"publisherData\": { \"sourceName\" : \"Foo News\" }" |
| 145 " }]" | 156 " }]" |
| 146 " }" | 157 " }" |
| 147 "}]}"; | 158 "}]}"; |
| 148 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK, | 159 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK, |
| 149 net::URLRequestStatus::SUCCESS); | 160 net::URLRequestStatus::SUCCESS); |
| 150 EXPECT_CALL(mock_callback(), Run(/*snippets=*/SizeIs(1), | 161 EXPECT_CALL(mock_callback(), Run(/*snippets=*/SizeIs(1), |
| 151 /*status_message=*/std::string())) | 162 /*status_message=*/std::string())) |
| 152 .Times(1); | 163 .Times(1); |
| 153 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), /*count=*/1); | 164 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), |
| 165 /*language_code=*/"en-US", |
| 166 /*count=*/1); |
| 154 RunUntilIdle(); | 167 RunUntilIdle(); |
| 155 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); | 168 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); |
| 156 EXPECT_THAT(histogram_tester().GetAllSamples( | 169 EXPECT_THAT(histogram_tester().GetAllSamples( |
| 157 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), | 170 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), |
| 158 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); | 171 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); |
| 159 } | 172 } |
| 160 | 173 |
| 161 TEST_F(NTPSnippetsFetcherTest, ShouldFetchSuccessfullyEmptyList) { | 174 TEST_F(NTPSnippetsFetcherTest, ShouldFetchSuccessfullyEmptyList) { |
| 162 const std::string kJsonStr = "{\"recos\": []}"; | 175 const std::string kJsonStr = "{\"recos\": []}"; |
| 163 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK, | 176 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK, |
| 164 net::URLRequestStatus::SUCCESS); | 177 net::URLRequestStatus::SUCCESS); |
| 165 EXPECT_CALL(mock_callback(), Run(/*snippets=*/IsEmpty(), | 178 EXPECT_CALL(mock_callback(), Run(/*snippets=*/IsEmpty(), |
| 166 /*status_message=*/std::string())) | 179 /*status_message=*/std::string())) |
| 167 .Times(1); | 180 .Times(1); |
| 168 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), /*count=*/1); | 181 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), |
| 182 /*language_code=*/"en-US", |
| 183 /*count=*/1); |
| 169 RunUntilIdle(); | 184 RunUntilIdle(); |
| 170 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); | 185 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); |
| 171 EXPECT_THAT(histogram_tester().GetAllSamples( | 186 EXPECT_THAT(histogram_tester().GetAllSamples( |
| 172 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), | 187 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), |
| 173 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); | 188 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); |
| 174 } | 189 } |
| 175 | 190 |
| 176 TEST_F(NTPSnippetsFetcherTest, ShouldReportEmptyHostsError) { | 191 TEST_F(NTPSnippetsFetcherTest, ShouldReportEmptyHostsError) { |
| 177 EXPECT_CALL(mock_callback(), | 192 EXPECT_CALL(mock_callback(), |
| 178 Run(/*snippets=*/IsEmpty(), | 193 Run(/*snippets=*/IsEmpty(), |
| 179 /*status_message=*/"Cannot fetch for empty hosts list.")) | 194 /*status_message=*/"Cannot fetch for empty hosts list.")) |
| 180 .Times(1); | 195 .Times(1); |
| 181 snippets_fetcher().FetchSnippetsFromHosts(/*hosts=*/std::set<std::string>(), | 196 snippets_fetcher().FetchSnippetsFromHosts(/*hosts=*/std::set<std::string>(), |
| 197 /*language_code=*/"en-US", |
| 182 /*count=*/1); | 198 /*count=*/1); |
| 183 RunUntilIdle(); | 199 RunUntilIdle(); |
| 184 EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty()); | 200 EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty()); |
| 185 EXPECT_THAT(histogram_tester().GetAllSamples( | 201 EXPECT_THAT(histogram_tester().GetAllSamples( |
| 186 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), | 202 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), |
| 187 IsEmpty()); | 203 IsEmpty()); |
| 188 } | 204 } |
| 189 | 205 |
| 190 TEST_F(NTPSnippetsFetcherTest, ShouldRestrictToHosts) { | 206 TEST_F(NTPSnippetsFetcherTest, ShouldRestrictToHosts) { |
| 191 net::TestURLFetcherFactory test_url_fetcher_factory; | 207 net::TestURLFetcherFactory test_url_fetcher_factory; |
| 192 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), /*count=*/17); | 208 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), |
| 209 /*language_code=*/"en-US", |
| 210 /*count=*/17); |
| 193 net::TestURLFetcher* fetcher = test_url_fetcher_factory.GetFetcherByID(0); | 211 net::TestURLFetcher* fetcher = test_url_fetcher_factory.GetFetcherByID(0); |
| 194 ASSERT_THAT(fetcher, NotNull()); | 212 ASSERT_THAT(fetcher, NotNull()); |
| 195 std::unique_ptr<base::Value> value = | 213 std::unique_ptr<base::Value> value = |
| 196 base::JSONReader::Read(fetcher->upload_data()); | 214 base::JSONReader::Read(fetcher->upload_data()); |
| 197 ASSERT_TRUE(value); | 215 ASSERT_TRUE(value); |
| 198 const base::DictionaryValue* dict; | 216 const base::DictionaryValue* dict; |
| 199 ASSERT_TRUE(value->GetAsDictionary(&dict)); | 217 ASSERT_TRUE(value->GetAsDictionary(&dict)); |
| 200 const base::DictionaryValue* local_scoring_params; | 218 const base::DictionaryValue* local_scoring_params; |
| 201 ASSERT_TRUE(dict->GetDictionary("advanced_options.local_scoring_params", | 219 ASSERT_TRUE(dict->GetDictionary("advanced_options.local_scoring_params", |
| 202 &local_scoring_params)); | 220 &local_scoring_params)); |
| 203 const base::DictionaryValue* content_selectors; | 221 const base::DictionaryValue* content_selectors; |
| 204 ASSERT_TRUE(local_scoring_params->GetDictionary("content_selectors", | 222 ASSERT_TRUE(local_scoring_params->GetDictionary("content_selectors", |
| 205 &content_selectors)); | 223 &content_selectors)); |
| 206 std::string content_selector_value; | 224 std::string content_selector_value; |
| 207 EXPECT_TRUE(content_selectors->GetString("value", &content_selector_value)); | 225 EXPECT_TRUE(content_selectors->GetString("value", &content_selector_value)); |
| 208 EXPECT_THAT(content_selector_value, Eq("www.somehost.com")); | 226 EXPECT_THAT(content_selector_value, Eq("www.somehost.com")); |
| 209 } | 227 } |
| 210 | 228 |
| 211 TEST_F(NTPSnippetsFetcherTest, ShouldReportUrlStatusError) { | 229 TEST_F(NTPSnippetsFetcherTest, ShouldReportUrlStatusError) { |
| 212 SetFakeResponse(/*data=*/std::string(), net::HTTP_NOT_FOUND, | 230 SetFakeResponse(/*data=*/std::string(), net::HTTP_NOT_FOUND, |
| 213 net::URLRequestStatus::FAILED); | 231 net::URLRequestStatus::FAILED); |
| 214 EXPECT_CALL(mock_callback(), | 232 EXPECT_CALL(mock_callback(), |
| 215 Run(/*snippets=*/IsEmpty(), | 233 Run(/*snippets=*/IsEmpty(), |
| 216 /*status_message=*/"URLRequestStatus error -2")) | 234 /*status_message=*/"URLRequestStatus error -2")) |
| 217 .Times(1); | 235 .Times(1); |
| 218 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), /*count=*/1); | 236 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), |
| 237 /*language_code=*/"en-US", |
| 238 /*count=*/1); |
| 219 RunUntilIdle(); | 239 RunUntilIdle(); |
| 220 EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty()); | 240 EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty()); |
| 221 EXPECT_THAT(histogram_tester().GetAllSamples( | 241 EXPECT_THAT(histogram_tester().GetAllSamples( |
| 222 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), | 242 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), |
| 223 ElementsAre(base::Bucket(/*min=*/-2, /*count=*/1))); | 243 ElementsAre(base::Bucket(/*min=*/-2, /*count=*/1))); |
| 224 } | 244 } |
| 225 | 245 |
| 226 TEST_F(NTPSnippetsFetcherTest, ShouldReportHttpError) { | 246 TEST_F(NTPSnippetsFetcherTest, ShouldReportHttpError) { |
| 227 SetFakeResponse(/*data=*/std::string(), net::HTTP_NOT_FOUND, | 247 SetFakeResponse(/*data=*/std::string(), net::HTTP_NOT_FOUND, |
| 228 net::URLRequestStatus::SUCCESS); | 248 net::URLRequestStatus::SUCCESS); |
| 229 EXPECT_CALL(mock_callback(), Run(/*snippets=*/IsEmpty(), | 249 EXPECT_CALL(mock_callback(), Run(/*snippets=*/IsEmpty(), |
| 230 /*status_message=*/"HTTP error 404")) | 250 /*status_message=*/"HTTP error 404")) |
| 231 .Times(1); | 251 .Times(1); |
| 232 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), /*count=*/1); | 252 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), |
| 253 /*language_code=*/"en-US", |
| 254 /*count=*/1); |
| 233 RunUntilIdle(); | 255 RunUntilIdle(); |
| 234 EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty()); | 256 EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty()); |
| 235 EXPECT_THAT(histogram_tester().GetAllSamples( | 257 EXPECT_THAT(histogram_tester().GetAllSamples( |
| 236 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), | 258 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), |
| 237 ElementsAre(base::Bucket(/*min=*/404, /*count=*/1))); | 259 ElementsAre(base::Bucket(/*min=*/404, /*count=*/1))); |
| 238 } | 260 } |
| 239 | 261 |
| 240 TEST_F(NTPSnippetsFetcherTest, ShouldReportJsonError) { | 262 TEST_F(NTPSnippetsFetcherTest, ShouldReportJsonError) { |
| 241 const std::string kInvalidJsonStr = "{ \"recos\": []"; | 263 const std::string kInvalidJsonStr = "{ \"recos\": []"; |
| 242 SetFakeResponse(/*data=*/kInvalidJsonStr, net::HTTP_OK, | 264 SetFakeResponse(/*data=*/kInvalidJsonStr, net::HTTP_OK, |
| 243 net::URLRequestStatus::SUCCESS); | 265 net::URLRequestStatus::SUCCESS); |
| 244 EXPECT_CALL( | 266 EXPECT_CALL( |
| 245 mock_callback(), | 267 mock_callback(), |
| 246 Run(/*snippets=*/IsEmpty(), | 268 Run(/*snippets=*/IsEmpty(), |
| 247 /*status_message=*/StartsWith("Received invalid JSON (error "))) | 269 /*status_message=*/StartsWith("Received invalid JSON (error "))) |
| 248 .Times(1); | 270 .Times(1); |
| 249 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), /*count=*/1); | 271 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), |
| 272 /*language_code=*/"en-US", |
| 273 /*count=*/1); |
| 250 RunUntilIdle(); | 274 RunUntilIdle(); |
| 251 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kInvalidJsonStr)); | 275 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kInvalidJsonStr)); |
| 252 EXPECT_THAT(histogram_tester().GetAllSamples( | 276 EXPECT_THAT(histogram_tester().GetAllSamples( |
| 253 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), | 277 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), |
| 254 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); | 278 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); |
| 255 } | 279 } |
| 256 | 280 |
| 257 TEST_F(NTPSnippetsFetcherTest, ShouldReportJsonErrorForEmptyResponse) { | 281 TEST_F(NTPSnippetsFetcherTest, ShouldReportJsonErrorForEmptyResponse) { |
| 258 SetFakeResponse(/*data=*/std::string(), net::HTTP_OK, | 282 SetFakeResponse(/*data=*/std::string(), net::HTTP_OK, |
| 259 net::URLRequestStatus::SUCCESS); | 283 net::URLRequestStatus::SUCCESS); |
| 260 EXPECT_CALL( | 284 EXPECT_CALL( |
| 261 mock_callback(), | 285 mock_callback(), |
| 262 Run(/*snippets=*/IsEmpty(), | 286 Run(/*snippets=*/IsEmpty(), |
| 263 /*status_message=*/StartsWith("Received invalid JSON (error "))) | 287 /*status_message=*/StartsWith("Received invalid JSON (error "))) |
| 264 .Times(1); | 288 .Times(1); |
| 265 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), /*count=*/1); | 289 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), |
| 290 /*language_code=*/"en-US", |
| 291 /*count=*/1); |
| 266 RunUntilIdle(); | 292 RunUntilIdle(); |
| 267 EXPECT_THAT(snippets_fetcher().last_json(), std::string()); | 293 EXPECT_THAT(snippets_fetcher().last_json(), std::string()); |
| 268 EXPECT_THAT(histogram_tester().GetAllSamples( | 294 EXPECT_THAT(histogram_tester().GetAllSamples( |
| 269 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), | 295 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), |
| 270 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); | 296 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); |
| 271 } | 297 } |
| 272 | 298 |
| 273 TEST_F(NTPSnippetsFetcherTest, ShouldReportInvalidListError) { | 299 TEST_F(NTPSnippetsFetcherTest, ShouldReportInvalidListError) { |
| 274 const std::string kJsonStr = | 300 const std::string kJsonStr = |
| 275 "{\"recos\": [{ \"contentInfo\": { \"foo\" : \"bar\" }}]}"; | 301 "{\"recos\": [{ \"contentInfo\": { \"foo\" : \"bar\" }}]}"; |
| 276 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK, | 302 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK, |
| 277 net::URLRequestStatus::SUCCESS); | 303 net::URLRequestStatus::SUCCESS); |
| 278 EXPECT_CALL(mock_callback(), Run(/*snippets=*/IsEmpty(), | 304 EXPECT_CALL(mock_callback(), Run(/*snippets=*/IsEmpty(), |
| 279 /*status_message=*/"Invalid / empty list.")) | 305 /*status_message=*/"Invalid / empty list.")) |
| 280 .Times(1); | 306 .Times(1); |
| 281 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), /*count=*/1); | 307 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), |
| 308 /*language_code=*/"en-US", |
| 309 /*count=*/1); |
| 282 RunUntilIdle(); | 310 RunUntilIdle(); |
| 283 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); | 311 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); |
| 284 EXPECT_THAT(histogram_tester().GetAllSamples( | 312 EXPECT_THAT(histogram_tester().GetAllSamples( |
| 285 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), | 313 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), |
| 286 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); | 314 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); |
| 287 } | 315 } |
| 288 | 316 |
| 289 // This test actually verifies that the test setup itself is sane, to prevent | 317 // This test actually verifies that the test setup itself is sane, to prevent |
| 290 // hard-to-reproduce test failures. | 318 // hard-to-reproduce test failures. |
| 291 TEST_F(NTPSnippetsFetcherTest, ShouldReportHttpErrorForMissingBakedResponse) { | 319 TEST_F(NTPSnippetsFetcherTest, ShouldReportHttpErrorForMissingBakedResponse) { |
| 292 InitFakeURLFetcherFactory(); | 320 InitFakeURLFetcherFactory(); |
| 293 EXPECT_CALL(mock_callback(), Run(/*snippets=*/IsEmpty(), | 321 EXPECT_CALL(mock_callback(), Run(/*snippets=*/IsEmpty(), |
| 294 /*status_message=*/Not(IsEmpty()))) | 322 /*status_message=*/Not(IsEmpty()))) |
| 295 .Times(1); | 323 .Times(1); |
| 296 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), /*count=*/1); | 324 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), |
| 325 /*language_code=*/"en-US", |
| 326 /*count=*/1); |
| 297 RunUntilIdle(); | 327 RunUntilIdle(); |
| 298 } | 328 } |
| 299 | 329 |
| 300 TEST_F(NTPSnippetsFetcherTest, ShouldCancelOngoingFetch) { | 330 TEST_F(NTPSnippetsFetcherTest, ShouldCancelOngoingFetch) { |
| 301 const std::string kJsonStr = "{ \"recos\": [] }"; | 331 const std::string kJsonStr = "{ \"recos\": [] }"; |
| 302 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK, | 332 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK, |
| 303 net::URLRequestStatus::SUCCESS); | 333 net::URLRequestStatus::SUCCESS); |
| 304 EXPECT_CALL(mock_callback(), Run(/*snippets=*/IsEmpty(), | 334 EXPECT_CALL(mock_callback(), Run(/*snippets=*/IsEmpty(), |
| 305 /*status_message=*/std::string())) | 335 /*status_message=*/std::string())) |
| 306 .Times(1); | 336 .Times(1); |
| 307 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), /*count=*/1); | 337 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), |
| 338 /*language_code=*/"en-US", |
| 339 /*count=*/1); |
| 308 // Second call to FetchSnippetsFromHosts() overrides/cancels the previous. | 340 // Second call to FetchSnippetsFromHosts() overrides/cancels the previous. |
| 309 // Callback is expected to be called once. | 341 // Callback is expected to be called once. |
| 310 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), /*count=*/1); | 342 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), |
| 343 /*language_code=*/"en-US", |
| 344 /*count=*/1); |
| 311 RunUntilIdle(); | 345 RunUntilIdle(); |
| 312 EXPECT_THAT(histogram_tester().GetAllSamples( | 346 EXPECT_THAT(histogram_tester().GetAllSamples( |
| 313 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), | 347 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), |
| 314 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); | 348 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); |
| 315 } | 349 } |
| 316 | 350 |
| 317 } // namespace | 351 } // namespace |
| 318 } // namespace ntp_snippets | 352 } // namespace ntp_snippets |
| OLD | NEW |