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

Commit d77ca213 authored by Massimo Carli's avatar Massimo Carli
Browse files

[1/n] Add LetterboxRule to AppCompat Flicker test

Defines LetterboxRule JUnit rule to handle Letterbox
styles and state

Bug: 234498861
Test: Run `atest WMShellFlickerTestsOther`

Change-Id: Ie561292a57e050cf85ee107e84c1a709c8f985e1
parent 0e61b8a1
Loading
Loading
Loading
Loading
+8 −49
Original line number Diff line number Diff line
@@ -17,7 +17,6 @@
package com.android.wm.shell.flicker.appcompat

import android.content.Context
import android.system.helpers.CommandsHelper
import android.tools.common.traces.component.ComponentNameMatcher
import android.tools.device.flicker.legacy.FlickerBuilder
import android.tools.device.flicker.legacy.FlickerTestData
@@ -29,15 +28,18 @@ import com.android.wm.shell.flicker.appWindowIsVisibleAtEnd
import com.android.wm.shell.flicker.appWindowIsVisibleAtStart
import com.android.wm.shell.flicker.appWindowKeepVisible
import com.android.wm.shell.flicker.layerKeepVisible
import org.junit.After

import org.junit.Assume
import org.junit.Before
import org.junit.Rule

abstract class BaseAppCompat(flicker: LegacyFlickerTest) : BaseTest(flicker) {
    protected val context: Context = instrumentation.context
    protected val letterboxApp = LetterboxAppHelper(instrumentation)
    lateinit var cmdHelper: CommandsHelper
    private lateinit var letterboxStyle: HashMap<String, String>

    @JvmField
    @Rule
    val letterboxRule: LetterboxRule = LetterboxRule()

    /** {@inheritDoc} */
    override val transition: FlickerBuilder.() -> Unit
@@ -52,50 +54,7 @@ abstract class BaseAppCompat(flicker: LegacyFlickerTest) : BaseTest(flicker) {

    @Before
    fun before() {
        cmdHelper = CommandsHelper.getInstance(instrumentation)
        Assume.assumeTrue(tapl.isTablet && isIgnoreOrientationRequest())
        letterboxStyle = mapLetterboxStyle()
        resetLetterboxStyle()
        setLetterboxEducationEnabled(false)
    }

    @After
    fun after() {
        resetLetterboxStyle()
    }

    private fun mapLetterboxStyle(): HashMap<String, String> {
        val res = cmdHelper.executeShellCommand("wm get-letterbox-style")
        val lines = res.lines()
        val map = HashMap<String, String>()
        for (line in lines) {
            val keyValuePair = line.split(":")
            if (keyValuePair.size == 2) {
                val key = keyValuePair[0].trim()
                map[key] = keyValuePair[1].trim()
            }
        }
        return map
    }

    private fun getLetterboxStyle(): HashMap<String, String> {
        if (!::letterboxStyle.isInitialized) {
            letterboxStyle = mapLetterboxStyle()
        }
        return letterboxStyle
    }

    private fun resetLetterboxStyle() {
        cmdHelper.executeShellCommand("wm reset-letterbox-style")
    }

    private fun setLetterboxEducationEnabled(enabled: Boolean) {
        cmdHelper.executeShellCommand("wm set-letterbox-style --isEducationEnabled $enabled")
    }

    private fun isIgnoreOrientationRequest(): Boolean {
        val res = cmdHelper.executeShellCommand("wm get-ignore-orientation-request")
        return res != null && res.contains("true")
        Assume.assumeTrue(tapl.isTablet && letterboxRule.isIgnoreOrientationRequest)
    }

    fun FlickerTestData.setStartRotation() = setRotation(flicker.scenario.startRotation)
@@ -115,7 +74,7 @@ abstract class BaseAppCompat(flicker: LegacyFlickerTest) : BaseTest(flicker) {

    /** Only run on tests with config_letterboxActivityCornersRadius != 0 in devices */
    private fun assumeLetterboxRoundedCornersEnabled() {
        Assume.assumeTrue(getLetterboxStyle().getValue("Corner radius") != "0")
        Assume.assumeTrue(letterboxRule.hasCornerRadius)
    }

    fun assertLetterboxAppVisibleAtStartAndEnd() {
+108 −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.wm.shell.flicker.appcompat

import android.app.Instrumentation
import android.system.helpers.CommandsHelper
import androidx.test.platform.app.InstrumentationRegistry
import org.junit.rules.TestRule
import org.junit.runner.Description
import org.junit.runners.model.Statement

/**
 * JUnit Rule to handle letterboxStyles and states
 */
class LetterboxRule(
        private val withLetterboxEducationEnabled: Boolean = false,
        private val instrumentation: Instrumentation = InstrumentationRegistry.getInstrumentation(),
        private val cmdHelper: CommandsHelper = CommandsHelper.getInstance(instrumentation)
) : TestRule {

    private val execAdb: (String) -> String = {cmd -> cmdHelper.executeShellCommand(cmd)}
    private lateinit var _letterboxStyle: MutableMap<String, String>

    val letterboxStyle: Map<String, String>
        get() {
            if (!::_letterboxStyle.isInitialized) {
                _letterboxStyle = mapLetterboxStyle()
            }
            return _letterboxStyle
        }

    val cornerRadius: Int?
        get() = asInt(letterboxStyle["Corner radius"])

    val hasCornerRadius: Boolean
        get() {
            val radius = cornerRadius
            return radius != null && radius > 0
        }

    val isIgnoreOrientationRequest: Boolean
        get() = execAdb("wm get-ignore-orientation-request")?.contains("true") ?: false

    override fun apply(base: Statement?, description: Description?): Statement {
        resetLetterboxStyle()
        _letterboxStyle = mapLetterboxStyle()
        val isLetterboxEducationEnabled = _letterboxStyle.getValue("Is education enabled")
        var hasLetterboxEducationStateChanged = false
        if ("$withLetterboxEducationEnabled" != isLetterboxEducationEnabled) {
            hasLetterboxEducationStateChanged = true
            execAdb("wm set-letterbox-style --isEducationEnabled " +
                    withLetterboxEducationEnabled)
        }
        return try {
            object : Statement() {
                @Throws(Throwable::class)
                override fun evaluate() {
                    base!!.evaluate()
                }
            }
        } finally {
            if (hasLetterboxEducationStateChanged) {
                execAdb("wm set-letterbox-style --isEducationEnabled " +
                        isLetterboxEducationEnabled
                )
            }
            resetLetterboxStyle()
        }
    }

    private fun mapLetterboxStyle(): HashMap<String, String> {
        val res = execAdb("wm get-letterbox-style")
        val lines = res.lines()
        val map = HashMap<String, String>()
        for (line in lines) {
            val keyValuePair = line.split(":")
            if (keyValuePair.size == 2) {
                val key = keyValuePair[0].trim()
                map[key] = keyValuePair[1].trim()
            }
        }
        return map
    }

    private fun resetLetterboxStyle() {
        execAdb("wm reset-letterbox-style")
    }

    private fun asInt(str: String?): Int? = try {
        str?.toInt()
    } catch (e: NumberFormatException) {
        null
    }
}
 No newline at end of file
+1 −1
Original line number Diff line number Diff line
@@ -31,7 +31,7 @@ import org.junit.runners.Parameterized
/**
 * Test launching app in size compat mode.
 *
 * To run this test: `atest WMShellFlickerTests:OpenAppInSizeCompatModeTest`
 * To run this test: `atest WMShellFlickerTestsOther:OpenAppInSizeCompatModeTest`
 *
 * Actions:
 * ```
+3 −1
Original line number Diff line number Diff line
@@ -32,7 +32,9 @@ import org.junit.runners.Parameterized
/**
 * Test launching a fixed portrait letterboxed app in landscape and repositioning to the right.
 *
 * To run this test: `atest WMShellFlickerTests:RepositionFixedPortraitAppTest` Actions:
 * To run this test: `atest WMShellFlickerTestsOther:RepositionFixedPortraitAppTest`
 *
 * Actions:
 *
 *  ```
 *      Launch a fixed portrait app in landscape to letterbox app
+1 −1
Original line number Diff line number Diff line
@@ -31,7 +31,7 @@ import org.junit.runners.Parameterized
/**
 * Test restarting app in size compat mode.
 *
 * To run this test: `atest WMShellFlickerTests:RestartAppInSizeCompatModeTest`
 * To run this test: `atest WMShellFlickerTestsOther:RestartAppInSizeCompatModeTest`
 *
 * Actions:
 * ```