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

Commit 0c3a3125 authored by Vadim Tryshev's avatar Vadim Tryshev Committed by Automerger Merge Worker
Browse files

Merge "Verifying success of setting default Launcher in tests" into sc-dev am: ba302e8a

Original change: https://googleplex-android-review.googlesource.com/c/platform/packages/apps/Launcher3/+/15158349

Change-Id: I45b7cdf29ea93351fb2a47a7ec207739de5e3745
parents bb6b8eef ba302e8a
Loading
Loading
Loading
Loading
+47 −9
Original line number Diff line number Diff line
@@ -15,20 +15,26 @@
 */
package com.android.launcher3.util.rule;

import static com.android.launcher3.tapl.TestHelpers.getLauncherInMyProcess;

import static androidx.test.InstrumentationRegistry.getInstrumentation;

import static com.android.launcher3.tapl.TestHelpers.getLauncherInMyProcess;

import android.content.ComponentName;
import android.content.pm.ActivityInfo;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.test.InstrumentationRegistry;
import androidx.test.uiautomator.UiDevice;

import com.android.systemui.shared.system.PackageManagerWrapper;

import org.junit.Assert;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;

import androidx.annotation.Nullable;
import androidx.test.InstrumentationRegistry;
import androidx.test.uiautomator.UiDevice;
import java.util.ArrayList;

/**
 * Test rule which executes a shell command at the start of the test.
@@ -37,10 +43,19 @@ public class ShellCommandRule implements TestRule {

    private final String mCmd;
    private final String mRevertCommand;
    private final boolean mCheckSuccess;
    private final Runnable mAdditionalChecks;

    public ShellCommandRule(String cmd, @Nullable String revertCommand) {
    public ShellCommandRule(String cmd, @Nullable String revertCommand, boolean checkSuccess,
            Runnable additionalChecks) {
        mCmd = cmd;
        mRevertCommand = revertCommand;
        mCheckSuccess = checkSuccess;
        mAdditionalChecks = additionalChecks;
    }

    public ShellCommandRule(String cmd, @Nullable String revertCommand) {
        this(cmd, revertCommand, false, null);
    }

    @Override
@@ -48,12 +63,27 @@ public class ShellCommandRule implements TestRule {
        return new Statement() {
            @Override
            public void evaluate() throws Throwable {
                final String result =
                        UiDevice.getInstance(getInstrumentation()).executeShellCommand(mCmd);
                if (mCheckSuccess) {
                    Assert.assertTrue(
                            "Failed command: " + mCmd + ", result: " + result,
                            "Success".equals(result.replaceAll("\\s", "")));
                }
                if (mAdditionalChecks != null) mAdditionalChecks.run();
                try {
                    base.evaluate();
                } finally {
                    if (mRevertCommand != null) {
                        UiDevice.getInstance(getInstrumentation()).executeShellCommand(mRevertCommand);
                        final String revertResult = UiDevice.getInstance(
                                getInstrumentation()).executeShellCommand(
                                mRevertCommand);
                        if (mCheckSuccess) {
                            Assert.assertTrue(
                                    "Failed command: " + mRevertCommand
                                            + ", result: " + revertResult,
                                    "Success".equals(result.replaceAll("\\s", "")));
                        }
                    }
                }
            }
@@ -72,7 +102,15 @@ public class ShellCommandRule implements TestRule {
     * Sets the target launcher as default launcher.
     */
    public static ShellCommandRule setDefaultLauncher() {
        return new ShellCommandRule(getLauncherCommand(getLauncherInMyProcess()), null);
        final ActivityInfo launcher = getLauncherInMyProcess();
        Log.d("b/187080582", "Launcher: " + new ComponentName(launcher.packageName, launcher.name)
                .flattenToString());
        return new ShellCommandRule(getLauncherCommand(launcher), null, true, () ->
                Assert.assertEquals("Setting default launcher failed",
                        new ComponentName(launcher.packageName, launcher.name)
                                .flattenToString(),
                        PackageManagerWrapper.getInstance().getHomeActivities(new ArrayList<>())
                                .flattenToString()));
    }

    public static String getLauncherCommand(ActivityInfo launcher) {