Merge "Catch exception of unregisterNetworkCallback"

This commit is contained in:
Treehugger Robot
2021-08-05 01:37:30 +00:00
committed by Gerrit Code Review

View File

@@ -381,7 +381,7 @@ public class ConnectivityManagerTest {
try { try {
assertNotNull("Couldn't restore Internet connectivity", callback.waitForAvailable()); assertNotNull("Couldn't restore Internet connectivity", callback.waitForAvailable());
} finally { } finally {
mCm.unregisterNetworkCallback(callback); unregisterCallbackQuietly(callback);
} }
} }
@@ -802,12 +802,12 @@ public class ConnectivityManagerTest {
} catch (InterruptedException e) { } catch (InterruptedException e) {
fail("Broadcast receiver or NetworkCallback wait was interrupted."); fail("Broadcast receiver or NetworkCallback wait was interrupted.");
} finally { } finally {
mCm.unregisterNetworkCallback(callback); unregisterCallbackQuietly(callback);
mCm.unregisterNetworkCallback(defaultTrackingCallback); unregisterCallbackQuietly(defaultTrackingCallback);
if (TestUtils.shouldTestSApis()) { if (TestUtils.shouldTestSApis()) {
mCm.unregisterNetworkCallback(systemDefaultCallback); unregisterCallbackQuietly(systemDefaultCallback);
mCm.unregisterNetworkCallback(perUidCallback); unregisterCallbackQuietly(perUidCallback);
mCm.unregisterNetworkCallback(bestMatchingCallback); unregisterCallbackQuietly(bestMatchingCallback);
} }
} }
} }
@@ -993,7 +993,7 @@ public class ConnectivityManagerTest {
} catch (InterruptedException e) { } catch (InterruptedException e) {
fail("NetworkCallback wait was interrupted."); fail("NetworkCallback wait was interrupted.");
} finally { } finally {
mCm.unregisterNetworkCallback(callback); unregisterCallbackQuietly(callback);
} }
} }
@@ -1021,7 +1021,7 @@ public class ConnectivityManagerTest {
} catch (InterruptedException e) { } catch (InterruptedException e) {
fail("NetworkCallback wait was interrupted."); fail("NetworkCallback wait was interrupted.");
} finally { } finally {
mCm.unregisterNetworkCallback(callback); unregisterCallbackQuietly(callback);
if (previousWifiEnabledState) { if (previousWifiEnabledState) {
mCtsNetUtils.connectToWifi(); mCtsNetUtils.connectToWifi();
} }
@@ -1875,8 +1875,8 @@ public class ConnectivityManagerTest {
if (supportWifi) waitForAvailable(wifiCb); if (supportWifi) waitForAvailable(wifiCb);
if (supportTelephony) waitForAvailable(telephonyCb); if (supportTelephony) waitForAvailable(telephonyCb);
} finally { } finally {
if (supportWifi) mCm.unregisterNetworkCallback(wifiCb); if (supportWifi) unregisterCallbackQuietly(wifiCb);
if (supportTelephony) mCm.unregisterNetworkCallback(telephonyCb); if (supportTelephony) unregisterCallbackQuietly(telephonyCb);
// Restore the previous state of airplane mode and permissions: // Restore the previous state of airplane mode and permissions:
runShellCommand("cmd connectivity airplane-mode " runShellCommand("cmd connectivity airplane-mode "
+ (isAirplaneModeEnabled ? "enable" : "disable")); + (isAirplaneModeEnabled ? "enable" : "disable"));
@@ -2014,7 +2014,7 @@ public class ConnectivityManagerTest {
assertNotNull("NetworkCapabilities of the network is null", nc); assertNotNull("NetworkCapabilities of the network is null", nc);
assertEquals(hasSsid, Pattern.compile(ssid).matcher(nc.toString()).find()); assertEquals(hasSsid, Pattern.compile(ssid).matcher(nc.toString()).find());
} finally { } finally {
mCm.unregisterNetworkCallback(callback); unregisterCallbackQuietly(callback);
} }
} }
@@ -2097,7 +2097,7 @@ public class ConnectivityManagerTest {
} }
testNetworkInterface.getFileDescriptor().close(); testNetworkInterface.getFileDescriptor().close();
}, new String[] { android.Manifest.permission.MANAGE_TEST_NETWORKS }); }, new String[] { android.Manifest.permission.MANAGE_TEST_NETWORKS });
mCm.unregisterNetworkCallback(callback); unregisterCallbackQuietly(callback);
} }
} }
@@ -2552,7 +2552,7 @@ public class ConnectivityManagerTest {
// Reject partial connectivity network should cause the network being torn down // Reject partial connectivity network should cause the network being torn down
assertEquals(network, cb.waitForLost()); assertEquals(network, cb.waitForLost());
} finally { } finally {
mCm.unregisterNetworkCallback(cb); unregisterCallbackQuietly(cb);
resetValidationConfig(); resetValidationConfig();
// Wifi will not automatically reconnect to the network. ensureWifiDisconnected cannot // Wifi will not automatically reconnect to the network. ensureWifiDisconnected cannot
// apply here. Thus, turn off wifi first and restart to restore. // apply here. Thus, turn off wifi first and restart to restore.
@@ -2593,7 +2593,7 @@ public class ConnectivityManagerTest {
}); });
waitForLost(wifiCb); waitForLost(wifiCb);
} finally { } finally {
mCm.unregisterNetworkCallback(wifiCb); unregisterCallbackQuietly(wifiCb);
resetValidationConfig(); resetValidationConfig();
/// Wifi will not automatically reconnect to the network. ensureWifiDisconnected cannot /// Wifi will not automatically reconnect to the network. ensureWifiDisconnected cannot
// apply here. Thus, turn off wifi first and restart to restore. // apply here. Thus, turn off wifi first and restart to restore.
@@ -2658,8 +2658,8 @@ public class ConnectivityManagerTest {
wifiCb.assertNoCallbackThat(NO_CALLBACK_TIMEOUT_MS, wifiCb.assertNoCallbackThat(NO_CALLBACK_TIMEOUT_MS,
c -> !(c instanceof CallbackEntry.LinkPropertiesChanged)); c -> !(c instanceof CallbackEntry.LinkPropertiesChanged));
} finally { } finally {
mCm.unregisterNetworkCallback(wifiCb); unregisterCallbackQuietly(wifiCb);
mCm.unregisterNetworkCallback(defaultCb); unregisterCallbackQuietly(defaultCb);
resetAvoidBadWifi(previousAvoidBadWifi); resetAvoidBadWifi(previousAvoidBadWifi);
resetValidationConfig(); resetValidationConfig();
// Reconnect wifi to reset the wifi status // Reconnect wifi to reset the wifi status
@@ -2701,7 +2701,7 @@ public class ConnectivityManagerTest {
mCm.registerNetworkCallback(new NetworkRequest.Builder().build(), cb); mCm.registerNetworkCallback(new NetworkRequest.Builder().build(), cb);
return future.get(timeout, TimeUnit.MILLISECONDS); return future.get(timeout, TimeUnit.MILLISECONDS);
} finally { } finally {
mCm.unregisterNetworkCallback(cb); unregisterCallbackQuietly(cb);
} }
} }
@@ -2788,6 +2788,14 @@ public class ConnectivityManagerTest {
System.currentTimeMillis() + WIFI_CONNECT_TIMEOUT_MS); System.currentTimeMillis() + WIFI_CONNECT_TIMEOUT_MS);
} }
private void unregisterCallbackQuietly(NetworkCallback cb) {
try {
mCm.unregisterNetworkCallback(cb);
} catch (IllegalArgumentException e) {
Log.i(TAG, "Try to unregister a not registered NetworkCallback!");
}
}
@AppModeFull(reason = "Cannot get WifiManager in instant app mode") @AppModeFull(reason = "Cannot get WifiManager in instant app mode")
@Test @Test
public void testMobileDataPreferredUids() throws Exception { public void testMobileDataPreferredUids() throws Exception {
@@ -2850,8 +2858,8 @@ public class ConnectivityManagerTest {
// Active network for CtsNetTestCases uid should change back to wifi. // Active network for CtsNetTestCases uid should change back to wifi.
assertEquals(wifiNetwork, mCm.getActiveNetwork()); assertEquals(wifiNetwork, mCm.getActiveNetwork());
} finally { } finally {
mCm.unregisterNetworkCallback(systemDefaultCb); unregisterCallbackQuietly(systemDefaultCb);
mCm.unregisterNetworkCallback(defaultTrackingCb); unregisterCallbackQuietly(defaultTrackingCb);
// Restore setting. // Restore setting.
ConnectivitySettingsManager.setMobileDataPreferredUids( ConnectivitySettingsManager.setMobileDataPreferredUids(
@@ -2953,7 +2961,7 @@ public class ConnectivityManagerTest {
// TODD: Have a significant signal to know the uids has been send to netd. // TODD: Have a significant signal to know the uids has been send to netd.
assertBindSocketToNetworkSuccess(network); assertBindSocketToNetworkSuccess(network);
} finally { } finally {
mCm.unregisterNetworkCallback(testNetworkCb); unregisterCallbackQuietly(testNetworkCb);
agent.unregister(); agent.unregister();
// Restore setting. // Restore setting.