除了高层框架如Robotium的solo,我们也可以直接调用SDK底层的提供的Instrumentation的API来实现如前几篇文章描述的创建一个note的功能。总所周知之Robotium就是基于Instrumentation的框架高层抽象实现的一个项目,所以对比《Robotium创建一个Note的实例》,我们可以看到robotium的实现比起直接调用Instrumetnation简介了很多。这就好比你用文本编辑器直接编写调用WIN32 API和通过Visual Studio调用高层封装的MFC实现相同功能的应用的差别。
该测试代码是建立在测试目标项目NotePad之下的
切记项目AndroidManifest.xml有两个地方需要添加才能正常运行
相对之前的例子,我们这里没有提供最后的查找到新创建的Note然后删除的功能,因为我暂时还没有查到在Instrumentation如何获得一个没有ID的ListView下面的一个TextView,当前我知道的Instrumeentation下只有findViewById的功能。
实例如下:
package come.excample.android.notepad.test; import com.example.android.notepad.NoteEditor; import com.example.android.notepad.NotesList; import com.example.android.notepad.R; import android.app.Activity; import android.app.Instrumentation; import android.app.Instrumentation.ActivityMonitor; import android.content.Intent; import android.os.SystemClock; import android.test.InstrumentationTestCase; import android.view.KeyEvent; import android.widget.TextView; public class InstrumentationTest extends InstrumentationTestCase { NotesList mActivity = null; //private static Instrumentation instrumentation = new Instrumentation(); @Override protected void setUp() throws Exception { super.setUp(); //Start the NotesList activity by instrument Intent intent = new Intent(); intent.setClassName("com.example.android.notepad", NotesList.class.getName()); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); mActivity = (NotesList) getInstrumentation().startActivitySync(intent); } @Override protected void tearDown() { mActivity.finish(); try { super.tearDown(); } catch (Exception e) { e.printStackTrace(); } } public void testActivity() throws Exception { //Add activity monitor to check whether the NoteEditor activity's ready ActivityMonitor am = getInstrumentation().addMonitor(NoteEditor.class.getName(), null, false); //Evoke the system menu and press on the menu entry "Add note"; getInstrumentation().sendKeyDownUpSync(KeyEvent.KEYCODE_MENU); getInstrumentation().invokeMenuActionSync(mActivity, R.id.menu_add, 0); //Direct to the NoteEditor activity Activity noteEditorActivity = getInstrumentation().waitForMonitorWithTimeout(am, 60000); assertEquals(NoteEditor.class,noteEditorActivity.getClass()); SystemClock.sleep(3000); //assertEquals(true, getInstrumentation().checkMonitorHit(am, 1)); //Input the text of the new created note TextView noteEditor = (TextView) noteEditorActivity.findViewById(R.id.note); getInstrumentation().runOnMainSync(new PerformSetText(noteEditor,"Note1")); //Save the new created note getInstrumentation().sendKeyDownUpSync(KeyEvent.KEYCODE_MENU); getInstrumentation().invokeMenuActionSync(noteEditorActivity, R.id.menu_save, 0); } private class PerformSetText implements Runnable { TextView tv; String txt; public PerformSetText(TextView t,String text) { tv = t; txt = text; } public void run() { tv.setText(txt); } } //TBD, currently don't know the way to perform deleting of a menu entry by context menu options, have to roll with it /* private class PerformLongClick implements Runnable { TextView textView; public PerformLongClick(TextView object) { textView = (TextView) object; } public void run() { textView.performLongClick(); } } */ }
SDK Instrumentation创建一个Note的实例
原文地址:http://blog.csdn.net/zhubaitian/article/details/39546371