【转】使用Android Instrument 自动测试 WIFI_SERVICE

http://blog.csdn.net/stevenliyong/archive/2010/05/06/5563759.aspx

 

使用Android Instrument 自动测试 WIFI_SERVICE


Porting 了新的Wifi hardware_legancy,

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

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

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

 测试代码

AllTest.java

view plaincopy to clipboardprint?
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;  
    }  
 

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

view plaincopy to clipboardprint?
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);  
            }     
        }  
 

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

view plaincopy to clipboardprint?
<?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>  
<?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

view plaincopy to clipboardprint?
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) 
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

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/stevenliyong/archive/2010/05/06/5563759.aspx

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值