Ignore non test package events for AppStandbyControllerTests

Fixes: 165604824
Test: atest AppStandbbyControllerTests --rerun-until-failure 10
Change-Id: Id444028fba7f7177613e4b49c2231745f4125b01
(cherry picked from commit 4eb996de3625ff62f8271ef67eb9bcc564a2b5b3)
diff --git a/services/tests/servicestests/src/com/android/server/usage/AppStandbyControllerTests.java b/services/tests/servicestests/src/com/android/server/usage/AppStandbyControllerTests.java
index 4dec7a1..fcd3a51 100644
--- a/services/tests/servicestests/src/com/android/server/usage/AppStandbyControllerTests.java
+++ b/services/tests/servicestests/src/com/android/server/usage/AppStandbyControllerTests.java
@@ -139,10 +139,13 @@
     private AppStandbyController mController;
 
     private CountDownLatch mStateChangedLatch = new CountDownLatch(1);
+    private String mLatchPkgName = null;
     private AppIdleStateChangeListener mListener = new AppIdleStateChangeListener() {
         @Override
         public void onAppIdleStateChanged(String packageName, int userId,
                 boolean idle, int bucket, int reason) {
+            // Ignore events not related to mLatchPkgName, if set.
+            if (mLatchPkgName != null && !mLatchPkgName.equals(packageName)) return;
             mStateChangedLatch.countDown();
         }
     };
@@ -374,6 +377,7 @@
                         mInjector.mElapsedRealtime, false));
 
         controller.addListener(mListener);
+        mLatchPkgName = null;
         return controller;
     }
 
@@ -1377,7 +1381,7 @@
 
     @Test
     public void testUnexemptedSyncScheduled() throws Exception {
-        mStateChangedLatch = new CountDownLatch(1);
+        rearmLatch(PACKAGE_1);
         mController.addListener(mListener);
         assertEquals("Test package did not start in the Never bucket", STANDBY_BUCKET_NEVER,
                 getStandbyBucket(mController, PACKAGE_1));
@@ -1389,7 +1393,7 @@
 
         setAndAssertBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_RARE, REASON_MAIN_FORCED_BY_SYSTEM);
 
-        mStateChangedLatch = new CountDownLatch(1);
+        rearmLatch(PACKAGE_1);
         mController.postReportSyncScheduled(PACKAGE_1, USER_ID, false);
         mStateChangedLatch.await(100, TimeUnit.MILLISECONDS);
         assertEquals("Unexempted sync scheduled should not elevate a non Never bucket",
@@ -1400,7 +1404,7 @@
     public void testExemptedSyncScheduled() throws Exception {
         setAndAssertBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_RARE, REASON_MAIN_FORCED_BY_SYSTEM);
         mInjector.mDeviceIdleMode = true;
-        mStateChangedLatch = new CountDownLatch(1);
+        rearmLatch(PACKAGE_1);
         mController.postReportSyncScheduled(PACKAGE_1, USER_ID, true);
         mStateChangedLatch.await(100, TimeUnit.MILLISECONDS);
         assertEquals("Exempted sync scheduled in doze should set bucket to working set",
@@ -1408,7 +1412,7 @@
 
         setAndAssertBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_RARE, REASON_MAIN_FORCED_BY_SYSTEM);
         mInjector.mDeviceIdleMode = false;
-        mStateChangedLatch = new CountDownLatch(1);
+        rearmLatch(PACKAGE_1);
         mController.postReportSyncScheduled(PACKAGE_1, USER_ID, true);
         mStateChangedLatch.await(100, TimeUnit.MILLISECONDS);
         assertEquals("Exempted sync scheduled while not in doze should set bucket to active",
@@ -1558,10 +1562,19 @@
     }
 
     private void setAndAssertBucket(String pkg, int user, int bucket, int reason) throws Exception {
-        mStateChangedLatch = new CountDownLatch(1);
+        rearmLatch(pkg);
         mController.setAppStandbyBucket(pkg, user, bucket, reason);
         mStateChangedLatch.await(100, TimeUnit.MILLISECONDS);
         assertEquals("Failed to set package bucket", bucket,
                 getStandbyBucket(mController, PACKAGE_1));
     }
+
+    private void rearmLatch(String pkgName) {
+        mLatchPkgName = pkgName;
+        mStateChangedLatch = new CountDownLatch(1);
+    }
+
+    private void rearmLatch() {
+        rearmLatch(null);
+    }
 }