Android Instrument 自动测试 WIFI on off

文章来源:http://blog.csdn.net/stevenliyong/article/details/5563759

 

发现可以使用Android Instrument 和Android Test Project 来做测试。

1. 在Eclipse 里新建 一个 Android Test Project.

测试代码

AllTest.java

 

package com.example.wifitoggle; 

import junit.framework.Test; 
import junit.framework.TestSuite; 

public class AllTests { 

public static Test suite() { 
TestSuite suite = new TestSuite( 
"Test for com.android.settings.testsuite"); 
//$JUnit-BEGIN$ 

//$JUnit-END$ 
return suite; 
} 

} 


WifiSettingTest.java

package com.example.wifitoggle.test;

//import Forwarding;
import junit.framework.Assert;
import junit.framework.TestCase;
import android.app.Activity;
import android.content.Intent;
import android.net.wifi.WifiManager;
import android.util.Log;
import android.os.Bundle;
import android.content.Context;
import android.test.AndroidTestCase;
public class WifiSettingTest extends AndroidTestCase {

	private WifiManager mWifiManager = null;
	private final int TEST_COUNT = 10;
	private final int INTERVAL_DELAY_MS = 2000;
	
	protected void setUp() throws Exception {
		super.setUp();
	     	Context serviceManager = getContext();
             	mWifiManager = (WifiManager) serviceManager.getSystemService(Context.WIFI_SERVICE);     
	}
	
	private void sleep(int ms) {
		try {
			Thread.sleep(ms);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        }
	
	public void testTurnOnOff() {
		boolean enable = false;
		boolean result = false;
    	 
    	 	for(int i  = 0; i < TEST_COUNT; i++) {
	    		final int WAIT_TIMEOUT_S = 30;
	    	 	int wifiState = mWifiManager.getWifiState();
	         	if(wifiState == WifiManager.WIFI_STATE_ENABLED) {
	        		result = false;
				Log.w("WIFI TEST", "Turning wifi off...");
	        	 	mWifiManager.setWifiEnabled(false);
	        	 	for(int j = 0; j < WAIT_TIMEOUT_S; j ++) {
	        			sleep(1000);
	        	     		wifiState = mWifiManager.getWifiState();
	        	     		if(wifiState == WifiManager.WIFI_STATE_DISABLED) {
	        	    	 		result = true;
						Log.w("WIFI TEST", "Turning wifi off success.");
	        	     	 		break;
	        	     		} else if (wifiState == WifiManager.WIFI_STATE_UNKNOWN) {
						Log.w("WIFI TEST", "Turning wifi off failed.");
	        	    	 		break;
	        	     		}
	             		}
	         	} else if (wifiState == WifiManager.WIFI_STATE_DISABLED) {
	        	 	result = false;
				Log.w("WIFI TEST", "Turning wifi on...");
	        	 	mWifiManager.setWifiEnabled(true);
	        	 	for(int j = 0; j < WAIT_TIMEOUT_S; j ++) {
	        			sleep(1000);
	        	     		wifiState = mWifiManager.getWifiState();
	        	     		if(wifiState == WifiManager.WIFI_STATE_ENABLED) {
	        	    	 		result = true;
						Log.w("WIFI TEST", "Turning wifi on success.");
	        	     	 		break;
	        	     		} else if (wifiState == WifiManager.WIFI_STATE_UNKNOWN) {
						Log.w("WIFI TEST", "Turning wifi on failed.");
	        	    	 		break;
	        	     		}
	             		}
	         	}
	        	Assert.assertTrue(result);
	        	
	        	sleep(INTERVAL_DELAY_MS);
    	 	}	
     	}

}


AndroidManifest.xml



    
    
    
    
    
    
    

 

wifitoggletest.apk 生成后就可以通过instrument 和 adb协议来测试 WIFI_SERVICE 功能了。
# croot
# adb uninstall com.example.wifitoggle.tests (It is not necessary for the first time running.)
# adb install ./out/target/product/eagle/data/app/wifitoggletest.apk
# adb shell am instrument -w com.example.wifitoggle.tests/android.test.InstrumentationTestRunner

你可能感兴趣的:(Test)