Merge "Verifying success of setting default Launcher in tests" into sc-dev am: ba302e8a6a
Original change: https://googleplex-android-review.googlesource.com/c/platform/packages/apps/Launcher3/+/15158349 Change-Id: I6c886b1f14df1f72d35a65b495185a44de0a8a0d
This commit is contained in:
@@ -15,21 +15,27 @@
|
|||||||
*/
|
*/
|
||||||
package com.android.launcher3.util.rule;
|
package com.android.launcher3.util.rule;
|
||||||
|
|
||||||
import static com.android.launcher3.tapl.TestHelpers.getLauncherInMyProcess;
|
|
||||||
|
|
||||||
import static androidx.test.InstrumentationRegistry.getInstrumentation;
|
import static androidx.test.InstrumentationRegistry.getInstrumentation;
|
||||||
|
|
||||||
|
import static com.android.launcher3.tapl.TestHelpers.getLauncherInMyProcess;
|
||||||
|
|
||||||
import android.content.ComponentName;
|
import android.content.ComponentName;
|
||||||
import android.content.pm.ActivityInfo;
|
import android.content.pm.ActivityInfo;
|
||||||
|
import android.util.Log;
|
||||||
import org.junit.rules.TestRule;
|
|
||||||
import org.junit.runner.Description;
|
|
||||||
import org.junit.runners.model.Statement;
|
|
||||||
|
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
import androidx.test.InstrumentationRegistry;
|
import androidx.test.InstrumentationRegistry;
|
||||||
import androidx.test.uiautomator.UiDevice;
|
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 java.util.ArrayList;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test rule which executes a shell command at the start of the test.
|
* 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 mCmd;
|
||||||
private final String mRevertCommand;
|
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;
|
mCmd = cmd;
|
||||||
mRevertCommand = revertCommand;
|
mRevertCommand = revertCommand;
|
||||||
|
mCheckSuccess = checkSuccess;
|
||||||
|
mAdditionalChecks = additionalChecks;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ShellCommandRule(String cmd, @Nullable String revertCommand) {
|
||||||
|
this(cmd, revertCommand, false, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -48,12 +63,27 @@ public class ShellCommandRule implements TestRule {
|
|||||||
return new Statement() {
|
return new Statement() {
|
||||||
@Override
|
@Override
|
||||||
public void evaluate() throws Throwable {
|
public void evaluate() throws Throwable {
|
||||||
UiDevice.getInstance(getInstrumentation()).executeShellCommand(mCmd);
|
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 {
|
try {
|
||||||
base.evaluate();
|
base.evaluate();
|
||||||
} finally {
|
} finally {
|
||||||
if (mRevertCommand != null) {
|
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.
|
* Sets the target launcher as default launcher.
|
||||||
*/
|
*/
|
||||||
public static ShellCommandRule setDefaultLauncher() {
|
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) {
|
public static String getLauncherCommand(ActivityInfo launcher) {
|
||||||
|
|||||||
Reference in New Issue
Block a user