1 /* 2 * Copyright 2024 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 androidx.camera.testing.impl 18 19 import android.app.Activity 20 import androidx.camera.camera2.pipe.integration.CameraPipeConfig 21 import androidx.camera.core.CameraXConfig 22 import androidx.camera.core.Logger 23 import androidx.test.core.app.ActivityScenario 24 import org.junit.Assume.assumeTrue 25 26 /** 27 * Utility object to hold the convenience functions for internal testing. 28 * 29 * This should never be publicly available directly. 30 */ 31 public object InternalTestConvenience { 32 public const val LOG_TAG: String = "InternalTestConvenience" 33 34 /** 35 * Executes the given [block] function on this [ActivityScenario] resource and finally closes it 36 * without throwing in some cases for the convenience of camera tests. 37 * 38 * [ActivityScenario.close] may throw an exception in some cases due to bugs usually unrelated 39 * to a camera test. This function suppresses the exceptions in such cases for the convenience 40 * of tests where issues related to clearing up resources aren't related. 41 * 42 * @param block a function to process this resource. 43 * @return the result of [block] function invoked on this resource. 44 */ useInCameraTestnull45 public inline fun <A : Activity, R> ActivityScenario<A>.useInCameraTest( 46 block: (ActivityScenario<A>) -> R, 47 ): R { 48 try { 49 return block(this) 50 } finally { 51 try { 52 close() 53 } catch (e: Throwable) { 54 if (AndroidUtil.isEmulator(28)) { 55 Logger.w(LOG_TAG, "Suppressed exception from ActivityScenario.close()", e) 56 } else { 57 // rethrow in case it's not a known issue 58 throw e 59 } 60 } 61 } 62 } 63 64 /** 65 * Ignores a test for CameraPipe config, only outside CameraX lab test by default. 66 * 67 * The default behavior is not to ignore in lab environment so that the true failure rate can be 68 * tracked in CameraX internal dashboards without blocking in other environments like AndroidX 69 * presubmit. 70 * 71 * @param message A message to pass to [assumeTrue]. 72 * @param evenInLab False by default, does not ignore the test if it's running in CameraX lab 73 * environment. If true, this behavior is overridden and ignored in lab environment as well. 74 * @receiver A `String` that represents the name of the [CameraXConfig] used in CameraX tests, 75 * e.g. the `CameraPipeConfig::class.simpleName` or `Camera2Config::class.simpleName`. 76 */ ignoreTestForCameraPipenull77 public fun String.ignoreTestForCameraPipe(message: String, evenInLab: Boolean = false) { 78 if (!LabTestRule.isInLabTest() || evenInLab) { 79 assumeTrue(message, !this.contains(CameraPipeConfig::class.simpleName.toString())) 80 } 81 } 82 } 83