SDK Instrumentation创建一个Note的实例

除了高层框架如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();
	        }
	    }
	 */
	 
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值