summaryrefslogtreecommitdiff
path: root/app/src/test/java/org/terst/feedreader/readability/ArticleLitterTest.kt
blob: 121783b5f9658433560ac244cc234e7fe3226d53 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package org.terst.feedreader.readability

import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test

/**
 * The single entry point both cleaning sites call.
 *
 * The thing worth testing is that it applies *every* cleaner, because the failure it exists to
 * prevent is a cleaner that only runs on one of the two paths.
 */
class ArticleLitterTest {

    private val recirculationWidget =
        """<div data-mrf-recirculation="IL_x" data-testid="text-html">""" +
            """<p><h3><span>Trending</span></h3></p></div>"""

    private val preferredSourceBadge =
        """<p><a href="https://www.google.com/preferences/source?q=westhawaiitoday.com">""" +
            """<img src="https://example.com/google_preferred_source_badge_light_en.png" """ +
            """alt="Add as a preferred source on Google"></a></p>"""

    @Test
    fun `removes both kinds of litter from one body`() {
        val html = "$preferredSourceBadge<p>Real sentence.</p>$recirculationWidget"

        val cleaned = ArticleLitter.strip(html)!!

        assertFalse("badge survived: $cleaned", cleaned.contains("preferences/source"))
        assertFalse("widget survived: $cleaned", cleaned.contains("Trending"))
        assertTrue(cleaned.contains("Real sentence."))
    }

    @Test
    fun `leaves a clean article untouched`() {
        val html = "<p>Nothing to clean.</p>"

        assertEquals(html, ArticleLitter.strip(html))
    }

    @Test
    fun `copes with nothing`() {
        assertEquals(null, ArticleLitter.strip(null))
    }
}