Make test more reliable and debuggable

- Move items that are unlikely to fail (and are referenced
in tearDown()) earlier in setUp()
- Catch and log errors in tearDown() because otherwise they are
hiding errors in the tests
- Add a delay after sending the command to allow the service, since
that is an asynchronous action

Test: atest ConditionProviderServiceTest --rerun-until-failure 25
Fixes: 276194891
Change-Id: I4df50b0443eca143ec3ef38b80abdd2770b8a750
diff --git a/tests/tests/notificationlegacy/notificationlegacy27/src/android/app/notification/legacy/cts/ConditionProviderServiceTest.java b/tests/tests/notificationlegacy/notificationlegacy27/src/android/app/notification/legacy/cts/ConditionProviderServiceTest.java
index e77667b..633b4fb 100644
--- a/tests/tests/notificationlegacy/notificationlegacy27/src/android/app/notification/legacy/cts/ConditionProviderServiceTest.java
+++ b/tests/tests/notificationlegacy/notificationlegacy27/src/android/app/notification/legacy/cts/ConditionProviderServiceTest.java
@@ -43,6 +43,8 @@
 import androidx.test.InstrumentationRegistry;
 import androidx.test.runner.AndroidJUnit4;
 
+import com.android.compatibility.common.util.AmUtils;
+
 import junit.framework.Assert;
 
 import org.junit.After;
@@ -67,6 +69,11 @@
     @Before
     public void setUp() throws Exception {
         mContext = InstrumentationRegistry.getContext();
+        mModeReceiver = new ZenModeBroadcastReceiver();
+        mModeFilter = new IntentFilter();
+        mModeFilter.addAction(NotificationManager.ACTION_INTERRUPTION_FILTER_CHANGED);
+        mContext.registerReceiver(mModeReceiver, mModeFilter,
+                Context.RECEIVER_EXPORTED_UNAUDITED);
         toggleNotificationPolicyAccess(mContext.getPackageName(),
                 InstrumentationRegistry.getInstrumentation(), true);
         LegacyConditionProviderService.requestRebind(LegacyConditionProviderService.getId());
@@ -74,35 +81,34 @@
         mNm = (NotificationManager) mContext.getSystemService(
                 Context.NOTIFICATION_SERVICE);
         mNm.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_ALL);
-        mModeReceiver = new ZenModeBroadcastReceiver();
-        mModeFilter = new IntentFilter();
-        mModeFilter.addAction(NotificationManager.ACTION_INTERRUPTION_FILTER_CHANGED);
-        mContext.registerReceiver(mModeReceiver, mModeFilter,
-                Context.RECEIVER_EXPORTED_UNAUDITED);
     }
 
     @After
-    public void tearDown() throws Exception {
-        mContext.unregisterReceiver(mModeReceiver);
-        if (mNm == null) {
-            // assumption in setUp is false, so mNm is not initialized
-            return;
-        }
+    public void tearDown() {
         try {
-            for (String id : ids) {
-                if (id != null) {
-                    if (!mNm.removeAutomaticZenRule(id)) {
-                        throw new Exception("Could not remove rule " + id);
+            if (mNm != null) {
+                try {
+                    for (String id : ids) {
+                        if (id != null) {
+                            if (!mNm.removeAutomaticZenRule(id)) {
+                                throw new Exception("Could not remove rule " + id);
+                            }
+                            sleep(100);
+                            assertNull(mNm.getAutomaticZenRule(id));
+                        }
                     }
-                    sleep(100);
-                    assertNull(mNm.getAutomaticZenRule(id));
+                } finally {
+                    toggleNotificationPolicyAccess(mContext.getPackageName(),
+                            InstrumentationRegistry.getInstrumentation(), false);
+                    pollForConnection(LegacyConditionProviderService.class, false);
+                    pollForConnection(SecondaryConditionProviderService.class, false);
                 }
             }
-        } finally {
-            toggleNotificationPolicyAccess(mContext.getPackageName(),
-                    InstrumentationRegistry.getInstrumentation(), false);
-            pollForConnection(LegacyConditionProviderService.class, false);
-            pollForConnection(SecondaryConditionProviderService.class, false);
+            if (mModeReceiver != null) {
+                mContext.unregisterReceiver(mModeReceiver);
+            }
+        } catch (Exception e) {
+            Log.e(TAG, "Error cleaning up test", e);
         }
     }
 
@@ -280,6 +286,7 @@
         String command = " cmd notification " + (on ? "allow_dnd " : "disallow_dnd ") + packageName;
 
         runCommand(command, instrumentation);
+        AmUtils.waitForBroadcastBarrier();
 
         NotificationManager nm = mContext.getSystemService(NotificationManager.class);
         Assert.assertEquals("Notification Policy Access Grant is " +