~ knotless Search/
↑↓ move↵ open⇧↵ all results

grain.social/grain-android

1 collaborator
0
1
main
61 lines · 2.4 KB Raw History
package social.grain

import androidx.test.ext.junit.runners.AndroidJUnit4
import kotlinx.serialization.json.jsonArray
import kotlinx.serialization.json.jsonObject
import kotlinx.serialization.json.jsonPrimitive
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test
import org.junit.runner.RunWith
import social.grain.support.appContext
import social.grain.support.testJson
import social.grain.ui.components.MapSupport

/**
 * The generated style assets, as the app will read them.
 *
 * tools/basemap writes these and they are committed, so the thing to pin is
 * that what was committed still points where it should: a regenerate against
 * a changed theme package that dropped the tile source or the glyphs would
 * otherwise ship a blank map.
 */
@RunWith(AndroidJUnit4::class)
class BasemapStyleTest {

    private fun style(theme: String) = appContext.assets.open("basemap/$theme.json")
        .bufferedReader()
        .use { testJson.parseToJsonElement(it.readText()).jsonObject }

    @Test
    fun bothThemesDrawGrainsOwnTilesWithLabels() {
        for (theme in listOf("light", "dark")) {
            val style = style(theme)
            assertEquals(8, style["version"]?.jsonPrimitive?.content?.toInt())

            val source = style["sources"]!!.jsonObject["protomaps"]!!.jsonObject
            assertEquals("vector", source["type"]?.jsonPrimitive?.content)
            assertEquals(
                "https://tiles.grain.social/planet/{z}/{x}/{y}.mvt",
                source["tiles"]!!.jsonArray.single().jsonPrimitive.content,
            )
            assertTrue(source["attribution"]?.jsonPrimitive?.content!!.contains("OpenStreetMap"))

            // MapLibre Native draws no text without a glyph endpoint.
            assertTrue(style["glyphs"]?.jsonPrimitive?.content!!.contains("{fontstack}"))

            val layers = style["layers"]!!.jsonArray
            assertTrue(layers.size > 50)
            assertTrue(layers.any { it.jsonObject["type"]?.jsonPrimitive?.content == "symbol" })
            assertTrue(layers.all { it.jsonObject["source"]?.jsonPrimitive?.content in setOf(null, "protomaps") })
        }
    }

    @Test
    fun theRendererIsNotAvailableOffADevice() {
        // What the screens rely on to leave the map out here, rather than
        // crash trying to load a native library the host doesn't have.
        assertFalse(MapSupport.isAvailable)
    }
}