Fix a way for this test to flake.

While tests are hopefully cleaning up after themselves,
there is no guarantee that there is a currently available
default network milliseconds after any given test. Some
tests need to disconnect to check something, or to change
a property of the wifi network that they have to revert at
the end for example. Or, a test may fail leaving the device
without a default network.

To make sure the state is correctly cleaned up, have
tearDown make sure the device is connected to a working
Internet connection before the test ends, so that the next
test can be sure to find established connectivity
immediately. It's possible the device needs a few hundred
milliseconds to re-establish connectivity, so this patch
gives a grace of up to 30 seconds (the default waiting
timer for TestCallback) for connectivity to be restored at
the end of any test.

Bug: 161767594 and others
Test: ConnectivityManagerTest
Change-Id: I8cf3e6e6c116cabd5b04bf2562da84f7e635ca2a
diff --git a/tests/cts/net/src/android/net/cts/ConnectivityManagerTest.java b/tests/cts/net/src/android/net/cts/ConnectivityManagerTest.java
index 455da26..fb62514 100644
--- a/tests/cts/net/src/android/net/cts/ConnectivityManagerTest.java
+++ b/tests/cts/net/src/android/net/cts/ConnectivityManagerTest.java
@@ -219,6 +219,16 @@
         if (mCtsNetUtils.cellConnectAttempted()) {
             mCtsNetUtils.disconnectFromCell();
         }
+
+        // All tests in this class require a working Internet connection as they start. Make
+        // sure there is still one as they end that's ready to use for the next test to use.
+        final TestNetworkCallback callback = new TestNetworkCallback();
+        mCm.registerDefaultNetworkCallback(callback);
+        try {
+            assertNotNull("Couldn't restore Internet connectivity", callback.waitForAvailable());
+        } finally {
+            mCm.unregisterNetworkCallback(callback);
+        }
     }
 
     /**