1 package android.platform.test.rule 2 3 import android.platform.test.rule.Orientation.LANDSCAPE 4 import android.platform.test.rule.Orientation.PORTRAIT 5 import org.junit.rules.TestRule 6 import org.junit.runner.Description 7 import org.junit.runners.model.Statement 8 9 /** 10 * Makes each test of the class that uses this rule execute twice, in [Orientation.LANDSCAPE] and 11 * [Orientation.PORTRAIT] orientation. 12 */ 13 class PortraitLandscapeRule( 14 private val portraitDeviceFilter: List<DeviceTypeFilter> = listOf(DeviceTypeFilter.ANY), 15 private val landscapeDeviceFilter: List<DeviceTypeFilter> = listOf(DeviceTypeFilter.ANY) 16 ) : TestRule { 17 applynull18 override fun apply(base: Statement, description: Description): Statement = 19 object : Statement() { 20 override fun evaluate() { 21 try { 22 if (portraitDeviceFilter.any { it.match() }) { 23 base.runInOrientation(PORTRAIT) 24 } 25 if (landscapeDeviceFilter.any { it.match() }) { 26 base.runInOrientation(LANDSCAPE) 27 } 28 } finally { 29 RotationUtils.clearOrientationOverride() 30 } 31 } 32 } 33 Statementnull34 private fun Statement.runInOrientation(orientation: Orientation) { 35 RotationUtils.setOrientationOverride(orientation) 36 try { 37 evaluate() 38 } catch (e: Throwable) { 39 throw Exception("Test failed while in $orientation", e) 40 } 41 } 42 } 43