Skip to content

Commit

Permalink
Add additional LinkPreviewUtil unit tests.
Browse files Browse the repository at this point in the history
Also updated the date format -- funnily enough Android will work with
either Z or X in the format, but the test JVM will fail if it doesn't
use X. X is definitely the correct thing to use based on the Javadoc, I
think Android's implementation is just a little more lenient.
  • Loading branch information
greyson-signal committed Aug 27, 2020
1 parent 3a9a84a commit b1befbe
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ public OpenGraph(@NonNull Map<String, String> values, @Nullable String htmlTitle
}

public long getDate() {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ", Locale.getDefault());
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX", Locale.getDefault());

return Stream.of(values.get(KEY_PUBLISHED_TIME_1),
values.get(KEY_PUBLISHED_TIME_2),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,41 @@ public class LinkPreviewUtilTest_parseOpenGraphFields {

private final String html;
private final String title;
private final String description;
private final long date;
private final String imageUrl;

@Parameterized.Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][]{
// Normal
{ "<meta content=\"Daily Bugle\" property=\"og:title\">\n" +
"<meta content=\"https://images.com/my-image.jpg\" property=\"og:image\">",
"<meta content=\"https://images.com/my-image.jpg\" property=\"og:image\">" +
"<meta content=\"A newspaper\" property=\"og:description\">" +
"<meta content=\"1991-12-30T00:00:00+00:00\" property=\"og:published_time\">",
"Daily Bugle",
"A newspaper",
694051200000L,
"https://images.com/my-image.jpg"},

// Swap property orders
{ "<meta property=\"og:title\" content=\"Daily Bugle\">\n" +
"<meta property=\"og:image\" content=\"https://images.com/my-image.jpg\">",
"<meta property=\"og:image\" content=\"https://images.com/my-image.jpg\">" +
"<meta property=\"og:description\" content=\"A newspaper\">" +
"<meta property=\"og:published_time\" content=\"1991-12-30T00:00:00+00:00\">",
"Daily Bugle",
"A newspaper",
694051200000L,
"https://images.com/my-image.jpg"},

// Funny spacing
{ "< meta property = \"og:title\" content = \"Daily Bugle\" >\n\n" +
"< meta property = \"og:image\" content =\"https://images.com/my-image.jpg\" >",
"< meta property = \"og:image\" content =\"https://images.com/my-image.jpg\" >" +
"< meta property =\"og:description\" content =\"A newspaper\"> " +
"< meta property =\"og:published_time\" content= \"1991-12-30T00:00:00+00:00\"> ",
"Daily Bugle",
"A newspaper",
694051200000L,
"https://images.com/my-image.jpg"},

// Garbage in various places
Expand All @@ -45,16 +59,22 @@ public static Collection<Object[]> data() {
"<script type=\"text/javascript\">var a = </script>\n" +
"<meta property=\"og:image\" content=\"https://images.com/my-image.jpg\">",
"Daily Bugle",
null,
0,
"https://images.com/my-image.jpg"},

// Missing image
{ "<meta content=\"Daily Bugle\" property=\"og:title\">",
"Daily Bugle",
null,
0,
null},

// Missing title
{ "<meta content=\"https://images.com/my-image.jpg\" property=\"og:image\">",
null,
null,
0,
"https://images.com/my-image.jpg"},

// Has everything
Expand All @@ -63,46 +83,90 @@ public static Collection<Object[]> data() {
"<meta property=\"og:image\" content=\"https://images.com/my-image.jpg\">\n" +
"<link rel=\"icon\" href=\"https://images.com/favicon.png\" />",
"Daily Bugle",
null,
0,
"https://images.com/my-image.jpg"},

// Fallback to HTML title
{ "<title>Daily Bugle HTML</title>\n" +
"<meta property=\"og:image\" content=\"https://images.com/my-image.jpg\">\n" +
"<link rel=\"icon\" href=\"https://images.com/favicon.png\" />",
"Daily Bugle HTML",
null,
0,
"https://images.com/my-image.jpg"},

// Fallback to favicon
{ "<meta property=\"og:title\" content = \"Daily Bugle\">\n" +
"<title>Daily Bugle HTML</title>\n" +
"<link rel=\"icon\" href=\"https://images.com/favicon.png\" />",
"Daily Bugle",
null,
0,
"https://images.com/favicon.png"},

// Fallback to HTML title and favicon
{ "<title>Daily Bugle HTML</title>\n" +
"<link rel=\"icon\" href=\"https://images.com/favicon.png\" />",
"Daily Bugle HTML",
null,
0,
"https://images.com/favicon.png"},

// Different favicon formatting
{ "<title>Daily Bugle HTML</title>\n" +
"<link rel=\"shortcut icon\" href=\"https://images.com/favicon.png\" />",
"Daily Bugle HTML",
null,
0,
"https://images.com/favicon.png"},

// Date: published_time variation
{ "<meta content=\"1991-12-30T00:00:00+00:00\" property=\"article:published_time\">",
null,
null,
694051200000L,
null},

// Date: Use modified_time if there's no published_time
{ "<meta content=\"1991-12-30T00:00:00+00:00\" property=\"og:modified_time\">",
null,
null,
694051200000L,
null},

// Date: modified_time variation
{ "<meta content=\"1991-12-30T00:00:00+00:00\" property=\"article:modified_time\">",
null,
null,
694051200000L,
null},

// Date: Prefer published_time
{ "<meta content=\"1991-12-31T00:00:00+00:00\" property=\"og:modified_time\">" +
"<meta content=\"1991-12-30T00:00:00+00:00\" property=\"og:published_time\">",
null,
null,
694051200000L,
null},

});
}

public LinkPreviewUtilTest_parseOpenGraphFields(String html, String title, String imageUrl) {
this.html = html;
this.title = title;
this.imageUrl = imageUrl;
public LinkPreviewUtilTest_parseOpenGraphFields(String html, String title, String description, long date, String imageUrl) {
this.html = html;
this.title = title;
this.description = description;
this.date = date;
this.imageUrl = imageUrl;
}

@Test
public void parseOpenGraphFields() {
LinkPreviewUtil.OpenGraph openGraph = LinkPreviewUtil.parseOpenGraphFields(html, html -> html);
assertEquals(Optional.fromNullable(title), openGraph.getTitle());
assertEquals(Optional.fromNullable(description), openGraph.getDescription());
assertEquals(date, openGraph.getDate());
assertEquals(Optional.fromNullable(imageUrl), openGraph.getImageUrl());
}
}

0 comments on commit b1befbe

Please sign in to comment.