Android: Add ResettersForTesting.register() to //base
Used Android Studio's "Structured Replace" to add it to all setForTesting methods that contained a single assignment. Then went through all spots to reset to "null/false" vs |oldValue|. Removed calls to modified set*ForTesting(null/false) methods using grep/sed/python then removed empty @After methods. * Also cleans up some calls that had both @VisibleForTesting and ForTest suffix (annotation is incorrect in this case). * Also renamed a few test-only variables to have "ForTesting" suffix. Updated tests that broke as a result. Things that broke: * When setForTesting() was being called in @BeforeClass * When a value was set as a result of normal logic, and then setForTesting(null) was called to reset it (which then returned it to it's non-null value). Bug: 1416224 Change-Id: Id07066389f4ec0d69ebf4e3e1e6caca356cc36d4 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4626902 Reviewed-by: Tommy Nyquist <nyquist@chromium.org> Commit-Queue: Andrew Grieve <agrieve@chromium.org> Code-Coverage: Findit <findit-for-me@appspot.gserviceaccount.com> Cr-Commit-Position: refs/heads/main@{#1160862}
This commit is contained in:

committed by
Chromium LUCI CQ

parent
c880f90e67
commit
408a40cb7a
base
android
java
src
org
chromium
base
test
android
junit
src
org
chromium
base
@ -596,5 +596,6 @@ public class IntentUtils {
|
||||
|
||||
public static void setForceIsTrustedIntentForTesting(boolean isTrusted) {
|
||||
sForceTrustedIntentForTesting = isTrusted;
|
||||
ResettersForTesting.register(() -> sForceTrustedIntentForTesting = false);
|
||||
}
|
||||
}
|
||||
|
@ -13,8 +13,6 @@ import android.os.StatFs;
|
||||
import android.os.StrictMode;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
|
||||
import org.chromium.base.annotations.CalledByNative;
|
||||
import org.chromium.base.annotations.JNINamespace;
|
||||
import org.chromium.base.annotations.NativeMethods;
|
||||
@ -147,7 +145,6 @@ public class SysUtils {
|
||||
/**
|
||||
* Resets the cached value, if any.
|
||||
*/
|
||||
@VisibleForTesting
|
||||
public static void resetForTesting() {
|
||||
sLowEndDevice = null;
|
||||
sAmountOfPhysicalMemoryKB = null;
|
||||
@ -211,9 +208,12 @@ public class SysUtils {
|
||||
return false;
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public static void setAmountOfPhysicalMemoryKBForTesting(int physicalMemoryKB) {
|
||||
sAmountOfPhysicalMemoryKB = physicalMemoryKB;
|
||||
ResettersForTesting.register(() -> {
|
||||
sLowEndDevice = null;
|
||||
sAmountOfPhysicalMemoryKB = null;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -8,8 +8,6 @@ import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.os.Process;
|
||||
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
|
||||
import org.chromium.base.annotations.CalledByNative;
|
||||
import org.chromium.base.task.PostTask;
|
||||
import org.chromium.base.task.TaskTraits;
|
||||
@ -29,7 +27,7 @@ public class ThreadUtils {
|
||||
|
||||
private static volatile Handler sUiThreadHandler;
|
||||
|
||||
private static boolean sThreadAssertsDisabled;
|
||||
private static boolean sThreadAssertsDisabledForTesting;
|
||||
|
||||
/**
|
||||
* A helper object to ensure that interactions with a particular object only happens on a
|
||||
@ -71,7 +69,7 @@ public class ThreadUtils {
|
||||
* on.
|
||||
*/
|
||||
public void assertOnValidThread() {
|
||||
assert sThreadAssertsDisabled
|
||||
assert sThreadAssertsDisabledForTesting
|
||||
|| mThreadId == Process.myTid() : "Must only be used on a single thread.";
|
||||
}
|
||||
}
|
||||
@ -81,7 +79,6 @@ public class ThreadUtils {
|
||||
assert sUiThreadHandler == null;
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public static void clearUiThreadForTesting() {
|
||||
sWillOverride = false;
|
||||
PostTask.resetUiThreadForTesting(); // IN-TEST
|
||||
@ -236,7 +233,7 @@ public class ThreadUtils {
|
||||
* Can be disabled by setThreadAssertsDisabledForTesting(true).
|
||||
*/
|
||||
public static void assertOnUiThread() {
|
||||
if (sThreadAssertsDisabled) return;
|
||||
if (sThreadAssertsDisabledForTesting) return;
|
||||
|
||||
assert runningOnUiThread() : "Must be called on the UI thread.";
|
||||
}
|
||||
@ -249,7 +246,7 @@ public class ThreadUtils {
|
||||
* @see #assertOnUiThread()
|
||||
*/
|
||||
public static void checkUiThread() {
|
||||
if (!sThreadAssertsDisabled && !runningOnUiThread()) {
|
||||
if (!sThreadAssertsDisabledForTesting && !runningOnUiThread()) {
|
||||
throw new IllegalStateException("Must be called on the UI thread.");
|
||||
}
|
||||
}
|
||||
@ -260,7 +257,7 @@ public class ThreadUtils {
|
||||
* Can be disabled by setThreadAssertsDisabledForTesting(true).
|
||||
*/
|
||||
public static void assertOnBackgroundThread() {
|
||||
if (sThreadAssertsDisabled) return;
|
||||
if (sThreadAssertsDisabledForTesting) return;
|
||||
|
||||
assert !runningOnUiThread() : "Must be called on a thread other than UI.";
|
||||
}
|
||||
@ -273,7 +270,8 @@ public class ThreadUtils {
|
||||
* those tests).
|
||||
*/
|
||||
public static void setThreadAssertsDisabledForTesting(boolean disabled) {
|
||||
sThreadAssertsDisabled = disabled;
|
||||
sThreadAssertsDisabledForTesting = disabled;
|
||||
ResettersForTesting.register(() -> sThreadAssertsDisabledForTesting = false);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -21,6 +21,7 @@ import org.chromium.base.ContextUtils;
|
||||
import org.chromium.base.Log;
|
||||
import org.chromium.base.NativeLibraryLoadedStatus;
|
||||
import org.chromium.base.NativeLibraryLoadedStatus.NativeLibraryLoadedStatusProvider;
|
||||
import org.chromium.base.ResettersForTesting;
|
||||
import org.chromium.base.StrictModeContext;
|
||||
import org.chromium.base.TimeUtils.CurrentThreadTimeMillisTimer;
|
||||
import org.chromium.base.TimeUtils.UptimeMillisTimer;
|
||||
@ -897,9 +898,10 @@ public class LibraryLoader {
|
||||
* @param loader the mock library loader.
|
||||
*/
|
||||
@Deprecated
|
||||
@VisibleForTesting
|
||||
public static void setLibraryLoaderForTesting(LibraryLoader loader) {
|
||||
var oldValue = sInstance;
|
||||
sInstance = loader;
|
||||
ResettersForTesting.register(() -> sInstance = oldValue);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -13,6 +13,7 @@ import androidx.annotation.VisibleForTesting;
|
||||
import org.chromium.base.ContextUtils;
|
||||
import org.chromium.base.MemoryPressureLevel;
|
||||
import org.chromium.base.MemoryPressureListener;
|
||||
import org.chromium.base.ResettersForTesting;
|
||||
import org.chromium.base.ThreadUtils;
|
||||
import org.chromium.base.supplier.Supplier;
|
||||
import org.chromium.build.annotations.MainDex;
|
||||
@ -88,13 +89,8 @@ public class MemoryPressureMonitor {
|
||||
|
||||
private boolean mPollingEnabled;
|
||||
|
||||
// Changed by tests.
|
||||
private Supplier<Integer> mCurrentPressureSupplier =
|
||||
MemoryPressureMonitor::getCurrentMemoryPressure;
|
||||
|
||||
// Changed by tests.
|
||||
private MemoryPressureCallback mReportingCallback =
|
||||
MemoryPressureListener::notifyMemoryPressure;
|
||||
private Supplier<Integer> mCurrentPressureSupplierForTesting;
|
||||
private MemoryPressureCallback mReportingCallbackForTesting;
|
||||
|
||||
private final Runnable mThrottlingIntervalTask = this ::onThrottlingIntervalFinished;
|
||||
|
||||
@ -190,7 +186,11 @@ public class MemoryPressureMonitor {
|
||||
startThrottlingInterval();
|
||||
|
||||
mLastReportedPressure = pressure;
|
||||
mReportingCallback.onPressure(pressure);
|
||||
if (mReportingCallbackForTesting != null) {
|
||||
mReportingCallbackForTesting.onPressure(pressure);
|
||||
} else {
|
||||
MemoryPressureListener.notifyMemoryPressure(pressure);
|
||||
}
|
||||
}
|
||||
|
||||
private void onThrottlingIntervalFinished() {
|
||||
@ -212,7 +212,9 @@ public class MemoryPressureMonitor {
|
||||
}
|
||||
|
||||
private void reportCurrentPressure() {
|
||||
Integer pressure = mCurrentPressureSupplier.get();
|
||||
Integer pressure = mCurrentPressureSupplierForTesting != null
|
||||
? mCurrentPressureSupplierForTesting.get()
|
||||
: MemoryPressureMonitor.getCurrentMemoryPressure();
|
||||
if (pressure != null) {
|
||||
reportPressure(pressure);
|
||||
}
|
||||
@ -223,14 +225,14 @@ public class MemoryPressureMonitor {
|
||||
mIsInsideThrottlingInterval = true;
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public void setCurrentPressureSupplierForTesting(Supplier<Integer> supplier) {
|
||||
mCurrentPressureSupplier = supplier;
|
||||
mCurrentPressureSupplierForTesting = supplier;
|
||||
ResettersForTesting.register(() -> mCurrentPressureSupplierForTesting = null);
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public void setReportingCallbackForTesting(MemoryPressureCallback callback) {
|
||||
mReportingCallback = callback;
|
||||
mReportingCallbackForTesting = callback;
|
||||
ResettersForTesting.register(() -> mReportingCallbackForTesting = null);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -20,6 +20,7 @@ import androidx.collection.ArraySet;
|
||||
import org.chromium.base.BuildInfo;
|
||||
import org.chromium.base.ContextUtils;
|
||||
import org.chromium.base.Log;
|
||||
import org.chromium.base.ResettersForTesting;
|
||||
import org.chromium.base.SysUtils;
|
||||
import org.chromium.base.compat.ApiHelperForM;
|
||||
|
||||
@ -188,7 +189,6 @@ public abstract class ChildConnectionAllocator {
|
||||
* Factory method used with some tests to create an allocator with values passed in directly
|
||||
* instead of being retrieved from the AndroidManifest.xml.
|
||||
*/
|
||||
@VisibleForTesting
|
||||
public static FixedSizeAllocatorImpl createFixedForTesting(Runnable freeSlotCallback,
|
||||
String packageName, String serviceClassName, int serviceCount, boolean bindToCaller,
|
||||
boolean bindAsExternalService, boolean useStrongBinding) {
|
||||
@ -197,7 +197,6 @@ public abstract class ChildConnectionAllocator {
|
||||
serviceCount);
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public static VariableSizeAllocatorImpl createVariableSizeForTesting(Handler launcherHandler,
|
||||
String packageName, Runnable freeSlotCallback, String serviceClassName,
|
||||
boolean bindToCaller, boolean bindAsExternalService, boolean useStrongBinding,
|
||||
@ -207,7 +206,6 @@ public abstract class ChildConnectionAllocator {
|
||||
useStrongBinding, maxAllocated);
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public static Android10WorkaroundAllocatorImpl createWorkaroundForTesting(
|
||||
Handler launcherHandler, String packageName, Runnable freeSlotCallback,
|
||||
String serviceClassName, boolean bindToCaller, boolean bindAsExternalService,
|
||||
@ -329,12 +327,12 @@ public abstract class ChildConnectionAllocator {
|
||||
public abstract boolean anyConnectionAllocated();
|
||||
|
||||
/** @return the count of connections managed by the allocator */
|
||||
@VisibleForTesting
|
||||
public abstract int allocatedConnectionsCountForTesting();
|
||||
|
||||
@VisibleForTesting
|
||||
public void setConnectionFactoryForTesting(ConnectionFactory connectionFactory) {
|
||||
var oldValue = mConnectionFactory;
|
||||
mConnectionFactory = connectionFactory;
|
||||
ResettersForTesting.register(() -> mConnectionFactory = oldValue);
|
||||
}
|
||||
|
||||
private boolean isRunningOnLauncherThread() {
|
||||
@ -422,7 +420,6 @@ public abstract class ChildConnectionAllocator {
|
||||
return mChildProcessConnections.length - mFreeConnectionIndices.size();
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public ChildProcessConnection getChildProcessConnectionAtSlotForTesting(int slotNumber) {
|
||||
return mChildProcessConnections[slotNumber];
|
||||
}
|
||||
|
@ -10,6 +10,7 @@ import androidx.annotation.Nullable;
|
||||
|
||||
import org.chromium.base.Callback;
|
||||
import org.chromium.base.ObserverList;
|
||||
import org.chromium.base.ResettersForTesting;
|
||||
|
||||
/**
|
||||
* Concrete implementation of {@link ObservableSupplier} to be used by classes owning the
|
||||
@ -87,5 +88,6 @@ public class ObservableSupplierImpl<E> implements ObservableSupplier<E> {
|
||||
/** Used to allow developers to access supplier values on the instrumentation thread. */
|
||||
public static void setIgnoreThreadChecksForTesting(boolean ignoreThreadChecks) {
|
||||
sIgnoreThreadChecksForTesting = ignoreThreadChecks;
|
||||
ResettersForTesting.register(() -> sIgnoreThreadChecksForTesting = false);
|
||||
}
|
||||
}
|
||||
|
@ -7,6 +7,7 @@ package org.chromium.base.task;
|
||||
import android.os.Handler;
|
||||
|
||||
import org.chromium.base.Log;
|
||||
import org.chromium.base.ResettersForTesting;
|
||||
import org.chromium.base.ThreadUtils;
|
||||
import org.chromium.base.annotations.CalledByNative;
|
||||
import org.chromium.base.annotations.JNINamespace;
|
||||
@ -38,7 +39,7 @@ public class PostTask {
|
||||
private static volatile boolean sNativeInitialized;
|
||||
private static ChromeThreadPoolExecutor sPrenativeThreadPoolExecutor =
|
||||
new ChromeThreadPoolExecutor();
|
||||
private static volatile Executor sPrenativeThreadPoolExecutorOverride;
|
||||
private static volatile Executor sPrenativeThreadPoolExecutorForTesting;
|
||||
|
||||
private static final ThreadPoolTaskExecutor sThreadPoolTaskExecutor =
|
||||
new ThreadPoolTaskExecutor();
|
||||
@ -180,22 +181,23 @@ public class PostTask {
|
||||
* @param executor The Executor to use for pre-native thread pool tasks.
|
||||
*/
|
||||
public static void setPrenativeThreadPoolExecutorForTesting(Executor executor) {
|
||||
sPrenativeThreadPoolExecutorOverride = executor;
|
||||
sPrenativeThreadPoolExecutorForTesting = executor;
|
||||
ResettersForTesting.register(() -> sPrenativeThreadPoolExecutorForTesting = null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears an override set by setPrenativeThreadPoolExecutorOverrideForTesting.
|
||||
*/
|
||||
public static void resetPrenativeThreadPoolExecutorForTesting() {
|
||||
sPrenativeThreadPoolExecutorOverride = null;
|
||||
sPrenativeThreadPoolExecutorForTesting = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The current Executor that PrenativeThreadPool tasks should run on.
|
||||
*/
|
||||
static Executor getPrenativeThreadPoolExecutor() {
|
||||
if (sPrenativeThreadPoolExecutorOverride != null) {
|
||||
return sPrenativeThreadPoolExecutorOverride;
|
||||
if (sPrenativeThreadPoolExecutorForTesting != null) {
|
||||
return sPrenativeThreadPoolExecutorForTesting;
|
||||
}
|
||||
return sPrenativeThreadPoolExecutor;
|
||||
}
|
||||
|
@ -74,7 +74,6 @@ public class BaseRobolectricTestRunner extends RobolectricTestRunner {
|
||||
ApplicationStatus.destroyForJUnitTests();
|
||||
ContextUtils.clearApplicationContextForTests();
|
||||
PathUtils.resetForTesting();
|
||||
ThreadUtils.setThreadAssertsDisabledForTesting(false);
|
||||
ThreadUtils.clearUiThreadForTesting();
|
||||
super.afterTest(method);
|
||||
}
|
||||
|
Reference in New Issue
Block a user