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

Commit 35a97e75 authored by Alejandro Nijamkin's avatar Alejandro Nijamkin
Browse files

[flexiglass] Test for FoldPosture.

Flag: NA
Test: NA
Bug: 309524547
Change-Id: I6c8160dbb295cd2b01180deda84d0144b98c5e3f
parent 4ae4c388
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -93,8 +93,8 @@ import com.android.systemui.bouncer.ui.viewmodel.PinBouncerViewModel
import com.android.systemui.common.shared.model.Text.Companion.loadText
import com.android.systemui.common.ui.compose.Icon
import com.android.systemui.dagger.SysUISingleton
import com.android.systemui.fold.ui.composable.FoldPosture
import com.android.systemui.fold.ui.composable.foldPosture
import com.android.systemui.fold.ui.helper.FoldPosture
import com.android.systemui.res.R
import com.android.systemui.scene.shared.model.Direction
import com.android.systemui.scene.shared.model.SceneKey
+3 −39
Original line number Diff line number Diff line
@@ -23,19 +23,9 @@ import androidx.compose.runtime.getValue
import androidx.compose.runtime.produceState
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.LocalContext
import androidx.window.layout.FoldingFeature
import androidx.window.layout.WindowInfoTracker

sealed interface FoldPosture {
    /** A foldable device that's fully closed/folded or a device that doesn't support folding. */
    data object Folded : FoldPosture
    /** A foldable that's halfway open with the hinge held vertically. */
    data object Book : FoldPosture
    /** A foldable that's halfway open with the hinge held horizontally. */
    data object Tabletop : FoldPosture
    /** A foldable that's fully unfolded / flat. */
    data object FullyUnfolded : FoldPosture
}
import com.android.systemui.fold.ui.helper.FoldPosture
import com.android.systemui.fold.ui.helper.foldPostureInternal

/** Returns the [FoldPosture] of the device currently. */
@Composable
@@ -48,32 +38,6 @@ fun foldPosture(): State<FoldPosture> {
        initialValue = FoldPosture.Folded,
        key1 = layoutInfo,
    ) {
        value =
            layoutInfo
                ?.displayFeatures
                ?.firstNotNullOfOrNull { it as? FoldingFeature }
                .let { foldingFeature ->
                    when (foldingFeature?.state) {
                        null -> FoldPosture.Folded
                        FoldingFeature.State.HALF_OPENED ->
                            foldingFeature.orientation.toHalfwayPosture()
                        FoldingFeature.State.FLAT ->
                            if (foldingFeature.isSeparating) {
                                // Dual screen device.
                                foldingFeature.orientation.toHalfwayPosture()
                            } else {
                                FoldPosture.FullyUnfolded
                            }
                        else -> error("Unsupported state \"${foldingFeature.state}\"")
                    }
                }
    }
}

private fun FoldingFeature.Orientation.toHalfwayPosture(): FoldPosture {
    return when (this) {
        FoldingFeature.Orientation.HORIZONTAL -> FoldPosture.Tabletop
        FoldingFeature.Orientation.VERTICAL -> FoldPosture.Book
        else -> error("Unsupported orientation \"$this\"")
        value = foldPostureInternal(layoutInfo)
    }
}
+124 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.fold.ui.helper

import android.graphics.Rect
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import androidx.window.layout.FoldingFeature
import androidx.window.layout.WindowLayoutInfo
import com.android.systemui.SysuiTestCase
import com.google.common.truth.Truth.assertThat
import org.junit.Test
import org.junit.runner.RunWith

@SmallTest
@RunWith(AndroidJUnit4::class)
class FoldPostureTest : SysuiTestCase() {

    @Test
    fun foldPosture_whenNull_returnsFolded() {
        assertThat(foldPostureInternal(null)).isEqualTo(FoldPosture.Folded)
    }

    @Test
    fun foldPosture_whenHalfOpenHorizontally_returnsTabletop() {
        assertThat(
                foldPostureInternal(
                    createWindowLayoutInfo(
                        state = FoldingFeature.State.HALF_OPENED,
                        orientation = FoldingFeature.Orientation.HORIZONTAL,
                    )
                )
            )
            .isEqualTo(FoldPosture.Tabletop)
    }

    @Test
    fun foldPosture_whenHalfOpenVertically_returnsBook() {
        assertThat(
                foldPostureInternal(
                    createWindowLayoutInfo(
                        state = FoldingFeature.State.HALF_OPENED,
                        orientation = FoldingFeature.Orientation.VERTICAL,
                    )
                )
            )
            .isEqualTo(FoldPosture.Book)
    }

    @Test
    fun foldPosture_whenFlatAndNotSeparating_returnsFullyUnfolded() {
        assertThat(
                foldPostureInternal(
                    createWindowLayoutInfo(
                        state = FoldingFeature.State.FLAT,
                        orientation = FoldingFeature.Orientation.HORIZONTAL,
                        isSeparating = false,
                    )
                )
            )
            .isEqualTo(FoldPosture.FullyUnfolded)
    }

    @Test
    fun foldPosture_whenFlatAndSeparatingHorizontally_returnsTabletop() {
        assertThat(
                foldPostureInternal(
                    createWindowLayoutInfo(
                        state = FoldingFeature.State.FLAT,
                        isSeparating = true,
                        orientation = FoldingFeature.Orientation.HORIZONTAL,
                    )
                )
            )
            .isEqualTo(FoldPosture.Tabletop)
    }

    @Test
    fun foldPosture_whenFlatAndSeparatingVertically_returnsBook() {
        assertThat(
                foldPostureInternal(
                    createWindowLayoutInfo(
                        state = FoldingFeature.State.FLAT,
                        isSeparating = true,
                        orientation = FoldingFeature.Orientation.VERTICAL,
                    )
                )
            )
            .isEqualTo(FoldPosture.Book)
    }

    private fun createWindowLayoutInfo(
        state: FoldingFeature.State,
        orientation: FoldingFeature.Orientation = FoldingFeature.Orientation.VERTICAL,
        isSeparating: Boolean = false,
        occlusionType: FoldingFeature.OcclusionType = FoldingFeature.OcclusionType.NONE,
    ): WindowLayoutInfo {
        return WindowLayoutInfo(
            listOf(
                object : FoldingFeature {
                    override val bounds: Rect = Rect(0, 0, 100, 100)
                    override val isSeparating: Boolean = isSeparating
                    override val occlusionType: FoldingFeature.OcclusionType = occlusionType
                    override val orientation: FoldingFeature.Orientation = orientation
                    override val state: FoldingFeature.State = state
                }
            )
        )
    }
}
+65 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.fold.ui.helper

import androidx.annotation.VisibleForTesting
import androidx.window.layout.FoldingFeature
import androidx.window.layout.WindowLayoutInfo

sealed interface FoldPosture {
    /** A foldable device that's fully closed/folded or a device that doesn't support folding. */
    data object Folded : FoldPosture
    /** A foldable that's halfway open with the hinge held vertically. */
    data object Book : FoldPosture
    /** A foldable that's halfway open with the hinge held horizontally. */
    data object Tabletop : FoldPosture
    /** A foldable that's fully unfolded / flat. */
    data object FullyUnfolded : FoldPosture
}

/**
 * Internal version of `foldPosture` in the System UI Compose library, extracted here to allow for
 * testing that's not dependent on Compose.
 */
@VisibleForTesting
fun foldPostureInternal(layoutInfo: WindowLayoutInfo?): FoldPosture {
    return layoutInfo
        ?.displayFeatures
        ?.firstNotNullOfOrNull { it as? FoldingFeature }
        .let { foldingFeature ->
            when (foldingFeature?.state) {
                null -> FoldPosture.Folded
                FoldingFeature.State.HALF_OPENED -> foldingFeature.orientation.toHalfwayPosture()
                FoldingFeature.State.FLAT ->
                    if (foldingFeature.isSeparating) {
                        // Dual screen device.
                        foldingFeature.orientation.toHalfwayPosture()
                    } else {
                        FoldPosture.FullyUnfolded
                    }
                else -> error("Unsupported state \"${foldingFeature.state}\"")
            }
        }
}

private fun FoldingFeature.Orientation.toHalfwayPosture(): FoldPosture {
    return when (this) {
        FoldingFeature.Orientation.HORIZONTAL -> FoldPosture.Tabletop
        FoldingFeature.Orientation.VERTICAL -> FoldPosture.Book
        else -> error("Unsupported orientation \"$this\"")
    }
}