ConnectivityManagerMobileTest.java revision fc2dbd04005c32360eead803e29df3cc62209cd8
1/* 2 * Copyright (C) 2010 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 18 19package com.android.connectivitymanagertest.functional; 20 21import com.android.connectivitymanagertest.ConnectivityManagerTestActivity; 22 23import android.content.Intent; 24import android.content.Context; 25import android.app.Instrumentation; 26import android.os.Handler; 27import android.os.Message; 28import android.net.ConnectivityManager; 29import android.net.NetworkInfo; 30import android.net.NetworkInfo.State; 31import android.net.NetworkInfo.DetailedState; 32 33import android.test.suitebuilder.annotation.LargeTest; 34import android.test.ActivityInstrumentationTestCase2; 35import com.android.connectivitymanagertest.ConnectivityManagerTestRunner; 36import com.android.connectivitymanagertest.NetworkState; 37import android.util.Log; 38import junit.framework.*; 39 40public class ConnectivityManagerMobileTest 41 extends ActivityInstrumentationTestCase2<ConnectivityManagerTestActivity> { 42 43 private static final String LOG_TAG = "ConnectivityManagerMobileTest"; 44 private static final String PKG_NAME = "com.android.connectivitymanagertest"; 45 private static final long WIFI_CONNECTION_TIMEOUT = 30 * 1000; 46 private static final long WIFI_NOTIFICATION_TIMEOUT = 10 * 1000; 47 private String TEST_ACCESS_POINT; 48 private ConnectivityManagerTestActivity cmActivity; 49 50 public ConnectivityManagerMobileTest() { 51 super(PKG_NAME, ConnectivityManagerTestActivity.class); 52 } 53 54 @Override 55 public void setUp() throws Exception { 56 super.setUp(); 57 cmActivity = getActivity(); 58 ConnectivityManagerTestRunner mRunner = 59 (ConnectivityManagerTestRunner)getInstrumentation(); 60 TEST_ACCESS_POINT = mRunner.TEST_SSID; 61 // Each test case will start with cellular connection 62 verifyCellularConnection(); 63 } 64 65 @Override 66 public void tearDown() throws Exception { 67 // clear Wifi after each test case 68 cmActivity.clearWifi(); 69 cmActivity.finish(); 70 Log.v(LOG_TAG, "tear down ConnectivityManager test activity"); 71 super.tearDown(); 72 } 73 74 // help function to verify 3G connection 75 public void verifyCellularConnection() { 76 NetworkInfo extraNetInfo = cmActivity.mNetworkInfo; 77 assertEquals("network type is not MOBILE", ConnectivityManager.TYPE_MOBILE, 78 extraNetInfo.getType()); 79 assertTrue("not connected to cellular network", extraNetInfo.isConnected()); 80 assertTrue("no data connection", cmActivity.mState.equals(State.CONNECTED)); 81 } 82 83 // Test case 1: Test enabling Wifi without associating with any AP 84 @LargeTest 85 public void test3GToWifiNotification() { 86 // As Wifi stays in DISCONNECTED, the connectivity manager will not broadcast 87 // any network connectivity event for Wifi 88 NetworkInfo networkInfo = cmActivity.mCM.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); 89 cmActivity.setStateTransitionCriteria(ConnectivityManager.TYPE_MOBILE, networkInfo.getState(), 90 NetworkState.DO_NOTHING, State.CONNECTED); 91 networkInfo = cmActivity.mCM.getNetworkInfo(ConnectivityManager.TYPE_WIFI); 92 cmActivity.setStateTransitionCriteria(ConnectivityManager.TYPE_WIFI, networkInfo.getState(), 93 NetworkState.DO_NOTHING, State.DISCONNECTED); 94 // Eanble Wifi 95 cmActivity.enableWifi(); 96 try { 97 Thread.sleep(WIFI_NOTIFICATION_TIMEOUT); 98 } catch (Exception e) { 99 Log.v(LOG_TAG, "exception: " + e.toString()); 100 } 101 102 // validate state and broadcast 103 if (!cmActivity.validateNetworkStates(ConnectivityManager.TYPE_WIFI)) { 104 Log.v(LOG_TAG, "the state for WIFI is changed"); 105 Log.v(LOG_TAG, "reason: " + 106 cmActivity.getTransitionFailureReason(ConnectivityManager.TYPE_WIFI)); 107 assertTrue("state validation fail", false); 108 } 109 if (!cmActivity.validateNetworkStates(ConnectivityManager.TYPE_MOBILE)) { 110 Log.v(LOG_TAG, "the state for MOBILE is changed"); 111 Log.v(LOG_TAG, "reason: " + 112 cmActivity.getTransitionFailureReason(ConnectivityManager.TYPE_MOBILE)); 113 assertTrue(false); 114 } 115 // Verify that the device is still connected to MOBILE 116 verifyCellularConnection(); 117 } 118 119 // Test case 2: test connection to a given AP 120 @LargeTest 121 public void testConnectToWifi() { 122 assertNotNull("SSID is null", TEST_ACCESS_POINT); 123 //Prepare for connectivity verification 124 NetworkInfo networkInfo = cmActivity.mCM.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); 125 cmActivity.setStateTransitionCriteria(ConnectivityManager.TYPE_MOBILE, networkInfo.getState(), 126 NetworkState.TO_DISCONNECTION, State.DISCONNECTED); 127 networkInfo = cmActivity.mCM.getNetworkInfo(ConnectivityManager.TYPE_WIFI); 128 cmActivity.setStateTransitionCriteria(ConnectivityManager.TYPE_WIFI, networkInfo.getState(), 129 NetworkState.TO_CONNECTION, State.CONNECTED); 130 131 // Enable Wifi and connect to a test access point 132 assertTrue("failed to connect to " + TEST_ACCESS_POINT, 133 cmActivity.connectToWifi(TEST_ACCESS_POINT)); 134 try { 135 Thread.sleep(WIFI_CONNECTION_TIMEOUT); 136 } catch (Exception e) { 137 Log.v(LOG_TAG, "exception: " + e.toString()); 138 } 139 140 // validate states 141 if (!cmActivity.validateNetworkStates(ConnectivityManager.TYPE_WIFI)) { 142 Log.v(LOG_TAG, "Wifi state transition validation failed."); 143 Log.v(LOG_TAG, "reason: " + 144 cmActivity.getTransitionFailureReason(ConnectivityManager.TYPE_WIFI)); 145 assertTrue(false); 146 } 147 if (!cmActivity.validateNetworkStates(ConnectivityManager.TYPE_MOBILE)) { 148 Log.v(LOG_TAG, "Mobile state transition validation failed."); 149 Log.v(LOG_TAG, "reason: " + 150 cmActivity.getTransitionFailureReason(ConnectivityManager.TYPE_MOBILE)); 151 assertTrue(false); 152 } 153 } 154} 155