1 /* 2 * Copyright (C) 2009 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.net.cts; 18 19 20 import android.content.Context; 21 import android.net.ConnectivityManager; 22 import android.net.NetworkInfo; 23 import android.net.NetworkInfo.DetailedState; 24 import android.net.NetworkInfo.State; 25 import android.test.AndroidTestCase; 26 27 public class NetworkInfoTest extends AndroidTestCase { 28 29 public static final int TYPE_MOBILE = ConnectivityManager.TYPE_MOBILE; 30 public static final int TYPE_WIFI = ConnectivityManager.TYPE_WIFI; 31 public static final String MOBILE_TYPE_NAME = "mobile"; 32 public static final String WIFI_TYPE_NAME = "WIFI"; 33 34 public void testAccessNetworkInfoProperties() { 35 ConnectivityManager cm = (ConnectivityManager) getContext().getSystemService( 36 Context.CONNECTIVITY_SERVICE); 37 NetworkInfo[] ni = cm.getAllNetworkInfo(); 38 assertTrue(ni.length >= 1); 39 40 for (NetworkInfo netInfo: ni) { 41 switch (netInfo.getType()) { 42 case TYPE_MOBILE: 43 assertNetworkInfo(netInfo, MOBILE_TYPE_NAME); 44 break; 45 case TYPE_WIFI: 46 assertNetworkInfo(netInfo, WIFI_TYPE_NAME); 47 break; 48 // TODO: Add BLUETOOTH_TETHER testing 49 default: 50 break; 51 } 52 } 53 } 54 55 private void assertNetworkInfo(NetworkInfo netInfo, String expectedTypeName) { 56 assertEquals(expectedTypeName.compareToIgnoreCase(netInfo.getTypeName()), 0); 57 if(netInfo.isConnectedOrConnecting()) { 58 assertTrue(netInfo.isAvailable()); 59 if (State.CONNECTED == netInfo.getState()) { 60 assertTrue(netInfo.isConnected()); 61 } 62 assertTrue(State.CONNECTING == netInfo.getState() 63 || State.CONNECTED == netInfo.getState()); 64 assertTrue(DetailedState.SCANNING == netInfo.getDetailedState() 65 || DetailedState.CONNECTING == netInfo.getDetailedState() 66 || DetailedState.AUTHENTICATING == netInfo.getDetailedState() 67 || DetailedState.CONNECTED == netInfo.getDetailedState()); 68 } 69 assertNotNull(netInfo.toString()); 70 } 71 } 72