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

Commit e4d05388 authored by Felipe Leme's avatar Felipe Leme
Browse files

Uses a custom JUnit rule to preserve value of autofill service settings.

Resetting it is also fine, but it could accidentally "solve" performance issues
on other tests that are affected by the default service set by the OEM.

Test: mmma -j ./frameworks/base/apct-tests/perftests/core/ &&  \
  adb install -r $OUT/data/app/CorePerfTests/CorePerfTests.apk && \
  adb shell am instrument -w -e class android.view.autofill.AutofillPerfTest \
  com.android.perftests.core/android.support.test.runner.AndroidJUnitRunner

Bug: 38345816

Change-Id: Ic196ef3140697e64a5feb39f6b5363387fbf0b14
parent f9dc8d96
Loading
Loading
Loading
Loading
+6 −5
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ import android.os.Looper;
import android.os.Bundle;
import android.perftests.utils.PerfStatusReporter;
import android.perftests.utils.SettingsHelper;
import android.perftests.utils.SettingsStateKeeperRule;
import android.perftests.utils.ShellHelper;
import android.util.Log;
import android.view.View;
@@ -42,6 +43,7 @@ import java.util.Arrays;
import org.junit.Test;
import org.junit.After;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
@@ -67,6 +69,10 @@ public class AutofillPerfTest {
        mLayoutId = layoutId;
    }

    @ClassRule
    public static final SettingsStateKeeperRule mServiceSettingsKeeper = new SettingsStateKeeperRule(
            InstrumentationRegistry.getTargetContext(), Settings.Secure.AUTOFILL_SERVICE);

    @Rule
    public ActivityTestRule<StubActivity> mActivityRule =
            new ActivityTestRule<StubActivity>(StubActivity.class);
@@ -97,11 +103,6 @@ public class AutofillPerfTest {
        MyAutofillService.resetStaticState();
    }

    @After
    public void cleanup() {
        resetService();
    }

    /**
     * This is the baseline test for focusing the 2 views when autofill is disabled.
     */
+39 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package android.perftests.utils;

import android.content.Context;
import android.provider.Settings;

import androidx.annotation.NonNull;

/**
 * JUnit rule used to restore a {@link Settings} preference after the test is run.
 *
 * <p>It stores the current value before the test, and restores it after the test (if necessary).
 */
public class SettingsStateKeeperRule extends StateKeeperRule<String> {

    /**
     * Default constructor.
     *
     * @param context context used to retrieve the {@link Settings} provider.
     * @param key prefence key.
     */
    public SettingsStateKeeperRule(@NonNull Context context, @NonNull String key) {
        super(new SettingsStateManager(context, SettingsHelper.NAMESPACE_SECURE, key));
    }
}
+63 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.perftests.utils;

import android.content.Context;
import android.provider.Settings;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * Manages the state of a preference backed by {@link Settings}.
 */
public class SettingsStateManager implements StateManager<String> {

    private final Context mContext;
    private final String mNamespace;
    private final String mKey;

    /**
     * Default constructor.
     *
     * @param context context used to retrieve the {@link Settings} provider.
     * @param namespace settings namespace.
     * @param key prefence key.
     */
    public SettingsStateManager(@NonNull Context context, @NonNull String namespace,
            @NonNull String key) {
        mContext = context;
        mNamespace = namespace;
        mKey = key;
    }

    @Override
    public void set(@Nullable String value) {
        SettingsHelper.syncSet(mContext, mNamespace, mKey, value);
    }

    @Override
    @Nullable
    public String get() {
        return SettingsHelper.get(mNamespace, mKey);
    }

    @Override
    public String toString() {
        return "SettingsStateManager[namespace=" + mNamespace + ", key=" + mKey + "]";
    }
}
+63 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.perftests.utils;

import androidx.annotation.NonNull;

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

import java.util.Objects;

/**
 * JUnit rule used to restore a state after the test is run.
 *
 * <p>It stores the current state before the test, and restores it after the test (if necessary).
 */
public class StateKeeperRule<T> implements TestRule {

    private final StateManager<T> mStateManager;

    /**
     * Default constructor.
     *
     * @param stateManager abstraction used to manage the state.
     */
    public StateKeeperRule(StateManager<T> stateManager) {
        mStateManager = stateManager;
    }

    @Override
    public Statement apply(Statement base, Description description) {
        return new Statement() {

            @Override
            public void evaluate() throws Throwable {
                final T previousValue = mStateManager.get();
                try {
                    base.evaluate();
                } finally {
                    final T currentValue = mStateManager.get();
                    if (!Objects.equals(previousValue, currentValue)) {
                        mStateManager.set(previousValue);
                    }
                }
            }
        };
    }
}
+34 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package android.perftests.utils;

import androidx.annotation.Nullable;

/**
 * Abstraction for a state that is managed somewhere, like Android Settings.
 */
public interface StateManager<T> {

    /**
     * Sets a new state.
     */
    void set(@Nullable T value);

    /**
     * Gets the current state.
     */
    @Nullable T get();
}