Merge "Take in test instance as field." into main

This commit is contained in:
Brian Isganitis
2024-06-25 23:02:32 +00:00
committed by Android (Google) Code Review
3 changed files with 12 additions and 11 deletions
@@ -31,14 +31,15 @@ import com.android.quickstep.AllAppsActionManager
import com.android.quickstep.TouchInteractionService
import com.android.quickstep.TouchInteractionService.TISBinder
import org.junit.Assume.assumeTrue
import org.junit.rules.MethodRule
import org.junit.runners.model.FrameworkMethod
import org.junit.rules.TestRule
import org.junit.runner.Description
import org.junit.runners.model.Statement
/**
* Manages the Taskbar lifecycle for unit tests.
*
* Tests need to provide their target [context] through the constructor.
* Tests should pass in themselves as [testInstance]. They also need to provide their target
* [context] through the constructor.
*
* See [InjectController] for grabbing controller(s) under test with minimal boilerplate.
*
@@ -61,12 +62,11 @@ import org.junit.runners.model.Statement
* }
* ```
*/
class TaskbarUnitTestRule(private val context: Context) : MethodRule {
class TaskbarUnitTestRule(private val testInstance: Any, private val context: Context) : TestRule {
private val instrumentation = InstrumentationRegistry.getInstrumentation()
private val serviceTestRule = ServiceTestRule()
private lateinit var taskbarManager: TaskbarManager
private lateinit var target: Any
val activityContext: TaskbarActivityContext
get() {
@@ -74,10 +74,9 @@ class TaskbarUnitTestRule(private val context: Context) : MethodRule {
?: throw RuntimeException("Failed to obtain TaskbarActivityContext.")
}
override fun apply(base: Statement, method: FrameworkMethod, target: Any): Statement {
override fun apply(base: Statement, description: Description): Statement {
return object : Statement() {
override fun evaluate() {
this@TaskbarUnitTestRule.target = target
instrumentation.runOnMainSync {
assumeTrue(
@@ -141,11 +140,11 @@ class TaskbarUnitTestRule(private val context: Context) : MethodRule {
private fun injectControllers() {
val controllers = activityContext.controllers
val controllerFieldsByType = controllers.javaClass.fields.associateBy { it.type }
target.javaClass.fields
testInstance.javaClass.fields
.filter { it.isAnnotationPresent(InjectController::class.java) }
.forEach {
it.set(
target,
testInstance,
controllerFieldsByType[it.type]?.get(controllers)
?: throw NoSuchElementException("Failed to find controller for ${it.type}"),
)
@@ -42,7 +42,8 @@ import org.junit.runner.RunWith
@EmulatedDevices(["pixelFoldable2023", "pixelTablet2023"])
class TaskbarAllAppsControllerTest {
@get:Rule val taskbarUnitTestRule = TaskbarUnitTestRule(getInstrumentation().targetContext)
@get:Rule
val taskbarUnitTestRule = TaskbarUnitTestRule(this, getInstrumentation().targetContext)
@get:Rule val animatorTestRule = AnimatorTestRule(this)
@InjectController lateinit var allAppsController: TaskbarAllAppsController
@@ -41,7 +41,8 @@ import org.junit.runner.RunWith
@EmulatedDevices(["pixelFoldable2023"])
class TaskbarOverlayControllerTest {
@get:Rule val taskbarUnitTestRule = TaskbarUnitTestRule(getInstrumentation().targetContext)
@get:Rule
val taskbarUnitTestRule = TaskbarUnitTestRule(this, getInstrumentation().targetContext)
@InjectController lateinit var overlayController: TaskbarOverlayController
private val taskbarContext: TaskbarActivityContext