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

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: Minor fixes 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/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
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),
80 /*is_stable_channel=*/true), 88 /*is_stable_channel=*/true),
81 test_url_(base::StringPrintf(kTestContentSnippetsServerFormat, 89 test_url_(base::StringPrintf(kTestContentSnippetsServerFormat,
82 google_apis::GetAPIKey().c_str())) { 90 google_apis::GetAPIKey().c_str())) {
83 snippets_fetcher_.SetCallback( 91 snippets_fetcher_.SetCallback(
84 base::Bind(&MockSnippetsAvailableCallback::WrappedRun, 92 base::Bind(&MockSnippetsAvailableCallback::WrappedRun,
85 base::Unretained(&mock_callback_))); 93 base::Unretained(&mock_callback_)));
86 test_hosts_.insert("www.somehost.com"); 94 test_hosts_.insert("www.somehost.com");
87 } 95 }
88 96
89 NTPSnippetsFetcher& snippets_fetcher() { return snippets_fetcher_; } 97 NTPSnippetsFetcher& snippets_fetcher() { return snippets_fetcher_; }
(...skipping 17 matching lines...) Expand all
107 net::URLRequestStatus::Status status) { 115 net::URLRequestStatus::Status status) {
108 InitFakeURLFetcherFactory(); 116 InitFakeURLFetcherFactory();
109 fake_url_fetcher_factory_->SetFakeResponse(test_url_, response_data, 117 fake_url_fetcher_factory_->SetFakeResponse(test_url_, response_data,
110 response_code, status); 118 response_code, status);
111 } 119 }
112 120
113 private: 121 private:
114 FailingFakeURLFetcherFactory failing_url_fetcher_factory_; 122 FailingFakeURLFetcherFactory failing_url_fetcher_factory_;
115 // Initialized lazily in SetFakeResponse(). 123 // Initialized lazily in SetFakeResponse().
116 std::unique_ptr<net::FakeURLFetcherFactory> fake_url_fetcher_factory_; 124 std::unique_ptr<net::FakeURLFetcherFactory> fake_url_fetcher_factory_;
125 std::unique_ptr<TestSigninClient> signin_client_;
126 std::unique_ptr<AccountTrackerService> account_tracker_;
117 // Needed to use ThreadTaskRunnerHandle. 127 // Needed to use ThreadTaskRunnerHandle.
118 base::MessageLoop message_loop_; 128 base::MessageLoop message_loop_;
119 NTPSnippetsFetcher snippets_fetcher_; 129 NTPSnippetsFetcher snippets_fetcher_;
120 MockSnippetsAvailableCallback mock_callback_; 130 MockSnippetsAvailableCallback mock_callback_;
121 const GURL test_url_; 131 const GURL test_url_;
122 std::set<std::string> test_hosts_; 132 std::set<std::string> test_hosts_;
123 base::HistogramTester histogram_tester_; 133 base::HistogramTester histogram_tester_;
124 134
125 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsFetcherTest); 135 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsFetcherTest);
126 }; 136 };
(...skipping 16 matching lines...) Expand all
143 " \"corpusId\" : \"http://localhost/foobar\"," 153 " \"corpusId\" : \"http://localhost/foobar\","
144 " \"publisherData\": { \"sourceName\" : \"Foo News\" }" 154 " \"publisherData\": { \"sourceName\" : \"Foo News\" }"
145 " }]" 155 " }]"
146 " }" 156 " }"
147 "}]}"; 157 "}]}";
148 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK, 158 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK,
149 net::URLRequestStatus::SUCCESS); 159 net::URLRequestStatus::SUCCESS);
150 EXPECT_CALL(mock_callback(), Run(/*snippets=*/SizeIs(1), 160 EXPECT_CALL(mock_callback(), Run(/*snippets=*/SizeIs(1),
151 /*status_message=*/std::string())) 161 /*status_message=*/std::string()))
152 .Times(1); 162 .Times(1);
153 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), /*count=*/1); 163 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(),
164 /*language_code=*/"en-US",
165 /*count=*/1);
154 RunUntilIdle(); 166 RunUntilIdle();
155 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); 167 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr));
156 EXPECT_THAT(histogram_tester().GetAllSamples( 168 EXPECT_THAT(histogram_tester().GetAllSamples(
157 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), 169 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
158 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); 170 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1)));
159 } 171 }
160 172
161 TEST_F(NTPSnippetsFetcherTest, ShouldFetchSuccessfullyEmptyList) { 173 TEST_F(NTPSnippetsFetcherTest, ShouldFetchSuccessfullyEmptyList) {
162 const std::string kJsonStr = "{\"recos\": []}"; 174 const std::string kJsonStr = "{\"recos\": []}";
163 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK, 175 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK,
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
208 EXPECT_THAT(content_selector_value, Eq("www.somehost.com")); 220 EXPECT_THAT(content_selector_value, Eq("www.somehost.com"));
209 } 221 }
210 222
211 TEST_F(NTPSnippetsFetcherTest, ShouldReportUrlStatusError) { 223 TEST_F(NTPSnippetsFetcherTest, ShouldReportUrlStatusError) {
212 SetFakeResponse(/*data=*/std::string(), net::HTTP_NOT_FOUND, 224 SetFakeResponse(/*data=*/std::string(), net::HTTP_NOT_FOUND,
213 net::URLRequestStatus::FAILED); 225 net::URLRequestStatus::FAILED);
214 EXPECT_CALL(mock_callback(), 226 EXPECT_CALL(mock_callback(),
215 Run(/*snippets=*/IsEmpty(), 227 Run(/*snippets=*/IsEmpty(),
216 /*status_message=*/"URLRequestStatus error -2")) 228 /*status_message=*/"URLRequestStatus error -2"))
217 .Times(1); 229 .Times(1);
218 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), /*count=*/1); 230 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(),
231 /*language_code=*/"en-US",
232 /*count=*/1);
219 RunUntilIdle(); 233 RunUntilIdle();
220 EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty()); 234 EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty());
221 EXPECT_THAT(histogram_tester().GetAllSamples( 235 EXPECT_THAT(histogram_tester().GetAllSamples(
222 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), 236 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
223 ElementsAre(base::Bucket(/*min=*/-2, /*count=*/1))); 237 ElementsAre(base::Bucket(/*min=*/-2, /*count=*/1)));
224 } 238 }
225 239
226 TEST_F(NTPSnippetsFetcherTest, ShouldReportHttpError) { 240 TEST_F(NTPSnippetsFetcherTest, ShouldReportHttpError) {
227 SetFakeResponse(/*data=*/std::string(), net::HTTP_NOT_FOUND, 241 SetFakeResponse(/*data=*/std::string(), net::HTTP_NOT_FOUND,
228 net::URLRequestStatus::SUCCESS); 242 net::URLRequestStatus::SUCCESS);
229 EXPECT_CALL(mock_callback(), Run(/*snippets=*/IsEmpty(), 243 EXPECT_CALL(mock_callback(), Run(/*snippets=*/IsEmpty(),
230 /*status_message=*/"HTTP error 404")) 244 /*status_message=*/"HTTP error 404"))
231 .Times(1); 245 .Times(1);
232 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), /*count=*/1); 246 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(),
247 /*language_code=*/"en-US",
248 /*count=*/1);
233 RunUntilIdle(); 249 RunUntilIdle();
234 EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty()); 250 EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty());
235 EXPECT_THAT(histogram_tester().GetAllSamples( 251 EXPECT_THAT(histogram_tester().GetAllSamples(
236 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), 252 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
237 ElementsAre(base::Bucket(/*min=*/404, /*count=*/1))); 253 ElementsAre(base::Bucket(/*min=*/404, /*count=*/1)));
238 } 254 }
239 255
240 TEST_F(NTPSnippetsFetcherTest, ShouldReportJsonError) { 256 TEST_F(NTPSnippetsFetcherTest, ShouldReportJsonError) {
241 const std::string kInvalidJsonStr = "{ \"recos\": []"; 257 const std::string kInvalidJsonStr = "{ \"recos\": []";
242 SetFakeResponse(/*data=*/kInvalidJsonStr, net::HTTP_OK, 258 SetFakeResponse(/*data=*/kInvalidJsonStr, net::HTTP_OK,
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
286 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); 302 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1)));
287 } 303 }
288 304
289 // This test actually verifies that the test setup itself is sane, to prevent 305 // This test actually verifies that the test setup itself is sane, to prevent
290 // hard-to-reproduce test failures. 306 // hard-to-reproduce test failures.
291 TEST_F(NTPSnippetsFetcherTest, ShouldReportHttpErrorForMissingBakedResponse) { 307 TEST_F(NTPSnippetsFetcherTest, ShouldReportHttpErrorForMissingBakedResponse) {
292 InitFakeURLFetcherFactory(); 308 InitFakeURLFetcherFactory();
293 EXPECT_CALL(mock_callback(), Run(/*snippets=*/IsEmpty(), 309 EXPECT_CALL(mock_callback(), Run(/*snippets=*/IsEmpty(),
294 /*status_message=*/Not(IsEmpty()))) 310 /*status_message=*/Not(IsEmpty())))
295 .Times(1); 311 .Times(1);
296 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), /*count=*/1); 312 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(),
313 /*language_code=*/"en-US",
314 /*count=*/1);
297 RunUntilIdle(); 315 RunUntilIdle();
298 } 316 }
299 317
300 TEST_F(NTPSnippetsFetcherTest, ShouldCancelOngoingFetch) { 318 TEST_F(NTPSnippetsFetcherTest, ShouldCancelOngoingFetch) {
301 const std::string kJsonStr = "{ \"recos\": [] }"; 319 const std::string kJsonStr = "{ \"recos\": [] }";
302 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK, 320 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK,
303 net::URLRequestStatus::SUCCESS); 321 net::URLRequestStatus::SUCCESS);
304 EXPECT_CALL(mock_callback(), Run(/*snippets=*/IsEmpty(), 322 EXPECT_CALL(mock_callback(), Run(/*snippets=*/IsEmpty(),
305 /*status_message=*/std::string())) 323 /*status_message=*/std::string()))
306 .Times(1); 324 .Times(1);
307 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), /*count=*/1); 325 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(),
326 /*language_code=*/"en-US",
327 /*count=*/1);
308 // Second call to FetchSnippetsFromHosts() overrides/cancels the previous. 328 // Second call to FetchSnippetsFromHosts() overrides/cancels the previous.
309 // Callback is expected to be called once. 329 // Callback is expected to be called once.
310 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), /*count=*/1); 330 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(),
331 /*language_code=*/"en-US",
332 /*count=*/1);
311 RunUntilIdle(); 333 RunUntilIdle();
312 EXPECT_THAT(histogram_tester().GetAllSamples( 334 EXPECT_THAT(histogram_tester().GetAllSamples(
313 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), 335 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
314 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); 336 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1)));
315 } 337 }
316 338
317 } // namespace 339 } // namespace
318 } // namespace ntp_snippets 340 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698