Created
April 12, 2019 10:09
-
-
Save marcosholgado/3ecdcbdef82f2ccba8ede33ed30704e6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| open class ActivityPerfTestRule<T: Activity>(activityClass: Class<T>): | |
| ActivityTestRule<T>(activityClass) { | |
| private var monitor: IMonitor? = null | |
| private var annotation: PerformanceTest? = null | |
| init { | |
| if (API_LEVEL_ACTUAL <= 22) { | |
| error("Not supported by current platform.") | |
| } | |
| } | |
| override fun apply(base: Statement?, description: Description?): Statement { | |
| annotation = description?.getAnnotation(PerformanceTest::class.java) | |
| annotation?.let { | |
| monitor = PerfMonitor(InstrumentationRegistry.getInstrumentation(), | |
| it.processName) | |
| } | |
| return super.apply(base, description) | |
| } | |
| override fun beforeActivityLaunched() { | |
| monitor?.startIteration() | |
| super.beforeActivityLaunched() | |
| } | |
| override fun afterActivityFinished() { | |
| monitor?.let { | |
| val results = it.stopIteration() | |
| val res: Double = results?.get(annotation?.perfType?.type) as Double | |
| val assertion = when(annotation?.assertionType) { | |
| PerformanceTest.AssertionType.LESS -> res < annotation!!.threshold | |
| PerformanceTest.AssertionType.LESS_OR_EQUAL -> res <= annotation!!.threshold | |
| PerformanceTest.AssertionType.GREATER -> res > annotation!!.threshold | |
| PerformanceTest.AssertionType.GREATER_OR_EQUAL -> res >= annotation!!.threshold | |
| PerformanceTest.AssertionType.EQUAL -> res == annotation!!.threshold.toDouble() | |
| null -> false | |
| } | |
| TestCase.assertTrue( | |
| String.format( | |
| "Monitor: %s, Expected: %d, Received: %f.", | |
| annotation?.perfType?.type, annotation!!.threshold, | |
| res | |
| ), | |
| assertion | |
| ) | |
| } | |
| super.afterActivityFinished() | |
| } | |
| companion object { | |
| internal val API_LEVEL_ACTUAL = | |
| Build.VERSION.SDK_INT + if ("REL" == Build.VERSION.CODENAME) 0 else 1 | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment