diff options
| -rw-r--r-- | packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/stack/ViewStateTest.kt | 57 | ||||
| -rw-r--r-- | packages/SystemUI/tests/utils/src/com/android/systemui/log/LogAssert.kt | 23 |
2 files changed, 39 insertions, 41 deletions
diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/stack/ViewStateTest.kt b/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/stack/ViewStateTest.kt index da543d4454b8..cd6bb5f4966a 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/stack/ViewStateTest.kt +++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/stack/ViewStateTest.kt @@ -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. - viewState.alpha = 0.1f - viewState.xTranslation = 0f - viewState.yTranslation = 10f - viewState.zTranslation = 20f - viewState.scaleX = 0.5f - viewState.scaleY = 0.25f - - expectWtfs(0) + assertDoesNotLogWtf { + viewState.alpha = 0.1f + viewState.xTranslation = 0f + viewState.yTranslation = 10f + viewState.zTranslation = 20f + viewState.scaleX = 0.5f + viewState.scaleY = 0.25f + } // 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) - } } diff --git a/packages/SystemUI/tests/utils/src/com/android/systemui/log/LogAssert.kt b/packages/SystemUI/tests/utils/src/com/android/systemui/log/LogAssert.kt index 6ccb3bc2812e..5e67182d7353 100644 --- a/packages/SystemUI/tests/utils/src/com/android/systemui/log/LogAssert.kt +++ b/packages/SystemUI/tests/utils/src/com/android/systemui/log/LogAssert.kt @@ -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 * |