Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a0790d15 authored by João Henrique's avatar João Henrique
Browse files

test: add test for global style rules in DisplayHtmlTest

This commit adds a test to `DisplayHtmlTest.kt` to verify that the `wrapMessageContent` function correctly adds global CSS style rules. Specifically, it checks for the presence of `word-break: break-word;` and `overflow-wrap: break-word;` rules applied to the `*` selector.

A helper assertion function `containsStyleRulesFor` is also introduced to facilitate checking CSS rules within the HTML output.
parent 1d869bd4
Loading
Loading
Loading
Loading
+30 −0
Original line number Original line Diff line number Diff line
@@ -2,6 +2,7 @@ package com.fsck.k9.message.html


import assertk.Assert
import assertk.Assert
import assertk.assertThat
import assertk.assertThat
import assertk.assertions.contains
import assertk.assertions.hasSize
import assertk.assertions.hasSize
import assertk.assertions.isEqualTo
import assertk.assertions.isEqualTo
import org.jsoup.Jsoup
import org.jsoup.Jsoup
@@ -40,6 +41,35 @@ class DisplayHtmlTest {
        assertThat(html).bodyText().isEqualTo(content)
        assertThat(html).bodyText().isEqualTo(content)
    }
    }


    @Test
    fun wrapMessageContent_addsGlobalStyleRules() {
        val html = displayHtml.wrapMessageContent("test")

        assertThat(html).containsStyleRulesFor(
            selector = "*",
            "word-break: break-word;",
            "overflow-wrap: break-word;",
        )
    }

    private fun Assert<String>.containsStyleRulesFor(selector: String, vararg expectedRules: String) = given { html ->
        val styleContent = Jsoup.parse(html)
            .select("style")
            .joinToString("\n") { it.data() }

        val selectorPattern = Regex.escape(selector).replace("\\*", "\\\\*")
        val selectorBlock = Regex("$selectorPattern\\s*\\{([^}]*)\\}", RegexOption.MULTILINE)
            .find(styleContent)
            ?.groupValues?.get(1)
            ?.trim()

        requireNotNull(selectorBlock) { "No style block found for selector: $selector" }

        expectedRules.forEach { rule ->
            assertThat(selectorBlock).contains(rule)
        }
    }

    private fun Assert<String>.containsHtmlElement(cssQuery: String) = given { actual ->
    private fun Assert<String>.containsHtmlElement(cssQuery: String) = given { actual ->
        assertThat(actual).htmlElements(cssQuery).hasSize(1)
        assertThat(actual).htmlElements(cssQuery).hasSize(1)
    }
    }