Merge "Disable the services before each test"

This commit is contained in:
Joanne Chung
2020-09-24 02:25:09 +00:00
committed by Android (Google) Code Review
4 changed files with 74 additions and 36 deletions

View File

@@ -16,19 +16,23 @@
package android.view.autofill;
import static com.android.compatibility.common.util.ShellUtils.runShellCommand;
import static org.junit.Assert.assertTrue;
import android.os.Looper;
import android.perftests.utils.PerfStatusReporter;
import android.perftests.utils.PerfTestActivity;
import android.perftests.utils.SettingsHelper;
import android.perftests.utils.SettingsStateKeeperRule;
import android.provider.Settings;
import android.util.Log;
import androidx.test.InstrumentationRegistry;
import androidx.test.rule.ActivityTestRule;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.rules.RuleChain;
@@ -38,6 +42,8 @@ import org.junit.rules.RuleChain;
*/
public abstract class AbstractAutofillPerfTestCase {
private static final String TAG = "AbstractAutofillPerfTestCase";
@ClassRule
public static final SettingsStateKeeperRule mServiceSettingsKeeper =
new SettingsStateKeeperRule(InstrumentationRegistry.getTargetContext(),
@@ -60,6 +66,27 @@ public abstract class AbstractAutofillPerfTestCase {
mLayoutId = layoutId;
}
@BeforeClass
public static void disableDefaultAugmentedService() {
Log.v(TAG, "@BeforeClass: disableDefaultAugmentedService()");
setDefaultAugmentedAutofillServiceEnabled(false);
}
@AfterClass
public static void enableDefaultAugmentedService() {
Log.v(TAG, "@AfterClass: enableDefaultAugmentedService()");
setDefaultAugmentedAutofillServiceEnabled(true);
}
/**
* Enables / disables the default augmented autofill service.
*/
private static void setDefaultAugmentedAutofillServiceEnabled(boolean enabled) {
Log.d(TAG, "setDefaultAugmentedAutofillServiceEnabled(): " + enabled);
runShellCommand("cmd autofill set default-augmented-service-enabled 0 %s",
Boolean.toString(enabled));
}
/**
* Prepares the activity so that by the time the test is run it has reference to its fields.
*/
@@ -80,23 +107,4 @@ public abstract class AbstractAutofillPerfTestCase {
* Initializes the {@link PerfTestActivity} after it was launched.
*/
protected abstract void onCreate(PerfTestActivity activity);
/**
* Uses the {@code settings} binary to set the autofill service.
*/
protected void setService() {
SettingsHelper.syncSet(InstrumentationRegistry.getTargetContext(),
SettingsHelper.NAMESPACE_SECURE,
Settings.Secure.AUTOFILL_SERVICE,
MyAutofillService.COMPONENT_NAME);
}
/**
* Uses the {@code settings} binary to reset the autofill service.
*/
protected void resetService() {
SettingsHelper.syncDelete(InstrumentationRegistry.getTargetContext(),
SettingsHelper.NAMESPACE_SECURE,
Settings.Secure.AUTOFILL_SERVICE);
}
}

View File

@@ -18,10 +18,13 @@ package android.view.autofill;
import static com.android.compatibility.common.util.ShellUtils.runShellCommand;
import android.perftests.utils.SettingsHelper;
import android.provider.Settings;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.test.InstrumentationRegistry;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;
@@ -44,19 +47,26 @@ final class AutofillTestWatcher extends TestWatcher {
@Override
protected void starting(Description description) {
super.starting(description);
final String testName = description.getDisplayName();
Log.i(TAG, "Starting " + testName);
enableVerboseLog();
MyAutofillService.resetStaticState();
MyAutofillService.setEnabled(true);
// Prepare the service before each test.
// Disable the current AutofillService.
resetAutofillService();
// Set MyAutofillService status enable, it can start to accept the calls.
enableMyAutofillService();
setServiceWatcher();
}
@Override
protected void finished(Description description) {
super.finished(description);
final String testName = description.getDisplayName();
Log.i(TAG, "Finished " + testName);
restoreLogLevel();
disableService();
// Set MyAutofillService status disable, so the calls are ignored.
disableMyAutofillService();
clearServiceWatcher();
}
@@ -67,12 +77,31 @@ final class AutofillTestWatcher extends TestWatcher {
}
}
private void enableService() {
/**
* Uses the {@code settings} binary to set the autofill service.
*/
void setAutofillService() {
SettingsHelper.syncSet(InstrumentationRegistry.getTargetContext(),
SettingsHelper.NAMESPACE_SECURE,
Settings.Secure.AUTOFILL_SERVICE,
MyAutofillService.COMPONENT_NAME);
}
/**
* Uses the {@code settings} binary to reset the autofill service.
*/
void resetAutofillService() {
SettingsHelper.syncDelete(InstrumentationRegistry.getTargetContext(),
SettingsHelper.NAMESPACE_SECURE,
Settings.Secure.AUTOFILL_SERVICE);
}
private void enableMyAutofillService() {
MyAutofillService.resetStaticState();
MyAutofillService.setEnabled(true);
}
private void disableService() {
private void disableMyAutofillService() {
// Must disable service so calls are ignored in case of errors during the test case;
// otherwise, other tests will fail because these calls are made in the UI thread (as both
// the service, the tests, and the app run in the same process).
@@ -88,7 +117,7 @@ final class AutofillTestWatcher extends TestWatcher {
}
private void restoreLogLevel() {
Log.w(TAG, "restoreLogLevel to " + mOriginalLogLevel);
Log.d(TAG, "restoreLogLevel to " + mOriginalLogLevel);
if (!mOriginalLogLevel.equals("verbose")) {
runShellCommand("cmd autofill set log_level %s", mOriginalLogLevel);
}

View File

@@ -55,7 +55,7 @@ public class LoginTest extends AbstractAutofillPerfTestCase {
*/
@Test
public void testFocus_noService() throws Throwable {
resetService();
mTestWatcher.resetAutofillService();
BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
while (state.keepRunning()) {
@@ -73,7 +73,7 @@ public class LoginTest extends AbstractAutofillPerfTestCase {
@Test
public void testFocus_serviceDoesNotAutofill() throws Throwable {
MyAutofillService.newCannedResponse().reply();
setService();
mTestWatcher.setAutofillService();
// Must first focus in a field to trigger autofill and wait for service response
// outside the loop
@@ -102,7 +102,7 @@ public class LoginTest extends AbstractAutofillPerfTestCase {
.setUsername(mUsername.getAutofillId(), "user")
.setPassword(mPassword.getAutofillId(), "pass")
.reply();
setService();
mTestWatcher.setAutofillService();
// Callback is used to slow down the calls made to the autofill server so the
// app is not crashed due to binder exhaustion. But the time spent waiting for the callbacks
@@ -157,7 +157,7 @@ public class LoginTest extends AbstractAutofillPerfTestCase {
.setUsername(mUsername.getAutofillId(), "user")
.setIgnored(mPassword.getAutofillId())
.reply();
setService();
mTestWatcher.setAutofillService();
// Callback is used to slow down the calls made to the autofill server so the
// app is not crashed due to binder exhaustion. But the time spent waiting for the callbacks
@@ -201,7 +201,7 @@ public class LoginTest extends AbstractAutofillPerfTestCase {
*/
@Test
public void testChange_noService() throws Throwable {
resetService();
mTestWatcher.resetAutofillService();
changeTest(false);
}
@@ -213,7 +213,7 @@ public class LoginTest extends AbstractAutofillPerfTestCase {
@Test
public void testChange_serviceDoesNotAutofill() throws Throwable {
MyAutofillService.newCannedResponse().reply();
setService();
mTestWatcher.setAutofillService();
changeTest(true);
}
@@ -227,7 +227,7 @@ public class LoginTest extends AbstractAutofillPerfTestCase {
.setUsername(mUsername.getAutofillId(), "user")
.setPassword(mPassword.getAutofillId(), "pass")
.reply();
setService();
mTestWatcher.setAutofillService();
changeTest(true);
}
@@ -242,7 +242,7 @@ public class LoginTest extends AbstractAutofillPerfTestCase {
.setUsername(mUsername.getAutofillId(), "user")
.setIgnored(mPassword.getAutofillId())
.reply();
setService();
mTestWatcher.setAutofillService();
changeTest(true);
}
@@ -274,7 +274,7 @@ public class LoginTest extends AbstractAutofillPerfTestCase {
.setUsername(mUsername.getAutofillId(), "user")
.setPassword(mPassword.getAutofillId(), "pass")
.reply();
setService();
mTestWatcher.setAutofillService();
MyAutofillCallback callback = new MyAutofillCallback();
mAfm.registerCallback(callback);

View File

@@ -126,6 +126,7 @@ public class MyAutofillService extends AutofillService {
onError("ignoring onFillRequest(): response not set", callback);
return;
}
// TODO(b/162216576): fix error FillResponse
Dataset.Builder dataset = new Dataset.Builder(newDatasetPresentation("dataset"));
boolean hasData = false;
if (response.mUsername != null) {