summaryrefslogtreecommitdiff
path: root/app/src/test/java/org/terst/feedreader/ui/ReaderMathTest.kt
blob: fea5d22c246f182775a40771fbca84729a38acef (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
package org.terst.feedreader.ui

import org.junit.Assert.assertTrue
import org.junit.Test

/**
 * The reader's end of maths rendering.
 *
 * Extraction converts a page's TeX source to MathML (see ReaderExtractor), which the
 * WebView renders natively -- no script, no webfont, no stylesheet of KaTeX's. Two things
 * still have to be true of the reader HTML itself: the MathML has to reach the WebView
 * intact (the wrapping passes rewrite iframes and images, and must not touch it), and a
 * wide equation must not be able to drag the whole article into horizontal scrolling --
 * an aligned multi-line derivation is routinely wider than a phone screen, and `math`
 * does not obey the `img`/`svg` clamps already in the stylesheet.
 */
class ReaderMathTest {

    private val displayMath =
        "<math xmlns=\"http://www.w3.org/1998/Math/MathML\" display=\"block\"><semantics>" +
            "<mtable><mtr><mtd><mi>t</mi></mtd></mtr></mtable></semantics></math>"

    @Test
    fun `mathml reaches the webview unchanged`() {
        val html = wrapReaderHtml("<p>A slice of the set:</p>$displayMath<p>and so on.</p>")
        assertTrue("MathML must survive the reader's HTML assembly verbatim", html.contains(displayMath))
    }

    @Test
    fun `a wide equation scrolls inside itself instead of scrolling the article`() {
        val html = wrapReaderHtml("<p>x</p>$displayMath")
        val style = html.substringAfter("<style>").substringBefore("</style>")
        val displayMathRule = style.lines()
            .joinToString(" ")
            .substringAfter("math[display=\"block\"]", "")
            .substringBefore("}")
        assertTrue(
            "the stylesheet needs a math[display=\"block\"] rule, found none in:\n$style",
            displayMathRule.isNotBlank()
        )
        assertTrue(
            "display maths must scroll within its own box, got: $displayMathRule",
            displayMathRule.contains("overflow-x: auto")
        )
    }
}