From 734c6f3974b6cc6cf22ea27d80332d33dd4639ca Mon Sep 17 00:00:00 2001 From: Winson Chung Date: Fri, 15 Dec 2023 05:08:53 +0000 Subject: [PATCH] Workaround for handling single device specific state in dump tests - There's a DeviceProfile state that is dynamic on a specific device class (based on a fw resource), this causes problems with the dump tests which compare the device profile against static prior dumps. For now, we can just update the expected dump based on the state of the resource to ensure that the current device profile state on this device matches. To do this, we also need to consolidate the various duplicate methods to assert the current and golden dumps match to have a common place to adjust the expected dump. Bug: 315230497 Test: atest DeviceProfileDumpTest Test: atest DeviceProfileAlternativeDisplaysDumpTest Change-Id: I5130d330878757702af07e166a669cc76452b271 --- .../launcher3/AbstractDeviceProfileTest.kt | 30 +++++++++++++++++++ .../nonquickstep/DeviceProfileDumpTest.kt | 6 +--- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/tests/src/com/android/launcher3/AbstractDeviceProfileTest.kt b/tests/src/com/android/launcher3/AbstractDeviceProfileTest.kt index e46726dc50..d44ccf57d5 100644 --- a/tests/src/com/android/launcher3/AbstractDeviceProfileTest.kt +++ b/tests/src/com/android/launcher3/AbstractDeviceProfileTest.kt @@ -17,11 +17,13 @@ package com.android.launcher3 import android.content.Context import android.content.res.Configuration +import android.content.res.Resources import android.graphics.Point import android.graphics.Rect import android.util.DisplayMetrics import android.view.Surface import androidx.test.core.app.ApplicationProvider +import androidx.test.platform.app.InstrumentationRegistry import com.android.launcher3.testing.shared.ResourceUtils import com.android.launcher3.util.DisplayController import com.android.launcher3.util.MainThreadInitializedObject.SandboxContext @@ -30,6 +32,8 @@ import com.android.launcher3.util.WindowBounds import com.android.launcher3.util.rule.TestStabilityRule import com.android.launcher3.util.window.CachedDisplayInfo import com.android.launcher3.util.window.WindowManagerProxy +import com.android.wm.shell.Flags +import com.google.common.truth.Truth import java.io.BufferedReader import java.io.File import java.io.PrintWriter @@ -49,11 +53,18 @@ import org.mockito.kotlin.whenever * For an implementation that mocks InvariantDeviceProfile, use [FakeInvariantDeviceProfileTest] */ abstract class AbstractDeviceProfileTest { + protected val testContext: Context = InstrumentationRegistry.getInstrumentation().context protected lateinit var context: SandboxContext protected open val runningContext: Context = ApplicationProvider.getApplicationContext() private val displayController: DisplayController = mock() private val windowManagerProxy: WindowManagerProxy = mock() private val launcherPrefs: LauncherPrefs = mock() + private val allowLeftRightSplitInPortrait: Boolean = initAllowLeftRightSplitInPortrait() + fun initAllowLeftRightSplitInPortrait() : Boolean { + val res = Resources.getSystem(); + val resId = res.getIdentifier("config_leftRightSplitInPortrait", "bool", "android") + return Flags.enableLeftRightSplitInPortrait() && resId > 0 && res.getBoolean(resId) + } @Rule @JvmField val testStabilityRule = TestStabilityRule() @@ -306,6 +317,25 @@ abstract class AbstractDeviceProfileTest { whenever(info.isTransientTaskbar).thenReturn(isGestureMode) } + /** Asserts that the given device profile matches a previously dumped device profile state. */ + protected fun assertDump(dp: DeviceProfile, folderName: String, filename: String) { + val dump = dump(context!!, dp, "${folderName}_$filename.txt") + var expected = readDumpFromAssets(testContext, "$folderName/$filename.txt") + + // TODO(b/315230497): We don't currently have device-specific device profile dumps, so just + // update the result before we do the comparison + if (allowLeftRightSplitInPortrait) { + val isLeftRightSplitInPortrait = when { + allowLeftRightSplitInPortrait && dp.isTablet -> !dp.isLandscape + else -> dp.isLandscape + } + expected = expected.replace(Regex("isLeftRightSplit:\\w+"), + "isLeftRightSplit:$isLeftRightSplitInPortrait") + } + + Truth.assertThat(dump).isEqualTo(expected) + } + /** Create a new dump of DeviceProfile, saves to a file in the device and returns it */ protected fun dump(context: Context, dp: DeviceProfile, fileName: String): String { val stringWriter = StringWriter() diff --git a/tests/src/com/android/launcher3/nonquickstep/DeviceProfileDumpTest.kt b/tests/src/com/android/launcher3/nonquickstep/DeviceProfileDumpTest.kt index 9b67310314..9409ac1c1a 100644 --- a/tests/src/com/android/launcher3/nonquickstep/DeviceProfileDumpTest.kt +++ b/tests/src/com/android/launcher3/nonquickstep/DeviceProfileDumpTest.kt @@ -30,7 +30,6 @@ import org.junit.runner.RunWith @SmallTest @RunWith(AndroidJUnit4::class) class DeviceProfileDumpTest : AbstractDeviceProfileTest() { - private val testContext: Context = InstrumentationRegistry.getInstrumentation().context private val folderName: String = "DeviceProfileDumpTest" @Test fun phonePortrait3Button() { @@ -154,9 +153,6 @@ class DeviceProfileDumpTest : AbstractDeviceProfileTest() { } private fun assertDump(dp: DeviceProfile, filename: String) { - val dump = dump(context!!, dp, "${folderName}_$filename.txt") - val expected = readDumpFromAssets(testContext, "$folderName/$filename.txt") - - assertThat(dump).isEqualTo(expected) + assertDump(dp, folderName, filename); } }