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

Commit 6688f6ec authored by Michal Brzezinski's avatar Michal Brzezinski Committed by Michał Brzeziński
Browse files

Waiting for FakeLatencyTracker’s listener to be registered

Because listener inside LatencyTracker `startListeningForLatencyTrackerConfigChanges` is registered using `BackgroundThread.getHandler()` there’s no guarantee that it will be registered before test starts running and asserting stuff. 
This change allows passing Executor so we can control where adding callback - and callback itself - is executed.

Test: atest FrameworksCoreTests_jank:com.android.internal.util.LatencyTrackerTest --rerun-until-failure 100 - it also requires forcing Settings to not use cache for all tests to pass
Flag: EXEMPT bugfix
Bug: 412954344
Change-Id: Ia87486b30451baa0c1c9a78aaa489e242f906e2d
parent 4e5a274e
Loading
Loading
Loading
Loading
+9 −4
Original line number Diff line number Diff line
@@ -84,6 +84,7 @@ import com.android.internal.os.BackgroundThread;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.Locale;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;

@@ -441,7 +442,8 @@ public class LatencyTracker {

        static {
            sLatencyTracker = new LatencyTracker();
            sLatencyTracker.startListeningForLatencyTrackerConfigChanges();
            sLatencyTracker.startListeningForLatencyTrackerConfigChanges(
                    BackgroundThread.getExecutor());
        }
    }

@@ -496,10 +498,13 @@ public class LatencyTracker {
     *
     * <p>This is not used for production usages of this class outside of testing as we are
     * using a single static object.
     *
     * @param executor used for reading initial properties, listener registration and running
     *                 callbacks from that listener
     */
    @VisibleForTesting
    @RequiresPermission(Manifest.permission.READ_DEVICE_CONFIG)
    public void startListeningForLatencyTrackerConfigChanges() {
    public void startListeningForLatencyTrackerConfigChanges(Executor executor) {
        final Context context = ActivityThread.currentApplication();
        if (context == null) {
            Log.e(
@@ -521,12 +526,12 @@ public class LatencyTracker {
        }

        // Post initialization to the background in case we're running on the main thread.
        BackgroundThread.getHandler().post(() -> {
        executor.execute(() -> {
            try {
                this.updateProperties(
                        DeviceConfig.getProperties(NAMESPACE_LATENCY_TRACKER));
                DeviceConfig.addOnPropertiesChangedListener(NAMESPACE_LATENCY_TRACKER,
                        BackgroundThread.getExecutor(), mOnPropertiesChangedListener);
                        executor, mOnPropertiesChangedListener);
            } catch (SecurityException ex) {
                // In case of running tests that the main thread passes the check,
                // but the background thread doesn't have necessary permissions.
+1 −1
Original line number Diff line number Diff line
@@ -59,7 +59,7 @@ public final class FakeLatencyTracker extends LatencyTracker {
        Log.i(TAG, "done disabling all actions");
        FakeLatencyTracker fakeLatencyTracker = new FakeLatencyTracker();
        Log.i(TAG, "done creating tracker object");
        fakeLatencyTracker.startListeningForLatencyTrackerConfigChanges();
        fakeLatencyTracker.startListeningForLatencyTrackerConfigChanges(Runnable::run);
        // always return the fake in the disabled state and let the client control the desired state
        fakeLatencyTracker.waitForGlobalEnabledState(false);
        fakeLatencyTracker.waitForAllPropertiesEnableState(false);