Split auto dismissing functionality from heads up

Refactor HeadsUpManager so that the auto dismissing alert functionality
is separate and reusable for future use with ambient pulses.  Add tests
for this new class and also a few for HeadsUpMananger specific logic

First in two-part CL to split ambient pulse logic from heads up logic.
Second CL will add an ambient pulse manager which uses the alert logic
introduced here and involve more of the actual split.

Test: runtest systemui, manual
Change-Id: I08069562e7bea4c8e25674aa35237e1bb8cf4475
diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/AlertingNotificationManagerTest.java b/packages/SystemUI/tests/src/com/android/systemui/statusbar/AlertingNotificationManagerTest.java
new file mode 100644
index 0000000..f04a115
--- /dev/null
+++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/AlertingNotificationManagerTest.java
@@ -0,0 +1,176 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+package com.android.systemui.statusbar;
+
+import static junit.framework.Assert.assertFalse;
+import static junit.framework.Assert.assertTrue;
+
+import static org.junit.Assert.assertEquals;
+
+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.support.test.filters.SmallTest;
+import android.testing.AndroidTestingRunner;
+import android.testing.TestableLooper;
+
+import com.android.systemui.R;
+import com.android.systemui.SysuiTestCase;
+import com.android.systemui.statusbar.AlertingNotificationManager;
+import com.android.systemui.statusbar.notification.NotificationData;
+import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow;
+
+
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnit;
+import org.mockito.junit.MockitoRule;
+
+@SmallTest
+@RunWith(AndroidTestingRunner.class)
+@TestableLooper.RunWithLooper
+public class AlertingNotificationManagerTest extends SysuiTestCase {
+    @Rule
+    public MockitoRule rule = MockitoJUnit.rule();
+
+    private static final String TEST_PACKAGE_NAME = "test";
+    private static final int TEST_UID = 0;
+
+    private static final int TEST_MINIMUM_DISPLAY_TIME = 200;
+    private static final int TEST_AUTO_DISMISS_TIME = 500;
+    // Number of notifications to use in tests requiring multiple notifications
+    private static final int TEST_NUM_NOTIFICATIONS = 4;
+    private static final int TEST_TIMEOUT_TIME = 10000;
+    private final Runnable TEST_TIMEOUT_RUNNABLE = () -> mTimedOut = true;
+
+    private AlertingNotificationManager mAlertingNotificationManager;
+
+    protected NotificationData.Entry mEntry;
+    protected Handler mTestHandler;
+    private StatusBarNotification mSbn;
+    private boolean mTimedOut = false;
+
+    @Mock protected ExpandableNotificationRow mRow;
+
+    private final class TestableAlertingNotificationManager extends AlertingNotificationManager {
+        private TestableAlertingNotificationManager() {
+            mMinimumDisplayTime = TEST_MINIMUM_DISPLAY_TIME;
+            mAutoDismissNotificationDecay = TEST_AUTO_DISMISS_TIME;
+            mHandler = mTestHandler;
+        }
+
+        @Override
+        protected void onAlertEntryAdded(AlertEntry alertEntry) {}
+
+        @Override
+        protected void onAlertEntryRemoved(AlertEntry alertEntry) {}
+    }
+
+    protected AlertingNotificationManager createAlertingNotificationManager() {
+        return new TestableAlertingNotificationManager();
+    }
+
+    private StatusBarNotification createNewNotification(int id) {
+        Notification.Builder n = new Notification.Builder(mContext, "")
+                .setSmallIcon(R.drawable.ic_person)
+                .setContentTitle("Title")
+                .setContentText("Text");
+        return new StatusBarNotification(
+                TEST_PACKAGE_NAME /* pkg */,
+                TEST_PACKAGE_NAME,
+                id,
+                null /* tag */,
+                TEST_UID,
+                0 /* initialPid */,
+                n.build(),
+                new UserHandle(ActivityManager.getCurrentUser()),
+                null /* overrideGroupKey */,
+                0 /* postTime */);
+    }
+
+    @Before
+    public void setUp() {
+        mTestHandler = Handler.createAsync(Looper.myLooper());
+        mSbn = createNewNotification(0 /* id */);
+        mEntry = new NotificationData.Entry(mSbn);
+        mEntry.row = mRow;
+
+        mAlertingNotificationManager = createAlertingNotificationManager();
+    }
+
+    @Test
+    public void testShowNotification_addsEntry() {
+        mAlertingNotificationManager.showNotification(mEntry);
+
+        assertTrue(mAlertingNotificationManager.contains(mEntry.key));
+        assertTrue(mAlertingNotificationManager.hasNotifications());
+        assertEquals(mEntry, mAlertingNotificationManager.getEntry(mEntry.key));
+    }
+
+    @Test
+    public void testShowNotification_autoDismisses() {
+        mAlertingNotificationManager.showNotification(mEntry);
+        mTestHandler.postDelayed(TEST_TIMEOUT_RUNNABLE, TEST_TIMEOUT_TIME);
+
+        // Wait for remove runnable and then process it immediately
+        TestableLooper.get(this).processMessages(1);
+
+        assertFalse("Test timed out", mTimedOut);
+        assertFalse(mAlertingNotificationManager.contains(mEntry.key));
+    }
+
+    @Test
+    public void testRemoveNotification_removeDeferred() {
+        mAlertingNotificationManager.showNotification(mEntry);
+
+        // Try to remove but defer, since the notification has not been shown long enough.
+        mAlertingNotificationManager.removeNotification(mEntry.key, false /* releaseImmediately */);
+
+        assertTrue(mAlertingNotificationManager.contains(mEntry.key));
+    }
+
+    @Test
+    public void testRemoveNotification_forceRemove() {
+        mAlertingNotificationManager.showNotification(mEntry);
+
+        //Remove forcibly with releaseImmediately = true.
+        mAlertingNotificationManager.removeNotification(mEntry.key, true /* releaseImmediately */);
+
+        assertFalse(mAlertingNotificationManager.contains(mEntry.key));
+    }
+
+    @Test
+    public void testReleaseAllImmediately() {
+        for (int i = 0; i < TEST_NUM_NOTIFICATIONS; i++) {
+            StatusBarNotification sbn = createNewNotification(i);
+            NotificationData.Entry entry = new NotificationData.Entry(sbn);
+            entry.row = mRow;
+            mAlertingNotificationManager.showNotification(entry);
+        }
+
+        mAlertingNotificationManager.releaseAllImmediately();
+
+        assertEquals(0, mAlertingNotificationManager.getAllEntries().count());
+    }
+}