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

Commit 320804e0 authored by Julia Tuttle's avatar Julia Tuttle Committed by Android (Google) Code Review
Browse files

Merge "Convert ViewStateTest to use assert{DoesNotLog,Logs}Wtf" into main

parents cd36b3c3 6ce5ba51
Loading
Loading
Loading
Loading
+16 −41
Original line number Diff line number Diff line
@@ -17,10 +17,10 @@
package com.android.systemui.statusbar.notification.stack

import android.testing.AndroidTestingRunner
import android.util.Log
import android.util.Log.TerribleFailureHandler
import androidx.test.filters.SmallTest
import com.android.systemui.SysuiTestCase
import com.android.systemui.log.assertDoesNotLogWtf
import com.android.systemui.log.assertLogsWtf
import kotlin.math.log2
import kotlin.math.sqrt
import org.junit.Assert
@@ -32,61 +32,36 @@ import org.junit.runner.RunWith
class ViewStateTest : SysuiTestCase() {
    private val viewState = ViewState()

    private var wtfHandler: TerribleFailureHandler? = null
    private var wtfCount = 0

    @Suppress("DIVISION_BY_ZERO")
    @Test
    fun testWtfs() {
        interceptWtfs()

        // Setting valid values doesn't cause any wtfs.
        assertDoesNotLogWtf {
            viewState.alpha = 0.1f
            viewState.xTranslation = 0f
            viewState.yTranslation = 10f
            viewState.zTranslation = 20f
            viewState.scaleX = 0.5f
            viewState.scaleY = 0.25f

        expectWtfs(0)
        }

        // Setting NaN values leads to wtfs being logged, and the value not being changed.
        viewState.alpha = 0.0f / 0.0f
        expectWtfs(1)
        assertLogsWtf { viewState.alpha = 0.0f / 0.0f }
        Assert.assertEquals(viewState.alpha, 0.1f)

        viewState.xTranslation = Float.NaN
        expectWtfs(2)
        assertLogsWtf { viewState.xTranslation = Float.NaN }
        Assert.assertEquals(viewState.xTranslation, 0f)

        viewState.yTranslation = log2(-10.0).toFloat()
        expectWtfs(3)
        assertLogsWtf { viewState.yTranslation = log2(-10.0).toFloat() }
        Assert.assertEquals(viewState.yTranslation, 10f)

        viewState.zTranslation = sqrt(-1.0).toFloat()
        expectWtfs(4)
        assertLogsWtf { viewState.zTranslation = sqrt(-1.0).toFloat() }
        Assert.assertEquals(viewState.zTranslation, 20f)

        viewState.scaleX = Float.POSITIVE_INFINITY + Float.NEGATIVE_INFINITY
        expectWtfs(5)
        assertLogsWtf { viewState.scaleX = Float.POSITIVE_INFINITY + Float.NEGATIVE_INFINITY }
        Assert.assertEquals(viewState.scaleX, 0.5f)

        viewState.scaleY = Float.POSITIVE_INFINITY * 0
        expectWtfs(6)
        assertLogsWtf { viewState.scaleY = Float.POSITIVE_INFINITY * 0 }
        Assert.assertEquals(viewState.scaleY, 0.25f)
    }

    private fun interceptWtfs() {
        wtfCount = 0
        wtfHandler =
            Log.setWtfHandler { _: String?, e: Log.TerribleFailure, _: Boolean ->
                Log.e("ViewStateTest", "Observed WTF: $e")
                wtfCount++
            }
    }

    private fun expectWtfs(expectedWtfCount: Int) {
        Assert.assertNotNull(wtfHandler)
        Assert.assertEquals(expectedWtfCount, wtfCount)
    }
}
+23 −0
Original line number Diff line number Diff line
@@ -20,6 +20,29 @@ import android.util.Log
import android.util.Log.TerribleFailureHandler
import junit.framework.Assert

/** Asserts that the given block does not make a call to Log.wtf */
fun assertDoesNotLogWtf(
    message: String = "Expected Log.wtf not to be called",
    notLoggingBlock: () -> Unit,
) {
    var caught: TerribleFailureLog? = null
    val newHandler = TerribleFailureHandler { tag, failure, system ->
        caught = TerribleFailureLog(tag, failure, system)
    }
    val oldHandler = Log.setWtfHandler(newHandler)
    try {
        notLoggingBlock()
    } finally {
        Log.setWtfHandler(oldHandler)
    }
    caught?.let { throw AssertionError("$message: $it", it.failure) }
}

fun assertDoesNotLogWtf(
    message: String = "Expected Log.wtf not to be called",
    notLoggingRunnable: Runnable,
) = assertDoesNotLogWtf(message = message) { notLoggingRunnable.run() }

/**
 * Assert that the given block makes a call to Log.wtf
 *