Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 1812030b authored by Brian Isganitis's avatar Brian Isganitis Committed by Android (Google) Code Review
Browse files

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

parents 4d1c6e55 a07a7c2f
Loading
Loading
Loading
Loading
+8 −9
Original line number Diff line number Diff line
@@ -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}"),
                )
+2 −1
Original line number Diff line number Diff line
@@ -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
+2 −1
Original line number Diff line number Diff line
@@ -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