关于Android使用Instrumentation做功能测试的时候遇到的一个问题
最近在看测试方面的东西,看到官网上有一个使用Instrumentation做功能测试的例子,自己敲了敲,但是在自己的手机上就是测不过。
经过调试,我发现是我手机上的输入法把输入事件拦截了,需要多输入一些空格给输入法,让它来把字符填入EditText中才行。
这件事再次印证了android中事件传播的机制,keyevent先传递给当前上下文,然后分发给窗体实例,然后是输入法,接着发给父VIEW,一层层的发往焦点控件
touchevent则是反过来的
下面是我的测试代码,有三个类,Lesson3,Lesson3start,lesson3test
Lesson3.java
package com.example.u3.testpractice; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.inputmethod.InputMethodManager; import android.widget.Button; import android.widget.EditText; /** * Created by U3 on 2015/4/11. */ public class Lesson3 extends Activity { private final String exString = "hi, unit test"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.lesson3layout); Button mButton = (Button)findViewById(R.id.bt_lesson3); Button mButton2 = (Button)findViewById(R.id.bt_lesson3_2); final EditText mEditText = (EditText)findViewById(R.id.et_edittext); mButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent mIntent = new Intent(Lesson3.this,Lesson3start.class); mIntent.putExtra("ExString",exString); startActivity(mIntent); finish(); } }); mButton2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent mIntent = new Intent(Lesson3.this,Lesson3start.class); final String msg = mEditText.getText().toString(); mIntent.putExtra("ExString",msg); startActivity(mIntent); finish(); } }); } } //该类代码很简单,不做过多说明
lesson3start.java
public class Lesson3start extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.lesson3layout); TextView mTextView = (TextView)findViewById(R.id.tv_lesson3); String exString = getIntent().getStringExtra("ExString"); mTextView.setText(exString); } }
下面是最关键的测试类代码,lesson3test2.java
public class Lesson3Test2 extends ActivityInstrumentationTestCase2{ private Lesson3 lesson3test; private Lesson3start lesson3start; private static final String TEXT_MSG1 = "hello receiver"; private static final String TEXT_MSG2 = "helloreceiver"; public Lesson3Test2() { super(Lesson3.class); } @Override protected void setUp() throws Exception { super.setUp(); setActivityInitialTouchMode(true);//设置触摸模式 lesson3test = getActivity();//得到activity } public void testPrecondition() { assertNotNull("Activity is not run", lesson3test); } public void testSendandRecive_Text() { final Button mButton = (Button) lesson3test.findViewById(R.id.bt_lesson3_2); final EditText mEditText = (EditText) lesson3test.findViewById(R.id.et_edittext); Instrumentation.ActivityMonitor reciveMonitor = getInstrumentation().addMonitor(Lesson3start.class.getName(), null, false);//设置monitor检测activity getInstrumentation().runOnMainSync(new Runnable() { @Override public void run() { mEditText.requestFocus();//主线程获得焦点 } }); getInstrumentation().waitForIdleSync();//等待主线程完成操作 getInstrumentation().sendStringSync(TEXT_MSG1+" ");//输入String getInstrumentation().waitForIdleSync();//等待 TouchUtils.clickView(this,mButton);//模拟点击按钮 lesson3start = (Lesson3start)reciveMonitor.waitForActivityWithTimeout(1000); assertNotNull("not start activity",lesson3start); assertEquals("moniter not hit",1,reciveMonitor.getHits()); assertEquals("wrong class to start",Lesson3start.class,lesson3start.getClass()); final TextView testText = (TextView)lesson3start.findViewById(R.id.tv_lesson3); assertEquals("wrong text to send",TEXT_MSG2,testText.getText());//测试结果 } }