【转】使用Android Instrument 自动测试 WIFI_SERVICE
来源:互联网 发布:ubuntu移动文件夹 编辑:程序博客网 时间:2024/05/16 16:13
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
- 【转】使用Android Instrument 自动测试 WIFI_SERVICE
- 使用Android Instrument 自动测试 WIFI_SERVICE
- 使用Android Instrument 自动测试 WIFI_SERVICE
- Android Instrument自动测试:Wifi_service
- Android Wifi:使用Android Instrument 自动测试 WIFI_SERVICE
- Android Instrument 自动测试 WIFI on off
- Android Instrument测试命令使用小记
- Android WiFi管理(WIFI_SERVICE)
- Android 经验: 不能用 instrument 替代的 CTS 测试
- 调试教程 – 使用UIRecorder Instrument将测试自动化
- Android 使用monkey自动测试
- Android自动测试:monkey使用
- Android Studio中使用 instrument junit4 环境配置问题
- Instrument的使用
- Instrument使用总结
- Android 中使用JUnit进行自动测试
- Android 自动测试工具Monkey的使用
- Xcode自带的instrument中的Automation实现自动化测试简单使用
- SQL知识大杂烩
- Ubuntu10.04 +JDK5
- 汇编指令
- 【转】Android Wifi Driver Porting
- 重温C#设计模式---1
- 【转】使用Android Instrument 自动测试 WIFI_SERVICE
- C++宏整理
- 试用Windows Embedded 6.0 R3开发手记
- 策略模式VS桥梁模式
- Chapter 8 Polymorphism
- 记以往的尴尬事
- System.TypeLoadException: Could not load type 'System.DateTimeOffset' from assembly 'mscorlib, Version=2.0.0.0, Culture=neutral,
- 【转】Android 加速度传感器 (G-Sensor)
- 目前,网络上面,最暴利的行业,有几点,1、男人好色类 2、女人爱美类 3、老人要健康 4、小孩要智慧 5、企业要赚钱。