Use mockito.after() instead of Thread.sleep

Because the change moves the call to hander, the test needs to wait
for a timeout to make sure the call is called. Instead of using the
Thread.sleep(), it's better to use mockito.after().

Bug: 29385425
Test: atest PackageMonitorCallbackHelperTest
Change-Id: Id6ac8d6cdbe28f31bae0837414c6970f1df6f608
diff --git a/services/tests/mockingservicestests/src/com/android/server/pm/PackageMonitorCallbackHelperTest.java b/services/tests/mockingservicestests/src/com/android/server/pm/PackageMonitorCallbackHelperTest.java
index 9698552..2c2b1f5 100644
--- a/services/tests/mockingservicestests/src/com/android/server/pm/PackageMonitorCallbackHelperTest.java
+++ b/services/tests/mockingservicestests/src/com/android/server/pm/PackageMonitorCallbackHelperTest.java
@@ -19,10 +19,9 @@
 import static com.google.common.truth.Truth.assertThat;
 
 import static org.mockito.ArgumentMatchers.any;
-import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.after;
 import static org.mockito.Mockito.reset;
 import static org.mockito.Mockito.spy;
-import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
@@ -52,6 +51,7 @@
 
     private static final String FAKE_PACKAGE_NAME = "com.android.server.pm.fakeapp";
     private static final int FAKE_PACKAGE_UID = 123;
+    private static final int WAIT_CALLBACK_CALLED_IN_MS = 300;
 
     private final Handler mHandler = new Handler(Looper.getMainLooper());
     PackageMonitorCallbackHelper mPackageMonitorCallbackHelper;
@@ -78,9 +78,8 @@
 
         mPackageMonitorCallbackHelper.notifyPackageMonitor(Intent.ACTION_PACKAGE_ADDED,
                 FAKE_PACKAGE_NAME, createFakeBundle(), new int[]{0} /* userIds */);
-        Thread.sleep(300);
 
-        verify(callback, never()).sendResult(any());
+        verify(callback, after(WAIT_CALLBACK_CALLED_IN_MS).never()).sendResult(any());
     }
 
     @Test
@@ -90,17 +89,15 @@
         mPackageMonitorCallbackHelper.registerPackageMonitorCallback(callback, 0 /* userId */);
         mPackageMonitorCallbackHelper.notifyPackageMonitor(Intent.ACTION_PACKAGE_ADDED,
                 FAKE_PACKAGE_NAME, createFakeBundle(), new int[]{0});
-        Thread.sleep(300);
 
-        verify(callback, times(1)).sendResult(any());
+        verify(callback, after(WAIT_CALLBACK_CALLED_IN_MS).times(1)).sendResult(any());
 
         reset(callback);
         mPackageMonitorCallbackHelper.unregisterPackageMonitorCallback(callback);
         mPackageMonitorCallbackHelper.notifyPackageMonitor(Intent.ACTION_PACKAGE_ADDED,
                 FAKE_PACKAGE_NAME, createFakeBundle(), new int[]{0} /* userIds */);
-        Thread.sleep(300);
 
-        verify(callback, never()).sendResult(any());
+        verify(callback, after(WAIT_CALLBACK_CALLED_IN_MS).never()).sendResult(any());
     }
 
     @Test
@@ -110,10 +107,10 @@
         mPackageMonitorCallbackHelper.registerPackageMonitorCallback(callback, 0 /* userId */);
         mPackageMonitorCallbackHelper.notifyPackageMonitor(Intent.ACTION_PACKAGE_ADDED,
                 FAKE_PACKAGE_NAME, createFakeBundle(), new int[]{0} /* userIds */);
-        Thread.sleep(300);
 
         ArgumentCaptor<Bundle> bundleCaptor = ArgumentCaptor.forClass(Bundle.class);
-        verify(callback, times(1)).sendResult(bundleCaptor.capture());
+        verify(callback, after(WAIT_CALLBACK_CALLED_IN_MS).times(1)).sendResult(
+                bundleCaptor.capture());
         Bundle bundle = bundleCaptor.getValue();
         Intent intent = bundle.getParcelable(
                 PackageManager.EXTRA_PACKAGE_MONITOR_CALLBACK_RESULT, Intent.class);
@@ -132,9 +129,8 @@
         // Notify for user 10
         mPackageMonitorCallbackHelper.notifyPackageMonitor(Intent.ACTION_PACKAGE_ADDED,
                 FAKE_PACKAGE_NAME, createFakeBundle(), new int[]{10} /* userIds */);
-        Thread.sleep(300);
 
-        verify(callback, never()).sendResult(any());
+        verify(callback, after(WAIT_CALLBACK_CALLED_IN_MS).never()).sendResult(any());
     }
 
     @Test
@@ -148,10 +144,10 @@
         mPackageMonitorCallbackHelper.notifyPackageChanged(FAKE_PACKAGE_NAME,
                 false /* dontKillApp */, components, FAKE_PACKAGE_UID, null /* reason */,
                 new int[]{0} /* userIds */);
-        Thread.sleep(300);
 
         ArgumentCaptor<Bundle> bundleCaptor = ArgumentCaptor.forClass(Bundle.class);
-        verify(callback, times(1)).sendResult(bundleCaptor.capture());
+        verify(callback, after(WAIT_CALLBACK_CALLED_IN_MS).times(1)).sendResult(
+                bundleCaptor.capture());
         Bundle bundle = bundleCaptor.getValue();
         Intent intent = bundle.getParcelable(
                 PackageManager.EXTRA_PACKAGE_MONITOR_CALLBACK_RESULT, Intent.class);
@@ -175,10 +171,10 @@
         mPackageMonitorCallbackHelper.notifyPackageAddedForNewUsers(FAKE_PACKAGE_NAME,
                 FAKE_PACKAGE_UID, new int[]{0} /* userIds */, new int[0],
                 PackageInstaller.DATA_LOADER_TYPE_STREAMING);
-        Thread.sleep(300);
 
         ArgumentCaptor<Bundle> bundleCaptor = ArgumentCaptor.forClass(Bundle.class);
-        verify(callback, times(1)).sendResult(bundleCaptor.capture());
+        verify(callback, after(WAIT_CALLBACK_CALLED_IN_MS).times(1)).sendResult(
+                bundleCaptor.capture());
         Bundle bundle = bundleCaptor.getValue();
         Intent intent = bundle.getParcelable(
                 PackageManager.EXTRA_PACKAGE_MONITOR_CALLBACK_RESULT, Intent.class);
@@ -198,10 +194,10 @@
         mPackageMonitorCallbackHelper.notifyResourcesChanged(true /* mediaStatus */,
                 true /* replacing */, new String[]{FAKE_PACKAGE_NAME},
                 new int[]{FAKE_PACKAGE_UID} /* uids */);
-        Thread.sleep(300);
 
         ArgumentCaptor<Bundle> bundleCaptor = ArgumentCaptor.forClass(Bundle.class);
-        verify(callback, times(1)).sendResult(bundleCaptor.capture());
+        verify(callback, after(WAIT_CALLBACK_CALLED_IN_MS).times(1)).sendResult(
+                bundleCaptor.capture());
         Bundle bundle = bundleCaptor.getValue();
         Intent intent = bundle.getParcelable(
                 PackageManager.EXTRA_PACKAGE_MONITOR_CALLBACK_RESULT, Intent.class);