diff options
| author | 2023-12-11 17:42:02 +0000 | |
|---|---|---|
| committer | 2023-12-11 17:42:02 +0000 | |
| commit | aa03774d08b99761bd1aa0b35beec3e100b2802f (patch) | |
| tree | 40f5f677921ea0b2e68959b6d9c87358a021e506 | |
| parent | f350b43332217e8c810d08e117cebd2b615b9cc8 (diff) | |
| parent | 8036c4fa67b9bf8f76507822609b6095f29186cd (diff) | |
Merge "Convert HeadsUpManager et al to use an Executor" into main
7 files changed, 130 insertions, 179 deletions
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/AlertingNotificationManager.java b/packages/SystemUI/src/com/android/systemui/statusbar/AlertingNotificationManager.java index d23c85a6d796..cfbd015f1cd8 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/AlertingNotificationManager.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/AlertingNotificationManager.java @@ -18,16 +18,16 @@ package com.android.systemui.statusbar; import android.annotation.NonNull; import android.annotation.Nullable; -import android.os.Handler; import android.util.ArrayMap; import android.util.ArraySet; +import android.util.Log; import android.view.accessibility.AccessibilityEvent; -import com.android.internal.annotations.VisibleForTesting; import com.android.systemui.dagger.qualifiers.Main; import com.android.systemui.statusbar.notification.collection.NotificationEntry; import com.android.systemui.statusbar.notification.row.NotificationRowContentBinder.InflationFlag; import com.android.systemui.statusbar.policy.HeadsUpManagerLogger; +import com.android.systemui.util.concurrency.DelayableExecutor; import com.android.systemui.util.time.SystemClock; import java.util.stream.Stream; @@ -46,13 +46,12 @@ public abstract class AlertingNotificationManager { protected int mMinimumDisplayTime; protected int mStickyForSomeTimeAutoDismissTime; protected int mAutoDismissTime; - @VisibleForTesting - public Handler mHandler; + private DelayableExecutor mExecutor; - public AlertingNotificationManager(HeadsUpManagerLogger logger, @Main Handler handler, - SystemClock systemClock) { + public AlertingNotificationManager(HeadsUpManagerLogger logger, + SystemClock systemClock, @Main DelayableExecutor executor) { mLogger = logger; - mHandler = handler; + mExecutor = executor; mSystemClock = systemClock; } @@ -264,6 +263,7 @@ public abstract class AlertingNotificationManager { public long mEarliestRemovalTime; @Nullable protected Runnable mRemoveAlertRunnable; + @Nullable private Runnable mCancelRemoveAlertRunnable; public void setEntry(@NonNull final NotificationEntry entry) { setEntry(entry, () -> removeAlertEntry(entry.getKey())); @@ -291,13 +291,15 @@ public abstract class AlertingNotificationManager { if (updatePostTime) { mPostTime = Math.max(mPostTime, now); } - removeAutoRemovalCallbacks("updateEntry (will be rescheduled)"); - if (!isSticky()) { - final long finishTime = calculateFinishTime(); - final long timeLeft = Math.max(finishTime - now, mMinimumDisplayTime); - mHandler.postDelayed(mRemoveAlertRunnable, timeLeft); + if (isSticky()) { + removeAutoRemovalCallbacks("updateEntry (sticky)"); + return; } + + final long finishTime = calculateFinishTime(); + final long timeLeft = Math.max(finishTime - now, mMinimumDisplayTime); + scheduleAutoRemovalCallback(timeLeft, "updateEntry (not sticky)"); } /** @@ -340,21 +342,50 @@ public abstract class AlertingNotificationManager { * Clear any pending removal runnables. */ public void removeAutoRemovalCallbacks(@Nullable String reason) { - if (mRemoveAlertRunnable != null) { + final boolean removed = removeAutoRemovalCallbackInternal(); + + if (removed) { mLogger.logAutoRemoveCanceled(mEntry, reason); - mHandler.removeCallbacks(mRemoveAlertRunnable); } } + private void scheduleAutoRemovalCallback(long delayMillis, @NonNull String reason) { + if (mRemoveAlertRunnable == null) { + Log.wtf(TAG, "scheduleAutoRemovalCallback with no callback set"); + return; + } + + final boolean removed = removeAutoRemovalCallbackInternal(); + + if (removed) { + mLogger.logAutoRemoveRescheduled(mEntry, delayMillis, reason); + } else { + mLogger.logAutoRemoveScheduled(mEntry, delayMillis, reason); + } + + + mCancelRemoveAlertRunnable = mExecutor.executeDelayed(mRemoveAlertRunnable, + delayMillis); + } + + private boolean removeAutoRemovalCallbackInternal() { + final boolean scheduled = (mCancelRemoveAlertRunnable != null); + + if (scheduled) { + mCancelRemoveAlertRunnable.run(); + mCancelRemoveAlertRunnable = null; + } + + return scheduled; + } + /** * Remove the alert at the earliest allowed removal time. */ public void removeAsSoonAsPossible() { if (mRemoveAlertRunnable != null) { - removeAutoRemovalCallbacks("removeAsSoonAsPossible (will be rescheduled)"); - final long timeLeft = mEarliestRemovalTime - mSystemClock.elapsedRealtime(); - mHandler.postDelayed(mRemoveAlertRunnable, timeLeft); + scheduleAutoRemovalCallback(timeLeft, "removeAsSoonAsPossible"); } } diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/HeadsUpManagerPhone.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/HeadsUpManagerPhone.java index 644c8962b93d..e66d9e8ca531 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/HeadsUpManagerPhone.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/HeadsUpManagerPhone.java @@ -48,6 +48,7 @@ import com.android.systemui.statusbar.policy.ConfigurationController; import com.android.systemui.statusbar.policy.HeadsUpManagerLogger; import com.android.systemui.statusbar.policy.OnHeadsUpChangedListener; import com.android.systemui.statusbar.policy.OnHeadsUpPhoneListenerChange; +import com.android.systemui.util.concurrency.DelayableExecutor; import com.android.systemui.util.kotlin.JavaAdapter; import com.android.systemui.util.settings.GlobalSettings; import com.android.systemui.util.time.SystemClock; @@ -119,12 +120,13 @@ public class HeadsUpManagerPhone extends BaseHeadsUpManager implements OnHeadsUp @Main Handler handler, GlobalSettings globalSettings, SystemClock systemClock, + @Main DelayableExecutor executor, AccessibilityManagerWrapper accessibilityManagerWrapper, UiEventLogger uiEventLogger, JavaAdapter javaAdapter, ShadeInteractor shadeInteractor) { - super(context, logger, handler, globalSettings, systemClock, accessibilityManagerWrapper, - uiEventLogger); + super(context, logger, handler, globalSettings, systemClock, executor, + accessibilityManagerWrapper, uiEventLogger); Resources resources = mContext.getResources(); mExtensionTime = resources.getInteger(R.integer.ambient_notification_extension_time); statusBarStateController.addCallback(mStatusBarStateListener); diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/BaseHeadsUpManager.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/BaseHeadsUpManager.java index 8054b04529c8..e9711284cfca 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/BaseHeadsUpManager.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/BaseHeadsUpManager.java @@ -38,6 +38,7 @@ import com.android.systemui.statusbar.AlertingNotificationManager; import com.android.systemui.statusbar.notification.collection.NotificationEntry; import com.android.systemui.statusbar.notification.row.NotificationRowContentBinder.InflationFlag; import com.android.systemui.util.ListenerSet; +import com.android.systemui.util.concurrency.DelayableExecutor; import com.android.systemui.util.settings.GlobalSettings; import com.android.systemui.util.time.SystemClock; @@ -87,9 +88,10 @@ public abstract class BaseHeadsUpManager extends AlertingNotificationManager imp @Main Handler handler, GlobalSettings globalSettings, SystemClock systemClock, + @Main DelayableExecutor executor, AccessibilityManagerWrapper accessibilityManagerWrapper, UiEventLogger uiEventLogger) { - super(logger, handler, systemClock); + super(logger, systemClock, executor); mContext = context; mAccessibilityMgr = accessibilityManagerWrapper; mUiEventLogger = uiEventLogger; diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/HeadsUpManagerLogger.kt b/packages/SystemUI/src/com/android/systemui/statusbar/policy/HeadsUpManagerLogger.kt index ef07eed467dc..f6154afec273 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/HeadsUpManagerLogger.kt +++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/HeadsUpManagerLogger.kt @@ -66,6 +66,26 @@ class HeadsUpManagerLogger @Inject constructor( }) } + fun logAutoRemoveScheduled(entry: NotificationEntry, delayMillis: Long, reason: String) { + buffer.log(TAG, INFO, { + str1 = entry.logKey + long1 = delayMillis + str2 = reason + }, { + "schedule auto remove of $str1 in $long1 ms reason: $str2" + }) + } + + fun logAutoRemoveRescheduled(entry: NotificationEntry, delayMillis: Long, reason: String) { + buffer.log(TAG, INFO, { + str1 = entry.logKey + long1 = delayMillis + str2 = reason + }, { + "reschedule auto remove of $str1 in $long1 ms reason: $str2" + }) + } + fun logAutoRemoveCanceled(entry: NotificationEntry, reason: String?) { buffer.log(TAG, INFO, { str1 = entry.logKey diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/AlertingNotificationManagerTest.java b/packages/SystemUI/tests/src/com/android/systemui/statusbar/AlertingNotificationManagerTest.java index a3cff87e63b8..76c401547ce9 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/AlertingNotificationManagerTest.java +++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/AlertingNotificationManagerTest.java @@ -30,8 +30,6 @@ import static org.mockito.Mockito.spy; import android.app.ActivityManager; import android.app.Notification; -import android.os.Handler; -import android.os.Looper; import android.os.UserHandle; import android.service.notification.StatusBarNotification; import android.testing.AndroidTestingRunner; @@ -45,12 +43,12 @@ import com.android.systemui.statusbar.notification.collection.NotificationEntry; import com.android.systemui.statusbar.notification.collection.NotificationEntryBuilder; import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow; import com.android.systemui.statusbar.policy.HeadsUpManagerLogger; +import com.android.systemui.util.concurrency.DelayableExecutor; +import com.android.systemui.util.concurrency.FakeExecutor; import com.android.systemui.util.settings.FakeGlobalSettings; +import com.android.systemui.util.time.FakeSystemClock; import com.android.systemui.util.time.SystemClock; -import com.android.systemui.util.time.SystemClockImpl; -import org.junit.After; -import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; @@ -73,27 +71,24 @@ public class AlertingNotificationManagerTest extends SysuiTestCase { protected static final int TEST_STICKY_AUTO_DISMISS_TIME = 800; // Number of notifications to use in tests requiring multiple notifications private static final int TEST_NUM_NOTIFICATIONS = 4; - protected static final int TEST_TIMEOUT_TIME = 2_000; - protected final Runnable mTestTimeoutRunnable = () -> mTimedOut = true; - protected Handler mTestHandler; protected final FakeGlobalSettings mGlobalSettings = new FakeGlobalSettings(); - protected final SystemClock mSystemClock = new SystemClockImpl(); - protected boolean mTimedOut = false; + protected final FakeSystemClock mSystemClock = new FakeSystemClock(); + protected final FakeExecutor mExecutor = new FakeExecutor(mSystemClock); @Mock protected ExpandableNotificationRow mRow; static { assertThat(TEST_MINIMUM_DISPLAY_TIME).isLessThan(TEST_AUTO_DISMISS_TIME); assertThat(TEST_AUTO_DISMISS_TIME).isLessThan(TEST_STICKY_AUTO_DISMISS_TIME); - assertThat(TEST_STICKY_AUTO_DISMISS_TIME).isLessThan(TEST_TIMEOUT_TIME); } private static class TestableAlertingNotificationManager extends AlertingNotificationManager { private AlertEntry mLastCreatedEntry; - private TestableAlertingNotificationManager(Handler handler, SystemClock systemClock) { - super(new HeadsUpManagerLogger(logcatLogBuffer()), handler, systemClock); + private TestableAlertingNotificationManager(SystemClock systemClock, + DelayableExecutor executor) { + super(new HeadsUpManagerLogger(logcatLogBuffer()), systemClock, executor); mMinimumDisplayTime = TEST_MINIMUM_DISPLAY_TIME; mAutoDismissTime = TEST_AUTO_DISMISS_TIME; mStickyForSomeTimeAutoDismissTime = TEST_STICKY_AUTO_DISMISS_TIME; @@ -118,7 +113,7 @@ public class AlertingNotificationManagerTest extends SysuiTestCase { } protected AlertingNotificationManager createAlertingNotificationManager() { - return new TestableAlertingNotificationManager(mTestHandler, mSystemClock); + return new TestableAlertingNotificationManager(mSystemClock, mExecutor); } protected StatusBarNotification createSbn(int id, Notification n) { @@ -155,35 +150,6 @@ public class AlertingNotificationManagerTest extends SysuiTestCase { return new NotificationEntryBuilder().setSbn(createSbn(id)).build(); } - protected void verifyAlertingAtTime(AlertingNotificationManager anm, NotificationEntry entry, - boolean shouldBeAlerting, int whenToCheckAlertingMillis, String whenCondition) { - final Boolean[] wasAlerting = {null}; - final Runnable checkAlerting = - () -> wasAlerting[0] = anm.isAlerting(entry.getKey()); - - mTestHandler.postDelayed(checkAlerting, whenToCheckAlertingMillis); - mTestHandler.postDelayed(mTestTimeoutRunnable, TEST_TIMEOUT_TIME); - TestableLooper.get(this).processMessages(2); - - assertFalse("Test timed out", mTimedOut); - if (shouldBeAlerting) { - assertTrue("Should still be alerting after " + whenCondition, wasAlerting[0]); - } else { - assertFalse("Should not still be alerting after " + whenCondition, wasAlerting[0]); - } - assertFalse("Should not still be alerting after processing", - anm.isAlerting(entry.getKey())); - } - - @Before - public void setUp() { - mTestHandler = Handler.createAsync(Looper.myLooper()); - } - - @After - public void tearDown() { - mTestHandler.removeCallbacksAndMessages(null); - } @Test public void testShowNotification_addsEntry() { @@ -203,9 +169,7 @@ public class AlertingNotificationManagerTest extends SysuiTestCase { final NotificationEntry entry = createEntry(/* id = */ 0); alm.showNotification(entry); - - verifyAlertingAtTime(alm, entry, false, TEST_AUTO_DISMISS_TIME * 3 / 2, - "auto dismiss time"); + mSystemClock.advanceTime(TEST_AUTO_DISMISS_TIME * 3 / 2); assertFalse(alm.isAlerting(entry.getKey())); } @@ -217,10 +181,8 @@ public class AlertingNotificationManagerTest extends SysuiTestCase { alm.showNotification(entry); - // Try to remove but defer, since the notification has not been shown long enough. - final boolean removedImmediately = alm.removeNotification(entry.getKey(), - false /* releaseImmediately */); - + final boolean removedImmediately = alm.removeNotification( + entry.getKey(), /* releaseImmediately = */ false); assertFalse(removedImmediately); assertTrue(alm.isAlerting(entry.getKey())); } @@ -232,10 +194,8 @@ public class AlertingNotificationManagerTest extends SysuiTestCase { alm.showNotification(entry); - // Remove forcibly with releaseImmediately = true. - final boolean removedImmediately = alm.removeNotification(entry.getKey(), - true /* releaseImmediately */); - + final boolean removedImmediately = alm.removeNotification( + entry.getKey(), /* releaseImmediately = */ true); assertTrue(removedImmediately); assertFalse(alm.isAlerting(entry.getKey())); } diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/HeadsUpManagerPhoneTest.java b/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/HeadsUpManagerPhoneTest.java index 37ee32204f32..e1bd89fa7c34 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/HeadsUpManagerPhoneTest.java +++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/HeadsUpManagerPhoneTest.java @@ -17,6 +17,7 @@ package com.android.systemui.statusbar.phone; import static com.android.systemui.dump.LogBufferHelperKt.logcatLogBuffer; +import static com.android.systemui.util.concurrency.MockExecutorHandlerKt.mockExecutorHandler; import static junit.framework.Assert.assertFalse; import static junit.framework.Assert.assertTrue; @@ -25,7 +26,6 @@ import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.Mockito.when; import android.content.Context; -import android.os.Handler; import android.testing.AndroidTestingRunner; import android.testing.TestableLooper; @@ -45,11 +45,11 @@ import com.android.systemui.statusbar.policy.AccessibilityManagerWrapper; import com.android.systemui.statusbar.policy.ConfigurationController; import com.android.systemui.statusbar.policy.HeadsUpManager; import com.android.systemui.statusbar.policy.HeadsUpManagerLogger; +import com.android.systemui.util.concurrency.DelayableExecutor; import com.android.systemui.util.kotlin.JavaAdapter; import com.android.systemui.util.settings.GlobalSettings; import com.android.systemui.util.time.SystemClock; -import org.junit.After; import org.junit.Before; import org.junit.Ignore; import org.junit.Rule; @@ -88,9 +88,9 @@ public class HeadsUpManagerPhoneTest extends AlertingNotificationManagerTest { StatusBarStateController statusBarStateController, KeyguardBypassController keyguardBypassController, ConfigurationController configurationController, - Handler handler, GlobalSettings globalSettings, SystemClock systemClock, + DelayableExecutor executor, AccessibilityManagerWrapper accessibilityManagerWrapper, UiEventLogger uiEventLogger, JavaAdapter javaAdapter, @@ -104,9 +104,10 @@ public class HeadsUpManagerPhoneTest extends AlertingNotificationManagerTest { groupManager, visualStabilityProvider, configurationController, - handler, + mockExecutorHandler(executor), globalSettings, systemClock, + executor, accessibilityManagerWrapper, uiEventLogger, javaAdapter, @@ -126,9 +127,9 @@ public class HeadsUpManagerPhoneTest extends AlertingNotificationManagerTest { mStatusBarStateController, mBypassController, mConfigurationController, - mTestHandler, mGlobalSettings, mSystemClock, + mExecutor, mAccessibilityManagerWrapper, mUiEventLogger, mJavaAdapter, @@ -142,7 +143,6 @@ public class HeadsUpManagerPhoneTest extends AlertingNotificationManagerTest { } @Before - @Override public void setUp() { when(mShadeInteractor.isAnyExpanded()).thenReturn(StateFlowKt.MutableStateFlow(false)); final AccessibilityManagerWrapper accessibilityMgr = @@ -153,14 +153,6 @@ public class HeadsUpManagerPhoneTest extends AlertingNotificationManagerTest { mDependency.injectMockDependency(NotificationShadeWindowController.class); mContext.getOrCreateTestableResources().addOverride( R.integer.ambient_notification_extension_time, 500); - - super.setUp(); - } - - @After - @Override - public void tearDown() { - super.tearDown(); } @Test @@ -224,8 +216,8 @@ public class HeadsUpManagerPhoneTest extends AlertingNotificationManagerTest { hmp.showNotification(entry); hmp.extendHeadsUp(); + mSystemClock.advanceTime(TEST_AUTO_DISMISS_TIME + hmp.mExtensionTime / 2); - final int pastNormalTimeMillis = TEST_AUTO_DISMISS_TIME + hmp.mExtensionTime / 2; - verifyAlertingAtTime(hmp, entry, true, pastNormalTimeMillis, "normal time"); + assertTrue(hmp.isAlerting(entry.getKey())); } } diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/policy/BaseHeadsUpManagerTest.java b/packages/SystemUI/tests/src/com/android/systemui/statusbar/policy/BaseHeadsUpManagerTest.java index 2940c398c315..4c893e3538a3 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/policy/BaseHeadsUpManagerTest.java +++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/policy/BaseHeadsUpManagerTest.java @@ -19,6 +19,7 @@ package com.android.systemui.statusbar.policy; import static android.app.Notification.FLAG_FSI_REQUESTED_BUT_DENIED; import static com.android.systemui.dump.LogBufferHelperKt.logcatLogBuffer; +import static com.android.systemui.util.concurrency.MockExecutorHandlerKt.mockExecutorHandler; import static com.google.common.truth.Truth.assertThat; @@ -41,7 +42,6 @@ import android.app.Person; import android.content.Context; import android.content.Intent; import android.graphics.Region; -import android.os.Handler; import android.testing.AndroidTestingRunner; import android.testing.TestableLooper; @@ -56,11 +56,10 @@ import com.android.systemui.statusbar.AlertingNotificationManager; import com.android.systemui.statusbar.AlertingNotificationManagerTest; import com.android.systemui.statusbar.notification.collection.NotificationEntry; import com.android.systemui.statusbar.notification.collection.NotificationEntryBuilder; +import com.android.systemui.util.concurrency.DelayableExecutor; import com.android.systemui.util.settings.GlobalSettings; import com.android.systemui.util.time.SystemClock; -import org.junit.After; -import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; @@ -77,7 +76,6 @@ public class BaseHeadsUpManagerTest extends AlertingNotificationManagerTest { private static final int TEST_TOUCH_ACCEPTANCE_TIME = 200; private static final int TEST_A11Y_AUTO_DISMISS_TIME = 1_000; - private static final int TEST_A11Y_TIMEOUT_TIME = 3_000; private UiEventLoggerFake mUiEventLoggerFake = new UiEventLoggerFake(); private final HeadsUpManagerLogger mLogger = spy(new HeadsUpManagerLogger(logcatLogBuffer())); @@ -87,25 +85,18 @@ public class BaseHeadsUpManagerTest extends AlertingNotificationManagerTest { assertThat(TEST_MINIMUM_DISPLAY_TIME).isLessThan(TEST_AUTO_DISMISS_TIME); assertThat(TEST_AUTO_DISMISS_TIME).isLessThan(TEST_STICKY_AUTO_DISMISS_TIME); assertThat(TEST_STICKY_AUTO_DISMISS_TIME).isLessThan(TEST_A11Y_AUTO_DISMISS_TIME); - - assertThat(TEST_TOUCH_ACCEPTANCE_TIME + TEST_AUTO_DISMISS_TIME).isLessThan( - TEST_TIMEOUT_TIME); - assertThat(TEST_TOUCH_ACCEPTANCE_TIME + TEST_STICKY_AUTO_DISMISS_TIME).isLessThan( - TEST_TIMEOUT_TIME); - assertThat(TEST_TOUCH_ACCEPTANCE_TIME + TEST_A11Y_AUTO_DISMISS_TIME).isLessThan( - TEST_A11Y_TIMEOUT_TIME); } private final class TestableHeadsUpManager extends BaseHeadsUpManager { TestableHeadsUpManager(Context context, HeadsUpManagerLogger logger, - Handler handler, + DelayableExecutor executor, GlobalSettings globalSettings, SystemClock systemClock, AccessibilityManagerWrapper accessibilityManagerWrapper, UiEventLogger uiEventLogger) { - super(context, logger, handler, globalSettings, systemClock, - accessibilityManagerWrapper, uiEventLogger); + super(context, logger, mockExecutorHandler(executor), globalSettings, systemClock, + executor, accessibilityManagerWrapper, uiEventLogger); mTouchAcceptanceDelay = TEST_TOUCH_ACCEPTANCE_TIME; mMinimumDisplayTime = TEST_MINIMUM_DISPLAY_TIME; mAutoDismissTime = TEST_AUTO_DISMISS_TIME; @@ -183,7 +174,7 @@ public class BaseHeadsUpManagerTest extends AlertingNotificationManagerTest { } private BaseHeadsUpManager createHeadsUpManager() { - return new TestableHeadsUpManager(mContext, mLogger, mTestHandler, mGlobalSettings, + return new TestableHeadsUpManager(mContext, mLogger, mExecutor, mGlobalSettings, mSystemClock, mAccessibilityMgr, mUiEventLoggerFake); } @@ -234,18 +225,6 @@ public class BaseHeadsUpManagerTest extends AlertingNotificationManagerTest { } - @Before - @Override - public void setUp() { - super.setUp(); - } - - @After - @Override - public void tearDown() { - super.tearDown(); - } - @Test public void testHunRemovedLogging() { final BaseHeadsUpManager hum = createHeadsUpManager(); @@ -305,10 +284,9 @@ public class BaseHeadsUpManagerTest extends AlertingNotificationManagerTest { useAccessibilityTimeout(false); hum.showNotification(entry); + mSystemClock.advanceTime(TEST_TOUCH_ACCEPTANCE_TIME / 2 + TEST_AUTO_DISMISS_TIME); - final int pastJustAutoDismissMillis = - TEST_TOUCH_ACCEPTANCE_TIME / 2 + TEST_AUTO_DISMISS_TIME; - verifyAlertingAtTime(hum, entry, true, pastJustAutoDismissMillis, "just auto dismiss"); + assertTrue(hum.isAlerting(entry.getKey())); } @@ -319,10 +297,10 @@ public class BaseHeadsUpManagerTest extends AlertingNotificationManagerTest { useAccessibilityTimeout(false); hum.showNotification(entry); + mSystemClock.advanceTime(TEST_TOUCH_ACCEPTANCE_TIME + + (TEST_AUTO_DISMISS_TIME + TEST_A11Y_AUTO_DISMISS_TIME) / 2); - final int pastDefaultTimeoutMillis = TEST_TOUCH_ACCEPTANCE_TIME - + (TEST_AUTO_DISMISS_TIME + TEST_A11Y_AUTO_DISMISS_TIME) / 2; - verifyAlertingAtTime(hum, entry, false, pastDefaultTimeoutMillis, "default timeout"); + assertFalse(hum.isAlerting(entry.getKey())); } @@ -333,10 +311,10 @@ public class BaseHeadsUpManagerTest extends AlertingNotificationManagerTest { useAccessibilityTimeout(false); hum.showNotification(entry); + mSystemClock.advanceTime(TEST_TOUCH_ACCEPTANCE_TIME + + (TEST_AUTO_DISMISS_TIME + TEST_STICKY_AUTO_DISMISS_TIME) / 2); - final int pastDefaultTimeoutMillis = TEST_TOUCH_ACCEPTANCE_TIME - + (TEST_AUTO_DISMISS_TIME + TEST_STICKY_AUTO_DISMISS_TIME) / 2; - verifyAlertingAtTime(hum, entry, true, pastDefaultTimeoutMillis, "default timeout"); + assertTrue(hum.isAlerting(entry.getKey())); } @@ -347,18 +325,9 @@ public class BaseHeadsUpManagerTest extends AlertingNotificationManagerTest { useAccessibilityTimeout(false); hum.showNotification(entry); + mSystemClock.advanceTime(TEST_TOUCH_ACCEPTANCE_TIME + 2 * TEST_A11Y_AUTO_DISMISS_TIME); - final int pastLongestAutoDismissMillis = - TEST_TOUCH_ACCEPTANCE_TIME + 2 * TEST_A11Y_AUTO_DISMISS_TIME; - final Boolean[] wasAlerting = {null}; - final Runnable checkAlerting = - () -> wasAlerting[0] = hum.isAlerting(entry.getKey()); - mTestHandler.postDelayed(checkAlerting, pastLongestAutoDismissMillis); - TestableLooper.get(this).processMessages(1); - - assertTrue("Should still be alerting past longest auto-dismiss", wasAlerting[0]); - assertTrue("Should still be alerting after processing", - hum.isAlerting(entry.getKey())); + assertTrue(hum.isAlerting(entry.getKey())); } @@ -369,10 +338,10 @@ public class BaseHeadsUpManagerTest extends AlertingNotificationManagerTest { useAccessibilityTimeout(true); hum.showNotification(entry); + mSystemClock.advanceTime(TEST_TOUCH_ACCEPTANCE_TIME + + (TEST_AUTO_DISMISS_TIME + TEST_A11Y_AUTO_DISMISS_TIME) / 2); - final int pastDefaultTimeoutMillis = TEST_TOUCH_ACCEPTANCE_TIME - + (TEST_AUTO_DISMISS_TIME + TEST_A11Y_AUTO_DISMISS_TIME) / 2; - verifyAlertingAtTime(hum, entry, true, pastDefaultTimeoutMillis, "default timeout"); + assertTrue(hum.isAlerting(entry.getKey())); } @@ -383,10 +352,10 @@ public class BaseHeadsUpManagerTest extends AlertingNotificationManagerTest { useAccessibilityTimeout(true); hum.showNotification(entry); + mSystemClock.advanceTime(TEST_TOUCH_ACCEPTANCE_TIME + + (TEST_STICKY_AUTO_DISMISS_TIME + TEST_A11Y_AUTO_DISMISS_TIME) / 2); - final int pastStickyTimeoutMillis = TEST_TOUCH_ACCEPTANCE_TIME - + (TEST_STICKY_AUTO_DISMISS_TIME + TEST_A11Y_AUTO_DISMISS_TIME) / 2; - verifyAlertingAtTime(hum, entry, true, pastStickyTimeoutMillis, "sticky timeout"); + assertTrue(hum.isAlerting(entry.getKey())); } @@ -398,18 +367,14 @@ public class BaseHeadsUpManagerTest extends AlertingNotificationManagerTest { hum.showNotification(entry); - // Try to remove but defer, since the notification has not been shown long enough. final boolean removedImmediately = hum.removeNotification( - entry.getKey(), false /* releaseImmediately */); + entry.getKey(), /* releaseImmediately = */ false); + assertFalse(removedImmediately); + assertTrue(hum.isAlerting(entry.getKey())); - assertFalse("HUN should not be removed before minimum display time", removedImmediately); - assertTrue("HUN should still be alerting before minimum display time", - hum.isAlerting(entry.getKey())); + mSystemClock.advanceTime((TEST_MINIMUM_DISPLAY_TIME + TEST_AUTO_DISMISS_TIME) / 2); - final int pastMinimumDisplayTimeMillis = - (TEST_MINIMUM_DISPLAY_TIME + TEST_AUTO_DISMISS_TIME) / 2; - verifyAlertingAtTime(hum, entry, false, pastMinimumDisplayTimeMillis, - "minimum display time"); + assertFalse(hum.isAlerting(entry.getKey())); } @@ -420,32 +385,13 @@ public class BaseHeadsUpManagerTest extends AlertingNotificationManagerTest { useAccessibilityTimeout(false); hum.showNotification(entry); + mSystemClock.advanceTime((TEST_MINIMUM_DISPLAY_TIME + TEST_AUTO_DISMISS_TIME) / 2); + + assertTrue(hum.isAlerting(entry.getKey())); - // After the minimum display time: - // 1. Check whether the notification is still alerting. - // 2. Try to remove it and check whether the remove succeeded. - // 3. Check whether it is still alerting after trying to remove it. - final Boolean[] livedPastMinimumDisplayTime = {null}; - final Boolean[] removedAfterMinimumDisplayTime = {null}; - final Boolean[] livedPastRemoveAfterMinimumDisplayTime = {null}; - final Runnable pastMinimumDisplayTimeRunnable = () -> { - livedPastMinimumDisplayTime[0] = hum.isAlerting(entry.getKey()); - removedAfterMinimumDisplayTime[0] = hum.removeNotification( - entry.getKey(), /* releaseImmediately = */ false); - livedPastRemoveAfterMinimumDisplayTime[0] = hum.isAlerting(entry.getKey()); - }; - final int pastMinimumDisplayTimeMillis = - (TEST_MINIMUM_DISPLAY_TIME + TEST_AUTO_DISMISS_TIME) / 2; - mTestHandler.postDelayed(pastMinimumDisplayTimeRunnable, pastMinimumDisplayTimeMillis); - // Wait until the minimum display time has passed before attempting removal. - TestableLooper.get(this).processMessages(1); - - assertTrue("HUN should live past minimum display time", - livedPastMinimumDisplayTime[0]); - assertTrue("HUN should be removed immediately past minimum display time", - removedAfterMinimumDisplayTime[0]); - assertFalse("HUN should not live after being removed past minimum display time", - livedPastRemoveAfterMinimumDisplayTime[0]); + final boolean removedImmediately = hum.removeNotification( + entry.getKey(), /* releaseImmediately = */ false); + assertTrue(removedImmediately); assertFalse(hum.isAlerting(entry.getKey())); } @@ -457,10 +403,8 @@ public class BaseHeadsUpManagerTest extends AlertingNotificationManagerTest { hum.showNotification(entry); - // Remove forcibly with releaseImmediately = true. final boolean removedImmediately = hum.removeNotification( entry.getKey(), /* releaseImmediately = */ true); - assertTrue(removedImmediately); assertFalse(hum.isAlerting(entry.getKey())); } |