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

Commit 42927577 authored by Graciela Putri's avatar Graciela Putri
Browse files

Add ktfmt for tests in shell/compatui

Flag: EXEMPT PURE_REFACTOR
Bug: 432663954
Test: NA
Change-Id: I61d8ce9ea1a845c2da5968c4480b8bae17ffcb7a
parent f07841b1
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -20,7 +20,7 @@ clang_format = --commit ${PREUPLOAD_COMMIT} --style file --extensions c,h,cc,cpp
               tests/
               tools/
bpfmt = -d
ktfmt = --kotlinlang-style --include-dirs=services/permission,packages/SystemUI,libs/WindowManager/Shell/src/com/android/wm/shell/freeform,libs/WindowManager/Shell/src/com/android/wm/shell/desktopmode,libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/desktopmode,libs/WindowManager/Shell/src/com/android/wm/shell/windowdecor,libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/windowdecor,libs/WindowManager/Shell/multivalentTests/src/com/android/wm/shell/desktopmode,apct-tests,tests/Input,tests/StructuredConcurrencyPerfTests,services/tests/servicestests/src/com/android/server/supervision,libs/WindowManager/Shell/src/com/android/wm/shell/compatui
ktfmt = --kotlinlang-style --include-dirs=services/permission,packages/SystemUI,libs/WindowManager/Shell/src/com/android/wm/shell/freeform,libs/WindowManager/Shell/src/com/android/wm/shell/desktopmode,libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/desktopmode,libs/WindowManager/Shell/src/com/android/wm/shell/windowdecor,libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/windowdecor,libs/WindowManager/Shell/multivalentTests/src/com/android/wm/shell/desktopmode,apct-tests,tests/Input,tests/StructuredConcurrencyPerfTests,services/tests/servicestests/src/com/android/server/supervision,libs/WindowManager/Shell/src/com/android/wm/shell/compatui,libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/compatui
google_java_format = --include-dirs=services/core/java/com/android/server/adb,tests/AppJankTest

[Hook Scripts]
+7 −10
Original line number Diff line number Diff line
@@ -38,8 +38,7 @@ import org.mockito.kotlin.mock
/**
 * Tests for {@link CompatUIComponent}.
 *
 * Build/Install/Run:
 *  atest WMShellUnitTests:CompatUIComponentTest
 * Build/Install/Run: atest WMShellUnitTests:CompatUIComponentTest
 */
@RunWith(AndroidTestingRunner::class)
@SmallTest
@@ -56,9 +55,7 @@ class CompatUIComponentTest : ShellTestCase() {
    private lateinit var position: Point
    private lateinit var componentState: CompatUIComponentState

    @JvmField
    @Rule
    val compatUIHandlerRule: CompatUIHandlerRule = CompatUIHandlerRule()
    @JvmField @Rule val compatUIHandlerRule: CompatUIHandlerRule = CompatUIHandlerRule()

    @Before
    fun setUp() {
@@ -71,22 +68,22 @@ class CompatUIComponentTest : ShellTestCase() {
        syncQueue = mock<SyncTransactionQueue>()
        displayLayout = mock<DisplayLayout>()
        component =
            CompatUIComponent(spec.getSpec(),
            CompatUIComponent(
                spec.getSpec(),
                "compId",
                mContext,
                state,
                info,
                syncQueue,
                displayLayout)
                displayLayout,
            )
        componentState = object : CompatUIComponentState {}
        state.registerUIComponent("compId", component, componentState)
    }

    @Test
    fun `when initLayout is invoked spec fields are used`() {
        compatUIHandlerRule.postBlocking {
            component.initLayout(info)
        }
        compatUIHandlerRule.postBlocking { component.initLayout(info) }
        with(layout) {
            assertViewBuilderInvocation(1)
            assertEquals(info, lastViewBuilderCompatUIInfo)
+6 −11
Original line number Diff line number Diff line
@@ -22,20 +22,14 @@ import org.junit.rules.TestRule
import org.junit.runner.Description
import org.junit.runners.model.Statement

/**
 * Utility {@link TestRule} to manage Handlers in Compat UI tests.
 */
/** Utility {@link TestRule} to manage Handlers in Compat UI tests. */
class CompatUIHandlerRule : TestRule {

    private lateinit var handler: HandlerThread

    /**
     * Makes the HandlerThread available during the test
     */
    /** Makes the HandlerThread available during the test */
    override fun apply(base: Statement?, description: Description?): Statement {
        handler = HandlerThread("CompatUIHandler").apply {
            start()
        }
        handler = HandlerThread("CompatUIHandler").apply { start() }
        return object : Statement() {
            @Throws(Throwable::class)
            override fun evaluate() {
@@ -50,6 +44,7 @@ class CompatUIHandlerRule : TestRule {

    /**
     * Posts a {@link Runnable} for the Handler
     *
     * @param runnable The Runnable to execute
     */
    fun postBlocking(runnable: Runnable) {
+6 −15
Original line number Diff line number Diff line
@@ -22,29 +22,20 @@ import junit.framework.Assert.assertEquals
import junit.framework.Assert.assertNotNull
import junit.framework.Assert.assertNull

/**
 * Asserts no component state exists for the given CompatUISpec
 */
/** Asserts no component state exists for the given CompatUISpec */
internal fun CompatUIState.assertHasNoStateFor(componentId: String) =
    assertNull(stateForComponent(componentId))

/**
 * Asserts component state for the given CompatUISpec
 */
/** Asserts component state for the given CompatUISpec */
internal fun CompatUIState.assertHasStateEqualsTo(
    componentId: String,
    expected: CompatUIComponentState
) =
    assertEquals(stateForComponent(componentId), expected)
    expected: CompatUIComponentState,
) = assertEquals(stateForComponent(componentId), expected)

/**
 * Asserts no component exists for the given CompatUISpec
 */
/** Asserts no component exists for the given CompatUISpec */
internal fun CompatUIState.assertHasNoComponentFor(componentId: String) =
    assertNull(getUIComponent(componentId))

/**
 * Asserts component for the given CompatUISpec
 */
/** Asserts component for the given CompatUISpec */
internal fun CompatUIState.assertHasComponentFor(componentId: String) =
    assertNotNull(getUIComponent(componentId))
+46 −86
Original line number Diff line number Diff line
@@ -23,7 +23,6 @@ import androidx.test.filters.SmallTest
import com.android.wm.shell.ShellTestCase
import com.android.wm.shell.TestShellExecutor
import com.android.wm.shell.common.DisplayController
import com.android.wm.shell.common.ShellExecutor
import com.android.wm.shell.common.SyncTransactionQueue
import com.android.wm.shell.compatui.api.CompatUIComponentState
import com.android.wm.shell.compatui.api.CompatUIInfo
@@ -37,16 +36,13 @@ import org.mockito.kotlin.mock
/**
 * Tests for {@link DefaultCompatUIHandler}.
 *
 * Build/Install/Run:
 *  atest WMShellUnitTests:DefaultCompatUIHandlerTest
 * Build/Install/Run: atest WMShellUnitTests:DefaultCompatUIHandlerTest
 */
@RunWith(AndroidTestingRunner::class)
@SmallTest
class DefaultCompatUIHandlerTest : ShellTestCase() {

    @JvmField
    @Rule
    val compatUIHandlerRule: CompatUIHandlerRule = CompatUIHandlerRule()
    @JvmField @Rule val compatUIHandlerRule: CompatUIHandlerRule = CompatUIHandlerRule()

    lateinit var compatUIRepository: FakeCompatUIRepository
    lateinit var compatUIHandler: DefaultCompatUIHandler
@@ -72,28 +68,24 @@ class DefaultCompatUIHandlerTest : ShellTestCase() {
                compatUIState,
                fakeIdGenerator,
                componentFactory,
                shellExecutor)
                shellExecutor,
            )
    }

    @Test
    fun `when creationReturn is false no state is stored`() {
        // We add a spec to the repository
        val fakeLifecycle = FakeCompatUILifecyclePredicates(
            creationReturn = false,
            removalReturn = false
        )
        val fakeLifecycle =
            FakeCompatUILifecyclePredicates(creationReturn = false, removalReturn = false)
        val fakeCompatUILayout = FakeCompatUILayout(viewBuilderReturn = View(mContext))
        val fakeCompatUISpec =
            FakeCompatUISpec(name = "one",
                lifecycle = fakeLifecycle,
                layout = fakeCompatUILayout).getSpec()
            FakeCompatUISpec(name = "one", lifecycle = fakeLifecycle, layout = fakeCompatUILayout)
                .getSpec()
        compatUIRepository.addSpec(fakeCompatUISpec)

        val generatedId = fakeIdGenerator.generatedComponentId

        compatUIHandlerRule.postBlocking {
            compatUIHandler.onCompatInfoChanged(testCompatUIInfo())
        }
        compatUIHandlerRule.postBlocking { compatUIHandler.onCompatInfoChanged(testCompatUIInfo()) }

        fakeIdGenerator.assertGenerateInvocations(1)
        fakeLifecycle.assertCreationInvocation(1)
@@ -102,9 +94,7 @@ class DefaultCompatUIHandlerTest : ShellTestCase() {
        compatUIState.assertHasNoStateFor(generatedId)
        compatUIState.assertHasNoComponentFor(generatedId)

        compatUIHandlerRule.postBlocking {
            compatUIHandler.onCompatInfoChanged(testCompatUIInfo())
        }
        compatUIHandlerRule.postBlocking { compatUIHandler.onCompatInfoChanged(testCompatUIInfo()) }
        fakeLifecycle.assertCreationInvocation(2)
        fakeLifecycle.assertRemovalInvocation(0)
        fakeLifecycle.assertInitialStateInvocation(0)
@@ -115,22 +105,17 @@ class DefaultCompatUIHandlerTest : ShellTestCase() {
    @Test
    fun `when creationReturn is true and no state is created no state is stored`() {
        // We add a spec to the repository
        val fakeLifecycle = FakeCompatUILifecyclePredicates(
            creationReturn = true,
            removalReturn = false
        )
        val fakeLifecycle =
            FakeCompatUILifecyclePredicates(creationReturn = true, removalReturn = false)
        val fakeCompatUILayout = FakeCompatUILayout(viewBuilderReturn = View(mContext))
        val fakeCompatUISpec =
            FakeCompatUISpec(name = "one",
                lifecycle = fakeLifecycle,
                layout = fakeCompatUILayout).getSpec()
            FakeCompatUISpec(name = "one", lifecycle = fakeLifecycle, layout = fakeCompatUILayout)
                .getSpec()
        compatUIRepository.addSpec(fakeCompatUISpec)

        val generatedId = fakeIdGenerator.generatedComponentId

        compatUIHandlerRule.postBlocking {
            compatUIHandler.onCompatInfoChanged(testCompatUIInfo())
        }
        compatUIHandlerRule.postBlocking { compatUIHandler.onCompatInfoChanged(testCompatUIInfo()) }

        fakeLifecycle.assertCreationInvocation(1)
        fakeLifecycle.assertRemovalInvocation(0)
@@ -138,9 +123,7 @@ class DefaultCompatUIHandlerTest : ShellTestCase() {
        compatUIState.assertHasNoStateFor(generatedId)
        compatUIState.assertHasComponentFor(generatedId)

        compatUIHandlerRule.postBlocking {
            compatUIHandler.onCompatInfoChanged(testCompatUIInfo())
        }
        compatUIHandlerRule.postBlocking { compatUIHandler.onCompatInfoChanged(testCompatUIInfo()) }

        fakeLifecycle.assertCreationInvocation(1)
        fakeLifecycle.assertRemovalInvocation(1)
@@ -153,23 +136,21 @@ class DefaultCompatUIHandlerTest : ShellTestCase() {
    fun `when creationReturn is true and state is created state is stored`() {
        val fakeComponentState = object : CompatUIComponentState {}
        // We add a spec to the repository
        val fakeLifecycle = FakeCompatUILifecyclePredicates(
        val fakeLifecycle =
            FakeCompatUILifecyclePredicates(
                creationReturn = true,
                removalReturn = false,
            initialState = { _, _ -> fakeComponentState }
                initialState = { _, _ -> fakeComponentState },
            )
        val fakeCompatUILayout = FakeCompatUILayout(viewBuilderReturn = View(mContext))
        val fakeCompatUISpec =
            FakeCompatUISpec(name = "one",
                lifecycle = fakeLifecycle,
                layout = fakeCompatUILayout).getSpec()
            FakeCompatUISpec(name = "one", lifecycle = fakeLifecycle, layout = fakeCompatUILayout)
                .getSpec()
        compatUIRepository.addSpec(fakeCompatUISpec)

        val generatedId = fakeIdGenerator.generatedComponentId

        compatUIHandlerRule.postBlocking {
            compatUIHandler.onCompatInfoChanged(testCompatUIInfo())
        }
        compatUIHandlerRule.postBlocking { compatUIHandler.onCompatInfoChanged(testCompatUIInfo()) }

        fakeLifecycle.assertCreationInvocation(1)
        fakeLifecycle.assertRemovalInvocation(0)
@@ -177,9 +158,7 @@ class DefaultCompatUIHandlerTest : ShellTestCase() {
        compatUIState.assertHasStateEqualsTo(generatedId, fakeComponentState)
        compatUIState.assertHasComponentFor(generatedId)

        compatUIHandlerRule.postBlocking {
            compatUIHandler.onCompatInfoChanged(testCompatUIInfo())
        }
        compatUIHandlerRule.postBlocking { compatUIHandler.onCompatInfoChanged(testCompatUIInfo()) }

        fakeLifecycle.assertCreationInvocation(1)
        fakeLifecycle.assertRemovalInvocation(1)
@@ -192,23 +171,21 @@ class DefaultCompatUIHandlerTest : ShellTestCase() {
    fun `when lifecycle is complete and state is created state is stored and removed`() {
        val fakeComponentState = object : CompatUIComponentState {}
        // We add a spec to the repository
        val fakeLifecycle = FakeCompatUILifecyclePredicates(
        val fakeLifecycle =
            FakeCompatUILifecyclePredicates(
                creationReturn = true,
                removalReturn = true,
            initialState = { _, _ -> fakeComponentState }
                initialState = { _, _ -> fakeComponentState },
            )
        val fakeCompatUILayout = FakeCompatUILayout(viewBuilderReturn = View(mContext))
        val fakeCompatUISpec =
            FakeCompatUISpec(name = "one",
                lifecycle = fakeLifecycle,
                layout = fakeCompatUILayout).getSpec()
            FakeCompatUISpec(name = "one", lifecycle = fakeLifecycle, layout = fakeCompatUILayout)
                .getSpec()
        compatUIRepository.addSpec(fakeCompatUISpec)

        val generatedId = fakeIdGenerator.generatedComponentId

        compatUIHandlerRule.postBlocking {
            compatUIHandler.onCompatInfoChanged(testCompatUIInfo())
        }
        compatUIHandlerRule.postBlocking { compatUIHandler.onCompatInfoChanged(testCompatUIInfo()) }

        fakeLifecycle.assertCreationInvocation(1)
        fakeLifecycle.assertRemovalInvocation(0)
@@ -216,9 +193,7 @@ class DefaultCompatUIHandlerTest : ShellTestCase() {
        compatUIState.assertHasStateEqualsTo(generatedId, fakeComponentState)
        compatUIState.assertHasComponentFor(generatedId)

        compatUIHandlerRule.postBlocking {
            compatUIHandler.onCompatInfoChanged(testCompatUIInfo())
        }
        compatUIHandlerRule.postBlocking { compatUIHandler.onCompatInfoChanged(testCompatUIInfo()) }

        fakeLifecycle.assertCreationInvocation(1)
        fakeLifecycle.assertRemovalInvocation(1)
@@ -230,51 +205,37 @@ class DefaultCompatUIHandlerTest : ShellTestCase() {
    @Test
    fun `idGenerator is invoked every time a component is created`() {
        // We add a spec to the repository
        val fakeLifecycle = FakeCompatUILifecyclePredicates(
            creationReturn = true,
            removalReturn = true,
        )
        val fakeLifecycle =
            FakeCompatUILifecyclePredicates(creationReturn = true, removalReturn = true)
        val fakeCompatUILayout = FakeCompatUILayout(viewBuilderReturn = View(mContext))
        val fakeCompatUISpec = FakeCompatUISpec("one", fakeLifecycle,
            fakeCompatUILayout).getSpec()
        val fakeCompatUISpec = FakeCompatUISpec("one", fakeLifecycle, fakeCompatUILayout).getSpec()
        compatUIRepository.addSpec(fakeCompatUISpec)
        // Component creation
        fakeIdGenerator.assertGenerateInvocations(0)
        compatUIHandlerRule.postBlocking {
            compatUIHandler.onCompatInfoChanged(testCompatUIInfo())
        }
        compatUIHandlerRule.postBlocking { compatUIHandler.onCompatInfoChanged(testCompatUIInfo()) }
        fakeIdGenerator.assertGenerateInvocations(1)

        compatUIHandlerRule.postBlocking {
            compatUIHandler.onCompatInfoChanged(testCompatUIInfo())
        }
        compatUIHandlerRule.postBlocking { compatUIHandler.onCompatInfoChanged(testCompatUIInfo()) }
        fakeIdGenerator.assertGenerateInvocations(2)
    }

    @Test
    fun `viewBuilder and viewBinder invoked if component is created and released when destroyed`() {
        // We add a spec to the repository
        val fakeLifecycle = FakeCompatUILifecyclePredicates(
            creationReturn = true,
            removalReturn = true,
        )
        val fakeLifecycle =
            FakeCompatUILifecyclePredicates(creationReturn = true, removalReturn = true)
        val fakeCompatUILayout = FakeCompatUILayout(viewBuilderReturn = View(mContext))
        val fakeCompatUISpec = FakeCompatUISpec("one", fakeLifecycle,
            fakeCompatUILayout).getSpec()
        val fakeCompatUISpec = FakeCompatUISpec("one", fakeLifecycle, fakeCompatUILayout).getSpec()
        compatUIRepository.addSpec(fakeCompatUISpec)

        compatUIHandlerRule.postBlocking {
            compatUIHandler.onCompatInfoChanged(testCompatUIInfo())
        }
        compatUIHandlerRule.postBlocking { compatUIHandler.onCompatInfoChanged(testCompatUIInfo()) }
        shellExecutor.flushAll()
        componentFactory.assertInvocations(1)
        fakeCompatUILayout.assertViewBuilderInvocation(1)
        fakeCompatUILayout.assertViewBinderInvocation(1)
        fakeCompatUILayout.assertViewReleaserInvocation(0)

        compatUIHandlerRule.postBlocking {
            compatUIHandler.onCompatInfoChanged(testCompatUIInfo())
        }
        compatUIHandlerRule.postBlocking { compatUIHandler.onCompatInfoChanged(testCompatUIInfo()) }
        shellExecutor.flushAll()

        componentFactory.assertInvocations(1)
@@ -283,7 +244,6 @@ class DefaultCompatUIHandlerTest : ShellTestCase() {
        fakeCompatUILayout.assertViewReleaserInvocation(1)
    }


    private fun testCompatUIInfo(): CompatUIInfo {
        val taskInfo = ActivityManager.RunningTaskInfo()
        taskInfo.taskId = 1
Loading