OSDN Git Service

Reduce usages of main looper in sysui tests
authorJason Monk <jmonk@google.com>
Wed, 16 May 2018 00:24:07 +0000 (20:24 -0400)
committerJason Monk <jmonk@google.com>
Thu, 17 May 2018 14:32:31 +0000 (10:32 -0400)
Push over to a standard testable looper, and a testable looper +
setAsMain when needed.

Also make tests more synchronous and single threaded as possible.
This will make them more deterministic and speeds them up noticeably.

Test: runtest systemui
Bug: 79550837
Change-Id: Iab0eb794329d7b1de95aef904ec08ecae7dadc98

33 files changed:
packages/SystemUI/src/com/android/systemui/statusbar/notification/NotificationInflater.java
packages/SystemUI/tests/src/com/android/keyguard/KeyguardMessageAreaTest.java
packages/SystemUI/tests/src/com/android/systemui/ExpandHelperTest.java
packages/SystemUI/tests/src/com/android/systemui/doze/DozeTriggersTest.java
packages/SystemUI/tests/src/com/android/systemui/pip/phone/PipTouchStateTest.java
packages/SystemUI/tests/src/com/android/systemui/qs/QSSecurityFooterTest.java
packages/SystemUI/tests/src/com/android/systemui/qs/external/TileLifecycleManagerTest.java
packages/SystemUI/tests/src/com/android/systemui/qs/external/TileServiceManagerTest.java
packages/SystemUI/tests/src/com/android/systemui/statusbar/AppOpsListenerTest.java
packages/SystemUI/tests/src/com/android/systemui/statusbar/ExpandableNotificationRowTest.java
packages/SystemUI/tests/src/com/android/systemui/statusbar/NonPhoneDependencyTest.java
packages/SystemUI/tests/src/com/android/systemui/statusbar/NotificationBlockingHelperManagerTest.java
packages/SystemUI/tests/src/com/android/systemui/statusbar/NotificationCustomViewWrapperTest.java
packages/SystemUI/tests/src/com/android/systemui/statusbar/NotificationDataTest.java
packages/SystemUI/tests/src/com/android/systemui/statusbar/NotificationEntryManagerTest.java
packages/SystemUI/tests/src/com/android/systemui/statusbar/NotificationGutsManagerTest.java
packages/SystemUI/tests/src/com/android/systemui/statusbar/NotificationListenerTest.java
packages/SystemUI/tests/src/com/android/systemui/statusbar/NotificationLockscreenUserManagerTest.java
packages/SystemUI/tests/src/com/android/systemui/statusbar/NotificationLoggerTest.java
packages/SystemUI/tests/src/com/android/systemui/statusbar/NotificationRemoteInputManagerTest.java
packages/SystemUI/tests/src/com/android/systemui/statusbar/NotificationTestHelper.java
packages/SystemUI/tests/src/com/android/systemui/statusbar/NotificationViewHierarchyManagerTest.java
packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/AboveShelfObserverTest.java
packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/NotificationInflaterTest.java
packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/NotificationViewWrapperTest.java
packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/AutoTileManagerTest.java
packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/HeadsUpAppearanceControllerTest.java
packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/StatusBarTest.java
packages/SystemUI/tests/src/com/android/systemui/statusbar/policy/RemoteInputViewTest.java
packages/SystemUI/tests/src/com/android/systemui/statusbar/policy/SmartReplyConstantsTest.java
packages/SystemUI/tests/src/com/android/systemui/statusbar/policy/ZenModeControllerImplTest.java
packages/SystemUI/tests/src/com/android/systemui/statusbar/stack/NotificationChildrenContainerTest.java
packages/SystemUI/tests/src/com/android/systemui/statusbar/stack/NotificationRoundnessManagerTest.java

index 0143d01..1303057 100644 (file)
@@ -137,9 +137,15 @@ public class NotificationInflater {
             return;
         }
         StatusBarNotification sbn = mRow.getEntry().notification;
-        new AsyncInflationTask(sbn, reInflateFlags, mRow, mIsLowPriority,
+        AsyncInflationTask task = new AsyncInflationTask(sbn, reInflateFlags, mRow,
+                mIsLowPriority,
                 mIsChildInGroup, mUsesIncreasedHeight, mUsesIncreasedHeadsUpHeight, mRedactAmbient,
-                mCallback, mRemoteViewClickHandler).execute();
+                mCallback, mRemoteViewClickHandler);
+        if (mCallback != null && mCallback.doInflateSynchronous()) {
+            task.onPostExecute(task.doInBackground());
+        } else {
+            task.execute();
+        }
     }
 
     @VisibleForTesting
@@ -331,6 +337,30 @@ public class NotificationInflater {
             final HashMap<Integer, CancellationSignal> runningInflations,
             ApplyCallback applyCallback) {
         RemoteViews newContentView = applyCallback.getRemoteView();
+        if (callback != null && callback.doInflateSynchronous()) {
+            try {
+                if (isNewView) {
+                    View v = newContentView.apply(
+                            result.packageContext,
+                            parentLayout,
+                            remoteViewClickHandler);
+                    v.setIsRootNamespace(true);
+                    applyCallback.setResultView(v);
+                } else {
+                    newContentView.reapply(
+                            result.packageContext,
+                            existingView,
+                            remoteViewClickHandler);
+                    existingWrapper.onReinflated();
+                }
+            } catch (Exception e) {
+                handleInflationError(runningInflations, e, entry.notification, callback);
+                // Add a running inflation to make sure we don't trigger callbacks.
+                // Safe to do because only happens in tests.
+                runningInflations.put(inflationId, new CancellationSignal());
+            }
+            return;
+        }
         RemoteViews.OnViewAppliedListener listener
                 = new RemoteViews.OnViewAppliedListener() {
 
@@ -515,6 +545,13 @@ public class NotificationInflater {
     public interface InflationCallback {
         void handleInflationException(StatusBarNotification notification, Exception e);
         void onAsyncInflationFinished(NotificationData.Entry entry);
+
+        /**
+         * Used to disable async-ness for tests. Should only be used for tests.
+         */
+        default boolean doInflateSynchronous() {
+            return false;
+        }
     }
 
     public void onDensityOrFontScaleChanged() {
@@ -646,6 +683,11 @@ public class NotificationInflater {
             mRow.onNotificationUpdated();
             mCallback.onAsyncInflationFinished(mRow.getEntry());
         }
+
+        @Override
+        public boolean doInflateSynchronous() {
+            return mCallback != null && mCallback.doInflateSynchronous();
+        }
     }
 
     @VisibleForTesting
index f3be945..f8ca262 100644 (file)
@@ -20,10 +20,9 @@ import static junit.framework.Assert.assertEquals;
 
 import static org.mockito.Mockito.mock;
 
-import android.os.Handler;
-import android.os.Looper;
-import android.support.test.runner.AndroidJUnit4;
 import android.test.suitebuilder.annotation.SmallTest;
+import android.testing.AndroidTestingRunner;
+import android.testing.TestableLooper.RunWithLooper;
 
 import com.android.systemui.SysuiTestCase;
 
@@ -32,33 +31,25 @@ import org.junit.Test;
 import org.junit.runner.RunWith;
 
 @SmallTest
-@RunWith(AndroidJUnit4.class)
+@RunWith(AndroidTestingRunner.class)
+@RunWithLooper
 public class KeyguardMessageAreaTest extends SysuiTestCase {
-    private Handler mHandler = new Handler(Looper.getMainLooper());
     private KeyguardMessageArea mMessageArea;
 
     @Before
     public void setUp() throws Exception {
         KeyguardUpdateMonitor monitor = mock(KeyguardUpdateMonitor.class);
-        mHandler.post(()-> mMessageArea = new KeyguardMessageArea(mContext, null, monitor));
+        mMessageArea = new KeyguardMessageArea(mContext, null, monitor);
         waitForIdleSync();
     }
 
     @Test
     public void clearFollowedByMessage_keepsMessage() {
-        mHandler.post(()-> {
-            mMessageArea.setMessage("");
-            mMessageArea.setMessage("test");
-        });
-
-        waitForIdleSync();
+        mMessageArea.setMessage("");
+        mMessageArea.setMessage("test");
 
         CharSequence[] messageText = new CharSequence[1];
-        mHandler.post(()-> {
-            messageText[0] = mMessageArea.getText();
-        });
-
-        waitForIdleSync();
+        messageText[0] = mMessageArea.getText();
 
         assertEquals("test", messageText[0]);
     }
index aa84098..08c4235 100644 (file)
@@ -22,6 +22,8 @@ import android.support.test.InstrumentationRegistry;
 import android.support.test.annotation.UiThreadTest;
 import android.support.test.filters.SmallTest;
 import android.support.test.runner.AndroidJUnit4;
+import android.testing.AndroidTestingRunner;
+import android.testing.TestableLooper.RunWithLooper;
 
 import com.android.systemui.statusbar.ExpandableNotificationRow;
 import com.android.systemui.statusbar.NotificationTestHelper;
@@ -35,7 +37,8 @@ import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
 @SmallTest
-@RunWith(AndroidJUnit4.class)
+@RunWith(AndroidTestingRunner.class)
+@RunWithLooper(setAsMainLooper = true)
 public class ExpandHelperTest extends SysuiTestCase {
 
     private ExpandableNotificationRow mRow;
@@ -47,9 +50,7 @@ public class ExpandHelperTest extends SysuiTestCase {
         Context context = getContext();
         mRow = new NotificationTestHelper(context).createRow();
         mCallback = mock(ExpandHelper.Callback.class);
-        InstrumentationRegistry.getInstrumentation().runOnMainSync(
-                () -> mExpandHelper = new ExpandHelper(context, mCallback, 10, 100));
-
+        mExpandHelper = new ExpandHelper(context, mCallback, 10, 100);
     }
 
     @Test
index a8ea1c0..c2da7f5 100644 (file)
@@ -29,7 +29,8 @@ import android.os.Handler;
 import android.os.Looper;
 import android.support.test.InstrumentationRegistry;
 import android.support.test.filters.SmallTest;
-import android.support.test.runner.AndroidJUnit4;
+import android.testing.AndroidTestingRunner;
+import android.testing.TestableLooper.RunWithLooper;
 
 import com.android.internal.hardware.AmbientDisplayConfiguration;
 import com.android.systemui.SysuiTestCase;
@@ -46,7 +47,8 @@ import org.junit.runner.RunWith;
 
 @SmallTest
 @Ignore("failing")
-@RunWith(AndroidJUnit4.class)
+@RunWith(AndroidTestingRunner.class)
+@RunWithLooper(setAsMainLooper = true)
 public class DozeTriggersTest extends SysuiTestCase {
     private DozeTriggers mTriggers;
     private DozeMachine mMachine;
@@ -54,7 +56,6 @@ public class DozeTriggersTest extends SysuiTestCase {
     private AmbientDisplayConfiguration mConfig;
     private DozeParameters mParameters;
     private FakeSensorManager mSensors;
-    private Handler mHandler;
     private WakeLock mWakeLock;
     private Instrumentation mInstrumentation;
     private AlarmManager mAlarmManager;
@@ -74,40 +75,29 @@ public class DozeTriggersTest extends SysuiTestCase {
         mConfig = DozeConfigurationUtil.createMockConfig();
         mParameters = DozeConfigurationUtil.createMockParameters();
         mSensors = new FakeSensorManager(mContext);
-        mHandler = new Handler(Looper.getMainLooper());
         mWakeLock = new WakeLockFake();
 
-        mInstrumentation.runOnMainSync(() -> {
-            mTriggers = new DozeTriggers(mContext, mMachine, mHost, mAlarmManager,
-                    mConfig, mParameters, mSensors, mHandler, mWakeLock, true);
-        });
+        mTriggers = new DozeTriggers(mContext, mMachine, mHost, mAlarmManager, mConfig, mParameters,
+                mSensors, Handler.createAsync(Looper.myLooper()), mWakeLock, true);
     }
 
     @Test
     public void testOnNotification_stillWorksAfterOneFailedProxCheck() throws Exception {
         when(mMachine.getState()).thenReturn(DozeMachine.State.DOZE);
 
-        mInstrumentation.runOnMainSync(()->{
-            mTriggers.transitionTo(DozeMachine.State.UNINITIALIZED, DozeMachine.State.INITIALIZED);
-            mTriggers.transitionTo(DozeMachine.State.INITIALIZED, DozeMachine.State.DOZE);
+        mTriggers.transitionTo(DozeMachine.State.UNINITIALIZED, DozeMachine.State.INITIALIZED);
+        mTriggers.transitionTo(DozeMachine.State.INITIALIZED, DozeMachine.State.DOZE);
 
-            mHost.callback.onNotificationHeadsUp();
-        });
+        mHost.callback.onNotificationHeadsUp();
 
-        mInstrumentation.runOnMainSync(() -> {
-            mSensors.getMockProximitySensor().sendProximityResult(false); /* Near */
-        });
+        mSensors.getMockProximitySensor().sendProximityResult(false); /* Near */
 
         verify(mMachine, never()).requestState(any());
         verify(mMachine, never()).requestPulse(anyInt());
 
-        mInstrumentation.runOnMainSync(()->{
-            mHost.callback.onNotificationHeadsUp();
-        });
+        mHost.callback.onNotificationHeadsUp();
 
-        mInstrumentation.runOnMainSync(() -> {
-            mSensors.getMockProximitySensor().sendProximityResult(true); /* Far */
-        });
+        mSensors.getMockProximitySensor().sendProximityResult(true); /* Far */
 
         verify(mMachine).requestPulse(anyInt());
     }
index b8c946d..b7c1e8e 100644 (file)
@@ -24,43 +24,36 @@ import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
 
 import android.os.Handler;
-import android.os.HandlerThread;
 import android.os.Looper;
 import android.os.SystemClock;
 import android.support.test.filters.SmallTest;
-import android.support.test.runner.AndroidJUnit4;
+import android.testing.AndroidTestingRunner;
+import android.testing.TestableLooper;
+import android.testing.TestableLooper.RunWithLooper;
 import android.view.MotionEvent;
 import android.view.ViewConfiguration;
 
 import com.android.systemui.SysuiTestCase;
-import com.android.systemui.pip.phone.PipTouchState;
 
 import org.junit.Before;
-import org.junit.Ignore;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 
 import java.util.concurrent.CountDownLatch;
-import java.util.concurrent.TimeUnit;
 
-@RunWith(AndroidJUnit4.class)
+@RunWith(AndroidTestingRunner.class)
 @SmallTest
+@RunWithLooper
 public class PipTouchStateTest extends SysuiTestCase {
 
-    private Handler mHandler;
-    private HandlerThread mHandlerThread;
     private PipTouchState mTouchState;
     private CountDownLatch mDoubleTapCallbackTriggeredLatch;
 
     @Before
     public void setUp() throws Exception {
-        mHandlerThread = new HandlerThread("PipTouchStateTestThread");
-        mHandlerThread.start();
-        mHandler = new Handler(mHandlerThread.getLooper());
-
         mDoubleTapCallbackTriggeredLatch = new CountDownLatch(1);
         mTouchState = new PipTouchState(ViewConfiguration.get(getContext()),
-                mHandler, () -> {
+                Handler.createAsync(Looper.myLooper()), () -> {
             mDoubleTapCallbackTriggeredLatch.countDown();
         });
         assertFalse(mTouchState.isDoubleTap());
@@ -91,7 +84,10 @@ public class PipTouchStateTest extends SysuiTestCase {
 
         assertTrue(mTouchState.getDoubleTapTimeoutCallbackDelay() == 10);
         mTouchState.scheduleDoubleTapTimeoutCallback();
-        mDoubleTapCallbackTriggeredLatch.await(1, TimeUnit.SECONDS);
+
+        // TODO: Remove this sleep. Its only being added because it speeds up this test a bit.
+        Thread.sleep(15);
+        TestableLooper.get(this).processAllMessages();
         assertTrue(mDoubleTapCallbackTriggeredLatch.getCount() == 0);
     }
 
index 4f98836..33b347a 100644 (file)
@@ -22,12 +22,15 @@ import static org.mockito.Mockito.when;
 
 import android.content.Context;
 import android.content.pm.UserInfo;
-import android.os.Handler;
 import android.os.Looper;
 import android.os.UserManager;
 import android.provider.Settings;
-import android.support.test.runner.AndroidJUnit4;
 import android.test.suitebuilder.annotation.SmallTest;
+import android.testing.AndroidTestingRunner;
+import android.testing.LayoutInflaterBuilder;
+import android.testing.TestableImageView;
+import android.testing.TestableLooper;
+import android.testing.TestableLooper.RunWithLooper;
 import android.text.SpannableStringBuilder;
 import android.view.LayoutInflater;
 import android.view.View;
@@ -38,8 +41,6 @@ import com.android.systemui.Dependency;
 import com.android.systemui.R;
 import com.android.systemui.SysuiTestCase;
 import com.android.systemui.statusbar.policy.SecurityController;
-import android.testing.LayoutInflaterBuilder;
-import android.testing.TestableImageView;
 
 import org.junit.Before;
 import org.junit.Test;
@@ -56,7 +57,8 @@ import org.mockito.Mockito;
 */
 
 @SmallTest
-@RunWith(AndroidJUnit4.class)
+@RunWith(AndroidTestingRunner.class)
+@RunWithLooper(setAsMainLooper = true)
 public class QSSecurityFooterTest extends SysuiTestCase {
 
     private final String MANAGING_ORGANIZATION = "organization";
@@ -81,12 +83,10 @@ public class QSSecurityFooterTest extends SysuiTestCase {
                         .build());
         mUserManager = Mockito.mock(UserManager.class);
         mContext.addMockSystemService(Context.USER_SERVICE, mUserManager);
-        Handler h = new Handler(Looper.getMainLooper());
-        h.post(() -> mFooter = new QSSecurityFooter(null, mContext));
-        waitForIdleSync(h);
+        mFooter = new QSSecurityFooter(null, mContext);
         mRootView = (ViewGroup) mFooter.getView();
-        mFooterText = (TextView) mRootView.findViewById(R.id.footer_text);
-        mFooterIcon = (TestableImageView) mRootView.findViewById(R.id.footer_icon);
+        mFooterText = mRootView.findViewById(R.id.footer_text);
+        mFooterIcon = mRootView.findViewById(R.id.footer_icon);
         mFooter.setHostEnvironment(null);
     }
 
@@ -95,7 +95,7 @@ public class QSSecurityFooterTest extends SysuiTestCase {
         when(mSecurityController.isDeviceManaged()).thenReturn(false);
         mFooter.refreshState();
 
-        waitForIdleSync(mFooter.mHandler);
+        TestableLooper.get(this).processAllMessages();
         assertEquals(View.GONE, mRootView.getVisibility());
     }
 
@@ -105,7 +105,7 @@ public class QSSecurityFooterTest extends SysuiTestCase {
         when(mSecurityController.getDeviceOwnerOrganizationName()).thenReturn(null);
         mFooter.refreshState();
 
-        waitForIdleSync(mFooter.mHandler);
+        TestableLooper.get(this).processAllMessages();
         assertEquals(mContext.getString(R.string.quick_settings_disclosure_management),
                      mFooterText.getText());
         assertEquals(View.VISIBLE, mRootView.getVisibility());
@@ -121,7 +121,7 @@ public class QSSecurityFooterTest extends SysuiTestCase {
                 .thenReturn(MANAGING_ORGANIZATION);
         mFooter.refreshState();
 
-        waitForIdleSync(mFooter.mHandler);
+        TestableLooper.get(this).processAllMessages();
         assertEquals(mContext.getString(R.string.quick_settings_disclosure_named_management,
                                         MANAGING_ORGANIZATION),
                 mFooterText.getText());
@@ -142,7 +142,7 @@ public class QSSecurityFooterTest extends SysuiTestCase {
 
         mFooter.refreshState();
 
-        waitForIdleSync(mFooter.mHandler);
+        TestableLooper.get(this).processAllMessages();
         assertEquals(View.GONE, mRootView.getVisibility());
     }
 
@@ -152,7 +152,7 @@ public class QSSecurityFooterTest extends SysuiTestCase {
         when(mSecurityController.isNetworkLoggingEnabled()).thenReturn(true);
         mFooter.refreshState();
 
-        waitForIdleSync(mFooter.mHandler);
+        TestableLooper.get(this).processAllMessages();
         assertEquals(mContext.getString(R.string.quick_settings_disclosure_management_monitoring),
                 mFooterText.getText());
         assertEquals(View.VISIBLE, mFooterIcon.getVisibility());
@@ -164,7 +164,7 @@ public class QSSecurityFooterTest extends SysuiTestCase {
                 .thenReturn(MANAGING_ORGANIZATION);
         mFooter.refreshState();
 
-        waitForIdleSync(mFooter.mHandler);
+        TestableLooper.get(this).processAllMessages();
         assertEquals(mContext.getString(
                              R.string.quick_settings_disclosure_named_management_monitoring,
                              MANAGING_ORGANIZATION),
@@ -177,7 +177,7 @@ public class QSSecurityFooterTest extends SysuiTestCase {
         when(mSecurityController.hasCACertInCurrentUser()).thenReturn(true);
         mFooter.refreshState();
 
-        waitForIdleSync(mFooter.mHandler);
+        TestableLooper.get(this).processAllMessages();
         assertEquals(mContext.getString(R.string.quick_settings_disclosure_management_monitoring),
                 mFooterText.getText());
     }
@@ -189,7 +189,7 @@ public class QSSecurityFooterTest extends SysuiTestCase {
         when(mSecurityController.getPrimaryVpnName()).thenReturn(VPN_PACKAGE);
         mFooter.refreshState();
 
-        waitForIdleSync(mFooter.mHandler);
+        TestableLooper.get(this).processAllMessages();
         assertEquals(mContext.getString(R.string.quick_settings_disclosure_management_named_vpn,
                                         VPN_PACKAGE),
                      mFooterText.getText());
@@ -201,7 +201,7 @@ public class QSSecurityFooterTest extends SysuiTestCase {
                 .thenReturn(MANAGING_ORGANIZATION);
         mFooter.refreshState();
 
-        waitForIdleSync(mFooter.mHandler);
+        TestableLooper.get(this).processAllMessages();
         assertEquals(mContext.getString(
                               R.string.quick_settings_disclosure_named_management_named_vpn,
                               MANAGING_ORGANIZATION, VPN_PACKAGE),
@@ -216,7 +216,7 @@ public class QSSecurityFooterTest extends SysuiTestCase {
         when(mSecurityController.getWorkProfileVpnName()).thenReturn(VPN_PACKAGE_2);
         mFooter.refreshState();
 
-        waitForIdleSync(mFooter.mHandler);
+        TestableLooper.get(this).processAllMessages();
         assertEquals(mContext.getString(R.string.quick_settings_disclosure_management_vpns),
                      mFooterText.getText());
         assertEquals(View.VISIBLE, mFooterIcon.getVisibility());
@@ -227,7 +227,7 @@ public class QSSecurityFooterTest extends SysuiTestCase {
                 .thenReturn(MANAGING_ORGANIZATION);
         mFooter.refreshState();
 
-        waitForIdleSync(mFooter.mHandler);
+        TestableLooper.get(this).processAllMessages();
         assertEquals(mContext.getString(R.string.quick_settings_disclosure_named_management_vpns,
                                         MANAGING_ORGANIZATION),
                      mFooterText.getText());
@@ -241,7 +241,7 @@ public class QSSecurityFooterTest extends SysuiTestCase {
         when(mSecurityController.getPrimaryVpnName()).thenReturn("VPN Test App");
         mFooter.refreshState();
 
-        waitForIdleSync(mFooter.mHandler);
+        TestableLooper.get(this).processAllMessages();
         assertEquals(View.VISIBLE, mFooterIcon.getVisibility());
         assertEquals(R.drawable.ic_qs_vpn, mFooterIcon.getLastImageResource());
         assertEquals(mContext.getString(R.string.quick_settings_disclosure_management_monitoring),
@@ -254,7 +254,7 @@ public class QSSecurityFooterTest extends SysuiTestCase {
         when(mSecurityController.hasCACertInWorkProfile()).thenReturn(true);
         mFooter.refreshState();
 
-        waitForIdleSync(mFooter.mHandler);
+        TestableLooper.get(this).processAllMessages();
         // -1 == never set.
         assertEquals(-1, mFooterIcon.getLastImageResource());
         assertEquals(mContext.getString(
@@ -266,7 +266,7 @@ public class QSSecurityFooterTest extends SysuiTestCase {
                 .thenReturn(MANAGING_ORGANIZATION);
         mFooter.refreshState();
 
-        waitForIdleSync(mFooter.mHandler);
+        TestableLooper.get(this).processAllMessages();
         assertEquals(mContext.getString(
                              R.string.quick_settings_disclosure_named_managed_profile_monitoring,
                              MANAGING_ORGANIZATION),
@@ -279,7 +279,7 @@ public class QSSecurityFooterTest extends SysuiTestCase {
         when(mSecurityController.hasCACertInCurrentUser()).thenReturn(true);
         mFooter.refreshState();
 
-        waitForIdleSync(mFooter.mHandler);
+        TestableLooper.get(this).processAllMessages();
         // -1 == never set.
         assertEquals(-1, mFooterIcon.getLastImageResource());
         assertEquals(mContext.getString(R.string.quick_settings_disclosure_monitoring),
@@ -293,7 +293,7 @@ public class QSSecurityFooterTest extends SysuiTestCase {
         when(mSecurityController.getWorkProfileVpnName()).thenReturn(VPN_PACKAGE_2);
         mFooter.refreshState();
 
-        waitForIdleSync(mFooter.mHandler);
+        TestableLooper.get(this).processAllMessages();
         assertEquals(R.drawable.ic_qs_vpn, mFooterIcon.getLastImageResource());
         assertEquals(mContext.getString(R.string.quick_settings_disclosure_vpns),
                      mFooterText.getText());
@@ -305,7 +305,7 @@ public class QSSecurityFooterTest extends SysuiTestCase {
         when(mSecurityController.getWorkProfileVpnName()).thenReturn(VPN_PACKAGE_2);
         mFooter.refreshState();
 
-        waitForIdleSync(mFooter.mHandler);
+        TestableLooper.get(this).processAllMessages();
         assertEquals(R.drawable.ic_qs_vpn, mFooterIcon.getLastImageResource());
         assertEquals(mContext.getString(
                              R.string.quick_settings_disclosure_managed_profile_named_vpn,
@@ -319,7 +319,7 @@ public class QSSecurityFooterTest extends SysuiTestCase {
         when(mSecurityController.getPrimaryVpnName()).thenReturn(VPN_PACKAGE);
         mFooter.refreshState();
 
-        waitForIdleSync(mFooter.mHandler);
+        TestableLooper.get(this).processAllMessages();
         assertEquals(R.drawable.ic_qs_vpn, mFooterIcon.getLastImageResource());
         assertEquals(mContext.getString(R.string.quick_settings_disclosure_named_vpn,
                                         VPN_PACKAGE),
@@ -328,7 +328,7 @@ public class QSSecurityFooterTest extends SysuiTestCase {
         when(mSecurityController.hasWorkProfile()).thenReturn(true);
         mFooter.refreshState();
 
-        waitForIdleSync(mFooter.mHandler);
+        TestableLooper.get(this).processAllMessages();
         assertEquals(mContext.getString(
                              R.string.quick_settings_disclosure_personal_profile_named_vpn,
                              VPN_PACKAGE),
index 66ec7dd..e5e8ae3 100644 (file)
@@ -81,7 +81,7 @@ public class TileLifecycleManagerTest extends SysuiTestCase {
         mUser = new UserHandle(UserHandle.myUserId());
         mThread = new HandlerThread("TestThread");
         mThread.start();
-        mHandler = new Handler(mThread.getLooper());
+        mHandler = Handler.createAsync(mThread.getLooper());
         mStateManager = new TileLifecycleManager(mHandler, mContext,
                 Mockito.mock(IQSService.class), new Tile(),
                 mTileServiceIntent,
index c2237c9..cc74324 100644 (file)
@@ -46,7 +46,7 @@ public class TileServiceManagerTest extends SysuiTestCase {
     public void setUp() throws Exception {
         mThread = new HandlerThread("TestThread");
         mThread.start();
-        mHandler = new Handler(mThread.getLooper());
+        mHandler = Handler.createAsync(mThread.getLooper());
         mTileServices = Mockito.mock(TileServices.class);
         Mockito.when(mTileServices.getContext()).thenReturn(mContext);
         mTileLifecycle = Mockito.mock(TileLifecycleManager.class);
index dc0e2b0..0feaa5a 100644 (file)
@@ -16,8 +16,6 @@
 
 package com.android.systemui.statusbar;
 
-import static org.mockito.ArgumentMatchers.any;
-import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
@@ -53,7 +51,6 @@ public class AppOpsListenerTest extends SysuiTestCase {
     @Mock private ForegroundServiceController mFsc;
 
     private AppOpsListener mListener;
-    private Handler mHandler;
 
     @Before
     public void setUp() {
@@ -61,8 +58,7 @@ public class AppOpsListenerTest extends SysuiTestCase {
         mDependency.injectTestDependency(NotificationEntryManager.class, mEntryManager);
         mDependency.injectTestDependency(ForegroundServiceController.class, mFsc);
         getContext().addMockSystemService(AppOpsManager.class, mAppOpsManager);
-        mHandler = new Handler(Looper.getMainLooper());
-        when(mPresenter.getHandler()).thenReturn(mHandler);
+        when(mPresenter.getHandler()).thenReturn(Handler.createAsync(Looper.myLooper()));
 
         mListener = new AppOpsListener(mContext);
     }
@@ -85,7 +81,7 @@ public class AppOpsListenerTest extends SysuiTestCase {
         mListener.setUpWithPresenter(mPresenter, mEntryManager);
         mListener.onOpActiveChanged(
                 AppOpsManager.OP_RECORD_AUDIO, TEST_UID, TEST_PACKAGE_NAME, true);
-        waitForIdleSync(mHandler);
+        TestableLooper.get(this).processAllMessages();
         verify(mEntryManager, times(1)).updateNotificationsForAppOp(
                 AppOpsManager.OP_RECORD_AUDIO, TEST_UID, TEST_PACKAGE_NAME, true);
     }
@@ -95,7 +91,7 @@ public class AppOpsListenerTest extends SysuiTestCase {
         mListener.setUpWithPresenter(mPresenter, mEntryManager);
         mListener.onOpActiveChanged(
                 AppOpsManager.OP_RECORD_AUDIO, TEST_UID, TEST_PACKAGE_NAME, true);
-        waitForIdleSync(mHandler);
+        TestableLooper.get(this).processAllMessages();
         verify(mFsc, times(1)).onAppOpChanged(
                 AppOpsManager.OP_RECORD_AUDIO, TEST_UID, TEST_PACKAGE_NAME, true);
     }
index 2a4a5ad..ff12c53 100644 (file)
@@ -17,6 +17,7 @@
 package com.android.systemui.statusbar;
 
 import static android.app.NotificationManager.IMPORTANCE_DEFAULT;
+
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
@@ -33,7 +34,8 @@ import static org.mockito.Mockito.when;
 import android.app.AppOpsManager;
 import android.app.NotificationChannel;
 import android.support.test.filters.SmallTest;
-import android.support.test.runner.AndroidJUnit4;
+import android.testing.AndroidTestingRunner;
+import android.testing.TestableLooper.RunWithLooper;
 import android.util.ArraySet;
 import android.view.NotificationHeaderView;
 import android.view.View;
@@ -49,15 +51,14 @@ import org.junit.Rule;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.Mock;
-import org.mockito.Spy;
 import org.mockito.junit.MockitoJUnit;
 import org.mockito.junit.MockitoRule;
 
 import java.util.List;
-import java.util.function.Consumer;
 
 @SmallTest
-@RunWith(AndroidJUnit4.class)
+@RunWith(AndroidTestingRunner.class)
+@RunWithLooper(setAsMainLooper = true)
 public class ExpandableNotificationRowTest extends SysuiTestCase {
 
     private ExpandableNotificationRow mGroupRow;
index 1204dda..2af0c3e 100644 (file)
@@ -55,13 +55,10 @@ public class NonPhoneDependencyTest extends SysuiTestCase {
     @Mock private NotificationGutsManager.OnSettingsClickListener mOnClickListener;
     @Mock private NotificationRemoteInputManager.Callback mRemoteInputManagerCallback;
 
-    private Handler mHandler;
-
     @Before
     public void setUp() {
         MockitoAnnotations.initMocks(this);
-        mHandler = new Handler(Looper.getMainLooper());
-        when(mPresenter.getHandler()).thenReturn(mHandler);
+        when(mPresenter.getHandler()).thenReturn(Handler.createAsync(Looper.myLooper()));
     }
 
     @Test
index a6d87af..9638541 100644 (file)
@@ -52,7 +52,7 @@ import static org.mockito.Mockito.when;
 @SmallTest
 @FlakyTest
 @org.junit.runner.RunWith(AndroidTestingRunner.class)
-@TestableLooper.RunWithLooper
+@TestableLooper.RunWithLooper(setAsMainLooper = true)
 public class NotificationBlockingHelperManagerTest extends SysuiTestCase {
 
     private NotificationBlockingHelperManager mBlockingHelperManager;
index 2ff86c3..a34588d 100644 (file)
@@ -16,9 +16,9 @@
 
 package com.android.systemui.statusbar;
 
-import android.support.test.annotation.UiThreadTest;
 import android.support.test.filters.SmallTest;
-import android.support.test.runner.AndroidJUnit4;
+import android.testing.AndroidTestingRunner;
+import android.testing.TestableLooper.RunWithLooper;
 import android.view.View;
 import android.widget.RemoteViews;
 
@@ -33,7 +33,8 @@ import org.junit.Test;
 import org.junit.runner.RunWith;
 
 @SmallTest
-@RunWith(AndroidJUnit4.class)
+@RunWith(AndroidTestingRunner.class)
+@RunWithLooper(setAsMainLooper = true)
 public class NotificationCustomViewWrapperTest extends SysuiTestCase {
 
     private ExpandableNotificationRow mRow;
index 8bdaff9..609e032 100644 (file)
@@ -47,6 +47,8 @@ import android.service.notification.StatusBarNotification;
 import android.support.test.annotation.UiThreadTest;
 import android.support.test.filters.SmallTest;
 import android.support.test.runner.AndroidJUnit4;
+import android.testing.AndroidTestingRunner;
+import android.testing.TestableLooper.RunWithLooper;
 import android.util.ArraySet;
 
 import com.android.systemui.ForegroundServiceController;
@@ -61,7 +63,8 @@ import org.mockito.Mock;
 import org.mockito.MockitoAnnotations;
 
 @SmallTest
-@RunWith(AndroidJUnit4.class)
+@RunWith(AndroidTestingRunner.class)
+@RunWithLooper(setAsMainLooper = true)
 public class NotificationDataTest extends SysuiTestCase {
 
     private static final int UID_NORMAL = 123;
index 8172626..f05cf6b 100644 (file)
@@ -100,7 +100,6 @@ public class NotificationEntryManagerTest extends SysuiTestCase {
 
     private NotificationData.Entry mEntry;
     private StatusBarNotification mSbn;
-    private Handler mHandler;
     private TestableNotificationEntryManager mEntryManager;
     private CountDownLatch mCountDownLatch;
 
@@ -160,10 +159,9 @@ public class NotificationEntryManagerTest extends SysuiTestCase {
         mDependency.injectTestDependency(VisualStabilityManager.class, mVisualStabilityManager);
         mDependency.injectTestDependency(MetricsLogger.class, mMetricsLogger);
 
-        mHandler = new Handler(Looper.getMainLooper());
         mCountDownLatch = new CountDownLatch(1);
 
-        when(mPresenter.getHandler()).thenReturn(mHandler);
+        when(mPresenter.getHandler()).thenReturn(Handler.createAsync(Looper.myLooper()));
         when(mPresenter.getNotificationLockscreenUserManager()).thenReturn(mLockscreenUserManager);
         when(mPresenter.getGroupManager()).thenReturn(mGroupManager);
         when(mRemoteInputManager.getController()).thenReturn(mRemoteInputController);
@@ -188,6 +186,7 @@ public class NotificationEntryManagerTest extends SysuiTestCase {
     @Test
     public void testAddNotification() throws Exception {
         com.android.systemui.util.Assert.isNotMainThread();
+        TestableLooper.get(this).processAllMessages();
 
         doAnswer(invocation -> {
             mCountDownLatch.countDown();
@@ -196,12 +195,10 @@ public class NotificationEntryManagerTest extends SysuiTestCase {
 
         // Post on main thread, otherwise we will be stuck waiting here for the inflation finished
         // callback forever, since it won't execute until the tests ends.
-        mHandler.post(() -> {
-            mEntryManager.addNotification(mSbn, mRankingMap);
-        });
-        assertTrue(mCountDownLatch.await(1, TimeUnit.MINUTES));
-        assertTrue(mEntryManager.getCountDownLatch().await(1, TimeUnit.MINUTES));
-        waitForIdleSync(mHandler);
+        mEntryManager.addNotification(mSbn, mRankingMap);
+        TestableLooper.get(this).processMessages(1);
+        assertTrue(mCountDownLatch.await(10, TimeUnit.SECONDS));
+        assertTrue(mEntryManager.getCountDownLatch().await(10, TimeUnit.SECONDS));
 
         // Check that no inflation error occurred.
         verify(mBarService, never()).onNotificationError(any(), any(), anyInt(), anyInt(), anyInt(),
@@ -228,17 +225,16 @@ public class NotificationEntryManagerTest extends SysuiTestCase {
     @Test
     public void testUpdateNotification() throws Exception {
         com.android.systemui.util.Assert.isNotMainThread();
+        TestableLooper.get(this).processAllMessages();
 
         mEntryManager.getNotificationData().add(mEntry);
 
         setUserSentiment(mEntry.key, NotificationListenerService.Ranking.USER_SENTIMENT_NEGATIVE);
 
-        mHandler.post(() -> {
-            mEntryManager.updateNotification(mSbn, mRankingMap);
-        });
+        mEntryManager.updateNotification(mSbn, mRankingMap);
+        TestableLooper.get(this).processMessages(1);
         // Wait for content update.
-        mEntryManager.getCountDownLatch().await(1, TimeUnit.MINUTES);
-        waitForIdleSync(mHandler);
+        assertTrue(mEntryManager.getCountDownLatch().await(10, TimeUnit.SECONDS));
 
         verify(mBarService, never()).onNotificationError(any(), any(), anyInt(), anyInt(), anyInt(),
                 any(), anyInt());
@@ -259,10 +255,7 @@ public class NotificationEntryManagerTest extends SysuiTestCase {
         mEntry.row = mRow;
         mEntryManager.getNotificationData().add(mEntry);
 
-        mHandler.post(() -> {
-            mEntryManager.removeNotification(mSbn.getKey(), mRankingMap);
-        });
-        waitForIdleSync(mHandler);
+        mEntryManager.removeNotification(mSbn.getKey(), mRankingMap);
 
         verify(mBarService, never()).onNotificationError(any(), any(), anyInt(), anyInt(), anyInt(),
                 any(), anyInt());
@@ -287,12 +280,9 @@ public class NotificationEntryManagerTest extends SysuiTestCase {
         mEntry.row = mRow;
         mEntryManager.getNotificationData().add(mEntry);
 
-        mHandler.post(() -> {
-            mEntryManager.updateNotificationsForAppOp(
-                    AppOpsManager.OP_CAMERA, mEntry.notification.getUid(),
-                    mEntry.notification.getPackageName(), true);
-        });
-        waitForIdleSync(mHandler);
+        mEntryManager.updateNotificationsForAppOp(
+                AppOpsManager.OP_CAMERA, mEntry.notification.getUid(),
+                mEntry.notification.getPackageName(), true);
 
         verify(mPresenter, times(1)).updateNotificationViews();
         assertTrue(mEntryManager.getNotificationData().get(mEntry.key).mActiveAppOps.contains(
@@ -305,10 +295,7 @@ public class NotificationEntryManagerTest extends SysuiTestCase {
 
         when(mForegroundServiceController.getStandardLayoutKey(anyInt(), anyString()))
                 .thenReturn(null);
-        mHandler.post(() -> {
-            mEntryManager.updateNotificationsForAppOp(AppOpsManager.OP_CAMERA, 1000, "pkg", true);
-        });
-        waitForIdleSync(mHandler);
+        mEntryManager.updateNotificationsForAppOp(AppOpsManager.OP_CAMERA, 1000, "pkg", true);
 
         verify(mPresenter, never()).updateNotificationViews();
     }
index 0ef2d05..cba1b54 100644 (file)
@@ -74,7 +74,7 @@ import java.util.Set;
  */
 @SmallTest
 @RunWith(AndroidTestingRunner.class)
-@TestableLooper.RunWithLooper
+@TestableLooper.RunWithLooper(setAsMainLooper = true)
 public class NotificationGutsManagerTest extends SysuiTestCase {
     private static final String TEST_CHANNEL_ID = "NotificationManagerServiceTestChannelId";
 
@@ -95,7 +95,7 @@ public class NotificationGutsManagerTest extends SysuiTestCase {
     @Before
     public void setUp() {
         mTestableLooper = TestableLooper.get(this);
-        mHandler = new Handler(mTestableLooper.getLooper());
+        mHandler = Handler.createAsync(mTestableLooper.getLooper());
 
         mHelper = new NotificationTestHelper(mContext);
 
index ef5f071..c6397e6 100644 (file)
@@ -59,7 +59,6 @@ public class NotificationListenerTest extends SysuiTestCase {
     @Mock private NotificationRemoteInputManager mRemoteInputManager;
 
     private NotificationListener mListener;
-    private Handler mHandler;
     private StatusBarNotification mSbn;
     private Set<String> mKeysKeptForRemoteInput;
 
@@ -70,10 +69,9 @@ public class NotificationListenerTest extends SysuiTestCase {
         mDependency.injectTestDependency(NotificationRemoteInputManager.class,
                 mRemoteInputManager);
 
-        mHandler = new Handler(Looper.getMainLooper());
         mKeysKeptForRemoteInput = new HashSet<>();
 
-        when(mPresenter.getHandler()).thenReturn(mHandler);
+        when(mPresenter.getHandler()).thenReturn(Handler.createAsync(Looper.myLooper()));
         when(mEntryManager.getNotificationData()).thenReturn(mNotificationData);
         when(mRemoteInputManager.getKeysKeptForRemoteInput()).thenReturn(mKeysKeptForRemoteInput);
 
@@ -87,7 +85,7 @@ public class NotificationListenerTest extends SysuiTestCase {
     @Test
     public void testNotificationAddCallsAddNotification() {
         mListener.onNotificationPosted(mSbn, mRanking);
-        waitForIdleSync(mHandler);
+        TestableLooper.get(this).processAllMessages();
         verify(mEntryManager).addNotification(mSbn, mRanking);
     }
 
@@ -95,7 +93,7 @@ public class NotificationListenerTest extends SysuiTestCase {
     public void testPostNotificationRemovesKeyKeptForRemoteInput() {
         mKeysKeptForRemoteInput.add(mSbn.getKey());
         mListener.onNotificationPosted(mSbn, mRanking);
-        waitForIdleSync(mHandler);
+        TestableLooper.get(this).processAllMessages();
         assertTrue(mKeysKeptForRemoteInput.isEmpty());
     }
 
@@ -103,21 +101,21 @@ public class NotificationListenerTest extends SysuiTestCase {
     public void testNotificationUpdateCallsUpdateNotification() {
         when(mNotificationData.get(mSbn.getKey())).thenReturn(new NotificationData.Entry(mSbn));
         mListener.onNotificationPosted(mSbn, mRanking);
-        waitForIdleSync(mHandler);
+        TestableLooper.get(this).processAllMessages();
         verify(mEntryManager).updateNotification(mSbn, mRanking);
     }
 
     @Test
     public void testNotificationRemovalCallsRemoveNotification() {
         mListener.onNotificationRemoved(mSbn, mRanking);
-        waitForIdleSync(mHandler);
+        TestableLooper.get(this).processAllMessages();
         verify(mEntryManager).removeNotification(mSbn.getKey(), mRanking);
     }
 
     @Test
     public void testRankingUpdateCallsNotificationRankingUpdate() {
         mListener.onNotificationRankingUpdate(mRanking);
-        waitForIdleSync(mHandler);
+        TestableLooper.get(this).processAllMessages();
         // RankingMap may be modified by plugins.
         verify(mEntryManager).updateNotificationRanking(any());
     }
index cb8f7ce..2401519 100644 (file)
@@ -63,7 +63,6 @@ public class NotificationLockscreenUserManagerTest extends SysuiTestCase {
     @Mock private DeviceProvisionedController mDeviceProvisionedController;
 
     private int mCurrentUserId;
-    private Handler mHandler;
     private TestNotificationLockscreenUserManager mLockscreenUserManager;
 
     @Before
@@ -73,13 +72,12 @@ public class NotificationLockscreenUserManagerTest extends SysuiTestCase {
         mDependency.injectTestDependency(DeviceProvisionedController.class,
                 mDeviceProvisionedController);
 
-        mHandler = new Handler(Looper.getMainLooper());
         mContext.addMockSystemService(UserManager.class, mUserManager);
         mCurrentUserId = ActivityManager.getCurrentUser();
 
         when(mUserManager.getProfiles(mCurrentUserId)).thenReturn(Lists.newArrayList(
                 new UserInfo(mCurrentUserId, "", 0), new UserInfo(mCurrentUserId + 1, "", 0)));
-        when(mPresenter.getHandler()).thenReturn(mHandler);
+        when(mPresenter.getHandler()).thenReturn(Handler.createAsync(Looper.myLooper()));
 
         mLockscreenUserManager = new TestNotificationLockscreenUserManager(mContext);
         mLockscreenUserManager.setUpWithPresenter(mPresenter, mEntryManager);
index 14fada5..bbb03d7 100644 (file)
@@ -26,7 +26,6 @@ import android.app.Notification;
 import android.os.Handler;
 import android.os.Looper;
 import android.os.UserHandle;
-import android.service.notification.NotificationListenerService;
 import android.service.notification.StatusBarNotification;
 import android.support.test.filters.SmallTest;
 import android.testing.AndroidTestingRunner;
@@ -35,7 +34,6 @@ import android.testing.TestableLooper;
 import com.android.internal.statusbar.IStatusBarService;
 import com.android.internal.statusbar.NotificationVisibility;
 import com.android.systemui.SysuiTestCase;
-import com.android.systemui.UiOffloadThread;
 
 import com.google.android.collect.Lists;
 
@@ -48,7 +46,7 @@ import org.mockito.MockitoAnnotations;
 
 @SmallTest
 @RunWith(AndroidTestingRunner.class)
-@TestableLooper.RunWithLooper
+@TestableLooper.RunWithLooper(setAsMainLooper = true)
 public class NotificationLoggerTest extends SysuiTestCase {
     private static final String TEST_PACKAGE_NAME = "test";
     private static final int TEST_UID = 0;
@@ -89,7 +87,7 @@ public class NotificationLoggerTest extends SysuiTestCase {
         when(mListContainer.isInVisibleLocation(any())).thenReturn(true);
         when(mNotificationData.getActiveNotifications()).thenReturn(Lists.newArrayList(mEntry));
         mLogger.getChildLocationsChangedListenerForTest().onChildLocationsChanged();
-        waitForIdleSync(mLogger.getHandlerForTest());
+        TestableLooper.get(this).processAllMessages();
         waitForUiOffloadThread();
 
         NotificationVisibility[] newlyVisibleKeys = {
@@ -101,7 +99,7 @@ public class NotificationLoggerTest extends SysuiTestCase {
         // |mEntry| won't change visibility, so it shouldn't be reported again:
         Mockito.reset(mBarService);
         mLogger.getChildLocationsChangedListenerForTest().onChildLocationsChanged();
-        waitForIdleSync(mLogger.getHandlerForTest());
+        TestableLooper.get(this).processAllMessages();
         waitForUiOffloadThread();
 
         verify(mBarService, never()).onNotificationVisibilityChanged(any(), any());
@@ -113,7 +111,7 @@ public class NotificationLoggerTest extends SysuiTestCase {
         when(mListContainer.isInVisibleLocation(any())).thenReturn(true);
         when(mNotificationData.getActiveNotifications()).thenReturn(Lists.newArrayList(mEntry));
         mLogger.getChildLocationsChangedListenerForTest().onChildLocationsChanged();
-        waitForIdleSync(mLogger.getHandlerForTest());
+        TestableLooper.get(this).processAllMessages();
         waitForUiOffloadThread();
         Mockito.reset(mBarService);
 
@@ -128,8 +126,8 @@ public class NotificationLoggerTest extends SysuiTestCase {
 
         public TestableNotificationLogger(IStatusBarService barService) {
             mBarService = barService;
-            // Make this on the main thread so we can wait for it during tests.
-            mHandler = new Handler(Looper.getMainLooper());
+            // Make this on the current thread so we can wait for it during tests.
+            mHandler = Handler.createAsync(Looper.myLooper());
         }
 
         public OnChildLocationsChangedListener
index 4829cb2..8b2f6cd 100644 (file)
@@ -45,7 +45,6 @@ public class NotificationRemoteInputManagerTest extends SysuiTestCase {
     @Mock private NotificationEntryManager mEntryManager;
     @Mock private NotificationLockscreenUserManager mLockscreenUserManager;
 
-    private Handler mHandler;
     private TestableNotificationRemoteInputManager mRemoteInputManager;
     private StatusBarNotification mSbn;
     private NotificationData.Entry mEntry;
@@ -56,9 +55,8 @@ public class NotificationRemoteInputManagerTest extends SysuiTestCase {
         mDependency.injectTestDependency(NotificationEntryManager.class, mEntryManager);
         mDependency.injectTestDependency(NotificationLockscreenUserManager.class,
                 mLockscreenUserManager);
-        mHandler = new Handler(Looper.getMainLooper());
 
-        when(mPresenter.getHandler()).thenReturn(mHandler);
+        when(mPresenter.getHandler()).thenReturn(Handler.createAsync(Looper.myLooper()));
         when(mEntryManager.getLatestRankingMap()).thenReturn(mRanking);
 
         mRemoteInputManager = new TestableNotificationRemoteInputManager(mContext);
index de2e6bf..2b0c6bf 100644 (file)
@@ -143,12 +143,10 @@ public class NotificationTestHelper {
             throws Exception {
         LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
                 mContext.LAYOUT_INFLATER_SERVICE);
-        mInstrumentation.runOnMainSync(() ->
-                mRow = (ExpandableNotificationRow) inflater.inflate(
-                        R.layout.status_bar_notification_row,
-                        null /* root */,
-                        false /* attachToRoot */)
-        );
+        mRow = (ExpandableNotificationRow) inflater.inflate(
+                R.layout.status_bar_notification_row,
+                null /* root */,
+                false /* attachToRoot */);
         ExpandableNotificationRow row = mRow;
         row.setGroupManager(mGroupManager);
         row.setHeadsUpManager(mHeadsUpManager);
index 76ed452..0d0d1f8 100644 (file)
@@ -50,7 +50,7 @@ import java.util.List;
 
 @SmallTest
 @RunWith(AndroidTestingRunner.class)
-@TestableLooper.RunWithLooper
+@TestableLooper.RunWithLooper(setAsMainLooper = true)
 public class NotificationViewHierarchyManagerTest extends SysuiTestCase {
     @Mock private NotificationPresenter mPresenter;
     @Mock private NotificationData mNotificationData;
index 1ee9b32..00e9995 100644 (file)
@@ -21,6 +21,8 @@ import static org.mockito.Mockito.verify;
 
 import android.support.test.runner.AndroidJUnit4;
 import android.test.suitebuilder.annotation.SmallTest;
+import android.testing.AndroidTestingRunner;
+import android.testing.TestableLooper.RunWithLooper;
 import android.widget.FrameLayout;
 
 import com.android.systemui.SysuiTestCase;
@@ -33,7 +35,8 @@ import org.junit.Test;
 import org.junit.runner.RunWith;
 
 @SmallTest
-@RunWith(AndroidJUnit4.class)
+@RunWith(AndroidTestingRunner.class)
+@RunWithLooper(setAsMainLooper = true)
 public class AboveShelfObserverTest extends SysuiTestCase {
 
     private AboveShelfObserver mObserver;
index 83a2883..aa8a08c 100644 (file)
@@ -18,6 +18,7 @@ package com.android.systemui.statusbar.notification;
 
 import static com.android.systemui.statusbar.notification.NotificationInflater.FLAG_REINFLATE_ALL;
 
+import static org.junit.Assert.assertTrue;
 import static org.mockito.Mockito.spy;
 import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
@@ -28,10 +29,9 @@ import android.os.CancellationSignal;
 import android.os.Handler;
 import android.os.Looper;
 import android.service.notification.StatusBarNotification;
-import android.support.test.annotation.UiThreadTest;
-import android.support.test.filters.FlakyTest;
 import android.support.test.filters.SmallTest;
-import android.support.test.runner.AndroidJUnit4;
+import android.testing.AndroidTestingRunner;
+import android.testing.TestableLooper.RunWithLooper;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.RemoteViews;
@@ -52,9 +52,11 @@ import org.junit.runner.RunWith;
 import java.util.HashMap;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.Executor;
+import java.util.concurrent.TimeUnit;
 
 @SmallTest
-@RunWith(AndroidJUnit4.class)
+@RunWith(AndroidTestingRunner.class)
+@RunWithLooper(setAsMainLooper = true)
 public class NotificationInflaterTest extends SysuiTestCase {
 
     private NotificationInflater mNotificationInflater;
@@ -85,7 +87,6 @@ public class NotificationInflaterTest extends SysuiTestCase {
     }
 
     @Test
-    @UiThreadTest
     public void testIncreasedHeadsUpBeingUsed() {
         mNotificationInflater.setUsesIncreasedHeadsUpHeight(true);
         Notification.Builder builder = spy(mBuilder);
@@ -94,7 +95,6 @@ public class NotificationInflaterTest extends SysuiTestCase {
     }
 
     @Test
-    @UiThreadTest
     public void testIncreasedHeightBeingUsed() {
         mNotificationInflater.setUsesIncreasedHeight(true);
         Notification.Builder builder = spy(mBuilder);
@@ -115,8 +115,8 @@ public class NotificationInflaterTest extends SysuiTestCase {
         mRow.getEntry().cachedBigContentView = null;
         runThenWaitForInflation(() -> mNotificationInflater.inflateNotificationViews(
                 NotificationInflater.FLAG_REINFLATE_EXPANDED_VIEW), mNotificationInflater);
-        Assert.assertTrue(mRow.getPrivateLayout().getChildCount() == 1);
-        Assert.assertTrue(mRow.getPrivateLayout().getChildAt(0)
+        assertTrue(mRow.getPrivateLayout().getChildCount() == 1);
+        assertTrue(mRow.getPrivateLayout().getChildAt(0)
                 == mRow.getPrivateLayout().getExpandedChild());
         verify(mRow).onNotificationUpdated();
     }
@@ -128,7 +128,7 @@ public class NotificationInflaterTest extends SysuiTestCase {
                 = new RemoteViews(mContext.getPackageName(), R.layout.status_bar);
         runThenWaitForInflation(() -> mNotificationInflater.inflateNotificationViews(),
                 true /* expectingException */, mNotificationInflater);
-        Assert.assertTrue(mRow.getPrivateLayout().getChildCount() == 0);
+        assertTrue(mRow.getPrivateLayout().getChildCount() == 0);
         verify(mRow, times(0)).onNotificationUpdated();
     }
 
@@ -181,12 +181,11 @@ public class NotificationInflaterTest extends SysuiTestCase {
                                 R.layout.custom_view_dark);
                     }
                 });
-        countDownLatch.await();
+        assertTrue(countDownLatch.await(500, TimeUnit.MILLISECONDS));
     }
 
     /* Cancelling requires us to be on the UI thread otherwise we might have a race */
     @Test
-    @UiThreadTest
     public void testSupersedesExistingTask() throws Exception {
         mNotificationInflater.inflateNotificationViews();
         mNotificationInflater.setIsLowPriority(true);
@@ -219,7 +218,6 @@ public class NotificationInflaterTest extends SysuiTestCase {
 
     private static void runThenWaitForInflation(Runnable block, boolean expectingException,
             NotificationInflater inflater) throws Exception {
-        com.android.systemui.util.Assert.isNotMainThread();
         CountDownLatch countDownLatch = new CountDownLatch(1);
         final ExceptionHolder exceptionHolder = new ExceptionHolder();
         inflater.setInflationCallback(new NotificationInflater.InflationCallback() {
@@ -240,9 +238,14 @@ public class NotificationInflaterTest extends SysuiTestCase {
                 }
                 countDownLatch.countDown();
             }
+
+            @Override
+            public boolean doInflateSynchronous() {
+                return true;
+            }
         });
         block.run();
-        countDownLatch.await();
+        assertTrue(countDownLatch.await(500, TimeUnit.MILLISECONDS));
         if (exceptionHolder.mException != null) {
             throw exceptionHolder.mException;
         }
@@ -257,7 +260,7 @@ public class NotificationInflaterTest extends SysuiTestCase {
     }
 
     private class AsyncFailRemoteView extends RemoteViews {
-        Handler mHandler = new Handler(Looper.getMainLooper());
+        Handler mHandler = Handler.createAsync(Looper.getMainLooper());
 
         public AsyncFailRemoteView(String packageName, int layoutId) {
             super(packageName, layoutId);
index 9da28a0..7e2e505 100644 (file)
@@ -20,6 +20,8 @@ import android.content.Context;
 import android.support.test.InstrumentationRegistry;
 import android.support.test.filters.SmallTest;
 import android.support.test.runner.AndroidJUnit4;
+import android.testing.AndroidTestingRunner;
+import android.testing.TestableLooper.RunWithLooper;
 import android.view.View;
 
 import com.android.systemui.SysuiTestCase;
@@ -30,8 +32,9 @@ import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 
-@RunWith(AndroidJUnit4.class)
+@RunWith(AndroidTestingRunner.class)
 @SmallTest
+@RunWithLooper(setAsMainLooper = true)
 public class NotificationViewWrapperTest extends SysuiTestCase {
 
     @Test
index 36fd499..23365a4 100644 (file)
@@ -51,7 +51,7 @@ public class AutoTileManagerTest extends SysuiTestCase {
     public void setUp() throws Exception {
         MockitoAnnotations.initMocks(this);
         mAutoTileManager = new AutoTileManager(mContext, mAutoAddTracker, mQsTileHost,
-                new Handler(TestableLooper.get(this).getLooper()));
+                Handler.createAsync(TestableLooper.get(this).getLooper()));
     }
 
     @Test
index 7a61bfe..537bfd4 100644 (file)
@@ -26,6 +26,8 @@ import static org.mockito.Mockito.when;
 
 import android.support.test.filters.SmallTest;
 import android.support.test.runner.AndroidJUnit4;
+import android.testing.AndroidTestingRunner;
+import android.testing.TestableLooper.RunWithLooper;
 import android.view.View;
 import android.widget.TextView;
 
@@ -47,7 +49,8 @@ import org.junit.runner.RunWith;
 import java.util.HashSet;
 
 @SmallTest
-@RunWith(AndroidJUnit4.class)
+@RunWith(AndroidTestingRunner.class)
+@RunWithLooper(setAsMainLooper = true)
 public class HeadsUpAppearanceControllerTest extends SysuiTestCase {
 
     private final NotificationStackScrollLayout mStackScroller =
index 37e0005..0a412b9 100644 (file)
@@ -48,6 +48,7 @@ import android.os.Binder;
 import android.os.Handler;
 import android.os.HandlerThread;
 import android.os.IPowerManager;
+import android.os.Looper;
 import android.os.PowerManager;
 import android.os.RemoteException;
 import android.os.UserHandle;
@@ -173,10 +174,8 @@ public class StatusBarTest extends SysuiTestCase {
         mNotificationLogger = new NotificationLogger();
 
         IPowerManager powerManagerService = mock(IPowerManager.class);
-        HandlerThread handlerThread = new HandlerThread("TestThread");
-        handlerThread.start();
         mPowerManager = new PowerManager(mContext, powerManagerService,
-                new Handler(handlerThread.getLooper()));
+                Handler.createAsync(Looper.myLooper()));
 
         CommandQueue commandQueue = mock(CommandQueue.class);
         when(commandQueue.asBinder()).thenReturn(new Binder());
index 7e1aba5..938dfca 100644 (file)
@@ -21,6 +21,7 @@ import android.app.RemoteInput;
 import android.content.Intent;
 import android.content.IntentFilter;
 import android.content.pm.ShortcutManager;
+import android.os.Handler;
 import android.support.test.filters.SmallTest;
 import android.testing.AndroidTestingRunner;
 import android.testing.TestableLooper;
@@ -28,6 +29,7 @@ import android.view.View;
 import android.widget.EditText;
 import android.widget.ImageButton;
 
+import com.android.systemui.Dependency;
 import com.android.systemui.R;
 import com.android.systemui.SysuiTestCase;
 import com.android.systemui.statusbar.ExpandableNotificationRow;
@@ -42,7 +44,7 @@ import org.mockito.Mock;
 import org.mockito.MockitoAnnotations;
 
 @RunWith(AndroidTestingRunner.class)
-@TestableLooper.RunWithLooper
+@TestableLooper.RunWithLooper(setAsMainLooper = true)
 @SmallTest
 public class RemoteInputViewTest extends SysuiTestCase {
 
@@ -60,7 +62,8 @@ public class RemoteInputViewTest extends SysuiTestCase {
         MockitoAnnotations.initMocks(this);
 
         mReceiver = new BlockingQueueIntentReceiver();
-        mContext.registerReceiver(mReceiver, new IntentFilter(TEST_ACTION));
+        mContext.registerReceiver(mReceiver, new IntentFilter(TEST_ACTION), null,
+                Handler.createAsync(Dependency.get(Dependency.BG_LOOPER)));
 
         // Avoid SecurityException RemoteInputView#sendRemoteInput().
         mContext.addMockSystemService(ShortcutManager.class, mShortcutManager);
index aca7c9c..2266b47 100644 (file)
@@ -51,7 +51,7 @@ public class SmartReplyConstantsTest extends SysuiTestCase {
         resources.addOverride(R.bool.config_smart_replies_in_notifications_enabled, true);
         resources.addOverride(
                 R.integer.config_smart_replies_in_notifications_max_squeeze_remeasure_attempts, 7);
-        mConstants = new SmartReplyConstants(new Handler(Looper.getMainLooper()), mContext);
+        mConstants = new SmartReplyConstants(Handler.createAsync(Looper.myLooper()), mContext);
     }
 
     @Test
index ff65587..7437e83 100644 (file)
@@ -24,6 +24,7 @@ import static org.mockito.Mockito.when;
 
 import android.app.NotificationManager;
 import android.os.Handler;
+import android.os.Looper;
 import android.provider.Settings;
 import android.service.notification.ZenModeConfig;
 import android.support.test.filters.SmallTest;
@@ -58,7 +59,7 @@ public class ZenModeControllerImplTest extends SysuiTestCase {
         mContext.addMockSystemService(NotificationManager.class, mNm);
         when(mNm.getZenModeConfig()).thenReturn(mConfig);
 
-        mController = new ZenModeControllerImpl(mContext, new Handler());
+        mController = new ZenModeControllerImpl(mContext, Handler.createAsync(Looper.myLooper()));
     }
 
     @Test
index 5ac965c..8a74019 100644 (file)
@@ -18,6 +18,8 @@ package com.android.systemui.statusbar.stack;
 
 import android.support.test.filters.SmallTest;
 import android.support.test.runner.AndroidJUnit4;
+import android.testing.AndroidTestingRunner;
+import android.testing.TestableLooper.RunWithLooper;
 import android.view.NotificationHeaderView;
 import android.view.View;
 
@@ -31,7 +33,8 @@ import org.junit.Test;
 import org.junit.runner.RunWith;
 
 @SmallTest
-@RunWith(AndroidJUnit4.class)
+@RunWith(AndroidTestingRunner.class)
+@RunWithLooper(setAsMainLooper = true)
 public class NotificationChildrenContainerTest extends SysuiTestCase {
 
     private ExpandableNotificationRow mGroup;
index 28d1aff..16e69f4 100644 (file)
 
 package com.android.systemui.statusbar.stack;
 
-import static org.mockito.ArgumentMatchers.anyBoolean;
-import static org.mockito.ArgumentMatchers.anyFloat;
-import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.Mockito.atLeast;
 import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.reset;
-import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.verifyZeroInteractions;
-import static org.mockito.Mockito.when;
 
-import android.support.test.annotation.UiThreadTest;
 import android.support.test.filters.SmallTest;
-import android.support.test.runner.AndroidJUnit4;
+import android.testing.AndroidTestingRunner;
+import android.testing.TestableLooper.RunWithLooper;
 import android.view.View;
 
 import com.android.systemui.SysuiTestCase;
-import com.android.systemui.statusbar.ActivatableNotificationView;
 import com.android.systemui.statusbar.ExpandableNotificationRow;
 import com.android.systemui.statusbar.NotificationTestHelper;
-import com.android.systemui.statusbar.StatusBarState;
-import com.android.systemui.statusbar.phone.ScrimController;
-import com.android.systemui.statusbar.phone.StatusBar;
 
 import org.junit.Assert;
 import org.junit.Before;
@@ -46,10 +35,10 @@ import org.junit.Test;
 import org.junit.runner.RunWith;
 
 import java.util.HashSet;
-import java.util.function.Consumer;
 
 @SmallTest
-@RunWith(AndroidJUnit4.class)
+@RunWith(AndroidTestingRunner.class)
+@RunWithLooper(setAsMainLooper = true)
 public class NotificationRoundnessManagerTest extends SysuiTestCase {
 
     private NotificationRoundnessManager mRoundnessManager = new NotificationRoundnessManager();