除了高層框架如Robotium的solo,我們也可以直接調(diào)用SDK底層的提供的Instrumentation的API來實(shí)現(xiàn)如前幾篇文章描述的創(chuàng)建一個(gè)note的功能??偹苤甊obotium就是基于Instrumentation的框架高層抽象實(shí)現(xiàn)的一個(gè)項(xiàng)目,所以對比《Robotium創(chuàng)建一個(gè)Note的實(shí)例》,我們可以看到robotium的實(shí)現(xiàn)比起直接調(diào)用Instrumetnation簡介了很多。這就好比你用文本編輯器直接編寫調(diào)用WIN32 API和通過Visual Studio調(diào)用高層封裝的MFC實(shí)現(xiàn)相同功能的應(yīng)用的差別。
公司主營業(yè)務(wù):成都網(wǎng)站設(shè)計(jì)、做網(wǎng)站、移動(dòng)網(wǎng)站開發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實(shí)現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競爭能力。創(chuàng)新互聯(lián)是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊(duì)。公司秉承以“開放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對我們的高要求,感謝他們從不同領(lǐng)域給我們帶來的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會(huì)用頭腦與智慧不斷的給客戶帶來驚喜。創(chuàng)新互聯(lián)推出江達(dá)免費(fèi)做網(wǎng)站回饋大家。
該測試代碼是建立在測試目標(biāo)項(xiàng)目NotePad之下的
相對之前的例子,我們這里沒有提供最后的查找到新創(chuàng)建的Note然后刪除的功能,因?yàn)槲視簳r(shí)還沒有查到在Instrumentation如何獲得一個(gè)沒有ID的ListView下面的一個(gè)TextView,當(dāng)前我知道的Instrumeentation下只有findViewById的功能。
實(shí)例如下:
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(); } } */ }
網(wǎng)頁標(biāo)題:SDKInstrumentation創(chuàng)建一個(gè)Note的實(shí)例
文章出自:http://www.ekvhdxd.cn/article14/ghdgge.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、外貿(mào)建站、網(wǎng)站營銷、網(wǎng)站排名、網(wǎng)站內(nèi)鏈、建站公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)