faviKonSnoop: add AppleTouchIconSnooper and WhatWgSnooper
authorDa Risk <da_risk@geekorum.com>
Fri, 06 Sep 2019 17:34:55 -0700
changeset 449 b3cd1ea9c96b
parent 448 c907fe7b9faf
child 450 f23d64d2ba7b
faviKonSnoop: add AppleTouchIconSnooper and WhatWgSnooper
faviKonSnoop/src/main/kotlin/snoopers/AppleTouchIconSnooper.kt
faviKonSnoop/src/main/kotlin/snoopers/LinkRelSnooper.kt
faviKonSnoop/src/main/kotlin/snoopers/WhatWgSnooper.kt
faviKonSnoop/src/test/kotlin/snoopers/LinkRelSnooperTest.kt
faviKonSnoop/src/test/kotlin/snoopers/WhatWgSnooperTest.kt
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/faviKonSnoop/src/main/kotlin/snoopers/AppleTouchIconSnooper.kt	Fri Sep 06 17:34:55 2019 -0700
@@ -0,0 +1,26 @@
+/*
+ * Geekttrss is a RSS feed reader application on the Android Platform.
+ *
+ * Copyright (C) 2017-2019 by Frederic-Charles Barthelery.
+ *
+ * This file is part of Geekttrss.
+ *
+ * Geekttrss is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Geekttrss is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Geekttrss.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package com.geekorum.favikonsnoop.snoopers
+
+/**
+ * https://developer.apple.com/library/archive/documentation/AppleApplications/Reference/SafariWebContent/ConfiguringWebApplications/ConfiguringWebApplications.html
+ */
+class AppleTouchIconSnooper : LinkRelSnooper("apple-touch-icon")
--- a/faviKonSnoop/src/main/kotlin/snoopers/LinkRelSnooper.kt	Fri Sep 06 17:22:34 2019 -0700
+++ b/faviKonSnoop/src/main/kotlin/snoopers/LinkRelSnooper.kt	Fri Sep 06 17:34:55 2019 -0700
@@ -28,17 +28,14 @@
 import org.jsoup.Jsoup
 import java.io.InputStream
 
-/**
- * https://html.spec.whatwg.org/#rel-icon
- */
-class LinkRelSnooper(
+open class LinkRelSnooper(
     val relValue: String
 ) : Snooper() {
 
     override fun snoop(baseUrl: String, content: InputStream): Collection<FaviconInfo> {
         val document = Jsoup.parse(content, null, baseUrl)
 
-        val linksRel = document.head()?.let { head ->
+        return document.head()?.let { head ->
             head.getElementsByTag("link")
                 .filter {
                     it.attr("rel") == relValue
@@ -54,9 +51,6 @@
                     }
                 }
         } ?: emptyList()
-        val faviconLegacyUrl = "$baseUrl/favicon.ico"
-        val legacy = FaviconInfo(faviconLegacyUrl)
-        return linksRel + legacy
     }
 
     internal fun parseSizes(size: String): Collection<Dimension> {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/faviKonSnoop/src/main/kotlin/snoopers/WhatWgSnooper.kt	Fri Sep 06 17:34:55 2019 -0700
@@ -0,0 +1,36 @@
+/*
+ * Geekttrss is a RSS feed reader application on the Android Platform.
+ *
+ * Copyright (C) 2017-2019 by Frederic-Charles Barthelery.
+ *
+ * This file is part of Geekttrss.
+ *
+ * Geekttrss is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Geekttrss is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Geekttrss.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package com.geekorum.favikonsnoop.snoopers
+
+import com.geekorum.favikonsnoop.FaviconInfo
+import java.io.InputStream
+
+/**
+ * https://html.spec.whatwg.org/#rel-icon
+ */
+class WhatWgSnooper : LinkRelSnooper("icon") {
+    override fun snoop(baseUrl: String, content: InputStream): Collection<FaviconInfo> {
+        val linksResult = super.snoop(baseUrl, content)
+        val faviconLegacyUrl = "$baseUrl/favicon.ico"
+        val legacy = FaviconInfo(faviconLegacyUrl)
+        return linksResult + legacy
+    }
+}
--- a/faviKonSnoop/src/test/kotlin/snoopers/LinkRelSnooperTest.kt	Fri Sep 06 17:22:34 2019 -0700
+++ b/faviKonSnoop/src/test/kotlin/snoopers/LinkRelSnooperTest.kt	Fri Sep 06 17:34:55 2019 -0700
@@ -116,21 +116,21 @@
     }
 
     @Test
-    fun testInvalidReturnsFallbackFavicon() {
+    fun testInvalidReturnsEmpty() {
         val result = INVALID_HTML.byteInputStream().use {
             subject.snoop("http://exemple.com", it)
         }
 
-        assertThat(result).containsExactly(FaviconInfo("http://exemple.com/favicon.ico"))
+        assertThat(result).isEmpty()
     }
 
     @Test
-    fun testNoLinkReturnsFallbackFavicon() {
+    fun testNoLinkReturnsEmpty() {
         val result = NO_LINK_HTML.byteInputStream().use {
             subject.snoop("http://exemple.com", it)
         }
 
-        assertThat(result).containsExactly(FaviconInfo("http://exemple.com/favicon.ico"))
+        assertThat(result).isEmpty()
     }
 
     @Test
@@ -140,7 +140,6 @@
         }
 
         assertThat(result).containsExactly(
-            FaviconInfo("http://exemple.com/favicon.ico"),
             FaviconInfo("http://exemple.com/favicon.png",
                 mimeType = "image/png",
                 dimension = FixedDimension(16, 16)
@@ -155,7 +154,6 @@
         }
 
         assertThat(result).containsExactly(
-            FaviconInfo("http://exemple.com/favicon.ico"),
             FaviconInfo("http://exemple.com/mac.icns",
                 dimension = FixedDimension(128, 128)
             ),
@@ -178,7 +176,6 @@
         }
 
         assertThat(result).containsExactly(
-            FaviconInfo("http://exemple.com/favicon.ico"),
             FaviconInfo("http://exemple.com/favicon.png",
                 mimeType = "image/png",
                 dimension = FixedDimension(16, 16)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/faviKonSnoop/src/test/kotlin/snoopers/WhatWgSnooperTest.kt	Fri Sep 06 17:34:55 2019 -0700
@@ -0,0 +1,114 @@
+/*
+ * Geekttrss is a RSS feed reader application on the Android Platform.
+ *
+ * Copyright (C) 2017-2019 by Frederic-Charles Barthelery.
+ *
+ * This file is part of Geekttrss.
+ *
+ * Geekttrss is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Geekttrss is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Geekttrss.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package com.geekorum.favikonsnoop.snoopers
+
+import com.geekorum.favikonsnoop.AdaptiveDimension
+import com.geekorum.favikonsnoop.FaviconInfo
+import com.geekorum.favikonsnoop.FixedDimension
+import com.google.common.truth.Truth.assertThat
+import kotlin.test.BeforeTest
+import kotlin.test.Test
+
+private val MANY_HTML =
+    """
+    <html lang="en">
+     <head>
+      <title>lsForums — Inbox</title>
+      <link rel=icon href=favicon.png sizes="16x16" type="image/png">
+      <link rel=icon href=windows.ico sizes="32x32 48x48" type="image/vnd.microsoft.icon">
+      <link rel=icon href=mac.icns sizes="128x128 512x512 8192x8192 32768x32768">
+      <link rel=icon href=iphone.png sizes="57x57" type="image/png">
+      <link rel=icon href=gnome.svg sizes="any" type="image/svg+xml">
+      <link rel=stylesheet href=lsforums.css>
+      <script src=lsforums.js></script>
+      <meta name=application-name content="lsForums">
+     </head>
+    </html>
+    """.trimIndent()
+
+private val INVALID_HTML =
+    """fw""".trimIndent()
+
+class WhatWgSnooperTest {
+    lateinit var subject: LinkRelSnooper
+
+    @BeforeTest
+    fun setUp() {
+        subject = WhatWgSnooper()
+    }
+
+    @Test
+    fun testInvalidReturnsFallbackFavicon() {
+        val result = INVALID_HTML.byteInputStream().use {
+            subject.snoop("http://exemple.com", it)
+        }
+
+        assertThat(result).containsExactly(FaviconInfo("http://exemple.com/favicon.ico"))
+    }
+
+    @Test
+    fun testManyLinkReturnsCorrectResult() {
+        val result = MANY_HTML.byteInputStream().use {
+            subject.snoop("http://exemple.com", it)
+        }
+
+        assertThat(result).containsExactly(
+            FaviconInfo("http://exemple.com/favicon.ico"),
+            FaviconInfo("http://exemple.com/favicon.png",
+                mimeType = "image/png",
+                dimension = FixedDimension(16, 16)
+            ),
+            // mac
+            FaviconInfo("http://exemple.com/mac.icns",
+                dimension = FixedDimension(128, 128)
+            ),
+            FaviconInfo("http://exemple.com/mac.icns",
+                dimension = FixedDimension(512, 512)
+            ),
+            FaviconInfo("http://exemple.com/mac.icns",
+                dimension = FixedDimension(8192, 8192)
+            ),
+            FaviconInfo("http://exemple.com/mac.icns",
+                dimension = FixedDimension(32768, 32768)
+            ),
+            // windo2s
+            FaviconInfo("http://exemple.com/windows.ico",
+                dimension = FixedDimension(32, 32),
+                mimeType = "image/vnd.microsoft.icon"
+            ),
+            FaviconInfo("http://exemple.com/windows.ico",
+                dimension = FixedDimension(48, 48),
+                mimeType = "image/vnd.microsoft.icon"
+            ),
+            // iphone
+            FaviconInfo("http://exemple.com/iphone.png",
+                mimeType = "image/png",
+                dimension = FixedDimension(57,57)
+            ),
+            // gnome
+            FaviconInfo("http://exemple.com/gnome.svg",
+                mimeType = "image/svg+xml",
+                dimension = AdaptiveDimension
+            )
+        )
+    }
+
+}