Hi there I am new to Android Junit testing:
I have written some test code in MainActivityFunctionalTest.java file
MainActivityFunctionalTest.java:
package com.example.myfirstapp2.test;
public class MainActivityFunctionalTest extends ActivityInstrumentationTestCase2<Login>{
private static final String TAG = "MainActivityFunctionalTest";
private Login activity;
public MainActivityFunctionalTest() {
super(Login.class);
}
@Override
protected void setUp() throws Exception {
Log.d(TAG,"Set-Up");
super.setUp();
setActivityInitialTouchMode(false);
activity = getActivity();
}
public void testStartSecondActivity() throws Exception {
// add monitor to check for the second activity
ActivityMonitor monitor =
getInstrumentation().
addMonitor(DisplayMessageActivity.class.getName(), null, false);
//addMonitor(MainActivity.class.getName(), null, false);
// find button and click it
Button view = (Button) activity.findViewById(R.id.btnLogin);
// TouchUtils handles the sync with the main thread internally
TouchUtils.clickView(this, view);
// to click on a click, e.g., in a listview
// listView.getChildAt(0);
// wait 2 seconds for the start of the activity
DisplayMessageActivity startedActivity = (DisplayMessageActivity)
monitor
.waitForActivityWithTimeout(5000);
assertNotNull(startedActivity);
// search for the textView
TextView textView = (TextView) startedActivity.findViewById(R.id.Email);
// check that the TextView is on the screen
ViewAsserts.assertOnScreen(startedActivity.getWindow().getDecorView(),
textView);
// validate the text on the TextView
assertEquals("Text incorrect", "1http://www.vogella.com",
textView.getText().toString());
// press back and click again
this.sendKeys(KeyEvent.KEYCODE_BACK);
TouchUtils.clickView(this, view);
}
}
However,I get an error:
java.lang.SecurityException: Injecting to another application requires INJECT_EVENTS permission
at com.example.myfirstapp2.test.MainActivityFunctionalTest.testStartSecondActivity(MainActivityFunctionalTest.java:70)
TouchUtils.clickView(this, view);
Please help
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…