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

Commit 6e88b696 authored by Tyler Freeman's avatar Tyler Freeman Committed by Android (Google) Code Review
Browse files

Merge "fix(non linear font scaling): fix test from timing out." into udc-dev

parents 265f1696 0ae54f47
Loading
Loading
Loading
Loading
+36 −3
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@ import kotlin.math.ceil
import kotlin.math.floor
import org.junit.Test
import org.junit.runner.RunWith
import kotlin.random.Random.Default.nextFloat

@Presubmit
@RunWith(AndroidJUnit4::class)
@@ -124,10 +125,12 @@ class FontScaleConverterFactoryTest {
    @LargeTest
    @Test
    fun allFeasibleScalesAndConversionsDoNotCrash() {
        generateSequenceOfFractions(-10f..10f, step = 0.01f)
            .mapNotNull{ FontScaleConverterFactory.forScale(it) }!!
        generateSequenceOfFractions(-10f..10f, step = 0.1f)
            .fuzzFractions()
            .mapNotNull{ FontScaleConverterFactory.forScale(it) }
            .flatMap{ table ->
                generateSequenceOfFractions(-2000f..2000f, step = 0.01f)
                generateSequenceOfFractions(-2000f..2000f, step = 0.1f)
                    .fuzzFractions()
                    .map{ Pair(table, it) }
            }
            .forEach { (table, sp) ->
@@ -172,6 +175,30 @@ class FontScaleConverterFactoryTest {
        assertThat(fractions).doesNotContain(-.35f)
    }

    @Test
    fun testFuzzFractions() {
        val numFuzzedFractions = 6
        val fractions = generateSequenceOfFractions(-1000f..1000f, step = 0.1f)
            .fuzzFractions()
            .toList()
        fractions.forEach {
            assertThat(it).isAtLeast(-1000f)
            assertThat(it).isLessThan(1001f)
        }

        val numGeneratedFractions = 1000 * 2 * 10 + 1 // Don't forget the 0 in the middle!
        assertThat(fractions).hasSize(numGeneratedFractions * numFuzzedFractions)

        assertThat(fractions).contains(100f)
        assertThat(fractions).contains(500.1f)
        assertThat(fractions).contains(500.2f)
        assertThat(fractions).contains(0.2f)
        assertThat(fractions).contains(0f)
        assertThat(fractions).contains(-10f)
        assertThat(fractions).contains(-10f)
        assertThat(fractions).contains(-10.3f)
    }

    companion object {
        private const val CONVERSION_TOLERANCE = 0.05f
    }
@@ -188,3 +215,9 @@ fun generateSequenceOfFractions(
        .takeWhile { it <= endInclusive }
        .map{ it.toFloat() / multiplier }
}

private fun Sequence<Float>.fuzzFractions(): Sequence<Float> {
    return flatMap { i ->
        listOf(i, i + 0.01f, i + 0.054f, i + 0.099f, i + nextFloat(), i + nextFloat())
    }
}