关于Android使用Instrumentation做功能测试的时候遇到的一个问题
2015-04-12 13:57
881 查看
最近在看测试方面的东西,看到官网上有一个使用Instrumentation做功能测试的例子,自己敲了敲,但是在自己的手机上就是测不过。
经过调试,我发现是我手机上的输入法把输入事件拦截了,需要多输入一些空格给输入法,让它来把字符填入EditText中才行。
这件事再次印证了android中事件传播的机制,keyevent先传递给当前上下文,然后分发给窗体实例,然后是输入法,接着发给父VIEW,一层层的发往焦点控件
touchevent则是反过来的
下面是我的测试代码,有三个类,Lesson3,Lesson3start,lesson3test
Lesson3.java
lesson3start.java
下面是最关键的测试类代码,lesson3test2.java
经过调试,我发现是我手机上的输入法把输入事件拦截了,需要多输入一些空格给输入法,让它来把字符填入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<Lesson3> { 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());//测试结果 } }
相关文章推荐
- 关于使用facebook的Android调试工具Stetho时遇到的问题
- 关于android开发在布局中使用sharp时遇到的问题
- 关于NavigationView在使用中遇到的一个小问题
- android theme主题使用遇到的一个小问题
- 关于android webview 图片使用同一个src导致只加载第一张的问题
- android使用AIDL进程间通信遇到的一个小问题
- 在使用mybaitis传参数的时候,仅传入一个类型为String的参数所遇到的问题
- 关于Android7.0系统使用webview遇到的一个问题(二级跳转后界面空白)
- 使用session时候,遇到的一个问题(请求解决方法)
- 关于android Listview 使用时遇到的几个问题
- 在使用JES8的时候遇到的一个问题
- 关于spring restful使用中遇到的一个性能问题
- 关于一个android工程同时使用多个工程库,在编译时出现的问题
- 关于使用Hibernate注解的时候遇到的问题
- 关于您提到使用WebServices时候遇到“基础连接以关闭”的问题
- 关于2.3系统使用FrameLayout遇到的一个问题
- 关于Android开发中使用System.exit(0)的问题(如写返回键响应时用到双击退出函数的时候)
- 使用spymemcached spring整合的时候遇到的一个配置问题
- 使用Android Killer时遇到的一个奇怪的问题
- Android 关于使用阿里Freeline遇到的问题