diff --git a/packages/SystemUI/tests/src/com/android/systemui/appops/AppOpsControllerTest.java b/packages/SystemUI/tests/src/com/android/systemui/appops/AppOpsControllerTest.java index 50b7af27f7d99..e0049d1349f16 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/appops/AppOpsControllerTest.java +++ b/packages/SystemUI/tests/src/com/android/systemui/appops/AppOpsControllerTest.java @@ -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 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); + } + } }