Add additional LinkPreviewUtil unit tests.
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.master
parent
3a9a84a0b1
commit
b1befbeefc
|
@ -200,7 +200,7 @@ public final class LinkPreviewUtil {
|
|||
}
|
||||
|
||||
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),
|
||||
|
|
|
@ -15,6 +15,8 @@ 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
|
||||
|
@ -22,20 +24,32 @@ public class LinkPreviewUtilTest_parseOpenGraphFields {
|
|||
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
|
||||
|
@ -45,16 +59,22 @@ public class LinkPreviewUtilTest_parseOpenGraphFields {
|
|||
"<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
|
||||
|
@ -63,6 +83,8 @@ public class LinkPreviewUtilTest_parseOpenGraphFields {
|
|||
"<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
|
||||
|
@ -70,6 +92,8 @@ public class LinkPreviewUtilTest_parseOpenGraphFields {
|
|||
"<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
|
||||
|
@ -77,32 +101,72 @@ public class LinkPreviewUtilTest_parseOpenGraphFields {
|
|||
"<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());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue