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

Commit d7f84600 authored by Mark Renouf's avatar Mark Renouf
Browse files

Handle RootTaskInfo with no child tasks

If a visible RootTask is present but childTasksIds, etc is empty,
the call to .first() will throw a NoSuchElementException. Skip
these within the previous filter operation using 'hasChildTasks'.

Change-Id: I777cd7e370e37bb923b1903e7c67bcfc382cc89c
parent 39b9c220
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -30,3 +30,5 @@ internal fun RootTaskInfo.childTasksTopDown(): Sequence<ChildTaskModel> {
        )
    }
}

internal fun RootTaskInfo.hasChildTasks() = childTaskUserIds.isNotEmpty()
+3 −1
Original line number Diff line number Diff line
@@ -57,7 +57,9 @@ constructor(
        // Find the first non PiP rootTask with a top child task owned by a work user
        val (rootTask, childTask) =
            content.rootTasks
                .filter { it.isVisible && it.windowingMode != WINDOWING_MODE_PINNED }
                .filter {
                    it.isVisible && it.windowingMode != WINDOWING_MODE_PINNED && it.hasChildTasks()
                }
                .map { it to it.childTasksTopDown().first() }
                .firstOrNull { (_, child) ->
                    profileTypes.getProfileType(child.userId) == ProfileType.WORK
+12 −0
Original line number Diff line number Diff line
@@ -188,6 +188,18 @@ object DisplayContentScenarios {
     * actual values returned by ActivityTaskManager
     */
    object RootTasks {
        /** An empty RootTaskInfo with no child tasks. */
        val emptyWithNoChildTasks =
            newRootTaskInfo(
                taskId = 2,
                visible = true,
                running = true,
                numActivities = 0,
                bounds = FULL_SCREEN,
            ) {
                emptyList()
            }

        /**
         * The empty RootTaskInfo that is always at the end of a list from ActivityTaskManager when
         * no other visible activities are in split mode
+27 −0
Original line number Diff line number Diff line
@@ -22,16 +22,19 @@ import android.platform.test.annotations.DisableFlags
import android.platform.test.annotations.EnableFlags
import android.platform.test.flag.junit.SetFlagsRule
import com.android.systemui.kosmos.Kosmos
import com.android.systemui.screenshot.data.model.DisplayContentModel
import com.android.systemui.screenshot.data.model.DisplayContentScenarios.ActivityNames.FILES
import com.android.systemui.screenshot.data.model.DisplayContentScenarios.ActivityNames.YOUTUBE
import com.android.systemui.screenshot.data.model.DisplayContentScenarios.Bounds.FREE_FORM
import com.android.systemui.screenshot.data.model.DisplayContentScenarios.Bounds.FULL_SCREEN
import com.android.systemui.screenshot.data.model.DisplayContentScenarios.Bounds.SPLIT_TOP
import com.android.systemui.screenshot.data.model.DisplayContentScenarios.RootTasks
import com.android.systemui.screenshot.data.model.DisplayContentScenarios.TaskSpec
import com.android.systemui.screenshot.data.model.DisplayContentScenarios.freeFormApps
import com.android.systemui.screenshot.data.model.DisplayContentScenarios.pictureInPictureApp
import com.android.systemui.screenshot.data.model.DisplayContentScenarios.singleFullScreen
import com.android.systemui.screenshot.data.model.DisplayContentScenarios.splitScreenApps
import com.android.systemui.screenshot.data.model.SystemUiState
import com.android.systemui.screenshot.data.repository.profileTypeRepository
import com.android.systemui.screenshot.policy.CapturePolicy.PolicyResult
import com.android.systemui.screenshot.policy.CapturePolicy.PolicyResult.NotMatched
@@ -54,6 +57,30 @@ class WorkProfilePolicyTest {
    private val kosmos = Kosmos()
    private val policy = WorkProfilePolicy(kosmos.profileTypeRepository)

    /**
     * There is no guarantee that every RootTaskInfo contains a non-empty list of child tasks. Test
     * the case where the RootTaskInfo would match but child tasks are empty.
     */
    @Test
    fun withEmptyChildTasks_notMatched() = runTest {
        val result =
            policy.check(
                DisplayContentModel(
                    displayId = 0,
                    systemUiState = SystemUiState(shadeExpanded = false),
                    rootTasks = listOf(RootTasks.emptyWithNoChildTasks)
                )
            )

        assertThat(result)
            .isEqualTo(
                NotMatched(
                    WorkProfilePolicy.NAME,
                    WORK_TASK_NOT_TOP,
                )
            )
    }

    @Test
    fun noWorkApp_notMatched() = runTest {
        val result =