您好,登錄后才能下訂單哦!
除了高層框架如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(); } } */ }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。