diff --git a/app/src/main/java/org/thoughtcrime/securesms/linkpreview/LinkPreviewUtil.java b/app/src/main/java/org/thoughtcrime/securesms/linkpreview/LinkPreviewUtil.java index 2d878ccadc..b4b6855d8b 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/linkpreview/LinkPreviewUtil.java +++ b/app/src/main/java/org/thoughtcrime/securesms/linkpreview/LinkPreviewUtil.java @@ -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), diff --git a/app/src/test/java/org/thoughtcrime/securesms/linkpreview/LinkPreviewUtilTest_parseOpenGraphFields.java b/app/src/test/java/org/thoughtcrime/securesms/linkpreview/LinkPreviewUtilTest_parseOpenGraphFields.java index 502fafc4c5..e8171d5206 100644 --- a/app/src/test/java/org/thoughtcrime/securesms/linkpreview/LinkPreviewUtilTest_parseOpenGraphFields.java +++ b/app/src/test/java/org/thoughtcrime/securesms/linkpreview/LinkPreviewUtilTest_parseOpenGraphFields.java @@ -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 { "\n" + - "", + "" + + "" + + "", "Daily Bugle", + "A newspaper", + 694051200000L, "https://images.com/my-image.jpg"}, // Swap property orders { "\n" + - "", + "" + + "" + + "", "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 { "\n" + "", "Daily Bugle", + null, + 0, "https://images.com/my-image.jpg"}, // Missing image { "", "Daily Bugle", + null, + 0, null}, // Missing title { "", null, + null, + 0, "https://images.com/my-image.jpg"}, // Has everything @@ -63,6 +83,8 @@ public class LinkPreviewUtilTest_parseOpenGraphFields { "\n" + "", "Daily Bugle", + null, + 0, "https://images.com/my-image.jpg"}, // Fallback to HTML title @@ -70,6 +92,8 @@ public class LinkPreviewUtilTest_parseOpenGraphFields { "\n" + "", "Daily Bugle HTML", + null, + 0, "https://images.com/my-image.jpg"}, // Fallback to favicon @@ -77,32 +101,72 @@ public class LinkPreviewUtilTest_parseOpenGraphFields { "Daily Bugle HTML\n" + "", "Daily Bugle", + null, + 0, "https://images.com/favicon.png"}, // Fallback to HTML title and favicon { "Daily Bugle HTML\n" + "", "Daily Bugle HTML", + null, + 0, "https://images.com/favicon.png"}, // Different favicon formatting { "Daily Bugle HTML\n" + "", "Daily Bugle HTML", + null, + 0, "https://images.com/favicon.png"}, + + // Date: published_time variation + { "", + null, + null, + 694051200000L, + null}, + + // Date: Use modified_time if there's no published_time + { "", + null, + null, + 694051200000L, + null}, + + // Date: modified_time variation + { "", + null, + null, + 694051200000L, + null}, + + // Date: Prefer 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()); } }