1 /* <lambda>null2 * Copyright (C) 2023 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.systemui.log 18 19 import android.util.Log 20 import android.util.Log.TerribleFailureHandler 21 import org.junit.rules.TestRule 22 import org.junit.runner.Description 23 import org.junit.runners.model.Statement 24 25 class LogWtfHandlerRule : TestRule { 26 27 private var started = false 28 private var handler = ThrowAndFailAtEnd 29 30 override fun apply(base: Statement, description: Description): Statement { 31 return object : Statement() { 32 override fun evaluate() { 33 started = true 34 val originalWtfHandler = Log.setWtfHandler(handler) 35 var failure: Throwable? = null 36 try { 37 base.evaluate() 38 } catch (ex: Throwable) { 39 failure = ex.runAndAddSuppressed { handler.onTestFailure(ex) } 40 } finally { 41 failure = failure.runAndAddSuppressed { handler.onTestFinished() } 42 Log.setWtfHandler(originalWtfHandler) 43 } 44 if (failure != null) { 45 throw failure 46 } 47 } 48 } 49 } 50 51 fun Throwable?.runAndAddSuppressed(block: () -> Unit): Throwable? { 52 try { 53 block() 54 } catch (t: Throwable) { 55 if (this == null) { 56 return t 57 } 58 addSuppressed(t) 59 } 60 return this 61 } 62 63 fun setWtfHandler(handler: TerribleFailureTestHandler) { 64 check(!started) { "Should only be called before the test starts" } 65 this.handler = handler 66 } 67 68 fun interface TerribleFailureTestHandler : TerribleFailureHandler { 69 fun onTestFailure(failure: Throwable) {} 70 fun onTestFinished() {} 71 } 72 73 companion object Handlers { 74 val ThrowAndFailAtEnd 75 get() = 76 object : TerribleFailureTestHandler { 77 val failures = mutableListOf<Log.TerribleFailure>() 78 79 override fun onTerribleFailure( 80 tag: String, 81 what: Log.TerribleFailure, 82 system: Boolean 83 ) { 84 failures.add(what) 85 throw what 86 } 87 88 override fun onTestFailure(failure: Throwable) { 89 super.onTestFailure(failure) 90 } 91 92 override fun onTestFinished() { 93 if (failures.isNotEmpty()) { 94 throw AssertionError("Unexpected Log.wtf calls: $failures", failures[0]) 95 } 96 } 97 } 98 99 val JustThrow = TerribleFailureTestHandler { _, what, _ -> throw what } 100 101 val JustFailAtEnd 102 get() = 103 object : TerribleFailureTestHandler { 104 val failures = mutableListOf<Log.TerribleFailure>() 105 106 override fun onTerribleFailure( 107 tag: String, 108 what: Log.TerribleFailure, 109 system: Boolean 110 ) { 111 failures.add(what) 112 } 113 114 override fun onTestFinished() { 115 if (failures.isNotEmpty()) { 116 throw AssertionError("Unexpected Log.wtf calls: $failures", failures[0]) 117 } 118 } 119 } 120 } 121 } 122