Backport from HC to Fix NetworkInfoTest
Bug 4322307
Fixed some other assertions that didn't check for
either CONNECTING or CONNECTED. Cleaned up some
code and removed some extraneous checks.
Original description:
"Fix cts tests for Wi-Fi only devices.
For Wi-Fi only device,
startUsingNetworkFeature() returns Phone.APN_TYPE_NOT_AVAILABLE
stopUsingNetworkFeature() return 1;
In NetworkInfoTest:
cm.getAllNetworkInfo() returns an array of NetworkInfo.
But the index is not based on network type."
Change-Id: I0b5e3d0cfe5fac18bca0a5ca2ce4cc73bc4dfa16
diff --git a/tests/tests/net/src/android/net/cts/ConnectivityManagerTest.java b/tests/tests/net/src/android/net/cts/ConnectivityManagerTest.java
index 12cc21e..96a935b 100644
--- a/tests/tests/net/src/android/net/cts/ConnectivityManagerTest.java
+++ b/tests/tests/net/src/android/net/cts/ConnectivityManagerTest.java
@@ -51,8 +51,9 @@
private ConnectivityManager mCm;
private WifiManager mWifiManager;
private PackageManager mPackageManager;
- // must include both mobile data + wifi
- private static final int MIN_NUM_NETWORK_TYPES = 2;
+
+ // device could have only one interface: data, wifi.
+ private static final int MIN_NUM_NETWORK_TYPES = 1;
@Override
protected void setUp() throws Exception {
@@ -68,28 +69,27 @@
args = {int.class}
)
public void testGetNetworkInfo() {
-
- // this test assumes that there are at least two network types.
assertTrue(mCm.getAllNetworkInfo().length >= MIN_NUM_NETWORK_TYPES);
- NetworkInfo ni = mCm.getNetworkInfo(1);
- State state = ni.getState();
- assertTrue(State.UNKNOWN.ordinal() >= state.ordinal()
- && state.ordinal() >= State.CONNECTING.ordinal());
- DetailedState ds = ni.getDetailedState();
- assertTrue(DetailedState.FAILED.ordinal() >= ds.ordinal()
- && ds.ordinal() >= DetailedState.IDLE.ordinal());
-
- ni = mCm.getNetworkInfo(0);
- state = ni.getState();
- assertTrue(State.UNKNOWN.ordinal() >= state.ordinal()
- && state.ordinal() >= State.CONNECTING.ordinal());
- ds = ni.getDetailedState();
- assertTrue(DetailedState.FAILED.ordinal() >= ds.ordinal()
- && ds.ordinal() >= DetailedState.IDLE.ordinal());
-
+ NetworkInfo ni = mCm.getNetworkInfo(TYPE_WIFI);
+ if (ni != null) {
+ State state = ni.getState();
+ assertTrue(State.UNKNOWN.ordinal() >= state.ordinal()
+ && state.ordinal() >= State.CONNECTING.ordinal());
+ DetailedState ds = ni.getDetailedState();
+ assertTrue(DetailedState.FAILED.ordinal() >= ds.ordinal()
+ && ds.ordinal() >= DetailedState.IDLE.ordinal());
+ }
+ ni = mCm.getNetworkInfo(TYPE_MOBILE);
+ if (ni != null) {
+ State state = ni.getState();
+ assertTrue(State.UNKNOWN.ordinal() >= state.ordinal()
+ && state.ordinal() >= State.CONNECTING.ordinal());
+ DetailedState ds = ni.getDetailedState();
+ assertTrue(DetailedState.FAILED.ordinal() >= ds.ordinal()
+ && ds.ordinal() >= DetailedState.IDLE.ordinal());
+ }
ni = mCm.getNetworkInfo(-1);
assertNull(ni);
-
}
@TestTargets({
@@ -142,9 +142,21 @@
final String invalidateFeature = "invalidateFeature";
final String mmsFeature = "enableMMS";
final int failureCode = -1;
+ final int wifiOnlyStartFailureCode = 3;
+ final int wifiOnlyStopFailureCode = 1;
- assertEquals(failureCode, mCm.startUsingNetworkFeature(TYPE_MOBILE, invalidateFeature));
- assertEquals(failureCode, mCm.stopUsingNetworkFeature(TYPE_MOBILE, invalidateFeature));
+ NetworkInfo ni = mCm.getNetworkInfo(TYPE_MOBILE);
+ if (ni != null) {
+ assertEquals(failureCode, mCm.startUsingNetworkFeature(TYPE_MOBILE,
+ invalidateFeature));
+ assertEquals(failureCode, mCm.stopUsingNetworkFeature(TYPE_MOBILE,
+ invalidateFeature));
+ } else {
+ assertEquals(wifiOnlyStartFailureCode, mCm.startUsingNetworkFeature(TYPE_MOBILE,
+ invalidateFeature));
+ assertEquals(wifiOnlyStopFailureCode, mCm.stopUsingNetworkFeature(TYPE_MOBILE,
+ invalidateFeature));
+ }
// Should return failure(-1) because MMS is not supported on WIFI.
assertEquals(failureCode, mCm.startUsingNetworkFeature(TYPE_WIFI, mmsFeature));
diff --git a/tests/tests/net/src/android/net/cts/NetworkInfoTest.java b/tests/tests/net/src/android/net/cts/NetworkInfoTest.java
index 99e8e15..6800c43 100644
--- a/tests/tests/net/src/android/net/cts/NetworkInfoTest.java
+++ b/tests/tests/net/src/android/net/cts/NetworkInfoTest.java
@@ -16,16 +16,14 @@
package android.net.cts;
+import dalvik.annotation.TestTargetClass;
+
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.DetailedState;
import android.net.NetworkInfo.State;
import android.test.AndroidTestCase;
-import dalvik.annotation.TestLevel;
-import dalvik.annotation.TestTargetClass;
-import dalvik.annotation.TestTargetNew;
-import dalvik.annotation.TestTargets;
@TestTargetClass(NetworkInfo.class)
public class NetworkInfoTest extends AndroidTestCase {
@@ -35,135 +33,41 @@
public static final String MOBILE_TYPE_NAME = "mobile";
public static final String WIFI_TYPE_NAME = "WIFI";
- @TestTargets({
- @TestTargetNew(
- level = TestLevel.COMPLETE,
- method = "isConnectedOrConnecting",
- args = {}
- ),
- @TestTargetNew(
- level = TestLevel.COMPLETE,
- method = "setFailover",
- args = {boolean.class}
- ),
- @TestTargetNew(
- level = TestLevel.COMPLETE,
- method = "isFailover",
- args = {}
- ),
- @TestTargetNew(
- level = TestLevel.COMPLETE,
- method = "isRoaming",
- args = {}
- ),
- @TestTargetNew(
- level = TestLevel.COMPLETE,
- method = "getType",
- args = {}
- ),
- @TestTargetNew(
- level = TestLevel.COMPLETE,
- method = "getSubtype",
- args = {}
- ),
- @TestTargetNew(
- level = TestLevel.COMPLETE,
- method = "getTypeName",
- args = {}
- ),
- @TestTargetNew(
- level = TestLevel.COMPLETE,
- method = "getSubtypeName",
- args = {}
- ),
- @TestTargetNew(
- level = TestLevel.COMPLETE,
- method = "setIsAvailable",
- args = {boolean.class}
- ),
- @TestTargetNew(
- level = TestLevel.COMPLETE,
- method = "isAvailable",
- args = {}
- ),
- @TestTargetNew(
- level = TestLevel.COMPLETE,
- method = "isConnected",
- args = {}
- ),
- @TestTargetNew(
- level = TestLevel.COMPLETE,
- method = "getDetailedState",
- args = {}
- ),
- @TestTargetNew(
- level = TestLevel.COMPLETE,
- method = "getState",
- args = {}
- ),
- @TestTargetNew(
- level = TestLevel.COMPLETE,
- method = "getReason",
- args = {}
- ),
- @TestTargetNew(
- level = TestLevel.COMPLETE,
- method = "getExtraInfo",
- args = {}
- ),
- @TestTargetNew(
- level = TestLevel.COMPLETE,
- method = "toString",
- args = {}
- )
- })
public void testAccessNetworkInfoProperties() {
ConnectivityManager cm = (ConnectivityManager) getContext().getSystemService(
Context.CONNECTIVITY_SERVICE);
-
NetworkInfo[] ni = cm.getAllNetworkInfo();
- assertTrue(ni.length >= 2);
+ assertTrue(ni.length >= 1);
- assertFalse(ni[TYPE_MOBILE].isFailover());
- assertFalse(ni[TYPE_WIFI].isFailover());
-
- // test environment:connect as TYPE_MOBILE, and connect to internet.
- assertEquals(TYPE_MOBILE, ni[TYPE_MOBILE].getType());
- assertEquals(TYPE_WIFI, ni[TYPE_WIFI].getType());
-
- // don't know the return value
- ni[TYPE_MOBILE].getSubtype();
- ni[TYPE_WIFI].getSubtype();
-
- assertEquals(MOBILE_TYPE_NAME, ni[TYPE_MOBILE].getTypeName());
- assertEquals(WIFI_TYPE_NAME, ni[TYPE_WIFI].getTypeName());
-
- // don't know the return value
- ni[TYPE_MOBILE].getSubtypeName();
- ni[TYPE_WIFI].getSubtypeName();
-
- if(ni[TYPE_MOBILE].isConnectedOrConnecting()) {
- assertTrue(ni[TYPE_MOBILE].isAvailable());
- assertTrue(ni[TYPE_MOBILE].isConnected());
- assertEquals(State.CONNECTED, ni[TYPE_MOBILE].getState());
- assertEquals(DetailedState.CONNECTED, ni[TYPE_MOBILE].getDetailedState());
- ni[TYPE_MOBILE].getReason();
- ni[TYPE_MOBILE].getExtraInfo();
+ for (NetworkInfo netInfo: ni) {
+ switch (netInfo.getType()) {
+ case TYPE_MOBILE:
+ assertNetworkInfo(netInfo, MOBILE_TYPE_NAME);
+ break;
+ case TYPE_WIFI:
+ assertNetworkInfo(netInfo, WIFI_TYPE_NAME);
+ break;
+ // TODO: Add BLUETOOTH_TETHER testing
+ default:
+ break;
+ }
}
+ }
- if(ni[TYPE_WIFI].isConnectedOrConnecting()) {
- assertTrue(ni[TYPE_WIFI].isAvailable());
- assertTrue(ni[TYPE_WIFI].isConnected());
- assertEquals(State.CONNECTED, ni[TYPE_WIFI].getState());
- assertEquals(DetailedState.CONNECTED, ni[TYPE_WIFI].getDetailedState());
- ni[TYPE_WIFI].getReason();
- ni[TYPE_WIFI].getExtraInfo();
+ private void assertNetworkInfo(NetworkInfo netInfo, String expectedTypeName) {
+ assertEquals(expectedTypeName, netInfo.getTypeName());
+ if(netInfo.isConnectedOrConnecting()) {
+ assertTrue(netInfo.isAvailable());
+ if (State.CONNECTED == netInfo.getState()) {
+ assertTrue(netInfo.isConnected());
+ }
+ assertTrue(State.CONNECTING == netInfo.getState()
+ || State.CONNECTED == netInfo.getState());
+ assertTrue(DetailedState.SCANNING == netInfo.getDetailedState()
+ || DetailedState.CONNECTING == netInfo.getDetailedState()
+ || DetailedState.AUTHENTICATING == netInfo.getDetailedState()
+ || DetailedState.CONNECTED == netInfo.getDetailedState());
}
-
- assertFalse(ni[TYPE_MOBILE].isRoaming());
- assertFalse(ni[TYPE_WIFI].isRoaming());
-
- assertNotNull(ni[TYPE_MOBILE].toString());
- assertNotNull(ni[TYPE_WIFI].toString());
+ assertNotNull(netInfo.toString());
}
}