Adding tests for fallback recents when a 3rd party launcher is installed

Change-Id: I1d4da13cc779f49832008b12b9628d01631faffe
This commit is contained in:
Sunny Goyal
2018-10-05 16:19:38 -07:00
parent 9720452363
commit 4ed58d6c03
24 changed files with 609 additions and 206 deletions

View File

@@ -15,17 +15,20 @@
*/
package com.android.launcher3.util.rule;
import static com.android.launcher3.tapl.TestHelpers.getLauncherInMyProcess;
import static androidx.test.InstrumentationRegistry.getInstrumentation;
import android.content.ComponentName;
import android.content.pm.ActivityInfo;
import android.os.ParcelFileDescriptor;
import androidx.test.InstrumentationRegistry;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import java.io.FileInputStream;
import java.io.IOException;
import androidx.annotation.Nullable;
import androidx.test.InstrumentationRegistry;
import androidx.test.uiautomator.UiDevice;
/**
* Test rule which executes a shell command at the start of the test.
@@ -33,40 +36,28 @@ import java.io.IOException;
public class ShellCommandRule implements TestRule {
private final String mCmd;
private final String mRevertCommand;
public ShellCommandRule(String cmd) {
public ShellCommandRule(String cmd, @Nullable String revertCommand) {
mCmd = cmd;
mRevertCommand = revertCommand;
}
@Override
public Statement apply(Statement base, Description description) {
return new MyStatement(base, mCmd);
}
public static void runShellCommand(String command) throws IOException {
ParcelFileDescriptor pfd = InstrumentationRegistry.getInstrumentation().getUiAutomation()
.executeShellCommand(command);
// Read the input stream fully.
FileInputStream fis = new ParcelFileDescriptor.AutoCloseInputStream(pfd);
while (fis.read() != -1);
fis.close();
}
private static class MyStatement extends Statement {
private final Statement mBase;
private final String mCmd;
public MyStatement(Statement base, String cmd) {
mBase = base;
mCmd = cmd;
}
@Override
public void evaluate() throws Throwable {
runShellCommand(mCmd);
mBase.evaluate();
}
return new Statement() {
@Override
public void evaluate() throws Throwable {
UiDevice.getInstance(getInstrumentation()).executeShellCommand(mCmd);
try {
base.evaluate();
} finally {
if (mRevertCommand != null) {
UiDevice.getInstance(getInstrumentation()).executeShellCommand(mRevertCommand);
}
}
}
};
}
/**
@@ -74,17 +65,26 @@ public class ShellCommandRule implements TestRule {
*/
public static ShellCommandRule grandWidgetBind() {
return new ShellCommandRule("appwidget grantbind --package "
+ InstrumentationRegistry.getTargetContext().getPackageName());
+ InstrumentationRegistry.getTargetContext().getPackageName(), null);
}
/**
* Sets the target launcher as default launcher.
*/
public static ShellCommandRule setDefaultLauncher() {
ActivityInfo launcher = InstrumentationRegistry.getTargetContext().getPackageManager()
.queryIntentActivities(LauncherActivityRule.getHomeIntent(), 0).get(0)
.activityInfo;
return new ShellCommandRule("cmd package set-home-activity " +
new ComponentName(launcher.packageName, launcher.name).flattenToString());
return new ShellCommandRule(getLauncherCommand(getLauncherInMyProcess()), null);
}
public static String getLauncherCommand(ActivityInfo launcher) {
return "cmd package set-home-activity " +
new ComponentName(launcher.packageName, launcher.name).flattenToString();
}
/**
* Disables heads up notification for the duration of the test
*/
public static ShellCommandRule disableHeadsUpNotification() {
return new ShellCommandRule("settings put global heads_up_notifications_enabled 0",
"settings put global heads_up_notifications_enabled 1");
}
}