Merge "Edit NEM tests to prevent flakiness"
diff --git a/packages/SystemUI/tests/src/com/android/systemui/TestableDependency.java b/packages/SystemUI/tests/src/com/android/systemui/TestableDependency.java
index 2ecc8ea..b3071f9 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/TestableDependency.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/TestableDependency.java
@@ -19,8 +19,11 @@
 import android.content.Context;
 import android.util.ArrayMap;
 import android.util.ArraySet;
+import android.util.Log;
 
 public class TestableDependency extends Dependency {
+    private static final String TAG = "TestableDependency";
+
     private final ArrayMap<Object, Object> mObjs = new ArrayMap<>();
     private final ArraySet<Object> mInstantiatedObjects = new ArraySet<>();
 
@@ -44,7 +47,7 @@
 
     public <T> void injectTestDependency(Class<T> key, T obj) {
         if (mInstantiatedObjects.contains(key)) {
-            throw new IllegalStateException(key + " was already initialized");
+            Log.d(TAG, key + " was already initialized but overriding with testDependency.");
         }
         mObjs.put(key, obj);
     }
diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/NotificationEntryManagerTest.java b/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/NotificationEntryManagerTest.java
index 6a78909..296d0cef 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/NotificationEntryManagerTest.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/NotificationEntryManagerTest.java
@@ -45,7 +45,6 @@
 import android.content.Intent;
 import android.graphics.drawable.Icon;
 import android.os.Handler;
-import android.os.Looper;
 import android.os.UserHandle;
 import android.service.notification.NotificationListenerService.Ranking;
 import android.service.notification.NotificationListenerService.RankingMap;
@@ -88,7 +87,6 @@
 import com.android.systemui.statusbar.notification.row.NotifRemoteViewCache;
 import com.android.systemui.statusbar.notification.row.NotificationContentInflater;
 import com.android.systemui.statusbar.notification.row.NotificationGutsManager;
-import com.android.systemui.statusbar.notification.row.NotificationRowContentBinder;
 import com.android.systemui.statusbar.notification.row.RowInflaterTask;
 import com.android.systemui.statusbar.notification.row.dagger.NotificationRowComponent;
 import com.android.systemui.statusbar.notification.stack.NotificationListContainer;
@@ -99,6 +97,7 @@
 import com.android.systemui.util.Assert;
 import com.android.systemui.util.leak.LeakDetector;
 
+import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -190,15 +189,14 @@
     @Before
     public void setUp() {
         MockitoAnnotations.initMocks(this);
-        if (!mDependency.hasInstantiatedDependency(SmartReplyController.class)) {
-            mDependency.injectMockDependency(SmartReplyController.class);
-        }
+        mDependency.injectMockDependency(SmartReplyController.class);
         mDependency.injectMockDependency(NotificationMediaManager.class);
 
         mCountDownLatch = new CountDownLatch(1);
 
+        Assert.sMainLooper = TestableLooper.get(this).getLooper();
         mDependency.injectTestDependency(Dependency.MAIN_HANDLER,
-                Handler.createAsync(Looper.myLooper()));
+                Handler.createAsync(TestableLooper.get(this).getLooper()));
         when(mRemoteInputManager.getController()).thenReturn(mRemoteInputController);
         when(mListContainer.getViewParentForNotification(any())).thenReturn(
                 new FrameLayout(mContext));
@@ -208,9 +206,10 @@
 
         mEntry.expandedIcon = mock(StatusBarIconView.class);
 
-        NotificationRowContentBinder contentBinder = new NotificationContentInflater(
+        NotificationContentInflater contentBinder = new NotificationContentInflater(
                 mock(NotifRemoteViewCache.class),
                 mRemoteInputManager);
+        contentBinder.setInflateSynchronously(true);
 
         when(mNotificationRowComponentBuilder.activatableNotificationView(any()))
                 .thenReturn(mNotificationRowComponentBuilder);
@@ -263,6 +262,12 @@
                 mEntry.getKey(), Ranking.USER_SENTIMENT_NEUTRAL);
     }
 
+    @After
+    public void tearDown() {
+        // CLEAN UP inflation tasks so they don't callback in a future test
+        mEntry.abortTask();
+    }
+
     @Test
     public void testAddNotification() throws Exception {
         TestableLooper.get(this).processAllMessages();
@@ -348,9 +353,6 @@
 
     @Test
     public void testRemoveNotification() {
-        // Row inflation happens off thread, so pretend that this test looper is main
-        Assert.sMainLooper = TestableLooper.get(this).getLooper();
-
         mEntry.setRow(mRow);
         mEntryManager.addActiveNotificationForTest(mEntry);
 
@@ -468,9 +470,6 @@
 
     @Test
     public void testLifetimeExtenders_whenRetentionEndsNotificationIsRemoved() {
-        // Row inflation happens off thread, so pretend that this test looper is main
-        Assert.sMainLooper = TestableLooper.get(this).getLooper();
-
         // GIVEN an entry manager with a notification whose life has been extended
         mEntryManager.addActiveNotificationForTest(mEntry);
         final FakeNotificationLifetimeExtender extender = new FakeNotificationLifetimeExtender();
@@ -561,9 +560,6 @@
 
     @Test
     public void testRemoveInterceptor_notInterceptedGetsRemoved() {
-        // Row inflation happens off thread, so pretend that this test looper is main
-        Assert.sMainLooper = TestableLooper.get(this).getLooper();
-
         // GIVEN an entry manager with a notification
         mEntryManager.addActiveNotificationForTest(mEntry);
 
@@ -626,7 +622,6 @@
 
     @Test
     public void testGetNotificationsForCurrentUser_shouldFilterNonCurrentUserNotifications() {
-        Assert.sMainLooper = TestableLooper.get(this).getLooper();
         Notification.Builder n = new Notification.Builder(mContext, "di")
                 .setSmallIcon(R.drawable.ic_person)
                 .setContentTitle("Title")
diff --git a/tests/testables/src/android/testing/TestableLooper.java b/tests/testables/src/android/testing/TestableLooper.java
index 8eac3ea..fe0224a 100644
--- a/tests/testables/src/android/testing/TestableLooper.java
+++ b/tests/testables/src/android/testing/TestableLooper.java
@@ -234,7 +234,9 @@
             try {
                 mLooper = setAsMain ? Looper.getMainLooper() : createLooper();
                 mTestableLooper = new TestableLooper(mLooper, false);
-                mTestableLooper.getLooper().getThread().setName(test.getClass().getName());
+                if (!setAsMain) {
+                    mTestableLooper.getLooper().getThread().setName(test.getClass().getName());
+                }
             } catch (Exception e) {
                 throw new RuntimeException(e);
             }