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

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

Issue 2077973002: Generate snippets request JSON with base::Value. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Avoid bracket initialization. Created 4 years, 6 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
« no previous file with comments | « components/ntp_snippets/ntp_snippets_fetcher.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/threading/thread_task_runner_handle.h" 12 #include "base/threading/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" 16 #include "components/signin/core/browser/account_tracker_service.h"
17 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h" 17 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h"
18 #include "components/signin/core/browser/fake_signin_manager.h" 18 #include "components/signin/core/browser/fake_signin_manager.h"
19 #include "components/signin/core/browser/test_signin_client.h" 19 #include "components/signin/core/browser/test_signin_client.h"
20 #include "google_apis/google_api_keys.h" 20 #include "google_apis/google_api_keys.h"
21 #include "net/url_request/test_url_fetcher_factory.h" 21 #include "net/url_request/test_url_fetcher_factory.h"
22 #include "net/url_request/url_request_test_util.h" 22 #include "net/url_request/url_request_test_util.h"
23 #include "testing/gmock/include/gmock/gmock.h" 23 #include "testing/gmock/include/gmock/gmock.h"
24 #include "testing/gtest/include/gtest/gtest.h" 24 #include "testing/gtest/include/gtest/gtest.h"
25 25
26 namespace ntp_snippets { 26 namespace ntp_snippets {
27
27 namespace { 28 namespace {
28 29
29 using testing::ElementsAre; 30 using testing::ElementsAre;
30 using testing::Eq; 31 using testing::Eq;
31 using testing::IsEmpty; 32 using testing::IsEmpty;
32 using testing::IsNull; 33 using testing::IsNull;
33 using testing::Not; 34 using testing::Not;
34 using testing::NotNull; 35 using testing::NotNull;
35 using testing::PrintToString; 36 using testing::PrintToString;
36 using testing::SizeIs; 37 using testing::SizeIs;
37 using testing::StartsWith; 38 using testing::StartsWith;
38 39
39 const char kTestContentSnippetsServerFormat[] = 40 const char kTestContentSnippetsServerFormat[] =
40 "https://chromereader-pa.googleapis.com/v1/fetch?key=%s"; 41 "https://chromereader-pa.googleapis.com/v1/fetch?key=%s";
41 // Artificial time delay for JSON parsing. 42 // Artificial time delay for JSON parsing.
42 const int64_t kTestJsonParsingLatencyMs = 20; 43 const int64_t kTestJsonParsingLatencyMs = 20;
43 44
44 MATCHER(HasValue, "") { 45 MATCHER(HasValue, "") {
45 return static_cast<bool>(arg); 46 return static_cast<bool>(arg);
46 } 47 }
47 48
48 MATCHER_P(PointeeSizeIs, 49 MATCHER_P(PointeeSizeIs,
49 size, 50 size,
50 std::string("contains a value with size ") + PrintToString(size)) { 51 std::string("contains a value with size ") + PrintToString(size)) {
51 return arg && static_cast<int>(arg->size()) == size; 52 return arg && static_cast<int>(arg->size()) == size;
52 } 53 }
53 54
55 MATCHER_P(EqualsJSON, json, "equals JSON") {
56 std::unique_ptr<base::Value> expected = base::JSONReader::Read(json);
57 if (!expected) {
58 *result_listener << "INTERNAL ERROR: couldn't parse expected JSON";
59 return false;
60 }
61
62 std::string err_msg;
63 int err_line, err_col;
64 std::unique_ptr<base::Value> actual = base::JSONReader::ReadAndReturnError(
65 arg, base::JSON_PARSE_RFC, nullptr, &err_msg, &err_line, &err_col);
66 if (!actual) {
67 *result_listener << "input:" << err_line << ":" << err_col << ": "
68 << "parse error: " << err_msg;
69 return false;
70 }
71 return base::Value::Equals(actual.get(), expected.get());
72 }
73
54 class MockSnippetsAvailableCallback { 74 class MockSnippetsAvailableCallback {
55 public: 75 public:
56 // Workaround for gMock's lack of support for movable arguments. 76 // Workaround for gMock's lack of support for movable arguments.
57 void WrappedRun(NTPSnippetsFetcher::OptionalSnippets snippets) { 77 void WrappedRun(NTPSnippetsFetcher::OptionalSnippets snippets) {
58 Run(snippets); 78 Run(snippets);
59 } 79 }
60 80
61 MOCK_METHOD1(Run, void(const NTPSnippetsFetcher::OptionalSnippets& snippets)); 81 MOCK_METHOD1(Run, void(const NTPSnippetsFetcher::OptionalSnippets& snippets));
62 }; 82 };
63 83
(...skipping 23 matching lines...) Expand all
87 107
88 void ParseJsonDelayed( 108 void ParseJsonDelayed(
89 const std::string& json, 109 const std::string& json,
90 const ntp_snippets::NTPSnippetsFetcher::SuccessCallback& success_callback, 110 const ntp_snippets::NTPSnippetsFetcher::SuccessCallback& success_callback,
91 const ntp_snippets::NTPSnippetsFetcher::ErrorCallback& error_callback) { 111 const ntp_snippets::NTPSnippetsFetcher::ErrorCallback& error_callback) {
92 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( 112 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
93 FROM_HERE, base::Bind(&ParseJson, json, success_callback, error_callback), 113 FROM_HERE, base::Bind(&ParseJson, json, success_callback, error_callback),
94 base::TimeDelta::FromMilliseconds(kTestJsonParsingLatencyMs)); 114 base::TimeDelta::FromMilliseconds(kTestJsonParsingLatencyMs));
95 } 115 }
96 116
117 } // namespace
118
97 class NTPSnippetsFetcherTest : public testing::Test { 119 class NTPSnippetsFetcherTest : public testing::Test {
98 public: 120 public:
99 NTPSnippetsFetcherTest() 121 NTPSnippetsFetcherTest()
100 : mock_task_runner_(new base::TestMockTimeTaskRunner()), 122 : mock_task_runner_(new base::TestMockTimeTaskRunner()),
101 mock_task_runner_handle_(mock_task_runner_), 123 mock_task_runner_handle_(mock_task_runner_),
102 signin_client_(new TestSigninClient(nullptr)), 124 signin_client_(new TestSigninClient(nullptr)),
103 account_tracker_(new AccountTrackerService()), 125 account_tracker_(new AccountTrackerService()),
104 fake_signin_manager_(new FakeSigninManagerBase(signin_client_.get(), 126 fake_signin_manager_(new FakeSigninManagerBase(signin_client_.get(),
105 account_tracker_.get())), 127 account_tracker_.get())),
106 fake_token_service_(new FakeProfileOAuth2TokenService()), 128 fake_token_service_(new FakeProfileOAuth2TokenService()),
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
164 NTPSnippetsFetcher snippets_fetcher_; 186 NTPSnippetsFetcher snippets_fetcher_;
165 MockSnippetsAvailableCallback mock_callback_; 187 MockSnippetsAvailableCallback mock_callback_;
166 const std::string test_lang_; 188 const std::string test_lang_;
167 const GURL test_url_; 189 const GURL test_url_;
168 std::set<std::string> test_hosts_; 190 std::set<std::string> test_hosts_;
169 base::HistogramTester histogram_tester_; 191 base::HistogramTester histogram_tester_;
170 192
171 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsFetcherTest); 193 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsFetcherTest);
172 }; 194 };
173 195
196 TEST_F(NTPSnippetsFetcherTest, BuildRequestAuthenticated) {
197 EXPECT_THAT(NTPSnippetsFetcher::BuildRequest("0BFUSGAIA", true, "en",
198 {"chromium.org"}, 25),
199 EqualsJSON("{"
200 " \"response_detail_level\": \"STANDARD\","
201 " \"obfuscated_gaia_id\": \"0BFUSGAIA\","
202 " \"advanced_options\": {"
203 " \"local_scoring_params\": {"
204 " \"content_params\": {"
205 " \"only_return_personalized_results\": true,"
206 " \"user_segment\": \"en\""
207 " },"
208 " \"content_restricts\": ["
209 " {"
210 " \"type\": \"METADATA\","
211 " \"value\": \"TITLE\""
212 " },"
213 " {"
214 " \"type\": \"METADATA\","
215 " \"value\": \"SNIPPET\""
216 " },"
217 " {"
218 " \"type\": \"METADATA\","
219 " \"value\": \"THUMBNAIL\""
220 " }"
221 " ],"
222 " \"content_selectors\": ["
223 " {"
224 " \"type\": \"HOST_RESTRICT\","
225 " \"value\": \"chromium.org\""
226 " }"
227 " ]"
228 " },"
229 " \"global_scoring_params\": {"
230 " \"num_to_return\": 25,"
231 " \"sort_type\": 1"
232 " }"
233 " }"
234 "}"));
235 }
236
237 TEST_F(NTPSnippetsFetcherTest, BuildRequestUnauthenticated) {
238 EXPECT_THAT(NTPSnippetsFetcher::BuildRequest("", false, "",
239 std::set<std::string>(), 10),
240 EqualsJSON("{"
241 " \"response_detail_level\": \"STANDARD\","
242 " \"advanced_options\": {"
243 " \"local_scoring_params\": {"
244 " \"content_params\": {"
245 " \"only_return_personalized_results\": false"
246 " },"
247 " \"content_restricts\": ["
248 " {"
249 " \"type\": \"METADATA\","
250 " \"value\": \"TITLE\""
251 " },"
252 " {"
253 " \"type\": \"METADATA\","
254 " \"value\": \"SNIPPET\""
255 " },"
256 " {"
257 " \"type\": \"METADATA\","
258 " \"value\": \"THUMBNAIL\""
259 " }"
260 " ],"
261 " \"content_selectors\": []"
262 " },"
263 " \"global_scoring_params\": {"
264 " \"num_to_return\": 10,"
265 " \"sort_type\": 1"
266 " }"
267 " }"
268 "}"));
269 }
270
174 TEST_F(NTPSnippetsFetcherTest, ShouldNotFetchOnCreation) { 271 TEST_F(NTPSnippetsFetcherTest, ShouldNotFetchOnCreation) {
175 // The lack of registered baked in responses would cause any fetch to fail. 272 // The lack of registered baked in responses would cause any fetch to fail.
176 FastForwardUntilNoTasksRemain(); 273 FastForwardUntilNoTasksRemain();
177 EXPECT_THAT(histogram_tester().GetAllSamples( 274 EXPECT_THAT(histogram_tester().GetAllSamples(
178 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), 275 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
179 IsEmpty()); 276 IsEmpty());
180 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), 277 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"),
181 IsEmpty()); 278 IsEmpty());
182 EXPECT_THAT(snippets_fetcher().last_status(), IsEmpty()); 279 EXPECT_THAT(snippets_fetcher().last_status(), IsEmpty());
183 } 280 }
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after
400 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), 497 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"),
401 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1))); 498 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1)));
402 EXPECT_THAT(histogram_tester().GetAllSamples( 499 EXPECT_THAT(histogram_tester().GetAllSamples(
403 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), 500 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
404 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); 501 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1)));
405 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), 502 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"),
406 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, 503 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs,
407 /*count=*/1))); 504 /*count=*/1)));
408 } 505 }
409 506
410 } // namespace
411 } // namespace ntp_snippets 507 } // namespace ntp_snippets
OLDNEW
« no previous file with comments | « components/ntp_snippets/ntp_snippets_fetcher.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698