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

Unified 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « components/ntp_snippets/ntp_snippets_fetcher.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: components/ntp_snippets/ntp_snippets_fetcher_unittest.cc
diff --git a/components/ntp_snippets/ntp_snippets_fetcher_unittest.cc b/components/ntp_snippets/ntp_snippets_fetcher_unittest.cc
index c67f165c3d28489becd708c2448e7251df4050f9..523e1d52b10ca85dfed95be7ca9d096274d465ab 100644
--- a/components/ntp_snippets/ntp_snippets_fetcher_unittest.cc
+++ b/components/ntp_snippets/ntp_snippets_fetcher_unittest.cc
@@ -24,6 +24,7 @@
#include "testing/gtest/include/gtest/gtest.h"
namespace ntp_snippets {
+
namespace {
using testing::ElementsAre;
@@ -51,6 +52,25 @@ MATCHER_P(PointeeSizeIs,
return arg && static_cast<int>(arg->size()) == size;
}
+MATCHER_P(EqualsJSON, json, "equals JSON") {
+ std::unique_ptr<base::Value> expected = base::JSONReader::Read(json);
+ if (!expected) {
+ *result_listener << "INTERNAL ERROR: couldn't parse expected JSON";
+ return false;
+ }
+
+ std::string err_msg;
+ int err_line, err_col;
+ std::unique_ptr<base::Value> actual = base::JSONReader::ReadAndReturnError(
+ arg, base::JSON_PARSE_RFC, nullptr, &err_msg, &err_line, &err_col);
+ if (!actual) {
+ *result_listener << "input:" << err_line << ":" << err_col << ": "
+ << "parse error: " << err_msg;
+ return false;
+ }
+ return base::Value::Equals(actual.get(), expected.get());
+}
+
class MockSnippetsAvailableCallback {
public:
// Workaround for gMock's lack of support for movable arguments.
@@ -94,6 +114,8 @@ void ParseJsonDelayed(
base::TimeDelta::FromMilliseconds(kTestJsonParsingLatencyMs));
}
+} // namespace
+
class NTPSnippetsFetcherTest : public testing::Test {
public:
NTPSnippetsFetcherTest()
@@ -171,6 +193,81 @@ class NTPSnippetsFetcherTest : public testing::Test {
DISALLOW_COPY_AND_ASSIGN(NTPSnippetsFetcherTest);
};
+TEST_F(NTPSnippetsFetcherTest, BuildRequestAuthenticated) {
+ EXPECT_THAT(NTPSnippetsFetcher::BuildRequest("0BFUSGAIA", true, "en",
+ {"chromium.org"}, 25),
+ EqualsJSON("{"
+ " \"response_detail_level\": \"STANDARD\","
+ " \"obfuscated_gaia_id\": \"0BFUSGAIA\","
+ " \"advanced_options\": {"
+ " \"local_scoring_params\": {"
+ " \"content_params\": {"
+ " \"only_return_personalized_results\": true,"
+ " \"user_segment\": \"en\""
+ " },"
+ " \"content_restricts\": ["
+ " {"
+ " \"type\": \"METADATA\","
+ " \"value\": \"TITLE\""
+ " },"
+ " {"
+ " \"type\": \"METADATA\","
+ " \"value\": \"SNIPPET\""
+ " },"
+ " {"
+ " \"type\": \"METADATA\","
+ " \"value\": \"THUMBNAIL\""
+ " }"
+ " ],"
+ " \"content_selectors\": ["
+ " {"
+ " \"type\": \"HOST_RESTRICT\","
+ " \"value\": \"chromium.org\""
+ " }"
+ " ]"
+ " },"
+ " \"global_scoring_params\": {"
+ " \"num_to_return\": 25,"
+ " \"sort_type\": 1"
+ " }"
+ " }"
+ "}"));
+}
+
+TEST_F(NTPSnippetsFetcherTest, BuildRequestUnauthenticated) {
+ EXPECT_THAT(NTPSnippetsFetcher::BuildRequest("", false, "",
+ std::set<std::string>(), 10),
+ EqualsJSON("{"
+ " \"response_detail_level\": \"STANDARD\","
+ " \"advanced_options\": {"
+ " \"local_scoring_params\": {"
+ " \"content_params\": {"
+ " \"only_return_personalized_results\": false"
+ " },"
+ " \"content_restricts\": ["
+ " {"
+ " \"type\": \"METADATA\","
+ " \"value\": \"TITLE\""
+ " },"
+ " {"
+ " \"type\": \"METADATA\","
+ " \"value\": \"SNIPPET\""
+ " },"
+ " {"
+ " \"type\": \"METADATA\","
+ " \"value\": \"THUMBNAIL\""
+ " }"
+ " ],"
+ " \"content_selectors\": []"
+ " },"
+ " \"global_scoring_params\": {"
+ " \"num_to_return\": 10,"
+ " \"sort_type\": 1"
+ " }"
+ " }"
+ "}"));
+}
+
TEST_F(NTPSnippetsFetcherTest, ShouldNotFetchOnCreation) {
// The lack of registered baked in responses would cause any fetch to fail.
FastForwardUntilNoTasksRemain();
@@ -407,5 +504,4 @@ TEST_F(NTPSnippetsFetcherTest, ShouldCancelOngoingFetch) {
/*count=*/1)));
}
-} // namespace
} // namespace ntp_snippets
« 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