From b38eedcb8f7f3e88563639f693ea35b4c90b7f86 Mon Sep 17 00:00:00 2001 From: Neil Fuller Date: Thu, 18 Jun 2015 13:57:17 +0100 Subject: [PATCH] Remove cell / wifi manipulation from ApacheHttpClientTest The testcase was manipulating the Wifi / Cell networks, presumably in an effort to make data pass over each type of network. The test was sending / receiving data using the loopback interface so it doesn't make much sense. testExecute_withMobile would fail if the device did not have a SIM / mobile data connection *and* if the wifi network was up when the test ran; it would fail waiting for the mobile data network to enter the CONNECTED state. If the wifi connection was down at the start it would assume the mobile data connection was up already and proceed with the test (which passed, because it didn't actually use the network). This change chops out parts of this test to leave just HttpClient-related code. Bug: 21874093 Change-Id: I8248b840f58f939661eff2345f70a9e786e593f3 --- .../net/http/cts/ApacheHttpClientTest.java | 121 +----------------- 1 file changed, 2 insertions(+), 119 deletions(-) diff --git a/tests/cts/net/src/android/net/http/cts/ApacheHttpClientTest.java b/tests/cts/net/src/android/net/http/cts/ApacheHttpClientTest.java index 7d9189ff33..2e5306df89 100644 --- a/tests/cts/net/src/android/net/http/cts/ApacheHttpClientTest.java +++ b/tests/cts/net/src/android/net/http/cts/ApacheHttpClientTest.java @@ -21,56 +21,27 @@ import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; -import android.content.BroadcastReceiver; -import android.content.Context; -import android.content.Intent; -import android.content.IntentFilter; -import android.content.pm.PackageManager; -import android.net.ConnectivityManager; -import android.net.NetworkInfo; -import android.net.NetworkInfo.State; import android.net.Uri; -import android.net.wifi.WifiManager; import android.test.AndroidTestCase; -import android.util.Log; import android.webkit.cts.CtsTestServer; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.List; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.TimeUnit; public class ApacheHttpClientTest extends AndroidTestCase { - private static final String TAG = ApacheHttpClientTest.class.getSimpleName(); - private static final int NUM_DOWNLOADS = 20; private static final int SMALL_DOWNLOAD_SIZE = 100 * 1024; private CtsTestServer mWebServer; - private WifiManager mWifiManager; - - private ConnectivityManager mConnectivityManager; - - private boolean mHasTelephony; - - private boolean mHasWifi; - @Override protected void setUp() throws Exception { super.setUp(); mWebServer = new CtsTestServer(mContext); - mWifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE); - mConnectivityManager = (ConnectivityManager) - mContext.getSystemService(Context.CONNECTIVITY_SERVICE); - - PackageManager packageManager = mContext.getPackageManager(); - mHasTelephony = packageManager.hasSystemFeature(PackageManager.FEATURE_TELEPHONY); - mHasWifi = packageManager.hasSystemFeature(PackageManager.FEATURE_WIFI); } @Override @@ -79,25 +50,8 @@ public class ApacheHttpClientTest extends AndroidTestCase { mWebServer.shutdown(); } - public void testExecute_withMobile() throws Exception { - if (mHasTelephony) { - disconnectWifiToConnectToMobile(); - } - + public void testExecute() throws Exception { downloadMultipleFiles(); - - if (mHasWifi) { - connectToWifi(); - } - } - - public void testExecute_withWifi() throws Exception { - if (mHasWifi) { - if (!mWifiManager.isWifiEnabled()) { - connectToWifi(); - } - downloadMultipleFiles(); - } } private void downloadMultipleFiles() throws ClientProtocolException, IOException { @@ -134,77 +88,6 @@ public class ApacheHttpClientTest extends AndroidTestCase { numBytes += bytesRead; } } - assertEquals(message, SMALL_DOWNLOAD_SIZE, numBytes); - } - - private void connectToWifi() throws InterruptedException { - if (!mWifiManager.isWifiEnabled()) { - ConnectivityActionReceiver receiver = - new ConnectivityActionReceiver(ConnectivityManager.TYPE_WIFI, State.CONNECTED); - IntentFilter filter = new IntentFilter(); - filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); - mContext.registerReceiver(receiver, filter); - - assertTrue(mWifiManager.setWifiEnabled(true)); - assertTrue("Wifi must be configured to connect to an access point for this test.", - receiver.waitForStateChange()); - - mContext.unregisterReceiver(receiver); - } - } - - private void disconnectWifiToConnectToMobile() throws InterruptedException { - if (mHasWifi && mWifiManager.isWifiEnabled()) { - ConnectivityActionReceiver connectMobileReceiver = - new ConnectivityActionReceiver(ConnectivityManager.TYPE_MOBILE, - State.CONNECTED); - ConnectivityActionReceiver disconnectWifiReceiver = - new ConnectivityActionReceiver(ConnectivityManager.TYPE_WIFI, - State.DISCONNECTED); - IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION); - mContext.registerReceiver(connectMobileReceiver, filter); - mContext.registerReceiver(disconnectWifiReceiver, filter); - - assertTrue(mWifiManager.setWifiEnabled(false)); - assertTrue(disconnectWifiReceiver.waitForStateChange()); - assertTrue(connectMobileReceiver.waitForStateChange()); - - mContext.unregisterReceiver(connectMobileReceiver); - mContext.unregisterReceiver(disconnectWifiReceiver); - } - } - - /** Receiver that captures the last connectivity change's network type and state. */ - private class ConnectivityActionReceiver extends BroadcastReceiver { - - private final CountDownLatch mReceiveLatch = new CountDownLatch(1); - - private final int mNetworkType; - - private final State mExpectedState; - - ConnectivityActionReceiver(int networkType, State expectedState) { - mNetworkType = networkType; - mExpectedState = expectedState; - } - - public void onReceive(Context context, Intent intent) { - NetworkInfo networkInfo = intent.getExtras() - .getParcelable(ConnectivityManager.EXTRA_NETWORK_INFO); - int networkType = networkInfo.getType(); - State networkState = networkInfo.getState(); - Log.i(TAG, "Network type: " + networkType + " State: " + networkInfo.getState()); - if (networkType == mNetworkType && networkInfo.getState() == mExpectedState) { - mReceiveLatch.countDown(); - } - } - - public boolean waitForStateChange() throws InterruptedException { - return mReceiveLatch.await(30, TimeUnit.SECONDS) || hasExpectedState(); - } - - private boolean hasExpectedState() { - return mExpectedState == mConnectivityManager.getNetworkInfo(mNetworkType).getState(); - } + assertEquals(message, expectedNumBytes, numBytes); } }