使用Android Instrument 自动测试 WIFI_SERVICE

 

Porting 了新的Wifi hardware_legancy,

这个时候需要对对  Android Wifi On/Off 作压力测试

 

发现可以使用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

 

 

2. Eclipse 环境下的sdk版本 可能与实际 Android src 不兼容。 所以测试 WIFI SERVICE 这类framework 层代码,

需要将 测试程序放到Android 环境下编译,而不是在Eclipse 下编译。

实现这一步,只要简单的写一个Android.mk 然后将测试程序 目录拷贝到 packages/app/ 下, 再运行 mm 编译即可.

Android.mk

LOCAL_PATH:= $(call my-dir) include $(CLEAR_VARS) # We only want this apk build for tests. LOCAL_MODULE_TAGS := tests # Also link against our own custom library. LOCAL_JAVA_LIBRARIES := android.test.runner #LOCAL_JAVA_LIBRARIES += com.android.settings # Include all test java files. LOCAL_SRC_FILES := $(call all-java-files-under, src) LOCAL_PACKAGE_NAME := wifitoggletest #LOCAL_INSTRUMENTATION_FOR := wifitoggle LOCAL_CERTIFICATE := platform include $(BUILD_PACKAGE)

 

# cd $ANDROID_PATH

# source ./build/envsetup.sh

# cd packages/app/WifiToggleTest
# mm

 

 

3.

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

 

 

 

你可能感兴趣的:(Android,技术相关,android,测试,service,eclipse,include,delay)