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

Commit b97f2d0f authored by Massimo Carli's avatar Massimo Carli Committed by Android (Google) Code Review
Browse files

Merge "[39/n] Add runOnFilteredItem to LetterboxUtils" into main

parents 30a88ce5 51fd5c5c
Loading
Loading
Loading
Loading
+14 −0
Original line number Diff line number Diff line
@@ -89,6 +89,20 @@ object LetterboxUtils {
            }
            return onMissed(key, this)
        }

        /*
        * Executes [onItem] on the [item] for the [key]s for a given [filter] predicate.
        */
        fun <V, K> MutableMap<K, V>.runOnFilteredItem(
            filter: (K) -> Boolean,
            onItem: (K, V) -> Unit = { _, _ -> },
        ) {
            this.forEach { k, v ->
                if (filter(k)) {
                    onItem(k, v)
                }
            }
        }
    }

    // Utility methods about Transaction usage in Letterbox.
+22 −0
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ import android.testing.AndroidTestingRunner
import android.view.SurfaceControl
import androidx.test.filters.SmallTest
import com.android.wm.shell.ShellTestCase
import com.android.wm.shell.compatui.letterbox.LetterboxUtils.Maps.runOnFilteredItem
import com.android.wm.shell.compatui.letterbox.LetterboxUtils.Maps.runOnItem
import com.android.wm.shell.compatui.letterbox.LetterboxUtils.Transactions.moveAndCrop
import java.util.function.Consumer
@@ -114,6 +115,15 @@ class LetterboxUtilsTest : ShellTestCase() {
        }
    }

    @Test
    fun `runOnFilterItem executes onItem when the predicate is true`() {
        runTestScenario { r ->
            r.initMap(1 to 2, 2 to 4, 3 to 6, 4 to 8)
            r.runOnFilteredItem<Int> { k -> k % 2 == 0 }
            r.verifyOnFilteredInvoked(mapOf(2 to 4, 4 to 8))
        }
    }

    @Test
    fun `moveAndCrop invoked Move and then Crop and Visible`() {
        runTestScenario { r ->
@@ -157,6 +167,7 @@ class LetterboxUtilsTest : ShellTestCase() {
        private var onItemState: Int? = null
        private var onMissingStateKey: Int? = null
        private var onMissingStateMap: MutableMap<Int, Int>? = null
        private var onFilteredStateMap = mutableMapOf<Int, Int>()

        private val surface = SurfaceControl()

@@ -206,6 +217,13 @@ class LetterboxUtilsTest : ShellTestCase() {
            })
        }

        fun <T> runOnFilteredItem(predicate: (Int) -> Boolean) {
            onFilteredStateMap.clear()
            testableMap.runOnFilteredItem(predicate) { k, v ->
                onFilteredStateMap[k] = v
            }
        }

        fun verifyOnItemInvoked(expectedItem: Int) {
            assertEquals(expectedItem, onItemState)
        }
@@ -219,6 +237,10 @@ class LetterboxUtilsTest : ShellTestCase() {
            assertEquals(onMissingStateMap, testableMap)
        }

        fun verifyOnFilteredInvoked(expected: Map<Int, Int>) {
            assertEquals(expected, onFilteredStateMap)
        }

        fun verifyOnMissingNotInvoked() {
            assertNull(onMissingStateKey)
            assertNull(onMissingStateMap)