Skip to content

Commit

Permalink
Overhaul ntp_snippets_service_unittest.cc.
Browse files Browse the repository at this point in the history
A partial list of things done here:

  * Move tests that verified NTPSnippet::best_source() behavior into
    ntp_snippet_test.cc; they don't belong here.
  * Switch it to use the chromecontentsuggestions-pa. Testing new
    features like server-provided categories is not possible without
    this.
  * Replace MockContentSuggestionsProviderObserver with a fake. It
    shouldn't have been a mock, since we weren't using it to test
    behavior, only state.
  * In one case, test using what the observer sees, instead of peeking
    in through debugging hooks.
  * Get rid of special test base class and Mock::VerifyAndClear calls; I
    consider these anti-patterns.
  * Initialize the service better; give it an empty list of snippets for
    its first fetch, and wait for it to complete the fetch before
    returning (I think this is what was most strongly implicated by the
    failures with the other CL).
  * Create the service and expect the scheduling directly from each
    case, so they are more readable individually.

Review-Url: https://codereview.chromium.org/2285133004
Cr-Commit-Position: refs/heads/master@{#415358}
  • Loading branch information
sfiera authored and Commit bot committed Aug 30, 2016
1 parent 740cb04 commit e88d168
Show file tree
Hide file tree
Showing 5 changed files with 672 additions and 571 deletions.
226 changes: 221 additions & 5 deletions components/ntp_snippets/ntp_snippet_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@
namespace ntp_snippets {
namespace {

std::unique_ptr<NTPSnippet> SnippetFromContentSuggestionJSON(
const std::string& json) {
auto json_value = base::JSONReader::Read(json);
base::DictionaryValue* json_dict;
if (!json_value->GetAsDictionary(&json_dict)) {
return nullptr;
}
return NTPSnippet::CreateFromContentSuggestionsDictionary(*json_dict);
}

TEST(NTPSnippetTest, FromChromeContentSuggestionsDictionary) {
const std::string kJsonStr =
"{"
Expand All @@ -26,11 +36,7 @@ TEST(NTPSnippetTest, FromChromeContentSuggestionsDictionary) {
" \"ampUrl\" : \"http://localhost/amp\","
" \"faviconUrl\" : \"http://localhost/favicon.ico\" "
"}";
auto json_value = base::JSONReader::Read(kJsonStr);
base::DictionaryValue* json_dict;
ASSERT_TRUE(json_value->GetAsDictionary(&json_dict));

auto snippet = NTPSnippet::CreateFromContentSuggestionsDictionary(*json_dict);
auto snippet = SnippetFromContentSuggestionJSON(kJsonStr);
ASSERT_THAT(snippet, testing::NotNull());

EXPECT_EQ(snippet->id(), "http://localhost/foobar");
Expand All @@ -47,5 +53,215 @@ TEST(NTPSnippetTest, FromChromeContentSuggestionsDictionary) {
EXPECT_EQ(snippet->best_source().amp_url, GURL("http://localhost/amp"));
}

std::unique_ptr<NTPSnippet> SnippetFromChromeReaderDict(
std::unique_ptr<base::DictionaryValue> dict) {
if (!dict) {
return nullptr;
}
return NTPSnippet::CreateFromChromeReaderDictionary(*dict);
}

std::unique_ptr<base::DictionaryValue> SnippetWithTwoSources() {
const std::string kJsonStr =
"{\n"
" \"contentInfo\": {\n"
" \"url\": \"http://url.com\",\n"
" \"title\": \"Source 1 Title\",\n"
" \"snippet\": \"Source 1 Snippet\",\n"
" \"thumbnailUrl\": \"http://url.com/thumbnail\",\n"
" \"creationTimestampSec\": 1234567890,\n"
" \"expiryTimestampSec\": 2345678901,\n"
" \"sourceCorpusInfo\": [{\n"
" \"corpusId\": \"http://source1.com\",\n"
" \"publisherData\": {\n"
" \"sourceName\": \"Source 1\"\n"
" },\n"
" \"ampUrl\": \"http://source1.amp.com\"\n"
" }, {\n"
" \"corpusId\": \"http://source2.com\",\n"
" \"publisherData\": {\n"
" \"sourceName\": \"Source 2\"\n"
" },\n"
" \"ampUrl\": \"http://source2.amp.com\"\n"
" }]\n"
" },\n"
" \"score\": 5.0\n"
"}\n";

auto json_value = base::JSONReader::Read(kJsonStr);
base::DictionaryValue* json_dict;
if (!json_value->GetAsDictionary(&json_dict)) {
return nullptr;
}
return json_dict->CreateDeepCopy();
}

TEST(NTPSnippetTest, TestMultipleSources) {
auto snippet = SnippetFromChromeReaderDict(SnippetWithTwoSources());
ASSERT_THAT(snippet, testing::NotNull());

// Expect the first source to be chosen.
EXPECT_EQ(snippet->sources().size(), 2u);
EXPECT_EQ(snippet->id(), "http://url.com");
EXPECT_EQ(snippet->best_source().url, GURL("http://source1.com"));
EXPECT_EQ(snippet->best_source().publisher_name, std::string("Source 1"));
EXPECT_EQ(snippet->best_source().amp_url, GURL("http://source1.amp.com"));
}

TEST(NTPSnippetTest, TestMultipleIncompleteSources1) {
// Set Source 2 to have no AMP url, and Source 1 to have no publisher name
// Source 2 should win since we favor publisher name over amp url
auto dict = SnippetWithTwoSources();
base::ListValue* sources;
ASSERT_TRUE(dict->GetList("contentInfo.sourceCorpusInfo", &sources));
base::DictionaryValue* source;
ASSERT_TRUE(sources->GetDictionary(0, &source));
source->Remove("publisherData.sourceName", nullptr);
ASSERT_TRUE(sources->GetDictionary(1, &source));
source->Remove("ampUrl", nullptr);

auto snippet = SnippetFromChromeReaderDict(std::move(dict));
ASSERT_THAT(snippet, testing::NotNull());

EXPECT_EQ(snippet->sources().size(), 2u);
EXPECT_EQ(snippet->id(), "http://url.com");
EXPECT_EQ(snippet->best_source().url, GURL("http://source2.com"));
EXPECT_EQ(snippet->best_source().publisher_name, std::string("Source 2"));
EXPECT_EQ(snippet->best_source().amp_url, GURL());
}

TEST(NTPSnippetTest, TestMultipleIncompleteSources2) {
// Set Source 1 to have no AMP url, and Source 2 to have no publisher name
// Source 1 should win in this case since we prefer publisher name to AMP url
auto dict = SnippetWithTwoSources();
base::ListValue* sources;
ASSERT_TRUE(dict->GetList("contentInfo.sourceCorpusInfo", &sources));
base::DictionaryValue* source;
ASSERT_TRUE(sources->GetDictionary(0, &source));
source->Remove("ampUrl", nullptr);
ASSERT_TRUE(sources->GetDictionary(1, &source));
source->Remove("publisherData.sourceName", nullptr);

auto snippet = SnippetFromChromeReaderDict(std::move(dict));
ASSERT_THAT(snippet, testing::NotNull());

EXPECT_EQ(snippet->sources().size(), 2u);
EXPECT_EQ(snippet->id(), "http://url.com");
EXPECT_EQ(snippet->best_source().url, GURL("http://source1.com"));
EXPECT_EQ(snippet->best_source().publisher_name, std::string("Source 1"));
EXPECT_EQ(snippet->best_source().amp_url, GURL());
}

TEST(NTPSnippetTest, TestMultipleIncompleteSources3) {
// Set source 1 to have no AMP url and no source, and source 2 to only have
// amp url. There should be no snippets since we only add sources we consider
// complete
auto dict = SnippetWithTwoSources();
base::ListValue* sources;
ASSERT_TRUE(dict->GetList("contentInfo.sourceCorpusInfo", &sources));
base::DictionaryValue* source;
ASSERT_TRUE(sources->GetDictionary(0, &source));
source->Remove("publisherData.sourceName", nullptr);
source->Remove("ampUrl", nullptr);
ASSERT_TRUE(sources->GetDictionary(1, &source));
source->Remove("publisherData.sourceName", nullptr);

auto snippet = SnippetFromChromeReaderDict(std::move(dict));
ASSERT_THAT(snippet, testing::NotNull());
ASSERT_FALSE(snippet->is_complete());
}

std::unique_ptr<base::DictionaryValue> SnippetWithThreeSources() {
const std::string kJsonStr =
"{\n"
" \"contentInfo\": {\n"
" \"url\": \"http://url.com\",\n"
" \"title\": \"Source 1 Title\",\n"
" \"snippet\": \"Source 1 Snippet\",\n"
" \"thumbnailUrl\": \"http://url.com/thumbnail\",\n"
" \"creationTimestampSec\": 1234567890,\n"
" \"expiryTimestampSec\": 2345678901,\n"
" \"sourceCorpusInfo\": [{\n"
" \"corpusId\": \"http://source1.com\",\n"
" \"publisherData\": {\n"
" \"sourceName\": \"Source 1\"\n"
" },\n"
" \"ampUrl\": \"http://source1.amp.com\"\n"
" }, {\n"
" \"corpusId\": \"http://source2.com\",\n"
" \"publisherData\": {\n"
" \"sourceName\": \"Source 2\"\n"
" },\n"
" \"ampUrl\": \"http://source2.amp.com\"\n"
" }, {\n"
" \"corpusId\": \"http://source3.com\",\n"
" \"publisherData\": {\n"
" \"sourceName\": \"Source 3\"\n"
" },\n"
" \"ampUrl\": \"http://source3.amp.com\"\n"
" }]\n"
" },\n"
" \"score\": 5.0\n"
"}\n";

auto json_value = base::JSONReader::Read(kJsonStr);
base::DictionaryValue* json_dict;
if (!json_value->GetAsDictionary(&json_dict)) {
return nullptr;
}
return json_dict->CreateDeepCopy();
}

TEST(NTPSnippetTest, TestMultipleCompleteSources1) {
// Test 2 complete sources, we should choose the first complete source
auto dict = SnippetWithThreeSources();
base::ListValue* sources;
ASSERT_TRUE(dict->GetList("contentInfo.sourceCorpusInfo", &sources));
base::DictionaryValue* source;
ASSERT_TRUE(sources->GetDictionary(1, &source));
source->Remove("publisherData.sourceName", nullptr);

auto snippet = SnippetFromChromeReaderDict(std::move(dict));
ASSERT_THAT(snippet, testing::NotNull());

EXPECT_EQ(snippet->sources().size(), 3u);
EXPECT_EQ(snippet->id(), "http://url.com");
EXPECT_EQ(snippet->best_source().url, GURL("http://source1.com"));
EXPECT_EQ(snippet->best_source().publisher_name, std::string("Source 1"));
EXPECT_EQ(snippet->best_source().amp_url, GURL("http://source1.amp.com"));
}

TEST(NTPSnippetTest, TestMultipleCompleteSources2) {
// Test 2 complete sources, we should choose the first complete source
auto dict = SnippetWithThreeSources();
base::ListValue* sources;
ASSERT_TRUE(dict->GetList("contentInfo.sourceCorpusInfo", &sources));
base::DictionaryValue* source;
ASSERT_TRUE(sources->GetDictionary(0, &source));
source->Remove("publisherData.sourceName", nullptr);

auto snippet = SnippetFromChromeReaderDict(std::move(dict));
ASSERT_THAT(snippet, testing::NotNull());

EXPECT_EQ(snippet->sources().size(), 3u);
EXPECT_EQ(snippet->id(), "http://url.com");
EXPECT_EQ(snippet->best_source().url, GURL("http://source2.com"));
EXPECT_EQ(snippet->best_source().publisher_name, std::string("Source 2"));
EXPECT_EQ(snippet->best_source().amp_url, GURL("http://source2.amp.com"));
}

TEST(NTPSnippetTest, TestMultipleCompleteSources3) {
// Test 3 complete sources, we should choose the first complete source
auto dict = SnippetWithThreeSources();
auto snippet = SnippetFromChromeReaderDict(std::move(dict));
ASSERT_THAT(snippet, testing::NotNull());

EXPECT_EQ(snippet->sources().size(), 3u);
EXPECT_EQ(snippet->id(), "http://url.com");
EXPECT_EQ(snippet->best_source().url, GURL("http://source1.com"));
EXPECT_EQ(snippet->best_source().publisher_name, std::string("Source 1"));
EXPECT_EQ(snippet->best_source().amp_url, GURL("http://source1.amp.com"));
}

} // namespace
} // namespace ntp_snippets
Loading

0 comments on commit e88d168

Please sign in to comment.