diff options
Diffstat (limited to 'core/tests')
4 files changed, 482 insertions, 41 deletions
diff --git a/core/tests/ConnectivityManagerTest/AndroidManifest.xml b/core/tests/ConnectivityManagerTest/AndroidManifest.xml index 76b58e1..c318577 100644 --- a/core/tests/ConnectivityManagerTest/AndroidManifest.xml +++ b/core/tests/ConnectivityManagerTest/AndroidManifest.xml @@ -46,5 +46,5 @@ <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> <uses-permission android:name="android.permission.WRITE_SETTINGS" /> - + <uses-permission android:name="android.permission.WAKE_LOCK" /> </manifest> diff --git a/core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/ConnectivityManagerTestActivity.java b/core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/ConnectivityManagerTestActivity.java index badcc1b..6475655 100644 --- a/core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/ConnectivityManagerTestActivity.java +++ b/core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/ConnectivityManagerTestActivity.java @@ -32,6 +32,7 @@ import android.net.NetworkInfo.State; import android.net.wifi.WifiConfiguration; import android.net.wifi.WifiManager; +import android.net.wifi.WifiInfo; import android.net.wifi.ScanResult; import android.net.wifi.WifiConfiguration.KeyMgmt; @@ -58,6 +59,11 @@ public class ConnectivityManagerTestActivity extends Activity { public String mReason; public boolean mScanResultIsAvailable = false; public ConnectivityManager mCM; + public Object wifiObject = new Object(); + public Object connectivityObject = new Object(); + public int mWifiState; + public NetworkInfo mWifiNetworkInfo; + public String mBssid; /* * Control Wifi States @@ -67,7 +73,7 @@ public class ConnectivityManagerTestActivity extends Activity { /* * Verify connectivity state */ - public static final int NUM_NETWORK_TYPES = ConnectivityManager.MAX_NETWORK_TYPE; + public static final int NUM_NETWORK_TYPES = ConnectivityManager.MAX_NETWORK_TYPE + 1; NetworkState[] connectivityState = new NetworkState[NUM_NETWORK_TYPES]; /** @@ -77,6 +83,7 @@ public class ConnectivityManagerTestActivity extends Activity { private class ConnectivityReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { + Log.v(LOG_TAG, "ConnectivityReceiver: onReceive() is called with " + intent); String action = intent.getAction(); if (!action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) { Log.v("ConnectivityReceiver", "onReceive() called with " + intent); @@ -100,10 +107,16 @@ public class ConnectivityManagerTestActivity extends Activity { mReason = intent.getStringExtra(ConnectivityManager.EXTRA_REASON); mIsFailOver = intent.getBooleanExtra(ConnectivityManager.EXTRA_IS_FAILOVER, false); + + Log.v(LOG_TAG, "mNetworkInfo: " + mNetworkInfo.toString()); + if (mOtherNetworkInfo != null) { + Log.v(LOG_TAG, "mOtherNetworkInfo: " + mOtherNetworkInfo.toString()); + } recordNetworkState(mNetworkInfo.getType(), mNetworkInfo.getState()); if (mOtherNetworkInfo != null) { recordNetworkState(mOtherNetworkInfo.getType(), mOtherNetworkInfo.getState()); } + notifyNetworkConnectivityChange(); } } @@ -111,11 +124,25 @@ public class ConnectivityManagerTestActivity extends Activity { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); - if (!action.equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) { - Log.v(LOG_TAG, "onReceive() is calleld with " + intent); + Log.v("WifiReceiver", "onReceive() is calleld with " + intent); + if (action.equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) { + notifyScanResult(); + } else if (action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) { + mWifiNetworkInfo = + (NetworkInfo) intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO); + Log.v(LOG_TAG, "mWifiNetworkInfo: " + mWifiNetworkInfo.toString()); + if (mWifiNetworkInfo.getState() == State.CONNECTED) { + mBssid = intent.getStringExtra(WifiManager.EXTRA_BSSID); + } + notifyWifiState(); + } else if (action.equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) { + mWifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, + WifiManager.WIFI_STATE_UNKNOWN); + notifyWifiState(); + } + else { return; } - notifyScanResult(); } } @@ -134,14 +161,19 @@ public class ConnectivityManagerTestActivity extends Activity { setContentView(contentView); setTitle("ConnectivityManagerTestActivity"); - mConnectivityReceiver = new ConnectivityReceiver(); + // register a connectivity receiver for CONNECTIVITY_ACTION; + mConnectivityReceiver = new ConnectivityReceiver(); registerReceiver(mConnectivityReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)); mWifiReceiver = new WifiReceiver(); - registerReceiver(mWifiReceiver, - new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)); + IntentFilter mIntentFilter = new IntentFilter(); + mIntentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION); + mIntentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION); + mIntentFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION); + registerReceiver(mWifiReceiver, mIntentFilter); + // Get an instance of ConnectivityManager mCM = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); // Get an instance of WifiManager @@ -166,7 +198,7 @@ public class ConnectivityManagerTestActivity extends Activity { // deposit a network state public void recordNetworkState(int networkType, State networkState) { Log.v(LOG_TAG, "record network state for network " + networkType + - " state is " + networkState); + ", state is " + networkState); connectivityState[networkType].recordState(networkState); } @@ -190,6 +222,12 @@ public class ConnectivityManagerTestActivity extends Activity { return connectivityState[networkType].getReason(); } + private void notifyNetworkConnectivityChange() { + synchronized(connectivityObject) { + Log.v(LOG_TAG, "notify network connectivity changed"); + connectivityObject.notifyAll(); + } + } private void notifyScanResult() { synchronized (this) { Log.v(LOG_TAG, "notify that scan results are available"); @@ -197,6 +235,13 @@ public class ConnectivityManagerTestActivity extends Activity { } } + public void notifyWifiState() { + synchronized (wifiObject) { + Log.v(LOG_TAG, "notify wifi state changed"); + wifiObject.notify(); + } + } + // Return true if device is currently connected to mobile network public boolean isConnectedToMobile() { return (mNetworkInfo.getType() == ConnectivityManager.TYPE_MOBILE); @@ -259,9 +304,9 @@ public class ConnectivityManagerTestActivity extends Activity { config.SSID = sr.SSID; config.allowedKeyManagement.set(KeyMgmt.NONE); int networkId = mWifiManager.addNetwork(config); - mWifiManager.saveConfiguration(); // Connect to network by disabling others. mWifiManager.enableNetwork(networkId, true); + mWifiManager.saveConfiguration(); mWifiManager.reconnect(); break; } @@ -275,21 +320,17 @@ public class ConnectivityManagerTestActivity extends Activity { return true; } - /** - * Disable Wifi - * @return true if Wifi is disabled successfully - */ - public boolean disableWiFi() { - return mWifiManager.setWifiEnabled(false); - } - - /** - * Disconnect from the current Wifi and clear the configuration list + /* + * Disconnect from the current AP */ - public boolean clearWifi() { - if (mWifiManager.isWifiEnabled()) { + public boolean disconnectAP() { + if (mWifiManager.isWifiEnabled()) { //remove the current network Id - int curNetworkId = mWifiManager.getConnectionInfo().getNetworkId(); + WifiInfo curWifi = mWifiManager.getConnectionInfo(); + if (curWifi == null) { + return false; + } + int curNetworkId = curWifi.getNetworkId(); mWifiManager.removeNetwork(curNetworkId); mWifiManager.saveConfiguration(); @@ -303,16 +344,33 @@ public class ConnectivityManagerTestActivity extends Activity { mWifiManager.removeNetwork(conf.networkId); } } - mWifiManager.saveConfiguration(); - // disable Wifi + } + mWifiManager.saveConfiguration(); + return true; + } + /** + * Disable Wifi + * @return true if Wifi is disabled successfully + */ + public boolean disableWifi() { + return mWifiManager.setWifiEnabled(false); + } + + /** + * Disconnect from the current Wifi and clear the configuration list + */ + public boolean clearWifi() { + if (!disconnectAP()) { + return false; + } + // Disable Wifi if (!mWifiManager.setWifiEnabled(false)) { return false; } - // wait for the actions to be completed + // Wait for the actions to be completed try { Thread.sleep(5*1000); } catch (InterruptedException e) {} - } return true; } diff --git a/core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/NetworkState.java b/core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/NetworkState.java index 2e5a0f8..d586396 100644 --- a/core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/NetworkState.java +++ b/core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/NetworkState.java @@ -143,14 +143,14 @@ public class NetworkState { } State lastState = mStateDepository.get(mStateDepository.size() - 1); if ( lastState != mTransitionTarget) { - mReason += " the last state should be CONNECTED, but it is " + lastState; + mReason += "The last state should be " + mTransitionTarget + ", but it is " + lastState; return false; } for (int i = 1; i < mStateDepository.size(); i++) { State preState = mStateDepository.get(i-1); State curState = mStateDepository.get(i); if ((preState == State.DISCONNECTED) && ((curState == State.CONNECTING) || - (curState == State.CONNECTED))) { + (curState == State.CONNECTED) || (curState == State.DISCONNECTED))) { continue; } else if ((preState == State.CONNECTING) && (curState == State.CONNECTED)) { continue; diff --git a/core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/functional/ConnectivityManagerMobileTest.java b/core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/functional/ConnectivityManagerMobileTest.java index ad564ae..7641afe 100644 --- a/core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/functional/ConnectivityManagerMobileTest.java +++ b/core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/functional/ConnectivityManagerMobileTest.java @@ -14,14 +14,14 @@ * limitations under the License. */ - - package com.android.connectivitymanagertest.functional; import com.android.connectivitymanagertest.ConnectivityManagerTestActivity; import android.content.Intent; import android.content.Context; +import android.os.PowerManager; +import android.os.PowerManager.WakeLock; import android.app.Instrumentation; import android.os.Handler; import android.os.Message; @@ -29,23 +29,24 @@ import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.net.NetworkInfo.State; import android.net.NetworkInfo.DetailedState; +import android.net.wifi.WifiManager; import android.test.suitebuilder.annotation.LargeTest; import android.test.ActivityInstrumentationTestCase2; import com.android.connectivitymanagertest.ConnectivityManagerTestRunner; import com.android.connectivitymanagertest.NetworkState; import android.util.Log; -import junit.framework.*; public class ConnectivityManagerMobileTest extends ActivityInstrumentationTestCase2<ConnectivityManagerTestActivity> { - private static final String LOG_TAG = "ConnectivityManagerMobileTest"; private static final String PKG_NAME = "com.android.connectivitymanagertest"; - private static final long WIFI_CONNECTION_TIMEOUT = 30 * 1000; - private static final long WIFI_NOTIFICATION_TIMEOUT = 10 * 1000; + private static final long STATE_TRANSITION_SHORT_TIMEOUT = 5 * 1000; + private static final long STATE_TRANSITION_LONG_TIMEOUT = 30 * 1000; + private String TEST_ACCESS_POINT; private ConnectivityManagerTestActivity cmActivity; + private WakeLock wl; public ConnectivityManagerMobileTest() { super(PKG_NAME, ConnectivityManagerTestActivity.class); @@ -58,16 +59,22 @@ public class ConnectivityManagerMobileTest ConnectivityManagerTestRunner mRunner = (ConnectivityManagerTestRunner)getInstrumentation(); TEST_ACCESS_POINT = mRunner.TEST_SSID; + PowerManager pm = (PowerManager)getInstrumentation(). + getContext().getSystemService(Context.POWER_SERVICE); + wl = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "CMWakeLock"); + wl.acquire(); // Each test case will start with cellular connection + waitForNetworkState(ConnectivityManager.TYPE_MOBILE, State.CONNECTED, + STATE_TRANSITION_LONG_TIMEOUT); verifyCellularConnection(); } @Override public void tearDown() throws Exception { - // clear Wifi after each test case - cmActivity.clearWifi(); cmActivity.finish(); - Log.v(LOG_TAG, "tear down ConnectivityManager test activity"); + Log.v(LOG_TAG, "tear down ConnectivityManagerTestActivity"); + wl.release(); + cmActivity.clearWifi(); super.tearDown(); } @@ -80,6 +87,66 @@ public class ConnectivityManagerMobileTest assertTrue("no data connection", cmActivity.mState.equals(State.CONNECTED)); } + // Wait for network connectivity state: CONNECTING, CONNECTED, SUSPENDED, + // DISCONNECTING, DISCONNECTED, UNKNOWN + private void waitForNetworkState(int networkType, State expectedState, long timeout) { + long startTime = System.currentTimeMillis(); + // In case the broadcast is already sent out, no need to wait + if (cmActivity.mCM.getNetworkInfo(networkType).getState() == expectedState) { + return; + } else { + while (true) { + if ((System.currentTimeMillis() - startTime) > timeout) { + assertFalse("Wait for network state timeout", true); + } + Log.v(LOG_TAG, "Wait for the connectivity state for network: " + networkType + + " to be " + expectedState.toString()); + synchronized (cmActivity.connectivityObject) { + try { + cmActivity.connectivityObject.wait(STATE_TRANSITION_SHORT_TIMEOUT); + } catch (InterruptedException e) { + e.printStackTrace(); + } + if ((cmActivity.mNetworkInfo.getType() != networkType) || + (cmActivity.mNetworkInfo.getState() != expectedState)) { + Log.v(LOG_TAG, "network state for " + cmActivity.mNetworkInfo.getType() + + "is: " + cmActivity.mNetworkInfo.getState()); + continue; + } + break; + } + } + } + } + + // Wait for Wifi state: WIFI_STATE_DISABLED, WIFI_STATE_DISABLING, WIFI_STATE_ENABLED, + // WIFI_STATE_ENALBING, WIFI_STATE_UNKNOWN + private void waitForWifiState(int expectedState, long timeout) { + long startTime = System.currentTimeMillis(); + if (cmActivity.mWifiState == expectedState) { + return; + } else { + while (true) { + if ((System.currentTimeMillis() - startTime) > timeout) { + assertFalse("Wait for Wifi state timeout", true); + } + Log.v(LOG_TAG, "Wait for wifi state to be: " + expectedState); + synchronized (cmActivity.wifiObject) { + try { + cmActivity.wifiObject.wait(5*1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + if (cmActivity.mWifiState != expectedState) { + Log.v(LOG_TAG, "Wifi state is: " + cmActivity.mWifiNetworkInfo.getState()); + continue; + } + break; + } + } + } + } + // Test case 1: Test enabling Wifi without associating with any AP @LargeTest public void test3GToWifiNotification() { @@ -94,7 +161,7 @@ public class ConnectivityManagerMobileTest // Eanble Wifi cmActivity.enableWifi(); try { - Thread.sleep(WIFI_NOTIFICATION_TIMEOUT); + Thread.sleep(2 * STATE_TRANSITION_SHORT_TIMEOUT); } catch (Exception e) { Log.v(LOG_TAG, "exception: " + e.toString()); } @@ -110,7 +177,7 @@ public class ConnectivityManagerMobileTest Log.v(LOG_TAG, "the state for MOBILE is changed"); Log.v(LOG_TAG, "reason: " + cmActivity.getTransitionFailureReason(ConnectivityManager.TYPE_MOBILE)); - assertTrue(false); + assertTrue("state validation fail", false); } // Verify that the device is still connected to MOBILE verifyCellularConnection(); @@ -131,12 +198,124 @@ public class ConnectivityManagerMobileTest // Enable Wifi and connect to a test access point assertTrue("failed to connect to " + TEST_ACCESS_POINT, cmActivity.connectToWifi(TEST_ACCESS_POINT)); + + waitForWifiState(WifiManager.WIFI_STATE_ENABLED, STATE_TRANSITION_LONG_TIMEOUT); + Log.v(LOG_TAG, "wifi state is enabled"); + waitForNetworkState(ConnectivityManager.TYPE_WIFI, State.CONNECTED, + STATE_TRANSITION_LONG_TIMEOUT); + waitForNetworkState(ConnectivityManager.TYPE_MOBILE, State.DISCONNECTED, + STATE_TRANSITION_LONG_TIMEOUT); + + // validate states + if (!cmActivity.validateNetworkStates(ConnectivityManager.TYPE_WIFI)) { + Log.v(LOG_TAG, "Wifi state transition validation failed."); + Log.v(LOG_TAG, "reason: " + + cmActivity.getTransitionFailureReason(ConnectivityManager.TYPE_WIFI)); + assertTrue(false); + } + if (!cmActivity.validateNetworkStates(ConnectivityManager.TYPE_MOBILE)) { + Log.v(LOG_TAG, "Mobile state transition validation failed."); + Log.v(LOG_TAG, "reason: " + + cmActivity.getTransitionFailureReason(ConnectivityManager.TYPE_MOBILE)); + assertTrue(false); + } + } + + // Test case 3: connect to Wifi with known AP + @LargeTest + public void testConnectToWifWithKnownAP() { + assertNotNull("SSID is null", TEST_ACCESS_POINT); + // Connect to TEST_ACCESS_POINT + assertTrue("failed to connect to " + TEST_ACCESS_POINT, + cmActivity.connectToWifi(TEST_ACCESS_POINT)); + waitForWifiState(WifiManager.WIFI_STATE_ENABLED, STATE_TRANSITION_LONG_TIMEOUT); + waitForNetworkState(ConnectivityManager.TYPE_WIFI, State.CONNECTED, + STATE_TRANSITION_LONG_TIMEOUT); + + try { + Thread.sleep(STATE_TRANSITION_SHORT_TIMEOUT); + } catch (Exception e) { + Log.v(LOG_TAG, "exception: " + e.toString()); + } + + // Disable Wifi + Log.v(LOG_TAG, "Disable Wifi"); + if (!cmActivity.disableWifi()) { + Log.v(LOG_TAG, "disable Wifi failed"); + return; + } + + // Wait for the Wifi state to be DISABLED + waitForWifiState(WifiManager.WIFI_STATE_DISABLED, STATE_TRANSITION_LONG_TIMEOUT); + waitForNetworkState(ConnectivityManager.TYPE_WIFI, State.DISCONNECTED, + STATE_TRANSITION_LONG_TIMEOUT); + waitForNetworkState(ConnectivityManager.TYPE_MOBILE, State.CONNECTED, + STATE_TRANSITION_LONG_TIMEOUT); + + //Prepare for connectivity state verification + NetworkInfo networkInfo = cmActivity.mCM.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); + cmActivity.setStateTransitionCriteria(ConnectivityManager.TYPE_MOBILE, + networkInfo.getState(), NetworkState.DO_NOTHING, + State.DISCONNECTED); + networkInfo = cmActivity.mCM.getNetworkInfo(ConnectivityManager.TYPE_WIFI); + cmActivity.setStateTransitionCriteria(ConnectivityManager.TYPE_WIFI, networkInfo.getState(), + NetworkState.TO_CONNECTION, State.CONNECTED); + + // Enable Wifi again + Log.v(LOG_TAG, "Enable Wifi again"); + cmActivity.enableWifi(); + + // Wait for Wifi to be connected and mobile to be disconnected + waitForNetworkState(ConnectivityManager.TYPE_WIFI, State.CONNECTED, + STATE_TRANSITION_LONG_TIMEOUT); + waitForNetworkState(ConnectivityManager.TYPE_MOBILE, State.DISCONNECTED, + STATE_TRANSITION_LONG_TIMEOUT); + + // validate wifi states + if (!cmActivity.validateNetworkStates(ConnectivityManager.TYPE_WIFI)) { + Log.v(LOG_TAG, "Wifi state transition validation failed."); + Log.v(LOG_TAG, "reason: " + + cmActivity.getTransitionFailureReason(ConnectivityManager.TYPE_WIFI)); + assertTrue(false); + } + } + + // Test case 4: test disconnect Wifi + @LargeTest + public void testDisconnectWifi() { + assertNotNull("SSID is null", TEST_ACCESS_POINT); + + // connect to Wifi + assertTrue("failed to connect to " + TEST_ACCESS_POINT, + cmActivity.connectToWifi(TEST_ACCESS_POINT)); + + waitForNetworkState(ConnectivityManager.TYPE_WIFI, State.CONNECTED, + STATE_TRANSITION_LONG_TIMEOUT); + + // Wait for a few seconds to avoid the state that both Mobile and Wifi is connected try { - Thread.sleep(WIFI_CONNECTION_TIMEOUT); + Thread.sleep(STATE_TRANSITION_SHORT_TIMEOUT); } catch (Exception e) { Log.v(LOG_TAG, "exception: " + e.toString()); } + NetworkInfo networkInfo = cmActivity.mCM.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); + cmActivity.setStateTransitionCriteria(ConnectivityManager.TYPE_MOBILE, + networkInfo.getState(), + NetworkState.TO_CONNECTION, + State.CONNECTED); + networkInfo = cmActivity.mCM.getNetworkInfo(ConnectivityManager.TYPE_WIFI); + cmActivity.setStateTransitionCriteria(ConnectivityManager.TYPE_WIFI, networkInfo.getState(), + NetworkState.TO_DISCONNECTION, State.DISCONNECTED); + + // clear Wifi + cmActivity.clearWifi(); + + waitForNetworkState(ConnectivityManager.TYPE_WIFI, State.DISCONNECTED, + STATE_TRANSITION_LONG_TIMEOUT); + waitForNetworkState(ConnectivityManager.TYPE_MOBILE, State.CONNECTED, + STATE_TRANSITION_LONG_TIMEOUT); + // validate states if (!cmActivity.validateNetworkStates(ConnectivityManager.TYPE_WIFI)) { Log.v(LOG_TAG, "Wifi state transition validation failed."); @@ -151,4 +330,208 @@ public class ConnectivityManagerMobileTest assertTrue(false); } } + + // Test case 5: test connectivity from 3G to airplane mode, then to 3G again + @LargeTest + public void testDataConnectionWith3GToAmTo3G() { + //Prepare for state verification + NetworkInfo networkInfo = cmActivity.mCM.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); + cmActivity.setStateTransitionCriteria(ConnectivityManager.TYPE_MOBILE, + networkInfo.getState(), + NetworkState.TO_DISCONNECTION, + State.DISCONNECTED); + networkInfo = cmActivity.mCM.getNetworkInfo(ConnectivityManager.TYPE_WIFI); + assertEquals(State.DISCONNECTED, networkInfo.getState()); + cmActivity.setStateTransitionCriteria(ConnectivityManager.TYPE_WIFI, networkInfo.getState(), + NetworkState.DO_NOTHING, State.DISCONNECTED); + + // Enable airplane mode + cmActivity.setAirplaneMode(getInstrumentation().getContext(), true); + try { + Thread.sleep(STATE_TRANSITION_SHORT_TIMEOUT); + } catch (Exception e) { + Log.v(LOG_TAG, "exception: " + e.toString()); + } + + // Validate the state transition + if (!cmActivity.validateNetworkStates(ConnectivityManager.TYPE_WIFI)) { + Log.v(LOG_TAG, "Wifi state transition validation failed."); + Log.v(LOG_TAG, "reason: " + + cmActivity.getTransitionFailureReason(ConnectivityManager.TYPE_WIFI)); + assertTrue(false); + } + if (!cmActivity.validateNetworkStates(ConnectivityManager.TYPE_MOBILE)) { + Log.v(LOG_TAG, "Mobile state transition validation failed."); + Log.v(LOG_TAG, "reason: " + + cmActivity.getTransitionFailureReason(ConnectivityManager.TYPE_MOBILE)); + assertTrue(false); + } + + // reset state recorder + networkInfo = cmActivity.mCM.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); + cmActivity.setStateTransitionCriteria(ConnectivityManager.TYPE_MOBILE, + networkInfo.getState(), + NetworkState.TO_CONNECTION, + State.CONNECTED); + networkInfo = cmActivity.mCM.getNetworkInfo(ConnectivityManager.TYPE_WIFI); + cmActivity.setStateTransitionCriteria(ConnectivityManager.TYPE_WIFI, networkInfo.getState(), + NetworkState.DO_NOTHING, State.DISCONNECTED); + + // disable airplane mode + cmActivity.setAirplaneMode(getInstrumentation().getContext(), false); + + waitForNetworkState(ConnectivityManager.TYPE_MOBILE, State.CONNECTED, + STATE_TRANSITION_LONG_TIMEOUT); + + // Validate the state transition + if (!cmActivity.validateNetworkStates(ConnectivityManager.TYPE_MOBILE)) { + Log.v(LOG_TAG, "Mobile state transition validation failed."); + Log.v(LOG_TAG, "reason: " + + cmActivity.getTransitionFailureReason(ConnectivityManager.TYPE_MOBILE)); + assertTrue(false); + } + if (!cmActivity.validateNetworkStates(ConnectivityManager.TYPE_WIFI)) { + Log.v(LOG_TAG, "Wifi state transition validation failed."); + Log.v(LOG_TAG, "reason: " + + cmActivity.getTransitionFailureReason(ConnectivityManager.TYPE_WIFI)); + assertTrue(false); + } + } + + // Test case 6: test connectivity with airplane mode Wifi connected + @LargeTest + public void testDataConnectionOverAMWithWifi() { + assertNotNull("SSID is null", TEST_ACCESS_POINT); + // Eanble airplane mode + cmActivity.setAirplaneMode(getInstrumentation().getContext(), true); + + waitForNetworkState(ConnectivityManager.TYPE_MOBILE, State.DISCONNECTED, + STATE_TRANSITION_LONG_TIMEOUT); + + NetworkInfo networkInfo = cmActivity.mCM.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); + cmActivity.setStateTransitionCriteria(ConnectivityManager.TYPE_MOBILE, + networkInfo.getState(), + NetworkState.DO_NOTHING, + State.DISCONNECTED); + networkInfo = cmActivity.mCM.getNetworkInfo(ConnectivityManager.TYPE_WIFI); + cmActivity.setStateTransitionCriteria(ConnectivityManager.TYPE_WIFI, networkInfo.getState(), + NetworkState.TO_CONNECTION, State.CONNECTED); + + // Connect to Wifi + assertTrue("failed to connect to " + TEST_ACCESS_POINT, + cmActivity.connectToWifi(TEST_ACCESS_POINT)); + waitForNetworkState(ConnectivityManager.TYPE_WIFI, State.CONNECTED, + STATE_TRANSITION_LONG_TIMEOUT); + + // validate state and broadcast + if (!cmActivity.validateNetworkStates(ConnectivityManager.TYPE_WIFI)) { + Log.v(LOG_TAG, "state validate for Wifi failed"); + Log.v(LOG_TAG, "reason: " + + cmActivity.getTransitionFailureReason(ConnectivityManager.TYPE_WIFI)); + assertTrue("State validation failed", false); + } + if (!cmActivity.validateNetworkStates(ConnectivityManager.TYPE_MOBILE)) { + Log.v(LOG_TAG, "state validation for Mobile failed"); + Log.v(LOG_TAG, "reason: " + + cmActivity.getTransitionFailureReason(ConnectivityManager.TYPE_MOBILE)); + assertTrue("state validation failed", false); + } + cmActivity.setAirplaneMode(getInstrumentation().getContext(), false); + } + + // Test case 7: test connectivity while transit from Wifi->AM->Wifi + @LargeTest + public void testDataConnectionWithWifiToAMToWifi () { + // Connect to TEST_ACCESS_POINT + assertNotNull("SSID is null", TEST_ACCESS_POINT); + // Connect to Wifi + assertTrue("failed to connect to " + TEST_ACCESS_POINT, + cmActivity.connectToWifi(TEST_ACCESS_POINT)); + + waitForNetworkState(ConnectivityManager.TYPE_WIFI, State.CONNECTED, + STATE_TRANSITION_LONG_TIMEOUT); + + try { + Thread.sleep(STATE_TRANSITION_SHORT_TIMEOUT); + } catch (Exception e) { + Log.v(LOG_TAG, "exception: " + e.toString()); + } + + // Enable airplane mode without clearing Wifi + cmActivity.setAirplaneMode(getInstrumentation().getContext(), true); + + waitForNetworkState(ConnectivityManager.TYPE_WIFI, State.DISCONNECTED, + STATE_TRANSITION_LONG_TIMEOUT); + + try { + Thread.sleep(STATE_TRANSITION_SHORT_TIMEOUT); + } catch (Exception e) { + Log.v(LOG_TAG, "exception: " + e.toString()); + } + + // Prepare for state validation + NetworkInfo networkInfo = cmActivity.mCM.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); + cmActivity.setStateTransitionCriteria(ConnectivityManager.TYPE_MOBILE, + networkInfo.getState(),NetworkState.DO_NOTHING,State.DISCONNECTED); + networkInfo = cmActivity.mCM.getNetworkInfo(ConnectivityManager.TYPE_WIFI); + assertEquals(State.DISCONNECTED, networkInfo.getState()); + cmActivity.setStateTransitionCriteria(ConnectivityManager.TYPE_WIFI, + networkInfo.getState(), NetworkState.TO_CONNECTION, State.CONNECTED); + + // Disable airplane mode + cmActivity.setAirplaneMode(getInstrumentation().getContext(), false); + + waitForNetworkState(ConnectivityManager.TYPE_WIFI, State.CONNECTED, + STATE_TRANSITION_LONG_TIMEOUT); + + // validate the state transition + if (!cmActivity.validateNetworkStates(ConnectivityManager.TYPE_WIFI)) { + Log.v(LOG_TAG, "Wifi state transition validation failed."); + Log.v(LOG_TAG, "reason: " + + cmActivity.getTransitionFailureReason(ConnectivityManager.TYPE_WIFI)); + assertTrue(false); + } + if (!cmActivity.validateNetworkStates(ConnectivityManager.TYPE_MOBILE)) { + Log.v(LOG_TAG, "Mobile state transition validation failed."); + Log.v(LOG_TAG, "reason: " + + cmActivity.getTransitionFailureReason(ConnectivityManager.TYPE_MOBILE)); + assertTrue(false); + } + } + + // Test case 8: test wifi state change while connecting/disconnecting to/from an AP + @LargeTest + public void testWifiStateChange () { + assertNotNull("SSID is null", TEST_ACCESS_POINT); + //Connect to TEST_ACCESS_POINT + assertTrue("failed to connect to " + TEST_ACCESS_POINT, + cmActivity.connectToWifi(TEST_ACCESS_POINT)); + waitForWifiState(WifiManager.WIFI_STATE_ENABLED, STATE_TRANSITION_LONG_TIMEOUT); + waitForNetworkState(ConnectivityManager.TYPE_WIFI, State.CONNECTED, + STATE_TRANSITION_LONG_TIMEOUT); + assertNotNull("Not associated with any AP", + cmActivity.mWifiManager.getConnectionInfo().getBSSID()); + + try { + Thread.sleep(STATE_TRANSITION_SHORT_TIMEOUT); + } catch (Exception e) { + Log.v(LOG_TAG, "exception: " + e.toString()); + } + + // Disconnect from the current AP + Log.v(LOG_TAG, "disconnect from the AP"); + if (!cmActivity.disconnectAP()) { + Log.v(LOG_TAG, "failed to disconnect from " + TEST_ACCESS_POINT); + } + + // Verify the connectivity state for Wifi is DISCONNECTED + waitForNetworkState(ConnectivityManager.TYPE_WIFI, State.DISCONNECTED, + STATE_TRANSITION_LONG_TIMEOUT); + + if (!cmActivity.disableWifi()) { + Log.v(LOG_TAG, "disable Wifi failed"); + return; + } + waitForWifiState(WifiManager.WIFI_STATE_DISABLED, STATE_TRANSITION_LONG_TIMEOUT); + } } |