From 61b18d0d8ed901b202f402ab351bd0481271ff02 Mon Sep 17 00:00:00 2001 From: Joel Scherpelz Date: Wed, 12 Apr 2017 11:32:02 +0900 Subject: [PATCH] Refactor repeated WIFI bringup code in ConnectivityManagerTest This is a minor code health change motivated by upcoming additions to ConnectivityManagerTest. Common code to drive WIFI to a connected state is now shared along with automatic cleanup code to restore wifi to its original state in tearDown(). Bug: 33333670 Test: CtsNetTestCases test passes Change-Id: I62a2748f113650f7a235af3074e33cc0cefe4a3b (cherry picked from commit 49eaabd72c1a5346750342ffc956c4b903ca532c) --- .../net/cts/ConnectivityManagerTest.java | 46 ++++++++++--------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/tests/cts/net/src/android/net/cts/ConnectivityManagerTest.java b/tests/cts/net/src/android/net/cts/ConnectivityManagerTest.java index 24871ca156..22dfd1babc 100644 --- a/tests/cts/net/src/android/net/cts/ConnectivityManagerTest.java +++ b/tests/cts/net/src/android/net/cts/ConnectivityManagerTest.java @@ -91,6 +91,7 @@ public class ConnectivityManagerTest extends AndroidTestCase { private PackageManager mPackageManager; private final HashMap mNetworks = new HashMap(); + boolean mWifiConnectAttempted; @Override protected void setUp() throws Exception { @@ -99,6 +100,7 @@ public class ConnectivityManagerTest extends AndroidTestCase { mCm = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE); mWifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE); mPackageManager = mContext.getPackageManager(); + mWifiConnectAttempted = false; // Get com.android.internal.R.array.networkAttributes int resId = mContext.getResources().getIdentifier("networkAttributes", "array", "android"); @@ -116,6 +118,27 @@ public class ConnectivityManagerTest extends AndroidTestCase { } } + @Override + protected void tearDown() throws Exception { + // Return WiFi to its original disabled state after tests that explicitly connect. + if (mWifiConnectAttempted) { + disconnectFromWifi(null); + } + } + + /** + * Make sure WiFi is connected to an access point if it is not already. If + * WiFi is enabled as a result of this function, it will be disabled + * automatically in tearDown(). + */ + private Network ensureWifiConnected() { + if (mWifiManager.isWifiEnabled()) { + return getWifiNetwork(); + } + mWifiConnectAttempted = true; + return connectToWifi(); + } + public void testIsNetworkTypeValid() { assertTrue(ConnectivityManager.isNetworkTypeValid(ConnectivityManager.TYPE_MOBILE)); assertTrue(ConnectivityManager.isNetworkTypeValid(ConnectivityManager.TYPE_WIFI)); @@ -298,14 +321,10 @@ public class ConnectivityManagerTest extends AndroidTestCase { final TestNetworkCallback defaultTrackingCallback = new TestNetworkCallback(); mCm.registerDefaultNetworkCallback(defaultTrackingCallback); - final boolean previousWifiEnabledState = mWifiManager.isWifiEnabled(); Network wifiNetwork = null; try { - // Make sure WiFi is connected to an access point to start with. - if (!previousWifiEnabledState) { - connectToWifi(); - } + ensureWifiConnected(); // Now we should expect to get a network callback about availability of the wifi // network even if it was already connected as a state-based action when the callback @@ -321,11 +340,6 @@ public class ConnectivityManagerTest extends AndroidTestCase { } finally { mCm.unregisterNetworkCallback(callback); mCm.unregisterNetworkCallback(defaultTrackingCallback); - - // Return WiFi to its original enabled/disabled state. - if (!previousWifiEnabledState) { - disconnectFromWifi(wifiNetwork); - } } } @@ -357,13 +371,8 @@ public class ConnectivityManagerTest extends AndroidTestCase { // We will register for a WIFI network being available or lost. mCm.registerNetworkCallback(makeWifiNetworkRequest(), pendingIntent); - final boolean previousWifiEnabledState = mWifiManager.isWifiEnabled(); - try { - // Make sure WiFi is connected to an access point to start with. - if (!previousWifiEnabledState) { - connectToWifi(); - } + ensureWifiConnected(); // Now we expect to get the Intent delivered notifying of the availability of the wifi // network even if it was already connected as a state-based action when the callback @@ -376,11 +385,6 @@ public class ConnectivityManagerTest extends AndroidTestCase { mCm.unregisterNetworkCallback(pendingIntent); pendingIntent.cancel(); mContext.unregisterReceiver(receiver); - - // Return WiFi to its original enabled/disabled state. - if (!previousWifiEnabledState) { - disconnectFromWifi(null); - } } }