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

Commit f450476c authored by Sunny Goyal's avatar Sunny Goyal Committed by Android (Google) Code Review
Browse files

Merge "Adding support for overriding flag values in test" into tm-qpr-dev

parents 750009fa b138ec9f
Loading
Loading
Loading
Loading
+12 −2
Original line number Diff line number Diff line
@@ -21,9 +21,14 @@ import static com.android.launcher3.uioverrides.flags.FlagsFactory.getReleaseFla

import android.content.Context;

import androidx.annotation.VisibleForTesting;

import com.android.launcher3.BuildConfig;
import com.android.launcher3.Utilities;

import java.util.function.Predicate;
import java.util.function.ToIntFunction;

/**
 * Defines a set of flags used to control various launcher behaviors.
 *
@@ -33,6 +38,11 @@ public final class FeatureFlags {

    public static final String FLAGS_PREF_NAME = "featureFlags";

    @VisibleForTesting
    public static Predicate<BooleanFlag> sBooleanReader = f -> f.mCurrentValue;
    @VisibleForTesting
    public static ToIntFunction<IntFlag> sIntReader = f -> f.mCurrentValue;

    private FeatureFlags() { }

    public static boolean showFlagTogglerUi(Context context) {
@@ -385,7 +395,7 @@ public final class FeatureFlags {
        }

        public boolean get() {
            return mCurrentValue;
            return sBooleanReader.test(this);
        }
    }

@@ -401,7 +411,7 @@ public final class FeatureFlags {
        }

        public int get() {
            return mCurrentValue;
            return sIntReader.applyAsInt(this);
        }
    }
}
+36 −0
Original line number Diff line number Diff line
@@ -27,12 +27,18 @@ import android.os.UserHandle;

import androidx.test.uiautomator.UiDevice;

import com.android.launcher3.config.FeatureFlags;
import com.android.launcher3.config.FeatureFlags.BooleanFlag;
import com.android.launcher3.config.FeatureFlags.IntFlag;

import org.junit.Assert;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.CountDownLatch;
import java.util.function.Predicate;
import java.util.function.ToIntFunction;

public class TestUtil {
    public static final String DUMMY_PACKAGE = "com.example.android.aardwolf";
@@ -68,6 +74,36 @@ public class TestUtil {
        }
    }

    /**
     * Utility class to override a boolean flag during test. Note that the returned SafeCloseable
     * must be closed to restore the original state
     */
    public static SafeCloseable overrideFlag(BooleanFlag flag, boolean value) {
        Predicate<BooleanFlag> originalProxy = FeatureFlags.sBooleanReader;
        Predicate<BooleanFlag> testProxy = f -> f == flag ? value : originalProxy.test(f);
        FeatureFlags.sBooleanReader = testProxy;
        return () -> {
            if (FeatureFlags.sBooleanReader == testProxy) {
                FeatureFlags.sBooleanReader = originalProxy;
            }
        };
    }

    /**
     * Utility class to override a int flag during test. Note that the returned SafeCloseable
     * must be closed to restore the original state
     */
    public static SafeCloseable overrideFlag(IntFlag flag, int value) {
        ToIntFunction<IntFlag> originalProxy = FeatureFlags.sIntReader;
        ToIntFunction<IntFlag> testProxy = f -> f == flag ? value : originalProxy.applyAsInt(f);
        FeatureFlags.sIntReader = testProxy;
        return () -> {
            if (FeatureFlags.sIntReader == testProxy) {
                FeatureFlags.sIntReader = originalProxy;
            }
        };
    }

    public static void uninstallDummyApp() throws IOException {
        UiDevice.getInstance(getInstrumentation()).executeShellCommand(
                "pm uninstall " + DUMMY_PACKAGE);