Give the tests the ability to emulate other devices screens

This code contains utility clases that can change the display
of a device and make it look like another device.
The function DisplayEmulator#emulate receives a DeviceEmulationData
a certain grid to emulate and a callback, everyting that happens
inside of the callback will happen when the device is being emulated
and can be used by other tests.

Example test:
package com.android.launcher3.deviceemulator;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.MediumTest;
import com.android.launcher3.deviceemulator.models.DeviceEmulationData;
import com.android.launcher3.ui.AbstractLauncherUiTest;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.util.concurrent.TimeUnit;

@MediumTest
@RunWith(AndroidJUnit4.class)
public class TestTest extends AbstractLauncherUiTest {
    @Test
    public void testEmulation() throws Exception {
        String deviceCode = "pixel6pro";
        DeviceEmulationData deviceData = DeviceEmulationData.getDevice(deviceCode);
        String grid = "normal";
        DisplayEmulator displayEmulator = new DisplayEmulator(mTargetContext);

        displayEmulator.emulate(deviceData, grid, () ->{
            TimeUnit.SECONDS.sleep(10);
            return true;
        });
    }
}

Test: You could use the test above to make your device look like a
Pixel6 pro for 10 secons.
Fix: 229028257

Change-Id: Icd79be405a2e14dda0bc5f555b0e46149e16f912
This commit is contained in:
Sebastian Franco
2022-03-25 13:49:59 -07:00
parent 795a9a8cee
commit 00aff95ac0
6 changed files with 390 additions and 11 deletions

View File

@@ -22,7 +22,6 @@ import static com.android.launcher3.ResourceUtils.INVALID_RESOURCE_HANDLE;
import static com.android.launcher3.ResourceUtils.NAVBAR_HEIGHT;
import static com.android.launcher3.ResourceUtils.NAVBAR_HEIGHT_LANDSCAPE;
import static com.android.launcher3.ResourceUtils.NAVBAR_LANDSCAPE_LEFT_RIGHT_SIZE;
import static com.android.launcher3.ResourceUtils.getDimenByName;
import static com.android.launcher3.Utilities.dpiFromPx;
import static com.android.launcher3.util.MainThreadInitializedObject.forOverride;
import static com.android.launcher3.util.RotationUtils.deltaRotation;
@@ -157,16 +156,16 @@ public class WindowManagerProxy implements ResourceBasedOverride {
int bottomNav = isTablet
? 0
: (config.screenHeightDp > config.screenWidthDp
? getDimenByName(NAVBAR_HEIGHT, systemRes, 0)
? getDimenByName(NAVBAR_HEIGHT, systemRes)
: (isGesture
? getDimenByName(NAVBAR_HEIGHT_LANDSCAPE, systemRes, 0)
? getDimenByName(NAVBAR_HEIGHT_LANDSCAPE, systemRes)
: 0));
Insets newNavInsets = Insets.of(navInsets.left, navInsets.top, navInsets.right, bottomNav);
insetsBuilder.setInsets(WindowInsets.Type.navigationBars(), newNavInsets);
insetsBuilder.setInsetsIgnoringVisibility(WindowInsets.Type.navigationBars(), newNavInsets);
Insets statusBarInsets = oldInsets.getInsets(WindowInsets.Type.statusBars());
int statusBarHeight = getDimenByName("status_bar_height", systemRes, 0);
int statusBarHeight = getDimenByName("status_bar_height", systemRes);
Insets newStatusBarInsets = Insets.of(
statusBarInsets.left,
Math.max(statusBarInsets.top, statusBarHeight),
@@ -222,23 +221,23 @@ public class WindowManagerProxy implements ResourceBasedOverride {
boolean isTabletOrGesture = isTablet
|| (Utilities.ATLEAST_R && isGestureNav(context));
int statusBarHeight = getDimenByName("status_bar_height", systemRes, 0);
int statusBarHeight = getDimenByName("status_bar_height", systemRes);
int navBarHeightPortrait, navBarHeightLandscape, navbarWidthLandscape;
navBarHeightPortrait = isTablet
? (mTaskbarDrawnInProcess
? 0 : systemRes.getDimensionPixelSize(R.dimen.taskbar_size))
: getDimenByName(NAVBAR_HEIGHT, systemRes, 0);
: getDimenByName(NAVBAR_HEIGHT, systemRes);
navBarHeightLandscape = isTablet
? (mTaskbarDrawnInProcess
? 0 : systemRes.getDimensionPixelSize(R.dimen.taskbar_size))
: (isTabletOrGesture
? getDimenByName(NAVBAR_HEIGHT_LANDSCAPE, systemRes, 0) : 0);
? getDimenByName(NAVBAR_HEIGHT_LANDSCAPE, systemRes) : 0);
navbarWidthLandscape = isTabletOrGesture
? 0
: getDimenByName(NAVBAR_LANDSCAPE_LEFT_RIGHT_SIZE, systemRes, 0);
: getDimenByName(NAVBAR_LANDSCAPE_LEFT_RIGHT_SIZE, systemRes);
WindowBounds[] result = new WindowBounds[4];
Point tempSize = new Point();
@@ -274,6 +273,13 @@ public class WindowManagerProxy implements ResourceBasedOverride {
return result;
}
/**
* Wrapper around the utility method for easier emulation
*/
protected int getDimenByName(String resName, Resources res) {
return ResourceUtils.getDimenByName(resName, res, 0);
}
protected boolean isGestureNav(Context context) {
return ResourceUtils.getIntegerByName("config_navBarInteractionMode",
context.getResources(), INVALID_RESOURCE_HANDLE) == 2;