Android Instrument自动测试:Wifi_service

 

Porting 了新的Wifi hardware_legancy,

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

 

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

 

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

 

 测试代码

AllTest.java

[java] view plain copy print ?
  1. package com.example.wifitoggle;  
  2.   
  3. import junit.framework.Test;  
  4. import junit.framework.TestSuite;  
  5.   
  6. public class AllTests {  
  7.   
  8.     public static Test suite() {  
  9.         TestSuite suite = new TestSuite(  
  10.                 "Test for com.android.settings.testsuite");  
  11.         //$JUnit-BEGIN$   
  12.   
  13.         //$JUnit-END$   
  14.         return suite;  
  15.     }  
  16.   
  17. }  
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

[java] view plain copy print ?
  1. package com.example.wifitoggle.test;  
  2.   
  3. //import Forwarding;   
  4. import junit.framework.Assert;  
  5. import junit.framework.TestCase;  
  6. import android.app.Activity;  
  7. import android.content.Intent;  
  8. import android.net.wifi.WifiManager;  
  9. import android.util.Log;  
  10. import android.os.Bundle;  
  11. import android.content.Context;  
  12. import android.test.AndroidTestCase;  
  13. public class WifiSettingTest extends AndroidTestCase {  
  14.   
  15.     private WifiManager mWifiManager = null;  
  16.     private final int TEST_COUNT = 10;  
  17.     private final int INTERVAL_DELAY_MS = 2000;  
  18.       
  19.     protected void setUp() throws Exception {  
  20.         super.setUp();  
  21.             Context serviceManager = getContext();  
  22.                 mWifiManager = (WifiManager) serviceManager.getSystemService(Context.WIFI_SERVICE);       
  23.     }  
  24.       
  25.     private void sleep(int ms) {  
  26.         try {  
  27.             Thread.sleep(ms);  
  28.         } catch (InterruptedException e) {  
  29.             // TODO Auto-generated catch block   
  30.             e.printStackTrace();  
  31.         }  
  32.         }  
  33.       
  34.     public void testTurnOnOff() {  
  35.         boolean enable = false;  
  36.         boolean result = false;  
  37.            
  38.             for(int i  = 0; i < TEST_COUNT; i++) {  
  39.                 final int WAIT_TIMEOUT_S = 30;  
  40.                 int wifiState = mWifiManager.getWifiState();  
  41.                 if(wifiState == WifiManager.WIFI_STATE_ENABLED) {  
  42.                     result = false;  
  43.                 Log.w("WIFI TEST""Turning wifi off...");  
  44.                     mWifiManager.setWifiEnabled(false);  
  45.                     for(int j = 0; j < WAIT_TIMEOUT_S; j ++) {  
  46.                         sleep(1000);  
  47.                             wifiState = mWifiManager.getWifiState();  
  48.                             if(wifiState == WifiManager.WIFI_STATE_DISABLED) {  
  49.                                 result = true;  
  50.                         Log.w("WIFI TEST""Turning wifi off success.");  
  51.                                 break;  
  52.                             } else if (wifiState == WifiManager.WIFI_STATE_UNKNOWN) {  
  53.                         Log.w("WIFI TEST""Turning wifi off failed.");  
  54.                                 break;  
  55.                             }  
  56.                         }  
  57.                 } else if (wifiState == WifiManager.WIFI_STATE_DISABLED) {  
  58.                     result = false;  
  59.                 Log.w("WIFI TEST""Turning wifi on...");  
  60.                     mWifiManager.setWifiEnabled(true);  
  61.                     for(int j = 0; j < WAIT_TIMEOUT_S; j ++) {  
  62.                         sleep(1000);  
  63.                             wifiState = mWifiManager.getWifiState();  
  64.                             if(wifiState == WifiManager.WIFI_STATE_ENABLED) {  
  65.                                 result = true;  
  66.                         Log.w("WIFI TEST""Turning wifi on success.");  
  67.                                 break;  
  68.                             } else if (wifiState == WifiManager.WIFI_STATE_UNKNOWN) {  
  69.                         Log.w("WIFI TEST""Turning wifi on failed.");  
  70.                                 break;  
  71.                             }  
  72.                         }  
  73.                 }  
  74.                 Assert.assertTrue(result);  
  75.                   
  76.                 sleep(INTERVAL_DELAY_MS);  
  77.             }     
  78.         }  
  79.   
  80. }  
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

[xhtml] view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.       package="com.example.wifitoggle.tests"  
  4.       android:versionCode="1"  
  5.       android:versionName="1.0"  
  6. android:sharedUserId="android.uid.system">  
  7.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  8.     <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />  
  9.     <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />  
  10.       
  11.     <uses-library android:name="android.test.runner" />  
  12.     </application>  
  13.     <instrumentation android:targetPackage="com.example.wifitoggle.tests" android:name="android.test.InstrumentationTestRunner" />  
  14. </manifest>   
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.wifitoggle.tests" android:versionCode="1" android:versionName="1.0" android:sharedUserId="android.uid.system"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> <uses-library android:name="android.test.runner" /> </application> <instrumentation android:targetPackage="com.example.wifitoggle.tests" android:name="android.test.InstrumentationTestRunner" /> </manifest>

 

 

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

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

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

Android.mk

[xhtml] view plain copy print ?
  1. LOCAL_PATH:= $(call my-dir)  
  2. include $(CLEAR_VARS)  
  3.   
  4. # We only want this apk build for tests.  
  5. LOCAL_MODULE_TAGS :tests  
  6. # Also link against our own custom library.  
  7. LOCAL_JAVA_LIBRARIES :android.test.runner  
  8.   
  9. #LOCAL_JAVA_LIBRARIES += com.android.settings  
  10.   
  11. # Include all test java files.  
  12. LOCAL_SRC_FILES := $(call all-java-files-under, src)  
  13.   
  14. LOCAL_PACKAGE_NAME :wifitoggletest  
  15.   
  16. #LOCAL_INSTRUMENTATION_FOR :wifitoggle  
  17.   
  18. LOCAL_CERTIFICATE :platform  
  19.   
  20. include $(BUILD_PACKAGE)  
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

 

 

 

你可能感兴趣的:(java,android,service,测试,include,delay)