以下为源码,可参考:
MainActivity代码
package com.example.jc.instrumentsample;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity implements View.OnClickListener {
private Button btn;
private TextView tv;
private EditText num1;
private EditText num2;
private EditText rs;
private Button mult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button_change);
btn.setOnClickListener(this);
tv = (TextView) findViewById(R.id.textView_hello);
num1 =(EditText)findViewById(R.id.editText_num1);
num2 =(EditText)findViewById(R.id.editText_num2);
rs = (EditText)findViewById(R.id.editText_rs);
mult = (Button)findViewById(R.id.button_multip);
mult.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.button_change:
changeTextView();
break;
case R.id.button_multip:
computeMultip();
default:
break;
}
}
//乘法
private void computeMultip() {
double n1 = Double.parseDouble(num1.getText().toString());
double n2 = Double.parseDouble(num2.getText().toString());
double res = n1*n2;
rs.setText(String.valueOf(res));
}
//    textView变换
private void changeTextView() {tv.setText("this is my first android");}
//    加法
public int add(int a, int b) {return a + b;}
//减法
public int sub(int a, int b) {return b - a;}
}
sampleTest
package com.example.jc.instrumentsample;
import android.content.Intent;
import android.os.SystemClock;
import android.test.InstrumentationTestCase;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
/**
* Created by jc on 15/9/1.
*/
public class sampleTest extends InstrumentationTestCase {
private MainActivity sample = null;
private Button btn = null;
private TextView textView = null;
private EditText num1 = null;
private EditText num2 = null;
private EditText rs = null;
private Button mult = null;
@Override
protected void setUp() throws Exception {
try {
Log.d("1111","111");
super.setUp();
}catch (Exception e){
e.printStackTrace();
}
Intent intent = new Intent();
intent.setClassName("com.example.jc.instrumentsample", MainActivity.class.getName());
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
sample = (MainActivity)getInstrumentation().startActivitySync(intent);
btn = (Button)sample.findViewById(R.id.button_change);
textView = (TextView)sample.findViewById(R.id.textView_hello);
num1 = (EditText)sample.findViewById(R.id.editText_num1);
num2 = (EditText)sample.findViewById(R.id.editText_num2);
rs = (EditText)sample.findViewById(R.id.editText_rs);
mult = (Button)sample.findViewById(R.id.button_multip);
}
@Override
protected void tearDown() throws Exception {
sample.finish();
try {
super.tearDown();
}catch (Exception e){
e.printStackTrace();
}
}
public  void testMultip(){
SystemClock.sleep(1000);
getInstrumentation().runOnMainSync(new PerFromClickMultip(mult));
SystemClock.sleep(1000);
assertEquals("210.0", rs.getText().toString());
}
private class PerFromClickMultip implements Runnable {
private Button m = null;
public PerFromClickMultip(Button mult) {
m = mult;
}
@Override
public void run() {
num1.setText(String.valueOf(21));
num2.setText(String.valueOf(10));
double n1 = Double.parseDouble(num1.getText().toString());
double n2 = Double.parseDouble(num2.getText().toString());
double n3 = n1* n2;
rs.setText(String.valueOf(n3));
}
}
public void testActivity(){
Log.d("testActivity","testActivity");
SystemClock.sleep(1500);
getInstrumentation().runOnMainSync(new PerFromClick(btn));
SystemClock.sleep(1500);
assertEquals("hello andriod",textView.getText().toString());
}
private class PerFromClick implements Runnable {
Button btn2;
public PerFromClick(Button button){
btn2 = button;
}
@Override
public void run() {
btn2.performClick();
}
}
public  void testAdd(){
String tag = "testAdd";
Log.d("testActivity","testAdd");
int test = sample.add(1,1);
assertEquals(3,test);
}
public  void testSub(){
String tag = "testSub";
Log.d("testActivity","testAdd");
int test = sample.sub(2,1);
assertEquals(1,test);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.jc.instrumentsample">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<uses-library android:name="android.test.runner" />
<activity
android:name=".MainActivity"
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:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.example.jc.instrumentsample" />
</manifest>