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

Commit 00249338 authored by Fabian Kozynski's avatar Fabian Kozynski
Browse files

Deflake test in AppOpsControllerTest

Remove time dependence on the test. Instead, fake delay by storing the
scheduling until later.

Test: atest AppOpsControllerTest
Fixes: 156569031
Change-Id: I01ccd2e4a577d688fc8d592fdfd69542186000a0
parent f7b69012
Loading
Loading
Loading
Loading
+29 −10
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package com.android.systemui.appops;
import static junit.framework.TestCase.assertFalse;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
@@ -29,9 +30,8 @@ import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

import static java.lang.Thread.sleep;

import android.app.AppOpsManager;
import android.os.Looper;
import android.os.UserHandle;
import android.testing.AndroidTestingRunner;
import android.testing.TestableLooper;
@@ -229,12 +229,7 @@ public class AppOpsControllerTest extends SysuiTestCase {
    @Test
    public void testActiveOpNotRemovedAfterNoted() throws InterruptedException {
        // Replaces the timeout delay with 5 ms
        AppOpsControllerImpl.H testHandler = mController.new H(mTestableLooper.getLooper()) {
            @Override
            public void scheduleRemoval(AppOpItem item, long timeToRemoval) {
                super.scheduleRemoval(item, 5L);
            }
        };
        TestHandler testHandler = new TestHandler(mTestableLooper.getLooper());

        mController.addCallback(new int[]{AppOpsManager.OP_FINE_LOCATION}, mCallback);
        mController.setBGHandler(testHandler);
@@ -245,6 +240,10 @@ public class AppOpsControllerTest extends SysuiTestCase {
        mController.onOpNoted(AppOpsManager.OP_FINE_LOCATION, TEST_UID, TEST_PACKAGE_NAME,
                AppOpsManager.MODE_ALLOWED);

        // Check that we "scheduled" the removal. Don't actually schedule until we are ready to
        // process messages at a later time.
        assertNotNull(testHandler.mDelayScheduled);

        mTestableLooper.processAllMessages();
        List<AppOpItem> list = mController.getActiveAppOps();
        verify(mCallback).onActiveStateChanged(
@@ -253,8 +252,8 @@ public class AppOpsControllerTest extends SysuiTestCase {
        // Duplicates are not removed between active and noted
        assertEquals(2, list.size());

        sleep(10L);

        // Now is later, so we can schedule delayed messages.
        testHandler.scheduleDelayed();
        mTestableLooper.processAllMessages();

        verify(mCallback, never()).onActiveStateChanged(
@@ -321,4 +320,24 @@ public class AppOpsControllerTest extends SysuiTestCase {
        verify(mCallback).onActiveStateChanged(
                AppOpsManager.OP_FINE_LOCATION, TEST_UID, TEST_PACKAGE_NAME, true);
    }

    private class TestHandler extends AppOpsControllerImpl.H {
        TestHandler(Looper looper) {
            mController.super(looper);
        }

        Runnable mDelayScheduled;

        void scheduleDelayed() {
            if (mDelayScheduled != null) {
                mDelayScheduled.run();
                mDelayScheduled = null;
            }
        }

        @Override
        public void scheduleRemoval(AppOpItem item, long timeToRemoval) {
            mDelayScheduled = () -> super.scheduleRemoval(item, 0L);
        }
    }
}