InstrumentationTestCase类的使用

本文参考:http://blog.csdn.net/xianming01/article/details/7893391

InstrumentationTestCase类封装了Junit类,是Android的测试类。下面以一个实例讲解它的使用:

该实例是用来测试Webkit的加载的,测试的是WebView的loadUrl()方法:


WebkitLoad.java

package com.android.webkit;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;

public class WebkitLoad extends Activity {
    /** Called when the activity is first created. */
	private Button btn1, btn2;
	private WebView webview;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btn1 = (Button)findViewById(R.id.btn1);
        btn2 = (Button)findViewById(R.id.btn2);
        webview = (WebView)findViewById(R.id.webView1);
        
        btn1.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				webview.loadUrl("file:///android_asset/3.htm");
			}});
        
        btn2.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				webview.loadUrl("file:///android_asset/1.html");
			}});
    }
}


main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
<Button 
	android:text="Button" 
	android:id="@+id/btn1" 
	android:layout_width="wrap_content" 
	android:layout_height="wrap_content">
</Button>
<Button 
	android:text="Button" 
	android:id="@+id/btn2" 
	android:layout_width="wrap_content" 
	android:layout_height="wrap_content">
</Button>
<WebView 
	android:id="@+id/webView1" 
	android:layout_width="match_parent" 
	android:layout_height="match_parent">
</WebView>
</LinearLayout>

以上是被测试对象的代码,下面是测试的代码:

(1)改写AndroidManifest.xml文件:

主要是添加“<uses-library android:name="android.test.runner" />” 和 “<instrumentation android:targetPackage="com.android.webkit" android:name="android.test.InstrumentationTestRunner" />”

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.android.webkit"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="1" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
    <uses-library android:name="android.test.runner" />  
    
        <activity android:name=".WebkitLoad"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
    <instrumentation android:targetPackage="com.android.webkit"  
        android:name="android.test.InstrumentationTestRunner" />  
    
</manifest>


(2)在src目录下添加目录com.anroid.webkit.test,

WebkitLoadTest.java

package com.android.webkit.test;

import com.android.webkit.R;
import com.android.webkit.WebkitLoad;

import android.content.Intent;
import android.os.SystemClock;
import android.test.InstrumentationTestCase;
import android.widget.Button;

public class WebkitLoadTest extends InstrumentationTestCase {

	private Button btn1, btn2;
	private WebkitLoad webkitload;
	@Override
	protected void tearDown() throws Exception {
		// TODO Auto-generated method stub
		webkitload.finish();
		super.tearDown();
	}

	@Override
	protected void setUp() throws Exception {
		// TODO Auto-generated method stub
		super.setUp();
		Intent intent = new Intent();
		intent.setClassName("com.android.webkit", WebkitLoad.class.getName());
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		webkitload = (WebkitLoad)getInstrumentation().startActivitySync(intent);
		btn1 = (Button)webkitload.findViewById(R.id.btn1);
		btn2 = (Button)webkitload.findViewById(R.id.btn2);
	}
	
	public void testActivity() throws Exception{
		SystemClock.sleep(2000);
		for(int i = 0; i < 10; i++){
			getInstrumentation().runOnMainSync(new PerformClick(btn1));
			SystemClock.sleep(2000);
			getInstrumentation().runOnMainSync(new PerformClick(btn2));
			SystemClock.sleep(2000);
		}
	}

}

PerformClick.java

package com.android.webkit.test;

import android.widget.Button;

public class PerformClick implements Runnable {
	Button btn;
	public PerformClick(Button button){
		btn = button;
	}
	@Override
	public void run() {
		// TODO Auto-generated method stub
		btn.performClick();
	}

}





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值